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 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
613 struct isl_basic_map
*bmap
, unsigned extra
,
614 unsigned n_eq
, unsigned n_ineq
)
617 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
622 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
623 if (isl_blk_is_error(bmap
->block
))
626 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
631 bmap
->block2
= isl_blk_empty();
634 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
635 if (isl_blk_is_error(bmap
->block2
))
638 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
643 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
644 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
646 for (i
= 0; i
< extra
; ++i
)
647 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
651 bmap
->c_size
= n_eq
+ n_ineq
;
652 bmap
->eq
= bmap
->ineq
+ n_ineq
;
661 isl_basic_map_free(bmap
);
665 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
666 unsigned nparam
, unsigned dim
, unsigned extra
,
667 unsigned n_eq
, unsigned n_ineq
)
669 struct isl_basic_map
*bmap
;
670 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
671 return (struct isl_basic_set
*)bmap
;
674 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
675 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
677 struct isl_basic_map
*bmap
;
680 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
681 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
682 return (struct isl_basic_set
*)bmap
;
688 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
689 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
691 struct isl_basic_map
*bmap
;
695 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
700 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
706 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
707 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
708 unsigned n_eq
, unsigned n_ineq
)
710 struct isl_basic_map
*bmap
;
713 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
717 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
721 static void dup_constraints(
722 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
725 unsigned total
= isl_basic_map_total_dim(src
);
727 for (i
= 0; i
< src
->n_eq
; ++i
) {
728 int j
= isl_basic_map_alloc_equality(dst
);
729 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
732 for (i
= 0; i
< src
->n_ineq
; ++i
) {
733 int j
= isl_basic_map_alloc_inequality(dst
);
734 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
737 for (i
= 0; i
< src
->n_div
; ++i
) {
738 int j
= isl_basic_map_alloc_div(dst
);
739 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
741 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
744 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
746 struct isl_basic_map
*dup
;
750 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
751 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
754 dup_constraints(dup
, bmap
);
755 dup
->flags
= bmap
->flags
;
756 dup
->sample
= isl_vec_copy(bmap
->sample
);
760 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
762 struct isl_basic_map
*dup
;
764 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
765 return (struct isl_basic_set
*)dup
;
768 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
773 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
777 return isl_basic_set_dup(bset
);
780 struct isl_set
*isl_set_copy(struct isl_set
*set
)
789 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
794 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
798 bmap
= isl_basic_map_dup(bmap
);
800 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
804 struct isl_map
*isl_map_copy(struct isl_map
*map
)
813 void isl_basic_map_free(struct isl_basic_map
*bmap
)
821 isl_ctx_deref(bmap
->ctx
);
823 isl_blk_free(bmap
->ctx
, bmap
->block2
);
825 isl_blk_free(bmap
->ctx
, bmap
->block
);
826 isl_vec_free(bmap
->sample
);
827 isl_space_free(bmap
->dim
);
831 void isl_basic_set_free(struct isl_basic_set
*bset
)
833 isl_basic_map_free((struct isl_basic_map
*)bset
);
836 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
838 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
841 __isl_give isl_map
*isl_map_align_params_map_map_and(
842 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
843 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
844 __isl_take isl_map
*map2
))
848 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
849 return fn(map1
, map2
);
850 if (!isl_space_has_named_params(map1
->dim
) ||
851 !isl_space_has_named_params(map2
->dim
))
852 isl_die(map1
->ctx
, isl_error_invalid
,
853 "unaligned unnamed parameters", goto error
);
854 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
855 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
856 return fn(map1
, map2
);
863 static int align_params_map_map_and_test(__isl_keep isl_map
*map1
,
864 __isl_keep isl_map
*map2
,
865 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
871 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
872 return fn(map1
, map2
);
873 if (!isl_space_has_named_params(map1
->dim
) ||
874 !isl_space_has_named_params(map2
->dim
))
875 isl_die(map1
->ctx
, isl_error_invalid
,
876 "unaligned unnamed parameters", return -1);
877 map1
= isl_map_copy(map1
);
878 map2
= isl_map_copy(map2
);
879 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
880 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
887 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
893 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
894 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
896 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
897 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
898 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
899 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
900 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
901 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
903 int j
= isl_basic_map_alloc_inequality(bmap
);
907 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
908 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
915 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
916 bmap
->extra
- bmap
->n_div
);
920 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
922 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
925 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
929 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
934 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
936 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
939 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
944 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
946 if (pos
!= bmap
->n_eq
- 1) {
948 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
949 bmap
->eq
[bmap
->n_eq
- 1] = t
;
955 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
957 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
960 void isl_basic_map_inequality_to_equality(
961 struct isl_basic_map
*bmap
, unsigned pos
)
966 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
967 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
972 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
973 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
974 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
975 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
978 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
980 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
983 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
989 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
990 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
991 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
992 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
993 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
994 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
995 1 + isl_basic_map_total_dim(bmap
),
996 bmap
->extra
- bmap
->n_div
);
997 return bmap
->n_ineq
++;
1000 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1002 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1005 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1009 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1014 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1016 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1019 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1024 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1026 if (pos
!= bmap
->n_ineq
- 1) {
1027 t
= bmap
->ineq
[pos
];
1028 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1029 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1030 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1036 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1038 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1041 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1046 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1049 k
= isl_basic_map_alloc_equality(bmap
);
1052 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1055 isl_basic_map_free(bmap
);
1059 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1062 return (isl_basic_set
*)
1063 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1066 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1071 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1074 k
= isl_basic_map_alloc_inequality(bmap
);
1077 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1080 isl_basic_map_free(bmap
);
1084 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1087 return (isl_basic_set
*)
1088 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1091 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1095 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1096 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1097 1 + 1 + isl_basic_map_total_dim(bmap
),
1098 bmap
->extra
- bmap
->n_div
);
1099 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1100 return bmap
->n_div
++;
1103 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1105 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1108 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1112 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1117 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1119 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1122 /* Copy constraint from src to dst, putting the vars of src at offset
1123 * dim_off in dst and the divs of src at offset div_off in dst.
1124 * If both sets are actually map, then dim_off applies to the input
1127 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1128 struct isl_basic_map
*src_map
, isl_int
*src
,
1129 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1131 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1132 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1133 unsigned src_in
= isl_basic_map_n_in(src_map
);
1134 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1135 unsigned src_out
= isl_basic_map_n_out(src_map
);
1136 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1137 isl_int_set(dst
[0], src
[0]);
1138 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1139 if (dst_nparam
> src_nparam
)
1140 isl_seq_clr(dst
+1+src_nparam
,
1141 dst_nparam
- src_nparam
);
1142 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1143 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1145 isl_min(dst_in
-in_off
, src_in
));
1146 if (dst_in
-in_off
> src_in
)
1147 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1148 dst_in
- in_off
- src_in
);
1149 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1150 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1151 src
+1+src_nparam
+src_in
,
1152 isl_min(dst_out
-out_off
, src_out
));
1153 if (dst_out
-out_off
> src_out
)
1154 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1155 dst_out
- out_off
- src_out
);
1156 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1157 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1158 src
+1+src_nparam
+src_in
+src_out
,
1159 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1160 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1161 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1162 div_off
+src_map
->n_div
,
1163 dst_map
->n_div
- div_off
- src_map
->n_div
);
1166 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1167 struct isl_basic_map
*src_map
, isl_int
*src
,
1168 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1170 isl_int_set(dst
[0], src
[0]);
1171 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1174 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1175 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1180 if (!bmap1
|| !bmap2
)
1183 div_off
= bmap1
->n_div
;
1185 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1186 int i1
= isl_basic_map_alloc_equality(bmap1
);
1189 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1190 i_pos
, o_pos
, div_off
);
1193 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1194 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1197 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1198 i_pos
, o_pos
, div_off
);
1201 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1202 int i1
= isl_basic_map_alloc_div(bmap1
);
1205 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1206 i_pos
, o_pos
, div_off
);
1209 isl_basic_map_free(bmap2
);
1214 isl_basic_map_free(bmap1
);
1215 isl_basic_map_free(bmap2
);
1219 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1220 struct isl_basic_set
*bset2
, unsigned pos
)
1222 return (struct isl_basic_set
*)
1223 add_constraints((struct isl_basic_map
*)bset1
,
1224 (struct isl_basic_map
*)bset2
, 0, pos
);
1227 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1228 __isl_take isl_space
*dim
, unsigned extra
,
1229 unsigned n_eq
, unsigned n_ineq
)
1231 struct isl_basic_map
*ext
;
1241 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1242 base
->extra
>= base
->n_div
+ extra
;
1244 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1245 room_for_ineq(base
, n_ineq
)) {
1246 isl_space_free(dim
);
1250 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1251 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1252 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1253 extra
+= base
->extra
;
1255 n_ineq
+= base
->n_ineq
;
1257 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1263 ext
->sample
= isl_vec_copy(base
->sample
);
1264 flags
= base
->flags
;
1265 ext
= add_constraints(ext
, base
, 0, 0);
1268 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1274 isl_space_free(dim
);
1275 isl_basic_map_free(base
);
1279 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1280 __isl_take isl_space
*dim
, unsigned extra
,
1281 unsigned n_eq
, unsigned n_ineq
)
1283 return (struct isl_basic_set
*)
1284 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1285 extra
, n_eq
, n_ineq
);
1288 struct isl_basic_map
*isl_basic_map_extend_constraints(
1289 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1293 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1297 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1298 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1299 unsigned n_eq
, unsigned n_ineq
)
1301 struct isl_basic_map
*bmap
;
1306 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1310 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1313 isl_basic_map_free(base
);
1317 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1318 unsigned nparam
, unsigned dim
, unsigned extra
,
1319 unsigned n_eq
, unsigned n_ineq
)
1321 return (struct isl_basic_set
*)
1322 isl_basic_map_extend((struct isl_basic_map
*)base
,
1323 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1326 struct isl_basic_set
*isl_basic_set_extend_constraints(
1327 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1329 return (struct isl_basic_set
*)
1330 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1334 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1336 return (struct isl_basic_set
*)
1337 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1340 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1345 if (bmap
->ref
> 1) {
1347 bmap
= isl_basic_map_dup(bmap
);
1350 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1354 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1362 return isl_set_dup(set
);
1365 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1373 return isl_map_dup(map
);
1376 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1377 unsigned a_len
, unsigned b_len
)
1379 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1380 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1381 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1384 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1385 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1393 isl_assert(bmap
->ctx
,
1394 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1396 if (n1
== 0 || n2
== 0)
1399 bmap
= isl_basic_map_cow(bmap
);
1403 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1404 if (isl_blk_is_error(blk
))
1407 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1409 bmap
->eq
[i
] + pos
, n1
, n2
);
1411 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1413 bmap
->ineq
[i
] + pos
, n1
, n2
);
1415 for (i
= 0; i
< bmap
->n_div
; ++i
)
1417 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1419 isl_blk_free(bmap
->ctx
, blk
);
1421 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1422 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1423 return isl_basic_map_finalize(bmap
);
1425 isl_basic_map_free(bmap
);
1429 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1430 __isl_take isl_basic_set
*bset
, unsigned n
)
1435 nparam
= isl_basic_set_n_param(bset
);
1436 dim
= isl_basic_set_n_dim(bset
);
1437 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1439 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1441 isl_basic_set_free(bset
);
1445 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1451 total
= isl_basic_map_total_dim(bmap
);
1452 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1453 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1455 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1457 i
= isl_basic_map_alloc_equality(bmap
);
1461 isl_int_set_si(bmap
->eq
[i
][0], 1);
1462 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1463 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1464 isl_vec_free(bmap
->sample
);
1465 bmap
->sample
= NULL
;
1466 return isl_basic_map_finalize(bmap
);
1468 isl_basic_map_free(bmap
);
1472 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1474 return (struct isl_basic_set
*)
1475 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1478 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1481 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1482 isl_int
*t
= bmap
->div
[a
];
1483 bmap
->div
[a
] = bmap
->div
[b
];
1486 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1487 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1489 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1490 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1492 for (i
= 0; i
< bmap
->n_div
; ++i
)
1493 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1494 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1497 /* Eliminate the specified n dimensions starting at first from the
1498 * constraints using Fourier-Motzkin. The dimensions themselves
1501 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1502 enum isl_dim_type type
, unsigned first
, unsigned n
)
1511 map
= isl_map_cow(map
);
1514 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1515 first
+= pos(map
->dim
, type
) - 1;
1517 for (i
= 0; i
< map
->n
; ++i
) {
1518 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1528 /* Eliminate the specified n dimensions starting at first from the
1529 * constraints using Fourier-Motzkin. The dimensions themselves
1532 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1533 enum isl_dim_type type
, unsigned first
, unsigned n
)
1535 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1538 /* Eliminate the specified n dimensions starting at first from the
1539 * constraints using Fourier-Motzkin. The dimensions themselves
1542 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1543 unsigned first
, unsigned n
)
1545 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1548 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1549 __isl_take isl_basic_map
*bmap
)
1553 bmap
= isl_basic_map_eliminate_vars(bmap
,
1554 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1558 return isl_basic_map_finalize(bmap
);
1561 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1562 __isl_take isl_basic_set
*bset
)
1564 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1565 (struct isl_basic_map
*)bset
);
1568 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1577 map
= isl_map_cow(map
);
1581 for (i
= 0; i
< map
->n
; ++i
) {
1582 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1592 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1594 return isl_map_remove_divs(set
);
1597 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1598 enum isl_dim_type type
, unsigned first
, unsigned n
)
1602 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1604 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1606 bmap
= isl_basic_map_eliminate_vars(bmap
,
1607 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1610 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1612 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1615 isl_basic_map_free(bmap
);
1619 /* Return true if the definition of the given div (recursively) involves
1620 * any of the given variables.
1622 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1623 unsigned first
, unsigned n
)
1626 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1628 if (isl_int_is_zero(bmap
->div
[div
][0]))
1630 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1633 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1634 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1636 if (div_involves_vars(bmap
, i
, first
, n
))
1643 /* Remove all divs (recursively) involving any of the given dimensions
1644 * in their definitions.
1646 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1647 __isl_take isl_basic_map
*bmap
,
1648 enum isl_dim_type type
, unsigned first
, unsigned n
)
1654 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1656 first
+= isl_basic_map_offset(bmap
, type
);
1658 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1659 if (!div_involves_vars(bmap
, i
, first
, n
))
1661 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1669 isl_basic_map_free(bmap
);
1673 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1674 enum isl_dim_type type
, unsigned first
, unsigned n
)
1683 map
= isl_map_cow(map
);
1687 for (i
= 0; i
< map
->n
; ++i
) {
1688 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1699 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1700 enum isl_dim_type type
, unsigned first
, unsigned n
)
1702 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1706 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1707 enum isl_dim_type type
, unsigned first
, unsigned n
)
1714 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1715 isl_die(bmap
->ctx
, isl_error_invalid
,
1716 "index out of bounds", return -1);
1718 first
+= isl_basic_map_offset(bmap
, type
);
1719 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1720 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1722 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1723 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1729 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1730 enum isl_dim_type type
, unsigned first
, unsigned n
)
1737 if (first
+ n
> isl_map_dim(map
, type
))
1738 isl_die(map
->ctx
, isl_error_invalid
,
1739 "index out of bounds", return -1);
1741 for (i
= 0; i
< map
->n
; ++i
) {
1742 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1744 if (involves
< 0 || !involves
)
1751 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1752 enum isl_dim_type type
, unsigned first
, unsigned n
)
1754 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1757 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1758 enum isl_dim_type type
, unsigned first
, unsigned n
)
1760 return isl_map_involves_dims(set
, type
, first
, n
);
1763 /* Return true if the definition of the given div is unknown or depends
1766 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1769 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1771 if (isl_int_is_zero(bmap
->div
[div
][0]))
1774 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1775 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1777 if (div_is_unknown(bmap
, i
))
1784 /* Remove all divs that are unknown or defined in terms of unknown divs.
1786 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1787 __isl_take isl_basic_map
*bmap
)
1794 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1795 if (!div_is_unknown(bmap
, i
))
1797 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1803 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1812 map
= isl_map_cow(map
);
1816 for (i
= 0; i
< map
->n
; ++i
) {
1817 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1827 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1829 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1832 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1833 __isl_take isl_basic_set
*bset
,
1834 enum isl_dim_type type
, unsigned first
, unsigned n
)
1836 return (isl_basic_set
*)
1837 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1840 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1841 enum isl_dim_type type
, unsigned first
, unsigned n
)
1848 map
= isl_map_cow(map
);
1851 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1853 for (i
= 0; i
< map
->n
; ++i
) {
1854 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1855 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1859 map
= isl_map_drop(map
, type
, first
, n
);
1866 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1867 enum isl_dim_type type
, unsigned first
, unsigned n
)
1869 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1872 /* Project out n inputs starting at first using Fourier-Motzkin */
1873 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1874 unsigned first
, unsigned n
)
1876 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1879 static void dump_term(struct isl_basic_map
*bmap
,
1880 isl_int c
, int pos
, FILE *out
)
1883 unsigned in
= isl_basic_map_n_in(bmap
);
1884 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1885 unsigned nparam
= isl_basic_map_n_param(bmap
);
1887 isl_int_print(out
, c
, 0);
1889 if (!isl_int_is_one(c
))
1890 isl_int_print(out
, c
, 0);
1891 if (pos
< 1 + nparam
) {
1892 name
= isl_space_get_dim_name(bmap
->dim
,
1893 isl_dim_param
, pos
- 1);
1895 fprintf(out
, "%s", name
);
1897 fprintf(out
, "p%d", pos
- 1);
1898 } else if (pos
< 1 + nparam
+ in
)
1899 fprintf(out
, "i%d", pos
- 1 - nparam
);
1900 else if (pos
< 1 + nparam
+ dim
)
1901 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1903 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1907 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1908 int sign
, FILE *out
)
1912 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1916 for (i
= 0, first
= 1; i
< len
; ++i
) {
1917 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1920 fprintf(out
, " + ");
1922 isl_int_abs(v
, c
[i
]);
1923 dump_term(bmap
, v
, i
, out
);
1930 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1931 const char *op
, FILE *out
, int indent
)
1935 fprintf(out
, "%*s", indent
, "");
1937 dump_constraint_sign(bmap
, c
, 1, out
);
1938 fprintf(out
, " %s ", op
);
1939 dump_constraint_sign(bmap
, c
, -1, out
);
1943 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1944 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
1946 fprintf(out
, "%*s", indent
, "");
1947 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1952 static void dump_constraints(struct isl_basic_map
*bmap
,
1953 isl_int
**c
, unsigned n
,
1954 const char *op
, FILE *out
, int indent
)
1958 for (i
= 0; i
< n
; ++i
)
1959 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1962 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1966 unsigned total
= isl_basic_map_total_dim(bmap
);
1968 for (j
= 0; j
< 1 + total
; ++j
) {
1969 if (isl_int_is_zero(exp
[j
]))
1971 if (!first
&& isl_int_is_pos(exp
[j
]))
1973 dump_term(bmap
, exp
[j
], j
, out
);
1978 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1982 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1983 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1985 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1986 fprintf(out
, "%*s", indent
, "");
1987 fprintf(out
, "e%d = [(", i
);
1988 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1990 isl_int_print(out
, bmap
->div
[i
][0], 0);
1991 fprintf(out
, "]\n");
1995 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1996 FILE *out
, int indent
)
1999 fprintf(out
, "null basic set\n");
2003 fprintf(out
, "%*s", indent
, "");
2004 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2005 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2006 bset
->extra
, bset
->flags
);
2007 dump((struct isl_basic_map
*)bset
, out
, indent
);
2010 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2011 FILE *out
, int indent
)
2014 fprintf(out
, "null basic map\n");
2018 fprintf(out
, "%*s", indent
, "");
2019 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2020 "flags: %x, n_name: %d\n",
2022 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2023 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2024 dump(bmap
, out
, indent
);
2027 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2032 total
= isl_basic_map_total_dim(bmap
);
2033 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2034 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2035 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2036 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2040 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2043 struct isl_set
*set
;
2047 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2048 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2049 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2050 sizeof(struct isl_set
) +
2051 (n
- 1) * sizeof(struct isl_basic_set
*));
2055 set
->ctx
= dim
->ctx
;
2056 isl_ctx_ref(set
->ctx
);
2064 isl_space_free(dim
);
2068 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2069 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2071 struct isl_set
*set
;
2074 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2078 set
= isl_set_alloc_space(dims
, n
, flags
);
2082 /* Make sure "map" has room for at least "n" more basic maps.
2084 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2087 struct isl_map
*grown
= NULL
;
2091 isl_assert(map
->ctx
, n
>= 0, goto error
);
2092 if (map
->n
+ n
<= map
->size
)
2094 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2097 for (i
= 0; i
< map
->n
; ++i
) {
2098 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2106 isl_map_free(grown
);
2111 /* Make sure "set" has room for at least "n" more basic sets.
2113 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2115 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2118 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2121 struct isl_set
*dup
;
2126 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2129 for (i
= 0; i
< set
->n
; ++i
)
2130 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2134 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2136 return isl_map_from_basic_map(bset
);
2139 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2141 struct isl_map
*map
;
2146 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2147 return isl_map_add_basic_map(map
, bmap
);
2150 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2151 __isl_take isl_basic_set
*bset
)
2153 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2154 (struct isl_basic_map
*)bset
);
2157 void isl_set_free(struct isl_set
*set
)
2167 isl_ctx_deref(set
->ctx
);
2168 for (i
= 0; i
< set
->n
; ++i
)
2169 isl_basic_set_free(set
->p
[i
]);
2170 isl_space_free(set
->dim
);
2174 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2179 fprintf(out
, "null set\n");
2183 fprintf(out
, "%*s", indent
, "");
2184 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2185 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2187 for (i
= 0; i
< set
->n
; ++i
) {
2188 fprintf(out
, "%*s", indent
, "");
2189 fprintf(out
, "basic set %d:\n", i
);
2190 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2194 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2199 fprintf(out
, "null map\n");
2203 fprintf(out
, "%*s", indent
, "");
2204 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2205 "flags: %x, n_name: %d\n",
2206 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2207 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2208 for (i
= 0; i
< map
->n
; ++i
) {
2209 fprintf(out
, "%*s", indent
, "");
2210 fprintf(out
, "basic map %d:\n", i
);
2211 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2215 struct isl_basic_map
*isl_basic_map_intersect_domain(
2216 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2218 struct isl_basic_map
*bmap_domain
;
2223 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2224 bset
->dim
, isl_dim_param
), goto error
);
2226 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2227 isl_assert(bset
->ctx
,
2228 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2230 bmap
= isl_basic_map_cow(bmap
);
2233 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2234 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2235 bmap_domain
= isl_basic_map_from_domain(bset
);
2236 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2238 bmap
= isl_basic_map_simplify(bmap
);
2239 return isl_basic_map_finalize(bmap
);
2241 isl_basic_map_free(bmap
);
2242 isl_basic_set_free(bset
);
2246 struct isl_basic_map
*isl_basic_map_intersect_range(
2247 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2249 struct isl_basic_map
*bmap_range
;
2254 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2255 bset
->dim
, isl_dim_param
), goto error
);
2257 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2258 isl_assert(bset
->ctx
,
2259 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2261 if (isl_basic_set_is_universe(bset
)) {
2262 isl_basic_set_free(bset
);
2266 bmap
= isl_basic_map_cow(bmap
);
2269 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2270 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2271 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2272 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2274 bmap
= isl_basic_map_simplify(bmap
);
2275 return isl_basic_map_finalize(bmap
);
2277 isl_basic_map_free(bmap
);
2278 isl_basic_set_free(bset
);
2282 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2288 total
= 1 + isl_basic_map_total_dim(bmap
);
2289 if (total
!= vec
->size
)
2294 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2295 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2296 if (!isl_int_is_zero(s
)) {
2302 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2303 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2304 if (isl_int_is_neg(s
)) {
2315 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2317 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2320 struct isl_basic_map
*isl_basic_map_intersect(
2321 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2323 struct isl_vec
*sample
= NULL
;
2325 if (!bmap1
|| !bmap2
)
2328 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2329 bmap2
->dim
, isl_dim_param
), goto error
);
2330 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2331 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2332 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2333 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2334 return isl_basic_map_intersect(bmap2
, bmap1
);
2336 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2337 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2338 isl_assert(bmap1
->ctx
,
2339 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2341 if (bmap1
->sample
&&
2342 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2343 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2344 sample
= isl_vec_copy(bmap1
->sample
);
2345 else if (bmap2
->sample
&&
2346 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2347 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2348 sample
= isl_vec_copy(bmap2
->sample
);
2350 bmap1
= isl_basic_map_cow(bmap1
);
2353 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2354 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2355 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2358 isl_vec_free(sample
);
2360 isl_vec_free(bmap1
->sample
);
2361 bmap1
->sample
= sample
;
2364 bmap1
= isl_basic_map_simplify(bmap1
);
2365 return isl_basic_map_finalize(bmap1
);
2368 isl_vec_free(sample
);
2369 isl_basic_map_free(bmap1
);
2370 isl_basic_map_free(bmap2
);
2374 struct isl_basic_set
*isl_basic_set_intersect(
2375 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2377 return (struct isl_basic_set
*)
2378 isl_basic_map_intersect(
2379 (struct isl_basic_map
*)bset1
,
2380 (struct isl_basic_map
*)bset2
);
2383 /* Special case of isl_map_intersect, where both map1 and map2
2384 * are convex, without any divs and such that either map1 or map2
2385 * contains a single constraint. This constraint is then simply
2386 * added to the other map.
2388 static __isl_give isl_map
*map_intersect_add_constraint(
2389 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2391 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2392 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2393 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2394 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2396 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2397 return isl_map_intersect(map2
, map1
);
2399 isl_assert(map2
->ctx
,
2400 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2402 map1
= isl_map_cow(map1
);
2405 if (isl_map_plain_is_empty(map1
)) {
2409 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2410 if (map2
->p
[0]->n_eq
== 1)
2411 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2413 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2414 map2
->p
[0]->ineq
[0]);
2416 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2417 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2421 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2422 isl_basic_map_free(map1
->p
[0]);
2435 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2436 __isl_take isl_map
*map2
)
2439 struct isl_map
*result
;
2445 if (isl_map_plain_is_empty(map1
) &&
2446 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2450 if (isl_map_plain_is_empty(map2
) &&
2451 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2456 if (map1
->n
== 1 && map2
->n
== 1 &&
2457 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2458 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2459 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2460 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2461 return map_intersect_add_constraint(map1
, map2
);
2462 if (isl_space_dim(map1
->dim
, isl_dim_all
) ==
2463 isl_space_dim(map1
->dim
, isl_dim_param
) &&
2464 isl_space_dim(map2
->dim
, isl_dim_all
) !=
2465 isl_space_dim(map2
->dim
, isl_dim_param
))
2466 return isl_map_intersect(map2
, map1
);
2468 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2469 isl_space_dim(map2
->dim
, isl_dim_param
))
2470 isl_assert(map1
->ctx
,
2471 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2473 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2474 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2475 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2477 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2478 map1
->n
* map2
->n
, flags
);
2481 for (i
= 0; i
< map1
->n
; ++i
)
2482 for (j
= 0; j
< map2
->n
; ++j
) {
2483 struct isl_basic_map
*part
;
2484 part
= isl_basic_map_intersect(
2485 isl_basic_map_copy(map1
->p
[i
]),
2486 isl_basic_map_copy(map2
->p
[j
]));
2487 if (isl_basic_map_is_empty(part
))
2488 isl_basic_map_free(part
);
2490 result
= isl_map_add_basic_map(result
, part
);
2503 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2504 __isl_take isl_map
*map2
)
2506 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2509 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2511 return (struct isl_set
*)
2512 isl_map_intersect((struct isl_map
*)set1
,
2513 (struct isl_map
*)set2
);
2516 /* The current implementation of isl_map_intersect accepts intersections
2517 * with parameter domains, so we can just call that for now.
2519 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2520 __isl_take isl_set
*params
)
2522 return isl_map_intersect(map
, params
);
2525 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2526 __isl_take isl_map
*map2
)
2528 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2531 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2532 __isl_take isl_set
*params
)
2534 return isl_map_intersect_params(set
, params
);
2537 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2540 struct isl_basic_set
*bset
;
2545 bmap
= isl_basic_map_cow(bmap
);
2548 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2549 in
= isl_basic_map_n_in(bmap
);
2550 bset
= isl_basic_set_from_basic_map(bmap
);
2551 bset
= isl_basic_set_swap_vars(bset
, in
);
2552 return isl_basic_map_from_basic_set(bset
, dim
);
2555 static __isl_give isl_basic_map
*basic_map_space_reset(
2556 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2560 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2563 space
= isl_basic_map_get_space(bmap
);
2564 space
= isl_space_reset(space
, type
);
2565 bmap
= isl_basic_map_reset_space(bmap
, space
);
2569 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2570 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2573 struct isl_basic_map
*res
;
2574 struct isl_dim_map
*dim_map
;
2575 unsigned total
, off
;
2576 enum isl_dim_type t
;
2579 return basic_map_space_reset(bmap
, type
);
2584 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2586 total
= isl_basic_map_total_dim(bmap
) + n
;
2587 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2589 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2591 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2593 unsigned size
= isl_basic_map_dim(bmap
, t
);
2594 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2596 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2597 pos
, size
- pos
, off
+ pos
+ n
);
2599 off
+= isl_space_dim(res_dim
, t
);
2601 isl_dim_map_div(dim_map
, bmap
, off
);
2603 res
= isl_basic_map_alloc_space(res_dim
,
2604 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2605 if (isl_basic_map_is_rational(bmap
))
2606 res
= isl_basic_map_set_rational(res
);
2607 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2608 return isl_basic_map_finalize(res
);
2611 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2612 enum isl_dim_type type
, unsigned n
)
2616 return isl_basic_map_insert(bmap
, type
,
2617 isl_basic_map_dim(bmap
, type
), n
);
2620 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2621 enum isl_dim_type type
, unsigned n
)
2625 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2626 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2628 isl_basic_set_free(bset
);
2632 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2633 enum isl_dim_type type
)
2637 if (!isl_space_is_named_or_nested(map
->dim
, type
))
2640 space
= isl_map_get_space(map
);
2641 space
= isl_space_reset(space
, type
);
2642 map
= isl_map_reset_space(map
, space
);
2646 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2647 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2652 return map_space_reset(map
, type
);
2654 map
= isl_map_cow(map
);
2658 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2662 for (i
= 0; i
< map
->n
; ++i
) {
2663 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2674 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2675 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2677 return isl_map_insert_dims(set
, type
, pos
, n
);
2680 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2681 enum isl_dim_type type
, unsigned n
)
2685 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2688 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2689 enum isl_dim_type type
, unsigned n
)
2693 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2694 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2700 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2701 __isl_take isl_basic_map
*bmap
,
2702 enum isl_dim_type dst_type
, unsigned dst_pos
,
2703 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2705 struct isl_dim_map
*dim_map
;
2706 struct isl_basic_map
*res
;
2707 enum isl_dim_type t
;
2708 unsigned total
, off
;
2715 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2718 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2721 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2723 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2724 pos(bmap
->dim
, src_type
) + src_pos
+
2725 ((src_type
< dst_type
) ? n
: 0)) {
2726 bmap
= isl_basic_map_cow(bmap
);
2730 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2731 src_type
, src_pos
, n
);
2735 bmap
= isl_basic_map_finalize(bmap
);
2740 total
= isl_basic_map_total_dim(bmap
);
2741 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2744 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2745 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2746 if (t
== dst_type
) {
2747 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2750 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2753 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2754 dst_pos
, size
- dst_pos
, off
);
2755 off
+= size
- dst_pos
;
2756 } else if (t
== src_type
) {
2757 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2760 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2761 src_pos
+ n
, size
- src_pos
- n
, off
);
2762 off
+= size
- src_pos
- n
;
2764 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2768 isl_dim_map_div(dim_map
, bmap
, off
);
2770 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2771 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2772 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2774 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2775 src_type
, src_pos
, n
);
2779 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2780 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2781 bmap
= isl_basic_map_finalize(bmap
);
2785 isl_basic_map_free(bmap
);
2789 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2790 enum isl_dim_type dst_type
, unsigned dst_pos
,
2791 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2793 return (isl_basic_set
*)isl_basic_map_move_dims(
2794 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2797 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2798 enum isl_dim_type dst_type
, unsigned dst_pos
,
2799 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2803 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2804 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2805 src_type
, src_pos
, n
);
2811 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2812 enum isl_dim_type dst_type
, unsigned dst_pos
,
2813 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2822 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2825 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2828 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2830 map
= isl_map_cow(map
);
2834 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2838 for (i
= 0; i
< map
->n
; ++i
) {
2839 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2841 src_type
, src_pos
, n
);
2852 /* Move the specified dimensions to the last columns right before
2853 * the divs. Don't change the dimension specification of bmap.
2854 * That's the responsibility of the caller.
2856 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2857 enum isl_dim_type type
, unsigned first
, unsigned n
)
2859 struct isl_dim_map
*dim_map
;
2860 struct isl_basic_map
*res
;
2861 enum isl_dim_type t
;
2862 unsigned total
, off
;
2866 if (pos(bmap
->dim
, type
) + first
+ n
==
2867 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2870 total
= isl_basic_map_total_dim(bmap
);
2871 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2874 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2875 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2877 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2880 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2881 first
, n
, total
- bmap
->n_div
- n
);
2882 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2883 first
+ n
, size
- (first
+ n
), off
);
2884 off
+= size
- (first
+ n
);
2886 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2890 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2892 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2893 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2894 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2898 /* Turn the n dimensions of type type, starting at first
2899 * into existentially quantified variables.
2901 __isl_give isl_basic_map
*isl_basic_map_project_out(
2902 __isl_take isl_basic_map
*bmap
,
2903 enum isl_dim_type type
, unsigned first
, unsigned n
)
2916 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2917 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2919 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2922 bmap
= move_last(bmap
, type
, first
, n
);
2923 bmap
= isl_basic_map_cow(bmap
);
2927 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
2928 old
= bmap
->block2
.data
;
2929 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2930 (bmap
->extra
+ n
) * (1 + row_size
));
2931 if (!bmap
->block2
.data
)
2933 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2936 for (i
= 0; i
< n
; ++i
) {
2937 new_div
[i
] = bmap
->block2
.data
+
2938 (bmap
->extra
+ i
) * (1 + row_size
);
2939 isl_seq_clr(new_div
[i
], 1 + row_size
);
2941 for (i
= 0; i
< bmap
->extra
; ++i
)
2942 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2944 bmap
->div
= new_div
;
2948 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2951 bmap
= isl_basic_map_simplify(bmap
);
2952 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2953 return isl_basic_map_finalize(bmap
);
2955 isl_basic_map_free(bmap
);
2959 /* Turn the n dimensions of type type, starting at first
2960 * into existentially quantified variables.
2962 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2963 enum isl_dim_type type
, unsigned first
, unsigned n
)
2965 return (isl_basic_set
*)isl_basic_map_project_out(
2966 (isl_basic_map
*)bset
, type
, first
, n
);
2969 /* Turn the n dimensions of type type, starting at first
2970 * into existentially quantified variables.
2972 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2973 enum isl_dim_type type
, unsigned first
, unsigned n
)
2983 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2985 map
= isl_map_cow(map
);
2989 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2993 for (i
= 0; i
< map
->n
; ++i
) {
2994 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3005 /* Turn the n dimensions of type type, starting at first
3006 * into existentially quantified variables.
3008 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3009 enum isl_dim_type type
, unsigned first
, unsigned n
)
3011 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3014 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3018 for (i
= 0; i
< n
; ++i
) {
3019 j
= isl_basic_map_alloc_div(bmap
);
3022 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3026 isl_basic_map_free(bmap
);
3030 struct isl_basic_map
*isl_basic_map_apply_range(
3031 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3033 isl_space
*dim_result
= NULL
;
3034 struct isl_basic_map
*bmap
;
3035 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3036 struct isl_dim_map
*dim_map1
, *dim_map2
;
3038 if (!bmap1
|| !bmap2
)
3041 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3042 isl_space_copy(bmap2
->dim
));
3044 n_in
= isl_basic_map_n_in(bmap1
);
3045 n_out
= isl_basic_map_n_out(bmap2
);
3046 n
= isl_basic_map_n_out(bmap1
);
3047 nparam
= isl_basic_map_n_param(bmap1
);
3049 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3050 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3051 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3052 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3053 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3054 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3055 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3056 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3057 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3058 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3059 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3061 bmap
= isl_basic_map_alloc_space(dim_result
,
3062 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3063 bmap1
->n_eq
+ bmap2
->n_eq
,
3064 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3065 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3066 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3067 bmap
= add_divs(bmap
, n
);
3068 bmap
= isl_basic_map_simplify(bmap
);
3069 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3070 return isl_basic_map_finalize(bmap
);
3072 isl_basic_map_free(bmap1
);
3073 isl_basic_map_free(bmap2
);
3077 struct isl_basic_set
*isl_basic_set_apply(
3078 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3083 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3086 return (struct isl_basic_set
*)
3087 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3089 isl_basic_set_free(bset
);
3090 isl_basic_map_free(bmap
);
3094 struct isl_basic_map
*isl_basic_map_apply_domain(
3095 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3097 if (!bmap1
|| !bmap2
)
3100 isl_assert(bmap1
->ctx
,
3101 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3102 isl_assert(bmap1
->ctx
,
3103 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3106 bmap1
= isl_basic_map_reverse(bmap1
);
3107 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3108 return isl_basic_map_reverse(bmap1
);
3110 isl_basic_map_free(bmap1
);
3111 isl_basic_map_free(bmap2
);
3115 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3116 * A \cap B -> f(A) + f(B)
3118 struct isl_basic_map
*isl_basic_map_sum(
3119 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3121 unsigned n_in
, n_out
, nparam
, total
, pos
;
3122 struct isl_basic_map
*bmap
= NULL
;
3123 struct isl_dim_map
*dim_map1
, *dim_map2
;
3126 if (!bmap1
|| !bmap2
)
3129 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3132 nparam
= isl_basic_map_n_param(bmap1
);
3133 n_in
= isl_basic_map_n_in(bmap1
);
3134 n_out
= isl_basic_map_n_out(bmap1
);
3136 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3137 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3138 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3139 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3140 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3141 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3142 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3143 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3144 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3145 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3146 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3148 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3149 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3150 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3151 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3152 for (i
= 0; i
< n_out
; ++i
) {
3153 int j
= isl_basic_map_alloc_equality(bmap
);
3156 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3157 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3158 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3159 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3161 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3162 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3163 bmap
= add_divs(bmap
, 2 * n_out
);
3165 bmap
= isl_basic_map_simplify(bmap
);
3166 return isl_basic_map_finalize(bmap
);
3168 isl_basic_map_free(bmap
);
3169 isl_basic_map_free(bmap1
);
3170 isl_basic_map_free(bmap2
);
3174 /* Given two maps A -> f(A) and B -> g(B), construct a map
3175 * A \cap B -> f(A) + f(B)
3177 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3179 struct isl_map
*result
;
3185 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3187 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3188 map1
->n
* map2
->n
, 0);
3191 for (i
= 0; i
< map1
->n
; ++i
)
3192 for (j
= 0; j
< map2
->n
; ++j
) {
3193 struct isl_basic_map
*part
;
3194 part
= isl_basic_map_sum(
3195 isl_basic_map_copy(map1
->p
[i
]),
3196 isl_basic_map_copy(map2
->p
[j
]));
3197 if (isl_basic_map_is_empty(part
))
3198 isl_basic_map_free(part
);
3200 result
= isl_map_add_basic_map(result
, part
);
3213 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3214 __isl_take isl_set
*set2
)
3216 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3219 /* Given a basic map A -> f(A), construct A -> -f(A).
3221 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3226 bmap
= isl_basic_map_cow(bmap
);
3230 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3231 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3232 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3233 for (j
= 0; j
< n
; ++j
)
3234 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3235 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3236 for (j
= 0; j
< n
; ++j
)
3237 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3238 for (i
= 0; i
< bmap
->n_div
; ++i
)
3239 for (j
= 0; j
< n
; ++j
)
3240 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3241 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3242 return isl_basic_map_finalize(bmap
);
3245 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3247 return isl_basic_map_neg(bset
);
3250 /* Given a map A -> f(A), construct A -> -f(A).
3252 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3256 map
= isl_map_cow(map
);
3260 for (i
= 0; i
< map
->n
; ++i
) {
3261 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3272 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3274 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3277 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3278 * A -> floor(f(A)/d).
3280 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3283 unsigned n_in
, n_out
, nparam
, total
, pos
;
3284 struct isl_basic_map
*result
= NULL
;
3285 struct isl_dim_map
*dim_map
;
3291 nparam
= isl_basic_map_n_param(bmap
);
3292 n_in
= isl_basic_map_n_in(bmap
);
3293 n_out
= isl_basic_map_n_out(bmap
);
3295 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3296 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3297 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3298 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3299 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3300 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3302 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3303 bmap
->n_div
+ n_out
,
3304 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3305 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3306 result
= add_divs(result
, n_out
);
3307 for (i
= 0; i
< n_out
; ++i
) {
3309 j
= isl_basic_map_alloc_inequality(result
);
3312 isl_seq_clr(result
->ineq
[j
], 1+total
);
3313 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3314 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3315 j
= isl_basic_map_alloc_inequality(result
);
3318 isl_seq_clr(result
->ineq
[j
], 1+total
);
3319 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3320 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3321 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3324 result
= isl_basic_map_simplify(result
);
3325 return isl_basic_map_finalize(result
);
3327 isl_basic_map_free(result
);
3331 /* Given a map A -> f(A) and an integer d, construct a map
3332 * A -> floor(f(A)/d).
3334 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3338 map
= isl_map_cow(map
);
3342 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3343 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3344 for (i
= 0; i
< map
->n
; ++i
) {
3345 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3356 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3362 i
= isl_basic_map_alloc_equality(bmap
);
3365 nparam
= isl_basic_map_n_param(bmap
);
3366 n_in
= isl_basic_map_n_in(bmap
);
3367 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3368 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3369 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3370 return isl_basic_map_finalize(bmap
);
3372 isl_basic_map_free(bmap
);
3376 /* Add a constraints to "bmap" expressing i_pos < o_pos
3378 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3384 i
= isl_basic_map_alloc_inequality(bmap
);
3387 nparam
= isl_basic_map_n_param(bmap
);
3388 n_in
= isl_basic_map_n_in(bmap
);
3389 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3390 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3391 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3392 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3393 return isl_basic_map_finalize(bmap
);
3395 isl_basic_map_free(bmap
);
3399 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3401 static __isl_give isl_basic_map
*var_less_or_equal(
3402 __isl_take 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
][1+nparam
+pos
], -1);
3415 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3416 return isl_basic_map_finalize(bmap
);
3418 isl_basic_map_free(bmap
);
3422 /* Add a constraints to "bmap" expressing i_pos > o_pos
3424 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3430 i
= isl_basic_map_alloc_inequality(bmap
);
3433 nparam
= isl_basic_map_n_param(bmap
);
3434 n_in
= isl_basic_map_n_in(bmap
);
3435 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3436 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3437 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3438 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3439 return isl_basic_map_finalize(bmap
);
3441 isl_basic_map_free(bmap
);
3445 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3447 static __isl_give isl_basic_map
*var_more_or_equal(
3448 __isl_take 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
][1+nparam
+pos
], 1);
3461 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3462 return isl_basic_map_finalize(bmap
);
3464 isl_basic_map_free(bmap
);
3468 __isl_give isl_basic_map
*isl_basic_map_equal(
3469 __isl_take isl_space
*dim
, unsigned n_equal
)
3472 struct isl_basic_map
*bmap
;
3473 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3476 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3477 bmap
= var_equal(bmap
, i
);
3478 return isl_basic_map_finalize(bmap
);
3481 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3483 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3487 struct isl_basic_map
*bmap
;
3488 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3491 for (i
= 0; i
< pos
&& bmap
; ++i
)
3492 bmap
= var_equal(bmap
, i
);
3494 bmap
= var_less(bmap
, pos
);
3495 return isl_basic_map_finalize(bmap
);
3498 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3500 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3501 __isl_take isl_space
*dim
, unsigned pos
)
3504 isl_basic_map
*bmap
;
3506 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3507 for (i
= 0; i
< pos
; ++i
)
3508 bmap
= var_equal(bmap
, i
);
3509 bmap
= var_less_or_equal(bmap
, pos
);
3510 return isl_basic_map_finalize(bmap
);
3513 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3515 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3519 struct isl_basic_map
*bmap
;
3520 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3523 for (i
= 0; i
< pos
&& bmap
; ++i
)
3524 bmap
= var_equal(bmap
, i
);
3526 bmap
= var_more(bmap
, pos
);
3527 return isl_basic_map_finalize(bmap
);
3530 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3532 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3533 __isl_take isl_space
*dim
, unsigned pos
)
3536 isl_basic_map
*bmap
;
3538 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3539 for (i
= 0; i
< pos
; ++i
)
3540 bmap
= var_equal(bmap
, i
);
3541 bmap
= var_more_or_equal(bmap
, pos
);
3542 return isl_basic_map_finalize(bmap
);
3545 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3546 unsigned n
, int equal
)
3548 struct isl_map
*map
;
3551 if (n
== 0 && equal
)
3552 return isl_map_universe(dims
);
3554 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3556 for (i
= 0; i
+ 1 < n
; ++i
)
3557 map
= isl_map_add_basic_map(map
,
3558 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3561 map
= isl_map_add_basic_map(map
,
3562 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3564 map
= isl_map_add_basic_map(map
,
3565 isl_basic_map_less_at(dims
, n
- 1));
3567 isl_space_free(dims
);
3572 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3576 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3579 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3581 return map_lex_lte_first(dim
, n
, 0);
3584 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3586 return map_lex_lte_first(dim
, n
, 1);
3589 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3591 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3594 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3596 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3599 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3600 unsigned n
, int equal
)
3602 struct isl_map
*map
;
3605 if (n
== 0 && equal
)
3606 return isl_map_universe(dims
);
3608 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3610 for (i
= 0; i
+ 1 < n
; ++i
)
3611 map
= isl_map_add_basic_map(map
,
3612 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3615 map
= isl_map_add_basic_map(map
,
3616 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3618 map
= isl_map_add_basic_map(map
,
3619 isl_basic_map_more_at(dims
, n
- 1));
3621 isl_space_free(dims
);
3626 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3630 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3633 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3635 return map_lex_gte_first(dim
, n
, 0);
3638 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3640 return map_lex_gte_first(dim
, n
, 1);
3643 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3645 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3648 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3650 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3653 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3654 __isl_take isl_set
*set2
)
3657 map
= isl_map_lex_le(isl_set_get_space(set1
));
3658 map
= isl_map_intersect_domain(map
, set1
);
3659 map
= isl_map_intersect_range(map
, set2
);
3663 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3664 __isl_take isl_set
*set2
)
3667 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3668 map
= isl_map_intersect_domain(map
, set1
);
3669 map
= isl_map_intersect_range(map
, set2
);
3673 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3674 __isl_take isl_set
*set2
)
3677 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3678 map
= isl_map_intersect_domain(map
, set1
);
3679 map
= isl_map_intersect_range(map
, set2
);
3683 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3684 __isl_take isl_set
*set2
)
3687 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3688 map
= isl_map_intersect_domain(map
, set1
);
3689 map
= isl_map_intersect_range(map
, set2
);
3693 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3694 __isl_take isl_map
*map2
)
3697 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3698 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3699 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3703 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3704 __isl_take isl_map
*map2
)
3707 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3708 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3709 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3713 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3714 __isl_take isl_map
*map2
)
3717 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3718 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3719 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3723 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3724 __isl_take isl_map
*map2
)
3727 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3728 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3729 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3733 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3734 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3736 struct isl_basic_map
*bmap
;
3738 bset
= isl_basic_set_cow(bset
);
3742 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3743 isl_space_free(bset
->dim
);
3744 bmap
= (struct isl_basic_map
*) bset
;
3746 return isl_basic_map_finalize(bmap
);
3748 isl_basic_set_free(bset
);
3749 isl_space_free(dim
);
3753 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3757 if (bmap
->dim
->n_in
== 0)
3758 return (struct isl_basic_set
*)bmap
;
3759 bmap
= isl_basic_map_cow(bmap
);
3762 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3765 bmap
= isl_basic_map_finalize(bmap
);
3766 return (struct isl_basic_set
*)bmap
;
3768 isl_basic_map_free(bmap
);
3772 /* For a div d = floor(f/m), add the constraints
3775 * -(f-(n-1)) + m d >= 0
3777 * Note that the second constraint is the negation of
3781 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3782 unsigned pos
, isl_int
*div
)
3785 unsigned total
= isl_basic_map_total_dim(bmap
);
3787 i
= isl_basic_map_alloc_inequality(bmap
);
3790 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3791 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3793 j
= isl_basic_map_alloc_inequality(bmap
);
3796 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3797 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3798 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3802 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3803 unsigned pos
, isl_int
*div
)
3805 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3809 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3811 unsigned total
= isl_basic_map_total_dim(bmap
);
3812 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3814 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3818 struct isl_basic_set
*isl_basic_map_underlying_set(
3819 struct isl_basic_map
*bmap
)
3823 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3825 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3826 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3827 return (struct isl_basic_set
*)bmap
;
3828 bmap
= isl_basic_map_cow(bmap
);
3831 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3834 bmap
->extra
-= bmap
->n_div
;
3836 bmap
= isl_basic_map_finalize(bmap
);
3837 return (struct isl_basic_set
*)bmap
;
3842 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3843 __isl_take isl_basic_set
*bset
)
3845 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3848 struct isl_basic_map
*isl_basic_map_overlying_set(
3849 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3851 struct isl_basic_map
*bmap
;
3852 struct isl_ctx
*ctx
;
3859 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3860 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3861 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3863 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3864 isl_basic_map_free(like
);
3865 return (struct isl_basic_map
*)bset
;
3867 bset
= isl_basic_set_cow(bset
);
3870 total
= bset
->dim
->n_out
+ bset
->extra
;
3871 bmap
= (struct isl_basic_map
*)bset
;
3872 isl_space_free(bmap
->dim
);
3873 bmap
->dim
= isl_space_copy(like
->dim
);
3876 bmap
->n_div
= like
->n_div
;
3877 bmap
->extra
+= like
->n_div
;
3881 ltotal
= total
- bmap
->extra
+ like
->extra
;
3884 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3885 bmap
->extra
* (1 + 1 + total
));
3886 if (isl_blk_is_error(bmap
->block2
))
3888 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3892 for (i
= 0; i
< bmap
->extra
; ++i
)
3893 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3894 for (i
= 0; i
< like
->n_div
; ++i
) {
3895 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3896 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3898 bmap
= isl_basic_map_extend_constraints(bmap
,
3899 0, 2 * like
->n_div
);
3900 for (i
= 0; i
< like
->n_div
; ++i
) {
3901 if (isl_int_is_zero(bmap
->div
[i
][0]))
3903 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3907 isl_basic_map_free(like
);
3908 bmap
= isl_basic_map_simplify(bmap
);
3909 bmap
= isl_basic_map_finalize(bmap
);
3912 isl_basic_map_free(like
);
3913 isl_basic_set_free(bset
);
3917 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3918 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3920 return (struct isl_basic_set
*)
3921 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3924 struct isl_set
*isl_set_from_underlying_set(
3925 struct isl_set
*set
, struct isl_basic_set
*like
)
3931 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3933 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3934 isl_basic_set_free(like
);
3937 set
= isl_set_cow(set
);
3940 for (i
= 0; i
< set
->n
; ++i
) {
3941 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3942 isl_basic_set_copy(like
));
3946 isl_space_free(set
->dim
);
3947 set
->dim
= isl_space_copy(like
->dim
);
3950 isl_basic_set_free(like
);
3953 isl_basic_set_free(like
);
3958 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3962 map
= isl_map_cow(map
);
3965 map
->dim
= isl_space_cow(map
->dim
);
3969 for (i
= 1; i
< map
->n
; ++i
)
3970 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3972 for (i
= 0; i
< map
->n
; ++i
) {
3973 map
->p
[i
] = (struct isl_basic_map
*)
3974 isl_basic_map_underlying_set(map
->p
[i
]);
3979 map
->dim
= isl_space_underlying(map
->dim
, 0);
3981 isl_space_free(map
->dim
);
3982 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
3986 return (struct isl_set
*)map
;
3992 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3994 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3997 __isl_give isl_basic_map
*isl_basic_map_reset_space(
3998 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4000 bmap
= isl_basic_map_cow(bmap
);
4004 isl_space_free(bmap
->dim
);
4007 bmap
= isl_basic_map_finalize(bmap
);
4011 isl_basic_map_free(bmap
);
4012 isl_space_free(dim
);
4016 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4017 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4019 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4023 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4024 __isl_take isl_space
*dim
)
4028 map
= isl_map_cow(map
);
4032 for (i
= 0; i
< map
->n
; ++i
) {
4033 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4034 isl_space_copy(dim
));
4038 isl_space_free(map
->dim
);
4044 isl_space_free(dim
);
4048 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4049 __isl_take isl_space
*dim
)
4051 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4054 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4057 struct isl_basic_set
*domain
;
4063 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4065 n_in
= isl_basic_map_n_in(bmap
);
4066 n_out
= isl_basic_map_n_out(bmap
);
4067 domain
= isl_basic_set_from_basic_map(bmap
);
4068 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4070 domain
= isl_basic_set_reset_space(domain
, dim
);
4075 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4079 return isl_space_may_be_set(bmap
->dim
);
4082 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4086 if (isl_basic_map_may_be_set(bmap
))
4088 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4091 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4092 __isl_take isl_basic_map
*bmap
)
4096 isl_basic_map
*domain
;
4097 int nparam
, n_in
, n_out
;
4100 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4101 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4102 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4104 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4105 domain
= isl_basic_map_universe(dim
);
4107 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4108 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4109 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4111 total
= isl_basic_map_total_dim(bmap
);
4113 for (i
= 0; i
< n_in
; ++i
) {
4114 k
= isl_basic_map_alloc_equality(bmap
);
4117 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4118 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4119 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4122 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4123 return isl_basic_map_finalize(bmap
);
4125 isl_basic_map_free(bmap
);
4129 __isl_give isl_basic_map
*isl_basic_map_range_map(
4130 __isl_take isl_basic_map
*bmap
)
4134 isl_basic_map
*range
;
4135 int nparam
, n_in
, n_out
;
4138 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4139 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4140 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4142 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4143 range
= isl_basic_map_universe(dim
);
4145 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4146 bmap
= isl_basic_map_apply_range(bmap
, range
);
4147 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4149 total
= isl_basic_map_total_dim(bmap
);
4151 for (i
= 0; i
< n_out
; ++i
) {
4152 k
= isl_basic_map_alloc_equality(bmap
);
4155 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4156 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4157 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4160 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4161 return isl_basic_map_finalize(bmap
);
4163 isl_basic_map_free(bmap
);
4167 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4171 return isl_space_may_be_set(map
->dim
);
4174 struct isl_set
*isl_map_range(struct isl_map
*map
)
4177 struct isl_set
*set
;
4181 if (isl_map_may_be_set(map
))
4182 return (isl_set
*)map
;
4184 map
= isl_map_cow(map
);
4188 set
= (struct isl_set
*) map
;
4189 set
->dim
= isl_space_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
4192 for (i
= 0; i
< map
->n
; ++i
) {
4193 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4197 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4198 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4205 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4208 isl_space
*domain_dim
;
4210 map
= isl_map_cow(map
);
4214 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4215 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4216 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4219 for (i
= 0; i
< map
->n
; ++i
) {
4220 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4224 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4225 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4232 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4235 isl_space
*range_dim
;
4237 map
= isl_map_cow(map
);
4241 range_dim
= isl_space_range(isl_map_get_space(map
));
4242 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4243 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4246 for (i
= 0; i
< map
->n
; ++i
) {
4247 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4251 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4252 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4259 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4260 __isl_take isl_space
*dim
)
4263 struct isl_map
*map
= NULL
;
4265 set
= isl_set_cow(set
);
4268 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4269 map
= (struct isl_map
*)set
;
4270 for (i
= 0; i
< set
->n
; ++i
) {
4271 map
->p
[i
] = isl_basic_map_from_basic_set(
4272 set
->p
[i
], isl_space_copy(dim
));
4276 isl_space_free(map
->dim
);
4280 isl_space_free(dim
);
4285 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4286 __isl_take isl_basic_set
*bset
)
4288 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4291 __isl_give isl_basic_map
*isl_basic_map_from_range(
4292 __isl_take isl_basic_set
*bset
)
4294 return (isl_basic_map
*)bset
;
4297 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4299 return (struct isl_map
*)set
;
4302 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4304 return isl_map_reverse(isl_map_from_range(set
));
4307 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4308 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4310 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4311 isl_basic_map_from_range(range
));
4314 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4315 __isl_take isl_set
*range
)
4317 return isl_map_apply_range(isl_map_from_domain(domain
),
4318 isl_map_from_range(range
));
4321 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4324 struct isl_set
*set
= NULL
;
4328 map
= isl_map_cow(map
);
4331 map
->dim
= isl_space_as_set_space(map
->dim
);
4334 set
= (struct isl_set
*)map
;
4335 for (i
= 0; i
< map
->n
; ++i
) {
4336 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4346 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4349 struct isl_map
*map
;
4353 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4354 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4355 sizeof(struct isl_map
) +
4356 (n
- 1) * sizeof(struct isl_basic_map
*));
4360 map
->ctx
= dim
->ctx
;
4361 isl_ctx_ref(map
->ctx
);
4369 isl_space_free(dim
);
4373 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4374 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4377 struct isl_map
*map
;
4380 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4384 map
= isl_map_alloc_space(dims
, n
, flags
);
4388 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4390 struct isl_basic_map
*bmap
;
4391 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4392 bmap
= isl_basic_map_set_to_empty(bmap
);
4396 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4398 struct isl_basic_set
*bset
;
4399 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4400 bset
= isl_basic_set_set_to_empty(bset
);
4404 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4406 struct isl_basic_map
*bmap
;
4409 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4410 bmap
= isl_basic_map_set_to_empty(bmap
);
4414 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4416 struct isl_basic_map
*bmap
;
4419 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4420 bmap
= isl_basic_map_set_to_empty(bmap
);
4424 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4426 struct isl_basic_set
*bset
;
4429 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4430 bset
= isl_basic_set_set_to_empty(bset
);
4434 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4436 struct isl_basic_map
*bmap
;
4437 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4438 bmap
= isl_basic_map_finalize(bmap
);
4442 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4444 struct isl_basic_set
*bset
;
4445 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4446 bset
= isl_basic_set_finalize(bset
);
4450 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4453 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4454 isl_basic_map
*bmap
;
4456 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4457 for (i
= 0; i
< total
; ++i
) {
4458 int k
= isl_basic_map_alloc_inequality(bmap
);
4461 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4462 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4466 isl_basic_map_free(bmap
);
4470 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4472 return isl_basic_map_nat_universe(dim
);
4475 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4477 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4480 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4482 return isl_map_nat_universe(dim
);
4485 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4486 __isl_keep isl_basic_map
*model
)
4490 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4493 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4497 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4500 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4501 __isl_keep isl_set
*model
)
4505 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4508 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4510 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4513 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4517 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4520 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4524 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4527 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4529 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4532 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4536 return isl_set_empty(isl_space_copy(model
->dim
));
4539 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4541 struct isl_map
*map
;
4544 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4545 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4549 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4551 struct isl_set
*set
;
4554 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4555 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4559 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4563 return isl_set_universe(isl_space_copy(model
->dim
));
4566 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4569 struct isl_map
*dup
;
4573 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4574 for (i
= 0; i
< map
->n
; ++i
)
4575 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4579 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4580 __isl_take isl_basic_map
*bmap
)
4584 if (isl_basic_map_plain_is_empty(bmap
)) {
4585 isl_basic_map_free(bmap
);
4588 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4589 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4590 map
->p
[map
->n
] = bmap
;
4592 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4598 isl_basic_map_free(bmap
);
4602 void isl_map_free(struct isl_map
*map
)
4612 isl_ctx_deref(map
->ctx
);
4613 for (i
= 0; i
< map
->n
; ++i
)
4614 isl_basic_map_free(map
->p
[i
]);
4615 isl_space_free(map
->dim
);
4619 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4620 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4624 base
= isl_map_cow(base
);
4628 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4631 for (i
= 0; i
< base
->n
; ++i
) {
4632 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4633 isl_space_copy(base
->dim
), 0, 0, 0);
4643 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4644 unsigned nparam
, unsigned dim
)
4646 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4650 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4651 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4655 bmap
= isl_basic_map_cow(bmap
);
4656 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4657 j
= isl_basic_map_alloc_equality(bmap
);
4660 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4661 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4662 isl_int_set_si(bmap
->eq
[j
][0], value
);
4663 bmap
= isl_basic_map_simplify(bmap
);
4664 return isl_basic_map_finalize(bmap
);
4666 isl_basic_map_free(bmap
);
4670 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4671 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4675 bmap
= isl_basic_map_cow(bmap
);
4676 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4677 j
= isl_basic_map_alloc_equality(bmap
);
4680 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4681 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4682 isl_int_set(bmap
->eq
[j
][0], value
);
4683 bmap
= isl_basic_map_simplify(bmap
);
4684 return isl_basic_map_finalize(bmap
);
4686 isl_basic_map_free(bmap
);
4690 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4691 enum isl_dim_type type
, unsigned pos
, int value
)
4695 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4696 return isl_basic_map_fix_pos_si(bmap
,
4697 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4699 isl_basic_map_free(bmap
);
4703 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4704 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4708 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4709 return isl_basic_map_fix_pos(bmap
,
4710 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4712 isl_basic_map_free(bmap
);
4716 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4717 enum isl_dim_type type
, unsigned pos
, int value
)
4719 return (struct isl_basic_set
*)
4720 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4724 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4725 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4727 return (struct isl_basic_set
*)
4728 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4732 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4733 unsigned input
, int value
)
4735 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4738 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4739 unsigned dim
, int value
)
4741 return (struct isl_basic_set
*)
4742 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4743 isl_dim_set
, dim
, value
);
4746 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4747 enum isl_dim_type type
, unsigned pos
, int value
)
4751 map
= isl_map_cow(map
);
4755 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4756 for (i
= 0; i
< map
->n
; ++i
) {
4757 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4761 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4768 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4769 enum isl_dim_type type
, unsigned pos
, int value
)
4771 return (struct isl_set
*)
4772 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4775 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4776 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4780 map
= isl_map_cow(map
);
4784 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4785 for (i
= 0; i
< map
->n
; ++i
) {
4786 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4790 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4797 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4798 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4800 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4803 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4804 unsigned input
, int value
)
4806 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4809 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4811 return (struct isl_set
*)
4812 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4815 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4816 __isl_take isl_basic_map
*bmap
,
4817 enum isl_dim_type type
, unsigned pos
, int value
)
4823 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4824 pos
+= isl_basic_map_offset(bmap
, type
);
4825 bmap
= isl_basic_map_cow(bmap
);
4826 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4827 j
= isl_basic_map_alloc_inequality(bmap
);
4830 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4831 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4832 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4833 bmap
= isl_basic_map_simplify(bmap
);
4834 return isl_basic_map_finalize(bmap
);
4836 isl_basic_map_free(bmap
);
4840 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4841 unsigned dim
, isl_int value
)
4845 bset
= isl_basic_set_cow(bset
);
4846 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4847 j
= isl_basic_set_alloc_inequality(bset
);
4850 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4851 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4852 isl_int_neg(bset
->ineq
[j
][0], value
);
4853 bset
= isl_basic_set_simplify(bset
);
4854 return isl_basic_set_finalize(bset
);
4856 isl_basic_set_free(bset
);
4860 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4861 enum isl_dim_type type
, unsigned pos
, int value
)
4865 map
= isl_map_cow(map
);
4869 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4870 for (i
= 0; i
< map
->n
; ++i
) {
4871 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4876 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4883 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4884 enum isl_dim_type type
, unsigned pos
, int value
)
4886 return (struct isl_set
*)
4887 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4890 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4895 set
= isl_set_cow(set
);
4899 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4900 for (i
= 0; i
< set
->n
; ++i
) {
4901 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4911 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4915 map
= isl_map_cow(map
);
4919 map
->dim
= isl_space_reverse(map
->dim
);
4922 for (i
= 0; i
< map
->n
; ++i
) {
4923 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4927 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4934 static struct isl_map
*isl_basic_map_partial_lexopt(
4935 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4936 struct isl_set
**empty
, int max
)
4940 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4941 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4943 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4945 isl_basic_map_free(bmap
);
4946 isl_basic_set_free(dom
);
4952 struct isl_map
*isl_basic_map_partial_lexmax(
4953 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4954 struct isl_set
**empty
)
4956 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4959 struct isl_map
*isl_basic_map_partial_lexmin(
4960 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4961 struct isl_set
**empty
)
4963 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4966 struct isl_set
*isl_basic_set_partial_lexmin(
4967 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4968 struct isl_set
**empty
)
4970 return (struct isl_set
*)
4971 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4975 struct isl_set
*isl_basic_set_partial_lexmax(
4976 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4977 struct isl_set
**empty
)
4979 return (struct isl_set
*)
4980 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4984 /* Given a basic map "bmap", compute the lexicographically minimal
4985 * (or maximal) image element for each domain element in dom.
4986 * Set *empty to those elements in dom that do not have an image element.
4988 * We first make sure the basic sets in dom are disjoint and then
4989 * simply collect the results over each of the basic sets separately.
4990 * We could probably improve the efficiency a bit by moving the union
4991 * domain down into the parametric integer programming.
4993 static __isl_give isl_map
*basic_map_partial_lexopt(
4994 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4995 __isl_give isl_set
**empty
, int max
)
4998 struct isl_map
*res
;
5000 dom
= isl_set_make_disjoint(dom
);
5004 if (isl_set_plain_is_empty(dom
)) {
5005 res
= isl_map_empty_like_basic_map(bmap
);
5006 *empty
= isl_set_empty_like(dom
);
5008 isl_basic_map_free(bmap
);
5012 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5013 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5015 for (i
= 1; i
< dom
->n
; ++i
) {
5016 struct isl_map
*res_i
;
5017 struct isl_set
*empty_i
;
5019 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5020 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5022 res
= isl_map_union_disjoint(res
, res_i
);
5023 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5027 isl_basic_map_free(bmap
);
5032 isl_basic_map_free(bmap
);
5036 /* Given a map "map", compute the lexicographically minimal
5037 * (or maximal) image element for each domain element in dom.
5038 * Set *empty to those elements in dom that do not have an image element.
5040 * We first compute the lexicographically minimal or maximal element
5041 * in the first basic map. This results in a partial solution "res"
5042 * and a subset "todo" of dom that still need to be handled.
5043 * We then consider each of the remaining maps in "map" and successively
5044 * improve both "res" and "todo".
5046 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5047 * Assume we are computing the lexicographical maximum.
5048 * We first compute the lexicographically maximal element in basic map i.
5049 * This results in a partial solution res_i and a subset todo_i.
5050 * Then we combine these results with those obtain for the first k basic maps
5051 * to obtain a result that is valid for the first k+1 basic maps.
5052 * In particular, the set where there is no solution is the set where
5053 * there is no solution for the first k basic maps and also no solution
5054 * for the ith basic map, i.e.,
5056 * todo^i = todo^k * todo_i
5058 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5059 * solutions, arbitrarily breaking ties in favor of res^k.
5060 * That is, when res^k(a) >= res_i(a), we pick res^k and
5061 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5062 * the lexicographic order.)
5063 * In practice, we compute
5065 * res^k * (res_i . "<=")
5069 * res_i * (res^k . "<")
5071 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5072 * where only one of res^k and res_i provides a solution and we simply pick
5079 * Note that we only compute these intersections when dom(res^k) intersects
5080 * dom(res_i). Otherwise, the only effect of these intersections is to
5081 * potentially break up res^k and res_i into smaller pieces.
5082 * We want to avoid such splintering as much as possible.
5083 * In fact, an earlier implementation of this function would look for
5084 * better results in the domain of res^k and for extra results in todo^k,
5085 * but this would always result in a splintering according to todo^k,
5086 * even when the domain of basic map i is disjoint from the domains of
5087 * the previous basic maps.
5089 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5090 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5091 __isl_give isl_set
**empty
, int max
)
5094 struct isl_map
*res
;
5095 struct isl_set
*todo
;
5100 if (isl_map_plain_is_empty(map
)) {
5108 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5109 isl_set_copy(dom
), &todo
, max
);
5111 for (i
= 1; i
< map
->n
; ++i
) {
5115 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5117 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5118 isl_set_copy(dom
), &todo_i
, max
);
5121 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5122 le
= isl_map_lex_le(dim
);
5124 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5125 le
= isl_map_lex_ge(dim
);
5127 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5128 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5129 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5130 le
= isl_map_intersect(le
, isl_map_copy(res
));
5132 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5133 res
= isl_map_intersect_domain(res
,
5134 isl_set_copy(todo_i
));
5135 res_i
= isl_map_intersect_domain(res_i
,
5136 isl_set_copy(todo
));
5139 res
= isl_map_union_disjoint(res
, res_i
);
5140 res
= isl_map_union_disjoint(res
, lt
);
5141 res
= isl_map_union_disjoint(res
, le
);
5143 todo
= isl_set_intersect(todo
, todo_i
);
5163 /* Given a map "map", compute the lexicographically minimal
5164 * (or maximal) image element for each domain element in dom.
5165 * Set *empty to those elements in dom that do not have an image element.
5167 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5169 static __isl_give isl_map
*isl_map_partial_lexopt(
5170 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5171 __isl_give isl_set
**empty
, int max
)
5175 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5176 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5177 if (!isl_space_has_named_params(map
->dim
) ||
5178 !isl_space_has_named_params(dom
->dim
))
5179 isl_die(map
->ctx
, isl_error_invalid
,
5180 "unaligned unnamed parameters", goto error
);
5181 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5182 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5183 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5192 __isl_give isl_map
*isl_map_partial_lexmax(
5193 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5194 __isl_give isl_set
**empty
)
5196 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5199 __isl_give isl_map
*isl_map_partial_lexmin(
5200 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5201 __isl_give isl_set
**empty
)
5203 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5206 __isl_give isl_set
*isl_set_partial_lexmin(
5207 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5208 __isl_give isl_set
**empty
)
5210 return (struct isl_set
*)
5211 isl_map_partial_lexmin((struct isl_map
*)set
,
5215 __isl_give isl_set
*isl_set_partial_lexmax(
5216 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5217 __isl_give isl_set
**empty
)
5219 return (struct isl_set
*)
5220 isl_map_partial_lexmax((struct isl_map
*)set
,
5224 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5226 struct isl_basic_set
*dom
= NULL
;
5231 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5232 dom
= isl_basic_set_universe(dom_dim
);
5233 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5235 isl_basic_map_free(bmap
);
5239 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5241 return isl_basic_map_lexopt(bmap
, 0);
5244 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5246 return isl_basic_map_lexopt(bmap
, 1);
5249 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5251 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5254 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5256 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5259 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5261 struct isl_set
*dom
= NULL
;
5266 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5267 dom
= isl_set_universe(dom_dim
);
5268 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5274 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5276 return isl_map_lexopt(map
, 0);
5279 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5281 return isl_map_lexopt(map
, 1);
5284 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5286 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5289 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5291 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5294 /* Construct a map that equates the two given dimensions in the given space.
5296 static __isl_give isl_map
*equate(__isl_take isl_space
*dim
,
5297 enum isl_dim_type src_type
, int src_pos
,
5298 enum isl_dim_type dst_type
, int dst_pos
)
5300 isl_basic_map
*bmap
;
5303 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5304 k
= isl_basic_map_alloc_equality(bmap
);
5307 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
5308 src_pos
+= isl_basic_map_offset(bmap
, src_type
);
5309 dst_pos
+= isl_basic_map_offset(bmap
, dst_type
);
5310 isl_int_set_si(bmap
->eq
[k
][src_pos
], 1);
5311 isl_int_set_si(bmap
->eq
[k
][dst_pos
], -1);
5313 return isl_map_from_basic_map(bmap
);
5315 isl_basic_map_free(bmap
);
5319 /* Extract the first and only affine expression from list
5320 * and then add it to *pwaff with the given dom.
5321 * This domain is known to be disjoint from other domains
5322 * because of the way isl_basic_set_foreach_lexmax works.
5324 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5325 __isl_take isl_aff_list
*list
, void *user
)
5327 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5329 isl_pw_aff
**pwaff
= user
;
5330 isl_pw_aff
*pwaff_i
;
5332 if (isl_aff_list_n_aff(list
) != 1)
5333 isl_die(ctx
, isl_error_internal
,
5334 "expecting single element list", goto error
);
5336 aff
= isl_aff_list_get_aff(list
, 0);
5337 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5339 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5341 isl_aff_list_free(list
);
5345 isl_basic_set_free(dom
);
5346 isl_aff_list_free(list
);
5350 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5351 * dimension as an isl_pw_aff.
5353 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5354 * call update_dim_opt on each leaf of the result.
5356 static __isl_give isl_pw_aff
*basic_set_dim_opt(__isl_keep isl_basic_set
*bset
,
5359 isl_space
*dim
= isl_basic_set_get_space(bset
);
5363 dim
= isl_space_domain(isl_space_from_range(dim
));
5364 pwaff
= isl_pw_aff_empty(dim
);
5366 r
= isl_basic_set_foreach_lexopt(bset
, max
, &update_dim_opt
, &pwaff
);
5368 return isl_pw_aff_free(pwaff
);
5373 /* Compute the minimum or maximum of the given set dimension
5374 * as a function of the parameters, but independently of
5375 * the other set dimensions.
5377 * We first project the set onto the given dimension and then compute
5378 * the "lexicographic" maximum in each basic set, combining the results
5379 * using isl_pw_aff_union_max.
5381 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5388 map
= isl_map_from_domain(set
);
5389 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
5390 map
= isl_map_intersect(map
,
5391 equate(isl_map_get_space(map
), isl_dim_in
, pos
,
5393 set
= isl_map_range(map
);
5398 isl_space
*dim
= isl_set_get_space(set
);
5399 dim
= isl_space_domain(isl_space_from_range(dim
));
5401 return isl_pw_aff_empty(dim
);
5404 pwaff
= basic_set_dim_opt(set
->p
[0], max
);
5405 for (i
= 1; i
< set
->n
; ++i
) {
5406 isl_pw_aff
*pwaff_i
;
5408 pwaff_i
= basic_set_dim_opt(set
->p
[i
], max
);
5409 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5417 /* Compute the maximum of the given set dimension as a function of the
5418 * parameters, but independently of the other set dimensions.
5420 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5422 return set_dim_opt(set
, pos
, 1);
5425 /* Compute the minimum of the given set dimension as a function of the
5426 * parameters, but independently of the other set dimensions.
5428 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5430 return set_dim_opt(set
, pos
, 0);
5433 /* Apply a preimage specified by "mat" on the parameters of "bset".
5434 * bset is assumed to have only parameters and divs.
5436 static struct isl_basic_set
*basic_set_parameter_preimage(
5437 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5444 bset
->dim
= isl_space_cow(bset
->dim
);
5448 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5450 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5452 bset
->dim
->nparam
= 0;
5453 bset
->dim
->n_out
= nparam
;
5454 bset
= isl_basic_set_preimage(bset
, mat
);
5456 bset
->dim
->nparam
= bset
->dim
->n_out
;
5457 bset
->dim
->n_out
= 0;
5462 isl_basic_set_free(bset
);
5466 /* Apply a preimage specified by "mat" on the parameters of "set".
5467 * set is assumed to have only parameters and divs.
5469 static struct isl_set
*set_parameter_preimage(
5470 struct isl_set
*set
, struct isl_mat
*mat
)
5472 isl_space
*dim
= NULL
;
5478 dim
= isl_space_copy(set
->dim
);
5479 dim
= isl_space_cow(dim
);
5483 nparam
= isl_set_dim(set
, isl_dim_param
);
5485 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5488 dim
->n_out
= nparam
;
5489 isl_set_reset_space(set
, dim
);
5490 set
= isl_set_preimage(set
, mat
);
5493 dim
= isl_space_copy(set
->dim
);
5494 dim
= isl_space_cow(dim
);
5497 dim
->nparam
= dim
->n_out
;
5499 isl_set_reset_space(set
, dim
);
5502 isl_space_free(dim
);
5509 /* Intersect the basic set "bset" with the affine space specified by the
5510 * equalities in "eq".
5512 static struct isl_basic_set
*basic_set_append_equalities(
5513 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5521 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5526 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5527 for (i
= 0; i
< eq
->n_row
; ++i
) {
5528 k
= isl_basic_set_alloc_equality(bset
);
5531 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5532 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5536 bset
= isl_basic_set_gauss(bset
, NULL
);
5537 bset
= isl_basic_set_finalize(bset
);
5542 isl_basic_set_free(bset
);
5546 /* Intersect the set "set" with the affine space specified by the
5547 * equalities in "eq".
5549 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5557 for (i
= 0; i
< set
->n
; ++i
) {
5558 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5571 /* Project the given basic set onto its parameter domain, possibly introducing
5572 * new, explicit, existential variables in the constraints.
5573 * The input has parameters and (possibly implicit) existential variables.
5574 * The output has the same parameters, but only
5575 * explicit existentially quantified variables.
5577 * The actual projection is performed by pip, but pip doesn't seem
5578 * to like equalities very much, so we first remove the equalities
5579 * among the parameters by performing a variable compression on
5580 * the parameters. Afterward, an inverse transformation is performed
5581 * and the equalities among the parameters are inserted back in.
5583 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5587 struct isl_mat
*T
, *T2
;
5588 struct isl_set
*set
;
5589 unsigned nparam
, n_div
;
5591 bset
= isl_basic_set_cow(bset
);
5595 if (bset
->n_eq
== 0)
5596 return isl_basic_set_lexmin(bset
);
5598 isl_basic_set_gauss(bset
, NULL
);
5600 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5601 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5603 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5604 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5607 if (i
== bset
->n_eq
)
5608 return isl_basic_set_lexmin(bset
);
5610 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5612 eq
= isl_mat_cow(eq
);
5613 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5614 if (T
&& T
->n_col
== 0) {
5618 bset
= isl_basic_set_set_to_empty(bset
);
5619 return isl_set_from_basic_set(bset
);
5621 bset
= basic_set_parameter_preimage(bset
, T
);
5623 set
= isl_basic_set_lexmin(bset
);
5624 set
= set_parameter_preimage(set
, T2
);
5625 set
= set_append_equalities(set
, eq
);
5629 /* Compute an explicit representation for all the existentially
5630 * quantified variables.
5631 * The input and output dimensions are first turned into parameters.
5632 * compute_divs then returns a map with the same parameters and
5633 * no input or output dimensions and the dimension specification
5634 * is reset to that of the input.
5636 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5638 struct isl_basic_set
*bset
;
5639 struct isl_set
*set
;
5640 struct isl_map
*map
;
5641 isl_space
*dim
, *orig_dim
= NULL
;
5646 bmap
= isl_basic_map_cow(bmap
);
5650 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5651 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5652 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5653 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5657 orig_dim
= bmap
->dim
;
5659 bset
= (struct isl_basic_set
*)bmap
;
5661 set
= parameter_compute_divs(bset
);
5662 map
= (struct isl_map
*)set
;
5663 map
= isl_map_reset_space(map
, orig_dim
);
5667 isl_basic_map_free(bmap
);
5671 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5679 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5680 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5681 if (isl_int_is_zero(bmap
->div
[i
][0]))
5683 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5689 static int map_divs_known(__isl_keep isl_map
*map
)
5696 for (i
= 0; i
< map
->n
; ++i
) {
5697 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5705 /* If bmap contains any unknown divs, then compute explicit
5706 * expressions for them. However, this computation may be
5707 * quite expensive, so first try to remove divs that aren't
5710 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5713 struct isl_map
*map
;
5715 known
= isl_basic_map_divs_known(bmap
);
5719 return isl_map_from_basic_map(bmap
);
5721 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5723 known
= isl_basic_map_divs_known(bmap
);
5727 return isl_map_from_basic_map(bmap
);
5729 map
= compute_divs(bmap
);
5732 isl_basic_map_free(bmap
);
5736 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5740 struct isl_map
*res
;
5747 known
= map_divs_known(map
);
5755 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5756 for (i
= 1 ; i
< map
->n
; ++i
) {
5758 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5759 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5760 res
= isl_map_union_disjoint(res
, r2
);
5762 res
= isl_map_union(res
, r2
);
5769 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5771 return (struct isl_set
*)
5772 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5775 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5777 return (struct isl_set
*)
5778 isl_map_compute_divs((struct isl_map
*)set
);
5781 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5784 struct isl_set
*set
;
5789 map
= isl_map_cow(map
);
5793 set
= (struct isl_set
*)map
;
5794 set
->dim
= isl_space_domain(set
->dim
);
5797 for (i
= 0; i
< map
->n
; ++i
) {
5798 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5802 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5803 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5810 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
5811 __isl_take isl_map
*map2
)
5815 struct isl_map
*map
= NULL
;
5829 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
5831 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5832 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5833 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5835 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5836 map1
->n
+ map2
->n
, flags
);
5839 for (i
= 0; i
< map1
->n
; ++i
) {
5840 map
= isl_map_add_basic_map(map
,
5841 isl_basic_map_copy(map1
->p
[i
]));
5845 for (i
= 0; i
< map2
->n
; ++i
) {
5846 map
= isl_map_add_basic_map(map
,
5847 isl_basic_map_copy(map2
->p
[i
]));
5861 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
5862 __isl_take isl_map
*map2
)
5864 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
5867 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5869 map1
= isl_map_union_disjoint(map1
, map2
);
5873 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5877 struct isl_set
*isl_set_union_disjoint(
5878 struct isl_set
*set1
, struct isl_set
*set2
)
5880 return (struct isl_set
*)
5881 isl_map_union_disjoint(
5882 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5885 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5887 return (struct isl_set
*)
5888 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5891 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
5892 __isl_take isl_set
*set
)
5895 struct isl_map
*result
;
5901 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
5902 isl_die(set
->ctx
, isl_error_invalid
,
5903 "parameters don't match", goto error
);
5905 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
5906 !isl_map_compatible_range(map
, set
))
5907 isl_die(set
->ctx
, isl_error_invalid
,
5908 "incompatible spaces", goto error
);
5910 if (isl_set_plain_is_universe(set
)) {
5915 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5916 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5917 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5919 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
5920 map
->n
* set
->n
, flags
);
5923 for (i
= 0; i
< map
->n
; ++i
)
5924 for (j
= 0; j
< set
->n
; ++j
) {
5925 result
= isl_map_add_basic_map(result
,
5926 isl_basic_map_intersect_range(
5927 isl_basic_map_copy(map
->p
[i
]),
5928 isl_basic_set_copy(set
->p
[j
])));
5941 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
5942 __isl_take isl_set
*set
)
5944 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
5947 struct isl_map
*isl_map_intersect_domain(
5948 struct isl_map
*map
, struct isl_set
*set
)
5950 return isl_map_reverse(
5951 isl_map_intersect_range(isl_map_reverse(map
), set
));
5954 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
5955 __isl_take isl_map
*map2
)
5959 map1
= isl_map_reverse(map1
);
5960 map1
= isl_map_apply_range(map1
, map2
);
5961 return isl_map_reverse(map1
);
5968 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
5969 __isl_take isl_map
*map2
)
5971 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
5974 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
5975 __isl_take isl_map
*map2
)
5977 isl_space
*dim_result
;
5978 struct isl_map
*result
;
5984 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
5985 isl_space_copy(map2
->dim
));
5987 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
5990 for (i
= 0; i
< map1
->n
; ++i
)
5991 for (j
= 0; j
< map2
->n
; ++j
) {
5992 result
= isl_map_add_basic_map(result
,
5993 isl_basic_map_apply_range(
5994 isl_basic_map_copy(map1
->p
[i
]),
5995 isl_basic_map_copy(map2
->p
[j
])));
6001 if (result
&& result
->n
<= 1)
6002 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6010 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6011 __isl_take isl_map
*map2
)
6013 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6017 * returns range - domain
6019 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6021 isl_space
*dims
, *target_dim
;
6022 struct isl_basic_set
*bset
;
6029 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6030 bmap
->dim
, isl_dim_out
),
6032 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6033 dim
= isl_basic_map_n_in(bmap
);
6034 nparam
= isl_basic_map_n_param(bmap
);
6035 bset
= isl_basic_set_from_basic_map(bmap
);
6036 bset
= isl_basic_set_cow(bset
);
6037 dims
= isl_basic_set_get_space(bset
);
6038 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6039 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6040 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6041 for (i
= 0; i
< dim
; ++i
) {
6042 int j
= isl_basic_map_alloc_equality(
6043 (struct isl_basic_map
*)bset
);
6046 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6047 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6048 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6049 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6051 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6052 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6055 isl_basic_map_free(bmap
);
6060 * returns range - domain
6062 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6066 struct isl_set
*result
;
6071 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6072 map
->dim
, isl_dim_out
),
6074 dim
= isl_map_get_space(map
);
6075 dim
= isl_space_domain(dim
);
6076 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6079 for (i
= 0; i
< map
->n
; ++i
)
6080 result
= isl_set_add_basic_set(result
,
6081 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6090 * returns [domain -> range] -> range - domain
6092 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6093 __isl_take isl_basic_map
*bmap
)
6097 isl_basic_map
*domain
;
6101 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6102 isl_die(bmap
->ctx
, isl_error_invalid
,
6103 "domain and range don't match", goto error
);
6105 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6106 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6108 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6109 domain
= isl_basic_map_universe(dim
);
6111 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6112 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6113 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6115 total
= isl_basic_map_total_dim(bmap
);
6117 for (i
= 0; i
< n
; ++i
) {
6118 k
= isl_basic_map_alloc_equality(bmap
);
6121 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6122 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6123 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6124 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6127 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6128 return isl_basic_map_finalize(bmap
);
6130 isl_basic_map_free(bmap
);
6135 * returns [domain -> range] -> range - domain
6137 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6140 isl_space
*domain_dim
;
6145 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6146 isl_die(map
->ctx
, isl_error_invalid
,
6147 "domain and range don't match", goto error
);
6149 map
= isl_map_cow(map
);
6153 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6154 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6155 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6158 for (i
= 0; i
< map
->n
; ++i
) {
6159 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6163 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6170 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6172 struct isl_basic_map
*bmap
;
6180 nparam
= dims
->nparam
;
6182 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6186 for (i
= 0; i
< dim
; ++i
) {
6187 int j
= isl_basic_map_alloc_equality(bmap
);
6190 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6191 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6192 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6194 return isl_basic_map_finalize(bmap
);
6196 isl_basic_map_free(bmap
);
6200 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6204 if (dim
->n_in
!= dim
->n_out
)
6205 isl_die(dim
->ctx
, isl_error_invalid
,
6206 "number of input and output dimensions needs to be "
6207 "the same", goto error
);
6208 return basic_map_identity(dim
);
6210 isl_space_free(dim
);
6214 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6216 if (!model
|| !model
->dim
)
6218 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6221 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6223 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6226 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6228 if (!model
|| !model
->dim
)
6230 return isl_map_identity(isl_space_copy(model
->dim
));
6233 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6235 if (!model
|| !model
->dim
)
6237 return isl_map_identity(isl_space_copy(model
->dim
));
6240 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6242 isl_space
*dim
= isl_set_get_space(set
);
6244 id
= isl_map_identity(isl_space_map_from_set(dim
));
6245 return isl_map_intersect_range(id
, set
);
6248 /* Construct a basic set with all set dimensions having only non-negative
6251 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6252 __isl_take isl_space
*space
)
6257 struct isl_basic_set
*bset
;
6261 nparam
= space
->nparam
;
6263 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6266 for (i
= 0; i
< dim
; ++i
) {
6267 int k
= isl_basic_set_alloc_inequality(bset
);
6270 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6271 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6275 isl_basic_set_free(bset
);
6279 /* Construct the half-space x_pos >= 0.
6281 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6285 isl_basic_set
*nonneg
;
6287 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6288 k
= isl_basic_set_alloc_inequality(nonneg
);
6291 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6292 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6294 return isl_basic_set_finalize(nonneg
);
6296 isl_basic_set_free(nonneg
);
6300 /* Construct the half-space x_pos <= -1.
6302 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6307 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6308 k
= isl_basic_set_alloc_inequality(neg
);
6311 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6312 isl_int_set_si(neg
->ineq
[k
][0], -1);
6313 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6315 return isl_basic_set_finalize(neg
);
6317 isl_basic_set_free(neg
);
6321 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6322 enum isl_dim_type type
, unsigned first
, unsigned n
)
6325 isl_basic_set
*nonneg
;
6333 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6335 for (i
= 0; i
< n
; ++i
) {
6336 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6337 pos(set
->dim
, type
) + first
+ i
);
6338 neg
= neg_halfspace(isl_set_get_space(set
),
6339 pos(set
->dim
, type
) + first
+ i
);
6341 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6350 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6351 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6358 if (isl_set_plain_is_empty(set
)) {
6363 return fn(set
, signs
, user
);
6366 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6368 half
= isl_set_intersect(half
, isl_set_copy(set
));
6369 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6373 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6375 half
= isl_set_intersect(half
, set
);
6376 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6382 /* Call "fn" on the intersections of "set" with each of the orthants
6383 * (except for obviously empty intersections). The orthant is identified
6384 * by the signs array, with each entry having value 1 or -1 according
6385 * to the sign of the corresponding variable.
6387 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6388 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6398 if (isl_set_plain_is_empty(set
))
6401 nparam
= isl_set_dim(set
, isl_dim_param
);
6402 nvar
= isl_set_dim(set
, isl_dim_set
);
6404 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6406 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6414 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6416 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6419 int isl_basic_map_is_subset(
6420 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6423 struct isl_map
*map1
;
6424 struct isl_map
*map2
;
6426 if (!bmap1
|| !bmap2
)
6429 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6430 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6432 is_subset
= isl_map_is_subset(map1
, map2
);
6440 int isl_basic_map_is_equal(
6441 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6445 if (!bmap1
|| !bmap2
)
6447 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6450 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6454 int isl_basic_set_is_equal(
6455 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6457 return isl_basic_map_is_equal(
6458 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6461 int isl_map_is_empty(struct isl_map
*map
)
6468 for (i
= 0; i
< map
->n
; ++i
) {
6469 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6478 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6480 return map
? map
->n
== 0 : -1;
6483 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6485 return isl_map_plain_is_empty(map
);
6488 int isl_set_plain_is_empty(struct isl_set
*set
)
6490 return set
? set
->n
== 0 : -1;
6493 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6495 return isl_set_plain_is_empty(set
);
6498 int isl_set_is_empty(struct isl_set
*set
)
6500 return isl_map_is_empty((struct isl_map
*)set
);
6503 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6508 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6511 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6516 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6519 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6525 is_subset
= isl_map_is_subset(map1
, map2
);
6528 is_subset
= isl_map_is_subset(map2
, map1
);
6532 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6534 return align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6537 int isl_basic_map_is_strict_subset(
6538 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6542 if (!bmap1
|| !bmap2
)
6544 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6547 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6548 if (is_subset
== -1)
6553 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6559 is_subset
= isl_map_is_subset(map1
, map2
);
6562 is_subset
= isl_map_is_subset(map2
, map1
);
6563 if (is_subset
== -1)
6568 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6570 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6573 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6577 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6580 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6584 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6587 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6594 for (i
= 0; i
< map
->n
; ++i
) {
6595 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6603 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6605 return isl_map_plain_is_universe((isl_map
*) set
);
6608 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6610 return isl_set_plain_is_universe(set
);
6613 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6615 struct isl_basic_set
*bset
= NULL
;
6616 struct isl_vec
*sample
= NULL
;
6623 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6626 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6627 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6628 copy
= isl_basic_map_remove_redundancies(copy
);
6629 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6630 isl_basic_map_free(copy
);
6634 total
= 1 + isl_basic_map_total_dim(bmap
);
6635 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6636 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6642 isl_vec_free(bmap
->sample
);
6643 bmap
->sample
= NULL
;
6644 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6647 sample
= isl_basic_set_sample_vec(bset
);
6650 empty
= sample
->size
== 0;
6651 isl_vec_free(bmap
->sample
);
6652 bmap
->sample
= sample
;
6654 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6659 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6663 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6666 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6668 return isl_basic_map_plain_is_empty(bmap
);
6671 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6675 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6678 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6680 return isl_basic_set_plain_is_empty(bset
);
6683 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6685 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6688 struct isl_map
*isl_basic_map_union(
6689 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6691 struct isl_map
*map
;
6692 if (!bmap1
|| !bmap2
)
6695 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6697 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6700 map
= isl_map_add_basic_map(map
, bmap1
);
6701 map
= isl_map_add_basic_map(map
, bmap2
);
6704 isl_basic_map_free(bmap1
);
6705 isl_basic_map_free(bmap2
);
6709 struct isl_set
*isl_basic_set_union(
6710 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6712 return (struct isl_set
*)isl_basic_map_union(
6713 (struct isl_basic_map
*)bset1
,
6714 (struct isl_basic_map
*)bset2
);
6717 /* Order divs such that any div only depends on previous divs */
6718 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6726 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6728 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6730 if (isl_int_is_zero(bmap
->div
[i
][0]))
6732 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6736 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6742 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6744 return (struct isl_basic_set
*)
6745 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6748 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6755 for (i
= 0; i
< map
->n
; ++i
) {
6756 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6767 /* Apply the expansion computed by isl_merge_divs.
6768 * The expansion itself is given by "exp" while the resulting
6769 * list of divs is given by "div".
6771 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6772 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6777 bset
= isl_basic_set_cow(bset
);
6781 if (div
->n_row
< bset
->n_div
)
6782 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6783 "not an expansion", goto error
);
6785 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
6786 div
->n_row
- bset
->n_div
, 0,
6787 2 * (div
->n_row
- bset
->n_div
));
6789 n_div
= bset
->n_div
;
6790 for (i
= n_div
; i
< div
->n_row
; ++i
)
6791 if (isl_basic_set_alloc_div(bset
) < 0)
6795 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6796 if (j
>= 0 && exp
[j
] == i
) {
6798 isl_basic_map_swap_div(bset
, i
, j
);
6801 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6802 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6810 isl_basic_set_free(bset
);
6815 /* Look for a div in dst that corresponds to the div "div" in src.
6816 * The divs before "div" in src and dst are assumed to be the same.
6818 * Returns -1 if no corresponding div was found and the position
6819 * of the corresponding div in dst otherwise.
6821 static int find_div(struct isl_basic_map
*dst
,
6822 struct isl_basic_map
*src
, unsigned div
)
6826 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
6828 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6829 for (i
= div
; i
< dst
->n_div
; ++i
)
6830 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6831 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6832 dst
->n_div
- div
) == -1)
6837 struct isl_basic_map
*isl_basic_map_align_divs(
6838 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6841 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
6846 if (src
->n_div
== 0)
6849 for (i
= 0; i
< src
->n_div
; ++i
)
6850 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6852 src
= isl_basic_map_order_divs(src
);
6853 dst
= isl_basic_map_cow(dst
);
6854 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
6855 src
->n_div
, 0, 2 * src
->n_div
);
6858 for (i
= 0; i
< src
->n_div
; ++i
) {
6859 int j
= find_div(dst
, src
, i
);
6861 j
= isl_basic_map_alloc_div(dst
);
6864 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6865 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6866 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6870 isl_basic_map_swap_div(dst
, i
, j
);
6874 isl_basic_map_free(dst
);
6878 struct isl_basic_set
*isl_basic_set_align_divs(
6879 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6881 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6882 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6885 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6893 map
= isl_map_compute_divs(map
);
6894 map
= isl_map_cow(map
);
6898 for (i
= 1; i
< map
->n
; ++i
)
6899 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6900 for (i
= 1; i
< map
->n
; ++i
)
6901 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6903 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6907 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6909 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6912 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
6913 __isl_take isl_map
*map
)
6917 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6918 map
= isl_map_intersect_domain(map
, set
);
6919 set
= isl_map_range(map
);
6927 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
6928 __isl_take isl_map
*map
)
6930 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
6933 /* There is no need to cow as removing empty parts doesn't change
6934 * the meaning of the set.
6936 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6943 for (i
= map
->n
-1; i
>= 0; --i
) {
6944 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6946 isl_basic_map_free(map
->p
[i
]);
6947 if (i
!= map
->n
-1) {
6948 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6949 map
->p
[i
] = map
->p
[map
->n
-1];
6957 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6959 return (struct isl_set
*)
6960 isl_map_remove_empty_parts((struct isl_map
*)set
);
6963 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6965 struct isl_basic_map
*bmap
;
6966 if (!map
|| map
->n
== 0)
6968 bmap
= map
->p
[map
->n
-1];
6969 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6970 return isl_basic_map_copy(bmap
);
6973 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6975 return (struct isl_basic_set
*)
6976 isl_map_copy_basic_map((struct isl_map
*)set
);
6979 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6980 __isl_keep isl_basic_map
*bmap
)
6986 for (i
= map
->n
-1; i
>= 0; --i
) {
6987 if (map
->p
[i
] != bmap
)
6989 map
= isl_map_cow(map
);
6992 isl_basic_map_free(map
->p
[i
]);
6993 if (i
!= map
->n
-1) {
6994 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6995 map
->p
[i
] = map
->p
[map
->n
-1];
7006 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7007 struct isl_basic_set
*bset
)
7009 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7010 (struct isl_basic_map
*)bset
);
7013 /* Given two basic sets bset1 and bset2, compute the maximal difference
7014 * between the values of dimension pos in bset1 and those in bset2
7015 * for any common value of the parameters and dimensions preceding pos.
7017 static enum isl_lp_result
basic_set_maximal_difference_at(
7018 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7019 int pos
, isl_int
*opt
)
7022 struct isl_basic_map
*bmap1
= NULL
;
7023 struct isl_basic_map
*bmap2
= NULL
;
7024 struct isl_ctx
*ctx
;
7025 struct isl_vec
*obj
;
7028 unsigned dim1
, dim2
;
7029 enum isl_lp_result res
;
7031 if (!bset1
|| !bset2
)
7032 return isl_lp_error
;
7034 nparam
= isl_basic_set_n_param(bset1
);
7035 dim1
= isl_basic_set_n_dim(bset1
);
7036 dim2
= isl_basic_set_n_dim(bset2
);
7037 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7038 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7039 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7040 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7041 if (!bmap1
|| !bmap2
)
7043 bmap1
= isl_basic_map_cow(bmap1
);
7044 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7045 pos
, (dim1
- pos
) + (dim2
- pos
),
7046 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7047 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7050 total
= isl_basic_map_total_dim(bmap1
);
7052 obj
= isl_vec_alloc(ctx
, 1 + total
);
7053 isl_seq_clr(obj
->block
.data
, 1 + total
);
7054 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7055 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7058 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7060 isl_basic_map_free(bmap1
);
7064 isl_basic_map_free(bmap1
);
7065 isl_basic_map_free(bmap2
);
7066 return isl_lp_error
;
7069 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7070 * for any common value of the parameters and dimensions preceding pos
7071 * in both basic sets, the values of dimension pos in bset1 are
7072 * smaller or larger than those in bset2.
7075 * 1 if bset1 follows bset2
7076 * -1 if bset1 precedes bset2
7077 * 0 if bset1 and bset2 are incomparable
7078 * -2 if some error occurred.
7080 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7081 struct isl_basic_set
*bset2
, int pos
)
7084 enum isl_lp_result res
;
7089 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7091 if (res
== isl_lp_empty
)
7093 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7094 res
== isl_lp_unbounded
)
7096 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7105 /* Given two basic sets bset1 and bset2, check whether
7106 * for any common value of the parameters and dimensions preceding pos
7107 * there is a value of dimension pos in bset1 that is larger
7108 * than a value of the same dimension in bset2.
7111 * 1 if there exists such a pair
7112 * 0 if there is no such pair, but there is a pair of equal values
7114 * -2 if some error occurred.
7116 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7117 __isl_keep isl_basic_set
*bset2
, int pos
)
7120 enum isl_lp_result res
;
7125 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7127 if (res
== isl_lp_empty
)
7129 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7130 res
== isl_lp_unbounded
)
7132 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7134 else if (res
== isl_lp_ok
)
7143 /* Given two sets set1 and set2, check whether
7144 * for any common value of the parameters and dimensions preceding pos
7145 * there is a value of dimension pos in set1 that is larger
7146 * than a value of the same dimension in set2.
7149 * 1 if there exists such a pair
7150 * 0 if there is no such pair, but there is a pair of equal values
7152 * -2 if some error occurred.
7154 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7155 __isl_keep isl_set
*set2
, int pos
)
7163 for (i
= 0; i
< set1
->n
; ++i
)
7164 for (j
= 0; j
< set2
->n
; ++j
) {
7166 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7167 if (f
== 1 || f
== -2)
7176 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7177 unsigned pos
, isl_int
*val
)
7185 total
= isl_basic_map_total_dim(bmap
);
7186 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7187 for (; d
+1 > pos
; --d
)
7188 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7192 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7194 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7196 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7199 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7205 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7206 unsigned pos
, isl_int
*val
)
7218 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7221 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7222 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7223 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7224 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7228 isl_int_set(*val
, v
);
7234 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7235 unsigned pos
, isl_int
*val
)
7237 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7241 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7244 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7247 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7248 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7250 if (pos
>= isl_basic_map_dim(bmap
, type
))
7252 return isl_basic_map_plain_has_fixed_var(bmap
,
7253 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7256 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7257 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7259 if (pos
>= isl_map_dim(map
, type
))
7261 return isl_map_plain_has_fixed_var(map
,
7262 map_offset(map
, type
) - 1 + pos
, val
);
7265 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7266 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7268 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7271 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7272 * then return this fixed value in *val.
7274 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7275 unsigned dim
, isl_int
*val
)
7277 return isl_basic_set_plain_has_fixed_var(bset
,
7278 isl_basic_set_n_param(bset
) + dim
, val
);
7281 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7282 * then return this fixed value in *val.
7284 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7285 unsigned dim
, isl_int
*val
)
7287 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7290 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7291 unsigned dim
, isl_int
*val
)
7293 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7296 /* Check if input variable in has fixed value and if so and if val is not NULL,
7297 * then return this fixed value in *val.
7299 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7300 unsigned in
, isl_int
*val
)
7302 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7305 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7306 * and if val is not NULL, then return this lower bound in *val.
7308 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7309 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7311 int i
, i_eq
= -1, i_ineq
= -1;
7318 total
= isl_basic_set_total_dim(bset
);
7319 nparam
= isl_basic_set_n_param(bset
);
7320 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7321 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7327 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7328 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7330 if (i_eq
!= -1 || i_ineq
!= -1)
7334 if (i_eq
== -1 && i_ineq
== -1)
7336 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7337 /* The coefficient should always be one due to normalization. */
7338 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7340 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7342 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7343 total
- nparam
- dim
- 1) != -1)
7346 isl_int_neg(*val
, c
[0]);
7350 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7351 unsigned dim
, isl_int
*val
)
7363 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7367 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7369 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7370 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7372 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7376 isl_int_set(*val
, v
);
7387 /* uset_gist depends on constraints without existentially quantified
7388 * variables sorting first.
7390 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7392 const struct constraint
*c1
= (const struct constraint
*)p1
;
7393 const struct constraint
*c2
= (const struct constraint
*)p2
;
7395 unsigned size
= isl_min(c1
->size
, c2
->size
);
7397 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7398 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7403 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7406 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7407 struct isl_basic_map
*bmap
)
7410 struct constraint
*c
;
7415 total
= isl_basic_map_total_dim(bmap
);
7416 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7419 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7421 c
[i
].c
= bmap
->ineq
[i
];
7423 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7424 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7425 bmap
->ineq
[i
] = c
[i
].c
;
7429 isl_basic_map_free(bmap
);
7433 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7434 __isl_take isl_basic_set
*bset
)
7436 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7437 (struct isl_basic_map
*)bset
);
7440 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7444 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7446 bmap
= isl_basic_map_remove_redundancies(bmap
);
7447 bmap
= isl_basic_map_sort_constraints(bmap
);
7448 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7452 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7454 return (struct isl_basic_set
*)isl_basic_map_normalize(
7455 (struct isl_basic_map
*)bset
);
7458 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7459 const __isl_keep isl_basic_map
*bmap2
)
7466 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7467 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7468 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7469 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7470 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7471 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7472 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7473 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7474 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7475 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7476 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7478 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7480 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7482 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7483 return bmap1
->n_eq
- bmap2
->n_eq
;
7484 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7485 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7486 if (bmap1
->n_div
!= bmap2
->n_div
)
7487 return bmap1
->n_div
- bmap2
->n_div
;
7488 total
= isl_basic_map_total_dim(bmap1
);
7489 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7490 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7494 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7495 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7499 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7500 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7507 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7508 __isl_keep isl_basic_map
*bmap2
)
7510 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7513 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7514 __isl_keep isl_basic_set
*bset2
)
7516 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7517 (isl_basic_map
*)bset2
);
7520 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7522 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7523 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7525 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7528 /* We normalize in place, but if anything goes wrong we need
7529 * to return NULL, so we need to make sure we don't change the
7530 * meaning of any possible other copies of map.
7532 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7535 struct isl_basic_map
*bmap
;
7539 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7541 for (i
= 0; i
< map
->n
; ++i
) {
7542 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7545 isl_basic_map_free(map
->p
[i
]);
7548 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7549 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7550 map
= isl_map_remove_empty_parts(map
);
7553 for (i
= map
->n
- 1; i
>= 1; --i
) {
7554 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7556 isl_basic_map_free(map
->p
[i
-1]);
7557 for (j
= i
; j
< map
->n
; ++j
)
7558 map
->p
[j
-1] = map
->p
[j
];
7568 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7570 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7573 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7583 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7586 map1
= isl_map_copy(map1
);
7587 map2
= isl_map_copy(map2
);
7588 map1
= isl_map_normalize(map1
);
7589 map2
= isl_map_normalize(map2
);
7592 equal
= map1
->n
== map2
->n
;
7593 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7594 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7607 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7609 return isl_map_plain_is_equal(map1
, map2
);
7612 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7614 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7615 (struct isl_map
*)set2
);
7618 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7620 return isl_set_plain_is_equal(set1
, set2
);
7623 /* Return an interval that ranges from min to max (inclusive)
7625 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7626 isl_int min
, isl_int max
)
7629 struct isl_basic_set
*bset
= NULL
;
7631 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7635 k
= isl_basic_set_alloc_inequality(bset
);
7638 isl_int_set_si(bset
->ineq
[k
][1], 1);
7639 isl_int_neg(bset
->ineq
[k
][0], min
);
7641 k
= isl_basic_set_alloc_inequality(bset
);
7644 isl_int_set_si(bset
->ineq
[k
][1], -1);
7645 isl_int_set(bset
->ineq
[k
][0], max
);
7649 isl_basic_set_free(bset
);
7653 /* Return the Cartesian product of the basic sets in list (in the given order).
7655 __isl_give isl_basic_set
*isl_basic_set_list_product(
7656 __isl_take
struct isl_basic_set_list
*list
)
7664 struct isl_basic_set
*product
= NULL
;
7668 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7669 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7670 nparam
= isl_basic_set_n_param(list
->p
[0]);
7671 dim
= isl_basic_set_n_dim(list
->p
[0]);
7672 extra
= list
->p
[0]->n_div
;
7673 n_eq
= list
->p
[0]->n_eq
;
7674 n_ineq
= list
->p
[0]->n_ineq
;
7675 for (i
= 1; i
< list
->n
; ++i
) {
7676 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7677 isl_assert(list
->ctx
,
7678 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7679 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7680 extra
+= list
->p
[i
]->n_div
;
7681 n_eq
+= list
->p
[i
]->n_eq
;
7682 n_ineq
+= list
->p
[i
]->n_ineq
;
7684 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7689 for (i
= 0; i
< list
->n
; ++i
) {
7690 isl_basic_set_add_constraints(product
,
7691 isl_basic_set_copy(list
->p
[i
]), dim
);
7692 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7694 isl_basic_set_list_free(list
);
7697 isl_basic_set_free(product
);
7698 isl_basic_set_list_free(list
);
7702 struct isl_basic_map
*isl_basic_map_product(
7703 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7705 isl_space
*dim_result
= NULL
;
7706 struct isl_basic_map
*bmap
;
7707 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7708 struct isl_dim_map
*dim_map1
, *dim_map2
;
7710 if (!bmap1
|| !bmap2
)
7713 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7714 bmap2
->dim
, isl_dim_param
), goto error
);
7715 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
7716 isl_space_copy(bmap2
->dim
));
7718 in1
= isl_basic_map_n_in(bmap1
);
7719 in2
= isl_basic_map_n_in(bmap2
);
7720 out1
= isl_basic_map_n_out(bmap1
);
7721 out2
= isl_basic_map_n_out(bmap2
);
7722 nparam
= isl_basic_map_n_param(bmap1
);
7724 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7725 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7726 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7727 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7728 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7729 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7730 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7731 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7732 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7733 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7734 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7736 bmap
= isl_basic_map_alloc_space(dim_result
,
7737 bmap1
->n_div
+ bmap2
->n_div
,
7738 bmap1
->n_eq
+ bmap2
->n_eq
,
7739 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7740 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7741 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7742 bmap
= isl_basic_map_simplify(bmap
);
7743 return isl_basic_map_finalize(bmap
);
7745 isl_basic_map_free(bmap1
);
7746 isl_basic_map_free(bmap2
);
7750 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7751 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7753 isl_basic_map
*prod
;
7755 prod
= isl_basic_map_product(bmap1
, bmap2
);
7756 prod
= isl_basic_map_flatten(prod
);
7760 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7761 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7763 return isl_basic_map_flat_range_product(bset1
, bset2
);
7766 __isl_give isl_basic_map
*isl_basic_map_domain_product(
7767 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7769 isl_space
*space_result
= NULL
;
7770 isl_basic_map
*bmap
;
7771 unsigned in1
, in2
, out
, nparam
, total
, pos
;
7772 struct isl_dim_map
*dim_map1
, *dim_map2
;
7774 if (!bmap1
|| !bmap2
)
7777 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
7778 isl_space_copy(bmap2
->dim
));
7780 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7781 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
7782 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
7783 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
7785 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
7786 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7787 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7788 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7789 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7790 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7791 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7792 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7793 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
7794 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
7795 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7797 bmap
= isl_basic_map_alloc_space(space_result
,
7798 bmap1
->n_div
+ bmap2
->n_div
,
7799 bmap1
->n_eq
+ bmap2
->n_eq
,
7800 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7801 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7802 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7803 bmap
= isl_basic_map_simplify(bmap
);
7804 return isl_basic_map_finalize(bmap
);
7806 isl_basic_map_free(bmap1
);
7807 isl_basic_map_free(bmap2
);
7811 __isl_give isl_basic_map
*isl_basic_map_range_product(
7812 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7814 isl_space
*dim_result
= NULL
;
7815 isl_basic_map
*bmap
;
7816 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7817 struct isl_dim_map
*dim_map1
, *dim_map2
;
7819 if (!bmap1
|| !bmap2
)
7822 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
7823 isl_space_copy(bmap2
->dim
));
7825 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7826 out1
= isl_basic_map_n_out(bmap1
);
7827 out2
= isl_basic_map_n_out(bmap2
);
7828 nparam
= isl_basic_map_n_param(bmap1
);
7830 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7831 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7832 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7833 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7834 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7835 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7836 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7837 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7838 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7839 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7840 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7842 bmap
= isl_basic_map_alloc_space(dim_result
,
7843 bmap1
->n_div
+ bmap2
->n_div
,
7844 bmap1
->n_eq
+ bmap2
->n_eq
,
7845 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7846 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7847 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7848 bmap
= isl_basic_map_simplify(bmap
);
7849 return isl_basic_map_finalize(bmap
);
7851 isl_basic_map_free(bmap1
);
7852 isl_basic_map_free(bmap2
);
7856 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
7857 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7859 isl_basic_map
*prod
;
7861 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
7862 prod
= isl_basic_map_flatten_range(prod
);
7866 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7867 __isl_take isl_map
*map2
,
7868 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
7869 __isl_take isl_space
*right
),
7870 __isl_give isl_basic_map
*(*basic_map_product
)(
7871 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7874 struct isl_map
*result
;
7880 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
7881 map2
->dim
, isl_dim_param
), goto error
);
7883 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7884 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7885 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7887 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
7888 isl_space_copy(map2
->dim
)),
7889 map1
->n
* map2
->n
, flags
);
7892 for (i
= 0; i
< map1
->n
; ++i
)
7893 for (j
= 0; j
< map2
->n
; ++j
) {
7894 struct isl_basic_map
*part
;
7895 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7896 isl_basic_map_copy(map2
->p
[j
]));
7897 if (isl_basic_map_is_empty(part
))
7898 isl_basic_map_free(part
);
7900 result
= isl_map_add_basic_map(result
, part
);
7913 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7915 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
7916 __isl_take isl_map
*map2
)
7918 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
7921 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
7922 __isl_take isl_map
*map2
)
7924 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
7927 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7929 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7930 __isl_take isl_map
*map2
)
7934 prod
= isl_map_product(map1
, map2
);
7935 prod
= isl_map_flatten(prod
);
7939 /* Given two set A and B, construct its Cartesian product A x B.
7941 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7943 return isl_map_range_product(set1
, set2
);
7946 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7947 __isl_take isl_set
*set2
)
7949 return isl_map_flat_range_product(set1
, set2
);
7952 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
7954 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
7955 __isl_take isl_map
*map2
)
7957 return map_product(map1
, map2
, &isl_space_domain_product
,
7958 &isl_basic_map_domain_product
);
7961 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7963 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
7964 __isl_take isl_map
*map2
)
7966 return map_product(map1
, map2
, &isl_space_range_product
,
7967 &isl_basic_map_range_product
);
7970 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
7971 __isl_take isl_map
*map2
)
7973 return isl_map_align_params_map_map_and(map1
, map2
,
7974 &map_domain_product_aligned
);
7977 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7978 __isl_take isl_map
*map2
)
7980 return isl_map_align_params_map_map_and(map1
, map2
,
7981 &map_range_product_aligned
);
7984 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
7986 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
7987 __isl_take isl_map
*map2
)
7991 prod
= isl_map_domain_product(map1
, map2
);
7992 prod
= isl_map_flatten_domain(prod
);
7996 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7998 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
7999 __isl_take isl_map
*map2
)
8003 prod
= isl_map_range_product(map1
, map2
);
8004 prod
= isl_map_flatten_range(prod
);
8008 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8011 uint32_t hash
= isl_hash_init();
8016 bmap
= isl_basic_map_copy(bmap
);
8017 bmap
= isl_basic_map_normalize(bmap
);
8020 total
= isl_basic_map_total_dim(bmap
);
8021 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8022 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8024 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8025 isl_hash_hash(hash
, c_hash
);
8027 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8028 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8030 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8031 isl_hash_hash(hash
, c_hash
);
8033 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8034 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8036 if (isl_int_is_zero(bmap
->div
[i
][0]))
8038 isl_hash_byte(hash
, i
& 0xFF);
8039 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8040 isl_hash_hash(hash
, c_hash
);
8042 isl_basic_map_free(bmap
);
8046 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8048 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8051 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8058 map
= isl_map_copy(map
);
8059 map
= isl_map_normalize(map
);
8063 hash
= isl_hash_init();
8064 for (i
= 0; i
< map
->n
; ++i
) {
8066 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8067 isl_hash_hash(hash
, bmap_hash
);
8075 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8077 return isl_map_get_hash((isl_map
*)set
);
8080 /* Check if the value for dimension dim is completely determined
8081 * by the values of the other parameters and variables.
8082 * That is, check if dimension dim is involved in an equality.
8084 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8091 nparam
= isl_basic_set_n_param(bset
);
8092 for (i
= 0; i
< bset
->n_eq
; ++i
)
8093 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8098 /* Check if the value for dimension dim is completely determined
8099 * by the values of the other parameters and variables.
8100 * That is, check if dimension dim is involved in an equality
8101 * for each of the subsets.
8103 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8109 for (i
= 0; i
< set
->n
; ++i
) {
8111 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8118 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8120 return set
? set
->n
: 0;
8123 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8124 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8131 for (i
= 0; i
< map
->n
; ++i
)
8132 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8138 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8139 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8146 for (i
= 0; i
< set
->n
; ++i
)
8147 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8153 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8160 bset
= isl_basic_set_cow(bset
);
8164 dim
= isl_basic_set_get_space(bset
);
8165 dim
= isl_space_lift(dim
, bset
->n_div
);
8168 isl_space_free(bset
->dim
);
8170 bset
->extra
-= bset
->n_div
;
8173 bset
= isl_basic_set_finalize(bset
);
8177 isl_basic_set_free(bset
);
8181 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8187 set
= isl_set_align_divs(set
);
8192 set
= isl_set_cow(set
);
8196 n_div
= set
->p
[0]->n_div
;
8197 dim
= isl_set_get_space(set
);
8198 dim
= isl_space_lift(dim
, n_div
);
8201 isl_space_free(set
->dim
);
8204 for (i
= 0; i
< set
->n
; ++i
) {
8205 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8216 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8219 struct isl_basic_map
*bmap
;
8226 set
= isl_set_align_divs(set
);
8231 dim
= isl_set_get_space(set
);
8232 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8234 return isl_map_identity(isl_space_map_from_set(dim
));
8237 n_div
= set
->p
[0]->n_div
;
8238 dim
= isl_space_map_from_set(dim
);
8239 n_param
= isl_space_dim(dim
, isl_dim_param
);
8240 n_set
= isl_space_dim(dim
, isl_dim_in
);
8241 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8242 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8243 for (i
= 0; i
< n_set
; ++i
)
8244 bmap
= var_equal(bmap
, i
);
8246 total
= n_param
+ n_set
+ n_set
+ n_div
;
8247 for (i
= 0; i
< n_div
; ++i
) {
8248 k
= isl_basic_map_alloc_inequality(bmap
);
8251 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8252 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8253 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8254 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8255 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8256 set
->p
[0]->div
[i
][0]);
8258 l
= isl_basic_map_alloc_inequality(bmap
);
8261 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8262 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8263 set
->p
[0]->div
[i
][0]);
8264 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8268 bmap
= isl_basic_map_simplify(bmap
);
8269 bmap
= isl_basic_map_finalize(bmap
);
8270 return isl_map_from_basic_map(bmap
);
8273 isl_basic_map_free(bmap
);
8277 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8285 dim
= isl_basic_set_total_dim(bset
);
8286 size
+= bset
->n_eq
* (1 + dim
);
8287 size
+= bset
->n_ineq
* (1 + dim
);
8288 size
+= bset
->n_div
* (2 + dim
);
8293 int isl_set_size(__isl_keep isl_set
*set
)
8301 for (i
= 0; i
< set
->n
; ++i
)
8302 size
+= isl_basic_set_size(set
->p
[i
]);
8307 /* Check if there is any lower bound (if lower == 0) and/or upper
8308 * bound (if upper == 0) on the specified dim.
8310 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8311 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8318 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8320 pos
+= isl_basic_map_offset(bmap
, type
);
8322 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8323 if (isl_int_is_zero(bmap
->div
[i
][0]))
8325 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8329 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8330 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8333 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8334 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8341 return lower
&& upper
;
8344 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8345 enum isl_dim_type type
, unsigned pos
)
8347 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8350 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8351 enum isl_dim_type type
, unsigned pos
)
8353 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8356 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8357 enum isl_dim_type type
, unsigned pos
)
8359 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8362 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8363 enum isl_dim_type type
, unsigned pos
)
8370 for (i
= 0; i
< map
->n
; ++i
) {
8372 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8373 if (bounded
< 0 || !bounded
)
8380 /* Return 1 if the specified dim is involved in both an upper bound
8381 * and a lower bound.
8383 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8384 enum isl_dim_type type
, unsigned pos
)
8386 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8389 static int has_bound(__isl_keep isl_map
*map
,
8390 enum isl_dim_type type
, unsigned pos
,
8391 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8392 enum isl_dim_type type
, unsigned pos
))
8399 for (i
= 0; i
< map
->n
; ++i
) {
8401 bounded
= fn(map
->p
[i
], type
, pos
);
8402 if (bounded
< 0 || bounded
)
8409 /* Return 1 if the specified dim is involved in any lower bound.
8411 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8412 enum isl_dim_type type
, unsigned pos
)
8414 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8417 /* Return 1 if the specified dim is involved in any upper bound.
8419 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8420 enum isl_dim_type type
, unsigned pos
)
8422 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8425 /* For each of the "n" variables starting at "first", determine
8426 * the sign of the variable and put the results in the first "n"
8427 * elements of the array "signs".
8429 * 1 means that the variable is non-negative
8430 * -1 means that the variable is non-positive
8431 * 0 means the variable attains both positive and negative values.
8433 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8434 unsigned first
, unsigned n
, int *signs
)
8436 isl_vec
*bound
= NULL
;
8437 struct isl_tab
*tab
= NULL
;
8438 struct isl_tab_undo
*snap
;
8441 if (!bset
|| !signs
)
8444 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8445 tab
= isl_tab_from_basic_set(bset
);
8449 isl_seq_clr(bound
->el
, bound
->size
);
8450 isl_int_set_si(bound
->el
[0], -1);
8452 snap
= isl_tab_snap(tab
);
8453 for (i
= 0; i
< n
; ++i
) {
8456 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8457 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8460 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8461 if (isl_tab_rollback(tab
, snap
) < 0)
8469 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8470 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8473 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8474 if (isl_tab_rollback(tab
, snap
) < 0)
8477 signs
[i
] = empty
? -1 : 0;
8481 isl_vec_free(bound
);
8485 isl_vec_free(bound
);
8489 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8490 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8492 if (!bset
|| !signs
)
8494 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8497 first
+= pos(bset
->dim
, type
) - 1;
8498 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8501 /* Check if the given basic map is obviously single-valued.
8502 * In particular, for each output dimension, check that there is
8503 * an equality that defines the output dimension in terms of
8504 * earlier dimensions.
8506 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8516 total
= 1 + isl_basic_map_total_dim(bmap
);
8517 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8518 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8520 for (i
= 0; i
< n_out
; ++i
) {
8521 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8522 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8524 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8525 total
- (o_out
+ i
+ 1)) == -1)
8528 if (j
>= bmap
->n_eq
)
8535 /* Check if the given map is obviously single-valued.
8537 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8546 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8549 /* Check if the given map is single-valued.
8554 * and check if the result is a subset of the identity mapping.
8556 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8563 sv
= isl_map_plain_is_single_valued(map
);
8567 test
= isl_map_reverse(isl_map_copy(map
));
8568 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8570 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8571 id
= isl_map_identity(dim
);
8573 sv
= isl_map_is_subset(test
, id
);
8581 int isl_map_is_injective(__isl_keep isl_map
*map
)
8585 map
= isl_map_copy(map
);
8586 map
= isl_map_reverse(map
);
8587 in
= isl_map_is_single_valued(map
);
8593 /* Check if the given map is obviously injective.
8595 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8599 map
= isl_map_copy(map
);
8600 map
= isl_map_reverse(map
);
8601 in
= isl_map_plain_is_single_valued(map
);
8607 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8611 sv
= isl_map_is_single_valued(map
);
8615 return isl_map_is_injective(map
);
8618 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8620 return isl_map_is_single_valued((isl_map
*)set
);
8623 int isl_map_is_translation(__isl_keep isl_map
*map
)
8628 delta
= isl_map_deltas(isl_map_copy(map
));
8629 ok
= isl_set_is_singleton(delta
);
8630 isl_set_free(delta
);
8635 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8637 if (isl_seq_first_non_zero(p
, pos
) != -1)
8639 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8644 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8653 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8656 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8657 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8658 for (j
= 0; j
< nvar
; ++j
) {
8659 int lower
= 0, upper
= 0;
8660 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8661 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8663 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8669 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8670 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8672 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8674 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8679 if (!lower
|| !upper
)
8686 int isl_set_is_box(__isl_keep isl_set
*set
)
8693 return isl_basic_set_is_box(set
->p
[0]);
8696 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8701 return isl_space_is_wrapping(bset
->dim
);
8704 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8709 return isl_space_is_wrapping(set
->dim
);
8712 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8714 bmap
= isl_basic_map_cow(bmap
);
8718 bmap
->dim
= isl_space_wrap(bmap
->dim
);
8722 bmap
= isl_basic_map_finalize(bmap
);
8724 return (isl_basic_set
*)bmap
;
8726 isl_basic_map_free(bmap
);
8730 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8734 map
= isl_map_cow(map
);
8738 for (i
= 0; i
< map
->n
; ++i
) {
8739 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8743 map
->dim
= isl_space_wrap(map
->dim
);
8747 return (isl_set
*)map
;
8753 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8755 bset
= isl_basic_set_cow(bset
);
8759 bset
->dim
= isl_space_unwrap(bset
->dim
);
8763 bset
= isl_basic_set_finalize(bset
);
8765 return (isl_basic_map
*)bset
;
8767 isl_basic_set_free(bset
);
8771 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8778 if (!isl_set_is_wrapping(set
))
8779 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8782 set
= isl_set_cow(set
);
8786 for (i
= 0; i
< set
->n
; ++i
) {
8787 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8792 set
->dim
= isl_space_unwrap(set
->dim
);
8796 return (isl_map
*)set
;
8802 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8803 enum isl_dim_type type
)
8808 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
8811 bmap
= isl_basic_map_cow(bmap
);
8815 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
8819 bmap
= isl_basic_map_finalize(bmap
);
8823 isl_basic_map_free(bmap
);
8827 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8828 enum isl_dim_type type
)
8835 if (!isl_space_is_named_or_nested(map
->dim
, type
))
8838 map
= isl_map_cow(map
);
8842 for (i
= 0; i
< map
->n
; ++i
) {
8843 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8847 map
->dim
= isl_space_reset(map
->dim
, type
);
8857 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8862 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8865 bmap
= isl_basic_map_cow(bmap
);
8869 bmap
->dim
= isl_space_flatten(bmap
->dim
);
8873 bmap
= isl_basic_map_finalize(bmap
);
8877 isl_basic_map_free(bmap
);
8881 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8883 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8886 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
8887 __isl_take isl_basic_map
*bmap
)
8892 if (!bmap
->dim
->nested
[0])
8895 bmap
= isl_basic_map_cow(bmap
);
8899 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
8903 bmap
= isl_basic_map_finalize(bmap
);
8907 isl_basic_map_free(bmap
);
8911 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
8912 __isl_take isl_basic_map
*bmap
)
8917 if (!bmap
->dim
->nested
[1])
8920 bmap
= isl_basic_map_cow(bmap
);
8924 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
8928 bmap
= isl_basic_map_finalize(bmap
);
8932 isl_basic_map_free(bmap
);
8936 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8943 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8946 map
= isl_map_cow(map
);
8950 for (i
= 0; i
< map
->n
; ++i
) {
8951 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8955 map
->dim
= isl_space_flatten(map
->dim
);
8965 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8967 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8970 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8972 isl_space
*dim
, *flat_dim
;
8975 dim
= isl_set_get_space(set
);
8976 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
8977 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
8978 map
= isl_map_intersect_domain(map
, set
);
8983 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
8990 if (!map
->dim
->nested
[0])
8993 map
= isl_map_cow(map
);
8997 for (i
= 0; i
< map
->n
; ++i
) {
8998 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9002 map
->dim
= isl_space_flatten_domain(map
->dim
);
9012 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9019 if (!map
->dim
->nested
[1])
9022 map
= isl_map_cow(map
);
9026 for (i
= 0; i
< map
->n
; ++i
) {
9027 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9031 map
->dim
= isl_space_flatten_range(map
->dim
);
9041 /* Reorder the dimensions of "bmap" according to the given dim_map
9042 * and set the dimension specification to "dim".
9044 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9045 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9049 bmap
= isl_basic_map_cow(bmap
);
9050 if (!bmap
|| !dim
|| !dim_map
)
9053 res
= isl_basic_map_alloc_space(dim
,
9054 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9055 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9056 res
= isl_basic_map_finalize(res
);
9060 isl_basic_map_free(bmap
);
9061 isl_space_free(dim
);
9065 /* Reorder the dimensions of "map" according to given reordering.
9067 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9068 __isl_take isl_reordering
*r
)
9071 struct isl_dim_map
*dim_map
;
9073 map
= isl_map_cow(map
);
9074 dim_map
= isl_dim_map_from_reordering(r
);
9075 if (!map
|| !r
|| !dim_map
)
9078 for (i
= 0; i
< map
->n
; ++i
) {
9079 struct isl_dim_map
*dim_map_i
;
9081 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9083 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9084 isl_space_copy(r
->dim
), dim_map_i
);
9090 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9092 isl_reordering_free(r
);
9098 isl_reordering_free(r
);
9102 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9103 __isl_take isl_reordering
*r
)
9105 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9108 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9109 __isl_take isl_space
*model
)
9116 ctx
= isl_space_get_ctx(model
);
9117 if (!isl_space_has_named_params(model
))
9118 isl_die(ctx
, isl_error_invalid
,
9119 "model has unnamed parameters", goto error
);
9120 if (!isl_space_has_named_params(map
->dim
))
9121 isl_die(ctx
, isl_error_invalid
,
9122 "relation has unnamed parameters", goto error
);
9123 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9124 isl_reordering
*exp
;
9126 model
= isl_space_drop_dims(model
, isl_dim_in
,
9127 0, isl_space_dim(model
, isl_dim_in
));
9128 model
= isl_space_drop_dims(model
, isl_dim_out
,
9129 0, isl_space_dim(model
, isl_dim_out
));
9130 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9131 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9132 map
= isl_map_realign(map
, exp
);
9135 isl_space_free(model
);
9138 isl_space_free(model
);
9143 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9144 __isl_take isl_space
*model
)
9146 return isl_map_align_params(set
, model
);
9149 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9150 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9151 enum isl_dim_type c2
, enum isl_dim_type c3
,
9152 enum isl_dim_type c4
, enum isl_dim_type c5
)
9154 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9155 struct isl_mat
*mat
;
9161 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9162 isl_basic_map_total_dim(bmap
) + 1);
9165 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9166 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9167 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9168 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9169 isl_int_set(mat
->row
[i
][pos
],
9170 bmap
->eq
[i
][off
+ k
]);
9178 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9179 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9180 enum isl_dim_type c2
, enum isl_dim_type c3
,
9181 enum isl_dim_type c4
, enum isl_dim_type c5
)
9183 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9184 struct isl_mat
*mat
;
9190 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9191 isl_basic_map_total_dim(bmap
) + 1);
9194 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9195 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9196 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9197 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9198 isl_int_set(mat
->row
[i
][pos
],
9199 bmap
->ineq
[i
][off
+ k
]);
9207 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9208 __isl_take isl_space
*dim
,
9209 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9210 enum isl_dim_type c2
, enum isl_dim_type c3
,
9211 enum isl_dim_type c4
, enum isl_dim_type c5
)
9213 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9214 isl_basic_map
*bmap
;
9220 if (!dim
|| !eq
|| !ineq
)
9223 if (eq
->n_col
!= ineq
->n_col
)
9224 isl_die(dim
->ctx
, isl_error_invalid
,
9225 "equalities and inequalities matrices should have "
9226 "same number of columns", goto error
);
9228 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9230 if (eq
->n_col
< total
)
9231 isl_die(dim
->ctx
, isl_error_invalid
,
9232 "number of columns too small", goto error
);
9234 extra
= eq
->n_col
- total
;
9236 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9237 eq
->n_row
, ineq
->n_row
);
9240 for (i
= 0; i
< extra
; ++i
) {
9241 k
= isl_basic_map_alloc_div(bmap
);
9244 isl_int_set_si(bmap
->div
[k
][0], 0);
9246 for (i
= 0; i
< eq
->n_row
; ++i
) {
9247 l
= isl_basic_map_alloc_equality(bmap
);
9250 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9251 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9252 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9253 isl_int_set(bmap
->eq
[l
][off
+ k
],
9259 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9260 l
= isl_basic_map_alloc_inequality(bmap
);
9263 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9264 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9265 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9266 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9273 isl_space_free(dim
);
9279 isl_space_free(dim
);
9285 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9286 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9287 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9289 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9290 c1
, c2
, c3
, c4
, isl_dim_in
);
9293 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9294 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9295 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9297 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9298 c1
, c2
, c3
, c4
, isl_dim_in
);
9301 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9302 __isl_take isl_space
*dim
,
9303 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9304 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9306 return (isl_basic_set
*)
9307 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9308 c1
, c2
, c3
, c4
, isl_dim_in
);
9311 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9316 return isl_space_can_zip(bmap
->dim
);
9319 int isl_map_can_zip(__isl_keep isl_map
*map
)
9324 return isl_space_can_zip(map
->dim
);
9327 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9328 * (A -> C) -> (B -> D).
9330 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9339 if (!isl_basic_map_can_zip(bmap
))
9340 isl_die(bmap
->ctx
, isl_error_invalid
,
9341 "basic map cannot be zipped", goto error
);
9342 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9343 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9344 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9345 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9346 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9349 bmap
->dim
= isl_space_zip(bmap
->dim
);
9354 isl_basic_map_free(bmap
);
9358 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9359 * (A -> C) -> (B -> D).
9361 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9368 if (!isl_map_can_zip(map
))
9369 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9372 map
= isl_map_cow(map
);
9376 for (i
= 0; i
< map
->n
; ++i
) {
9377 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9382 map
->dim
= isl_space_zip(map
->dim
);
9392 /* Construct a basic map mapping the domain of the affine expression
9393 * to a one-dimensional range prescribed by the affine expression.
9395 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9399 isl_local_space
*ls
;
9400 isl_basic_map
*bmap
;
9405 ls
= isl_aff_get_local_space(aff
);
9406 ls
= isl_local_space_from_domain(ls
);
9407 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
9408 bmap
= isl_basic_map_from_local_space(ls
);
9409 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9410 k
= isl_basic_map_alloc_equality(bmap
);
9414 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9415 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9416 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9417 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9418 aff
->v
->size
- (pos
+ 1));
9421 bmap
= isl_basic_map_finalize(bmap
);
9425 isl_basic_map_free(bmap
);
9429 /* Construct a basic map mapping a domain in the given space to
9430 * to an n-dimensional range, with n the number of elements in the list,
9431 * where each coordinate in the range is prescribed by the
9432 * corresponding affine expression.
9433 * The domains of all affine expressions in the list are assumed to match
9436 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9437 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9441 isl_basic_map
*bmap
;
9446 dim
= isl_space_from_domain(domain_dim
);
9447 bmap
= isl_basic_map_universe(dim
);
9449 for (i
= 0; i
< list
->n
; ++i
) {
9451 isl_basic_map
*bmap_i
;
9453 aff
= isl_aff_copy(list
->p
[i
]);
9454 bmap_i
= isl_basic_map_from_aff(aff
);
9456 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9459 isl_aff_list_free(list
);
9463 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9464 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9466 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9469 /* Add a constraint imposing that the given two dimensions are equal.
9471 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9472 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9474 isl_basic_map
*bmap
= NULL
;
9480 if (pos1
>= isl_map_dim(map
, type1
))
9481 isl_die(map
->ctx
, isl_error_invalid
,
9482 "index out of bounds", goto error
);
9483 if (pos2
>= isl_map_dim(map
, type2
))
9484 isl_die(map
->ctx
, isl_error_invalid
,
9485 "index out of bounds", goto error
);
9487 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9488 i
= isl_basic_map_alloc_equality(bmap
);
9491 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9492 pos1
+= isl_basic_map_offset(bmap
, type1
);
9493 pos2
+= isl_basic_map_offset(bmap
, type2
);
9494 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9495 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9496 bmap
= isl_basic_map_finalize(bmap
);
9498 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9502 isl_basic_map_free(bmap
);
9507 /* Add a constraint imposing that the given two dimensions have opposite values.
9509 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9510 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9512 isl_basic_map
*bmap
= NULL
;
9518 if (pos1
>= isl_map_dim(map
, type1
))
9519 isl_die(map
->ctx
, isl_error_invalid
,
9520 "index out of bounds", goto error
);
9521 if (pos2
>= isl_map_dim(map
, type2
))
9522 isl_die(map
->ctx
, isl_error_invalid
,
9523 "index out of bounds", goto error
);
9525 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9526 i
= isl_basic_map_alloc_equality(bmap
);
9529 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9530 pos1
+= isl_basic_map_offset(bmap
, type1
);
9531 pos2
+= isl_basic_map_offset(bmap
, type2
);
9532 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9533 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9534 bmap
= isl_basic_map_finalize(bmap
);
9536 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9540 isl_basic_map_free(bmap
);