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 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2433 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2435 return isl_basic_set_intersect(bset1
, bset2
);
2438 /* Special case of isl_map_intersect, where both map1 and map2
2439 * are convex, without any divs and such that either map1 or map2
2440 * contains a single constraint. This constraint is then simply
2441 * added to the other map.
2443 static __isl_give isl_map
*map_intersect_add_constraint(
2444 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2446 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2447 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2448 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2449 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2451 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2452 return isl_map_intersect(map2
, map1
);
2454 isl_assert(map2
->ctx
,
2455 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2457 map1
= isl_map_cow(map1
);
2460 if (isl_map_plain_is_empty(map1
)) {
2464 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2465 if (map2
->p
[0]->n_eq
== 1)
2466 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2468 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2469 map2
->p
[0]->ineq
[0]);
2471 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2472 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2476 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2477 isl_basic_map_free(map1
->p
[0]);
2490 /* map2 may be either a parameter domain or a map living in the same
2493 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2494 __isl_take isl_map
*map2
)
2497 struct isl_map
*result
;
2503 if (isl_map_plain_is_empty(map1
) &&
2504 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2508 if (isl_map_plain_is_empty(map2
) &&
2509 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2514 if (map1
->n
== 1 && map2
->n
== 1 &&
2515 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2516 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2517 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2518 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2519 return map_intersect_add_constraint(map1
, map2
);
2521 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2522 isl_space_dim(map2
->dim
, isl_dim_param
))
2523 isl_assert(map1
->ctx
,
2524 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2526 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2527 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2528 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2530 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2531 map1
->n
* map2
->n
, flags
);
2534 for (i
= 0; i
< map1
->n
; ++i
)
2535 for (j
= 0; j
< map2
->n
; ++j
) {
2536 struct isl_basic_map
*part
;
2537 part
= isl_basic_map_intersect(
2538 isl_basic_map_copy(map1
->p
[i
]),
2539 isl_basic_map_copy(map2
->p
[j
]));
2540 if (isl_basic_map_is_empty(part
))
2541 isl_basic_map_free(part
);
2543 result
= isl_map_add_basic_map(result
, part
);
2556 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2557 __isl_take isl_map
*map2
)
2561 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2562 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2563 "spaces don't match", goto error
);
2564 return map_intersect_internal(map1
, map2
);
2571 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2572 __isl_take isl_map
*map2
)
2574 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2577 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2579 return (struct isl_set
*)
2580 isl_map_intersect((struct isl_map
*)set1
,
2581 (struct isl_map
*)set2
);
2584 /* map_intersect_internal accepts intersections
2585 * with parameter domains, so we can just call that function.
2587 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2588 __isl_take isl_set
*params
)
2590 return map_intersect_internal(map
, params
);
2593 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2594 __isl_take isl_map
*map2
)
2596 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
2599 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2600 __isl_take isl_set
*params
)
2602 return isl_map_intersect_params(set
, params
);
2605 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2608 struct isl_basic_set
*bset
;
2613 bmap
= isl_basic_map_cow(bmap
);
2616 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
2617 in
= isl_basic_map_n_in(bmap
);
2618 bset
= isl_basic_set_from_basic_map(bmap
);
2619 bset
= isl_basic_set_swap_vars(bset
, in
);
2620 return isl_basic_map_from_basic_set(bset
, dim
);
2623 static __isl_give isl_basic_map
*basic_map_space_reset(
2624 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
2628 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
2631 space
= isl_basic_map_get_space(bmap
);
2632 space
= isl_space_reset(space
, type
);
2633 bmap
= isl_basic_map_reset_space(bmap
, space
);
2637 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2638 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2641 struct isl_basic_map
*res
;
2642 struct isl_dim_map
*dim_map
;
2643 unsigned total
, off
;
2644 enum isl_dim_type t
;
2647 return basic_map_space_reset(bmap
, type
);
2652 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
2654 total
= isl_basic_map_total_dim(bmap
) + n
;
2655 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2657 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2659 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2661 unsigned size
= isl_basic_map_dim(bmap
, t
);
2662 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2664 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2665 pos
, size
- pos
, off
+ pos
+ n
);
2667 off
+= isl_space_dim(res_dim
, t
);
2669 isl_dim_map_div(dim_map
, bmap
, off
);
2671 res
= isl_basic_map_alloc_space(res_dim
,
2672 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2673 if (isl_basic_map_is_rational(bmap
))
2674 res
= isl_basic_map_set_rational(res
);
2675 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2676 return isl_basic_map_finalize(res
);
2679 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2680 enum isl_dim_type type
, unsigned n
)
2684 return isl_basic_map_insert(bmap
, type
,
2685 isl_basic_map_dim(bmap
, type
), n
);
2688 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2689 enum isl_dim_type type
, unsigned n
)
2693 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2694 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2696 isl_basic_set_free(bset
);
2700 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
2701 enum isl_dim_type type
)
2705 if (!isl_space_is_named_or_nested(map
->dim
, type
))
2708 space
= isl_map_get_space(map
);
2709 space
= isl_space_reset(space
, type
);
2710 map
= isl_map_reset_space(map
, space
);
2714 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
2715 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2720 return map_space_reset(map
, type
);
2722 map
= isl_map_cow(map
);
2726 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
2730 for (i
= 0; i
< map
->n
; ++i
) {
2731 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2742 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
2743 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2745 return isl_map_insert_dims(set
, type
, pos
, n
);
2748 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2749 enum isl_dim_type type
, unsigned n
)
2753 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
2756 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2757 enum isl_dim_type type
, unsigned n
)
2761 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2762 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2768 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2769 __isl_take isl_basic_map
*bmap
,
2770 enum isl_dim_type dst_type
, unsigned dst_pos
,
2771 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2773 struct isl_dim_map
*dim_map
;
2774 struct isl_basic_map
*res
;
2775 enum isl_dim_type t
;
2776 unsigned total
, off
;
2783 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2786 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2789 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2791 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2792 pos(bmap
->dim
, src_type
) + src_pos
+
2793 ((src_type
< dst_type
) ? n
: 0)) {
2794 bmap
= isl_basic_map_cow(bmap
);
2798 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2799 src_type
, src_pos
, n
);
2803 bmap
= isl_basic_map_finalize(bmap
);
2808 total
= isl_basic_map_total_dim(bmap
);
2809 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2812 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2813 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2814 if (t
== dst_type
) {
2815 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2818 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2821 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2822 dst_pos
, size
- dst_pos
, off
);
2823 off
+= size
- dst_pos
;
2824 } else if (t
== src_type
) {
2825 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2828 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2829 src_pos
+ n
, size
- src_pos
- n
, off
);
2830 off
+= size
- src_pos
- n
;
2832 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2836 isl_dim_map_div(dim_map
, bmap
, off
);
2838 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2839 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2840 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2842 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
2843 src_type
, src_pos
, n
);
2847 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2848 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2849 bmap
= isl_basic_map_finalize(bmap
);
2853 isl_basic_map_free(bmap
);
2857 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2858 enum isl_dim_type dst_type
, unsigned dst_pos
,
2859 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2861 return (isl_basic_set
*)isl_basic_map_move_dims(
2862 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2865 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2866 enum isl_dim_type dst_type
, unsigned dst_pos
,
2867 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2871 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2872 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2873 src_type
, src_pos
, n
);
2879 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2880 enum isl_dim_type dst_type
, unsigned dst_pos
,
2881 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2890 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2893 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2896 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2898 map
= isl_map_cow(map
);
2902 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2906 for (i
= 0; i
< map
->n
; ++i
) {
2907 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2909 src_type
, src_pos
, n
);
2920 /* Move the specified dimensions to the last columns right before
2921 * the divs. Don't change the dimension specification of bmap.
2922 * That's the responsibility of the caller.
2924 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2925 enum isl_dim_type type
, unsigned first
, unsigned n
)
2927 struct isl_dim_map
*dim_map
;
2928 struct isl_basic_map
*res
;
2929 enum isl_dim_type t
;
2930 unsigned total
, off
;
2934 if (pos(bmap
->dim
, type
) + first
+ n
==
2935 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
2938 total
= isl_basic_map_total_dim(bmap
);
2939 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2942 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2943 unsigned size
= isl_space_dim(bmap
->dim
, t
);
2945 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2948 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2949 first
, n
, total
- bmap
->n_div
- n
);
2950 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2951 first
+ n
, size
- (first
+ n
), off
);
2952 off
+= size
- (first
+ n
);
2954 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2958 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2960 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
2961 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2962 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2966 /* Turn the n dimensions of type type, starting at first
2967 * into existentially quantified variables.
2969 __isl_give isl_basic_map
*isl_basic_map_project_out(
2970 __isl_take isl_basic_map
*bmap
,
2971 enum isl_dim_type type
, unsigned first
, unsigned n
)
2984 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2985 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2987 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2990 bmap
= move_last(bmap
, type
, first
, n
);
2991 bmap
= isl_basic_map_cow(bmap
);
2995 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
2996 old
= bmap
->block2
.data
;
2997 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2998 (bmap
->extra
+ n
) * (1 + row_size
));
2999 if (!bmap
->block2
.data
)
3001 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3004 for (i
= 0; i
< n
; ++i
) {
3005 new_div
[i
] = bmap
->block2
.data
+
3006 (bmap
->extra
+ i
) * (1 + row_size
);
3007 isl_seq_clr(new_div
[i
], 1 + row_size
);
3009 for (i
= 0; i
< bmap
->extra
; ++i
)
3010 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3012 bmap
->div
= new_div
;
3016 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3019 bmap
= isl_basic_map_simplify(bmap
);
3020 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3021 return isl_basic_map_finalize(bmap
);
3023 isl_basic_map_free(bmap
);
3027 /* Turn the n dimensions of type type, starting at first
3028 * into existentially quantified variables.
3030 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3031 enum isl_dim_type type
, unsigned first
, unsigned n
)
3033 return (isl_basic_set
*)isl_basic_map_project_out(
3034 (isl_basic_map
*)bset
, type
, first
, n
);
3037 /* Turn the n dimensions of type type, starting at first
3038 * into existentially quantified variables.
3040 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3041 enum isl_dim_type type
, unsigned first
, unsigned n
)
3051 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3053 map
= isl_map_cow(map
);
3057 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3061 for (i
= 0; i
< map
->n
; ++i
) {
3062 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3073 /* Turn the n dimensions of type type, starting at first
3074 * into existentially quantified variables.
3076 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3077 enum isl_dim_type type
, unsigned first
, unsigned n
)
3079 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3082 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3086 for (i
= 0; i
< n
; ++i
) {
3087 j
= isl_basic_map_alloc_div(bmap
);
3090 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3094 isl_basic_map_free(bmap
);
3098 struct isl_basic_map
*isl_basic_map_apply_range(
3099 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3101 isl_space
*dim_result
= NULL
;
3102 struct isl_basic_map
*bmap
;
3103 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3104 struct isl_dim_map
*dim_map1
, *dim_map2
;
3106 if (!bmap1
|| !bmap2
)
3109 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3110 isl_space_copy(bmap2
->dim
));
3112 n_in
= isl_basic_map_n_in(bmap1
);
3113 n_out
= isl_basic_map_n_out(bmap2
);
3114 n
= isl_basic_map_n_out(bmap1
);
3115 nparam
= isl_basic_map_n_param(bmap1
);
3117 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3118 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3119 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3120 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3121 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3122 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3123 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3124 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3125 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3126 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3127 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3129 bmap
= isl_basic_map_alloc_space(dim_result
,
3130 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3131 bmap1
->n_eq
+ bmap2
->n_eq
,
3132 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3133 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3134 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3135 bmap
= add_divs(bmap
, n
);
3136 bmap
= isl_basic_map_simplify(bmap
);
3137 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3138 return isl_basic_map_finalize(bmap
);
3140 isl_basic_map_free(bmap1
);
3141 isl_basic_map_free(bmap2
);
3145 struct isl_basic_set
*isl_basic_set_apply(
3146 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3151 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3154 return (struct isl_basic_set
*)
3155 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3157 isl_basic_set_free(bset
);
3158 isl_basic_map_free(bmap
);
3162 struct isl_basic_map
*isl_basic_map_apply_domain(
3163 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3165 if (!bmap1
|| !bmap2
)
3168 isl_assert(bmap1
->ctx
,
3169 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3170 isl_assert(bmap1
->ctx
,
3171 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3174 bmap1
= isl_basic_map_reverse(bmap1
);
3175 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3176 return isl_basic_map_reverse(bmap1
);
3178 isl_basic_map_free(bmap1
);
3179 isl_basic_map_free(bmap2
);
3183 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3184 * A \cap B -> f(A) + f(B)
3186 struct isl_basic_map
*isl_basic_map_sum(
3187 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3189 unsigned n_in
, n_out
, nparam
, total
, pos
;
3190 struct isl_basic_map
*bmap
= NULL
;
3191 struct isl_dim_map
*dim_map1
, *dim_map2
;
3194 if (!bmap1
|| !bmap2
)
3197 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3200 nparam
= isl_basic_map_n_param(bmap1
);
3201 n_in
= isl_basic_map_n_in(bmap1
);
3202 n_out
= isl_basic_map_n_out(bmap1
);
3204 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3205 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3206 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3207 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3208 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3209 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3210 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3211 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3212 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3213 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3214 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3216 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3217 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3218 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3219 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3220 for (i
= 0; i
< n_out
; ++i
) {
3221 int j
= isl_basic_map_alloc_equality(bmap
);
3224 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3225 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3226 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3227 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3229 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3230 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3231 bmap
= add_divs(bmap
, 2 * n_out
);
3233 bmap
= isl_basic_map_simplify(bmap
);
3234 return isl_basic_map_finalize(bmap
);
3236 isl_basic_map_free(bmap
);
3237 isl_basic_map_free(bmap1
);
3238 isl_basic_map_free(bmap2
);
3242 /* Given two maps A -> f(A) and B -> g(B), construct a map
3243 * A \cap B -> f(A) + f(B)
3245 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3247 struct isl_map
*result
;
3253 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3255 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3256 map1
->n
* map2
->n
, 0);
3259 for (i
= 0; i
< map1
->n
; ++i
)
3260 for (j
= 0; j
< map2
->n
; ++j
) {
3261 struct isl_basic_map
*part
;
3262 part
= isl_basic_map_sum(
3263 isl_basic_map_copy(map1
->p
[i
]),
3264 isl_basic_map_copy(map2
->p
[j
]));
3265 if (isl_basic_map_is_empty(part
))
3266 isl_basic_map_free(part
);
3268 result
= isl_map_add_basic_map(result
, part
);
3281 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3282 __isl_take isl_set
*set2
)
3284 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3287 /* Given a basic map A -> f(A), construct A -> -f(A).
3289 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3294 bmap
= isl_basic_map_cow(bmap
);
3298 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3299 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3300 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3301 for (j
= 0; j
< n
; ++j
)
3302 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3303 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3304 for (j
= 0; j
< n
; ++j
)
3305 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3306 for (i
= 0; i
< bmap
->n_div
; ++i
)
3307 for (j
= 0; j
< n
; ++j
)
3308 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3309 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3310 return isl_basic_map_finalize(bmap
);
3313 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3315 return isl_basic_map_neg(bset
);
3318 /* Given a map A -> f(A), construct A -> -f(A).
3320 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3324 map
= isl_map_cow(map
);
3328 for (i
= 0; i
< map
->n
; ++i
) {
3329 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3340 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3342 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3345 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3346 * A -> floor(f(A)/d).
3348 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3351 unsigned n_in
, n_out
, nparam
, total
, pos
;
3352 struct isl_basic_map
*result
= NULL
;
3353 struct isl_dim_map
*dim_map
;
3359 nparam
= isl_basic_map_n_param(bmap
);
3360 n_in
= isl_basic_map_n_in(bmap
);
3361 n_out
= isl_basic_map_n_out(bmap
);
3363 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3364 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3365 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3366 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3367 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3368 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3370 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3371 bmap
->n_div
+ n_out
,
3372 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3373 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3374 result
= add_divs(result
, n_out
);
3375 for (i
= 0; i
< n_out
; ++i
) {
3377 j
= isl_basic_map_alloc_inequality(result
);
3380 isl_seq_clr(result
->ineq
[j
], 1+total
);
3381 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3382 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3383 j
= isl_basic_map_alloc_inequality(result
);
3386 isl_seq_clr(result
->ineq
[j
], 1+total
);
3387 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3388 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3389 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3392 result
= isl_basic_map_simplify(result
);
3393 return isl_basic_map_finalize(result
);
3395 isl_basic_map_free(result
);
3399 /* Given a map A -> f(A) and an integer d, construct a map
3400 * A -> floor(f(A)/d).
3402 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3406 map
= isl_map_cow(map
);
3410 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3411 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3412 for (i
= 0; i
< map
->n
; ++i
) {
3413 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3424 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3430 i
= isl_basic_map_alloc_equality(bmap
);
3433 nparam
= isl_basic_map_n_param(bmap
);
3434 n_in
= isl_basic_map_n_in(bmap
);
3435 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3436 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3437 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3438 return isl_basic_map_finalize(bmap
);
3440 isl_basic_map_free(bmap
);
3444 /* Add a constraints to "bmap" expressing i_pos < o_pos
3446 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3452 i
= isl_basic_map_alloc_inequality(bmap
);
3455 nparam
= isl_basic_map_n_param(bmap
);
3456 n_in
= isl_basic_map_n_in(bmap
);
3457 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3458 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3459 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3460 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3461 return isl_basic_map_finalize(bmap
);
3463 isl_basic_map_free(bmap
);
3467 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3469 static __isl_give isl_basic_map
*var_less_or_equal(
3470 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3476 i
= isl_basic_map_alloc_inequality(bmap
);
3479 nparam
= isl_basic_map_n_param(bmap
);
3480 n_in
= isl_basic_map_n_in(bmap
);
3481 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3482 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3483 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3484 return isl_basic_map_finalize(bmap
);
3486 isl_basic_map_free(bmap
);
3490 /* Add a constraints to "bmap" expressing i_pos > o_pos
3492 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3498 i
= isl_basic_map_alloc_inequality(bmap
);
3501 nparam
= isl_basic_map_n_param(bmap
);
3502 n_in
= isl_basic_map_n_in(bmap
);
3503 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3504 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3505 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3506 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3507 return isl_basic_map_finalize(bmap
);
3509 isl_basic_map_free(bmap
);
3513 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3515 static __isl_give isl_basic_map
*var_more_or_equal(
3516 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3522 i
= isl_basic_map_alloc_inequality(bmap
);
3525 nparam
= isl_basic_map_n_param(bmap
);
3526 n_in
= isl_basic_map_n_in(bmap
);
3527 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3528 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3529 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3530 return isl_basic_map_finalize(bmap
);
3532 isl_basic_map_free(bmap
);
3536 __isl_give isl_basic_map
*isl_basic_map_equal(
3537 __isl_take isl_space
*dim
, unsigned n_equal
)
3540 struct isl_basic_map
*bmap
;
3541 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
3544 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3545 bmap
= var_equal(bmap
, i
);
3546 return isl_basic_map_finalize(bmap
);
3549 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3551 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
3555 struct isl_basic_map
*bmap
;
3556 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3559 for (i
= 0; i
< pos
&& bmap
; ++i
)
3560 bmap
= var_equal(bmap
, i
);
3562 bmap
= var_less(bmap
, pos
);
3563 return isl_basic_map_finalize(bmap
);
3566 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3568 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3569 __isl_take isl_space
*dim
, unsigned pos
)
3572 isl_basic_map
*bmap
;
3574 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3575 for (i
= 0; i
< pos
; ++i
)
3576 bmap
= var_equal(bmap
, i
);
3577 bmap
= var_less_or_equal(bmap
, pos
);
3578 return isl_basic_map_finalize(bmap
);
3581 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3583 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
3587 struct isl_basic_map
*bmap
;
3588 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3591 for (i
= 0; i
< pos
&& bmap
; ++i
)
3592 bmap
= var_equal(bmap
, i
);
3594 bmap
= var_more(bmap
, pos
);
3595 return isl_basic_map_finalize(bmap
);
3598 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3600 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3601 __isl_take isl_space
*dim
, unsigned pos
)
3604 isl_basic_map
*bmap
;
3606 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
3607 for (i
= 0; i
< pos
; ++i
)
3608 bmap
= var_equal(bmap
, i
);
3609 bmap
= var_more_or_equal(bmap
, pos
);
3610 return isl_basic_map_finalize(bmap
);
3613 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
3614 unsigned n
, int equal
)
3616 struct isl_map
*map
;
3619 if (n
== 0 && equal
)
3620 return isl_map_universe(dims
);
3622 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3624 for (i
= 0; i
+ 1 < n
; ++i
)
3625 map
= isl_map_add_basic_map(map
,
3626 isl_basic_map_less_at(isl_space_copy(dims
), i
));
3629 map
= isl_map_add_basic_map(map
,
3630 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3632 map
= isl_map_add_basic_map(map
,
3633 isl_basic_map_less_at(dims
, n
- 1));
3635 isl_space_free(dims
);
3640 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
3644 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3647 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
3649 return map_lex_lte_first(dim
, n
, 0);
3652 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
3654 return map_lex_lte_first(dim
, n
, 1);
3657 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
3659 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
3662 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
3664 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
3667 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
3668 unsigned n
, int equal
)
3670 struct isl_map
*map
;
3673 if (n
== 0 && equal
)
3674 return isl_map_universe(dims
);
3676 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
3678 for (i
= 0; i
+ 1 < n
; ++i
)
3679 map
= isl_map_add_basic_map(map
,
3680 isl_basic_map_more_at(isl_space_copy(dims
), i
));
3683 map
= isl_map_add_basic_map(map
,
3684 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3686 map
= isl_map_add_basic_map(map
,
3687 isl_basic_map_more_at(dims
, n
- 1));
3689 isl_space_free(dims
);
3694 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
3698 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3701 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
3703 return map_lex_gte_first(dim
, n
, 0);
3706 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
3708 return map_lex_gte_first(dim
, n
, 1);
3711 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
3713 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
3716 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
3718 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
3721 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3722 __isl_take isl_set
*set2
)
3725 map
= isl_map_lex_le(isl_set_get_space(set1
));
3726 map
= isl_map_intersect_domain(map
, set1
);
3727 map
= isl_map_intersect_range(map
, set2
);
3731 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3732 __isl_take isl_set
*set2
)
3735 map
= isl_map_lex_lt(isl_set_get_space(set1
));
3736 map
= isl_map_intersect_domain(map
, set1
);
3737 map
= isl_map_intersect_range(map
, set2
);
3741 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3742 __isl_take isl_set
*set2
)
3745 map
= isl_map_lex_ge(isl_set_get_space(set1
));
3746 map
= isl_map_intersect_domain(map
, set1
);
3747 map
= isl_map_intersect_range(map
, set2
);
3751 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3752 __isl_take isl_set
*set2
)
3755 map
= isl_map_lex_gt(isl_set_get_space(set1
));
3756 map
= isl_map_intersect_domain(map
, set1
);
3757 map
= isl_map_intersect_range(map
, set2
);
3761 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3762 __isl_take isl_map
*map2
)
3765 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
3766 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3767 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3771 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3772 __isl_take isl_map
*map2
)
3775 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
3776 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3777 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3781 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3782 __isl_take isl_map
*map2
)
3785 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
3786 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3787 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3791 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3792 __isl_take isl_map
*map2
)
3795 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
3796 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3797 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3801 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
3802 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
3804 struct isl_basic_map
*bmap
;
3806 bset
= isl_basic_set_cow(bset
);
3810 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
3811 isl_space_free(bset
->dim
);
3812 bmap
= (struct isl_basic_map
*) bset
;
3814 return isl_basic_map_finalize(bmap
);
3816 isl_basic_set_free(bset
);
3817 isl_space_free(dim
);
3821 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3825 if (bmap
->dim
->n_in
== 0)
3826 return (struct isl_basic_set
*)bmap
;
3827 bmap
= isl_basic_map_cow(bmap
);
3830 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
3833 bmap
= isl_basic_map_finalize(bmap
);
3834 return (struct isl_basic_set
*)bmap
;
3836 isl_basic_map_free(bmap
);
3840 /* For a div d = floor(f/m), add the constraints
3843 * -(f-(n-1)) + m d >= 0
3845 * Note that the second constraint is the negation of
3849 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3850 unsigned pos
, isl_int
*div
)
3853 unsigned total
= isl_basic_map_total_dim(bmap
);
3855 i
= isl_basic_map_alloc_inequality(bmap
);
3858 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3859 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3861 j
= isl_basic_map_alloc_inequality(bmap
);
3864 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3865 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3866 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3870 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3871 unsigned pos
, isl_int
*div
)
3873 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3877 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3879 unsigned total
= isl_basic_map_total_dim(bmap
);
3880 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3882 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3886 struct isl_basic_set
*isl_basic_map_underlying_set(
3887 struct isl_basic_map
*bmap
)
3891 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3893 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3894 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3895 return (struct isl_basic_set
*)bmap
;
3896 bmap
= isl_basic_map_cow(bmap
);
3899 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
3902 bmap
->extra
-= bmap
->n_div
;
3904 bmap
= isl_basic_map_finalize(bmap
);
3905 return (struct isl_basic_set
*)bmap
;
3910 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3911 __isl_take isl_basic_set
*bset
)
3913 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3916 struct isl_basic_map
*isl_basic_map_overlying_set(
3917 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3919 struct isl_basic_map
*bmap
;
3920 struct isl_ctx
*ctx
;
3927 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3928 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3929 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3931 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3932 isl_basic_map_free(like
);
3933 return (struct isl_basic_map
*)bset
;
3935 bset
= isl_basic_set_cow(bset
);
3938 total
= bset
->dim
->n_out
+ bset
->extra
;
3939 bmap
= (struct isl_basic_map
*)bset
;
3940 isl_space_free(bmap
->dim
);
3941 bmap
->dim
= isl_space_copy(like
->dim
);
3944 bmap
->n_div
= like
->n_div
;
3945 bmap
->extra
+= like
->n_div
;
3949 ltotal
= total
- bmap
->extra
+ like
->extra
;
3952 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3953 bmap
->extra
* (1 + 1 + total
));
3954 if (isl_blk_is_error(bmap
->block2
))
3956 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3960 for (i
= 0; i
< bmap
->extra
; ++i
)
3961 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3962 for (i
= 0; i
< like
->n_div
; ++i
) {
3963 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3964 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3966 bmap
= isl_basic_map_extend_constraints(bmap
,
3967 0, 2 * like
->n_div
);
3968 for (i
= 0; i
< like
->n_div
; ++i
) {
3969 if (isl_int_is_zero(bmap
->div
[i
][0]))
3971 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3975 isl_basic_map_free(like
);
3976 bmap
= isl_basic_map_simplify(bmap
);
3977 bmap
= isl_basic_map_finalize(bmap
);
3980 isl_basic_map_free(like
);
3981 isl_basic_set_free(bset
);
3985 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3986 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3988 return (struct isl_basic_set
*)
3989 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3992 struct isl_set
*isl_set_from_underlying_set(
3993 struct isl_set
*set
, struct isl_basic_set
*like
)
3999 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4001 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4002 isl_basic_set_free(like
);
4005 set
= isl_set_cow(set
);
4008 for (i
= 0; i
< set
->n
; ++i
) {
4009 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4010 isl_basic_set_copy(like
));
4014 isl_space_free(set
->dim
);
4015 set
->dim
= isl_space_copy(like
->dim
);
4018 isl_basic_set_free(like
);
4021 isl_basic_set_free(like
);
4026 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4030 map
= isl_map_cow(map
);
4033 map
->dim
= isl_space_cow(map
->dim
);
4037 for (i
= 1; i
< map
->n
; ++i
)
4038 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4040 for (i
= 0; i
< map
->n
; ++i
) {
4041 map
->p
[i
] = (struct isl_basic_map
*)
4042 isl_basic_map_underlying_set(map
->p
[i
]);
4047 map
->dim
= isl_space_underlying(map
->dim
, 0);
4049 isl_space_free(map
->dim
);
4050 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4054 return (struct isl_set
*)map
;
4060 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4062 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4065 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4066 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4068 bmap
= isl_basic_map_cow(bmap
);
4072 isl_space_free(bmap
->dim
);
4075 bmap
= isl_basic_map_finalize(bmap
);
4079 isl_basic_map_free(bmap
);
4080 isl_space_free(dim
);
4084 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4085 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4087 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4091 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4092 __isl_take isl_space
*dim
)
4096 map
= isl_map_cow(map
);
4100 for (i
= 0; i
< map
->n
; ++i
) {
4101 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4102 isl_space_copy(dim
));
4106 isl_space_free(map
->dim
);
4112 isl_space_free(dim
);
4116 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4117 __isl_take isl_space
*dim
)
4119 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4122 /* Compute the parameter domain of the given basic set.
4124 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4129 if (isl_basic_set_is_params(bset
))
4132 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4133 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4134 space
= isl_basic_set_get_space(bset
);
4135 space
= isl_space_params(space
);
4136 bset
= isl_basic_set_reset_space(bset
, space
);
4140 /* Compute the parameter domain of the given set.
4142 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4147 if (isl_set_is_params(set
))
4150 n
= isl_set_dim(set
, isl_dim_set
);
4151 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4152 space
= isl_set_get_space(set
);
4153 space
= isl_space_params(space
);
4154 set
= isl_set_reset_space(set
, space
);
4158 /* Construct a zero-dimensional set with the given parameter domain.
4160 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4163 space
= isl_set_get_space(set
);
4164 space
= isl_space_set_from_params(space
);
4165 set
= isl_set_reset_space(set
, space
);
4169 /* Compute the parameter domain of the given map.
4171 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4176 n
= isl_map_dim(map
, isl_dim_in
);
4177 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4178 n
= isl_map_dim(map
, isl_dim_out
);
4179 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4180 space
= isl_map_get_space(map
);
4181 space
= isl_space_params(space
);
4182 map
= isl_map_reset_space(map
, space
);
4186 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4189 struct isl_basic_set
*domain
;
4195 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4197 n_in
= isl_basic_map_n_in(bmap
);
4198 n_out
= isl_basic_map_n_out(bmap
);
4199 domain
= isl_basic_set_from_basic_map(bmap
);
4200 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4202 domain
= isl_basic_set_reset_space(domain
, dim
);
4207 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4211 return isl_space_may_be_set(bmap
->dim
);
4214 /* Is this basic map actually a set?
4215 * Users should never call this function. Outside of isl,
4216 * the type should indicate whether something is a set or a map.
4218 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4222 return isl_space_is_set(bmap
->dim
);
4225 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4229 if (isl_basic_map_is_set(bmap
))
4231 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4234 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4235 __isl_take isl_basic_map
*bmap
)
4239 isl_basic_map
*domain
;
4240 int nparam
, n_in
, n_out
;
4243 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4244 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4245 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4247 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4248 domain
= isl_basic_map_universe(dim
);
4250 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4251 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4252 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4254 total
= isl_basic_map_total_dim(bmap
);
4256 for (i
= 0; i
< n_in
; ++i
) {
4257 k
= isl_basic_map_alloc_equality(bmap
);
4260 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4261 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4262 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4265 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4266 return isl_basic_map_finalize(bmap
);
4268 isl_basic_map_free(bmap
);
4272 __isl_give isl_basic_map
*isl_basic_map_range_map(
4273 __isl_take isl_basic_map
*bmap
)
4277 isl_basic_map
*range
;
4278 int nparam
, n_in
, n_out
;
4281 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4282 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4283 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4285 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4286 range
= isl_basic_map_universe(dim
);
4288 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4289 bmap
= isl_basic_map_apply_range(bmap
, range
);
4290 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4292 total
= isl_basic_map_total_dim(bmap
);
4294 for (i
= 0; i
< n_out
; ++i
) {
4295 k
= isl_basic_map_alloc_equality(bmap
);
4298 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4299 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4300 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4303 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4304 return isl_basic_map_finalize(bmap
);
4306 isl_basic_map_free(bmap
);
4310 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4314 return isl_space_may_be_set(map
->dim
);
4317 /* Is this map actually a set?
4318 * Users should never call this function. Outside of isl,
4319 * the type should indicate whether something is a set or a map.
4321 int isl_map_is_set(__isl_keep isl_map
*map
)
4325 return isl_space_is_set(map
->dim
);
4328 struct isl_set
*isl_map_range(struct isl_map
*map
)
4331 struct isl_set
*set
;
4335 if (isl_map_is_set(map
))
4336 return (isl_set
*)map
;
4338 map
= isl_map_cow(map
);
4342 set
= (struct isl_set
*) map
;
4343 set
->dim
= isl_space_range(set
->dim
);
4346 for (i
= 0; i
< map
->n
; ++i
) {
4347 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4351 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4352 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4359 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4362 isl_space
*domain_dim
;
4364 map
= isl_map_cow(map
);
4368 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4369 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4370 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4373 for (i
= 0; i
< map
->n
; ++i
) {
4374 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4378 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4379 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4386 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4389 isl_space
*range_dim
;
4391 map
= isl_map_cow(map
);
4395 range_dim
= isl_space_range(isl_map_get_space(map
));
4396 range_dim
= isl_space_from_range(range_dim
);
4397 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4398 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4401 for (i
= 0; i
< map
->n
; ++i
) {
4402 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4406 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4407 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4414 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4415 __isl_take isl_space
*dim
)
4418 struct isl_map
*map
= NULL
;
4420 set
= isl_set_cow(set
);
4423 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4424 map
= (struct isl_map
*)set
;
4425 for (i
= 0; i
< set
->n
; ++i
) {
4426 map
->p
[i
] = isl_basic_map_from_basic_set(
4427 set
->p
[i
], isl_space_copy(dim
));
4431 isl_space_free(map
->dim
);
4435 isl_space_free(dim
);
4440 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4441 __isl_take isl_basic_set
*bset
)
4443 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4446 __isl_give isl_basic_map
*isl_basic_map_from_range(
4447 __isl_take isl_basic_set
*bset
)
4450 space
= isl_basic_set_get_space(bset
);
4451 space
= isl_space_from_range(space
);
4452 bset
= isl_basic_set_reset_space(bset
, space
);
4453 return (isl_basic_map
*)bset
;
4456 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4459 space
= isl_set_get_space(set
);
4460 space
= isl_space_from_range(space
);
4461 set
= isl_set_reset_space(set
, space
);
4462 return (struct isl_map
*)set
;
4465 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4467 return isl_map_reverse(isl_map_from_range(set
));
4470 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4471 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4473 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4476 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4477 __isl_take isl_set
*range
)
4479 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4482 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4485 struct isl_set
*set
= NULL
;
4489 map
= isl_map_cow(map
);
4492 map
->dim
= isl_space_as_set_space(map
->dim
);
4495 set
= (struct isl_set
*)map
;
4496 for (i
= 0; i
< map
->n
; ++i
) {
4497 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4507 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4510 struct isl_map
*map
;
4515 isl_die(dim
->ctx
, isl_error_internal
,
4516 "negative number of basic maps", goto error
);
4517 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4518 sizeof(struct isl_map
) +
4519 (n
- 1) * sizeof(struct isl_basic_map
*));
4523 map
->ctx
= dim
->ctx
;
4524 isl_ctx_ref(map
->ctx
);
4532 isl_space_free(dim
);
4536 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4537 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4540 struct isl_map
*map
;
4543 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
4547 map
= isl_map_alloc_space(dims
, n
, flags
);
4551 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
4553 struct isl_basic_map
*bmap
;
4554 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
4555 bmap
= isl_basic_map_set_to_empty(bmap
);
4559 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
4561 struct isl_basic_set
*bset
;
4562 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
4563 bset
= isl_basic_set_set_to_empty(bset
);
4567 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4569 struct isl_basic_map
*bmap
;
4572 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4573 bmap
= isl_basic_map_set_to_empty(bmap
);
4577 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4579 struct isl_basic_map
*bmap
;
4582 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4583 bmap
= isl_basic_map_set_to_empty(bmap
);
4587 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4589 struct isl_basic_set
*bset
;
4592 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
4593 bset
= isl_basic_set_set_to_empty(bset
);
4597 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
4599 struct isl_basic_map
*bmap
;
4600 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
4601 bmap
= isl_basic_map_finalize(bmap
);
4605 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
4607 struct isl_basic_set
*bset
;
4608 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
4609 bset
= isl_basic_set_finalize(bset
);
4613 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
4616 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
4617 isl_basic_map
*bmap
;
4619 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
4620 for (i
= 0; i
< total
; ++i
) {
4621 int k
= isl_basic_map_alloc_inequality(bmap
);
4624 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4625 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4629 isl_basic_map_free(bmap
);
4633 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
4635 return isl_basic_map_nat_universe(dim
);
4638 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
4640 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4643 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
4645 return isl_map_nat_universe(dim
);
4648 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4649 __isl_keep isl_basic_map
*model
)
4653 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4656 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4660 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4663 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4664 __isl_keep isl_set
*model
)
4668 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
4671 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
4673 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4676 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4680 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4683 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4687 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4690 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
4692 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
4695 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4699 return isl_set_empty(isl_space_copy(model
->dim
));
4702 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
4704 struct isl_map
*map
;
4707 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4708 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4712 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
4714 struct isl_set
*set
;
4717 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
4718 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4722 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4726 return isl_set_universe(isl_space_copy(model
->dim
));
4729 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4732 struct isl_map
*dup
;
4736 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
4737 for (i
= 0; i
< map
->n
; ++i
)
4738 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4742 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4743 __isl_take isl_basic_map
*bmap
)
4747 if (isl_basic_map_plain_is_empty(bmap
)) {
4748 isl_basic_map_free(bmap
);
4751 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
4752 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4753 map
->p
[map
->n
] = bmap
;
4755 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4761 isl_basic_map_free(bmap
);
4765 void isl_map_free(struct isl_map
*map
)
4775 isl_ctx_deref(map
->ctx
);
4776 for (i
= 0; i
< map
->n
; ++i
)
4777 isl_basic_map_free(map
->p
[i
]);
4778 isl_space_free(map
->dim
);
4782 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4783 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4787 base
= isl_map_cow(base
);
4791 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
4794 for (i
= 0; i
< base
->n
; ++i
) {
4795 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
4796 isl_space_copy(base
->dim
), 0, 0, 0);
4806 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4807 unsigned nparam
, unsigned dim
)
4809 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4813 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4814 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4818 bmap
= isl_basic_map_cow(bmap
);
4819 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4820 j
= isl_basic_map_alloc_equality(bmap
);
4823 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4824 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4825 isl_int_set_si(bmap
->eq
[j
][0], value
);
4826 bmap
= isl_basic_map_simplify(bmap
);
4827 return isl_basic_map_finalize(bmap
);
4829 isl_basic_map_free(bmap
);
4833 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4834 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4838 bmap
= isl_basic_map_cow(bmap
);
4839 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4840 j
= isl_basic_map_alloc_equality(bmap
);
4843 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4844 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4845 isl_int_set(bmap
->eq
[j
][0], value
);
4846 bmap
= isl_basic_map_simplify(bmap
);
4847 return isl_basic_map_finalize(bmap
);
4849 isl_basic_map_free(bmap
);
4853 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4854 enum isl_dim_type type
, unsigned pos
, int value
)
4858 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4859 return isl_basic_map_fix_pos_si(bmap
,
4860 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4862 isl_basic_map_free(bmap
);
4866 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4867 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4871 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4872 return isl_basic_map_fix_pos(bmap
,
4873 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4875 isl_basic_map_free(bmap
);
4879 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4880 enum isl_dim_type type
, unsigned pos
, int value
)
4882 return (struct isl_basic_set
*)
4883 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4887 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4888 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4890 return (struct isl_basic_set
*)
4891 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4895 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4896 unsigned input
, int value
)
4898 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4901 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4902 unsigned dim
, int value
)
4904 return (struct isl_basic_set
*)
4905 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4906 isl_dim_set
, dim
, value
);
4909 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4910 enum isl_dim_type type
, unsigned pos
, int value
)
4914 map
= isl_map_cow(map
);
4918 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4919 for (i
= 0; i
< map
->n
; ++i
) {
4920 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4924 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4931 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4932 enum isl_dim_type type
, unsigned pos
, int value
)
4934 return (struct isl_set
*)
4935 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4938 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4939 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4943 map
= isl_map_cow(map
);
4947 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4948 for (i
= 0; i
< map
->n
; ++i
) {
4949 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4953 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4960 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4961 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4963 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4966 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4967 unsigned input
, int value
)
4969 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4972 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4974 return (struct isl_set
*)
4975 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4978 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4979 __isl_take isl_basic_map
*bmap
,
4980 enum isl_dim_type type
, unsigned pos
, int value
)
4986 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4987 pos
+= isl_basic_map_offset(bmap
, type
);
4988 bmap
= isl_basic_map_cow(bmap
);
4989 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4990 j
= isl_basic_map_alloc_inequality(bmap
);
4993 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4994 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4995 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4996 bmap
= isl_basic_map_simplify(bmap
);
4997 return isl_basic_map_finalize(bmap
);
4999 isl_basic_map_free(bmap
);
5003 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5004 unsigned dim
, isl_int value
)
5008 bset
= isl_basic_set_cow(bset
);
5009 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5010 j
= isl_basic_set_alloc_inequality(bset
);
5013 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5014 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5015 isl_int_neg(bset
->ineq
[j
][0], value
);
5016 bset
= isl_basic_set_simplify(bset
);
5017 return isl_basic_set_finalize(bset
);
5019 isl_basic_set_free(bset
);
5023 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5024 enum isl_dim_type type
, unsigned pos
, int value
)
5028 map
= isl_map_cow(map
);
5032 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5033 for (i
= 0; i
< map
->n
; ++i
) {
5034 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
5039 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5046 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5047 enum isl_dim_type type
, unsigned pos
, int value
)
5049 return (struct isl_set
*)
5050 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5053 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5058 set
= isl_set_cow(set
);
5062 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5063 for (i
= 0; i
< set
->n
; ++i
) {
5064 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5074 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5078 map
= isl_map_cow(map
);
5082 map
->dim
= isl_space_reverse(map
->dim
);
5085 for (i
= 0; i
< map
->n
; ++i
) {
5086 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5090 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5097 static struct isl_map
*isl_basic_map_partial_lexopt(
5098 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5099 struct isl_set
**empty
, int max
)
5103 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5104 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5106 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5108 isl_basic_map_free(bmap
);
5109 isl_basic_set_free(dom
);
5115 struct isl_map
*isl_basic_map_partial_lexmax(
5116 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5117 struct isl_set
**empty
)
5119 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5122 struct isl_map
*isl_basic_map_partial_lexmin(
5123 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5124 struct isl_set
**empty
)
5126 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5129 struct isl_set
*isl_basic_set_partial_lexmin(
5130 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5131 struct isl_set
**empty
)
5133 return (struct isl_set
*)
5134 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5138 struct isl_set
*isl_basic_set_partial_lexmax(
5139 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5140 struct isl_set
**empty
)
5142 return (struct isl_set
*)
5143 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5147 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5148 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5149 __isl_give isl_set
**empty
)
5151 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5154 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5155 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5156 __isl_give isl_set
**empty
)
5158 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5161 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5162 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5163 __isl_give isl_set
**empty
)
5165 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5168 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5169 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5170 __isl_give isl_set
**empty
)
5172 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5175 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5176 __isl_take isl_basic_map
*bmap
, int max
)
5178 isl_basic_set
*dom
= NULL
;
5179 isl_space
*dom_space
;
5183 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5184 dom
= isl_basic_set_universe(dom_space
);
5185 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5187 isl_basic_map_free(bmap
);
5191 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5192 __isl_take isl_basic_map
*bmap
)
5194 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5197 /* Given a basic map "bmap", compute the lexicographically minimal
5198 * (or maximal) image element for each domain element in dom.
5199 * Set *empty to those elements in dom that do not have an image element.
5201 * We first make sure the basic sets in dom are disjoint and then
5202 * simply collect the results over each of the basic sets separately.
5203 * We could probably improve the efficiency a bit by moving the union
5204 * domain down into the parametric integer programming.
5206 static __isl_give isl_map
*basic_map_partial_lexopt(
5207 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
5208 __isl_give isl_set
**empty
, int max
)
5211 struct isl_map
*res
;
5213 dom
= isl_set_make_disjoint(dom
);
5217 if (isl_set_plain_is_empty(dom
)) {
5218 res
= isl_map_empty_like_basic_map(bmap
);
5219 *empty
= isl_set_empty_like(dom
);
5221 isl_basic_map_free(bmap
);
5225 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5226 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
5228 for (i
= 1; i
< dom
->n
; ++i
) {
5229 struct isl_map
*res_i
;
5230 struct isl_set
*empty_i
;
5232 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
5233 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
5235 res
= isl_map_union_disjoint(res
, res_i
);
5236 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
5240 isl_basic_map_free(bmap
);
5245 isl_basic_map_free(bmap
);
5249 /* Given a map "map", compute the lexicographically minimal
5250 * (or maximal) image element for each domain element in dom.
5251 * Set *empty to those elements in dom that do not have an image element.
5253 * We first compute the lexicographically minimal or maximal element
5254 * in the first basic map. This results in a partial solution "res"
5255 * and a subset "todo" of dom that still need to be handled.
5256 * We then consider each of the remaining maps in "map" and successively
5257 * improve both "res" and "todo".
5259 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5260 * Assume we are computing the lexicographical maximum.
5261 * We first compute the lexicographically maximal element in basic map i.
5262 * This results in a partial solution res_i and a subset todo_i.
5263 * Then we combine these results with those obtain for the first k basic maps
5264 * to obtain a result that is valid for the first k+1 basic maps.
5265 * In particular, the set where there is no solution is the set where
5266 * there is no solution for the first k basic maps and also no solution
5267 * for the ith basic map, i.e.,
5269 * todo^i = todo^k * todo_i
5271 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5272 * solutions, arbitrarily breaking ties in favor of res^k.
5273 * That is, when res^k(a) >= res_i(a), we pick res^k and
5274 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5275 * the lexicographic order.)
5276 * In practice, we compute
5278 * res^k * (res_i . "<=")
5282 * res_i * (res^k . "<")
5284 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5285 * where only one of res^k and res_i provides a solution and we simply pick
5292 * Note that we only compute these intersections when dom(res^k) intersects
5293 * dom(res_i). Otherwise, the only effect of these intersections is to
5294 * potentially break up res^k and res_i into smaller pieces.
5295 * We want to avoid such splintering as much as possible.
5296 * In fact, an earlier implementation of this function would look for
5297 * better results in the domain of res^k and for extra results in todo^k,
5298 * but this would always result in a splintering according to todo^k,
5299 * even when the domain of basic map i is disjoint from the domains of
5300 * the previous basic maps.
5302 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
5303 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5304 __isl_give isl_set
**empty
, int max
)
5307 struct isl_map
*res
;
5308 struct isl_set
*todo
;
5313 if (isl_map_plain_is_empty(map
)) {
5321 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
5322 isl_set_copy(dom
), &todo
, max
);
5324 for (i
= 1; i
< map
->n
; ++i
) {
5328 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
5330 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
5331 isl_set_copy(dom
), &todo_i
, max
);
5334 lt
= isl_map_lex_lt(isl_space_copy(dim
));
5335 le
= isl_map_lex_le(dim
);
5337 lt
= isl_map_lex_gt(isl_space_copy(dim
));
5338 le
= isl_map_lex_ge(dim
);
5340 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
5341 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
5342 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
5343 le
= isl_map_intersect(le
, isl_map_copy(res
));
5345 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
5346 res
= isl_map_intersect_domain(res
,
5347 isl_set_copy(todo_i
));
5348 res_i
= isl_map_intersect_domain(res_i
,
5349 isl_set_copy(todo
));
5352 res
= isl_map_union_disjoint(res
, res_i
);
5353 res
= isl_map_union_disjoint(res
, lt
);
5354 res
= isl_map_union_disjoint(res
, le
);
5356 todo
= isl_set_intersect(todo
, todo_i
);
5376 /* Given a map "map", compute the lexicographically minimal
5377 * (or maximal) image element for each domain element in dom.
5378 * Set *empty to those elements in dom that do not have an image element.
5380 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5382 static __isl_give isl_map
*isl_map_partial_lexopt(
5383 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5384 __isl_give isl_set
**empty
, int max
)
5388 if (isl_space_match(map
->dim
, isl_dim_param
, dom
->dim
, isl_dim_param
))
5389 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5390 if (!isl_space_has_named_params(map
->dim
) ||
5391 !isl_space_has_named_params(dom
->dim
))
5392 isl_die(map
->ctx
, isl_error_invalid
,
5393 "unaligned unnamed parameters", goto error
);
5394 map
= isl_map_align_params(map
, isl_map_get_space(dom
));
5395 dom
= isl_map_align_params(dom
, isl_map_get_space(map
));
5396 return isl_map_partial_lexopt_aligned(map
, dom
, empty
, max
);
5405 __isl_give isl_map
*isl_map_partial_lexmax(
5406 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5407 __isl_give isl_set
**empty
)
5409 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
5412 __isl_give isl_map
*isl_map_partial_lexmin(
5413 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5414 __isl_give isl_set
**empty
)
5416 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
5419 __isl_give isl_set
*isl_set_partial_lexmin(
5420 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5421 __isl_give isl_set
**empty
)
5423 return (struct isl_set
*)
5424 isl_map_partial_lexmin((struct isl_map
*)set
,
5428 __isl_give isl_set
*isl_set_partial_lexmax(
5429 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
5430 __isl_give isl_set
**empty
)
5432 return (struct isl_set
*)
5433 isl_map_partial_lexmax((struct isl_map
*)set
,
5437 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
5439 struct isl_basic_set
*dom
= NULL
;
5444 dom_dim
= isl_space_domain(isl_space_copy(bmap
->dim
));
5445 dom
= isl_basic_set_universe(dom_dim
);
5446 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
5448 isl_basic_map_free(bmap
);
5452 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
5454 return isl_basic_map_lexopt(bmap
, 0);
5457 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
5459 return isl_basic_map_lexopt(bmap
, 1);
5462 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5464 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5467 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5469 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5472 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5474 struct isl_set
*dom
= NULL
;
5479 dom_dim
= isl_space_domain(isl_space_copy(map
->dim
));
5480 dom
= isl_set_universe(dom_dim
);
5481 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5487 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5489 return isl_map_lexopt(map
, 0);
5492 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5494 return isl_map_lexopt(map
, 1);
5497 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5499 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5502 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5504 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5507 /* Extract the first and only affine expression from list
5508 * and then add it to *pwaff with the given dom.
5509 * This domain is known to be disjoint from other domains
5510 * because of the way isl_basic_map_foreach_lexmax works.
5512 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
5513 __isl_take isl_aff_list
*list
, void *user
)
5515 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5517 isl_pw_aff
**pwaff
= user
;
5518 isl_pw_aff
*pwaff_i
;
5520 if (isl_aff_list_n_aff(list
) != 1)
5521 isl_die(ctx
, isl_error_internal
,
5522 "expecting single element list", goto error
);
5524 aff
= isl_aff_list_get_aff(list
, 0);
5525 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5527 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5529 isl_aff_list_free(list
);
5533 isl_basic_set_free(dom
);
5534 isl_aff_list_free(list
);
5538 /* Given a basic map with one output dimension, compute the minimum or
5539 * maximum of that dimension as an isl_pw_aff.
5541 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5542 * call update_dim_opt on each leaf of the result.
5544 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
5547 isl_space
*dim
= isl_basic_map_get_space(bmap
);
5551 dim
= isl_space_from_domain(isl_space_domain(dim
));
5552 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
5553 pwaff
= isl_pw_aff_empty(dim
);
5555 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
5557 return isl_pw_aff_free(pwaff
);
5562 /* Compute the minimum or maximum of the given output dimension
5563 * as a function of the parameters and the input dimensions,
5564 * but independently of the other output dimensions.
5566 * We first project out the other output dimension and then compute
5567 * the "lexicographic" maximum in each basic map, combining the results
5568 * using isl_pw_aff_union_max.
5570 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
5577 n_out
= isl_map_dim(map
, isl_dim_out
);
5578 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
5579 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
5584 isl_space
*dim
= isl_map_get_space(map
);
5585 dim
= isl_space_domain(isl_space_from_range(dim
));
5587 return isl_pw_aff_empty(dim
);
5590 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
5591 for (i
= 1; i
< map
->n
; ++i
) {
5592 isl_pw_aff
*pwaff_i
;
5594 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
5595 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
5603 /* Compute the maximum of the given output dimension as a function of the
5604 * parameters and input dimensions, but independently of
5605 * the other output dimensions.
5607 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
5609 return map_dim_opt(map
, pos
, 1);
5612 /* Compute the minimum or maximum of the given set dimension
5613 * as a function of the parameters,
5614 * but independently of the other set dimensions.
5616 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
5619 return map_dim_opt(set
, pos
, max
);
5622 /* Compute the maximum of the given set dimension as a function of the
5623 * parameters, but independently of the other set dimensions.
5625 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5627 return set_dim_opt(set
, pos
, 1);
5630 /* Compute the minimum of the given set dimension as a function of the
5631 * parameters, but independently of the other set dimensions.
5633 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
5635 return set_dim_opt(set
, pos
, 0);
5638 /* Apply a preimage specified by "mat" on the parameters of "bset".
5639 * bset is assumed to have only parameters and divs.
5641 static struct isl_basic_set
*basic_set_parameter_preimage(
5642 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5649 bset
->dim
= isl_space_cow(bset
->dim
);
5653 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5655 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5657 bset
->dim
->nparam
= 0;
5658 bset
->dim
->n_out
= nparam
;
5659 bset
= isl_basic_set_preimage(bset
, mat
);
5661 bset
->dim
->nparam
= bset
->dim
->n_out
;
5662 bset
->dim
->n_out
= 0;
5667 isl_basic_set_free(bset
);
5671 /* Apply a preimage specified by "mat" on the parameters of "set".
5672 * set is assumed to have only parameters and divs.
5674 static struct isl_set
*set_parameter_preimage(
5675 struct isl_set
*set
, struct isl_mat
*mat
)
5677 isl_space
*dim
= NULL
;
5683 dim
= isl_space_copy(set
->dim
);
5684 dim
= isl_space_cow(dim
);
5688 nparam
= isl_set_dim(set
, isl_dim_param
);
5690 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5693 dim
->n_out
= nparam
;
5694 isl_set_reset_space(set
, dim
);
5695 set
= isl_set_preimage(set
, mat
);
5698 dim
= isl_space_copy(set
->dim
);
5699 dim
= isl_space_cow(dim
);
5702 dim
->nparam
= dim
->n_out
;
5704 isl_set_reset_space(set
, dim
);
5707 isl_space_free(dim
);
5714 /* Intersect the basic set "bset" with the affine space specified by the
5715 * equalities in "eq".
5717 static struct isl_basic_set
*basic_set_append_equalities(
5718 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5726 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
5731 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
5732 for (i
= 0; i
< eq
->n_row
; ++i
) {
5733 k
= isl_basic_set_alloc_equality(bset
);
5736 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5737 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5741 bset
= isl_basic_set_gauss(bset
, NULL
);
5742 bset
= isl_basic_set_finalize(bset
);
5747 isl_basic_set_free(bset
);
5751 /* Intersect the set "set" with the affine space specified by the
5752 * equalities in "eq".
5754 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5762 for (i
= 0; i
< set
->n
; ++i
) {
5763 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5776 /* Project the given basic set onto its parameter domain, possibly introducing
5777 * new, explicit, existential variables in the constraints.
5778 * The input has parameters and (possibly implicit) existential variables.
5779 * The output has the same parameters, but only
5780 * explicit existentially quantified variables.
5782 * The actual projection is performed by pip, but pip doesn't seem
5783 * to like equalities very much, so we first remove the equalities
5784 * among the parameters by performing a variable compression on
5785 * the parameters. Afterward, an inverse transformation is performed
5786 * and the equalities among the parameters are inserted back in.
5788 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5792 struct isl_mat
*T
, *T2
;
5793 struct isl_set
*set
;
5794 unsigned nparam
, n_div
;
5796 bset
= isl_basic_set_cow(bset
);
5800 if (bset
->n_eq
== 0)
5801 return isl_basic_set_lexmin(bset
);
5803 isl_basic_set_gauss(bset
, NULL
);
5805 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5806 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5808 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5809 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5812 if (i
== bset
->n_eq
)
5813 return isl_basic_set_lexmin(bset
);
5815 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5817 eq
= isl_mat_cow(eq
);
5818 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5819 if (T
&& T
->n_col
== 0) {
5823 bset
= isl_basic_set_set_to_empty(bset
);
5824 return isl_set_from_basic_set(bset
);
5826 bset
= basic_set_parameter_preimage(bset
, T
);
5828 set
= isl_basic_set_lexmin(bset
);
5829 set
= set_parameter_preimage(set
, T2
);
5830 set
= set_append_equalities(set
, eq
);
5834 /* Compute an explicit representation for all the existentially
5835 * quantified variables.
5836 * The input and output dimensions are first turned into parameters.
5837 * compute_divs then returns a map with the same parameters and
5838 * no input or output dimensions and the dimension specification
5839 * is reset to that of the input.
5841 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5843 struct isl_basic_set
*bset
;
5844 struct isl_set
*set
;
5845 struct isl_map
*map
;
5846 isl_space
*dim
, *orig_dim
= NULL
;
5851 bmap
= isl_basic_map_cow(bmap
);
5855 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5856 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5857 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5858 dim
= isl_space_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5862 orig_dim
= bmap
->dim
;
5864 bset
= (struct isl_basic_set
*)bmap
;
5866 set
= parameter_compute_divs(bset
);
5867 map
= (struct isl_map
*)set
;
5868 map
= isl_map_reset_space(map
, orig_dim
);
5872 isl_basic_map_free(bmap
);
5876 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5884 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
5885 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5886 if (isl_int_is_zero(bmap
->div
[i
][0]))
5888 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5894 static int map_divs_known(__isl_keep isl_map
*map
)
5901 for (i
= 0; i
< map
->n
; ++i
) {
5902 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5910 /* If bmap contains any unknown divs, then compute explicit
5911 * expressions for them. However, this computation may be
5912 * quite expensive, so first try to remove divs that aren't
5915 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5918 struct isl_map
*map
;
5920 known
= isl_basic_map_divs_known(bmap
);
5924 return isl_map_from_basic_map(bmap
);
5926 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5928 known
= isl_basic_map_divs_known(bmap
);
5932 return isl_map_from_basic_map(bmap
);
5934 map
= compute_divs(bmap
);
5937 isl_basic_map_free(bmap
);
5941 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5945 struct isl_map
*res
;
5952 known
= map_divs_known(map
);
5960 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5961 for (i
= 1 ; i
< map
->n
; ++i
) {
5963 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5964 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5965 res
= isl_map_union_disjoint(res
, r2
);
5967 res
= isl_map_union(res
, r2
);
5974 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5976 return (struct isl_set
*)
5977 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5980 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5982 return (struct isl_set
*)
5983 isl_map_compute_divs((struct isl_map
*)set
);
5986 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5989 struct isl_set
*set
;
5994 map
= isl_map_cow(map
);
5998 set
= (struct isl_set
*)map
;
5999 set
->dim
= isl_space_domain(set
->dim
);
6002 for (i
= 0; i
< map
->n
; ++i
) {
6003 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6007 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6008 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6015 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6016 __isl_take isl_map
*map2
)
6020 struct isl_map
*map
= NULL
;
6034 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
6036 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6037 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6038 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6040 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
6041 map1
->n
+ map2
->n
, flags
);
6044 for (i
= 0; i
< map1
->n
; ++i
) {
6045 map
= isl_map_add_basic_map(map
,
6046 isl_basic_map_copy(map1
->p
[i
]));
6050 for (i
= 0; i
< map2
->n
; ++i
) {
6051 map
= isl_map_add_basic_map(map
,
6052 isl_basic_map_copy(map2
->p
[i
]));
6066 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
6067 __isl_take isl_map
*map2
)
6069 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
6072 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
6074 map1
= isl_map_union_disjoint(map1
, map2
);
6078 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
6082 struct isl_set
*isl_set_union_disjoint(
6083 struct isl_set
*set1
, struct isl_set
*set2
)
6085 return (struct isl_set
*)
6086 isl_map_union_disjoint(
6087 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
6090 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
6092 return (struct isl_set
*)
6093 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6096 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
6097 __isl_take isl_set
*set
)
6100 struct isl_map
*result
;
6106 if (!isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
6107 isl_die(set
->ctx
, isl_error_invalid
,
6108 "parameters don't match", goto error
);
6110 if (isl_space_dim(set
->dim
, isl_dim_set
) != 0 &&
6111 !isl_map_compatible_range(map
, set
))
6112 isl_die(set
->ctx
, isl_error_invalid
,
6113 "incompatible spaces", goto error
);
6115 if (isl_set_plain_is_universe(set
)) {
6120 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
6121 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
6122 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6124 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
6125 map
->n
* set
->n
, flags
);
6128 for (i
= 0; i
< map
->n
; ++i
)
6129 for (j
= 0; j
< set
->n
; ++j
) {
6130 result
= isl_map_add_basic_map(result
,
6131 isl_basic_map_intersect_range(
6132 isl_basic_map_copy(map
->p
[i
]),
6133 isl_basic_set_copy(set
->p
[j
])));
6146 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
6147 __isl_take isl_set
*set
)
6149 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
6152 struct isl_map
*isl_map_intersect_domain(
6153 struct isl_map
*map
, struct isl_set
*set
)
6155 return isl_map_reverse(
6156 isl_map_intersect_range(isl_map_reverse(map
), set
));
6159 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
6160 __isl_take isl_map
*map2
)
6164 map1
= isl_map_reverse(map1
);
6165 map1
= isl_map_apply_range(map1
, map2
);
6166 return isl_map_reverse(map1
);
6173 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
6174 __isl_take isl_map
*map2
)
6176 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
6179 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
6180 __isl_take isl_map
*map2
)
6182 isl_space
*dim_result
;
6183 struct isl_map
*result
;
6189 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
6190 isl_space_copy(map2
->dim
));
6192 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
6195 for (i
= 0; i
< map1
->n
; ++i
)
6196 for (j
= 0; j
< map2
->n
; ++j
) {
6197 result
= isl_map_add_basic_map(result
,
6198 isl_basic_map_apply_range(
6199 isl_basic_map_copy(map1
->p
[i
]),
6200 isl_basic_map_copy(map2
->p
[j
])));
6206 if (result
&& result
->n
<= 1)
6207 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
6215 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
6216 __isl_take isl_map
*map2
)
6218 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
6222 * returns range - domain
6224 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
6226 isl_space
*dims
, *target_dim
;
6227 struct isl_basic_set
*bset
;
6234 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
6235 bmap
->dim
, isl_dim_out
),
6237 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
6238 dim
= isl_basic_map_n_in(bmap
);
6239 nparam
= isl_basic_map_n_param(bmap
);
6240 bset
= isl_basic_set_from_basic_map(bmap
);
6241 bset
= isl_basic_set_cow(bset
);
6242 dims
= isl_basic_set_get_space(bset
);
6243 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
6244 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
6245 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
6246 for (i
= 0; i
< dim
; ++i
) {
6247 int j
= isl_basic_map_alloc_equality(
6248 (struct isl_basic_map
*)bset
);
6251 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
6252 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
6253 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
6254 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
6256 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
6257 bset
= isl_basic_set_reset_space(bset
, target_dim
);
6260 isl_basic_map_free(bmap
);
6265 * returns range - domain
6267 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
6271 struct isl_set
*result
;
6276 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
6277 map
->dim
, isl_dim_out
),
6279 dim
= isl_map_get_space(map
);
6280 dim
= isl_space_domain(dim
);
6281 result
= isl_set_alloc_space(dim
, map
->n
, 0);
6284 for (i
= 0; i
< map
->n
; ++i
)
6285 result
= isl_set_add_basic_set(result
,
6286 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
6295 * returns [domain -> range] -> range - domain
6297 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
6298 __isl_take isl_basic_map
*bmap
)
6302 isl_basic_map
*domain
;
6306 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
6307 isl_die(bmap
->ctx
, isl_error_invalid
,
6308 "domain and range don't match", goto error
);
6310 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6311 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
6313 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
6314 domain
= isl_basic_map_universe(dim
);
6316 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6317 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6318 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
6320 total
= isl_basic_map_total_dim(bmap
);
6322 for (i
= 0; i
< n
; ++i
) {
6323 k
= isl_basic_map_alloc_equality(bmap
);
6326 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
6327 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
6328 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
6329 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
6332 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6333 return isl_basic_map_finalize(bmap
);
6335 isl_basic_map_free(bmap
);
6340 * returns [domain -> range] -> range - domain
6342 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
6345 isl_space
*domain_dim
;
6350 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
6351 isl_die(map
->ctx
, isl_error_invalid
,
6352 "domain and range don't match", goto error
);
6354 map
= isl_map_cow(map
);
6358 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
6359 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
6360 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
6363 for (i
= 0; i
< map
->n
; ++i
) {
6364 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
6368 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6375 __isl_give
struct isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
6377 struct isl_basic_map
*bmap
;
6385 nparam
= dims
->nparam
;
6387 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
6391 for (i
= 0; i
< dim
; ++i
) {
6392 int j
= isl_basic_map_alloc_equality(bmap
);
6395 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6396 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
6397 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
6399 return isl_basic_map_finalize(bmap
);
6401 isl_basic_map_free(bmap
);
6405 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
6409 if (dim
->n_in
!= dim
->n_out
)
6410 isl_die(dim
->ctx
, isl_error_invalid
,
6411 "number of input and output dimensions needs to be "
6412 "the same", goto error
);
6413 return basic_map_identity(dim
);
6415 isl_space_free(dim
);
6419 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
6421 if (!model
|| !model
->dim
)
6423 return isl_basic_map_identity(isl_space_copy(model
->dim
));
6426 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
6428 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
6431 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
6433 if (!model
|| !model
->dim
)
6435 return isl_map_identity(isl_space_copy(model
->dim
));
6438 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
6440 if (!model
|| !model
->dim
)
6442 return isl_map_identity(isl_space_copy(model
->dim
));
6445 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
6447 isl_space
*dim
= isl_set_get_space(set
);
6449 id
= isl_map_identity(isl_space_map_from_set(dim
));
6450 return isl_map_intersect_range(id
, set
);
6453 /* Construct a basic set with all set dimensions having only non-negative
6456 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
6457 __isl_take isl_space
*space
)
6462 struct isl_basic_set
*bset
;
6466 nparam
= space
->nparam
;
6468 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
6471 for (i
= 0; i
< dim
; ++i
) {
6472 int k
= isl_basic_set_alloc_inequality(bset
);
6475 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
6476 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
6480 isl_basic_set_free(bset
);
6484 /* Construct the half-space x_pos >= 0.
6486 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
6490 isl_basic_set
*nonneg
;
6492 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6493 k
= isl_basic_set_alloc_inequality(nonneg
);
6496 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
6497 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
6499 return isl_basic_set_finalize(nonneg
);
6501 isl_basic_set_free(nonneg
);
6505 /* Construct the half-space x_pos <= -1.
6507 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
6512 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
6513 k
= isl_basic_set_alloc_inequality(neg
);
6516 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6517 isl_int_set_si(neg
->ineq
[k
][0], -1);
6518 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6520 return isl_basic_set_finalize(neg
);
6522 isl_basic_set_free(neg
);
6526 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6527 enum isl_dim_type type
, unsigned first
, unsigned n
)
6530 isl_basic_set
*nonneg
;
6538 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6540 for (i
= 0; i
< n
; ++i
) {
6541 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
6542 pos(set
->dim
, type
) + first
+ i
);
6543 neg
= neg_halfspace(isl_set_get_space(set
),
6544 pos(set
->dim
, type
) + first
+ i
);
6546 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6555 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6556 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6563 if (isl_set_plain_is_empty(set
)) {
6568 return fn(set
, signs
, user
);
6571 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
6573 half
= isl_set_intersect(half
, isl_set_copy(set
));
6574 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6578 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
6580 half
= isl_set_intersect(half
, set
);
6581 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6587 /* Call "fn" on the intersections of "set" with each of the orthants
6588 * (except for obviously empty intersections). The orthant is identified
6589 * by the signs array, with each entry having value 1 or -1 according
6590 * to the sign of the corresponding variable.
6592 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6593 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6603 if (isl_set_plain_is_empty(set
))
6606 nparam
= isl_set_dim(set
, isl_dim_param
);
6607 nvar
= isl_set_dim(set
, isl_dim_set
);
6609 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6611 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6619 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6621 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6624 int isl_basic_map_is_subset(
6625 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6628 struct isl_map
*map1
;
6629 struct isl_map
*map2
;
6631 if (!bmap1
|| !bmap2
)
6634 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6635 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6637 is_subset
= isl_map_is_subset(map1
, map2
);
6645 int isl_basic_map_is_equal(
6646 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6650 if (!bmap1
|| !bmap2
)
6652 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6655 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6659 int isl_basic_set_is_equal(
6660 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6662 return isl_basic_map_is_equal(
6663 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6666 int isl_map_is_empty(struct isl_map
*map
)
6673 for (i
= 0; i
< map
->n
; ++i
) {
6674 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6683 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6685 return map
? map
->n
== 0 : -1;
6688 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6690 return isl_map_plain_is_empty(map
);
6693 int isl_set_plain_is_empty(struct isl_set
*set
)
6695 return set
? set
->n
== 0 : -1;
6698 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6700 return isl_set_plain_is_empty(set
);
6703 int isl_set_is_empty(struct isl_set
*set
)
6705 return isl_map_is_empty((struct isl_map
*)set
);
6708 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6713 return isl_space_is_equal(map1
->dim
, map2
->dim
);
6716 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6721 return isl_space_is_equal(set1
->dim
, set2
->dim
);
6724 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6730 is_subset
= isl_map_is_subset(map1
, map2
);
6733 is_subset
= isl_map_is_subset(map2
, map1
);
6737 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6739 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
6742 int isl_basic_map_is_strict_subset(
6743 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6747 if (!bmap1
|| !bmap2
)
6749 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6752 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6753 if (is_subset
== -1)
6758 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6764 is_subset
= isl_map_is_subset(map1
, map2
);
6767 is_subset
= isl_map_is_subset(map2
, map1
);
6768 if (is_subset
== -1)
6773 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6775 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6778 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6782 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6785 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6789 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6792 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6799 for (i
= 0; i
< map
->n
; ++i
) {
6800 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6808 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6810 return isl_map_plain_is_universe((isl_map
*) set
);
6813 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6815 return isl_set_plain_is_universe(set
);
6818 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6820 struct isl_basic_set
*bset
= NULL
;
6821 struct isl_vec
*sample
= NULL
;
6828 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6831 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6832 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6833 copy
= isl_basic_map_remove_redundancies(copy
);
6834 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6835 isl_basic_map_free(copy
);
6839 total
= 1 + isl_basic_map_total_dim(bmap
);
6840 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6841 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6847 isl_vec_free(bmap
->sample
);
6848 bmap
->sample
= NULL
;
6849 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6852 sample
= isl_basic_set_sample_vec(bset
);
6855 empty
= sample
->size
== 0;
6856 isl_vec_free(bmap
->sample
);
6857 bmap
->sample
= sample
;
6859 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6864 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6868 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6871 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6873 return isl_basic_map_plain_is_empty(bmap
);
6876 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6880 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6883 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6885 return isl_basic_set_plain_is_empty(bset
);
6888 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6890 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6893 struct isl_map
*isl_basic_map_union(
6894 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6896 struct isl_map
*map
;
6897 if (!bmap1
|| !bmap2
)
6900 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6902 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
6905 map
= isl_map_add_basic_map(map
, bmap1
);
6906 map
= isl_map_add_basic_map(map
, bmap2
);
6909 isl_basic_map_free(bmap1
);
6910 isl_basic_map_free(bmap2
);
6914 struct isl_set
*isl_basic_set_union(
6915 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6917 return (struct isl_set
*)isl_basic_map_union(
6918 (struct isl_basic_map
*)bset1
,
6919 (struct isl_basic_map
*)bset2
);
6922 /* Order divs such that any div only depends on previous divs */
6923 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6931 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6933 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6935 if (isl_int_is_zero(bmap
->div
[i
][0]))
6937 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6941 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6947 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6949 return (struct isl_basic_set
*)
6950 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6953 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6960 for (i
= 0; i
< map
->n
; ++i
) {
6961 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6972 /* Apply the expansion computed by isl_merge_divs.
6973 * The expansion itself is given by "exp" while the resulting
6974 * list of divs is given by "div".
6976 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6977 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6982 bset
= isl_basic_set_cow(bset
);
6986 if (div
->n_row
< bset
->n_div
)
6987 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6988 "not an expansion", goto error
);
6990 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
6991 div
->n_row
- bset
->n_div
, 0,
6992 2 * (div
->n_row
- bset
->n_div
));
6994 n_div
= bset
->n_div
;
6995 for (i
= n_div
; i
< div
->n_row
; ++i
)
6996 if (isl_basic_set_alloc_div(bset
) < 0)
7000 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
7001 if (j
>= 0 && exp
[j
] == i
) {
7003 isl_basic_map_swap_div(bset
, i
, j
);
7006 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
7007 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
7015 isl_basic_set_free(bset
);
7020 /* Look for a div in dst that corresponds to the div "div" in src.
7021 * The divs before "div" in src and dst are assumed to be the same.
7023 * Returns -1 if no corresponding div was found and the position
7024 * of the corresponding div in dst otherwise.
7026 static int find_div(struct isl_basic_map
*dst
,
7027 struct isl_basic_map
*src
, unsigned div
)
7031 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7033 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
7034 for (i
= div
; i
< dst
->n_div
; ++i
)
7035 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
7036 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
7037 dst
->n_div
- div
) == -1)
7042 struct isl_basic_map
*isl_basic_map_align_divs(
7043 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
7046 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
7051 if (src
->n_div
== 0)
7054 for (i
= 0; i
< src
->n_div
; ++i
)
7055 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
7057 src
= isl_basic_map_order_divs(src
);
7058 dst
= isl_basic_map_cow(dst
);
7059 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
7060 src
->n_div
, 0, 2 * src
->n_div
);
7063 for (i
= 0; i
< src
->n_div
; ++i
) {
7064 int j
= find_div(dst
, src
, i
);
7066 j
= isl_basic_map_alloc_div(dst
);
7069 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
7070 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
7071 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
7075 isl_basic_map_swap_div(dst
, i
, j
);
7079 isl_basic_map_free(dst
);
7083 struct isl_basic_set
*isl_basic_set_align_divs(
7084 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
7086 return (struct isl_basic_set
*)isl_basic_map_align_divs(
7087 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
7090 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
7098 map
= isl_map_compute_divs(map
);
7099 map
= isl_map_cow(map
);
7103 for (i
= 1; i
< map
->n
; ++i
)
7104 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
7105 for (i
= 1; i
< map
->n
; ++i
)
7106 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
7108 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7112 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
7114 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
7117 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
7118 __isl_take isl_map
*map
)
7122 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
7123 map
= isl_map_intersect_domain(map
, set
);
7124 set
= isl_map_range(map
);
7132 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
7133 __isl_take isl_map
*map
)
7135 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
7138 /* There is no need to cow as removing empty parts doesn't change
7139 * the meaning of the set.
7141 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
7148 for (i
= map
->n
-1; i
>= 0; --i
) {
7149 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
7151 isl_basic_map_free(map
->p
[i
]);
7152 if (i
!= map
->n
-1) {
7153 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7154 map
->p
[i
] = map
->p
[map
->n
-1];
7162 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
7164 return (struct isl_set
*)
7165 isl_map_remove_empty_parts((struct isl_map
*)set
);
7168 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
7170 struct isl_basic_map
*bmap
;
7171 if (!map
|| map
->n
== 0)
7173 bmap
= map
->p
[map
->n
-1];
7174 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
7175 return isl_basic_map_copy(bmap
);
7178 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
7180 return (struct isl_basic_set
*)
7181 isl_map_copy_basic_map((struct isl_map
*)set
);
7184 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
7185 __isl_keep isl_basic_map
*bmap
)
7191 for (i
= map
->n
-1; i
>= 0; --i
) {
7192 if (map
->p
[i
] != bmap
)
7194 map
= isl_map_cow(map
);
7197 isl_basic_map_free(map
->p
[i
]);
7198 if (i
!= map
->n
-1) {
7199 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
7200 map
->p
[i
] = map
->p
[map
->n
-1];
7211 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
7212 struct isl_basic_set
*bset
)
7214 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
7215 (struct isl_basic_map
*)bset
);
7218 /* Given two basic sets bset1 and bset2, compute the maximal difference
7219 * between the values of dimension pos in bset1 and those in bset2
7220 * for any common value of the parameters and dimensions preceding pos.
7222 static enum isl_lp_result
basic_set_maximal_difference_at(
7223 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
7224 int pos
, isl_int
*opt
)
7227 struct isl_basic_map
*bmap1
= NULL
;
7228 struct isl_basic_map
*bmap2
= NULL
;
7229 struct isl_ctx
*ctx
;
7230 struct isl_vec
*obj
;
7233 unsigned dim1
, dim2
;
7234 enum isl_lp_result res
;
7236 if (!bset1
|| !bset2
)
7237 return isl_lp_error
;
7239 nparam
= isl_basic_set_n_param(bset1
);
7240 dim1
= isl_basic_set_n_dim(bset1
);
7241 dim2
= isl_basic_set_n_dim(bset2
);
7242 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
7243 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
7244 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
7245 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
7246 if (!bmap1
|| !bmap2
)
7248 bmap1
= isl_basic_map_cow(bmap1
);
7249 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
7250 pos
, (dim1
- pos
) + (dim2
- pos
),
7251 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
7252 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
7255 total
= isl_basic_map_total_dim(bmap1
);
7257 obj
= isl_vec_alloc(ctx
, 1 + total
);
7258 isl_seq_clr(obj
->block
.data
, 1 + total
);
7259 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
7260 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
7263 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
7265 isl_basic_map_free(bmap1
);
7269 isl_basic_map_free(bmap1
);
7270 isl_basic_map_free(bmap2
);
7271 return isl_lp_error
;
7274 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7275 * for any common value of the parameters and dimensions preceding pos
7276 * in both basic sets, the values of dimension pos in bset1 are
7277 * smaller or larger than those in bset2.
7280 * 1 if bset1 follows bset2
7281 * -1 if bset1 precedes bset2
7282 * 0 if bset1 and bset2 are incomparable
7283 * -2 if some error occurred.
7285 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
7286 struct isl_basic_set
*bset2
, int pos
)
7289 enum isl_lp_result res
;
7294 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7296 if (res
== isl_lp_empty
)
7298 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7299 res
== isl_lp_unbounded
)
7301 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7310 /* Given two basic sets bset1 and bset2, check whether
7311 * for any common value of the parameters and dimensions preceding pos
7312 * there is a value of dimension pos in bset1 that is larger
7313 * than a value of the same dimension in bset2.
7316 * 1 if there exists such a pair
7317 * 0 if there is no such pair, but there is a pair of equal values
7319 * -2 if some error occurred.
7321 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
7322 __isl_keep isl_basic_set
*bset2
, int pos
)
7325 enum isl_lp_result res
;
7330 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
7332 if (res
== isl_lp_empty
)
7334 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
7335 res
== isl_lp_unbounded
)
7337 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
7339 else if (res
== isl_lp_ok
)
7348 /* Given two sets set1 and set2, check whether
7349 * for any common value of the parameters and dimensions preceding pos
7350 * there is a value of dimension pos in set1 that is larger
7351 * than a value of the same dimension in set2.
7354 * 1 if there exists such a pair
7355 * 0 if there is no such pair, but there is a pair of equal values
7357 * -2 if some error occurred.
7359 int isl_set_follows_at(__isl_keep isl_set
*set1
,
7360 __isl_keep isl_set
*set2
, int pos
)
7368 for (i
= 0; i
< set1
->n
; ++i
)
7369 for (j
= 0; j
< set2
->n
; ++j
) {
7371 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
7372 if (f
== 1 || f
== -2)
7381 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
7382 unsigned pos
, isl_int
*val
)
7390 total
= isl_basic_map_total_dim(bmap
);
7391 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
7392 for (; d
+1 > pos
; --d
)
7393 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
7397 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
7399 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
7401 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
7404 isl_int_neg(*val
, bmap
->eq
[i
][0]);
7410 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
7411 unsigned pos
, isl_int
*val
)
7423 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
7426 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
7427 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
7428 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
7429 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7433 isl_int_set(*val
, v
);
7439 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
7440 unsigned pos
, isl_int
*val
)
7442 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
7446 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
7449 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
7452 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
7453 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7455 if (pos
>= isl_basic_map_dim(bmap
, type
))
7457 return isl_basic_map_plain_has_fixed_var(bmap
,
7458 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
7461 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
7462 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7464 if (pos
>= isl_map_dim(map
, type
))
7466 return isl_map_plain_has_fixed_var(map
,
7467 map_offset(map
, type
) - 1 + pos
, val
);
7470 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
7471 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7473 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
7476 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
7477 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
7479 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
7482 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7483 * then return this fixed value in *val.
7485 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
7486 unsigned dim
, isl_int
*val
)
7488 return isl_basic_set_plain_has_fixed_var(bset
,
7489 isl_basic_set_n_param(bset
) + dim
, val
);
7492 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7493 * then return this fixed value in *val.
7495 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
7496 unsigned dim
, isl_int
*val
)
7498 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
7501 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
7502 unsigned dim
, isl_int
*val
)
7504 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
7507 /* Check if input variable in has fixed value and if so and if val is not NULL,
7508 * then return this fixed value in *val.
7510 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
7511 unsigned in
, isl_int
*val
)
7513 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
7516 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7517 * and if val is not NULL, then return this lower bound in *val.
7519 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7520 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7522 int i
, i_eq
= -1, i_ineq
= -1;
7529 total
= isl_basic_set_total_dim(bset
);
7530 nparam
= isl_basic_set_n_param(bset
);
7531 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7532 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7538 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7539 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7541 if (i_eq
!= -1 || i_ineq
!= -1)
7545 if (i_eq
== -1 && i_ineq
== -1)
7547 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7548 /* The coefficient should always be one due to normalization. */
7549 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7551 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7553 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7554 total
- nparam
- dim
- 1) != -1)
7557 isl_int_neg(*val
, c
[0]);
7561 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7562 unsigned dim
, isl_int
*val
)
7574 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7578 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7580 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7581 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7583 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7587 isl_int_set(*val
, v
);
7598 /* uset_gist depends on constraints without existentially quantified
7599 * variables sorting first.
7601 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7603 const struct constraint
*c1
= (const struct constraint
*)p1
;
7604 const struct constraint
*c2
= (const struct constraint
*)p2
;
7606 unsigned size
= isl_min(c1
->size
, c2
->size
);
7608 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7609 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7614 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7617 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7618 struct isl_basic_map
*bmap
)
7621 struct constraint
*c
;
7626 total
= isl_basic_map_total_dim(bmap
);
7627 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7630 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7632 c
[i
].c
= bmap
->ineq
[i
];
7634 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7635 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7636 bmap
->ineq
[i
] = c
[i
].c
;
7640 isl_basic_map_free(bmap
);
7644 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7645 __isl_take isl_basic_set
*bset
)
7647 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7648 (struct isl_basic_map
*)bset
);
7651 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7655 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7657 bmap
= isl_basic_map_remove_redundancies(bmap
);
7658 bmap
= isl_basic_map_sort_constraints(bmap
);
7659 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7663 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7665 return (struct isl_basic_set
*)isl_basic_map_normalize(
7666 (struct isl_basic_map
*)bset
);
7669 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7670 const __isl_keep isl_basic_map
*bmap2
)
7677 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7678 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7679 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7680 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7681 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7682 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7683 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7684 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7685 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7686 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7687 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7689 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7691 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7693 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7694 return bmap1
->n_eq
- bmap2
->n_eq
;
7695 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7696 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7697 if (bmap1
->n_div
!= bmap2
->n_div
)
7698 return bmap1
->n_div
- bmap2
->n_div
;
7699 total
= isl_basic_map_total_dim(bmap1
);
7700 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7701 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7705 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7706 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7710 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7711 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7718 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
7719 const __isl_keep isl_basic_set
*bset2
)
7721 return isl_basic_map_plain_cmp(bset1
, bset2
);
7724 int isl_set_plain_cmp(const __isl_keep isl_set
*set1
,
7725 const __isl_keep isl_set
*set2
)
7731 if (set1
->n
!= set2
->n
)
7732 return set1
->n
- set2
->n
;
7734 for (i
= 0; i
< set1
->n
; ++i
) {
7735 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
7743 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7744 __isl_keep isl_basic_map
*bmap2
)
7746 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7749 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7750 __isl_keep isl_basic_set
*bset2
)
7752 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7753 (isl_basic_map
*)bset2
);
7756 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7758 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7759 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7761 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7764 /* We normalize in place, but if anything goes wrong we need
7765 * to return NULL, so we need to make sure we don't change the
7766 * meaning of any possible other copies of map.
7768 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7771 struct isl_basic_map
*bmap
;
7775 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7777 for (i
= 0; i
< map
->n
; ++i
) {
7778 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7781 isl_basic_map_free(map
->p
[i
]);
7784 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7785 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7786 map
= isl_map_remove_empty_parts(map
);
7789 for (i
= map
->n
- 1; i
>= 1; --i
) {
7790 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7792 isl_basic_map_free(map
->p
[i
-1]);
7793 for (j
= i
; j
< map
->n
; ++j
)
7794 map
->p
[j
-1] = map
->p
[j
];
7804 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7806 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7809 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7819 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7822 map1
= isl_map_copy(map1
);
7823 map2
= isl_map_copy(map2
);
7824 map1
= isl_map_normalize(map1
);
7825 map2
= isl_map_normalize(map2
);
7828 equal
= map1
->n
== map2
->n
;
7829 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7830 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7843 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7845 return isl_map_plain_is_equal(map1
, map2
);
7848 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7850 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7851 (struct isl_map
*)set2
);
7854 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7856 return isl_set_plain_is_equal(set1
, set2
);
7859 /* Return an interval that ranges from min to max (inclusive)
7861 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7862 isl_int min
, isl_int max
)
7865 struct isl_basic_set
*bset
= NULL
;
7867 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7871 k
= isl_basic_set_alloc_inequality(bset
);
7874 isl_int_set_si(bset
->ineq
[k
][1], 1);
7875 isl_int_neg(bset
->ineq
[k
][0], min
);
7877 k
= isl_basic_set_alloc_inequality(bset
);
7880 isl_int_set_si(bset
->ineq
[k
][1], -1);
7881 isl_int_set(bset
->ineq
[k
][0], max
);
7885 isl_basic_set_free(bset
);
7889 /* Return the Cartesian product of the basic sets in list (in the given order).
7891 __isl_give isl_basic_set
*isl_basic_set_list_product(
7892 __isl_take
struct isl_basic_set_list
*list
)
7900 struct isl_basic_set
*product
= NULL
;
7904 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7905 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7906 nparam
= isl_basic_set_n_param(list
->p
[0]);
7907 dim
= isl_basic_set_n_dim(list
->p
[0]);
7908 extra
= list
->p
[0]->n_div
;
7909 n_eq
= list
->p
[0]->n_eq
;
7910 n_ineq
= list
->p
[0]->n_ineq
;
7911 for (i
= 1; i
< list
->n
; ++i
) {
7912 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7913 isl_assert(list
->ctx
,
7914 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7915 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7916 extra
+= list
->p
[i
]->n_div
;
7917 n_eq
+= list
->p
[i
]->n_eq
;
7918 n_ineq
+= list
->p
[i
]->n_ineq
;
7920 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7925 for (i
= 0; i
< list
->n
; ++i
) {
7926 isl_basic_set_add_constraints(product
,
7927 isl_basic_set_copy(list
->p
[i
]), dim
);
7928 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7930 isl_basic_set_list_free(list
);
7933 isl_basic_set_free(product
);
7934 isl_basic_set_list_free(list
);
7938 struct isl_basic_map
*isl_basic_map_product(
7939 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7941 isl_space
*dim_result
= NULL
;
7942 struct isl_basic_map
*bmap
;
7943 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7944 struct isl_dim_map
*dim_map1
, *dim_map2
;
7946 if (!bmap1
|| !bmap2
)
7949 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
7950 bmap2
->dim
, isl_dim_param
), goto error
);
7951 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
7952 isl_space_copy(bmap2
->dim
));
7954 in1
= isl_basic_map_n_in(bmap1
);
7955 in2
= isl_basic_map_n_in(bmap2
);
7956 out1
= isl_basic_map_n_out(bmap1
);
7957 out2
= isl_basic_map_n_out(bmap2
);
7958 nparam
= isl_basic_map_n_param(bmap1
);
7960 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7961 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7962 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7963 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7964 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7965 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7966 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7967 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7968 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7969 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7970 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7972 bmap
= isl_basic_map_alloc_space(dim_result
,
7973 bmap1
->n_div
+ bmap2
->n_div
,
7974 bmap1
->n_eq
+ bmap2
->n_eq
,
7975 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7976 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7977 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7978 bmap
= isl_basic_map_simplify(bmap
);
7979 return isl_basic_map_finalize(bmap
);
7981 isl_basic_map_free(bmap1
);
7982 isl_basic_map_free(bmap2
);
7986 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7987 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7989 isl_basic_map
*prod
;
7991 prod
= isl_basic_map_product(bmap1
, bmap2
);
7992 prod
= isl_basic_map_flatten(prod
);
7996 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7997 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7999 return isl_basic_map_flat_range_product(bset1
, bset2
);
8002 __isl_give isl_basic_map
*isl_basic_map_domain_product(
8003 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8005 isl_space
*space_result
= NULL
;
8006 isl_basic_map
*bmap
;
8007 unsigned in1
, in2
, out
, nparam
, total
, pos
;
8008 struct isl_dim_map
*dim_map1
, *dim_map2
;
8010 if (!bmap1
|| !bmap2
)
8013 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
8014 isl_space_copy(bmap2
->dim
));
8016 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8017 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
8018 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
8019 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
8021 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
8022 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8023 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8024 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8025 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8026 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8027 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
8028 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
8029 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
8030 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
8031 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8033 bmap
= isl_basic_map_alloc_space(space_result
,
8034 bmap1
->n_div
+ bmap2
->n_div
,
8035 bmap1
->n_eq
+ bmap2
->n_eq
,
8036 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8037 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8038 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8039 bmap
= isl_basic_map_simplify(bmap
);
8040 return isl_basic_map_finalize(bmap
);
8042 isl_basic_map_free(bmap1
);
8043 isl_basic_map_free(bmap2
);
8047 __isl_give isl_basic_map
*isl_basic_map_range_product(
8048 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8050 isl_space
*dim_result
= NULL
;
8051 isl_basic_map
*bmap
;
8052 unsigned in
, out1
, out2
, nparam
, total
, pos
;
8053 struct isl_dim_map
*dim_map1
, *dim_map2
;
8055 if (!bmap1
|| !bmap2
)
8058 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
8059 isl_space_copy(bmap2
->dim
));
8061 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
8062 out1
= isl_basic_map_n_out(bmap1
);
8063 out2
= isl_basic_map_n_out(bmap2
);
8064 nparam
= isl_basic_map_n_param(bmap1
);
8066 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
8067 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8068 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
8069 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
8070 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
8071 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
8072 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
8073 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
8074 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
8075 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
8076 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
8078 bmap
= isl_basic_map_alloc_space(dim_result
,
8079 bmap1
->n_div
+ bmap2
->n_div
,
8080 bmap1
->n_eq
+ bmap2
->n_eq
,
8081 bmap1
->n_ineq
+ bmap2
->n_ineq
);
8082 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
8083 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
8084 bmap
= isl_basic_map_simplify(bmap
);
8085 return isl_basic_map_finalize(bmap
);
8087 isl_basic_map_free(bmap1
);
8088 isl_basic_map_free(bmap2
);
8092 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
8093 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
8095 isl_basic_map
*prod
;
8097 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
8098 prod
= isl_basic_map_flatten_range(prod
);
8102 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
8103 __isl_take isl_map
*map2
,
8104 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
8105 __isl_take isl_space
*right
),
8106 __isl_give isl_basic_map
*(*basic_map_product
)(
8107 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
8110 struct isl_map
*result
;
8116 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
8117 map2
->dim
, isl_dim_param
), goto error
);
8119 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8120 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8121 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8123 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
8124 isl_space_copy(map2
->dim
)),
8125 map1
->n
* map2
->n
, flags
);
8128 for (i
= 0; i
< map1
->n
; ++i
)
8129 for (j
= 0; j
< map2
->n
; ++j
) {
8130 struct isl_basic_map
*part
;
8131 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
8132 isl_basic_map_copy(map2
->p
[j
]));
8133 if (isl_basic_map_is_empty(part
))
8134 isl_basic_map_free(part
);
8136 result
= isl_map_add_basic_map(result
, part
);
8149 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8151 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
8152 __isl_take isl_map
*map2
)
8154 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
8157 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
8158 __isl_take isl_map
*map2
)
8160 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
8163 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8165 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
8166 __isl_take isl_map
*map2
)
8170 prod
= isl_map_product(map1
, map2
);
8171 prod
= isl_map_flatten(prod
);
8175 /* Given two set A and B, construct its Cartesian product A x B.
8177 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
8179 return isl_map_range_product(set1
, set2
);
8182 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
8183 __isl_take isl_set
*set2
)
8185 return isl_map_flat_range_product(set1
, set2
);
8188 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8190 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
8191 __isl_take isl_map
*map2
)
8193 return map_product(map1
, map2
, &isl_space_domain_product
,
8194 &isl_basic_map_domain_product
);
8197 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8199 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
8200 __isl_take isl_map
*map2
)
8202 return map_product(map1
, map2
, &isl_space_range_product
,
8203 &isl_basic_map_range_product
);
8206 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
8207 __isl_take isl_map
*map2
)
8209 return isl_map_align_params_map_map_and(map1
, map2
,
8210 &map_domain_product_aligned
);
8213 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
8214 __isl_take isl_map
*map2
)
8216 return isl_map_align_params_map_map_and(map1
, map2
,
8217 &map_range_product_aligned
);
8220 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8222 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
8223 __isl_take isl_map
*map2
)
8227 prod
= isl_map_domain_product(map1
, map2
);
8228 prod
= isl_map_flatten_domain(prod
);
8232 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8234 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
8235 __isl_take isl_map
*map2
)
8239 prod
= isl_map_range_product(map1
, map2
);
8240 prod
= isl_map_flatten_range(prod
);
8244 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
8247 uint32_t hash
= isl_hash_init();
8252 bmap
= isl_basic_map_copy(bmap
);
8253 bmap
= isl_basic_map_normalize(bmap
);
8256 total
= isl_basic_map_total_dim(bmap
);
8257 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
8258 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
8260 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
8261 isl_hash_hash(hash
, c_hash
);
8263 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
8264 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8266 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
8267 isl_hash_hash(hash
, c_hash
);
8269 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
8270 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8272 if (isl_int_is_zero(bmap
->div
[i
][0]))
8274 isl_hash_byte(hash
, i
& 0xFF);
8275 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
8276 isl_hash_hash(hash
, c_hash
);
8278 isl_basic_map_free(bmap
);
8282 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
8284 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
8287 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
8294 map
= isl_map_copy(map
);
8295 map
= isl_map_normalize(map
);
8299 hash
= isl_hash_init();
8300 for (i
= 0; i
< map
->n
; ++i
) {
8302 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
8303 isl_hash_hash(hash
, bmap_hash
);
8311 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
8313 return isl_map_get_hash((isl_map
*)set
);
8316 /* Check if the value for dimension dim is completely determined
8317 * by the values of the other parameters and variables.
8318 * That is, check if dimension dim is involved in an equality.
8320 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
8327 nparam
= isl_basic_set_n_param(bset
);
8328 for (i
= 0; i
< bset
->n_eq
; ++i
)
8329 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
8334 /* Check if the value for dimension dim is completely determined
8335 * by the values of the other parameters and variables.
8336 * That is, check if dimension dim is involved in an equality
8337 * for each of the subsets.
8339 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
8345 for (i
= 0; i
< set
->n
; ++i
) {
8347 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
8354 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
8356 return set
? set
->n
: 0;
8359 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
8360 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
8367 for (i
= 0; i
< map
->n
; ++i
)
8368 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
8374 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
8375 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
8382 for (i
= 0; i
< set
->n
; ++i
)
8383 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
8389 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
8396 bset
= isl_basic_set_cow(bset
);
8400 dim
= isl_basic_set_get_space(bset
);
8401 dim
= isl_space_lift(dim
, bset
->n_div
);
8404 isl_space_free(bset
->dim
);
8406 bset
->extra
-= bset
->n_div
;
8409 bset
= isl_basic_set_finalize(bset
);
8413 isl_basic_set_free(bset
);
8417 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
8423 set
= isl_set_align_divs(set
);
8428 set
= isl_set_cow(set
);
8432 n_div
= set
->p
[0]->n_div
;
8433 dim
= isl_set_get_space(set
);
8434 dim
= isl_space_lift(dim
, n_div
);
8437 isl_space_free(set
->dim
);
8440 for (i
= 0; i
< set
->n
; ++i
) {
8441 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
8452 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
8455 struct isl_basic_map
*bmap
;
8462 set
= isl_set_align_divs(set
);
8467 dim
= isl_set_get_space(set
);
8468 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
8470 return isl_map_identity(isl_space_map_from_set(dim
));
8473 n_div
= set
->p
[0]->n_div
;
8474 dim
= isl_space_map_from_set(dim
);
8475 n_param
= isl_space_dim(dim
, isl_dim_param
);
8476 n_set
= isl_space_dim(dim
, isl_dim_in
);
8477 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
8478 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
8479 for (i
= 0; i
< n_set
; ++i
)
8480 bmap
= var_equal(bmap
, i
);
8482 total
= n_param
+ n_set
+ n_set
+ n_div
;
8483 for (i
= 0; i
< n_div
; ++i
) {
8484 k
= isl_basic_map_alloc_inequality(bmap
);
8487 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
8488 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
8489 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
8490 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
8491 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
8492 set
->p
[0]->div
[i
][0]);
8494 l
= isl_basic_map_alloc_inequality(bmap
);
8497 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
8498 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
8499 set
->p
[0]->div
[i
][0]);
8500 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
8504 bmap
= isl_basic_map_simplify(bmap
);
8505 bmap
= isl_basic_map_finalize(bmap
);
8506 return isl_map_from_basic_map(bmap
);
8509 isl_basic_map_free(bmap
);
8513 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
8521 dim
= isl_basic_set_total_dim(bset
);
8522 size
+= bset
->n_eq
* (1 + dim
);
8523 size
+= bset
->n_ineq
* (1 + dim
);
8524 size
+= bset
->n_div
* (2 + dim
);
8529 int isl_set_size(__isl_keep isl_set
*set
)
8537 for (i
= 0; i
< set
->n
; ++i
)
8538 size
+= isl_basic_set_size(set
->p
[i
]);
8543 /* Check if there is any lower bound (if lower == 0) and/or upper
8544 * bound (if upper == 0) on the specified dim.
8546 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
8547 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
8554 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
8556 pos
+= isl_basic_map_offset(bmap
, type
);
8558 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8559 if (isl_int_is_zero(bmap
->div
[i
][0]))
8561 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
8565 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8566 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
8569 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8570 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
8577 return lower
&& upper
;
8580 int isl_basic_map_dim_is_bounded(__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, 0);
8586 int isl_basic_map_dim_has_lower_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
, 0, 1);
8592 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
8593 enum isl_dim_type type
, unsigned pos
)
8595 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
8598 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
8599 enum isl_dim_type type
, unsigned pos
)
8606 for (i
= 0; i
< map
->n
; ++i
) {
8608 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
8609 if (bounded
< 0 || !bounded
)
8616 /* Return 1 if the specified dim is involved in both an upper bound
8617 * and a lower bound.
8619 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
8620 enum isl_dim_type type
, unsigned pos
)
8622 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
8625 static int has_bound(__isl_keep isl_map
*map
,
8626 enum isl_dim_type type
, unsigned pos
,
8627 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
8628 enum isl_dim_type type
, unsigned pos
))
8635 for (i
= 0; i
< map
->n
; ++i
) {
8637 bounded
= fn(map
->p
[i
], type
, pos
);
8638 if (bounded
< 0 || bounded
)
8645 /* Return 1 if the specified dim is involved in any lower bound.
8647 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
8648 enum isl_dim_type type
, unsigned pos
)
8650 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
8653 /* Return 1 if the specified dim is involved in any upper bound.
8655 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
8656 enum isl_dim_type type
, unsigned pos
)
8658 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
8661 /* For each of the "n" variables starting at "first", determine
8662 * the sign of the variable and put the results in the first "n"
8663 * elements of the array "signs".
8665 * 1 means that the variable is non-negative
8666 * -1 means that the variable is non-positive
8667 * 0 means the variable attains both positive and negative values.
8669 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
8670 unsigned first
, unsigned n
, int *signs
)
8672 isl_vec
*bound
= NULL
;
8673 struct isl_tab
*tab
= NULL
;
8674 struct isl_tab_undo
*snap
;
8677 if (!bset
|| !signs
)
8680 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
8681 tab
= isl_tab_from_basic_set(bset
);
8685 isl_seq_clr(bound
->el
, bound
->size
);
8686 isl_int_set_si(bound
->el
[0], -1);
8688 snap
= isl_tab_snap(tab
);
8689 for (i
= 0; i
< n
; ++i
) {
8692 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8693 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8696 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8697 if (isl_tab_rollback(tab
, snap
) < 0)
8705 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8706 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8709 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8710 if (isl_tab_rollback(tab
, snap
) < 0)
8713 signs
[i
] = empty
? -1 : 0;
8717 isl_vec_free(bound
);
8721 isl_vec_free(bound
);
8725 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8726 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8728 if (!bset
|| !signs
)
8730 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8733 first
+= pos(bset
->dim
, type
) - 1;
8734 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8737 /* Check if the given basic map is obviously single-valued.
8738 * In particular, for each output dimension, check that there is
8739 * an equality that defines the output dimension in terms of
8740 * earlier dimensions.
8742 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8752 total
= 1 + isl_basic_map_total_dim(bmap
);
8753 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8754 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8756 for (i
= 0; i
< n_out
; ++i
) {
8757 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8758 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8760 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8761 total
- (o_out
+ i
+ 1)) == -1)
8764 if (j
>= bmap
->n_eq
)
8771 /* Check if the given map is obviously single-valued.
8773 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8782 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8785 /* Check if the given map is single-valued.
8790 * and check if the result is a subset of the identity mapping.
8792 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8799 sv
= isl_map_plain_is_single_valued(map
);
8803 test
= isl_map_reverse(isl_map_copy(map
));
8804 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8806 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
8807 id
= isl_map_identity(dim
);
8809 sv
= isl_map_is_subset(test
, id
);
8817 int isl_map_is_injective(__isl_keep isl_map
*map
)
8821 map
= isl_map_copy(map
);
8822 map
= isl_map_reverse(map
);
8823 in
= isl_map_is_single_valued(map
);
8829 /* Check if the given map is obviously injective.
8831 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8835 map
= isl_map_copy(map
);
8836 map
= isl_map_reverse(map
);
8837 in
= isl_map_plain_is_single_valued(map
);
8843 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8847 sv
= isl_map_is_single_valued(map
);
8851 return isl_map_is_injective(map
);
8854 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8856 return isl_map_is_single_valued((isl_map
*)set
);
8859 int isl_map_is_translation(__isl_keep isl_map
*map
)
8864 delta
= isl_map_deltas(isl_map_copy(map
));
8865 ok
= isl_set_is_singleton(delta
);
8866 isl_set_free(delta
);
8871 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8873 if (isl_seq_first_non_zero(p
, pos
) != -1)
8875 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8880 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8889 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8892 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8893 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
8894 for (j
= 0; j
< nvar
; ++j
) {
8895 int lower
= 0, upper
= 0;
8896 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8897 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8899 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8905 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8906 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8908 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8910 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8915 if (!lower
|| !upper
)
8922 int isl_set_is_box(__isl_keep isl_set
*set
)
8929 return isl_basic_set_is_box(set
->p
[0]);
8932 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8937 return isl_space_is_wrapping(bset
->dim
);
8940 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8945 return isl_space_is_wrapping(set
->dim
);
8948 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8950 bmap
= isl_basic_map_cow(bmap
);
8954 bmap
->dim
= isl_space_wrap(bmap
->dim
);
8958 bmap
= isl_basic_map_finalize(bmap
);
8960 return (isl_basic_set
*)bmap
;
8962 isl_basic_map_free(bmap
);
8966 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8970 map
= isl_map_cow(map
);
8974 for (i
= 0; i
< map
->n
; ++i
) {
8975 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8979 map
->dim
= isl_space_wrap(map
->dim
);
8983 return (isl_set
*)map
;
8989 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8991 bset
= isl_basic_set_cow(bset
);
8995 bset
->dim
= isl_space_unwrap(bset
->dim
);
8999 bset
= isl_basic_set_finalize(bset
);
9001 return (isl_basic_map
*)bset
;
9003 isl_basic_set_free(bset
);
9007 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
9014 if (!isl_set_is_wrapping(set
))
9015 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
9018 set
= isl_set_cow(set
);
9022 for (i
= 0; i
< set
->n
; ++i
) {
9023 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
9028 set
->dim
= isl_space_unwrap(set
->dim
);
9032 return (isl_map
*)set
;
9038 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
9039 enum isl_dim_type type
)
9044 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
9047 bmap
= isl_basic_map_cow(bmap
);
9051 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
9055 bmap
= isl_basic_map_finalize(bmap
);
9059 isl_basic_map_free(bmap
);
9063 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
9064 enum isl_dim_type type
)
9071 if (!isl_space_is_named_or_nested(map
->dim
, type
))
9074 map
= isl_map_cow(map
);
9078 for (i
= 0; i
< map
->n
; ++i
) {
9079 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
9083 map
->dim
= isl_space_reset(map
->dim
, type
);
9093 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
9098 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
9101 bmap
= isl_basic_map_cow(bmap
);
9105 bmap
->dim
= isl_space_flatten(bmap
->dim
);
9109 bmap
= isl_basic_map_finalize(bmap
);
9113 isl_basic_map_free(bmap
);
9117 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
9119 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
9122 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
9123 __isl_take isl_basic_map
*bmap
)
9128 if (!bmap
->dim
->nested
[0])
9131 bmap
= isl_basic_map_cow(bmap
);
9135 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
9139 bmap
= isl_basic_map_finalize(bmap
);
9143 isl_basic_map_free(bmap
);
9147 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
9148 __isl_take isl_basic_map
*bmap
)
9153 if (!bmap
->dim
->nested
[1])
9156 bmap
= isl_basic_map_cow(bmap
);
9160 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
9164 bmap
= isl_basic_map_finalize(bmap
);
9168 isl_basic_map_free(bmap
);
9172 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
9179 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
9182 map
= isl_map_cow(map
);
9186 for (i
= 0; i
< map
->n
; ++i
) {
9187 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
9191 map
->dim
= isl_space_flatten(map
->dim
);
9201 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
9203 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
9206 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
9208 isl_space
*dim
, *flat_dim
;
9211 dim
= isl_set_get_space(set
);
9212 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
9213 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
9214 map
= isl_map_intersect_domain(map
, set
);
9219 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
9226 if (!map
->dim
->nested
[0])
9229 map
= isl_map_cow(map
);
9233 for (i
= 0; i
< map
->n
; ++i
) {
9234 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
9238 map
->dim
= isl_space_flatten_domain(map
->dim
);
9248 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
9255 if (!map
->dim
->nested
[1])
9258 map
= isl_map_cow(map
);
9262 for (i
= 0; i
< map
->n
; ++i
) {
9263 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
9267 map
->dim
= isl_space_flatten_range(map
->dim
);
9277 /* Reorder the dimensions of "bmap" according to the given dim_map
9278 * and set the dimension specification to "dim".
9280 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
9281 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
9285 bmap
= isl_basic_map_cow(bmap
);
9286 if (!bmap
|| !dim
|| !dim_map
)
9289 res
= isl_basic_map_alloc_space(dim
,
9290 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
9291 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
9292 res
= isl_basic_map_finalize(res
);
9296 isl_basic_map_free(bmap
);
9297 isl_space_free(dim
);
9301 /* Reorder the dimensions of "map" according to given reordering.
9303 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
9304 __isl_take isl_reordering
*r
)
9307 struct isl_dim_map
*dim_map
;
9309 map
= isl_map_cow(map
);
9310 dim_map
= isl_dim_map_from_reordering(r
);
9311 if (!map
|| !r
|| !dim_map
)
9314 for (i
= 0; i
< map
->n
; ++i
) {
9315 struct isl_dim_map
*dim_map_i
;
9317 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
9319 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
9320 isl_space_copy(r
->dim
), dim_map_i
);
9326 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
9328 isl_reordering_free(r
);
9334 isl_reordering_free(r
);
9338 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
9339 __isl_take isl_reordering
*r
)
9341 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
9344 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
9345 __isl_take isl_space
*model
)
9352 ctx
= isl_space_get_ctx(model
);
9353 if (!isl_space_has_named_params(model
))
9354 isl_die(ctx
, isl_error_invalid
,
9355 "model has unnamed parameters", goto error
);
9356 if (!isl_space_has_named_params(map
->dim
))
9357 isl_die(ctx
, isl_error_invalid
,
9358 "relation has unnamed parameters", goto error
);
9359 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
9360 isl_reordering
*exp
;
9362 model
= isl_space_drop_dims(model
, isl_dim_in
,
9363 0, isl_space_dim(model
, isl_dim_in
));
9364 model
= isl_space_drop_dims(model
, isl_dim_out
,
9365 0, isl_space_dim(model
, isl_dim_out
));
9366 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
9367 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
9368 map
= isl_map_realign(map
, exp
);
9371 isl_space_free(model
);
9374 isl_space_free(model
);
9379 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
9380 __isl_take isl_space
*model
)
9382 return isl_map_align_params(set
, model
);
9385 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
9386 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9387 enum isl_dim_type c2
, enum isl_dim_type c3
,
9388 enum isl_dim_type c4
, enum isl_dim_type c5
)
9390 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9391 struct isl_mat
*mat
;
9397 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
9398 isl_basic_map_total_dim(bmap
) + 1);
9401 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9402 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9403 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9404 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9405 isl_int_set(mat
->row
[i
][pos
],
9406 bmap
->eq
[i
][off
+ k
]);
9414 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
9415 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
9416 enum isl_dim_type c2
, enum isl_dim_type c3
,
9417 enum isl_dim_type c4
, enum isl_dim_type c5
)
9419 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9420 struct isl_mat
*mat
;
9426 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
9427 isl_basic_map_total_dim(bmap
) + 1);
9430 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
9431 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9432 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9433 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9434 isl_int_set(mat
->row
[i
][pos
],
9435 bmap
->ineq
[i
][off
+ k
]);
9443 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
9444 __isl_take isl_space
*dim
,
9445 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9446 enum isl_dim_type c2
, enum isl_dim_type c3
,
9447 enum isl_dim_type c4
, enum isl_dim_type c5
)
9449 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
9450 isl_basic_map
*bmap
;
9456 if (!dim
|| !eq
|| !ineq
)
9459 if (eq
->n_col
!= ineq
->n_col
)
9460 isl_die(dim
->ctx
, isl_error_invalid
,
9461 "equalities and inequalities matrices should have "
9462 "same number of columns", goto error
);
9464 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
9466 if (eq
->n_col
< total
)
9467 isl_die(dim
->ctx
, isl_error_invalid
,
9468 "number of columns too small", goto error
);
9470 extra
= eq
->n_col
- total
;
9472 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
9473 eq
->n_row
, ineq
->n_row
);
9476 for (i
= 0; i
< extra
; ++i
) {
9477 k
= isl_basic_map_alloc_div(bmap
);
9480 isl_int_set_si(bmap
->div
[k
][0], 0);
9482 for (i
= 0; i
< eq
->n_row
; ++i
) {
9483 l
= isl_basic_map_alloc_equality(bmap
);
9486 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9487 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9488 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9489 isl_int_set(bmap
->eq
[l
][off
+ k
],
9495 for (i
= 0; i
< ineq
->n_row
; ++i
) {
9496 l
= isl_basic_map_alloc_inequality(bmap
);
9499 for (j
= 0, pos
= 0; j
< 5; ++j
) {
9500 int off
= isl_basic_map_offset(bmap
, c
[j
]);
9501 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
9502 isl_int_set(bmap
->ineq
[l
][off
+ k
],
9509 isl_space_free(dim
);
9515 isl_space_free(dim
);
9521 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
9522 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9523 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9525 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
9526 c1
, c2
, c3
, c4
, isl_dim_in
);
9529 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
9530 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
9531 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9533 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
9534 c1
, c2
, c3
, c4
, isl_dim_in
);
9537 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
9538 __isl_take isl_space
*dim
,
9539 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
9540 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
9542 return (isl_basic_set
*)
9543 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
9544 c1
, c2
, c3
, c4
, isl_dim_in
);
9547 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
9552 return isl_space_can_zip(bmap
->dim
);
9555 int isl_map_can_zip(__isl_keep isl_map
*map
)
9560 return isl_space_can_zip(map
->dim
);
9563 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9564 * (A -> C) -> (B -> D).
9566 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
9575 if (!isl_basic_map_can_zip(bmap
))
9576 isl_die(bmap
->ctx
, isl_error_invalid
,
9577 "basic map cannot be zipped", goto error
);
9578 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
9579 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
9580 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
9581 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
9582 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
9585 bmap
->dim
= isl_space_zip(bmap
->dim
);
9590 isl_basic_map_free(bmap
);
9594 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9595 * (A -> C) -> (B -> D).
9597 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
9604 if (!isl_map_can_zip(map
))
9605 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
9608 map
= isl_map_cow(map
);
9612 for (i
= 0; i
< map
->n
; ++i
) {
9613 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
9618 map
->dim
= isl_space_zip(map
->dim
);
9628 /* Construct a basic map mapping the domain of the affine expression
9629 * to a one-dimensional range prescribed by the affine expression.
9631 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
9635 isl_local_space
*ls
;
9636 isl_basic_map
*bmap
;
9641 ls
= isl_aff_get_local_space(aff
);
9642 bmap
= isl_basic_map_from_local_space(ls
);
9643 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
9644 k
= isl_basic_map_alloc_equality(bmap
);
9648 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
9649 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
9650 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
9651 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
9652 aff
->v
->size
- (pos
+ 1));
9655 bmap
= isl_basic_map_finalize(bmap
);
9659 isl_basic_map_free(bmap
);
9663 /* Construct a basic map mapping the domain the multi-affine expression
9664 * to its range, with each dimension in the range equated to the
9665 * corresponding affine expression.
9667 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
9668 __isl_take isl_multi_aff
*maff
)
9672 isl_basic_map
*bmap
;
9677 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
9678 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
9679 "invalid space", return isl_multi_aff_free(maff
));
9681 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
9682 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
9684 for (i
= 0; i
< maff
->n
; ++i
) {
9686 isl_basic_map
*bmap_i
;
9688 aff
= isl_aff_copy(maff
->p
[i
]);
9689 bmap_i
= isl_basic_map_from_aff(aff
);
9691 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9694 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
9696 isl_multi_aff_free(maff
);
9700 /* Construct a basic map mapping a domain in the given space to
9701 * to an n-dimensional range, with n the number of elements in the list,
9702 * where each coordinate in the range is prescribed by the
9703 * corresponding affine expression.
9704 * The domains of all affine expressions in the list are assumed to match
9707 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
9708 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
9712 isl_basic_map
*bmap
;
9717 dim
= isl_space_from_domain(domain_dim
);
9718 bmap
= isl_basic_map_universe(dim
);
9720 for (i
= 0; i
< list
->n
; ++i
) {
9722 isl_basic_map
*bmap_i
;
9724 aff
= isl_aff_copy(list
->p
[i
]);
9725 bmap_i
= isl_basic_map_from_aff(aff
);
9727 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
9730 isl_aff_list_free(list
);
9734 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
9735 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9737 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
9740 /* Add a constraint imposing that the given two dimensions are equal.
9742 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
9743 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9745 isl_basic_map
*bmap
= NULL
;
9751 if (pos1
>= isl_map_dim(map
, type1
))
9752 isl_die(map
->ctx
, isl_error_invalid
,
9753 "index out of bounds", goto error
);
9754 if (pos2
>= isl_map_dim(map
, type2
))
9755 isl_die(map
->ctx
, isl_error_invalid
,
9756 "index out of bounds", goto error
);
9758 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9759 i
= isl_basic_map_alloc_equality(bmap
);
9762 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9763 pos1
+= isl_basic_map_offset(bmap
, type1
);
9764 pos2
+= isl_basic_map_offset(bmap
, type2
);
9765 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
9766 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9767 bmap
= isl_basic_map_finalize(bmap
);
9769 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9773 isl_basic_map_free(bmap
);
9778 /* Add a constraint imposing that the given two dimensions have opposite values.
9780 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
9781 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
9783 isl_basic_map
*bmap
= NULL
;
9789 if (pos1
>= isl_map_dim(map
, type1
))
9790 isl_die(map
->ctx
, isl_error_invalid
,
9791 "index out of bounds", goto error
);
9792 if (pos2
>= isl_map_dim(map
, type2
))
9793 isl_die(map
->ctx
, isl_error_invalid
,
9794 "index out of bounds", goto error
);
9796 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
9797 i
= isl_basic_map_alloc_equality(bmap
);
9800 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
9801 pos1
+= isl_basic_map_offset(bmap
, type1
);
9802 pos2
+= isl_basic_map_offset(bmap
, type2
);
9803 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
9804 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
9805 bmap
= isl_basic_map_finalize(bmap
);
9807 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
9811 isl_basic_map_free(bmap
);
9816 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
9820 isl_local_space
*ls
;
9825 if (!isl_basic_map_divs_known(bmap
))
9826 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
9827 "some divs are unknown", return NULL
);
9829 ls
= isl_basic_map_get_local_space(bmap
);
9830 div
= isl_local_space_get_div(ls
, pos
);
9831 isl_local_space_free(ls
);
9836 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
9839 return isl_basic_map_get_div(bset
, pos
);
9842 /* Plug in "subs" for dimension "type", "pos" of "bset".
9844 * Let i be the dimension to replace and let "subs" be of the form
9848 * Any integer division with a non-zero coefficient for i,
9850 * floor((a i + g)/m)
9854 * floor((a f + d g)/(m d))
9856 * Constraints of the form
9864 __isl_give isl_basic_set
*isl_basic_set_substitute(
9865 __isl_take isl_basic_set
*bset
,
9866 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9872 if (bset
&& isl_basic_set_plain_is_empty(bset
))
9875 bset
= isl_basic_set_cow(bset
);
9879 ctx
= isl_basic_set_get_ctx(bset
);
9880 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
9881 isl_die(ctx
, isl_error_invalid
,
9882 "spaces don't match", goto error
);
9883 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
9884 isl_die(ctx
, isl_error_unsupported
,
9885 "cannot handle divs yet", goto error
);
9887 pos
+= isl_basic_set_offset(bset
, type
);
9891 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9892 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
9894 isl_int_set(v
, bset
->eq
[i
][pos
]);
9895 isl_int_set_si(bset
->eq
[i
][pos
], 0);
9896 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
9897 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9900 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9901 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
9903 isl_int_set(v
, bset
->ineq
[i
][pos
]);
9904 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
9905 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
9906 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9909 for (i
= 0; i
< bset
->n_div
; ++i
) {
9910 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
9912 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
9913 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
9914 isl_seq_combine(bset
->div
[i
] + 1,
9915 subs
->v
->el
[0], bset
->div
[i
] + 1,
9916 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
9917 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
9922 bset
= isl_basic_set_simplify(bset
);
9923 return isl_basic_set_finalize(bset
);
9925 isl_basic_set_free(bset
);
9929 /* Plug in "subs" for dimension "type", "pos" of "set".
9931 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
9932 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
9936 if (set
&& isl_set_plain_is_empty(set
))
9939 set
= isl_set_cow(set
);
9943 for (i
= set
->n
- 1; i
>= 0; --i
) {
9944 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
9947 if (isl_basic_set_plain_is_empty(set
->p
[i
])) {
9948 isl_basic_set_free(set
->p
[i
]);
9949 if (i
!= set
->n
- 1)
9950 set
->p
[i
] = set
->p
[set
->n
- 1];