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
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
34 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
37 case isl_dim_param
: return dim
->nparam
;
38 case isl_dim_in
: return dim
->n_in
;
39 case isl_dim_out
: return dim
->n_out
;
40 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
45 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
48 case isl_dim_param
: return 1;
49 case isl_dim_in
: return 1 + dim
->nparam
;
50 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
56 enum isl_dim_type type
)
61 case isl_dim_cst
: return 1;
64 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
65 case isl_dim_div
: return bmap
->n_div
;
66 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
71 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
73 return map
? n(map
->dim
, type
) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
78 return set
? n(set
->dim
, type
) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
82 enum isl_dim_type type
)
84 isl_space
*dim
= bmap
->dim
;
86 case isl_dim_cst
: return 0;
87 case isl_dim_param
: return 1;
88 case isl_dim_in
: return 1 + dim
->nparam
;
89 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
90 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
95 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
96 enum isl_dim_type type
)
98 return isl_basic_map_offset(bset
, type
);
101 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
103 return pos(map
->dim
, type
);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
107 enum isl_dim_type type
)
109 return isl_basic_map_dim(bset
, type
);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
114 return isl_basic_set_dim(bset
, isl_dim_set
);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
119 return isl_basic_set_dim(bset
, isl_dim_param
);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
124 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
127 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
129 return isl_set_dim(set
, isl_dim_set
);
132 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
134 return isl_set_dim(set
, isl_dim_param
);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
139 return bmap
? bmap
->dim
->n_in
: 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
144 return bmap
? bmap
->dim
->n_out
: 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
149 return bmap
? bmap
->dim
->nparam
: 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
154 return bmap
? bmap
->n_div
: 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
159 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
162 unsigned isl_map_n_in(const struct isl_map
*map
)
164 return map
? map
->dim
->n_in
: 0;
167 unsigned isl_map_n_out(const struct isl_map
*map
)
169 return map
? map
->dim
->n_out
: 0;
172 unsigned isl_map_n_param(const struct isl_map
*map
)
174 return map
? map
->dim
->nparam
: 0;
177 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
182 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
185 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
188 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
189 struct isl_basic_set
*bset
)
194 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
197 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
200 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
205 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
208 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
211 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
212 struct isl_basic_set
*bset
)
217 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
220 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
223 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
225 return bmap
? bmap
->ctx
: NULL
;
228 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
230 return bset
? bset
->ctx
: NULL
;
233 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
235 return map
? map
->ctx
: NULL
;
238 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
240 return set
? set
->ctx
: NULL
;
243 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
247 return isl_space_copy(bmap
->dim
);
250 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
254 return isl_space_copy(bset
->dim
);
257 __isl_give isl_local_space
*isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map
*bmap
)
267 total
= isl_basic_map_total_dim(bmap
);
268 ls
= isl_local_space_alloc(isl_space_copy(bmap
->dim
), bmap
->n_div
);
272 for (i
= 0; i
< bmap
->n_div
; ++i
)
273 isl_seq_cpy(ls
->div
->row
[i
], bmap
->div
[i
], 2 + total
);
278 __isl_give isl_local_space
*isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set
*bset
)
281 return isl_basic_map_get_local_space(bset
);
284 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
285 __isl_take isl_local_space
*ls
)
294 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
295 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
296 n_div
, 0, 2 * n_div
);
298 for (i
= 0; i
< n_div
; ++i
)
299 if (isl_basic_map_alloc_div(bmap
) < 0)
302 for (i
= 0; i
< n_div
; ++i
) {
303 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
304 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
308 isl_local_space_free(ls
);
311 isl_local_space_free(ls
);
312 isl_basic_map_free(bmap
);
316 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
317 __isl_take isl_local_space
*ls
)
319 return isl_basic_map_from_local_space(ls
);
322 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
326 return isl_space_copy(map
->dim
);
329 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
333 return isl_space_copy(set
->dim
);
336 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
339 bmap
= isl_basic_map_cow(bmap
);
342 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
345 bmap
= isl_basic_map_finalize(bmap
);
348 isl_basic_map_free(bmap
);
352 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set
*bset
, const char *s
)
355 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
359 enum isl_dim_type type
)
361 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
364 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
365 enum isl_dim_type type
, const char *s
)
369 map
= isl_map_cow(map
);
373 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
377 for (i
= 0; i
< map
->n
; ++i
) {
378 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
389 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
390 enum isl_dim_type type
)
392 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
395 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
398 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
401 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
402 enum isl_dim_type type
, __isl_take isl_id
*id
)
404 map
= isl_map_cow(map
);
406 return isl_id_free(id
);
408 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
410 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
413 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
414 __isl_take isl_id
*id
)
416 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
419 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
420 enum isl_dim_type type
)
422 map
= isl_map_cow(map
);
426 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
428 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
431 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
433 return isl_map_reset_tuple_id(set
, isl_dim_set
);
436 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
438 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
441 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
442 enum isl_dim_type type
)
444 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
447 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
449 return isl_map_has_tuple_id(set
, isl_dim_set
);
452 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
454 return isl_map_get_tuple_id(set
, isl_dim_set
);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
459 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
464 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
468 enum isl_dim_type type
, unsigned pos
)
470 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
474 enum isl_dim_type type
, unsigned pos
)
476 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
479 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
480 enum isl_dim_type type
, unsigned pos
)
482 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
485 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
486 enum isl_dim_type type
, unsigned pos
)
488 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
491 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map
*bmap
,
493 enum isl_dim_type type
, unsigned pos
, const char *s
)
497 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
502 isl_basic_map_free(bmap
);
506 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
507 enum isl_dim_type type
, unsigned pos
, const char *s
)
514 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
518 for (i
= 0; i
< map
->n
; ++i
) {
519 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
530 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
531 __isl_take isl_basic_set
*bset
,
532 enum isl_dim_type type
, unsigned pos
, const char *s
)
534 return (isl_basic_set
*)isl_basic_map_set_dim_name(
535 (isl_basic_map
*)bset
, type
, pos
, s
);
538 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
539 enum isl_dim_type type
, unsigned pos
, const char *s
)
541 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
544 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
545 enum isl_dim_type type
, unsigned pos
)
547 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
550 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
551 enum isl_dim_type type
, unsigned pos
)
553 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
556 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
557 enum isl_dim_type type
, unsigned pos
)
559 return isl_map_has_dim_id(set
, type
, pos
);
562 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
563 enum isl_dim_type type
, unsigned pos
)
565 return isl_map_get_dim_id(set
, type
, pos
);
568 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
569 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
571 map
= isl_map_cow(map
);
573 return isl_id_free(id
);
575 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
577 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
580 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
581 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
583 return isl_map_set_dim_id(set
, type
, pos
, id
);
586 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
587 __isl_keep isl_id
*id
)
591 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
594 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
595 __isl_keep isl_id
*id
)
597 return isl_map_find_dim_by_id(set
, type
, id
);
600 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
604 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
607 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
609 return isl_basic_map_is_rational(bset
);
612 /* Is this basic set a parameter domain?
614 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
618 return isl_space_is_params(bset
->dim
);
621 /* Is this set a parameter domain?
623 int isl_set_is_params(__isl_keep isl_set
*set
)
627 return isl_space_is_params(set
->dim
);
630 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
631 struct isl_basic_map
*bmap
, unsigned extra
,
632 unsigned n_eq
, unsigned n_ineq
)
635 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
640 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
641 if (isl_blk_is_error(bmap
->block
))
644 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
649 bmap
->block2
= isl_blk_empty();
652 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
653 if (isl_blk_is_error(bmap
->block2
))
656 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
661 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
662 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
664 for (i
= 0; i
< extra
; ++i
)
665 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
669 bmap
->c_size
= n_eq
+ n_ineq
;
670 bmap
->eq
= bmap
->ineq
+ n_ineq
;
679 isl_basic_map_free(bmap
);
683 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
684 unsigned nparam
, unsigned dim
, unsigned extra
,
685 unsigned n_eq
, unsigned n_ineq
)
687 struct isl_basic_map
*bmap
;
690 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
694 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
695 return (struct isl_basic_set
*)bmap
;
698 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
699 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
701 struct isl_basic_map
*bmap
;
704 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
705 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
706 return (struct isl_basic_set
*)bmap
;
712 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
713 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
715 struct isl_basic_map
*bmap
;
719 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
724 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
730 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
731 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
732 unsigned n_eq
, unsigned n_ineq
)
734 struct isl_basic_map
*bmap
;
737 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
741 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
745 static void dup_constraints(
746 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
749 unsigned total
= isl_basic_map_total_dim(src
);
751 for (i
= 0; i
< src
->n_eq
; ++i
) {
752 int j
= isl_basic_map_alloc_equality(dst
);
753 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
756 for (i
= 0; i
< src
->n_ineq
; ++i
) {
757 int j
= isl_basic_map_alloc_inequality(dst
);
758 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
761 for (i
= 0; i
< src
->n_div
; ++i
) {
762 int j
= isl_basic_map_alloc_div(dst
);
763 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
765 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
768 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
770 struct isl_basic_map
*dup
;
774 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
775 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
778 dup_constraints(dup
, bmap
);
779 dup
->flags
= bmap
->flags
;
780 dup
->sample
= isl_vec_copy(bmap
->sample
);
784 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
786 struct isl_basic_map
*dup
;
788 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
789 return (struct isl_basic_set
*)dup
;
792 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
797 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
801 return isl_basic_set_dup(bset
);
804 struct isl_set
*isl_set_copy(struct isl_set
*set
)
813 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
818 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
822 bmap
= isl_basic_map_dup(bmap
);
824 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
828 struct isl_map
*isl_map_copy(struct isl_map
*map
)
837 void isl_basic_map_free(struct isl_basic_map
*bmap
)
845 isl_ctx_deref(bmap
->ctx
);
847 isl_blk_free(bmap
->ctx
, bmap
->block2
);
849 isl_blk_free(bmap
->ctx
, bmap
->block
);
850 isl_vec_free(bmap
->sample
);
851 isl_space_free(bmap
->dim
);
855 void isl_basic_set_free(struct isl_basic_set
*bset
)
857 isl_basic_map_free((struct isl_basic_map
*)bset
);
860 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
862 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
865 __isl_give isl_map
*isl_map_align_params_map_map_and(
866 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
867 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
868 __isl_take isl_map
*map2
))
872 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
873 return fn(map1
, map2
);
874 if (!isl_space_has_named_params(map1
->dim
) ||
875 !isl_space_has_named_params(map2
->dim
))
876 isl_die(map1
->ctx
, isl_error_invalid
,
877 "unaligned unnamed parameters", goto error
);
878 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
879 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
880 return fn(map1
, map2
);
887 static int align_params_map_map_and_test(__isl_keep isl_map
*map1
,
888 __isl_keep isl_map
*map2
,
889 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
895 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
896 return fn(map1
, map2
);
897 if (!isl_space_has_named_params(map1
->dim
) ||
898 !isl_space_has_named_params(map2
->dim
))
899 isl_die(map1
->ctx
, isl_error_invalid
,
900 "unaligned unnamed parameters", return -1);
901 map1
= isl_map_copy(map1
);
902 map2
= isl_map_copy(map2
);
903 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
904 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
911 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
917 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
918 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
920 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
921 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
922 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
923 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
924 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
925 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
927 int j
= isl_basic_map_alloc_inequality(bmap
);
931 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
932 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
939 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
940 bmap
->extra
- bmap
->n_div
);
944 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
946 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
949 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
953 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
958 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
960 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
963 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
968 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
970 if (pos
!= bmap
->n_eq
- 1) {
972 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
973 bmap
->eq
[bmap
->n_eq
- 1] = t
;
979 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
981 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
984 void isl_basic_map_inequality_to_equality(
985 struct isl_basic_map
*bmap
, unsigned pos
)
990 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
991 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
996 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
997 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
998 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
999 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1002 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1004 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1007 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1009 struct isl_ctx
*ctx
;
1013 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1014 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1015 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1016 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1017 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1018 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1019 1 + isl_basic_map_total_dim(bmap
),
1020 bmap
->extra
- bmap
->n_div
);
1021 return bmap
->n_ineq
++;
1024 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1026 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1029 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1033 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1038 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1040 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1043 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1048 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1050 if (pos
!= bmap
->n_ineq
- 1) {
1051 t
= bmap
->ineq
[pos
];
1052 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1053 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1054 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1060 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1062 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1065 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1070 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1073 k
= isl_basic_map_alloc_equality(bmap
);
1076 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1079 isl_basic_map_free(bmap
);
1083 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1086 return (isl_basic_set
*)
1087 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1090 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1095 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1098 k
= isl_basic_map_alloc_inequality(bmap
);
1101 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1104 isl_basic_map_free(bmap
);
1108 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1111 return (isl_basic_set
*)
1112 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1115 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1119 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1120 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1121 1 + 1 + isl_basic_map_total_dim(bmap
),
1122 bmap
->extra
- bmap
->n_div
);
1123 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1124 return bmap
->n_div
++;
1127 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1129 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1132 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1136 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1141 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1143 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1146 /* Copy constraint from src to dst, putting the vars of src at offset
1147 * dim_off in dst and the divs of src at offset div_off in dst.
1148 * If both sets are actually map, then dim_off applies to the input
1151 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1152 struct isl_basic_map
*src_map
, isl_int
*src
,
1153 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1155 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1156 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1157 unsigned src_in
= isl_basic_map_n_in(src_map
);
1158 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1159 unsigned src_out
= isl_basic_map_n_out(src_map
);
1160 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1161 isl_int_set(dst
[0], src
[0]);
1162 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1163 if (dst_nparam
> src_nparam
)
1164 isl_seq_clr(dst
+1+src_nparam
,
1165 dst_nparam
- src_nparam
);
1166 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1167 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1169 isl_min(dst_in
-in_off
, src_in
));
1170 if (dst_in
-in_off
> src_in
)
1171 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1172 dst_in
- in_off
- src_in
);
1173 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1174 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1175 src
+1+src_nparam
+src_in
,
1176 isl_min(dst_out
-out_off
, src_out
));
1177 if (dst_out
-out_off
> src_out
)
1178 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1179 dst_out
- out_off
- src_out
);
1180 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1181 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1182 src
+1+src_nparam
+src_in
+src_out
,
1183 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1184 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1185 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1186 div_off
+src_map
->n_div
,
1187 dst_map
->n_div
- div_off
- src_map
->n_div
);
1190 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1191 struct isl_basic_map
*src_map
, isl_int
*src
,
1192 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1194 isl_int_set(dst
[0], src
[0]);
1195 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1198 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1199 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1204 if (!bmap1
|| !bmap2
)
1207 div_off
= bmap1
->n_div
;
1209 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1210 int i1
= isl_basic_map_alloc_equality(bmap1
);
1213 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1214 i_pos
, o_pos
, div_off
);
1217 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1218 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1221 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1222 i_pos
, o_pos
, div_off
);
1225 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1226 int i1
= isl_basic_map_alloc_div(bmap1
);
1229 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1230 i_pos
, o_pos
, div_off
);
1233 isl_basic_map_free(bmap2
);
1238 isl_basic_map_free(bmap1
);
1239 isl_basic_map_free(bmap2
);
1243 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1244 struct isl_basic_set
*bset2
, unsigned pos
)
1246 return (struct isl_basic_set
*)
1247 add_constraints((struct isl_basic_map
*)bset1
,
1248 (struct isl_basic_map
*)bset2
, 0, pos
);
1251 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1252 __isl_take isl_space
*dim
, unsigned extra
,
1253 unsigned n_eq
, unsigned n_ineq
)
1255 struct isl_basic_map
*ext
;
1265 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1266 base
->extra
>= base
->n_div
+ extra
;
1268 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1269 room_for_ineq(base
, n_ineq
)) {
1270 isl_space_free(dim
);
1274 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1275 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1276 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1277 extra
+= base
->extra
;
1279 n_ineq
+= base
->n_ineq
;
1281 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1287 ext
->sample
= isl_vec_copy(base
->sample
);
1288 flags
= base
->flags
;
1289 ext
= add_constraints(ext
, base
, 0, 0);
1292 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1298 isl_space_free(dim
);
1299 isl_basic_map_free(base
);
1303 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1304 __isl_take isl_space
*dim
, unsigned extra
,
1305 unsigned n_eq
, unsigned n_ineq
)
1307 return (struct isl_basic_set
*)
1308 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1309 extra
, n_eq
, n_ineq
);
1312 struct isl_basic_map
*isl_basic_map_extend_constraints(
1313 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1317 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1321 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1322 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1323 unsigned n_eq
, unsigned n_ineq
)
1325 struct isl_basic_map
*bmap
;
1330 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1334 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1337 isl_basic_map_free(base
);
1341 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1342 unsigned nparam
, unsigned dim
, unsigned extra
,
1343 unsigned n_eq
, unsigned n_ineq
)
1345 return (struct isl_basic_set
*)
1346 isl_basic_map_extend((struct isl_basic_map
*)base
,
1347 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1350 struct isl_basic_set
*isl_basic_set_extend_constraints(
1351 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1353 return (struct isl_basic_set
*)
1354 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1358 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1360 return (struct isl_basic_set
*)
1361 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1364 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1369 if (bmap
->ref
> 1) {
1371 bmap
= isl_basic_map_dup(bmap
);
1374 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1378 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1386 return isl_set_dup(set
);
1389 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1397 return isl_map_dup(map
);
1400 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1401 unsigned a_len
, unsigned b_len
)
1403 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1404 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1405 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1408 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1409 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1417 isl_assert(bmap
->ctx
,
1418 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1420 if (n1
== 0 || n2
== 0)
1423 bmap
= isl_basic_map_cow(bmap
);
1427 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1428 if (isl_blk_is_error(blk
))
1431 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1433 bmap
->eq
[i
] + pos
, n1
, n2
);
1435 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1437 bmap
->ineq
[i
] + pos
, n1
, n2
);
1439 for (i
= 0; i
< bmap
->n_div
; ++i
)
1441 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1443 isl_blk_free(bmap
->ctx
, blk
);
1445 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1446 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1447 return isl_basic_map_finalize(bmap
);
1449 isl_basic_map_free(bmap
);
1453 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1454 __isl_take isl_basic_set
*bset
, unsigned n
)
1459 nparam
= isl_basic_set_n_param(bset
);
1460 dim
= isl_basic_set_n_dim(bset
);
1461 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1463 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1465 isl_basic_set_free(bset
);
1469 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1475 total
= isl_basic_map_total_dim(bmap
);
1476 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1477 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1479 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1481 i
= isl_basic_map_alloc_equality(bmap
);
1485 isl_int_set_si(bmap
->eq
[i
][0], 1);
1486 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1487 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1488 isl_vec_free(bmap
->sample
);
1489 bmap
->sample
= NULL
;
1490 return isl_basic_map_finalize(bmap
);
1492 isl_basic_map_free(bmap
);
1496 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1498 return (struct isl_basic_set
*)
1499 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1502 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1505 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1506 isl_int
*t
= bmap
->div
[a
];
1507 bmap
->div
[a
] = bmap
->div
[b
];
1510 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1511 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1513 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1514 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1516 for (i
= 0; i
< bmap
->n_div
; ++i
)
1517 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1518 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1521 /* Eliminate the specified n dimensions starting at first from the
1522 * constraints using Fourier-Motzkin. The dimensions themselves
1525 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1526 enum isl_dim_type type
, unsigned first
, unsigned n
)
1535 map
= isl_map_cow(map
);
1538 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1539 first
+= pos(map
->dim
, type
) - 1;
1541 for (i
= 0; i
< map
->n
; ++i
) {
1542 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1552 /* Eliminate the specified n dimensions starting at first from the
1553 * constraints using Fourier-Motzkin. The dimensions themselves
1556 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1557 enum isl_dim_type type
, unsigned first
, unsigned n
)
1559 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1562 /* Eliminate the specified n dimensions starting at first from the
1563 * constraints using Fourier-Motzkin. The dimensions themselves
1566 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1567 unsigned first
, unsigned n
)
1569 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1572 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1573 __isl_take isl_basic_map
*bmap
)
1577 bmap
= isl_basic_map_eliminate_vars(bmap
,
1578 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1582 return isl_basic_map_finalize(bmap
);
1585 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1586 __isl_take isl_basic_set
*bset
)
1588 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1589 (struct isl_basic_map
*)bset
);
1592 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1601 map
= isl_map_cow(map
);
1605 for (i
= 0; i
< map
->n
; ++i
) {
1606 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1616 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1618 return isl_map_remove_divs(set
);
1621 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1622 enum isl_dim_type type
, unsigned first
, unsigned n
)
1626 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1628 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1630 bmap
= isl_basic_map_eliminate_vars(bmap
,
1631 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1634 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1636 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1639 isl_basic_map_free(bmap
);
1643 /* Return true if the definition of the given div (recursively) involves
1644 * any of the given variables.
1646 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1647 unsigned first
, unsigned n
)
1650 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1652 if (isl_int_is_zero(bmap
->div
[div
][0]))
1654 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1657 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1658 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1660 if (div_involves_vars(bmap
, i
, first
, n
))
1667 /* Remove all divs (recursively) involving any of the given dimensions
1668 * in their definitions.
1670 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1671 __isl_take isl_basic_map
*bmap
,
1672 enum isl_dim_type type
, unsigned first
, unsigned n
)
1678 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1680 first
+= isl_basic_map_offset(bmap
, type
);
1682 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1683 if (!div_involves_vars(bmap
, i
, first
, n
))
1685 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1693 isl_basic_map_free(bmap
);
1697 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1698 enum isl_dim_type type
, unsigned first
, unsigned n
)
1707 map
= isl_map_cow(map
);
1711 for (i
= 0; i
< map
->n
; ++i
) {
1712 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1723 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1724 enum isl_dim_type type
, unsigned first
, unsigned n
)
1726 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1730 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1731 enum isl_dim_type type
, unsigned first
, unsigned n
)
1738 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1739 isl_die(bmap
->ctx
, isl_error_invalid
,
1740 "index out of bounds", return -1);
1742 first
+= isl_basic_map_offset(bmap
, type
);
1743 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1744 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1746 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1747 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1753 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1754 enum isl_dim_type type
, unsigned first
, unsigned n
)
1761 if (first
+ n
> isl_map_dim(map
, type
))
1762 isl_die(map
->ctx
, isl_error_invalid
,
1763 "index out of bounds", return -1);
1765 for (i
= 0; i
< map
->n
; ++i
) {
1766 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1768 if (involves
< 0 || !involves
)
1775 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1776 enum isl_dim_type type
, unsigned first
, unsigned n
)
1778 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1781 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1782 enum isl_dim_type type
, unsigned first
, unsigned n
)
1784 return isl_map_involves_dims(set
, type
, first
, n
);
1787 /* Return true if the definition of the given div is unknown or depends
1790 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1793 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1795 if (isl_int_is_zero(bmap
->div
[div
][0]))
1798 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1799 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1801 if (div_is_unknown(bmap
, i
))
1808 /* Remove all divs that are unknown or defined in terms of unknown divs.
1810 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1811 __isl_take isl_basic_map
*bmap
)
1818 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1819 if (!div_is_unknown(bmap
, i
))
1821 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1827 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1836 map
= isl_map_cow(map
);
1840 for (i
= 0; i
< map
->n
; ++i
) {
1841 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1851 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1853 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1856 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1857 __isl_take isl_basic_set
*bset
,
1858 enum isl_dim_type type
, unsigned first
, unsigned n
)
1860 return (isl_basic_set
*)
1861 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1864 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1865 enum isl_dim_type type
, unsigned first
, unsigned n
)
1872 map
= isl_map_cow(map
);
1875 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1877 for (i
= 0; i
< map
->n
; ++i
) {
1878 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1879 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1883 map
= isl_map_drop(map
, type
, first
, n
);
1890 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1891 enum isl_dim_type type
, unsigned first
, unsigned n
)
1893 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1896 /* Project out n inputs starting at first using Fourier-Motzkin */
1897 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1898 unsigned first
, unsigned n
)
1900 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1903 static void dump_term(struct isl_basic_map
*bmap
,
1904 isl_int c
, int pos
, FILE *out
)
1907 unsigned in
= isl_basic_map_n_in(bmap
);
1908 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1909 unsigned nparam
= isl_basic_map_n_param(bmap
);
1911 isl_int_print(out
, c
, 0);
1913 if (!isl_int_is_one(c
))
1914 isl_int_print(out
, c
, 0);
1915 if (pos
< 1 + nparam
) {
1916 name
= isl_space_get_dim_name(bmap
->dim
,
1917 isl_dim_param
, pos
- 1);
1919 fprintf(out
, "%s", name
);
1921 fprintf(out
, "p%d", pos
- 1);
1922 } else if (pos
< 1 + nparam
+ in
)
1923 fprintf(out
, "i%d", pos
- 1 - nparam
);
1924 else if (pos
< 1 + nparam
+ dim
)
1925 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1927 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1931 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1932 int sign
, FILE *out
)
1936 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1940 for (i
= 0, first
= 1; i
< len
; ++i
) {
1941 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1944 fprintf(out
, " + ");
1946 isl_int_abs(v
, c
[i
]);
1947 dump_term(bmap
, v
, i
, out
);
1954 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1955 const char *op
, FILE *out
, int indent
)
1959 fprintf(out
, "%*s", indent
, "");
1961 dump_constraint_sign(bmap
, c
, 1, out
);
1962 fprintf(out
, " %s ", op
);
1963 dump_constraint_sign(bmap
, c
, -1, out
);
1967 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1968 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
1970 fprintf(out
, "%*s", indent
, "");
1971 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1976 static void dump_constraints(struct isl_basic_map
*bmap
,
1977 isl_int
**c
, unsigned n
,
1978 const char *op
, FILE *out
, int indent
)
1982 for (i
= 0; i
< n
; ++i
)
1983 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1986 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1990 unsigned total
= isl_basic_map_total_dim(bmap
);
1992 for (j
= 0; j
< 1 + total
; ++j
) {
1993 if (isl_int_is_zero(exp
[j
]))
1995 if (!first
&& isl_int_is_pos(exp
[j
]))
1997 dump_term(bmap
, exp
[j
], j
, out
);
2002 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2006 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2007 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2009 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2010 fprintf(out
, "%*s", indent
, "");
2011 fprintf(out
, "e%d = [(", i
);
2012 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2014 isl_int_print(out
, bmap
->div
[i
][0], 0);
2015 fprintf(out
, "]\n");
2019 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2020 FILE *out
, int indent
)
2023 fprintf(out
, "null basic set\n");
2027 fprintf(out
, "%*s", indent
, "");
2028 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2029 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2030 bset
->extra
, bset
->flags
);
2031 dump((struct isl_basic_map
*)bset
, out
, indent
);
2034 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2035 FILE *out
, int indent
)
2038 fprintf(out
, "null basic map\n");
2042 fprintf(out
, "%*s", indent
, "");
2043 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2044 "flags: %x, n_name: %d\n",
2046 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2047 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2048 dump(bmap
, out
, indent
);
2051 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2056 total
= isl_basic_map_total_dim(bmap
);
2057 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2058 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2059 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2060 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2064 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2067 struct isl_set
*set
;
2071 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2072 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2073 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2074 sizeof(struct isl_set
) +
2075 (n
- 1) * sizeof(struct isl_basic_set
*));
2079 set
->ctx
= dim
->ctx
;
2080 isl_ctx_ref(set
->ctx
);
2088 isl_space_free(dim
);
2092 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2093 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2095 struct isl_set
*set
;
2098 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2102 set
= isl_set_alloc_space(dims
, n
, flags
);
2106 /* Make sure "map" has room for at least "n" more basic maps.
2108 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2111 struct isl_map
*grown
= NULL
;
2115 isl_assert(map
->ctx
, n
>= 0, goto error
);
2116 if (map
->n
+ n
<= map
->size
)
2118 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2121 for (i
= 0; i
< map
->n
; ++i
) {
2122 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2130 isl_map_free(grown
);
2135 /* Make sure "set" has room for at least "n" more basic sets.
2137 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2139 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2142 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2145 struct isl_set
*dup
;
2150 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2153 for (i
= 0; i
< set
->n
; ++i
)
2154 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2158 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2160 return isl_map_from_basic_map(bset
);
2163 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2165 struct isl_map
*map
;
2170 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2171 return isl_map_add_basic_map(map
, bmap
);
2174 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2175 __isl_take isl_basic_set
*bset
)
2177 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2178 (struct isl_basic_map
*)bset
);
2181 void isl_set_free(struct isl_set
*set
)
2191 isl_ctx_deref(set
->ctx
);
2192 for (i
= 0; i
< set
->n
; ++i
)
2193 isl_basic_set_free(set
->p
[i
]);
2194 isl_space_free(set
->dim
);
2198 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2203 fprintf(out
, "null set\n");
2207 fprintf(out
, "%*s", indent
, "");
2208 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2209 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2211 for (i
= 0; i
< set
->n
; ++i
) {
2212 fprintf(out
, "%*s", indent
, "");
2213 fprintf(out
, "basic set %d:\n", i
);
2214 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2218 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2223 fprintf(out
, "null map\n");
2227 fprintf(out
, "%*s", indent
, "");
2228 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2229 "flags: %x, n_name: %d\n",
2230 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2231 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2232 for (i
= 0; i
< map
->n
; ++i
) {
2233 fprintf(out
, "%*s", indent
, "");
2234 fprintf(out
, "basic map %d:\n", i
);
2235 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2239 struct isl_basic_map
*isl_basic_map_intersect_domain(
2240 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2242 struct isl_basic_map
*bmap_domain
;
2247 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2248 bset
->dim
, isl_dim_param
), goto error
);
2250 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2251 isl_assert(bset
->ctx
,
2252 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2254 bmap
= isl_basic_map_cow(bmap
);
2257 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2258 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2259 bmap_domain
= isl_basic_map_from_domain(bset
);
2260 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2262 bmap
= isl_basic_map_simplify(bmap
);
2263 return isl_basic_map_finalize(bmap
);
2265 isl_basic_map_free(bmap
);
2266 isl_basic_set_free(bset
);
2270 struct isl_basic_map
*isl_basic_map_intersect_range(
2271 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2273 struct isl_basic_map
*bmap_range
;
2278 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2279 bset
->dim
, isl_dim_param
), goto error
);
2281 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2282 isl_assert(bset
->ctx
,
2283 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2285 if (isl_basic_set_is_universe(bset
)) {
2286 isl_basic_set_free(bset
);
2290 bmap
= isl_basic_map_cow(bmap
);
2293 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2294 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2295 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2296 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2298 bmap
= isl_basic_map_simplify(bmap
);
2299 return isl_basic_map_finalize(bmap
);
2301 isl_basic_map_free(bmap
);
2302 isl_basic_set_free(bset
);
2306 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2312 total
= 1 + isl_basic_map_total_dim(bmap
);
2313 if (total
!= vec
->size
)
2318 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2319 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2320 if (!isl_int_is_zero(s
)) {
2326 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2327 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2328 if (isl_int_is_neg(s
)) {
2339 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2341 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2344 struct isl_basic_map
*isl_basic_map_intersect(
2345 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2347 struct isl_vec
*sample
= NULL
;
2349 if (!bmap1
|| !bmap2
)
2352 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2353 bmap2
->dim
, isl_dim_param
), goto error
);
2354 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2355 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2356 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2357 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2358 return isl_basic_map_intersect(bmap2
, bmap1
);
2360 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2361 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2362 isl_assert(bmap1
->ctx
,
2363 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2365 if (bmap1
->sample
&&
2366 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2367 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2368 sample
= isl_vec_copy(bmap1
->sample
);
2369 else if (bmap2
->sample
&&
2370 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2371 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2372 sample
= isl_vec_copy(bmap2
->sample
);
2374 bmap1
= isl_basic_map_cow(bmap1
);
2377 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2378 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2379 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2382 isl_vec_free(sample
);
2384 isl_vec_free(bmap1
->sample
);
2385 bmap1
->sample
= sample
;
2388 bmap1
= isl_basic_map_simplify(bmap1
);
2389 return isl_basic_map_finalize(bmap1
);
2392 isl_vec_free(sample
);
2393 isl_basic_map_free(bmap1
);
2394 isl_basic_map_free(bmap2
);
2398 struct isl_basic_set
*isl_basic_set_intersect(
2399 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2401 return (struct isl_basic_set
*)
2402 isl_basic_map_intersect(
2403 (struct isl_basic_map
*)bset1
,
2404 (struct isl_basic_map
*)bset2
);
2407 /* Special case of isl_map_intersect, where both map1 and map2
2408 * are convex, without any divs and such that either map1 or map2
2409 * contains a single constraint. This constraint is then simply
2410 * added to the other map.
2412 static __isl_give isl_map
*map_intersect_add_constraint(
2413 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2415 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2416 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2417 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2418 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2420 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2421 return isl_map_intersect(map2
, map1
);
2423 isl_assert(map2
->ctx
,
2424 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2426 map1
= isl_map_cow(map1
);
2429 if (isl_map_plain_is_empty(map1
)) {
2433 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2434 if (map2
->p
[0]->n_eq
== 1)
2435 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2437 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2438 map2
->p
[0]->ineq
[0]);
2440 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2441 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2445 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2446 isl_basic_map_free(map1
->p
[0]);
2459 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2460 __isl_take isl_map
*map2
)
2463 struct isl_map
*result
;
2469 if (isl_map_plain_is_empty(map1
) &&
2470 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2474 if (isl_map_plain_is_empty(map2
) &&
2475 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2480 if (map1
->n
== 1 && map2
->n
== 1 &&
2481 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2482 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2483 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2484 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2485 return map_intersect_add_constraint(map1
, map2
);
2486 if (isl_space_dim(map1
->dim
, isl_dim_all
) ==
2487 isl_space_dim(map1
->dim
, isl_dim_param
) &&
2488 isl_space_dim(map2
->dim
, isl_dim_all
) !=
2489 isl_space_dim(map2
->dim
, isl_dim_param
))
2490 return isl_map_intersect(map2
, map1
);
2492 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2493 isl_space_dim(map2
->dim
, isl_dim_param
))
2494 isl_assert(map1
->ctx
,
2495 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2497 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2498 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2499 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2501 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2502 map1
->n
* map2
->n
, flags
);
2505 for (i
= 0; i
< map1
->n
; ++i
)
2506 for (j
= 0; j
< map2
->n
; ++j
) {
2507 struct isl_basic_map
*part
;
2508 part
= isl_basic_map_intersect(
2509 isl_basic_map_copy(map1
->p
[i
]),
2510 isl_basic_map_copy(map2
->p
[j
]));
2511 if (isl_basic_map_is_empty(part
))
2512 isl_basic_map_free(part
);
2514 result
= isl_map_add_basic_map(result
, part
);
2527 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2528 __isl_take isl_map
*map2
)
2530 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2533 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2535 return (struct isl_set
*)
2536 isl_map_intersect((struct isl_map
*)set1
,
2537 (struct isl_map
*)set2
);
2540 /* The current implementation of isl_map_intersect accepts intersections
2541 * with parameter domains, so we can just call that for now.
2543 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2544 __isl_take isl_set
*params
)
2546 return isl_map_intersect(map
, params
);
2549 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2550 __isl_take isl_map
*map2
)
2552 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2555 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2556 __isl_take isl_set
*params
)
2558 return isl_map_intersect_params(set
, params
);
2561 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2564 struct isl_basic_set
*bset
;
2569 bmap
= isl_basic_map_cow(bmap
);
2572 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2573 in
= isl_basic_map_n_in(bmap
);
2574 bset
= isl_basic_set_from_basic_map(bmap
);
2575 bset
= isl_basic_set_swap_vars(bset
, in
);
2576 return isl_basic_map_from_basic_set(bset
, dim
);
2579 static __isl_give isl_basic_map
*basic_map_space_reset(
2580 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2584 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2587 space
= isl_basic_map_get_space(bmap
);
2588 space
= isl_space_reset(space
, type
);
2589 bmap
= isl_basic_map_reset_space(bmap
, space
);
2593 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2594 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2597 struct isl_basic_map
*res
;
2598 struct isl_dim_map
*dim_map
;
2599 unsigned total
, off
;
2600 enum isl_dim_type t
;
2603 return basic_map_space_reset(bmap
, type
);
2608 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2610 total
= isl_basic_map_total_dim(bmap
) + n
;
2611 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2613 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2615 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2617 unsigned size
= isl_basic_map_dim(bmap
, t
);
2618 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2620 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2621 pos
, size
- pos
, off
+ pos
+ n
);
2623 off
+= isl_space_dim(res_dim
, t
);
2625 isl_dim_map_div(dim_map
, bmap
, off
);
2627 res
= isl_basic_map_alloc_space(res_dim
,
2628 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2629 if (isl_basic_map_is_rational(bmap
))
2630 res
= isl_basic_map_set_rational(res
);
2631 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2632 return isl_basic_map_finalize(res
);
2635 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2636 enum isl_dim_type type
, unsigned n
)
2640 return isl_basic_map_insert(bmap
, type
,
2641 isl_basic_map_dim(bmap
, type
), n
);
2644 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2645 enum isl_dim_type type
, unsigned n
)
2649 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2650 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2652 isl_basic_set_free(bset
);
2656 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2657 enum isl_dim_type type
)
2661 if (!isl_space_is_named_or_nested(map
->dim
, type
))
2664 space
= isl_map_get_space(map
);
2665 space
= isl_space_reset(space
, type
);
2666 map
= isl_map_reset_space(map
, space
);
2670 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2671 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2676 return map_space_reset(map
, type
);
2678 map
= isl_map_cow(map
);
2682 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2686 for (i
= 0; i
< map
->n
; ++i
) {
2687 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2698 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2699 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2701 return isl_map_insert_dims(set
, type
, pos
, n
);
2704 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2705 enum isl_dim_type type
, unsigned n
)
2709 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2712 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2713 enum isl_dim_type type
, unsigned n
)
2717 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2718 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2724 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2725 __isl_take isl_basic_map
*bmap
,
2726 enum isl_dim_type dst_type
, unsigned dst_pos
,
2727 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2729 struct isl_dim_map
*dim_map
;
2730 struct isl_basic_map
*res
;
2731 enum isl_dim_type t
;
2732 unsigned total
, off
;
2739 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2742 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2745 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2747 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2748 pos(bmap
->dim
, src_type
) + src_pos
+
2749 ((src_type
< dst_type
) ? n
: 0)) {
2750 bmap
= isl_basic_map_cow(bmap
);
2754 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2755 src_type
, src_pos
, n
);
2759 bmap
= isl_basic_map_finalize(bmap
);
2764 total
= isl_basic_map_total_dim(bmap
);
2765 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2768 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2769 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2770 if (t
== dst_type
) {
2771 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2774 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2777 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2778 dst_pos
, size
- dst_pos
, off
);
2779 off
+= size
- dst_pos
;
2780 } else if (t
== src_type
) {
2781 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2784 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2785 src_pos
+ n
, size
- src_pos
- n
, off
);
2786 off
+= size
- src_pos
- n
;
2788 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2792 isl_dim_map_div(dim_map
, bmap
, off
);
2794 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2795 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2796 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2798 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2799 src_type
, src_pos
, n
);
2803 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2804 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2805 bmap
= isl_basic_map_finalize(bmap
);
2809 isl_basic_map_free(bmap
);
2813 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2814 enum isl_dim_type dst_type
, unsigned dst_pos
,
2815 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2817 return (isl_basic_set
*)isl_basic_map_move_dims(
2818 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2821 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2822 enum isl_dim_type dst_type
, unsigned dst_pos
,
2823 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2827 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2828 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2829 src_type
, src_pos
, n
);
2835 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2836 enum isl_dim_type dst_type
, unsigned dst_pos
,
2837 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2846 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2849 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2852 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2854 map
= isl_map_cow(map
);
2858 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2862 for (i
= 0; i
< map
->n
; ++i
) {
2863 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2865 src_type
, src_pos
, n
);
2876 /* Move the specified dimensions to the last columns right before
2877 * the divs. Don't change the dimension specification of bmap.
2878 * That's the responsibility of the caller.
2880 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2881 enum isl_dim_type type
, unsigned first
, unsigned n
)
2883 struct isl_dim_map
*dim_map
;
2884 struct isl_basic_map
*res
;
2885 enum isl_dim_type t
;
2886 unsigned total
, off
;
2890 if (pos(bmap
->dim
, type
) + first
+ n
==
2891 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2894 total
= isl_basic_map_total_dim(bmap
);
2895 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2898 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2899 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2901 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2904 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2905 first
, n
, total
- bmap
->n_div
- n
);
2906 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2907 first
+ n
, size
- (first
+ n
), off
);
2908 off
+= size
- (first
+ n
);
2910 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2914 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2916 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2917 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2918 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2922 /* Turn the n dimensions of type type, starting at first
2923 * into existentially quantified variables.
2925 __isl_give isl_basic_map
*isl_basic_map_project_out(
2926 __isl_take isl_basic_map
*bmap
,
2927 enum isl_dim_type type
, unsigned first
, unsigned n
)
2940 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2941 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2943 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2946 bmap
= move_last(bmap
, type
, first
, n
);
2947 bmap
= isl_basic_map_cow(bmap
);
2951 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
2952 old
= bmap
->block2
.data
;
2953 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2954 (bmap
->extra
+ n
) * (1 + row_size
));
2955 if (!bmap
->block2
.data
)
2957 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2960 for (i
= 0; i
< n
; ++i
) {
2961 new_div
[i
] = bmap
->block2
.data
+
2962 (bmap
->extra
+ i
) * (1 + row_size
);
2963 isl_seq_clr(new_div
[i
], 1 + row_size
);
2965 for (i
= 0; i
< bmap
->extra
; ++i
)
2966 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2968 bmap
->div
= new_div
;
2972 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2975 bmap
= isl_basic_map_simplify(bmap
);
2976 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2977 return isl_basic_map_finalize(bmap
);
2979 isl_basic_map_free(bmap
);
2983 /* Turn the n dimensions of type type, starting at first
2984 * into existentially quantified variables.
2986 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2987 enum isl_dim_type type
, unsigned first
, unsigned n
)
2989 return (isl_basic_set
*)isl_basic_map_project_out(
2990 (isl_basic_map
*)bset
, type
, first
, n
);
2993 /* Turn the n dimensions of type type, starting at first
2994 * into existentially quantified variables.
2996 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2997 enum isl_dim_type type
, unsigned first
, unsigned n
)
3007 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3009 map
= isl_map_cow(map
);
3013 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3017 for (i
= 0; i
< map
->n
; ++i
) {
3018 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3029 /* Turn the n dimensions of type type, starting at first
3030 * into existentially quantified variables.
3032 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3033 enum isl_dim_type type
, unsigned first
, unsigned n
)
3035 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3038 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3042 for (i
= 0; i
< n
; ++i
) {
3043 j
= isl_basic_map_alloc_div(bmap
);
3046 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3050 isl_basic_map_free(bmap
);
3054 struct isl_basic_map
*isl_basic_map_apply_range(
3055 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3057 isl_space
*dim_result
= NULL
;
3058 struct isl_basic_map
*bmap
;
3059 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3060 struct isl_dim_map
*dim_map1
, *dim_map2
;
3062 if (!bmap1
|| !bmap2
)
3065 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3066 isl_space_copy(bmap2
->dim
));
3068 n_in
= isl_basic_map_n_in(bmap1
);
3069 n_out
= isl_basic_map_n_out(bmap2
);
3070 n
= isl_basic_map_n_out(bmap1
);
3071 nparam
= isl_basic_map_n_param(bmap1
);
3073 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3074 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3075 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3076 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3077 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3078 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3079 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3080 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3081 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3082 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3083 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3085 bmap
= isl_basic_map_alloc_space(dim_result
,
3086 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3087 bmap1
->n_eq
+ bmap2
->n_eq
,
3088 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3089 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3090 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3091 bmap
= add_divs(bmap
, n
);
3092 bmap
= isl_basic_map_simplify(bmap
);
3093 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3094 return isl_basic_map_finalize(bmap
);
3096 isl_basic_map_free(bmap1
);
3097 isl_basic_map_free(bmap2
);
3101 struct isl_basic_set
*isl_basic_set_apply(
3102 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3107 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3110 return (struct isl_basic_set
*)
3111 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3113 isl_basic_set_free(bset
);
3114 isl_basic_map_free(bmap
);
3118 struct isl_basic_map
*isl_basic_map_apply_domain(
3119 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3121 if (!bmap1
|| !bmap2
)
3124 isl_assert(bmap1
->ctx
,
3125 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3126 isl_assert(bmap1
->ctx
,
3127 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3130 bmap1
= isl_basic_map_reverse(bmap1
);
3131 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3132 return isl_basic_map_reverse(bmap1
);
3134 isl_basic_map_free(bmap1
);
3135 isl_basic_map_free(bmap2
);
3139 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3140 * A \cap B -> f(A) + f(B)
3142 struct isl_basic_map
*isl_basic_map_sum(
3143 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3145 unsigned n_in
, n_out
, nparam
, total
, pos
;
3146 struct isl_basic_map
*bmap
= NULL
;
3147 struct isl_dim_map
*dim_map1
, *dim_map2
;
3150 if (!bmap1
|| !bmap2
)
3153 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3156 nparam
= isl_basic_map_n_param(bmap1
);
3157 n_in
= isl_basic_map_n_in(bmap1
);
3158 n_out
= isl_basic_map_n_out(bmap1
);
3160 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3161 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3162 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3163 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3164 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3165 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3166 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3167 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3168 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3169 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3170 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3172 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3173 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3174 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3175 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3176 for (i
= 0; i
< n_out
; ++i
) {
3177 int j
= isl_basic_map_alloc_equality(bmap
);
3180 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3181 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3182 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3183 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3185 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3186 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3187 bmap
= add_divs(bmap
, 2 * n_out
);
3189 bmap
= isl_basic_map_simplify(bmap
);
3190 return isl_basic_map_finalize(bmap
);
3192 isl_basic_map_free(bmap
);
3193 isl_basic_map_free(bmap1
);
3194 isl_basic_map_free(bmap2
);
3198 /* Given two maps A -> f(A) and B -> g(B), construct a map
3199 * A \cap B -> f(A) + f(B)
3201 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3203 struct isl_map
*result
;
3209 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3211 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3212 map1
->n
* map2
->n
, 0);
3215 for (i
= 0; i
< map1
->n
; ++i
)
3216 for (j
= 0; j
< map2
->n
; ++j
) {
3217 struct isl_basic_map
*part
;
3218 part
= isl_basic_map_sum(
3219 isl_basic_map_copy(map1
->p
[i
]),
3220 isl_basic_map_copy(map2
->p
[j
]));
3221 if (isl_basic_map_is_empty(part
))
3222 isl_basic_map_free(part
);
3224 result
= isl_map_add_basic_map(result
, part
);
3237 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3238 __isl_take isl_set
*set2
)
3240 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3243 /* Given a basic map A -> f(A), construct A -> -f(A).
3245 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3250 bmap
= isl_basic_map_cow(bmap
);
3254 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3255 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3256 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3257 for (j
= 0; j
< n
; ++j
)
3258 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3259 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3260 for (j
= 0; j
< n
; ++j
)
3261 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3262 for (i
= 0; i
< bmap
->n_div
; ++i
)
3263 for (j
= 0; j
< n
; ++j
)
3264 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3265 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3266 return isl_basic_map_finalize(bmap
);
3269 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3271 return isl_basic_map_neg(bset
);
3274 /* Given a map A -> f(A), construct A -> -f(A).
3276 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3280 map
= isl_map_cow(map
);
3284 for (i
= 0; i
< map
->n
; ++i
) {
3285 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3296 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3298 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3301 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3302 * A -> floor(f(A)/d).
3304 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3307 unsigned n_in
, n_out
, nparam
, total
, pos
;
3308 struct isl_basic_map
*result
= NULL
;
3309 struct isl_dim_map
*dim_map
;
3315 nparam
= isl_basic_map_n_param(bmap
);
3316 n_in
= isl_basic_map_n_in(bmap
);
3317 n_out
= isl_basic_map_n_out(bmap
);
3319 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3320 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3321 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3322 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3323 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3324 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3326 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3327 bmap
->n_div
+ n_out
,
3328 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3329 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3330 result
= add_divs(result
, n_out
);
3331 for (i
= 0; i
< n_out
; ++i
) {
3333 j
= isl_basic_map_alloc_inequality(result
);
3336 isl_seq_clr(result
->ineq
[j
], 1+total
);
3337 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3338 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3339 j
= isl_basic_map_alloc_inequality(result
);
3342 isl_seq_clr(result
->ineq
[j
], 1+total
);
3343 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3344 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3345 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3348 result
= isl_basic_map_simplify(result
);
3349 return isl_basic_map_finalize(result
);
3351 isl_basic_map_free(result
);
3355 /* Given a map A -> f(A) and an integer d, construct a map
3356 * A -> floor(f(A)/d).
3358 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3362 map
= isl_map_cow(map
);
3366 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3367 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3368 for (i
= 0; i
< map
->n
; ++i
) {
3369 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3380 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3386 i
= isl_basic_map_alloc_equality(bmap
);
3389 nparam
= isl_basic_map_n_param(bmap
);
3390 n_in
= isl_basic_map_n_in(bmap
);
3391 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3392 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3393 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3394 return isl_basic_map_finalize(bmap
);
3396 isl_basic_map_free(bmap
);
3400 /* Add a constraints to "bmap" expressing i_pos < o_pos
3402 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3408 i
= isl_basic_map_alloc_inequality(bmap
);
3411 nparam
= isl_basic_map_n_param(bmap
);
3412 n_in
= isl_basic_map_n_in(bmap
);
3413 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3414 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3415 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3416 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3417 return isl_basic_map_finalize(bmap
);
3419 isl_basic_map_free(bmap
);
3423 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3425 static __isl_give isl_basic_map
*var_less_or_equal(
3426 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3432 i
= isl_basic_map_alloc_inequality(bmap
);
3435 nparam
= isl_basic_map_n_param(bmap
);
3436 n_in
= isl_basic_map_n_in(bmap
);
3437 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3438 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3439 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3440 return isl_basic_map_finalize(bmap
);
3442 isl_basic_map_free(bmap
);
3446 /* Add a constraints to "bmap" expressing i_pos > o_pos
3448 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3454 i
= isl_basic_map_alloc_inequality(bmap
);
3457 nparam
= isl_basic_map_n_param(bmap
);
3458 n_in
= isl_basic_map_n_in(bmap
);
3459 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3460 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3461 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3462 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3463 return isl_basic_map_finalize(bmap
);
3465 isl_basic_map_free(bmap
);
3469 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3471 static __isl_give isl_basic_map
*var_more_or_equal(
3472 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3478 i
= isl_basic_map_alloc_inequality(bmap
);
3481 nparam
= isl_basic_map_n_param(bmap
);
3482 n_in
= isl_basic_map_n_in(bmap
);
3483 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3484 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3485 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3486 return isl_basic_map_finalize(bmap
);
3488 isl_basic_map_free(bmap
);
3492 __isl_give isl_basic_map
*isl_basic_map_equal(
3493 __isl_take isl_space
*dim
, unsigned n_equal
)
3496 struct isl_basic_map
*bmap
;
3497 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3500 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3501 bmap
= var_equal(bmap
, i
);
3502 return isl_basic_map_finalize(bmap
);
3505 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3507 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3511 struct isl_basic_map
*bmap
;
3512 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3515 for (i
= 0; i
< pos
&& bmap
; ++i
)
3516 bmap
= var_equal(bmap
, i
);
3518 bmap
= var_less(bmap
, pos
);
3519 return isl_basic_map_finalize(bmap
);
3522 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3524 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3525 __isl_take isl_space
*dim
, unsigned pos
)
3528 isl_basic_map
*bmap
;
3530 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3531 for (i
= 0; i
< pos
; ++i
)
3532 bmap
= var_equal(bmap
, i
);
3533 bmap
= var_less_or_equal(bmap
, pos
);
3534 return isl_basic_map_finalize(bmap
);
3537 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3539 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3543 struct isl_basic_map
*bmap
;
3544 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3547 for (i
= 0; i
< pos
&& bmap
; ++i
)
3548 bmap
= var_equal(bmap
, i
);
3550 bmap
= var_more(bmap
, pos
);
3551 return isl_basic_map_finalize(bmap
);
3554 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3556 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3557 __isl_take isl_space
*dim
, unsigned pos
)
3560 isl_basic_map
*bmap
;
3562 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3563 for (i
= 0; i
< pos
; ++i
)
3564 bmap
= var_equal(bmap
, i
);
3565 bmap
= var_more_or_equal(bmap
, pos
);
3566 return isl_basic_map_finalize(bmap
);
3569 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3570 unsigned n
, int equal
)
3572 struct isl_map
*map
;
3575 if (n
== 0 && equal
)
3576 return isl_map_universe(dims
);
3578 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3580 for (i
= 0; i
+ 1 < n
; ++i
)
3581 map
= isl_map_add_basic_map(map
,
3582 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3585 map
= isl_map_add_basic_map(map
,
3586 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3588 map
= isl_map_add_basic_map(map
,
3589 isl_basic_map_less_at(dims
, n
- 1));
3591 isl_space_free(dims
);
3596 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3600 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3603 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3605 return map_lex_lte_first(dim
, n
, 0);
3608 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3610 return map_lex_lte_first(dim
, n
, 1);
3613 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3615 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3618 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3620 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3623 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3624 unsigned n
, int equal
)
3626 struct isl_map
*map
;
3629 if (n
== 0 && equal
)
3630 return isl_map_universe(dims
);
3632 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3634 for (i
= 0; i
+ 1 < n
; ++i
)
3635 map
= isl_map_add_basic_map(map
,
3636 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3639 map
= isl_map_add_basic_map(map
,
3640 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3642 map
= isl_map_add_basic_map(map
,
3643 isl_basic_map_more_at(dims
, n
- 1));
3645 isl_space_free(dims
);
3650 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3654 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3657 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3659 return map_lex_gte_first(dim
, n
, 0);
3662 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3664 return map_lex_gte_first(dim
, n
, 1);
3667 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3669 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3672 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3674 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3677 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3678 __isl_take isl_set
*set2
)
3681 map
= isl_map_lex_le(isl_set_get_space(set1
));
3682 map
= isl_map_intersect_domain(map
, set1
);
3683 map
= isl_map_intersect_range(map
, set2
);
3687 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3688 __isl_take isl_set
*set2
)
3691 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3692 map
= isl_map_intersect_domain(map
, set1
);
3693 map
= isl_map_intersect_range(map
, set2
);
3697 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3698 __isl_take isl_set
*set2
)
3701 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3702 map
= isl_map_intersect_domain(map
, set1
);
3703 map
= isl_map_intersect_range(map
, set2
);
3707 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3708 __isl_take isl_set
*set2
)
3711 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3712 map
= isl_map_intersect_domain(map
, set1
);
3713 map
= isl_map_intersect_range(map
, set2
);
3717 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3718 __isl_take isl_map
*map2
)
3721 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3722 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3723 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3727 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3728 __isl_take isl_map
*map2
)
3731 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3732 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3733 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3737 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3738 __isl_take isl_map
*map2
)
3741 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3742 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3743 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3747 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3748 __isl_take isl_map
*map2
)
3751 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3752 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3753 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3757 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3758 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3760 struct isl_basic_map
*bmap
;
3762 bset
= isl_basic_set_cow(bset
);
3766 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3767 isl_space_free(bset
->dim
);
3768 bmap
= (struct isl_basic_map
*) bset
;
3770 return isl_basic_map_finalize(bmap
);
3772 isl_basic_set_free(bset
);
3773 isl_space_free(dim
);
3777 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3781 if (bmap
->dim
->n_in
== 0)
3782 return (struct isl_basic_set
*)bmap
;
3783 bmap
= isl_basic_map_cow(bmap
);
3786 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3789 bmap
= isl_basic_map_finalize(bmap
);
3790 return (struct isl_basic_set
*)bmap
;
3792 isl_basic_map_free(bmap
);
3796 /* For a div d = floor(f/m), add the constraints
3799 * -(f-(n-1)) + m d >= 0
3801 * Note that the second constraint is the negation of
3805 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3806 unsigned pos
, isl_int
*div
)
3809 unsigned total
= isl_basic_map_total_dim(bmap
);
3811 i
= isl_basic_map_alloc_inequality(bmap
);
3814 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3815 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3817 j
= isl_basic_map_alloc_inequality(bmap
);
3820 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3821 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3822 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3826 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3827 unsigned pos
, isl_int
*div
)
3829 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3833 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3835 unsigned total
= isl_basic_map_total_dim(bmap
);
3836 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3838 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3842 struct isl_basic_set
*isl_basic_map_underlying_set(
3843 struct isl_basic_map
*bmap
)
3847 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3849 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3850 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3851 return (struct isl_basic_set
*)bmap
;
3852 bmap
= isl_basic_map_cow(bmap
);
3855 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3858 bmap
->extra
-= bmap
->n_div
;
3860 bmap
= isl_basic_map_finalize(bmap
);
3861 return (struct isl_basic_set
*)bmap
;
3866 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3867 __isl_take isl_basic_set
*bset
)
3869 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3872 struct isl_basic_map
*isl_basic_map_overlying_set(
3873 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3875 struct isl_basic_map
*bmap
;
3876 struct isl_ctx
*ctx
;
3883 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3884 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3885 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3887 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3888 isl_basic_map_free(like
);
3889 return (struct isl_basic_map
*)bset
;
3891 bset
= isl_basic_set_cow(bset
);
3894 total
= bset
->dim
->n_out
+ bset
->extra
;
3895 bmap
= (struct isl_basic_map
*)bset
;
3896 isl_space_free(bmap
->dim
);
3897 bmap
->dim
= isl_space_copy(like
->dim
);
3900 bmap
->n_div
= like
->n_div
;
3901 bmap
->extra
+= like
->n_div
;
3905 ltotal
= total
- bmap
->extra
+ like
->extra
;
3908 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3909 bmap
->extra
* (1 + 1 + total
));
3910 if (isl_blk_is_error(bmap
->block2
))
3912 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3916 for (i
= 0; i
< bmap
->extra
; ++i
)
3917 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3918 for (i
= 0; i
< like
->n_div
; ++i
) {
3919 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3920 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3922 bmap
= isl_basic_map_extend_constraints(bmap
,
3923 0, 2 * like
->n_div
);
3924 for (i
= 0; i
< like
->n_div
; ++i
) {
3925 if (isl_int_is_zero(bmap
->div
[i
][0]))
3927 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3931 isl_basic_map_free(like
);
3932 bmap
= isl_basic_map_simplify(bmap
);
3933 bmap
= isl_basic_map_finalize(bmap
);
3936 isl_basic_map_free(like
);
3937 isl_basic_set_free(bset
);
3941 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3942 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3944 return (struct isl_basic_set
*)
3945 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3948 struct isl_set
*isl_set_from_underlying_set(
3949 struct isl_set
*set
, struct isl_basic_set
*like
)
3955 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3957 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3958 isl_basic_set_free(like
);
3961 set
= isl_set_cow(set
);
3964 for (i
= 0; i
< set
->n
; ++i
) {
3965 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3966 isl_basic_set_copy(like
));
3970 isl_space_free(set
->dim
);
3971 set
->dim
= isl_space_copy(like
->dim
);
3974 isl_basic_set_free(like
);
3977 isl_basic_set_free(like
);
3982 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3986 map
= isl_map_cow(map
);
3989 map
->dim
= isl_space_cow(map
->dim
);
3993 for (i
= 1; i
< map
->n
; ++i
)
3994 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3996 for (i
= 0; i
< map
->n
; ++i
) {
3997 map
->p
[i
] = (struct isl_basic_map
*)
3998 isl_basic_map_underlying_set(map
->p
[i
]);
4003 map
->dim
= isl_space_underlying(map
->dim
, 0);
4005 isl_space_free(map
->dim
);
4006 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4010 return (struct isl_set
*)map
;
4016 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4018 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4021 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4022 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4024 bmap
= isl_basic_map_cow(bmap
);
4028 isl_space_free(bmap
->dim
);
4031 bmap
= isl_basic_map_finalize(bmap
);
4035 isl_basic_map_free(bmap
);
4036 isl_space_free(dim
);
4040 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4041 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4043 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4047 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4048 __isl_take isl_space
*dim
)
4052 map
= isl_map_cow(map
);
4056 for (i
= 0; i
< map
->n
; ++i
) {
4057 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4058 isl_space_copy(dim
));
4062 isl_space_free(map
->dim
);
4068 isl_space_free(dim
);
4072 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4073 __isl_take isl_space
*dim
)
4075 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4078 /* Compute the parameter domain of the given basic set.
4080 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4085 if (isl_basic_set_is_params(bset
))
4088 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4089 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4090 space
= isl_basic_set_get_space(bset
);
4091 space
= isl_space_params(space
);
4092 bset
= isl_basic_set_reset_space(bset
, space
);
4096 /* Compute the parameter domain of the given set.
4098 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4103 if (isl_set_is_params(set
))
4106 n
= isl_set_dim(set
, isl_dim_set
);
4107 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4108 space
= isl_set_get_space(set
);
4109 space
= isl_space_params(space
);
4110 set
= isl_set_reset_space(set
, space
);
4114 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4117 struct isl_basic_set
*domain
;
4123 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4125 n_in
= isl_basic_map_n_in(bmap
);
4126 n_out
= isl_basic_map_n_out(bmap
);
4127 domain
= isl_basic_set_from_basic_map(bmap
);
4128 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4130 domain
= isl_basic_set_reset_space(domain
, dim
);
4135 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4139 return isl_space_may_be_set(bmap
->dim
);
4142 /* Is this basic map actually a set?
4143 * Users should never call this function. Outside of isl,
4144 * the type should indicate whether something is a set or a map.
4146 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4150 return isl_space_is_set(bmap
->dim
);
4153 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4157 if (isl_basic_map_is_set(bmap
))
4159 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4162 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4163 __isl_take isl_basic_map
*bmap
)
4167 isl_basic_map
*domain
;
4168 int nparam
, n_in
, n_out
;
4171 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4172 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4173 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4175 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4176 domain
= isl_basic_map_universe(dim
);
4178 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4179 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4180 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4182 total
= isl_basic_map_total_dim(bmap
);
4184 for (i
= 0; i
< n_in
; ++i
) {
4185 k
= isl_basic_map_alloc_equality(bmap
);
4188 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4189 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4190 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4193 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4194 return isl_basic_map_finalize(bmap
);
4196 isl_basic_map_free(bmap
);
4200 __isl_give isl_basic_map
*isl_basic_map_range_map(
4201 __isl_take isl_basic_map
*bmap
)
4205 isl_basic_map
*range
;
4206 int nparam
, n_in
, n_out
;
4209 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4210 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4211 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4213 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4214 range
= isl_basic_map_universe(dim
);
4216 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4217 bmap
= isl_basic_map_apply_range(bmap
, range
);
4218 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4220 total
= isl_basic_map_total_dim(bmap
);
4222 for (i
= 0; i
< n_out
; ++i
) {
4223 k
= isl_basic_map_alloc_equality(bmap
);
4226 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4227 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4228 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4231 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4232 return isl_basic_map_finalize(bmap
);
4234 isl_basic_map_free(bmap
);
4238 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4242 return isl_space_may_be_set(map
->dim
);
4245 /* Is this map actually a set?
4246 * Users should never call this function. Outside of isl,
4247 * the type should indicate whether something is a set or a map.
4249 int isl_map_is_set(__isl_keep isl_map
*map
)
4253 return isl_space_is_set(map
->dim
);
4256 struct isl_set
*isl_map_range(struct isl_map
*map
)
4259 struct isl_set
*set
;
4263 if (isl_map_is_set(map
))
4264 return (isl_set
*)map
;
4266 map
= isl_map_cow(map
);
4270 set
= (struct isl_set
*) map
;
4271 set
->dim
= isl_space_range(set
->dim
);
4274 for (i
= 0; i
< map
->n
; ++i
) {
4275 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4279 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4280 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4287 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4290 isl_space
*domain_dim
;
4292 map
= isl_map_cow(map
);
4296 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4297 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4298 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4301 for (i
= 0; i
< map
->n
; ++i
) {
4302 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4306 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4307 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4314 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4317 isl_space
*range_dim
;
4319 map
= isl_map_cow(map
);
4323 range_dim
= isl_space_range(isl_map_get_space(map
));
4324 range_dim
= isl_space_from_range(range_dim
);
4325 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4326 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4329 for (i
= 0; i
< map
->n
; ++i
) {
4330 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4334 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4335 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4342 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4343 __isl_take isl_space
*dim
)
4346 struct isl_map
*map
= NULL
;
4348 set
= isl_set_cow(set
);
4351 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4352 map
= (struct isl_map
*)set
;
4353 for (i
= 0; i
< set
->n
; ++i
) {
4354 map
->p
[i
] = isl_basic_map_from_basic_set(
4355 set
->p
[i
], isl_space_copy(dim
));
4359 isl_space_free(map
->dim
);
4363 isl_space_free(dim
);
4368 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4369 __isl_take isl_basic_set
*bset
)
4371 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4374 __isl_give isl_basic_map
*isl_basic_map_from_range(
4375 __isl_take isl_basic_set
*bset
)
4378 space
= isl_basic_set_get_space(bset
);
4379 space
= isl_space_from_range(space
);
4380 bset
= isl_basic_set_reset_space(bset
, space
);
4381 return (isl_basic_map
*)bset
;
4384 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4387 space
= isl_set_get_space(set
);
4388 space
= isl_space_from_range(space
);
4389 set
= isl_set_reset_space(set
, space
);
4390 return (struct isl_map
*)set
;
4393 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4395 return isl_map_reverse(isl_map_from_range(set
));
4398 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4399 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4401 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4404 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4405 __isl_take isl_set
*range
)
4407 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4410 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4413 struct isl_set
*set
= NULL
;
4417 map
= isl_map_cow(map
);
4420 map
->dim
= isl_space_as_set_space(map
->dim
);
4423 set
= (struct isl_set
*)map
;
4424 for (i
= 0; i
< map
->n
; ++i
) {
4425 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4435 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4438 struct isl_map
*map
;
4442 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4443 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4444 sizeof(struct isl_map
) +
4445 (n
- 1) * sizeof(struct isl_basic_map
*));
4449 map
->ctx
= dim
->ctx
;
4450 isl_ctx_ref(map
->ctx
);
4458 isl_space_free(dim
);
4462 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4463 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4466 struct isl_map
*map
;
4469 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4473 map
= isl_map_alloc_space(dims
, n
, flags
);
4477 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4479 struct isl_basic_map
*bmap
;
4480 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4481 bmap
= isl_basic_map_set_to_empty(bmap
);
4485 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4487 struct isl_basic_set
*bset
;
4488 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4489 bset
= isl_basic_set_set_to_empty(bset
);
4493 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4495 struct isl_basic_map
*bmap
;
4498 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4499 bmap
= isl_basic_map_set_to_empty(bmap
);
4503 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4505 struct isl_basic_map
*bmap
;
4508 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4509 bmap
= isl_basic_map_set_to_empty(bmap
);
4513 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4515 struct isl_basic_set
*bset
;
4518 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4519 bset
= isl_basic_set_set_to_empty(bset
);
4523 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4525 struct isl_basic_map
*bmap
;
4526 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4527 bmap
= isl_basic_map_finalize(bmap
);
4531 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4533 struct isl_basic_set
*bset
;
4534 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4535 bset
= isl_basic_set_finalize(bset
);
4539 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4542 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4543 isl_basic_map
*bmap
;
4545 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4546 for (i
= 0; i
< total
; ++i
) {
4547 int k
= isl_basic_map_alloc_inequality(bmap
);
4550 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4551 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4555 isl_basic_map_free(bmap
);
4559 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4561 return isl_basic_map_nat_universe(dim
);
4564 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4566 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4569 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4571 return isl_map_nat_universe(dim
);
4574 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4575 __isl_keep isl_basic_map
*model
)
4579 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4582 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4586 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4589 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4590 __isl_keep isl_set
*model
)
4594 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4597 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4599 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4602 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4606 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4609 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4613 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4616 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4618 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4621 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4625 return isl_set_empty(isl_space_copy(model
->dim
));
4628 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4630 struct isl_map
*map
;
4633 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4634 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4638 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4640 struct isl_set
*set
;
4643 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4644 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4648 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4652 return isl_set_universe(isl_space_copy(model
->dim
));
4655 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4658 struct isl_map
*dup
;
4662 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4663 for (i
= 0; i
< map
->n
; ++i
)
4664 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4668 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4669 __isl_take isl_basic_map
*bmap
)
4673 if (isl_basic_map_plain_is_empty(bmap
)) {
4674 isl_basic_map_free(bmap
);
4677 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4678 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4679 map
->p
[map
->n
] = bmap
;
4681 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4687 isl_basic_map_free(bmap
);
4691 void isl_map_free(struct isl_map
*map
)
4701 isl_ctx_deref(map
->ctx
);
4702 for (i
= 0; i
< map
->n
; ++i
)
4703 isl_basic_map_free(map
->p
[i
]);
4704 isl_space_free(map
->dim
);
4708 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4709 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4713 base
= isl_map_cow(base
);
4717 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4720 for (i
= 0; i
< base
->n
; ++i
) {
4721 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4722 isl_space_copy(base
->dim
), 0, 0, 0);
4732 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4733 unsigned nparam
, unsigned dim
)
4735 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4739 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4740 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4744 bmap
= isl_basic_map_cow(bmap
);
4745 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4746 j
= isl_basic_map_alloc_equality(bmap
);
4749 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4750 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4751 isl_int_set_si(bmap
->eq
[j
][0], value
);
4752 bmap
= isl_basic_map_simplify(bmap
);
4753 return isl_basic_map_finalize(bmap
);
4755 isl_basic_map_free(bmap
);
4759 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4760 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4764 bmap
= isl_basic_map_cow(bmap
);
4765 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4766 j
= isl_basic_map_alloc_equality(bmap
);
4769 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4770 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4771 isl_int_set(bmap
->eq
[j
][0], value
);
4772 bmap
= isl_basic_map_simplify(bmap
);
4773 return isl_basic_map_finalize(bmap
);
4775 isl_basic_map_free(bmap
);
4779 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4780 enum isl_dim_type type
, unsigned pos
, int value
)
4784 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4785 return isl_basic_map_fix_pos_si(bmap
,
4786 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4788 isl_basic_map_free(bmap
);
4792 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4793 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4797 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4798 return isl_basic_map_fix_pos(bmap
,
4799 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4801 isl_basic_map_free(bmap
);
4805 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4806 enum isl_dim_type type
, unsigned pos
, int value
)
4808 return (struct isl_basic_set
*)
4809 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4813 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4814 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4816 return (struct isl_basic_set
*)
4817 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4821 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4822 unsigned input
, int value
)
4824 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4827 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4828 unsigned dim
, int value
)
4830 return (struct isl_basic_set
*)
4831 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4832 isl_dim_set
, dim
, value
);
4835 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4836 enum isl_dim_type type
, unsigned pos
, int value
)
4840 map
= isl_map_cow(map
);
4844 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4845 for (i
= 0; i
< map
->n
; ++i
) {
4846 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4850 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4857 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4858 enum isl_dim_type type
, unsigned pos
, int value
)
4860 return (struct isl_set
*)
4861 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4864 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4865 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4869 map
= isl_map_cow(map
);
4873 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4874 for (i
= 0; i
< map
->n
; ++i
) {
4875 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4879 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4886 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4887 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4889 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4892 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4893 unsigned input
, int value
)
4895 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4898 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4900 return (struct isl_set
*)
4901 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4904 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4905 __isl_take isl_basic_map
*bmap
,
4906 enum isl_dim_type type
, unsigned pos
, int value
)
4912 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4913 pos
+= isl_basic_map_offset(bmap
, type
);
4914 bmap
= isl_basic_map_cow(bmap
);
4915 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4916 j
= isl_basic_map_alloc_inequality(bmap
);
4919 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4920 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4921 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4922 bmap
= isl_basic_map_simplify(bmap
);
4923 return isl_basic_map_finalize(bmap
);
4925 isl_basic_map_free(bmap
);
4929 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4930 unsigned dim
, isl_int value
)
4934 bset
= isl_basic_set_cow(bset
);
4935 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4936 j
= isl_basic_set_alloc_inequality(bset
);
4939 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4940 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4941 isl_int_neg(bset
->ineq
[j
][0], value
);
4942 bset
= isl_basic_set_simplify(bset
);
4943 return isl_basic_set_finalize(bset
);
4945 isl_basic_set_free(bset
);
4949 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4950 enum isl_dim_type type
, unsigned pos
, int value
)
4954 map
= isl_map_cow(map
);
4958 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4959 for (i
= 0; i
< map
->n
; ++i
) {
4960 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4965 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4972 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4973 enum isl_dim_type type
, unsigned pos
, int value
)
4975 return (struct isl_set
*)
4976 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4979 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4984 set
= isl_set_cow(set
);
4988 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4989 for (i
= 0; i
< set
->n
; ++i
) {
4990 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5000 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5004 map
= isl_map_cow(map
);
5008 map
->dim
= isl_space_reverse(map
->dim
);
5011 for (i
= 0; i
< map
->n
; ++i
) {
5012 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5016 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5023 static struct isl_map
*isl_basic_map_partial_lexopt(
5024 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5025 struct isl_set
**empty
, int max
)
5029 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5030 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5032 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5034 isl_basic_map_free(bmap
);
5035 isl_basic_set_free(dom
);
5041 struct isl_map
*isl_basic_map_partial_lexmax(
5042 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5043 struct isl_set
**empty
)
5045 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5048 struct isl_map
*isl_basic_map_partial_lexmin(
5049 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5050 struct isl_set
**empty
)
5052 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5055 struct isl_set
*isl_basic_set_partial_lexmin(
5056 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5057 struct isl_set
**empty
)
5059 return (struct isl_set
*)
5060 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5064 struct isl_set
*isl_basic_set_partial_lexmax(
5065 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5066 struct isl_set
**empty
)
5068 return (struct isl_set
*)
5069 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5073 /* Given a basic map "bmap", compute the lexicographically minimal
5074 * (or maximal) image element for each domain element in dom.
5075 * Set *empty to those elements in dom that do not have an image element.
5077 * We first make sure the basic sets in dom are disjoint and then
5078 * simply collect the results over each of the basic sets separately.
5079 * We could probably improve the efficiency a bit by moving the union
5080 * domain down into the parametric integer programming.
5082 static __isl_give isl_map
*basic_map_partial_lexopt(
5083 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5084 __isl_give isl_set
**empty
, int max
)
5087 struct isl_map
*res
;
5089 dom
= isl_set_make_disjoint(dom
);
5093 if (isl_set_plain_is_empty(dom
)) {
5094 res
= isl_map_empty_like_basic_map(bmap
);
5095 *empty
= isl_set_empty_like(dom
);
5097 isl_basic_map_free(bmap
);
5101 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5102 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5104 for (i
= 1; i
< dom
->n
; ++i
) {
5105 struct isl_map
*res_i
;
5106 struct isl_set
*empty_i
;
5108 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5109 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5111 res
= isl_map_union_disjoint(res
, res_i
);
5112 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5116 isl_basic_map_free(bmap
);
5121 isl_basic_map_free(bmap
);
5125 /* Given a map "map", compute the lexicographically minimal
5126 * (or maximal) image element for each domain element in dom.
5127 * Set *empty to those elements in dom that do not have an image element.
5129 * We first compute the lexicographically minimal or maximal element
5130 * in the first basic map. This results in a partial solution "res"
5131 * and a subset "todo" of dom that still need to be handled.
5132 * We then consider each of the remaining maps in "map" and successively
5133 * improve both "res" and "todo".
5135 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5136 * Assume we are computing the lexicographical maximum.
5137 * We first compute the lexicographically maximal element in basic map i.
5138 * This results in a partial solution res_i and a subset todo_i.
5139 * Then we combine these results with those obtain for the first k basic maps
5140 * to obtain a result that is valid for the first k+1 basic maps.
5141 * In particular, the set where there is no solution is the set where
5142 * there is no solution for the first k basic maps and also no solution
5143 * for the ith basic map, i.e.,
5145 * todo^i = todo^k * todo_i
5147 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5148 * solutions, arbitrarily breaking ties in favor of res^k.
5149 * That is, when res^k(a) >= res_i(a), we pick res^k and
5150 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5151 * the lexicographic order.)
5152 * In practice, we compute
5154 * res^k * (res_i . "<=")
5158 * res_i * (res^k . "<")
5160 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5161 * where only one of res^k and res_i provides a solution and we simply pick
5168 * Note that we only compute these intersections when dom(res^k) intersects
5169 * dom(res_i). Otherwise, the only effect of these intersections is to
5170 * potentially break up res^k and res_i into smaller pieces.
5171 * We want to avoid such splintering as much as possible.
5172 * In fact, an earlier implementation of this function would look for
5173 * better results in the domain of res^k and for extra results in todo^k,
5174 * but this would always result in a splintering according to todo^k,
5175 * even when the domain of basic map i is disjoint from the domains of
5176 * the previous basic maps.
5178 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5179 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5180 __isl_give isl_set
**empty
, int max
)
5183 struct isl_map
*res
;
5184 struct isl_set
*todo
;
5189 if (isl_map_plain_is_empty(map
)) {
5197 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5198 isl_set_copy(dom
), &todo
, max
);
5200 for (i
= 1; i
< map
->n
; ++i
) {
5204 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5206 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5207 isl_set_copy(dom
), &todo_i
, max
);
5210 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5211 le
= isl_map_lex_le(dim
);
5213 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5214 le
= isl_map_lex_ge(dim
);
5216 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5217 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5218 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5219 le
= isl_map_intersect(le
, isl_map_copy(res
));
5221 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5222 res
= isl_map_intersect_domain(res
,
5223 isl_set_copy(todo_i
));
5224 res_i
= isl_map_intersect_domain(res_i
,
5225 isl_set_copy(todo
));
5228 res
= isl_map_union_disjoint(res
, res_i
);
5229 res
= isl_map_union_disjoint(res
, lt
);
5230 res
= isl_map_union_disjoint(res
, le
);
5232 todo
= isl_set_intersect(todo
, todo_i
);
5252 /* Given a map "map", compute the lexicographically minimal
5253 * (or maximal) image element for each domain element in dom.
5254 * Set *empty to those elements in dom that do not have an image element.
5256 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5258 static __isl_give isl_map
*isl_map_partial_lexopt(
5259 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5260 __isl_give isl_set
**empty
, int max
)
5264 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5265 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5266 if (!isl_space_has_named_params(map
->dim
) ||
5267 !isl_space_has_named_params(dom
->dim
))
5268 isl_die(map
->ctx
, isl_error_invalid
,
5269 "unaligned unnamed parameters", goto error
);
5270 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5271 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5272 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5281 __isl_give isl_map
*isl_map_partial_lexmax(
5282 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5283 __isl_give isl_set
**empty
)
5285 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5288 __isl_give isl_map
*isl_map_partial_lexmin(
5289 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5290 __isl_give isl_set
**empty
)
5292 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5295 __isl_give isl_set
*isl_set_partial_lexmin(
5296 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5297 __isl_give isl_set
**empty
)
5299 return (struct isl_set
*)
5300 isl_map_partial_lexmin((struct isl_map
*)set
,
5304 __isl_give isl_set
*isl_set_partial_lexmax(
5305 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5306 __isl_give isl_set
**empty
)
5308 return (struct isl_set
*)
5309 isl_map_partial_lexmax((struct isl_map
*)set
,
5313 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5315 struct isl_basic_set
*dom
= NULL
;
5320 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5321 dom
= isl_basic_set_universe(dom_dim
);
5322 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5324 isl_basic_map_free(bmap
);
5328 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5330 return isl_basic_map_lexopt(bmap
, 0);
5333 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5335 return isl_basic_map_lexopt(bmap
, 1);
5338 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5340 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5343 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5345 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5348 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5350 struct isl_set
*dom
= NULL
;
5355 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5356 dom
= isl_set_universe(dom_dim
);
5357 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5363 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5365 return isl_map_lexopt(map
, 0);
5368 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5370 return isl_map_lexopt(map
, 1);
5373 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5375 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5378 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5380 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5383 /* Construct a map that equates the two given dimensions in the given space.
5385 static __isl_give isl_map
*equate(__isl_take isl_space
*dim
,
5386 enum isl_dim_type src_type
, int src_pos
,
5387 enum isl_dim_type dst_type
, int dst_pos
)
5389 isl_basic_map
*bmap
;
5392 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5393 k
= isl_basic_map_alloc_equality(bmap
);
5396 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
5397 src_pos
+= isl_basic_map_offset(bmap
, src_type
);
5398 dst_pos
+= isl_basic_map_offset(bmap
, dst_type
);
5399 isl_int_set_si(bmap
->eq
[k
][src_pos
], 1);
5400 isl_int_set_si(bmap
->eq
[k
][dst_pos
], -1);
5402 return isl_map_from_basic_map(bmap
);
5404 isl_basic_map_free(bmap
);
5408 /* Extract the first and only affine expression from list
5409 * and then add it to *pwaff with the given dom.
5410 * This domain is known to be disjoint from other domains
5411 * because of the way isl_basic_set_foreach_lexmax works.
5413 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5414 __isl_take isl_aff_list
*list
, void *user
)
5416 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5418 isl_pw_aff
**pwaff
= user
;
5419 isl_pw_aff
*pwaff_i
;
5421 if (isl_aff_list_n_aff(list
) != 1)
5422 isl_die(ctx
, isl_error_internal
,
5423 "expecting single element list", goto error
);
5425 aff
= isl_aff_list_get_aff(list
, 0);
5426 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5428 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5430 isl_aff_list_free(list
);
5434 isl_basic_set_free(dom
);
5435 isl_aff_list_free(list
);
5439 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5440 * dimension as an isl_pw_aff.
5442 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5443 * call update_dim_opt on each leaf of the result.
5445 static __isl_give isl_pw_aff
*basic_set_dim_opt(__isl_keep isl_basic_set
*bset
,
5448 isl_space
*dim
= isl_basic_set_get_space(bset
);
5452 dim
= isl_space_params(dim
);
5453 pwaff
= isl_pw_aff_empty(dim
);
5455 r
= isl_basic_set_foreach_lexopt(bset
, max
, &update_dim_opt
, &pwaff
);
5457 return isl_pw_aff_free(pwaff
);
5462 /* Compute the minimum or maximum of the given set dimension
5463 * as a function of the parameters, but independently of
5464 * the other set dimensions.
5466 * We first project the set onto the given dimension and then compute
5467 * the "lexicographic" maximum in each basic set, combining the results
5468 * using isl_pw_aff_union_max.
5470 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5477 map
= isl_map_from_domain(set
);
5478 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
5479 map
= isl_map_intersect(map
,
5480 equate(isl_map_get_space(map
), isl_dim_in
, pos
,
5482 set
= isl_map_range(map
);
5487 isl_space
*dim
= isl_set_get_space(set
);
5488 dim
= isl_space_domain(isl_space_from_range(dim
));
5490 return isl_pw_aff_empty(dim
);
5493 pwaff
= basic_set_dim_opt(set
->p
[0], max
);
5494 for (i
= 1; i
< set
->n
; ++i
) {
5495 isl_pw_aff
*pwaff_i
;
5497 pwaff_i
= basic_set_dim_opt(set
->p
[i
], max
);
5498 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5506 /* Compute the maximum of the given set dimension as a function of the
5507 * parameters, but independently of the other set dimensions.
5509 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5511 return set_dim_opt(set
, pos
, 1);
5514 /* Compute the minimum of the given set dimension as a function of the
5515 * parameters, but independently of the other set dimensions.
5517 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5519 return set_dim_opt(set
, pos
, 0);
5522 /* Apply a preimage specified by "mat" on the parameters of "bset".
5523 * bset is assumed to have only parameters and divs.
5525 static struct isl_basic_set
*basic_set_parameter_preimage(
5526 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5533 bset
->dim
= isl_space_cow(bset
->dim
);
5537 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5539 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5541 bset
->dim
->nparam
= 0;
5542 bset
->dim
->n_out
= nparam
;
5543 bset
= isl_basic_set_preimage(bset
, mat
);
5545 bset
->dim
->nparam
= bset
->dim
->n_out
;
5546 bset
->dim
->n_out
= 0;
5551 isl_basic_set_free(bset
);
5555 /* Apply a preimage specified by "mat" on the parameters of "set".
5556 * set is assumed to have only parameters and divs.
5558 static struct isl_set
*set_parameter_preimage(
5559 struct isl_set
*set
, struct isl_mat
*mat
)
5561 isl_space
*dim
= NULL
;
5567 dim
= isl_space_copy(set
->dim
);
5568 dim
= isl_space_cow(dim
);
5572 nparam
= isl_set_dim(set
, isl_dim_param
);
5574 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5577 dim
->n_out
= nparam
;
5578 isl_set_reset_space(set
, dim
);
5579 set
= isl_set_preimage(set
, mat
);
5582 dim
= isl_space_copy(set
->dim
);
5583 dim
= isl_space_cow(dim
);
5586 dim
->nparam
= dim
->n_out
;
5588 isl_set_reset_space(set
, dim
);
5591 isl_space_free(dim
);
5598 /* Intersect the basic set "bset" with the affine space specified by the
5599 * equalities in "eq".
5601 static struct isl_basic_set
*basic_set_append_equalities(
5602 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5610 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5615 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5616 for (i
= 0; i
< eq
->n_row
; ++i
) {
5617 k
= isl_basic_set_alloc_equality(bset
);
5620 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5621 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5625 bset
= isl_basic_set_gauss(bset
, NULL
);
5626 bset
= isl_basic_set_finalize(bset
);
5631 isl_basic_set_free(bset
);
5635 /* Intersect the set "set" with the affine space specified by the
5636 * equalities in "eq".
5638 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5646 for (i
= 0; i
< set
->n
; ++i
) {
5647 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5660 /* Project the given basic set onto its parameter domain, possibly introducing
5661 * new, explicit, existential variables in the constraints.
5662 * The input has parameters and (possibly implicit) existential variables.
5663 * The output has the same parameters, but only
5664 * explicit existentially quantified variables.
5666 * The actual projection is performed by pip, but pip doesn't seem
5667 * to like equalities very much, so we first remove the equalities
5668 * among the parameters by performing a variable compression on
5669 * the parameters. Afterward, an inverse transformation is performed
5670 * and the equalities among the parameters are inserted back in.
5672 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5676 struct isl_mat
*T
, *T2
;
5677 struct isl_set
*set
;
5678 unsigned nparam
, n_div
;
5680 bset
= isl_basic_set_cow(bset
);
5684 if (bset
->n_eq
== 0)
5685 return isl_basic_set_lexmin(bset
);
5687 isl_basic_set_gauss(bset
, NULL
);
5689 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5690 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5692 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5693 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5696 if (i
== bset
->n_eq
)
5697 return isl_basic_set_lexmin(bset
);
5699 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5701 eq
= isl_mat_cow(eq
);
5702 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5703 if (T
&& T
->n_col
== 0) {
5707 bset
= isl_basic_set_set_to_empty(bset
);
5708 return isl_set_from_basic_set(bset
);
5710 bset
= basic_set_parameter_preimage(bset
, T
);
5712 set
= isl_basic_set_lexmin(bset
);
5713 set
= set_parameter_preimage(set
, T2
);
5714 set
= set_append_equalities(set
, eq
);
5718 /* Compute an explicit representation for all the existentially
5719 * quantified variables.
5720 * The input and output dimensions are first turned into parameters.
5721 * compute_divs then returns a map with the same parameters and
5722 * no input or output dimensions and the dimension specification
5723 * is reset to that of the input.
5725 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5727 struct isl_basic_set
*bset
;
5728 struct isl_set
*set
;
5729 struct isl_map
*map
;
5730 isl_space
*dim
, *orig_dim
= NULL
;
5735 bmap
= isl_basic_map_cow(bmap
);
5739 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5740 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5741 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5742 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5746 orig_dim
= bmap
->dim
;
5748 bset
= (struct isl_basic_set
*)bmap
;
5750 set
= parameter_compute_divs(bset
);
5751 map
= (struct isl_map
*)set
;
5752 map
= isl_map_reset_space(map
, orig_dim
);
5756 isl_basic_map_free(bmap
);
5760 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5768 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5769 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5770 if (isl_int_is_zero(bmap
->div
[i
][0]))
5772 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5778 static int map_divs_known(__isl_keep isl_map
*map
)
5785 for (i
= 0; i
< map
->n
; ++i
) {
5786 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5794 /* If bmap contains any unknown divs, then compute explicit
5795 * expressions for them. However, this computation may be
5796 * quite expensive, so first try to remove divs that aren't
5799 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5802 struct isl_map
*map
;
5804 known
= isl_basic_map_divs_known(bmap
);
5808 return isl_map_from_basic_map(bmap
);
5810 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5812 known
= isl_basic_map_divs_known(bmap
);
5816 return isl_map_from_basic_map(bmap
);
5818 map
= compute_divs(bmap
);
5821 isl_basic_map_free(bmap
);
5825 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5829 struct isl_map
*res
;
5836 known
= map_divs_known(map
);
5844 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5845 for (i
= 1 ; i
< map
->n
; ++i
) {
5847 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5848 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5849 res
= isl_map_union_disjoint(res
, r2
);
5851 res
= isl_map_union(res
, r2
);
5858 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5860 return (struct isl_set
*)
5861 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5864 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5866 return (struct isl_set
*)
5867 isl_map_compute_divs((struct isl_map
*)set
);
5870 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5873 struct isl_set
*set
;
5878 map
= isl_map_cow(map
);
5882 set
= (struct isl_set
*)map
;
5883 set
->dim
= isl_space_domain(set
->dim
);
5886 for (i
= 0; i
< map
->n
; ++i
) {
5887 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5891 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5892 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5899 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
5900 __isl_take isl_map
*map2
)
5904 struct isl_map
*map
= NULL
;
5918 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
5920 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5921 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5922 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5924 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5925 map1
->n
+ map2
->n
, flags
);
5928 for (i
= 0; i
< map1
->n
; ++i
) {
5929 map
= isl_map_add_basic_map(map
,
5930 isl_basic_map_copy(map1
->p
[i
]));
5934 for (i
= 0; i
< map2
->n
; ++i
) {
5935 map
= isl_map_add_basic_map(map
,
5936 isl_basic_map_copy(map2
->p
[i
]));
5950 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
5951 __isl_take isl_map
*map2
)
5953 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
5956 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5958 map1
= isl_map_union_disjoint(map1
, map2
);
5962 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5966 struct isl_set
*isl_set_union_disjoint(
5967 struct isl_set
*set1
, struct isl_set
*set2
)
5969 return (struct isl_set
*)
5970 isl_map_union_disjoint(
5971 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5974 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5976 return (struct isl_set
*)
5977 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5980 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
5981 __isl_take isl_set
*set
)
5984 struct isl_map
*result
;
5990 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
5991 isl_die(set
->ctx
, isl_error_invalid
,
5992 "parameters don't match", goto error
);
5994 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
5995 !isl_map_compatible_range(map
, set
))
5996 isl_die(set
->ctx
, isl_error_invalid
,
5997 "incompatible spaces", goto error
);
5999 if (isl_set_plain_is_universe(set
)) {
6004 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6005 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6006 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6008 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6009 map
->n
* set
->n
, flags
);
6012 for (i
= 0; i
< map
->n
; ++i
)
6013 for (j
= 0; j
< set
->n
; ++j
) {
6014 result
= isl_map_add_basic_map(result
,
6015 isl_basic_map_intersect_range(
6016 isl_basic_map_copy(map
->p
[i
]),
6017 isl_basic_set_copy(set
->p
[j
])));
6030 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6031 __isl_take isl_set
*set
)
6033 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6036 struct isl_map
*isl_map_intersect_domain(
6037 struct isl_map
*map
, struct isl_set
*set
)
6039 return isl_map_reverse(
6040 isl_map_intersect_range(isl_map_reverse(map
), set
));
6043 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6044 __isl_take isl_map
*map2
)
6048 map1
= isl_map_reverse(map1
);
6049 map1
= isl_map_apply_range(map1
, map2
);
6050 return isl_map_reverse(map1
);
6057 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6058 __isl_take isl_map
*map2
)
6060 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6063 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6064 __isl_take isl_map
*map2
)
6066 isl_space
*dim_result
;
6067 struct isl_map
*result
;
6073 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6074 isl_space_copy(map2
->dim
));
6076 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6079 for (i
= 0; i
< map1
->n
; ++i
)
6080 for (j
= 0; j
< map2
->n
; ++j
) {
6081 result
= isl_map_add_basic_map(result
,
6082 isl_basic_map_apply_range(
6083 isl_basic_map_copy(map1
->p
[i
]),
6084 isl_basic_map_copy(map2
->p
[j
])));
6090 if (result
&& result
->n
<= 1)
6091 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6099 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6100 __isl_take isl_map
*map2
)
6102 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6106 * returns range - domain
6108 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6110 isl_space
*dims
, *target_dim
;
6111 struct isl_basic_set
*bset
;
6118 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6119 bmap
->dim
, isl_dim_out
),
6121 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6122 dim
= isl_basic_map_n_in(bmap
);
6123 nparam
= isl_basic_map_n_param(bmap
);
6124 bset
= isl_basic_set_from_basic_map(bmap
);
6125 bset
= isl_basic_set_cow(bset
);
6126 dims
= isl_basic_set_get_space(bset
);
6127 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6128 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6129 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6130 for (i
= 0; i
< dim
; ++i
) {
6131 int j
= isl_basic_map_alloc_equality(
6132 (struct isl_basic_map
*)bset
);
6135 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6136 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6137 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6138 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6140 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6141 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6144 isl_basic_map_free(bmap
);
6149 * returns range - domain
6151 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6155 struct isl_set
*result
;
6160 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6161 map
->dim
, isl_dim_out
),
6163 dim
= isl_map_get_space(map
);
6164 dim
= isl_space_domain(dim
);
6165 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6168 for (i
= 0; i
< map
->n
; ++i
)
6169 result
= isl_set_add_basic_set(result
,
6170 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6179 * returns [domain -> range] -> range - domain
6181 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6182 __isl_take isl_basic_map
*bmap
)
6186 isl_basic_map
*domain
;
6190 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6191 isl_die(bmap
->ctx
, isl_error_invalid
,
6192 "domain and range don't match", goto error
);
6194 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6195 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6197 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6198 domain
= isl_basic_map_universe(dim
);
6200 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6201 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6202 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6204 total
= isl_basic_map_total_dim(bmap
);
6206 for (i
= 0; i
< n
; ++i
) {
6207 k
= isl_basic_map_alloc_equality(bmap
);
6210 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6211 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6212 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6213 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6216 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6217 return isl_basic_map_finalize(bmap
);
6219 isl_basic_map_free(bmap
);
6224 * returns [domain -> range] -> range - domain
6226 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6229 isl_space
*domain_dim
;
6234 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6235 isl_die(map
->ctx
, isl_error_invalid
,
6236 "domain and range don't match", goto error
);
6238 map
= isl_map_cow(map
);
6242 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6243 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6244 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6247 for (i
= 0; i
< map
->n
; ++i
) {
6248 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6252 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6259 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6261 struct isl_basic_map
*bmap
;
6269 nparam
= dims
->nparam
;
6271 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6275 for (i
= 0; i
< dim
; ++i
) {
6276 int j
= isl_basic_map_alloc_equality(bmap
);
6279 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6280 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6281 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6283 return isl_basic_map_finalize(bmap
);
6285 isl_basic_map_free(bmap
);
6289 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6293 if (dim
->n_in
!= dim
->n_out
)
6294 isl_die(dim
->ctx
, isl_error_invalid
,
6295 "number of input and output dimensions needs to be "
6296 "the same", goto error
);
6297 return basic_map_identity(dim
);
6299 isl_space_free(dim
);
6303 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6305 if (!model
|| !model
->dim
)
6307 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6310 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6312 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6315 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6317 if (!model
|| !model
->dim
)
6319 return isl_map_identity(isl_space_copy(model
->dim
));
6322 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6324 if (!model
|| !model
->dim
)
6326 return isl_map_identity(isl_space_copy(model
->dim
));
6329 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6331 isl_space
*dim
= isl_set_get_space(set
);
6333 id
= isl_map_identity(isl_space_map_from_set(dim
));
6334 return isl_map_intersect_range(id
, set
);
6337 /* Construct a basic set with all set dimensions having only non-negative
6340 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6341 __isl_take isl_space
*space
)
6346 struct isl_basic_set
*bset
;
6350 nparam
= space
->nparam
;
6352 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6355 for (i
= 0; i
< dim
; ++i
) {
6356 int k
= isl_basic_set_alloc_inequality(bset
);
6359 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6360 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6364 isl_basic_set_free(bset
);
6368 /* Construct the half-space x_pos >= 0.
6370 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6374 isl_basic_set
*nonneg
;
6376 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6377 k
= isl_basic_set_alloc_inequality(nonneg
);
6380 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6381 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6383 return isl_basic_set_finalize(nonneg
);
6385 isl_basic_set_free(nonneg
);
6389 /* Construct the half-space x_pos <= -1.
6391 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6396 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6397 k
= isl_basic_set_alloc_inequality(neg
);
6400 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6401 isl_int_set_si(neg
->ineq
[k
][0], -1);
6402 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6404 return isl_basic_set_finalize(neg
);
6406 isl_basic_set_free(neg
);
6410 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6411 enum isl_dim_type type
, unsigned first
, unsigned n
)
6414 isl_basic_set
*nonneg
;
6422 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6424 for (i
= 0; i
< n
; ++i
) {
6425 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6426 pos(set
->dim
, type
) + first
+ i
);
6427 neg
= neg_halfspace(isl_set_get_space(set
),
6428 pos(set
->dim
, type
) + first
+ i
);
6430 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6439 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6440 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6447 if (isl_set_plain_is_empty(set
)) {
6452 return fn(set
, signs
, user
);
6455 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6457 half
= isl_set_intersect(half
, isl_set_copy(set
));
6458 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6462 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6464 half
= isl_set_intersect(half
, set
);
6465 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6471 /* Call "fn" on the intersections of "set" with each of the orthants
6472 * (except for obviously empty intersections). The orthant is identified
6473 * by the signs array, with each entry having value 1 or -1 according
6474 * to the sign of the corresponding variable.
6476 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6477 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6487 if (isl_set_plain_is_empty(set
))
6490 nparam
= isl_set_dim(set
, isl_dim_param
);
6491 nvar
= isl_set_dim(set
, isl_dim_set
);
6493 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6495 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6503 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6505 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6508 int isl_basic_map_is_subset(
6509 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6512 struct isl_map
*map1
;
6513 struct isl_map
*map2
;
6515 if (!bmap1
|| !bmap2
)
6518 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6519 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6521 is_subset
= isl_map_is_subset(map1
, map2
);
6529 int isl_basic_map_is_equal(
6530 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6534 if (!bmap1
|| !bmap2
)
6536 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6539 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6543 int isl_basic_set_is_equal(
6544 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6546 return isl_basic_map_is_equal(
6547 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6550 int isl_map_is_empty(struct isl_map
*map
)
6557 for (i
= 0; i
< map
->n
; ++i
) {
6558 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6567 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6569 return map
? map
->n
== 0 : -1;
6572 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6574 return isl_map_plain_is_empty(map
);
6577 int isl_set_plain_is_empty(struct isl_set
*set
)
6579 return set
? set
->n
== 0 : -1;
6582 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6584 return isl_set_plain_is_empty(set
);
6587 int isl_set_is_empty(struct isl_set
*set
)
6589 return isl_map_is_empty((struct isl_map
*)set
);
6592 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6597 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6600 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6605 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6608 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6614 is_subset
= isl_map_is_subset(map1
, map2
);
6617 is_subset
= isl_map_is_subset(map2
, map1
);
6621 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6623 return align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6626 int isl_basic_map_is_strict_subset(
6627 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6631 if (!bmap1
|| !bmap2
)
6633 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6636 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6637 if (is_subset
== -1)
6642 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6648 is_subset
= isl_map_is_subset(map1
, map2
);
6651 is_subset
= isl_map_is_subset(map2
, map1
);
6652 if (is_subset
== -1)
6657 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6659 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6662 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6666 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6669 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6673 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6676 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6683 for (i
= 0; i
< map
->n
; ++i
) {
6684 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6692 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6694 return isl_map_plain_is_universe((isl_map
*) set
);
6697 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6699 return isl_set_plain_is_universe(set
);
6702 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6704 struct isl_basic_set
*bset
= NULL
;
6705 struct isl_vec
*sample
= NULL
;
6712 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6715 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6716 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6717 copy
= isl_basic_map_remove_redundancies(copy
);
6718 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6719 isl_basic_map_free(copy
);
6723 total
= 1 + isl_basic_map_total_dim(bmap
);
6724 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6725 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6731 isl_vec_free(bmap
->sample
);
6732 bmap
->sample
= NULL
;
6733 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6736 sample
= isl_basic_set_sample_vec(bset
);
6739 empty
= sample
->size
== 0;
6740 isl_vec_free(bmap
->sample
);
6741 bmap
->sample
= sample
;
6743 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6748 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6752 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6755 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6757 return isl_basic_map_plain_is_empty(bmap
);
6760 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6764 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6767 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6769 return isl_basic_set_plain_is_empty(bset
);
6772 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6774 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6777 struct isl_map
*isl_basic_map_union(
6778 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6780 struct isl_map
*map
;
6781 if (!bmap1
|| !bmap2
)
6784 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6786 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6789 map
= isl_map_add_basic_map(map
, bmap1
);
6790 map
= isl_map_add_basic_map(map
, bmap2
);
6793 isl_basic_map_free(bmap1
);
6794 isl_basic_map_free(bmap2
);
6798 struct isl_set
*isl_basic_set_union(
6799 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6801 return (struct isl_set
*)isl_basic_map_union(
6802 (struct isl_basic_map
*)bset1
,
6803 (struct isl_basic_map
*)bset2
);
6806 /* Order divs such that any div only depends on previous divs */
6807 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6815 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6817 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6819 if (isl_int_is_zero(bmap
->div
[i
][0]))
6821 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6825 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6831 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6833 return (struct isl_basic_set
*)
6834 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6837 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6844 for (i
= 0; i
< map
->n
; ++i
) {
6845 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6856 /* Apply the expansion computed by isl_merge_divs.
6857 * The expansion itself is given by "exp" while the resulting
6858 * list of divs is given by "div".
6860 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6861 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6866 bset
= isl_basic_set_cow(bset
);
6870 if (div
->n_row
< bset
->n_div
)
6871 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6872 "not an expansion", goto error
);
6874 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
6875 div
->n_row
- bset
->n_div
, 0,
6876 2 * (div
->n_row
- bset
->n_div
));
6878 n_div
= bset
->n_div
;
6879 for (i
= n_div
; i
< div
->n_row
; ++i
)
6880 if (isl_basic_set_alloc_div(bset
) < 0)
6884 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6885 if (j
>= 0 && exp
[j
] == i
) {
6887 isl_basic_map_swap_div(bset
, i
, j
);
6890 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6891 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6899 isl_basic_set_free(bset
);
6904 /* Look for a div in dst that corresponds to the div "div" in src.
6905 * The divs before "div" in src and dst are assumed to be the same.
6907 * Returns -1 if no corresponding div was found and the position
6908 * of the corresponding div in dst otherwise.
6910 static int find_div(struct isl_basic_map
*dst
,
6911 struct isl_basic_map
*src
, unsigned div
)
6915 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
6917 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6918 for (i
= div
; i
< dst
->n_div
; ++i
)
6919 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6920 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6921 dst
->n_div
- div
) == -1)
6926 struct isl_basic_map
*isl_basic_map_align_divs(
6927 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6930 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
6935 if (src
->n_div
== 0)
6938 for (i
= 0; i
< src
->n_div
; ++i
)
6939 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6941 src
= isl_basic_map_order_divs(src
);
6942 dst
= isl_basic_map_cow(dst
);
6943 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
6944 src
->n_div
, 0, 2 * src
->n_div
);
6947 for (i
= 0; i
< src
->n_div
; ++i
) {
6948 int j
= find_div(dst
, src
, i
);
6950 j
= isl_basic_map_alloc_div(dst
);
6953 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6954 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6955 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6959 isl_basic_map_swap_div(dst
, i
, j
);
6963 isl_basic_map_free(dst
);
6967 struct isl_basic_set
*isl_basic_set_align_divs(
6968 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6970 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6971 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6974 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6982 map
= isl_map_compute_divs(map
);
6983 map
= isl_map_cow(map
);
6987 for (i
= 1; i
< map
->n
; ++i
)
6988 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6989 for (i
= 1; i
< map
->n
; ++i
)
6990 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6992 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6996 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6998 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7001 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7002 __isl_take isl_map
*map
)
7006 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7007 map
= isl_map_intersect_domain(map
, set
);
7008 set
= isl_map_range(map
);
7016 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7017 __isl_take isl_map
*map
)
7019 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7022 /* There is no need to cow as removing empty parts doesn't change
7023 * the meaning of the set.
7025 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7032 for (i
= map
->n
-1; i
>= 0; --i
) {
7033 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7035 isl_basic_map_free(map
->p
[i
]);
7036 if (i
!= map
->n
-1) {
7037 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7038 map
->p
[i
] = map
->p
[map
->n
-1];
7046 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7048 return (struct isl_set
*)
7049 isl_map_remove_empty_parts((struct isl_map
*)set
);
7052 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7054 struct isl_basic_map
*bmap
;
7055 if (!map
|| map
->n
== 0)
7057 bmap
= map
->p
[map
->n
-1];
7058 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7059 return isl_basic_map_copy(bmap
);
7062 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7064 return (struct isl_basic_set
*)
7065 isl_map_copy_basic_map((struct isl_map
*)set
);
7068 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7069 __isl_keep isl_basic_map
*bmap
)
7075 for (i
= map
->n
-1; i
>= 0; --i
) {
7076 if (map
->p
[i
] != bmap
)
7078 map
= isl_map_cow(map
);
7081 isl_basic_map_free(map
->p
[i
]);
7082 if (i
!= map
->n
-1) {
7083 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7084 map
->p
[i
] = map
->p
[map
->n
-1];
7095 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7096 struct isl_basic_set
*bset
)
7098 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7099 (struct isl_basic_map
*)bset
);
7102 /* Given two basic sets bset1 and bset2, compute the maximal difference
7103 * between the values of dimension pos in bset1 and those in bset2
7104 * for any common value of the parameters and dimensions preceding pos.
7106 static enum isl_lp_result
basic_set_maximal_difference_at(
7107 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7108 int pos
, isl_int
*opt
)
7111 struct isl_basic_map
*bmap1
= NULL
;
7112 struct isl_basic_map
*bmap2
= NULL
;
7113 struct isl_ctx
*ctx
;
7114 struct isl_vec
*obj
;
7117 unsigned dim1
, dim2
;
7118 enum isl_lp_result res
;
7120 if (!bset1
|| !bset2
)
7121 return isl_lp_error
;
7123 nparam
= isl_basic_set_n_param(bset1
);
7124 dim1
= isl_basic_set_n_dim(bset1
);
7125 dim2
= isl_basic_set_n_dim(bset2
);
7126 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7127 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7128 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7129 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7130 if (!bmap1
|| !bmap2
)
7132 bmap1
= isl_basic_map_cow(bmap1
);
7133 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7134 pos
, (dim1
- pos
) + (dim2
- pos
),
7135 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7136 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7139 total
= isl_basic_map_total_dim(bmap1
);
7141 obj
= isl_vec_alloc(ctx
, 1 + total
);
7142 isl_seq_clr(obj
->block
.data
, 1 + total
);
7143 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7144 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7147 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7149 isl_basic_map_free(bmap1
);
7153 isl_basic_map_free(bmap1
);
7154 isl_basic_map_free(bmap2
);
7155 return isl_lp_error
;
7158 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7159 * for any common value of the parameters and dimensions preceding pos
7160 * in both basic sets, the values of dimension pos in bset1 are
7161 * smaller or larger than those in bset2.
7164 * 1 if bset1 follows bset2
7165 * -1 if bset1 precedes bset2
7166 * 0 if bset1 and bset2 are incomparable
7167 * -2 if some error occurred.
7169 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7170 struct isl_basic_set
*bset2
, int pos
)
7173 enum isl_lp_result res
;
7178 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7180 if (res
== isl_lp_empty
)
7182 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7183 res
== isl_lp_unbounded
)
7185 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7194 /* Given two basic sets bset1 and bset2, check whether
7195 * for any common value of the parameters and dimensions preceding pos
7196 * there is a value of dimension pos in bset1 that is larger
7197 * than a value of the same dimension in bset2.
7200 * 1 if there exists such a pair
7201 * 0 if there is no such pair, but there is a pair of equal values
7203 * -2 if some error occurred.
7205 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7206 __isl_keep isl_basic_set
*bset2
, int pos
)
7209 enum isl_lp_result res
;
7214 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7216 if (res
== isl_lp_empty
)
7218 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7219 res
== isl_lp_unbounded
)
7221 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7223 else if (res
== isl_lp_ok
)
7232 /* Given two sets set1 and set2, check whether
7233 * for any common value of the parameters and dimensions preceding pos
7234 * there is a value of dimension pos in set1 that is larger
7235 * than a value of the same dimension in set2.
7238 * 1 if there exists such a pair
7239 * 0 if there is no such pair, but there is a pair of equal values
7241 * -2 if some error occurred.
7243 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7244 __isl_keep isl_set
*set2
, int pos
)
7252 for (i
= 0; i
< set1
->n
; ++i
)
7253 for (j
= 0; j
< set2
->n
; ++j
) {
7255 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7256 if (f
== 1 || f
== -2)
7265 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7266 unsigned pos
, isl_int
*val
)
7274 total
= isl_basic_map_total_dim(bmap
);
7275 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7276 for (; d
+1 > pos
; --d
)
7277 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7281 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7283 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7285 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7288 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7294 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7295 unsigned pos
, isl_int
*val
)
7307 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7310 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7311 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7312 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7313 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7317 isl_int_set(*val
, v
);
7323 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7324 unsigned pos
, isl_int
*val
)
7326 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7330 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7333 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7336 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7337 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7339 if (pos
>= isl_basic_map_dim(bmap
, type
))
7341 return isl_basic_map_plain_has_fixed_var(bmap
,
7342 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7345 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7346 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7348 if (pos
>= isl_map_dim(map
, type
))
7350 return isl_map_plain_has_fixed_var(map
,
7351 map_offset(map
, type
) - 1 + pos
, val
);
7354 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7355 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7357 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7360 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7361 * then return this fixed value in *val.
7363 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7364 unsigned dim
, isl_int
*val
)
7366 return isl_basic_set_plain_has_fixed_var(bset
,
7367 isl_basic_set_n_param(bset
) + dim
, val
);
7370 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7371 * then return this fixed value in *val.
7373 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7374 unsigned dim
, isl_int
*val
)
7376 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7379 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7380 unsigned dim
, isl_int
*val
)
7382 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7385 /* Check if input variable in has fixed value and if so and if val is not NULL,
7386 * then return this fixed value in *val.
7388 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7389 unsigned in
, isl_int
*val
)
7391 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7394 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7395 * and if val is not NULL, then return this lower bound in *val.
7397 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7398 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7400 int i
, i_eq
= -1, i_ineq
= -1;
7407 total
= isl_basic_set_total_dim(bset
);
7408 nparam
= isl_basic_set_n_param(bset
);
7409 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7410 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7416 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7417 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7419 if (i_eq
!= -1 || i_ineq
!= -1)
7423 if (i_eq
== -1 && i_ineq
== -1)
7425 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7426 /* The coefficient should always be one due to normalization. */
7427 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7429 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7431 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7432 total
- nparam
- dim
- 1) != -1)
7435 isl_int_neg(*val
, c
[0]);
7439 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7440 unsigned dim
, isl_int
*val
)
7452 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7456 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7458 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7459 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7461 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7465 isl_int_set(*val
, v
);
7476 /* uset_gist depends on constraints without existentially quantified
7477 * variables sorting first.
7479 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7481 const struct constraint
*c1
= (const struct constraint
*)p1
;
7482 const struct constraint
*c2
= (const struct constraint
*)p2
;
7484 unsigned size
= isl_min(c1
->size
, c2
->size
);
7486 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7487 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7492 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7495 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7496 struct isl_basic_map
*bmap
)
7499 struct constraint
*c
;
7504 total
= isl_basic_map_total_dim(bmap
);
7505 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7508 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7510 c
[i
].c
= bmap
->ineq
[i
];
7512 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7513 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7514 bmap
->ineq
[i
] = c
[i
].c
;
7518 isl_basic_map_free(bmap
);
7522 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7523 __isl_take isl_basic_set
*bset
)
7525 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7526 (struct isl_basic_map
*)bset
);
7529 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7533 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7535 bmap
= isl_basic_map_remove_redundancies(bmap
);
7536 bmap
= isl_basic_map_sort_constraints(bmap
);
7537 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7541 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7543 return (struct isl_basic_set
*)isl_basic_map_normalize(
7544 (struct isl_basic_map
*)bset
);
7547 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7548 const __isl_keep isl_basic_map
*bmap2
)
7555 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7556 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7557 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7558 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7559 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7560 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7561 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7562 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7563 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7564 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7565 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7567 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7569 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7571 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7572 return bmap1
->n_eq
- bmap2
->n_eq
;
7573 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7574 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7575 if (bmap1
->n_div
!= bmap2
->n_div
)
7576 return bmap1
->n_div
- bmap2
->n_div
;
7577 total
= isl_basic_map_total_dim(bmap1
);
7578 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7579 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7583 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7584 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7588 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7589 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7596 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7597 __isl_keep isl_basic_map
*bmap2
)
7599 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7602 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7603 __isl_keep isl_basic_set
*bset2
)
7605 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7606 (isl_basic_map
*)bset2
);
7609 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7611 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7612 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7614 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7617 /* We normalize in place, but if anything goes wrong we need
7618 * to return NULL, so we need to make sure we don't change the
7619 * meaning of any possible other copies of map.
7621 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7624 struct isl_basic_map
*bmap
;
7628 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7630 for (i
= 0; i
< map
->n
; ++i
) {
7631 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7634 isl_basic_map_free(map
->p
[i
]);
7637 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7638 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7639 map
= isl_map_remove_empty_parts(map
);
7642 for (i
= map
->n
- 1; i
>= 1; --i
) {
7643 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7645 isl_basic_map_free(map
->p
[i
-1]);
7646 for (j
= i
; j
< map
->n
; ++j
)
7647 map
->p
[j
-1] = map
->p
[j
];
7657 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7659 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7662 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7672 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7675 map1
= isl_map_copy(map1
);
7676 map2
= isl_map_copy(map2
);
7677 map1
= isl_map_normalize(map1
);
7678 map2
= isl_map_normalize(map2
);
7681 equal
= map1
->n
== map2
->n
;
7682 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7683 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7696 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7698 return isl_map_plain_is_equal(map1
, map2
);
7701 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7703 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7704 (struct isl_map
*)set2
);
7707 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7709 return isl_set_plain_is_equal(set1
, set2
);
7712 /* Return an interval that ranges from min to max (inclusive)
7714 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7715 isl_int min
, isl_int max
)
7718 struct isl_basic_set
*bset
= NULL
;
7720 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7724 k
= isl_basic_set_alloc_inequality(bset
);
7727 isl_int_set_si(bset
->ineq
[k
][1], 1);
7728 isl_int_neg(bset
->ineq
[k
][0], min
);
7730 k
= isl_basic_set_alloc_inequality(bset
);
7733 isl_int_set_si(bset
->ineq
[k
][1], -1);
7734 isl_int_set(bset
->ineq
[k
][0], max
);
7738 isl_basic_set_free(bset
);
7742 /* Return the Cartesian product of the basic sets in list (in the given order).
7744 __isl_give isl_basic_set
*isl_basic_set_list_product(
7745 __isl_take
struct isl_basic_set_list
*list
)
7753 struct isl_basic_set
*product
= NULL
;
7757 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7758 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7759 nparam
= isl_basic_set_n_param(list
->p
[0]);
7760 dim
= isl_basic_set_n_dim(list
->p
[0]);
7761 extra
= list
->p
[0]->n_div
;
7762 n_eq
= list
->p
[0]->n_eq
;
7763 n_ineq
= list
->p
[0]->n_ineq
;
7764 for (i
= 1; i
< list
->n
; ++i
) {
7765 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7766 isl_assert(list
->ctx
,
7767 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7768 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7769 extra
+= list
->p
[i
]->n_div
;
7770 n_eq
+= list
->p
[i
]->n_eq
;
7771 n_ineq
+= list
->p
[i
]->n_ineq
;
7773 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7778 for (i
= 0; i
< list
->n
; ++i
) {
7779 isl_basic_set_add_constraints(product
,
7780 isl_basic_set_copy(list
->p
[i
]), dim
);
7781 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7783 isl_basic_set_list_free(list
);
7786 isl_basic_set_free(product
);
7787 isl_basic_set_list_free(list
);
7791 struct isl_basic_map
*isl_basic_map_product(
7792 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7794 isl_space
*dim_result
= NULL
;
7795 struct isl_basic_map
*bmap
;
7796 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7797 struct isl_dim_map
*dim_map1
, *dim_map2
;
7799 if (!bmap1
|| !bmap2
)
7802 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7803 bmap2
->dim
, isl_dim_param
), goto error
);
7804 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
7805 isl_space_copy(bmap2
->dim
));
7807 in1
= isl_basic_map_n_in(bmap1
);
7808 in2
= isl_basic_map_n_in(bmap2
);
7809 out1
= isl_basic_map_n_out(bmap1
);
7810 out2
= isl_basic_map_n_out(bmap2
);
7811 nparam
= isl_basic_map_n_param(bmap1
);
7813 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7814 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7815 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7816 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7817 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7818 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7819 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7820 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7821 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7822 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7823 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7825 bmap
= isl_basic_map_alloc_space(dim_result
,
7826 bmap1
->n_div
+ bmap2
->n_div
,
7827 bmap1
->n_eq
+ bmap2
->n_eq
,
7828 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7829 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7830 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7831 bmap
= isl_basic_map_simplify(bmap
);
7832 return isl_basic_map_finalize(bmap
);
7834 isl_basic_map_free(bmap1
);
7835 isl_basic_map_free(bmap2
);
7839 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7840 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7842 isl_basic_map
*prod
;
7844 prod
= isl_basic_map_product(bmap1
, bmap2
);
7845 prod
= isl_basic_map_flatten(prod
);
7849 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7850 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7852 return isl_basic_map_flat_range_product(bset1
, bset2
);
7855 __isl_give isl_basic_map
*isl_basic_map_domain_product(
7856 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7858 isl_space
*space_result
= NULL
;
7859 isl_basic_map
*bmap
;
7860 unsigned in1
, in2
, out
, nparam
, total
, pos
;
7861 struct isl_dim_map
*dim_map1
, *dim_map2
;
7863 if (!bmap1
|| !bmap2
)
7866 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
7867 isl_space_copy(bmap2
->dim
));
7869 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7870 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
7871 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
7872 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
7874 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
7875 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7876 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7877 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7878 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7879 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7880 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7881 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7882 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
7883 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
7884 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7886 bmap
= isl_basic_map_alloc_space(space_result
,
7887 bmap1
->n_div
+ bmap2
->n_div
,
7888 bmap1
->n_eq
+ bmap2
->n_eq
,
7889 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7890 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7891 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7892 bmap
= isl_basic_map_simplify(bmap
);
7893 return isl_basic_map_finalize(bmap
);
7895 isl_basic_map_free(bmap1
);
7896 isl_basic_map_free(bmap2
);
7900 __isl_give isl_basic_map
*isl_basic_map_range_product(
7901 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7903 isl_space
*dim_result
= NULL
;
7904 isl_basic_map
*bmap
;
7905 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7906 struct isl_dim_map
*dim_map1
, *dim_map2
;
7908 if (!bmap1
|| !bmap2
)
7911 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
7912 isl_space_copy(bmap2
->dim
));
7914 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7915 out1
= isl_basic_map_n_out(bmap1
);
7916 out2
= isl_basic_map_n_out(bmap2
);
7917 nparam
= isl_basic_map_n_param(bmap1
);
7919 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7920 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7921 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7922 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7923 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7924 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7925 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7926 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7927 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7928 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7929 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7931 bmap
= isl_basic_map_alloc_space(dim_result
,
7932 bmap1
->n_div
+ bmap2
->n_div
,
7933 bmap1
->n_eq
+ bmap2
->n_eq
,
7934 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7935 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7936 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7937 bmap
= isl_basic_map_simplify(bmap
);
7938 return isl_basic_map_finalize(bmap
);
7940 isl_basic_map_free(bmap1
);
7941 isl_basic_map_free(bmap2
);
7945 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
7946 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7948 isl_basic_map
*prod
;
7950 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
7951 prod
= isl_basic_map_flatten_range(prod
);
7955 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7956 __isl_take isl_map
*map2
,
7957 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
7958 __isl_take isl_space
*right
),
7959 __isl_give isl_basic_map
*(*basic_map_product
)(
7960 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7963 struct isl_map
*result
;
7969 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
7970 map2
->dim
, isl_dim_param
), goto error
);
7972 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7973 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7974 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7976 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
7977 isl_space_copy(map2
->dim
)),
7978 map1
->n
* map2
->n
, flags
);
7981 for (i
= 0; i
< map1
->n
; ++i
)
7982 for (j
= 0; j
< map2
->n
; ++j
) {
7983 struct isl_basic_map
*part
;
7984 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7985 isl_basic_map_copy(map2
->p
[j
]));
7986 if (isl_basic_map_is_empty(part
))
7987 isl_basic_map_free(part
);
7989 result
= isl_map_add_basic_map(result
, part
);
8002 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8004 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8005 __isl_take isl_map
*map2
)
8007 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8010 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8011 __isl_take isl_map
*map2
)
8013 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8016 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8018 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8019 __isl_take isl_map
*map2
)
8023 prod
= isl_map_product(map1
, map2
);
8024 prod
= isl_map_flatten(prod
);
8028 /* Given two set A and B, construct its Cartesian product A x B.
8030 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8032 return isl_map_range_product(set1
, set2
);
8035 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8036 __isl_take isl_set
*set2
)
8038 return isl_map_flat_range_product(set1
, set2
);
8041 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8043 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8044 __isl_take isl_map
*map2
)
8046 return map_product(map1
, map2
, &isl_space_domain_product
,
8047 &isl_basic_map_domain_product
);
8050 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8052 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8053 __isl_take isl_map
*map2
)
8055 return map_product(map1
, map2
, &isl_space_range_product
,
8056 &isl_basic_map_range_product
);
8059 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8060 __isl_take isl_map
*map2
)
8062 return isl_map_align_params_map_map_and(map1
, map2
,
8063 &map_domain_product_aligned
);
8066 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8067 __isl_take isl_map
*map2
)
8069 return isl_map_align_params_map_map_and(map1
, map2
,
8070 &map_range_product_aligned
);
8073 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8075 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8076 __isl_take isl_map
*map2
)
8080 prod
= isl_map_domain_product(map1
, map2
);
8081 prod
= isl_map_flatten_domain(prod
);
8085 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8087 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8088 __isl_take isl_map
*map2
)
8092 prod
= isl_map_range_product(map1
, map2
);
8093 prod
= isl_map_flatten_range(prod
);
8097 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8100 uint32_t hash
= isl_hash_init();
8105 bmap
= isl_basic_map_copy(bmap
);
8106 bmap
= isl_basic_map_normalize(bmap
);
8109 total
= isl_basic_map_total_dim(bmap
);
8110 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8111 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8113 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8114 isl_hash_hash(hash
, c_hash
);
8116 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8117 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8119 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8120 isl_hash_hash(hash
, c_hash
);
8122 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8123 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8125 if (isl_int_is_zero(bmap
->div
[i
][0]))
8127 isl_hash_byte(hash
, i
& 0xFF);
8128 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8129 isl_hash_hash(hash
, c_hash
);
8131 isl_basic_map_free(bmap
);
8135 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8137 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8140 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8147 map
= isl_map_copy(map
);
8148 map
= isl_map_normalize(map
);
8152 hash
= isl_hash_init();
8153 for (i
= 0; i
< map
->n
; ++i
) {
8155 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8156 isl_hash_hash(hash
, bmap_hash
);
8164 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8166 return isl_map_get_hash((isl_map
*)set
);
8169 /* Check if the value for dimension dim is completely determined
8170 * by the values of the other parameters and variables.
8171 * That is, check if dimension dim is involved in an equality.
8173 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8180 nparam
= isl_basic_set_n_param(bset
);
8181 for (i
= 0; i
< bset
->n_eq
; ++i
)
8182 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8187 /* Check if the value for dimension dim is completely determined
8188 * by the values of the other parameters and variables.
8189 * That is, check if dimension dim is involved in an equality
8190 * for each of the subsets.
8192 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8198 for (i
= 0; i
< set
->n
; ++i
) {
8200 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8207 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8209 return set
? set
->n
: 0;
8212 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8213 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8220 for (i
= 0; i
< map
->n
; ++i
)
8221 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8227 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8228 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8235 for (i
= 0; i
< set
->n
; ++i
)
8236 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8242 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8249 bset
= isl_basic_set_cow(bset
);
8253 dim
= isl_basic_set_get_space(bset
);
8254 dim
= isl_space_lift(dim
, bset
->n_div
);
8257 isl_space_free(bset
->dim
);
8259 bset
->extra
-= bset
->n_div
;
8262 bset
= isl_basic_set_finalize(bset
);
8266 isl_basic_set_free(bset
);
8270 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8276 set
= isl_set_align_divs(set
);
8281 set
= isl_set_cow(set
);
8285 n_div
= set
->p
[0]->n_div
;
8286 dim
= isl_set_get_space(set
);
8287 dim
= isl_space_lift(dim
, n_div
);
8290 isl_space_free(set
->dim
);
8293 for (i
= 0; i
< set
->n
; ++i
) {
8294 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8305 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8308 struct isl_basic_map
*bmap
;
8315 set
= isl_set_align_divs(set
);
8320 dim
= isl_set_get_space(set
);
8321 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8323 return isl_map_identity(isl_space_map_from_set(dim
));
8326 n_div
= set
->p
[0]->n_div
;
8327 dim
= isl_space_map_from_set(dim
);
8328 n_param
= isl_space_dim(dim
, isl_dim_param
);
8329 n_set
= isl_space_dim(dim
, isl_dim_in
);
8330 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8331 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8332 for (i
= 0; i
< n_set
; ++i
)
8333 bmap
= var_equal(bmap
, i
);
8335 total
= n_param
+ n_set
+ n_set
+ n_div
;
8336 for (i
= 0; i
< n_div
; ++i
) {
8337 k
= isl_basic_map_alloc_inequality(bmap
);
8340 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8341 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8342 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8343 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8344 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8345 set
->p
[0]->div
[i
][0]);
8347 l
= isl_basic_map_alloc_inequality(bmap
);
8350 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8351 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8352 set
->p
[0]->div
[i
][0]);
8353 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8357 bmap
= isl_basic_map_simplify(bmap
);
8358 bmap
= isl_basic_map_finalize(bmap
);
8359 return isl_map_from_basic_map(bmap
);
8362 isl_basic_map_free(bmap
);
8366 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8374 dim
= isl_basic_set_total_dim(bset
);
8375 size
+= bset
->n_eq
* (1 + dim
);
8376 size
+= bset
->n_ineq
* (1 + dim
);
8377 size
+= bset
->n_div
* (2 + dim
);
8382 int isl_set_size(__isl_keep isl_set
*set
)
8390 for (i
= 0; i
< set
->n
; ++i
)
8391 size
+= isl_basic_set_size(set
->p
[i
]);
8396 /* Check if there is any lower bound (if lower == 0) and/or upper
8397 * bound (if upper == 0) on the specified dim.
8399 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8400 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8407 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8409 pos
+= isl_basic_map_offset(bmap
, type
);
8411 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8412 if (isl_int_is_zero(bmap
->div
[i
][0]))
8414 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8418 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8419 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8422 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8423 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8430 return lower
&& upper
;
8433 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8434 enum isl_dim_type type
, unsigned pos
)
8436 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8439 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8440 enum isl_dim_type type
, unsigned pos
)
8442 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8445 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8446 enum isl_dim_type type
, unsigned pos
)
8448 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8451 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8452 enum isl_dim_type type
, unsigned pos
)
8459 for (i
= 0; i
< map
->n
; ++i
) {
8461 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8462 if (bounded
< 0 || !bounded
)
8469 /* Return 1 if the specified dim is involved in both an upper bound
8470 * and a lower bound.
8472 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8473 enum isl_dim_type type
, unsigned pos
)
8475 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8478 static int has_bound(__isl_keep isl_map
*map
,
8479 enum isl_dim_type type
, unsigned pos
,
8480 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8481 enum isl_dim_type type
, unsigned pos
))
8488 for (i
= 0; i
< map
->n
; ++i
) {
8490 bounded
= fn(map
->p
[i
], type
, pos
);
8491 if (bounded
< 0 || bounded
)
8498 /* Return 1 if the specified dim is involved in any lower bound.
8500 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8501 enum isl_dim_type type
, unsigned pos
)
8503 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8506 /* Return 1 if the specified dim is involved in any upper bound.
8508 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8509 enum isl_dim_type type
, unsigned pos
)
8511 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8514 /* For each of the "n" variables starting at "first", determine
8515 * the sign of the variable and put the results in the first "n"
8516 * elements of the array "signs".
8518 * 1 means that the variable is non-negative
8519 * -1 means that the variable is non-positive
8520 * 0 means the variable attains both positive and negative values.
8522 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8523 unsigned first
, unsigned n
, int *signs
)
8525 isl_vec
*bound
= NULL
;
8526 struct isl_tab
*tab
= NULL
;
8527 struct isl_tab_undo
*snap
;
8530 if (!bset
|| !signs
)
8533 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8534 tab
= isl_tab_from_basic_set(bset
);
8538 isl_seq_clr(bound
->el
, bound
->size
);
8539 isl_int_set_si(bound
->el
[0], -1);
8541 snap
= isl_tab_snap(tab
);
8542 for (i
= 0; i
< n
; ++i
) {
8545 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8546 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8549 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8550 if (isl_tab_rollback(tab
, snap
) < 0)
8558 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8559 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8562 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8563 if (isl_tab_rollback(tab
, snap
) < 0)
8566 signs
[i
] = empty
? -1 : 0;
8570 isl_vec_free(bound
);
8574 isl_vec_free(bound
);
8578 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8579 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8581 if (!bset
|| !signs
)
8583 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8586 first
+= pos(bset
->dim
, type
) - 1;
8587 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8590 /* Check if the given basic map is obviously single-valued.
8591 * In particular, for each output dimension, check that there is
8592 * an equality that defines the output dimension in terms of
8593 * earlier dimensions.
8595 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8605 total
= 1 + isl_basic_map_total_dim(bmap
);
8606 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8607 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8609 for (i
= 0; i
< n_out
; ++i
) {
8610 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8611 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8613 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8614 total
- (o_out
+ i
+ 1)) == -1)
8617 if (j
>= bmap
->n_eq
)
8624 /* Check if the given map is obviously single-valued.
8626 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8635 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8638 /* Check if the given map is single-valued.
8643 * and check if the result is a subset of the identity mapping.
8645 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8652 sv
= isl_map_plain_is_single_valued(map
);
8656 test
= isl_map_reverse(isl_map_copy(map
));
8657 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8659 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8660 id
= isl_map_identity(dim
);
8662 sv
= isl_map_is_subset(test
, id
);
8670 int isl_map_is_injective(__isl_keep isl_map
*map
)
8674 map
= isl_map_copy(map
);
8675 map
= isl_map_reverse(map
);
8676 in
= isl_map_is_single_valued(map
);
8682 /* Check if the given map is obviously injective.
8684 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8688 map
= isl_map_copy(map
);
8689 map
= isl_map_reverse(map
);
8690 in
= isl_map_plain_is_single_valued(map
);
8696 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8700 sv
= isl_map_is_single_valued(map
);
8704 return isl_map_is_injective(map
);
8707 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8709 return isl_map_is_single_valued((isl_map
*)set
);
8712 int isl_map_is_translation(__isl_keep isl_map
*map
)
8717 delta
= isl_map_deltas(isl_map_copy(map
));
8718 ok
= isl_set_is_singleton(delta
);
8719 isl_set_free(delta
);
8724 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8726 if (isl_seq_first_non_zero(p
, pos
) != -1)
8728 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8733 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8742 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8745 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8746 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8747 for (j
= 0; j
< nvar
; ++j
) {
8748 int lower
= 0, upper
= 0;
8749 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8750 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8752 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8758 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8759 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8761 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8763 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8768 if (!lower
|| !upper
)
8775 int isl_set_is_box(__isl_keep isl_set
*set
)
8782 return isl_basic_set_is_box(set
->p
[0]);
8785 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8790 return isl_space_is_wrapping(bset
->dim
);
8793 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8798 return isl_space_is_wrapping(set
->dim
);
8801 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8803 bmap
= isl_basic_map_cow(bmap
);
8807 bmap
->dim
= isl_space_wrap(bmap
->dim
);
8811 bmap
= isl_basic_map_finalize(bmap
);
8813 return (isl_basic_set
*)bmap
;
8815 isl_basic_map_free(bmap
);
8819 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8823 map
= isl_map_cow(map
);
8827 for (i
= 0; i
< map
->n
; ++i
) {
8828 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8832 map
->dim
= isl_space_wrap(map
->dim
);
8836 return (isl_set
*)map
;
8842 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8844 bset
= isl_basic_set_cow(bset
);
8848 bset
->dim
= isl_space_unwrap(bset
->dim
);
8852 bset
= isl_basic_set_finalize(bset
);
8854 return (isl_basic_map
*)bset
;
8856 isl_basic_set_free(bset
);
8860 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8867 if (!isl_set_is_wrapping(set
))
8868 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8871 set
= isl_set_cow(set
);
8875 for (i
= 0; i
< set
->n
; ++i
) {
8876 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8881 set
->dim
= isl_space_unwrap(set
->dim
);
8885 return (isl_map
*)set
;
8891 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8892 enum isl_dim_type type
)
8897 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
8900 bmap
= isl_basic_map_cow(bmap
);
8904 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
8908 bmap
= isl_basic_map_finalize(bmap
);
8912 isl_basic_map_free(bmap
);
8916 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8917 enum isl_dim_type type
)
8924 if (!isl_space_is_named_or_nested(map
->dim
, type
))
8927 map
= isl_map_cow(map
);
8931 for (i
= 0; i
< map
->n
; ++i
) {
8932 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8936 map
->dim
= isl_space_reset(map
->dim
, type
);
8946 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8951 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8954 bmap
= isl_basic_map_cow(bmap
);
8958 bmap
->dim
= isl_space_flatten(bmap
->dim
);
8962 bmap
= isl_basic_map_finalize(bmap
);
8966 isl_basic_map_free(bmap
);
8970 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8972 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8975 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
8976 __isl_take isl_basic_map
*bmap
)
8981 if (!bmap
->dim
->nested
[0])
8984 bmap
= isl_basic_map_cow(bmap
);
8988 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
8992 bmap
= isl_basic_map_finalize(bmap
);
8996 isl_basic_map_free(bmap
);
9000 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9001 __isl_take isl_basic_map
*bmap
)
9006 if (!bmap
->dim
->nested
[1])
9009 bmap
= isl_basic_map_cow(bmap
);
9013 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9017 bmap
= isl_basic_map_finalize(bmap
);
9021 isl_basic_map_free(bmap
);
9025 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9032 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9035 map
= isl_map_cow(map
);
9039 for (i
= 0; i
< map
->n
; ++i
) {
9040 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9044 map
->dim
= isl_space_flatten(map
->dim
);
9054 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9056 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9059 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9061 isl_space
*dim
, *flat_dim
;
9064 dim
= isl_set_get_space(set
);
9065 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9066 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9067 map
= isl_map_intersect_domain(map
, set
);
9072 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9079 if (!map
->dim
->nested
[0])
9082 map
= isl_map_cow(map
);
9086 for (i
= 0; i
< map
->n
; ++i
) {
9087 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9091 map
->dim
= isl_space_flatten_domain(map
->dim
);
9101 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9108 if (!map
->dim
->nested
[1])
9111 map
= isl_map_cow(map
);
9115 for (i
= 0; i
< map
->n
; ++i
) {
9116 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9120 map
->dim
= isl_space_flatten_range(map
->dim
);
9130 /* Reorder the dimensions of "bmap" according to the given dim_map
9131 * and set the dimension specification to "dim".
9133 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9134 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9138 bmap
= isl_basic_map_cow(bmap
);
9139 if (!bmap
|| !dim
|| !dim_map
)
9142 res
= isl_basic_map_alloc_space(dim
,
9143 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9144 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9145 res
= isl_basic_map_finalize(res
);
9149 isl_basic_map_free(bmap
);
9150 isl_space_free(dim
);
9154 /* Reorder the dimensions of "map" according to given reordering.
9156 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9157 __isl_take isl_reordering
*r
)
9160 struct isl_dim_map
*dim_map
;
9162 map
= isl_map_cow(map
);
9163 dim_map
= isl_dim_map_from_reordering(r
);
9164 if (!map
|| !r
|| !dim_map
)
9167 for (i
= 0; i
< map
->n
; ++i
) {
9168 struct isl_dim_map
*dim_map_i
;
9170 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9172 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9173 isl_space_copy(r
->dim
), dim_map_i
);
9179 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9181 isl_reordering_free(r
);
9187 isl_reordering_free(r
);
9191 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9192 __isl_take isl_reordering
*r
)
9194 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9197 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9198 __isl_take isl_space
*model
)
9205 ctx
= isl_space_get_ctx(model
);
9206 if (!isl_space_has_named_params(model
))
9207 isl_die(ctx
, isl_error_invalid
,
9208 "model has unnamed parameters", goto error
);
9209 if (!isl_space_has_named_params(map
->dim
))
9210 isl_die(ctx
, isl_error_invalid
,
9211 "relation has unnamed parameters", goto error
);
9212 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9213 isl_reordering
*exp
;
9215 model
= isl_space_drop_dims(model
, isl_dim_in
,
9216 0, isl_space_dim(model
, isl_dim_in
));
9217 model
= isl_space_drop_dims(model
, isl_dim_out
,
9218 0, isl_space_dim(model
, isl_dim_out
));
9219 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9220 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9221 map
= isl_map_realign(map
, exp
);
9224 isl_space_free(model
);
9227 isl_space_free(model
);
9232 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9233 __isl_take isl_space
*model
)
9235 return isl_map_align_params(set
, model
);
9238 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9239 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9240 enum isl_dim_type c2
, enum isl_dim_type c3
,
9241 enum isl_dim_type c4
, enum isl_dim_type c5
)
9243 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9244 struct isl_mat
*mat
;
9250 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9251 isl_basic_map_total_dim(bmap
) + 1);
9254 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9255 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9256 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9257 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9258 isl_int_set(mat
->row
[i
][pos
],
9259 bmap
->eq
[i
][off
+ k
]);
9267 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9268 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9269 enum isl_dim_type c2
, enum isl_dim_type c3
,
9270 enum isl_dim_type c4
, enum isl_dim_type c5
)
9272 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9273 struct isl_mat
*mat
;
9279 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9280 isl_basic_map_total_dim(bmap
) + 1);
9283 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9284 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9285 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9286 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9287 isl_int_set(mat
->row
[i
][pos
],
9288 bmap
->ineq
[i
][off
+ k
]);
9296 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9297 __isl_take isl_space
*dim
,
9298 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9299 enum isl_dim_type c2
, enum isl_dim_type c3
,
9300 enum isl_dim_type c4
, enum isl_dim_type c5
)
9302 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9303 isl_basic_map
*bmap
;
9309 if (!dim
|| !eq
|| !ineq
)
9312 if (eq
->n_col
!= ineq
->n_col
)
9313 isl_die(dim
->ctx
, isl_error_invalid
,
9314 "equalities and inequalities matrices should have "
9315 "same number of columns", goto error
);
9317 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9319 if (eq
->n_col
< total
)
9320 isl_die(dim
->ctx
, isl_error_invalid
,
9321 "number of columns too small", goto error
);
9323 extra
= eq
->n_col
- total
;
9325 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9326 eq
->n_row
, ineq
->n_row
);
9329 for (i
= 0; i
< extra
; ++i
) {
9330 k
= isl_basic_map_alloc_div(bmap
);
9333 isl_int_set_si(bmap
->div
[k
][0], 0);
9335 for (i
= 0; i
< eq
->n_row
; ++i
) {
9336 l
= isl_basic_map_alloc_equality(bmap
);
9339 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9340 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9341 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9342 isl_int_set(bmap
->eq
[l
][off
+ k
],
9348 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9349 l
= isl_basic_map_alloc_inequality(bmap
);
9352 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9353 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9354 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9355 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9362 isl_space_free(dim
);
9368 isl_space_free(dim
);
9374 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9375 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9376 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9378 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9379 c1
, c2
, c3
, c4
, isl_dim_in
);
9382 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9383 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9384 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9386 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9387 c1
, c2
, c3
, c4
, isl_dim_in
);
9390 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9391 __isl_take isl_space
*dim
,
9392 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9393 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9395 return (isl_basic_set
*)
9396 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9397 c1
, c2
, c3
, c4
, isl_dim_in
);
9400 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9405 return isl_space_can_zip(bmap
->dim
);
9408 int isl_map_can_zip(__isl_keep isl_map
*map
)
9413 return isl_space_can_zip(map
->dim
);
9416 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9417 * (A -> C) -> (B -> D).
9419 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9428 if (!isl_basic_map_can_zip(bmap
))
9429 isl_die(bmap
->ctx
, isl_error_invalid
,
9430 "basic map cannot be zipped", goto error
);
9431 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9432 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9433 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9434 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9435 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9438 bmap
->dim
= isl_space_zip(bmap
->dim
);
9443 isl_basic_map_free(bmap
);
9447 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9448 * (A -> C) -> (B -> D).
9450 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9457 if (!isl_map_can_zip(map
))
9458 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9461 map
= isl_map_cow(map
);
9465 for (i
= 0; i
< map
->n
; ++i
) {
9466 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9471 map
->dim
= isl_space_zip(map
->dim
);
9481 /* Construct a basic map mapping the domain of the affine expression
9482 * to a one-dimensional range prescribed by the affine expression.
9484 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9488 isl_local_space
*ls
;
9489 isl_basic_map
*bmap
;
9494 ls
= isl_aff_get_local_space(aff
);
9495 ls
= isl_local_space_from_domain(ls
);
9496 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
9497 bmap
= isl_basic_map_from_local_space(ls
);
9498 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9499 k
= isl_basic_map_alloc_equality(bmap
);
9503 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9504 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9505 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9506 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9507 aff
->v
->size
- (pos
+ 1));
9510 bmap
= isl_basic_map_finalize(bmap
);
9514 isl_basic_map_free(bmap
);
9518 /* Construct a basic map mapping a domain in the given space to
9519 * to an n-dimensional range, with n the number of elements in the list,
9520 * where each coordinate in the range is prescribed by the
9521 * corresponding affine expression.
9522 * The domains of all affine expressions in the list are assumed to match
9525 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9526 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9530 isl_basic_map
*bmap
;
9535 dim
= isl_space_from_domain(domain_dim
);
9536 bmap
= isl_basic_map_universe(dim
);
9538 for (i
= 0; i
< list
->n
; ++i
) {
9540 isl_basic_map
*bmap_i
;
9542 aff
= isl_aff_copy(list
->p
[i
]);
9543 bmap_i
= isl_basic_map_from_aff(aff
);
9545 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9548 isl_aff_list_free(list
);
9552 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9553 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9555 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9558 /* Add a constraint imposing that the given two dimensions are equal.
9560 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9561 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9563 isl_basic_map
*bmap
= NULL
;
9569 if (pos1
>= isl_map_dim(map
, type1
))
9570 isl_die(map
->ctx
, isl_error_invalid
,
9571 "index out of bounds", goto error
);
9572 if (pos2
>= isl_map_dim(map
, type2
))
9573 isl_die(map
->ctx
, isl_error_invalid
,
9574 "index out of bounds", goto error
);
9576 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9577 i
= isl_basic_map_alloc_equality(bmap
);
9580 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9581 pos1
+= isl_basic_map_offset(bmap
, type1
);
9582 pos2
+= isl_basic_map_offset(bmap
, type2
);
9583 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9584 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9585 bmap
= isl_basic_map_finalize(bmap
);
9587 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9591 isl_basic_map_free(bmap
);
9596 /* Add a constraint imposing that the given two dimensions have opposite values.
9598 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9599 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9601 isl_basic_map
*bmap
= NULL
;
9607 if (pos1
>= isl_map_dim(map
, type1
))
9608 isl_die(map
->ctx
, isl_error_invalid
,
9609 "index out of bounds", goto error
);
9610 if (pos2
>= isl_map_dim(map
, type2
))
9611 isl_die(map
->ctx
, isl_error_invalid
,
9612 "index out of bounds", goto error
);
9614 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9615 i
= isl_basic_map_alloc_equality(bmap
);
9618 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9619 pos1
+= isl_basic_map_offset(bmap
, type1
);
9620 pos2
+= isl_basic_map_offset(bmap
, type2
);
9621 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9622 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9623 bmap
= isl_basic_map_finalize(bmap
);
9625 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9629 isl_basic_map_free(bmap
);