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 /* Does "bmap" have an explicit representation for all local variables?
6960 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6964 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
6965 first
= isl_basic_map_first_unknown_div(bmap
);
6967 return isl_bool_error
;
6971 /* Do all basic maps in "map" have an explicit representation
6972 * for all local variables?
6974 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
6979 return isl_bool_error
;
6981 for (i
= 0; i
< map
->n
; ++i
) {
6982 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6987 return isl_bool_true
;
6990 /* If bmap contains any unknown divs, then compute explicit
6991 * expressions for them. However, this computation may be
6992 * quite expensive, so first try to remove divs that aren't
6995 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6998 struct isl_map
*map
;
7000 known
= isl_basic_map_divs_known(bmap
);
7004 return isl_map_from_basic_map(bmap
);
7006 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7008 known
= isl_basic_map_divs_known(bmap
);
7012 return isl_map_from_basic_map(bmap
);
7014 map
= compute_divs(bmap
);
7017 isl_basic_map_free(bmap
);
7021 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7025 struct isl_map
*res
;
7032 known
= isl_map_divs_known(map
);
7040 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7041 for (i
= 1 ; i
< map
->n
; ++i
) {
7043 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7044 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7045 res
= isl_map_union_disjoint(res
, r2
);
7047 res
= isl_map_union(res
, r2
);
7054 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7056 return (struct isl_set
*)
7057 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7060 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7062 return (struct isl_set
*)
7063 isl_map_compute_divs((struct isl_map
*)set
);
7066 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7069 struct isl_set
*set
;
7074 map
= isl_map_cow(map
);
7078 set
= (struct isl_set
*)map
;
7079 set
->dim
= isl_space_domain(set
->dim
);
7082 for (i
= 0; i
< map
->n
; ++i
) {
7083 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7087 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7088 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7095 /* Return the union of "map1" and "map2", where we assume for now that
7096 * "map1" and "map2" are disjoint. Note that the basic maps inside
7097 * "map1" or "map2" may not be disjoint from each other.
7098 * Also note that this function is also called from isl_map_union,
7099 * which takes care of handling the situation where "map1" and "map2"
7100 * may not be disjoint.
7102 * If one of the inputs is empty, we can simply return the other input.
7103 * Similarly, if one of the inputs is universal, then it is equal to the union.
7105 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7106 __isl_take isl_map
*map2
)
7110 struct isl_map
*map
= NULL
;
7116 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7117 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7118 "spaces don't match", goto error
);
7129 is_universe
= isl_map_plain_is_universe(map1
);
7130 if (is_universe
< 0)
7137 is_universe
= isl_map_plain_is_universe(map2
);
7138 if (is_universe
< 0)
7145 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7146 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7147 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7149 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7150 map1
->n
+ map2
->n
, flags
);
7153 for (i
= 0; i
< map1
->n
; ++i
) {
7154 map
= isl_map_add_basic_map(map
,
7155 isl_basic_map_copy(map1
->p
[i
]));
7159 for (i
= 0; i
< map2
->n
; ++i
) {
7160 map
= isl_map_add_basic_map(map
,
7161 isl_basic_map_copy(map2
->p
[i
]));
7175 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7176 * guaranteed to be disjoint by the caller.
7178 * Note that this functions is called from within isl_map_make_disjoint,
7179 * so we have to be careful not to touch the constraints of the inputs
7182 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7183 __isl_take isl_map
*map2
)
7185 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7188 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7189 * not be disjoint. The parameters are assumed to have been aligned.
7191 * We currently simply call map_union_disjoint, the internal operation
7192 * of which does not really depend on the inputs being disjoint.
7193 * If the result contains more than one basic map, then we clear
7194 * the disjoint flag since the result may contain basic maps from
7195 * both inputs and these are not guaranteed to be disjoint.
7197 * As a special case, if "map1" and "map2" are obviously equal,
7198 * then we simply return "map1".
7200 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7201 __isl_take isl_map
*map2
)
7208 equal
= isl_map_plain_is_equal(map1
, map2
);
7216 map1
= map_union_disjoint(map1
, map2
);
7220 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7228 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7231 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7232 __isl_take isl_map
*map2
)
7234 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7237 struct isl_set
*isl_set_union_disjoint(
7238 struct isl_set
*set1
, struct isl_set
*set2
)
7240 return (struct isl_set
*)
7241 isl_map_union_disjoint(
7242 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7245 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7247 return (struct isl_set
*)
7248 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7251 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7254 * "map" and "set" are assumed to be compatible and non-NULL.
7256 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7257 __isl_take isl_set
*set
,
7258 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7259 __isl_take isl_basic_set
*bset
))
7262 struct isl_map
*result
;
7265 if (isl_set_plain_is_universe(set
)) {
7270 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7271 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7272 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7274 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7275 map
->n
* set
->n
, flags
);
7276 for (i
= 0; result
&& i
< map
->n
; ++i
)
7277 for (j
= 0; j
< set
->n
; ++j
) {
7278 result
= isl_map_add_basic_map(result
,
7279 fn(isl_basic_map_copy(map
->p
[i
]),
7280 isl_basic_set_copy(set
->p
[j
])));
7290 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7291 __isl_take isl_set
*set
)
7296 if (!isl_map_compatible_range(map
, set
))
7297 isl_die(set
->ctx
, isl_error_invalid
,
7298 "incompatible spaces", goto error
);
7300 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7307 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7308 __isl_take isl_set
*set
)
7310 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7313 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7314 __isl_take isl_set
*set
)
7319 if (!isl_map_compatible_domain(map
, set
))
7320 isl_die(set
->ctx
, isl_error_invalid
,
7321 "incompatible spaces", goto error
);
7323 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7330 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7331 __isl_take isl_set
*set
)
7333 return isl_map_align_params_map_map_and(map
, set
,
7334 &map_intersect_domain
);
7337 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7338 __isl_take isl_map
*map2
)
7342 map1
= isl_map_reverse(map1
);
7343 map1
= isl_map_apply_range(map1
, map2
);
7344 return isl_map_reverse(map1
);
7351 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7352 __isl_take isl_map
*map2
)
7354 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7357 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7358 __isl_take isl_map
*map2
)
7360 isl_space
*dim_result
;
7361 struct isl_map
*result
;
7367 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7368 isl_space_copy(map2
->dim
));
7370 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7373 for (i
= 0; i
< map1
->n
; ++i
)
7374 for (j
= 0; j
< map2
->n
; ++j
) {
7375 result
= isl_map_add_basic_map(result
,
7376 isl_basic_map_apply_range(
7377 isl_basic_map_copy(map1
->p
[i
]),
7378 isl_basic_map_copy(map2
->p
[j
])));
7384 if (result
&& result
->n
<= 1)
7385 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7393 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7394 __isl_take isl_map
*map2
)
7396 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7400 * returns range - domain
7402 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7404 isl_space
*target_space
;
7405 struct isl_basic_set
*bset
;
7412 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7413 bmap
->dim
, isl_dim_out
),
7415 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7416 dim
= isl_basic_map_n_in(bmap
);
7417 nparam
= isl_basic_map_n_param(bmap
);
7418 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7419 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7420 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7421 for (i
= 0; i
< dim
; ++i
) {
7422 int j
= isl_basic_map_alloc_equality(bmap
);
7424 bmap
= isl_basic_map_free(bmap
);
7427 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7428 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7429 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7430 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7432 bset
= isl_basic_map_domain(bmap
);
7433 bset
= isl_basic_set_reset_space(bset
, target_space
);
7436 isl_basic_map_free(bmap
);
7441 * returns range - domain
7443 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7447 struct isl_set
*result
;
7452 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7453 map
->dim
, isl_dim_out
),
7455 dim
= isl_map_get_space(map
);
7456 dim
= isl_space_domain(dim
);
7457 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7460 for (i
= 0; i
< map
->n
; ++i
)
7461 result
= isl_set_add_basic_set(result
,
7462 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7471 * returns [domain -> range] -> range - domain
7473 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7474 __isl_take isl_basic_map
*bmap
)
7478 isl_basic_map
*domain
;
7482 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7483 bmap
->dim
, isl_dim_out
))
7484 isl_die(bmap
->ctx
, isl_error_invalid
,
7485 "domain and range don't match", goto error
);
7487 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7488 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7490 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7491 domain
= isl_basic_map_universe(dim
);
7493 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7494 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7495 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7497 total
= isl_basic_map_total_dim(bmap
);
7499 for (i
= 0; i
< n
; ++i
) {
7500 k
= isl_basic_map_alloc_equality(bmap
);
7503 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7504 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7505 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7506 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7509 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7510 return isl_basic_map_finalize(bmap
);
7512 isl_basic_map_free(bmap
);
7517 * returns [domain -> range] -> range - domain
7519 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7522 isl_space
*domain_dim
;
7527 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7528 map
->dim
, isl_dim_out
))
7529 isl_die(map
->ctx
, isl_error_invalid
,
7530 "domain and range don't match", goto error
);
7532 map
= isl_map_cow(map
);
7536 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7537 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7538 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7541 for (i
= 0; i
< map
->n
; ++i
) {
7542 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7546 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7553 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7555 struct isl_basic_map
*bmap
;
7563 nparam
= dims
->nparam
;
7565 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7569 for (i
= 0; i
< dim
; ++i
) {
7570 int j
= isl_basic_map_alloc_equality(bmap
);
7573 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7574 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7575 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7577 return isl_basic_map_finalize(bmap
);
7579 isl_basic_map_free(bmap
);
7583 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7587 if (dim
->n_in
!= dim
->n_out
)
7588 isl_die(dim
->ctx
, isl_error_invalid
,
7589 "number of input and output dimensions needs to be "
7590 "the same", goto error
);
7591 return basic_map_identity(dim
);
7593 isl_space_free(dim
);
7597 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7599 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7602 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7604 isl_space
*dim
= isl_set_get_space(set
);
7606 id
= isl_map_identity(isl_space_map_from_set(dim
));
7607 return isl_map_intersect_range(id
, set
);
7610 /* Construct a basic set with all set dimensions having only non-negative
7613 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7614 __isl_take isl_space
*space
)
7619 struct isl_basic_set
*bset
;
7623 nparam
= space
->nparam
;
7625 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7628 for (i
= 0; i
< dim
; ++i
) {
7629 int k
= isl_basic_set_alloc_inequality(bset
);
7632 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7633 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7637 isl_basic_set_free(bset
);
7641 /* Construct the half-space x_pos >= 0.
7643 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7647 isl_basic_set
*nonneg
;
7649 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7650 k
= isl_basic_set_alloc_inequality(nonneg
);
7653 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7654 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7656 return isl_basic_set_finalize(nonneg
);
7658 isl_basic_set_free(nonneg
);
7662 /* Construct the half-space x_pos <= -1.
7664 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7669 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7670 k
= isl_basic_set_alloc_inequality(neg
);
7673 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7674 isl_int_set_si(neg
->ineq
[k
][0], -1);
7675 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7677 return isl_basic_set_finalize(neg
);
7679 isl_basic_set_free(neg
);
7683 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7684 enum isl_dim_type type
, unsigned first
, unsigned n
)
7688 isl_basic_set
*nonneg
;
7696 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7698 offset
= pos(set
->dim
, type
);
7699 for (i
= 0; i
< n
; ++i
) {
7700 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7701 offset
+ first
+ i
);
7702 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7704 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7713 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7714 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7721 if (isl_set_plain_is_empty(set
)) {
7726 return fn(set
, signs
, user
);
7729 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7731 half
= isl_set_intersect(half
, isl_set_copy(set
));
7732 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7736 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7738 half
= isl_set_intersect(half
, set
);
7739 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7745 /* Call "fn" on the intersections of "set" with each of the orthants
7746 * (except for obviously empty intersections). The orthant is identified
7747 * by the signs array, with each entry having value 1 or -1 according
7748 * to the sign of the corresponding variable.
7750 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7751 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7761 if (isl_set_plain_is_empty(set
))
7764 nparam
= isl_set_dim(set
, isl_dim_param
);
7765 nvar
= isl_set_dim(set
, isl_dim_set
);
7767 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7769 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7777 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7779 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7782 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7783 __isl_keep isl_basic_map
*bmap2
)
7786 struct isl_map
*map1
;
7787 struct isl_map
*map2
;
7789 if (!bmap1
|| !bmap2
)
7790 return isl_bool_error
;
7792 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7793 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7795 is_subset
= isl_map_is_subset(map1
, map2
);
7803 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7804 __isl_keep isl_basic_set
*bset2
)
7806 return isl_basic_map_is_subset(bset1
, bset2
);
7809 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7810 __isl_keep isl_basic_map
*bmap2
)
7814 if (!bmap1
|| !bmap2
)
7815 return isl_bool_error
;
7816 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7817 if (is_subset
!= isl_bool_true
)
7819 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7823 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7824 __isl_keep isl_basic_set
*bset2
)
7826 return isl_basic_map_is_equal(
7827 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7830 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7836 return isl_bool_error
;
7837 for (i
= 0; i
< map
->n
; ++i
) {
7838 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7840 return isl_bool_error
;
7842 return isl_bool_false
;
7844 return isl_bool_true
;
7847 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7849 return map
? map
->n
== 0 : isl_bool_error
;
7852 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7854 return set
? set
->n
== 0 : isl_bool_error
;
7857 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7859 return isl_map_is_empty((struct isl_map
*)set
);
7862 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7867 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7870 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7875 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7878 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7883 return isl_bool_error
;
7884 is_subset
= isl_map_is_subset(map1
, map2
);
7885 if (is_subset
!= isl_bool_true
)
7887 is_subset
= isl_map_is_subset(map2
, map1
);
7891 /* Is "map1" equal to "map2"?
7893 * First check if they are obviously equal.
7894 * If not, then perform a more detailed analysis.
7896 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7900 equal
= isl_map_plain_is_equal(map1
, map2
);
7901 if (equal
< 0 || equal
)
7903 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7906 isl_bool
isl_basic_map_is_strict_subset(
7907 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7911 if (!bmap1
|| !bmap2
)
7912 return isl_bool_error
;
7913 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7914 if (is_subset
!= isl_bool_true
)
7916 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7917 if (is_subset
== isl_bool_error
)
7922 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7923 __isl_keep isl_map
*map2
)
7928 return isl_bool_error
;
7929 is_subset
= isl_map_is_subset(map1
, map2
);
7930 if (is_subset
!= isl_bool_true
)
7932 is_subset
= isl_map_is_subset(map2
, map1
);
7933 if (is_subset
== isl_bool_error
)
7938 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7939 __isl_keep isl_set
*set2
)
7941 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7944 /* Is "bmap" obviously equal to the universe with the same space?
7946 * That is, does it not have any constraints?
7948 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7951 return isl_bool_error
;
7952 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7955 /* Is "bset" obviously equal to the universe with the same space?
7957 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7959 return isl_basic_map_plain_is_universe(bset
);
7962 /* If "c" does not involve any existentially quantified variables,
7963 * then set *univ to false and abort
7965 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7967 isl_bool
*univ
= user
;
7970 n
= isl_constraint_dim(c
, isl_dim_div
);
7971 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
7972 isl_constraint_free(c
);
7973 if (*univ
< 0 || !*univ
)
7974 return isl_stat_error
;
7978 /* Is "bmap" equal to the universe with the same space?
7980 * First check if it is obviously equal to the universe.
7981 * If not and if there are any constraints not involving
7982 * existentially quantified variables, then it is certainly
7983 * not equal to the universe.
7984 * Otherwise, check if the universe is a subset of "bmap".
7986 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
7989 isl_basic_map
*test
;
7991 univ
= isl_basic_map_plain_is_universe(bmap
);
7992 if (univ
< 0 || univ
)
7994 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
7995 return isl_bool_false
;
7996 univ
= isl_bool_true
;
7997 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
7999 return isl_bool_error
;
8000 if (univ
< 0 || !univ
)
8002 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8003 univ
= isl_basic_map_is_subset(test
, bmap
);
8004 isl_basic_map_free(test
);
8008 /* Is "bset" equal to the universe with the same space?
8010 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8012 return isl_basic_map_is_universe(bset
);
8015 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8020 return isl_bool_error
;
8022 for (i
= 0; i
< map
->n
; ++i
) {
8023 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8028 return isl_bool_false
;
8031 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8033 return isl_map_plain_is_universe((isl_map
*) set
);
8036 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8038 struct isl_basic_set
*bset
= NULL
;
8039 struct isl_vec
*sample
= NULL
;
8040 isl_bool empty
, non_empty
;
8043 return isl_bool_error
;
8045 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8046 return isl_bool_true
;
8048 if (isl_basic_map_plain_is_universe(bmap
))
8049 return isl_bool_false
;
8051 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8052 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8053 copy
= isl_basic_map_remove_redundancies(copy
);
8054 empty
= isl_basic_map_plain_is_empty(copy
);
8055 isl_basic_map_free(copy
);
8059 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8061 return isl_bool_error
;
8063 return isl_bool_false
;
8064 isl_vec_free(bmap
->sample
);
8065 bmap
->sample
= NULL
;
8066 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8068 return isl_bool_error
;
8069 sample
= isl_basic_set_sample_vec(bset
);
8071 return isl_bool_error
;
8072 empty
= sample
->size
== 0;
8073 isl_vec_free(bmap
->sample
);
8074 bmap
->sample
= sample
;
8076 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8081 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8084 return isl_bool_error
;
8085 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8088 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8091 return isl_bool_error
;
8092 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8095 /* Is "bmap" known to be non-empty?
8097 * That is, is the cached sample still valid?
8099 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8104 return isl_bool_error
;
8106 return isl_bool_false
;
8107 total
= 1 + isl_basic_map_total_dim(bmap
);
8108 if (bmap
->sample
->size
!= total
)
8109 return isl_bool_false
;
8110 return isl_basic_map_contains(bmap
, bmap
->sample
);
8113 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8115 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8118 struct isl_map
*isl_basic_map_union(
8119 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8121 struct isl_map
*map
;
8122 if (!bmap1
|| !bmap2
)
8125 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8127 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8130 map
= isl_map_add_basic_map(map
, bmap1
);
8131 map
= isl_map_add_basic_map(map
, bmap2
);
8134 isl_basic_map_free(bmap1
);
8135 isl_basic_map_free(bmap2
);
8139 struct isl_set
*isl_basic_set_union(
8140 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8142 return (struct isl_set
*)isl_basic_map_union(
8143 (struct isl_basic_map
*)bset1
,
8144 (struct isl_basic_map
*)bset2
);
8147 /* Order divs such that any div only depends on previous divs */
8148 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8156 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8158 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8160 if (isl_int_is_zero(bmap
->div
[i
][0]))
8162 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8167 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8168 "integer division depends on itself",
8169 return isl_basic_map_free(bmap
));
8170 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8176 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8178 return (struct isl_basic_set
*)
8179 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8182 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8189 for (i
= 0; i
< map
->n
; ++i
) {
8190 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8201 /* Apply the expansion computed by isl_merge_divs.
8202 * The expansion itself is given by "exp" while the resulting
8203 * list of divs is given by "div".
8205 * Move the integer divisions of "bmap" into the right position
8206 * according to "exp" and then introduce the additional integer
8207 * divisions, adding div constraints.
8208 * The moving should be done first to avoid moving coefficients
8209 * in the definitions of the extra integer divisions.
8211 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8212 __isl_take isl_basic_set
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8217 bmap
= isl_basic_map_cow(bmap
);
8221 if (div
->n_row
< bmap
->n_div
)
8222 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8223 "not an expansion", goto error
);
8225 n_div
= bmap
->n_div
;
8226 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8227 div
->n_row
- n_div
, 0,
8228 2 * (div
->n_row
- n_div
));
8230 for (i
= n_div
; i
< div
->n_row
; ++i
)
8231 if (isl_basic_map_alloc_div(bmap
) < 0)
8234 for (j
= n_div
- 1; j
>= 0; --j
) {
8237 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8240 for (i
= 0; i
< div
->n_row
; ++i
) {
8241 if (j
< n_div
&& exp
[j
] == i
) {
8244 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8245 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8247 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8255 isl_basic_map_free(bmap
);
8260 /* Apply the expansion computed by isl_merge_divs.
8261 * The expansion itself is given by "exp" while the resulting
8262 * list of divs is given by "div".
8264 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8265 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8267 return isl_basic_map_expand_divs(bset
, div
, exp
);
8270 /* Look for a div in dst that corresponds to the div "div" in src.
8271 * The divs before "div" in src and dst are assumed to be the same.
8273 * Returns -1 if no corresponding div was found and the position
8274 * of the corresponding div in dst otherwise.
8276 static int find_div(struct isl_basic_map
*dst
,
8277 struct isl_basic_map
*src
, unsigned div
)
8281 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8283 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8284 for (i
= div
; i
< dst
->n_div
; ++i
)
8285 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8286 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8287 dst
->n_div
- div
) == -1)
8292 /* Align the divs of "dst" to those of "src", adding divs from "src"
8293 * if needed. That is, make sure that the first src->n_div divs
8294 * of the result are equal to those of src.
8296 * The result is not finalized as by design it will have redundant
8297 * divs if any divs from "src" were copied.
8299 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8300 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8303 int known
, extended
;
8307 return isl_basic_map_free(dst
);
8309 if (src
->n_div
== 0)
8312 known
= isl_basic_map_divs_known(src
);
8314 return isl_basic_map_free(dst
);
8316 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8317 "some src divs are unknown",
8318 return isl_basic_map_free(dst
));
8320 src
= isl_basic_map_order_divs(src
);
8323 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8324 for (i
= 0; i
< src
->n_div
; ++i
) {
8325 int j
= find_div(dst
, src
, i
);
8328 int extra
= src
->n_div
- i
;
8329 dst
= isl_basic_map_cow(dst
);
8332 dst
= isl_basic_map_extend_space(dst
,
8333 isl_space_copy(dst
->dim
),
8334 extra
, 0, 2 * extra
);
8337 j
= isl_basic_map_alloc_div(dst
);
8339 return isl_basic_map_free(dst
);
8340 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8341 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8342 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8343 return isl_basic_map_free(dst
);
8346 isl_basic_map_swap_div(dst
, i
, j
);
8351 struct isl_basic_set
*isl_basic_set_align_divs(
8352 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8354 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8355 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8358 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8366 map
= isl_map_compute_divs(map
);
8367 map
= isl_map_cow(map
);
8371 for (i
= 1; i
< map
->n
; ++i
)
8372 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8373 for (i
= 1; i
< map
->n
; ++i
) {
8374 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8376 return isl_map_free(map
);
8379 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8383 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8385 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8388 /* Align the divs of the basic maps in "map" to those
8389 * of the basic maps in "list", as well as to the other basic maps in "map".
8390 * The elements in "list" are assumed to have known divs.
8392 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8393 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8397 map
= isl_map_compute_divs(map
);
8398 map
= isl_map_cow(map
);
8400 return isl_map_free(map
);
8404 n
= isl_basic_map_list_n_basic_map(list
);
8405 for (i
= 0; i
< n
; ++i
) {
8406 isl_basic_map
*bmap
;
8408 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8409 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8410 isl_basic_map_free(bmap
);
8413 return isl_map_free(map
);
8415 return isl_map_align_divs(map
);
8418 /* Align the divs of each element of "list" to those of "bmap".
8419 * Both "bmap" and the elements of "list" are assumed to have known divs.
8421 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8422 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8427 return isl_basic_map_list_free(list
);
8429 n
= isl_basic_map_list_n_basic_map(list
);
8430 for (i
= 0; i
< n
; ++i
) {
8431 isl_basic_map
*bmap_i
;
8433 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8434 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8435 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8441 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8442 __isl_take isl_map
*map
)
8446 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8447 map
= isl_map_intersect_domain(map
, set
);
8448 set
= isl_map_range(map
);
8456 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8457 __isl_take isl_map
*map
)
8459 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8462 /* There is no need to cow as removing empty parts doesn't change
8463 * the meaning of the set.
8465 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8472 for (i
= map
->n
- 1; i
>= 0; --i
)
8473 remove_if_empty(map
, i
);
8478 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8480 return (struct isl_set
*)
8481 isl_map_remove_empty_parts((struct isl_map
*)set
);
8484 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8486 struct isl_basic_map
*bmap
;
8487 if (!map
|| map
->n
== 0)
8489 bmap
= map
->p
[map
->n
-1];
8490 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8491 return isl_basic_map_copy(bmap
);
8494 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8496 return (struct isl_basic_set
*)
8497 isl_map_copy_basic_map((struct isl_map
*)set
);
8500 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8501 __isl_keep isl_basic_map
*bmap
)
8507 for (i
= map
->n
-1; i
>= 0; --i
) {
8508 if (map
->p
[i
] != bmap
)
8510 map
= isl_map_cow(map
);
8513 isl_basic_map_free(map
->p
[i
]);
8514 if (i
!= map
->n
-1) {
8515 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8516 map
->p
[i
] = map
->p
[map
->n
-1];
8527 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8528 struct isl_basic_set
*bset
)
8530 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8531 (struct isl_basic_map
*)bset
);
8534 /* Given two basic sets bset1 and bset2, compute the maximal difference
8535 * between the values of dimension pos in bset1 and those in bset2
8536 * for any common value of the parameters and dimensions preceding pos.
8538 static enum isl_lp_result
basic_set_maximal_difference_at(
8539 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8540 int pos
, isl_int
*opt
)
8543 struct isl_basic_map
*bmap1
= NULL
;
8544 struct isl_basic_map
*bmap2
= NULL
;
8545 struct isl_ctx
*ctx
;
8546 struct isl_vec
*obj
;
8549 unsigned dim1
, dim2
;
8550 enum isl_lp_result res
;
8552 if (!bset1
|| !bset2
)
8553 return isl_lp_error
;
8555 nparam
= isl_basic_set_n_param(bset1
);
8556 dim1
= isl_basic_set_n_dim(bset1
);
8557 dim2
= isl_basic_set_n_dim(bset2
);
8558 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8559 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8560 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8561 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8562 if (!bmap1
|| !bmap2
)
8564 bmap1
= isl_basic_map_cow(bmap1
);
8565 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8566 pos
, (dim1
- pos
) + (dim2
- pos
),
8567 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8568 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8571 total
= isl_basic_map_total_dim(bmap1
);
8573 obj
= isl_vec_alloc(ctx
, 1 + total
);
8576 isl_seq_clr(obj
->block
.data
, 1 + total
);
8577 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8578 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8579 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8581 isl_basic_map_free(bmap1
);
8585 isl_basic_map_free(bmap2
);
8587 isl_basic_map_free(bmap1
);
8588 return isl_lp_error
;
8591 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8592 * for any common value of the parameters and dimensions preceding pos
8593 * in both basic sets, the values of dimension pos in bset1 are
8594 * smaller or larger than those in bset2.
8597 * 1 if bset1 follows bset2
8598 * -1 if bset1 precedes bset2
8599 * 0 if bset1 and bset2 are incomparable
8600 * -2 if some error occurred.
8602 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8603 struct isl_basic_set
*bset2
, int pos
)
8606 enum isl_lp_result res
;
8611 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8613 if (res
== isl_lp_empty
)
8615 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8616 res
== isl_lp_unbounded
)
8618 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8627 /* Given two basic sets bset1 and bset2, check whether
8628 * for any common value of the parameters and dimensions preceding pos
8629 * there is a value of dimension pos in bset1 that is larger
8630 * than a value of the same dimension in bset2.
8633 * 1 if there exists such a pair
8634 * 0 if there is no such pair, but there is a pair of equal values
8636 * -2 if some error occurred.
8638 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8639 __isl_keep isl_basic_set
*bset2
, int pos
)
8642 enum isl_lp_result res
;
8647 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8649 if (res
== isl_lp_empty
)
8651 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8652 res
== isl_lp_unbounded
)
8654 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8656 else if (res
== isl_lp_ok
)
8665 /* Given two sets set1 and set2, check whether
8666 * for any common value of the parameters and dimensions preceding pos
8667 * there is a value of dimension pos in set1 that is larger
8668 * than a value of the same dimension in set2.
8671 * 1 if there exists such a pair
8672 * 0 if there is no such pair, but there is a pair of equal values
8674 * -2 if some error occurred.
8676 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8677 __isl_keep isl_set
*set2
, int pos
)
8685 for (i
= 0; i
< set1
->n
; ++i
)
8686 for (j
= 0; j
< set2
->n
; ++j
) {
8688 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8689 if (f
== 1 || f
== -2)
8698 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8699 unsigned pos
, isl_int
*val
)
8707 total
= isl_basic_map_total_dim(bmap
);
8708 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8709 for (; d
+1 > pos
; --d
)
8710 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8714 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8716 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8718 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8721 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8727 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8728 unsigned pos
, isl_int
*val
)
8740 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8743 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8744 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8745 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8746 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8750 isl_int_set(*val
, v
);
8756 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8757 unsigned pos
, isl_int
*val
)
8759 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8763 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8766 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8769 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8770 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8772 if (pos
>= isl_basic_map_dim(bmap
, type
))
8774 return isl_basic_map_plain_has_fixed_var(bmap
,
8775 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8778 /* If "bmap" obviously lies on a hyperplane where the given dimension
8779 * has a fixed value, then return that value.
8780 * Otherwise return NaN.
8782 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8783 __isl_keep isl_basic_map
*bmap
,
8784 enum isl_dim_type type
, unsigned pos
)
8792 ctx
= isl_basic_map_get_ctx(bmap
);
8793 v
= isl_val_alloc(ctx
);
8796 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8798 return isl_val_free(v
);
8800 isl_int_set_si(v
->d
, 1);
8804 return isl_val_nan(ctx
);
8807 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8808 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8810 if (pos
>= isl_map_dim(map
, type
))
8812 return isl_map_plain_has_fixed_var(map
,
8813 map_offset(map
, type
) - 1 + pos
, val
);
8816 /* If "map" obviously lies on a hyperplane where the given dimension
8817 * has a fixed value, then return that value.
8818 * Otherwise return NaN.
8820 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8821 enum isl_dim_type type
, unsigned pos
)
8829 ctx
= isl_map_get_ctx(map
);
8830 v
= isl_val_alloc(ctx
);
8833 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8835 return isl_val_free(v
);
8837 isl_int_set_si(v
->d
, 1);
8841 return isl_val_nan(ctx
);
8844 /* If "set" obviously lies on a hyperplane where the given dimension
8845 * has a fixed value, then return that value.
8846 * Otherwise return NaN.
8848 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8849 enum isl_dim_type type
, unsigned pos
)
8851 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8854 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8855 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8857 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8860 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8861 * then return this fixed value in *val.
8863 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8864 unsigned dim
, isl_int
*val
)
8866 return isl_basic_set_plain_has_fixed_var(bset
,
8867 isl_basic_set_n_param(bset
) + dim
, val
);
8870 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8871 * then return this fixed value in *val.
8873 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8874 unsigned dim
, isl_int
*val
)
8876 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8879 /* Check if input variable in has fixed value and if so and if val is not NULL,
8880 * then return this fixed value in *val.
8882 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8883 unsigned in
, isl_int
*val
)
8885 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8888 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8889 * and if val is not NULL, then return this lower bound in *val.
8891 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8892 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8894 int i
, i_eq
= -1, i_ineq
= -1;
8901 total
= isl_basic_set_total_dim(bset
);
8902 nparam
= isl_basic_set_n_param(bset
);
8903 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8904 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8910 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8911 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8913 if (i_eq
!= -1 || i_ineq
!= -1)
8917 if (i_eq
== -1 && i_ineq
== -1)
8919 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8920 /* The coefficient should always be one due to normalization. */
8921 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8923 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8925 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8926 total
- nparam
- dim
- 1) != -1)
8929 isl_int_neg(*val
, c
[0]);
8933 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8934 unsigned dim
, isl_int
*val
)
8946 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8950 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8952 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8953 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8955 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8959 isl_int_set(*val
, v
);
8965 /* Return -1 if the constraint "c1" should be sorted before "c2"
8966 * and 1 if it should be sorted after "c2".
8967 * Return 0 if the two constraints are the same (up to the constant term).
8969 * In particular, if a constraint involves later variables than another
8970 * then it is sorted after this other constraint.
8971 * uset_gist depends on constraints without existentially quantified
8972 * variables sorting first.
8974 * For constraints that have the same latest variable, those
8975 * with the same coefficient for this latest variable (first in absolute value
8976 * and then in actual value) are grouped together.
8977 * This is useful for detecting pairs of constraints that can
8978 * be chained in their printed representation.
8980 * Finally, within a group, constraints are sorted according to
8981 * their coefficients (excluding the constant term).
8983 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8985 isl_int
**c1
= (isl_int
**) p1
;
8986 isl_int
**c2
= (isl_int
**) p2
;
8988 unsigned size
= *(unsigned *) arg
;
8991 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8992 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8997 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9000 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9004 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9007 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9008 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9009 * and 0 if the two constraints are the same (up to the constant term).
9011 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9012 isl_int
*c1
, isl_int
*c2
)
9018 total
= isl_basic_map_total_dim(bmap
);
9019 return sort_constraint_cmp(&c1
, &c2
, &total
);
9022 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9023 __isl_take isl_basic_map
*bmap
)
9029 if (bmap
->n_ineq
== 0)
9031 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9033 total
= isl_basic_map_total_dim(bmap
);
9034 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9035 &sort_constraint_cmp
, &total
) < 0)
9036 return isl_basic_map_free(bmap
);
9040 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9041 __isl_take isl_basic_set
*bset
)
9043 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9044 (struct isl_basic_map
*)bset
);
9047 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9051 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9053 bmap
= isl_basic_map_remove_redundancies(bmap
);
9054 bmap
= isl_basic_map_sort_constraints(bmap
);
9056 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9060 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9062 return (struct isl_basic_set
*)isl_basic_map_normalize(
9063 (struct isl_basic_map
*)bset
);
9066 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9067 const __isl_keep isl_basic_map
*bmap2
)
9072 if (!bmap1
|| !bmap2
)
9077 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9078 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9079 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9080 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9081 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9082 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9083 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9084 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9085 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9086 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9087 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9089 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9091 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9093 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9094 return bmap1
->n_eq
- bmap2
->n_eq
;
9095 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9096 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9097 if (bmap1
->n_div
!= bmap2
->n_div
)
9098 return bmap1
->n_div
- bmap2
->n_div
;
9099 total
= isl_basic_map_total_dim(bmap1
);
9100 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9101 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9105 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9106 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9110 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9111 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9118 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9119 const __isl_keep isl_basic_set
*bset2
)
9121 return isl_basic_map_plain_cmp(bset1
, bset2
);
9124 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9130 if (set1
->n
!= set2
->n
)
9131 return set1
->n
- set2
->n
;
9133 for (i
= 0; i
< set1
->n
; ++i
) {
9134 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9142 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9143 __isl_keep isl_basic_map
*bmap2
)
9145 if (!bmap1
|| !bmap2
)
9146 return isl_bool_error
;
9147 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9150 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9151 __isl_keep isl_basic_set
*bset2
)
9153 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9154 (isl_basic_map
*)bset2
);
9157 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9159 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9160 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9162 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9165 /* Sort the basic maps of "map" and remove duplicate basic maps.
9167 * While removing basic maps, we make sure that the basic maps remain
9168 * sorted because isl_map_normalize expects the basic maps of the result
9171 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9175 map
= isl_map_remove_empty_parts(map
);
9178 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9179 for (i
= map
->n
- 1; i
>= 1; --i
) {
9180 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9182 isl_basic_map_free(map
->p
[i
-1]);
9183 for (j
= i
; j
< map
->n
; ++j
)
9184 map
->p
[j
- 1] = map
->p
[j
];
9191 /* Remove obvious duplicates among the basic maps of "map".
9193 * Unlike isl_map_normalize, this function does not remove redundant
9194 * constraints and only removes duplicates that have exactly the same
9195 * constraints in the input. It does sort the constraints and
9196 * the basic maps to ease the detection of duplicates.
9198 * If "map" has already been normalized or if the basic maps are
9199 * disjoint, then there can be no duplicates.
9201 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9204 isl_basic_map
*bmap
;
9210 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9212 for (i
= 0; i
< map
->n
; ++i
) {
9213 bmap
= isl_basic_map_copy(map
->p
[i
]);
9214 bmap
= isl_basic_map_sort_constraints(bmap
);
9216 return isl_map_free(map
);
9217 isl_basic_map_free(map
->p
[i
]);
9221 map
= sort_and_remove_duplicates(map
);
9225 /* We normalize in place, but if anything goes wrong we need
9226 * to return NULL, so we need to make sure we don't change the
9227 * meaning of any possible other copies of map.
9229 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9232 struct isl_basic_map
*bmap
;
9236 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9238 for (i
= 0; i
< map
->n
; ++i
) {
9239 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9242 isl_basic_map_free(map
->p
[i
]);
9246 map
= sort_and_remove_duplicates(map
);
9248 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9255 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9257 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9260 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9261 __isl_keep isl_map
*map2
)
9267 return isl_bool_error
;
9270 return isl_bool_true
;
9271 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9272 return isl_bool_false
;
9274 map1
= isl_map_copy(map1
);
9275 map2
= isl_map_copy(map2
);
9276 map1
= isl_map_normalize(map1
);
9277 map2
= isl_map_normalize(map2
);
9280 equal
= map1
->n
== map2
->n
;
9281 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9282 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9292 return isl_bool_error
;
9295 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9296 __isl_keep isl_set
*set2
)
9298 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9299 (struct isl_map
*)set2
);
9302 /* Return an interval that ranges from min to max (inclusive)
9304 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9305 isl_int min
, isl_int max
)
9308 struct isl_basic_set
*bset
= NULL
;
9310 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9314 k
= isl_basic_set_alloc_inequality(bset
);
9317 isl_int_set_si(bset
->ineq
[k
][1], 1);
9318 isl_int_neg(bset
->ineq
[k
][0], min
);
9320 k
= isl_basic_set_alloc_inequality(bset
);
9323 isl_int_set_si(bset
->ineq
[k
][1], -1);
9324 isl_int_set(bset
->ineq
[k
][0], max
);
9328 isl_basic_set_free(bset
);
9332 /* Return the basic maps in "map" as a list.
9334 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9335 __isl_keep isl_map
*map
)
9339 isl_basic_map_list
*list
;
9343 ctx
= isl_map_get_ctx(map
);
9344 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9346 for (i
= 0; i
< map
->n
; ++i
) {
9347 isl_basic_map
*bmap
;
9349 bmap
= isl_basic_map_copy(map
->p
[i
]);
9350 list
= isl_basic_map_list_add(list
, bmap
);
9356 /* Return the intersection of the elements in the non-empty list "list".
9357 * All elements are assumed to live in the same space.
9359 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9360 __isl_take isl_basic_map_list
*list
)
9363 isl_basic_map
*bmap
;
9367 n
= isl_basic_map_list_n_basic_map(list
);
9369 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9370 "expecting non-empty list", goto error
);
9372 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9373 for (i
= 1; i
< n
; ++i
) {
9374 isl_basic_map
*bmap_i
;
9376 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9377 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9380 isl_basic_map_list_free(list
);
9383 isl_basic_map_list_free(list
);
9387 /* Return the intersection of the elements in the non-empty list "list".
9388 * All elements are assumed to live in the same space.
9390 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9391 __isl_take isl_basic_set_list
*list
)
9393 return isl_basic_map_list_intersect(list
);
9396 /* Return the union of the elements in the non-empty list "list".
9397 * All elements are assumed to live in the same space.
9399 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9406 n
= isl_set_list_n_set(list
);
9408 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9409 "expecting non-empty list", goto error
);
9411 set
= isl_set_list_get_set(list
, 0);
9412 for (i
= 1; i
< n
; ++i
) {
9415 set_i
= isl_set_list_get_set(list
, i
);
9416 set
= isl_set_union(set
, set_i
);
9419 isl_set_list_free(list
);
9422 isl_set_list_free(list
);
9426 /* Return the Cartesian product of the basic sets in list (in the given order).
9428 __isl_give isl_basic_set
*isl_basic_set_list_product(
9429 __isl_take
struct isl_basic_set_list
*list
)
9437 struct isl_basic_set
*product
= NULL
;
9441 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9442 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9443 nparam
= isl_basic_set_n_param(list
->p
[0]);
9444 dim
= isl_basic_set_n_dim(list
->p
[0]);
9445 extra
= list
->p
[0]->n_div
;
9446 n_eq
= list
->p
[0]->n_eq
;
9447 n_ineq
= list
->p
[0]->n_ineq
;
9448 for (i
= 1; i
< list
->n
; ++i
) {
9449 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9450 isl_assert(list
->ctx
,
9451 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9452 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9453 extra
+= list
->p
[i
]->n_div
;
9454 n_eq
+= list
->p
[i
]->n_eq
;
9455 n_ineq
+= list
->p
[i
]->n_ineq
;
9457 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9462 for (i
= 0; i
< list
->n
; ++i
) {
9463 isl_basic_set_add_constraints(product
,
9464 isl_basic_set_copy(list
->p
[i
]), dim
);
9465 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9467 isl_basic_set_list_free(list
);
9470 isl_basic_set_free(product
);
9471 isl_basic_set_list_free(list
);
9475 struct isl_basic_map
*isl_basic_map_product(
9476 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9478 isl_space
*dim_result
= NULL
;
9479 struct isl_basic_map
*bmap
;
9480 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9481 struct isl_dim_map
*dim_map1
, *dim_map2
;
9483 if (!bmap1
|| !bmap2
)
9486 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9487 bmap2
->dim
, isl_dim_param
), goto error
);
9488 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9489 isl_space_copy(bmap2
->dim
));
9491 in1
= isl_basic_map_n_in(bmap1
);
9492 in2
= isl_basic_map_n_in(bmap2
);
9493 out1
= isl_basic_map_n_out(bmap1
);
9494 out2
= isl_basic_map_n_out(bmap2
);
9495 nparam
= isl_basic_map_n_param(bmap1
);
9497 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9498 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9499 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9500 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9501 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9502 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9503 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9504 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9505 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9506 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9507 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9509 bmap
= isl_basic_map_alloc_space(dim_result
,
9510 bmap1
->n_div
+ bmap2
->n_div
,
9511 bmap1
->n_eq
+ bmap2
->n_eq
,
9512 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9513 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9514 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9515 bmap
= isl_basic_map_simplify(bmap
);
9516 return isl_basic_map_finalize(bmap
);
9518 isl_basic_map_free(bmap1
);
9519 isl_basic_map_free(bmap2
);
9523 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9524 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9526 isl_basic_map
*prod
;
9528 prod
= isl_basic_map_product(bmap1
, bmap2
);
9529 prod
= isl_basic_map_flatten(prod
);
9533 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9534 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9536 return isl_basic_map_flat_range_product(bset1
, bset2
);
9539 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9540 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9542 isl_space
*space_result
= NULL
;
9543 isl_basic_map
*bmap
;
9544 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9545 struct isl_dim_map
*dim_map1
, *dim_map2
;
9547 if (!bmap1
|| !bmap2
)
9550 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9551 isl_space_copy(bmap2
->dim
));
9553 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9554 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9555 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9556 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9558 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9559 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9560 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9561 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9562 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9563 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9564 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9565 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9566 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9567 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9568 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9570 bmap
= isl_basic_map_alloc_space(space_result
,
9571 bmap1
->n_div
+ bmap2
->n_div
,
9572 bmap1
->n_eq
+ bmap2
->n_eq
,
9573 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9574 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9575 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9576 bmap
= isl_basic_map_simplify(bmap
);
9577 return isl_basic_map_finalize(bmap
);
9579 isl_basic_map_free(bmap1
);
9580 isl_basic_map_free(bmap2
);
9584 __isl_give isl_basic_map
*isl_basic_map_range_product(
9585 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9587 isl_space
*dim_result
= NULL
;
9588 isl_basic_map
*bmap
;
9589 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9590 struct isl_dim_map
*dim_map1
, *dim_map2
;
9592 if (!bmap1
|| !bmap2
)
9595 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9596 bmap2
->dim
, isl_dim_param
))
9597 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9598 "parameters don't match", goto error
);
9600 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9601 isl_space_copy(bmap2
->dim
));
9603 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9604 out1
= isl_basic_map_n_out(bmap1
);
9605 out2
= isl_basic_map_n_out(bmap2
);
9606 nparam
= isl_basic_map_n_param(bmap1
);
9608 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9609 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9610 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9611 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9612 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9613 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9614 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9615 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9616 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9617 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9618 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9620 bmap
= isl_basic_map_alloc_space(dim_result
,
9621 bmap1
->n_div
+ bmap2
->n_div
,
9622 bmap1
->n_eq
+ bmap2
->n_eq
,
9623 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9624 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9625 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9626 bmap
= isl_basic_map_simplify(bmap
);
9627 return isl_basic_map_finalize(bmap
);
9629 isl_basic_map_free(bmap1
);
9630 isl_basic_map_free(bmap2
);
9634 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9635 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9637 isl_basic_map
*prod
;
9639 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9640 prod
= isl_basic_map_flatten_range(prod
);
9644 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9645 * and collect the results.
9646 * The result live in the space obtained by calling "space_product"
9647 * on the spaces of "map1" and "map2".
9648 * If "remove_duplicates" is set then the result may contain duplicates
9649 * (even if the inputs do not) and so we try and remove the obvious
9652 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9653 __isl_take isl_map
*map2
,
9654 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9655 __isl_take isl_space
*right
),
9656 __isl_give isl_basic_map
*(*basic_map_product
)(
9657 __isl_take isl_basic_map
*left
,
9658 __isl_take isl_basic_map
*right
),
9659 int remove_duplicates
)
9662 struct isl_map
*result
;
9668 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9669 map2
->dim
, isl_dim_param
), goto error
);
9671 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9672 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9673 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9675 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9676 isl_space_copy(map2
->dim
)),
9677 map1
->n
* map2
->n
, flags
);
9680 for (i
= 0; i
< map1
->n
; ++i
)
9681 for (j
= 0; j
< map2
->n
; ++j
) {
9682 struct isl_basic_map
*part
;
9683 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9684 isl_basic_map_copy(map2
->p
[j
]));
9685 if (isl_basic_map_is_empty(part
))
9686 isl_basic_map_free(part
);
9688 result
= isl_map_add_basic_map(result
, part
);
9692 if (remove_duplicates
)
9693 result
= isl_map_remove_obvious_duplicates(result
);
9703 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9705 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9706 __isl_take isl_map
*map2
)
9708 return map_product(map1
, map2
, &isl_space_product
,
9709 &isl_basic_map_product
, 0);
9712 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9713 __isl_take isl_map
*map2
)
9715 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9718 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9720 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9721 __isl_take isl_map
*map2
)
9725 prod
= isl_map_product(map1
, map2
);
9726 prod
= isl_map_flatten(prod
);
9730 /* Given two set A and B, construct its Cartesian product A x B.
9732 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9734 return isl_map_range_product(set1
, set2
);
9737 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9738 __isl_take isl_set
*set2
)
9740 return isl_map_flat_range_product(set1
, set2
);
9743 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9745 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9746 __isl_take isl_map
*map2
)
9748 return map_product(map1
, map2
, &isl_space_domain_product
,
9749 &isl_basic_map_domain_product
, 1);
9752 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9754 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9755 __isl_take isl_map
*map2
)
9757 return map_product(map1
, map2
, &isl_space_range_product
,
9758 &isl_basic_map_range_product
, 1);
9761 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9762 __isl_take isl_map
*map2
)
9764 return isl_map_align_params_map_map_and(map1
, map2
,
9765 &map_domain_product_aligned
);
9768 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9769 __isl_take isl_map
*map2
)
9771 return isl_map_align_params_map_map_and(map1
, map2
,
9772 &map_range_product_aligned
);
9775 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9777 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9780 int total1
, keep1
, total2
, keep2
;
9784 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9785 !isl_space_range_is_wrapping(map
->dim
))
9786 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9787 "not a product", return isl_map_free(map
));
9789 space
= isl_map_get_space(map
);
9790 total1
= isl_space_dim(space
, isl_dim_in
);
9791 total2
= isl_space_dim(space
, isl_dim_out
);
9792 space
= isl_space_factor_domain(space
);
9793 keep1
= isl_space_dim(space
, isl_dim_in
);
9794 keep2
= isl_space_dim(space
, isl_dim_out
);
9795 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9796 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9797 map
= isl_map_reset_space(map
, space
);
9802 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9804 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9807 int total1
, keep1
, total2
, keep2
;
9811 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9812 !isl_space_range_is_wrapping(map
->dim
))
9813 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9814 "not a product", return isl_map_free(map
));
9816 space
= isl_map_get_space(map
);
9817 total1
= isl_space_dim(space
, isl_dim_in
);
9818 total2
= isl_space_dim(space
, isl_dim_out
);
9819 space
= isl_space_factor_range(space
);
9820 keep1
= isl_space_dim(space
, isl_dim_in
);
9821 keep2
= isl_space_dim(space
, isl_dim_out
);
9822 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9823 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9824 map
= isl_map_reset_space(map
, space
);
9829 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9831 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9838 if (!isl_space_domain_is_wrapping(map
->dim
))
9839 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9840 "domain is not a product", return isl_map_free(map
));
9842 space
= isl_map_get_space(map
);
9843 total
= isl_space_dim(space
, isl_dim_in
);
9844 space
= isl_space_domain_factor_domain(space
);
9845 keep
= isl_space_dim(space
, isl_dim_in
);
9846 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9847 map
= isl_map_reset_space(map
, space
);
9852 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9854 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9861 if (!isl_space_domain_is_wrapping(map
->dim
))
9862 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9863 "domain is not a product", return isl_map_free(map
));
9865 space
= isl_map_get_space(map
);
9866 total
= isl_space_dim(space
, isl_dim_in
);
9867 space
= isl_space_domain_factor_range(space
);
9868 keep
= isl_space_dim(space
, isl_dim_in
);
9869 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9870 map
= isl_map_reset_space(map
, space
);
9875 /* Given a map A -> [B -> C], extract the map A -> B.
9877 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9884 if (!isl_space_range_is_wrapping(map
->dim
))
9885 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9886 "range is not a product", return isl_map_free(map
));
9888 space
= isl_map_get_space(map
);
9889 total
= isl_space_dim(space
, isl_dim_out
);
9890 space
= isl_space_range_factor_domain(space
);
9891 keep
= isl_space_dim(space
, isl_dim_out
);
9892 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9893 map
= isl_map_reset_space(map
, space
);
9898 /* Given a map A -> [B -> C], extract the map A -> C.
9900 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9907 if (!isl_space_range_is_wrapping(map
->dim
))
9908 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9909 "range is not a product", return isl_map_free(map
));
9911 space
= isl_map_get_space(map
);
9912 total
= isl_space_dim(space
, isl_dim_out
);
9913 space
= isl_space_range_factor_range(space
);
9914 keep
= isl_space_dim(space
, isl_dim_out
);
9915 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9916 map
= isl_map_reset_space(map
, space
);
9921 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9923 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9924 __isl_take isl_map
*map2
)
9928 prod
= isl_map_domain_product(map1
, map2
);
9929 prod
= isl_map_flatten_domain(prod
);
9933 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9935 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9936 __isl_take isl_map
*map2
)
9940 prod
= isl_map_range_product(map1
, map2
);
9941 prod
= isl_map_flatten_range(prod
);
9945 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9948 uint32_t hash
= isl_hash_init();
9953 bmap
= isl_basic_map_copy(bmap
);
9954 bmap
= isl_basic_map_normalize(bmap
);
9957 total
= isl_basic_map_total_dim(bmap
);
9958 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9959 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9961 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9962 isl_hash_hash(hash
, c_hash
);
9964 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9965 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9967 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9968 isl_hash_hash(hash
, c_hash
);
9970 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9971 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9973 if (isl_int_is_zero(bmap
->div
[i
][0]))
9975 isl_hash_byte(hash
, i
& 0xFF);
9976 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9977 isl_hash_hash(hash
, c_hash
);
9979 isl_basic_map_free(bmap
);
9983 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9985 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9988 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9995 map
= isl_map_copy(map
);
9996 map
= isl_map_normalize(map
);
10000 hash
= isl_hash_init();
10001 for (i
= 0; i
< map
->n
; ++i
) {
10002 uint32_t bmap_hash
;
10003 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10004 isl_hash_hash(hash
, bmap_hash
);
10012 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10014 return isl_map_get_hash((isl_map
*)set
);
10017 /* Check if the value for dimension dim is completely determined
10018 * by the values of the other parameters and variables.
10019 * That is, check if dimension dim is involved in an equality.
10021 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10028 nparam
= isl_basic_set_n_param(bset
);
10029 for (i
= 0; i
< bset
->n_eq
; ++i
)
10030 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10035 /* Check if the value for dimension dim is completely determined
10036 * by the values of the other parameters and variables.
10037 * That is, check if dimension dim is involved in an equality
10038 * for each of the subsets.
10040 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10046 for (i
= 0; i
< set
->n
; ++i
) {
10048 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10055 /* Return the number of basic maps in the (current) representation of "map".
10057 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10059 return map
? map
->n
: 0;
10062 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10064 return set
? set
->n
: 0;
10067 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10068 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10073 return isl_stat_error
;
10075 for (i
= 0; i
< map
->n
; ++i
)
10076 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10077 return isl_stat_error
;
10079 return isl_stat_ok
;
10082 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10083 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10088 return isl_stat_error
;
10090 for (i
= 0; i
< set
->n
; ++i
)
10091 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10092 return isl_stat_error
;
10094 return isl_stat_ok
;
10097 /* Return a list of basic sets, the union of which is equal to "set".
10099 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10100 __isl_keep isl_set
*set
)
10103 isl_basic_set_list
*list
;
10108 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10109 for (i
= 0; i
< set
->n
; ++i
) {
10110 isl_basic_set
*bset
;
10112 bset
= isl_basic_set_copy(set
->p
[i
]);
10113 list
= isl_basic_set_list_add(list
, bset
);
10119 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10126 bset
= isl_basic_set_cow(bset
);
10130 dim
= isl_basic_set_get_space(bset
);
10131 dim
= isl_space_lift(dim
, bset
->n_div
);
10134 isl_space_free(bset
->dim
);
10136 bset
->extra
-= bset
->n_div
;
10139 bset
= isl_basic_set_finalize(bset
);
10143 isl_basic_set_free(bset
);
10147 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10153 set
= isl_set_align_divs(set
);
10158 set
= isl_set_cow(set
);
10162 n_div
= set
->p
[0]->n_div
;
10163 dim
= isl_set_get_space(set
);
10164 dim
= isl_space_lift(dim
, n_div
);
10167 isl_space_free(set
->dim
);
10170 for (i
= 0; i
< set
->n
; ++i
) {
10171 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10182 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10185 struct isl_basic_map
*bmap
;
10192 set
= isl_set_align_divs(set
);
10197 dim
= isl_set_get_space(set
);
10198 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10200 return isl_map_identity(isl_space_map_from_set(dim
));
10203 n_div
= set
->p
[0]->n_div
;
10204 dim
= isl_space_map_from_set(dim
);
10205 n_param
= isl_space_dim(dim
, isl_dim_param
);
10206 n_set
= isl_space_dim(dim
, isl_dim_in
);
10207 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10208 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10209 for (i
= 0; i
< n_set
; ++i
)
10210 bmap
= var_equal(bmap
, i
);
10212 total
= n_param
+ n_set
+ n_set
+ n_div
;
10213 for (i
= 0; i
< n_div
; ++i
) {
10214 k
= isl_basic_map_alloc_inequality(bmap
);
10217 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10218 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10219 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10220 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10221 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10222 set
->p
[0]->div
[i
][0]);
10224 l
= isl_basic_map_alloc_inequality(bmap
);
10227 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10228 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10229 set
->p
[0]->div
[i
][0]);
10230 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10234 bmap
= isl_basic_map_simplify(bmap
);
10235 bmap
= isl_basic_map_finalize(bmap
);
10236 return isl_map_from_basic_map(bmap
);
10239 isl_basic_map_free(bmap
);
10243 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10251 dim
= isl_basic_set_total_dim(bset
);
10252 size
+= bset
->n_eq
* (1 + dim
);
10253 size
+= bset
->n_ineq
* (1 + dim
);
10254 size
+= bset
->n_div
* (2 + dim
);
10259 int isl_set_size(__isl_keep isl_set
*set
)
10267 for (i
= 0; i
< set
->n
; ++i
)
10268 size
+= isl_basic_set_size(set
->p
[i
]);
10273 /* Check if there is any lower bound (if lower == 0) and/or upper
10274 * bound (if upper == 0) on the specified dim.
10276 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10277 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10282 return isl_bool_error
;
10284 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10285 return isl_bool_error
);
10287 pos
+= isl_basic_map_offset(bmap
, type
);
10289 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10290 if (isl_int_is_zero(bmap
->div
[i
][0]))
10292 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10293 return isl_bool_true
;
10296 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10297 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10298 return isl_bool_true
;
10300 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10301 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10308 return lower
&& upper
;
10311 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10312 enum isl_dim_type type
, unsigned pos
)
10314 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10317 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10318 enum isl_dim_type type
, unsigned pos
)
10320 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10323 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10324 enum isl_dim_type type
, unsigned pos
)
10326 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10329 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10330 enum isl_dim_type type
, unsigned pos
)
10337 for (i
= 0; i
< map
->n
; ++i
) {
10339 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10340 if (bounded
< 0 || !bounded
)
10347 /* Return 1 if the specified dim is involved in both an upper bound
10348 * and a lower bound.
10350 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10351 enum isl_dim_type type
, unsigned pos
)
10353 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10356 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10358 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10359 enum isl_dim_type type
, unsigned pos
,
10360 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10361 enum isl_dim_type type
, unsigned pos
))
10366 return isl_bool_error
;
10368 for (i
= 0; i
< map
->n
; ++i
) {
10370 bounded
= fn(map
->p
[i
], type
, pos
);
10371 if (bounded
< 0 || bounded
)
10375 return isl_bool_false
;
10378 /* Return 1 if the specified dim is involved in any lower bound.
10380 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10381 enum isl_dim_type type
, unsigned pos
)
10383 return has_any_bound(set
, type
, pos
,
10384 &isl_basic_map_dim_has_lower_bound
);
10387 /* Return 1 if the specified dim is involved in any upper bound.
10389 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10390 enum isl_dim_type type
, unsigned pos
)
10392 return has_any_bound(set
, type
, pos
,
10393 &isl_basic_map_dim_has_upper_bound
);
10396 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10398 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10399 enum isl_dim_type type
, unsigned pos
,
10400 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10401 enum isl_dim_type type
, unsigned pos
))
10406 return isl_bool_error
;
10408 for (i
= 0; i
< map
->n
; ++i
) {
10410 bounded
= fn(map
->p
[i
], type
, pos
);
10411 if (bounded
< 0 || !bounded
)
10415 return isl_bool_true
;
10418 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10420 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10421 enum isl_dim_type type
, unsigned pos
)
10423 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10426 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10428 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10429 enum isl_dim_type type
, unsigned pos
)
10431 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10434 /* For each of the "n" variables starting at "first", determine
10435 * the sign of the variable and put the results in the first "n"
10436 * elements of the array "signs".
10438 * 1 means that the variable is non-negative
10439 * -1 means that the variable is non-positive
10440 * 0 means the variable attains both positive and negative values.
10442 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10443 unsigned first
, unsigned n
, int *signs
)
10445 isl_vec
*bound
= NULL
;
10446 struct isl_tab
*tab
= NULL
;
10447 struct isl_tab_undo
*snap
;
10450 if (!bset
|| !signs
)
10453 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10454 tab
= isl_tab_from_basic_set(bset
, 0);
10455 if (!bound
|| !tab
)
10458 isl_seq_clr(bound
->el
, bound
->size
);
10459 isl_int_set_si(bound
->el
[0], -1);
10461 snap
= isl_tab_snap(tab
);
10462 for (i
= 0; i
< n
; ++i
) {
10465 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10466 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10468 empty
= tab
->empty
;
10469 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10470 if (isl_tab_rollback(tab
, snap
) < 0)
10478 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10479 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10481 empty
= tab
->empty
;
10482 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10483 if (isl_tab_rollback(tab
, snap
) < 0)
10486 signs
[i
] = empty
? -1 : 0;
10490 isl_vec_free(bound
);
10494 isl_vec_free(bound
);
10498 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10499 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10501 if (!bset
|| !signs
)
10503 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10506 first
+= pos(bset
->dim
, type
) - 1;
10507 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10510 /* Is it possible for the integer division "div" to depend (possibly
10511 * indirectly) on any output dimensions?
10513 * If the div is undefined, then we conservatively assume that it
10514 * may depend on them.
10515 * Otherwise, we check if it actually depends on them or on any integer
10516 * divisions that may depend on them.
10518 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10521 unsigned n_out
, o_out
;
10522 unsigned n_div
, o_div
;
10524 if (isl_int_is_zero(bmap
->div
[div
][0]))
10527 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10528 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10530 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10533 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10534 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10536 for (i
= 0; i
< n_div
; ++i
) {
10537 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10539 if (div_may_involve_output(bmap
, i
))
10546 /* Return the first integer division of "bmap" in the range
10547 * [first, first + n[ that may depend on any output dimensions and
10548 * that has a non-zero coefficient in "c" (where the first coefficient
10549 * in "c" corresponds to integer division "first").
10551 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10552 isl_int
*c
, int first
, int n
)
10559 for (k
= first
; k
< first
+ n
; ++k
) {
10560 if (isl_int_is_zero(c
[k
]))
10562 if (div_may_involve_output(bmap
, k
))
10569 /* Look for a pair of inequality constraints in "bmap" of the form
10571 * -l + i >= 0 or i >= l
10573 * n + l - i >= 0 or i <= l + n
10575 * with n < "m" and i the output dimension at position "pos".
10576 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10577 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10578 * and earlier output dimensions, as well as integer divisions that do
10579 * not involve any of the output dimensions.
10581 * Return the index of the first inequality constraint or bmap->n_ineq
10582 * if no such pair can be found.
10584 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10585 int pos
, isl_int m
)
10590 unsigned n_div
, o_div
;
10591 unsigned n_out
, o_out
;
10597 ctx
= isl_basic_map_get_ctx(bmap
);
10598 total
= isl_basic_map_total_dim(bmap
);
10599 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10600 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10601 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10602 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10603 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10604 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10606 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10607 n_out
- (pos
+ 1)) != -1)
10609 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10612 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10613 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10616 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10617 bmap
->ineq
[j
] + 1, total
))
10621 if (j
>= bmap
->n_ineq
)
10623 isl_int_add(bmap
->ineq
[i
][0],
10624 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10625 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10626 isl_int_sub(bmap
->ineq
[i
][0],
10627 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10630 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10636 return bmap
->n_ineq
;
10639 /* Return the index of the equality of "bmap" that defines
10640 * the output dimension "pos" in terms of earlier dimensions.
10641 * The equality may also involve integer divisions, as long
10642 * as those integer divisions are defined in terms of
10643 * parameters or input dimensions.
10644 * In this case, *div is set to the number of integer divisions and
10645 * *ineq is set to the number of inequality constraints (provided
10646 * div and ineq are not NULL).
10648 * The equality may also involve a single integer division involving
10649 * the output dimensions (typically only output dimension "pos") as
10650 * long as the coefficient of output dimension "pos" is 1 or -1 and
10651 * there is a pair of constraints i >= l and i <= l + n, with i referring
10652 * to output dimension "pos", l an expression involving only earlier
10653 * dimensions and n smaller than the coefficient of the integer division
10654 * in the equality. In this case, the output dimension can be defined
10655 * in terms of a modulo expression that does not involve the integer division.
10656 * *div is then set to this single integer division and
10657 * *ineq is set to the index of constraint i >= l.
10659 * Return bmap->n_eq if there is no such equality.
10660 * Return -1 on error.
10662 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10663 int pos
, int *div
, int *ineq
)
10666 unsigned n_out
, o_out
;
10667 unsigned n_div
, o_div
;
10672 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10673 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10674 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10675 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10678 *ineq
= bmap
->n_ineq
;
10681 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10682 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10684 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10685 n_out
- (pos
+ 1)) != -1)
10687 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10691 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10692 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10694 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10695 k
+ 1, n_div
- (k
+1)) < n_div
)
10697 l
= find_modulo_constraint_pair(bmap
, pos
,
10698 bmap
->eq
[j
][o_div
+ k
]);
10701 if (l
>= bmap
->n_ineq
)
10713 /* Check if the given basic map is obviously single-valued.
10714 * In particular, for each output dimension, check that there is
10715 * an equality that defines the output dimension in terms of
10716 * earlier dimensions.
10718 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10724 return isl_bool_error
;
10726 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10728 for (i
= 0; i
< n_out
; ++i
) {
10731 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10734 return isl_bool_error
;
10735 if (eq
>= bmap
->n_eq
)
10736 return isl_bool_false
;
10739 return isl_bool_true
;
10742 /* Check if the given basic map is single-valued.
10743 * We simply compute
10747 * and check if the result is a subset of the identity mapping.
10749 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10752 isl_basic_map
*test
;
10756 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10760 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10761 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10763 space
= isl_basic_map_get_space(bmap
);
10764 space
= isl_space_map_from_set(isl_space_range(space
));
10765 id
= isl_basic_map_identity(space
);
10767 sv
= isl_basic_map_is_subset(test
, id
);
10769 isl_basic_map_free(test
);
10770 isl_basic_map_free(id
);
10775 /* Check if the given map is obviously single-valued.
10777 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10780 return isl_bool_error
;
10782 return isl_bool_true
;
10784 return isl_bool_false
;
10786 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10789 /* Check if the given map is single-valued.
10790 * We simply compute
10794 * and check if the result is a subset of the identity mapping.
10796 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10803 sv
= isl_map_plain_is_single_valued(map
);
10807 test
= isl_map_reverse(isl_map_copy(map
));
10808 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10810 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10811 id
= isl_map_identity(dim
);
10813 sv
= isl_map_is_subset(test
, id
);
10815 isl_map_free(test
);
10821 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10825 map
= isl_map_copy(map
);
10826 map
= isl_map_reverse(map
);
10827 in
= isl_map_is_single_valued(map
);
10833 /* Check if the given map is obviously injective.
10835 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10839 map
= isl_map_copy(map
);
10840 map
= isl_map_reverse(map
);
10841 in
= isl_map_plain_is_single_valued(map
);
10847 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10851 sv
= isl_map_is_single_valued(map
);
10855 return isl_map_is_injective(map
);
10858 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10860 return isl_map_is_single_valued((isl_map
*)set
);
10863 /* Does "map" only map elements to themselves?
10865 * If the domain and range spaces are different, then "map"
10866 * is considered not to be an identity relation, even if it is empty.
10867 * Otherwise, construct the maximal identity relation and
10868 * check whether "map" is a subset of this relation.
10870 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10874 isl_bool equal
, is_identity
;
10876 space
= isl_map_get_space(map
);
10877 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10878 isl_space_free(space
);
10879 if (equal
< 0 || !equal
)
10882 id
= isl_map_identity(isl_map_get_space(map
));
10883 is_identity
= isl_map_is_subset(map
, id
);
10886 return is_identity
;
10889 int isl_map_is_translation(__isl_keep isl_map
*map
)
10894 delta
= isl_map_deltas(isl_map_copy(map
));
10895 ok
= isl_set_is_singleton(delta
);
10896 isl_set_free(delta
);
10901 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10903 if (isl_seq_first_non_zero(p
, pos
) != -1)
10905 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10910 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10919 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10922 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10923 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10924 for (j
= 0; j
< nvar
; ++j
) {
10925 int lower
= 0, upper
= 0;
10926 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10927 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10929 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10933 if (i
< bset
->n_eq
)
10935 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10936 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10938 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10940 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10945 if (!lower
|| !upper
)
10952 int isl_set_is_box(__isl_keep isl_set
*set
)
10959 return isl_basic_set_is_box(set
->p
[0]);
10962 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10965 return isl_bool_error
;
10967 return isl_space_is_wrapping(bset
->dim
);
10970 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10973 return isl_bool_error
;
10975 return isl_space_is_wrapping(set
->dim
);
10978 /* Modify the space of "map" through a call to "change".
10979 * If "can_change" is set (not NULL), then first call it to check
10980 * if the modification is allowed, printing the error message "cannot_change"
10983 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10984 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10985 const char *cannot_change
,
10986 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10994 ok
= can_change
? can_change(map
) : isl_bool_true
;
10996 return isl_map_free(map
);
10998 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10999 return isl_map_free(map
));
11001 space
= change(isl_map_get_space(map
));
11002 map
= isl_map_reset_space(map
, space
);
11007 /* Is the domain of "map" a wrapped relation?
11009 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11012 return isl_bool_error
;
11014 return isl_space_domain_is_wrapping(map
->dim
);
11017 /* Is the range of "map" a wrapped relation?
11019 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11022 return isl_bool_error
;
11024 return isl_space_range_is_wrapping(map
->dim
);
11027 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11029 bmap
= isl_basic_map_cow(bmap
);
11033 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11037 bmap
= isl_basic_map_finalize(bmap
);
11039 return (isl_basic_set
*)bmap
;
11041 isl_basic_map_free(bmap
);
11045 /* Given a map A -> B, return the set (A -> B).
11047 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11049 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11052 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11054 bset
= isl_basic_set_cow(bset
);
11058 bset
->dim
= isl_space_unwrap(bset
->dim
);
11062 bset
= isl_basic_set_finalize(bset
);
11064 return (isl_basic_map
*)bset
;
11066 isl_basic_set_free(bset
);
11070 /* Given a set (A -> B), return the map A -> B.
11071 * Error out if "set" is not of the form (A -> B).
11073 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11075 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11076 "not a wrapping set", &isl_space_unwrap
);
11079 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11080 enum isl_dim_type type
)
11085 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11088 bmap
= isl_basic_map_cow(bmap
);
11092 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11096 bmap
= isl_basic_map_finalize(bmap
);
11100 isl_basic_map_free(bmap
);
11104 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11105 enum isl_dim_type type
)
11112 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11115 map
= isl_map_cow(map
);
11119 for (i
= 0; i
< map
->n
; ++i
) {
11120 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11124 map
->dim
= isl_space_reset(map
->dim
, type
);
11134 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11139 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11142 bmap
= isl_basic_map_cow(bmap
);
11146 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11150 bmap
= isl_basic_map_finalize(bmap
);
11154 isl_basic_map_free(bmap
);
11158 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11160 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11163 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11164 __isl_take isl_basic_map
*bmap
)
11169 if (!bmap
->dim
->nested
[0])
11172 bmap
= isl_basic_map_cow(bmap
);
11176 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11180 bmap
= isl_basic_map_finalize(bmap
);
11184 isl_basic_map_free(bmap
);
11188 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11189 __isl_take isl_basic_map
*bmap
)
11194 if (!bmap
->dim
->nested
[1])
11197 bmap
= isl_basic_map_cow(bmap
);
11201 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11205 bmap
= isl_basic_map_finalize(bmap
);
11209 isl_basic_map_free(bmap
);
11213 /* Remove any internal structure from the spaces of domain and range of "map".
11215 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11220 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11223 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11226 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11228 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11231 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11233 isl_space
*dim
, *flat_dim
;
11236 dim
= isl_set_get_space(set
);
11237 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11238 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11239 map
= isl_map_intersect_domain(map
, set
);
11244 /* Remove any internal structure from the space of the domain of "map".
11246 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11251 if (!map
->dim
->nested
[0])
11254 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11257 /* Remove any internal structure from the space of the range of "map".
11259 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11264 if (!map
->dim
->nested
[1])
11267 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11270 /* Reorder the dimensions of "bmap" according to the given dim_map
11271 * and set the dimension specification to "dim" and
11272 * perform Gaussian elimination on the result.
11274 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11275 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11277 isl_basic_map
*res
;
11280 bmap
= isl_basic_map_cow(bmap
);
11281 if (!bmap
|| !dim
|| !dim_map
)
11284 flags
= bmap
->flags
;
11285 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11286 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11287 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11288 res
= isl_basic_map_alloc_space(dim
,
11289 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11290 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11292 res
->flags
= flags
;
11293 res
= isl_basic_map_gauss(res
, NULL
);
11294 res
= isl_basic_map_finalize(res
);
11298 isl_basic_map_free(bmap
);
11299 isl_space_free(dim
);
11303 /* Reorder the dimensions of "map" according to given reordering.
11305 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11306 __isl_take isl_reordering
*r
)
11309 struct isl_dim_map
*dim_map
;
11311 map
= isl_map_cow(map
);
11312 dim_map
= isl_dim_map_from_reordering(r
);
11313 if (!map
|| !r
|| !dim_map
)
11316 for (i
= 0; i
< map
->n
; ++i
) {
11317 struct isl_dim_map
*dim_map_i
;
11319 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11321 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11322 isl_space_copy(r
->dim
), dim_map_i
);
11328 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11330 isl_reordering_free(r
);
11336 isl_reordering_free(r
);
11340 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11341 __isl_take isl_reordering
*r
)
11343 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11346 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11347 __isl_take isl_space
*model
)
11351 if (!map
|| !model
)
11354 ctx
= isl_space_get_ctx(model
);
11355 if (!isl_space_has_named_params(model
))
11356 isl_die(ctx
, isl_error_invalid
,
11357 "model has unnamed parameters", goto error
);
11358 if (!isl_space_has_named_params(map
->dim
))
11359 isl_die(ctx
, isl_error_invalid
,
11360 "relation has unnamed parameters", goto error
);
11361 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11362 isl_reordering
*exp
;
11364 model
= isl_space_drop_dims(model
, isl_dim_in
,
11365 0, isl_space_dim(model
, isl_dim_in
));
11366 model
= isl_space_drop_dims(model
, isl_dim_out
,
11367 0, isl_space_dim(model
, isl_dim_out
));
11368 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11369 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11370 map
= isl_map_realign(map
, exp
);
11373 isl_space_free(model
);
11376 isl_space_free(model
);
11381 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11382 __isl_take isl_space
*model
)
11384 return isl_map_align_params(set
, model
);
11387 /* Align the parameters of "bmap" to those of "model", introducing
11388 * additional parameters if needed.
11390 __isl_give isl_basic_map
*isl_basic_map_align_params(
11391 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11395 if (!bmap
|| !model
)
11398 ctx
= isl_space_get_ctx(model
);
11399 if (!isl_space_has_named_params(model
))
11400 isl_die(ctx
, isl_error_invalid
,
11401 "model has unnamed parameters", goto error
);
11402 if (!isl_space_has_named_params(bmap
->dim
))
11403 isl_die(ctx
, isl_error_invalid
,
11404 "relation has unnamed parameters", goto error
);
11405 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11406 isl_reordering
*exp
;
11407 struct isl_dim_map
*dim_map
;
11409 model
= isl_space_drop_dims(model
, isl_dim_in
,
11410 0, isl_space_dim(model
, isl_dim_in
));
11411 model
= isl_space_drop_dims(model
, isl_dim_out
,
11412 0, isl_space_dim(model
, isl_dim_out
));
11413 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11414 exp
= isl_reordering_extend_space(exp
,
11415 isl_basic_map_get_space(bmap
));
11416 dim_map
= isl_dim_map_from_reordering(exp
);
11417 bmap
= isl_basic_map_realign(bmap
,
11418 exp
? isl_space_copy(exp
->dim
) : NULL
,
11419 isl_dim_map_extend(dim_map
, bmap
));
11420 isl_reordering_free(exp
);
11424 isl_space_free(model
);
11427 isl_space_free(model
);
11428 isl_basic_map_free(bmap
);
11432 /* Align the parameters of "bset" to those of "model", introducing
11433 * additional parameters if needed.
11435 __isl_give isl_basic_set
*isl_basic_set_align_params(
11436 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11438 return isl_basic_map_align_params(bset
, model
);
11441 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11442 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11443 enum isl_dim_type c2
, enum isl_dim_type c3
,
11444 enum isl_dim_type c4
, enum isl_dim_type c5
)
11446 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11447 struct isl_mat
*mat
;
11453 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11454 isl_basic_map_total_dim(bmap
) + 1);
11457 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11458 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11459 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11460 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11461 isl_int_set(mat
->row
[i
][pos
],
11462 bmap
->eq
[i
][off
+ k
]);
11470 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11471 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11472 enum isl_dim_type c2
, enum isl_dim_type c3
,
11473 enum isl_dim_type c4
, enum isl_dim_type c5
)
11475 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11476 struct isl_mat
*mat
;
11482 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11483 isl_basic_map_total_dim(bmap
) + 1);
11486 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11487 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11488 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11489 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11490 isl_int_set(mat
->row
[i
][pos
],
11491 bmap
->ineq
[i
][off
+ k
]);
11499 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11500 __isl_take isl_space
*dim
,
11501 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11502 enum isl_dim_type c2
, enum isl_dim_type c3
,
11503 enum isl_dim_type c4
, enum isl_dim_type c5
)
11505 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11506 isl_basic_map
*bmap
;
11512 if (!dim
|| !eq
|| !ineq
)
11515 if (eq
->n_col
!= ineq
->n_col
)
11516 isl_die(dim
->ctx
, isl_error_invalid
,
11517 "equalities and inequalities matrices should have "
11518 "same number of columns", goto error
);
11520 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11522 if (eq
->n_col
< total
)
11523 isl_die(dim
->ctx
, isl_error_invalid
,
11524 "number of columns too small", goto error
);
11526 extra
= eq
->n_col
- total
;
11528 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11529 eq
->n_row
, ineq
->n_row
);
11532 for (i
= 0; i
< extra
; ++i
) {
11533 k
= isl_basic_map_alloc_div(bmap
);
11536 isl_int_set_si(bmap
->div
[k
][0], 0);
11538 for (i
= 0; i
< eq
->n_row
; ++i
) {
11539 l
= isl_basic_map_alloc_equality(bmap
);
11542 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11543 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11544 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11545 isl_int_set(bmap
->eq
[l
][off
+ k
],
11551 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11552 l
= isl_basic_map_alloc_inequality(bmap
);
11555 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11556 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11557 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11558 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11559 ineq
->row
[i
][pos
]);
11565 isl_space_free(dim
);
11567 isl_mat_free(ineq
);
11569 bmap
= isl_basic_map_simplify(bmap
);
11570 return isl_basic_map_finalize(bmap
);
11572 isl_space_free(dim
);
11574 isl_mat_free(ineq
);
11578 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11579 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11580 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11582 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11583 c1
, c2
, c3
, c4
, isl_dim_in
);
11586 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11587 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11588 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11590 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11591 c1
, c2
, c3
, c4
, isl_dim_in
);
11594 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11595 __isl_take isl_space
*dim
,
11596 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11597 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11599 return (isl_basic_set
*)
11600 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11601 c1
, c2
, c3
, c4
, isl_dim_in
);
11604 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11607 return isl_bool_error
;
11609 return isl_space_can_zip(bmap
->dim
);
11612 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11615 return isl_bool_error
;
11617 return isl_space_can_zip(map
->dim
);
11620 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11621 * (A -> C) -> (B -> D).
11623 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11632 if (!isl_basic_map_can_zip(bmap
))
11633 isl_die(bmap
->ctx
, isl_error_invalid
,
11634 "basic map cannot be zipped", goto error
);
11635 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11636 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11637 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11638 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11639 bmap
= isl_basic_map_cow(bmap
);
11640 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11643 bmap
->dim
= isl_space_zip(bmap
->dim
);
11646 bmap
= isl_basic_map_mark_final(bmap
);
11649 isl_basic_map_free(bmap
);
11653 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11654 * (A -> C) -> (B -> D).
11656 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11663 if (!isl_map_can_zip(map
))
11664 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11667 map
= isl_map_cow(map
);
11671 for (i
= 0; i
< map
->n
; ++i
) {
11672 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11677 map
->dim
= isl_space_zip(map
->dim
);
11687 /* Can we apply isl_basic_map_curry to "bmap"?
11688 * That is, does it have a nested relation in its domain?
11690 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11693 return isl_bool_error
;
11695 return isl_space_can_curry(bmap
->dim
);
11698 /* Can we apply isl_map_curry to "map"?
11699 * That is, does it have a nested relation in its domain?
11701 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11704 return isl_bool_error
;
11706 return isl_space_can_curry(map
->dim
);
11709 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11712 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11718 if (!isl_basic_map_can_curry(bmap
))
11719 isl_die(bmap
->ctx
, isl_error_invalid
,
11720 "basic map cannot be curried", goto error
);
11721 bmap
= isl_basic_map_cow(bmap
);
11724 bmap
->dim
= isl_space_curry(bmap
->dim
);
11727 bmap
= isl_basic_map_mark_final(bmap
);
11730 isl_basic_map_free(bmap
);
11734 /* Given a map (A -> B) -> C, return the corresponding map
11737 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11739 return isl_map_change_space(map
, &isl_map_can_curry
,
11740 "map cannot be curried", &isl_space_curry
);
11743 /* Can isl_map_range_curry be applied to "map"?
11744 * That is, does it have a nested relation in its range,
11745 * the domain of which is itself a nested relation?
11747 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11750 return isl_bool_error
;
11752 return isl_space_can_range_curry(map
->dim
);
11755 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11756 * A -> (B -> (C -> D)).
11758 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11760 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11761 "map range cannot be curried",
11762 &isl_space_range_curry
);
11765 /* Can we apply isl_basic_map_uncurry to "bmap"?
11766 * That is, does it have a nested relation in its domain?
11768 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11771 return isl_bool_error
;
11773 return isl_space_can_uncurry(bmap
->dim
);
11776 /* Can we apply isl_map_uncurry to "map"?
11777 * That is, does it have a nested relation in its domain?
11779 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11782 return isl_bool_error
;
11784 return isl_space_can_uncurry(map
->dim
);
11787 /* Given a basic map A -> (B -> C), return the corresponding basic map
11790 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11796 if (!isl_basic_map_can_uncurry(bmap
))
11797 isl_die(bmap
->ctx
, isl_error_invalid
,
11798 "basic map cannot be uncurried",
11799 return isl_basic_map_free(bmap
));
11800 bmap
= isl_basic_map_cow(bmap
);
11803 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11805 return isl_basic_map_free(bmap
);
11806 bmap
= isl_basic_map_mark_final(bmap
);
11810 /* Given a map A -> (B -> C), return the corresponding map
11813 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11815 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11816 "map cannot be uncurried", &isl_space_uncurry
);
11819 /* Construct a basic map mapping the domain of the affine expression
11820 * to a one-dimensional range prescribed by the affine expression.
11822 * A NaN affine expression cannot be converted to a basic map.
11824 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11829 isl_local_space
*ls
;
11830 isl_basic_map
*bmap
= NULL
;
11834 is_nan
= isl_aff_is_nan(aff
);
11838 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11839 "cannot convert NaN", goto error
);
11841 ls
= isl_aff_get_local_space(aff
);
11842 bmap
= isl_basic_map_from_local_space(ls
);
11843 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11844 k
= isl_basic_map_alloc_equality(bmap
);
11848 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11849 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11850 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11851 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11852 aff
->v
->size
- (pos
+ 1));
11855 bmap
= isl_basic_map_finalize(bmap
);
11859 isl_basic_map_free(bmap
);
11863 /* Construct a map mapping the domain of the affine expression
11864 * to a one-dimensional range prescribed by the affine expression.
11866 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11868 isl_basic_map
*bmap
;
11870 bmap
= isl_basic_map_from_aff(aff
);
11871 return isl_map_from_basic_map(bmap
);
11874 /* Construct a basic map mapping the domain the multi-affine expression
11875 * to its range, with each dimension in the range equated to the
11876 * corresponding affine expression.
11878 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11879 __isl_take isl_multi_aff
*maff
)
11883 isl_basic_map
*bmap
;
11888 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11889 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11890 "invalid space", goto error
);
11892 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11893 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11895 for (i
= 0; i
< maff
->n
; ++i
) {
11897 isl_basic_map
*bmap_i
;
11899 aff
= isl_aff_copy(maff
->p
[i
]);
11900 bmap_i
= isl_basic_map_from_aff(aff
);
11902 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11905 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11907 isl_multi_aff_free(maff
);
11910 isl_multi_aff_free(maff
);
11914 /* Construct a map mapping the domain the multi-affine expression
11915 * to its range, with each dimension in the range equated to the
11916 * corresponding affine expression.
11918 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11920 isl_basic_map
*bmap
;
11922 bmap
= isl_basic_map_from_multi_aff(maff
);
11923 return isl_map_from_basic_map(bmap
);
11926 /* Construct a basic map mapping a domain in the given space to
11927 * to an n-dimensional range, with n the number of elements in the list,
11928 * where each coordinate in the range is prescribed by the
11929 * corresponding affine expression.
11930 * The domains of all affine expressions in the list are assumed to match
11933 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11934 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11938 isl_basic_map
*bmap
;
11943 dim
= isl_space_from_domain(domain_dim
);
11944 bmap
= isl_basic_map_universe(dim
);
11946 for (i
= 0; i
< list
->n
; ++i
) {
11948 isl_basic_map
*bmap_i
;
11950 aff
= isl_aff_copy(list
->p
[i
]);
11951 bmap_i
= isl_basic_map_from_aff(aff
);
11953 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11956 isl_aff_list_free(list
);
11960 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11961 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11963 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11966 /* Construct a basic map where the given dimensions are equal to each other.
11968 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11969 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11971 isl_basic_map
*bmap
= NULL
;
11977 if (pos1
>= isl_space_dim(space
, type1
))
11978 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11979 "index out of bounds", goto error
);
11980 if (pos2
>= isl_space_dim(space
, type2
))
11981 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11982 "index out of bounds", goto error
);
11984 if (type1
== type2
&& pos1
== pos2
)
11985 return isl_basic_map_universe(space
);
11987 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11988 i
= isl_basic_map_alloc_equality(bmap
);
11991 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11992 pos1
+= isl_basic_map_offset(bmap
, type1
);
11993 pos2
+= isl_basic_map_offset(bmap
, type2
);
11994 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11995 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11996 bmap
= isl_basic_map_finalize(bmap
);
11997 isl_space_free(space
);
12000 isl_space_free(space
);
12001 isl_basic_map_free(bmap
);
12005 /* Add a constraint imposing that the given two dimensions are equal.
12007 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12008 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12012 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12014 bmap
= isl_basic_map_intersect(bmap
, eq
);
12019 /* Add a constraint imposing that the given two dimensions are equal.
12021 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12022 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12024 isl_basic_map
*bmap
;
12026 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12028 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12033 /* Add a constraint imposing that the given two dimensions have opposite values.
12035 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12036 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12038 isl_basic_map
*bmap
= NULL
;
12044 if (pos1
>= isl_map_dim(map
, type1
))
12045 isl_die(map
->ctx
, isl_error_invalid
,
12046 "index out of bounds", goto error
);
12047 if (pos2
>= isl_map_dim(map
, type2
))
12048 isl_die(map
->ctx
, isl_error_invalid
,
12049 "index out of bounds", goto error
);
12051 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12052 i
= isl_basic_map_alloc_equality(bmap
);
12055 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12056 pos1
+= isl_basic_map_offset(bmap
, type1
);
12057 pos2
+= isl_basic_map_offset(bmap
, type2
);
12058 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12059 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12060 bmap
= isl_basic_map_finalize(bmap
);
12062 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12066 isl_basic_map_free(bmap
);
12071 /* Construct a constraint imposing that the value of the first dimension is
12072 * greater than or equal to that of the second.
12074 static __isl_give isl_constraint
*constraint_order_ge(
12075 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12076 enum isl_dim_type type2
, int pos2
)
12083 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12085 if (pos1
>= isl_constraint_dim(c
, type1
))
12086 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12087 "index out of bounds", return isl_constraint_free(c
));
12088 if (pos2
>= isl_constraint_dim(c
, type2
))
12089 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12090 "index out of bounds", return isl_constraint_free(c
));
12092 if (type1
== type2
&& pos1
== pos2
)
12095 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12096 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12101 /* Add a constraint imposing that the value of the first dimension is
12102 * greater than or equal to that of the second.
12104 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12105 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12110 if (type1
== type2
&& pos1
== pos2
)
12112 space
= isl_basic_map_get_space(bmap
);
12113 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12114 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12119 /* Add a constraint imposing that the value of the first dimension is
12120 * greater than or equal to that of the second.
12122 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12123 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12128 if (type1
== type2
&& pos1
== pos2
)
12130 space
= isl_map_get_space(map
);
12131 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12132 map
= isl_map_add_constraint(map
, c
);
12137 /* Add a constraint imposing that the value of the first dimension is
12138 * less than or equal to that of the second.
12140 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12141 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12143 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12146 /* Construct a basic map where the value of the first dimension is
12147 * greater than that of the second.
12149 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12150 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12152 isl_basic_map
*bmap
= NULL
;
12158 if (pos1
>= isl_space_dim(space
, type1
))
12159 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12160 "index out of bounds", goto error
);
12161 if (pos2
>= isl_space_dim(space
, type2
))
12162 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12163 "index out of bounds", goto error
);
12165 if (type1
== type2
&& pos1
== pos2
)
12166 return isl_basic_map_empty(space
);
12168 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12169 i
= isl_basic_map_alloc_inequality(bmap
);
12171 return isl_basic_map_free(bmap
);
12172 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12173 pos1
+= isl_basic_map_offset(bmap
, type1
);
12174 pos2
+= isl_basic_map_offset(bmap
, type2
);
12175 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12176 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12177 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12178 bmap
= isl_basic_map_finalize(bmap
);
12182 isl_space_free(space
);
12183 isl_basic_map_free(bmap
);
12187 /* Add a constraint imposing that the value of the first dimension is
12188 * greater than that of the second.
12190 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12191 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12195 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12197 bmap
= isl_basic_map_intersect(bmap
, gt
);
12202 /* Add a constraint imposing that the value of the first dimension is
12203 * greater than that of the second.
12205 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12206 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12208 isl_basic_map
*bmap
;
12210 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12212 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12217 /* Add a constraint imposing that the value of the first dimension is
12218 * smaller than that of the second.
12220 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12221 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12223 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12226 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12230 isl_local_space
*ls
;
12235 if (!isl_basic_map_divs_known(bmap
))
12236 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12237 "some divs are unknown", return NULL
);
12239 ls
= isl_basic_map_get_local_space(bmap
);
12240 div
= isl_local_space_get_div(ls
, pos
);
12241 isl_local_space_free(ls
);
12246 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12249 return isl_basic_map_get_div(bset
, pos
);
12252 /* Plug in "subs" for dimension "type", "pos" of "bset".
12254 * Let i be the dimension to replace and let "subs" be of the form
12258 * Any integer division with a non-zero coefficient for i,
12260 * floor((a i + g)/m)
12264 * floor((a f + d g)/(m d))
12266 * Constraints of the form
12274 * We currently require that "subs" is an integral expression.
12275 * Handling rational expressions may require us to add stride constraints
12276 * as we do in isl_basic_set_preimage_multi_aff.
12278 __isl_give isl_basic_set
*isl_basic_set_substitute(
12279 __isl_take isl_basic_set
*bset
,
12280 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12286 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12289 bset
= isl_basic_set_cow(bset
);
12290 if (!bset
|| !subs
)
12293 ctx
= isl_basic_set_get_ctx(bset
);
12294 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12295 isl_die(ctx
, isl_error_invalid
,
12296 "spaces don't match", goto error
);
12297 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12298 isl_die(ctx
, isl_error_unsupported
,
12299 "cannot handle divs yet", goto error
);
12300 if (!isl_int_is_one(subs
->v
->el
[0]))
12301 isl_die(ctx
, isl_error_invalid
,
12302 "can only substitute integer expressions", goto error
);
12304 pos
+= isl_basic_set_offset(bset
, type
);
12308 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12309 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12311 isl_int_set(v
, bset
->eq
[i
][pos
]);
12312 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12313 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12314 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12317 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12318 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12320 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12321 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12322 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12323 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12326 for (i
= 0; i
< bset
->n_div
; ++i
) {
12327 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12329 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12330 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12331 isl_seq_combine(bset
->div
[i
] + 1,
12332 subs
->v
->el
[0], bset
->div
[i
] + 1,
12333 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12334 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12339 bset
= isl_basic_set_simplify(bset
);
12340 return isl_basic_set_finalize(bset
);
12342 isl_basic_set_free(bset
);
12346 /* Plug in "subs" for dimension "type", "pos" of "set".
12348 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12349 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12353 if (set
&& isl_set_plain_is_empty(set
))
12356 set
= isl_set_cow(set
);
12360 for (i
= set
->n
- 1; i
>= 0; --i
) {
12361 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12362 if (remove_if_empty(set
, i
) < 0)
12372 /* Check if the range of "ma" is compatible with the domain or range
12373 * (depending on "type") of "bmap".
12374 * Return -1 if anything is wrong.
12376 static int check_basic_map_compatible_range_multi_aff(
12377 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12378 __isl_keep isl_multi_aff
*ma
)
12381 isl_space
*ma_space
;
12383 ma_space
= isl_multi_aff_get_space(ma
);
12385 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12389 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12390 "parameters don't match", goto error
);
12391 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12395 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12396 "spaces don't match", goto error
);
12398 isl_space_free(ma_space
);
12401 isl_space_free(ma_space
);
12405 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12406 * coefficients before the transformed range of dimensions,
12407 * the "n_after" coefficients after the transformed range of dimensions
12408 * and the coefficients of the other divs in "bmap".
12410 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12411 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12416 isl_local_space
*ls
;
12421 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12425 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12426 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12427 for (i
= 0; i
< n_div
; ++i
) {
12428 int o_bmap
= 0, o_ls
= 0;
12430 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12431 o_bmap
+= 1 + 1 + n_param
;
12432 o_ls
+= 1 + 1 + n_param
;
12433 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12434 o_bmap
+= n_before
;
12435 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12436 ls
->div
->row
[i
] + o_ls
, n_set
);
12439 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12441 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12442 ls
->div
->row
[i
] + o_ls
, n_div
);
12445 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12446 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12450 isl_local_space_free(ls
);
12453 isl_local_space_free(ls
);
12457 /* How many stride constraints does "ma" enforce?
12458 * That is, how many of the affine expressions have a denominator
12459 * different from one?
12461 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12466 for (i
= 0; i
< ma
->n
; ++i
)
12467 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12473 /* For each affine expression in ma of the form
12475 * x_i = (f_i y + h_i)/m_i
12477 * with m_i different from one, add a constraint to "bmap"
12480 * f_i y + h_i = m_i alpha_i
12482 * with alpha_i an additional existentially quantified variable.
12484 * The input variables of "ma" correspond to a subset of the variables
12485 * of "bmap". There are "n_before" variables in "bmap" before this
12486 * subset and "n_after" variables after this subset.
12487 * The integer divisions of the affine expressions in "ma" are assumed
12488 * to have been aligned. There are "n_div_ma" of them and
12489 * they appear first in "bmap", straight after the "n_after" variables.
12491 static __isl_give isl_basic_map
*add_ma_strides(
12492 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12493 int n_before
, int n_after
, int n_div_ma
)
12501 total
= isl_basic_map_total_dim(bmap
);
12502 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12503 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12504 for (i
= 0; i
< ma
->n
; ++i
) {
12505 int o_bmap
= 0, o_ma
= 1;
12507 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12509 div
= isl_basic_map_alloc_div(bmap
);
12510 k
= isl_basic_map_alloc_equality(bmap
);
12511 if (div
< 0 || k
< 0)
12513 isl_int_set_si(bmap
->div
[div
][0], 0);
12514 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12515 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12516 o_bmap
+= 1 + n_param
;
12517 o_ma
+= 1 + n_param
;
12518 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12519 o_bmap
+= n_before
;
12520 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12521 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12524 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12526 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12527 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12528 o_bmap
+= n_div_ma
;
12530 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12531 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12537 isl_basic_map_free(bmap
);
12541 /* Replace the domain or range space (depending on "type) of "space" by "set".
12543 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12544 enum isl_dim_type type
, __isl_take isl_space
*set
)
12546 if (type
== isl_dim_in
) {
12547 space
= isl_space_range(space
);
12548 space
= isl_space_map_from_domain_and_range(set
, space
);
12550 space
= isl_space_domain(space
);
12551 space
= isl_space_map_from_domain_and_range(space
, set
);
12557 /* Compute the preimage of the domain or range (depending on "type")
12558 * of "bmap" under the function represented by "ma".
12559 * In other words, plug in "ma" in the domain or range of "bmap".
12560 * The result is a basic map that lives in the same space as "bmap"
12561 * except that the domain or range has been replaced by
12562 * the domain space of "ma".
12564 * If bmap is represented by
12566 * A(p) + S u + B x + T v + C(divs) >= 0,
12568 * where u and x are input and output dimensions if type == isl_dim_out
12569 * while x and v are input and output dimensions if type == isl_dim_in,
12570 * and ma is represented by
12572 * x = D(p) + F(y) + G(divs')
12574 * then the result is
12576 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12578 * The divs in the input set are similarly adjusted.
12581 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12585 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12586 * B_i G(divs') + c_i(divs))/n_i)
12588 * If bmap is not a rational map and if F(y) involves any denominators
12590 * x_i = (f_i y + h_i)/m_i
12592 * then additional constraints are added to ensure that we only
12593 * map back integer points. That is we enforce
12595 * f_i y + h_i = m_i alpha_i
12597 * with alpha_i an additional existentially quantified variable.
12599 * We first copy over the divs from "ma".
12600 * Then we add the modified constraints and divs from "bmap".
12601 * Finally, we add the stride constraints, if needed.
12603 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12604 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12605 __isl_take isl_multi_aff
*ma
)
12609 isl_basic_map
*res
= NULL
;
12610 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12611 isl_int f
, c1
, c2
, g
;
12612 int rational
, strides
;
12619 ma
= isl_multi_aff_align_divs(ma
);
12622 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12625 if (type
== isl_dim_in
) {
12627 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12629 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12632 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12633 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12635 space
= isl_multi_aff_get_domain_space(ma
);
12636 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12637 rational
= isl_basic_map_is_rational(bmap
);
12638 strides
= rational
? 0 : multi_aff_strides(ma
);
12639 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12640 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12642 res
= isl_basic_map_set_rational(res
);
12644 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12645 if (isl_basic_map_alloc_div(res
) < 0)
12648 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12651 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12652 k
= isl_basic_map_alloc_equality(res
);
12655 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12656 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12659 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12660 k
= isl_basic_map_alloc_inequality(res
);
12663 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12664 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12667 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12668 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12669 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12672 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12673 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12678 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12684 isl_basic_map_free(bmap
);
12685 isl_multi_aff_free(ma
);
12686 res
= isl_basic_set_simplify(res
);
12687 return isl_basic_map_finalize(res
);
12693 isl_basic_map_free(bmap
);
12694 isl_multi_aff_free(ma
);
12695 isl_basic_map_free(res
);
12699 /* Compute the preimage of "bset" under the function represented by "ma".
12700 * In other words, plug in "ma" in "bset". The result is a basic set
12701 * that lives in the domain space of "ma".
12703 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12704 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12706 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12709 /* Compute the preimage of the domain of "bmap" under the function
12710 * represented by "ma".
12711 * In other words, plug in "ma" in the domain of "bmap".
12712 * The result is a basic map that lives in the same space as "bmap"
12713 * except that the domain has been replaced by the domain space of "ma".
12715 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12716 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12718 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12721 /* Compute the preimage of the range of "bmap" under the function
12722 * represented by "ma".
12723 * In other words, plug in "ma" in the range of "bmap".
12724 * The result is a basic map that lives in the same space as "bmap"
12725 * except that the range has been replaced by the domain space of "ma".
12727 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12728 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12730 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12733 /* Check if the range of "ma" is compatible with the domain or range
12734 * (depending on "type") of "map".
12735 * Return -1 if anything is wrong.
12737 static int check_map_compatible_range_multi_aff(
12738 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12739 __isl_keep isl_multi_aff
*ma
)
12742 isl_space
*ma_space
;
12744 ma_space
= isl_multi_aff_get_space(ma
);
12745 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12746 isl_space_free(ma_space
);
12748 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12749 "spaces don't match", return -1);
12753 /* Compute the preimage of the domain or range (depending on "type")
12754 * of "map" under the function represented by "ma".
12755 * In other words, plug in "ma" in the domain or range of "map".
12756 * The result is a map that lives in the same space as "map"
12757 * except that the domain or range has been replaced by
12758 * the domain space of "ma".
12760 * The parameters are assumed to have been aligned.
12762 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12763 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12768 map
= isl_map_cow(map
);
12769 ma
= isl_multi_aff_align_divs(ma
);
12772 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12775 for (i
= 0; i
< map
->n
; ++i
) {
12776 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12777 isl_multi_aff_copy(ma
));
12782 space
= isl_multi_aff_get_domain_space(ma
);
12783 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12785 isl_space_free(map
->dim
);
12790 isl_multi_aff_free(ma
);
12792 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12793 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12796 isl_multi_aff_free(ma
);
12801 /* Compute the preimage of the domain or range (depending on "type")
12802 * of "map" under the function represented by "ma".
12803 * In other words, plug in "ma" in the domain or range of "map".
12804 * The result is a map that lives in the same space as "map"
12805 * except that the domain or range has been replaced by
12806 * the domain space of "ma".
12808 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12809 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12814 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12815 return map_preimage_multi_aff(map
, type
, ma
);
12817 if (!isl_space_has_named_params(map
->dim
) ||
12818 !isl_space_has_named_params(ma
->space
))
12819 isl_die(map
->ctx
, isl_error_invalid
,
12820 "unaligned unnamed parameters", goto error
);
12821 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12822 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12824 return map_preimage_multi_aff(map
, type
, ma
);
12826 isl_multi_aff_free(ma
);
12827 return isl_map_free(map
);
12830 /* Compute the preimage of "set" under the function represented by "ma".
12831 * In other words, plug in "ma" in "set". The result is a set
12832 * that lives in the domain space of "ma".
12834 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12835 __isl_take isl_multi_aff
*ma
)
12837 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12840 /* Compute the preimage of the domain of "map" under the function
12841 * represented by "ma".
12842 * In other words, plug in "ma" in the domain of "map".
12843 * The result is a map that lives in the same space as "map"
12844 * except that the domain has been replaced by the domain space of "ma".
12846 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12847 __isl_take isl_multi_aff
*ma
)
12849 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12852 /* Compute the preimage of the range of "map" under the function
12853 * represented by "ma".
12854 * In other words, plug in "ma" in the range of "map".
12855 * The result is a map that lives in the same space as "map"
12856 * except that the range has been replaced by the domain space of "ma".
12858 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12859 __isl_take isl_multi_aff
*ma
)
12861 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12864 /* Compute the preimage of "map" under the function represented by "pma".
12865 * In other words, plug in "pma" in the domain or range of "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that the space of type "type" has been replaced by
12868 * the domain space of "pma".
12870 * The parameters of "map" and "pma" are assumed to have been aligned.
12872 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12873 __isl_take isl_map
*map
, enum isl_dim_type type
,
12874 __isl_take isl_pw_multi_aff
*pma
)
12883 isl_pw_multi_aff_free(pma
);
12884 res
= isl_map_empty(isl_map_get_space(map
));
12889 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12890 isl_multi_aff_copy(pma
->p
[0].maff
));
12891 if (type
== isl_dim_in
)
12892 res
= isl_map_intersect_domain(res
,
12893 isl_map_copy(pma
->p
[0].set
));
12895 res
= isl_map_intersect_range(res
,
12896 isl_map_copy(pma
->p
[0].set
));
12898 for (i
= 1; i
< pma
->n
; ++i
) {
12901 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12902 isl_multi_aff_copy(pma
->p
[i
].maff
));
12903 if (type
== isl_dim_in
)
12904 res_i
= isl_map_intersect_domain(res_i
,
12905 isl_map_copy(pma
->p
[i
].set
));
12907 res_i
= isl_map_intersect_range(res_i
,
12908 isl_map_copy(pma
->p
[i
].set
));
12909 res
= isl_map_union(res
, res_i
);
12912 isl_pw_multi_aff_free(pma
);
12916 isl_pw_multi_aff_free(pma
);
12921 /* Compute the preimage of "map" under the function represented by "pma".
12922 * In other words, plug in "pma" in the domain or range of "map".
12923 * The result is a map that lives in the same space as "map",
12924 * except that the space of type "type" has been replaced by
12925 * the domain space of "pma".
12927 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12928 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12933 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12934 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12936 if (!isl_space_has_named_params(map
->dim
) ||
12937 !isl_space_has_named_params(pma
->dim
))
12938 isl_die(map
->ctx
, isl_error_invalid
,
12939 "unaligned unnamed parameters", goto error
);
12940 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12941 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12943 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12945 isl_pw_multi_aff_free(pma
);
12946 return isl_map_free(map
);
12949 /* Compute the preimage of "set" under the function represented by "pma".
12950 * In other words, plug in "pma" in "set". The result is a set
12951 * that lives in the domain space of "pma".
12953 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12954 __isl_take isl_pw_multi_aff
*pma
)
12956 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12959 /* Compute the preimage of the domain of "map" under the function
12960 * represented by "pma".
12961 * In other words, plug in "pma" in the domain of "map".
12962 * The result is a map that lives in the same space as "map",
12963 * except that domain space has been replaced by the domain space of "pma".
12965 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12966 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12968 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12971 /* Compute the preimage of the range of "map" under the function
12972 * represented by "pma".
12973 * In other words, plug in "pma" in the range of "map".
12974 * The result is a map that lives in the same space as "map",
12975 * except that range space has been replaced by the domain space of "pma".
12977 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12978 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12980 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12983 /* Compute the preimage of "map" under the function represented by "mpa".
12984 * In other words, plug in "mpa" in the domain or range of "map".
12985 * The result is a map that lives in the same space as "map",
12986 * except that the space of type "type" has been replaced by
12987 * the domain space of "mpa".
12989 * If the map does not involve any constraints that refer to the
12990 * dimensions of the substituted space, then the only possible
12991 * effect of "mpa" on the map is to map the space to a different space.
12992 * We create a separate isl_multi_aff to effectuate this change
12993 * in order to avoid spurious splitting of the map along the pieces
12996 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12997 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13000 isl_pw_multi_aff
*pma
;
13005 n
= isl_map_dim(map
, type
);
13006 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13010 space
= isl_multi_pw_aff_get_space(mpa
);
13011 isl_multi_pw_aff_free(mpa
);
13012 ma
= isl_multi_aff_zero(space
);
13013 return isl_map_preimage_multi_aff(map
, type
, ma
);
13016 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13017 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13020 isl_multi_pw_aff_free(mpa
);
13024 /* Compute the preimage of "map" under the function represented by "mpa".
13025 * In other words, plug in "mpa" in the domain "map".
13026 * The result is a map that lives in the same space as "map",
13027 * except that domain space has been replaced by the domain space of "mpa".
13029 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13030 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13032 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13035 /* Compute the preimage of "set" by the function represented by "mpa".
13036 * In other words, plug in "mpa" in "set".
13038 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13039 __isl_take isl_multi_pw_aff
*mpa
)
13041 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);