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
)
495 bmap
= isl_basic_map_cow(bmap
);
498 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
503 isl_basic_map_free(bmap
);
507 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
508 enum isl_dim_type type
, unsigned pos
, const char *s
)
512 map
= isl_map_cow(map
);
516 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
520 for (i
= 0; i
< map
->n
; ++i
) {
521 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
532 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
533 __isl_take isl_basic_set
*bset
,
534 enum isl_dim_type type
, unsigned pos
, const char *s
)
536 return (isl_basic_set
*)isl_basic_map_set_dim_name(
537 (isl_basic_map
*)bset
, type
, pos
, s
);
540 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
541 enum isl_dim_type type
, unsigned pos
, const char *s
)
543 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
546 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
547 enum isl_dim_type type
, unsigned pos
)
549 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
552 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
553 enum isl_dim_type type
, unsigned pos
)
555 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
558 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
559 enum isl_dim_type type
, unsigned pos
)
561 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
564 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
565 enum isl_dim_type type
, unsigned pos
)
567 return isl_map_has_dim_id(set
, type
, pos
);
570 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
571 enum isl_dim_type type
, unsigned pos
)
573 return isl_map_get_dim_id(set
, type
, pos
);
576 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
577 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
579 map
= isl_map_cow(map
);
581 return isl_id_free(id
);
583 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
585 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
588 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
589 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
591 return isl_map_set_dim_id(set
, type
, pos
, id
);
594 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
595 __isl_keep isl_id
*id
)
599 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
602 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
603 __isl_keep isl_id
*id
)
605 return isl_map_find_dim_by_id(set
, type
, id
);
608 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
613 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
616 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
620 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
623 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
625 return isl_basic_map_is_rational(bset
);
628 /* Is this basic set a parameter domain?
630 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
634 return isl_space_is_params(bset
->dim
);
637 /* Is this set a parameter domain?
639 int isl_set_is_params(__isl_keep isl_set
*set
)
643 return isl_space_is_params(set
->dim
);
646 /* Is this map actually a parameter domain?
647 * Users should never call this function. Outside of isl,
648 * a map can never be a parameter domain.
650 int isl_map_is_params(__isl_keep isl_map
*map
)
654 return isl_space_is_params(map
->dim
);
657 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
658 struct isl_basic_map
*bmap
, unsigned extra
,
659 unsigned n_eq
, unsigned n_ineq
)
662 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
667 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
668 if (isl_blk_is_error(bmap
->block
))
671 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
676 bmap
->block2
= isl_blk_empty();
679 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
680 if (isl_blk_is_error(bmap
->block2
))
683 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
688 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
689 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
691 for (i
= 0; i
< extra
; ++i
)
692 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
696 bmap
->c_size
= n_eq
+ n_ineq
;
697 bmap
->eq
= bmap
->ineq
+ n_ineq
;
706 isl_basic_map_free(bmap
);
710 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
711 unsigned nparam
, unsigned dim
, unsigned extra
,
712 unsigned n_eq
, unsigned n_ineq
)
714 struct isl_basic_map
*bmap
;
717 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
721 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
722 return (struct isl_basic_set
*)bmap
;
725 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
726 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
728 struct isl_basic_map
*bmap
;
731 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
732 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
733 return (struct isl_basic_set
*)bmap
;
739 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
740 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
742 struct isl_basic_map
*bmap
;
746 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
751 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
757 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
758 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
759 unsigned n_eq
, unsigned n_ineq
)
761 struct isl_basic_map
*bmap
;
764 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
768 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
772 static void dup_constraints(
773 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
776 unsigned total
= isl_basic_map_total_dim(src
);
778 for (i
= 0; i
< src
->n_eq
; ++i
) {
779 int j
= isl_basic_map_alloc_equality(dst
);
780 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
783 for (i
= 0; i
< src
->n_ineq
; ++i
) {
784 int j
= isl_basic_map_alloc_inequality(dst
);
785 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
788 for (i
= 0; i
< src
->n_div
; ++i
) {
789 int j
= isl_basic_map_alloc_div(dst
);
790 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
792 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
795 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
797 struct isl_basic_map
*dup
;
801 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
802 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
805 dup_constraints(dup
, bmap
);
806 dup
->flags
= bmap
->flags
;
807 dup
->sample
= isl_vec_copy(bmap
->sample
);
811 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
813 struct isl_basic_map
*dup
;
815 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
816 return (struct isl_basic_set
*)dup
;
819 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
824 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
828 return isl_basic_set_dup(bset
);
831 struct isl_set
*isl_set_copy(struct isl_set
*set
)
840 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
845 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
849 bmap
= isl_basic_map_dup(bmap
);
851 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
855 struct isl_map
*isl_map_copy(struct isl_map
*map
)
864 void isl_basic_map_free(struct isl_basic_map
*bmap
)
872 isl_ctx_deref(bmap
->ctx
);
874 isl_blk_free(bmap
->ctx
, bmap
->block2
);
876 isl_blk_free(bmap
->ctx
, bmap
->block
);
877 isl_vec_free(bmap
->sample
);
878 isl_space_free(bmap
->dim
);
882 void isl_basic_set_free(struct isl_basic_set
*bset
)
884 isl_basic_map_free((struct isl_basic_map
*)bset
);
887 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
889 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
892 __isl_give isl_map
*isl_map_align_params_map_map_and(
893 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
894 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
895 __isl_take isl_map
*map2
))
899 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
900 return fn(map1
, map2
);
901 if (!isl_space_has_named_params(map1
->dim
) ||
902 !isl_space_has_named_params(map2
->dim
))
903 isl_die(map1
->ctx
, isl_error_invalid
,
904 "unaligned unnamed parameters", goto error
);
905 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
906 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
907 return fn(map1
, map2
);
914 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
915 __isl_keep isl_map
*map2
,
916 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
922 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
923 return fn(map1
, map2
);
924 if (!isl_space_has_named_params(map1
->dim
) ||
925 !isl_space_has_named_params(map2
->dim
))
926 isl_die(map1
->ctx
, isl_error_invalid
,
927 "unaligned unnamed parameters", return -1);
928 map1
= isl_map_copy(map1
);
929 map2
= isl_map_copy(map2
);
930 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
931 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
938 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
944 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
945 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
947 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
948 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
949 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
950 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
951 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
952 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
954 int j
= isl_basic_map_alloc_inequality(bmap
);
958 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
959 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
966 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
967 bmap
->extra
- bmap
->n_div
);
971 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
973 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
976 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
980 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
985 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
987 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
990 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
995 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
997 if (pos
!= bmap
->n_eq
- 1) {
999 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1000 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1006 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1008 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1011 void isl_basic_map_inequality_to_equality(
1012 struct isl_basic_map
*bmap
, unsigned pos
)
1016 t
= bmap
->ineq
[pos
];
1017 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1018 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1023 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1024 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1025 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1026 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1029 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1031 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1034 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1036 struct isl_ctx
*ctx
;
1040 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1041 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1042 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1043 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1044 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1045 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1046 1 + isl_basic_map_total_dim(bmap
),
1047 bmap
->extra
- bmap
->n_div
);
1048 return bmap
->n_ineq
++;
1051 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1053 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1056 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1060 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1065 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1067 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1070 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1075 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1077 if (pos
!= bmap
->n_ineq
- 1) {
1078 t
= bmap
->ineq
[pos
];
1079 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1080 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1081 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1087 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1089 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1092 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1097 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1100 k
= isl_basic_map_alloc_equality(bmap
);
1103 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1106 isl_basic_map_free(bmap
);
1110 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1113 return (isl_basic_set
*)
1114 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1117 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1122 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1125 k
= isl_basic_map_alloc_inequality(bmap
);
1128 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1131 isl_basic_map_free(bmap
);
1135 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1138 return (isl_basic_set
*)
1139 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1142 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1146 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1147 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1148 1 + 1 + isl_basic_map_total_dim(bmap
),
1149 bmap
->extra
- bmap
->n_div
);
1150 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1151 return bmap
->n_div
++;
1154 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1156 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1159 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1163 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1168 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1170 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1173 /* Copy constraint from src to dst, putting the vars of src at offset
1174 * dim_off in dst and the divs of src at offset div_off in dst.
1175 * If both sets are actually map, then dim_off applies to the input
1178 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1179 struct isl_basic_map
*src_map
, isl_int
*src
,
1180 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1182 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1183 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1184 unsigned src_in
= isl_basic_map_n_in(src_map
);
1185 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1186 unsigned src_out
= isl_basic_map_n_out(src_map
);
1187 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1188 isl_int_set(dst
[0], src
[0]);
1189 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1190 if (dst_nparam
> src_nparam
)
1191 isl_seq_clr(dst
+1+src_nparam
,
1192 dst_nparam
- src_nparam
);
1193 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1194 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1196 isl_min(dst_in
-in_off
, src_in
));
1197 if (dst_in
-in_off
> src_in
)
1198 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1199 dst_in
- in_off
- src_in
);
1200 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1201 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1202 src
+1+src_nparam
+src_in
,
1203 isl_min(dst_out
-out_off
, src_out
));
1204 if (dst_out
-out_off
> src_out
)
1205 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1206 dst_out
- out_off
- src_out
);
1207 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1208 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1209 src
+1+src_nparam
+src_in
+src_out
,
1210 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1211 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1212 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1213 div_off
+src_map
->n_div
,
1214 dst_map
->n_div
- div_off
- src_map
->n_div
);
1217 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1218 struct isl_basic_map
*src_map
, isl_int
*src
,
1219 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1221 isl_int_set(dst
[0], src
[0]);
1222 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1225 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1226 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1231 if (!bmap1
|| !bmap2
)
1234 div_off
= bmap1
->n_div
;
1236 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1237 int i1
= isl_basic_map_alloc_equality(bmap1
);
1240 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1241 i_pos
, o_pos
, div_off
);
1244 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1245 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1248 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1249 i_pos
, o_pos
, div_off
);
1252 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1253 int i1
= isl_basic_map_alloc_div(bmap1
);
1256 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1257 i_pos
, o_pos
, div_off
);
1260 isl_basic_map_free(bmap2
);
1265 isl_basic_map_free(bmap1
);
1266 isl_basic_map_free(bmap2
);
1270 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1271 struct isl_basic_set
*bset2
, unsigned pos
)
1273 return (struct isl_basic_set
*)
1274 add_constraints((struct isl_basic_map
*)bset1
,
1275 (struct isl_basic_map
*)bset2
, 0, pos
);
1278 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1279 __isl_take isl_space
*dim
, unsigned extra
,
1280 unsigned n_eq
, unsigned n_ineq
)
1282 struct isl_basic_map
*ext
;
1292 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1293 base
->extra
>= base
->n_div
+ extra
;
1295 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1296 room_for_ineq(base
, n_ineq
)) {
1297 isl_space_free(dim
);
1301 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1302 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1303 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1304 extra
+= base
->extra
;
1306 n_ineq
+= base
->n_ineq
;
1308 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1314 ext
->sample
= isl_vec_copy(base
->sample
);
1315 flags
= base
->flags
;
1316 ext
= add_constraints(ext
, base
, 0, 0);
1319 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1325 isl_space_free(dim
);
1326 isl_basic_map_free(base
);
1330 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1331 __isl_take isl_space
*dim
, unsigned extra
,
1332 unsigned n_eq
, unsigned n_ineq
)
1334 return (struct isl_basic_set
*)
1335 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1336 extra
, n_eq
, n_ineq
);
1339 struct isl_basic_map
*isl_basic_map_extend_constraints(
1340 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1344 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1348 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1349 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1350 unsigned n_eq
, unsigned n_ineq
)
1352 struct isl_basic_map
*bmap
;
1357 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1361 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1364 isl_basic_map_free(base
);
1368 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1369 unsigned nparam
, unsigned dim
, unsigned extra
,
1370 unsigned n_eq
, unsigned n_ineq
)
1372 return (struct isl_basic_set
*)
1373 isl_basic_map_extend((struct isl_basic_map
*)base
,
1374 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1377 struct isl_basic_set
*isl_basic_set_extend_constraints(
1378 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1380 return (struct isl_basic_set
*)
1381 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1385 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1387 return (struct isl_basic_set
*)
1388 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1391 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1396 if (bmap
->ref
> 1) {
1398 bmap
= isl_basic_map_dup(bmap
);
1401 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1405 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1413 return isl_set_dup(set
);
1416 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1424 return isl_map_dup(map
);
1427 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1428 unsigned a_len
, unsigned b_len
)
1430 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1431 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1432 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1435 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1436 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1444 isl_assert(bmap
->ctx
,
1445 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1447 if (n1
== 0 || n2
== 0)
1450 bmap
= isl_basic_map_cow(bmap
);
1454 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1455 if (isl_blk_is_error(blk
))
1458 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1460 bmap
->eq
[i
] + pos
, n1
, n2
);
1462 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1464 bmap
->ineq
[i
] + pos
, n1
, n2
);
1466 for (i
= 0; i
< bmap
->n_div
; ++i
)
1468 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1470 isl_blk_free(bmap
->ctx
, blk
);
1472 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1473 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1474 return isl_basic_map_finalize(bmap
);
1476 isl_basic_map_free(bmap
);
1480 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1481 __isl_take isl_basic_set
*bset
, unsigned n
)
1486 nparam
= isl_basic_set_n_param(bset
);
1487 dim
= isl_basic_set_n_dim(bset
);
1488 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1490 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1492 isl_basic_set_free(bset
);
1496 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1502 total
= isl_basic_map_total_dim(bmap
);
1503 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1504 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1506 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1508 i
= isl_basic_map_alloc_equality(bmap
);
1512 isl_int_set_si(bmap
->eq
[i
][0], 1);
1513 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1514 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1515 isl_vec_free(bmap
->sample
);
1516 bmap
->sample
= NULL
;
1517 return isl_basic_map_finalize(bmap
);
1519 isl_basic_map_free(bmap
);
1523 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1525 return (struct isl_basic_set
*)
1526 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1529 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1532 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1533 isl_int
*t
= bmap
->div
[a
];
1534 bmap
->div
[a
] = bmap
->div
[b
];
1537 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1538 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1540 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1541 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1543 for (i
= 0; i
< bmap
->n_div
; ++i
)
1544 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1545 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1548 /* Eliminate the specified n dimensions starting at first from the
1549 * constraints using Fourier-Motzkin. The dimensions themselves
1552 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1553 enum isl_dim_type type
, unsigned first
, unsigned n
)
1562 map
= isl_map_cow(map
);
1566 for (i
= 0; i
< map
->n
; ++i
) {
1567 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1577 /* Eliminate the specified n dimensions starting at first from the
1578 * constraints using Fourier-Motzkin. The dimensions themselves
1581 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1582 enum isl_dim_type type
, unsigned first
, unsigned n
)
1584 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1587 /* Eliminate the specified n dimensions starting at first from the
1588 * constraints using Fourier-Motzkin. The dimensions themselves
1591 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1592 unsigned first
, unsigned n
)
1594 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1597 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1598 __isl_take isl_basic_map
*bmap
)
1602 bmap
= isl_basic_map_eliminate_vars(bmap
,
1603 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1607 return isl_basic_map_finalize(bmap
);
1610 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1611 __isl_take isl_basic_set
*bset
)
1613 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1614 (struct isl_basic_map
*)bset
);
1617 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1626 map
= isl_map_cow(map
);
1630 for (i
= 0; i
< map
->n
; ++i
) {
1631 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1641 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1643 return isl_map_remove_divs(set
);
1646 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1647 enum isl_dim_type type
, unsigned first
, unsigned n
)
1651 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1653 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1655 bmap
= isl_basic_map_eliminate_vars(bmap
,
1656 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1659 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1661 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1664 isl_basic_map_free(bmap
);
1668 /* Return true if the definition of the given div (recursively) involves
1669 * any of the given variables.
1671 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1672 unsigned first
, unsigned n
)
1675 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1677 if (isl_int_is_zero(bmap
->div
[div
][0]))
1679 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1682 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1683 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1685 if (div_involves_vars(bmap
, i
, first
, n
))
1692 /* Remove all divs (recursively) involving any of the given dimensions
1693 * in their definitions.
1695 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1696 __isl_take isl_basic_map
*bmap
,
1697 enum isl_dim_type type
, unsigned first
, unsigned n
)
1703 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1705 first
+= isl_basic_map_offset(bmap
, type
);
1707 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1708 if (!div_involves_vars(bmap
, i
, first
, n
))
1710 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1718 isl_basic_map_free(bmap
);
1722 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1723 enum isl_dim_type type
, unsigned first
, unsigned n
)
1732 map
= isl_map_cow(map
);
1736 for (i
= 0; i
< map
->n
; ++i
) {
1737 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1748 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1749 enum isl_dim_type type
, unsigned first
, unsigned n
)
1751 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1755 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
1756 enum isl_dim_type type
, unsigned first
, unsigned n
)
1763 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
1764 isl_die(bmap
->ctx
, isl_error_invalid
,
1765 "index out of bounds", return -1);
1767 first
+= isl_basic_map_offset(bmap
, type
);
1768 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1769 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
1771 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1772 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
1778 int isl_map_involves_dims(__isl_keep isl_map
*map
,
1779 enum isl_dim_type type
, unsigned first
, unsigned n
)
1786 if (first
+ n
> isl_map_dim(map
, type
))
1787 isl_die(map
->ctx
, isl_error_invalid
,
1788 "index out of bounds", return -1);
1790 for (i
= 0; i
< map
->n
; ++i
) {
1791 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
1793 if (involves
< 0 || involves
)
1800 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1801 enum isl_dim_type type
, unsigned first
, unsigned n
)
1803 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
1806 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1807 enum isl_dim_type type
, unsigned first
, unsigned n
)
1809 return isl_map_involves_dims(set
, type
, first
, n
);
1812 /* Return true if the definition of the given div is unknown or depends
1815 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1818 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1820 if (isl_int_is_zero(bmap
->div
[div
][0]))
1823 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1824 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1826 if (div_is_unknown(bmap
, i
))
1833 /* Remove all divs that are unknown or defined in terms of unknown divs.
1835 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1836 __isl_take isl_basic_map
*bmap
)
1843 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1844 if (!div_is_unknown(bmap
, i
))
1846 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1852 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1861 map
= isl_map_cow(map
);
1865 for (i
= 0; i
< map
->n
; ++i
) {
1866 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1876 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1878 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1881 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1882 __isl_take isl_basic_set
*bset
,
1883 enum isl_dim_type type
, unsigned first
, unsigned n
)
1885 return (isl_basic_set
*)
1886 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1889 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1890 enum isl_dim_type type
, unsigned first
, unsigned n
)
1897 map
= isl_map_cow(map
);
1900 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1902 for (i
= 0; i
< map
->n
; ++i
) {
1903 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1904 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1908 map
= isl_map_drop(map
, type
, first
, n
);
1915 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1916 enum isl_dim_type type
, unsigned first
, unsigned n
)
1918 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1921 /* Project out n inputs starting at first using Fourier-Motzkin */
1922 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1923 unsigned first
, unsigned n
)
1925 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1928 static void dump_term(struct isl_basic_map
*bmap
,
1929 isl_int c
, int pos
, FILE *out
)
1932 unsigned in
= isl_basic_map_n_in(bmap
);
1933 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1934 unsigned nparam
= isl_basic_map_n_param(bmap
);
1936 isl_int_print(out
, c
, 0);
1938 if (!isl_int_is_one(c
))
1939 isl_int_print(out
, c
, 0);
1940 if (pos
< 1 + nparam
) {
1941 name
= isl_space_get_dim_name(bmap
->dim
,
1942 isl_dim_param
, pos
- 1);
1944 fprintf(out
, "%s", name
);
1946 fprintf(out
, "p%d", pos
- 1);
1947 } else if (pos
< 1 + nparam
+ in
)
1948 fprintf(out
, "i%d", pos
- 1 - nparam
);
1949 else if (pos
< 1 + nparam
+ dim
)
1950 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1952 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1956 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1957 int sign
, FILE *out
)
1961 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1965 for (i
= 0, first
= 1; i
< len
; ++i
) {
1966 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1969 fprintf(out
, " + ");
1971 isl_int_abs(v
, c
[i
]);
1972 dump_term(bmap
, v
, i
, out
);
1979 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1980 const char *op
, FILE *out
, int indent
)
1984 fprintf(out
, "%*s", indent
, "");
1986 dump_constraint_sign(bmap
, c
, 1, out
);
1987 fprintf(out
, " %s ", op
);
1988 dump_constraint_sign(bmap
, c
, -1, out
);
1992 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1993 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
1995 fprintf(out
, "%*s", indent
, "");
1996 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2001 static void dump_constraints(struct isl_basic_map
*bmap
,
2002 isl_int
**c
, unsigned n
,
2003 const char *op
, FILE *out
, int indent
)
2007 for (i
= 0; i
< n
; ++i
)
2008 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2011 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2015 unsigned total
= isl_basic_map_total_dim(bmap
);
2017 for (j
= 0; j
< 1 + total
; ++j
) {
2018 if (isl_int_is_zero(exp
[j
]))
2020 if (!first
&& isl_int_is_pos(exp
[j
]))
2022 dump_term(bmap
, exp
[j
], j
, out
);
2027 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2031 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2032 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2034 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2035 fprintf(out
, "%*s", indent
, "");
2036 fprintf(out
, "e%d = [(", i
);
2037 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2039 isl_int_print(out
, bmap
->div
[i
][0], 0);
2040 fprintf(out
, "]\n");
2044 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2045 FILE *out
, int indent
)
2048 fprintf(out
, "null basic set\n");
2052 fprintf(out
, "%*s", indent
, "");
2053 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2054 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2055 bset
->extra
, bset
->flags
);
2056 dump((struct isl_basic_map
*)bset
, out
, indent
);
2059 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2060 FILE *out
, int indent
)
2063 fprintf(out
, "null basic map\n");
2067 fprintf(out
, "%*s", indent
, "");
2068 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2069 "flags: %x, n_name: %d\n",
2071 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2072 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2073 dump(bmap
, out
, indent
);
2076 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2081 total
= isl_basic_map_total_dim(bmap
);
2082 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2083 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2084 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2085 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2089 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2092 struct isl_set
*set
;
2096 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2097 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2098 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2099 sizeof(struct isl_set
) +
2100 (n
- 1) * sizeof(struct isl_basic_set
*));
2104 set
->ctx
= dim
->ctx
;
2105 isl_ctx_ref(set
->ctx
);
2113 isl_space_free(dim
);
2117 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2118 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2120 struct isl_set
*set
;
2123 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2127 set
= isl_set_alloc_space(dims
, n
, flags
);
2131 /* Make sure "map" has room for at least "n" more basic maps.
2133 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2136 struct isl_map
*grown
= NULL
;
2140 isl_assert(map
->ctx
, n
>= 0, goto error
);
2141 if (map
->n
+ n
<= map
->size
)
2143 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2146 for (i
= 0; i
< map
->n
; ++i
) {
2147 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2155 isl_map_free(grown
);
2160 /* Make sure "set" has room for at least "n" more basic sets.
2162 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2164 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2167 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2170 struct isl_set
*dup
;
2175 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2178 for (i
= 0; i
< set
->n
; ++i
)
2179 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2183 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2185 return isl_map_from_basic_map(bset
);
2188 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2190 struct isl_map
*map
;
2195 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2196 return isl_map_add_basic_map(map
, bmap
);
2199 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2200 __isl_take isl_basic_set
*bset
)
2202 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2203 (struct isl_basic_map
*)bset
);
2206 void isl_set_free(struct isl_set
*set
)
2216 isl_ctx_deref(set
->ctx
);
2217 for (i
= 0; i
< set
->n
; ++i
)
2218 isl_basic_set_free(set
->p
[i
]);
2219 isl_space_free(set
->dim
);
2223 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2228 fprintf(out
, "null set\n");
2232 fprintf(out
, "%*s", indent
, "");
2233 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2234 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2236 for (i
= 0; i
< set
->n
; ++i
) {
2237 fprintf(out
, "%*s", indent
, "");
2238 fprintf(out
, "basic set %d:\n", i
);
2239 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2243 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2248 fprintf(out
, "null map\n");
2252 fprintf(out
, "%*s", indent
, "");
2253 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2254 "flags: %x, n_name: %d\n",
2255 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2256 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2257 for (i
= 0; i
< map
->n
; ++i
) {
2258 fprintf(out
, "%*s", indent
, "");
2259 fprintf(out
, "basic map %d:\n", i
);
2260 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2264 struct isl_basic_map
*isl_basic_map_intersect_domain(
2265 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2267 struct isl_basic_map
*bmap_domain
;
2272 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2273 bset
->dim
, isl_dim_param
), goto error
);
2275 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2276 isl_assert(bset
->ctx
,
2277 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2279 bmap
= isl_basic_map_cow(bmap
);
2282 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2283 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2284 bmap_domain
= isl_basic_map_from_domain(bset
);
2285 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2287 bmap
= isl_basic_map_simplify(bmap
);
2288 return isl_basic_map_finalize(bmap
);
2290 isl_basic_map_free(bmap
);
2291 isl_basic_set_free(bset
);
2295 struct isl_basic_map
*isl_basic_map_intersect_range(
2296 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2298 struct isl_basic_map
*bmap_range
;
2303 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2304 bset
->dim
, isl_dim_param
), goto error
);
2306 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2307 isl_assert(bset
->ctx
,
2308 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2310 if (isl_basic_set_is_universe(bset
)) {
2311 isl_basic_set_free(bset
);
2315 bmap
= isl_basic_map_cow(bmap
);
2318 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2319 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2320 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2321 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2323 bmap
= isl_basic_map_simplify(bmap
);
2324 return isl_basic_map_finalize(bmap
);
2326 isl_basic_map_free(bmap
);
2327 isl_basic_set_free(bset
);
2331 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2337 total
= 1 + isl_basic_map_total_dim(bmap
);
2338 if (total
!= vec
->size
)
2343 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2344 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2345 if (!isl_int_is_zero(s
)) {
2351 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2352 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2353 if (isl_int_is_neg(s
)) {
2364 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2366 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2369 struct isl_basic_map
*isl_basic_map_intersect(
2370 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2372 struct isl_vec
*sample
= NULL
;
2374 if (!bmap1
|| !bmap2
)
2377 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2378 bmap2
->dim
, isl_dim_param
), goto error
);
2379 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2380 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2381 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2382 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2383 return isl_basic_map_intersect(bmap2
, bmap1
);
2385 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2386 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2387 isl_assert(bmap1
->ctx
,
2388 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2390 if (bmap1
->sample
&&
2391 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2392 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2393 sample
= isl_vec_copy(bmap1
->sample
);
2394 else if (bmap2
->sample
&&
2395 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2396 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2397 sample
= isl_vec_copy(bmap2
->sample
);
2399 bmap1
= isl_basic_map_cow(bmap1
);
2402 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2403 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2404 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2407 isl_vec_free(sample
);
2409 isl_vec_free(bmap1
->sample
);
2410 bmap1
->sample
= sample
;
2413 bmap1
= isl_basic_map_simplify(bmap1
);
2414 return isl_basic_map_finalize(bmap1
);
2417 isl_vec_free(sample
);
2418 isl_basic_map_free(bmap1
);
2419 isl_basic_map_free(bmap2
);
2423 struct isl_basic_set
*isl_basic_set_intersect(
2424 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2426 return (struct isl_basic_set
*)
2427 isl_basic_map_intersect(
2428 (struct isl_basic_map
*)bset1
,
2429 (struct isl_basic_map
*)bset2
);
2432 /* Special case of isl_map_intersect, where both map1 and map2
2433 * are convex, without any divs and such that either map1 or map2
2434 * contains a single constraint. This constraint is then simply
2435 * added to the other map.
2437 static __isl_give isl_map
*map_intersect_add_constraint(
2438 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2440 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2441 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2442 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2443 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2445 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2446 return isl_map_intersect(map2
, map1
);
2448 isl_assert(map2
->ctx
,
2449 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2451 map1
= isl_map_cow(map1
);
2454 if (isl_map_plain_is_empty(map1
)) {
2458 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2459 if (map2
->p
[0]->n_eq
== 1)
2460 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2462 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2463 map2
->p
[0]->ineq
[0]);
2465 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2466 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2470 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2471 isl_basic_map_free(map1
->p
[0]);
2484 /* map2 may be either a parameter domain or a map living in the same
2487 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2488 __isl_take isl_map
*map2
)
2491 struct isl_map
*result
;
2497 if (isl_map_plain_is_empty(map1
) &&
2498 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2502 if (isl_map_plain_is_empty(map2
) &&
2503 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2508 if (map1
->n
== 1 && map2
->n
== 1 &&
2509 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2510 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2511 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2512 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2513 return map_intersect_add_constraint(map1
, map2
);
2515 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2516 isl_space_dim(map2
->dim
, isl_dim_param
))
2517 isl_assert(map1
->ctx
,
2518 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2520 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2521 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2522 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2524 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2525 map1
->n
* map2
->n
, flags
);
2528 for (i
= 0; i
< map1
->n
; ++i
)
2529 for (j
= 0; j
< map2
->n
; ++j
) {
2530 struct isl_basic_map
*part
;
2531 part
= isl_basic_map_intersect(
2532 isl_basic_map_copy(map1
->p
[i
]),
2533 isl_basic_map_copy(map2
->p
[j
]));
2534 if (isl_basic_map_is_empty(part
))
2535 isl_basic_map_free(part
);
2537 result
= isl_map_add_basic_map(result
, part
);
2550 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2551 __isl_take isl_map
*map2
)
2555 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2556 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2557 "spaces don't match", goto error
);
2558 return map_intersect_internal(map1
, map2
);
2565 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2566 __isl_take isl_map
*map2
)
2568 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2571 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2573 return (struct isl_set
*)
2574 isl_map_intersect((struct isl_map
*)set1
,
2575 (struct isl_map
*)set2
);
2578 /* map_intersect_internal accepts intersections
2579 * with parameter domains, so we can just call that function.
2581 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2582 __isl_take isl_set
*params
)
2584 return map_intersect_internal(map
, params
);
2587 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2588 __isl_take isl_map
*map2
)
2590 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2593 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2594 __isl_take isl_set
*params
)
2596 return isl_map_intersect_params(set
, params
);
2599 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2602 struct isl_basic_set
*bset
;
2607 bmap
= isl_basic_map_cow(bmap
);
2610 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2611 in
= isl_basic_map_n_in(bmap
);
2612 bset
= isl_basic_set_from_basic_map(bmap
);
2613 bset
= isl_basic_set_swap_vars(bset
, in
);
2614 return isl_basic_map_from_basic_set(bset
, dim
);
2617 static __isl_give isl_basic_map
*basic_map_space_reset(
2618 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2622 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2625 space
= isl_basic_map_get_space(bmap
);
2626 space
= isl_space_reset(space
, type
);
2627 bmap
= isl_basic_map_reset_space(bmap
, space
);
2631 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2632 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2635 struct isl_basic_map
*res
;
2636 struct isl_dim_map
*dim_map
;
2637 unsigned total
, off
;
2638 enum isl_dim_type t
;
2641 return basic_map_space_reset(bmap
, type
);
2646 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2648 total
= isl_basic_map_total_dim(bmap
) + n
;
2649 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2651 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2653 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2655 unsigned size
= isl_basic_map_dim(bmap
, t
);
2656 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2658 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2659 pos
, size
- pos
, off
+ pos
+ n
);
2661 off
+= isl_space_dim(res_dim
, t
);
2663 isl_dim_map_div(dim_map
, bmap
, off
);
2665 res
= isl_basic_map_alloc_space(res_dim
,
2666 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2667 if (isl_basic_map_is_rational(bmap
))
2668 res
= isl_basic_map_set_rational(res
);
2669 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2670 return isl_basic_map_finalize(res
);
2673 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2674 enum isl_dim_type type
, unsigned n
)
2678 return isl_basic_map_insert(bmap
, type
,
2679 isl_basic_map_dim(bmap
, type
), n
);
2682 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2683 enum isl_dim_type type
, unsigned n
)
2687 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2688 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2690 isl_basic_set_free(bset
);
2694 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2695 enum isl_dim_type type
)
2699 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
2702 space
= isl_map_get_space(map
);
2703 space
= isl_space_reset(space
, type
);
2704 map
= isl_map_reset_space(map
, space
);
2708 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2709 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2714 return map_space_reset(map
, type
);
2716 map
= isl_map_cow(map
);
2720 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2724 for (i
= 0; i
< map
->n
; ++i
) {
2725 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2736 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2737 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2739 return isl_map_insert_dims(set
, type
, pos
, n
);
2742 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2743 enum isl_dim_type type
, unsigned n
)
2747 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2750 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2751 enum isl_dim_type type
, unsigned n
)
2755 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2756 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2762 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2763 __isl_take isl_basic_map
*bmap
,
2764 enum isl_dim_type dst_type
, unsigned dst_pos
,
2765 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2767 struct isl_dim_map
*dim_map
;
2768 struct isl_basic_map
*res
;
2769 enum isl_dim_type t
;
2770 unsigned total
, off
;
2777 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2780 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2783 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2785 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2786 pos(bmap
->dim
, src_type
) + src_pos
+
2787 ((src_type
< dst_type
) ? n
: 0)) {
2788 bmap
= isl_basic_map_cow(bmap
);
2792 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2793 src_type
, src_pos
, n
);
2797 bmap
= isl_basic_map_finalize(bmap
);
2802 total
= isl_basic_map_total_dim(bmap
);
2803 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2806 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2807 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2808 if (t
== dst_type
) {
2809 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2812 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2815 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2816 dst_pos
, size
- dst_pos
, off
);
2817 off
+= size
- dst_pos
;
2818 } else if (t
== src_type
) {
2819 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2822 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2823 src_pos
+ n
, size
- src_pos
- n
, off
);
2824 off
+= size
- src_pos
- n
;
2826 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2830 isl_dim_map_div(dim_map
, bmap
, off
);
2832 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2833 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2834 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2836 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2837 src_type
, src_pos
, n
);
2841 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2842 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2843 bmap
= isl_basic_map_finalize(bmap
);
2847 isl_basic_map_free(bmap
);
2851 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2852 enum isl_dim_type dst_type
, unsigned dst_pos
,
2853 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2855 return (isl_basic_set
*)isl_basic_map_move_dims(
2856 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2859 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2860 enum isl_dim_type dst_type
, unsigned dst_pos
,
2861 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2865 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2866 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2867 src_type
, src_pos
, n
);
2873 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2874 enum isl_dim_type dst_type
, unsigned dst_pos
,
2875 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2884 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2887 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2890 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2892 map
= isl_map_cow(map
);
2896 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2900 for (i
= 0; i
< map
->n
; ++i
) {
2901 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2903 src_type
, src_pos
, n
);
2914 /* Move the specified dimensions to the last columns right before
2915 * the divs. Don't change the dimension specification of bmap.
2916 * That's the responsibility of the caller.
2918 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2919 enum isl_dim_type type
, unsigned first
, unsigned n
)
2921 struct isl_dim_map
*dim_map
;
2922 struct isl_basic_map
*res
;
2923 enum isl_dim_type t
;
2924 unsigned total
, off
;
2928 if (pos(bmap
->dim
, type
) + first
+ n
==
2929 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2932 total
= isl_basic_map_total_dim(bmap
);
2933 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2936 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2937 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2939 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2942 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2943 first
, n
, total
- bmap
->n_div
- n
);
2944 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2945 first
+ n
, size
- (first
+ n
), off
);
2946 off
+= size
- (first
+ n
);
2948 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2952 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2954 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2955 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2956 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2960 /* Turn the n dimensions of type type, starting at first
2961 * into existentially quantified variables.
2963 __isl_give isl_basic_map
*isl_basic_map_project_out(
2964 __isl_take isl_basic_map
*bmap
,
2965 enum isl_dim_type type
, unsigned first
, unsigned n
)
2978 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2979 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2981 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2984 bmap
= move_last(bmap
, type
, first
, n
);
2985 bmap
= isl_basic_map_cow(bmap
);
2989 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
2990 old
= bmap
->block2
.data
;
2991 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2992 (bmap
->extra
+ n
) * (1 + row_size
));
2993 if (!bmap
->block2
.data
)
2995 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2998 for (i
= 0; i
< n
; ++i
) {
2999 new_div
[i
] = bmap
->block2
.data
+
3000 (bmap
->extra
+ i
) * (1 + row_size
);
3001 isl_seq_clr(new_div
[i
], 1 + row_size
);
3003 for (i
= 0; i
< bmap
->extra
; ++i
)
3004 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3006 bmap
->div
= new_div
;
3010 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3013 bmap
= isl_basic_map_simplify(bmap
);
3014 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3015 return isl_basic_map_finalize(bmap
);
3017 isl_basic_map_free(bmap
);
3021 /* Turn the n dimensions of type type, starting at first
3022 * into existentially quantified variables.
3024 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3025 enum isl_dim_type type
, unsigned first
, unsigned n
)
3027 return (isl_basic_set
*)isl_basic_map_project_out(
3028 (isl_basic_map
*)bset
, type
, first
, n
);
3031 /* Turn the n dimensions of type type, starting at first
3032 * into existentially quantified variables.
3034 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3035 enum isl_dim_type type
, unsigned first
, unsigned n
)
3045 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3047 map
= isl_map_cow(map
);
3051 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3055 for (i
= 0; i
< map
->n
; ++i
) {
3056 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3067 /* Turn the n dimensions of type type, starting at first
3068 * into existentially quantified variables.
3070 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3071 enum isl_dim_type type
, unsigned first
, unsigned n
)
3073 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3076 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3080 for (i
= 0; i
< n
; ++i
) {
3081 j
= isl_basic_map_alloc_div(bmap
);
3084 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3088 isl_basic_map_free(bmap
);
3092 struct isl_basic_map
*isl_basic_map_apply_range(
3093 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3095 isl_space
*dim_result
= NULL
;
3096 struct isl_basic_map
*bmap
;
3097 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3098 struct isl_dim_map
*dim_map1
, *dim_map2
;
3100 if (!bmap1
|| !bmap2
)
3103 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3104 isl_space_copy(bmap2
->dim
));
3106 n_in
= isl_basic_map_n_in(bmap1
);
3107 n_out
= isl_basic_map_n_out(bmap2
);
3108 n
= isl_basic_map_n_out(bmap1
);
3109 nparam
= isl_basic_map_n_param(bmap1
);
3111 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3112 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3113 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3114 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3115 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3116 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3117 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3118 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3119 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3120 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3121 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3123 bmap
= isl_basic_map_alloc_space(dim_result
,
3124 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3125 bmap1
->n_eq
+ bmap2
->n_eq
,
3126 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3127 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3128 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3129 bmap
= add_divs(bmap
, n
);
3130 bmap
= isl_basic_map_simplify(bmap
);
3131 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3132 return isl_basic_map_finalize(bmap
);
3134 isl_basic_map_free(bmap1
);
3135 isl_basic_map_free(bmap2
);
3139 struct isl_basic_set
*isl_basic_set_apply(
3140 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3145 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3148 return (struct isl_basic_set
*)
3149 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3151 isl_basic_set_free(bset
);
3152 isl_basic_map_free(bmap
);
3156 struct isl_basic_map
*isl_basic_map_apply_domain(
3157 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3159 if (!bmap1
|| !bmap2
)
3162 isl_assert(bmap1
->ctx
,
3163 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3164 isl_assert(bmap1
->ctx
,
3165 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3168 bmap1
= isl_basic_map_reverse(bmap1
);
3169 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3170 return isl_basic_map_reverse(bmap1
);
3172 isl_basic_map_free(bmap1
);
3173 isl_basic_map_free(bmap2
);
3177 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3178 * A \cap B -> f(A) + f(B)
3180 struct isl_basic_map
*isl_basic_map_sum(
3181 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3183 unsigned n_in
, n_out
, nparam
, total
, pos
;
3184 struct isl_basic_map
*bmap
= NULL
;
3185 struct isl_dim_map
*dim_map1
, *dim_map2
;
3188 if (!bmap1
|| !bmap2
)
3191 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3194 nparam
= isl_basic_map_n_param(bmap1
);
3195 n_in
= isl_basic_map_n_in(bmap1
);
3196 n_out
= isl_basic_map_n_out(bmap1
);
3198 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3199 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3200 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3201 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3202 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3203 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3204 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3205 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3206 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3207 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3208 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3210 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3211 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3212 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3213 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3214 for (i
= 0; i
< n_out
; ++i
) {
3215 int j
= isl_basic_map_alloc_equality(bmap
);
3218 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3219 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3220 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3221 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3223 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3224 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3225 bmap
= add_divs(bmap
, 2 * n_out
);
3227 bmap
= isl_basic_map_simplify(bmap
);
3228 return isl_basic_map_finalize(bmap
);
3230 isl_basic_map_free(bmap
);
3231 isl_basic_map_free(bmap1
);
3232 isl_basic_map_free(bmap2
);
3236 /* Given two maps A -> f(A) and B -> g(B), construct a map
3237 * A \cap B -> f(A) + f(B)
3239 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3241 struct isl_map
*result
;
3247 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3249 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3250 map1
->n
* map2
->n
, 0);
3253 for (i
= 0; i
< map1
->n
; ++i
)
3254 for (j
= 0; j
< map2
->n
; ++j
) {
3255 struct isl_basic_map
*part
;
3256 part
= isl_basic_map_sum(
3257 isl_basic_map_copy(map1
->p
[i
]),
3258 isl_basic_map_copy(map2
->p
[j
]));
3259 if (isl_basic_map_is_empty(part
))
3260 isl_basic_map_free(part
);
3262 result
= isl_map_add_basic_map(result
, part
);
3275 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3276 __isl_take isl_set
*set2
)
3278 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3281 /* Given a basic map A -> f(A), construct A -> -f(A).
3283 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3288 bmap
= isl_basic_map_cow(bmap
);
3292 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3293 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3294 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3295 for (j
= 0; j
< n
; ++j
)
3296 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3297 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3298 for (j
= 0; j
< n
; ++j
)
3299 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3300 for (i
= 0; i
< bmap
->n_div
; ++i
)
3301 for (j
= 0; j
< n
; ++j
)
3302 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3303 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3304 return isl_basic_map_finalize(bmap
);
3307 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3309 return isl_basic_map_neg(bset
);
3312 /* Given a map A -> f(A), construct A -> -f(A).
3314 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3318 map
= isl_map_cow(map
);
3322 for (i
= 0; i
< map
->n
; ++i
) {
3323 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3334 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3336 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3339 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3340 * A -> floor(f(A)/d).
3342 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3345 unsigned n_in
, n_out
, nparam
, total
, pos
;
3346 struct isl_basic_map
*result
= NULL
;
3347 struct isl_dim_map
*dim_map
;
3353 nparam
= isl_basic_map_n_param(bmap
);
3354 n_in
= isl_basic_map_n_in(bmap
);
3355 n_out
= isl_basic_map_n_out(bmap
);
3357 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3358 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3359 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3360 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3361 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3362 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3364 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3365 bmap
->n_div
+ n_out
,
3366 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3367 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3368 result
= add_divs(result
, n_out
);
3369 for (i
= 0; i
< n_out
; ++i
) {
3371 j
= isl_basic_map_alloc_inequality(result
);
3374 isl_seq_clr(result
->ineq
[j
], 1+total
);
3375 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3376 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3377 j
= isl_basic_map_alloc_inequality(result
);
3380 isl_seq_clr(result
->ineq
[j
], 1+total
);
3381 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3382 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3383 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3386 result
= isl_basic_map_simplify(result
);
3387 return isl_basic_map_finalize(result
);
3389 isl_basic_map_free(result
);
3393 /* Given a map A -> f(A) and an integer d, construct a map
3394 * A -> floor(f(A)/d).
3396 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3400 map
= isl_map_cow(map
);
3404 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3405 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3406 for (i
= 0; i
< map
->n
; ++i
) {
3407 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3418 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3424 i
= isl_basic_map_alloc_equality(bmap
);
3427 nparam
= isl_basic_map_n_param(bmap
);
3428 n_in
= isl_basic_map_n_in(bmap
);
3429 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3430 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3431 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3432 return isl_basic_map_finalize(bmap
);
3434 isl_basic_map_free(bmap
);
3438 /* Add a constraints to "bmap" expressing i_pos < o_pos
3440 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3446 i
= isl_basic_map_alloc_inequality(bmap
);
3449 nparam
= isl_basic_map_n_param(bmap
);
3450 n_in
= isl_basic_map_n_in(bmap
);
3451 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3452 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3453 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3454 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3455 return isl_basic_map_finalize(bmap
);
3457 isl_basic_map_free(bmap
);
3461 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3463 static __isl_give isl_basic_map
*var_less_or_equal(
3464 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3470 i
= isl_basic_map_alloc_inequality(bmap
);
3473 nparam
= isl_basic_map_n_param(bmap
);
3474 n_in
= isl_basic_map_n_in(bmap
);
3475 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3476 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3477 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3478 return isl_basic_map_finalize(bmap
);
3480 isl_basic_map_free(bmap
);
3484 /* Add a constraints to "bmap" expressing i_pos > o_pos
3486 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3492 i
= isl_basic_map_alloc_inequality(bmap
);
3495 nparam
= isl_basic_map_n_param(bmap
);
3496 n_in
= isl_basic_map_n_in(bmap
);
3497 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3498 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3499 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3500 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3501 return isl_basic_map_finalize(bmap
);
3503 isl_basic_map_free(bmap
);
3507 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3509 static __isl_give isl_basic_map
*var_more_or_equal(
3510 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3516 i
= isl_basic_map_alloc_inequality(bmap
);
3519 nparam
= isl_basic_map_n_param(bmap
);
3520 n_in
= isl_basic_map_n_in(bmap
);
3521 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3522 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3523 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3524 return isl_basic_map_finalize(bmap
);
3526 isl_basic_map_free(bmap
);
3530 __isl_give isl_basic_map
*isl_basic_map_equal(
3531 __isl_take isl_space
*dim
, unsigned n_equal
)
3534 struct isl_basic_map
*bmap
;
3535 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3538 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3539 bmap
= var_equal(bmap
, i
);
3540 return isl_basic_map_finalize(bmap
);
3543 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3545 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3549 struct isl_basic_map
*bmap
;
3550 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3553 for (i
= 0; i
< pos
&& bmap
; ++i
)
3554 bmap
= var_equal(bmap
, i
);
3556 bmap
= var_less(bmap
, pos
);
3557 return isl_basic_map_finalize(bmap
);
3560 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3562 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3563 __isl_take isl_space
*dim
, unsigned pos
)
3566 isl_basic_map
*bmap
;
3568 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3569 for (i
= 0; i
< pos
; ++i
)
3570 bmap
= var_equal(bmap
, i
);
3571 bmap
= var_less_or_equal(bmap
, pos
);
3572 return isl_basic_map_finalize(bmap
);
3575 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3577 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3581 struct isl_basic_map
*bmap
;
3582 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3585 for (i
= 0; i
< pos
&& bmap
; ++i
)
3586 bmap
= var_equal(bmap
, i
);
3588 bmap
= var_more(bmap
, pos
);
3589 return isl_basic_map_finalize(bmap
);
3592 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3594 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3595 __isl_take isl_space
*dim
, unsigned pos
)
3598 isl_basic_map
*bmap
;
3600 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3601 for (i
= 0; i
< pos
; ++i
)
3602 bmap
= var_equal(bmap
, i
);
3603 bmap
= var_more_or_equal(bmap
, pos
);
3604 return isl_basic_map_finalize(bmap
);
3607 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3608 unsigned n
, int equal
)
3610 struct isl_map
*map
;
3613 if (n
== 0 && equal
)
3614 return isl_map_universe(dims
);
3616 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3618 for (i
= 0; i
+ 1 < n
; ++i
)
3619 map
= isl_map_add_basic_map(map
,
3620 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3623 map
= isl_map_add_basic_map(map
,
3624 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3626 map
= isl_map_add_basic_map(map
,
3627 isl_basic_map_less_at(dims
, n
- 1));
3629 isl_space_free(dims
);
3634 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3638 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3641 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3643 return map_lex_lte_first(dim
, n
, 0);
3646 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3648 return map_lex_lte_first(dim
, n
, 1);
3651 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3653 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3656 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3658 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3661 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3662 unsigned n
, int equal
)
3664 struct isl_map
*map
;
3667 if (n
== 0 && equal
)
3668 return isl_map_universe(dims
);
3670 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3672 for (i
= 0; i
+ 1 < n
; ++i
)
3673 map
= isl_map_add_basic_map(map
,
3674 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3677 map
= isl_map_add_basic_map(map
,
3678 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3680 map
= isl_map_add_basic_map(map
,
3681 isl_basic_map_more_at(dims
, n
- 1));
3683 isl_space_free(dims
);
3688 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3692 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3695 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3697 return map_lex_gte_first(dim
, n
, 0);
3700 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3702 return map_lex_gte_first(dim
, n
, 1);
3705 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3707 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3710 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3712 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3715 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3716 __isl_take isl_set
*set2
)
3719 map
= isl_map_lex_le(isl_set_get_space(set1
));
3720 map
= isl_map_intersect_domain(map
, set1
);
3721 map
= isl_map_intersect_range(map
, set2
);
3725 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3726 __isl_take isl_set
*set2
)
3729 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3730 map
= isl_map_intersect_domain(map
, set1
);
3731 map
= isl_map_intersect_range(map
, set2
);
3735 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3736 __isl_take isl_set
*set2
)
3739 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3740 map
= isl_map_intersect_domain(map
, set1
);
3741 map
= isl_map_intersect_range(map
, set2
);
3745 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3746 __isl_take isl_set
*set2
)
3749 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3750 map
= isl_map_intersect_domain(map
, set1
);
3751 map
= isl_map_intersect_range(map
, set2
);
3755 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3756 __isl_take isl_map
*map2
)
3759 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3760 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3761 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3765 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3766 __isl_take isl_map
*map2
)
3769 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3770 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3771 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3775 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3776 __isl_take isl_map
*map2
)
3779 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3780 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3781 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3785 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3786 __isl_take isl_map
*map2
)
3789 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3790 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3791 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3795 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3796 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3798 struct isl_basic_map
*bmap
;
3800 bset
= isl_basic_set_cow(bset
);
3804 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3805 isl_space_free(bset
->dim
);
3806 bmap
= (struct isl_basic_map
*) bset
;
3808 return isl_basic_map_finalize(bmap
);
3810 isl_basic_set_free(bset
);
3811 isl_space_free(dim
);
3815 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3819 if (bmap
->dim
->n_in
== 0)
3820 return (struct isl_basic_set
*)bmap
;
3821 bmap
= isl_basic_map_cow(bmap
);
3824 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3827 bmap
= isl_basic_map_finalize(bmap
);
3828 return (struct isl_basic_set
*)bmap
;
3830 isl_basic_map_free(bmap
);
3834 /* For a div d = floor(f/m), add the constraints
3837 * -(f-(n-1)) + m d >= 0
3839 * Note that the second constraint is the negation of
3843 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3844 unsigned pos
, isl_int
*div
)
3847 unsigned total
= isl_basic_map_total_dim(bmap
);
3849 i
= isl_basic_map_alloc_inequality(bmap
);
3852 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3853 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3855 j
= isl_basic_map_alloc_inequality(bmap
);
3858 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3859 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3860 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3864 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3865 unsigned pos
, isl_int
*div
)
3867 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3871 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3873 unsigned total
= isl_basic_map_total_dim(bmap
);
3874 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3876 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3880 struct isl_basic_set
*isl_basic_map_underlying_set(
3881 struct isl_basic_map
*bmap
)
3885 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3887 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3888 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3889 return (struct isl_basic_set
*)bmap
;
3890 bmap
= isl_basic_map_cow(bmap
);
3893 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3896 bmap
->extra
-= bmap
->n_div
;
3898 bmap
= isl_basic_map_finalize(bmap
);
3899 return (struct isl_basic_set
*)bmap
;
3904 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3905 __isl_take isl_basic_set
*bset
)
3907 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3910 struct isl_basic_map
*isl_basic_map_overlying_set(
3911 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3913 struct isl_basic_map
*bmap
;
3914 struct isl_ctx
*ctx
;
3921 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3922 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3923 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3925 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3926 isl_basic_map_free(like
);
3927 return (struct isl_basic_map
*)bset
;
3929 bset
= isl_basic_set_cow(bset
);
3932 total
= bset
->dim
->n_out
+ bset
->extra
;
3933 bmap
= (struct isl_basic_map
*)bset
;
3934 isl_space_free(bmap
->dim
);
3935 bmap
->dim
= isl_space_copy(like
->dim
);
3938 bmap
->n_div
= like
->n_div
;
3939 bmap
->extra
+= like
->n_div
;
3943 ltotal
= total
- bmap
->extra
+ like
->extra
;
3946 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3947 bmap
->extra
* (1 + 1 + total
));
3948 if (isl_blk_is_error(bmap
->block2
))
3950 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3954 for (i
= 0; i
< bmap
->extra
; ++i
)
3955 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3956 for (i
= 0; i
< like
->n_div
; ++i
) {
3957 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3958 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3960 bmap
= isl_basic_map_extend_constraints(bmap
,
3961 0, 2 * like
->n_div
);
3962 for (i
= 0; i
< like
->n_div
; ++i
) {
3963 if (isl_int_is_zero(bmap
->div
[i
][0]))
3965 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3969 isl_basic_map_free(like
);
3970 bmap
= isl_basic_map_simplify(bmap
);
3971 bmap
= isl_basic_map_finalize(bmap
);
3974 isl_basic_map_free(like
);
3975 isl_basic_set_free(bset
);
3979 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3980 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3982 return (struct isl_basic_set
*)
3983 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3986 struct isl_set
*isl_set_from_underlying_set(
3987 struct isl_set
*set
, struct isl_basic_set
*like
)
3993 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3995 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3996 isl_basic_set_free(like
);
3999 set
= isl_set_cow(set
);
4002 for (i
= 0; i
< set
->n
; ++i
) {
4003 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4004 isl_basic_set_copy(like
));
4008 isl_space_free(set
->dim
);
4009 set
->dim
= isl_space_copy(like
->dim
);
4012 isl_basic_set_free(like
);
4015 isl_basic_set_free(like
);
4020 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4024 map
= isl_map_cow(map
);
4027 map
->dim
= isl_space_cow(map
->dim
);
4031 for (i
= 1; i
< map
->n
; ++i
)
4032 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4034 for (i
= 0; i
< map
->n
; ++i
) {
4035 map
->p
[i
] = (struct isl_basic_map
*)
4036 isl_basic_map_underlying_set(map
->p
[i
]);
4041 map
->dim
= isl_space_underlying(map
->dim
, 0);
4043 isl_space_free(map
->dim
);
4044 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4048 return (struct isl_set
*)map
;
4054 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4056 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4059 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4060 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4062 bmap
= isl_basic_map_cow(bmap
);
4066 isl_space_free(bmap
->dim
);
4069 bmap
= isl_basic_map_finalize(bmap
);
4073 isl_basic_map_free(bmap
);
4074 isl_space_free(dim
);
4078 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4079 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4081 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4085 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4086 __isl_take isl_space
*dim
)
4090 map
= isl_map_cow(map
);
4094 for (i
= 0; i
< map
->n
; ++i
) {
4095 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4096 isl_space_copy(dim
));
4100 isl_space_free(map
->dim
);
4106 isl_space_free(dim
);
4110 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4111 __isl_take isl_space
*dim
)
4113 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4116 /* Compute the parameter domain of the given basic set.
4118 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4123 if (isl_basic_set_is_params(bset
))
4126 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4127 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4128 space
= isl_basic_set_get_space(bset
);
4129 space
= isl_space_params(space
);
4130 bset
= isl_basic_set_reset_space(bset
, space
);
4134 /* Compute the parameter domain of the given set.
4136 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4141 if (isl_set_is_params(set
))
4144 n
= isl_set_dim(set
, isl_dim_set
);
4145 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4146 space
= isl_set_get_space(set
);
4147 space
= isl_space_params(space
);
4148 set
= isl_set_reset_space(set
, space
);
4152 /* Construct a zero-dimensional set with the given parameter domain.
4154 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4157 space
= isl_set_get_space(set
);
4158 space
= isl_space_set_from_params(space
);
4159 set
= isl_set_reset_space(set
, space
);
4163 /* Compute the parameter domain of the given map.
4165 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4170 n
= isl_map_dim(map
, isl_dim_in
);
4171 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4172 n
= isl_map_dim(map
, isl_dim_out
);
4173 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4174 space
= isl_map_get_space(map
);
4175 space
= isl_space_params(space
);
4176 map
= isl_map_reset_space(map
, space
);
4180 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4183 struct isl_basic_set
*domain
;
4189 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4191 n_in
= isl_basic_map_n_in(bmap
);
4192 n_out
= isl_basic_map_n_out(bmap
);
4193 domain
= isl_basic_set_from_basic_map(bmap
);
4194 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4196 domain
= isl_basic_set_reset_space(domain
, dim
);
4201 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4205 return isl_space_may_be_set(bmap
->dim
);
4208 /* Is this basic map actually a set?
4209 * Users should never call this function. Outside of isl,
4210 * the type should indicate whether something is a set or a map.
4212 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4216 return isl_space_is_set(bmap
->dim
);
4219 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4223 if (isl_basic_map_is_set(bmap
))
4225 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4228 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4229 __isl_take isl_basic_map
*bmap
)
4233 isl_basic_map
*domain
;
4234 int nparam
, n_in
, n_out
;
4237 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4238 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4239 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4241 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4242 domain
= isl_basic_map_universe(dim
);
4244 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4245 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4246 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4248 total
= isl_basic_map_total_dim(bmap
);
4250 for (i
= 0; i
< n_in
; ++i
) {
4251 k
= isl_basic_map_alloc_equality(bmap
);
4254 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4255 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4256 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4259 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4260 return isl_basic_map_finalize(bmap
);
4262 isl_basic_map_free(bmap
);
4266 __isl_give isl_basic_map
*isl_basic_map_range_map(
4267 __isl_take isl_basic_map
*bmap
)
4271 isl_basic_map
*range
;
4272 int nparam
, n_in
, n_out
;
4275 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4276 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4277 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4279 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4280 range
= isl_basic_map_universe(dim
);
4282 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4283 bmap
= isl_basic_map_apply_range(bmap
, range
);
4284 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4286 total
= isl_basic_map_total_dim(bmap
);
4288 for (i
= 0; i
< n_out
; ++i
) {
4289 k
= isl_basic_map_alloc_equality(bmap
);
4292 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4293 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4294 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4297 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4298 return isl_basic_map_finalize(bmap
);
4300 isl_basic_map_free(bmap
);
4304 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4308 return isl_space_may_be_set(map
->dim
);
4311 /* Is this map actually a set?
4312 * Users should never call this function. Outside of isl,
4313 * the type should indicate whether something is a set or a map.
4315 int isl_map_is_set(__isl_keep isl_map
*map
)
4319 return isl_space_is_set(map
->dim
);
4322 struct isl_set
*isl_map_range(struct isl_map
*map
)
4325 struct isl_set
*set
;
4329 if (isl_map_is_set(map
))
4330 return (isl_set
*)map
;
4332 map
= isl_map_cow(map
);
4336 set
= (struct isl_set
*) map
;
4337 set
->dim
= isl_space_range(set
->dim
);
4340 for (i
= 0; i
< map
->n
; ++i
) {
4341 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4345 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4346 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4353 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4356 isl_space
*domain_dim
;
4358 map
= isl_map_cow(map
);
4362 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4363 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4364 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4367 for (i
= 0; i
< map
->n
; ++i
) {
4368 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4372 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4373 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4380 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4383 isl_space
*range_dim
;
4385 map
= isl_map_cow(map
);
4389 range_dim
= isl_space_range(isl_map_get_space(map
));
4390 range_dim
= isl_space_from_range(range_dim
);
4391 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4392 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4395 for (i
= 0; i
< map
->n
; ++i
) {
4396 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4400 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4401 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4408 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4409 __isl_take isl_space
*dim
)
4412 struct isl_map
*map
= NULL
;
4414 set
= isl_set_cow(set
);
4417 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4418 map
= (struct isl_map
*)set
;
4419 for (i
= 0; i
< set
->n
; ++i
) {
4420 map
->p
[i
] = isl_basic_map_from_basic_set(
4421 set
->p
[i
], isl_space_copy(dim
));
4425 isl_space_free(map
->dim
);
4429 isl_space_free(dim
);
4434 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4435 __isl_take isl_basic_set
*bset
)
4437 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4440 __isl_give isl_basic_map
*isl_basic_map_from_range(
4441 __isl_take isl_basic_set
*bset
)
4444 space
= isl_basic_set_get_space(bset
);
4445 space
= isl_space_from_range(space
);
4446 bset
= isl_basic_set_reset_space(bset
, space
);
4447 return (isl_basic_map
*)bset
;
4450 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4453 space
= isl_set_get_space(set
);
4454 space
= isl_space_from_range(space
);
4455 set
= isl_set_reset_space(set
, space
);
4456 return (struct isl_map
*)set
;
4459 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4461 return isl_map_reverse(isl_map_from_range(set
));
4464 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4465 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4467 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4470 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4471 __isl_take isl_set
*range
)
4473 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4476 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4479 struct isl_set
*set
= NULL
;
4483 map
= isl_map_cow(map
);
4486 map
->dim
= isl_space_as_set_space(map
->dim
);
4489 set
= (struct isl_set
*)map
;
4490 for (i
= 0; i
< map
->n
; ++i
) {
4491 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4501 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4504 struct isl_map
*map
;
4509 isl_die(dim
->ctx
, isl_error_internal
,
4510 "negative number of basic maps", goto error
);
4511 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4512 sizeof(struct isl_map
) +
4513 (n
- 1) * sizeof(struct isl_basic_map
*));
4517 map
->ctx
= dim
->ctx
;
4518 isl_ctx_ref(map
->ctx
);
4526 isl_space_free(dim
);
4530 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4531 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4534 struct isl_map
*map
;
4537 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4541 map
= isl_map_alloc_space(dims
, n
, flags
);
4545 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4547 struct isl_basic_map
*bmap
;
4548 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4549 bmap
= isl_basic_map_set_to_empty(bmap
);
4553 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4555 struct isl_basic_set
*bset
;
4556 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4557 bset
= isl_basic_set_set_to_empty(bset
);
4561 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4563 struct isl_basic_map
*bmap
;
4566 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4567 bmap
= isl_basic_map_set_to_empty(bmap
);
4571 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4573 struct isl_basic_map
*bmap
;
4576 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4577 bmap
= isl_basic_map_set_to_empty(bmap
);
4581 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4583 struct isl_basic_set
*bset
;
4586 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4587 bset
= isl_basic_set_set_to_empty(bset
);
4591 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4593 struct isl_basic_map
*bmap
;
4594 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4595 bmap
= isl_basic_map_finalize(bmap
);
4599 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4601 struct isl_basic_set
*bset
;
4602 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4603 bset
= isl_basic_set_finalize(bset
);
4607 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4610 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4611 isl_basic_map
*bmap
;
4613 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4614 for (i
= 0; i
< total
; ++i
) {
4615 int k
= isl_basic_map_alloc_inequality(bmap
);
4618 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4619 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4623 isl_basic_map_free(bmap
);
4627 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4629 return isl_basic_map_nat_universe(dim
);
4632 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4634 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4637 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4639 return isl_map_nat_universe(dim
);
4642 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4643 __isl_keep isl_basic_map
*model
)
4647 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4650 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4654 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4657 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4658 __isl_keep isl_set
*model
)
4662 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4665 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4667 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4670 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4674 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4677 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4681 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4684 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4686 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4689 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4693 return isl_set_empty(isl_space_copy(model
->dim
));
4696 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4698 struct isl_map
*map
;
4701 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4702 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4706 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4708 struct isl_set
*set
;
4711 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4712 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4716 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4720 return isl_set_universe(isl_space_copy(model
->dim
));
4723 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4726 struct isl_map
*dup
;
4730 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4731 for (i
= 0; i
< map
->n
; ++i
)
4732 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4736 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4737 __isl_take isl_basic_map
*bmap
)
4741 if (isl_basic_map_plain_is_empty(bmap
)) {
4742 isl_basic_map_free(bmap
);
4745 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4746 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4747 map
->p
[map
->n
] = bmap
;
4749 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4755 isl_basic_map_free(bmap
);
4759 void isl_map_free(struct isl_map
*map
)
4769 isl_ctx_deref(map
->ctx
);
4770 for (i
= 0; i
< map
->n
; ++i
)
4771 isl_basic_map_free(map
->p
[i
]);
4772 isl_space_free(map
->dim
);
4776 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4777 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4781 base
= isl_map_cow(base
);
4785 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4788 for (i
= 0; i
< base
->n
; ++i
) {
4789 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4790 isl_space_copy(base
->dim
), 0, 0, 0);
4800 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4801 unsigned nparam
, unsigned dim
)
4803 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4807 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4808 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4812 bmap
= isl_basic_map_cow(bmap
);
4813 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4814 j
= isl_basic_map_alloc_equality(bmap
);
4817 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4818 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4819 isl_int_set_si(bmap
->eq
[j
][0], value
);
4820 bmap
= isl_basic_map_simplify(bmap
);
4821 return isl_basic_map_finalize(bmap
);
4823 isl_basic_map_free(bmap
);
4827 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4828 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4832 bmap
= isl_basic_map_cow(bmap
);
4833 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4834 j
= isl_basic_map_alloc_equality(bmap
);
4837 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4838 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4839 isl_int_set(bmap
->eq
[j
][0], value
);
4840 bmap
= isl_basic_map_simplify(bmap
);
4841 return isl_basic_map_finalize(bmap
);
4843 isl_basic_map_free(bmap
);
4847 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4848 enum isl_dim_type type
, unsigned pos
, int value
)
4852 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4853 return isl_basic_map_fix_pos_si(bmap
,
4854 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4856 isl_basic_map_free(bmap
);
4860 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4861 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4865 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4866 return isl_basic_map_fix_pos(bmap
,
4867 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4869 isl_basic_map_free(bmap
);
4873 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4874 enum isl_dim_type type
, unsigned pos
, int value
)
4876 return (struct isl_basic_set
*)
4877 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4881 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4882 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4884 return (struct isl_basic_set
*)
4885 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4889 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4890 unsigned input
, int value
)
4892 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4895 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4896 unsigned dim
, int value
)
4898 return (struct isl_basic_set
*)
4899 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4900 isl_dim_set
, dim
, value
);
4903 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4904 enum isl_dim_type type
, unsigned pos
, int value
)
4908 map
= isl_map_cow(map
);
4912 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4913 for (i
= 0; i
< map
->n
; ++i
) {
4914 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4918 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4925 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4926 enum isl_dim_type type
, unsigned pos
, int value
)
4928 return (struct isl_set
*)
4929 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4932 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4933 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4937 map
= isl_map_cow(map
);
4941 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4942 for (i
= 0; i
< map
->n
; ++i
) {
4943 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4947 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4954 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4955 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4957 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4960 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4961 unsigned input
, int value
)
4963 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4966 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4968 return (struct isl_set
*)
4969 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4972 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4973 __isl_take isl_basic_map
*bmap
,
4974 enum isl_dim_type type
, unsigned pos
, int value
)
4980 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4981 pos
+= isl_basic_map_offset(bmap
, type
);
4982 bmap
= isl_basic_map_cow(bmap
);
4983 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4984 j
= isl_basic_map_alloc_inequality(bmap
);
4987 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4988 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4989 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4990 bmap
= isl_basic_map_simplify(bmap
);
4991 return isl_basic_map_finalize(bmap
);
4993 isl_basic_map_free(bmap
);
4997 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4998 unsigned dim
, isl_int value
)
5002 bset
= isl_basic_set_cow(bset
);
5003 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5004 j
= isl_basic_set_alloc_inequality(bset
);
5007 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5008 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5009 isl_int_neg(bset
->ineq
[j
][0], value
);
5010 bset
= isl_basic_set_simplify(bset
);
5011 return isl_basic_set_finalize(bset
);
5013 isl_basic_set_free(bset
);
5017 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5018 enum isl_dim_type type
, unsigned pos
, int value
)
5022 map
= isl_map_cow(map
);
5026 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5027 for (i
= 0; i
< map
->n
; ++i
) {
5028 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
5033 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5040 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5041 enum isl_dim_type type
, unsigned pos
, int value
)
5043 return (struct isl_set
*)
5044 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5047 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5052 set
= isl_set_cow(set
);
5056 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5057 for (i
= 0; i
< set
->n
; ++i
) {
5058 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5068 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5072 map
= isl_map_cow(map
);
5076 map
->dim
= isl_space_reverse(map
->dim
);
5079 for (i
= 0; i
< map
->n
; ++i
) {
5080 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5084 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5091 static struct isl_map
*isl_basic_map_partial_lexopt(
5092 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5093 struct isl_set
**empty
, int max
)
5097 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5098 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5100 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5102 isl_basic_map_free(bmap
);
5103 isl_basic_set_free(dom
);
5109 struct isl_map
*isl_basic_map_partial_lexmax(
5110 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5111 struct isl_set
**empty
)
5113 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5116 struct isl_map
*isl_basic_map_partial_lexmin(
5117 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5118 struct isl_set
**empty
)
5120 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5123 struct isl_set
*isl_basic_set_partial_lexmin(
5124 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5125 struct isl_set
**empty
)
5127 return (struct isl_set
*)
5128 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5132 struct isl_set
*isl_basic_set_partial_lexmax(
5133 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5134 struct isl_set
**empty
)
5136 return (struct isl_set
*)
5137 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5141 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5142 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5143 __isl_give isl_set
**empty
)
5145 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5148 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5149 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5150 __isl_give isl_set
**empty
)
5152 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5155 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5156 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5157 __isl_give isl_set
**empty
)
5159 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5162 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5163 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5164 __isl_give isl_set
**empty
)
5166 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5169 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5170 __isl_take isl_basic_map
*bmap
, int max
)
5172 isl_basic_set
*dom
= NULL
;
5173 isl_space
*dom_space
;
5177 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5178 dom
= isl_basic_set_universe(dom_space
);
5179 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5181 isl_basic_map_free(bmap
);
5185 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5186 __isl_take isl_basic_map
*bmap
)
5188 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5191 /* Given a basic map "bmap", compute the lexicographically minimal
5192 * (or maximal) image element for each domain element in dom.
5193 * Set *empty to those elements in dom that do not have an image element.
5195 * We first make sure the basic sets in dom are disjoint and then
5196 * simply collect the results over each of the basic sets separately.
5197 * We could probably improve the efficiency a bit by moving the union
5198 * domain down into the parametric integer programming.
5200 static __isl_give isl_map
*basic_map_partial_lexopt(
5201 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5202 __isl_give isl_set
**empty
, int max
)
5205 struct isl_map
*res
;
5207 dom
= isl_set_make_disjoint(dom
);
5211 if (isl_set_plain_is_empty(dom
)) {
5212 res
= isl_map_empty_like_basic_map(bmap
);
5213 *empty
= isl_set_empty_like(dom
);
5215 isl_basic_map_free(bmap
);
5219 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5220 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5222 for (i
= 1; i
< dom
->n
; ++i
) {
5223 struct isl_map
*res_i
;
5224 struct isl_set
*empty_i
;
5226 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5227 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5229 res
= isl_map_union_disjoint(res
, res_i
);
5230 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5234 isl_basic_map_free(bmap
);
5239 isl_basic_map_free(bmap
);
5243 /* Given a map "map", compute the lexicographically minimal
5244 * (or maximal) image element for each domain element in dom.
5245 * Set *empty to those elements in dom that do not have an image element.
5247 * We first compute the lexicographically minimal or maximal element
5248 * in the first basic map. This results in a partial solution "res"
5249 * and a subset "todo" of dom that still need to be handled.
5250 * We then consider each of the remaining maps in "map" and successively
5251 * improve both "res" and "todo".
5253 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5254 * Assume we are computing the lexicographical maximum.
5255 * We first compute the lexicographically maximal element in basic map i.
5256 * This results in a partial solution res_i and a subset todo_i.
5257 * Then we combine these results with those obtain for the first k basic maps
5258 * to obtain a result that is valid for the first k+1 basic maps.
5259 * In particular, the set where there is no solution is the set where
5260 * there is no solution for the first k basic maps and also no solution
5261 * for the ith basic map, i.e.,
5263 * todo^i = todo^k * todo_i
5265 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5266 * solutions, arbitrarily breaking ties in favor of res^k.
5267 * That is, when res^k(a) >= res_i(a), we pick res^k and
5268 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5269 * the lexicographic order.)
5270 * In practice, we compute
5272 * res^k * (res_i . "<=")
5276 * res_i * (res^k . "<")
5278 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5279 * where only one of res^k and res_i provides a solution and we simply pick
5286 * Note that we only compute these intersections when dom(res^k) intersects
5287 * dom(res_i). Otherwise, the only effect of these intersections is to
5288 * potentially break up res^k and res_i into smaller pieces.
5289 * We want to avoid such splintering as much as possible.
5290 * In fact, an earlier implementation of this function would look for
5291 * better results in the domain of res^k and for extra results in todo^k,
5292 * but this would always result in a splintering according to todo^k,
5293 * even when the domain of basic map i is disjoint from the domains of
5294 * the previous basic maps.
5296 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5297 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5298 __isl_give isl_set
**empty
, int max
)
5301 struct isl_map
*res
;
5302 struct isl_set
*todo
;
5307 if (isl_map_plain_is_empty(map
)) {
5315 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5316 isl_set_copy(dom
), &todo
, max
);
5318 for (i
= 1; i
< map
->n
; ++i
) {
5322 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5324 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5325 isl_set_copy(dom
), &todo_i
, max
);
5328 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5329 le
= isl_map_lex_le(dim
);
5331 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5332 le
= isl_map_lex_ge(dim
);
5334 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5335 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5336 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5337 le
= isl_map_intersect(le
, isl_map_copy(res
));
5339 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5340 res
= isl_map_intersect_domain(res
,
5341 isl_set_copy(todo_i
));
5342 res_i
= isl_map_intersect_domain(res_i
,
5343 isl_set_copy(todo
));
5346 res
= isl_map_union_disjoint(res
, res_i
);
5347 res
= isl_map_union_disjoint(res
, lt
);
5348 res
= isl_map_union_disjoint(res
, le
);
5350 todo
= isl_set_intersect(todo
, todo_i
);
5370 /* Given a map "map", compute the lexicographically minimal
5371 * (or maximal) image element for each domain element in dom.
5372 * Set *empty to those elements in dom that do not have an image element.
5374 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5376 static __isl_give isl_map
*isl_map_partial_lexopt(
5377 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5378 __isl_give isl_set
**empty
, int max
)
5382 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5383 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5384 if (!isl_space_has_named_params(map
->dim
) ||
5385 !isl_space_has_named_params(dom
->dim
))
5386 isl_die(map
->ctx
, isl_error_invalid
,
5387 "unaligned unnamed parameters", goto error
);
5388 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5389 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5390 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5399 __isl_give isl_map
*isl_map_partial_lexmax(
5400 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5401 __isl_give isl_set
**empty
)
5403 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5406 __isl_give isl_map
*isl_map_partial_lexmin(
5407 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5408 __isl_give isl_set
**empty
)
5410 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5413 __isl_give isl_set
*isl_set_partial_lexmin(
5414 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5415 __isl_give isl_set
**empty
)
5417 return (struct isl_set
*)
5418 isl_map_partial_lexmin((struct isl_map
*)set
,
5422 __isl_give isl_set
*isl_set_partial_lexmax(
5423 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5424 __isl_give isl_set
**empty
)
5426 return (struct isl_set
*)
5427 isl_map_partial_lexmax((struct isl_map
*)set
,
5431 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5433 struct isl_basic_set
*dom
= NULL
;
5438 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5439 dom
= isl_basic_set_universe(dom_dim
);
5440 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5442 isl_basic_map_free(bmap
);
5446 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5448 return isl_basic_map_lexopt(bmap
, 0);
5451 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5453 return isl_basic_map_lexopt(bmap
, 1);
5456 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5458 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5461 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5463 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5466 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5468 struct isl_set
*dom
= NULL
;
5473 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5474 dom
= isl_set_universe(dom_dim
);
5475 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5481 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5483 return isl_map_lexopt(map
, 0);
5486 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5488 return isl_map_lexopt(map
, 1);
5491 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5493 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5496 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5498 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5501 /* Extract the first and only affine expression from list
5502 * and then add it to *pwaff with the given dom.
5503 * This domain is known to be disjoint from other domains
5504 * because of the way isl_basic_map_foreach_lexmax works.
5506 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5507 __isl_take isl_aff_list
*list
, void *user
)
5509 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5511 isl_pw_aff
**pwaff
= user
;
5512 isl_pw_aff
*pwaff_i
;
5514 if (isl_aff_list_n_aff(list
) != 1)
5515 isl_die(ctx
, isl_error_internal
,
5516 "expecting single element list", goto error
);
5518 aff
= isl_aff_list_get_aff(list
, 0);
5519 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5521 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5523 isl_aff_list_free(list
);
5527 isl_basic_set_free(dom
);
5528 isl_aff_list_free(list
);
5532 /* Given a basic map with one output dimension, compute the minimum or
5533 * maximum of that dimension as an isl_pw_aff.
5535 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5536 * call update_dim_opt on each leaf of the result.
5538 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5541 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5545 dim
= isl_space_from_domain(isl_space_domain(dim
));
5546 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5547 pwaff
= isl_pw_aff_empty(dim
);
5549 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5551 return isl_pw_aff_free(pwaff
);
5556 /* Compute the minimum or maximum of the given output dimension
5557 * as a function of the parameters and the input dimensions,
5558 * but independently of the other output dimensions.
5560 * We first project out the other output dimension and then compute
5561 * the "lexicographic" maximum in each basic map, combining the results
5562 * using isl_pw_aff_union_max.
5564 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5571 n_out
= isl_map_dim(map
, isl_dim_out
);
5572 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5573 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5578 isl_space
*dim
= isl_map_get_space(map
);
5579 dim
= isl_space_domain(isl_space_from_range(dim
));
5581 return isl_pw_aff_empty(dim
);
5584 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5585 for (i
= 1; i
< map
->n
; ++i
) {
5586 isl_pw_aff
*pwaff_i
;
5588 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5589 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5597 /* Compute the maximum of the given output dimension as a function of the
5598 * parameters and input dimensions, but independently of
5599 * the other output dimensions.
5601 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5603 return map_dim_opt(map
, pos
, 1);
5606 /* Compute the minimum or maximum of the given set dimension
5607 * as a function of the parameters,
5608 * but independently of the other set dimensions.
5610 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5613 return map_dim_opt(set
, pos
, max
);
5616 /* Compute the maximum of the given set dimension as a function of the
5617 * parameters, but independently of the other set dimensions.
5619 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5621 return set_dim_opt(set
, pos
, 1);
5624 /* Compute the minimum of the given set dimension as a function of the
5625 * parameters, but independently of the other set dimensions.
5627 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5629 return set_dim_opt(set
, pos
, 0);
5632 /* Apply a preimage specified by "mat" on the parameters of "bset".
5633 * bset is assumed to have only parameters and divs.
5635 static struct isl_basic_set
*basic_set_parameter_preimage(
5636 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5643 bset
->dim
= isl_space_cow(bset
->dim
);
5647 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5649 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5651 bset
->dim
->nparam
= 0;
5652 bset
->dim
->n_out
= nparam
;
5653 bset
= isl_basic_set_preimage(bset
, mat
);
5655 bset
->dim
->nparam
= bset
->dim
->n_out
;
5656 bset
->dim
->n_out
= 0;
5661 isl_basic_set_free(bset
);
5665 /* Apply a preimage specified by "mat" on the parameters of "set".
5666 * set is assumed to have only parameters and divs.
5668 static struct isl_set
*set_parameter_preimage(
5669 struct isl_set
*set
, struct isl_mat
*mat
)
5671 isl_space
*dim
= NULL
;
5677 dim
= isl_space_copy(set
->dim
);
5678 dim
= isl_space_cow(dim
);
5682 nparam
= isl_set_dim(set
, isl_dim_param
);
5684 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5687 dim
->n_out
= nparam
;
5688 isl_set_reset_space(set
, dim
);
5689 set
= isl_set_preimage(set
, mat
);
5692 dim
= isl_space_copy(set
->dim
);
5693 dim
= isl_space_cow(dim
);
5696 dim
->nparam
= dim
->n_out
;
5698 isl_set_reset_space(set
, dim
);
5701 isl_space_free(dim
);
5708 /* Intersect the basic set "bset" with the affine space specified by the
5709 * equalities in "eq".
5711 static struct isl_basic_set
*basic_set_append_equalities(
5712 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5720 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5725 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5726 for (i
= 0; i
< eq
->n_row
; ++i
) {
5727 k
= isl_basic_set_alloc_equality(bset
);
5730 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5731 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5735 bset
= isl_basic_set_gauss(bset
, NULL
);
5736 bset
= isl_basic_set_finalize(bset
);
5741 isl_basic_set_free(bset
);
5745 /* Intersect the set "set" with the affine space specified by the
5746 * equalities in "eq".
5748 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5756 for (i
= 0; i
< set
->n
; ++i
) {
5757 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5770 /* Project the given basic set onto its parameter domain, possibly introducing
5771 * new, explicit, existential variables in the constraints.
5772 * The input has parameters and (possibly implicit) existential variables.
5773 * The output has the same parameters, but only
5774 * explicit existentially quantified variables.
5776 * The actual projection is performed by pip, but pip doesn't seem
5777 * to like equalities very much, so we first remove the equalities
5778 * among the parameters by performing a variable compression on
5779 * the parameters. Afterward, an inverse transformation is performed
5780 * and the equalities among the parameters are inserted back in.
5782 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5786 struct isl_mat
*T
, *T2
;
5787 struct isl_set
*set
;
5788 unsigned nparam
, n_div
;
5790 bset
= isl_basic_set_cow(bset
);
5794 if (bset
->n_eq
== 0)
5795 return isl_basic_set_lexmin(bset
);
5797 isl_basic_set_gauss(bset
, NULL
);
5799 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5800 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5802 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5803 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5806 if (i
== bset
->n_eq
)
5807 return isl_basic_set_lexmin(bset
);
5809 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5811 eq
= isl_mat_cow(eq
);
5812 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5813 if (T
&& T
->n_col
== 0) {
5817 bset
= isl_basic_set_set_to_empty(bset
);
5818 return isl_set_from_basic_set(bset
);
5820 bset
= basic_set_parameter_preimage(bset
, T
);
5822 set
= isl_basic_set_lexmin(bset
);
5823 set
= set_parameter_preimage(set
, T2
);
5824 set
= set_append_equalities(set
, eq
);
5828 /* Compute an explicit representation for all the existentially
5829 * quantified variables.
5830 * The input and output dimensions are first turned into parameters.
5831 * compute_divs then returns a map with the same parameters and
5832 * no input or output dimensions and the dimension specification
5833 * is reset to that of the input.
5835 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5837 struct isl_basic_set
*bset
;
5838 struct isl_set
*set
;
5839 struct isl_map
*map
;
5840 isl_space
*dim
, *orig_dim
= NULL
;
5845 bmap
= isl_basic_map_cow(bmap
);
5849 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5850 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5851 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5852 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5856 orig_dim
= bmap
->dim
;
5858 bset
= (struct isl_basic_set
*)bmap
;
5860 set
= parameter_compute_divs(bset
);
5861 map
= (struct isl_map
*)set
;
5862 map
= isl_map_reset_space(map
, orig_dim
);
5866 isl_basic_map_free(bmap
);
5870 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5878 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5879 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5880 if (isl_int_is_zero(bmap
->div
[i
][0]))
5882 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5888 static int map_divs_known(__isl_keep isl_map
*map
)
5895 for (i
= 0; i
< map
->n
; ++i
) {
5896 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5904 /* If bmap contains any unknown divs, then compute explicit
5905 * expressions for them. However, this computation may be
5906 * quite expensive, so first try to remove divs that aren't
5909 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5912 struct isl_map
*map
;
5914 known
= isl_basic_map_divs_known(bmap
);
5918 return isl_map_from_basic_map(bmap
);
5920 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5922 known
= isl_basic_map_divs_known(bmap
);
5926 return isl_map_from_basic_map(bmap
);
5928 map
= compute_divs(bmap
);
5931 isl_basic_map_free(bmap
);
5935 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5939 struct isl_map
*res
;
5946 known
= map_divs_known(map
);
5954 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5955 for (i
= 1 ; i
< map
->n
; ++i
) {
5957 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5958 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5959 res
= isl_map_union_disjoint(res
, r2
);
5961 res
= isl_map_union(res
, r2
);
5968 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5970 return (struct isl_set
*)
5971 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5974 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5976 return (struct isl_set
*)
5977 isl_map_compute_divs((struct isl_map
*)set
);
5980 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5983 struct isl_set
*set
;
5988 map
= isl_map_cow(map
);
5992 set
= (struct isl_set
*)map
;
5993 set
->dim
= isl_space_domain(set
->dim
);
5996 for (i
= 0; i
< map
->n
; ++i
) {
5997 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6001 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6002 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6009 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6010 __isl_take isl_map
*map2
)
6014 struct isl_map
*map
= NULL
;
6028 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6030 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6031 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6032 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6034 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6035 map1
->n
+ map2
->n
, flags
);
6038 for (i
= 0; i
< map1
->n
; ++i
) {
6039 map
= isl_map_add_basic_map(map
,
6040 isl_basic_map_copy(map1
->p
[i
]));
6044 for (i
= 0; i
< map2
->n
; ++i
) {
6045 map
= isl_map_add_basic_map(map
,
6046 isl_basic_map_copy(map2
->p
[i
]));
6060 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6061 __isl_take isl_map
*map2
)
6063 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6066 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6068 map1
= isl_map_union_disjoint(map1
, map2
);
6072 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6076 struct isl_set
*isl_set_union_disjoint(
6077 struct isl_set
*set1
, struct isl_set
*set2
)
6079 return (struct isl_set
*)
6080 isl_map_union_disjoint(
6081 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6084 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6086 return (struct isl_set
*)
6087 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6090 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6091 __isl_take isl_set
*set
)
6094 struct isl_map
*result
;
6100 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6101 isl_die(set
->ctx
, isl_error_invalid
,
6102 "parameters don't match", goto error
);
6104 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6105 !isl_map_compatible_range(map
, set
))
6106 isl_die(set
->ctx
, isl_error_invalid
,
6107 "incompatible spaces", goto error
);
6109 if (isl_set_plain_is_universe(set
)) {
6114 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6115 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6116 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6118 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6119 map
->n
* set
->n
, flags
);
6122 for (i
= 0; i
< map
->n
; ++i
)
6123 for (j
= 0; j
< set
->n
; ++j
) {
6124 result
= isl_map_add_basic_map(result
,
6125 isl_basic_map_intersect_range(
6126 isl_basic_map_copy(map
->p
[i
]),
6127 isl_basic_set_copy(set
->p
[j
])));
6140 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6141 __isl_take isl_set
*set
)
6143 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6146 struct isl_map
*isl_map_intersect_domain(
6147 struct isl_map
*map
, struct isl_set
*set
)
6149 return isl_map_reverse(
6150 isl_map_intersect_range(isl_map_reverse(map
), set
));
6153 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6154 __isl_take isl_map
*map2
)
6158 map1
= isl_map_reverse(map1
);
6159 map1
= isl_map_apply_range(map1
, map2
);
6160 return isl_map_reverse(map1
);
6167 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6168 __isl_take isl_map
*map2
)
6170 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6173 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6174 __isl_take isl_map
*map2
)
6176 isl_space
*dim_result
;
6177 struct isl_map
*result
;
6183 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6184 isl_space_copy(map2
->dim
));
6186 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6189 for (i
= 0; i
< map1
->n
; ++i
)
6190 for (j
= 0; j
< map2
->n
; ++j
) {
6191 result
= isl_map_add_basic_map(result
,
6192 isl_basic_map_apply_range(
6193 isl_basic_map_copy(map1
->p
[i
]),
6194 isl_basic_map_copy(map2
->p
[j
])));
6200 if (result
&& result
->n
<= 1)
6201 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6209 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6210 __isl_take isl_map
*map2
)
6212 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6216 * returns range - domain
6218 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6220 isl_space
*dims
, *target_dim
;
6221 struct isl_basic_set
*bset
;
6228 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6229 bmap
->dim
, isl_dim_out
),
6231 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6232 dim
= isl_basic_map_n_in(bmap
);
6233 nparam
= isl_basic_map_n_param(bmap
);
6234 bset
= isl_basic_set_from_basic_map(bmap
);
6235 bset
= isl_basic_set_cow(bset
);
6236 dims
= isl_basic_set_get_space(bset
);
6237 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6238 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6239 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6240 for (i
= 0; i
< dim
; ++i
) {
6241 int j
= isl_basic_map_alloc_equality(
6242 (struct isl_basic_map
*)bset
);
6245 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6246 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6247 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6248 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6250 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6251 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6254 isl_basic_map_free(bmap
);
6259 * returns range - domain
6261 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6265 struct isl_set
*result
;
6270 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6271 map
->dim
, isl_dim_out
),
6273 dim
= isl_map_get_space(map
);
6274 dim
= isl_space_domain(dim
);
6275 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6278 for (i
= 0; i
< map
->n
; ++i
)
6279 result
= isl_set_add_basic_set(result
,
6280 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6289 * returns [domain -> range] -> range - domain
6291 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6292 __isl_take isl_basic_map
*bmap
)
6296 isl_basic_map
*domain
;
6300 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6301 isl_die(bmap
->ctx
, isl_error_invalid
,
6302 "domain and range don't match", goto error
);
6304 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6305 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6307 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6308 domain
= isl_basic_map_universe(dim
);
6310 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6311 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6312 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6314 total
= isl_basic_map_total_dim(bmap
);
6316 for (i
= 0; i
< n
; ++i
) {
6317 k
= isl_basic_map_alloc_equality(bmap
);
6320 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6321 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6322 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6323 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6326 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6327 return isl_basic_map_finalize(bmap
);
6329 isl_basic_map_free(bmap
);
6334 * returns [domain -> range] -> range - domain
6336 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6339 isl_space
*domain_dim
;
6344 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6345 isl_die(map
->ctx
, isl_error_invalid
,
6346 "domain and range don't match", goto error
);
6348 map
= isl_map_cow(map
);
6352 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6353 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6354 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6357 for (i
= 0; i
< map
->n
; ++i
) {
6358 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6362 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6369 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6371 struct isl_basic_map
*bmap
;
6379 nparam
= dims
->nparam
;
6381 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6385 for (i
= 0; i
< dim
; ++i
) {
6386 int j
= isl_basic_map_alloc_equality(bmap
);
6389 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6390 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6391 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6393 return isl_basic_map_finalize(bmap
);
6395 isl_basic_map_free(bmap
);
6399 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6403 if (dim
->n_in
!= dim
->n_out
)
6404 isl_die(dim
->ctx
, isl_error_invalid
,
6405 "number of input and output dimensions needs to be "
6406 "the same", goto error
);
6407 return basic_map_identity(dim
);
6409 isl_space_free(dim
);
6413 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6415 if (!model
|| !model
->dim
)
6417 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6420 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6422 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6425 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6427 if (!model
|| !model
->dim
)
6429 return isl_map_identity(isl_space_copy(model
->dim
));
6432 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6434 if (!model
|| !model
->dim
)
6436 return isl_map_identity(isl_space_copy(model
->dim
));
6439 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6441 isl_space
*dim
= isl_set_get_space(set
);
6443 id
= isl_map_identity(isl_space_map_from_set(dim
));
6444 return isl_map_intersect_range(id
, set
);
6447 /* Construct a basic set with all set dimensions having only non-negative
6450 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6451 __isl_take isl_space
*space
)
6456 struct isl_basic_set
*bset
;
6460 nparam
= space
->nparam
;
6462 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6465 for (i
= 0; i
< dim
; ++i
) {
6466 int k
= isl_basic_set_alloc_inequality(bset
);
6469 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6470 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6474 isl_basic_set_free(bset
);
6478 /* Construct the half-space x_pos >= 0.
6480 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6484 isl_basic_set
*nonneg
;
6486 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6487 k
= isl_basic_set_alloc_inequality(nonneg
);
6490 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6491 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6493 return isl_basic_set_finalize(nonneg
);
6495 isl_basic_set_free(nonneg
);
6499 /* Construct the half-space x_pos <= -1.
6501 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6506 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6507 k
= isl_basic_set_alloc_inequality(neg
);
6510 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6511 isl_int_set_si(neg
->ineq
[k
][0], -1);
6512 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6514 return isl_basic_set_finalize(neg
);
6516 isl_basic_set_free(neg
);
6520 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6521 enum isl_dim_type type
, unsigned first
, unsigned n
)
6524 isl_basic_set
*nonneg
;
6532 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6534 for (i
= 0; i
< n
; ++i
) {
6535 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6536 pos(set
->dim
, type
) + first
+ i
);
6537 neg
= neg_halfspace(isl_set_get_space(set
),
6538 pos(set
->dim
, type
) + first
+ i
);
6540 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6549 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6550 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6557 if (isl_set_plain_is_empty(set
)) {
6562 return fn(set
, signs
, user
);
6565 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6567 half
= isl_set_intersect(half
, isl_set_copy(set
));
6568 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6572 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6574 half
= isl_set_intersect(half
, set
);
6575 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6581 /* Call "fn" on the intersections of "set" with each of the orthants
6582 * (except for obviously empty intersections). The orthant is identified
6583 * by the signs array, with each entry having value 1 or -1 according
6584 * to the sign of the corresponding variable.
6586 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6587 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6597 if (isl_set_plain_is_empty(set
))
6600 nparam
= isl_set_dim(set
, isl_dim_param
);
6601 nvar
= isl_set_dim(set
, isl_dim_set
);
6603 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6605 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6613 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6615 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6618 int isl_basic_map_is_subset(
6619 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6622 struct isl_map
*map1
;
6623 struct isl_map
*map2
;
6625 if (!bmap1
|| !bmap2
)
6628 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6629 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6631 is_subset
= isl_map_is_subset(map1
, map2
);
6639 int isl_basic_map_is_equal(
6640 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6644 if (!bmap1
|| !bmap2
)
6646 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6649 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6653 int isl_basic_set_is_equal(
6654 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6656 return isl_basic_map_is_equal(
6657 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6660 int isl_map_is_empty(struct isl_map
*map
)
6667 for (i
= 0; i
< map
->n
; ++i
) {
6668 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6677 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6679 return map
? map
->n
== 0 : -1;
6682 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6684 return isl_map_plain_is_empty(map
);
6687 int isl_set_plain_is_empty(struct isl_set
*set
)
6689 return set
? set
->n
== 0 : -1;
6692 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6694 return isl_set_plain_is_empty(set
);
6697 int isl_set_is_empty(struct isl_set
*set
)
6699 return isl_map_is_empty((struct isl_map
*)set
);
6702 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6707 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6710 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6715 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6718 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6724 is_subset
= isl_map_is_subset(map1
, map2
);
6727 is_subset
= isl_map_is_subset(map2
, map1
);
6731 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6733 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6736 int isl_basic_map_is_strict_subset(
6737 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6741 if (!bmap1
|| !bmap2
)
6743 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6746 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6747 if (is_subset
== -1)
6752 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6758 is_subset
= isl_map_is_subset(map1
, map2
);
6761 is_subset
= isl_map_is_subset(map2
, map1
);
6762 if (is_subset
== -1)
6767 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6769 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6772 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6776 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6779 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6783 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6786 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6793 for (i
= 0; i
< map
->n
; ++i
) {
6794 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6802 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6804 return isl_map_plain_is_universe((isl_map
*) set
);
6807 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6809 return isl_set_plain_is_universe(set
);
6812 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6814 struct isl_basic_set
*bset
= NULL
;
6815 struct isl_vec
*sample
= NULL
;
6822 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6825 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6826 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6827 copy
= isl_basic_map_remove_redundancies(copy
);
6828 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6829 isl_basic_map_free(copy
);
6833 total
= 1 + isl_basic_map_total_dim(bmap
);
6834 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6835 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6841 isl_vec_free(bmap
->sample
);
6842 bmap
->sample
= NULL
;
6843 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6846 sample
= isl_basic_set_sample_vec(bset
);
6849 empty
= sample
->size
== 0;
6850 isl_vec_free(bmap
->sample
);
6851 bmap
->sample
= sample
;
6853 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6858 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6862 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6865 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6867 return isl_basic_map_plain_is_empty(bmap
);
6870 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6874 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6877 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6879 return isl_basic_set_plain_is_empty(bset
);
6882 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6884 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6887 struct isl_map
*isl_basic_map_union(
6888 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6890 struct isl_map
*map
;
6891 if (!bmap1
|| !bmap2
)
6894 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6896 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6899 map
= isl_map_add_basic_map(map
, bmap1
);
6900 map
= isl_map_add_basic_map(map
, bmap2
);
6903 isl_basic_map_free(bmap1
);
6904 isl_basic_map_free(bmap2
);
6908 struct isl_set
*isl_basic_set_union(
6909 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6911 return (struct isl_set
*)isl_basic_map_union(
6912 (struct isl_basic_map
*)bset1
,
6913 (struct isl_basic_map
*)bset2
);
6916 /* Order divs such that any div only depends on previous divs */
6917 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6925 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6927 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6929 if (isl_int_is_zero(bmap
->div
[i
][0]))
6931 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6935 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6941 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6943 return (struct isl_basic_set
*)
6944 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6947 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6954 for (i
= 0; i
< map
->n
; ++i
) {
6955 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6966 /* Apply the expansion computed by isl_merge_divs.
6967 * The expansion itself is given by "exp" while the resulting
6968 * list of divs is given by "div".
6970 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6971 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6976 bset
= isl_basic_set_cow(bset
);
6980 if (div
->n_row
< bset
->n_div
)
6981 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6982 "not an expansion", goto error
);
6984 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
6985 div
->n_row
- bset
->n_div
, 0,
6986 2 * (div
->n_row
- bset
->n_div
));
6988 n_div
= bset
->n_div
;
6989 for (i
= n_div
; i
< div
->n_row
; ++i
)
6990 if (isl_basic_set_alloc_div(bset
) < 0)
6994 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6995 if (j
>= 0 && exp
[j
] == i
) {
6997 isl_basic_map_swap_div(bset
, i
, j
);
7000 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7001 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7009 isl_basic_set_free(bset
);
7014 /* Look for a div in dst that corresponds to the div "div" in src.
7015 * The divs before "div" in src and dst are assumed to be the same.
7017 * Returns -1 if no corresponding div was found and the position
7018 * of the corresponding div in dst otherwise.
7020 static int find_div(struct isl_basic_map
*dst
,
7021 struct isl_basic_map
*src
, unsigned div
)
7025 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7027 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7028 for (i
= div
; i
< dst
->n_div
; ++i
)
7029 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7030 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7031 dst
->n_div
- div
) == -1)
7036 struct isl_basic_map
*isl_basic_map_align_divs(
7037 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7040 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7045 if (src
->n_div
== 0)
7048 for (i
= 0; i
< src
->n_div
; ++i
)
7049 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7051 src
= isl_basic_map_order_divs(src
);
7052 dst
= isl_basic_map_cow(dst
);
7053 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7054 src
->n_div
, 0, 2 * src
->n_div
);
7057 for (i
= 0; i
< src
->n_div
; ++i
) {
7058 int j
= find_div(dst
, src
, i
);
7060 j
= isl_basic_map_alloc_div(dst
);
7063 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7064 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7065 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7069 isl_basic_map_swap_div(dst
, i
, j
);
7073 isl_basic_map_free(dst
);
7077 struct isl_basic_set
*isl_basic_set_align_divs(
7078 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7080 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7081 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7084 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7092 map
= isl_map_compute_divs(map
);
7093 map
= isl_map_cow(map
);
7097 for (i
= 1; i
< map
->n
; ++i
)
7098 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7099 for (i
= 1; i
< map
->n
; ++i
)
7100 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7102 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7106 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7108 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7111 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7112 __isl_take isl_map
*map
)
7116 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7117 map
= isl_map_intersect_domain(map
, set
);
7118 set
= isl_map_range(map
);
7126 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7127 __isl_take isl_map
*map
)
7129 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7132 /* There is no need to cow as removing empty parts doesn't change
7133 * the meaning of the set.
7135 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7142 for (i
= map
->n
-1; i
>= 0; --i
) {
7143 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7145 isl_basic_map_free(map
->p
[i
]);
7146 if (i
!= map
->n
-1) {
7147 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7148 map
->p
[i
] = map
->p
[map
->n
-1];
7156 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7158 return (struct isl_set
*)
7159 isl_map_remove_empty_parts((struct isl_map
*)set
);
7162 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7164 struct isl_basic_map
*bmap
;
7165 if (!map
|| map
->n
== 0)
7167 bmap
= map
->p
[map
->n
-1];
7168 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7169 return isl_basic_map_copy(bmap
);
7172 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7174 return (struct isl_basic_set
*)
7175 isl_map_copy_basic_map((struct isl_map
*)set
);
7178 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7179 __isl_keep isl_basic_map
*bmap
)
7185 for (i
= map
->n
-1; i
>= 0; --i
) {
7186 if (map
->p
[i
] != bmap
)
7188 map
= isl_map_cow(map
);
7191 isl_basic_map_free(map
->p
[i
]);
7192 if (i
!= map
->n
-1) {
7193 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7194 map
->p
[i
] = map
->p
[map
->n
-1];
7205 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7206 struct isl_basic_set
*bset
)
7208 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7209 (struct isl_basic_map
*)bset
);
7212 /* Given two basic sets bset1 and bset2, compute the maximal difference
7213 * between the values of dimension pos in bset1 and those in bset2
7214 * for any common value of the parameters and dimensions preceding pos.
7216 static enum isl_lp_result
basic_set_maximal_difference_at(
7217 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7218 int pos
, isl_int
*opt
)
7221 struct isl_basic_map
*bmap1
= NULL
;
7222 struct isl_basic_map
*bmap2
= NULL
;
7223 struct isl_ctx
*ctx
;
7224 struct isl_vec
*obj
;
7227 unsigned dim1
, dim2
;
7228 enum isl_lp_result res
;
7230 if (!bset1
|| !bset2
)
7231 return isl_lp_error
;
7233 nparam
= isl_basic_set_n_param(bset1
);
7234 dim1
= isl_basic_set_n_dim(bset1
);
7235 dim2
= isl_basic_set_n_dim(bset2
);
7236 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7237 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7238 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7239 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7240 if (!bmap1
|| !bmap2
)
7242 bmap1
= isl_basic_map_cow(bmap1
);
7243 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7244 pos
, (dim1
- pos
) + (dim2
- pos
),
7245 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7246 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7249 total
= isl_basic_map_total_dim(bmap1
);
7251 obj
= isl_vec_alloc(ctx
, 1 + total
);
7252 isl_seq_clr(obj
->block
.data
, 1 + total
);
7253 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7254 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7257 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7259 isl_basic_map_free(bmap1
);
7263 isl_basic_map_free(bmap1
);
7264 isl_basic_map_free(bmap2
);
7265 return isl_lp_error
;
7268 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7269 * for any common value of the parameters and dimensions preceding pos
7270 * in both basic sets, the values of dimension pos in bset1 are
7271 * smaller or larger than those in bset2.
7274 * 1 if bset1 follows bset2
7275 * -1 if bset1 precedes bset2
7276 * 0 if bset1 and bset2 are incomparable
7277 * -2 if some error occurred.
7279 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7280 struct isl_basic_set
*bset2
, int pos
)
7283 enum isl_lp_result res
;
7288 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7290 if (res
== isl_lp_empty
)
7292 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7293 res
== isl_lp_unbounded
)
7295 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7304 /* Given two basic sets bset1 and bset2, check whether
7305 * for any common value of the parameters and dimensions preceding pos
7306 * there is a value of dimension pos in bset1 that is larger
7307 * than a value of the same dimension in bset2.
7310 * 1 if there exists such a pair
7311 * 0 if there is no such pair, but there is a pair of equal values
7313 * -2 if some error occurred.
7315 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7316 __isl_keep isl_basic_set
*bset2
, int pos
)
7319 enum isl_lp_result res
;
7324 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7326 if (res
== isl_lp_empty
)
7328 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7329 res
== isl_lp_unbounded
)
7331 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7333 else if (res
== isl_lp_ok
)
7342 /* Given two sets set1 and set2, check whether
7343 * for any common value of the parameters and dimensions preceding pos
7344 * there is a value of dimension pos in set1 that is larger
7345 * than a value of the same dimension in set2.
7348 * 1 if there exists such a pair
7349 * 0 if there is no such pair, but there is a pair of equal values
7351 * -2 if some error occurred.
7353 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7354 __isl_keep isl_set
*set2
, int pos
)
7362 for (i
= 0; i
< set1
->n
; ++i
)
7363 for (j
= 0; j
< set2
->n
; ++j
) {
7365 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7366 if (f
== 1 || f
== -2)
7375 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7376 unsigned pos
, isl_int
*val
)
7384 total
= isl_basic_map_total_dim(bmap
);
7385 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7386 for (; d
+1 > pos
; --d
)
7387 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7391 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7393 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7395 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7398 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7404 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7405 unsigned pos
, isl_int
*val
)
7417 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7420 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7421 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7422 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7423 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7427 isl_int_set(*val
, v
);
7433 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7434 unsigned pos
, isl_int
*val
)
7436 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7440 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7443 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7446 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7447 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7449 if (pos
>= isl_basic_map_dim(bmap
, type
))
7451 return isl_basic_map_plain_has_fixed_var(bmap
,
7452 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7455 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7456 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7458 if (pos
>= isl_map_dim(map
, type
))
7460 return isl_map_plain_has_fixed_var(map
,
7461 map_offset(map
, type
) - 1 + pos
, val
);
7464 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7465 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7467 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7470 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7471 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7473 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7476 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7477 * then return this fixed value in *val.
7479 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7480 unsigned dim
, isl_int
*val
)
7482 return isl_basic_set_plain_has_fixed_var(bset
,
7483 isl_basic_set_n_param(bset
) + dim
, val
);
7486 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7487 * then return this fixed value in *val.
7489 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7490 unsigned dim
, isl_int
*val
)
7492 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7495 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7496 unsigned dim
, isl_int
*val
)
7498 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7501 /* Check if input variable in has fixed value and if so and if val is not NULL,
7502 * then return this fixed value in *val.
7504 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7505 unsigned in
, isl_int
*val
)
7507 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7510 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7511 * and if val is not NULL, then return this lower bound in *val.
7513 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7514 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7516 int i
, i_eq
= -1, i_ineq
= -1;
7523 total
= isl_basic_set_total_dim(bset
);
7524 nparam
= isl_basic_set_n_param(bset
);
7525 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7526 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7532 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7533 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7535 if (i_eq
!= -1 || i_ineq
!= -1)
7539 if (i_eq
== -1 && i_ineq
== -1)
7541 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7542 /* The coefficient should always be one due to normalization. */
7543 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7545 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7547 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7548 total
- nparam
- dim
- 1) != -1)
7551 isl_int_neg(*val
, c
[0]);
7555 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7556 unsigned dim
, isl_int
*val
)
7568 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7572 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7574 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7575 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7577 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7581 isl_int_set(*val
, v
);
7592 /* uset_gist depends on constraints without existentially quantified
7593 * variables sorting first.
7595 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7597 const struct constraint
*c1
= (const struct constraint
*)p1
;
7598 const struct constraint
*c2
= (const struct constraint
*)p2
;
7600 unsigned size
= isl_min(c1
->size
, c2
->size
);
7602 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7603 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7608 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7611 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7612 struct isl_basic_map
*bmap
)
7615 struct constraint
*c
;
7620 total
= isl_basic_map_total_dim(bmap
);
7621 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7624 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7626 c
[i
].c
= bmap
->ineq
[i
];
7628 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7629 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7630 bmap
->ineq
[i
] = c
[i
].c
;
7634 isl_basic_map_free(bmap
);
7638 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7639 __isl_take isl_basic_set
*bset
)
7641 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7642 (struct isl_basic_map
*)bset
);
7645 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7649 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7651 bmap
= isl_basic_map_remove_redundancies(bmap
);
7652 bmap
= isl_basic_map_sort_constraints(bmap
);
7653 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7657 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7659 return (struct isl_basic_set
*)isl_basic_map_normalize(
7660 (struct isl_basic_map
*)bset
);
7663 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7664 const __isl_keep isl_basic_map
*bmap2
)
7671 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7672 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7673 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7674 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7675 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7676 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7677 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7678 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7679 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7680 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7681 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7683 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7685 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7687 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7688 return bmap1
->n_eq
- bmap2
->n_eq
;
7689 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7690 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7691 if (bmap1
->n_div
!= bmap2
->n_div
)
7692 return bmap1
->n_div
- bmap2
->n_div
;
7693 total
= isl_basic_map_total_dim(bmap1
);
7694 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7695 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7699 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7700 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7704 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7705 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7712 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7713 const __isl_keep isl_basic_set
*bset2
)
7715 return isl_basic_map_plain_cmp(bset1
, bset2
);
7718 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7719 const __isl_keep isl_set
*set2
)
7725 if (set1
->n
!= set2
->n
)
7726 return set1
->n
- set2
->n
;
7728 for (i
= 0; i
< set1
->n
; ++i
) {
7729 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7737 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7738 __isl_keep isl_basic_map
*bmap2
)
7740 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7743 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7744 __isl_keep isl_basic_set
*bset2
)
7746 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7747 (isl_basic_map
*)bset2
);
7750 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7752 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7753 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7755 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7758 /* We normalize in place, but if anything goes wrong we need
7759 * to return NULL, so we need to make sure we don't change the
7760 * meaning of any possible other copies of map.
7762 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7765 struct isl_basic_map
*bmap
;
7769 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7771 for (i
= 0; i
< map
->n
; ++i
) {
7772 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7775 isl_basic_map_free(map
->p
[i
]);
7778 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7779 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7780 map
= isl_map_remove_empty_parts(map
);
7783 for (i
= map
->n
- 1; i
>= 1; --i
) {
7784 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7786 isl_basic_map_free(map
->p
[i
-1]);
7787 for (j
= i
; j
< map
->n
; ++j
)
7788 map
->p
[j
-1] = map
->p
[j
];
7798 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7800 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7803 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7813 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7816 map1
= isl_map_copy(map1
);
7817 map2
= isl_map_copy(map2
);
7818 map1
= isl_map_normalize(map1
);
7819 map2
= isl_map_normalize(map2
);
7822 equal
= map1
->n
== map2
->n
;
7823 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7824 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7837 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7839 return isl_map_plain_is_equal(map1
, map2
);
7842 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7844 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7845 (struct isl_map
*)set2
);
7848 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7850 return isl_set_plain_is_equal(set1
, set2
);
7853 /* Return an interval that ranges from min to max (inclusive)
7855 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7856 isl_int min
, isl_int max
)
7859 struct isl_basic_set
*bset
= NULL
;
7861 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7865 k
= isl_basic_set_alloc_inequality(bset
);
7868 isl_int_set_si(bset
->ineq
[k
][1], 1);
7869 isl_int_neg(bset
->ineq
[k
][0], min
);
7871 k
= isl_basic_set_alloc_inequality(bset
);
7874 isl_int_set_si(bset
->ineq
[k
][1], -1);
7875 isl_int_set(bset
->ineq
[k
][0], max
);
7879 isl_basic_set_free(bset
);
7883 /* Return the Cartesian product of the basic sets in list (in the given order).
7885 __isl_give isl_basic_set
*isl_basic_set_list_product(
7886 __isl_take
struct isl_basic_set_list
*list
)
7894 struct isl_basic_set
*product
= NULL
;
7898 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7899 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7900 nparam
= isl_basic_set_n_param(list
->p
[0]);
7901 dim
= isl_basic_set_n_dim(list
->p
[0]);
7902 extra
= list
->p
[0]->n_div
;
7903 n_eq
= list
->p
[0]->n_eq
;
7904 n_ineq
= list
->p
[0]->n_ineq
;
7905 for (i
= 1; i
< list
->n
; ++i
) {
7906 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7907 isl_assert(list
->ctx
,
7908 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7909 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7910 extra
+= list
->p
[i
]->n_div
;
7911 n_eq
+= list
->p
[i
]->n_eq
;
7912 n_ineq
+= list
->p
[i
]->n_ineq
;
7914 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7919 for (i
= 0; i
< list
->n
; ++i
) {
7920 isl_basic_set_add_constraints(product
,
7921 isl_basic_set_copy(list
->p
[i
]), dim
);
7922 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7924 isl_basic_set_list_free(list
);
7927 isl_basic_set_free(product
);
7928 isl_basic_set_list_free(list
);
7932 struct isl_basic_map
*isl_basic_map_product(
7933 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7935 isl_space
*dim_result
= NULL
;
7936 struct isl_basic_map
*bmap
;
7937 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7938 struct isl_dim_map
*dim_map1
, *dim_map2
;
7940 if (!bmap1
|| !bmap2
)
7943 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7944 bmap2
->dim
, isl_dim_param
), goto error
);
7945 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
7946 isl_space_copy(bmap2
->dim
));
7948 in1
= isl_basic_map_n_in(bmap1
);
7949 in2
= isl_basic_map_n_in(bmap2
);
7950 out1
= isl_basic_map_n_out(bmap1
);
7951 out2
= isl_basic_map_n_out(bmap2
);
7952 nparam
= isl_basic_map_n_param(bmap1
);
7954 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7955 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7956 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7957 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7958 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7959 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7960 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7961 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7962 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7963 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7964 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7966 bmap
= isl_basic_map_alloc_space(dim_result
,
7967 bmap1
->n_div
+ bmap2
->n_div
,
7968 bmap1
->n_eq
+ bmap2
->n_eq
,
7969 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7970 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7971 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7972 bmap
= isl_basic_map_simplify(bmap
);
7973 return isl_basic_map_finalize(bmap
);
7975 isl_basic_map_free(bmap1
);
7976 isl_basic_map_free(bmap2
);
7980 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7981 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7983 isl_basic_map
*prod
;
7985 prod
= isl_basic_map_product(bmap1
, bmap2
);
7986 prod
= isl_basic_map_flatten(prod
);
7990 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7991 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7993 return isl_basic_map_flat_range_product(bset1
, bset2
);
7996 __isl_give isl_basic_map
*isl_basic_map_domain_product(
7997 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7999 isl_space
*space_result
= NULL
;
8000 isl_basic_map
*bmap
;
8001 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8002 struct isl_dim_map
*dim_map1
, *dim_map2
;
8004 if (!bmap1
|| !bmap2
)
8007 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8008 isl_space_copy(bmap2
->dim
));
8010 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8011 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8012 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8013 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8015 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8016 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8017 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8018 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8019 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8020 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8021 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8022 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8023 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8024 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8025 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8027 bmap
= isl_basic_map_alloc_space(space_result
,
8028 bmap1
->n_div
+ bmap2
->n_div
,
8029 bmap1
->n_eq
+ bmap2
->n_eq
,
8030 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8031 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8032 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8033 bmap
= isl_basic_map_simplify(bmap
);
8034 return isl_basic_map_finalize(bmap
);
8036 isl_basic_map_free(bmap1
);
8037 isl_basic_map_free(bmap2
);
8041 __isl_give isl_basic_map
*isl_basic_map_range_product(
8042 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8044 isl_space
*dim_result
= NULL
;
8045 isl_basic_map
*bmap
;
8046 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8047 struct isl_dim_map
*dim_map1
, *dim_map2
;
8049 if (!bmap1
|| !bmap2
)
8052 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8053 isl_space_copy(bmap2
->dim
));
8055 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8056 out1
= isl_basic_map_n_out(bmap1
);
8057 out2
= isl_basic_map_n_out(bmap2
);
8058 nparam
= isl_basic_map_n_param(bmap1
);
8060 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8061 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8062 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8063 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8064 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8065 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8066 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8067 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8068 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8069 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8070 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8072 bmap
= isl_basic_map_alloc_space(dim_result
,
8073 bmap1
->n_div
+ bmap2
->n_div
,
8074 bmap1
->n_eq
+ bmap2
->n_eq
,
8075 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8076 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8077 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8078 bmap
= isl_basic_map_simplify(bmap
);
8079 return isl_basic_map_finalize(bmap
);
8081 isl_basic_map_free(bmap1
);
8082 isl_basic_map_free(bmap2
);
8086 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8087 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8089 isl_basic_map
*prod
;
8091 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8092 prod
= isl_basic_map_flatten_range(prod
);
8096 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8097 __isl_take isl_map
*map2
,
8098 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8099 __isl_take isl_space
*right
),
8100 __isl_give isl_basic_map
*(*basic_map_product
)(
8101 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8104 struct isl_map
*result
;
8110 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8111 map2
->dim
, isl_dim_param
), goto error
);
8113 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8114 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8115 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8117 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8118 isl_space_copy(map2
->dim
)),
8119 map1
->n
* map2
->n
, flags
);
8122 for (i
= 0; i
< map1
->n
; ++i
)
8123 for (j
= 0; j
< map2
->n
; ++j
) {
8124 struct isl_basic_map
*part
;
8125 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8126 isl_basic_map_copy(map2
->p
[j
]));
8127 if (isl_basic_map_is_empty(part
))
8128 isl_basic_map_free(part
);
8130 result
= isl_map_add_basic_map(result
, part
);
8143 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8145 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8146 __isl_take isl_map
*map2
)
8148 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8151 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8152 __isl_take isl_map
*map2
)
8154 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8157 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8159 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8160 __isl_take isl_map
*map2
)
8164 prod
= isl_map_product(map1
, map2
);
8165 prod
= isl_map_flatten(prod
);
8169 /* Given two set A and B, construct its Cartesian product A x B.
8171 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8173 return isl_map_range_product(set1
, set2
);
8176 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8177 __isl_take isl_set
*set2
)
8179 return isl_map_flat_range_product(set1
, set2
);
8182 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8184 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8185 __isl_take isl_map
*map2
)
8187 return map_product(map1
, map2
, &isl_space_domain_product
,
8188 &isl_basic_map_domain_product
);
8191 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8193 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8194 __isl_take isl_map
*map2
)
8196 return map_product(map1
, map2
, &isl_space_range_product
,
8197 &isl_basic_map_range_product
);
8200 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8201 __isl_take isl_map
*map2
)
8203 return isl_map_align_params_map_map_and(map1
, map2
,
8204 &map_domain_product_aligned
);
8207 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8208 __isl_take isl_map
*map2
)
8210 return isl_map_align_params_map_map_and(map1
, map2
,
8211 &map_range_product_aligned
);
8214 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8216 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8217 __isl_take isl_map
*map2
)
8221 prod
= isl_map_domain_product(map1
, map2
);
8222 prod
= isl_map_flatten_domain(prod
);
8226 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8228 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8229 __isl_take isl_map
*map2
)
8233 prod
= isl_map_range_product(map1
, map2
);
8234 prod
= isl_map_flatten_range(prod
);
8238 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8241 uint32_t hash
= isl_hash_init();
8246 bmap
= isl_basic_map_copy(bmap
);
8247 bmap
= isl_basic_map_normalize(bmap
);
8250 total
= isl_basic_map_total_dim(bmap
);
8251 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8252 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8254 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8255 isl_hash_hash(hash
, c_hash
);
8257 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8258 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8260 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8261 isl_hash_hash(hash
, c_hash
);
8263 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8264 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8266 if (isl_int_is_zero(bmap
->div
[i
][0]))
8268 isl_hash_byte(hash
, i
& 0xFF);
8269 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8270 isl_hash_hash(hash
, c_hash
);
8272 isl_basic_map_free(bmap
);
8276 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8278 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8281 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8288 map
= isl_map_copy(map
);
8289 map
= isl_map_normalize(map
);
8293 hash
= isl_hash_init();
8294 for (i
= 0; i
< map
->n
; ++i
) {
8296 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8297 isl_hash_hash(hash
, bmap_hash
);
8305 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8307 return isl_map_get_hash((isl_map
*)set
);
8310 /* Check if the value for dimension dim is completely determined
8311 * by the values of the other parameters and variables.
8312 * That is, check if dimension dim is involved in an equality.
8314 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8321 nparam
= isl_basic_set_n_param(bset
);
8322 for (i
= 0; i
< bset
->n_eq
; ++i
)
8323 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8328 /* Check if the value for dimension dim is completely determined
8329 * by the values of the other parameters and variables.
8330 * That is, check if dimension dim is involved in an equality
8331 * for each of the subsets.
8333 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8339 for (i
= 0; i
< set
->n
; ++i
) {
8341 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8348 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8350 return set
? set
->n
: 0;
8353 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8354 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8361 for (i
= 0; i
< map
->n
; ++i
)
8362 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8368 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8369 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8376 for (i
= 0; i
< set
->n
; ++i
)
8377 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8383 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8390 bset
= isl_basic_set_cow(bset
);
8394 dim
= isl_basic_set_get_space(bset
);
8395 dim
= isl_space_lift(dim
, bset
->n_div
);
8398 isl_space_free(bset
->dim
);
8400 bset
->extra
-= bset
->n_div
;
8403 bset
= isl_basic_set_finalize(bset
);
8407 isl_basic_set_free(bset
);
8411 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8417 set
= isl_set_align_divs(set
);
8422 set
= isl_set_cow(set
);
8426 n_div
= set
->p
[0]->n_div
;
8427 dim
= isl_set_get_space(set
);
8428 dim
= isl_space_lift(dim
, n_div
);
8431 isl_space_free(set
->dim
);
8434 for (i
= 0; i
< set
->n
; ++i
) {
8435 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8446 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8449 struct isl_basic_map
*bmap
;
8456 set
= isl_set_align_divs(set
);
8461 dim
= isl_set_get_space(set
);
8462 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8464 return isl_map_identity(isl_space_map_from_set(dim
));
8467 n_div
= set
->p
[0]->n_div
;
8468 dim
= isl_space_map_from_set(dim
);
8469 n_param
= isl_space_dim(dim
, isl_dim_param
);
8470 n_set
= isl_space_dim(dim
, isl_dim_in
);
8471 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8472 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8473 for (i
= 0; i
< n_set
; ++i
)
8474 bmap
= var_equal(bmap
, i
);
8476 total
= n_param
+ n_set
+ n_set
+ n_div
;
8477 for (i
= 0; i
< n_div
; ++i
) {
8478 k
= isl_basic_map_alloc_inequality(bmap
);
8481 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8482 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8483 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8484 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8485 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8486 set
->p
[0]->div
[i
][0]);
8488 l
= isl_basic_map_alloc_inequality(bmap
);
8491 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8492 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8493 set
->p
[0]->div
[i
][0]);
8494 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8498 bmap
= isl_basic_map_simplify(bmap
);
8499 bmap
= isl_basic_map_finalize(bmap
);
8500 return isl_map_from_basic_map(bmap
);
8503 isl_basic_map_free(bmap
);
8507 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8515 dim
= isl_basic_set_total_dim(bset
);
8516 size
+= bset
->n_eq
* (1 + dim
);
8517 size
+= bset
->n_ineq
* (1 + dim
);
8518 size
+= bset
->n_div
* (2 + dim
);
8523 int isl_set_size(__isl_keep isl_set
*set
)
8531 for (i
= 0; i
< set
->n
; ++i
)
8532 size
+= isl_basic_set_size(set
->p
[i
]);
8537 /* Check if there is any lower bound (if lower == 0) and/or upper
8538 * bound (if upper == 0) on the specified dim.
8540 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8541 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8548 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8550 pos
+= isl_basic_map_offset(bmap
, type
);
8552 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8553 if (isl_int_is_zero(bmap
->div
[i
][0]))
8555 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8559 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8560 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8563 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8564 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8571 return lower
&& upper
;
8574 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8575 enum isl_dim_type type
, unsigned pos
)
8577 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
8580 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
8581 enum isl_dim_type type
, unsigned pos
)
8583 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
8586 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8587 enum isl_dim_type type
, unsigned pos
)
8589 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8592 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8593 enum isl_dim_type type
, unsigned pos
)
8600 for (i
= 0; i
< map
->n
; ++i
) {
8602 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8603 if (bounded
< 0 || !bounded
)
8610 /* Return 1 if the specified dim is involved in both an upper bound
8611 * and a lower bound.
8613 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8614 enum isl_dim_type type
, unsigned pos
)
8616 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8619 static int has_bound(__isl_keep isl_map
*map
,
8620 enum isl_dim_type type
, unsigned pos
,
8621 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8622 enum isl_dim_type type
, unsigned pos
))
8629 for (i
= 0; i
< map
->n
; ++i
) {
8631 bounded
= fn(map
->p
[i
], type
, pos
);
8632 if (bounded
< 0 || bounded
)
8639 /* Return 1 if the specified dim is involved in any lower bound.
8641 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8642 enum isl_dim_type type
, unsigned pos
)
8644 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8647 /* Return 1 if the specified dim is involved in any upper bound.
8649 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8650 enum isl_dim_type type
, unsigned pos
)
8652 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8655 /* For each of the "n" variables starting at "first", determine
8656 * the sign of the variable and put the results in the first "n"
8657 * elements of the array "signs".
8659 * 1 means that the variable is non-negative
8660 * -1 means that the variable is non-positive
8661 * 0 means the variable attains both positive and negative values.
8663 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8664 unsigned first
, unsigned n
, int *signs
)
8666 isl_vec
*bound
= NULL
;
8667 struct isl_tab
*tab
= NULL
;
8668 struct isl_tab_undo
*snap
;
8671 if (!bset
|| !signs
)
8674 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8675 tab
= isl_tab_from_basic_set(bset
);
8679 isl_seq_clr(bound
->el
, bound
->size
);
8680 isl_int_set_si(bound
->el
[0], -1);
8682 snap
= isl_tab_snap(tab
);
8683 for (i
= 0; i
< n
; ++i
) {
8686 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8687 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8690 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8691 if (isl_tab_rollback(tab
, snap
) < 0)
8699 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8700 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8703 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8704 if (isl_tab_rollback(tab
, snap
) < 0)
8707 signs
[i
] = empty
? -1 : 0;
8711 isl_vec_free(bound
);
8715 isl_vec_free(bound
);
8719 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8720 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8722 if (!bset
|| !signs
)
8724 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8727 first
+= pos(bset
->dim
, type
) - 1;
8728 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8731 /* Check if the given basic map is obviously single-valued.
8732 * In particular, for each output dimension, check that there is
8733 * an equality that defines the output dimension in terms of
8734 * earlier dimensions.
8736 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8746 total
= 1 + isl_basic_map_total_dim(bmap
);
8747 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8748 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8750 for (i
= 0; i
< n_out
; ++i
) {
8751 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8752 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8754 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8755 total
- (o_out
+ i
+ 1)) == -1)
8758 if (j
>= bmap
->n_eq
)
8765 /* Check if the given map is obviously single-valued.
8767 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8776 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8779 /* Check if the given map is single-valued.
8784 * and check if the result is a subset of the identity mapping.
8786 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8793 sv
= isl_map_plain_is_single_valued(map
);
8797 test
= isl_map_reverse(isl_map_copy(map
));
8798 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8800 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8801 id
= isl_map_identity(dim
);
8803 sv
= isl_map_is_subset(test
, id
);
8811 int isl_map_is_injective(__isl_keep isl_map
*map
)
8815 map
= isl_map_copy(map
);
8816 map
= isl_map_reverse(map
);
8817 in
= isl_map_is_single_valued(map
);
8823 /* Check if the given map is obviously injective.
8825 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8829 map
= isl_map_copy(map
);
8830 map
= isl_map_reverse(map
);
8831 in
= isl_map_plain_is_single_valued(map
);
8837 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8841 sv
= isl_map_is_single_valued(map
);
8845 return isl_map_is_injective(map
);
8848 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8850 return isl_map_is_single_valued((isl_map
*)set
);
8853 int isl_map_is_translation(__isl_keep isl_map
*map
)
8858 delta
= isl_map_deltas(isl_map_copy(map
));
8859 ok
= isl_set_is_singleton(delta
);
8860 isl_set_free(delta
);
8865 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8867 if (isl_seq_first_non_zero(p
, pos
) != -1)
8869 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8874 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8883 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8886 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8887 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8888 for (j
= 0; j
< nvar
; ++j
) {
8889 int lower
= 0, upper
= 0;
8890 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8891 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8893 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8899 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8900 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8902 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8904 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8909 if (!lower
|| !upper
)
8916 int isl_set_is_box(__isl_keep isl_set
*set
)
8923 return isl_basic_set_is_box(set
->p
[0]);
8926 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8931 return isl_space_is_wrapping(bset
->dim
);
8934 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8939 return isl_space_is_wrapping(set
->dim
);
8942 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8944 bmap
= isl_basic_map_cow(bmap
);
8948 bmap
->dim
= isl_space_wrap(bmap
->dim
);
8952 bmap
= isl_basic_map_finalize(bmap
);
8954 return (isl_basic_set
*)bmap
;
8956 isl_basic_map_free(bmap
);
8960 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8964 map
= isl_map_cow(map
);
8968 for (i
= 0; i
< map
->n
; ++i
) {
8969 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8973 map
->dim
= isl_space_wrap(map
->dim
);
8977 return (isl_set
*)map
;
8983 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8985 bset
= isl_basic_set_cow(bset
);
8989 bset
->dim
= isl_space_unwrap(bset
->dim
);
8993 bset
= isl_basic_set_finalize(bset
);
8995 return (isl_basic_map
*)bset
;
8997 isl_basic_set_free(bset
);
9001 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9008 if (!isl_set_is_wrapping(set
))
9009 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9012 set
= isl_set_cow(set
);
9016 for (i
= 0; i
< set
->n
; ++i
) {
9017 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9022 set
->dim
= isl_space_unwrap(set
->dim
);
9026 return (isl_map
*)set
;
9032 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9033 enum isl_dim_type type
)
9038 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9041 bmap
= isl_basic_map_cow(bmap
);
9045 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9049 bmap
= isl_basic_map_finalize(bmap
);
9053 isl_basic_map_free(bmap
);
9057 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9058 enum isl_dim_type type
)
9065 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9068 map
= isl_map_cow(map
);
9072 for (i
= 0; i
< map
->n
; ++i
) {
9073 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9077 map
->dim
= isl_space_reset(map
->dim
, type
);
9087 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9092 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9095 bmap
= isl_basic_map_cow(bmap
);
9099 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9103 bmap
= isl_basic_map_finalize(bmap
);
9107 isl_basic_map_free(bmap
);
9111 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9113 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9116 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9117 __isl_take isl_basic_map
*bmap
)
9122 if (!bmap
->dim
->nested
[0])
9125 bmap
= isl_basic_map_cow(bmap
);
9129 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9133 bmap
= isl_basic_map_finalize(bmap
);
9137 isl_basic_map_free(bmap
);
9141 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9142 __isl_take isl_basic_map
*bmap
)
9147 if (!bmap
->dim
->nested
[1])
9150 bmap
= isl_basic_map_cow(bmap
);
9154 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9158 bmap
= isl_basic_map_finalize(bmap
);
9162 isl_basic_map_free(bmap
);
9166 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9173 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9176 map
= isl_map_cow(map
);
9180 for (i
= 0; i
< map
->n
; ++i
) {
9181 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9185 map
->dim
= isl_space_flatten(map
->dim
);
9195 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9197 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9200 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9202 isl_space
*dim
, *flat_dim
;
9205 dim
= isl_set_get_space(set
);
9206 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9207 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9208 map
= isl_map_intersect_domain(map
, set
);
9213 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9220 if (!map
->dim
->nested
[0])
9223 map
= isl_map_cow(map
);
9227 for (i
= 0; i
< map
->n
; ++i
) {
9228 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9232 map
->dim
= isl_space_flatten_domain(map
->dim
);
9242 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9249 if (!map
->dim
->nested
[1])
9252 map
= isl_map_cow(map
);
9256 for (i
= 0; i
< map
->n
; ++i
) {
9257 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9261 map
->dim
= isl_space_flatten_range(map
->dim
);
9271 /* Reorder the dimensions of "bmap" according to the given dim_map
9272 * and set the dimension specification to "dim".
9274 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9275 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9279 bmap
= isl_basic_map_cow(bmap
);
9280 if (!bmap
|| !dim
|| !dim_map
)
9283 res
= isl_basic_map_alloc_space(dim
,
9284 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9285 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9286 res
= isl_basic_map_finalize(res
);
9290 isl_basic_map_free(bmap
);
9291 isl_space_free(dim
);
9295 /* Reorder the dimensions of "map" according to given reordering.
9297 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9298 __isl_take isl_reordering
*r
)
9301 struct isl_dim_map
*dim_map
;
9303 map
= isl_map_cow(map
);
9304 dim_map
= isl_dim_map_from_reordering(r
);
9305 if (!map
|| !r
|| !dim_map
)
9308 for (i
= 0; i
< map
->n
; ++i
) {
9309 struct isl_dim_map
*dim_map_i
;
9311 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9313 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9314 isl_space_copy(r
->dim
), dim_map_i
);
9320 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9322 isl_reordering_free(r
);
9328 isl_reordering_free(r
);
9332 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9333 __isl_take isl_reordering
*r
)
9335 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9338 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9339 __isl_take isl_space
*model
)
9346 ctx
= isl_space_get_ctx(model
);
9347 if (!isl_space_has_named_params(model
))
9348 isl_die(ctx
, isl_error_invalid
,
9349 "model has unnamed parameters", goto error
);
9350 if (!isl_space_has_named_params(map
->dim
))
9351 isl_die(ctx
, isl_error_invalid
,
9352 "relation has unnamed parameters", goto error
);
9353 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9354 isl_reordering
*exp
;
9356 model
= isl_space_drop_dims(model
, isl_dim_in
,
9357 0, isl_space_dim(model
, isl_dim_in
));
9358 model
= isl_space_drop_dims(model
, isl_dim_out
,
9359 0, isl_space_dim(model
, isl_dim_out
));
9360 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9361 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9362 map
= isl_map_realign(map
, exp
);
9365 isl_space_free(model
);
9368 isl_space_free(model
);
9373 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9374 __isl_take isl_space
*model
)
9376 return isl_map_align_params(set
, model
);
9379 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9380 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9381 enum isl_dim_type c2
, enum isl_dim_type c3
,
9382 enum isl_dim_type c4
, enum isl_dim_type c5
)
9384 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9385 struct isl_mat
*mat
;
9391 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9392 isl_basic_map_total_dim(bmap
) + 1);
9395 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9396 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9397 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9398 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9399 isl_int_set(mat
->row
[i
][pos
],
9400 bmap
->eq
[i
][off
+ k
]);
9408 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9409 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9410 enum isl_dim_type c2
, enum isl_dim_type c3
,
9411 enum isl_dim_type c4
, enum isl_dim_type c5
)
9413 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9414 struct isl_mat
*mat
;
9420 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9421 isl_basic_map_total_dim(bmap
) + 1);
9424 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9425 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9426 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9427 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9428 isl_int_set(mat
->row
[i
][pos
],
9429 bmap
->ineq
[i
][off
+ k
]);
9437 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9438 __isl_take isl_space
*dim
,
9439 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9440 enum isl_dim_type c2
, enum isl_dim_type c3
,
9441 enum isl_dim_type c4
, enum isl_dim_type c5
)
9443 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9444 isl_basic_map
*bmap
;
9450 if (!dim
|| !eq
|| !ineq
)
9453 if (eq
->n_col
!= ineq
->n_col
)
9454 isl_die(dim
->ctx
, isl_error_invalid
,
9455 "equalities and inequalities matrices should have "
9456 "same number of columns", goto error
);
9458 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9460 if (eq
->n_col
< total
)
9461 isl_die(dim
->ctx
, isl_error_invalid
,
9462 "number of columns too small", goto error
);
9464 extra
= eq
->n_col
- total
;
9466 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9467 eq
->n_row
, ineq
->n_row
);
9470 for (i
= 0; i
< extra
; ++i
) {
9471 k
= isl_basic_map_alloc_div(bmap
);
9474 isl_int_set_si(bmap
->div
[k
][0], 0);
9476 for (i
= 0; i
< eq
->n_row
; ++i
) {
9477 l
= isl_basic_map_alloc_equality(bmap
);
9480 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9481 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9482 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9483 isl_int_set(bmap
->eq
[l
][off
+ k
],
9489 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9490 l
= isl_basic_map_alloc_inequality(bmap
);
9493 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9494 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9495 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9496 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9503 isl_space_free(dim
);
9509 isl_space_free(dim
);
9515 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9516 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9517 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9519 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9520 c1
, c2
, c3
, c4
, isl_dim_in
);
9523 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9524 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9525 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9527 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9528 c1
, c2
, c3
, c4
, isl_dim_in
);
9531 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9532 __isl_take isl_space
*dim
,
9533 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9534 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9536 return (isl_basic_set
*)
9537 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9538 c1
, c2
, c3
, c4
, isl_dim_in
);
9541 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9546 return isl_space_can_zip(bmap
->dim
);
9549 int isl_map_can_zip(__isl_keep isl_map
*map
)
9554 return isl_space_can_zip(map
->dim
);
9557 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9558 * (A -> C) -> (B -> D).
9560 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9569 if (!isl_basic_map_can_zip(bmap
))
9570 isl_die(bmap
->ctx
, isl_error_invalid
,
9571 "basic map cannot be zipped", goto error
);
9572 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9573 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9574 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9575 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9576 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9579 bmap
->dim
= isl_space_zip(bmap
->dim
);
9584 isl_basic_map_free(bmap
);
9588 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9589 * (A -> C) -> (B -> D).
9591 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9598 if (!isl_map_can_zip(map
))
9599 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9602 map
= isl_map_cow(map
);
9606 for (i
= 0; i
< map
->n
; ++i
) {
9607 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9612 map
->dim
= isl_space_zip(map
->dim
);
9622 /* Construct a basic map mapping the domain of the affine expression
9623 * to a one-dimensional range prescribed by the affine expression.
9625 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9629 isl_local_space
*ls
;
9630 isl_basic_map
*bmap
;
9635 ls
= isl_aff_get_local_space(aff
);
9636 bmap
= isl_basic_map_from_local_space(ls
);
9637 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9638 k
= isl_basic_map_alloc_equality(bmap
);
9642 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9643 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9644 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9645 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9646 aff
->v
->size
- (pos
+ 1));
9649 bmap
= isl_basic_map_finalize(bmap
);
9653 isl_basic_map_free(bmap
);
9657 /* Construct a basic map mapping the domain the multi-affine expression
9658 * to its range, with each dimension in the range equated to the
9659 * corresponding affine expression.
9661 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9662 __isl_take isl_multi_aff
*maff
)
9666 isl_basic_map
*bmap
;
9671 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9672 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9673 "invalid space", return isl_multi_aff_free(maff
));
9675 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9676 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9678 for (i
= 0; i
< maff
->n
; ++i
) {
9680 isl_basic_map
*bmap_i
;
9682 aff
= isl_aff_copy(maff
->p
[i
]);
9683 bmap_i
= isl_basic_map_from_aff(aff
);
9685 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9688 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9690 isl_multi_aff_free(maff
);
9694 /* Construct a basic map mapping a domain in the given space to
9695 * to an n-dimensional range, with n the number of elements in the list,
9696 * where each coordinate in the range is prescribed by the
9697 * corresponding affine expression.
9698 * The domains of all affine expressions in the list are assumed to match
9701 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9702 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9706 isl_basic_map
*bmap
;
9711 dim
= isl_space_from_domain(domain_dim
);
9712 bmap
= isl_basic_map_universe(dim
);
9714 for (i
= 0; i
< list
->n
; ++i
) {
9716 isl_basic_map
*bmap_i
;
9718 aff
= isl_aff_copy(list
->p
[i
]);
9719 bmap_i
= isl_basic_map_from_aff(aff
);
9721 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9724 isl_aff_list_free(list
);
9728 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9729 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9731 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9734 /* Add a constraint imposing that the given two dimensions are equal.
9736 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9737 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9739 isl_basic_map
*bmap
= NULL
;
9745 if (pos1
>= isl_map_dim(map
, type1
))
9746 isl_die(map
->ctx
, isl_error_invalid
,
9747 "index out of bounds", goto error
);
9748 if (pos2
>= isl_map_dim(map
, type2
))
9749 isl_die(map
->ctx
, isl_error_invalid
,
9750 "index out of bounds", goto error
);
9752 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9753 i
= isl_basic_map_alloc_equality(bmap
);
9756 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9757 pos1
+= isl_basic_map_offset(bmap
, type1
);
9758 pos2
+= isl_basic_map_offset(bmap
, type2
);
9759 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9760 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9761 bmap
= isl_basic_map_finalize(bmap
);
9763 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9767 isl_basic_map_free(bmap
);
9772 /* Add a constraint imposing that the given two dimensions have opposite values.
9774 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9775 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9777 isl_basic_map
*bmap
= NULL
;
9783 if (pos1
>= isl_map_dim(map
, type1
))
9784 isl_die(map
->ctx
, isl_error_invalid
,
9785 "index out of bounds", goto error
);
9786 if (pos2
>= isl_map_dim(map
, type2
))
9787 isl_die(map
->ctx
, isl_error_invalid
,
9788 "index out of bounds", goto error
);
9790 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9791 i
= isl_basic_map_alloc_equality(bmap
);
9794 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9795 pos1
+= isl_basic_map_offset(bmap
, type1
);
9796 pos2
+= isl_basic_map_offset(bmap
, type2
);
9797 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9798 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9799 bmap
= isl_basic_map_finalize(bmap
);
9801 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9805 isl_basic_map_free(bmap
);
9810 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9814 isl_local_space
*ls
;
9819 if (!isl_basic_map_divs_known(bmap
))
9820 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9821 "some divs are unknown", return NULL
);
9823 ls
= isl_basic_map_get_local_space(bmap
);
9824 div
= isl_local_space_get_div(ls
, pos
);
9825 isl_local_space_free(ls
);
9830 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9833 return isl_basic_map_get_div(bset
, pos
);
9836 /* Plug in "subs" for dimension "type", "pos" of "bset".
9838 * Let i be the dimension to replace and let "subs" be of the form
9842 * Any integer division with a non-zero coefficient for i,
9844 * floor((a i + g)/m)
9848 * floor((a f + d g)/(m d))
9850 * Constraints of the form
9858 __isl_give isl_basic_set
*isl_basic_set_substitute(
9859 __isl_take isl_basic_set
*bset
,
9860 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9866 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9869 bset
= isl_basic_set_cow(bset
);
9873 ctx
= isl_basic_set_get_ctx(bset
);
9874 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9875 isl_die(ctx
, isl_error_invalid
,
9876 "spaces don't match", goto error
);
9877 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9878 isl_die(ctx
, isl_error_unsupported
,
9879 "cannot handle divs yet", goto error
);
9881 pos
+= isl_basic_set_offset(bset
, type
);
9885 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9886 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9888 isl_int_set(v
, bset
->eq
[i
][pos
]);
9889 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9890 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9891 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9894 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9895 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9897 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9898 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9899 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9900 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9903 for (i
= 0; i
< bset
->n_div
; ++i
) {
9904 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9906 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9907 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9908 isl_seq_combine(bset
->div
[i
] + 1,
9909 subs
->v
->el
[0], bset
->div
[i
] + 1,
9910 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9911 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9916 bset
= isl_basic_set_simplify(bset
);
9917 return isl_basic_set_finalize(bset
);
9919 isl_basic_set_free(bset
);
9923 /* Plug in "subs" for dimension "type", "pos" of "set".
9925 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
9926 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9930 if (set
&& isl_set_plain_is_empty(set
))
9933 set
= isl_set_cow(set
);
9937 for (i
= set
->n
- 1; i
>= 0; --i
) {
9938 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
9941 if (isl_basic_set_plain_is_empty(set
->p
[i
])) {
9942 isl_basic_set_free(set
->p
[i
]);
9943 if (i
!= set
->n
- 1)
9944 set
->p
[i
] = set
->p
[set
->n
- 1];