2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return dim
->nparam
;
50 case isl_dim_in
: return dim
->n_in
;
51 case isl_dim_out
: return dim
->n_out
;
52 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
57 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
60 case isl_dim_param
: return 1;
61 case isl_dim_in
: return 1 + dim
->nparam
;
62 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
67 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
68 enum isl_dim_type type
)
73 case isl_dim_cst
: return 1;
76 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
77 case isl_dim_div
: return bmap
->n_div
;
78 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
83 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
85 return map
? n(map
->dim
, type
) : 0;
88 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
90 return set
? n(set
->dim
, type
) : 0;
93 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
94 enum isl_dim_type type
)
96 isl_space
*dim
= bmap
->dim
;
98 case isl_dim_cst
: return 0;
99 case isl_dim_param
: return 1;
100 case isl_dim_in
: return 1 + dim
->nparam
;
101 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
102 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
107 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
108 enum isl_dim_type type
)
110 return isl_basic_map_offset(bset
, type
);
113 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
115 return pos(map
->dim
, type
);
118 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
119 enum isl_dim_type type
)
121 return isl_basic_map_dim(bset
, type
);
124 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
126 return isl_basic_set_dim(bset
, isl_dim_set
);
129 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
131 return isl_basic_set_dim(bset
, isl_dim_param
);
134 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
138 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
141 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
143 return isl_set_dim(set
, isl_dim_set
);
146 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
148 return isl_set_dim(set
, isl_dim_param
);
151 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
153 return bmap
? bmap
->dim
->n_in
: 0;
156 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
158 return bmap
? bmap
->dim
->n_out
: 0;
161 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
163 return bmap
? bmap
->dim
->nparam
: 0;
166 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
168 return bmap
? bmap
->n_div
: 0;
171 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
173 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
176 unsigned isl_map_n_in(const struct isl_map
*map
)
178 return map
? map
->dim
->n_in
: 0;
181 unsigned isl_map_n_out(const struct isl_map
*map
)
183 return map
? map
->dim
->n_out
: 0;
186 unsigned isl_map_n_param(const struct isl_map
*map
)
188 return map
? map
->dim
->nparam
: 0;
191 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
196 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
199 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
200 set
->dim
, isl_dim_set
);
203 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
204 __isl_keep isl_basic_set
*bset
)
208 return isl_bool_error
;
209 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
212 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
213 bset
->dim
, isl_dim_set
);
216 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
221 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
224 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
225 set
->dim
, isl_dim_set
);
228 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
229 struct isl_basic_set
*bset
)
234 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
237 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
238 bset
->dim
, isl_dim_set
);
241 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
243 return bmap
? bmap
->ctx
: NULL
;
246 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
248 return bset
? bset
->ctx
: NULL
;
251 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
253 return map
? map
->ctx
: NULL
;
256 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
258 return set
? set
->ctx
: NULL
;
261 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
265 return isl_space_copy(bmap
->dim
);
268 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
272 return isl_space_copy(bset
->dim
);
275 /* Extract the divs in "bmap" as a matrix.
277 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
288 ctx
= isl_basic_map_get_ctx(bmap
);
289 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
290 cols
= 1 + 1 + total
+ bmap
->n_div
;
291 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
295 for (i
= 0; i
< bmap
->n_div
; ++i
)
296 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
301 /* Extract the divs in "bset" as a matrix.
303 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
305 return isl_basic_map_get_divs(bset
);
308 __isl_give isl_local_space
*isl_basic_map_get_local_space(
309 __isl_keep isl_basic_map
*bmap
)
316 div
= isl_basic_map_get_divs(bmap
);
317 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
320 __isl_give isl_local_space
*isl_basic_set_get_local_space(
321 __isl_keep isl_basic_set
*bset
)
323 return isl_basic_map_get_local_space(bset
);
326 /* For each known div d = floor(f/m), add the constraints
329 * -(f-(m-1)) + m d >= 0
331 * Do not finalize the result.
333 static __isl_give isl_basic_map
*add_known_div_constraints(
334 __isl_take isl_basic_map
*bmap
)
341 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
344 bmap
= isl_basic_map_cow(bmap
);
345 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
348 for (i
= 0; i
< n_div
; ++i
) {
349 if (isl_int_is_zero(bmap
->div
[i
][0]))
351 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
352 return isl_basic_map_free(bmap
);
358 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
359 __isl_take isl_local_space
*ls
)
368 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
369 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
370 n_div
, 0, 2 * n_div
);
372 for (i
= 0; i
< n_div
; ++i
)
373 if (isl_basic_map_alloc_div(bmap
) < 0)
376 for (i
= 0; i
< n_div
; ++i
)
377 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
378 bmap
= add_known_div_constraints(bmap
);
380 isl_local_space_free(ls
);
383 isl_local_space_free(ls
);
384 isl_basic_map_free(bmap
);
388 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
389 __isl_take isl_local_space
*ls
)
391 return isl_basic_map_from_local_space(ls
);
394 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
398 return isl_space_copy(map
->dim
);
401 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
405 return isl_space_copy(set
->dim
);
408 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
409 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
411 bmap
= isl_basic_map_cow(bmap
);
414 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
417 bmap
= isl_basic_map_finalize(bmap
);
420 isl_basic_map_free(bmap
);
424 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
425 __isl_take isl_basic_set
*bset
, const char *s
)
427 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
430 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
431 enum isl_dim_type type
)
433 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
436 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
437 enum isl_dim_type type
, const char *s
)
441 map
= isl_map_cow(map
);
445 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
449 for (i
= 0; i
< map
->n
; ++i
) {
450 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
461 /* Replace the identifier of the tuple of type "type" by "id".
463 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
464 __isl_take isl_basic_map
*bmap
,
465 enum isl_dim_type type
, __isl_take isl_id
*id
)
467 bmap
= isl_basic_map_cow(bmap
);
470 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
472 return isl_basic_map_free(bmap
);
473 bmap
= isl_basic_map_finalize(bmap
);
480 /* Replace the identifier of the tuple by "id".
482 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
483 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
485 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
488 /* Does the input or output tuple have a name?
490 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
492 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
495 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
496 enum isl_dim_type type
)
498 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
501 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
504 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
507 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
508 enum isl_dim_type type
, __isl_take isl_id
*id
)
510 map
= isl_map_cow(map
);
514 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
516 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
522 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
523 __isl_take isl_id
*id
)
525 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
528 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
529 enum isl_dim_type type
)
531 map
= isl_map_cow(map
);
535 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
537 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
540 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
542 return isl_map_reset_tuple_id(set
, isl_dim_set
);
545 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
547 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
550 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
551 enum isl_dim_type type
)
553 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
556 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
558 return isl_map_has_tuple_id(set
, isl_dim_set
);
561 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
563 return isl_map_get_tuple_id(set
, isl_dim_set
);
566 /* Does the set tuple have a name?
568 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
571 return isl_bool_error
;
572 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
576 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
578 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
581 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
583 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
586 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
587 enum isl_dim_type type
, unsigned pos
)
589 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
592 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
593 enum isl_dim_type type
, unsigned pos
)
595 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
598 /* Does the given dimension have a name?
600 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
601 enum isl_dim_type type
, unsigned pos
)
604 return isl_bool_error
;
605 return isl_space_has_dim_name(map
->dim
, type
, pos
);
608 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
609 enum isl_dim_type type
, unsigned pos
)
611 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
614 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
615 enum isl_dim_type type
, unsigned pos
)
617 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
620 /* Does the given dimension have a name?
622 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
623 enum isl_dim_type type
, unsigned pos
)
626 return isl_bool_error
;
627 return isl_space_has_dim_name(set
->dim
, type
, pos
);
630 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
631 __isl_take isl_basic_map
*bmap
,
632 enum isl_dim_type type
, unsigned pos
, const char *s
)
634 bmap
= isl_basic_map_cow(bmap
);
637 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
640 return isl_basic_map_finalize(bmap
);
642 isl_basic_map_free(bmap
);
646 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
647 enum isl_dim_type type
, unsigned pos
, const char *s
)
651 map
= isl_map_cow(map
);
655 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
659 for (i
= 0; i
< map
->n
; ++i
) {
660 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
671 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
672 __isl_take isl_basic_set
*bset
,
673 enum isl_dim_type type
, unsigned pos
, const char *s
)
675 return (isl_basic_set
*)isl_basic_map_set_dim_name(
676 (isl_basic_map
*)bset
, type
, pos
, s
);
679 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
680 enum isl_dim_type type
, unsigned pos
, const char *s
)
682 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
685 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
686 enum isl_dim_type type
, unsigned pos
)
689 return isl_bool_error
;
690 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
693 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
694 enum isl_dim_type type
, unsigned pos
)
696 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
699 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
700 enum isl_dim_type type
, unsigned pos
)
702 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
705 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
706 enum isl_dim_type type
, unsigned pos
)
708 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
711 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
712 enum isl_dim_type type
, unsigned pos
)
714 return isl_map_has_dim_id(set
, type
, pos
);
717 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
718 enum isl_dim_type type
, unsigned pos
)
720 return isl_map_get_dim_id(set
, type
, pos
);
723 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
724 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
726 map
= isl_map_cow(map
);
730 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
732 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
738 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
739 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
741 return isl_map_set_dim_id(set
, type
, pos
, id
);
744 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
745 __isl_keep isl_id
*id
)
749 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
752 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
753 __isl_keep isl_id
*id
)
755 return isl_map_find_dim_by_id(set
, type
, id
);
758 /* Return the position of the dimension of the given type and name
760 * Return -1 if no such dimension can be found.
762 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
763 enum isl_dim_type type
, const char *name
)
767 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
770 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
775 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
778 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
781 return isl_map_find_dim_by_name(set
, type
, name
);
784 /* Reset the user pointer on all identifiers of parameters and tuples
785 * of the space of "map".
787 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
791 space
= isl_map_get_space(map
);
792 space
= isl_space_reset_user(space
);
793 map
= isl_map_reset_space(map
, space
);
798 /* Reset the user pointer on all identifiers of parameters and tuples
799 * of the space of "set".
801 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
803 return isl_map_reset_user(set
);
806 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
810 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
813 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
815 return isl_basic_map_is_rational(bset
);
818 /* Does "bmap" contain any rational points?
820 * If "bmap" has an equality for each dimension, equating the dimension
821 * to an integer constant, then it has no rational points, even if it
822 * is marked as rational.
824 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
826 int has_rational
= 1;
831 if (isl_basic_map_plain_is_empty(bmap
))
833 if (!isl_basic_map_is_rational(bmap
))
835 bmap
= isl_basic_map_copy(bmap
);
836 bmap
= isl_basic_map_implicit_equalities(bmap
);
839 total
= isl_basic_map_total_dim(bmap
);
840 if (bmap
->n_eq
== total
) {
842 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
843 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
846 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
847 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
849 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
857 isl_basic_map_free(bmap
);
862 /* Does "map" contain any rational points?
864 int isl_map_has_rational(__isl_keep isl_map
*map
)
871 for (i
= 0; i
< map
->n
; ++i
) {
872 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
873 if (has_rational
< 0)
881 /* Does "set" contain any rational points?
883 int isl_set_has_rational(__isl_keep isl_set
*set
)
885 return isl_map_has_rational(set
);
888 /* Is this basic set a parameter domain?
890 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
894 return isl_space_is_params(bset
->dim
);
897 /* Is this set a parameter domain?
899 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
902 return isl_bool_error
;
903 return isl_space_is_params(set
->dim
);
906 /* Is this map actually a parameter domain?
907 * Users should never call this function. Outside of isl,
908 * a map can never be a parameter domain.
910 int isl_map_is_params(__isl_keep isl_map
*map
)
914 return isl_space_is_params(map
->dim
);
917 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
918 struct isl_basic_map
*bmap
, unsigned extra
,
919 unsigned n_eq
, unsigned n_ineq
)
922 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
927 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
928 if (isl_blk_is_error(bmap
->block
))
931 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
932 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
936 bmap
->block2
= isl_blk_empty();
939 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
940 if (isl_blk_is_error(bmap
->block2
))
943 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
948 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
949 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
951 for (i
= 0; i
< extra
; ++i
)
952 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
956 bmap
->c_size
= n_eq
+ n_ineq
;
957 bmap
->eq
= bmap
->ineq
+ n_ineq
;
966 isl_basic_map_free(bmap
);
970 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
971 unsigned nparam
, unsigned dim
, unsigned extra
,
972 unsigned n_eq
, unsigned n_ineq
)
974 struct isl_basic_map
*bmap
;
977 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
981 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
982 return (struct isl_basic_set
*)bmap
;
985 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
986 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
988 struct isl_basic_map
*bmap
;
991 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
992 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
993 return (struct isl_basic_set
*)bmap
;
999 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1000 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1002 struct isl_basic_map
*bmap
;
1006 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1011 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1013 isl_space_free(dim
);
1017 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1018 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1019 unsigned n_eq
, unsigned n_ineq
)
1021 struct isl_basic_map
*bmap
;
1024 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1028 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1032 static void dup_constraints(
1033 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1036 unsigned total
= isl_basic_map_total_dim(src
);
1038 for (i
= 0; i
< src
->n_eq
; ++i
) {
1039 int j
= isl_basic_map_alloc_equality(dst
);
1040 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1043 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1044 int j
= isl_basic_map_alloc_inequality(dst
);
1045 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1048 for (i
= 0; i
< src
->n_div
; ++i
) {
1049 int j
= isl_basic_map_alloc_div(dst
);
1050 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1052 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1055 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1057 struct isl_basic_map
*dup
;
1061 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1062 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1065 dup_constraints(dup
, bmap
);
1066 dup
->flags
= bmap
->flags
;
1067 dup
->sample
= isl_vec_copy(bmap
->sample
);
1071 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1073 struct isl_basic_map
*dup
;
1075 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1076 return (struct isl_basic_set
*)dup
;
1079 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1084 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1088 return isl_basic_set_dup(bset
);
1091 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1100 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1105 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1109 bmap
= isl_basic_map_dup(bmap
);
1111 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1115 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1124 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1129 if (--bmap
->ref
> 0)
1132 isl_ctx_deref(bmap
->ctx
);
1134 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1136 isl_blk_free(bmap
->ctx
, bmap
->block
);
1137 isl_vec_free(bmap
->sample
);
1138 isl_space_free(bmap
->dim
);
1144 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1146 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1149 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1151 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1154 __isl_give isl_map
*isl_map_align_params_map_map_and(
1155 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1156 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1157 __isl_take isl_map
*map2
))
1161 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1162 return fn(map1
, map2
);
1163 if (!isl_space_has_named_params(map1
->dim
) ||
1164 !isl_space_has_named_params(map2
->dim
))
1165 isl_die(map1
->ctx
, isl_error_invalid
,
1166 "unaligned unnamed parameters", goto error
);
1167 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1168 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1169 return fn(map1
, map2
);
1176 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1177 __isl_keep isl_map
*map2
,
1178 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1183 return isl_bool_error
;
1184 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1185 return fn(map1
, map2
);
1186 if (!isl_space_has_named_params(map1
->dim
) ||
1187 !isl_space_has_named_params(map2
->dim
))
1188 isl_die(map1
->ctx
, isl_error_invalid
,
1189 "unaligned unnamed parameters", return isl_bool_error
);
1190 map1
= isl_map_copy(map1
);
1191 map2
= isl_map_copy(map2
);
1192 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1193 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1200 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1202 struct isl_ctx
*ctx
;
1206 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1207 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1210 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1211 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1212 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1213 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1214 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1216 int j
= isl_basic_map_alloc_inequality(bmap
);
1220 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1221 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1228 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1229 bmap
->extra
- bmap
->n_div
);
1230 return bmap
->n_eq
++;
1233 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1235 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1238 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1242 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1247 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1249 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1252 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1257 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1259 if (pos
!= bmap
->n_eq
- 1) {
1261 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1262 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1268 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1270 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1273 /* Turn inequality "pos" of "bmap" into an equality.
1275 * In particular, we move the inequality in front of the equalities
1276 * and move the last inequality in the position of the moved inequality.
1277 * Note that isl_tab_make_equalities_explicit depends on this particular
1278 * change in the ordering of the constraints.
1280 void isl_basic_map_inequality_to_equality(
1281 struct isl_basic_map
*bmap
, unsigned pos
)
1285 t
= bmap
->ineq
[pos
];
1286 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1287 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1292 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1293 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1294 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1295 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1298 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1300 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1303 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1305 struct isl_ctx
*ctx
;
1309 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1310 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1311 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1312 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1313 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1314 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1315 1 + isl_basic_map_total_dim(bmap
),
1316 bmap
->extra
- bmap
->n_div
);
1317 return bmap
->n_ineq
++;
1320 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1322 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1325 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1329 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1334 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1336 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1339 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1344 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1346 if (pos
!= bmap
->n_ineq
- 1) {
1347 t
= bmap
->ineq
[pos
];
1348 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1349 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1350 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1356 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1358 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1361 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1366 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1369 k
= isl_basic_map_alloc_equality(bmap
);
1372 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1375 isl_basic_map_free(bmap
);
1379 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1382 return (isl_basic_set
*)
1383 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1386 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1391 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1394 k
= isl_basic_map_alloc_inequality(bmap
);
1397 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1400 isl_basic_map_free(bmap
);
1404 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1407 return (isl_basic_set
*)
1408 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1411 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1415 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1416 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1417 1 + 1 + isl_basic_map_total_dim(bmap
),
1418 bmap
->extra
- bmap
->n_div
);
1419 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1420 return bmap
->n_div
++;
1423 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1425 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1428 /* Insert an extra integer division, prescribed by "div", to "bmap"
1429 * at (integer division) position "pos".
1431 * The integer division is first added at the end and then moved
1432 * into the right position.
1434 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1435 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1439 bmap
= isl_basic_map_cow(bmap
);
1441 return isl_basic_map_free(bmap
);
1443 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1444 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1445 "unexpected size", return isl_basic_map_free(bmap
));
1446 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1447 if (pos
< 0 || pos
> n_div
)
1448 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1449 "invalid position", return isl_basic_map_free(bmap
));
1451 bmap
= isl_basic_map_extend_space(bmap
,
1452 isl_basic_map_get_space(bmap
), 1, 0, 2);
1453 k
= isl_basic_map_alloc_div(bmap
);
1455 return isl_basic_map_free(bmap
);
1456 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1457 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1459 for (i
= k
; i
> pos
; --i
)
1460 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1465 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1469 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1474 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1476 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1479 /* Copy constraint from src to dst, putting the vars of src at offset
1480 * dim_off in dst and the divs of src at offset div_off in dst.
1481 * If both sets are actually map, then dim_off applies to the input
1484 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1485 struct isl_basic_map
*src_map
, isl_int
*src
,
1486 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1488 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1489 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1490 unsigned src_in
= isl_basic_map_n_in(src_map
);
1491 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1492 unsigned src_out
= isl_basic_map_n_out(src_map
);
1493 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1494 isl_int_set(dst
[0], src
[0]);
1495 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1496 if (dst_nparam
> src_nparam
)
1497 isl_seq_clr(dst
+1+src_nparam
,
1498 dst_nparam
- src_nparam
);
1499 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1500 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1502 isl_min(dst_in
-in_off
, src_in
));
1503 if (dst_in
-in_off
> src_in
)
1504 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1505 dst_in
- in_off
- src_in
);
1506 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1507 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1508 src
+1+src_nparam
+src_in
,
1509 isl_min(dst_out
-out_off
, src_out
));
1510 if (dst_out
-out_off
> src_out
)
1511 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1512 dst_out
- out_off
- src_out
);
1513 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1514 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1515 src
+1+src_nparam
+src_in
+src_out
,
1516 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1517 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1518 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1519 div_off
+src_map
->n_div
,
1520 dst_map
->n_div
- div_off
- src_map
->n_div
);
1523 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1524 struct isl_basic_map
*src_map
, isl_int
*src
,
1525 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1527 isl_int_set(dst
[0], src
[0]);
1528 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1531 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1532 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1537 if (!bmap1
|| !bmap2
)
1540 div_off
= bmap1
->n_div
;
1542 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1543 int i1
= isl_basic_map_alloc_equality(bmap1
);
1546 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1547 i_pos
, o_pos
, div_off
);
1550 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1551 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1554 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1555 i_pos
, o_pos
, div_off
);
1558 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1559 int i1
= isl_basic_map_alloc_div(bmap1
);
1562 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1563 i_pos
, o_pos
, div_off
);
1566 isl_basic_map_free(bmap2
);
1571 isl_basic_map_free(bmap1
);
1572 isl_basic_map_free(bmap2
);
1576 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1577 struct isl_basic_set
*bset2
, unsigned pos
)
1579 return (struct isl_basic_set
*)
1580 add_constraints((struct isl_basic_map
*)bset1
,
1581 (struct isl_basic_map
*)bset2
, 0, pos
);
1584 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1585 __isl_take isl_space
*dim
, unsigned extra
,
1586 unsigned n_eq
, unsigned n_ineq
)
1588 struct isl_basic_map
*ext
;
1598 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1599 base
->extra
>= base
->n_div
+ extra
;
1601 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1602 room_for_ineq(base
, n_ineq
)) {
1603 isl_space_free(dim
);
1607 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1608 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1609 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1610 extra
+= base
->extra
;
1612 n_ineq
+= base
->n_ineq
;
1614 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1620 ext
->sample
= isl_vec_copy(base
->sample
);
1621 flags
= base
->flags
;
1622 ext
= add_constraints(ext
, base
, 0, 0);
1625 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1631 isl_space_free(dim
);
1632 isl_basic_map_free(base
);
1636 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1637 __isl_take isl_space
*dim
, unsigned extra
,
1638 unsigned n_eq
, unsigned n_ineq
)
1640 return (struct isl_basic_set
*)
1641 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1642 extra
, n_eq
, n_ineq
);
1645 struct isl_basic_map
*isl_basic_map_extend_constraints(
1646 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1650 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1654 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1655 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1656 unsigned n_eq
, unsigned n_ineq
)
1658 struct isl_basic_map
*bmap
;
1663 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1667 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1670 isl_basic_map_free(base
);
1674 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1675 unsigned nparam
, unsigned dim
, unsigned extra
,
1676 unsigned n_eq
, unsigned n_ineq
)
1678 return (struct isl_basic_set
*)
1679 isl_basic_map_extend((struct isl_basic_map
*)base
,
1680 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1683 struct isl_basic_set
*isl_basic_set_extend_constraints(
1684 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1686 return (struct isl_basic_set
*)
1687 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1691 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1693 return (struct isl_basic_set
*)
1694 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1697 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1702 if (bmap
->ref
> 1) {
1704 bmap
= isl_basic_map_dup(bmap
);
1707 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1708 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1713 /* Clear all cached information in "map", either because it is about
1714 * to be modified or because it is being freed.
1715 * Always return the same pointer that is passed in.
1716 * This is needed for the use in isl_map_free.
1718 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1720 isl_basic_map_free(map
->cached_simple_hull
[0]);
1721 isl_basic_map_free(map
->cached_simple_hull
[1]);
1722 map
->cached_simple_hull
[0] = NULL
;
1723 map
->cached_simple_hull
[1] = NULL
;
1727 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1729 return isl_map_cow(set
);
1732 /* Return an isl_map that is equal to "map" and that has only
1733 * a single reference.
1735 * If the original input already has only one reference, then
1736 * simply return it, but clear all cached information, since
1737 * it may be rendered invalid by the operations that will be
1738 * performed on the result.
1740 * Otherwise, create a duplicate (without any cached information).
1742 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1748 return clear_caches(map
);
1750 return isl_map_dup(map
);
1753 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1754 unsigned a_len
, unsigned b_len
)
1756 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1757 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1758 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1761 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1762 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1770 isl_assert(bmap
->ctx
,
1771 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1773 if (n1
== 0 || n2
== 0)
1776 bmap
= isl_basic_map_cow(bmap
);
1780 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1781 if (isl_blk_is_error(blk
))
1784 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1786 bmap
->eq
[i
] + pos
, n1
, n2
);
1788 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1790 bmap
->ineq
[i
] + pos
, n1
, n2
);
1792 for (i
= 0; i
< bmap
->n_div
; ++i
)
1794 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1796 isl_blk_free(bmap
->ctx
, blk
);
1798 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1799 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1800 return isl_basic_map_finalize(bmap
);
1802 isl_basic_map_free(bmap
);
1806 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1812 total
= isl_basic_map_total_dim(bmap
);
1813 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1814 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1816 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1818 i
= isl_basic_map_alloc_equality(bmap
);
1822 isl_int_set_si(bmap
->eq
[i
][0], 1);
1823 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1824 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1825 isl_vec_free(bmap
->sample
);
1826 bmap
->sample
= NULL
;
1827 return isl_basic_map_finalize(bmap
);
1829 isl_basic_map_free(bmap
);
1833 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1835 return (struct isl_basic_set
*)
1836 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1839 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1842 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1844 isl_int
*t
= bmap
->div
[a
];
1845 bmap
->div
[a
] = bmap
->div
[b
];
1849 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1850 * div definitions accordingly.
1852 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1855 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1857 swap_div(bmap
, a
, b
);
1859 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1860 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1862 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1863 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1865 for (i
= 0; i
< bmap
->n_div
; ++i
)
1866 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1867 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1870 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1871 * div definitions accordingly.
1873 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1875 isl_basic_map_swap_div(bset
, a
, b
);
1878 /* Eliminate the specified n dimensions starting at first from the
1879 * constraints, without removing the dimensions from the space.
1880 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1882 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1883 enum isl_dim_type type
, unsigned first
, unsigned n
)
1892 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1893 isl_die(map
->ctx
, isl_error_invalid
,
1894 "index out of bounds", goto error
);
1896 map
= isl_map_cow(map
);
1900 for (i
= 0; i
< map
->n
; ++i
) {
1901 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1911 /* Eliminate the specified n dimensions starting at first from the
1912 * constraints, without removing the dimensions from the space.
1913 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1915 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1916 enum isl_dim_type type
, unsigned first
, unsigned n
)
1918 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1921 /* Eliminate the specified n dimensions starting at first from the
1922 * constraints, without removing the dimensions from the space.
1923 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1925 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1926 unsigned first
, unsigned n
)
1928 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1931 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1932 __isl_take isl_basic_map
*bmap
)
1936 bmap
= isl_basic_map_eliminate_vars(bmap
,
1937 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1941 return isl_basic_map_finalize(bmap
);
1944 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1945 __isl_take isl_basic_set
*bset
)
1947 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1948 (struct isl_basic_map
*)bset
);
1951 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1960 map
= isl_map_cow(map
);
1964 for (i
= 0; i
< map
->n
; ++i
) {
1965 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1975 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1977 return isl_map_remove_divs(set
);
1980 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1981 enum isl_dim_type type
, unsigned first
, unsigned n
)
1985 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1987 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1989 bmap
= isl_basic_map_eliminate_vars(bmap
,
1990 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1993 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1995 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1998 isl_basic_map_free(bmap
);
2002 /* Return true if the definition of the given div (recursively) involves
2003 * any of the given variables.
2005 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2006 unsigned first
, unsigned n
)
2009 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2011 if (isl_int_is_zero(bmap
->div
[div
][0]))
2013 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2016 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2017 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2019 if (div_involves_vars(bmap
, i
, first
, n
))
2026 /* Try and add a lower and/or upper bound on "div" to "bmap"
2027 * based on inequality "i".
2028 * "total" is the total number of variables (excluding the divs).
2029 * "v" is a temporary object that can be used during the calculations.
2030 * If "lb" is set, then a lower bound should be constructed.
2031 * If "ub" is set, then an upper bound should be constructed.
2033 * The calling function has already checked that the inequality does not
2034 * reference "div", but we still need to check that the inequality is
2035 * of the right form. We'll consider the case where we want to construct
2036 * a lower bound. The construction of upper bounds is similar.
2038 * Let "div" be of the form
2040 * q = floor((a + f(x))/d)
2042 * We essentially check if constraint "i" is of the form
2046 * so that we can use it to derive a lower bound on "div".
2047 * However, we allow a slightly more general form
2051 * with the condition that the coefficients of g(x) - f(x) are all
2053 * Rewriting this constraint as
2057 * adding a + f(x) to both sides and dividing by d, we obtain
2059 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2061 * Taking the floor on both sides, we obtain
2063 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2067 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2069 * In the case of an upper bound, we construct the constraint
2071 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2074 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2075 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2076 unsigned total
, isl_int v
, int lb
, int ub
)
2080 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2082 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2083 bmap
->div
[div
][1 + 1 + j
]);
2084 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2087 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2088 bmap
->div
[div
][1 + 1 + j
]);
2089 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2095 bmap
= isl_basic_map_cow(bmap
);
2096 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2098 int k
= isl_basic_map_alloc_inequality(bmap
);
2101 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2102 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2103 bmap
->div
[div
][1 + j
]);
2104 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2105 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2107 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2110 int k
= isl_basic_map_alloc_inequality(bmap
);
2113 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2114 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2115 bmap
->div
[div
][1 + j
]);
2116 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2117 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2119 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2122 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2125 isl_basic_map_free(bmap
);
2129 /* This function is called right before "div" is eliminated from "bmap"
2130 * using Fourier-Motzkin.
2131 * Look through the constraints of "bmap" for constraints on the argument
2132 * of the integer division and use them to construct constraints on the
2133 * integer division itself. These constraints can then be combined
2134 * during the Fourier-Motzkin elimination.
2135 * Note that it is only useful to introduce lower bounds on "div"
2136 * if "bmap" already contains upper bounds on "div" as the newly
2137 * introduce lower bounds can then be combined with the pre-existing
2138 * upper bounds. Similarly for upper bounds.
2139 * We therefore first check if "bmap" contains any lower and/or upper bounds
2142 * It is interesting to note that the introduction of these constraints
2143 * can indeed lead to more accurate results, even when compared to
2144 * deriving constraints on the argument of "div" from constraints on "div".
2145 * Consider, for example, the set
2147 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2149 * The second constraint can be rewritten as
2151 * 2 * [(-i-2j+3)/4] + k >= 0
2153 * from which we can derive
2155 * -i - 2j + 3 >= -2k
2161 * Combined with the first constraint, we obtain
2163 * -3 <= 3 + 2k or k >= -3
2165 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2166 * the first constraint, we obtain
2168 * [(i + 2j)/4] >= [-3/4] = -1
2170 * Combining this constraint with the second constraint, we obtain
2174 static __isl_give isl_basic_map
*insert_bounds_on_div(
2175 __isl_take isl_basic_map
*bmap
, int div
)
2178 int check_lb
, check_ub
;
2185 if (isl_int_is_zero(bmap
->div
[div
][0]))
2188 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2192 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2193 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2200 if (!check_lb
&& !check_ub
)
2205 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2206 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2209 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2210 check_lb
, check_ub
);
2218 /* Remove all divs (recursively) involving any of the given dimensions
2219 * in their definitions.
2221 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2222 __isl_take isl_basic_map
*bmap
,
2223 enum isl_dim_type type
, unsigned first
, unsigned n
)
2229 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2231 first
+= isl_basic_map_offset(bmap
, type
);
2233 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2234 if (!div_involves_vars(bmap
, i
, first
, n
))
2236 bmap
= insert_bounds_on_div(bmap
, i
);
2237 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2245 isl_basic_map_free(bmap
);
2249 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2250 __isl_take isl_basic_set
*bset
,
2251 enum isl_dim_type type
, unsigned first
, unsigned n
)
2253 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2256 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2257 enum isl_dim_type type
, unsigned first
, unsigned n
)
2266 map
= isl_map_cow(map
);
2270 for (i
= 0; i
< map
->n
; ++i
) {
2271 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2282 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2283 enum isl_dim_type type
, unsigned first
, unsigned n
)
2285 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2289 /* Does the desciption of "bmap" depend on the specified dimensions?
2290 * We also check whether the dimensions appear in any of the div definitions.
2291 * In principle there is no need for this check. If the dimensions appear
2292 * in a div definition, they also appear in the defining constraints of that
2295 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2296 enum isl_dim_type type
, unsigned first
, unsigned n
)
2301 return isl_bool_error
;
2303 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2304 isl_die(bmap
->ctx
, isl_error_invalid
,
2305 "index out of bounds", return isl_bool_error
);
2307 first
+= isl_basic_map_offset(bmap
, type
);
2308 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2309 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2310 return isl_bool_true
;
2311 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2312 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2313 return isl_bool_true
;
2314 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2315 if (isl_int_is_zero(bmap
->div
[i
][0]))
2317 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2318 return isl_bool_true
;
2321 return isl_bool_false
;
2324 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2325 enum isl_dim_type type
, unsigned first
, unsigned n
)
2330 return isl_bool_error
;
2332 if (first
+ n
> isl_map_dim(map
, type
))
2333 isl_die(map
->ctx
, isl_error_invalid
,
2334 "index out of bounds", return isl_bool_error
);
2336 for (i
= 0; i
< map
->n
; ++i
) {
2337 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2339 if (involves
< 0 || involves
)
2343 return isl_bool_false
;
2346 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2347 enum isl_dim_type type
, unsigned first
, unsigned n
)
2349 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2352 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2353 enum isl_dim_type type
, unsigned first
, unsigned n
)
2355 return isl_map_involves_dims(set
, type
, first
, n
);
2358 /* Does local variable "div" of "bmap" have a complete explicit representation?
2359 * Having a complete explicit representation requires not only
2360 * an explicit representation, but also that all local variables
2361 * that appear in this explicit representation in turn have
2362 * a complete explicit representation.
2364 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2367 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2370 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2371 if (marked
< 0 || marked
)
2372 return isl_bool_not(marked
);
2374 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2377 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2379 known
= isl_basic_map_div_is_known(bmap
, i
);
2380 if (known
< 0 || !known
)
2384 return isl_bool_true
;
2387 /* Does local variable "div" of "bset" have a complete explicit representation?
2389 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2391 return isl_basic_map_div_is_known(bset
, div
);
2394 /* Remove all divs that are unknown or defined in terms of unknown divs.
2396 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2397 __isl_take isl_basic_map
*bmap
)
2404 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2405 if (isl_basic_map_div_is_known(bmap
, i
))
2407 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2416 /* Remove all divs that are unknown or defined in terms of unknown divs.
2418 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2419 __isl_take isl_basic_set
*bset
)
2421 return isl_basic_map_remove_unknown_divs(bset
);
2424 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2433 map
= isl_map_cow(map
);
2437 for (i
= 0; i
< map
->n
; ++i
) {
2438 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2448 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2450 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2453 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2454 __isl_take isl_basic_set
*bset
,
2455 enum isl_dim_type type
, unsigned first
, unsigned n
)
2457 return (isl_basic_set
*)
2458 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2461 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2462 enum isl_dim_type type
, unsigned first
, unsigned n
)
2469 map
= isl_map_cow(map
);
2472 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2474 for (i
= 0; i
< map
->n
; ++i
) {
2475 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2476 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2480 map
= isl_map_drop(map
, type
, first
, n
);
2487 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2488 enum isl_dim_type type
, unsigned first
, unsigned n
)
2490 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2493 /* Project out n inputs starting at first using Fourier-Motzkin */
2494 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2495 unsigned first
, unsigned n
)
2497 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2500 static void dump_term(struct isl_basic_map
*bmap
,
2501 isl_int c
, int pos
, FILE *out
)
2504 unsigned in
= isl_basic_map_n_in(bmap
);
2505 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2506 unsigned nparam
= isl_basic_map_n_param(bmap
);
2508 isl_int_print(out
, c
, 0);
2510 if (!isl_int_is_one(c
))
2511 isl_int_print(out
, c
, 0);
2512 if (pos
< 1 + nparam
) {
2513 name
= isl_space_get_dim_name(bmap
->dim
,
2514 isl_dim_param
, pos
- 1);
2516 fprintf(out
, "%s", name
);
2518 fprintf(out
, "p%d", pos
- 1);
2519 } else if (pos
< 1 + nparam
+ in
)
2520 fprintf(out
, "i%d", pos
- 1 - nparam
);
2521 else if (pos
< 1 + nparam
+ dim
)
2522 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2524 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2528 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2529 int sign
, FILE *out
)
2533 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2537 for (i
= 0, first
= 1; i
< len
; ++i
) {
2538 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2541 fprintf(out
, " + ");
2543 isl_int_abs(v
, c
[i
]);
2544 dump_term(bmap
, v
, i
, out
);
2551 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2552 const char *op
, FILE *out
, int indent
)
2556 fprintf(out
, "%*s", indent
, "");
2558 dump_constraint_sign(bmap
, c
, 1, out
);
2559 fprintf(out
, " %s ", op
);
2560 dump_constraint_sign(bmap
, c
, -1, out
);
2564 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2565 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2567 fprintf(out
, "%*s", indent
, "");
2568 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2573 static void dump_constraints(struct isl_basic_map
*bmap
,
2574 isl_int
**c
, unsigned n
,
2575 const char *op
, FILE *out
, int indent
)
2579 for (i
= 0; i
< n
; ++i
)
2580 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2583 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2587 unsigned total
= isl_basic_map_total_dim(bmap
);
2589 for (j
= 0; j
< 1 + total
; ++j
) {
2590 if (isl_int_is_zero(exp
[j
]))
2592 if (!first
&& isl_int_is_pos(exp
[j
]))
2594 dump_term(bmap
, exp
[j
], j
, out
);
2599 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2603 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2604 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2606 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2607 fprintf(out
, "%*s", indent
, "");
2608 fprintf(out
, "e%d = [(", i
);
2609 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2611 isl_int_print(out
, bmap
->div
[i
][0], 0);
2612 fprintf(out
, "]\n");
2616 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2617 FILE *out
, int indent
)
2620 fprintf(out
, "null basic set\n");
2624 fprintf(out
, "%*s", indent
, "");
2625 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2626 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2627 bset
->extra
, bset
->flags
);
2628 dump((struct isl_basic_map
*)bset
, out
, indent
);
2631 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2632 FILE *out
, int indent
)
2635 fprintf(out
, "null basic map\n");
2639 fprintf(out
, "%*s", indent
, "");
2640 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2641 "flags: %x, n_name: %d\n",
2643 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2644 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2645 dump(bmap
, out
, indent
);
2648 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2653 total
= isl_basic_map_total_dim(bmap
);
2654 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2655 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2656 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2657 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2661 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2666 if (isl_space_dim(space
, isl_dim_in
) != 0)
2667 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2668 "set cannot have input dimensions", goto error
);
2669 return isl_map_alloc_space(space
, n
, flags
);
2671 isl_space_free(space
);
2675 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2676 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2678 struct isl_set
*set
;
2681 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2685 set
= isl_set_alloc_space(dims
, n
, flags
);
2689 /* Make sure "map" has room for at least "n" more basic maps.
2691 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2694 struct isl_map
*grown
= NULL
;
2698 isl_assert(map
->ctx
, n
>= 0, goto error
);
2699 if (map
->n
+ n
<= map
->size
)
2701 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2704 for (i
= 0; i
< map
->n
; ++i
) {
2705 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2713 isl_map_free(grown
);
2718 /* Make sure "set" has room for at least "n" more basic sets.
2720 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2722 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2725 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2728 struct isl_set
*dup
;
2733 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2736 for (i
= 0; i
< set
->n
; ++i
)
2737 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2741 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2743 return isl_map_from_basic_map(bset
);
2746 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2748 struct isl_map
*map
;
2753 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2754 return isl_map_add_basic_map(map
, bmap
);
2757 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2758 __isl_take isl_basic_set
*bset
)
2760 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2761 (struct isl_basic_map
*)bset
);
2764 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2766 return isl_map_free(set
);
2769 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2774 fprintf(out
, "null set\n");
2778 fprintf(out
, "%*s", indent
, "");
2779 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2780 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2782 for (i
= 0; i
< set
->n
; ++i
) {
2783 fprintf(out
, "%*s", indent
, "");
2784 fprintf(out
, "basic set %d:\n", i
);
2785 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2789 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2794 fprintf(out
, "null map\n");
2798 fprintf(out
, "%*s", indent
, "");
2799 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2800 "flags: %x, n_name: %d\n",
2801 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2802 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2803 for (i
= 0; i
< map
->n
; ++i
) {
2804 fprintf(out
, "%*s", indent
, "");
2805 fprintf(out
, "basic map %d:\n", i
);
2806 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2810 struct isl_basic_map
*isl_basic_map_intersect_domain(
2811 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2813 struct isl_basic_map
*bmap_domain
;
2818 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2819 bset
->dim
, isl_dim_param
), goto error
);
2821 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2822 isl_assert(bset
->ctx
,
2823 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2825 bmap
= isl_basic_map_cow(bmap
);
2828 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2829 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2830 bmap_domain
= isl_basic_map_from_domain(bset
);
2831 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2833 bmap
= isl_basic_map_simplify(bmap
);
2834 return isl_basic_map_finalize(bmap
);
2836 isl_basic_map_free(bmap
);
2837 isl_basic_set_free(bset
);
2841 struct isl_basic_map
*isl_basic_map_intersect_range(
2842 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2844 struct isl_basic_map
*bmap_range
;
2849 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2850 bset
->dim
, isl_dim_param
), goto error
);
2852 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2853 isl_assert(bset
->ctx
,
2854 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2856 if (isl_basic_set_plain_is_universe(bset
)) {
2857 isl_basic_set_free(bset
);
2861 bmap
= isl_basic_map_cow(bmap
);
2864 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2865 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2866 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2867 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2869 bmap
= isl_basic_map_simplify(bmap
);
2870 return isl_basic_map_finalize(bmap
);
2872 isl_basic_map_free(bmap
);
2873 isl_basic_set_free(bset
);
2877 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2878 __isl_keep isl_vec
*vec
)
2885 return isl_bool_error
;
2887 total
= 1 + isl_basic_map_total_dim(bmap
);
2888 if (total
!= vec
->size
)
2889 return isl_bool_error
;
2893 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2894 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2895 if (!isl_int_is_zero(s
)) {
2897 return isl_bool_false
;
2901 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2902 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2903 if (isl_int_is_neg(s
)) {
2905 return isl_bool_false
;
2911 return isl_bool_true
;
2914 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2915 __isl_keep isl_vec
*vec
)
2917 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2920 struct isl_basic_map
*isl_basic_map_intersect(
2921 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2923 struct isl_vec
*sample
= NULL
;
2925 if (!bmap1
|| !bmap2
)
2928 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2929 bmap2
->dim
, isl_dim_param
), goto error
);
2930 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2931 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2932 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2933 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2934 return isl_basic_map_intersect(bmap2
, bmap1
);
2936 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2937 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2938 isl_assert(bmap1
->ctx
,
2939 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2941 if (isl_basic_map_plain_is_empty(bmap1
)) {
2942 isl_basic_map_free(bmap2
);
2945 if (isl_basic_map_plain_is_empty(bmap2
)) {
2946 isl_basic_map_free(bmap1
);
2950 if (bmap1
->sample
&&
2951 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2952 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2953 sample
= isl_vec_copy(bmap1
->sample
);
2954 else if (bmap2
->sample
&&
2955 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2956 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2957 sample
= isl_vec_copy(bmap2
->sample
);
2959 bmap1
= isl_basic_map_cow(bmap1
);
2962 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2963 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2964 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2967 isl_vec_free(sample
);
2969 isl_vec_free(bmap1
->sample
);
2970 bmap1
->sample
= sample
;
2973 bmap1
= isl_basic_map_simplify(bmap1
);
2974 return isl_basic_map_finalize(bmap1
);
2977 isl_vec_free(sample
);
2978 isl_basic_map_free(bmap1
);
2979 isl_basic_map_free(bmap2
);
2983 struct isl_basic_set
*isl_basic_set_intersect(
2984 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2986 return (struct isl_basic_set
*)
2987 isl_basic_map_intersect(
2988 (struct isl_basic_map
*)bset1
,
2989 (struct isl_basic_map
*)bset2
);
2992 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2993 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2995 return isl_basic_set_intersect(bset1
, bset2
);
2998 /* Special case of isl_map_intersect, where both map1 and map2
2999 * are convex, without any divs and such that either map1 or map2
3000 * contains a single constraint. This constraint is then simply
3001 * added to the other map.
3003 static __isl_give isl_map
*map_intersect_add_constraint(
3004 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3006 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3007 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3008 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3009 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3011 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3012 return isl_map_intersect(map2
, map1
);
3014 isl_assert(map2
->ctx
,
3015 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3017 map1
= isl_map_cow(map1
);
3020 if (isl_map_plain_is_empty(map1
)) {
3024 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3025 if (map2
->p
[0]->n_eq
== 1)
3026 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3028 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3029 map2
->p
[0]->ineq
[0]);
3031 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3032 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3036 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3037 isl_basic_map_free(map1
->p
[0]);
3050 /* map2 may be either a parameter domain or a map living in the same
3053 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3054 __isl_take isl_map
*map2
)
3063 if ((isl_map_plain_is_empty(map1
) ||
3064 isl_map_plain_is_universe(map2
)) &&
3065 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3069 if ((isl_map_plain_is_empty(map2
) ||
3070 isl_map_plain_is_universe(map1
)) &&
3071 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3076 if (map1
->n
== 1 && map2
->n
== 1 &&
3077 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3078 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3079 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3080 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3081 return map_intersect_add_constraint(map1
, map2
);
3083 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3084 isl_space_dim(map2
->dim
, isl_dim_param
))
3085 isl_assert(map1
->ctx
,
3086 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3088 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3089 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3090 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3092 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3093 map1
->n
* map2
->n
, flags
);
3096 for (i
= 0; i
< map1
->n
; ++i
)
3097 for (j
= 0; j
< map2
->n
; ++j
) {
3098 struct isl_basic_map
*part
;
3099 part
= isl_basic_map_intersect(
3100 isl_basic_map_copy(map1
->p
[i
]),
3101 isl_basic_map_copy(map2
->p
[j
]));
3102 if (isl_basic_map_is_empty(part
) < 0)
3103 part
= isl_basic_map_free(part
);
3104 result
= isl_map_add_basic_map(result
, part
);
3117 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3118 __isl_take isl_map
*map2
)
3122 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3123 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3124 "spaces don't match", goto error
);
3125 return map_intersect_internal(map1
, map2
);
3132 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3133 __isl_take isl_map
*map2
)
3135 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3138 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3140 return (struct isl_set
*)
3141 isl_map_intersect((struct isl_map
*)set1
,
3142 (struct isl_map
*)set2
);
3145 /* map_intersect_internal accepts intersections
3146 * with parameter domains, so we can just call that function.
3148 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3149 __isl_take isl_set
*params
)
3151 return map_intersect_internal(map
, params
);
3154 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3155 __isl_take isl_map
*map2
)
3157 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3160 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3161 __isl_take isl_set
*params
)
3163 return isl_map_intersect_params(set
, params
);
3166 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3169 unsigned pos
, n1
, n2
;
3173 bmap
= isl_basic_map_cow(bmap
);
3176 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3177 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3178 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3179 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3180 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3181 return isl_basic_map_reset_space(bmap
, space
);
3184 static __isl_give isl_basic_map
*basic_map_space_reset(
3185 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3191 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3194 space
= isl_basic_map_get_space(bmap
);
3195 space
= isl_space_reset(space
, type
);
3196 bmap
= isl_basic_map_reset_space(bmap
, space
);
3200 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3201 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3202 unsigned pos
, unsigned n
)
3205 struct isl_basic_map
*res
;
3206 struct isl_dim_map
*dim_map
;
3207 unsigned total
, off
;
3208 enum isl_dim_type t
;
3211 return basic_map_space_reset(bmap
, type
);
3216 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3218 total
= isl_basic_map_total_dim(bmap
) + n
;
3219 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3221 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3223 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3225 unsigned size
= isl_basic_map_dim(bmap
, t
);
3226 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3228 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3229 pos
, size
- pos
, off
+ pos
+ n
);
3231 off
+= isl_space_dim(res_dim
, t
);
3233 isl_dim_map_div(dim_map
, bmap
, off
);
3235 res
= isl_basic_map_alloc_space(res_dim
,
3236 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3237 if (isl_basic_map_is_rational(bmap
))
3238 res
= isl_basic_map_set_rational(res
);
3239 if (isl_basic_map_plain_is_empty(bmap
)) {
3240 isl_basic_map_free(bmap
);
3242 return isl_basic_map_set_to_empty(res
);
3244 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3245 return isl_basic_map_finalize(res
);
3248 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3249 __isl_take isl_basic_set
*bset
,
3250 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3252 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3255 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3256 enum isl_dim_type type
, unsigned n
)
3260 return isl_basic_map_insert_dims(bmap
, type
,
3261 isl_basic_map_dim(bmap
, type
), n
);
3264 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3265 enum isl_dim_type type
, unsigned n
)
3269 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3270 return isl_basic_map_add_dims(bset
, type
, n
);
3272 isl_basic_set_free(bset
);
3276 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3277 enum isl_dim_type type
)
3281 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3284 space
= isl_map_get_space(map
);
3285 space
= isl_space_reset(space
, type
);
3286 map
= isl_map_reset_space(map
, space
);
3290 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3291 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3296 return map_space_reset(map
, type
);
3298 map
= isl_map_cow(map
);
3302 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3306 for (i
= 0; i
< map
->n
; ++i
) {
3307 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3318 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3319 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3321 return isl_map_insert_dims(set
, type
, pos
, n
);
3324 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3325 enum isl_dim_type type
, unsigned n
)
3329 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3332 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3333 enum isl_dim_type type
, unsigned n
)
3337 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3338 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3344 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3345 __isl_take isl_basic_map
*bmap
,
3346 enum isl_dim_type dst_type
, unsigned dst_pos
,
3347 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3349 struct isl_dim_map
*dim_map
;
3350 struct isl_basic_map
*res
;
3351 enum isl_dim_type t
;
3352 unsigned total
, off
;
3359 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3362 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3365 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3367 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3368 pos(bmap
->dim
, src_type
) + src_pos
+
3369 ((src_type
< dst_type
) ? n
: 0)) {
3370 bmap
= isl_basic_map_cow(bmap
);
3374 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3375 src_type
, src_pos
, n
);
3379 bmap
= isl_basic_map_finalize(bmap
);
3384 total
= isl_basic_map_total_dim(bmap
);
3385 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3388 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3389 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3390 if (t
== dst_type
) {
3391 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3394 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3397 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3398 dst_pos
, size
- dst_pos
, off
);
3399 off
+= size
- dst_pos
;
3400 } else if (t
== src_type
) {
3401 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3404 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3405 src_pos
+ n
, size
- src_pos
- n
, off
);
3406 off
+= size
- src_pos
- n
;
3408 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3412 isl_dim_map_div(dim_map
, bmap
, off
);
3414 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3415 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3416 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3420 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3421 src_type
, src_pos
, n
);
3425 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3426 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3427 bmap
= isl_basic_map_finalize(bmap
);
3431 isl_basic_map_free(bmap
);
3435 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3436 enum isl_dim_type dst_type
, unsigned dst_pos
,
3437 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3439 return (isl_basic_set
*)isl_basic_map_move_dims(
3440 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3443 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3444 enum isl_dim_type dst_type
, unsigned dst_pos
,
3445 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3449 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3450 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3451 src_type
, src_pos
, n
);
3457 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3458 enum isl_dim_type dst_type
, unsigned dst_pos
,
3459 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3468 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3471 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3474 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3476 map
= isl_map_cow(map
);
3480 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3484 for (i
= 0; i
< map
->n
; ++i
) {
3485 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3487 src_type
, src_pos
, n
);
3498 /* Move the specified dimensions to the last columns right before
3499 * the divs. Don't change the dimension specification of bmap.
3500 * That's the responsibility of the caller.
3502 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3503 enum isl_dim_type type
, unsigned first
, unsigned n
)
3505 struct isl_dim_map
*dim_map
;
3506 struct isl_basic_map
*res
;
3507 enum isl_dim_type t
;
3508 unsigned total
, off
;
3512 if (pos(bmap
->dim
, type
) + first
+ n
==
3513 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3516 total
= isl_basic_map_total_dim(bmap
);
3517 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3520 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3521 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3523 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3526 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3527 first
, n
, total
- bmap
->n_div
- n
);
3528 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3529 first
+ n
, size
- (first
+ n
), off
);
3530 off
+= size
- (first
+ n
);
3532 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3536 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3538 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3539 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3540 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3544 /* Insert "n" rows in the divs of "bmap".
3546 * The number of columns is not changed, which means that the last
3547 * dimensions of "bmap" are being reintepreted as the new divs.
3548 * The space of "bmap" is not adjusted, however, which means
3549 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3550 * from the space of "bmap" is the responsibility of the caller.
3552 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3560 bmap
= isl_basic_map_cow(bmap
);
3564 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3565 old
= bmap
->block2
.data
;
3566 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3567 (bmap
->extra
+ n
) * (1 + row_size
));
3568 if (!bmap
->block2
.data
)
3569 return isl_basic_map_free(bmap
);
3570 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3572 return isl_basic_map_free(bmap
);
3573 for (i
= 0; i
< n
; ++i
) {
3574 new_div
[i
] = bmap
->block2
.data
+
3575 (bmap
->extra
+ i
) * (1 + row_size
);
3576 isl_seq_clr(new_div
[i
], 1 + row_size
);
3578 for (i
= 0; i
< bmap
->extra
; ++i
)
3579 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3581 bmap
->div
= new_div
;
3588 /* Drop constraints from "bmap" that only involve the variables
3589 * of "type" in the range [first, first + n] that are not related
3590 * to any of the variables outside that interval.
3591 * These constraints cannot influence the values for the variables
3592 * outside the interval, except in case they cause "bmap" to be empty.
3593 * Only drop the constraints if "bmap" is known to be non-empty.
3595 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3596 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3597 unsigned first
, unsigned n
)
3601 unsigned dim
, n_div
;
3604 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3606 return isl_basic_map_free(bmap
);
3610 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3611 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3612 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3614 return isl_basic_map_free(bmap
);
3615 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3616 for (i
= 0; i
< first
; ++i
)
3618 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3621 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3626 /* Turn the n dimensions of type type, starting at first
3627 * into existentially quantified variables.
3629 * If a subset of the projected out variables are unrelated
3630 * to any of the variables that remain, then the constraints
3631 * involving this subset are simply dropped first.
3633 __isl_give isl_basic_map
*isl_basic_map_project_out(
3634 __isl_take isl_basic_map
*bmap
,
3635 enum isl_dim_type type
, unsigned first
, unsigned n
)
3638 return basic_map_space_reset(bmap
, type
);
3639 if (type
== isl_dim_div
)
3640 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3641 "cannot project out existentially quantified variables",
3642 return isl_basic_map_free(bmap
));
3644 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3648 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3649 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3651 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3654 bmap
= move_last(bmap
, type
, first
, n
);
3655 bmap
= isl_basic_map_cow(bmap
);
3656 bmap
= insert_div_rows(bmap
, n
);
3660 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3663 bmap
= isl_basic_map_simplify(bmap
);
3664 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3665 return isl_basic_map_finalize(bmap
);
3667 isl_basic_map_free(bmap
);
3671 /* Turn the n dimensions of type type, starting at first
3672 * into existentially quantified variables.
3674 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3675 enum isl_dim_type type
, unsigned first
, unsigned n
)
3677 return (isl_basic_set
*)isl_basic_map_project_out(
3678 (isl_basic_map
*)bset
, type
, first
, n
);
3681 /* Turn the n dimensions of type type, starting at first
3682 * into existentially quantified variables.
3684 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3685 enum isl_dim_type type
, unsigned first
, unsigned n
)
3693 return map_space_reset(map
, type
);
3695 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3697 map
= isl_map_cow(map
);
3701 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3705 for (i
= 0; i
< map
->n
; ++i
) {
3706 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3717 /* Turn the n dimensions of type type, starting at first
3718 * into existentially quantified variables.
3720 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3721 enum isl_dim_type type
, unsigned first
, unsigned n
)
3723 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3726 /* Return a map that projects the elements in "set" onto their
3727 * "n" set dimensions starting at "first".
3728 * "type" should be equal to isl_dim_set.
3730 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3731 enum isl_dim_type type
, unsigned first
, unsigned n
)
3739 if (type
!= isl_dim_set
)
3740 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3741 "only set dimensions can be projected out", goto error
);
3742 dim
= isl_set_dim(set
, isl_dim_set
);
3743 if (first
+ n
> dim
|| first
+ n
< first
)
3744 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3745 "index out of bounds", goto error
);
3747 map
= isl_map_from_domain(set
);
3748 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3749 for (i
= 0; i
< n
; ++i
)
3750 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3758 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3762 for (i
= 0; i
< n
; ++i
) {
3763 j
= isl_basic_map_alloc_div(bmap
);
3766 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3770 isl_basic_map_free(bmap
);
3774 struct isl_basic_map
*isl_basic_map_apply_range(
3775 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3777 isl_space
*dim_result
= NULL
;
3778 struct isl_basic_map
*bmap
;
3779 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3780 struct isl_dim_map
*dim_map1
, *dim_map2
;
3782 if (!bmap1
|| !bmap2
)
3784 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3785 bmap2
->dim
, isl_dim_param
))
3786 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3787 "parameters don't match", goto error
);
3788 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3789 bmap2
->dim
, isl_dim_in
))
3790 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3791 "spaces don't match", goto error
);
3793 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3794 isl_space_copy(bmap2
->dim
));
3796 n_in
= isl_basic_map_n_in(bmap1
);
3797 n_out
= isl_basic_map_n_out(bmap2
);
3798 n
= isl_basic_map_n_out(bmap1
);
3799 nparam
= isl_basic_map_n_param(bmap1
);
3801 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3802 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3803 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3804 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3805 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3806 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3807 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3808 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3809 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3810 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3811 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3813 bmap
= isl_basic_map_alloc_space(dim_result
,
3814 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3815 bmap1
->n_eq
+ bmap2
->n_eq
,
3816 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3817 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3818 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3819 bmap
= add_divs(bmap
, n
);
3820 bmap
= isl_basic_map_simplify(bmap
);
3821 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3822 return isl_basic_map_finalize(bmap
);
3824 isl_basic_map_free(bmap1
);
3825 isl_basic_map_free(bmap2
);
3829 struct isl_basic_set
*isl_basic_set_apply(
3830 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3835 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3838 return (struct isl_basic_set
*)
3839 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3841 isl_basic_set_free(bset
);
3842 isl_basic_map_free(bmap
);
3846 struct isl_basic_map
*isl_basic_map_apply_domain(
3847 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3849 if (!bmap1
|| !bmap2
)
3852 isl_assert(bmap1
->ctx
,
3853 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3854 isl_assert(bmap1
->ctx
,
3855 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3858 bmap1
= isl_basic_map_reverse(bmap1
);
3859 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3860 return isl_basic_map_reverse(bmap1
);
3862 isl_basic_map_free(bmap1
);
3863 isl_basic_map_free(bmap2
);
3867 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3868 * A \cap B -> f(A) + f(B)
3870 struct isl_basic_map
*isl_basic_map_sum(
3871 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3873 unsigned n_in
, n_out
, nparam
, total
, pos
;
3874 struct isl_basic_map
*bmap
= NULL
;
3875 struct isl_dim_map
*dim_map1
, *dim_map2
;
3878 if (!bmap1
|| !bmap2
)
3881 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3884 nparam
= isl_basic_map_n_param(bmap1
);
3885 n_in
= isl_basic_map_n_in(bmap1
);
3886 n_out
= isl_basic_map_n_out(bmap1
);
3888 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3889 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3890 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3891 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3892 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3895 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3896 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3897 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3898 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3900 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3901 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3902 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3903 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3904 for (i
= 0; i
< n_out
; ++i
) {
3905 int j
= isl_basic_map_alloc_equality(bmap
);
3908 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3909 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3910 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3911 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3913 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3914 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3915 bmap
= add_divs(bmap
, 2 * n_out
);
3917 bmap
= isl_basic_map_simplify(bmap
);
3918 return isl_basic_map_finalize(bmap
);
3920 isl_basic_map_free(bmap
);
3921 isl_basic_map_free(bmap1
);
3922 isl_basic_map_free(bmap2
);
3926 /* Given two maps A -> f(A) and B -> g(B), construct a map
3927 * A \cap B -> f(A) + f(B)
3929 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3931 struct isl_map
*result
;
3937 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3939 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3940 map1
->n
* map2
->n
, 0);
3943 for (i
= 0; i
< map1
->n
; ++i
)
3944 for (j
= 0; j
< map2
->n
; ++j
) {
3945 struct isl_basic_map
*part
;
3946 part
= isl_basic_map_sum(
3947 isl_basic_map_copy(map1
->p
[i
]),
3948 isl_basic_map_copy(map2
->p
[j
]));
3949 if (isl_basic_map_is_empty(part
))
3950 isl_basic_map_free(part
);
3952 result
= isl_map_add_basic_map(result
, part
);
3965 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3966 __isl_take isl_set
*set2
)
3968 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3971 /* Given a basic map A -> f(A), construct A -> -f(A).
3973 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3978 bmap
= isl_basic_map_cow(bmap
);
3982 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3983 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3984 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3985 for (j
= 0; j
< n
; ++j
)
3986 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3987 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3988 for (j
= 0; j
< n
; ++j
)
3989 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3990 for (i
= 0; i
< bmap
->n_div
; ++i
)
3991 for (j
= 0; j
< n
; ++j
)
3992 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3993 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3994 return isl_basic_map_finalize(bmap
);
3997 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3999 return isl_basic_map_neg(bset
);
4002 /* Given a map A -> f(A), construct A -> -f(A).
4004 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4008 map
= isl_map_cow(map
);
4012 for (i
= 0; i
< map
->n
; ++i
) {
4013 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4024 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4026 return (isl_set
*)isl_map_neg((isl_map
*)set
);
4029 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4030 * A -> floor(f(A)/d).
4032 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4035 unsigned n_in
, n_out
, nparam
, total
, pos
;
4036 struct isl_basic_map
*result
= NULL
;
4037 struct isl_dim_map
*dim_map
;
4043 nparam
= isl_basic_map_n_param(bmap
);
4044 n_in
= isl_basic_map_n_in(bmap
);
4045 n_out
= isl_basic_map_n_out(bmap
);
4047 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4048 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4049 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4050 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4051 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4052 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4054 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4055 bmap
->n_div
+ n_out
,
4056 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4057 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4058 result
= add_divs(result
, n_out
);
4059 for (i
= 0; i
< n_out
; ++i
) {
4061 j
= isl_basic_map_alloc_inequality(result
);
4064 isl_seq_clr(result
->ineq
[j
], 1+total
);
4065 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4066 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4067 j
= isl_basic_map_alloc_inequality(result
);
4070 isl_seq_clr(result
->ineq
[j
], 1+total
);
4071 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4072 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4073 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4076 result
= isl_basic_map_simplify(result
);
4077 return isl_basic_map_finalize(result
);
4079 isl_basic_map_free(result
);
4083 /* Given a map A -> f(A) and an integer d, construct a map
4084 * A -> floor(f(A)/d).
4086 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4090 map
= isl_map_cow(map
);
4094 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4095 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4096 for (i
= 0; i
< map
->n
; ++i
) {
4097 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4108 /* Given a map A -> f(A) and an integer d, construct a map
4109 * A -> floor(f(A)/d).
4111 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4112 __isl_take isl_val
*d
)
4116 if (!isl_val_is_int(d
))
4117 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4118 "expecting integer denominator", goto error
);
4119 map
= isl_map_floordiv(map
, d
->n
);
4128 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4134 i
= isl_basic_map_alloc_equality(bmap
);
4137 nparam
= isl_basic_map_n_param(bmap
);
4138 n_in
= isl_basic_map_n_in(bmap
);
4139 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4140 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4141 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4142 return isl_basic_map_finalize(bmap
);
4144 isl_basic_map_free(bmap
);
4148 /* Add a constraints to "bmap" expressing i_pos < o_pos
4150 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4156 i
= isl_basic_map_alloc_inequality(bmap
);
4159 nparam
= isl_basic_map_n_param(bmap
);
4160 n_in
= isl_basic_map_n_in(bmap
);
4161 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4162 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4163 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4164 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4165 return isl_basic_map_finalize(bmap
);
4167 isl_basic_map_free(bmap
);
4171 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4173 static __isl_give isl_basic_map
*var_less_or_equal(
4174 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4180 i
= isl_basic_map_alloc_inequality(bmap
);
4183 nparam
= isl_basic_map_n_param(bmap
);
4184 n_in
= isl_basic_map_n_in(bmap
);
4185 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4186 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4187 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4188 return isl_basic_map_finalize(bmap
);
4190 isl_basic_map_free(bmap
);
4194 /* Add a constraints to "bmap" expressing i_pos > o_pos
4196 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4202 i
= isl_basic_map_alloc_inequality(bmap
);
4205 nparam
= isl_basic_map_n_param(bmap
);
4206 n_in
= isl_basic_map_n_in(bmap
);
4207 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4208 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4209 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4210 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4211 return isl_basic_map_finalize(bmap
);
4213 isl_basic_map_free(bmap
);
4217 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4219 static __isl_give isl_basic_map
*var_more_or_equal(
4220 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4226 i
= isl_basic_map_alloc_inequality(bmap
);
4229 nparam
= isl_basic_map_n_param(bmap
);
4230 n_in
= isl_basic_map_n_in(bmap
);
4231 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4232 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4233 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4234 return isl_basic_map_finalize(bmap
);
4236 isl_basic_map_free(bmap
);
4240 __isl_give isl_basic_map
*isl_basic_map_equal(
4241 __isl_take isl_space
*dim
, unsigned n_equal
)
4244 struct isl_basic_map
*bmap
;
4245 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4248 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4249 bmap
= var_equal(bmap
, i
);
4250 return isl_basic_map_finalize(bmap
);
4253 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4255 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4259 struct isl_basic_map
*bmap
;
4260 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4263 for (i
= 0; i
< pos
&& bmap
; ++i
)
4264 bmap
= var_equal(bmap
, i
);
4266 bmap
= var_less(bmap
, pos
);
4267 return isl_basic_map_finalize(bmap
);
4270 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4272 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4273 __isl_take isl_space
*dim
, unsigned pos
)
4276 isl_basic_map
*bmap
;
4278 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4279 for (i
= 0; i
< pos
; ++i
)
4280 bmap
= var_equal(bmap
, i
);
4281 bmap
= var_less_or_equal(bmap
, pos
);
4282 return isl_basic_map_finalize(bmap
);
4285 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4287 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4291 struct isl_basic_map
*bmap
;
4292 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4295 for (i
= 0; i
< pos
&& bmap
; ++i
)
4296 bmap
= var_equal(bmap
, i
);
4298 bmap
= var_more(bmap
, pos
);
4299 return isl_basic_map_finalize(bmap
);
4302 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4304 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4305 __isl_take isl_space
*dim
, unsigned pos
)
4308 isl_basic_map
*bmap
;
4310 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4311 for (i
= 0; i
< pos
; ++i
)
4312 bmap
= var_equal(bmap
, i
);
4313 bmap
= var_more_or_equal(bmap
, pos
);
4314 return isl_basic_map_finalize(bmap
);
4317 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4318 unsigned n
, int equal
)
4320 struct isl_map
*map
;
4323 if (n
== 0 && equal
)
4324 return isl_map_universe(dims
);
4326 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4328 for (i
= 0; i
+ 1 < n
; ++i
)
4329 map
= isl_map_add_basic_map(map
,
4330 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4333 map
= isl_map_add_basic_map(map
,
4334 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4336 map
= isl_map_add_basic_map(map
,
4337 isl_basic_map_less_at(dims
, n
- 1));
4339 isl_space_free(dims
);
4344 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4348 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4351 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4353 return map_lex_lte_first(dim
, n
, 0);
4356 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4358 return map_lex_lte_first(dim
, n
, 1);
4361 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4363 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4366 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4368 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4371 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4372 unsigned n
, int equal
)
4374 struct isl_map
*map
;
4377 if (n
== 0 && equal
)
4378 return isl_map_universe(dims
);
4380 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4382 for (i
= 0; i
+ 1 < n
; ++i
)
4383 map
= isl_map_add_basic_map(map
,
4384 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4387 map
= isl_map_add_basic_map(map
,
4388 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4390 map
= isl_map_add_basic_map(map
,
4391 isl_basic_map_more_at(dims
, n
- 1));
4393 isl_space_free(dims
);
4398 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4402 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4405 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4407 return map_lex_gte_first(dim
, n
, 0);
4410 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4412 return map_lex_gte_first(dim
, n
, 1);
4415 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4417 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4420 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4422 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4425 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4426 __isl_take isl_set
*set2
)
4429 map
= isl_map_lex_le(isl_set_get_space(set1
));
4430 map
= isl_map_intersect_domain(map
, set1
);
4431 map
= isl_map_intersect_range(map
, set2
);
4435 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4436 __isl_take isl_set
*set2
)
4439 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4440 map
= isl_map_intersect_domain(map
, set1
);
4441 map
= isl_map_intersect_range(map
, set2
);
4445 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4446 __isl_take isl_set
*set2
)
4449 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4450 map
= isl_map_intersect_domain(map
, set1
);
4451 map
= isl_map_intersect_range(map
, set2
);
4455 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4456 __isl_take isl_set
*set2
)
4459 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4460 map
= isl_map_intersect_domain(map
, set1
);
4461 map
= isl_map_intersect_range(map
, set2
);
4465 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4466 __isl_take isl_map
*map2
)
4469 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4470 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4471 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4475 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4476 __isl_take isl_map
*map2
)
4479 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4480 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4481 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4485 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4486 __isl_take isl_map
*map2
)
4489 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4490 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4491 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4495 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4496 __isl_take isl_map
*map2
)
4499 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4500 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4501 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4505 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4506 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4508 struct isl_basic_map
*bmap
;
4510 bset
= isl_basic_set_cow(bset
);
4514 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4515 isl_space_free(bset
->dim
);
4516 bmap
= (struct isl_basic_map
*) bset
;
4518 return isl_basic_map_finalize(bmap
);
4520 isl_basic_set_free(bset
);
4521 isl_space_free(dim
);
4525 /* For a div d = floor(f/m), add the constraint
4529 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4530 unsigned pos
, isl_int
*div
)
4533 unsigned total
= isl_basic_map_total_dim(bmap
);
4535 i
= isl_basic_map_alloc_inequality(bmap
);
4538 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4539 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4544 /* For a div d = floor(f/m), add the constraint
4546 * -(f-(m-1)) + m d >= 0
4548 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4549 unsigned pos
, isl_int
*div
)
4552 unsigned total
= isl_basic_map_total_dim(bmap
);
4554 i
= isl_basic_map_alloc_inequality(bmap
);
4557 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4558 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4559 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4560 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4565 /* For a div d = floor(f/m), add the constraints
4568 * -(f-(m-1)) + m d >= 0
4570 * Note that the second constraint is the negation of
4574 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4575 unsigned pos
, isl_int
*div
)
4577 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4579 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4584 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4585 unsigned pos
, isl_int
*div
)
4587 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4591 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4593 unsigned total
= isl_basic_map_total_dim(bmap
);
4594 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4596 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4600 /* For each known div d = floor(f/m), add the constraints
4603 * -(f-(m-1)) + m d >= 0
4605 * Remove duplicate constraints in case of some these div constraints
4606 * already appear in "bmap".
4608 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4609 __isl_take isl_basic_map
*bmap
)
4615 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4619 bmap
= add_known_div_constraints(bmap
);
4620 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4621 bmap
= isl_basic_map_finalize(bmap
);
4625 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4627 * In particular, if this div is of the form d = floor(f/m),
4628 * then add the constraint
4632 * if sign < 0 or the constraint
4634 * -(f-(m-1)) + m d >= 0
4638 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4639 unsigned div
, int sign
)
4647 total
= isl_basic_map_total_dim(bmap
);
4648 div_pos
= total
- bmap
->n_div
+ div
;
4651 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4653 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4656 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4658 return isl_basic_map_add_div_constraints(bset
, div
);
4661 struct isl_basic_set
*isl_basic_map_underlying_set(
4662 struct isl_basic_map
*bmap
)
4666 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4668 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4669 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4670 return (struct isl_basic_set
*)bmap
;
4671 bmap
= isl_basic_map_cow(bmap
);
4674 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4677 bmap
->extra
-= bmap
->n_div
;
4679 bmap
= isl_basic_map_finalize(bmap
);
4680 return (struct isl_basic_set
*)bmap
;
4682 isl_basic_map_free(bmap
);
4686 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4687 __isl_take isl_basic_set
*bset
)
4689 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4692 /* Replace each element in "list" by the result of applying
4693 * isl_basic_map_underlying_set to the element.
4695 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4696 __isl_take isl_basic_map_list
*list
)
4703 n
= isl_basic_map_list_n_basic_map(list
);
4704 for (i
= 0; i
< n
; ++i
) {
4705 isl_basic_map
*bmap
;
4706 isl_basic_set
*bset
;
4708 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4709 bset
= isl_basic_set_underlying_set(bmap
);
4710 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4716 struct isl_basic_map
*isl_basic_map_overlying_set(
4717 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4719 struct isl_basic_map
*bmap
;
4720 struct isl_ctx
*ctx
;
4727 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4728 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4729 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4731 if (like
->n_div
== 0) {
4732 isl_space
*space
= isl_basic_map_get_space(like
);
4733 isl_basic_map_free(like
);
4734 return isl_basic_map_reset_space(bset
, space
);
4736 bset
= isl_basic_set_cow(bset
);
4739 total
= bset
->dim
->n_out
+ bset
->extra
;
4740 bmap
= (struct isl_basic_map
*)bset
;
4741 isl_space_free(bmap
->dim
);
4742 bmap
->dim
= isl_space_copy(like
->dim
);
4745 bmap
->n_div
= like
->n_div
;
4746 bmap
->extra
+= like
->n_div
;
4750 ltotal
= total
- bmap
->extra
+ like
->extra
;
4753 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4754 bmap
->extra
* (1 + 1 + total
));
4755 if (isl_blk_is_error(bmap
->block2
))
4757 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4761 for (i
= 0; i
< bmap
->extra
; ++i
)
4762 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4763 for (i
= 0; i
< like
->n_div
; ++i
) {
4764 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4765 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4767 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4769 isl_basic_map_free(like
);
4770 bmap
= isl_basic_map_simplify(bmap
);
4771 bmap
= isl_basic_map_finalize(bmap
);
4774 isl_basic_map_free(like
);
4775 isl_basic_set_free(bset
);
4779 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4780 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4782 return (struct isl_basic_set
*)
4783 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4786 struct isl_set
*isl_set_from_underlying_set(
4787 struct isl_set
*set
, struct isl_basic_set
*like
)
4793 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4795 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4796 isl_basic_set_free(like
);
4799 set
= isl_set_cow(set
);
4802 for (i
= 0; i
< set
->n
; ++i
) {
4803 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4804 isl_basic_set_copy(like
));
4808 isl_space_free(set
->dim
);
4809 set
->dim
= isl_space_copy(like
->dim
);
4812 isl_basic_set_free(like
);
4815 isl_basic_set_free(like
);
4820 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4824 map
= isl_map_cow(map
);
4827 map
->dim
= isl_space_cow(map
->dim
);
4831 for (i
= 1; i
< map
->n
; ++i
)
4832 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4834 for (i
= 0; i
< map
->n
; ++i
) {
4835 map
->p
[i
] = (struct isl_basic_map
*)
4836 isl_basic_map_underlying_set(map
->p
[i
]);
4841 map
->dim
= isl_space_underlying(map
->dim
, 0);
4843 isl_space_free(map
->dim
);
4844 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4848 return (struct isl_set
*)map
;
4854 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4856 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4859 /* Replace the space of "bmap" by "space".
4861 * If the space of "bmap" is identical to "space" (including the identifiers
4862 * of the input and output dimensions), then simply return the original input.
4864 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4865 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4871 equal
= isl_space_is_equal(bmap
->dim
, space
);
4872 if (equal
>= 0 && equal
)
4873 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4875 if (equal
>= 0 && equal
)
4876 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4877 space
, isl_dim_out
);
4881 isl_space_free(space
);
4884 bmap
= isl_basic_map_cow(bmap
);
4885 if (!bmap
|| !space
)
4888 isl_space_free(bmap
->dim
);
4891 bmap
= isl_basic_map_finalize(bmap
);
4895 isl_basic_map_free(bmap
);
4896 isl_space_free(space
);
4900 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4901 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4903 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4907 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4908 __isl_take isl_space
*dim
)
4912 map
= isl_map_cow(map
);
4916 for (i
= 0; i
< map
->n
; ++i
) {
4917 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4918 isl_space_copy(dim
));
4922 isl_space_free(map
->dim
);
4928 isl_space_free(dim
);
4932 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4933 __isl_take isl_space
*dim
)
4935 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4938 /* Compute the parameter domain of the given basic set.
4940 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4945 if (isl_basic_set_is_params(bset
))
4948 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4949 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4950 space
= isl_basic_set_get_space(bset
);
4951 space
= isl_space_params(space
);
4952 bset
= isl_basic_set_reset_space(bset
, space
);
4956 /* Construct a zero-dimensional basic set with the given parameter domain.
4958 __isl_give isl_basic_set
*isl_basic_set_from_params(
4959 __isl_take isl_basic_set
*bset
)
4962 space
= isl_basic_set_get_space(bset
);
4963 space
= isl_space_set_from_params(space
);
4964 bset
= isl_basic_set_reset_space(bset
, space
);
4968 /* Compute the parameter domain of the given set.
4970 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4975 if (isl_set_is_params(set
))
4978 n
= isl_set_dim(set
, isl_dim_set
);
4979 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4980 space
= isl_set_get_space(set
);
4981 space
= isl_space_params(space
);
4982 set
= isl_set_reset_space(set
, space
);
4986 /* Construct a zero-dimensional set with the given parameter domain.
4988 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4991 space
= isl_set_get_space(set
);
4992 space
= isl_space_set_from_params(space
);
4993 set
= isl_set_reset_space(set
, space
);
4997 /* Compute the parameter domain of the given map.
4999 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5004 n
= isl_map_dim(map
, isl_dim_in
);
5005 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5006 n
= isl_map_dim(map
, isl_dim_out
);
5007 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5008 space
= isl_map_get_space(map
);
5009 space
= isl_space_params(space
);
5010 map
= isl_map_reset_space(map
, space
);
5014 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5021 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5023 n_out
= isl_basic_map_n_out(bmap
);
5024 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5026 return isl_basic_map_reset_space(bmap
, space
);
5029 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5033 return isl_space_may_be_set(bmap
->dim
);
5036 /* Is this basic map actually a set?
5037 * Users should never call this function. Outside of isl,
5038 * the type should indicate whether something is a set or a map.
5040 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5044 return isl_space_is_set(bmap
->dim
);
5047 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5051 if (isl_basic_map_is_set(bmap
))
5053 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5056 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5057 __isl_take isl_basic_map
*bmap
)
5061 isl_basic_map
*domain
;
5062 int nparam
, n_in
, n_out
;
5065 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5066 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5067 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5069 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5070 domain
= isl_basic_map_universe(dim
);
5072 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5073 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5074 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5076 total
= isl_basic_map_total_dim(bmap
);
5078 for (i
= 0; i
< n_in
; ++i
) {
5079 k
= isl_basic_map_alloc_equality(bmap
);
5082 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5083 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5084 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5087 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5088 return isl_basic_map_finalize(bmap
);
5090 isl_basic_map_free(bmap
);
5094 __isl_give isl_basic_map
*isl_basic_map_range_map(
5095 __isl_take isl_basic_map
*bmap
)
5099 isl_basic_map
*range
;
5100 int nparam
, n_in
, n_out
;
5103 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5104 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5105 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5107 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5108 range
= isl_basic_map_universe(dim
);
5110 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5111 bmap
= isl_basic_map_apply_range(bmap
, range
);
5112 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5114 total
= isl_basic_map_total_dim(bmap
);
5116 for (i
= 0; i
< n_out
; ++i
) {
5117 k
= isl_basic_map_alloc_equality(bmap
);
5120 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5121 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5122 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5125 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5126 return isl_basic_map_finalize(bmap
);
5128 isl_basic_map_free(bmap
);
5132 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5136 return isl_space_may_be_set(map
->dim
);
5139 /* Is this map actually a set?
5140 * Users should never call this function. Outside of isl,
5141 * the type should indicate whether something is a set or a map.
5143 int isl_map_is_set(__isl_keep isl_map
*map
)
5147 return isl_space_is_set(map
->dim
);
5150 struct isl_set
*isl_map_range(struct isl_map
*map
)
5153 struct isl_set
*set
;
5157 if (isl_map_is_set(map
))
5158 return (isl_set
*)map
;
5160 map
= isl_map_cow(map
);
5164 set
= (struct isl_set
*) map
;
5165 set
->dim
= isl_space_range(set
->dim
);
5168 for (i
= 0; i
< map
->n
; ++i
) {
5169 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5173 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5174 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5181 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5185 map
= isl_map_cow(map
);
5189 map
->dim
= isl_space_domain_map(map
->dim
);
5192 for (i
= 0; i
< map
->n
; ++i
) {
5193 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5197 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5198 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5205 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5208 isl_space
*range_dim
;
5210 map
= isl_map_cow(map
);
5214 range_dim
= isl_space_range(isl_map_get_space(map
));
5215 range_dim
= isl_space_from_range(range_dim
);
5216 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5217 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5220 for (i
= 0; i
< map
->n
; ++i
) {
5221 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5225 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5226 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5233 /* Given a wrapped map of the form A[B -> C],
5234 * return the map A[B -> C] -> B.
5236 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5243 if (!isl_set_has_tuple_id(set
))
5244 return isl_map_domain_map(isl_set_unwrap(set
));
5246 id
= isl_set_get_tuple_id(set
);
5247 map
= isl_map_domain_map(isl_set_unwrap(set
));
5248 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5253 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5254 __isl_take isl_space
*dim
)
5257 struct isl_map
*map
= NULL
;
5259 set
= isl_set_cow(set
);
5262 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5263 map
= (struct isl_map
*)set
;
5264 for (i
= 0; i
< set
->n
; ++i
) {
5265 map
->p
[i
] = isl_basic_map_from_basic_set(
5266 set
->p
[i
], isl_space_copy(dim
));
5270 isl_space_free(map
->dim
);
5274 isl_space_free(dim
);
5279 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5280 __isl_take isl_basic_set
*bset
)
5282 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5285 __isl_give isl_basic_map
*isl_basic_map_from_range(
5286 __isl_take isl_basic_set
*bset
)
5289 space
= isl_basic_set_get_space(bset
);
5290 space
= isl_space_from_range(space
);
5291 bset
= isl_basic_set_reset_space(bset
, space
);
5292 return (isl_basic_map
*)bset
;
5295 /* Create a relation with the given set as range.
5296 * The domain of the created relation is a zero-dimensional
5297 * flat anonymous space.
5299 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5302 space
= isl_set_get_space(set
);
5303 space
= isl_space_from_range(space
);
5304 set
= isl_set_reset_space(set
, space
);
5305 return (struct isl_map
*)set
;
5308 /* Create a relation with the given set as domain.
5309 * The range of the created relation is a zero-dimensional
5310 * flat anonymous space.
5312 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5314 return isl_map_reverse(isl_map_from_range(set
));
5317 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5318 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5320 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5323 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5324 __isl_take isl_set
*range
)
5326 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5329 /* Return a newly allocated isl_map with given space and flags and
5330 * room for "n" basic maps.
5331 * Make sure that all cached information is cleared.
5333 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5336 struct isl_map
*map
;
5341 isl_die(space
->ctx
, isl_error_internal
,
5342 "negative number of basic maps", goto error
);
5343 map
= isl_calloc(space
->ctx
, struct isl_map
,
5344 sizeof(struct isl_map
) +
5345 (n
- 1) * sizeof(struct isl_basic_map
*));
5349 map
->ctx
= space
->ctx
;
5350 isl_ctx_ref(map
->ctx
);
5358 isl_space_free(space
);
5362 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5363 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5366 struct isl_map
*map
;
5369 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5373 map
= isl_map_alloc_space(dims
, n
, flags
);
5377 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5379 struct isl_basic_map
*bmap
;
5380 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5381 bmap
= isl_basic_map_set_to_empty(bmap
);
5385 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5387 struct isl_basic_set
*bset
;
5388 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5389 bset
= isl_basic_set_set_to_empty(bset
);
5393 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5395 struct isl_basic_map
*bmap
;
5396 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5397 bmap
= isl_basic_map_finalize(bmap
);
5401 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5403 struct isl_basic_set
*bset
;
5404 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5405 bset
= isl_basic_set_finalize(bset
);
5409 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5412 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5413 isl_basic_map
*bmap
;
5415 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5416 for (i
= 0; i
< total
; ++i
) {
5417 int k
= isl_basic_map_alloc_inequality(bmap
);
5420 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5421 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5425 isl_basic_map_free(bmap
);
5429 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5431 return isl_basic_map_nat_universe(dim
);
5434 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5436 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5439 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5441 return isl_map_nat_universe(dim
);
5444 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5446 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5449 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5451 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5454 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5456 struct isl_map
*map
;
5459 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5460 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5464 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5466 struct isl_set
*set
;
5469 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5470 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5474 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5477 struct isl_map
*dup
;
5481 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5482 for (i
= 0; i
< map
->n
; ++i
)
5483 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5487 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5488 __isl_take isl_basic_map
*bmap
)
5492 if (isl_basic_map_plain_is_empty(bmap
)) {
5493 isl_basic_map_free(bmap
);
5496 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5497 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5498 map
->p
[map
->n
] = bmap
;
5500 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5506 isl_basic_map_free(bmap
);
5510 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5521 isl_ctx_deref(map
->ctx
);
5522 for (i
= 0; i
< map
->n
; ++i
)
5523 isl_basic_map_free(map
->p
[i
]);
5524 isl_space_free(map
->dim
);
5530 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5531 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5535 bmap
= isl_basic_map_cow(bmap
);
5536 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5537 j
= isl_basic_map_alloc_equality(bmap
);
5540 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5541 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5542 isl_int_set_si(bmap
->eq
[j
][0], value
);
5543 bmap
= isl_basic_map_simplify(bmap
);
5544 return isl_basic_map_finalize(bmap
);
5546 isl_basic_map_free(bmap
);
5550 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5551 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5555 bmap
= isl_basic_map_cow(bmap
);
5556 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5557 j
= isl_basic_map_alloc_equality(bmap
);
5560 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5561 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5562 isl_int_set(bmap
->eq
[j
][0], value
);
5563 bmap
= isl_basic_map_simplify(bmap
);
5564 return isl_basic_map_finalize(bmap
);
5566 isl_basic_map_free(bmap
);
5570 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5571 enum isl_dim_type type
, unsigned pos
, int value
)
5575 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5576 return isl_basic_map_fix_pos_si(bmap
,
5577 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5579 isl_basic_map_free(bmap
);
5583 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5584 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5588 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5589 return isl_basic_map_fix_pos(bmap
,
5590 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5592 isl_basic_map_free(bmap
);
5596 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5597 * to be equal to "v".
5599 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5600 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5604 if (!isl_val_is_int(v
))
5605 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5606 "expecting integer value", goto error
);
5607 if (pos
>= isl_basic_map_dim(bmap
, type
))
5608 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5609 "index out of bounds", goto error
);
5610 pos
+= isl_basic_map_offset(bmap
, type
);
5611 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5615 isl_basic_map_free(bmap
);
5620 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5621 * to be equal to "v".
5623 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5624 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5626 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5629 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5630 enum isl_dim_type type
, unsigned pos
, int value
)
5632 return (struct isl_basic_set
*)
5633 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5637 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5638 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5640 return (struct isl_basic_set
*)
5641 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5645 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5646 unsigned input
, int value
)
5648 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5651 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5652 unsigned dim
, int value
)
5654 return (struct isl_basic_set
*)
5655 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5656 isl_dim_set
, dim
, value
);
5659 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5661 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5668 isl_basic_map_free(map
->p
[i
]);
5669 if (i
!= map
->n
- 1) {
5670 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5671 map
->p
[i
] = map
->p
[map
->n
- 1];
5678 /* Perform "fn" on each basic map of "map", where we may not be holding
5679 * the only reference to "map".
5680 * In particular, "fn" should be a semantics preserving operation
5681 * that we want to apply to all copies of "map". We therefore need
5682 * to be careful not to modify "map" in a way that breaks "map"
5683 * in case anything goes wrong.
5685 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5686 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5688 struct isl_basic_map
*bmap
;
5694 for (i
= map
->n
- 1; i
>= 0; --i
) {
5695 bmap
= isl_basic_map_copy(map
->p
[i
]);
5699 isl_basic_map_free(map
->p
[i
]);
5701 if (remove_if_empty(map
, i
) < 0)
5711 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5712 enum isl_dim_type type
, unsigned pos
, int value
)
5716 map
= isl_map_cow(map
);
5720 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5721 for (i
= map
->n
- 1; i
>= 0; --i
) {
5722 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5723 if (remove_if_empty(map
, i
) < 0)
5726 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5733 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5734 enum isl_dim_type type
, unsigned pos
, int value
)
5736 return (struct isl_set
*)
5737 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5740 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5741 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5745 map
= isl_map_cow(map
);
5749 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5750 for (i
= 0; i
< map
->n
; ++i
) {
5751 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5755 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5762 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5763 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5765 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5768 /* Fix the value of the variable at position "pos" of type "type" of "map"
5769 * to be equal to "v".
5771 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5772 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5776 map
= isl_map_cow(map
);
5780 if (!isl_val_is_int(v
))
5781 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5782 "expecting integer value", goto error
);
5783 if (pos
>= isl_map_dim(map
, type
))
5784 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5785 "index out of bounds", goto error
);
5786 for (i
= map
->n
- 1; i
>= 0; --i
) {
5787 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5789 if (remove_if_empty(map
, i
) < 0)
5792 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5801 /* Fix the value of the variable at position "pos" of type "type" of "set"
5802 * to be equal to "v".
5804 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5805 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5807 return isl_map_fix_val(set
, type
, pos
, v
);
5810 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5811 unsigned input
, int value
)
5813 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5816 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5818 return (struct isl_set
*)
5819 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5822 static __isl_give isl_basic_map
*basic_map_bound_si(
5823 __isl_take isl_basic_map
*bmap
,
5824 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5830 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5831 pos
+= isl_basic_map_offset(bmap
, type
);
5832 bmap
= isl_basic_map_cow(bmap
);
5833 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5834 j
= isl_basic_map_alloc_inequality(bmap
);
5837 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5839 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5840 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5842 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5843 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5845 bmap
= isl_basic_map_simplify(bmap
);
5846 return isl_basic_map_finalize(bmap
);
5848 isl_basic_map_free(bmap
);
5852 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5853 __isl_take isl_basic_map
*bmap
,
5854 enum isl_dim_type type
, unsigned pos
, int value
)
5856 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5859 /* Constrain the values of the given dimension to be no greater than "value".
5861 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5862 __isl_take isl_basic_map
*bmap
,
5863 enum isl_dim_type type
, unsigned pos
, int value
)
5865 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5868 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5869 unsigned dim
, isl_int value
)
5873 bset
= isl_basic_set_cow(bset
);
5874 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5875 j
= isl_basic_set_alloc_inequality(bset
);
5878 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5879 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5880 isl_int_neg(bset
->ineq
[j
][0], value
);
5881 bset
= isl_basic_set_simplify(bset
);
5882 return isl_basic_set_finalize(bset
);
5884 isl_basic_set_free(bset
);
5888 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5889 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5893 map
= isl_map_cow(map
);
5897 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5898 for (i
= 0; i
< map
->n
; ++i
) {
5899 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5900 type
, pos
, value
, upper
);
5904 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5911 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5912 enum isl_dim_type type
, unsigned pos
, int value
)
5914 return map_bound_si(map
, type
, pos
, value
, 0);
5917 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5918 enum isl_dim_type type
, unsigned pos
, int value
)
5920 return map_bound_si(map
, type
, pos
, value
, 1);
5923 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5924 enum isl_dim_type type
, unsigned pos
, int value
)
5926 return (struct isl_set
*)
5927 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5930 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5931 enum isl_dim_type type
, unsigned pos
, int value
)
5933 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5936 /* Bound the given variable of "bmap" from below (or above is "upper"
5937 * is set) to "value".
5939 static __isl_give isl_basic_map
*basic_map_bound(
5940 __isl_take isl_basic_map
*bmap
,
5941 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5947 if (pos
>= isl_basic_map_dim(bmap
, type
))
5948 isl_die(bmap
->ctx
, isl_error_invalid
,
5949 "index out of bounds", goto error
);
5950 pos
+= isl_basic_map_offset(bmap
, type
);
5951 bmap
= isl_basic_map_cow(bmap
);
5952 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5953 j
= isl_basic_map_alloc_inequality(bmap
);
5956 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5958 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5959 isl_int_set(bmap
->ineq
[j
][0], value
);
5961 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5962 isl_int_neg(bmap
->ineq
[j
][0], value
);
5964 bmap
= isl_basic_map_simplify(bmap
);
5965 return isl_basic_map_finalize(bmap
);
5967 isl_basic_map_free(bmap
);
5971 /* Bound the given variable of "map" from below (or above is "upper"
5972 * is set) to "value".
5974 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5975 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5979 map
= isl_map_cow(map
);
5983 if (pos
>= isl_map_dim(map
, type
))
5984 isl_die(map
->ctx
, isl_error_invalid
,
5985 "index out of bounds", goto error
);
5986 for (i
= map
->n
- 1; i
>= 0; --i
) {
5987 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5988 if (remove_if_empty(map
, i
) < 0)
5991 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5998 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5999 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6001 return map_bound(map
, type
, pos
, value
, 0);
6004 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6005 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6007 return map_bound(map
, type
, pos
, value
, 1);
6010 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6011 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6013 return isl_map_lower_bound(set
, type
, pos
, value
);
6016 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6017 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6019 return isl_map_upper_bound(set
, type
, pos
, value
);
6022 /* Force the values of the variable at position "pos" of type "type" of "set"
6023 * to be no smaller than "value".
6025 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6026 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6030 if (!isl_val_is_int(value
))
6031 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6032 "expecting integer value", goto error
);
6033 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6034 isl_val_free(value
);
6037 isl_val_free(value
);
6042 /* Force the values of the variable at position "pos" of type "type" of "set"
6043 * to be no greater than "value".
6045 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6046 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6050 if (!isl_val_is_int(value
))
6051 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6052 "expecting integer value", goto error
);
6053 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6054 isl_val_free(value
);
6057 isl_val_free(value
);
6062 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6067 set
= isl_set_cow(set
);
6071 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6072 for (i
= 0; i
< set
->n
; ++i
) {
6073 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6083 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6087 map
= isl_map_cow(map
);
6091 map
->dim
= isl_space_reverse(map
->dim
);
6094 for (i
= 0; i
< map
->n
; ++i
) {
6095 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6099 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6107 #define TYPE isl_pw_multi_aff
6109 #define SUFFIX _pw_multi_aff
6111 #define EMPTY isl_pw_multi_aff_empty
6113 #define ADD isl_pw_multi_aff_union_add
6114 #include "isl_map_lexopt_templ.c"
6116 /* Given a map "map", compute the lexicographically minimal
6117 * (or maximal) image element for each domain element in dom,
6118 * in the form of an isl_pw_multi_aff.
6119 * If "empty" is not NULL, then set *empty to those elements in dom that
6120 * do not have an image element.
6121 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6122 * should be computed over the domain of "map". "empty" is also NULL
6125 * We first compute the lexicographically minimal or maximal element
6126 * in the first basic map. This results in a partial solution "res"
6127 * and a subset "todo" of dom that still need to be handled.
6128 * We then consider each of the remaining maps in "map" and successively
6129 * update both "res" and "todo".
6130 * If "empty" is NULL, then the todo sets are not needed and therefore
6131 * also not computed.
6133 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6134 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6135 __isl_give isl_set
**empty
, unsigned flags
)
6139 isl_pw_multi_aff
*res
;
6142 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6143 if (!map
|| (!full
&& !dom
))
6146 if (isl_map_plain_is_empty(map
)) {
6151 return isl_pw_multi_aff_from_map(map
);
6154 res
= basic_map_partial_lexopt_pw_multi_aff(
6155 isl_basic_map_copy(map
->p
[0]),
6156 isl_set_copy(dom
), empty
, flags
);
6160 for (i
= 1; i
< map
->n
; ++i
) {
6161 isl_pw_multi_aff
*res_i
;
6163 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6164 isl_basic_map_copy(map
->p
[i
]),
6165 isl_set_copy(dom
), empty
, flags
);
6167 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6168 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6170 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6173 todo
= isl_set_intersect(todo
, *empty
);
6192 #define TYPE isl_map
6196 #define EMPTY isl_map_empty
6198 #define ADD isl_map_union_disjoint
6199 #include "isl_map_lexopt_templ.c"
6201 /* Given a map "map", compute the lexicographically minimal
6202 * (or maximal) image element for each domain element in "dom",
6203 * in the form of an isl_map.
6204 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6205 * do not have an image element.
6206 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6207 * should be computed over the domain of "map". "empty" is also NULL
6210 * If the input consists of more than one disjunct, then first
6211 * compute the desired result in the form of an isl_pw_multi_aff and
6212 * then convert that into an isl_map.
6214 * This function used to have an explicit implementation in terms
6215 * of isl_maps, but it would continually intersect the domains of
6216 * partial results with the complement of the domain of the next
6217 * partial solution, potentially leading to an explosion in the number
6218 * of disjuncts if there are several disjuncts in the input.
6219 * An even earlier implementation of this function would look for
6220 * better results in the domain of the partial result and for extra
6221 * results in the complement of this domain, which would lead to
6222 * even more splintering.
6224 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6225 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6226 __isl_give isl_set
**empty
, unsigned flags
)
6229 struct isl_map
*res
;
6230 isl_pw_multi_aff
*pma
;
6232 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6233 if (!map
|| (!full
&& !dom
))
6236 if (isl_map_plain_is_empty(map
)) {
6245 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6251 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6253 return isl_map_from_pw_multi_aff(pma
);
6262 __isl_give isl_map
*isl_map_partial_lexmax(
6263 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6264 __isl_give isl_set
**empty
)
6266 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6269 __isl_give isl_map
*isl_map_partial_lexmin(
6270 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6271 __isl_give isl_set
**empty
)
6273 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6276 __isl_give isl_set
*isl_set_partial_lexmin(
6277 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6278 __isl_give isl_set
**empty
)
6280 return (struct isl_set
*)
6281 isl_map_partial_lexmin((struct isl_map
*)set
,
6285 __isl_give isl_set
*isl_set_partial_lexmax(
6286 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6287 __isl_give isl_set
**empty
)
6289 return (struct isl_set
*)
6290 isl_map_partial_lexmax((struct isl_map
*)set
,
6294 /* Compute the lexicographic minimum (or maximum if "flags" includes
6295 * ISL_OPT_MAX) of "bset" over its parametric domain.
6297 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6300 return isl_basic_map_lexopt(bset
, flags
);
6303 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6305 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6308 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6310 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6313 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6315 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6318 /* Compute the lexicographic minimum of "bset" over its parametric domain
6319 * for the purpose of quantifier elimination.
6320 * That is, find an explicit representation for all the existentially
6321 * quantified variables in "bset" by computing their lexicographic
6324 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6325 __isl_take isl_basic_set
*bset
)
6327 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6330 /* Extract the first and only affine expression from list
6331 * and then add it to *pwaff with the given dom.
6332 * This domain is known to be disjoint from other domains
6333 * because of the way isl_basic_map_foreach_lexmax works.
6335 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6336 __isl_take isl_aff_list
*list
, void *user
)
6338 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6340 isl_pw_aff
**pwaff
= user
;
6341 isl_pw_aff
*pwaff_i
;
6345 if (isl_aff_list_n_aff(list
) != 1)
6346 isl_die(ctx
, isl_error_internal
,
6347 "expecting single element list", goto error
);
6349 aff
= isl_aff_list_get_aff(list
, 0);
6350 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6352 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6354 isl_aff_list_free(list
);
6358 isl_basic_set_free(dom
);
6359 isl_aff_list_free(list
);
6363 /* Given a basic map with one output dimension, compute the minimum or
6364 * maximum of that dimension as an isl_pw_aff.
6366 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6367 * call update_dim_opt on each leaf of the result.
6369 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6372 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6376 dim
= isl_space_from_domain(isl_space_domain(dim
));
6377 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6378 pwaff
= isl_pw_aff_empty(dim
);
6380 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6382 return isl_pw_aff_free(pwaff
);
6387 /* Compute the minimum or maximum of the given output dimension
6388 * as a function of the parameters and the input dimensions,
6389 * but independently of the other output dimensions.
6391 * We first project out the other output dimension and then compute
6392 * the "lexicographic" maximum in each basic map, combining the results
6393 * using isl_pw_aff_union_max.
6395 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6402 n_out
= isl_map_dim(map
, isl_dim_out
);
6403 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6404 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6409 isl_space
*dim
= isl_map_get_space(map
);
6411 return isl_pw_aff_empty(dim
);
6414 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6415 for (i
= 1; i
< map
->n
; ++i
) {
6416 isl_pw_aff
*pwaff_i
;
6418 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6419 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6427 /* Compute the minimum of the given output dimension as a function of the
6428 * parameters and input dimensions, but independently of
6429 * the other output dimensions.
6431 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6433 return map_dim_opt(map
, pos
, 0);
6436 /* Compute the maximum of the given output dimension as a function of the
6437 * parameters and input dimensions, but independently of
6438 * the other output dimensions.
6440 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6442 return map_dim_opt(map
, pos
, 1);
6445 /* Compute the minimum or maximum of the given set dimension
6446 * as a function of the parameters,
6447 * but independently of the other set dimensions.
6449 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6452 return map_dim_opt(set
, pos
, max
);
6455 /* Compute the maximum of the given set dimension as a function of the
6456 * parameters, but independently of the other set dimensions.
6458 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6460 return set_dim_opt(set
, pos
, 1);
6463 /* Compute the minimum of the given set dimension as a function of the
6464 * parameters, but independently of the other set dimensions.
6466 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6468 return set_dim_opt(set
, pos
, 0);
6471 /* Apply a preimage specified by "mat" on the parameters of "bset".
6472 * bset is assumed to have only parameters and divs.
6474 static struct isl_basic_set
*basic_set_parameter_preimage(
6475 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6482 bset
->dim
= isl_space_cow(bset
->dim
);
6486 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6488 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6490 bset
->dim
->nparam
= 0;
6491 bset
->dim
->n_out
= nparam
;
6492 bset
= isl_basic_set_preimage(bset
, mat
);
6494 bset
->dim
->nparam
= bset
->dim
->n_out
;
6495 bset
->dim
->n_out
= 0;
6500 isl_basic_set_free(bset
);
6504 /* Apply a preimage specified by "mat" on the parameters of "set".
6505 * set is assumed to have only parameters and divs.
6507 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6508 __isl_take isl_mat
*mat
)
6516 nparam
= isl_set_dim(set
, isl_dim_param
);
6518 if (mat
->n_row
!= 1 + nparam
)
6519 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6520 "unexpected number of rows", goto error
);
6522 space
= isl_set_get_space(set
);
6523 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6524 isl_dim_param
, 0, nparam
);
6525 set
= isl_set_reset_space(set
, space
);
6526 set
= isl_set_preimage(set
, mat
);
6527 nparam
= isl_set_dim(set
, isl_dim_out
);
6528 space
= isl_set_get_space(set
);
6529 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6530 isl_dim_out
, 0, nparam
);
6531 set
= isl_set_reset_space(set
, space
);
6539 /* Intersect the basic set "bset" with the affine space specified by the
6540 * equalities in "eq".
6542 static struct isl_basic_set
*basic_set_append_equalities(
6543 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6551 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6556 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6557 for (i
= 0; i
< eq
->n_row
; ++i
) {
6558 k
= isl_basic_set_alloc_equality(bset
);
6561 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6562 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6566 bset
= isl_basic_set_gauss(bset
, NULL
);
6567 bset
= isl_basic_set_finalize(bset
);
6572 isl_basic_set_free(bset
);
6576 /* Intersect the set "set" with the affine space specified by the
6577 * equalities in "eq".
6579 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6587 for (i
= 0; i
< set
->n
; ++i
) {
6588 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6601 /* Given a basic set "bset" that only involves parameters and existentially
6602 * quantified variables, return the index of the first equality
6603 * that only involves parameters. If there is no such equality then
6604 * return bset->n_eq.
6606 * This function assumes that isl_basic_set_gauss has been called on "bset".
6608 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6611 unsigned nparam
, n_div
;
6616 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6617 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6619 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6620 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6627 /* Compute an explicit representation for the existentially quantified
6628 * variables in "bset" by computing the "minimal value" of the set
6629 * variables. Since there are no set variables, the computation of
6630 * the minimal value essentially computes an explicit representation
6631 * of the non-empty part(s) of "bset".
6633 * The input only involves parameters and existentially quantified variables.
6634 * All equalities among parameters have been removed.
6636 * Since the existentially quantified variables in the result are in general
6637 * going to be different from those in the input, we first replace
6638 * them by the minimal number of variables based on their equalities.
6639 * This should simplify the parametric integer programming.
6641 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6643 isl_morph
*morph1
, *morph2
;
6649 if (bset
->n_eq
== 0)
6650 return isl_basic_set_lexmin_compute_divs(bset
);
6652 morph1
= isl_basic_set_parameter_compression(bset
);
6653 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6654 bset
= isl_basic_set_lift(bset
);
6655 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6656 bset
= isl_morph_basic_set(morph2
, bset
);
6657 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6658 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6660 set
= isl_basic_set_lexmin_compute_divs(bset
);
6662 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6667 /* Project the given basic set onto its parameter domain, possibly introducing
6668 * new, explicit, existential variables in the constraints.
6669 * The input has parameters and (possibly implicit) existential variables.
6670 * The output has the same parameters, but only
6671 * explicit existentially quantified variables.
6673 * The actual projection is performed by pip, but pip doesn't seem
6674 * to like equalities very much, so we first remove the equalities
6675 * among the parameters by performing a variable compression on
6676 * the parameters. Afterward, an inverse transformation is performed
6677 * and the equalities among the parameters are inserted back in.
6679 * The variable compression on the parameters may uncover additional
6680 * equalities that were only implicit before. We therefore check
6681 * if there are any new parameter equalities in the result and
6682 * if so recurse. The removal of parameter equalities is required
6683 * for the parameter compression performed by base_compute_divs.
6685 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6689 struct isl_mat
*T
, *T2
;
6690 struct isl_set
*set
;
6693 bset
= isl_basic_set_cow(bset
);
6697 if (bset
->n_eq
== 0)
6698 return base_compute_divs(bset
);
6700 bset
= isl_basic_set_gauss(bset
, NULL
);
6703 if (isl_basic_set_plain_is_empty(bset
))
6704 return isl_set_from_basic_set(bset
);
6706 i
= first_parameter_equality(bset
);
6707 if (i
== bset
->n_eq
)
6708 return base_compute_divs(bset
);
6710 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6711 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6713 eq
= isl_mat_cow(eq
);
6714 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6715 if (T
&& T
->n_col
== 0) {
6719 bset
= isl_basic_set_set_to_empty(bset
);
6720 return isl_set_from_basic_set(bset
);
6722 bset
= basic_set_parameter_preimage(bset
, T
);
6724 i
= first_parameter_equality(bset
);
6727 else if (i
== bset
->n_eq
)
6728 set
= base_compute_divs(bset
);
6730 set
= parameter_compute_divs(bset
);
6731 set
= set_parameter_preimage(set
, T2
);
6732 set
= set_append_equalities(set
, eq
);
6736 /* Insert the divs from "ls" before those of "bmap".
6738 * The number of columns is not changed, which means that the last
6739 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6740 * The caller is responsible for removing the same number of dimensions
6741 * from the space of "bmap".
6743 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6744 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6750 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6754 old_n_div
= bmap
->n_div
;
6755 bmap
= insert_div_rows(bmap
, n_div
);
6759 for (i
= 0; i
< n_div
; ++i
) {
6760 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6761 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6767 /* Replace the space of "bmap" by the space and divs of "ls".
6769 * If "ls" has any divs, then we simplify the result since we may
6770 * have discovered some additional equalities that could simplify
6771 * the div expressions.
6773 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6774 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6778 bmap
= isl_basic_map_cow(bmap
);
6782 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6783 bmap
= insert_divs_from_local_space(bmap
, ls
);
6787 isl_space_free(bmap
->dim
);
6788 bmap
->dim
= isl_local_space_get_space(ls
);
6792 isl_local_space_free(ls
);
6794 bmap
= isl_basic_map_simplify(bmap
);
6795 bmap
= isl_basic_map_finalize(bmap
);
6798 isl_basic_map_free(bmap
);
6799 isl_local_space_free(ls
);
6803 /* Replace the space of "map" by the space and divs of "ls".
6805 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6806 __isl_take isl_local_space
*ls
)
6810 map
= isl_map_cow(map
);
6814 for (i
= 0; i
< map
->n
; ++i
) {
6815 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6816 isl_local_space_copy(ls
));
6820 isl_space_free(map
->dim
);
6821 map
->dim
= isl_local_space_get_space(ls
);
6825 isl_local_space_free(ls
);
6828 isl_local_space_free(ls
);
6833 /* Compute an explicit representation for the existentially
6834 * quantified variables for which do not know any explicit representation yet.
6836 * We first sort the existentially quantified variables so that the
6837 * existentially quantified variables for which we already have an explicit
6838 * representation are placed before those for which we do not.
6839 * The input dimensions, the output dimensions and the existentially
6840 * quantified variables for which we already have an explicit
6841 * representation are then turned into parameters.
6842 * compute_divs returns a map with the same parameters and
6843 * no input or output dimensions and the dimension specification
6844 * is reset to that of the input, including the existentially quantified
6845 * variables for which we already had an explicit representation.
6847 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6849 struct isl_basic_set
*bset
;
6850 struct isl_set
*set
;
6851 struct isl_map
*map
;
6853 isl_local_space
*ls
;
6860 bmap
= isl_basic_map_sort_divs(bmap
);
6861 bmap
= isl_basic_map_cow(bmap
);
6865 n_known
= isl_basic_map_first_unknown_div(bmap
);
6867 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6869 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6870 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6871 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6872 dim
= isl_space_set_alloc(bmap
->ctx
,
6873 nparam
+ n_in
+ n_out
+ n_known
, 0);
6877 ls
= isl_basic_map_get_local_space(bmap
);
6878 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6879 n_known
, bmap
->n_div
- n_known
);
6881 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6882 swap_div(bmap
, i
- n_known
, i
);
6883 bmap
->n_div
-= n_known
;
6884 bmap
->extra
-= n_known
;
6886 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6887 bset
= (struct isl_basic_set
*)bmap
;
6889 set
= parameter_compute_divs(bset
);
6890 map
= (struct isl_map
*)set
;
6891 map
= replace_space_by_local_space(map
, ls
);
6895 isl_basic_map_free(bmap
);
6899 /* Remove the explicit representation of local variable "div",
6902 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6903 __isl_take isl_basic_map
*bmap
, int div
)
6907 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6909 return isl_basic_map_free(bmap
);
6913 bmap
= isl_basic_map_cow(bmap
);
6916 isl_int_set_si(bmap
->div
[div
][0], 0);
6920 /* Is local variable "div" of "bmap" marked as not having an explicit
6922 * Note that even if "div" is not marked in this way and therefore
6923 * has an explicit representation, this representation may still
6924 * depend (indirectly) on other local variables that do not
6925 * have an explicit representation.
6927 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6931 return isl_bool_error
;
6932 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6933 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6934 "position out of bounds", return isl_bool_error
);
6935 return isl_int_is_zero(bmap
->div
[div
][0]);
6938 /* Return the position of the first local variable that does not
6939 * have an explicit representation.
6940 * Return the total number of local variables if they all have
6941 * an explicit representation.
6942 * Return -1 on error.
6944 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6951 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6952 if (!isl_basic_map_div_is_known(bmap
, i
))
6958 /* Return the position of the first local variable that does not
6959 * have an explicit representation.
6960 * Return the total number of local variables if they all have
6961 * an explicit representation.
6962 * Return -1 on error.
6964 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
6966 return isl_basic_map_first_unknown_div(bset
);
6969 /* Does "bmap" have an explicit representation for all local variables?
6971 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6975 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
6976 first
= isl_basic_map_first_unknown_div(bmap
);
6978 return isl_bool_error
;
6982 /* Do all basic maps in "map" have an explicit representation
6983 * for all local variables?
6985 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
6990 return isl_bool_error
;
6992 for (i
= 0; i
< map
->n
; ++i
) {
6993 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6998 return isl_bool_true
;
7001 /* If bmap contains any unknown divs, then compute explicit
7002 * expressions for them. However, this computation may be
7003 * quite expensive, so first try to remove divs that aren't
7006 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7009 struct isl_map
*map
;
7011 known
= isl_basic_map_divs_known(bmap
);
7015 return isl_map_from_basic_map(bmap
);
7017 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7019 known
= isl_basic_map_divs_known(bmap
);
7023 return isl_map_from_basic_map(bmap
);
7025 map
= compute_divs(bmap
);
7028 isl_basic_map_free(bmap
);
7032 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7036 struct isl_map
*res
;
7043 known
= isl_map_divs_known(map
);
7051 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7052 for (i
= 1 ; i
< map
->n
; ++i
) {
7054 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7055 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7056 res
= isl_map_union_disjoint(res
, r2
);
7058 res
= isl_map_union(res
, r2
);
7065 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7067 return (struct isl_set
*)
7068 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7071 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7073 return (struct isl_set
*)
7074 isl_map_compute_divs((struct isl_map
*)set
);
7077 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7080 struct isl_set
*set
;
7085 map
= isl_map_cow(map
);
7089 set
= (struct isl_set
*)map
;
7090 set
->dim
= isl_space_domain(set
->dim
);
7093 for (i
= 0; i
< map
->n
; ++i
) {
7094 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7098 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7099 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7106 /* Return the union of "map1" and "map2", where we assume for now that
7107 * "map1" and "map2" are disjoint. Note that the basic maps inside
7108 * "map1" or "map2" may not be disjoint from each other.
7109 * Also note that this function is also called from isl_map_union,
7110 * which takes care of handling the situation where "map1" and "map2"
7111 * may not be disjoint.
7113 * If one of the inputs is empty, we can simply return the other input.
7114 * Similarly, if one of the inputs is universal, then it is equal to the union.
7116 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7117 __isl_take isl_map
*map2
)
7121 struct isl_map
*map
= NULL
;
7127 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7128 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7129 "spaces don't match", goto error
);
7140 is_universe
= isl_map_plain_is_universe(map1
);
7141 if (is_universe
< 0)
7148 is_universe
= isl_map_plain_is_universe(map2
);
7149 if (is_universe
< 0)
7156 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7157 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7158 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7160 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7161 map1
->n
+ map2
->n
, flags
);
7164 for (i
= 0; i
< map1
->n
; ++i
) {
7165 map
= isl_map_add_basic_map(map
,
7166 isl_basic_map_copy(map1
->p
[i
]));
7170 for (i
= 0; i
< map2
->n
; ++i
) {
7171 map
= isl_map_add_basic_map(map
,
7172 isl_basic_map_copy(map2
->p
[i
]));
7186 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7187 * guaranteed to be disjoint by the caller.
7189 * Note that this functions is called from within isl_map_make_disjoint,
7190 * so we have to be careful not to touch the constraints of the inputs
7193 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7194 __isl_take isl_map
*map2
)
7196 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7199 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7200 * not be disjoint. The parameters are assumed to have been aligned.
7202 * We currently simply call map_union_disjoint, the internal operation
7203 * of which does not really depend on the inputs being disjoint.
7204 * If the result contains more than one basic map, then we clear
7205 * the disjoint flag since the result may contain basic maps from
7206 * both inputs and these are not guaranteed to be disjoint.
7208 * As a special case, if "map1" and "map2" are obviously equal,
7209 * then we simply return "map1".
7211 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7212 __isl_take isl_map
*map2
)
7219 equal
= isl_map_plain_is_equal(map1
, map2
);
7227 map1
= map_union_disjoint(map1
, map2
);
7231 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7239 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7242 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7243 __isl_take isl_map
*map2
)
7245 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7248 struct isl_set
*isl_set_union_disjoint(
7249 struct isl_set
*set1
, struct isl_set
*set2
)
7251 return (struct isl_set
*)
7252 isl_map_union_disjoint(
7253 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7256 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7258 return (struct isl_set
*)
7259 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7262 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7265 * "map" and "set" are assumed to be compatible and non-NULL.
7267 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7268 __isl_take isl_set
*set
,
7269 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7270 __isl_take isl_basic_set
*bset
))
7273 struct isl_map
*result
;
7276 if (isl_set_plain_is_universe(set
)) {
7281 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7282 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7283 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7285 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7286 map
->n
* set
->n
, flags
);
7287 for (i
= 0; result
&& i
< map
->n
; ++i
)
7288 for (j
= 0; j
< set
->n
; ++j
) {
7289 result
= isl_map_add_basic_map(result
,
7290 fn(isl_basic_map_copy(map
->p
[i
]),
7291 isl_basic_set_copy(set
->p
[j
])));
7301 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7302 __isl_take isl_set
*set
)
7307 if (!isl_map_compatible_range(map
, set
))
7308 isl_die(set
->ctx
, isl_error_invalid
,
7309 "incompatible spaces", goto error
);
7311 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7318 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7319 __isl_take isl_set
*set
)
7321 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7324 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7325 __isl_take isl_set
*set
)
7330 if (!isl_map_compatible_domain(map
, set
))
7331 isl_die(set
->ctx
, isl_error_invalid
,
7332 "incompatible spaces", goto error
);
7334 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7341 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7342 __isl_take isl_set
*set
)
7344 return isl_map_align_params_map_map_and(map
, set
,
7345 &map_intersect_domain
);
7348 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7349 __isl_take isl_map
*map2
)
7353 map1
= isl_map_reverse(map1
);
7354 map1
= isl_map_apply_range(map1
, map2
);
7355 return isl_map_reverse(map1
);
7362 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7363 __isl_take isl_map
*map2
)
7365 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7368 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7369 __isl_take isl_map
*map2
)
7371 isl_space
*dim_result
;
7372 struct isl_map
*result
;
7378 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7379 isl_space_copy(map2
->dim
));
7381 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7384 for (i
= 0; i
< map1
->n
; ++i
)
7385 for (j
= 0; j
< map2
->n
; ++j
) {
7386 result
= isl_map_add_basic_map(result
,
7387 isl_basic_map_apply_range(
7388 isl_basic_map_copy(map1
->p
[i
]),
7389 isl_basic_map_copy(map2
->p
[j
])));
7395 if (result
&& result
->n
<= 1)
7396 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7404 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7405 __isl_take isl_map
*map2
)
7407 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7411 * returns range - domain
7413 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7415 isl_space
*target_space
;
7416 struct isl_basic_set
*bset
;
7423 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7424 bmap
->dim
, isl_dim_out
),
7426 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7427 dim
= isl_basic_map_n_in(bmap
);
7428 nparam
= isl_basic_map_n_param(bmap
);
7429 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7430 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7431 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7432 for (i
= 0; i
< dim
; ++i
) {
7433 int j
= isl_basic_map_alloc_equality(bmap
);
7435 bmap
= isl_basic_map_free(bmap
);
7438 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7439 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7440 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7441 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7443 bset
= isl_basic_map_domain(bmap
);
7444 bset
= isl_basic_set_reset_space(bset
, target_space
);
7447 isl_basic_map_free(bmap
);
7452 * returns range - domain
7454 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7458 struct isl_set
*result
;
7463 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7464 map
->dim
, isl_dim_out
),
7466 dim
= isl_map_get_space(map
);
7467 dim
= isl_space_domain(dim
);
7468 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7471 for (i
= 0; i
< map
->n
; ++i
)
7472 result
= isl_set_add_basic_set(result
,
7473 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7482 * returns [domain -> range] -> range - domain
7484 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7485 __isl_take isl_basic_map
*bmap
)
7489 isl_basic_map
*domain
;
7493 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7494 bmap
->dim
, isl_dim_out
))
7495 isl_die(bmap
->ctx
, isl_error_invalid
,
7496 "domain and range don't match", goto error
);
7498 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7499 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7501 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7502 domain
= isl_basic_map_universe(dim
);
7504 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7505 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7506 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7508 total
= isl_basic_map_total_dim(bmap
);
7510 for (i
= 0; i
< n
; ++i
) {
7511 k
= isl_basic_map_alloc_equality(bmap
);
7514 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7515 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7516 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7517 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7520 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7521 return isl_basic_map_finalize(bmap
);
7523 isl_basic_map_free(bmap
);
7528 * returns [domain -> range] -> range - domain
7530 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7533 isl_space
*domain_dim
;
7538 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7539 map
->dim
, isl_dim_out
))
7540 isl_die(map
->ctx
, isl_error_invalid
,
7541 "domain and range don't match", goto error
);
7543 map
= isl_map_cow(map
);
7547 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7548 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7549 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7552 for (i
= 0; i
< map
->n
; ++i
) {
7553 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7557 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7564 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7566 struct isl_basic_map
*bmap
;
7574 nparam
= dims
->nparam
;
7576 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7580 for (i
= 0; i
< dim
; ++i
) {
7581 int j
= isl_basic_map_alloc_equality(bmap
);
7584 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7585 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7586 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7588 return isl_basic_map_finalize(bmap
);
7590 isl_basic_map_free(bmap
);
7594 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7598 if (dim
->n_in
!= dim
->n_out
)
7599 isl_die(dim
->ctx
, isl_error_invalid
,
7600 "number of input and output dimensions needs to be "
7601 "the same", goto error
);
7602 return basic_map_identity(dim
);
7604 isl_space_free(dim
);
7608 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7610 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7613 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7615 isl_space
*dim
= isl_set_get_space(set
);
7617 id
= isl_map_identity(isl_space_map_from_set(dim
));
7618 return isl_map_intersect_range(id
, set
);
7621 /* Construct a basic set with all set dimensions having only non-negative
7624 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7625 __isl_take isl_space
*space
)
7630 struct isl_basic_set
*bset
;
7634 nparam
= space
->nparam
;
7636 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7639 for (i
= 0; i
< dim
; ++i
) {
7640 int k
= isl_basic_set_alloc_inequality(bset
);
7643 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7644 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7648 isl_basic_set_free(bset
);
7652 /* Construct the half-space x_pos >= 0.
7654 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7658 isl_basic_set
*nonneg
;
7660 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7661 k
= isl_basic_set_alloc_inequality(nonneg
);
7664 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7665 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7667 return isl_basic_set_finalize(nonneg
);
7669 isl_basic_set_free(nonneg
);
7673 /* Construct the half-space x_pos <= -1.
7675 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7680 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7681 k
= isl_basic_set_alloc_inequality(neg
);
7684 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7685 isl_int_set_si(neg
->ineq
[k
][0], -1);
7686 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7688 return isl_basic_set_finalize(neg
);
7690 isl_basic_set_free(neg
);
7694 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7695 enum isl_dim_type type
, unsigned first
, unsigned n
)
7699 isl_basic_set
*nonneg
;
7707 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7709 offset
= pos(set
->dim
, type
);
7710 for (i
= 0; i
< n
; ++i
) {
7711 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7712 offset
+ first
+ i
);
7713 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7715 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7724 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7725 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7732 if (isl_set_plain_is_empty(set
)) {
7737 return fn(set
, signs
, user
);
7740 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7742 half
= isl_set_intersect(half
, isl_set_copy(set
));
7743 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7747 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7749 half
= isl_set_intersect(half
, set
);
7750 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7756 /* Call "fn" on the intersections of "set" with each of the orthants
7757 * (except for obviously empty intersections). The orthant is identified
7758 * by the signs array, with each entry having value 1 or -1 according
7759 * to the sign of the corresponding variable.
7761 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7762 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7772 if (isl_set_plain_is_empty(set
))
7775 nparam
= isl_set_dim(set
, isl_dim_param
);
7776 nvar
= isl_set_dim(set
, isl_dim_set
);
7778 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7780 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7788 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7790 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7793 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7794 __isl_keep isl_basic_map
*bmap2
)
7797 struct isl_map
*map1
;
7798 struct isl_map
*map2
;
7800 if (!bmap1
|| !bmap2
)
7801 return isl_bool_error
;
7803 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7804 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7806 is_subset
= isl_map_is_subset(map1
, map2
);
7814 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7815 __isl_keep isl_basic_set
*bset2
)
7817 return isl_basic_map_is_subset(bset1
, bset2
);
7820 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7821 __isl_keep isl_basic_map
*bmap2
)
7825 if (!bmap1
|| !bmap2
)
7826 return isl_bool_error
;
7827 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7828 if (is_subset
!= isl_bool_true
)
7830 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7834 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7835 __isl_keep isl_basic_set
*bset2
)
7837 return isl_basic_map_is_equal(
7838 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7841 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7847 return isl_bool_error
;
7848 for (i
= 0; i
< map
->n
; ++i
) {
7849 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7851 return isl_bool_error
;
7853 return isl_bool_false
;
7855 return isl_bool_true
;
7858 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7860 return map
? map
->n
== 0 : isl_bool_error
;
7863 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7865 return set
? set
->n
== 0 : isl_bool_error
;
7868 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7870 return isl_map_is_empty((struct isl_map
*)set
);
7873 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7878 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7881 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7886 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7889 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7894 return isl_bool_error
;
7895 is_subset
= isl_map_is_subset(map1
, map2
);
7896 if (is_subset
!= isl_bool_true
)
7898 is_subset
= isl_map_is_subset(map2
, map1
);
7902 /* Is "map1" equal to "map2"?
7904 * First check if they are obviously equal.
7905 * If not, then perform a more detailed analysis.
7907 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7911 equal
= isl_map_plain_is_equal(map1
, map2
);
7912 if (equal
< 0 || equal
)
7914 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7917 isl_bool
isl_basic_map_is_strict_subset(
7918 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7922 if (!bmap1
|| !bmap2
)
7923 return isl_bool_error
;
7924 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7925 if (is_subset
!= isl_bool_true
)
7927 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7928 if (is_subset
== isl_bool_error
)
7933 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7934 __isl_keep isl_map
*map2
)
7939 return isl_bool_error
;
7940 is_subset
= isl_map_is_subset(map1
, map2
);
7941 if (is_subset
!= isl_bool_true
)
7943 is_subset
= isl_map_is_subset(map2
, map1
);
7944 if (is_subset
== isl_bool_error
)
7949 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7950 __isl_keep isl_set
*set2
)
7952 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7955 /* Is "bmap" obviously equal to the universe with the same space?
7957 * That is, does it not have any constraints?
7959 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7962 return isl_bool_error
;
7963 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7966 /* Is "bset" obviously equal to the universe with the same space?
7968 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7970 return isl_basic_map_plain_is_universe(bset
);
7973 /* If "c" does not involve any existentially quantified variables,
7974 * then set *univ to false and abort
7976 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7978 isl_bool
*univ
= user
;
7981 n
= isl_constraint_dim(c
, isl_dim_div
);
7982 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
7983 isl_constraint_free(c
);
7984 if (*univ
< 0 || !*univ
)
7985 return isl_stat_error
;
7989 /* Is "bmap" equal to the universe with the same space?
7991 * First check if it is obviously equal to the universe.
7992 * If not and if there are any constraints not involving
7993 * existentially quantified variables, then it is certainly
7994 * not equal to the universe.
7995 * Otherwise, check if the universe is a subset of "bmap".
7997 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8000 isl_basic_map
*test
;
8002 univ
= isl_basic_map_plain_is_universe(bmap
);
8003 if (univ
< 0 || univ
)
8005 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8006 return isl_bool_false
;
8007 univ
= isl_bool_true
;
8008 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8010 return isl_bool_error
;
8011 if (univ
< 0 || !univ
)
8013 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8014 univ
= isl_basic_map_is_subset(test
, bmap
);
8015 isl_basic_map_free(test
);
8019 /* Is "bset" equal to the universe with the same space?
8021 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8023 return isl_basic_map_is_universe(bset
);
8026 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8031 return isl_bool_error
;
8033 for (i
= 0; i
< map
->n
; ++i
) {
8034 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8039 return isl_bool_false
;
8042 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8044 return isl_map_plain_is_universe((isl_map
*) set
);
8047 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8049 struct isl_basic_set
*bset
= NULL
;
8050 struct isl_vec
*sample
= NULL
;
8051 isl_bool empty
, non_empty
;
8054 return isl_bool_error
;
8056 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8057 return isl_bool_true
;
8059 if (isl_basic_map_plain_is_universe(bmap
))
8060 return isl_bool_false
;
8062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8063 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8064 copy
= isl_basic_map_remove_redundancies(copy
);
8065 empty
= isl_basic_map_plain_is_empty(copy
);
8066 isl_basic_map_free(copy
);
8070 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8072 return isl_bool_error
;
8074 return isl_bool_false
;
8075 isl_vec_free(bmap
->sample
);
8076 bmap
->sample
= NULL
;
8077 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8079 return isl_bool_error
;
8080 sample
= isl_basic_set_sample_vec(bset
);
8082 return isl_bool_error
;
8083 empty
= sample
->size
== 0;
8084 isl_vec_free(bmap
->sample
);
8085 bmap
->sample
= sample
;
8087 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8092 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8095 return isl_bool_error
;
8096 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8099 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8102 return isl_bool_error
;
8103 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8106 /* Is "bmap" known to be non-empty?
8108 * That is, is the cached sample still valid?
8110 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8115 return isl_bool_error
;
8117 return isl_bool_false
;
8118 total
= 1 + isl_basic_map_total_dim(bmap
);
8119 if (bmap
->sample
->size
!= total
)
8120 return isl_bool_false
;
8121 return isl_basic_map_contains(bmap
, bmap
->sample
);
8124 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8126 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8129 struct isl_map
*isl_basic_map_union(
8130 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8132 struct isl_map
*map
;
8133 if (!bmap1
|| !bmap2
)
8136 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8138 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8141 map
= isl_map_add_basic_map(map
, bmap1
);
8142 map
= isl_map_add_basic_map(map
, bmap2
);
8145 isl_basic_map_free(bmap1
);
8146 isl_basic_map_free(bmap2
);
8150 struct isl_set
*isl_basic_set_union(
8151 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8153 return (struct isl_set
*)isl_basic_map_union(
8154 (struct isl_basic_map
*)bset1
,
8155 (struct isl_basic_map
*)bset2
);
8158 /* Order divs such that any div only depends on previous divs */
8159 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8167 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8169 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8171 if (isl_int_is_zero(bmap
->div
[i
][0]))
8173 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8178 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8179 "integer division depends on itself",
8180 return isl_basic_map_free(bmap
));
8181 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8187 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8189 return (struct isl_basic_set
*)
8190 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8193 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8200 for (i
= 0; i
< map
->n
; ++i
) {
8201 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8212 /* Apply the expansion computed by isl_merge_divs.
8213 * The expansion itself is given by "exp" while the resulting
8214 * list of divs is given by "div".
8216 * Move the integer divisions of "bmap" into the right position
8217 * according to "exp" and then introduce the additional integer
8218 * divisions, adding div constraints.
8219 * The moving should be done first to avoid moving coefficients
8220 * in the definitions of the extra integer divisions.
8222 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8223 __isl_take isl_basic_set
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8228 bmap
= isl_basic_map_cow(bmap
);
8232 if (div
->n_row
< bmap
->n_div
)
8233 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8234 "not an expansion", goto error
);
8236 n_div
= bmap
->n_div
;
8237 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8238 div
->n_row
- n_div
, 0,
8239 2 * (div
->n_row
- n_div
));
8241 for (i
= n_div
; i
< div
->n_row
; ++i
)
8242 if (isl_basic_map_alloc_div(bmap
) < 0)
8245 for (j
= n_div
- 1; j
>= 0; --j
) {
8248 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8251 for (i
= 0; i
< div
->n_row
; ++i
) {
8252 if (j
< n_div
&& exp
[j
] == i
) {
8255 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8256 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8258 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8266 isl_basic_map_free(bmap
);
8271 /* Apply the expansion computed by isl_merge_divs.
8272 * The expansion itself is given by "exp" while the resulting
8273 * list of divs is given by "div".
8275 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8276 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8278 return isl_basic_map_expand_divs(bset
, div
, exp
);
8281 /* Look for a div in dst that corresponds to the div "div" in src.
8282 * The divs before "div" in src and dst are assumed to be the same.
8284 * Returns -1 if no corresponding div was found and the position
8285 * of the corresponding div in dst otherwise.
8287 static int find_div(struct isl_basic_map
*dst
,
8288 struct isl_basic_map
*src
, unsigned div
)
8292 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8294 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8295 for (i
= div
; i
< dst
->n_div
; ++i
)
8296 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8297 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8298 dst
->n_div
- div
) == -1)
8303 /* Align the divs of "dst" to those of "src", adding divs from "src"
8304 * if needed. That is, make sure that the first src->n_div divs
8305 * of the result are equal to those of src.
8307 * The result is not finalized as by design it will have redundant
8308 * divs if any divs from "src" were copied.
8310 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8311 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8314 int known
, extended
;
8318 return isl_basic_map_free(dst
);
8320 if (src
->n_div
== 0)
8323 known
= isl_basic_map_divs_known(src
);
8325 return isl_basic_map_free(dst
);
8327 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8328 "some src divs are unknown",
8329 return isl_basic_map_free(dst
));
8331 src
= isl_basic_map_order_divs(src
);
8334 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8335 for (i
= 0; i
< src
->n_div
; ++i
) {
8336 int j
= find_div(dst
, src
, i
);
8339 int extra
= src
->n_div
- i
;
8340 dst
= isl_basic_map_cow(dst
);
8343 dst
= isl_basic_map_extend_space(dst
,
8344 isl_space_copy(dst
->dim
),
8345 extra
, 0, 2 * extra
);
8348 j
= isl_basic_map_alloc_div(dst
);
8350 return isl_basic_map_free(dst
);
8351 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8352 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8353 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8354 return isl_basic_map_free(dst
);
8357 isl_basic_map_swap_div(dst
, i
, j
);
8362 struct isl_basic_set
*isl_basic_set_align_divs(
8363 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8365 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8366 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8369 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8377 map
= isl_map_compute_divs(map
);
8378 map
= isl_map_cow(map
);
8382 for (i
= 1; i
< map
->n
; ++i
)
8383 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8384 for (i
= 1; i
< map
->n
; ++i
) {
8385 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8387 return isl_map_free(map
);
8390 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8394 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8396 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8399 /* Align the divs of the basic maps in "map" to those
8400 * of the basic maps in "list", as well as to the other basic maps in "map".
8401 * The elements in "list" are assumed to have known divs.
8403 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8404 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8408 map
= isl_map_compute_divs(map
);
8409 map
= isl_map_cow(map
);
8411 return isl_map_free(map
);
8415 n
= isl_basic_map_list_n_basic_map(list
);
8416 for (i
= 0; i
< n
; ++i
) {
8417 isl_basic_map
*bmap
;
8419 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8420 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8421 isl_basic_map_free(bmap
);
8424 return isl_map_free(map
);
8426 return isl_map_align_divs(map
);
8429 /* Align the divs of each element of "list" to those of "bmap".
8430 * Both "bmap" and the elements of "list" are assumed to have known divs.
8432 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8433 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8438 return isl_basic_map_list_free(list
);
8440 n
= isl_basic_map_list_n_basic_map(list
);
8441 for (i
= 0; i
< n
; ++i
) {
8442 isl_basic_map
*bmap_i
;
8444 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8445 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8446 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8452 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8453 __isl_take isl_map
*map
)
8457 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8458 map
= isl_map_intersect_domain(map
, set
);
8459 set
= isl_map_range(map
);
8467 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8468 __isl_take isl_map
*map
)
8470 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8473 /* There is no need to cow as removing empty parts doesn't change
8474 * the meaning of the set.
8476 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8483 for (i
= map
->n
- 1; i
>= 0; --i
)
8484 remove_if_empty(map
, i
);
8489 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8491 return (struct isl_set
*)
8492 isl_map_remove_empty_parts((struct isl_map
*)set
);
8495 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8497 struct isl_basic_map
*bmap
;
8498 if (!map
|| map
->n
== 0)
8500 bmap
= map
->p
[map
->n
-1];
8501 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8502 return isl_basic_map_copy(bmap
);
8505 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8507 return (struct isl_basic_set
*)
8508 isl_map_copy_basic_map((struct isl_map
*)set
);
8511 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8512 __isl_keep isl_basic_map
*bmap
)
8518 for (i
= map
->n
-1; i
>= 0; --i
) {
8519 if (map
->p
[i
] != bmap
)
8521 map
= isl_map_cow(map
);
8524 isl_basic_map_free(map
->p
[i
]);
8525 if (i
!= map
->n
-1) {
8526 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8527 map
->p
[i
] = map
->p
[map
->n
-1];
8538 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8539 struct isl_basic_set
*bset
)
8541 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8542 (struct isl_basic_map
*)bset
);
8545 /* Given two basic sets bset1 and bset2, compute the maximal difference
8546 * between the values of dimension pos in bset1 and those in bset2
8547 * for any common value of the parameters and dimensions preceding pos.
8549 static enum isl_lp_result
basic_set_maximal_difference_at(
8550 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8551 int pos
, isl_int
*opt
)
8554 struct isl_basic_map
*bmap1
= NULL
;
8555 struct isl_basic_map
*bmap2
= NULL
;
8556 struct isl_ctx
*ctx
;
8557 struct isl_vec
*obj
;
8560 unsigned dim1
, dim2
;
8561 enum isl_lp_result res
;
8563 if (!bset1
|| !bset2
)
8564 return isl_lp_error
;
8566 nparam
= isl_basic_set_n_param(bset1
);
8567 dim1
= isl_basic_set_n_dim(bset1
);
8568 dim2
= isl_basic_set_n_dim(bset2
);
8569 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8570 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8571 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8572 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8573 if (!bmap1
|| !bmap2
)
8575 bmap1
= isl_basic_map_cow(bmap1
);
8576 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8577 pos
, (dim1
- pos
) + (dim2
- pos
),
8578 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8579 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8582 total
= isl_basic_map_total_dim(bmap1
);
8584 obj
= isl_vec_alloc(ctx
, 1 + total
);
8587 isl_seq_clr(obj
->block
.data
, 1 + total
);
8588 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8589 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8590 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8592 isl_basic_map_free(bmap1
);
8596 isl_basic_map_free(bmap2
);
8598 isl_basic_map_free(bmap1
);
8599 return isl_lp_error
;
8602 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8603 * for any common value of the parameters and dimensions preceding pos
8604 * in both basic sets, the values of dimension pos in bset1 are
8605 * smaller or larger than those in bset2.
8608 * 1 if bset1 follows bset2
8609 * -1 if bset1 precedes bset2
8610 * 0 if bset1 and bset2 are incomparable
8611 * -2 if some error occurred.
8613 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8614 struct isl_basic_set
*bset2
, int pos
)
8617 enum isl_lp_result res
;
8622 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8624 if (res
== isl_lp_empty
)
8626 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8627 res
== isl_lp_unbounded
)
8629 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8638 /* Given two basic sets bset1 and bset2, check whether
8639 * for any common value of the parameters and dimensions preceding pos
8640 * there is a value of dimension pos in bset1 that is larger
8641 * than a value of the same dimension in bset2.
8644 * 1 if there exists such a pair
8645 * 0 if there is no such pair, but there is a pair of equal values
8647 * -2 if some error occurred.
8649 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8650 __isl_keep isl_basic_set
*bset2
, int pos
)
8653 enum isl_lp_result res
;
8658 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8660 if (res
== isl_lp_empty
)
8662 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8663 res
== isl_lp_unbounded
)
8665 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8667 else if (res
== isl_lp_ok
)
8676 /* Given two sets set1 and set2, check whether
8677 * for any common value of the parameters and dimensions preceding pos
8678 * there is a value of dimension pos in set1 that is larger
8679 * than a value of the same dimension in set2.
8682 * 1 if there exists such a pair
8683 * 0 if there is no such pair, but there is a pair of equal values
8685 * -2 if some error occurred.
8687 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8688 __isl_keep isl_set
*set2
, int pos
)
8696 for (i
= 0; i
< set1
->n
; ++i
)
8697 for (j
= 0; j
< set2
->n
; ++j
) {
8699 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8700 if (f
== 1 || f
== -2)
8709 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8710 unsigned pos
, isl_int
*val
)
8718 total
= isl_basic_map_total_dim(bmap
);
8719 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8720 for (; d
+1 > pos
; --d
)
8721 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8725 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8727 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8729 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8732 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8738 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8739 unsigned pos
, isl_int
*val
)
8751 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8754 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8755 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8756 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8757 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8761 isl_int_set(*val
, v
);
8767 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8768 unsigned pos
, isl_int
*val
)
8770 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8774 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8777 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8780 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8781 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8783 if (pos
>= isl_basic_map_dim(bmap
, type
))
8785 return isl_basic_map_plain_has_fixed_var(bmap
,
8786 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8789 /* If "bmap" obviously lies on a hyperplane where the given dimension
8790 * has a fixed value, then return that value.
8791 * Otherwise return NaN.
8793 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8794 __isl_keep isl_basic_map
*bmap
,
8795 enum isl_dim_type type
, unsigned pos
)
8803 ctx
= isl_basic_map_get_ctx(bmap
);
8804 v
= isl_val_alloc(ctx
);
8807 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8809 return isl_val_free(v
);
8811 isl_int_set_si(v
->d
, 1);
8815 return isl_val_nan(ctx
);
8818 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8819 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8821 if (pos
>= isl_map_dim(map
, type
))
8823 return isl_map_plain_has_fixed_var(map
,
8824 map_offset(map
, type
) - 1 + pos
, val
);
8827 /* If "map" obviously lies on a hyperplane where the given dimension
8828 * has a fixed value, then return that value.
8829 * Otherwise return NaN.
8831 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8832 enum isl_dim_type type
, unsigned pos
)
8840 ctx
= isl_map_get_ctx(map
);
8841 v
= isl_val_alloc(ctx
);
8844 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8846 return isl_val_free(v
);
8848 isl_int_set_si(v
->d
, 1);
8852 return isl_val_nan(ctx
);
8855 /* If "set" obviously lies on a hyperplane where the given dimension
8856 * has a fixed value, then return that value.
8857 * Otherwise return NaN.
8859 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8860 enum isl_dim_type type
, unsigned pos
)
8862 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8865 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8866 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8868 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8871 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8872 * then return this fixed value in *val.
8874 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8875 unsigned dim
, isl_int
*val
)
8877 return isl_basic_set_plain_has_fixed_var(bset
,
8878 isl_basic_set_n_param(bset
) + dim
, val
);
8881 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8882 * then return this fixed value in *val.
8884 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8885 unsigned dim
, isl_int
*val
)
8887 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8890 /* Check if input variable in has fixed value and if so and if val is not NULL,
8891 * then return this fixed value in *val.
8893 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8894 unsigned in
, isl_int
*val
)
8896 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8899 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8900 * and if val is not NULL, then return this lower bound in *val.
8902 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8903 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8905 int i
, i_eq
= -1, i_ineq
= -1;
8912 total
= isl_basic_set_total_dim(bset
);
8913 nparam
= isl_basic_set_n_param(bset
);
8914 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8915 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8921 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8922 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8924 if (i_eq
!= -1 || i_ineq
!= -1)
8928 if (i_eq
== -1 && i_ineq
== -1)
8930 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8931 /* The coefficient should always be one due to normalization. */
8932 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8934 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8936 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8937 total
- nparam
- dim
- 1) != -1)
8940 isl_int_neg(*val
, c
[0]);
8944 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8945 unsigned dim
, isl_int
*val
)
8957 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8961 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8963 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8964 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8966 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8970 isl_int_set(*val
, v
);
8976 /* Return -1 if the constraint "c1" should be sorted before "c2"
8977 * and 1 if it should be sorted after "c2".
8978 * Return 0 if the two constraints are the same (up to the constant term).
8980 * In particular, if a constraint involves later variables than another
8981 * then it is sorted after this other constraint.
8982 * uset_gist depends on constraints without existentially quantified
8983 * variables sorting first.
8985 * For constraints that have the same latest variable, those
8986 * with the same coefficient for this latest variable (first in absolute value
8987 * and then in actual value) are grouped together.
8988 * This is useful for detecting pairs of constraints that can
8989 * be chained in their printed representation.
8991 * Finally, within a group, constraints are sorted according to
8992 * their coefficients (excluding the constant term).
8994 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8996 isl_int
**c1
= (isl_int
**) p1
;
8997 isl_int
**c2
= (isl_int
**) p2
;
8999 unsigned size
= *(unsigned *) arg
;
9002 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9003 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9008 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9011 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9015 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9018 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9019 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9020 * and 0 if the two constraints are the same (up to the constant term).
9022 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9023 isl_int
*c1
, isl_int
*c2
)
9029 total
= isl_basic_map_total_dim(bmap
);
9030 return sort_constraint_cmp(&c1
, &c2
, &total
);
9033 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9034 __isl_take isl_basic_map
*bmap
)
9040 if (bmap
->n_ineq
== 0)
9042 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9044 total
= isl_basic_map_total_dim(bmap
);
9045 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9046 &sort_constraint_cmp
, &total
) < 0)
9047 return isl_basic_map_free(bmap
);
9051 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9052 __isl_take isl_basic_set
*bset
)
9054 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9055 (struct isl_basic_map
*)bset
);
9058 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9064 bmap
= isl_basic_map_remove_redundancies(bmap
);
9065 bmap
= isl_basic_map_sort_constraints(bmap
);
9067 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9071 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9073 return (struct isl_basic_set
*)isl_basic_map_normalize(
9074 (struct isl_basic_map
*)bset
);
9077 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9078 const __isl_keep isl_basic_map
*bmap2
)
9083 if (!bmap1
|| !bmap2
)
9088 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9089 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9090 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9091 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9092 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9093 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9094 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9095 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9096 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9097 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9098 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9100 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9102 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9104 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9105 return bmap1
->n_eq
- bmap2
->n_eq
;
9106 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9107 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9108 if (bmap1
->n_div
!= bmap2
->n_div
)
9109 return bmap1
->n_div
- bmap2
->n_div
;
9110 total
= isl_basic_map_total_dim(bmap1
);
9111 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9112 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9116 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9117 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9121 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9122 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9129 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9130 const __isl_keep isl_basic_set
*bset2
)
9132 return isl_basic_map_plain_cmp(bset1
, bset2
);
9135 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9141 if (set1
->n
!= set2
->n
)
9142 return set1
->n
- set2
->n
;
9144 for (i
= 0; i
< set1
->n
; ++i
) {
9145 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9153 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9154 __isl_keep isl_basic_map
*bmap2
)
9156 if (!bmap1
|| !bmap2
)
9157 return isl_bool_error
;
9158 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9161 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9162 __isl_keep isl_basic_set
*bset2
)
9164 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9165 (isl_basic_map
*)bset2
);
9168 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9170 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9171 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9173 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9176 /* Sort the basic maps of "map" and remove duplicate basic maps.
9178 * While removing basic maps, we make sure that the basic maps remain
9179 * sorted because isl_map_normalize expects the basic maps of the result
9182 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9186 map
= isl_map_remove_empty_parts(map
);
9189 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9190 for (i
= map
->n
- 1; i
>= 1; --i
) {
9191 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9193 isl_basic_map_free(map
->p
[i
-1]);
9194 for (j
= i
; j
< map
->n
; ++j
)
9195 map
->p
[j
- 1] = map
->p
[j
];
9202 /* Remove obvious duplicates among the basic maps of "map".
9204 * Unlike isl_map_normalize, this function does not remove redundant
9205 * constraints and only removes duplicates that have exactly the same
9206 * constraints in the input. It does sort the constraints and
9207 * the basic maps to ease the detection of duplicates.
9209 * If "map" has already been normalized or if the basic maps are
9210 * disjoint, then there can be no duplicates.
9212 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9215 isl_basic_map
*bmap
;
9221 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9223 for (i
= 0; i
< map
->n
; ++i
) {
9224 bmap
= isl_basic_map_copy(map
->p
[i
]);
9225 bmap
= isl_basic_map_sort_constraints(bmap
);
9227 return isl_map_free(map
);
9228 isl_basic_map_free(map
->p
[i
]);
9232 map
= sort_and_remove_duplicates(map
);
9236 /* We normalize in place, but if anything goes wrong we need
9237 * to return NULL, so we need to make sure we don't change the
9238 * meaning of any possible other copies of map.
9240 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9243 struct isl_basic_map
*bmap
;
9247 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9249 for (i
= 0; i
< map
->n
; ++i
) {
9250 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9253 isl_basic_map_free(map
->p
[i
]);
9257 map
= sort_and_remove_duplicates(map
);
9259 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9266 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9268 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9271 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9272 __isl_keep isl_map
*map2
)
9278 return isl_bool_error
;
9281 return isl_bool_true
;
9282 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9283 return isl_bool_false
;
9285 map1
= isl_map_copy(map1
);
9286 map2
= isl_map_copy(map2
);
9287 map1
= isl_map_normalize(map1
);
9288 map2
= isl_map_normalize(map2
);
9291 equal
= map1
->n
== map2
->n
;
9292 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9293 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9303 return isl_bool_error
;
9306 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9307 __isl_keep isl_set
*set2
)
9309 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9310 (struct isl_map
*)set2
);
9313 /* Return an interval that ranges from min to max (inclusive)
9315 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9316 isl_int min
, isl_int max
)
9319 struct isl_basic_set
*bset
= NULL
;
9321 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9325 k
= isl_basic_set_alloc_inequality(bset
);
9328 isl_int_set_si(bset
->ineq
[k
][1], 1);
9329 isl_int_neg(bset
->ineq
[k
][0], min
);
9331 k
= isl_basic_set_alloc_inequality(bset
);
9334 isl_int_set_si(bset
->ineq
[k
][1], -1);
9335 isl_int_set(bset
->ineq
[k
][0], max
);
9339 isl_basic_set_free(bset
);
9343 /* Return the basic maps in "map" as a list.
9345 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9346 __isl_keep isl_map
*map
)
9350 isl_basic_map_list
*list
;
9354 ctx
= isl_map_get_ctx(map
);
9355 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9357 for (i
= 0; i
< map
->n
; ++i
) {
9358 isl_basic_map
*bmap
;
9360 bmap
= isl_basic_map_copy(map
->p
[i
]);
9361 list
= isl_basic_map_list_add(list
, bmap
);
9367 /* Return the intersection of the elements in the non-empty list "list".
9368 * All elements are assumed to live in the same space.
9370 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9371 __isl_take isl_basic_map_list
*list
)
9374 isl_basic_map
*bmap
;
9378 n
= isl_basic_map_list_n_basic_map(list
);
9380 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9381 "expecting non-empty list", goto error
);
9383 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9384 for (i
= 1; i
< n
; ++i
) {
9385 isl_basic_map
*bmap_i
;
9387 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9388 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9391 isl_basic_map_list_free(list
);
9394 isl_basic_map_list_free(list
);
9398 /* Return the intersection of the elements in the non-empty list "list".
9399 * All elements are assumed to live in the same space.
9401 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9402 __isl_take isl_basic_set_list
*list
)
9404 return isl_basic_map_list_intersect(list
);
9407 /* Return the union of the elements in the non-empty list "list".
9408 * All elements are assumed to live in the same space.
9410 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9417 n
= isl_set_list_n_set(list
);
9419 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9420 "expecting non-empty list", goto error
);
9422 set
= isl_set_list_get_set(list
, 0);
9423 for (i
= 1; i
< n
; ++i
) {
9426 set_i
= isl_set_list_get_set(list
, i
);
9427 set
= isl_set_union(set
, set_i
);
9430 isl_set_list_free(list
);
9433 isl_set_list_free(list
);
9437 /* Return the Cartesian product of the basic sets in list (in the given order).
9439 __isl_give isl_basic_set
*isl_basic_set_list_product(
9440 __isl_take
struct isl_basic_set_list
*list
)
9448 struct isl_basic_set
*product
= NULL
;
9452 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9453 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9454 nparam
= isl_basic_set_n_param(list
->p
[0]);
9455 dim
= isl_basic_set_n_dim(list
->p
[0]);
9456 extra
= list
->p
[0]->n_div
;
9457 n_eq
= list
->p
[0]->n_eq
;
9458 n_ineq
= list
->p
[0]->n_ineq
;
9459 for (i
= 1; i
< list
->n
; ++i
) {
9460 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9461 isl_assert(list
->ctx
,
9462 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9463 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9464 extra
+= list
->p
[i
]->n_div
;
9465 n_eq
+= list
->p
[i
]->n_eq
;
9466 n_ineq
+= list
->p
[i
]->n_ineq
;
9468 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9473 for (i
= 0; i
< list
->n
; ++i
) {
9474 isl_basic_set_add_constraints(product
,
9475 isl_basic_set_copy(list
->p
[i
]), dim
);
9476 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9478 isl_basic_set_list_free(list
);
9481 isl_basic_set_free(product
);
9482 isl_basic_set_list_free(list
);
9486 struct isl_basic_map
*isl_basic_map_product(
9487 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9489 isl_space
*dim_result
= NULL
;
9490 struct isl_basic_map
*bmap
;
9491 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9492 struct isl_dim_map
*dim_map1
, *dim_map2
;
9494 if (!bmap1
|| !bmap2
)
9497 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9498 bmap2
->dim
, isl_dim_param
), goto error
);
9499 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9500 isl_space_copy(bmap2
->dim
));
9502 in1
= isl_basic_map_n_in(bmap1
);
9503 in2
= isl_basic_map_n_in(bmap2
);
9504 out1
= isl_basic_map_n_out(bmap1
);
9505 out2
= isl_basic_map_n_out(bmap2
);
9506 nparam
= isl_basic_map_n_param(bmap1
);
9508 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9509 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9510 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9511 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9512 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9513 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9514 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9515 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9516 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9517 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9518 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9520 bmap
= isl_basic_map_alloc_space(dim_result
,
9521 bmap1
->n_div
+ bmap2
->n_div
,
9522 bmap1
->n_eq
+ bmap2
->n_eq
,
9523 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9524 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9525 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9526 bmap
= isl_basic_map_simplify(bmap
);
9527 return isl_basic_map_finalize(bmap
);
9529 isl_basic_map_free(bmap1
);
9530 isl_basic_map_free(bmap2
);
9534 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9535 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9537 isl_basic_map
*prod
;
9539 prod
= isl_basic_map_product(bmap1
, bmap2
);
9540 prod
= isl_basic_map_flatten(prod
);
9544 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9545 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9547 return isl_basic_map_flat_range_product(bset1
, bset2
);
9550 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9551 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9553 isl_space
*space_result
= NULL
;
9554 isl_basic_map
*bmap
;
9555 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9556 struct isl_dim_map
*dim_map1
, *dim_map2
;
9558 if (!bmap1
|| !bmap2
)
9561 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9562 isl_space_copy(bmap2
->dim
));
9564 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9565 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9566 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9567 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9569 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9570 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9571 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9572 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9573 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9574 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9575 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9576 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9577 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9578 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9579 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9581 bmap
= isl_basic_map_alloc_space(space_result
,
9582 bmap1
->n_div
+ bmap2
->n_div
,
9583 bmap1
->n_eq
+ bmap2
->n_eq
,
9584 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9585 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9586 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9587 bmap
= isl_basic_map_simplify(bmap
);
9588 return isl_basic_map_finalize(bmap
);
9590 isl_basic_map_free(bmap1
);
9591 isl_basic_map_free(bmap2
);
9595 __isl_give isl_basic_map
*isl_basic_map_range_product(
9596 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9598 isl_space
*dim_result
= NULL
;
9599 isl_basic_map
*bmap
;
9600 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9601 struct isl_dim_map
*dim_map1
, *dim_map2
;
9603 if (!bmap1
|| !bmap2
)
9606 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9607 bmap2
->dim
, isl_dim_param
))
9608 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9609 "parameters don't match", goto error
);
9611 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9612 isl_space_copy(bmap2
->dim
));
9614 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9615 out1
= isl_basic_map_n_out(bmap1
);
9616 out2
= isl_basic_map_n_out(bmap2
);
9617 nparam
= isl_basic_map_n_param(bmap1
);
9619 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9620 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9621 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9622 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9623 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9624 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9625 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9626 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9627 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9628 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9629 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9631 bmap
= isl_basic_map_alloc_space(dim_result
,
9632 bmap1
->n_div
+ bmap2
->n_div
,
9633 bmap1
->n_eq
+ bmap2
->n_eq
,
9634 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9635 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9636 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9637 bmap
= isl_basic_map_simplify(bmap
);
9638 return isl_basic_map_finalize(bmap
);
9640 isl_basic_map_free(bmap1
);
9641 isl_basic_map_free(bmap2
);
9645 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9646 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9648 isl_basic_map
*prod
;
9650 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9651 prod
= isl_basic_map_flatten_range(prod
);
9655 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9656 * and collect the results.
9657 * The result live in the space obtained by calling "space_product"
9658 * on the spaces of "map1" and "map2".
9659 * If "remove_duplicates" is set then the result may contain duplicates
9660 * (even if the inputs do not) and so we try and remove the obvious
9663 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9664 __isl_take isl_map
*map2
,
9665 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9666 __isl_take isl_space
*right
),
9667 __isl_give isl_basic_map
*(*basic_map_product
)(
9668 __isl_take isl_basic_map
*left
,
9669 __isl_take isl_basic_map
*right
),
9670 int remove_duplicates
)
9673 struct isl_map
*result
;
9679 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9680 map2
->dim
, isl_dim_param
), goto error
);
9682 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9683 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9684 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9686 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9687 isl_space_copy(map2
->dim
)),
9688 map1
->n
* map2
->n
, flags
);
9691 for (i
= 0; i
< map1
->n
; ++i
)
9692 for (j
= 0; j
< map2
->n
; ++j
) {
9693 struct isl_basic_map
*part
;
9694 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9695 isl_basic_map_copy(map2
->p
[j
]));
9696 if (isl_basic_map_is_empty(part
))
9697 isl_basic_map_free(part
);
9699 result
= isl_map_add_basic_map(result
, part
);
9703 if (remove_duplicates
)
9704 result
= isl_map_remove_obvious_duplicates(result
);
9714 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9716 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9717 __isl_take isl_map
*map2
)
9719 return map_product(map1
, map2
, &isl_space_product
,
9720 &isl_basic_map_product
, 0);
9723 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9724 __isl_take isl_map
*map2
)
9726 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9729 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9731 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9732 __isl_take isl_map
*map2
)
9736 prod
= isl_map_product(map1
, map2
);
9737 prod
= isl_map_flatten(prod
);
9741 /* Given two set A and B, construct its Cartesian product A x B.
9743 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9745 return isl_map_range_product(set1
, set2
);
9748 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9749 __isl_take isl_set
*set2
)
9751 return isl_map_flat_range_product(set1
, set2
);
9754 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9756 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9757 __isl_take isl_map
*map2
)
9759 return map_product(map1
, map2
, &isl_space_domain_product
,
9760 &isl_basic_map_domain_product
, 1);
9763 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9765 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9766 __isl_take isl_map
*map2
)
9768 return map_product(map1
, map2
, &isl_space_range_product
,
9769 &isl_basic_map_range_product
, 1);
9772 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9773 __isl_take isl_map
*map2
)
9775 return isl_map_align_params_map_map_and(map1
, map2
,
9776 &map_domain_product_aligned
);
9779 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9780 __isl_take isl_map
*map2
)
9782 return isl_map_align_params_map_map_and(map1
, map2
,
9783 &map_range_product_aligned
);
9786 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9788 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9791 int total1
, keep1
, total2
, keep2
;
9795 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9796 !isl_space_range_is_wrapping(map
->dim
))
9797 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9798 "not a product", return isl_map_free(map
));
9800 space
= isl_map_get_space(map
);
9801 total1
= isl_space_dim(space
, isl_dim_in
);
9802 total2
= isl_space_dim(space
, isl_dim_out
);
9803 space
= isl_space_factor_domain(space
);
9804 keep1
= isl_space_dim(space
, isl_dim_in
);
9805 keep2
= isl_space_dim(space
, isl_dim_out
);
9806 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9807 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9808 map
= isl_map_reset_space(map
, space
);
9813 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9815 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9818 int total1
, keep1
, total2
, keep2
;
9822 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9823 !isl_space_range_is_wrapping(map
->dim
))
9824 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9825 "not a product", return isl_map_free(map
));
9827 space
= isl_map_get_space(map
);
9828 total1
= isl_space_dim(space
, isl_dim_in
);
9829 total2
= isl_space_dim(space
, isl_dim_out
);
9830 space
= isl_space_factor_range(space
);
9831 keep1
= isl_space_dim(space
, isl_dim_in
);
9832 keep2
= isl_space_dim(space
, isl_dim_out
);
9833 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9834 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9835 map
= isl_map_reset_space(map
, space
);
9840 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9842 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9849 if (!isl_space_domain_is_wrapping(map
->dim
))
9850 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9851 "domain is not a product", return isl_map_free(map
));
9853 space
= isl_map_get_space(map
);
9854 total
= isl_space_dim(space
, isl_dim_in
);
9855 space
= isl_space_domain_factor_domain(space
);
9856 keep
= isl_space_dim(space
, isl_dim_in
);
9857 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9858 map
= isl_map_reset_space(map
, space
);
9863 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9865 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9872 if (!isl_space_domain_is_wrapping(map
->dim
))
9873 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9874 "domain is not a product", return isl_map_free(map
));
9876 space
= isl_map_get_space(map
);
9877 total
= isl_space_dim(space
, isl_dim_in
);
9878 space
= isl_space_domain_factor_range(space
);
9879 keep
= isl_space_dim(space
, isl_dim_in
);
9880 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9881 map
= isl_map_reset_space(map
, space
);
9886 /* Given a map A -> [B -> C], extract the map A -> B.
9888 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9895 if (!isl_space_range_is_wrapping(map
->dim
))
9896 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9897 "range is not a product", return isl_map_free(map
));
9899 space
= isl_map_get_space(map
);
9900 total
= isl_space_dim(space
, isl_dim_out
);
9901 space
= isl_space_range_factor_domain(space
);
9902 keep
= isl_space_dim(space
, isl_dim_out
);
9903 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9904 map
= isl_map_reset_space(map
, space
);
9909 /* Given a map A -> [B -> C], extract the map A -> C.
9911 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9918 if (!isl_space_range_is_wrapping(map
->dim
))
9919 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9920 "range is not a product", return isl_map_free(map
));
9922 space
= isl_map_get_space(map
);
9923 total
= isl_space_dim(space
, isl_dim_out
);
9924 space
= isl_space_range_factor_range(space
);
9925 keep
= isl_space_dim(space
, isl_dim_out
);
9926 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9927 map
= isl_map_reset_space(map
, space
);
9932 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9934 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9935 __isl_take isl_map
*map2
)
9939 prod
= isl_map_domain_product(map1
, map2
);
9940 prod
= isl_map_flatten_domain(prod
);
9944 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9946 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9947 __isl_take isl_map
*map2
)
9951 prod
= isl_map_range_product(map1
, map2
);
9952 prod
= isl_map_flatten_range(prod
);
9956 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9959 uint32_t hash
= isl_hash_init();
9964 bmap
= isl_basic_map_copy(bmap
);
9965 bmap
= isl_basic_map_normalize(bmap
);
9968 total
= isl_basic_map_total_dim(bmap
);
9969 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9970 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9972 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9973 isl_hash_hash(hash
, c_hash
);
9975 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9976 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9978 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9979 isl_hash_hash(hash
, c_hash
);
9981 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9982 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9984 if (isl_int_is_zero(bmap
->div
[i
][0]))
9986 isl_hash_byte(hash
, i
& 0xFF);
9987 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9988 isl_hash_hash(hash
, c_hash
);
9990 isl_basic_map_free(bmap
);
9994 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9996 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9999 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10006 map
= isl_map_copy(map
);
10007 map
= isl_map_normalize(map
);
10011 hash
= isl_hash_init();
10012 for (i
= 0; i
< map
->n
; ++i
) {
10013 uint32_t bmap_hash
;
10014 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10015 isl_hash_hash(hash
, bmap_hash
);
10023 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10025 return isl_map_get_hash((isl_map
*)set
);
10028 /* Check if the value for dimension dim is completely determined
10029 * by the values of the other parameters and variables.
10030 * That is, check if dimension dim is involved in an equality.
10032 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10039 nparam
= isl_basic_set_n_param(bset
);
10040 for (i
= 0; i
< bset
->n_eq
; ++i
)
10041 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10046 /* Check if the value for dimension dim is completely determined
10047 * by the values of the other parameters and variables.
10048 * That is, check if dimension dim is involved in an equality
10049 * for each of the subsets.
10051 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10057 for (i
= 0; i
< set
->n
; ++i
) {
10059 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10066 /* Return the number of basic maps in the (current) representation of "map".
10068 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10070 return map
? map
->n
: 0;
10073 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10075 return set
? set
->n
: 0;
10078 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10079 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10084 return isl_stat_error
;
10086 for (i
= 0; i
< map
->n
; ++i
)
10087 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10088 return isl_stat_error
;
10090 return isl_stat_ok
;
10093 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10094 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10099 return isl_stat_error
;
10101 for (i
= 0; i
< set
->n
; ++i
)
10102 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10103 return isl_stat_error
;
10105 return isl_stat_ok
;
10108 /* Return a list of basic sets, the union of which is equal to "set".
10110 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10111 __isl_keep isl_set
*set
)
10114 isl_basic_set_list
*list
;
10119 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10120 for (i
= 0; i
< set
->n
; ++i
) {
10121 isl_basic_set
*bset
;
10123 bset
= isl_basic_set_copy(set
->p
[i
]);
10124 list
= isl_basic_set_list_add(list
, bset
);
10130 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10137 bset
= isl_basic_set_cow(bset
);
10141 dim
= isl_basic_set_get_space(bset
);
10142 dim
= isl_space_lift(dim
, bset
->n_div
);
10145 isl_space_free(bset
->dim
);
10147 bset
->extra
-= bset
->n_div
;
10150 bset
= isl_basic_set_finalize(bset
);
10154 isl_basic_set_free(bset
);
10158 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10164 set
= isl_set_align_divs(set
);
10169 set
= isl_set_cow(set
);
10173 n_div
= set
->p
[0]->n_div
;
10174 dim
= isl_set_get_space(set
);
10175 dim
= isl_space_lift(dim
, n_div
);
10178 isl_space_free(set
->dim
);
10181 for (i
= 0; i
< set
->n
; ++i
) {
10182 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10193 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10196 struct isl_basic_map
*bmap
;
10203 set
= isl_set_align_divs(set
);
10208 dim
= isl_set_get_space(set
);
10209 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10211 return isl_map_identity(isl_space_map_from_set(dim
));
10214 n_div
= set
->p
[0]->n_div
;
10215 dim
= isl_space_map_from_set(dim
);
10216 n_param
= isl_space_dim(dim
, isl_dim_param
);
10217 n_set
= isl_space_dim(dim
, isl_dim_in
);
10218 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10219 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10220 for (i
= 0; i
< n_set
; ++i
)
10221 bmap
= var_equal(bmap
, i
);
10223 total
= n_param
+ n_set
+ n_set
+ n_div
;
10224 for (i
= 0; i
< n_div
; ++i
) {
10225 k
= isl_basic_map_alloc_inequality(bmap
);
10228 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10229 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10230 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10231 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10232 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10233 set
->p
[0]->div
[i
][0]);
10235 l
= isl_basic_map_alloc_inequality(bmap
);
10238 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10239 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10240 set
->p
[0]->div
[i
][0]);
10241 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10245 bmap
= isl_basic_map_simplify(bmap
);
10246 bmap
= isl_basic_map_finalize(bmap
);
10247 return isl_map_from_basic_map(bmap
);
10250 isl_basic_map_free(bmap
);
10254 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10262 dim
= isl_basic_set_total_dim(bset
);
10263 size
+= bset
->n_eq
* (1 + dim
);
10264 size
+= bset
->n_ineq
* (1 + dim
);
10265 size
+= bset
->n_div
* (2 + dim
);
10270 int isl_set_size(__isl_keep isl_set
*set
)
10278 for (i
= 0; i
< set
->n
; ++i
)
10279 size
+= isl_basic_set_size(set
->p
[i
]);
10284 /* Check if there is any lower bound (if lower == 0) and/or upper
10285 * bound (if upper == 0) on the specified dim.
10287 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10288 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10293 return isl_bool_error
;
10295 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10296 return isl_bool_error
);
10298 pos
+= isl_basic_map_offset(bmap
, type
);
10300 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10301 if (isl_int_is_zero(bmap
->div
[i
][0]))
10303 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10304 return isl_bool_true
;
10307 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10308 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10309 return isl_bool_true
;
10311 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10312 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10319 return lower
&& upper
;
10322 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10323 enum isl_dim_type type
, unsigned pos
)
10325 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10328 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10329 enum isl_dim_type type
, unsigned pos
)
10331 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10334 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10335 enum isl_dim_type type
, unsigned pos
)
10337 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10340 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10341 enum isl_dim_type type
, unsigned pos
)
10348 for (i
= 0; i
< map
->n
; ++i
) {
10350 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10351 if (bounded
< 0 || !bounded
)
10358 /* Return 1 if the specified dim is involved in both an upper bound
10359 * and a lower bound.
10361 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10362 enum isl_dim_type type
, unsigned pos
)
10364 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10367 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10369 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10370 enum isl_dim_type type
, unsigned pos
,
10371 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10372 enum isl_dim_type type
, unsigned pos
))
10377 return isl_bool_error
;
10379 for (i
= 0; i
< map
->n
; ++i
) {
10381 bounded
= fn(map
->p
[i
], type
, pos
);
10382 if (bounded
< 0 || bounded
)
10386 return isl_bool_false
;
10389 /* Return 1 if the specified dim is involved in any lower bound.
10391 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10392 enum isl_dim_type type
, unsigned pos
)
10394 return has_any_bound(set
, type
, pos
,
10395 &isl_basic_map_dim_has_lower_bound
);
10398 /* Return 1 if the specified dim is involved in any upper bound.
10400 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10401 enum isl_dim_type type
, unsigned pos
)
10403 return has_any_bound(set
, type
, pos
,
10404 &isl_basic_map_dim_has_upper_bound
);
10407 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10409 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10410 enum isl_dim_type type
, unsigned pos
,
10411 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10412 enum isl_dim_type type
, unsigned pos
))
10417 return isl_bool_error
;
10419 for (i
= 0; i
< map
->n
; ++i
) {
10421 bounded
= fn(map
->p
[i
], type
, pos
);
10422 if (bounded
< 0 || !bounded
)
10426 return isl_bool_true
;
10429 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10431 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10432 enum isl_dim_type type
, unsigned pos
)
10434 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10437 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10439 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10440 enum isl_dim_type type
, unsigned pos
)
10442 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10445 /* For each of the "n" variables starting at "first", determine
10446 * the sign of the variable and put the results in the first "n"
10447 * elements of the array "signs".
10449 * 1 means that the variable is non-negative
10450 * -1 means that the variable is non-positive
10451 * 0 means the variable attains both positive and negative values.
10453 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10454 unsigned first
, unsigned n
, int *signs
)
10456 isl_vec
*bound
= NULL
;
10457 struct isl_tab
*tab
= NULL
;
10458 struct isl_tab_undo
*snap
;
10461 if (!bset
|| !signs
)
10464 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10465 tab
= isl_tab_from_basic_set(bset
, 0);
10466 if (!bound
|| !tab
)
10469 isl_seq_clr(bound
->el
, bound
->size
);
10470 isl_int_set_si(bound
->el
[0], -1);
10472 snap
= isl_tab_snap(tab
);
10473 for (i
= 0; i
< n
; ++i
) {
10476 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10477 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10479 empty
= tab
->empty
;
10480 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10481 if (isl_tab_rollback(tab
, snap
) < 0)
10489 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10490 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10492 empty
= tab
->empty
;
10493 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10494 if (isl_tab_rollback(tab
, snap
) < 0)
10497 signs
[i
] = empty
? -1 : 0;
10501 isl_vec_free(bound
);
10505 isl_vec_free(bound
);
10509 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10510 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10512 if (!bset
|| !signs
)
10514 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10517 first
+= pos(bset
->dim
, type
) - 1;
10518 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10521 /* Is it possible for the integer division "div" to depend (possibly
10522 * indirectly) on any output dimensions?
10524 * If the div is undefined, then we conservatively assume that it
10525 * may depend on them.
10526 * Otherwise, we check if it actually depends on them or on any integer
10527 * divisions that may depend on them.
10529 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10532 unsigned n_out
, o_out
;
10533 unsigned n_div
, o_div
;
10535 if (isl_int_is_zero(bmap
->div
[div
][0]))
10538 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10539 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10541 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10544 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10545 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10547 for (i
= 0; i
< n_div
; ++i
) {
10548 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10550 if (div_may_involve_output(bmap
, i
))
10557 /* Return the first integer division of "bmap" in the range
10558 * [first, first + n[ that may depend on any output dimensions and
10559 * that has a non-zero coefficient in "c" (where the first coefficient
10560 * in "c" corresponds to integer division "first").
10562 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10563 isl_int
*c
, int first
, int n
)
10570 for (k
= first
; k
< first
+ n
; ++k
) {
10571 if (isl_int_is_zero(c
[k
]))
10573 if (div_may_involve_output(bmap
, k
))
10580 /* Look for a pair of inequality constraints in "bmap" of the form
10582 * -l + i >= 0 or i >= l
10584 * n + l - i >= 0 or i <= l + n
10586 * with n < "m" and i the output dimension at position "pos".
10587 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10588 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10589 * and earlier output dimensions, as well as integer divisions that do
10590 * not involve any of the output dimensions.
10592 * Return the index of the first inequality constraint or bmap->n_ineq
10593 * if no such pair can be found.
10595 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10596 int pos
, isl_int m
)
10601 unsigned n_div
, o_div
;
10602 unsigned n_out
, o_out
;
10608 ctx
= isl_basic_map_get_ctx(bmap
);
10609 total
= isl_basic_map_total_dim(bmap
);
10610 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10611 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10612 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10613 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10614 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10615 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10617 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10618 n_out
- (pos
+ 1)) != -1)
10620 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10623 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10624 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10627 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10628 bmap
->ineq
[j
] + 1, total
))
10632 if (j
>= bmap
->n_ineq
)
10634 isl_int_add(bmap
->ineq
[i
][0],
10635 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10636 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10637 isl_int_sub(bmap
->ineq
[i
][0],
10638 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10641 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10647 return bmap
->n_ineq
;
10650 /* Return the index of the equality of "bmap" that defines
10651 * the output dimension "pos" in terms of earlier dimensions.
10652 * The equality may also involve integer divisions, as long
10653 * as those integer divisions are defined in terms of
10654 * parameters or input dimensions.
10655 * In this case, *div is set to the number of integer divisions and
10656 * *ineq is set to the number of inequality constraints (provided
10657 * div and ineq are not NULL).
10659 * The equality may also involve a single integer division involving
10660 * the output dimensions (typically only output dimension "pos") as
10661 * long as the coefficient of output dimension "pos" is 1 or -1 and
10662 * there is a pair of constraints i >= l and i <= l + n, with i referring
10663 * to output dimension "pos", l an expression involving only earlier
10664 * dimensions and n smaller than the coefficient of the integer division
10665 * in the equality. In this case, the output dimension can be defined
10666 * in terms of a modulo expression that does not involve the integer division.
10667 * *div is then set to this single integer division and
10668 * *ineq is set to the index of constraint i >= l.
10670 * Return bmap->n_eq if there is no such equality.
10671 * Return -1 on error.
10673 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10674 int pos
, int *div
, int *ineq
)
10677 unsigned n_out
, o_out
;
10678 unsigned n_div
, o_div
;
10683 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10684 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10685 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10686 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10689 *ineq
= bmap
->n_ineq
;
10692 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10693 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10695 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10696 n_out
- (pos
+ 1)) != -1)
10698 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10702 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10703 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10705 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10706 k
+ 1, n_div
- (k
+1)) < n_div
)
10708 l
= find_modulo_constraint_pair(bmap
, pos
,
10709 bmap
->eq
[j
][o_div
+ k
]);
10712 if (l
>= bmap
->n_ineq
)
10724 /* Check if the given basic map is obviously single-valued.
10725 * In particular, for each output dimension, check that there is
10726 * an equality that defines the output dimension in terms of
10727 * earlier dimensions.
10729 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10735 return isl_bool_error
;
10737 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10739 for (i
= 0; i
< n_out
; ++i
) {
10742 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10745 return isl_bool_error
;
10746 if (eq
>= bmap
->n_eq
)
10747 return isl_bool_false
;
10750 return isl_bool_true
;
10753 /* Check if the given basic map is single-valued.
10754 * We simply compute
10758 * and check if the result is a subset of the identity mapping.
10760 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10763 isl_basic_map
*test
;
10767 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10771 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10772 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10774 space
= isl_basic_map_get_space(bmap
);
10775 space
= isl_space_map_from_set(isl_space_range(space
));
10776 id
= isl_basic_map_identity(space
);
10778 sv
= isl_basic_map_is_subset(test
, id
);
10780 isl_basic_map_free(test
);
10781 isl_basic_map_free(id
);
10786 /* Check if the given map is obviously single-valued.
10788 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10791 return isl_bool_error
;
10793 return isl_bool_true
;
10795 return isl_bool_false
;
10797 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10800 /* Check if the given map is single-valued.
10801 * We simply compute
10805 * and check if the result is a subset of the identity mapping.
10807 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10814 sv
= isl_map_plain_is_single_valued(map
);
10818 test
= isl_map_reverse(isl_map_copy(map
));
10819 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10821 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10822 id
= isl_map_identity(dim
);
10824 sv
= isl_map_is_subset(test
, id
);
10826 isl_map_free(test
);
10832 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10836 map
= isl_map_copy(map
);
10837 map
= isl_map_reverse(map
);
10838 in
= isl_map_is_single_valued(map
);
10844 /* Check if the given map is obviously injective.
10846 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10850 map
= isl_map_copy(map
);
10851 map
= isl_map_reverse(map
);
10852 in
= isl_map_plain_is_single_valued(map
);
10858 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10862 sv
= isl_map_is_single_valued(map
);
10866 return isl_map_is_injective(map
);
10869 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10871 return isl_map_is_single_valued((isl_map
*)set
);
10874 /* Does "map" only map elements to themselves?
10876 * If the domain and range spaces are different, then "map"
10877 * is considered not to be an identity relation, even if it is empty.
10878 * Otherwise, construct the maximal identity relation and
10879 * check whether "map" is a subset of this relation.
10881 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10885 isl_bool equal
, is_identity
;
10887 space
= isl_map_get_space(map
);
10888 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10889 isl_space_free(space
);
10890 if (equal
< 0 || !equal
)
10893 id
= isl_map_identity(isl_map_get_space(map
));
10894 is_identity
= isl_map_is_subset(map
, id
);
10897 return is_identity
;
10900 int isl_map_is_translation(__isl_keep isl_map
*map
)
10905 delta
= isl_map_deltas(isl_map_copy(map
));
10906 ok
= isl_set_is_singleton(delta
);
10907 isl_set_free(delta
);
10912 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10914 if (isl_seq_first_non_zero(p
, pos
) != -1)
10916 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10921 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10930 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10933 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10934 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10935 for (j
= 0; j
< nvar
; ++j
) {
10936 int lower
= 0, upper
= 0;
10937 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10938 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10940 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10944 if (i
< bset
->n_eq
)
10946 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10947 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10949 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10951 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10956 if (!lower
|| !upper
)
10963 int isl_set_is_box(__isl_keep isl_set
*set
)
10970 return isl_basic_set_is_box(set
->p
[0]);
10973 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10976 return isl_bool_error
;
10978 return isl_space_is_wrapping(bset
->dim
);
10981 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10984 return isl_bool_error
;
10986 return isl_space_is_wrapping(set
->dim
);
10989 /* Modify the space of "map" through a call to "change".
10990 * If "can_change" is set (not NULL), then first call it to check
10991 * if the modification is allowed, printing the error message "cannot_change"
10994 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10995 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10996 const char *cannot_change
,
10997 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11005 ok
= can_change
? can_change(map
) : isl_bool_true
;
11007 return isl_map_free(map
);
11009 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11010 return isl_map_free(map
));
11012 space
= change(isl_map_get_space(map
));
11013 map
= isl_map_reset_space(map
, space
);
11018 /* Is the domain of "map" a wrapped relation?
11020 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11023 return isl_bool_error
;
11025 return isl_space_domain_is_wrapping(map
->dim
);
11028 /* Is the range of "map" a wrapped relation?
11030 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11033 return isl_bool_error
;
11035 return isl_space_range_is_wrapping(map
->dim
);
11038 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11040 bmap
= isl_basic_map_cow(bmap
);
11044 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11048 bmap
= isl_basic_map_finalize(bmap
);
11050 return (isl_basic_set
*)bmap
;
11052 isl_basic_map_free(bmap
);
11056 /* Given a map A -> B, return the set (A -> B).
11058 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11060 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11063 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11065 bset
= isl_basic_set_cow(bset
);
11069 bset
->dim
= isl_space_unwrap(bset
->dim
);
11073 bset
= isl_basic_set_finalize(bset
);
11075 return (isl_basic_map
*)bset
;
11077 isl_basic_set_free(bset
);
11081 /* Given a set (A -> B), return the map A -> B.
11082 * Error out if "set" is not of the form (A -> B).
11084 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11086 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11087 "not a wrapping set", &isl_space_unwrap
);
11090 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11091 enum isl_dim_type type
)
11096 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11099 bmap
= isl_basic_map_cow(bmap
);
11103 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11107 bmap
= isl_basic_map_finalize(bmap
);
11111 isl_basic_map_free(bmap
);
11115 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11116 enum isl_dim_type type
)
11123 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11126 map
= isl_map_cow(map
);
11130 for (i
= 0; i
< map
->n
; ++i
) {
11131 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11135 map
->dim
= isl_space_reset(map
->dim
, type
);
11145 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11150 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11153 bmap
= isl_basic_map_cow(bmap
);
11157 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11161 bmap
= isl_basic_map_finalize(bmap
);
11165 isl_basic_map_free(bmap
);
11169 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11171 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11174 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11175 __isl_take isl_basic_map
*bmap
)
11180 if (!bmap
->dim
->nested
[0])
11183 bmap
= isl_basic_map_cow(bmap
);
11187 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11191 bmap
= isl_basic_map_finalize(bmap
);
11195 isl_basic_map_free(bmap
);
11199 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11200 __isl_take isl_basic_map
*bmap
)
11205 if (!bmap
->dim
->nested
[1])
11208 bmap
= isl_basic_map_cow(bmap
);
11212 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11216 bmap
= isl_basic_map_finalize(bmap
);
11220 isl_basic_map_free(bmap
);
11224 /* Remove any internal structure from the spaces of domain and range of "map".
11226 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11231 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11234 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11237 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11239 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11242 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11244 isl_space
*dim
, *flat_dim
;
11247 dim
= isl_set_get_space(set
);
11248 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11249 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11250 map
= isl_map_intersect_domain(map
, set
);
11255 /* Remove any internal structure from the space of the domain of "map".
11257 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11262 if (!map
->dim
->nested
[0])
11265 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11268 /* Remove any internal structure from the space of the range of "map".
11270 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11275 if (!map
->dim
->nested
[1])
11278 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11281 /* Reorder the dimensions of "bmap" according to the given dim_map
11282 * and set the dimension specification to "dim" and
11283 * perform Gaussian elimination on the result.
11285 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11286 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11288 isl_basic_map
*res
;
11291 bmap
= isl_basic_map_cow(bmap
);
11292 if (!bmap
|| !dim
|| !dim_map
)
11295 flags
= bmap
->flags
;
11296 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11297 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11298 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11299 res
= isl_basic_map_alloc_space(dim
,
11300 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11301 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11303 res
->flags
= flags
;
11304 res
= isl_basic_map_gauss(res
, NULL
);
11305 res
= isl_basic_map_finalize(res
);
11309 isl_basic_map_free(bmap
);
11310 isl_space_free(dim
);
11314 /* Reorder the dimensions of "map" according to given reordering.
11316 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11317 __isl_take isl_reordering
*r
)
11320 struct isl_dim_map
*dim_map
;
11322 map
= isl_map_cow(map
);
11323 dim_map
= isl_dim_map_from_reordering(r
);
11324 if (!map
|| !r
|| !dim_map
)
11327 for (i
= 0; i
< map
->n
; ++i
) {
11328 struct isl_dim_map
*dim_map_i
;
11330 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11332 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11333 isl_space_copy(r
->dim
), dim_map_i
);
11339 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11341 isl_reordering_free(r
);
11347 isl_reordering_free(r
);
11351 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11352 __isl_take isl_reordering
*r
)
11354 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11357 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11358 __isl_take isl_space
*model
)
11362 if (!map
|| !model
)
11365 ctx
= isl_space_get_ctx(model
);
11366 if (!isl_space_has_named_params(model
))
11367 isl_die(ctx
, isl_error_invalid
,
11368 "model has unnamed parameters", goto error
);
11369 if (!isl_space_has_named_params(map
->dim
))
11370 isl_die(ctx
, isl_error_invalid
,
11371 "relation has unnamed parameters", goto error
);
11372 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11373 isl_reordering
*exp
;
11375 model
= isl_space_drop_dims(model
, isl_dim_in
,
11376 0, isl_space_dim(model
, isl_dim_in
));
11377 model
= isl_space_drop_dims(model
, isl_dim_out
,
11378 0, isl_space_dim(model
, isl_dim_out
));
11379 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11380 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11381 map
= isl_map_realign(map
, exp
);
11384 isl_space_free(model
);
11387 isl_space_free(model
);
11392 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11393 __isl_take isl_space
*model
)
11395 return isl_map_align_params(set
, model
);
11398 /* Align the parameters of "bmap" to those of "model", introducing
11399 * additional parameters if needed.
11401 __isl_give isl_basic_map
*isl_basic_map_align_params(
11402 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11406 if (!bmap
|| !model
)
11409 ctx
= isl_space_get_ctx(model
);
11410 if (!isl_space_has_named_params(model
))
11411 isl_die(ctx
, isl_error_invalid
,
11412 "model has unnamed parameters", goto error
);
11413 if (!isl_space_has_named_params(bmap
->dim
))
11414 isl_die(ctx
, isl_error_invalid
,
11415 "relation has unnamed parameters", goto error
);
11416 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11417 isl_reordering
*exp
;
11418 struct isl_dim_map
*dim_map
;
11420 model
= isl_space_drop_dims(model
, isl_dim_in
,
11421 0, isl_space_dim(model
, isl_dim_in
));
11422 model
= isl_space_drop_dims(model
, isl_dim_out
,
11423 0, isl_space_dim(model
, isl_dim_out
));
11424 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11425 exp
= isl_reordering_extend_space(exp
,
11426 isl_basic_map_get_space(bmap
));
11427 dim_map
= isl_dim_map_from_reordering(exp
);
11428 bmap
= isl_basic_map_realign(bmap
,
11429 exp
? isl_space_copy(exp
->dim
) : NULL
,
11430 isl_dim_map_extend(dim_map
, bmap
));
11431 isl_reordering_free(exp
);
11435 isl_space_free(model
);
11438 isl_space_free(model
);
11439 isl_basic_map_free(bmap
);
11443 /* Align the parameters of "bset" to those of "model", introducing
11444 * additional parameters if needed.
11446 __isl_give isl_basic_set
*isl_basic_set_align_params(
11447 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11449 return isl_basic_map_align_params(bset
, model
);
11452 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11453 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11454 enum isl_dim_type c2
, enum isl_dim_type c3
,
11455 enum isl_dim_type c4
, enum isl_dim_type c5
)
11457 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11458 struct isl_mat
*mat
;
11464 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11465 isl_basic_map_total_dim(bmap
) + 1);
11468 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11469 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11470 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11471 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11472 isl_int_set(mat
->row
[i
][pos
],
11473 bmap
->eq
[i
][off
+ k
]);
11481 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11482 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11483 enum isl_dim_type c2
, enum isl_dim_type c3
,
11484 enum isl_dim_type c4
, enum isl_dim_type c5
)
11486 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11487 struct isl_mat
*mat
;
11493 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11494 isl_basic_map_total_dim(bmap
) + 1);
11497 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11498 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11499 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11500 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11501 isl_int_set(mat
->row
[i
][pos
],
11502 bmap
->ineq
[i
][off
+ k
]);
11510 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11511 __isl_take isl_space
*dim
,
11512 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11513 enum isl_dim_type c2
, enum isl_dim_type c3
,
11514 enum isl_dim_type c4
, enum isl_dim_type c5
)
11516 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11517 isl_basic_map
*bmap
;
11523 if (!dim
|| !eq
|| !ineq
)
11526 if (eq
->n_col
!= ineq
->n_col
)
11527 isl_die(dim
->ctx
, isl_error_invalid
,
11528 "equalities and inequalities matrices should have "
11529 "same number of columns", goto error
);
11531 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11533 if (eq
->n_col
< total
)
11534 isl_die(dim
->ctx
, isl_error_invalid
,
11535 "number of columns too small", goto error
);
11537 extra
= eq
->n_col
- total
;
11539 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11540 eq
->n_row
, ineq
->n_row
);
11543 for (i
= 0; i
< extra
; ++i
) {
11544 k
= isl_basic_map_alloc_div(bmap
);
11547 isl_int_set_si(bmap
->div
[k
][0], 0);
11549 for (i
= 0; i
< eq
->n_row
; ++i
) {
11550 l
= isl_basic_map_alloc_equality(bmap
);
11553 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11554 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11555 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11556 isl_int_set(bmap
->eq
[l
][off
+ k
],
11562 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11563 l
= isl_basic_map_alloc_inequality(bmap
);
11566 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11567 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11568 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11569 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11570 ineq
->row
[i
][pos
]);
11576 isl_space_free(dim
);
11578 isl_mat_free(ineq
);
11580 bmap
= isl_basic_map_simplify(bmap
);
11581 return isl_basic_map_finalize(bmap
);
11583 isl_space_free(dim
);
11585 isl_mat_free(ineq
);
11589 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11590 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11591 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11593 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11594 c1
, c2
, c3
, c4
, isl_dim_in
);
11597 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11598 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11599 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11601 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11602 c1
, c2
, c3
, c4
, isl_dim_in
);
11605 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11606 __isl_take isl_space
*dim
,
11607 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11608 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11610 return (isl_basic_set
*)
11611 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11612 c1
, c2
, c3
, c4
, isl_dim_in
);
11615 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11618 return isl_bool_error
;
11620 return isl_space_can_zip(bmap
->dim
);
11623 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11626 return isl_bool_error
;
11628 return isl_space_can_zip(map
->dim
);
11631 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11632 * (A -> C) -> (B -> D).
11634 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11643 if (!isl_basic_map_can_zip(bmap
))
11644 isl_die(bmap
->ctx
, isl_error_invalid
,
11645 "basic map cannot be zipped", goto error
);
11646 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11647 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11648 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11649 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11650 bmap
= isl_basic_map_cow(bmap
);
11651 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11654 bmap
->dim
= isl_space_zip(bmap
->dim
);
11657 bmap
= isl_basic_map_mark_final(bmap
);
11660 isl_basic_map_free(bmap
);
11664 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11665 * (A -> C) -> (B -> D).
11667 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11674 if (!isl_map_can_zip(map
))
11675 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11678 map
= isl_map_cow(map
);
11682 for (i
= 0; i
< map
->n
; ++i
) {
11683 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11688 map
->dim
= isl_space_zip(map
->dim
);
11698 /* Can we apply isl_basic_map_curry to "bmap"?
11699 * That is, does it have a nested relation in its domain?
11701 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11704 return isl_bool_error
;
11706 return isl_space_can_curry(bmap
->dim
);
11709 /* Can we apply isl_map_curry to "map"?
11710 * That is, does it have a nested relation in its domain?
11712 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11715 return isl_bool_error
;
11717 return isl_space_can_curry(map
->dim
);
11720 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11723 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11729 if (!isl_basic_map_can_curry(bmap
))
11730 isl_die(bmap
->ctx
, isl_error_invalid
,
11731 "basic map cannot be curried", goto error
);
11732 bmap
= isl_basic_map_cow(bmap
);
11735 bmap
->dim
= isl_space_curry(bmap
->dim
);
11738 bmap
= isl_basic_map_mark_final(bmap
);
11741 isl_basic_map_free(bmap
);
11745 /* Given a map (A -> B) -> C, return the corresponding map
11748 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11750 return isl_map_change_space(map
, &isl_map_can_curry
,
11751 "map cannot be curried", &isl_space_curry
);
11754 /* Can isl_map_range_curry be applied to "map"?
11755 * That is, does it have a nested relation in its range,
11756 * the domain of which is itself a nested relation?
11758 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11761 return isl_bool_error
;
11763 return isl_space_can_range_curry(map
->dim
);
11766 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11767 * A -> (B -> (C -> D)).
11769 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11771 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11772 "map range cannot be curried",
11773 &isl_space_range_curry
);
11776 /* Can we apply isl_basic_map_uncurry to "bmap"?
11777 * That is, does it have a nested relation in its domain?
11779 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11782 return isl_bool_error
;
11784 return isl_space_can_uncurry(bmap
->dim
);
11787 /* Can we apply isl_map_uncurry to "map"?
11788 * That is, does it have a nested relation in its domain?
11790 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11793 return isl_bool_error
;
11795 return isl_space_can_uncurry(map
->dim
);
11798 /* Given a basic map A -> (B -> C), return the corresponding basic map
11801 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11807 if (!isl_basic_map_can_uncurry(bmap
))
11808 isl_die(bmap
->ctx
, isl_error_invalid
,
11809 "basic map cannot be uncurried",
11810 return isl_basic_map_free(bmap
));
11811 bmap
= isl_basic_map_cow(bmap
);
11814 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11816 return isl_basic_map_free(bmap
);
11817 bmap
= isl_basic_map_mark_final(bmap
);
11821 /* Given a map A -> (B -> C), return the corresponding map
11824 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11826 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11827 "map cannot be uncurried", &isl_space_uncurry
);
11830 /* Construct a basic map mapping the domain of the affine expression
11831 * to a one-dimensional range prescribed by the affine expression.
11833 * A NaN affine expression cannot be converted to a basic map.
11835 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11840 isl_local_space
*ls
;
11841 isl_basic_map
*bmap
= NULL
;
11845 is_nan
= isl_aff_is_nan(aff
);
11849 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11850 "cannot convert NaN", goto error
);
11852 ls
= isl_aff_get_local_space(aff
);
11853 bmap
= isl_basic_map_from_local_space(ls
);
11854 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11855 k
= isl_basic_map_alloc_equality(bmap
);
11859 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11860 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11861 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11862 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11863 aff
->v
->size
- (pos
+ 1));
11866 bmap
= isl_basic_map_finalize(bmap
);
11870 isl_basic_map_free(bmap
);
11874 /* Construct a map mapping the domain of the affine expression
11875 * to a one-dimensional range prescribed by the affine expression.
11877 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11879 isl_basic_map
*bmap
;
11881 bmap
= isl_basic_map_from_aff(aff
);
11882 return isl_map_from_basic_map(bmap
);
11885 /* Construct a basic map mapping the domain the multi-affine expression
11886 * to its range, with each dimension in the range equated to the
11887 * corresponding affine expression.
11889 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11890 __isl_take isl_multi_aff
*maff
)
11894 isl_basic_map
*bmap
;
11899 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11900 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11901 "invalid space", goto error
);
11903 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11904 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11906 for (i
= 0; i
< maff
->n
; ++i
) {
11908 isl_basic_map
*bmap_i
;
11910 aff
= isl_aff_copy(maff
->p
[i
]);
11911 bmap_i
= isl_basic_map_from_aff(aff
);
11913 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11916 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11918 isl_multi_aff_free(maff
);
11921 isl_multi_aff_free(maff
);
11925 /* Construct a map mapping the domain the multi-affine expression
11926 * to its range, with each dimension in the range equated to the
11927 * corresponding affine expression.
11929 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11931 isl_basic_map
*bmap
;
11933 bmap
= isl_basic_map_from_multi_aff(maff
);
11934 return isl_map_from_basic_map(bmap
);
11937 /* Construct a basic map mapping a domain in the given space to
11938 * to an n-dimensional range, with n the number of elements in the list,
11939 * where each coordinate in the range is prescribed by the
11940 * corresponding affine expression.
11941 * The domains of all affine expressions in the list are assumed to match
11944 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11945 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11949 isl_basic_map
*bmap
;
11954 dim
= isl_space_from_domain(domain_dim
);
11955 bmap
= isl_basic_map_universe(dim
);
11957 for (i
= 0; i
< list
->n
; ++i
) {
11959 isl_basic_map
*bmap_i
;
11961 aff
= isl_aff_copy(list
->p
[i
]);
11962 bmap_i
= isl_basic_map_from_aff(aff
);
11964 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11967 isl_aff_list_free(list
);
11971 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11972 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11974 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11977 /* Construct a basic map where the given dimensions are equal to each other.
11979 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11980 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11982 isl_basic_map
*bmap
= NULL
;
11988 if (pos1
>= isl_space_dim(space
, type1
))
11989 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11990 "index out of bounds", goto error
);
11991 if (pos2
>= isl_space_dim(space
, type2
))
11992 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11993 "index out of bounds", goto error
);
11995 if (type1
== type2
&& pos1
== pos2
)
11996 return isl_basic_map_universe(space
);
11998 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11999 i
= isl_basic_map_alloc_equality(bmap
);
12002 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12003 pos1
+= isl_basic_map_offset(bmap
, type1
);
12004 pos2
+= isl_basic_map_offset(bmap
, type2
);
12005 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12006 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12007 bmap
= isl_basic_map_finalize(bmap
);
12008 isl_space_free(space
);
12011 isl_space_free(space
);
12012 isl_basic_map_free(bmap
);
12016 /* Add a constraint imposing that the given two dimensions are equal.
12018 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12019 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12023 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12025 bmap
= isl_basic_map_intersect(bmap
, eq
);
12030 /* Add a constraint imposing that the given two dimensions are equal.
12032 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12033 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12035 isl_basic_map
*bmap
;
12037 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12039 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12044 /* Add a constraint imposing that the given two dimensions have opposite values.
12046 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12047 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12049 isl_basic_map
*bmap
= NULL
;
12055 if (pos1
>= isl_map_dim(map
, type1
))
12056 isl_die(map
->ctx
, isl_error_invalid
,
12057 "index out of bounds", goto error
);
12058 if (pos2
>= isl_map_dim(map
, type2
))
12059 isl_die(map
->ctx
, isl_error_invalid
,
12060 "index out of bounds", goto error
);
12062 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12063 i
= isl_basic_map_alloc_equality(bmap
);
12066 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12067 pos1
+= isl_basic_map_offset(bmap
, type1
);
12068 pos2
+= isl_basic_map_offset(bmap
, type2
);
12069 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12070 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12071 bmap
= isl_basic_map_finalize(bmap
);
12073 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12077 isl_basic_map_free(bmap
);
12082 /* Construct a constraint imposing that the value of the first dimension is
12083 * greater than or equal to that of the second.
12085 static __isl_give isl_constraint
*constraint_order_ge(
12086 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12087 enum isl_dim_type type2
, int pos2
)
12094 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12096 if (pos1
>= isl_constraint_dim(c
, type1
))
12097 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12098 "index out of bounds", return isl_constraint_free(c
));
12099 if (pos2
>= isl_constraint_dim(c
, type2
))
12100 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12101 "index out of bounds", return isl_constraint_free(c
));
12103 if (type1
== type2
&& pos1
== pos2
)
12106 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12107 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12112 /* Add a constraint imposing that the value of the first dimension is
12113 * greater than or equal to that of the second.
12115 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12116 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12121 if (type1
== type2
&& pos1
== pos2
)
12123 space
= isl_basic_map_get_space(bmap
);
12124 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12125 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12130 /* Add a constraint imposing that the value of the first dimension is
12131 * greater than or equal to that of the second.
12133 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12134 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12139 if (type1
== type2
&& pos1
== pos2
)
12141 space
= isl_map_get_space(map
);
12142 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12143 map
= isl_map_add_constraint(map
, c
);
12148 /* Add a constraint imposing that the value of the first dimension is
12149 * less than or equal to that of the second.
12151 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12152 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12154 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12157 /* Construct a basic map where the value of the first dimension is
12158 * greater than that of the second.
12160 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12161 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12163 isl_basic_map
*bmap
= NULL
;
12169 if (pos1
>= isl_space_dim(space
, type1
))
12170 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12171 "index out of bounds", goto error
);
12172 if (pos2
>= isl_space_dim(space
, type2
))
12173 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12174 "index out of bounds", goto error
);
12176 if (type1
== type2
&& pos1
== pos2
)
12177 return isl_basic_map_empty(space
);
12179 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12180 i
= isl_basic_map_alloc_inequality(bmap
);
12182 return isl_basic_map_free(bmap
);
12183 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12184 pos1
+= isl_basic_map_offset(bmap
, type1
);
12185 pos2
+= isl_basic_map_offset(bmap
, type2
);
12186 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12187 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12188 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12189 bmap
= isl_basic_map_finalize(bmap
);
12193 isl_space_free(space
);
12194 isl_basic_map_free(bmap
);
12198 /* Add a constraint imposing that the value of the first dimension is
12199 * greater than that of the second.
12201 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12202 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12206 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12208 bmap
= isl_basic_map_intersect(bmap
, gt
);
12213 /* Add a constraint imposing that the value of the first dimension is
12214 * greater than that of the second.
12216 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12217 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12219 isl_basic_map
*bmap
;
12221 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12223 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12228 /* Add a constraint imposing that the value of the first dimension is
12229 * smaller than that of the second.
12231 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12232 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12234 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12237 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12241 isl_local_space
*ls
;
12246 if (!isl_basic_map_divs_known(bmap
))
12247 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12248 "some divs are unknown", return NULL
);
12250 ls
= isl_basic_map_get_local_space(bmap
);
12251 div
= isl_local_space_get_div(ls
, pos
);
12252 isl_local_space_free(ls
);
12257 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12260 return isl_basic_map_get_div(bset
, pos
);
12263 /* Plug in "subs" for dimension "type", "pos" of "bset".
12265 * Let i be the dimension to replace and let "subs" be of the form
12269 * Any integer division with a non-zero coefficient for i,
12271 * floor((a i + g)/m)
12275 * floor((a f + d g)/(m d))
12277 * Constraints of the form
12285 * We currently require that "subs" is an integral expression.
12286 * Handling rational expressions may require us to add stride constraints
12287 * as we do in isl_basic_set_preimage_multi_aff.
12289 __isl_give isl_basic_set
*isl_basic_set_substitute(
12290 __isl_take isl_basic_set
*bset
,
12291 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12297 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12300 bset
= isl_basic_set_cow(bset
);
12301 if (!bset
|| !subs
)
12304 ctx
= isl_basic_set_get_ctx(bset
);
12305 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12306 isl_die(ctx
, isl_error_invalid
,
12307 "spaces don't match", goto error
);
12308 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12309 isl_die(ctx
, isl_error_unsupported
,
12310 "cannot handle divs yet", goto error
);
12311 if (!isl_int_is_one(subs
->v
->el
[0]))
12312 isl_die(ctx
, isl_error_invalid
,
12313 "can only substitute integer expressions", goto error
);
12315 pos
+= isl_basic_set_offset(bset
, type
);
12319 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12320 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12322 isl_int_set(v
, bset
->eq
[i
][pos
]);
12323 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12324 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12325 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12328 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12329 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12331 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12332 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12333 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12334 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12337 for (i
= 0; i
< bset
->n_div
; ++i
) {
12338 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12340 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12341 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12342 isl_seq_combine(bset
->div
[i
] + 1,
12343 subs
->v
->el
[0], bset
->div
[i
] + 1,
12344 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12345 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12350 bset
= isl_basic_set_simplify(bset
);
12351 return isl_basic_set_finalize(bset
);
12353 isl_basic_set_free(bset
);
12357 /* Plug in "subs" for dimension "type", "pos" of "set".
12359 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12360 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12364 if (set
&& isl_set_plain_is_empty(set
))
12367 set
= isl_set_cow(set
);
12371 for (i
= set
->n
- 1; i
>= 0; --i
) {
12372 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12373 if (remove_if_empty(set
, i
) < 0)
12383 /* Check if the range of "ma" is compatible with the domain or range
12384 * (depending on "type") of "bmap".
12385 * Return -1 if anything is wrong.
12387 static int check_basic_map_compatible_range_multi_aff(
12388 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12389 __isl_keep isl_multi_aff
*ma
)
12392 isl_space
*ma_space
;
12394 ma_space
= isl_multi_aff_get_space(ma
);
12396 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12400 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12401 "parameters don't match", goto error
);
12402 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12406 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12407 "spaces don't match", goto error
);
12409 isl_space_free(ma_space
);
12412 isl_space_free(ma_space
);
12416 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12417 * coefficients before the transformed range of dimensions,
12418 * the "n_after" coefficients after the transformed range of dimensions
12419 * and the coefficients of the other divs in "bmap".
12421 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12422 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12427 isl_local_space
*ls
;
12432 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12436 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12437 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12438 for (i
= 0; i
< n_div
; ++i
) {
12439 int o_bmap
= 0, o_ls
= 0;
12441 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12442 o_bmap
+= 1 + 1 + n_param
;
12443 o_ls
+= 1 + 1 + n_param
;
12444 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12445 o_bmap
+= n_before
;
12446 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12447 ls
->div
->row
[i
] + o_ls
, n_set
);
12450 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12452 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12453 ls
->div
->row
[i
] + o_ls
, n_div
);
12456 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12457 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12461 isl_local_space_free(ls
);
12464 isl_local_space_free(ls
);
12468 /* How many stride constraints does "ma" enforce?
12469 * That is, how many of the affine expressions have a denominator
12470 * different from one?
12472 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12477 for (i
= 0; i
< ma
->n
; ++i
)
12478 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12484 /* For each affine expression in ma of the form
12486 * x_i = (f_i y + h_i)/m_i
12488 * with m_i different from one, add a constraint to "bmap"
12491 * f_i y + h_i = m_i alpha_i
12493 * with alpha_i an additional existentially quantified variable.
12495 * The input variables of "ma" correspond to a subset of the variables
12496 * of "bmap". There are "n_before" variables in "bmap" before this
12497 * subset and "n_after" variables after this subset.
12498 * The integer divisions of the affine expressions in "ma" are assumed
12499 * to have been aligned. There are "n_div_ma" of them and
12500 * they appear first in "bmap", straight after the "n_after" variables.
12502 static __isl_give isl_basic_map
*add_ma_strides(
12503 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12504 int n_before
, int n_after
, int n_div_ma
)
12512 total
= isl_basic_map_total_dim(bmap
);
12513 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12514 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12515 for (i
= 0; i
< ma
->n
; ++i
) {
12516 int o_bmap
= 0, o_ma
= 1;
12518 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12520 div
= isl_basic_map_alloc_div(bmap
);
12521 k
= isl_basic_map_alloc_equality(bmap
);
12522 if (div
< 0 || k
< 0)
12524 isl_int_set_si(bmap
->div
[div
][0], 0);
12525 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12526 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12527 o_bmap
+= 1 + n_param
;
12528 o_ma
+= 1 + n_param
;
12529 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12530 o_bmap
+= n_before
;
12531 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12532 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12535 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12537 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12538 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12539 o_bmap
+= n_div_ma
;
12541 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12542 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12548 isl_basic_map_free(bmap
);
12552 /* Replace the domain or range space (depending on "type) of "space" by "set".
12554 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12555 enum isl_dim_type type
, __isl_take isl_space
*set
)
12557 if (type
== isl_dim_in
) {
12558 space
= isl_space_range(space
);
12559 space
= isl_space_map_from_domain_and_range(set
, space
);
12561 space
= isl_space_domain(space
);
12562 space
= isl_space_map_from_domain_and_range(space
, set
);
12568 /* Compute the preimage of the domain or range (depending on "type")
12569 * of "bmap" under the function represented by "ma".
12570 * In other words, plug in "ma" in the domain or range of "bmap".
12571 * The result is a basic map that lives in the same space as "bmap"
12572 * except that the domain or range has been replaced by
12573 * the domain space of "ma".
12575 * If bmap is represented by
12577 * A(p) + S u + B x + T v + C(divs) >= 0,
12579 * where u and x are input and output dimensions if type == isl_dim_out
12580 * while x and v are input and output dimensions if type == isl_dim_in,
12581 * and ma is represented by
12583 * x = D(p) + F(y) + G(divs')
12585 * then the result is
12587 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12589 * The divs in the input set are similarly adjusted.
12592 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12596 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12597 * B_i G(divs') + c_i(divs))/n_i)
12599 * If bmap is not a rational map and if F(y) involves any denominators
12601 * x_i = (f_i y + h_i)/m_i
12603 * then additional constraints are added to ensure that we only
12604 * map back integer points. That is we enforce
12606 * f_i y + h_i = m_i alpha_i
12608 * with alpha_i an additional existentially quantified variable.
12610 * We first copy over the divs from "ma".
12611 * Then we add the modified constraints and divs from "bmap".
12612 * Finally, we add the stride constraints, if needed.
12614 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12615 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12616 __isl_take isl_multi_aff
*ma
)
12620 isl_basic_map
*res
= NULL
;
12621 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12622 isl_int f
, c1
, c2
, g
;
12623 int rational
, strides
;
12630 ma
= isl_multi_aff_align_divs(ma
);
12633 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12636 if (type
== isl_dim_in
) {
12638 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12640 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12643 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12644 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12646 space
= isl_multi_aff_get_domain_space(ma
);
12647 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12648 rational
= isl_basic_map_is_rational(bmap
);
12649 strides
= rational
? 0 : multi_aff_strides(ma
);
12650 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12651 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12653 res
= isl_basic_map_set_rational(res
);
12655 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12656 if (isl_basic_map_alloc_div(res
) < 0)
12659 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12662 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12663 k
= isl_basic_map_alloc_equality(res
);
12666 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12667 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12670 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12671 k
= isl_basic_map_alloc_inequality(res
);
12674 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12675 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12678 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12679 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12680 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12683 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12684 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12689 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12695 isl_basic_map_free(bmap
);
12696 isl_multi_aff_free(ma
);
12697 res
= isl_basic_set_simplify(res
);
12698 return isl_basic_map_finalize(res
);
12704 isl_basic_map_free(bmap
);
12705 isl_multi_aff_free(ma
);
12706 isl_basic_map_free(res
);
12710 /* Compute the preimage of "bset" under the function represented by "ma".
12711 * In other words, plug in "ma" in "bset". The result is a basic set
12712 * that lives in the domain space of "ma".
12714 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12715 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12717 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12720 /* Compute the preimage of the domain of "bmap" under the function
12721 * represented by "ma".
12722 * In other words, plug in "ma" in the domain of "bmap".
12723 * The result is a basic map that lives in the same space as "bmap"
12724 * except that the domain has been replaced by the domain space of "ma".
12726 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12727 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12729 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12732 /* Compute the preimage of the range of "bmap" under the function
12733 * represented by "ma".
12734 * In other words, plug in "ma" in the range of "bmap".
12735 * The result is a basic map that lives in the same space as "bmap"
12736 * except that the range has been replaced by the domain space of "ma".
12738 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12739 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12741 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12744 /* Check if the range of "ma" is compatible with the domain or range
12745 * (depending on "type") of "map".
12746 * Return -1 if anything is wrong.
12748 static int check_map_compatible_range_multi_aff(
12749 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12750 __isl_keep isl_multi_aff
*ma
)
12753 isl_space
*ma_space
;
12755 ma_space
= isl_multi_aff_get_space(ma
);
12756 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12757 isl_space_free(ma_space
);
12759 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12760 "spaces don't match", return -1);
12764 /* Compute the preimage of the domain or range (depending on "type")
12765 * of "map" under the function represented by "ma".
12766 * In other words, plug in "ma" in the domain or range of "map".
12767 * The result is a map that lives in the same space as "map"
12768 * except that the domain or range has been replaced by
12769 * the domain space of "ma".
12771 * The parameters are assumed to have been aligned.
12773 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12774 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12779 map
= isl_map_cow(map
);
12780 ma
= isl_multi_aff_align_divs(ma
);
12783 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12786 for (i
= 0; i
< map
->n
; ++i
) {
12787 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12788 isl_multi_aff_copy(ma
));
12793 space
= isl_multi_aff_get_domain_space(ma
);
12794 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12796 isl_space_free(map
->dim
);
12801 isl_multi_aff_free(ma
);
12803 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12804 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12807 isl_multi_aff_free(ma
);
12812 /* Compute the preimage of the domain or range (depending on "type")
12813 * of "map" under the function represented by "ma".
12814 * In other words, plug in "ma" in the domain or range of "map".
12815 * The result is a map that lives in the same space as "map"
12816 * except that the domain or range has been replaced by
12817 * the domain space of "ma".
12819 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12820 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12825 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12826 return map_preimage_multi_aff(map
, type
, ma
);
12828 if (!isl_space_has_named_params(map
->dim
) ||
12829 !isl_space_has_named_params(ma
->space
))
12830 isl_die(map
->ctx
, isl_error_invalid
,
12831 "unaligned unnamed parameters", goto error
);
12832 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12833 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12835 return map_preimage_multi_aff(map
, type
, ma
);
12837 isl_multi_aff_free(ma
);
12838 return isl_map_free(map
);
12841 /* Compute the preimage of "set" under the function represented by "ma".
12842 * In other words, plug in "ma" in "set". The result is a set
12843 * that lives in the domain space of "ma".
12845 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12846 __isl_take isl_multi_aff
*ma
)
12848 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12851 /* Compute the preimage of the domain of "map" under the function
12852 * represented by "ma".
12853 * In other words, plug in "ma" in the domain of "map".
12854 * The result is a map that lives in the same space as "map"
12855 * except that the domain has been replaced by the domain space of "ma".
12857 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12858 __isl_take isl_multi_aff
*ma
)
12860 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12863 /* Compute the preimage of the range of "map" under the function
12864 * represented by "ma".
12865 * In other words, plug in "ma" in the range of "map".
12866 * The result is a map that lives in the same space as "map"
12867 * except that the range has been replaced by the domain space of "ma".
12869 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12870 __isl_take isl_multi_aff
*ma
)
12872 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12875 /* Compute the preimage of "map" under the function represented by "pma".
12876 * In other words, plug in "pma" in the domain or range of "map".
12877 * The result is a map that lives in the same space as "map",
12878 * except that the space of type "type" has been replaced by
12879 * the domain space of "pma".
12881 * The parameters of "map" and "pma" are assumed to have been aligned.
12883 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12884 __isl_take isl_map
*map
, enum isl_dim_type type
,
12885 __isl_take isl_pw_multi_aff
*pma
)
12894 isl_pw_multi_aff_free(pma
);
12895 res
= isl_map_empty(isl_map_get_space(map
));
12900 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12901 isl_multi_aff_copy(pma
->p
[0].maff
));
12902 if (type
== isl_dim_in
)
12903 res
= isl_map_intersect_domain(res
,
12904 isl_map_copy(pma
->p
[0].set
));
12906 res
= isl_map_intersect_range(res
,
12907 isl_map_copy(pma
->p
[0].set
));
12909 for (i
= 1; i
< pma
->n
; ++i
) {
12912 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12913 isl_multi_aff_copy(pma
->p
[i
].maff
));
12914 if (type
== isl_dim_in
)
12915 res_i
= isl_map_intersect_domain(res_i
,
12916 isl_map_copy(pma
->p
[i
].set
));
12918 res_i
= isl_map_intersect_range(res_i
,
12919 isl_map_copy(pma
->p
[i
].set
));
12920 res
= isl_map_union(res
, res_i
);
12923 isl_pw_multi_aff_free(pma
);
12927 isl_pw_multi_aff_free(pma
);
12932 /* Compute the preimage of "map" under the function represented by "pma".
12933 * In other words, plug in "pma" in the domain or range of "map".
12934 * The result is a map that lives in the same space as "map",
12935 * except that the space of type "type" has been replaced by
12936 * the domain space of "pma".
12938 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12939 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12944 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12945 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12947 if (!isl_space_has_named_params(map
->dim
) ||
12948 !isl_space_has_named_params(pma
->dim
))
12949 isl_die(map
->ctx
, isl_error_invalid
,
12950 "unaligned unnamed parameters", goto error
);
12951 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12952 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12954 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12956 isl_pw_multi_aff_free(pma
);
12957 return isl_map_free(map
);
12960 /* Compute the preimage of "set" under the function represented by "pma".
12961 * In other words, plug in "pma" in "set". The result is a set
12962 * that lives in the domain space of "pma".
12964 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12965 __isl_take isl_pw_multi_aff
*pma
)
12967 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12970 /* Compute the preimage of the domain of "map" under the function
12971 * represented by "pma".
12972 * In other words, plug in "pma" in the domain of "map".
12973 * The result is a map that lives in the same space as "map",
12974 * except that domain space has been replaced by the domain space of "pma".
12976 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12977 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12979 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12982 /* Compute the preimage of the range of "map" under the function
12983 * represented by "pma".
12984 * In other words, plug in "pma" in the range of "map".
12985 * The result is a map that lives in the same space as "map",
12986 * except that range space has been replaced by the domain space of "pma".
12988 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12989 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12991 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12994 /* Compute the preimage of "map" under the function represented by "mpa".
12995 * In other words, plug in "mpa" in the domain or range of "map".
12996 * The result is a map that lives in the same space as "map",
12997 * except that the space of type "type" has been replaced by
12998 * the domain space of "mpa".
13000 * If the map does not involve any constraints that refer to the
13001 * dimensions of the substituted space, then the only possible
13002 * effect of "mpa" on the map is to map the space to a different space.
13003 * We create a separate isl_multi_aff to effectuate this change
13004 * in order to avoid spurious splitting of the map along the pieces
13007 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13008 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13011 isl_pw_multi_aff
*pma
;
13016 n
= isl_map_dim(map
, type
);
13017 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13021 space
= isl_multi_pw_aff_get_space(mpa
);
13022 isl_multi_pw_aff_free(mpa
);
13023 ma
= isl_multi_aff_zero(space
);
13024 return isl_map_preimage_multi_aff(map
, type
, ma
);
13027 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13028 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13031 isl_multi_pw_aff_free(mpa
);
13035 /* Compute the preimage of "map" under the function represented by "mpa".
13036 * In other words, plug in "mpa" in the domain "map".
13037 * The result is a map that lives in the same space as "map",
13038 * except that domain space has been replaced by the domain space of "mpa".
13040 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13041 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13043 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13046 /* Compute the preimage of "set" by the function represented by "mpa".
13047 * In other words, plug in "mpa" in "set".
13049 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13050 __isl_take isl_multi_pw_aff
*mpa
)
13052 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);