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 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
204 struct isl_basic_set
*bset
)
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-(n-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 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1432 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1437 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1439 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1442 /* Copy constraint from src to dst, putting the vars of src at offset
1443 * dim_off in dst and the divs of src at offset div_off in dst.
1444 * If both sets are actually map, then dim_off applies to the input
1447 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1448 struct isl_basic_map
*src_map
, isl_int
*src
,
1449 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1451 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1452 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1453 unsigned src_in
= isl_basic_map_n_in(src_map
);
1454 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1455 unsigned src_out
= isl_basic_map_n_out(src_map
);
1456 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1457 isl_int_set(dst
[0], src
[0]);
1458 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1459 if (dst_nparam
> src_nparam
)
1460 isl_seq_clr(dst
+1+src_nparam
,
1461 dst_nparam
- src_nparam
);
1462 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1463 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1465 isl_min(dst_in
-in_off
, src_in
));
1466 if (dst_in
-in_off
> src_in
)
1467 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1468 dst_in
- in_off
- src_in
);
1469 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1470 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1471 src
+1+src_nparam
+src_in
,
1472 isl_min(dst_out
-out_off
, src_out
));
1473 if (dst_out
-out_off
> src_out
)
1474 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1475 dst_out
- out_off
- src_out
);
1476 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1477 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1478 src
+1+src_nparam
+src_in
+src_out
,
1479 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1480 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1481 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1482 div_off
+src_map
->n_div
,
1483 dst_map
->n_div
- div_off
- src_map
->n_div
);
1486 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1487 struct isl_basic_map
*src_map
, isl_int
*src
,
1488 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1490 isl_int_set(dst
[0], src
[0]);
1491 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1494 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1495 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1500 if (!bmap1
|| !bmap2
)
1503 div_off
= bmap1
->n_div
;
1505 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1506 int i1
= isl_basic_map_alloc_equality(bmap1
);
1509 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1510 i_pos
, o_pos
, div_off
);
1513 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1514 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1517 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1518 i_pos
, o_pos
, div_off
);
1521 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1522 int i1
= isl_basic_map_alloc_div(bmap1
);
1525 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1526 i_pos
, o_pos
, div_off
);
1529 isl_basic_map_free(bmap2
);
1534 isl_basic_map_free(bmap1
);
1535 isl_basic_map_free(bmap2
);
1539 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1540 struct isl_basic_set
*bset2
, unsigned pos
)
1542 return (struct isl_basic_set
*)
1543 add_constraints((struct isl_basic_map
*)bset1
,
1544 (struct isl_basic_map
*)bset2
, 0, pos
);
1547 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1548 __isl_take isl_space
*dim
, unsigned extra
,
1549 unsigned n_eq
, unsigned n_ineq
)
1551 struct isl_basic_map
*ext
;
1561 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1562 base
->extra
>= base
->n_div
+ extra
;
1564 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1565 room_for_ineq(base
, n_ineq
)) {
1566 isl_space_free(dim
);
1570 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1571 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1572 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1573 extra
+= base
->extra
;
1575 n_ineq
+= base
->n_ineq
;
1577 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1583 ext
->sample
= isl_vec_copy(base
->sample
);
1584 flags
= base
->flags
;
1585 ext
= add_constraints(ext
, base
, 0, 0);
1588 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1594 isl_space_free(dim
);
1595 isl_basic_map_free(base
);
1599 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1600 __isl_take isl_space
*dim
, unsigned extra
,
1601 unsigned n_eq
, unsigned n_ineq
)
1603 return (struct isl_basic_set
*)
1604 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1605 extra
, n_eq
, n_ineq
);
1608 struct isl_basic_map
*isl_basic_map_extend_constraints(
1609 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1613 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1617 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1618 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1619 unsigned n_eq
, unsigned n_ineq
)
1621 struct isl_basic_map
*bmap
;
1626 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1630 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1633 isl_basic_map_free(base
);
1637 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1638 unsigned nparam
, unsigned dim
, unsigned extra
,
1639 unsigned n_eq
, unsigned n_ineq
)
1641 return (struct isl_basic_set
*)
1642 isl_basic_map_extend((struct isl_basic_map
*)base
,
1643 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1646 struct isl_basic_set
*isl_basic_set_extend_constraints(
1647 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1649 return (struct isl_basic_set
*)
1650 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1654 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1656 return (struct isl_basic_set
*)
1657 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1660 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1665 if (bmap
->ref
> 1) {
1667 bmap
= isl_basic_map_dup(bmap
);
1670 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1671 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1676 /* Clear all cached information in "map", either because it is about
1677 * to be modified or because it is being freed.
1678 * Always return the same pointer that is passed in.
1679 * This is needed for the use in isl_map_free.
1681 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1683 isl_basic_map_free(map
->cached_simple_hull
[0]);
1684 isl_basic_map_free(map
->cached_simple_hull
[1]);
1685 map
->cached_simple_hull
[0] = NULL
;
1686 map
->cached_simple_hull
[1] = NULL
;
1690 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1692 return isl_map_cow(set
);
1695 /* Return an isl_map that is equal to "map" and that has only
1696 * a single reference.
1698 * If the original input already has only one reference, then
1699 * simply return it, but clear all cached information, since
1700 * it may be rendered invalid by the operations that will be
1701 * performed on the result.
1703 * Otherwise, create a duplicate (without any cached information).
1705 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1711 return clear_caches(map
);
1713 return isl_map_dup(map
);
1716 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1717 unsigned a_len
, unsigned b_len
)
1719 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1720 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1721 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1724 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1725 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1733 isl_assert(bmap
->ctx
,
1734 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1736 if (n1
== 0 || n2
== 0)
1739 bmap
= isl_basic_map_cow(bmap
);
1743 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1744 if (isl_blk_is_error(blk
))
1747 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1749 bmap
->eq
[i
] + pos
, n1
, n2
);
1751 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1753 bmap
->ineq
[i
] + pos
, n1
, n2
);
1755 for (i
= 0; i
< bmap
->n_div
; ++i
)
1757 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1759 isl_blk_free(bmap
->ctx
, blk
);
1761 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1762 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1763 return isl_basic_map_finalize(bmap
);
1765 isl_basic_map_free(bmap
);
1769 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1775 total
= isl_basic_map_total_dim(bmap
);
1776 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1777 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1779 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1781 i
= isl_basic_map_alloc_equality(bmap
);
1785 isl_int_set_si(bmap
->eq
[i
][0], 1);
1786 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1787 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1788 isl_vec_free(bmap
->sample
);
1789 bmap
->sample
= NULL
;
1790 return isl_basic_map_finalize(bmap
);
1792 isl_basic_map_free(bmap
);
1796 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1798 return (struct isl_basic_set
*)
1799 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1802 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1805 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1807 isl_int
*t
= bmap
->div
[a
];
1808 bmap
->div
[a
] = bmap
->div
[b
];
1812 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1813 * div definitions accordingly.
1815 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1818 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1820 swap_div(bmap
, a
, b
);
1822 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1823 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1825 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1826 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1828 for (i
= 0; i
< bmap
->n_div
; ++i
)
1829 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1830 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1833 /* Eliminate the specified n dimensions starting at first from the
1834 * constraints, without removing the dimensions from the space.
1835 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1837 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1838 enum isl_dim_type type
, unsigned first
, unsigned n
)
1847 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1848 isl_die(map
->ctx
, isl_error_invalid
,
1849 "index out of bounds", goto error
);
1851 map
= isl_map_cow(map
);
1855 for (i
= 0; i
< map
->n
; ++i
) {
1856 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1866 /* Eliminate the specified n dimensions starting at first from the
1867 * constraints, without removing the dimensions from the space.
1868 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1870 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1871 enum isl_dim_type type
, unsigned first
, unsigned n
)
1873 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1876 /* Eliminate the specified n dimensions starting at first from the
1877 * constraints, without removing the dimensions from the space.
1878 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1880 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1881 unsigned first
, unsigned n
)
1883 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1886 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1887 __isl_take isl_basic_map
*bmap
)
1891 bmap
= isl_basic_map_eliminate_vars(bmap
,
1892 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1896 return isl_basic_map_finalize(bmap
);
1899 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1900 __isl_take isl_basic_set
*bset
)
1902 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1903 (struct isl_basic_map
*)bset
);
1906 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1915 map
= isl_map_cow(map
);
1919 for (i
= 0; i
< map
->n
; ++i
) {
1920 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1930 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1932 return isl_map_remove_divs(set
);
1935 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1936 enum isl_dim_type type
, unsigned first
, unsigned n
)
1940 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1942 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1944 bmap
= isl_basic_map_eliminate_vars(bmap
,
1945 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1948 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1950 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1953 isl_basic_map_free(bmap
);
1957 /* Return true if the definition of the given div (recursively) involves
1958 * any of the given variables.
1960 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1961 unsigned first
, unsigned n
)
1964 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1966 if (isl_int_is_zero(bmap
->div
[div
][0]))
1968 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1971 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1972 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1974 if (div_involves_vars(bmap
, i
, first
, n
))
1981 /* Try and add a lower and/or upper bound on "div" to "bmap"
1982 * based on inequality "i".
1983 * "total" is the total number of variables (excluding the divs).
1984 * "v" is a temporary object that can be used during the calculations.
1985 * If "lb" is set, then a lower bound should be constructed.
1986 * If "ub" is set, then an upper bound should be constructed.
1988 * The calling function has already checked that the inequality does not
1989 * reference "div", but we still need to check that the inequality is
1990 * of the right form. We'll consider the case where we want to construct
1991 * a lower bound. The construction of upper bounds is similar.
1993 * Let "div" be of the form
1995 * q = floor((a + f(x))/d)
1997 * We essentially check if constraint "i" is of the form
2001 * so that we can use it to derive a lower bound on "div".
2002 * However, we allow a slightly more general form
2006 * with the condition that the coefficients of g(x) - f(x) are all
2008 * Rewriting this constraint as
2012 * adding a + f(x) to both sides and dividing by d, we obtain
2014 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2016 * Taking the floor on both sides, we obtain
2018 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2022 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2024 * In the case of an upper bound, we construct the constraint
2026 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2029 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2030 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2031 unsigned total
, isl_int v
, int lb
, int ub
)
2035 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2037 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2038 bmap
->div
[div
][1 + 1 + j
]);
2039 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2042 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2043 bmap
->div
[div
][1 + 1 + j
]);
2044 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2050 bmap
= isl_basic_map_cow(bmap
);
2051 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2053 int k
= isl_basic_map_alloc_inequality(bmap
);
2056 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2057 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2058 bmap
->div
[div
][1 + j
]);
2059 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2060 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2062 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2065 int k
= isl_basic_map_alloc_inequality(bmap
);
2068 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2069 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2070 bmap
->div
[div
][1 + j
]);
2071 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2072 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2074 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2077 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2080 isl_basic_map_free(bmap
);
2084 /* This function is called right before "div" is eliminated from "bmap"
2085 * using Fourier-Motzkin.
2086 * Look through the constraints of "bmap" for constraints on the argument
2087 * of the integer division and use them to construct constraints on the
2088 * integer division itself. These constraints can then be combined
2089 * during the Fourier-Motzkin elimination.
2090 * Note that it is only useful to introduce lower bounds on "div"
2091 * if "bmap" already contains upper bounds on "div" as the newly
2092 * introduce lower bounds can then be combined with the pre-existing
2093 * upper bounds. Similarly for upper bounds.
2094 * We therefore first check if "bmap" contains any lower and/or upper bounds
2097 * It is interesting to note that the introduction of these constraints
2098 * can indeed lead to more accurate results, even when compared to
2099 * deriving constraints on the argument of "div" from constraints on "div".
2100 * Consider, for example, the set
2102 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2104 * The second constraint can be rewritten as
2106 * 2 * [(-i-2j+3)/4] + k >= 0
2108 * from which we can derive
2110 * -i - 2j + 3 >= -2k
2116 * Combined with the first constraint, we obtain
2118 * -3 <= 3 + 2k or k >= -3
2120 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2121 * the first constraint, we obtain
2123 * [(i + 2j)/4] >= [-3/4] = -1
2125 * Combining this constraint with the second constraint, we obtain
2129 static __isl_give isl_basic_map
*insert_bounds_on_div(
2130 __isl_take isl_basic_map
*bmap
, int div
)
2133 int check_lb
, check_ub
;
2140 if (isl_int_is_zero(bmap
->div
[div
][0]))
2143 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2147 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2148 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2155 if (!check_lb
&& !check_ub
)
2160 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2161 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2164 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2165 check_lb
, check_ub
);
2173 /* Remove all divs (recursively) involving any of the given dimensions
2174 * in their definitions.
2176 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2177 __isl_take isl_basic_map
*bmap
,
2178 enum isl_dim_type type
, unsigned first
, unsigned n
)
2184 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2186 first
+= isl_basic_map_offset(bmap
, type
);
2188 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2189 if (!div_involves_vars(bmap
, i
, first
, n
))
2191 bmap
= insert_bounds_on_div(bmap
, i
);
2192 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2200 isl_basic_map_free(bmap
);
2204 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2205 __isl_take isl_basic_set
*bset
,
2206 enum isl_dim_type type
, unsigned first
, unsigned n
)
2208 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2211 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2212 enum isl_dim_type type
, unsigned first
, unsigned n
)
2221 map
= isl_map_cow(map
);
2225 for (i
= 0; i
< map
->n
; ++i
) {
2226 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2237 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2238 enum isl_dim_type type
, unsigned first
, unsigned n
)
2240 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2244 /* Does the desciption of "bmap" depend on the specified dimensions?
2245 * We also check whether the dimensions appear in any of the div definitions.
2246 * In principle there is no need for this check. If the dimensions appear
2247 * in a div definition, they also appear in the defining constraints of that
2250 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2251 enum isl_dim_type type
, unsigned first
, unsigned n
)
2256 return isl_bool_error
;
2258 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2259 isl_die(bmap
->ctx
, isl_error_invalid
,
2260 "index out of bounds", return isl_bool_error
);
2262 first
+= isl_basic_map_offset(bmap
, type
);
2263 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2264 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2265 return isl_bool_true
;
2266 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2267 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2268 return isl_bool_true
;
2269 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2270 if (isl_int_is_zero(bmap
->div
[i
][0]))
2272 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2273 return isl_bool_true
;
2276 return isl_bool_false
;
2279 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2280 enum isl_dim_type type
, unsigned first
, unsigned n
)
2285 return isl_bool_error
;
2287 if (first
+ n
> isl_map_dim(map
, type
))
2288 isl_die(map
->ctx
, isl_error_invalid
,
2289 "index out of bounds", return isl_bool_error
);
2291 for (i
= 0; i
< map
->n
; ++i
) {
2292 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2294 if (involves
< 0 || involves
)
2298 return isl_bool_false
;
2301 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2302 enum isl_dim_type type
, unsigned first
, unsigned n
)
2304 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2307 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2308 enum isl_dim_type type
, unsigned first
, unsigned n
)
2310 return isl_map_involves_dims(set
, type
, first
, n
);
2313 /* Return true if the definition of the given div is unknown or depends
2316 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2319 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2321 if (isl_int_is_zero(bmap
->div
[div
][0]))
2324 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2325 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2327 if (div_is_unknown(bmap
, i
))
2334 /* Remove all divs that are unknown or defined in terms of unknown divs.
2336 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2337 __isl_take isl_basic_map
*bmap
)
2344 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2345 if (!div_is_unknown(bmap
, i
))
2347 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2356 /* Remove all divs that are unknown or defined in terms of unknown divs.
2358 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2359 __isl_take isl_basic_set
*bset
)
2361 return isl_basic_map_remove_unknown_divs(bset
);
2364 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2373 map
= isl_map_cow(map
);
2377 for (i
= 0; i
< map
->n
; ++i
) {
2378 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2388 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2390 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2393 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2394 __isl_take isl_basic_set
*bset
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 return (isl_basic_set
*)
2398 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2401 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2402 enum isl_dim_type type
, unsigned first
, unsigned n
)
2409 map
= isl_map_cow(map
);
2412 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2414 for (i
= 0; i
< map
->n
; ++i
) {
2415 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2416 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2420 map
= isl_map_drop(map
, type
, first
, n
);
2427 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2428 enum isl_dim_type type
, unsigned first
, unsigned n
)
2430 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2433 /* Project out n inputs starting at first using Fourier-Motzkin */
2434 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2435 unsigned first
, unsigned n
)
2437 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2440 static void dump_term(struct isl_basic_map
*bmap
,
2441 isl_int c
, int pos
, FILE *out
)
2444 unsigned in
= isl_basic_map_n_in(bmap
);
2445 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2446 unsigned nparam
= isl_basic_map_n_param(bmap
);
2448 isl_int_print(out
, c
, 0);
2450 if (!isl_int_is_one(c
))
2451 isl_int_print(out
, c
, 0);
2452 if (pos
< 1 + nparam
) {
2453 name
= isl_space_get_dim_name(bmap
->dim
,
2454 isl_dim_param
, pos
- 1);
2456 fprintf(out
, "%s", name
);
2458 fprintf(out
, "p%d", pos
- 1);
2459 } else if (pos
< 1 + nparam
+ in
)
2460 fprintf(out
, "i%d", pos
- 1 - nparam
);
2461 else if (pos
< 1 + nparam
+ dim
)
2462 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2464 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2468 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2469 int sign
, FILE *out
)
2473 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2477 for (i
= 0, first
= 1; i
< len
; ++i
) {
2478 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2481 fprintf(out
, " + ");
2483 isl_int_abs(v
, c
[i
]);
2484 dump_term(bmap
, v
, i
, out
);
2491 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2492 const char *op
, FILE *out
, int indent
)
2496 fprintf(out
, "%*s", indent
, "");
2498 dump_constraint_sign(bmap
, c
, 1, out
);
2499 fprintf(out
, " %s ", op
);
2500 dump_constraint_sign(bmap
, c
, -1, out
);
2504 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2505 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2507 fprintf(out
, "%*s", indent
, "");
2508 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2513 static void dump_constraints(struct isl_basic_map
*bmap
,
2514 isl_int
**c
, unsigned n
,
2515 const char *op
, FILE *out
, int indent
)
2519 for (i
= 0; i
< n
; ++i
)
2520 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2523 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2527 unsigned total
= isl_basic_map_total_dim(bmap
);
2529 for (j
= 0; j
< 1 + total
; ++j
) {
2530 if (isl_int_is_zero(exp
[j
]))
2532 if (!first
&& isl_int_is_pos(exp
[j
]))
2534 dump_term(bmap
, exp
[j
], j
, out
);
2539 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2543 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2544 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2546 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2547 fprintf(out
, "%*s", indent
, "");
2548 fprintf(out
, "e%d = [(", i
);
2549 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2551 isl_int_print(out
, bmap
->div
[i
][0], 0);
2552 fprintf(out
, "]\n");
2556 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2557 FILE *out
, int indent
)
2560 fprintf(out
, "null basic set\n");
2564 fprintf(out
, "%*s", indent
, "");
2565 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2566 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2567 bset
->extra
, bset
->flags
);
2568 dump((struct isl_basic_map
*)bset
, out
, indent
);
2571 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2572 FILE *out
, int indent
)
2575 fprintf(out
, "null basic map\n");
2579 fprintf(out
, "%*s", indent
, "");
2580 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2581 "flags: %x, n_name: %d\n",
2583 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2584 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2585 dump(bmap
, out
, indent
);
2588 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2593 total
= isl_basic_map_total_dim(bmap
);
2594 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2595 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2596 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2597 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2601 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2606 if (isl_space_dim(space
, isl_dim_in
) != 0)
2607 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2608 "set cannot have input dimensions", goto error
);
2609 return isl_map_alloc_space(space
, n
, flags
);
2611 isl_space_free(space
);
2615 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2616 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2618 struct isl_set
*set
;
2621 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2625 set
= isl_set_alloc_space(dims
, n
, flags
);
2629 /* Make sure "map" has room for at least "n" more basic maps.
2631 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2634 struct isl_map
*grown
= NULL
;
2638 isl_assert(map
->ctx
, n
>= 0, goto error
);
2639 if (map
->n
+ n
<= map
->size
)
2641 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2644 for (i
= 0; i
< map
->n
; ++i
) {
2645 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2653 isl_map_free(grown
);
2658 /* Make sure "set" has room for at least "n" more basic sets.
2660 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2662 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2665 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2668 struct isl_set
*dup
;
2673 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2676 for (i
= 0; i
< set
->n
; ++i
)
2677 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2681 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2683 return isl_map_from_basic_map(bset
);
2686 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2688 struct isl_map
*map
;
2693 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2694 return isl_map_add_basic_map(map
, bmap
);
2697 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2698 __isl_take isl_basic_set
*bset
)
2700 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2701 (struct isl_basic_map
*)bset
);
2704 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2706 return isl_map_free(set
);
2709 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2714 fprintf(out
, "null set\n");
2718 fprintf(out
, "%*s", indent
, "");
2719 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2720 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2722 for (i
= 0; i
< set
->n
; ++i
) {
2723 fprintf(out
, "%*s", indent
, "");
2724 fprintf(out
, "basic set %d:\n", i
);
2725 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2729 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2734 fprintf(out
, "null map\n");
2738 fprintf(out
, "%*s", indent
, "");
2739 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2740 "flags: %x, n_name: %d\n",
2741 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2742 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2743 for (i
= 0; i
< map
->n
; ++i
) {
2744 fprintf(out
, "%*s", indent
, "");
2745 fprintf(out
, "basic map %d:\n", i
);
2746 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2750 struct isl_basic_map
*isl_basic_map_intersect_domain(
2751 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2753 struct isl_basic_map
*bmap_domain
;
2758 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2759 bset
->dim
, isl_dim_param
), goto error
);
2761 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2762 isl_assert(bset
->ctx
,
2763 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2765 bmap
= isl_basic_map_cow(bmap
);
2768 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2769 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2770 bmap_domain
= isl_basic_map_from_domain(bset
);
2771 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2773 bmap
= isl_basic_map_simplify(bmap
);
2774 return isl_basic_map_finalize(bmap
);
2776 isl_basic_map_free(bmap
);
2777 isl_basic_set_free(bset
);
2781 struct isl_basic_map
*isl_basic_map_intersect_range(
2782 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2784 struct isl_basic_map
*bmap_range
;
2789 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2790 bset
->dim
, isl_dim_param
), goto error
);
2792 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2793 isl_assert(bset
->ctx
,
2794 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2796 if (isl_basic_set_plain_is_universe(bset
)) {
2797 isl_basic_set_free(bset
);
2801 bmap
= isl_basic_map_cow(bmap
);
2804 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2805 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2806 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2807 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2809 bmap
= isl_basic_map_simplify(bmap
);
2810 return isl_basic_map_finalize(bmap
);
2812 isl_basic_map_free(bmap
);
2813 isl_basic_set_free(bset
);
2817 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2818 __isl_keep isl_vec
*vec
)
2825 return isl_bool_error
;
2827 total
= 1 + isl_basic_map_total_dim(bmap
);
2828 if (total
!= vec
->size
)
2829 return isl_bool_error
;
2833 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2834 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2835 if (!isl_int_is_zero(s
)) {
2837 return isl_bool_false
;
2841 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2842 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2843 if (isl_int_is_neg(s
)) {
2845 return isl_bool_false
;
2851 return isl_bool_true
;
2854 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2855 __isl_keep isl_vec
*vec
)
2857 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2860 struct isl_basic_map
*isl_basic_map_intersect(
2861 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2863 struct isl_vec
*sample
= NULL
;
2865 if (!bmap1
|| !bmap2
)
2868 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2869 bmap2
->dim
, isl_dim_param
), goto error
);
2870 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2871 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2872 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2873 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2874 return isl_basic_map_intersect(bmap2
, bmap1
);
2876 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2877 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2878 isl_assert(bmap1
->ctx
,
2879 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2881 if (isl_basic_map_plain_is_empty(bmap1
)) {
2882 isl_basic_map_free(bmap2
);
2885 if (isl_basic_map_plain_is_empty(bmap2
)) {
2886 isl_basic_map_free(bmap1
);
2890 if (bmap1
->sample
&&
2891 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2892 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2893 sample
= isl_vec_copy(bmap1
->sample
);
2894 else if (bmap2
->sample
&&
2895 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2896 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2897 sample
= isl_vec_copy(bmap2
->sample
);
2899 bmap1
= isl_basic_map_cow(bmap1
);
2902 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2903 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2904 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2907 isl_vec_free(sample
);
2909 isl_vec_free(bmap1
->sample
);
2910 bmap1
->sample
= sample
;
2913 bmap1
= isl_basic_map_simplify(bmap1
);
2914 return isl_basic_map_finalize(bmap1
);
2917 isl_vec_free(sample
);
2918 isl_basic_map_free(bmap1
);
2919 isl_basic_map_free(bmap2
);
2923 struct isl_basic_set
*isl_basic_set_intersect(
2924 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2926 return (struct isl_basic_set
*)
2927 isl_basic_map_intersect(
2928 (struct isl_basic_map
*)bset1
,
2929 (struct isl_basic_map
*)bset2
);
2932 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2933 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2935 return isl_basic_set_intersect(bset1
, bset2
);
2938 /* Special case of isl_map_intersect, where both map1 and map2
2939 * are convex, without any divs and such that either map1 or map2
2940 * contains a single constraint. This constraint is then simply
2941 * added to the other map.
2943 static __isl_give isl_map
*map_intersect_add_constraint(
2944 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2946 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2947 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2948 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2949 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2951 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2952 return isl_map_intersect(map2
, map1
);
2954 isl_assert(map2
->ctx
,
2955 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2957 map1
= isl_map_cow(map1
);
2960 if (isl_map_plain_is_empty(map1
)) {
2964 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2965 if (map2
->p
[0]->n_eq
== 1)
2966 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2968 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2969 map2
->p
[0]->ineq
[0]);
2971 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2972 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2976 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2977 isl_basic_map_free(map1
->p
[0]);
2990 /* map2 may be either a parameter domain or a map living in the same
2993 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2994 __isl_take isl_map
*map2
)
3003 if ((isl_map_plain_is_empty(map1
) ||
3004 isl_map_plain_is_universe(map2
)) &&
3005 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3009 if ((isl_map_plain_is_empty(map2
) ||
3010 isl_map_plain_is_universe(map1
)) &&
3011 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3016 if (map1
->n
== 1 && map2
->n
== 1 &&
3017 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3018 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3019 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3020 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3021 return map_intersect_add_constraint(map1
, map2
);
3023 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3024 isl_space_dim(map2
->dim
, isl_dim_param
))
3025 isl_assert(map1
->ctx
,
3026 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3028 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3029 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3030 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3032 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3033 map1
->n
* map2
->n
, flags
);
3036 for (i
= 0; i
< map1
->n
; ++i
)
3037 for (j
= 0; j
< map2
->n
; ++j
) {
3038 struct isl_basic_map
*part
;
3039 part
= isl_basic_map_intersect(
3040 isl_basic_map_copy(map1
->p
[i
]),
3041 isl_basic_map_copy(map2
->p
[j
]));
3042 if (isl_basic_map_is_empty(part
) < 0)
3043 part
= isl_basic_map_free(part
);
3044 result
= isl_map_add_basic_map(result
, part
);
3057 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3058 __isl_take isl_map
*map2
)
3062 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3063 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3064 "spaces don't match", goto error
);
3065 return map_intersect_internal(map1
, map2
);
3072 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3073 __isl_take isl_map
*map2
)
3075 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3078 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3080 return (struct isl_set
*)
3081 isl_map_intersect((struct isl_map
*)set1
,
3082 (struct isl_map
*)set2
);
3085 /* map_intersect_internal accepts intersections
3086 * with parameter domains, so we can just call that function.
3088 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3089 __isl_take isl_set
*params
)
3091 return map_intersect_internal(map
, params
);
3094 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3095 __isl_take isl_map
*map2
)
3097 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3100 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3101 __isl_take isl_set
*params
)
3103 return isl_map_intersect_params(set
, params
);
3106 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3109 unsigned pos
, n1
, n2
;
3113 bmap
= isl_basic_map_cow(bmap
);
3116 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3117 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3118 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3119 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3120 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3121 return isl_basic_map_reset_space(bmap
, space
);
3124 static __isl_give isl_basic_map
*basic_map_space_reset(
3125 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3131 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3134 space
= isl_basic_map_get_space(bmap
);
3135 space
= isl_space_reset(space
, type
);
3136 bmap
= isl_basic_map_reset_space(bmap
, space
);
3140 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3141 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3142 unsigned pos
, unsigned n
)
3145 struct isl_basic_map
*res
;
3146 struct isl_dim_map
*dim_map
;
3147 unsigned total
, off
;
3148 enum isl_dim_type t
;
3151 return basic_map_space_reset(bmap
, type
);
3156 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3158 total
= isl_basic_map_total_dim(bmap
) + n
;
3159 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3161 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3163 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3165 unsigned size
= isl_basic_map_dim(bmap
, t
);
3166 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3168 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3169 pos
, size
- pos
, off
+ pos
+ n
);
3171 off
+= isl_space_dim(res_dim
, t
);
3173 isl_dim_map_div(dim_map
, bmap
, off
);
3175 res
= isl_basic_map_alloc_space(res_dim
,
3176 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3177 if (isl_basic_map_is_rational(bmap
))
3178 res
= isl_basic_map_set_rational(res
);
3179 if (isl_basic_map_plain_is_empty(bmap
)) {
3180 isl_basic_map_free(bmap
);
3182 return isl_basic_map_set_to_empty(res
);
3184 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3185 return isl_basic_map_finalize(res
);
3188 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3189 __isl_take isl_basic_set
*bset
,
3190 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3192 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3195 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3196 enum isl_dim_type type
, unsigned n
)
3200 return isl_basic_map_insert_dims(bmap
, type
,
3201 isl_basic_map_dim(bmap
, type
), n
);
3204 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3205 enum isl_dim_type type
, unsigned n
)
3209 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3210 return isl_basic_map_add_dims(bset
, type
, n
);
3212 isl_basic_set_free(bset
);
3216 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3217 enum isl_dim_type type
)
3221 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3224 space
= isl_map_get_space(map
);
3225 space
= isl_space_reset(space
, type
);
3226 map
= isl_map_reset_space(map
, space
);
3230 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3231 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3236 return map_space_reset(map
, type
);
3238 map
= isl_map_cow(map
);
3242 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3246 for (i
= 0; i
< map
->n
; ++i
) {
3247 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3258 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3259 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3261 return isl_map_insert_dims(set
, type
, pos
, n
);
3264 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3265 enum isl_dim_type type
, unsigned n
)
3269 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3272 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3273 enum isl_dim_type type
, unsigned n
)
3277 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3278 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3284 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3285 __isl_take isl_basic_map
*bmap
,
3286 enum isl_dim_type dst_type
, unsigned dst_pos
,
3287 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3289 struct isl_dim_map
*dim_map
;
3290 struct isl_basic_map
*res
;
3291 enum isl_dim_type t
;
3292 unsigned total
, off
;
3299 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3302 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3305 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3307 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3308 pos(bmap
->dim
, src_type
) + src_pos
+
3309 ((src_type
< dst_type
) ? n
: 0)) {
3310 bmap
= isl_basic_map_cow(bmap
);
3314 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3315 src_type
, src_pos
, n
);
3319 bmap
= isl_basic_map_finalize(bmap
);
3324 total
= isl_basic_map_total_dim(bmap
);
3325 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3328 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3329 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3330 if (t
== dst_type
) {
3331 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3334 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3337 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3338 dst_pos
, size
- dst_pos
, off
);
3339 off
+= size
- dst_pos
;
3340 } else if (t
== src_type
) {
3341 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3344 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3345 src_pos
+ n
, size
- src_pos
- n
, off
);
3346 off
+= size
- src_pos
- n
;
3348 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3352 isl_dim_map_div(dim_map
, bmap
, off
);
3354 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3355 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3356 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3360 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3361 src_type
, src_pos
, n
);
3365 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3366 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3367 bmap
= isl_basic_map_finalize(bmap
);
3371 isl_basic_map_free(bmap
);
3375 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3376 enum isl_dim_type dst_type
, unsigned dst_pos
,
3377 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3379 return (isl_basic_set
*)isl_basic_map_move_dims(
3380 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3383 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3384 enum isl_dim_type dst_type
, unsigned dst_pos
,
3385 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3389 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3390 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3391 src_type
, src_pos
, n
);
3397 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3398 enum isl_dim_type dst_type
, unsigned dst_pos
,
3399 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3408 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3411 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3414 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3416 map
= isl_map_cow(map
);
3420 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3424 for (i
= 0; i
< map
->n
; ++i
) {
3425 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3427 src_type
, src_pos
, n
);
3438 /* Move the specified dimensions to the last columns right before
3439 * the divs. Don't change the dimension specification of bmap.
3440 * That's the responsibility of the caller.
3442 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3443 enum isl_dim_type type
, unsigned first
, unsigned n
)
3445 struct isl_dim_map
*dim_map
;
3446 struct isl_basic_map
*res
;
3447 enum isl_dim_type t
;
3448 unsigned total
, off
;
3452 if (pos(bmap
->dim
, type
) + first
+ n
==
3453 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3456 total
= isl_basic_map_total_dim(bmap
);
3457 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3460 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3461 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3463 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3466 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3467 first
, n
, total
- bmap
->n_div
- n
);
3468 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3469 first
+ n
, size
- (first
+ n
), off
);
3470 off
+= size
- (first
+ n
);
3472 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3476 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3478 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3479 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3480 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3484 /* Insert "n" rows in the divs of "bmap".
3486 * The number of columns is not changed, which means that the last
3487 * dimensions of "bmap" are being reintepreted as the new divs.
3488 * The space of "bmap" is not adjusted, however, which means
3489 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3490 * from the space of "bmap" is the responsibility of the caller.
3492 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3500 bmap
= isl_basic_map_cow(bmap
);
3504 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3505 old
= bmap
->block2
.data
;
3506 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3507 (bmap
->extra
+ n
) * (1 + row_size
));
3508 if (!bmap
->block2
.data
)
3509 return isl_basic_map_free(bmap
);
3510 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3512 return isl_basic_map_free(bmap
);
3513 for (i
= 0; i
< n
; ++i
) {
3514 new_div
[i
] = bmap
->block2
.data
+
3515 (bmap
->extra
+ i
) * (1 + row_size
);
3516 isl_seq_clr(new_div
[i
], 1 + row_size
);
3518 for (i
= 0; i
< bmap
->extra
; ++i
)
3519 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3521 bmap
->div
= new_div
;
3528 /* Drop constraints from "bmap" that only involve the variables
3529 * of "type" in the range [first, first + n] that are not related
3530 * to any of the variables outside that interval.
3531 * These constraints cannot influence the values for the variables
3532 * outside the interval, except in case they cause "bmap" to be empty.
3533 * Only drop the constraints if "bmap" is known to be non-empty.
3535 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3536 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3537 unsigned first
, unsigned n
)
3541 unsigned dim
, n_div
;
3544 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3546 return isl_basic_map_free(bmap
);
3550 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3551 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3552 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3554 return isl_basic_map_free(bmap
);
3555 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3556 for (i
= 0; i
< first
; ++i
)
3558 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3561 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3566 /* Turn the n dimensions of type type, starting at first
3567 * into existentially quantified variables.
3569 * If a subset of the projected out variables are unrelated
3570 * to any of the variables that remain, then the constraints
3571 * involving this subset are simply dropped first.
3573 __isl_give isl_basic_map
*isl_basic_map_project_out(
3574 __isl_take isl_basic_map
*bmap
,
3575 enum isl_dim_type type
, unsigned first
, unsigned n
)
3578 return basic_map_space_reset(bmap
, type
);
3579 if (type
== isl_dim_div
)
3580 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3581 "cannot project out existentially quantified variables",
3582 return isl_basic_map_free(bmap
));
3584 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3588 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3589 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3591 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3594 bmap
= move_last(bmap
, type
, first
, n
);
3595 bmap
= isl_basic_map_cow(bmap
);
3596 bmap
= insert_div_rows(bmap
, n
);
3600 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3603 bmap
= isl_basic_map_simplify(bmap
);
3604 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3605 return isl_basic_map_finalize(bmap
);
3607 isl_basic_map_free(bmap
);
3611 /* Turn the n dimensions of type type, starting at first
3612 * into existentially quantified variables.
3614 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3615 enum isl_dim_type type
, unsigned first
, unsigned n
)
3617 return (isl_basic_set
*)isl_basic_map_project_out(
3618 (isl_basic_map
*)bset
, type
, first
, n
);
3621 /* Turn the n dimensions of type type, starting at first
3622 * into existentially quantified variables.
3624 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3625 enum isl_dim_type type
, unsigned first
, unsigned n
)
3633 return map_space_reset(map
, type
);
3635 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3637 map
= isl_map_cow(map
);
3641 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3645 for (i
= 0; i
< map
->n
; ++i
) {
3646 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3657 /* Turn the n dimensions of type type, starting at first
3658 * into existentially quantified variables.
3660 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3661 enum isl_dim_type type
, unsigned first
, unsigned n
)
3663 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3666 /* Return a map that projects the elements in "set" onto their
3667 * "n" set dimensions starting at "first".
3668 * "type" should be equal to isl_dim_set.
3670 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3671 enum isl_dim_type type
, unsigned first
, unsigned n
)
3679 if (type
!= isl_dim_set
)
3680 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3681 "only set dimensions can be projected out", goto error
);
3682 dim
= isl_set_dim(set
, isl_dim_set
);
3683 if (first
+ n
> dim
|| first
+ n
< first
)
3684 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3685 "index out of bounds", goto error
);
3687 map
= isl_map_from_domain(set
);
3688 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3689 for (i
= 0; i
< n
; ++i
)
3690 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3698 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3702 for (i
= 0; i
< n
; ++i
) {
3703 j
= isl_basic_map_alloc_div(bmap
);
3706 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3710 isl_basic_map_free(bmap
);
3714 struct isl_basic_map
*isl_basic_map_apply_range(
3715 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3717 isl_space
*dim_result
= NULL
;
3718 struct isl_basic_map
*bmap
;
3719 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3720 struct isl_dim_map
*dim_map1
, *dim_map2
;
3722 if (!bmap1
|| !bmap2
)
3724 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3725 bmap2
->dim
, isl_dim_param
))
3726 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3727 "parameters don't match", goto error
);
3728 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3729 bmap2
->dim
, isl_dim_in
))
3730 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3731 "spaces don't match", goto error
);
3733 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3734 isl_space_copy(bmap2
->dim
));
3736 n_in
= isl_basic_map_n_in(bmap1
);
3737 n_out
= isl_basic_map_n_out(bmap2
);
3738 n
= isl_basic_map_n_out(bmap1
);
3739 nparam
= isl_basic_map_n_param(bmap1
);
3741 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3742 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3743 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3744 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3745 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3746 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3747 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3748 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3749 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3750 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3751 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3753 bmap
= isl_basic_map_alloc_space(dim_result
,
3754 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3755 bmap1
->n_eq
+ bmap2
->n_eq
,
3756 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3757 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3758 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3759 bmap
= add_divs(bmap
, n
);
3760 bmap
= isl_basic_map_simplify(bmap
);
3761 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3762 return isl_basic_map_finalize(bmap
);
3764 isl_basic_map_free(bmap1
);
3765 isl_basic_map_free(bmap2
);
3769 struct isl_basic_set
*isl_basic_set_apply(
3770 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3775 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3778 return (struct isl_basic_set
*)
3779 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3781 isl_basic_set_free(bset
);
3782 isl_basic_map_free(bmap
);
3786 struct isl_basic_map
*isl_basic_map_apply_domain(
3787 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3789 if (!bmap1
|| !bmap2
)
3792 isl_assert(bmap1
->ctx
,
3793 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3794 isl_assert(bmap1
->ctx
,
3795 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3798 bmap1
= isl_basic_map_reverse(bmap1
);
3799 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3800 return isl_basic_map_reverse(bmap1
);
3802 isl_basic_map_free(bmap1
);
3803 isl_basic_map_free(bmap2
);
3807 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3808 * A \cap B -> f(A) + f(B)
3810 struct isl_basic_map
*isl_basic_map_sum(
3811 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3813 unsigned n_in
, n_out
, nparam
, total
, pos
;
3814 struct isl_basic_map
*bmap
= NULL
;
3815 struct isl_dim_map
*dim_map1
, *dim_map2
;
3818 if (!bmap1
|| !bmap2
)
3821 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3824 nparam
= isl_basic_map_n_param(bmap1
);
3825 n_in
= isl_basic_map_n_in(bmap1
);
3826 n_out
= isl_basic_map_n_out(bmap1
);
3828 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3829 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3830 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3831 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3832 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3833 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3834 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3835 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3836 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3837 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3838 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3840 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3841 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3842 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3843 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3844 for (i
= 0; i
< n_out
; ++i
) {
3845 int j
= isl_basic_map_alloc_equality(bmap
);
3848 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3849 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3850 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3851 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3853 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3854 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3855 bmap
= add_divs(bmap
, 2 * n_out
);
3857 bmap
= isl_basic_map_simplify(bmap
);
3858 return isl_basic_map_finalize(bmap
);
3860 isl_basic_map_free(bmap
);
3861 isl_basic_map_free(bmap1
);
3862 isl_basic_map_free(bmap2
);
3866 /* Given two maps A -> f(A) and B -> g(B), construct a map
3867 * A \cap B -> f(A) + f(B)
3869 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3871 struct isl_map
*result
;
3877 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3879 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3880 map1
->n
* map2
->n
, 0);
3883 for (i
= 0; i
< map1
->n
; ++i
)
3884 for (j
= 0; j
< map2
->n
; ++j
) {
3885 struct isl_basic_map
*part
;
3886 part
= isl_basic_map_sum(
3887 isl_basic_map_copy(map1
->p
[i
]),
3888 isl_basic_map_copy(map2
->p
[j
]));
3889 if (isl_basic_map_is_empty(part
))
3890 isl_basic_map_free(part
);
3892 result
= isl_map_add_basic_map(result
, part
);
3905 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3906 __isl_take isl_set
*set2
)
3908 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3911 /* Given a basic map A -> f(A), construct A -> -f(A).
3913 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3918 bmap
= isl_basic_map_cow(bmap
);
3922 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3923 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3924 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3925 for (j
= 0; j
< n
; ++j
)
3926 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3927 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3928 for (j
= 0; j
< n
; ++j
)
3929 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3930 for (i
= 0; i
< bmap
->n_div
; ++i
)
3931 for (j
= 0; j
< n
; ++j
)
3932 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3933 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3934 return isl_basic_map_finalize(bmap
);
3937 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3939 return isl_basic_map_neg(bset
);
3942 /* Given a map A -> f(A), construct A -> -f(A).
3944 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3948 map
= isl_map_cow(map
);
3952 for (i
= 0; i
< map
->n
; ++i
) {
3953 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3964 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3966 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3969 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3970 * A -> floor(f(A)/d).
3972 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3975 unsigned n_in
, n_out
, nparam
, total
, pos
;
3976 struct isl_basic_map
*result
= NULL
;
3977 struct isl_dim_map
*dim_map
;
3983 nparam
= isl_basic_map_n_param(bmap
);
3984 n_in
= isl_basic_map_n_in(bmap
);
3985 n_out
= isl_basic_map_n_out(bmap
);
3987 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3988 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3989 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3990 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3991 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3992 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3994 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3995 bmap
->n_div
+ n_out
,
3996 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3997 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3998 result
= add_divs(result
, n_out
);
3999 for (i
= 0; i
< n_out
; ++i
) {
4001 j
= isl_basic_map_alloc_inequality(result
);
4004 isl_seq_clr(result
->ineq
[j
], 1+total
);
4005 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4006 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4007 j
= isl_basic_map_alloc_inequality(result
);
4010 isl_seq_clr(result
->ineq
[j
], 1+total
);
4011 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4012 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4013 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4016 result
= isl_basic_map_simplify(result
);
4017 return isl_basic_map_finalize(result
);
4019 isl_basic_map_free(result
);
4023 /* Given a map A -> f(A) and an integer d, construct a map
4024 * A -> floor(f(A)/d).
4026 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4030 map
= isl_map_cow(map
);
4034 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4035 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4036 for (i
= 0; i
< map
->n
; ++i
) {
4037 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4048 /* Given a map A -> f(A) and an integer d, construct a map
4049 * A -> floor(f(A)/d).
4051 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4052 __isl_take isl_val
*d
)
4056 if (!isl_val_is_int(d
))
4057 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4058 "expecting integer denominator", goto error
);
4059 map
= isl_map_floordiv(map
, d
->n
);
4068 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4074 i
= isl_basic_map_alloc_equality(bmap
);
4077 nparam
= isl_basic_map_n_param(bmap
);
4078 n_in
= isl_basic_map_n_in(bmap
);
4079 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4080 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4081 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4082 return isl_basic_map_finalize(bmap
);
4084 isl_basic_map_free(bmap
);
4088 /* Add a constraints to "bmap" expressing i_pos < o_pos
4090 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4096 i
= isl_basic_map_alloc_inequality(bmap
);
4099 nparam
= isl_basic_map_n_param(bmap
);
4100 n_in
= isl_basic_map_n_in(bmap
);
4101 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4102 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4103 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4104 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4105 return isl_basic_map_finalize(bmap
);
4107 isl_basic_map_free(bmap
);
4111 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4113 static __isl_give isl_basic_map
*var_less_or_equal(
4114 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4120 i
= isl_basic_map_alloc_inequality(bmap
);
4123 nparam
= isl_basic_map_n_param(bmap
);
4124 n_in
= isl_basic_map_n_in(bmap
);
4125 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4126 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4127 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4128 return isl_basic_map_finalize(bmap
);
4130 isl_basic_map_free(bmap
);
4134 /* Add a constraints to "bmap" expressing i_pos > o_pos
4136 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4142 i
= isl_basic_map_alloc_inequality(bmap
);
4145 nparam
= isl_basic_map_n_param(bmap
);
4146 n_in
= isl_basic_map_n_in(bmap
);
4147 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4148 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4149 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4150 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4151 return isl_basic_map_finalize(bmap
);
4153 isl_basic_map_free(bmap
);
4157 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4159 static __isl_give isl_basic_map
*var_more_or_equal(
4160 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4166 i
= isl_basic_map_alloc_inequality(bmap
);
4169 nparam
= isl_basic_map_n_param(bmap
);
4170 n_in
= isl_basic_map_n_in(bmap
);
4171 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4172 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4173 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4174 return isl_basic_map_finalize(bmap
);
4176 isl_basic_map_free(bmap
);
4180 __isl_give isl_basic_map
*isl_basic_map_equal(
4181 __isl_take isl_space
*dim
, unsigned n_equal
)
4184 struct isl_basic_map
*bmap
;
4185 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4188 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4189 bmap
= var_equal(bmap
, i
);
4190 return isl_basic_map_finalize(bmap
);
4193 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4195 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4199 struct isl_basic_map
*bmap
;
4200 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4203 for (i
= 0; i
< pos
&& bmap
; ++i
)
4204 bmap
= var_equal(bmap
, i
);
4206 bmap
= var_less(bmap
, pos
);
4207 return isl_basic_map_finalize(bmap
);
4210 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4212 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4213 __isl_take isl_space
*dim
, unsigned pos
)
4216 isl_basic_map
*bmap
;
4218 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4219 for (i
= 0; i
< pos
; ++i
)
4220 bmap
= var_equal(bmap
, i
);
4221 bmap
= var_less_or_equal(bmap
, pos
);
4222 return isl_basic_map_finalize(bmap
);
4225 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4227 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4231 struct isl_basic_map
*bmap
;
4232 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4235 for (i
= 0; i
< pos
&& bmap
; ++i
)
4236 bmap
= var_equal(bmap
, i
);
4238 bmap
= var_more(bmap
, pos
);
4239 return isl_basic_map_finalize(bmap
);
4242 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4244 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4245 __isl_take isl_space
*dim
, unsigned pos
)
4248 isl_basic_map
*bmap
;
4250 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4251 for (i
= 0; i
< pos
; ++i
)
4252 bmap
= var_equal(bmap
, i
);
4253 bmap
= var_more_or_equal(bmap
, pos
);
4254 return isl_basic_map_finalize(bmap
);
4257 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4258 unsigned n
, int equal
)
4260 struct isl_map
*map
;
4263 if (n
== 0 && equal
)
4264 return isl_map_universe(dims
);
4266 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4268 for (i
= 0; i
+ 1 < n
; ++i
)
4269 map
= isl_map_add_basic_map(map
,
4270 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4273 map
= isl_map_add_basic_map(map
,
4274 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4276 map
= isl_map_add_basic_map(map
,
4277 isl_basic_map_less_at(dims
, n
- 1));
4279 isl_space_free(dims
);
4284 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4288 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4291 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4293 return map_lex_lte_first(dim
, n
, 0);
4296 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4298 return map_lex_lte_first(dim
, n
, 1);
4301 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4303 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4306 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4308 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4311 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4312 unsigned n
, int equal
)
4314 struct isl_map
*map
;
4317 if (n
== 0 && equal
)
4318 return isl_map_universe(dims
);
4320 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4322 for (i
= 0; i
+ 1 < n
; ++i
)
4323 map
= isl_map_add_basic_map(map
,
4324 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4327 map
= isl_map_add_basic_map(map
,
4328 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4330 map
= isl_map_add_basic_map(map
,
4331 isl_basic_map_more_at(dims
, n
- 1));
4333 isl_space_free(dims
);
4338 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4342 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4345 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4347 return map_lex_gte_first(dim
, n
, 0);
4350 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4352 return map_lex_gte_first(dim
, n
, 1);
4355 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4357 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4360 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4362 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4365 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4366 __isl_take isl_set
*set2
)
4369 map
= isl_map_lex_le(isl_set_get_space(set1
));
4370 map
= isl_map_intersect_domain(map
, set1
);
4371 map
= isl_map_intersect_range(map
, set2
);
4375 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4376 __isl_take isl_set
*set2
)
4379 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4380 map
= isl_map_intersect_domain(map
, set1
);
4381 map
= isl_map_intersect_range(map
, set2
);
4385 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4386 __isl_take isl_set
*set2
)
4389 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4390 map
= isl_map_intersect_domain(map
, set1
);
4391 map
= isl_map_intersect_range(map
, set2
);
4395 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4396 __isl_take isl_set
*set2
)
4399 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4400 map
= isl_map_intersect_domain(map
, set1
);
4401 map
= isl_map_intersect_range(map
, set2
);
4405 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4406 __isl_take isl_map
*map2
)
4409 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4410 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4411 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4415 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4416 __isl_take isl_map
*map2
)
4419 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4420 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4421 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4425 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4426 __isl_take isl_map
*map2
)
4429 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4430 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4431 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4435 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4436 __isl_take isl_map
*map2
)
4439 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4440 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4441 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4445 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4446 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4448 struct isl_basic_map
*bmap
;
4450 bset
= isl_basic_set_cow(bset
);
4454 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4455 isl_space_free(bset
->dim
);
4456 bmap
= (struct isl_basic_map
*) bset
;
4458 return isl_basic_map_finalize(bmap
);
4460 isl_basic_set_free(bset
);
4461 isl_space_free(dim
);
4465 /* For a div d = floor(f/m), add the constraint
4469 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4470 unsigned pos
, isl_int
*div
)
4473 unsigned total
= isl_basic_map_total_dim(bmap
);
4475 i
= isl_basic_map_alloc_inequality(bmap
);
4478 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4479 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4484 /* For a div d = floor(f/m), add the constraint
4486 * -(f-(n-1)) + m d >= 0
4488 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4489 unsigned pos
, isl_int
*div
)
4492 unsigned total
= isl_basic_map_total_dim(bmap
);
4494 i
= isl_basic_map_alloc_inequality(bmap
);
4497 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4498 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4499 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4500 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4505 /* For a div d = floor(f/m), add the constraints
4508 * -(f-(n-1)) + m d >= 0
4510 * Note that the second constraint is the negation of
4514 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4515 unsigned pos
, isl_int
*div
)
4517 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4519 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4524 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4525 unsigned pos
, isl_int
*div
)
4527 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4531 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4533 unsigned total
= isl_basic_map_total_dim(bmap
);
4534 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4536 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4540 /* For each known div d = floor(f/m), add the constraints
4543 * -(f-(n-1)) + m d >= 0
4545 * Remove duplicate constraints in case of some these div constraints
4546 * already appear in "bmap".
4548 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4549 __isl_take isl_basic_map
*bmap
)
4555 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4559 bmap
= add_known_div_constraints(bmap
);
4560 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4561 bmap
= isl_basic_map_finalize(bmap
);
4565 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4567 * In particular, if this div is of the form d = floor(f/m),
4568 * then add the constraint
4572 * if sign < 0 or the constraint
4574 * -(f-(n-1)) + m d >= 0
4578 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4579 unsigned div
, int sign
)
4587 total
= isl_basic_map_total_dim(bmap
);
4588 div_pos
= total
- bmap
->n_div
+ div
;
4591 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4593 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4596 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4598 return isl_basic_map_add_div_constraints(bset
, div
);
4601 struct isl_basic_set
*isl_basic_map_underlying_set(
4602 struct isl_basic_map
*bmap
)
4606 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4608 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4609 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4610 return (struct isl_basic_set
*)bmap
;
4611 bmap
= isl_basic_map_cow(bmap
);
4614 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4617 bmap
->extra
-= bmap
->n_div
;
4619 bmap
= isl_basic_map_finalize(bmap
);
4620 return (struct isl_basic_set
*)bmap
;
4622 isl_basic_map_free(bmap
);
4626 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4627 __isl_take isl_basic_set
*bset
)
4629 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4632 /* Replace each element in "list" by the result of applying
4633 * isl_basic_map_underlying_set to the element.
4635 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4636 __isl_take isl_basic_map_list
*list
)
4643 n
= isl_basic_map_list_n_basic_map(list
);
4644 for (i
= 0; i
< n
; ++i
) {
4645 isl_basic_map
*bmap
;
4646 isl_basic_set
*bset
;
4648 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4649 bset
= isl_basic_set_underlying_set(bmap
);
4650 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4656 struct isl_basic_map
*isl_basic_map_overlying_set(
4657 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4659 struct isl_basic_map
*bmap
;
4660 struct isl_ctx
*ctx
;
4667 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4668 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4669 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4671 if (like
->n_div
== 0) {
4672 isl_space
*space
= isl_basic_map_get_space(like
);
4673 isl_basic_map_free(like
);
4674 return isl_basic_map_reset_space(bset
, space
);
4676 bset
= isl_basic_set_cow(bset
);
4679 total
= bset
->dim
->n_out
+ bset
->extra
;
4680 bmap
= (struct isl_basic_map
*)bset
;
4681 isl_space_free(bmap
->dim
);
4682 bmap
->dim
= isl_space_copy(like
->dim
);
4685 bmap
->n_div
= like
->n_div
;
4686 bmap
->extra
+= like
->n_div
;
4690 ltotal
= total
- bmap
->extra
+ like
->extra
;
4693 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4694 bmap
->extra
* (1 + 1 + total
));
4695 if (isl_blk_is_error(bmap
->block2
))
4697 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4701 for (i
= 0; i
< bmap
->extra
; ++i
)
4702 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4703 for (i
= 0; i
< like
->n_div
; ++i
) {
4704 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4705 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4707 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4709 isl_basic_map_free(like
);
4710 bmap
= isl_basic_map_simplify(bmap
);
4711 bmap
= isl_basic_map_finalize(bmap
);
4714 isl_basic_map_free(like
);
4715 isl_basic_set_free(bset
);
4719 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4720 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4722 return (struct isl_basic_set
*)
4723 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4726 struct isl_set
*isl_set_from_underlying_set(
4727 struct isl_set
*set
, struct isl_basic_set
*like
)
4733 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4735 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4736 isl_basic_set_free(like
);
4739 set
= isl_set_cow(set
);
4742 for (i
= 0; i
< set
->n
; ++i
) {
4743 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4744 isl_basic_set_copy(like
));
4748 isl_space_free(set
->dim
);
4749 set
->dim
= isl_space_copy(like
->dim
);
4752 isl_basic_set_free(like
);
4755 isl_basic_set_free(like
);
4760 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4764 map
= isl_map_cow(map
);
4767 map
->dim
= isl_space_cow(map
->dim
);
4771 for (i
= 1; i
< map
->n
; ++i
)
4772 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4774 for (i
= 0; i
< map
->n
; ++i
) {
4775 map
->p
[i
] = (struct isl_basic_map
*)
4776 isl_basic_map_underlying_set(map
->p
[i
]);
4781 map
->dim
= isl_space_underlying(map
->dim
, 0);
4783 isl_space_free(map
->dim
);
4784 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4788 return (struct isl_set
*)map
;
4794 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4796 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4799 /* Replace the space of "bmap" by "space".
4801 * If the space of "bmap" is identical to "space" (including the identifiers
4802 * of the input and output dimensions), then simply return the original input.
4804 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4805 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4811 equal
= isl_space_is_equal(bmap
->dim
, space
);
4812 if (equal
>= 0 && equal
)
4813 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4815 if (equal
>= 0 && equal
)
4816 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4817 space
, isl_dim_out
);
4821 isl_space_free(space
);
4824 bmap
= isl_basic_map_cow(bmap
);
4825 if (!bmap
|| !space
)
4828 isl_space_free(bmap
->dim
);
4831 bmap
= isl_basic_map_finalize(bmap
);
4835 isl_basic_map_free(bmap
);
4836 isl_space_free(space
);
4840 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4841 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4843 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4847 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4848 __isl_take isl_space
*dim
)
4852 map
= isl_map_cow(map
);
4856 for (i
= 0; i
< map
->n
; ++i
) {
4857 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4858 isl_space_copy(dim
));
4862 isl_space_free(map
->dim
);
4868 isl_space_free(dim
);
4872 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4873 __isl_take isl_space
*dim
)
4875 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4878 /* Compute the parameter domain of the given basic set.
4880 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4885 if (isl_basic_set_is_params(bset
))
4888 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4889 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4890 space
= isl_basic_set_get_space(bset
);
4891 space
= isl_space_params(space
);
4892 bset
= isl_basic_set_reset_space(bset
, space
);
4896 /* Construct a zero-dimensional basic set with the given parameter domain.
4898 __isl_give isl_basic_set
*isl_basic_set_from_params(
4899 __isl_take isl_basic_set
*bset
)
4902 space
= isl_basic_set_get_space(bset
);
4903 space
= isl_space_set_from_params(space
);
4904 bset
= isl_basic_set_reset_space(bset
, space
);
4908 /* Compute the parameter domain of the given set.
4910 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4915 if (isl_set_is_params(set
))
4918 n
= isl_set_dim(set
, isl_dim_set
);
4919 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4920 space
= isl_set_get_space(set
);
4921 space
= isl_space_params(space
);
4922 set
= isl_set_reset_space(set
, space
);
4926 /* Construct a zero-dimensional set with the given parameter domain.
4928 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4931 space
= isl_set_get_space(set
);
4932 space
= isl_space_set_from_params(space
);
4933 set
= isl_set_reset_space(set
, space
);
4937 /* Compute the parameter domain of the given map.
4939 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4944 n
= isl_map_dim(map
, isl_dim_in
);
4945 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4946 n
= isl_map_dim(map
, isl_dim_out
);
4947 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4948 space
= isl_map_get_space(map
);
4949 space
= isl_space_params(space
);
4950 map
= isl_map_reset_space(map
, space
);
4954 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4961 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4963 n_out
= isl_basic_map_n_out(bmap
);
4964 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4966 return isl_basic_map_reset_space(bmap
, space
);
4969 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4973 return isl_space_may_be_set(bmap
->dim
);
4976 /* Is this basic map actually a set?
4977 * Users should never call this function. Outside of isl,
4978 * the type should indicate whether something is a set or a map.
4980 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4984 return isl_space_is_set(bmap
->dim
);
4987 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4991 if (isl_basic_map_is_set(bmap
))
4993 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4996 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4997 __isl_take isl_basic_map
*bmap
)
5001 isl_basic_map
*domain
;
5002 int nparam
, n_in
, n_out
;
5005 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5006 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5007 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5009 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5010 domain
= isl_basic_map_universe(dim
);
5012 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5013 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5014 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5016 total
= isl_basic_map_total_dim(bmap
);
5018 for (i
= 0; i
< n_in
; ++i
) {
5019 k
= isl_basic_map_alloc_equality(bmap
);
5022 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5023 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5024 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5027 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5028 return isl_basic_map_finalize(bmap
);
5030 isl_basic_map_free(bmap
);
5034 __isl_give isl_basic_map
*isl_basic_map_range_map(
5035 __isl_take isl_basic_map
*bmap
)
5039 isl_basic_map
*range
;
5040 int nparam
, n_in
, n_out
;
5043 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5044 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5045 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5047 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5048 range
= isl_basic_map_universe(dim
);
5050 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5051 bmap
= isl_basic_map_apply_range(bmap
, range
);
5052 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5054 total
= isl_basic_map_total_dim(bmap
);
5056 for (i
= 0; i
< n_out
; ++i
) {
5057 k
= isl_basic_map_alloc_equality(bmap
);
5060 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5061 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5062 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5065 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5066 return isl_basic_map_finalize(bmap
);
5068 isl_basic_map_free(bmap
);
5072 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5076 return isl_space_may_be_set(map
->dim
);
5079 /* Is this map actually a set?
5080 * Users should never call this function. Outside of isl,
5081 * the type should indicate whether something is a set or a map.
5083 int isl_map_is_set(__isl_keep isl_map
*map
)
5087 return isl_space_is_set(map
->dim
);
5090 struct isl_set
*isl_map_range(struct isl_map
*map
)
5093 struct isl_set
*set
;
5097 if (isl_map_is_set(map
))
5098 return (isl_set
*)map
;
5100 map
= isl_map_cow(map
);
5104 set
= (struct isl_set
*) map
;
5105 set
->dim
= isl_space_range(set
->dim
);
5108 for (i
= 0; i
< map
->n
; ++i
) {
5109 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5113 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5114 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5121 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5125 map
= isl_map_cow(map
);
5129 map
->dim
= isl_space_domain_map(map
->dim
);
5132 for (i
= 0; i
< map
->n
; ++i
) {
5133 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5137 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5138 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5145 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5148 isl_space
*range_dim
;
5150 map
= isl_map_cow(map
);
5154 range_dim
= isl_space_range(isl_map_get_space(map
));
5155 range_dim
= isl_space_from_range(range_dim
);
5156 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5157 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5160 for (i
= 0; i
< map
->n
; ++i
) {
5161 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5165 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5166 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5173 /* Given a wrapped map of the form A[B -> C],
5174 * return the map A[B -> C] -> B.
5176 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5183 if (!isl_set_has_tuple_id(set
))
5184 return isl_map_domain_map(isl_set_unwrap(set
));
5186 id
= isl_set_get_tuple_id(set
);
5187 map
= isl_map_domain_map(isl_set_unwrap(set
));
5188 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5193 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5194 __isl_take isl_space
*dim
)
5197 struct isl_map
*map
= NULL
;
5199 set
= isl_set_cow(set
);
5202 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5203 map
= (struct isl_map
*)set
;
5204 for (i
= 0; i
< set
->n
; ++i
) {
5205 map
->p
[i
] = isl_basic_map_from_basic_set(
5206 set
->p
[i
], isl_space_copy(dim
));
5210 isl_space_free(map
->dim
);
5214 isl_space_free(dim
);
5219 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5220 __isl_take isl_basic_set
*bset
)
5222 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5225 __isl_give isl_basic_map
*isl_basic_map_from_range(
5226 __isl_take isl_basic_set
*bset
)
5229 space
= isl_basic_set_get_space(bset
);
5230 space
= isl_space_from_range(space
);
5231 bset
= isl_basic_set_reset_space(bset
, space
);
5232 return (isl_basic_map
*)bset
;
5235 /* Create a relation with the given set as range.
5236 * The domain of the created relation is a zero-dimensional
5237 * flat anonymous space.
5239 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5242 space
= isl_set_get_space(set
);
5243 space
= isl_space_from_range(space
);
5244 set
= isl_set_reset_space(set
, space
);
5245 return (struct isl_map
*)set
;
5248 /* Create a relation with the given set as domain.
5249 * The range of the created relation is a zero-dimensional
5250 * flat anonymous space.
5252 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5254 return isl_map_reverse(isl_map_from_range(set
));
5257 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5258 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5260 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5263 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5264 __isl_take isl_set
*range
)
5266 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5269 /* Return a newly allocated isl_map with given space and flags and
5270 * room for "n" basic maps.
5271 * Make sure that all cached information is cleared.
5273 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5276 struct isl_map
*map
;
5281 isl_die(space
->ctx
, isl_error_internal
,
5282 "negative number of basic maps", goto error
);
5283 map
= isl_calloc(space
->ctx
, struct isl_map
,
5284 sizeof(struct isl_map
) +
5285 (n
- 1) * sizeof(struct isl_basic_map
*));
5289 map
->ctx
= space
->ctx
;
5290 isl_ctx_ref(map
->ctx
);
5298 isl_space_free(space
);
5302 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5303 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5306 struct isl_map
*map
;
5309 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5313 map
= isl_map_alloc_space(dims
, n
, flags
);
5317 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5319 struct isl_basic_map
*bmap
;
5320 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5321 bmap
= isl_basic_map_set_to_empty(bmap
);
5325 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5327 struct isl_basic_set
*bset
;
5328 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5329 bset
= isl_basic_set_set_to_empty(bset
);
5333 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5335 struct isl_basic_map
*bmap
;
5336 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5337 bmap
= isl_basic_map_finalize(bmap
);
5341 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5343 struct isl_basic_set
*bset
;
5344 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5345 bset
= isl_basic_set_finalize(bset
);
5349 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5352 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5353 isl_basic_map
*bmap
;
5355 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5356 for (i
= 0; i
< total
; ++i
) {
5357 int k
= isl_basic_map_alloc_inequality(bmap
);
5360 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5361 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5365 isl_basic_map_free(bmap
);
5369 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5371 return isl_basic_map_nat_universe(dim
);
5374 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5376 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5379 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5381 return isl_map_nat_universe(dim
);
5384 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5386 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5389 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5391 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5394 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5396 struct isl_map
*map
;
5399 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5400 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5404 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5406 struct isl_set
*set
;
5409 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5410 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5414 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5417 struct isl_map
*dup
;
5421 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5422 for (i
= 0; i
< map
->n
; ++i
)
5423 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5427 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5428 __isl_take isl_basic_map
*bmap
)
5432 if (isl_basic_map_plain_is_empty(bmap
)) {
5433 isl_basic_map_free(bmap
);
5436 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5437 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5438 map
->p
[map
->n
] = bmap
;
5440 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5446 isl_basic_map_free(bmap
);
5450 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5461 isl_ctx_deref(map
->ctx
);
5462 for (i
= 0; i
< map
->n
; ++i
)
5463 isl_basic_map_free(map
->p
[i
]);
5464 isl_space_free(map
->dim
);
5470 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5471 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5475 bmap
= isl_basic_map_cow(bmap
);
5476 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5477 j
= isl_basic_map_alloc_equality(bmap
);
5480 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5481 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5482 isl_int_set_si(bmap
->eq
[j
][0], value
);
5483 bmap
= isl_basic_map_simplify(bmap
);
5484 return isl_basic_map_finalize(bmap
);
5486 isl_basic_map_free(bmap
);
5490 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5491 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5495 bmap
= isl_basic_map_cow(bmap
);
5496 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5497 j
= isl_basic_map_alloc_equality(bmap
);
5500 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5501 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5502 isl_int_set(bmap
->eq
[j
][0], value
);
5503 bmap
= isl_basic_map_simplify(bmap
);
5504 return isl_basic_map_finalize(bmap
);
5506 isl_basic_map_free(bmap
);
5510 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5511 enum isl_dim_type type
, unsigned pos
, int value
)
5515 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5516 return isl_basic_map_fix_pos_si(bmap
,
5517 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5519 isl_basic_map_free(bmap
);
5523 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5524 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5528 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5529 return isl_basic_map_fix_pos(bmap
,
5530 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5532 isl_basic_map_free(bmap
);
5536 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5537 * to be equal to "v".
5539 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5540 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5544 if (!isl_val_is_int(v
))
5545 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5546 "expecting integer value", goto error
);
5547 if (pos
>= isl_basic_map_dim(bmap
, type
))
5548 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5549 "index out of bounds", goto error
);
5550 pos
+= isl_basic_map_offset(bmap
, type
);
5551 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5555 isl_basic_map_free(bmap
);
5560 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5561 * to be equal to "v".
5563 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5564 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5566 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5569 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5570 enum isl_dim_type type
, unsigned pos
, int value
)
5572 return (struct isl_basic_set
*)
5573 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5577 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5578 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5580 return (struct isl_basic_set
*)
5581 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5585 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5586 unsigned input
, int value
)
5588 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5591 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5592 unsigned dim
, int value
)
5594 return (struct isl_basic_set
*)
5595 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5596 isl_dim_set
, dim
, value
);
5599 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5601 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5608 isl_basic_map_free(map
->p
[i
]);
5609 if (i
!= map
->n
- 1) {
5610 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5611 map
->p
[i
] = map
->p
[map
->n
- 1];
5618 /* Perform "fn" on each basic map of "map", where we may not be holding
5619 * the only reference to "map".
5620 * In particular, "fn" should be a semantics preserving operation
5621 * that we want to apply to all copies of "map". We therefore need
5622 * to be careful not to modify "map" in a way that breaks "map"
5623 * in case anything goes wrong.
5625 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5626 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5628 struct isl_basic_map
*bmap
;
5634 for (i
= map
->n
- 1; i
>= 0; --i
) {
5635 bmap
= isl_basic_map_copy(map
->p
[i
]);
5639 isl_basic_map_free(map
->p
[i
]);
5641 if (remove_if_empty(map
, i
) < 0)
5651 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5652 enum isl_dim_type type
, unsigned pos
, int value
)
5656 map
= isl_map_cow(map
);
5660 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5661 for (i
= map
->n
- 1; i
>= 0; --i
) {
5662 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5663 if (remove_if_empty(map
, i
) < 0)
5666 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5673 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5674 enum isl_dim_type type
, unsigned pos
, int value
)
5676 return (struct isl_set
*)
5677 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5680 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5681 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5685 map
= isl_map_cow(map
);
5689 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5690 for (i
= 0; i
< map
->n
; ++i
) {
5691 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5695 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5702 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5703 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5705 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5708 /* Fix the value of the variable at position "pos" of type "type" of "map"
5709 * to be equal to "v".
5711 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5712 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5716 map
= isl_map_cow(map
);
5720 if (!isl_val_is_int(v
))
5721 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5722 "expecting integer value", goto error
);
5723 if (pos
>= isl_map_dim(map
, type
))
5724 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5725 "index out of bounds", goto error
);
5726 for (i
= map
->n
- 1; i
>= 0; --i
) {
5727 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5729 if (remove_if_empty(map
, i
) < 0)
5732 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5741 /* Fix the value of the variable at position "pos" of type "type" of "set"
5742 * to be equal to "v".
5744 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5745 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5747 return isl_map_fix_val(set
, type
, pos
, v
);
5750 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5751 unsigned input
, int value
)
5753 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5756 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5758 return (struct isl_set
*)
5759 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5762 static __isl_give isl_basic_map
*basic_map_bound_si(
5763 __isl_take isl_basic_map
*bmap
,
5764 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5770 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5771 pos
+= isl_basic_map_offset(bmap
, type
);
5772 bmap
= isl_basic_map_cow(bmap
);
5773 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5774 j
= isl_basic_map_alloc_inequality(bmap
);
5777 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5779 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5780 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5782 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5783 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5785 bmap
= isl_basic_map_simplify(bmap
);
5786 return isl_basic_map_finalize(bmap
);
5788 isl_basic_map_free(bmap
);
5792 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5793 __isl_take isl_basic_map
*bmap
,
5794 enum isl_dim_type type
, unsigned pos
, int value
)
5796 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5799 /* Constrain the values of the given dimension to be no greater than "value".
5801 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5802 __isl_take isl_basic_map
*bmap
,
5803 enum isl_dim_type type
, unsigned pos
, int value
)
5805 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5808 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5809 unsigned dim
, isl_int value
)
5813 bset
= isl_basic_set_cow(bset
);
5814 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5815 j
= isl_basic_set_alloc_inequality(bset
);
5818 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5819 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5820 isl_int_neg(bset
->ineq
[j
][0], value
);
5821 bset
= isl_basic_set_simplify(bset
);
5822 return isl_basic_set_finalize(bset
);
5824 isl_basic_set_free(bset
);
5828 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5829 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5833 map
= isl_map_cow(map
);
5837 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5838 for (i
= 0; i
< map
->n
; ++i
) {
5839 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5840 type
, pos
, value
, upper
);
5844 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5851 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5852 enum isl_dim_type type
, unsigned pos
, int value
)
5854 return map_bound_si(map
, type
, pos
, value
, 0);
5857 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5858 enum isl_dim_type type
, unsigned pos
, int value
)
5860 return map_bound_si(map
, type
, pos
, value
, 1);
5863 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5864 enum isl_dim_type type
, unsigned pos
, int value
)
5866 return (struct isl_set
*)
5867 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5870 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5871 enum isl_dim_type type
, unsigned pos
, int value
)
5873 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5876 /* Bound the given variable of "bmap" from below (or above is "upper"
5877 * is set) to "value".
5879 static __isl_give isl_basic_map
*basic_map_bound(
5880 __isl_take isl_basic_map
*bmap
,
5881 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5887 if (pos
>= isl_basic_map_dim(bmap
, type
))
5888 isl_die(bmap
->ctx
, isl_error_invalid
,
5889 "index out of bounds", goto error
);
5890 pos
+= isl_basic_map_offset(bmap
, type
);
5891 bmap
= isl_basic_map_cow(bmap
);
5892 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5893 j
= isl_basic_map_alloc_inequality(bmap
);
5896 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5898 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5899 isl_int_set(bmap
->ineq
[j
][0], value
);
5901 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5902 isl_int_neg(bmap
->ineq
[j
][0], value
);
5904 bmap
= isl_basic_map_simplify(bmap
);
5905 return isl_basic_map_finalize(bmap
);
5907 isl_basic_map_free(bmap
);
5911 /* Bound the given variable of "map" from below (or above is "upper"
5912 * is set) to "value".
5914 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5915 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5919 map
= isl_map_cow(map
);
5923 if (pos
>= isl_map_dim(map
, type
))
5924 isl_die(map
->ctx
, isl_error_invalid
,
5925 "index out of bounds", goto error
);
5926 for (i
= map
->n
- 1; i
>= 0; --i
) {
5927 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5928 if (remove_if_empty(map
, i
) < 0)
5931 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5938 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5939 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5941 return map_bound(map
, type
, pos
, value
, 0);
5944 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5945 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5947 return map_bound(map
, type
, pos
, value
, 1);
5950 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5951 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5953 return isl_map_lower_bound(set
, type
, pos
, value
);
5956 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5957 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5959 return isl_map_upper_bound(set
, type
, pos
, value
);
5962 /* Force the values of the variable at position "pos" of type "type" of "set"
5963 * to be no smaller than "value".
5965 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5966 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5970 if (!isl_val_is_int(value
))
5971 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5972 "expecting integer value", goto error
);
5973 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5974 isl_val_free(value
);
5977 isl_val_free(value
);
5982 /* Force the values of the variable at position "pos" of type "type" of "set"
5983 * to be no greater than "value".
5985 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5986 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5990 if (!isl_val_is_int(value
))
5991 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5992 "expecting integer value", goto error
);
5993 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5994 isl_val_free(value
);
5997 isl_val_free(value
);
6002 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6007 set
= isl_set_cow(set
);
6011 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6012 for (i
= 0; i
< set
->n
; ++i
) {
6013 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6023 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6027 map
= isl_map_cow(map
);
6031 map
->dim
= isl_space_reverse(map
->dim
);
6034 for (i
= 0; i
< map
->n
; ++i
) {
6035 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6039 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6046 static struct isl_map
*isl_basic_map_partial_lexopt(
6047 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6048 struct isl_set
**empty
, int max
)
6050 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6053 struct isl_map
*isl_basic_map_partial_lexmax(
6054 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6055 struct isl_set
**empty
)
6057 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6060 struct isl_map
*isl_basic_map_partial_lexmin(
6061 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6062 struct isl_set
**empty
)
6064 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6067 struct isl_set
*isl_basic_set_partial_lexmin(
6068 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6069 struct isl_set
**empty
)
6071 return (struct isl_set
*)
6072 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6076 struct isl_set
*isl_basic_set_partial_lexmax(
6077 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6078 struct isl_set
**empty
)
6080 return (struct isl_set
*)
6081 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6085 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6086 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6087 __isl_give isl_set
**empty
)
6089 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6092 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6093 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6094 __isl_give isl_set
**empty
)
6096 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6099 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6100 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6101 __isl_give isl_set
**empty
)
6103 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6106 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6107 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6108 __isl_give isl_set
**empty
)
6110 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6113 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6114 __isl_take isl_basic_map
*bmap
, int max
)
6116 isl_basic_set
*dom
= NULL
;
6117 isl_space
*dom_space
;
6121 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6122 dom
= isl_basic_set_universe(dom_space
);
6123 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6125 isl_basic_map_free(bmap
);
6129 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6130 __isl_take isl_basic_map
*bmap
)
6132 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6136 #define TYPE isl_pw_multi_aff
6138 #define SUFFIX _pw_multi_aff
6140 #define EMPTY isl_pw_multi_aff_empty
6142 #define ADD isl_pw_multi_aff_union_add
6143 #include "isl_map_lexopt_templ.c"
6145 /* Given a map "map", compute the lexicographically minimal
6146 * (or maximal) image element for each domain element in dom,
6147 * in the form of an isl_pw_multi_aff.
6148 * If "empty" is not NULL, then set *empty to those elements in dom that
6149 * do not have an image element.
6151 * We first compute the lexicographically minimal or maximal element
6152 * in the first basic map. This results in a partial solution "res"
6153 * and a subset "todo" of dom that still need to be handled.
6154 * We then consider each of the remaining maps in "map" and successively
6155 * update both "res" and "todo".
6156 * If "empty" is NULL, then the todo sets are not needed and therefore
6157 * also not computed.
6159 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6160 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6161 __isl_give isl_set
**empty
, int max
)
6164 isl_pw_multi_aff
*res
;
6170 if (isl_map_plain_is_empty(map
)) {
6175 return isl_pw_multi_aff_from_map(map
);
6178 res
= basic_map_partial_lexopt_pw_multi_aff(
6179 isl_basic_map_copy(map
->p
[0]),
6180 isl_set_copy(dom
), empty
, max
);
6184 for (i
= 1; i
< map
->n
; ++i
) {
6185 isl_pw_multi_aff
*res_i
;
6187 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6188 isl_basic_map_copy(map
->p
[i
]),
6189 isl_set_copy(dom
), empty
, max
);
6192 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6194 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6197 todo
= isl_set_intersect(todo
, *empty
);
6216 #define TYPE isl_map
6220 #define EMPTY isl_map_empty
6222 #define ADD isl_map_union_disjoint
6223 #include "isl_map_lexopt_templ.c"
6225 /* Given a map "map", compute the lexicographically minimal
6226 * (or maximal) image element for each domain element in "dom",
6227 * in the form of an isl_map.
6228 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6229 * do not have an image element.
6231 * If the input consists of more than one disjunct, then first
6232 * compute the desired result in the form of an isl_pw_multi_aff and
6233 * then convert that into an isl_map.
6235 * This function used to have an explicit implementation in terms
6236 * of isl_maps, but it would continually intersect the domains of
6237 * partial results with the complement of the domain of the next
6238 * partial solution, potentially leading to an explosion in the number
6239 * of disjuncts if there are several disjuncts in the input.
6240 * An even earlier implementation of this function would look for
6241 * better results in the domain of the partial result and for extra
6242 * results in the complement of this domain, which would lead to
6243 * even more splintering.
6245 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6246 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6247 __isl_give isl_set
**empty
, int max
)
6249 struct isl_map
*res
;
6250 isl_pw_multi_aff
*pma
;
6255 if (isl_map_plain_is_empty(map
)) {
6264 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6270 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
, max
);
6271 return isl_map_from_pw_multi_aff(pma
);
6280 __isl_give isl_map
*isl_map_partial_lexmax(
6281 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6282 __isl_give isl_set
**empty
)
6284 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6287 __isl_give isl_map
*isl_map_partial_lexmin(
6288 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6289 __isl_give isl_set
**empty
)
6291 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6294 __isl_give isl_set
*isl_set_partial_lexmin(
6295 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6296 __isl_give isl_set
**empty
)
6298 return (struct isl_set
*)
6299 isl_map_partial_lexmin((struct isl_map
*)set
,
6303 __isl_give isl_set
*isl_set_partial_lexmax(
6304 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6305 __isl_give isl_set
**empty
)
6307 return (struct isl_set
*)
6308 isl_map_partial_lexmax((struct isl_map
*)set
,
6312 /* Compute the lexicographic minimum (or maximum if "max" is set)
6313 * of "bmap" over its domain.
6315 * Since we are not interested in the part of the domain space where
6316 * there is no solution, we initialize the domain to those constraints
6317 * of "bmap" that only involve the parameters and the input dimensions.
6318 * This relieves the parametric programming engine from detecting those
6319 * inequalities and transferring them to the context. More importantly,
6320 * it ensures that those inequalities are transferred first and not
6321 * intermixed with inequalities that actually split the domain.
6323 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6327 isl_basic_map
*copy
;
6330 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6331 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6332 copy
= isl_basic_map_copy(bmap
);
6333 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6334 isl_dim_div
, 0, n_div
);
6335 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6336 isl_dim_out
, 0, n_out
);
6337 dom
= isl_basic_map_domain(copy
);
6338 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6341 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6343 return isl_basic_map_lexopt(bmap
, 0);
6346 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6348 return isl_basic_map_lexopt(bmap
, 1);
6351 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6353 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6356 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6358 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6361 /* Extract the first and only affine expression from list
6362 * and then add it to *pwaff with the given dom.
6363 * This domain is known to be disjoint from other domains
6364 * because of the way isl_basic_map_foreach_lexmax works.
6366 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6367 __isl_take isl_aff_list
*list
, void *user
)
6369 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6371 isl_pw_aff
**pwaff
= user
;
6372 isl_pw_aff
*pwaff_i
;
6376 if (isl_aff_list_n_aff(list
) != 1)
6377 isl_die(ctx
, isl_error_internal
,
6378 "expecting single element list", goto error
);
6380 aff
= isl_aff_list_get_aff(list
, 0);
6381 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6383 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6385 isl_aff_list_free(list
);
6389 isl_basic_set_free(dom
);
6390 isl_aff_list_free(list
);
6394 /* Given a basic map with one output dimension, compute the minimum or
6395 * maximum of that dimension as an isl_pw_aff.
6397 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6398 * call update_dim_opt on each leaf of the result.
6400 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6403 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6407 dim
= isl_space_from_domain(isl_space_domain(dim
));
6408 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6409 pwaff
= isl_pw_aff_empty(dim
);
6411 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6413 return isl_pw_aff_free(pwaff
);
6418 /* Compute the minimum or maximum of the given output dimension
6419 * as a function of the parameters and the input dimensions,
6420 * but independently of the other output dimensions.
6422 * We first project out the other output dimension and then compute
6423 * the "lexicographic" maximum in each basic map, combining the results
6424 * using isl_pw_aff_union_max.
6426 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6433 n_out
= isl_map_dim(map
, isl_dim_out
);
6434 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6435 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6440 isl_space
*dim
= isl_map_get_space(map
);
6442 return isl_pw_aff_empty(dim
);
6445 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6446 for (i
= 1; i
< map
->n
; ++i
) {
6447 isl_pw_aff
*pwaff_i
;
6449 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6450 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6458 /* Compute the maximum of the given output dimension as a function of the
6459 * parameters and input dimensions, but independently of
6460 * the other output dimensions.
6462 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6464 return map_dim_opt(map
, pos
, 1);
6467 /* Compute the minimum or maximum of the given set dimension
6468 * as a function of the parameters,
6469 * but independently of the other set dimensions.
6471 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6474 return map_dim_opt(set
, pos
, max
);
6477 /* Compute the maximum of the given set dimension as a function of the
6478 * parameters, but independently of the other set dimensions.
6480 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6482 return set_dim_opt(set
, pos
, 1);
6485 /* Compute the minimum of the given set dimension as a function of the
6486 * parameters, but independently of the other set dimensions.
6488 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6490 return set_dim_opt(set
, pos
, 0);
6493 /* Apply a preimage specified by "mat" on the parameters of "bset".
6494 * bset is assumed to have only parameters and divs.
6496 static struct isl_basic_set
*basic_set_parameter_preimage(
6497 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6504 bset
->dim
= isl_space_cow(bset
->dim
);
6508 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6510 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6512 bset
->dim
->nparam
= 0;
6513 bset
->dim
->n_out
= nparam
;
6514 bset
= isl_basic_set_preimage(bset
, mat
);
6516 bset
->dim
->nparam
= bset
->dim
->n_out
;
6517 bset
->dim
->n_out
= 0;
6522 isl_basic_set_free(bset
);
6526 /* Apply a preimage specified by "mat" on the parameters of "set".
6527 * set is assumed to have only parameters and divs.
6529 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6530 __isl_take isl_mat
*mat
)
6538 nparam
= isl_set_dim(set
, isl_dim_param
);
6540 if (mat
->n_row
!= 1 + nparam
)
6541 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6542 "unexpected number of rows", goto error
);
6544 space
= isl_set_get_space(set
);
6545 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6546 isl_dim_param
, 0, nparam
);
6547 set
= isl_set_reset_space(set
, space
);
6548 set
= isl_set_preimage(set
, mat
);
6549 nparam
= isl_set_dim(set
, isl_dim_out
);
6550 space
= isl_set_get_space(set
);
6551 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6552 isl_dim_out
, 0, nparam
);
6553 set
= isl_set_reset_space(set
, space
);
6561 /* Intersect the basic set "bset" with the affine space specified by the
6562 * equalities in "eq".
6564 static struct isl_basic_set
*basic_set_append_equalities(
6565 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6573 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6578 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6579 for (i
= 0; i
< eq
->n_row
; ++i
) {
6580 k
= isl_basic_set_alloc_equality(bset
);
6583 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6584 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6588 bset
= isl_basic_set_gauss(bset
, NULL
);
6589 bset
= isl_basic_set_finalize(bset
);
6594 isl_basic_set_free(bset
);
6598 /* Intersect the set "set" with the affine space specified by the
6599 * equalities in "eq".
6601 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6609 for (i
= 0; i
< set
->n
; ++i
) {
6610 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6623 /* Given a basic set "bset" that only involves parameters and existentially
6624 * quantified variables, return the index of the first equality
6625 * that only involves parameters. If there is no such equality then
6626 * return bset->n_eq.
6628 * This function assumes that isl_basic_set_gauss has been called on "bset".
6630 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6633 unsigned nparam
, n_div
;
6638 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6639 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6641 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6642 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6649 /* Compute an explicit representation for the existentially quantified
6650 * variables in "bset" by computing the "minimal value" of the set
6651 * variables. Since there are no set variables, the computation of
6652 * the minimal value essentially computes an explicit representation
6653 * of the non-empty part(s) of "bset".
6655 * The input only involves parameters and existentially quantified variables.
6656 * All equalities among parameters have been removed.
6658 * Since the existentially quantified variables in the result are in general
6659 * going to be different from those in the input, we first replace
6660 * them by the minimal number of variables based on their equalities.
6661 * This should simplify the parametric integer programming.
6663 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6665 isl_morph
*morph1
, *morph2
;
6671 if (bset
->n_eq
== 0)
6672 return isl_basic_set_lexmin(bset
);
6674 morph1
= isl_basic_set_parameter_compression(bset
);
6675 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6676 bset
= isl_basic_set_lift(bset
);
6677 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6678 bset
= isl_morph_basic_set(morph2
, bset
);
6679 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6680 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6682 set
= isl_basic_set_lexmin(bset
);
6684 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6689 /* Project the given basic set onto its parameter domain, possibly introducing
6690 * new, explicit, existential variables in the constraints.
6691 * The input has parameters and (possibly implicit) existential variables.
6692 * The output has the same parameters, but only
6693 * explicit existentially quantified variables.
6695 * The actual projection is performed by pip, but pip doesn't seem
6696 * to like equalities very much, so we first remove the equalities
6697 * among the parameters by performing a variable compression on
6698 * the parameters. Afterward, an inverse transformation is performed
6699 * and the equalities among the parameters are inserted back in.
6701 * The variable compression on the parameters may uncover additional
6702 * equalities that were only implicit before. We therefore check
6703 * if there are any new parameter equalities in the result and
6704 * if so recurse. The removal of parameter equalities is required
6705 * for the parameter compression performed by base_compute_divs.
6707 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6711 struct isl_mat
*T
, *T2
;
6712 struct isl_set
*set
;
6715 bset
= isl_basic_set_cow(bset
);
6719 if (bset
->n_eq
== 0)
6720 return base_compute_divs(bset
);
6722 bset
= isl_basic_set_gauss(bset
, NULL
);
6725 if (isl_basic_set_plain_is_empty(bset
))
6726 return isl_set_from_basic_set(bset
);
6728 i
= first_parameter_equality(bset
);
6729 if (i
== bset
->n_eq
)
6730 return base_compute_divs(bset
);
6732 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6733 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6735 eq
= isl_mat_cow(eq
);
6736 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6737 if (T
&& T
->n_col
== 0) {
6741 bset
= isl_basic_set_set_to_empty(bset
);
6742 return isl_set_from_basic_set(bset
);
6744 bset
= basic_set_parameter_preimage(bset
, T
);
6746 i
= first_parameter_equality(bset
);
6749 else if (i
== bset
->n_eq
)
6750 set
= base_compute_divs(bset
);
6752 set
= parameter_compute_divs(bset
);
6753 set
= set_parameter_preimage(set
, T2
);
6754 set
= set_append_equalities(set
, eq
);
6758 /* Insert the divs from "ls" before those of "bmap".
6760 * The number of columns is not changed, which means that the last
6761 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6762 * The caller is responsible for removing the same number of dimensions
6763 * from the space of "bmap".
6765 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6766 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6772 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6776 old_n_div
= bmap
->n_div
;
6777 bmap
= insert_div_rows(bmap
, n_div
);
6781 for (i
= 0; i
< n_div
; ++i
) {
6782 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6783 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6789 /* Replace the space of "bmap" by the space and divs of "ls".
6791 * If "ls" has any divs, then we simplify the result since we may
6792 * have discovered some additional equalities that could simplify
6793 * the div expressions.
6795 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6796 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6800 bmap
= isl_basic_map_cow(bmap
);
6804 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6805 bmap
= insert_divs_from_local_space(bmap
, ls
);
6809 isl_space_free(bmap
->dim
);
6810 bmap
->dim
= isl_local_space_get_space(ls
);
6814 isl_local_space_free(ls
);
6816 bmap
= isl_basic_map_simplify(bmap
);
6817 bmap
= isl_basic_map_finalize(bmap
);
6820 isl_basic_map_free(bmap
);
6821 isl_local_space_free(ls
);
6825 /* Replace the space of "map" by the space and divs of "ls".
6827 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6828 __isl_take isl_local_space
*ls
)
6832 map
= isl_map_cow(map
);
6836 for (i
= 0; i
< map
->n
; ++i
) {
6837 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6838 isl_local_space_copy(ls
));
6842 isl_space_free(map
->dim
);
6843 map
->dim
= isl_local_space_get_space(ls
);
6847 isl_local_space_free(ls
);
6850 isl_local_space_free(ls
);
6855 /* Compute an explicit representation for the existentially
6856 * quantified variables for which do not know any explicit representation yet.
6858 * We first sort the existentially quantified variables so that the
6859 * existentially quantified variables for which we already have an explicit
6860 * representation are placed before those for which we do not.
6861 * The input dimensions, the output dimensions and the existentially
6862 * quantified variables for which we already have an explicit
6863 * representation are then turned into parameters.
6864 * compute_divs returns a map with the same parameters and
6865 * no input or output dimensions and the dimension specification
6866 * is reset to that of the input, including the existentially quantified
6867 * variables for which we already had an explicit representation.
6869 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6871 struct isl_basic_set
*bset
;
6872 struct isl_set
*set
;
6873 struct isl_map
*map
;
6875 isl_local_space
*ls
;
6882 bmap
= isl_basic_map_sort_divs(bmap
);
6883 bmap
= isl_basic_map_cow(bmap
);
6887 n_known
= isl_basic_map_first_unknown_div(bmap
);
6889 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6891 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6892 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6893 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6894 dim
= isl_space_set_alloc(bmap
->ctx
,
6895 nparam
+ n_in
+ n_out
+ n_known
, 0);
6899 ls
= isl_basic_map_get_local_space(bmap
);
6900 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6901 n_known
, bmap
->n_div
- n_known
);
6903 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6904 swap_div(bmap
, i
- n_known
, i
);
6905 bmap
->n_div
-= n_known
;
6906 bmap
->extra
-= n_known
;
6908 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6909 bset
= (struct isl_basic_set
*)bmap
;
6911 set
= parameter_compute_divs(bset
);
6912 map
= (struct isl_map
*)set
;
6913 map
= replace_space_by_local_space(map
, ls
);
6917 isl_basic_map_free(bmap
);
6921 /* Remove the explicit representation of local variable "div",
6924 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6925 __isl_take isl_basic_map
*bmap
, int div
)
6929 known
= isl_basic_map_div_is_known(bmap
, div
);
6931 return isl_basic_map_free(bmap
);
6935 bmap
= isl_basic_map_cow(bmap
);
6938 isl_int_set_si(bmap
->div
[div
][0], 0);
6942 /* Does local variable "div" of "bmap" have an explicit representation?
6944 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
6947 return isl_bool_error
;
6948 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6949 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6950 "position out of bounds", return isl_bool_error
);
6951 return !isl_int_is_zero(bmap
->div
[div
][0]);
6954 /* Return the position of the first local variable that does not
6955 * have an explicit representation.
6956 * Return the total number of local variables if they all have
6957 * an explicit representation.
6958 * Return -1 on error.
6960 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6967 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6968 if (!isl_basic_map_div_is_known(bmap
, i
))
6974 /* Does "bmap" have an explicit representation for all local variables?
6976 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6980 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
6981 first
= isl_basic_map_first_unknown_div(bmap
);
6983 return isl_bool_error
;
6987 /* Do all basic maps in "map" have an explicit representation
6988 * for all local variables?
6990 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
6995 return isl_bool_error
;
6997 for (i
= 0; i
< map
->n
; ++i
) {
6998 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7003 return isl_bool_true
;
7006 /* If bmap contains any unknown divs, then compute explicit
7007 * expressions for them. However, this computation may be
7008 * quite expensive, so first try to remove divs that aren't
7011 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7014 struct isl_map
*map
;
7016 known
= isl_basic_map_divs_known(bmap
);
7020 return isl_map_from_basic_map(bmap
);
7022 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7024 known
= isl_basic_map_divs_known(bmap
);
7028 return isl_map_from_basic_map(bmap
);
7030 map
= compute_divs(bmap
);
7033 isl_basic_map_free(bmap
);
7037 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7041 struct isl_map
*res
;
7048 known
= isl_map_divs_known(map
);
7056 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7057 for (i
= 1 ; i
< map
->n
; ++i
) {
7059 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7060 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7061 res
= isl_map_union_disjoint(res
, r2
);
7063 res
= isl_map_union(res
, r2
);
7070 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7072 return (struct isl_set
*)
7073 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7076 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7078 return (struct isl_set
*)
7079 isl_map_compute_divs((struct isl_map
*)set
);
7082 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7085 struct isl_set
*set
;
7090 map
= isl_map_cow(map
);
7094 set
= (struct isl_set
*)map
;
7095 set
->dim
= isl_space_domain(set
->dim
);
7098 for (i
= 0; i
< map
->n
; ++i
) {
7099 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7103 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7104 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7111 /* Return the union of "map1" and "map2", where we assume for now that
7112 * "map1" and "map2" are disjoint. Note that the basic maps inside
7113 * "map1" or "map2" may not be disjoint from each other.
7114 * Also note that this function is also called from isl_map_union,
7115 * which takes care of handling the situation where "map1" and "map2"
7116 * may not be disjoint.
7118 * If one of the inputs is empty, we can simply return the other input.
7119 * Similarly, if one of the inputs is universal, then it is equal to the union.
7121 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7122 __isl_take isl_map
*map2
)
7126 struct isl_map
*map
= NULL
;
7132 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7133 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7134 "spaces don't match", goto error
);
7145 is_universe
= isl_map_plain_is_universe(map1
);
7146 if (is_universe
< 0)
7153 is_universe
= isl_map_plain_is_universe(map2
);
7154 if (is_universe
< 0)
7161 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7162 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7163 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7165 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7166 map1
->n
+ map2
->n
, flags
);
7169 for (i
= 0; i
< map1
->n
; ++i
) {
7170 map
= isl_map_add_basic_map(map
,
7171 isl_basic_map_copy(map1
->p
[i
]));
7175 for (i
= 0; i
< map2
->n
; ++i
) {
7176 map
= isl_map_add_basic_map(map
,
7177 isl_basic_map_copy(map2
->p
[i
]));
7191 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7192 * guaranteed to be disjoint by the caller.
7194 * Note that this functions is called from within isl_map_make_disjoint,
7195 * so we have to be careful not to touch the constraints of the inputs
7198 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7199 __isl_take isl_map
*map2
)
7201 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7204 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7205 * not be disjoint. The parameters are assumed to have been aligned.
7207 * We currently simply call map_union_disjoint, the internal operation
7208 * of which does not really depend on the inputs being disjoint.
7209 * If the result contains more than one basic map, then we clear
7210 * the disjoint flag since the result may contain basic maps from
7211 * both inputs and these are not guaranteed to be disjoint.
7213 * As a special case, if "map1" and "map2" are obviously equal,
7214 * then we simply return "map1".
7216 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7217 __isl_take isl_map
*map2
)
7224 equal
= isl_map_plain_is_equal(map1
, map2
);
7232 map1
= map_union_disjoint(map1
, map2
);
7236 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7244 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7247 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7248 __isl_take isl_map
*map2
)
7250 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7253 struct isl_set
*isl_set_union_disjoint(
7254 struct isl_set
*set1
, struct isl_set
*set2
)
7256 return (struct isl_set
*)
7257 isl_map_union_disjoint(
7258 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7261 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7263 return (struct isl_set
*)
7264 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7267 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7270 * "map" and "set" are assumed to be compatible and non-NULL.
7272 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7273 __isl_take isl_set
*set
,
7274 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7275 __isl_take isl_basic_set
*bset
))
7278 struct isl_map
*result
;
7281 if (isl_set_plain_is_universe(set
)) {
7286 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7287 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7288 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7290 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7291 map
->n
* set
->n
, flags
);
7292 for (i
= 0; result
&& i
< map
->n
; ++i
)
7293 for (j
= 0; j
< set
->n
; ++j
) {
7294 result
= isl_map_add_basic_map(result
,
7295 fn(isl_basic_map_copy(map
->p
[i
]),
7296 isl_basic_set_copy(set
->p
[j
])));
7306 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7307 __isl_take isl_set
*set
)
7312 if (!isl_map_compatible_range(map
, set
))
7313 isl_die(set
->ctx
, isl_error_invalid
,
7314 "incompatible spaces", goto error
);
7316 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7323 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7324 __isl_take isl_set
*set
)
7326 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7329 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7330 __isl_take isl_set
*set
)
7335 if (!isl_map_compatible_domain(map
, set
))
7336 isl_die(set
->ctx
, isl_error_invalid
,
7337 "incompatible spaces", goto error
);
7339 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7346 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7347 __isl_take isl_set
*set
)
7349 return isl_map_align_params_map_map_and(map
, set
,
7350 &map_intersect_domain
);
7353 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7354 __isl_take isl_map
*map2
)
7358 map1
= isl_map_reverse(map1
);
7359 map1
= isl_map_apply_range(map1
, map2
);
7360 return isl_map_reverse(map1
);
7367 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7368 __isl_take isl_map
*map2
)
7370 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7373 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7374 __isl_take isl_map
*map2
)
7376 isl_space
*dim_result
;
7377 struct isl_map
*result
;
7383 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7384 isl_space_copy(map2
->dim
));
7386 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7389 for (i
= 0; i
< map1
->n
; ++i
)
7390 for (j
= 0; j
< map2
->n
; ++j
) {
7391 result
= isl_map_add_basic_map(result
,
7392 isl_basic_map_apply_range(
7393 isl_basic_map_copy(map1
->p
[i
]),
7394 isl_basic_map_copy(map2
->p
[j
])));
7400 if (result
&& result
->n
<= 1)
7401 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7409 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7410 __isl_take isl_map
*map2
)
7412 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7416 * returns range - domain
7418 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7420 isl_space
*target_space
;
7421 struct isl_basic_set
*bset
;
7428 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7429 bmap
->dim
, isl_dim_out
),
7431 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7432 dim
= isl_basic_map_n_in(bmap
);
7433 nparam
= isl_basic_map_n_param(bmap
);
7434 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7435 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7436 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7437 for (i
= 0; i
< dim
; ++i
) {
7438 int j
= isl_basic_map_alloc_equality(bmap
);
7440 bmap
= isl_basic_map_free(bmap
);
7443 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7444 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7445 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7446 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7448 bset
= isl_basic_map_domain(bmap
);
7449 bset
= isl_basic_set_reset_space(bset
, target_space
);
7452 isl_basic_map_free(bmap
);
7457 * returns range - domain
7459 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7463 struct isl_set
*result
;
7468 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7469 map
->dim
, isl_dim_out
),
7471 dim
= isl_map_get_space(map
);
7472 dim
= isl_space_domain(dim
);
7473 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7476 for (i
= 0; i
< map
->n
; ++i
)
7477 result
= isl_set_add_basic_set(result
,
7478 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7487 * returns [domain -> range] -> range - domain
7489 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7490 __isl_take isl_basic_map
*bmap
)
7494 isl_basic_map
*domain
;
7498 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7499 bmap
->dim
, isl_dim_out
))
7500 isl_die(bmap
->ctx
, isl_error_invalid
,
7501 "domain and range don't match", goto error
);
7503 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7504 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7506 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7507 domain
= isl_basic_map_universe(dim
);
7509 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7510 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7511 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7513 total
= isl_basic_map_total_dim(bmap
);
7515 for (i
= 0; i
< n
; ++i
) {
7516 k
= isl_basic_map_alloc_equality(bmap
);
7519 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7520 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7521 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7522 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7525 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7526 return isl_basic_map_finalize(bmap
);
7528 isl_basic_map_free(bmap
);
7533 * returns [domain -> range] -> range - domain
7535 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7538 isl_space
*domain_dim
;
7543 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7544 map
->dim
, isl_dim_out
))
7545 isl_die(map
->ctx
, isl_error_invalid
,
7546 "domain and range don't match", goto error
);
7548 map
= isl_map_cow(map
);
7552 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7553 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7554 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7557 for (i
= 0; i
< map
->n
; ++i
) {
7558 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7562 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7569 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7571 struct isl_basic_map
*bmap
;
7579 nparam
= dims
->nparam
;
7581 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7585 for (i
= 0; i
< dim
; ++i
) {
7586 int j
= isl_basic_map_alloc_equality(bmap
);
7589 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7590 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7591 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7593 return isl_basic_map_finalize(bmap
);
7595 isl_basic_map_free(bmap
);
7599 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7603 if (dim
->n_in
!= dim
->n_out
)
7604 isl_die(dim
->ctx
, isl_error_invalid
,
7605 "number of input and output dimensions needs to be "
7606 "the same", goto error
);
7607 return basic_map_identity(dim
);
7609 isl_space_free(dim
);
7613 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7615 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7618 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7620 isl_space
*dim
= isl_set_get_space(set
);
7622 id
= isl_map_identity(isl_space_map_from_set(dim
));
7623 return isl_map_intersect_range(id
, set
);
7626 /* Construct a basic set with all set dimensions having only non-negative
7629 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7630 __isl_take isl_space
*space
)
7635 struct isl_basic_set
*bset
;
7639 nparam
= space
->nparam
;
7641 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7644 for (i
= 0; i
< dim
; ++i
) {
7645 int k
= isl_basic_set_alloc_inequality(bset
);
7648 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7649 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7653 isl_basic_set_free(bset
);
7657 /* Construct the half-space x_pos >= 0.
7659 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7663 isl_basic_set
*nonneg
;
7665 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7666 k
= isl_basic_set_alloc_inequality(nonneg
);
7669 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7670 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7672 return isl_basic_set_finalize(nonneg
);
7674 isl_basic_set_free(nonneg
);
7678 /* Construct the half-space x_pos <= -1.
7680 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7685 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7686 k
= isl_basic_set_alloc_inequality(neg
);
7689 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7690 isl_int_set_si(neg
->ineq
[k
][0], -1);
7691 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7693 return isl_basic_set_finalize(neg
);
7695 isl_basic_set_free(neg
);
7699 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7700 enum isl_dim_type type
, unsigned first
, unsigned n
)
7704 isl_basic_set
*nonneg
;
7712 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7714 offset
= pos(set
->dim
, type
);
7715 for (i
= 0; i
< n
; ++i
) {
7716 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7717 offset
+ first
+ i
);
7718 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7720 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7729 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7730 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7737 if (isl_set_plain_is_empty(set
)) {
7742 return fn(set
, signs
, user
);
7745 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7747 half
= isl_set_intersect(half
, isl_set_copy(set
));
7748 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7752 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7754 half
= isl_set_intersect(half
, set
);
7755 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7761 /* Call "fn" on the intersections of "set" with each of the orthants
7762 * (except for obviously empty intersections). The orthant is identified
7763 * by the signs array, with each entry having value 1 or -1 according
7764 * to the sign of the corresponding variable.
7766 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7767 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7777 if (isl_set_plain_is_empty(set
))
7780 nparam
= isl_set_dim(set
, isl_dim_param
);
7781 nvar
= isl_set_dim(set
, isl_dim_set
);
7783 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7785 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7793 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7795 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7798 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7799 __isl_keep isl_basic_map
*bmap2
)
7802 struct isl_map
*map1
;
7803 struct isl_map
*map2
;
7805 if (!bmap1
|| !bmap2
)
7806 return isl_bool_error
;
7808 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7809 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7811 is_subset
= isl_map_is_subset(map1
, map2
);
7819 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7820 __isl_keep isl_basic_set
*bset2
)
7822 return isl_basic_map_is_subset(bset1
, bset2
);
7825 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7826 __isl_keep isl_basic_map
*bmap2
)
7830 if (!bmap1
|| !bmap2
)
7831 return isl_bool_error
;
7832 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7833 if (is_subset
!= isl_bool_true
)
7835 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7839 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7840 __isl_keep isl_basic_set
*bset2
)
7842 return isl_basic_map_is_equal(
7843 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7846 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7852 return isl_bool_error
;
7853 for (i
= 0; i
< map
->n
; ++i
) {
7854 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7856 return isl_bool_error
;
7858 return isl_bool_false
;
7860 return isl_bool_true
;
7863 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7865 return map
? map
->n
== 0 : isl_bool_error
;
7868 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7870 return set
? set
->n
== 0 : isl_bool_error
;
7873 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7875 return isl_map_is_empty((struct isl_map
*)set
);
7878 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7883 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7886 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7891 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7894 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7899 return isl_bool_error
;
7900 is_subset
= isl_map_is_subset(map1
, map2
);
7901 if (is_subset
!= isl_bool_true
)
7903 is_subset
= isl_map_is_subset(map2
, map1
);
7907 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7909 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7912 isl_bool
isl_basic_map_is_strict_subset(
7913 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7917 if (!bmap1
|| !bmap2
)
7918 return isl_bool_error
;
7919 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7920 if (is_subset
!= isl_bool_true
)
7922 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7923 if (is_subset
== isl_bool_error
)
7928 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7929 __isl_keep isl_map
*map2
)
7934 return isl_bool_error
;
7935 is_subset
= isl_map_is_subset(map1
, map2
);
7936 if (is_subset
!= isl_bool_true
)
7938 is_subset
= isl_map_is_subset(map2
, map1
);
7939 if (is_subset
== isl_bool_error
)
7944 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7945 __isl_keep isl_set
*set2
)
7947 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7950 /* Is "bmap" obviously equal to the universe with the same space?
7952 * That is, does it not have any constraints?
7954 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7957 return isl_bool_error
;
7958 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7961 /* Is "bset" obviously equal to the universe with the same space?
7963 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7965 return isl_basic_map_plain_is_universe(bset
);
7968 /* If "c" does not involve any existentially quantified variables,
7969 * then set *univ to false and abort
7971 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7973 isl_bool
*univ
= user
;
7976 n
= isl_constraint_dim(c
, isl_dim_div
);
7977 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
7978 isl_constraint_free(c
);
7979 if (*univ
< 0 || !*univ
)
7980 return isl_stat_error
;
7984 /* Is "bmap" equal to the universe with the same space?
7986 * First check if it is obviously equal to the universe.
7987 * If not and if there are any constraints not involving
7988 * existentially quantified variables, then it is certainly
7989 * not equal to the universe.
7990 * Otherwise, check if the universe is a subset of "bmap".
7992 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
7995 isl_basic_map
*test
;
7997 univ
= isl_basic_map_plain_is_universe(bmap
);
7998 if (univ
< 0 || univ
)
8000 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8001 return isl_bool_false
;
8002 univ
= isl_bool_true
;
8003 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8005 return isl_bool_error
;
8006 if (univ
< 0 || !univ
)
8008 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8009 univ
= isl_basic_map_is_subset(test
, bmap
);
8010 isl_basic_map_free(test
);
8014 /* Is "bset" equal to the universe with the same space?
8016 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8018 return isl_basic_map_is_universe(bset
);
8021 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8026 return isl_bool_error
;
8028 for (i
= 0; i
< map
->n
; ++i
) {
8029 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8034 return isl_bool_false
;
8037 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8039 return isl_map_plain_is_universe((isl_map
*) set
);
8042 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8044 struct isl_basic_set
*bset
= NULL
;
8045 struct isl_vec
*sample
= NULL
;
8046 isl_bool empty
, non_empty
;
8049 return isl_bool_error
;
8051 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8052 return isl_bool_true
;
8054 if (isl_basic_map_plain_is_universe(bmap
))
8055 return isl_bool_false
;
8057 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8058 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8059 copy
= isl_basic_map_remove_redundancies(copy
);
8060 empty
= isl_basic_map_plain_is_empty(copy
);
8061 isl_basic_map_free(copy
);
8065 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8067 return isl_bool_error
;
8069 return isl_bool_false
;
8070 isl_vec_free(bmap
->sample
);
8071 bmap
->sample
= NULL
;
8072 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8074 return isl_bool_error
;
8075 sample
= isl_basic_set_sample_vec(bset
);
8077 return isl_bool_error
;
8078 empty
= sample
->size
== 0;
8079 isl_vec_free(bmap
->sample
);
8080 bmap
->sample
= sample
;
8082 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8087 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8090 return isl_bool_error
;
8091 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8094 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8097 return isl_bool_error
;
8098 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8101 /* Is "bmap" known to be non-empty?
8103 * That is, is the cached sample still valid?
8105 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8110 return isl_bool_error
;
8112 return isl_bool_false
;
8113 total
= 1 + isl_basic_map_total_dim(bmap
);
8114 if (bmap
->sample
->size
!= total
)
8115 return isl_bool_false
;
8116 return isl_basic_map_contains(bmap
, bmap
->sample
);
8119 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8121 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8124 struct isl_map
*isl_basic_map_union(
8125 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8127 struct isl_map
*map
;
8128 if (!bmap1
|| !bmap2
)
8131 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8133 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8136 map
= isl_map_add_basic_map(map
, bmap1
);
8137 map
= isl_map_add_basic_map(map
, bmap2
);
8140 isl_basic_map_free(bmap1
);
8141 isl_basic_map_free(bmap2
);
8145 struct isl_set
*isl_basic_set_union(
8146 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8148 return (struct isl_set
*)isl_basic_map_union(
8149 (struct isl_basic_map
*)bset1
,
8150 (struct isl_basic_map
*)bset2
);
8153 /* Order divs such that any div only depends on previous divs */
8154 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8162 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8164 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8166 if (isl_int_is_zero(bmap
->div
[i
][0]))
8168 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8173 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8174 "integer division depends on itself",
8175 return isl_basic_map_free(bmap
));
8176 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8182 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8184 return (struct isl_basic_set
*)
8185 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8188 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8195 for (i
= 0; i
< map
->n
; ++i
) {
8196 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8207 /* Apply the expansion computed by isl_merge_divs.
8208 * The expansion itself is given by "exp" while the resulting
8209 * list of divs is given by "div".
8211 * Move the integer divisions of "bset" into the right position
8212 * according to "exp" and then introduce the additional integer
8213 * divisions, adding div constraints.
8214 * The moving should be done first to avoid moving coefficients
8215 * in the definitions of the extra integer divisions.
8217 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8218 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8223 bset
= isl_basic_set_cow(bset
);
8227 if (div
->n_row
< bset
->n_div
)
8228 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8229 "not an expansion", goto error
);
8231 n_div
= bset
->n_div
;
8232 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8233 div
->n_row
- n_div
, 0,
8234 2 * (div
->n_row
- n_div
));
8236 for (i
= n_div
; i
< div
->n_row
; ++i
)
8237 if (isl_basic_set_alloc_div(bset
) < 0)
8240 for (j
= n_div
- 1; j
>= 0; --j
) {
8243 isl_basic_map_swap_div(bset
, j
, exp
[j
]);
8246 for (i
= 0; i
< div
->n_row
; ++i
) {
8247 if (j
< n_div
&& exp
[j
] == i
) {
8250 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8251 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8259 isl_basic_set_free(bset
);
8264 /* Look for a div in dst that corresponds to the div "div" in src.
8265 * The divs before "div" in src and dst are assumed to be the same.
8267 * Returns -1 if no corresponding div was found and the position
8268 * of the corresponding div in dst otherwise.
8270 static int find_div(struct isl_basic_map
*dst
,
8271 struct isl_basic_map
*src
, unsigned div
)
8275 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8277 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8278 for (i
= div
; i
< dst
->n_div
; ++i
)
8279 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8280 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8281 dst
->n_div
- div
) == -1)
8286 /* Align the divs of "dst" to those of "src", adding divs from "src"
8287 * if needed. That is, make sure that the first src->n_div divs
8288 * of the result are equal to those of src.
8290 * The result is not finalized as by design it will have redundant
8291 * divs if any divs from "src" were copied.
8293 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8294 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8297 int known
, extended
;
8301 return isl_basic_map_free(dst
);
8303 if (src
->n_div
== 0)
8306 known
= isl_basic_map_divs_known(src
);
8308 return isl_basic_map_free(dst
);
8310 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8311 "some src divs are unknown",
8312 return isl_basic_map_free(dst
));
8314 src
= isl_basic_map_order_divs(src
);
8317 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8318 for (i
= 0; i
< src
->n_div
; ++i
) {
8319 int j
= find_div(dst
, src
, i
);
8322 int extra
= src
->n_div
- i
;
8323 dst
= isl_basic_map_cow(dst
);
8326 dst
= isl_basic_map_extend_space(dst
,
8327 isl_space_copy(dst
->dim
),
8328 extra
, 0, 2 * extra
);
8331 j
= isl_basic_map_alloc_div(dst
);
8333 return isl_basic_map_free(dst
);
8334 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8335 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8336 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8337 return isl_basic_map_free(dst
);
8340 isl_basic_map_swap_div(dst
, i
, j
);
8345 struct isl_basic_set
*isl_basic_set_align_divs(
8346 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8348 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8349 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8352 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8360 map
= isl_map_compute_divs(map
);
8361 map
= isl_map_cow(map
);
8365 for (i
= 1; i
< map
->n
; ++i
)
8366 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8367 for (i
= 1; i
< map
->n
; ++i
) {
8368 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8370 return isl_map_free(map
);
8373 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8377 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8379 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8382 /* Align the divs of the basic maps in "map" to those
8383 * of the basic maps in "list", as well as to the other basic maps in "map".
8384 * The elements in "list" are assumed to have known divs.
8386 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8387 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8391 map
= isl_map_compute_divs(map
);
8392 map
= isl_map_cow(map
);
8394 return isl_map_free(map
);
8398 n
= isl_basic_map_list_n_basic_map(list
);
8399 for (i
= 0; i
< n
; ++i
) {
8400 isl_basic_map
*bmap
;
8402 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8403 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8404 isl_basic_map_free(bmap
);
8407 return isl_map_free(map
);
8409 return isl_map_align_divs(map
);
8412 /* Align the divs of each element of "list" to those of "bmap".
8413 * Both "bmap" and the elements of "list" are assumed to have known divs.
8415 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8416 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8421 return isl_basic_map_list_free(list
);
8423 n
= isl_basic_map_list_n_basic_map(list
);
8424 for (i
= 0; i
< n
; ++i
) {
8425 isl_basic_map
*bmap_i
;
8427 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8428 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8429 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8435 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8436 __isl_take isl_map
*map
)
8440 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8441 map
= isl_map_intersect_domain(map
, set
);
8442 set
= isl_map_range(map
);
8450 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8451 __isl_take isl_map
*map
)
8453 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8456 /* There is no need to cow as removing empty parts doesn't change
8457 * the meaning of the set.
8459 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8466 for (i
= map
->n
- 1; i
>= 0; --i
)
8467 remove_if_empty(map
, i
);
8472 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8474 return (struct isl_set
*)
8475 isl_map_remove_empty_parts((struct isl_map
*)set
);
8478 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8480 struct isl_basic_map
*bmap
;
8481 if (!map
|| map
->n
== 0)
8483 bmap
= map
->p
[map
->n
-1];
8484 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8485 return isl_basic_map_copy(bmap
);
8488 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8490 return (struct isl_basic_set
*)
8491 isl_map_copy_basic_map((struct isl_map
*)set
);
8494 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8495 __isl_keep isl_basic_map
*bmap
)
8501 for (i
= map
->n
-1; i
>= 0; --i
) {
8502 if (map
->p
[i
] != bmap
)
8504 map
= isl_map_cow(map
);
8507 isl_basic_map_free(map
->p
[i
]);
8508 if (i
!= map
->n
-1) {
8509 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8510 map
->p
[i
] = map
->p
[map
->n
-1];
8521 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8522 struct isl_basic_set
*bset
)
8524 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8525 (struct isl_basic_map
*)bset
);
8528 /* Given two basic sets bset1 and bset2, compute the maximal difference
8529 * between the values of dimension pos in bset1 and those in bset2
8530 * for any common value of the parameters and dimensions preceding pos.
8532 static enum isl_lp_result
basic_set_maximal_difference_at(
8533 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8534 int pos
, isl_int
*opt
)
8537 struct isl_basic_map
*bmap1
= NULL
;
8538 struct isl_basic_map
*bmap2
= NULL
;
8539 struct isl_ctx
*ctx
;
8540 struct isl_vec
*obj
;
8543 unsigned dim1
, dim2
;
8544 enum isl_lp_result res
;
8546 if (!bset1
|| !bset2
)
8547 return isl_lp_error
;
8549 nparam
= isl_basic_set_n_param(bset1
);
8550 dim1
= isl_basic_set_n_dim(bset1
);
8551 dim2
= isl_basic_set_n_dim(bset2
);
8552 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8553 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8554 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8555 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8556 if (!bmap1
|| !bmap2
)
8558 bmap1
= isl_basic_map_cow(bmap1
);
8559 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8560 pos
, (dim1
- pos
) + (dim2
- pos
),
8561 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8562 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8565 total
= isl_basic_map_total_dim(bmap1
);
8567 obj
= isl_vec_alloc(ctx
, 1 + total
);
8570 isl_seq_clr(obj
->block
.data
, 1 + total
);
8571 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8572 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8573 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8575 isl_basic_map_free(bmap1
);
8579 isl_basic_map_free(bmap2
);
8581 isl_basic_map_free(bmap1
);
8582 return isl_lp_error
;
8585 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8586 * for any common value of the parameters and dimensions preceding pos
8587 * in both basic sets, the values of dimension pos in bset1 are
8588 * smaller or larger than those in bset2.
8591 * 1 if bset1 follows bset2
8592 * -1 if bset1 precedes bset2
8593 * 0 if bset1 and bset2 are incomparable
8594 * -2 if some error occurred.
8596 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8597 struct isl_basic_set
*bset2
, int pos
)
8600 enum isl_lp_result res
;
8605 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8607 if (res
== isl_lp_empty
)
8609 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8610 res
== isl_lp_unbounded
)
8612 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8621 /* Given two basic sets bset1 and bset2, check whether
8622 * for any common value of the parameters and dimensions preceding pos
8623 * there is a value of dimension pos in bset1 that is larger
8624 * than a value of the same dimension in bset2.
8627 * 1 if there exists such a pair
8628 * 0 if there is no such pair, but there is a pair of equal values
8630 * -2 if some error occurred.
8632 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8633 __isl_keep isl_basic_set
*bset2
, int pos
)
8636 enum isl_lp_result res
;
8641 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8643 if (res
== isl_lp_empty
)
8645 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8646 res
== isl_lp_unbounded
)
8648 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8650 else if (res
== isl_lp_ok
)
8659 /* Given two sets set1 and set2, check whether
8660 * for any common value of the parameters and dimensions preceding pos
8661 * there is a value of dimension pos in set1 that is larger
8662 * than a value of the same dimension in set2.
8665 * 1 if there exists such a pair
8666 * 0 if there is no such pair, but there is a pair of equal values
8668 * -2 if some error occurred.
8670 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8671 __isl_keep isl_set
*set2
, int pos
)
8679 for (i
= 0; i
< set1
->n
; ++i
)
8680 for (j
= 0; j
< set2
->n
; ++j
) {
8682 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8683 if (f
== 1 || f
== -2)
8692 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8693 unsigned pos
, isl_int
*val
)
8701 total
= isl_basic_map_total_dim(bmap
);
8702 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8703 for (; d
+1 > pos
; --d
)
8704 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8708 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8710 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8712 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8715 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8721 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8722 unsigned pos
, isl_int
*val
)
8734 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8737 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8738 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8739 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8740 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8744 isl_int_set(*val
, v
);
8750 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8751 unsigned pos
, isl_int
*val
)
8753 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8757 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8760 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8763 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8764 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8766 if (pos
>= isl_basic_map_dim(bmap
, type
))
8768 return isl_basic_map_plain_has_fixed_var(bmap
,
8769 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8772 /* If "bmap" obviously lies on a hyperplane where the given dimension
8773 * has a fixed value, then return that value.
8774 * Otherwise return NaN.
8776 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8777 __isl_keep isl_basic_map
*bmap
,
8778 enum isl_dim_type type
, unsigned pos
)
8786 ctx
= isl_basic_map_get_ctx(bmap
);
8787 v
= isl_val_alloc(ctx
);
8790 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8792 return isl_val_free(v
);
8794 isl_int_set_si(v
->d
, 1);
8798 return isl_val_nan(ctx
);
8801 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8802 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8804 if (pos
>= isl_map_dim(map
, type
))
8806 return isl_map_plain_has_fixed_var(map
,
8807 map_offset(map
, type
) - 1 + pos
, val
);
8810 /* If "map" obviously lies on a hyperplane where the given dimension
8811 * has a fixed value, then return that value.
8812 * Otherwise return NaN.
8814 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8815 enum isl_dim_type type
, unsigned pos
)
8823 ctx
= isl_map_get_ctx(map
);
8824 v
= isl_val_alloc(ctx
);
8827 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8829 return isl_val_free(v
);
8831 isl_int_set_si(v
->d
, 1);
8835 return isl_val_nan(ctx
);
8838 /* If "set" obviously lies on a hyperplane where the given dimension
8839 * has a fixed value, then return that value.
8840 * Otherwise return NaN.
8842 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8843 enum isl_dim_type type
, unsigned pos
)
8845 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8848 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8849 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8851 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8854 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8855 * then return this fixed value in *val.
8857 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8858 unsigned dim
, isl_int
*val
)
8860 return isl_basic_set_plain_has_fixed_var(bset
,
8861 isl_basic_set_n_param(bset
) + dim
, val
);
8864 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8865 * then return this fixed value in *val.
8867 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8868 unsigned dim
, isl_int
*val
)
8870 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8873 /* Check if input variable in has fixed value and if so and if val is not NULL,
8874 * then return this fixed value in *val.
8876 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8877 unsigned in
, isl_int
*val
)
8879 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8882 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8883 * and if val is not NULL, then return this lower bound in *val.
8885 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8886 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8888 int i
, i_eq
= -1, i_ineq
= -1;
8895 total
= isl_basic_set_total_dim(bset
);
8896 nparam
= isl_basic_set_n_param(bset
);
8897 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8898 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8904 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8905 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8907 if (i_eq
!= -1 || i_ineq
!= -1)
8911 if (i_eq
== -1 && i_ineq
== -1)
8913 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8914 /* The coefficient should always be one due to normalization. */
8915 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8917 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8919 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8920 total
- nparam
- dim
- 1) != -1)
8923 isl_int_neg(*val
, c
[0]);
8927 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8928 unsigned dim
, isl_int
*val
)
8940 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8944 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8946 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8947 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8949 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8953 isl_int_set(*val
, v
);
8959 /* Return -1 if the constraint "c1" should be sorted before "c2"
8960 * and 1 if it should be sorted after "c2".
8961 * Return 0 if the two constraints are the same (up to the constant term).
8963 * In particular, if a constraint involves later variables than another
8964 * then it is sorted after this other constraint.
8965 * uset_gist depends on constraints without existentially quantified
8966 * variables sorting first.
8968 * For constraints that have the same latest variable, those
8969 * with the same coefficient for this latest variable (first in absolute value
8970 * and then in actual value) are grouped together.
8971 * This is useful for detecting pairs of constraints that can
8972 * be chained in their printed representation.
8974 * Finally, within a group, constraints are sorted according to
8975 * their coefficients (excluding the constant term).
8977 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8979 isl_int
**c1
= (isl_int
**) p1
;
8980 isl_int
**c2
= (isl_int
**) p2
;
8982 unsigned size
= *(unsigned *) arg
;
8985 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8986 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8991 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8994 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8998 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9001 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9002 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9003 * and 0 if the two constraints are the same (up to the constant term).
9005 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9006 isl_int
*c1
, isl_int
*c2
)
9012 total
= isl_basic_map_total_dim(bmap
);
9013 return sort_constraint_cmp(&c1
, &c2
, &total
);
9016 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9017 __isl_take isl_basic_map
*bmap
)
9023 if (bmap
->n_ineq
== 0)
9025 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9027 total
= isl_basic_map_total_dim(bmap
);
9028 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9029 &sort_constraint_cmp
, &total
) < 0)
9030 return isl_basic_map_free(bmap
);
9034 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9035 __isl_take isl_basic_set
*bset
)
9037 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9038 (struct isl_basic_map
*)bset
);
9041 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9045 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9047 bmap
= isl_basic_map_remove_redundancies(bmap
);
9048 bmap
= isl_basic_map_sort_constraints(bmap
);
9050 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9054 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9056 return (struct isl_basic_set
*)isl_basic_map_normalize(
9057 (struct isl_basic_map
*)bset
);
9060 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9061 const __isl_keep isl_basic_map
*bmap2
)
9066 if (!bmap1
|| !bmap2
)
9071 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9072 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9073 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9074 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9075 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9076 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9077 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9078 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9079 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9080 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9081 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9083 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9085 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9087 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9088 return bmap1
->n_eq
- bmap2
->n_eq
;
9089 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9090 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9091 if (bmap1
->n_div
!= bmap2
->n_div
)
9092 return bmap1
->n_div
- bmap2
->n_div
;
9093 total
= isl_basic_map_total_dim(bmap1
);
9094 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9095 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9099 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9100 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9104 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9105 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9112 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9113 const __isl_keep isl_basic_set
*bset2
)
9115 return isl_basic_map_plain_cmp(bset1
, bset2
);
9118 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9124 if (set1
->n
!= set2
->n
)
9125 return set1
->n
- set2
->n
;
9127 for (i
= 0; i
< set1
->n
; ++i
) {
9128 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9136 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9137 __isl_keep isl_basic_map
*bmap2
)
9139 if (!bmap1
|| !bmap2
)
9140 return isl_bool_error
;
9141 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9144 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9145 __isl_keep isl_basic_set
*bset2
)
9147 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9148 (isl_basic_map
*)bset2
);
9151 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9153 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9154 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9156 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9159 /* Sort the basic maps of "map" and remove duplicate basic maps.
9161 * While removing basic maps, we make sure that the basic maps remain
9162 * sorted because isl_map_normalize expects the basic maps of the result
9165 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9169 map
= isl_map_remove_empty_parts(map
);
9172 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9173 for (i
= map
->n
- 1; i
>= 1; --i
) {
9174 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9176 isl_basic_map_free(map
->p
[i
-1]);
9177 for (j
= i
; j
< map
->n
; ++j
)
9178 map
->p
[j
- 1] = map
->p
[j
];
9185 /* Remove obvious duplicates among the basic maps of "map".
9187 * Unlike isl_map_normalize, this function does not remove redundant
9188 * constraints and only removes duplicates that have exactly the same
9189 * constraints in the input. It does sort the constraints and
9190 * the basic maps to ease the detection of duplicates.
9192 * If "map" has already been normalized or if the basic maps are
9193 * disjoint, then there can be no duplicates.
9195 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9198 isl_basic_map
*bmap
;
9204 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9206 for (i
= 0; i
< map
->n
; ++i
) {
9207 bmap
= isl_basic_map_copy(map
->p
[i
]);
9208 bmap
= isl_basic_map_sort_constraints(bmap
);
9210 return isl_map_free(map
);
9211 isl_basic_map_free(map
->p
[i
]);
9215 map
= sort_and_remove_duplicates(map
);
9219 /* We normalize in place, but if anything goes wrong we need
9220 * to return NULL, so we need to make sure we don't change the
9221 * meaning of any possible other copies of map.
9223 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9226 struct isl_basic_map
*bmap
;
9230 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9232 for (i
= 0; i
< map
->n
; ++i
) {
9233 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9236 isl_basic_map_free(map
->p
[i
]);
9240 map
= sort_and_remove_duplicates(map
);
9242 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9249 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9251 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9254 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9255 __isl_keep isl_map
*map2
)
9261 return isl_bool_error
;
9264 return isl_bool_true
;
9265 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9266 return isl_bool_false
;
9268 map1
= isl_map_copy(map1
);
9269 map2
= isl_map_copy(map2
);
9270 map1
= isl_map_normalize(map1
);
9271 map2
= isl_map_normalize(map2
);
9274 equal
= map1
->n
== map2
->n
;
9275 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9276 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9286 return isl_bool_error
;
9289 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9290 __isl_keep isl_set
*set2
)
9292 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9293 (struct isl_map
*)set2
);
9296 /* Return an interval that ranges from min to max (inclusive)
9298 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9299 isl_int min
, isl_int max
)
9302 struct isl_basic_set
*bset
= NULL
;
9304 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9308 k
= isl_basic_set_alloc_inequality(bset
);
9311 isl_int_set_si(bset
->ineq
[k
][1], 1);
9312 isl_int_neg(bset
->ineq
[k
][0], min
);
9314 k
= isl_basic_set_alloc_inequality(bset
);
9317 isl_int_set_si(bset
->ineq
[k
][1], -1);
9318 isl_int_set(bset
->ineq
[k
][0], max
);
9322 isl_basic_set_free(bset
);
9326 /* Return the basic maps in "map" as a list.
9328 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9329 __isl_keep isl_map
*map
)
9333 isl_basic_map_list
*list
;
9337 ctx
= isl_map_get_ctx(map
);
9338 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9340 for (i
= 0; i
< map
->n
; ++i
) {
9341 isl_basic_map
*bmap
;
9343 bmap
= isl_basic_map_copy(map
->p
[i
]);
9344 list
= isl_basic_map_list_add(list
, bmap
);
9350 /* Return the intersection of the elements in the non-empty list "list".
9351 * All elements are assumed to live in the same space.
9353 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9354 __isl_take isl_basic_map_list
*list
)
9357 isl_basic_map
*bmap
;
9361 n
= isl_basic_map_list_n_basic_map(list
);
9363 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9364 "expecting non-empty list", goto error
);
9366 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9367 for (i
= 1; i
< n
; ++i
) {
9368 isl_basic_map
*bmap_i
;
9370 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9371 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9374 isl_basic_map_list_free(list
);
9377 isl_basic_map_list_free(list
);
9381 /* Return the intersection of the elements in the non-empty list "list".
9382 * All elements are assumed to live in the same space.
9384 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9385 __isl_take isl_basic_set_list
*list
)
9387 return isl_basic_map_list_intersect(list
);
9390 /* Return the union of the elements in the non-empty list "list".
9391 * All elements are assumed to live in the same space.
9393 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9400 n
= isl_set_list_n_set(list
);
9402 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9403 "expecting non-empty list", goto error
);
9405 set
= isl_set_list_get_set(list
, 0);
9406 for (i
= 1; i
< n
; ++i
) {
9409 set_i
= isl_set_list_get_set(list
, i
);
9410 set
= isl_set_union(set
, set_i
);
9413 isl_set_list_free(list
);
9416 isl_set_list_free(list
);
9420 /* Return the Cartesian product of the basic sets in list (in the given order).
9422 __isl_give isl_basic_set
*isl_basic_set_list_product(
9423 __isl_take
struct isl_basic_set_list
*list
)
9431 struct isl_basic_set
*product
= NULL
;
9435 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9436 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9437 nparam
= isl_basic_set_n_param(list
->p
[0]);
9438 dim
= isl_basic_set_n_dim(list
->p
[0]);
9439 extra
= list
->p
[0]->n_div
;
9440 n_eq
= list
->p
[0]->n_eq
;
9441 n_ineq
= list
->p
[0]->n_ineq
;
9442 for (i
= 1; i
< list
->n
; ++i
) {
9443 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9444 isl_assert(list
->ctx
,
9445 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9446 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9447 extra
+= list
->p
[i
]->n_div
;
9448 n_eq
+= list
->p
[i
]->n_eq
;
9449 n_ineq
+= list
->p
[i
]->n_ineq
;
9451 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9456 for (i
= 0; i
< list
->n
; ++i
) {
9457 isl_basic_set_add_constraints(product
,
9458 isl_basic_set_copy(list
->p
[i
]), dim
);
9459 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9461 isl_basic_set_list_free(list
);
9464 isl_basic_set_free(product
);
9465 isl_basic_set_list_free(list
);
9469 struct isl_basic_map
*isl_basic_map_product(
9470 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9472 isl_space
*dim_result
= NULL
;
9473 struct isl_basic_map
*bmap
;
9474 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9475 struct isl_dim_map
*dim_map1
, *dim_map2
;
9477 if (!bmap1
|| !bmap2
)
9480 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9481 bmap2
->dim
, isl_dim_param
), goto error
);
9482 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9483 isl_space_copy(bmap2
->dim
));
9485 in1
= isl_basic_map_n_in(bmap1
);
9486 in2
= isl_basic_map_n_in(bmap2
);
9487 out1
= isl_basic_map_n_out(bmap1
);
9488 out2
= isl_basic_map_n_out(bmap2
);
9489 nparam
= isl_basic_map_n_param(bmap1
);
9491 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9492 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9493 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9494 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9495 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9496 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9497 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9498 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9499 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9500 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9501 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9503 bmap
= isl_basic_map_alloc_space(dim_result
,
9504 bmap1
->n_div
+ bmap2
->n_div
,
9505 bmap1
->n_eq
+ bmap2
->n_eq
,
9506 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9507 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9508 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9509 bmap
= isl_basic_map_simplify(bmap
);
9510 return isl_basic_map_finalize(bmap
);
9512 isl_basic_map_free(bmap1
);
9513 isl_basic_map_free(bmap2
);
9517 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9518 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9520 isl_basic_map
*prod
;
9522 prod
= isl_basic_map_product(bmap1
, bmap2
);
9523 prod
= isl_basic_map_flatten(prod
);
9527 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9528 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9530 return isl_basic_map_flat_range_product(bset1
, bset2
);
9533 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9534 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9536 isl_space
*space_result
= NULL
;
9537 isl_basic_map
*bmap
;
9538 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9539 struct isl_dim_map
*dim_map1
, *dim_map2
;
9541 if (!bmap1
|| !bmap2
)
9544 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9545 isl_space_copy(bmap2
->dim
));
9547 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9548 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9549 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9550 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9552 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9553 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9554 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9555 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9556 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9557 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9558 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9559 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9560 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9561 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9562 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9564 bmap
= isl_basic_map_alloc_space(space_result
,
9565 bmap1
->n_div
+ bmap2
->n_div
,
9566 bmap1
->n_eq
+ bmap2
->n_eq
,
9567 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9568 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9569 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9570 bmap
= isl_basic_map_simplify(bmap
);
9571 return isl_basic_map_finalize(bmap
);
9573 isl_basic_map_free(bmap1
);
9574 isl_basic_map_free(bmap2
);
9578 __isl_give isl_basic_map
*isl_basic_map_range_product(
9579 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9581 isl_space
*dim_result
= NULL
;
9582 isl_basic_map
*bmap
;
9583 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9584 struct isl_dim_map
*dim_map1
, *dim_map2
;
9586 if (!bmap1
|| !bmap2
)
9589 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9590 bmap2
->dim
, isl_dim_param
))
9591 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9592 "parameters don't match", goto error
);
9594 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9595 isl_space_copy(bmap2
->dim
));
9597 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9598 out1
= isl_basic_map_n_out(bmap1
);
9599 out2
= isl_basic_map_n_out(bmap2
);
9600 nparam
= isl_basic_map_n_param(bmap1
);
9602 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9603 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9604 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9605 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9606 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9607 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9608 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9609 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9610 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9611 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9612 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9614 bmap
= isl_basic_map_alloc_space(dim_result
,
9615 bmap1
->n_div
+ bmap2
->n_div
,
9616 bmap1
->n_eq
+ bmap2
->n_eq
,
9617 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9618 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9619 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9620 bmap
= isl_basic_map_simplify(bmap
);
9621 return isl_basic_map_finalize(bmap
);
9623 isl_basic_map_free(bmap1
);
9624 isl_basic_map_free(bmap2
);
9628 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9629 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9631 isl_basic_map
*prod
;
9633 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9634 prod
= isl_basic_map_flatten_range(prod
);
9638 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9639 * and collect the results.
9640 * The result live in the space obtained by calling "space_product"
9641 * on the spaces of "map1" and "map2".
9642 * If "remove_duplicates" is set then the result may contain duplicates
9643 * (even if the inputs do not) and so we try and remove the obvious
9646 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9647 __isl_take isl_map
*map2
,
9648 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9649 __isl_take isl_space
*right
),
9650 __isl_give isl_basic_map
*(*basic_map_product
)(
9651 __isl_take isl_basic_map
*left
,
9652 __isl_take isl_basic_map
*right
),
9653 int remove_duplicates
)
9656 struct isl_map
*result
;
9662 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9663 map2
->dim
, isl_dim_param
), goto error
);
9665 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9666 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9667 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9669 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9670 isl_space_copy(map2
->dim
)),
9671 map1
->n
* map2
->n
, flags
);
9674 for (i
= 0; i
< map1
->n
; ++i
)
9675 for (j
= 0; j
< map2
->n
; ++j
) {
9676 struct isl_basic_map
*part
;
9677 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9678 isl_basic_map_copy(map2
->p
[j
]));
9679 if (isl_basic_map_is_empty(part
))
9680 isl_basic_map_free(part
);
9682 result
= isl_map_add_basic_map(result
, part
);
9686 if (remove_duplicates
)
9687 result
= isl_map_remove_obvious_duplicates(result
);
9697 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9699 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9700 __isl_take isl_map
*map2
)
9702 return map_product(map1
, map2
, &isl_space_product
,
9703 &isl_basic_map_product
, 0);
9706 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9707 __isl_take isl_map
*map2
)
9709 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9712 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9714 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9715 __isl_take isl_map
*map2
)
9719 prod
= isl_map_product(map1
, map2
);
9720 prod
= isl_map_flatten(prod
);
9724 /* Given two set A and B, construct its Cartesian product A x B.
9726 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9728 return isl_map_range_product(set1
, set2
);
9731 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9732 __isl_take isl_set
*set2
)
9734 return isl_map_flat_range_product(set1
, set2
);
9737 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9739 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9740 __isl_take isl_map
*map2
)
9742 return map_product(map1
, map2
, &isl_space_domain_product
,
9743 &isl_basic_map_domain_product
, 1);
9746 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9748 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9749 __isl_take isl_map
*map2
)
9751 return map_product(map1
, map2
, &isl_space_range_product
,
9752 &isl_basic_map_range_product
, 1);
9755 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9756 __isl_take isl_map
*map2
)
9758 return isl_map_align_params_map_map_and(map1
, map2
,
9759 &map_domain_product_aligned
);
9762 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9763 __isl_take isl_map
*map2
)
9765 return isl_map_align_params_map_map_and(map1
, map2
,
9766 &map_range_product_aligned
);
9769 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9771 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9774 int total1
, keep1
, total2
, keep2
;
9778 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9779 !isl_space_range_is_wrapping(map
->dim
))
9780 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9781 "not a product", return isl_map_free(map
));
9783 space
= isl_map_get_space(map
);
9784 total1
= isl_space_dim(space
, isl_dim_in
);
9785 total2
= isl_space_dim(space
, isl_dim_out
);
9786 space
= isl_space_factor_domain(space
);
9787 keep1
= isl_space_dim(space
, isl_dim_in
);
9788 keep2
= isl_space_dim(space
, isl_dim_out
);
9789 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9790 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9791 map
= isl_map_reset_space(map
, space
);
9796 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9798 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9801 int total1
, keep1
, total2
, keep2
;
9805 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9806 !isl_space_range_is_wrapping(map
->dim
))
9807 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9808 "not a product", return isl_map_free(map
));
9810 space
= isl_map_get_space(map
);
9811 total1
= isl_space_dim(space
, isl_dim_in
);
9812 total2
= isl_space_dim(space
, isl_dim_out
);
9813 space
= isl_space_factor_range(space
);
9814 keep1
= isl_space_dim(space
, isl_dim_in
);
9815 keep2
= isl_space_dim(space
, isl_dim_out
);
9816 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9817 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9818 map
= isl_map_reset_space(map
, space
);
9823 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9825 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9832 if (!isl_space_domain_is_wrapping(map
->dim
))
9833 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9834 "domain is not a product", return isl_map_free(map
));
9836 space
= isl_map_get_space(map
);
9837 total
= isl_space_dim(space
, isl_dim_in
);
9838 space
= isl_space_domain_factor_domain(space
);
9839 keep
= isl_space_dim(space
, isl_dim_in
);
9840 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9841 map
= isl_map_reset_space(map
, space
);
9846 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9848 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9855 if (!isl_space_domain_is_wrapping(map
->dim
))
9856 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9857 "domain is not a product", return isl_map_free(map
));
9859 space
= isl_map_get_space(map
);
9860 total
= isl_space_dim(space
, isl_dim_in
);
9861 space
= isl_space_domain_factor_range(space
);
9862 keep
= isl_space_dim(space
, isl_dim_in
);
9863 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9864 map
= isl_map_reset_space(map
, space
);
9869 /* Given a map A -> [B -> C], extract the map A -> B.
9871 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9878 if (!isl_space_range_is_wrapping(map
->dim
))
9879 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9880 "range is not a product", return isl_map_free(map
));
9882 space
= isl_map_get_space(map
);
9883 total
= isl_space_dim(space
, isl_dim_out
);
9884 space
= isl_space_range_factor_domain(space
);
9885 keep
= isl_space_dim(space
, isl_dim_out
);
9886 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9887 map
= isl_map_reset_space(map
, space
);
9892 /* Given a map A -> [B -> C], extract the map A -> C.
9894 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9901 if (!isl_space_range_is_wrapping(map
->dim
))
9902 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9903 "range is not a product", return isl_map_free(map
));
9905 space
= isl_map_get_space(map
);
9906 total
= isl_space_dim(space
, isl_dim_out
);
9907 space
= isl_space_range_factor_range(space
);
9908 keep
= isl_space_dim(space
, isl_dim_out
);
9909 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9910 map
= isl_map_reset_space(map
, space
);
9915 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9917 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9918 __isl_take isl_map
*map2
)
9922 prod
= isl_map_domain_product(map1
, map2
);
9923 prod
= isl_map_flatten_domain(prod
);
9927 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9929 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9930 __isl_take isl_map
*map2
)
9934 prod
= isl_map_range_product(map1
, map2
);
9935 prod
= isl_map_flatten_range(prod
);
9939 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9942 uint32_t hash
= isl_hash_init();
9947 bmap
= isl_basic_map_copy(bmap
);
9948 bmap
= isl_basic_map_normalize(bmap
);
9951 total
= isl_basic_map_total_dim(bmap
);
9952 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9953 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9955 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9956 isl_hash_hash(hash
, c_hash
);
9958 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9959 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9961 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9962 isl_hash_hash(hash
, c_hash
);
9964 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9965 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9967 if (isl_int_is_zero(bmap
->div
[i
][0]))
9969 isl_hash_byte(hash
, i
& 0xFF);
9970 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9971 isl_hash_hash(hash
, c_hash
);
9973 isl_basic_map_free(bmap
);
9977 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9979 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9982 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9989 map
= isl_map_copy(map
);
9990 map
= isl_map_normalize(map
);
9994 hash
= isl_hash_init();
9995 for (i
= 0; i
< map
->n
; ++i
) {
9997 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9998 isl_hash_hash(hash
, bmap_hash
);
10006 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10008 return isl_map_get_hash((isl_map
*)set
);
10011 /* Check if the value for dimension dim is completely determined
10012 * by the values of the other parameters and variables.
10013 * That is, check if dimension dim is involved in an equality.
10015 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10022 nparam
= isl_basic_set_n_param(bset
);
10023 for (i
= 0; i
< bset
->n_eq
; ++i
)
10024 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10029 /* Check if the value for dimension dim is completely determined
10030 * by the values of the other parameters and variables.
10031 * That is, check if dimension dim is involved in an equality
10032 * for each of the subsets.
10034 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10040 for (i
= 0; i
< set
->n
; ++i
) {
10042 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10049 /* Return the number of basic maps in the (current) representation of "map".
10051 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10053 return map
? map
->n
: 0;
10056 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10058 return set
? set
->n
: 0;
10061 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10062 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10067 return isl_stat_error
;
10069 for (i
= 0; i
< map
->n
; ++i
)
10070 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10071 return isl_stat_error
;
10073 return isl_stat_ok
;
10076 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10077 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10082 return isl_stat_error
;
10084 for (i
= 0; i
< set
->n
; ++i
)
10085 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10086 return isl_stat_error
;
10088 return isl_stat_ok
;
10091 /* Return a list of basic sets, the union of which is equal to "set".
10093 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10094 __isl_keep isl_set
*set
)
10097 isl_basic_set_list
*list
;
10102 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10103 for (i
= 0; i
< set
->n
; ++i
) {
10104 isl_basic_set
*bset
;
10106 bset
= isl_basic_set_copy(set
->p
[i
]);
10107 list
= isl_basic_set_list_add(list
, bset
);
10113 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10120 bset
= isl_basic_set_cow(bset
);
10124 dim
= isl_basic_set_get_space(bset
);
10125 dim
= isl_space_lift(dim
, bset
->n_div
);
10128 isl_space_free(bset
->dim
);
10130 bset
->extra
-= bset
->n_div
;
10133 bset
= isl_basic_set_finalize(bset
);
10137 isl_basic_set_free(bset
);
10141 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10147 set
= isl_set_align_divs(set
);
10152 set
= isl_set_cow(set
);
10156 n_div
= set
->p
[0]->n_div
;
10157 dim
= isl_set_get_space(set
);
10158 dim
= isl_space_lift(dim
, n_div
);
10161 isl_space_free(set
->dim
);
10164 for (i
= 0; i
< set
->n
; ++i
) {
10165 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10176 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10179 struct isl_basic_map
*bmap
;
10186 set
= isl_set_align_divs(set
);
10191 dim
= isl_set_get_space(set
);
10192 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10194 return isl_map_identity(isl_space_map_from_set(dim
));
10197 n_div
= set
->p
[0]->n_div
;
10198 dim
= isl_space_map_from_set(dim
);
10199 n_param
= isl_space_dim(dim
, isl_dim_param
);
10200 n_set
= isl_space_dim(dim
, isl_dim_in
);
10201 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10202 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10203 for (i
= 0; i
< n_set
; ++i
)
10204 bmap
= var_equal(bmap
, i
);
10206 total
= n_param
+ n_set
+ n_set
+ n_div
;
10207 for (i
= 0; i
< n_div
; ++i
) {
10208 k
= isl_basic_map_alloc_inequality(bmap
);
10211 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10212 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10213 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10214 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10215 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10216 set
->p
[0]->div
[i
][0]);
10218 l
= isl_basic_map_alloc_inequality(bmap
);
10221 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10222 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10223 set
->p
[0]->div
[i
][0]);
10224 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10228 bmap
= isl_basic_map_simplify(bmap
);
10229 bmap
= isl_basic_map_finalize(bmap
);
10230 return isl_map_from_basic_map(bmap
);
10233 isl_basic_map_free(bmap
);
10237 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10245 dim
= isl_basic_set_total_dim(bset
);
10246 size
+= bset
->n_eq
* (1 + dim
);
10247 size
+= bset
->n_ineq
* (1 + dim
);
10248 size
+= bset
->n_div
* (2 + dim
);
10253 int isl_set_size(__isl_keep isl_set
*set
)
10261 for (i
= 0; i
< set
->n
; ++i
)
10262 size
+= isl_basic_set_size(set
->p
[i
]);
10267 /* Check if there is any lower bound (if lower == 0) and/or upper
10268 * bound (if upper == 0) on the specified dim.
10270 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10271 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10276 return isl_bool_error
;
10278 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10279 return isl_bool_error
);
10281 pos
+= isl_basic_map_offset(bmap
, type
);
10283 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10284 if (isl_int_is_zero(bmap
->div
[i
][0]))
10286 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10287 return isl_bool_true
;
10290 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10291 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10292 return isl_bool_true
;
10294 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10295 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10302 return lower
&& upper
;
10305 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10306 enum isl_dim_type type
, unsigned pos
)
10308 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10311 isl_bool
isl_basic_map_dim_has_lower_bound(__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, 1);
10317 isl_bool
isl_basic_map_dim_has_upper_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
, 1, 0);
10323 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10324 enum isl_dim_type type
, unsigned pos
)
10331 for (i
= 0; i
< map
->n
; ++i
) {
10333 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10334 if (bounded
< 0 || !bounded
)
10341 /* Return 1 if the specified dim is involved in both an upper bound
10342 * and a lower bound.
10344 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10345 enum isl_dim_type type
, unsigned pos
)
10347 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10350 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10352 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10353 enum isl_dim_type type
, unsigned pos
,
10354 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10355 enum isl_dim_type type
, unsigned pos
))
10360 return isl_bool_error
;
10362 for (i
= 0; i
< map
->n
; ++i
) {
10364 bounded
= fn(map
->p
[i
], type
, pos
);
10365 if (bounded
< 0 || bounded
)
10369 return isl_bool_false
;
10372 /* Return 1 if the specified dim is involved in any lower bound.
10374 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10375 enum isl_dim_type type
, unsigned pos
)
10377 return has_any_bound(set
, type
, pos
,
10378 &isl_basic_map_dim_has_lower_bound
);
10381 /* Return 1 if the specified dim is involved in any upper bound.
10383 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10384 enum isl_dim_type type
, unsigned pos
)
10386 return has_any_bound(set
, type
, pos
,
10387 &isl_basic_map_dim_has_upper_bound
);
10390 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10392 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10393 enum isl_dim_type type
, unsigned pos
,
10394 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10395 enum isl_dim_type type
, unsigned pos
))
10400 return isl_bool_error
;
10402 for (i
= 0; i
< map
->n
; ++i
) {
10404 bounded
= fn(map
->p
[i
], type
, pos
);
10405 if (bounded
< 0 || !bounded
)
10409 return isl_bool_true
;
10412 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10414 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10415 enum isl_dim_type type
, unsigned pos
)
10417 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10420 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10422 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10423 enum isl_dim_type type
, unsigned pos
)
10425 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10428 /* For each of the "n" variables starting at "first", determine
10429 * the sign of the variable and put the results in the first "n"
10430 * elements of the array "signs".
10432 * 1 means that the variable is non-negative
10433 * -1 means that the variable is non-positive
10434 * 0 means the variable attains both positive and negative values.
10436 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10437 unsigned first
, unsigned n
, int *signs
)
10439 isl_vec
*bound
= NULL
;
10440 struct isl_tab
*tab
= NULL
;
10441 struct isl_tab_undo
*snap
;
10444 if (!bset
|| !signs
)
10447 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10448 tab
= isl_tab_from_basic_set(bset
, 0);
10449 if (!bound
|| !tab
)
10452 isl_seq_clr(bound
->el
, bound
->size
);
10453 isl_int_set_si(bound
->el
[0], -1);
10455 snap
= isl_tab_snap(tab
);
10456 for (i
= 0; i
< n
; ++i
) {
10459 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10460 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10462 empty
= tab
->empty
;
10463 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10464 if (isl_tab_rollback(tab
, snap
) < 0)
10472 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10473 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10475 empty
= tab
->empty
;
10476 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10477 if (isl_tab_rollback(tab
, snap
) < 0)
10480 signs
[i
] = empty
? -1 : 0;
10484 isl_vec_free(bound
);
10488 isl_vec_free(bound
);
10492 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10493 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10495 if (!bset
|| !signs
)
10497 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10500 first
+= pos(bset
->dim
, type
) - 1;
10501 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10504 /* Is it possible for the integer division "div" to depend (possibly
10505 * indirectly) on any output dimensions?
10507 * If the div is undefined, then we conservatively assume that it
10508 * may depend on them.
10509 * Otherwise, we check if it actually depends on them or on any integer
10510 * divisions that may depend on them.
10512 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10515 unsigned n_out
, o_out
;
10516 unsigned n_div
, o_div
;
10518 if (isl_int_is_zero(bmap
->div
[div
][0]))
10521 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10522 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10524 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10527 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10528 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10530 for (i
= 0; i
< n_div
; ++i
) {
10531 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10533 if (div_may_involve_output(bmap
, i
))
10540 /* Return the first integer division of "bmap" in the range
10541 * [first, first + n[ that may depend on any output dimensions and
10542 * that has a non-zero coefficient in "c" (where the first coefficient
10543 * in "c" corresponds to integer division "first").
10545 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10546 isl_int
*c
, int first
, int n
)
10553 for (k
= first
; k
< first
+ n
; ++k
) {
10554 if (isl_int_is_zero(c
[k
]))
10556 if (div_may_involve_output(bmap
, k
))
10563 /* Look for a pair of inequality constraints in "bmap" of the form
10565 * -l + i >= 0 or i >= l
10567 * n + l - i >= 0 or i <= l + n
10569 * with n < "m" and i the output dimension at position "pos".
10570 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10571 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10572 * and earlier output dimensions, as well as integer divisions that do
10573 * not involve any of the output dimensions.
10575 * Return the index of the first inequality constraint or bmap->n_ineq
10576 * if no such pair can be found.
10578 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10579 int pos
, isl_int m
)
10584 unsigned n_div
, o_div
;
10585 unsigned n_out
, o_out
;
10591 ctx
= isl_basic_map_get_ctx(bmap
);
10592 total
= isl_basic_map_total_dim(bmap
);
10593 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10594 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10595 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10596 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10597 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10598 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10600 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10601 n_out
- (pos
+ 1)) != -1)
10603 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10606 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10607 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10610 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10611 bmap
->ineq
[j
] + 1, total
))
10615 if (j
>= bmap
->n_ineq
)
10617 isl_int_add(bmap
->ineq
[i
][0],
10618 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10619 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10620 isl_int_sub(bmap
->ineq
[i
][0],
10621 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10624 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10630 return bmap
->n_ineq
;
10633 /* Return the index of the equality of "bmap" that defines
10634 * the output dimension "pos" in terms of earlier dimensions.
10635 * The equality may also involve integer divisions, as long
10636 * as those integer divisions are defined in terms of
10637 * parameters or input dimensions.
10638 * In this case, *div is set to the number of integer divisions and
10639 * *ineq is set to the number of inequality constraints (provided
10640 * div and ineq are not NULL).
10642 * The equality may also involve a single integer division involving
10643 * the output dimensions (typically only output dimension "pos") as
10644 * long as the coefficient of output dimension "pos" is 1 or -1 and
10645 * there is a pair of constraints i >= l and i <= l + n, with i referring
10646 * to output dimension "pos", l an expression involving only earlier
10647 * dimensions and n smaller than the coefficient of the integer division
10648 * in the equality. In this case, the output dimension can be defined
10649 * in terms of a modulo expression that does not involve the integer division.
10650 * *div is then set to this single integer division and
10651 * *ineq is set to the index of constraint i >= l.
10653 * Return bmap->n_eq if there is no such equality.
10654 * Return -1 on error.
10656 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10657 int pos
, int *div
, int *ineq
)
10660 unsigned n_out
, o_out
;
10661 unsigned n_div
, o_div
;
10666 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10667 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10668 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10669 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10672 *ineq
= bmap
->n_ineq
;
10675 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10676 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10678 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10679 n_out
- (pos
+ 1)) != -1)
10681 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10685 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10686 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10688 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10689 k
+ 1, n_div
- (k
+1)) < n_div
)
10691 l
= find_modulo_constraint_pair(bmap
, pos
,
10692 bmap
->eq
[j
][o_div
+ k
]);
10695 if (l
>= bmap
->n_ineq
)
10707 /* Check if the given basic map is obviously single-valued.
10708 * In particular, for each output dimension, check that there is
10709 * an equality that defines the output dimension in terms of
10710 * earlier dimensions.
10712 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10718 return isl_bool_error
;
10720 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10722 for (i
= 0; i
< n_out
; ++i
) {
10725 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10728 return isl_bool_error
;
10729 if (eq
>= bmap
->n_eq
)
10730 return isl_bool_false
;
10733 return isl_bool_true
;
10736 /* Check if the given basic map is single-valued.
10737 * We simply compute
10741 * and check if the result is a subset of the identity mapping.
10743 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10746 isl_basic_map
*test
;
10750 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10754 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10755 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10757 space
= isl_basic_map_get_space(bmap
);
10758 space
= isl_space_map_from_set(isl_space_range(space
));
10759 id
= isl_basic_map_identity(space
);
10761 sv
= isl_basic_map_is_subset(test
, id
);
10763 isl_basic_map_free(test
);
10764 isl_basic_map_free(id
);
10769 /* Check if the given map is obviously single-valued.
10771 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10774 return isl_bool_error
;
10776 return isl_bool_true
;
10778 return isl_bool_false
;
10780 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10783 /* Check if the given map is single-valued.
10784 * We simply compute
10788 * and check if the result is a subset of the identity mapping.
10790 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10797 sv
= isl_map_plain_is_single_valued(map
);
10801 test
= isl_map_reverse(isl_map_copy(map
));
10802 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10804 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10805 id
= isl_map_identity(dim
);
10807 sv
= isl_map_is_subset(test
, id
);
10809 isl_map_free(test
);
10815 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10819 map
= isl_map_copy(map
);
10820 map
= isl_map_reverse(map
);
10821 in
= isl_map_is_single_valued(map
);
10827 /* Check if the given map is obviously injective.
10829 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10833 map
= isl_map_copy(map
);
10834 map
= isl_map_reverse(map
);
10835 in
= isl_map_plain_is_single_valued(map
);
10841 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10845 sv
= isl_map_is_single_valued(map
);
10849 return isl_map_is_injective(map
);
10852 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10854 return isl_map_is_single_valued((isl_map
*)set
);
10857 /* Does "map" only map elements to themselves?
10859 * If the domain and range spaces are different, then "map"
10860 * is considered not to be an identity relation, even if it is empty.
10861 * Otherwise, construct the maximal identity relation and
10862 * check whether "map" is a subset of this relation.
10864 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10868 isl_bool equal
, is_identity
;
10870 space
= isl_map_get_space(map
);
10871 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10872 isl_space_free(space
);
10873 if (equal
< 0 || !equal
)
10876 id
= isl_map_identity(isl_map_get_space(map
));
10877 is_identity
= isl_map_is_subset(map
, id
);
10880 return is_identity
;
10883 int isl_map_is_translation(__isl_keep isl_map
*map
)
10888 delta
= isl_map_deltas(isl_map_copy(map
));
10889 ok
= isl_set_is_singleton(delta
);
10890 isl_set_free(delta
);
10895 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10897 if (isl_seq_first_non_zero(p
, pos
) != -1)
10899 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10904 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10913 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10916 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10917 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10918 for (j
= 0; j
< nvar
; ++j
) {
10919 int lower
= 0, upper
= 0;
10920 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10921 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10923 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10927 if (i
< bset
->n_eq
)
10929 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10930 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10932 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10934 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10939 if (!lower
|| !upper
)
10946 int isl_set_is_box(__isl_keep isl_set
*set
)
10953 return isl_basic_set_is_box(set
->p
[0]);
10956 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10959 return isl_bool_error
;
10961 return isl_space_is_wrapping(bset
->dim
);
10964 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10967 return isl_bool_error
;
10969 return isl_space_is_wrapping(set
->dim
);
10972 /* Modify the space of "map" through a call to "change".
10973 * If "can_change" is set (not NULL), then first call it to check
10974 * if the modification is allowed, printing the error message "cannot_change"
10977 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10978 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10979 const char *cannot_change
,
10980 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10988 ok
= can_change
? can_change(map
) : isl_bool_true
;
10990 return isl_map_free(map
);
10992 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10993 return isl_map_free(map
));
10995 space
= change(isl_map_get_space(map
));
10996 map
= isl_map_reset_space(map
, space
);
11001 /* Is the domain of "map" a wrapped relation?
11003 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11006 return isl_bool_error
;
11008 return isl_space_domain_is_wrapping(map
->dim
);
11011 /* Is the range of "map" a wrapped relation?
11013 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11016 return isl_bool_error
;
11018 return isl_space_range_is_wrapping(map
->dim
);
11021 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11023 bmap
= isl_basic_map_cow(bmap
);
11027 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11031 bmap
= isl_basic_map_finalize(bmap
);
11033 return (isl_basic_set
*)bmap
;
11035 isl_basic_map_free(bmap
);
11039 /* Given a map A -> B, return the set (A -> B).
11041 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11043 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11046 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11048 bset
= isl_basic_set_cow(bset
);
11052 bset
->dim
= isl_space_unwrap(bset
->dim
);
11056 bset
= isl_basic_set_finalize(bset
);
11058 return (isl_basic_map
*)bset
;
11060 isl_basic_set_free(bset
);
11064 /* Given a set (A -> B), return the map A -> B.
11065 * Error out if "set" is not of the form (A -> B).
11067 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11069 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11070 "not a wrapping set", &isl_space_unwrap
);
11073 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11074 enum isl_dim_type type
)
11079 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11082 bmap
= isl_basic_map_cow(bmap
);
11086 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11090 bmap
= isl_basic_map_finalize(bmap
);
11094 isl_basic_map_free(bmap
);
11098 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11099 enum isl_dim_type type
)
11106 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11109 map
= isl_map_cow(map
);
11113 for (i
= 0; i
< map
->n
; ++i
) {
11114 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11118 map
->dim
= isl_space_reset(map
->dim
, type
);
11128 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11133 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11136 bmap
= isl_basic_map_cow(bmap
);
11140 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11144 bmap
= isl_basic_map_finalize(bmap
);
11148 isl_basic_map_free(bmap
);
11152 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11154 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11157 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11158 __isl_take isl_basic_map
*bmap
)
11163 if (!bmap
->dim
->nested
[0])
11166 bmap
= isl_basic_map_cow(bmap
);
11170 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11174 bmap
= isl_basic_map_finalize(bmap
);
11178 isl_basic_map_free(bmap
);
11182 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11183 __isl_take isl_basic_map
*bmap
)
11188 if (!bmap
->dim
->nested
[1])
11191 bmap
= isl_basic_map_cow(bmap
);
11195 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11199 bmap
= isl_basic_map_finalize(bmap
);
11203 isl_basic_map_free(bmap
);
11207 /* Remove any internal structure from the spaces of domain and range of "map".
11209 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11214 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11217 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11220 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11222 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11225 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11227 isl_space
*dim
, *flat_dim
;
11230 dim
= isl_set_get_space(set
);
11231 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11232 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11233 map
= isl_map_intersect_domain(map
, set
);
11238 /* Remove any internal structure from the space of the domain of "map".
11240 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11245 if (!map
->dim
->nested
[0])
11248 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11251 /* Remove any internal structure from the space of the range of "map".
11253 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11258 if (!map
->dim
->nested
[1])
11261 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11264 /* Reorder the dimensions of "bmap" according to the given dim_map
11265 * and set the dimension specification to "dim" and
11266 * perform Gaussian elimination on the result.
11268 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11269 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11271 isl_basic_map
*res
;
11274 bmap
= isl_basic_map_cow(bmap
);
11275 if (!bmap
|| !dim
|| !dim_map
)
11278 flags
= bmap
->flags
;
11279 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11280 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11281 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11282 res
= isl_basic_map_alloc_space(dim
,
11283 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11284 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11286 res
->flags
= flags
;
11287 res
= isl_basic_map_gauss(res
, NULL
);
11288 res
= isl_basic_map_finalize(res
);
11292 isl_basic_map_free(bmap
);
11293 isl_space_free(dim
);
11297 /* Reorder the dimensions of "map" according to given reordering.
11299 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11300 __isl_take isl_reordering
*r
)
11303 struct isl_dim_map
*dim_map
;
11305 map
= isl_map_cow(map
);
11306 dim_map
= isl_dim_map_from_reordering(r
);
11307 if (!map
|| !r
|| !dim_map
)
11310 for (i
= 0; i
< map
->n
; ++i
) {
11311 struct isl_dim_map
*dim_map_i
;
11313 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11315 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11316 isl_space_copy(r
->dim
), dim_map_i
);
11322 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11324 isl_reordering_free(r
);
11330 isl_reordering_free(r
);
11334 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11335 __isl_take isl_reordering
*r
)
11337 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11340 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11341 __isl_take isl_space
*model
)
11345 if (!map
|| !model
)
11348 ctx
= isl_space_get_ctx(model
);
11349 if (!isl_space_has_named_params(model
))
11350 isl_die(ctx
, isl_error_invalid
,
11351 "model has unnamed parameters", goto error
);
11352 if (!isl_space_has_named_params(map
->dim
))
11353 isl_die(ctx
, isl_error_invalid
,
11354 "relation has unnamed parameters", goto error
);
11355 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11356 isl_reordering
*exp
;
11358 model
= isl_space_drop_dims(model
, isl_dim_in
,
11359 0, isl_space_dim(model
, isl_dim_in
));
11360 model
= isl_space_drop_dims(model
, isl_dim_out
,
11361 0, isl_space_dim(model
, isl_dim_out
));
11362 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11363 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11364 map
= isl_map_realign(map
, exp
);
11367 isl_space_free(model
);
11370 isl_space_free(model
);
11375 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11376 __isl_take isl_space
*model
)
11378 return isl_map_align_params(set
, model
);
11381 /* Align the parameters of "bmap" to those of "model", introducing
11382 * additional parameters if needed.
11384 __isl_give isl_basic_map
*isl_basic_map_align_params(
11385 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11389 if (!bmap
|| !model
)
11392 ctx
= isl_space_get_ctx(model
);
11393 if (!isl_space_has_named_params(model
))
11394 isl_die(ctx
, isl_error_invalid
,
11395 "model has unnamed parameters", goto error
);
11396 if (!isl_space_has_named_params(bmap
->dim
))
11397 isl_die(ctx
, isl_error_invalid
,
11398 "relation has unnamed parameters", goto error
);
11399 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11400 isl_reordering
*exp
;
11401 struct isl_dim_map
*dim_map
;
11403 model
= isl_space_drop_dims(model
, isl_dim_in
,
11404 0, isl_space_dim(model
, isl_dim_in
));
11405 model
= isl_space_drop_dims(model
, isl_dim_out
,
11406 0, isl_space_dim(model
, isl_dim_out
));
11407 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11408 exp
= isl_reordering_extend_space(exp
,
11409 isl_basic_map_get_space(bmap
));
11410 dim_map
= isl_dim_map_from_reordering(exp
);
11411 bmap
= isl_basic_map_realign(bmap
,
11412 exp
? isl_space_copy(exp
->dim
) : NULL
,
11413 isl_dim_map_extend(dim_map
, bmap
));
11414 isl_reordering_free(exp
);
11418 isl_space_free(model
);
11421 isl_space_free(model
);
11422 isl_basic_map_free(bmap
);
11426 /* Align the parameters of "bset" to those of "model", introducing
11427 * additional parameters if needed.
11429 __isl_give isl_basic_set
*isl_basic_set_align_params(
11430 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11432 return isl_basic_map_align_params(bset
, model
);
11435 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11436 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11437 enum isl_dim_type c2
, enum isl_dim_type c3
,
11438 enum isl_dim_type c4
, enum isl_dim_type c5
)
11440 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11441 struct isl_mat
*mat
;
11447 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11448 isl_basic_map_total_dim(bmap
) + 1);
11451 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11452 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11453 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11454 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11455 isl_int_set(mat
->row
[i
][pos
],
11456 bmap
->eq
[i
][off
+ k
]);
11464 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11465 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11466 enum isl_dim_type c2
, enum isl_dim_type c3
,
11467 enum isl_dim_type c4
, enum isl_dim_type c5
)
11469 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11470 struct isl_mat
*mat
;
11476 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11477 isl_basic_map_total_dim(bmap
) + 1);
11480 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11481 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11482 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11483 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11484 isl_int_set(mat
->row
[i
][pos
],
11485 bmap
->ineq
[i
][off
+ k
]);
11493 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11494 __isl_take isl_space
*dim
,
11495 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11496 enum isl_dim_type c2
, enum isl_dim_type c3
,
11497 enum isl_dim_type c4
, enum isl_dim_type c5
)
11499 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11500 isl_basic_map
*bmap
;
11506 if (!dim
|| !eq
|| !ineq
)
11509 if (eq
->n_col
!= ineq
->n_col
)
11510 isl_die(dim
->ctx
, isl_error_invalid
,
11511 "equalities and inequalities matrices should have "
11512 "same number of columns", goto error
);
11514 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11516 if (eq
->n_col
< total
)
11517 isl_die(dim
->ctx
, isl_error_invalid
,
11518 "number of columns too small", goto error
);
11520 extra
= eq
->n_col
- total
;
11522 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11523 eq
->n_row
, ineq
->n_row
);
11526 for (i
= 0; i
< extra
; ++i
) {
11527 k
= isl_basic_map_alloc_div(bmap
);
11530 isl_int_set_si(bmap
->div
[k
][0], 0);
11532 for (i
= 0; i
< eq
->n_row
; ++i
) {
11533 l
= isl_basic_map_alloc_equality(bmap
);
11536 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11537 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11538 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11539 isl_int_set(bmap
->eq
[l
][off
+ k
],
11545 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11546 l
= isl_basic_map_alloc_inequality(bmap
);
11549 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11550 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11551 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11552 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11553 ineq
->row
[i
][pos
]);
11559 isl_space_free(dim
);
11561 isl_mat_free(ineq
);
11563 bmap
= isl_basic_map_simplify(bmap
);
11564 return isl_basic_map_finalize(bmap
);
11566 isl_space_free(dim
);
11568 isl_mat_free(ineq
);
11572 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11573 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11574 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11576 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11577 c1
, c2
, c3
, c4
, isl_dim_in
);
11580 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11581 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11582 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11584 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11585 c1
, c2
, c3
, c4
, isl_dim_in
);
11588 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11589 __isl_take isl_space
*dim
,
11590 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11591 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11593 return (isl_basic_set
*)
11594 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11595 c1
, c2
, c3
, c4
, isl_dim_in
);
11598 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11601 return isl_bool_error
;
11603 return isl_space_can_zip(bmap
->dim
);
11606 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11609 return isl_bool_error
;
11611 return isl_space_can_zip(map
->dim
);
11614 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11615 * (A -> C) -> (B -> D).
11617 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11626 if (!isl_basic_map_can_zip(bmap
))
11627 isl_die(bmap
->ctx
, isl_error_invalid
,
11628 "basic map cannot be zipped", goto error
);
11629 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11630 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11631 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11632 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11633 bmap
= isl_basic_map_cow(bmap
);
11634 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11637 bmap
->dim
= isl_space_zip(bmap
->dim
);
11640 bmap
= isl_basic_map_mark_final(bmap
);
11643 isl_basic_map_free(bmap
);
11647 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11648 * (A -> C) -> (B -> D).
11650 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11657 if (!isl_map_can_zip(map
))
11658 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11661 map
= isl_map_cow(map
);
11665 for (i
= 0; i
< map
->n
; ++i
) {
11666 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11671 map
->dim
= isl_space_zip(map
->dim
);
11681 /* Can we apply isl_basic_map_curry to "bmap"?
11682 * That is, does it have a nested relation in its domain?
11684 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11687 return isl_bool_error
;
11689 return isl_space_can_curry(bmap
->dim
);
11692 /* Can we apply isl_map_curry to "map"?
11693 * That is, does it have a nested relation in its domain?
11695 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11698 return isl_bool_error
;
11700 return isl_space_can_curry(map
->dim
);
11703 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11706 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11712 if (!isl_basic_map_can_curry(bmap
))
11713 isl_die(bmap
->ctx
, isl_error_invalid
,
11714 "basic map cannot be curried", goto error
);
11715 bmap
= isl_basic_map_cow(bmap
);
11718 bmap
->dim
= isl_space_curry(bmap
->dim
);
11721 bmap
= isl_basic_map_mark_final(bmap
);
11724 isl_basic_map_free(bmap
);
11728 /* Given a map (A -> B) -> C, return the corresponding map
11731 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11733 return isl_map_change_space(map
, &isl_map_can_curry
,
11734 "map cannot be curried", &isl_space_curry
);
11737 /* Can isl_map_range_curry be applied to "map"?
11738 * That is, does it have a nested relation in its range,
11739 * the domain of which is itself a nested relation?
11741 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11744 return isl_bool_error
;
11746 return isl_space_can_range_curry(map
->dim
);
11749 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11750 * A -> (B -> (C -> D)).
11752 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11754 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11755 "map range cannot be curried",
11756 &isl_space_range_curry
);
11759 /* Can we apply isl_basic_map_uncurry to "bmap"?
11760 * That is, does it have a nested relation in its domain?
11762 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11765 return isl_bool_error
;
11767 return isl_space_can_uncurry(bmap
->dim
);
11770 /* Can we apply isl_map_uncurry to "map"?
11771 * That is, does it have a nested relation in its domain?
11773 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11776 return isl_bool_error
;
11778 return isl_space_can_uncurry(map
->dim
);
11781 /* Given a basic map A -> (B -> C), return the corresponding basic map
11784 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11790 if (!isl_basic_map_can_uncurry(bmap
))
11791 isl_die(bmap
->ctx
, isl_error_invalid
,
11792 "basic map cannot be uncurried",
11793 return isl_basic_map_free(bmap
));
11794 bmap
= isl_basic_map_cow(bmap
);
11797 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11799 return isl_basic_map_free(bmap
);
11800 bmap
= isl_basic_map_mark_final(bmap
);
11804 /* Given a map A -> (B -> C), return the corresponding map
11807 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11809 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11810 "map cannot be uncurried", &isl_space_uncurry
);
11813 /* Construct a basic map mapping the domain of the affine expression
11814 * to a one-dimensional range prescribed by the affine expression.
11816 * A NaN affine expression cannot be converted to a basic map.
11818 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11823 isl_local_space
*ls
;
11824 isl_basic_map
*bmap
= NULL
;
11828 is_nan
= isl_aff_is_nan(aff
);
11832 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11833 "cannot convert NaN", goto error
);
11835 ls
= isl_aff_get_local_space(aff
);
11836 bmap
= isl_basic_map_from_local_space(ls
);
11837 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11838 k
= isl_basic_map_alloc_equality(bmap
);
11842 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11843 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11844 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11845 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11846 aff
->v
->size
- (pos
+ 1));
11849 bmap
= isl_basic_map_finalize(bmap
);
11853 isl_basic_map_free(bmap
);
11857 /* Construct a map mapping the domain of the affine expression
11858 * to a one-dimensional range prescribed by the affine expression.
11860 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11862 isl_basic_map
*bmap
;
11864 bmap
= isl_basic_map_from_aff(aff
);
11865 return isl_map_from_basic_map(bmap
);
11868 /* Construct a basic map mapping the domain the multi-affine expression
11869 * to its range, with each dimension in the range equated to the
11870 * corresponding affine expression.
11872 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11873 __isl_take isl_multi_aff
*maff
)
11877 isl_basic_map
*bmap
;
11882 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11883 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11884 "invalid space", goto error
);
11886 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11887 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11889 for (i
= 0; i
< maff
->n
; ++i
) {
11891 isl_basic_map
*bmap_i
;
11893 aff
= isl_aff_copy(maff
->p
[i
]);
11894 bmap_i
= isl_basic_map_from_aff(aff
);
11896 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11899 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11901 isl_multi_aff_free(maff
);
11904 isl_multi_aff_free(maff
);
11908 /* Construct a map mapping the domain the multi-affine expression
11909 * to its range, with each dimension in the range equated to the
11910 * corresponding affine expression.
11912 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11914 isl_basic_map
*bmap
;
11916 bmap
= isl_basic_map_from_multi_aff(maff
);
11917 return isl_map_from_basic_map(bmap
);
11920 /* Construct a basic map mapping a domain in the given space to
11921 * to an n-dimensional range, with n the number of elements in the list,
11922 * where each coordinate in the range is prescribed by the
11923 * corresponding affine expression.
11924 * The domains of all affine expressions in the list are assumed to match
11927 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11928 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11932 isl_basic_map
*bmap
;
11937 dim
= isl_space_from_domain(domain_dim
);
11938 bmap
= isl_basic_map_universe(dim
);
11940 for (i
= 0; i
< list
->n
; ++i
) {
11942 isl_basic_map
*bmap_i
;
11944 aff
= isl_aff_copy(list
->p
[i
]);
11945 bmap_i
= isl_basic_map_from_aff(aff
);
11947 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11950 isl_aff_list_free(list
);
11954 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11955 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11957 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11960 /* Construct a basic map where the given dimensions are equal to each other.
11962 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11963 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11965 isl_basic_map
*bmap
= NULL
;
11971 if (pos1
>= isl_space_dim(space
, type1
))
11972 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11973 "index out of bounds", goto error
);
11974 if (pos2
>= isl_space_dim(space
, type2
))
11975 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11976 "index out of bounds", goto error
);
11978 if (type1
== type2
&& pos1
== pos2
)
11979 return isl_basic_map_universe(space
);
11981 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11982 i
= isl_basic_map_alloc_equality(bmap
);
11985 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11986 pos1
+= isl_basic_map_offset(bmap
, type1
);
11987 pos2
+= isl_basic_map_offset(bmap
, type2
);
11988 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11989 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11990 bmap
= isl_basic_map_finalize(bmap
);
11991 isl_space_free(space
);
11994 isl_space_free(space
);
11995 isl_basic_map_free(bmap
);
11999 /* Add a constraint imposing that the given two dimensions are equal.
12001 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12002 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12006 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12008 bmap
= isl_basic_map_intersect(bmap
, eq
);
12013 /* Add a constraint imposing that the given two dimensions are equal.
12015 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12016 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12018 isl_basic_map
*bmap
;
12020 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12022 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12027 /* Add a constraint imposing that the given two dimensions have opposite values.
12029 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12030 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12032 isl_basic_map
*bmap
= NULL
;
12038 if (pos1
>= isl_map_dim(map
, type1
))
12039 isl_die(map
->ctx
, isl_error_invalid
,
12040 "index out of bounds", goto error
);
12041 if (pos2
>= isl_map_dim(map
, type2
))
12042 isl_die(map
->ctx
, isl_error_invalid
,
12043 "index out of bounds", goto error
);
12045 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12046 i
= isl_basic_map_alloc_equality(bmap
);
12049 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12050 pos1
+= isl_basic_map_offset(bmap
, type1
);
12051 pos2
+= isl_basic_map_offset(bmap
, type2
);
12052 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12053 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12054 bmap
= isl_basic_map_finalize(bmap
);
12056 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12060 isl_basic_map_free(bmap
);
12065 /* Construct a constraint imposing that the value of the first dimension is
12066 * greater than or equal to that of the second.
12068 static __isl_give isl_constraint
*constraint_order_ge(
12069 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12070 enum isl_dim_type type2
, int pos2
)
12077 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12079 if (pos1
>= isl_constraint_dim(c
, type1
))
12080 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12081 "index out of bounds", return isl_constraint_free(c
));
12082 if (pos2
>= isl_constraint_dim(c
, type2
))
12083 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12084 "index out of bounds", return isl_constraint_free(c
));
12086 if (type1
== type2
&& pos1
== pos2
)
12089 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12090 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12095 /* Add a constraint imposing that the value of the first dimension is
12096 * greater than or equal to that of the second.
12098 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12099 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12104 if (type1
== type2
&& pos1
== pos2
)
12106 space
= isl_basic_map_get_space(bmap
);
12107 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12108 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12113 /* Add a constraint imposing that the value of the first dimension is
12114 * greater than or equal to that of the second.
12116 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12117 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12122 if (type1
== type2
&& pos1
== pos2
)
12124 space
= isl_map_get_space(map
);
12125 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12126 map
= isl_map_add_constraint(map
, c
);
12131 /* Add a constraint imposing that the value of the first dimension is
12132 * less than or equal to that of the second.
12134 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12135 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12137 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12140 /* Construct a basic map where the value of the first dimension is
12141 * greater than that of the second.
12143 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12144 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12146 isl_basic_map
*bmap
= NULL
;
12152 if (pos1
>= isl_space_dim(space
, type1
))
12153 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12154 "index out of bounds", goto error
);
12155 if (pos2
>= isl_space_dim(space
, type2
))
12156 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12157 "index out of bounds", goto error
);
12159 if (type1
== type2
&& pos1
== pos2
)
12160 return isl_basic_map_empty(space
);
12162 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12163 i
= isl_basic_map_alloc_inequality(bmap
);
12165 return isl_basic_map_free(bmap
);
12166 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12167 pos1
+= isl_basic_map_offset(bmap
, type1
);
12168 pos2
+= isl_basic_map_offset(bmap
, type2
);
12169 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12170 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12171 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12172 bmap
= isl_basic_map_finalize(bmap
);
12176 isl_space_free(space
);
12177 isl_basic_map_free(bmap
);
12181 /* Add a constraint imposing that the value of the first dimension is
12182 * greater than that of the second.
12184 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12185 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12189 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12191 bmap
= isl_basic_map_intersect(bmap
, gt
);
12196 /* Add a constraint imposing that the value of the first dimension is
12197 * greater than that of the second.
12199 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12200 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12202 isl_basic_map
*bmap
;
12204 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12206 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12211 /* Add a constraint imposing that the value of the first dimension is
12212 * smaller than that of the second.
12214 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12215 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12217 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12220 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12224 isl_local_space
*ls
;
12229 if (!isl_basic_map_divs_known(bmap
))
12230 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12231 "some divs are unknown", return NULL
);
12233 ls
= isl_basic_map_get_local_space(bmap
);
12234 div
= isl_local_space_get_div(ls
, pos
);
12235 isl_local_space_free(ls
);
12240 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12243 return isl_basic_map_get_div(bset
, pos
);
12246 /* Plug in "subs" for dimension "type", "pos" of "bset".
12248 * Let i be the dimension to replace and let "subs" be of the form
12252 * Any integer division with a non-zero coefficient for i,
12254 * floor((a i + g)/m)
12258 * floor((a f + d g)/(m d))
12260 * Constraints of the form
12268 * We currently require that "subs" is an integral expression.
12269 * Handling rational expressions may require us to add stride constraints
12270 * as we do in isl_basic_set_preimage_multi_aff.
12272 __isl_give isl_basic_set
*isl_basic_set_substitute(
12273 __isl_take isl_basic_set
*bset
,
12274 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12280 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12283 bset
= isl_basic_set_cow(bset
);
12284 if (!bset
|| !subs
)
12287 ctx
= isl_basic_set_get_ctx(bset
);
12288 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12289 isl_die(ctx
, isl_error_invalid
,
12290 "spaces don't match", goto error
);
12291 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12292 isl_die(ctx
, isl_error_unsupported
,
12293 "cannot handle divs yet", goto error
);
12294 if (!isl_int_is_one(subs
->v
->el
[0]))
12295 isl_die(ctx
, isl_error_invalid
,
12296 "can only substitute integer expressions", goto error
);
12298 pos
+= isl_basic_set_offset(bset
, type
);
12302 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12303 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12305 isl_int_set(v
, bset
->eq
[i
][pos
]);
12306 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12307 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12308 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12311 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12312 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12314 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12315 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12316 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12317 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12320 for (i
= 0; i
< bset
->n_div
; ++i
) {
12321 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12323 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12324 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12325 isl_seq_combine(bset
->div
[i
] + 1,
12326 subs
->v
->el
[0], bset
->div
[i
] + 1,
12327 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12328 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12333 bset
= isl_basic_set_simplify(bset
);
12334 return isl_basic_set_finalize(bset
);
12336 isl_basic_set_free(bset
);
12340 /* Plug in "subs" for dimension "type", "pos" of "set".
12342 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12343 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12347 if (set
&& isl_set_plain_is_empty(set
))
12350 set
= isl_set_cow(set
);
12354 for (i
= set
->n
- 1; i
>= 0; --i
) {
12355 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12356 if (remove_if_empty(set
, i
) < 0)
12366 /* Check if the range of "ma" is compatible with the domain or range
12367 * (depending on "type") of "bmap".
12368 * Return -1 if anything is wrong.
12370 static int check_basic_map_compatible_range_multi_aff(
12371 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12372 __isl_keep isl_multi_aff
*ma
)
12375 isl_space
*ma_space
;
12377 ma_space
= isl_multi_aff_get_space(ma
);
12379 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12383 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12384 "parameters don't match", goto error
);
12385 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12389 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12390 "spaces don't match", goto error
);
12392 isl_space_free(ma_space
);
12395 isl_space_free(ma_space
);
12399 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12400 * coefficients before the transformed range of dimensions,
12401 * the "n_after" coefficients after the transformed range of dimensions
12402 * and the coefficients of the other divs in "bmap".
12404 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12405 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12410 isl_local_space
*ls
;
12415 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12419 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12420 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12421 for (i
= 0; i
< n_div
; ++i
) {
12422 int o_bmap
= 0, o_ls
= 0;
12424 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12425 o_bmap
+= 1 + 1 + n_param
;
12426 o_ls
+= 1 + 1 + n_param
;
12427 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12428 o_bmap
+= n_before
;
12429 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12430 ls
->div
->row
[i
] + o_ls
, n_set
);
12433 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12435 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12436 ls
->div
->row
[i
] + o_ls
, n_div
);
12439 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12440 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12444 isl_local_space_free(ls
);
12447 isl_local_space_free(ls
);
12451 /* How many stride constraints does "ma" enforce?
12452 * That is, how many of the affine expressions have a denominator
12453 * different from one?
12455 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12460 for (i
= 0; i
< ma
->n
; ++i
)
12461 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12467 /* For each affine expression in ma of the form
12469 * x_i = (f_i y + h_i)/m_i
12471 * with m_i different from one, add a constraint to "bmap"
12474 * f_i y + h_i = m_i alpha_i
12476 * with alpha_i an additional existentially quantified variable.
12478 * The input variables of "ma" correspond to a subset of the variables
12479 * of "bmap". There are "n_before" variables in "bmap" before this
12480 * subset and "n_after" variables after this subset.
12481 * The integer divisions of the affine expressions in "ma" are assumed
12482 * to have been aligned. There are "n_div_ma" of them and
12483 * they appear first in "bmap", straight after the "n_after" variables.
12485 static __isl_give isl_basic_map
*add_ma_strides(
12486 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12487 int n_before
, int n_after
, int n_div_ma
)
12495 total
= isl_basic_map_total_dim(bmap
);
12496 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12497 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12498 for (i
= 0; i
< ma
->n
; ++i
) {
12499 int o_bmap
= 0, o_ma
= 1;
12501 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12503 div
= isl_basic_map_alloc_div(bmap
);
12504 k
= isl_basic_map_alloc_equality(bmap
);
12505 if (div
< 0 || k
< 0)
12507 isl_int_set_si(bmap
->div
[div
][0], 0);
12508 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12509 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12510 o_bmap
+= 1 + n_param
;
12511 o_ma
+= 1 + n_param
;
12512 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12513 o_bmap
+= n_before
;
12514 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12515 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12518 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12520 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12521 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12522 o_bmap
+= n_div_ma
;
12524 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12525 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12531 isl_basic_map_free(bmap
);
12535 /* Replace the domain or range space (depending on "type) of "space" by "set".
12537 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12538 enum isl_dim_type type
, __isl_take isl_space
*set
)
12540 if (type
== isl_dim_in
) {
12541 space
= isl_space_range(space
);
12542 space
= isl_space_map_from_domain_and_range(set
, space
);
12544 space
= isl_space_domain(space
);
12545 space
= isl_space_map_from_domain_and_range(space
, set
);
12551 /* Compute the preimage of the domain or range (depending on "type")
12552 * of "bmap" under the function represented by "ma".
12553 * In other words, plug in "ma" in the domain or range of "bmap".
12554 * The result is a basic map that lives in the same space as "bmap"
12555 * except that the domain or range has been replaced by
12556 * the domain space of "ma".
12558 * If bmap is represented by
12560 * A(p) + S u + B x + T v + C(divs) >= 0,
12562 * where u and x are input and output dimensions if type == isl_dim_out
12563 * while x and v are input and output dimensions if type == isl_dim_in,
12564 * and ma is represented by
12566 * x = D(p) + F(y) + G(divs')
12568 * then the result is
12570 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12572 * The divs in the input set are similarly adjusted.
12575 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12579 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12580 * B_i G(divs') + c_i(divs))/n_i)
12582 * If bmap is not a rational map and if F(y) involves any denominators
12584 * x_i = (f_i y + h_i)/m_i
12586 * then additional constraints are added to ensure that we only
12587 * map back integer points. That is we enforce
12589 * f_i y + h_i = m_i alpha_i
12591 * with alpha_i an additional existentially quantified variable.
12593 * We first copy over the divs from "ma".
12594 * Then we add the modified constraints and divs from "bmap".
12595 * Finally, we add the stride constraints, if needed.
12597 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12598 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12599 __isl_take isl_multi_aff
*ma
)
12603 isl_basic_map
*res
= NULL
;
12604 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12605 isl_int f
, c1
, c2
, g
;
12606 int rational
, strides
;
12613 ma
= isl_multi_aff_align_divs(ma
);
12616 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12619 if (type
== isl_dim_in
) {
12621 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12623 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12626 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12627 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12629 space
= isl_multi_aff_get_domain_space(ma
);
12630 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12631 rational
= isl_basic_map_is_rational(bmap
);
12632 strides
= rational
? 0 : multi_aff_strides(ma
);
12633 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12634 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12636 res
= isl_basic_map_set_rational(res
);
12638 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12639 if (isl_basic_map_alloc_div(res
) < 0)
12642 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12645 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12646 k
= isl_basic_map_alloc_equality(res
);
12649 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12650 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12653 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12654 k
= isl_basic_map_alloc_inequality(res
);
12657 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12658 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12661 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12662 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12663 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12666 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12667 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12672 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12678 isl_basic_map_free(bmap
);
12679 isl_multi_aff_free(ma
);
12680 res
= isl_basic_set_simplify(res
);
12681 return isl_basic_map_finalize(res
);
12687 isl_basic_map_free(bmap
);
12688 isl_multi_aff_free(ma
);
12689 isl_basic_map_free(res
);
12693 /* Compute the preimage of "bset" under the function represented by "ma".
12694 * In other words, plug in "ma" in "bset". The result is a basic set
12695 * that lives in the domain space of "ma".
12697 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12698 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12700 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12703 /* Compute the preimage of the domain of "bmap" under the function
12704 * represented by "ma".
12705 * In other words, plug in "ma" in the domain of "bmap".
12706 * The result is a basic map that lives in the same space as "bmap"
12707 * except that the domain has been replaced by the domain space of "ma".
12709 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12710 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12712 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12715 /* Compute the preimage of the range of "bmap" under the function
12716 * represented by "ma".
12717 * In other words, plug in "ma" in the range of "bmap".
12718 * The result is a basic map that lives in the same space as "bmap"
12719 * except that the range has been replaced by the domain space of "ma".
12721 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12722 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12724 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12727 /* Check if the range of "ma" is compatible with the domain or range
12728 * (depending on "type") of "map".
12729 * Return -1 if anything is wrong.
12731 static int check_map_compatible_range_multi_aff(
12732 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12733 __isl_keep isl_multi_aff
*ma
)
12736 isl_space
*ma_space
;
12738 ma_space
= isl_multi_aff_get_space(ma
);
12739 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12740 isl_space_free(ma_space
);
12742 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12743 "spaces don't match", return -1);
12747 /* Compute the preimage of the domain or range (depending on "type")
12748 * of "map" under the function represented by "ma".
12749 * In other words, plug in "ma" in the domain or range of "map".
12750 * The result is a map that lives in the same space as "map"
12751 * except that the domain or range has been replaced by
12752 * the domain space of "ma".
12754 * The parameters are assumed to have been aligned.
12756 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12757 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12762 map
= isl_map_cow(map
);
12763 ma
= isl_multi_aff_align_divs(ma
);
12766 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12769 for (i
= 0; i
< map
->n
; ++i
) {
12770 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12771 isl_multi_aff_copy(ma
));
12776 space
= isl_multi_aff_get_domain_space(ma
);
12777 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12779 isl_space_free(map
->dim
);
12784 isl_multi_aff_free(ma
);
12786 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12787 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12790 isl_multi_aff_free(ma
);
12795 /* Compute the preimage of the domain or range (depending on "type")
12796 * of "map" under the function represented by "ma".
12797 * In other words, plug in "ma" in the domain or range of "map".
12798 * The result is a map that lives in the same space as "map"
12799 * except that the domain or range has been replaced by
12800 * the domain space of "ma".
12802 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12803 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12808 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12809 return map_preimage_multi_aff(map
, type
, ma
);
12811 if (!isl_space_has_named_params(map
->dim
) ||
12812 !isl_space_has_named_params(ma
->space
))
12813 isl_die(map
->ctx
, isl_error_invalid
,
12814 "unaligned unnamed parameters", goto error
);
12815 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12816 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12818 return map_preimage_multi_aff(map
, type
, ma
);
12820 isl_multi_aff_free(ma
);
12821 return isl_map_free(map
);
12824 /* Compute the preimage of "set" under the function represented by "ma".
12825 * In other words, plug in "ma" in "set". The result is a set
12826 * that lives in the domain space of "ma".
12828 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12829 __isl_take isl_multi_aff
*ma
)
12831 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12834 /* Compute the preimage of the domain of "map" under the function
12835 * represented by "ma".
12836 * In other words, plug in "ma" in the domain of "map".
12837 * The result is a map that lives in the same space as "map"
12838 * except that the domain has been replaced by the domain space of "ma".
12840 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12841 __isl_take isl_multi_aff
*ma
)
12843 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12846 /* Compute the preimage of the range of "map" under the function
12847 * represented by "ma".
12848 * In other words, plug in "ma" in the range of "map".
12849 * The result is a map that lives in the same space as "map"
12850 * except that the range has been replaced by the domain space of "ma".
12852 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12853 __isl_take isl_multi_aff
*ma
)
12855 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12858 /* Compute the preimage of "map" under the function represented by "pma".
12859 * In other words, plug in "pma" in the domain or range of "map".
12860 * The result is a map that lives in the same space as "map",
12861 * except that the space of type "type" has been replaced by
12862 * the domain space of "pma".
12864 * The parameters of "map" and "pma" are assumed to have been aligned.
12866 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12867 __isl_take isl_map
*map
, enum isl_dim_type type
,
12868 __isl_take isl_pw_multi_aff
*pma
)
12877 isl_pw_multi_aff_free(pma
);
12878 res
= isl_map_empty(isl_map_get_space(map
));
12883 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12884 isl_multi_aff_copy(pma
->p
[0].maff
));
12885 if (type
== isl_dim_in
)
12886 res
= isl_map_intersect_domain(res
,
12887 isl_map_copy(pma
->p
[0].set
));
12889 res
= isl_map_intersect_range(res
,
12890 isl_map_copy(pma
->p
[0].set
));
12892 for (i
= 1; i
< pma
->n
; ++i
) {
12895 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12896 isl_multi_aff_copy(pma
->p
[i
].maff
));
12897 if (type
== isl_dim_in
)
12898 res_i
= isl_map_intersect_domain(res_i
,
12899 isl_map_copy(pma
->p
[i
].set
));
12901 res_i
= isl_map_intersect_range(res_i
,
12902 isl_map_copy(pma
->p
[i
].set
));
12903 res
= isl_map_union(res
, res_i
);
12906 isl_pw_multi_aff_free(pma
);
12910 isl_pw_multi_aff_free(pma
);
12915 /* Compute the preimage of "map" under the function represented by "pma".
12916 * In other words, plug in "pma" in the domain or range of "map".
12917 * The result is a map that lives in the same space as "map",
12918 * except that the space of type "type" has been replaced by
12919 * the domain space of "pma".
12921 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12922 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12927 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12928 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12930 if (!isl_space_has_named_params(map
->dim
) ||
12931 !isl_space_has_named_params(pma
->dim
))
12932 isl_die(map
->ctx
, isl_error_invalid
,
12933 "unaligned unnamed parameters", goto error
);
12934 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12935 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12937 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12939 isl_pw_multi_aff_free(pma
);
12940 return isl_map_free(map
);
12943 /* Compute the preimage of "set" under the function represented by "pma".
12944 * In other words, plug in "pma" in "set". The result is a set
12945 * that lives in the domain space of "pma".
12947 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12948 __isl_take isl_pw_multi_aff
*pma
)
12950 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12953 /* Compute the preimage of the domain of "map" under the function
12954 * represented by "pma".
12955 * In other words, plug in "pma" in the domain of "map".
12956 * The result is a map that lives in the same space as "map",
12957 * except that domain space has been replaced by the domain space of "pma".
12959 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12960 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12962 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12965 /* Compute the preimage of the range of "map" under the function
12966 * represented by "pma".
12967 * In other words, plug in "pma" in the range of "map".
12968 * The result is a map that lives in the same space as "map",
12969 * except that range space has been replaced by the domain space of "pma".
12971 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12972 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12974 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12977 /* Compute the preimage of "map" under the function represented by "mpa".
12978 * In other words, plug in "mpa" in the domain or range of "map".
12979 * The result is a map that lives in the same space as "map",
12980 * except that the space of type "type" has been replaced by
12981 * the domain space of "mpa".
12983 * If the map does not involve any constraints that refer to the
12984 * dimensions of the substituted space, then the only possible
12985 * effect of "mpa" on the map is to map the space to a different space.
12986 * We create a separate isl_multi_aff to effectuate this change
12987 * in order to avoid spurious splitting of the map along the pieces
12990 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12991 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12994 isl_pw_multi_aff
*pma
;
12999 n
= isl_map_dim(map
, type
);
13000 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13004 space
= isl_multi_pw_aff_get_space(mpa
);
13005 isl_multi_pw_aff_free(mpa
);
13006 ma
= isl_multi_aff_zero(space
);
13007 return isl_map_preimage_multi_aff(map
, type
, ma
);
13010 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13011 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13014 isl_multi_pw_aff_free(mpa
);
13018 /* Compute the preimage of "map" under the function represented by "mpa".
13019 * In other words, plug in "mpa" in the domain "map".
13020 * The result is a map that lives in the same space as "map",
13021 * except that domain space has been replaced by the domain space of "mpa".
13023 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13024 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13026 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13029 /* Compute the preimage of "set" by the function represented by "mpa".
13030 * In other words, plug in "mpa" in "set".
13032 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13033 __isl_take isl_multi_pw_aff
*mpa
)
13035 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);