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-(m-1)) + m d >= 0
331 * Do not finalize the result.
333 static __isl_give isl_basic_map
*add_known_div_constraints(
334 __isl_take isl_basic_map
*bmap
)
341 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
344 bmap
= isl_basic_map_cow(bmap
);
345 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
348 for (i
= 0; i
< n_div
; ++i
) {
349 if (isl_int_is_zero(bmap
->div
[i
][0]))
351 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
352 return isl_basic_map_free(bmap
);
358 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
359 __isl_take isl_local_space
*ls
)
368 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
369 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
370 n_div
, 0, 2 * n_div
);
372 for (i
= 0; i
< n_div
; ++i
)
373 if (isl_basic_map_alloc_div(bmap
) < 0)
376 for (i
= 0; i
< n_div
; ++i
)
377 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
378 bmap
= add_known_div_constraints(bmap
);
380 isl_local_space_free(ls
);
383 isl_local_space_free(ls
);
384 isl_basic_map_free(bmap
);
388 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
389 __isl_take isl_local_space
*ls
)
391 return isl_basic_map_from_local_space(ls
);
394 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
398 return isl_space_copy(map
->dim
);
401 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
405 return isl_space_copy(set
->dim
);
408 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
409 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
411 bmap
= isl_basic_map_cow(bmap
);
414 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
417 bmap
= isl_basic_map_finalize(bmap
);
420 isl_basic_map_free(bmap
);
424 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
425 __isl_take isl_basic_set
*bset
, const char *s
)
427 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
430 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
431 enum isl_dim_type type
)
433 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
436 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
437 enum isl_dim_type type
, const char *s
)
441 map
= isl_map_cow(map
);
445 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
449 for (i
= 0; i
< map
->n
; ++i
) {
450 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
461 /* Replace the identifier of the tuple of type "type" by "id".
463 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
464 __isl_take isl_basic_map
*bmap
,
465 enum isl_dim_type type
, __isl_take isl_id
*id
)
467 bmap
= isl_basic_map_cow(bmap
);
470 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
472 return isl_basic_map_free(bmap
);
473 bmap
= isl_basic_map_finalize(bmap
);
480 /* Replace the identifier of the tuple by "id".
482 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
483 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
485 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
488 /* Does the input or output tuple have a name?
490 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
492 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
495 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
496 enum isl_dim_type type
)
498 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
501 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
504 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
507 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
508 enum isl_dim_type type
, __isl_take isl_id
*id
)
510 map
= isl_map_cow(map
);
514 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
516 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
522 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
523 __isl_take isl_id
*id
)
525 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
528 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
529 enum isl_dim_type type
)
531 map
= isl_map_cow(map
);
535 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
537 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
540 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
542 return isl_map_reset_tuple_id(set
, isl_dim_set
);
545 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
547 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
550 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
551 enum isl_dim_type type
)
553 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
556 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
558 return isl_map_has_tuple_id(set
, isl_dim_set
);
561 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
563 return isl_map_get_tuple_id(set
, isl_dim_set
);
566 /* Does the set tuple have a name?
568 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
571 return isl_bool_error
;
572 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
576 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
578 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
581 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
583 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
586 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
587 enum isl_dim_type type
, unsigned pos
)
589 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
592 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
593 enum isl_dim_type type
, unsigned pos
)
595 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
598 /* Does the given dimension have a name?
600 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
601 enum isl_dim_type type
, unsigned pos
)
604 return isl_bool_error
;
605 return isl_space_has_dim_name(map
->dim
, type
, pos
);
608 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
609 enum isl_dim_type type
, unsigned pos
)
611 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
614 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
615 enum isl_dim_type type
, unsigned pos
)
617 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
620 /* Does the given dimension have a name?
622 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
623 enum isl_dim_type type
, unsigned pos
)
626 return isl_bool_error
;
627 return isl_space_has_dim_name(set
->dim
, type
, pos
);
630 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
631 __isl_take isl_basic_map
*bmap
,
632 enum isl_dim_type type
, unsigned pos
, const char *s
)
634 bmap
= isl_basic_map_cow(bmap
);
637 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
640 return isl_basic_map_finalize(bmap
);
642 isl_basic_map_free(bmap
);
646 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
647 enum isl_dim_type type
, unsigned pos
, const char *s
)
651 map
= isl_map_cow(map
);
655 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
659 for (i
= 0; i
< map
->n
; ++i
) {
660 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
671 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
672 __isl_take isl_basic_set
*bset
,
673 enum isl_dim_type type
, unsigned pos
, const char *s
)
675 return (isl_basic_set
*)isl_basic_map_set_dim_name(
676 (isl_basic_map
*)bset
, type
, pos
, s
);
679 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
680 enum isl_dim_type type
, unsigned pos
, const char *s
)
682 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
685 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
686 enum isl_dim_type type
, unsigned pos
)
689 return isl_bool_error
;
690 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
693 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
694 enum isl_dim_type type
, unsigned pos
)
696 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
699 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
700 enum isl_dim_type type
, unsigned pos
)
702 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
705 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
706 enum isl_dim_type type
, unsigned pos
)
708 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
711 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
712 enum isl_dim_type type
, unsigned pos
)
714 return isl_map_has_dim_id(set
, type
, pos
);
717 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
718 enum isl_dim_type type
, unsigned pos
)
720 return isl_map_get_dim_id(set
, type
, pos
);
723 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
724 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
726 map
= isl_map_cow(map
);
730 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
732 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
738 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
739 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
741 return isl_map_set_dim_id(set
, type
, pos
, id
);
744 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
745 __isl_keep isl_id
*id
)
749 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
752 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
753 __isl_keep isl_id
*id
)
755 return isl_map_find_dim_by_id(set
, type
, id
);
758 /* Return the position of the dimension of the given type and name
760 * Return -1 if no such dimension can be found.
762 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
763 enum isl_dim_type type
, const char *name
)
767 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
770 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
775 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
778 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
781 return isl_map_find_dim_by_name(set
, type
, name
);
784 /* Reset the user pointer on all identifiers of parameters and tuples
785 * of the space of "map".
787 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
791 space
= isl_map_get_space(map
);
792 space
= isl_space_reset_user(space
);
793 map
= isl_map_reset_space(map
, space
);
798 /* Reset the user pointer on all identifiers of parameters and tuples
799 * of the space of "set".
801 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
803 return isl_map_reset_user(set
);
806 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
810 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
813 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
815 return isl_basic_map_is_rational(bset
);
818 /* Does "bmap" contain any rational points?
820 * If "bmap" has an equality for each dimension, equating the dimension
821 * to an integer constant, then it has no rational points, even if it
822 * is marked as rational.
824 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
826 int has_rational
= 1;
831 if (isl_basic_map_plain_is_empty(bmap
))
833 if (!isl_basic_map_is_rational(bmap
))
835 bmap
= isl_basic_map_copy(bmap
);
836 bmap
= isl_basic_map_implicit_equalities(bmap
);
839 total
= isl_basic_map_total_dim(bmap
);
840 if (bmap
->n_eq
== total
) {
842 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
843 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
846 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
847 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
849 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
857 isl_basic_map_free(bmap
);
862 /* Does "map" contain any rational points?
864 int isl_map_has_rational(__isl_keep isl_map
*map
)
871 for (i
= 0; i
< map
->n
; ++i
) {
872 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
873 if (has_rational
< 0)
881 /* Does "set" contain any rational points?
883 int isl_set_has_rational(__isl_keep isl_set
*set
)
885 return isl_map_has_rational(set
);
888 /* Is this basic set a parameter domain?
890 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
894 return isl_space_is_params(bset
->dim
);
897 /* Is this set a parameter domain?
899 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
902 return isl_bool_error
;
903 return isl_space_is_params(set
->dim
);
906 /* Is this map actually a parameter domain?
907 * Users should never call this function. Outside of isl,
908 * a map can never be a parameter domain.
910 int isl_map_is_params(__isl_keep isl_map
*map
)
914 return isl_space_is_params(map
->dim
);
917 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
918 struct isl_basic_map
*bmap
, unsigned extra
,
919 unsigned n_eq
, unsigned n_ineq
)
922 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
927 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
928 if (isl_blk_is_error(bmap
->block
))
931 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
932 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
936 bmap
->block2
= isl_blk_empty();
939 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
940 if (isl_blk_is_error(bmap
->block2
))
943 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
948 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
949 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
951 for (i
= 0; i
< extra
; ++i
)
952 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
956 bmap
->c_size
= n_eq
+ n_ineq
;
957 bmap
->eq
= bmap
->ineq
+ n_ineq
;
966 isl_basic_map_free(bmap
);
970 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
971 unsigned nparam
, unsigned dim
, unsigned extra
,
972 unsigned n_eq
, unsigned n_ineq
)
974 struct isl_basic_map
*bmap
;
977 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
981 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
982 return (struct isl_basic_set
*)bmap
;
985 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
986 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
988 struct isl_basic_map
*bmap
;
991 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
992 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
993 return (struct isl_basic_set
*)bmap
;
999 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1000 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1002 struct isl_basic_map
*bmap
;
1006 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1011 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1013 isl_space_free(dim
);
1017 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1018 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1019 unsigned n_eq
, unsigned n_ineq
)
1021 struct isl_basic_map
*bmap
;
1024 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1028 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1032 static void dup_constraints(
1033 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1036 unsigned total
= isl_basic_map_total_dim(src
);
1038 for (i
= 0; i
< src
->n_eq
; ++i
) {
1039 int j
= isl_basic_map_alloc_equality(dst
);
1040 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1043 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1044 int j
= isl_basic_map_alloc_inequality(dst
);
1045 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1048 for (i
= 0; i
< src
->n_div
; ++i
) {
1049 int j
= isl_basic_map_alloc_div(dst
);
1050 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1052 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1055 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1057 struct isl_basic_map
*dup
;
1061 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1062 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1065 dup_constraints(dup
, bmap
);
1066 dup
->flags
= bmap
->flags
;
1067 dup
->sample
= isl_vec_copy(bmap
->sample
);
1071 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1073 struct isl_basic_map
*dup
;
1075 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1076 return (struct isl_basic_set
*)dup
;
1079 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1084 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1088 return isl_basic_set_dup(bset
);
1091 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1100 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1105 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1109 bmap
= isl_basic_map_dup(bmap
);
1111 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1115 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1124 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1129 if (--bmap
->ref
> 0)
1132 isl_ctx_deref(bmap
->ctx
);
1134 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1136 isl_blk_free(bmap
->ctx
, bmap
->block
);
1137 isl_vec_free(bmap
->sample
);
1138 isl_space_free(bmap
->dim
);
1144 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1146 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1149 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1151 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1154 __isl_give isl_map
*isl_map_align_params_map_map_and(
1155 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1156 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1157 __isl_take isl_map
*map2
))
1161 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1162 return fn(map1
, map2
);
1163 if (!isl_space_has_named_params(map1
->dim
) ||
1164 !isl_space_has_named_params(map2
->dim
))
1165 isl_die(map1
->ctx
, isl_error_invalid
,
1166 "unaligned unnamed parameters", goto error
);
1167 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1168 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1169 return fn(map1
, map2
);
1176 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1177 __isl_keep isl_map
*map2
,
1178 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1183 return isl_bool_error
;
1184 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1185 return fn(map1
, map2
);
1186 if (!isl_space_has_named_params(map1
->dim
) ||
1187 !isl_space_has_named_params(map2
->dim
))
1188 isl_die(map1
->ctx
, isl_error_invalid
,
1189 "unaligned unnamed parameters", return isl_bool_error
);
1190 map1
= isl_map_copy(map1
);
1191 map2
= isl_map_copy(map2
);
1192 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1193 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1200 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1202 struct isl_ctx
*ctx
;
1206 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1207 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1210 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1211 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1212 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1213 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1214 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1216 int j
= isl_basic_map_alloc_inequality(bmap
);
1220 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1221 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1228 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1229 bmap
->extra
- bmap
->n_div
);
1230 return bmap
->n_eq
++;
1233 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1235 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1238 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1242 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1247 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1249 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1252 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1257 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1259 if (pos
!= bmap
->n_eq
- 1) {
1261 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1262 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1268 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1270 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1273 /* Turn inequality "pos" of "bmap" into an equality.
1275 * In particular, we move the inequality in front of the equalities
1276 * and move the last inequality in the position of the moved inequality.
1277 * Note that isl_tab_make_equalities_explicit depends on this particular
1278 * change in the ordering of the constraints.
1280 void isl_basic_map_inequality_to_equality(
1281 struct isl_basic_map
*bmap
, unsigned pos
)
1285 t
= bmap
->ineq
[pos
];
1286 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1287 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1292 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1293 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1294 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1295 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1298 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1300 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1303 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1305 struct isl_ctx
*ctx
;
1309 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1310 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1311 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1312 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1313 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1314 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1315 1 + isl_basic_map_total_dim(bmap
),
1316 bmap
->extra
- bmap
->n_div
);
1317 return bmap
->n_ineq
++;
1320 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1322 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1325 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1329 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1334 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1336 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1339 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1344 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1346 if (pos
!= bmap
->n_ineq
- 1) {
1347 t
= bmap
->ineq
[pos
];
1348 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1349 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1350 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1356 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1358 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1361 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1366 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1369 k
= isl_basic_map_alloc_equality(bmap
);
1372 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1375 isl_basic_map_free(bmap
);
1379 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1382 return (isl_basic_set
*)
1383 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1386 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1391 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1394 k
= isl_basic_map_alloc_inequality(bmap
);
1397 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1400 isl_basic_map_free(bmap
);
1404 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1407 return (isl_basic_set
*)
1408 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1411 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1415 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1416 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1417 1 + 1 + isl_basic_map_total_dim(bmap
),
1418 bmap
->extra
- bmap
->n_div
);
1419 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1420 return bmap
->n_div
++;
1423 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1425 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1428 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 /* Does local variable "div" of "bmap" have a complete explicit representation?
2314 * Having a complete explicit representation requires not only
2315 * an explicit representation, but also that all local variables
2316 * that appear in this explicit representation in turn have
2317 * a complete explicit representation.
2319 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2322 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2325 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2326 if (marked
< 0 || marked
)
2327 return isl_bool_not(marked
);
2329 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2332 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2334 known
= isl_basic_map_div_is_known(bmap
, i
);
2335 if (known
< 0 || !known
)
2339 return isl_bool_true
;
2342 /* Remove all divs that are unknown or defined in terms of unknown divs.
2344 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2345 __isl_take isl_basic_map
*bmap
)
2352 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2353 if (isl_basic_map_div_is_known(bmap
, i
))
2355 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2364 /* Remove all divs that are unknown or defined in terms of unknown divs.
2366 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2367 __isl_take isl_basic_set
*bset
)
2369 return isl_basic_map_remove_unknown_divs(bset
);
2372 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2381 map
= isl_map_cow(map
);
2385 for (i
= 0; i
< map
->n
; ++i
) {
2386 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2396 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2398 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2401 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2402 __isl_take isl_basic_set
*bset
,
2403 enum isl_dim_type type
, unsigned first
, unsigned n
)
2405 return (isl_basic_set
*)
2406 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2409 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2410 enum isl_dim_type type
, unsigned first
, unsigned n
)
2417 map
= isl_map_cow(map
);
2420 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2422 for (i
= 0; i
< map
->n
; ++i
) {
2423 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2424 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2428 map
= isl_map_drop(map
, type
, first
, n
);
2435 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2436 enum isl_dim_type type
, unsigned first
, unsigned n
)
2438 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2441 /* Project out n inputs starting at first using Fourier-Motzkin */
2442 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2443 unsigned first
, unsigned n
)
2445 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2448 static void dump_term(struct isl_basic_map
*bmap
,
2449 isl_int c
, int pos
, FILE *out
)
2452 unsigned in
= isl_basic_map_n_in(bmap
);
2453 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2454 unsigned nparam
= isl_basic_map_n_param(bmap
);
2456 isl_int_print(out
, c
, 0);
2458 if (!isl_int_is_one(c
))
2459 isl_int_print(out
, c
, 0);
2460 if (pos
< 1 + nparam
) {
2461 name
= isl_space_get_dim_name(bmap
->dim
,
2462 isl_dim_param
, pos
- 1);
2464 fprintf(out
, "%s", name
);
2466 fprintf(out
, "p%d", pos
- 1);
2467 } else if (pos
< 1 + nparam
+ in
)
2468 fprintf(out
, "i%d", pos
- 1 - nparam
);
2469 else if (pos
< 1 + nparam
+ dim
)
2470 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2472 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2476 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2477 int sign
, FILE *out
)
2481 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2485 for (i
= 0, first
= 1; i
< len
; ++i
) {
2486 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2489 fprintf(out
, " + ");
2491 isl_int_abs(v
, c
[i
]);
2492 dump_term(bmap
, v
, i
, out
);
2499 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2500 const char *op
, FILE *out
, int indent
)
2504 fprintf(out
, "%*s", indent
, "");
2506 dump_constraint_sign(bmap
, c
, 1, out
);
2507 fprintf(out
, " %s ", op
);
2508 dump_constraint_sign(bmap
, c
, -1, out
);
2512 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2513 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2515 fprintf(out
, "%*s", indent
, "");
2516 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2521 static void dump_constraints(struct isl_basic_map
*bmap
,
2522 isl_int
**c
, unsigned n
,
2523 const char *op
, FILE *out
, int indent
)
2527 for (i
= 0; i
< n
; ++i
)
2528 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2531 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2535 unsigned total
= isl_basic_map_total_dim(bmap
);
2537 for (j
= 0; j
< 1 + total
; ++j
) {
2538 if (isl_int_is_zero(exp
[j
]))
2540 if (!first
&& isl_int_is_pos(exp
[j
]))
2542 dump_term(bmap
, exp
[j
], j
, out
);
2547 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2551 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2552 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2554 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2555 fprintf(out
, "%*s", indent
, "");
2556 fprintf(out
, "e%d = [(", i
);
2557 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2559 isl_int_print(out
, bmap
->div
[i
][0], 0);
2560 fprintf(out
, "]\n");
2564 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2565 FILE *out
, int indent
)
2568 fprintf(out
, "null basic set\n");
2572 fprintf(out
, "%*s", indent
, "");
2573 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2574 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2575 bset
->extra
, bset
->flags
);
2576 dump((struct isl_basic_map
*)bset
, out
, indent
);
2579 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2580 FILE *out
, int indent
)
2583 fprintf(out
, "null basic map\n");
2587 fprintf(out
, "%*s", indent
, "");
2588 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2589 "flags: %x, n_name: %d\n",
2591 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2592 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2593 dump(bmap
, out
, indent
);
2596 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2601 total
= isl_basic_map_total_dim(bmap
);
2602 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2603 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2604 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2605 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2609 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2614 if (isl_space_dim(space
, isl_dim_in
) != 0)
2615 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2616 "set cannot have input dimensions", goto error
);
2617 return isl_map_alloc_space(space
, n
, flags
);
2619 isl_space_free(space
);
2623 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2624 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2626 struct isl_set
*set
;
2629 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2633 set
= isl_set_alloc_space(dims
, n
, flags
);
2637 /* Make sure "map" has room for at least "n" more basic maps.
2639 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2642 struct isl_map
*grown
= NULL
;
2646 isl_assert(map
->ctx
, n
>= 0, goto error
);
2647 if (map
->n
+ n
<= map
->size
)
2649 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2652 for (i
= 0; i
< map
->n
; ++i
) {
2653 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2661 isl_map_free(grown
);
2666 /* Make sure "set" has room for at least "n" more basic sets.
2668 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2670 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2673 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2676 struct isl_set
*dup
;
2681 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2684 for (i
= 0; i
< set
->n
; ++i
)
2685 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2689 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2691 return isl_map_from_basic_map(bset
);
2694 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2696 struct isl_map
*map
;
2701 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2702 return isl_map_add_basic_map(map
, bmap
);
2705 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2706 __isl_take isl_basic_set
*bset
)
2708 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2709 (struct isl_basic_map
*)bset
);
2712 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2714 return isl_map_free(set
);
2717 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2722 fprintf(out
, "null set\n");
2726 fprintf(out
, "%*s", indent
, "");
2727 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2728 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2730 for (i
= 0; i
< set
->n
; ++i
) {
2731 fprintf(out
, "%*s", indent
, "");
2732 fprintf(out
, "basic set %d:\n", i
);
2733 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2737 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2742 fprintf(out
, "null map\n");
2746 fprintf(out
, "%*s", indent
, "");
2747 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2748 "flags: %x, n_name: %d\n",
2749 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2750 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2751 for (i
= 0; i
< map
->n
; ++i
) {
2752 fprintf(out
, "%*s", indent
, "");
2753 fprintf(out
, "basic map %d:\n", i
);
2754 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2758 struct isl_basic_map
*isl_basic_map_intersect_domain(
2759 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2761 struct isl_basic_map
*bmap_domain
;
2766 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2767 bset
->dim
, isl_dim_param
), goto error
);
2769 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2770 isl_assert(bset
->ctx
,
2771 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2773 bmap
= isl_basic_map_cow(bmap
);
2776 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2777 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2778 bmap_domain
= isl_basic_map_from_domain(bset
);
2779 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2781 bmap
= isl_basic_map_simplify(bmap
);
2782 return isl_basic_map_finalize(bmap
);
2784 isl_basic_map_free(bmap
);
2785 isl_basic_set_free(bset
);
2789 struct isl_basic_map
*isl_basic_map_intersect_range(
2790 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2792 struct isl_basic_map
*bmap_range
;
2797 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2798 bset
->dim
, isl_dim_param
), goto error
);
2800 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2801 isl_assert(bset
->ctx
,
2802 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2804 if (isl_basic_set_plain_is_universe(bset
)) {
2805 isl_basic_set_free(bset
);
2809 bmap
= isl_basic_map_cow(bmap
);
2812 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2813 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2814 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2815 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2817 bmap
= isl_basic_map_simplify(bmap
);
2818 return isl_basic_map_finalize(bmap
);
2820 isl_basic_map_free(bmap
);
2821 isl_basic_set_free(bset
);
2825 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2826 __isl_keep isl_vec
*vec
)
2833 return isl_bool_error
;
2835 total
= 1 + isl_basic_map_total_dim(bmap
);
2836 if (total
!= vec
->size
)
2837 return isl_bool_error
;
2841 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2842 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2843 if (!isl_int_is_zero(s
)) {
2845 return isl_bool_false
;
2849 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2850 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2851 if (isl_int_is_neg(s
)) {
2853 return isl_bool_false
;
2859 return isl_bool_true
;
2862 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2863 __isl_keep isl_vec
*vec
)
2865 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2868 struct isl_basic_map
*isl_basic_map_intersect(
2869 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2871 struct isl_vec
*sample
= NULL
;
2873 if (!bmap1
|| !bmap2
)
2876 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2877 bmap2
->dim
, isl_dim_param
), goto error
);
2878 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2879 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2880 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2881 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2882 return isl_basic_map_intersect(bmap2
, bmap1
);
2884 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2885 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2886 isl_assert(bmap1
->ctx
,
2887 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2889 if (isl_basic_map_plain_is_empty(bmap1
)) {
2890 isl_basic_map_free(bmap2
);
2893 if (isl_basic_map_plain_is_empty(bmap2
)) {
2894 isl_basic_map_free(bmap1
);
2898 if (bmap1
->sample
&&
2899 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2900 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2901 sample
= isl_vec_copy(bmap1
->sample
);
2902 else if (bmap2
->sample
&&
2903 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2904 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2905 sample
= isl_vec_copy(bmap2
->sample
);
2907 bmap1
= isl_basic_map_cow(bmap1
);
2910 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2911 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2912 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2915 isl_vec_free(sample
);
2917 isl_vec_free(bmap1
->sample
);
2918 bmap1
->sample
= sample
;
2921 bmap1
= isl_basic_map_simplify(bmap1
);
2922 return isl_basic_map_finalize(bmap1
);
2925 isl_vec_free(sample
);
2926 isl_basic_map_free(bmap1
);
2927 isl_basic_map_free(bmap2
);
2931 struct isl_basic_set
*isl_basic_set_intersect(
2932 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2934 return (struct isl_basic_set
*)
2935 isl_basic_map_intersect(
2936 (struct isl_basic_map
*)bset1
,
2937 (struct isl_basic_map
*)bset2
);
2940 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2941 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2943 return isl_basic_set_intersect(bset1
, bset2
);
2946 /* Special case of isl_map_intersect, where both map1 and map2
2947 * are convex, without any divs and such that either map1 or map2
2948 * contains a single constraint. This constraint is then simply
2949 * added to the other map.
2951 static __isl_give isl_map
*map_intersect_add_constraint(
2952 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2954 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2955 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2956 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2957 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2959 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2960 return isl_map_intersect(map2
, map1
);
2962 isl_assert(map2
->ctx
,
2963 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2965 map1
= isl_map_cow(map1
);
2968 if (isl_map_plain_is_empty(map1
)) {
2972 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2973 if (map2
->p
[0]->n_eq
== 1)
2974 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2976 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2977 map2
->p
[0]->ineq
[0]);
2979 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2980 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2984 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2985 isl_basic_map_free(map1
->p
[0]);
2998 /* map2 may be either a parameter domain or a map living in the same
3001 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3002 __isl_take isl_map
*map2
)
3011 if ((isl_map_plain_is_empty(map1
) ||
3012 isl_map_plain_is_universe(map2
)) &&
3013 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3017 if ((isl_map_plain_is_empty(map2
) ||
3018 isl_map_plain_is_universe(map1
)) &&
3019 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3024 if (map1
->n
== 1 && map2
->n
== 1 &&
3025 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3026 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3027 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3028 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3029 return map_intersect_add_constraint(map1
, map2
);
3031 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3032 isl_space_dim(map2
->dim
, isl_dim_param
))
3033 isl_assert(map1
->ctx
,
3034 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3036 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3037 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3038 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3040 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3041 map1
->n
* map2
->n
, flags
);
3044 for (i
= 0; i
< map1
->n
; ++i
)
3045 for (j
= 0; j
< map2
->n
; ++j
) {
3046 struct isl_basic_map
*part
;
3047 part
= isl_basic_map_intersect(
3048 isl_basic_map_copy(map1
->p
[i
]),
3049 isl_basic_map_copy(map2
->p
[j
]));
3050 if (isl_basic_map_is_empty(part
) < 0)
3051 part
= isl_basic_map_free(part
);
3052 result
= isl_map_add_basic_map(result
, part
);
3065 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3066 __isl_take isl_map
*map2
)
3070 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3071 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3072 "spaces don't match", goto error
);
3073 return map_intersect_internal(map1
, map2
);
3080 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3081 __isl_take isl_map
*map2
)
3083 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3086 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3088 return (struct isl_set
*)
3089 isl_map_intersect((struct isl_map
*)set1
,
3090 (struct isl_map
*)set2
);
3093 /* map_intersect_internal accepts intersections
3094 * with parameter domains, so we can just call that function.
3096 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3097 __isl_take isl_set
*params
)
3099 return map_intersect_internal(map
, params
);
3102 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3103 __isl_take isl_map
*map2
)
3105 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3108 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3109 __isl_take isl_set
*params
)
3111 return isl_map_intersect_params(set
, params
);
3114 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3117 unsigned pos
, n1
, n2
;
3121 bmap
= isl_basic_map_cow(bmap
);
3124 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3125 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3126 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3127 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3128 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3129 return isl_basic_map_reset_space(bmap
, space
);
3132 static __isl_give isl_basic_map
*basic_map_space_reset(
3133 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3139 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3142 space
= isl_basic_map_get_space(bmap
);
3143 space
= isl_space_reset(space
, type
);
3144 bmap
= isl_basic_map_reset_space(bmap
, space
);
3148 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3149 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3150 unsigned pos
, unsigned n
)
3153 struct isl_basic_map
*res
;
3154 struct isl_dim_map
*dim_map
;
3155 unsigned total
, off
;
3156 enum isl_dim_type t
;
3159 return basic_map_space_reset(bmap
, type
);
3164 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3166 total
= isl_basic_map_total_dim(bmap
) + n
;
3167 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3169 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3171 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3173 unsigned size
= isl_basic_map_dim(bmap
, t
);
3174 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3176 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3177 pos
, size
- pos
, off
+ pos
+ n
);
3179 off
+= isl_space_dim(res_dim
, t
);
3181 isl_dim_map_div(dim_map
, bmap
, off
);
3183 res
= isl_basic_map_alloc_space(res_dim
,
3184 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3185 if (isl_basic_map_is_rational(bmap
))
3186 res
= isl_basic_map_set_rational(res
);
3187 if (isl_basic_map_plain_is_empty(bmap
)) {
3188 isl_basic_map_free(bmap
);
3190 return isl_basic_map_set_to_empty(res
);
3192 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3193 return isl_basic_map_finalize(res
);
3196 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3197 __isl_take isl_basic_set
*bset
,
3198 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3200 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3203 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3204 enum isl_dim_type type
, unsigned n
)
3208 return isl_basic_map_insert_dims(bmap
, type
,
3209 isl_basic_map_dim(bmap
, type
), n
);
3212 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3213 enum isl_dim_type type
, unsigned n
)
3217 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3218 return isl_basic_map_add_dims(bset
, type
, n
);
3220 isl_basic_set_free(bset
);
3224 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3225 enum isl_dim_type type
)
3229 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3232 space
= isl_map_get_space(map
);
3233 space
= isl_space_reset(space
, type
);
3234 map
= isl_map_reset_space(map
, space
);
3238 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3239 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3244 return map_space_reset(map
, type
);
3246 map
= isl_map_cow(map
);
3250 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3254 for (i
= 0; i
< map
->n
; ++i
) {
3255 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3266 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3267 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3269 return isl_map_insert_dims(set
, type
, pos
, n
);
3272 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3273 enum isl_dim_type type
, unsigned n
)
3277 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3280 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3281 enum isl_dim_type type
, unsigned n
)
3285 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3286 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3292 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3293 __isl_take isl_basic_map
*bmap
,
3294 enum isl_dim_type dst_type
, unsigned dst_pos
,
3295 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3297 struct isl_dim_map
*dim_map
;
3298 struct isl_basic_map
*res
;
3299 enum isl_dim_type t
;
3300 unsigned total
, off
;
3307 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3310 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3313 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3315 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3316 pos(bmap
->dim
, src_type
) + src_pos
+
3317 ((src_type
< dst_type
) ? n
: 0)) {
3318 bmap
= isl_basic_map_cow(bmap
);
3322 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3323 src_type
, src_pos
, n
);
3327 bmap
= isl_basic_map_finalize(bmap
);
3332 total
= isl_basic_map_total_dim(bmap
);
3333 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3336 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3337 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3338 if (t
== dst_type
) {
3339 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3342 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3345 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3346 dst_pos
, size
- dst_pos
, off
);
3347 off
+= size
- dst_pos
;
3348 } else if (t
== src_type
) {
3349 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3352 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3353 src_pos
+ n
, size
- src_pos
- n
, off
);
3354 off
+= size
- src_pos
- n
;
3356 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3360 isl_dim_map_div(dim_map
, bmap
, off
);
3362 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3363 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3364 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3368 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3369 src_type
, src_pos
, n
);
3373 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3374 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3375 bmap
= isl_basic_map_finalize(bmap
);
3379 isl_basic_map_free(bmap
);
3383 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3384 enum isl_dim_type dst_type
, unsigned dst_pos
,
3385 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3387 return (isl_basic_set
*)isl_basic_map_move_dims(
3388 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3391 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3392 enum isl_dim_type dst_type
, unsigned dst_pos
,
3393 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3397 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3398 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3399 src_type
, src_pos
, n
);
3405 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3406 enum isl_dim_type dst_type
, unsigned dst_pos
,
3407 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3416 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3419 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3422 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3424 map
= isl_map_cow(map
);
3428 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3432 for (i
= 0; i
< map
->n
; ++i
) {
3433 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3435 src_type
, src_pos
, n
);
3446 /* Move the specified dimensions to the last columns right before
3447 * the divs. Don't change the dimension specification of bmap.
3448 * That's the responsibility of the caller.
3450 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3451 enum isl_dim_type type
, unsigned first
, unsigned n
)
3453 struct isl_dim_map
*dim_map
;
3454 struct isl_basic_map
*res
;
3455 enum isl_dim_type t
;
3456 unsigned total
, off
;
3460 if (pos(bmap
->dim
, type
) + first
+ n
==
3461 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3464 total
= isl_basic_map_total_dim(bmap
);
3465 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3468 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3469 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3471 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3474 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3475 first
, n
, total
- bmap
->n_div
- n
);
3476 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3477 first
+ n
, size
- (first
+ n
), off
);
3478 off
+= size
- (first
+ n
);
3480 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3484 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3486 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3487 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3488 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3492 /* Insert "n" rows in the divs of "bmap".
3494 * The number of columns is not changed, which means that the last
3495 * dimensions of "bmap" are being reintepreted as the new divs.
3496 * The space of "bmap" is not adjusted, however, which means
3497 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3498 * from the space of "bmap" is the responsibility of the caller.
3500 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3508 bmap
= isl_basic_map_cow(bmap
);
3512 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3513 old
= bmap
->block2
.data
;
3514 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3515 (bmap
->extra
+ n
) * (1 + row_size
));
3516 if (!bmap
->block2
.data
)
3517 return isl_basic_map_free(bmap
);
3518 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3520 return isl_basic_map_free(bmap
);
3521 for (i
= 0; i
< n
; ++i
) {
3522 new_div
[i
] = bmap
->block2
.data
+
3523 (bmap
->extra
+ i
) * (1 + row_size
);
3524 isl_seq_clr(new_div
[i
], 1 + row_size
);
3526 for (i
= 0; i
< bmap
->extra
; ++i
)
3527 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3529 bmap
->div
= new_div
;
3536 /* Drop constraints from "bmap" that only involve the variables
3537 * of "type" in the range [first, first + n] that are not related
3538 * to any of the variables outside that interval.
3539 * These constraints cannot influence the values for the variables
3540 * outside the interval, except in case they cause "bmap" to be empty.
3541 * Only drop the constraints if "bmap" is known to be non-empty.
3543 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3544 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3545 unsigned first
, unsigned n
)
3549 unsigned dim
, n_div
;
3552 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3554 return isl_basic_map_free(bmap
);
3558 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3559 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3560 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3562 return isl_basic_map_free(bmap
);
3563 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3564 for (i
= 0; i
< first
; ++i
)
3566 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3569 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3574 /* Turn the n dimensions of type type, starting at first
3575 * into existentially quantified variables.
3577 * If a subset of the projected out variables are unrelated
3578 * to any of the variables that remain, then the constraints
3579 * involving this subset are simply dropped first.
3581 __isl_give isl_basic_map
*isl_basic_map_project_out(
3582 __isl_take isl_basic_map
*bmap
,
3583 enum isl_dim_type type
, unsigned first
, unsigned n
)
3586 return basic_map_space_reset(bmap
, type
);
3587 if (type
== isl_dim_div
)
3588 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3589 "cannot project out existentially quantified variables",
3590 return isl_basic_map_free(bmap
));
3592 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3596 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3597 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3599 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3602 bmap
= move_last(bmap
, type
, first
, n
);
3603 bmap
= isl_basic_map_cow(bmap
);
3604 bmap
= insert_div_rows(bmap
, n
);
3608 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3611 bmap
= isl_basic_map_simplify(bmap
);
3612 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3613 return isl_basic_map_finalize(bmap
);
3615 isl_basic_map_free(bmap
);
3619 /* Turn the n dimensions of type type, starting at first
3620 * into existentially quantified variables.
3622 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3623 enum isl_dim_type type
, unsigned first
, unsigned n
)
3625 return (isl_basic_set
*)isl_basic_map_project_out(
3626 (isl_basic_map
*)bset
, type
, first
, n
);
3629 /* Turn the n dimensions of type type, starting at first
3630 * into existentially quantified variables.
3632 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3633 enum isl_dim_type type
, unsigned first
, unsigned n
)
3641 return map_space_reset(map
, type
);
3643 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3645 map
= isl_map_cow(map
);
3649 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3653 for (i
= 0; i
< map
->n
; ++i
) {
3654 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3665 /* Turn the n dimensions of type type, starting at first
3666 * into existentially quantified variables.
3668 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3669 enum isl_dim_type type
, unsigned first
, unsigned n
)
3671 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3674 /* Return a map that projects the elements in "set" onto their
3675 * "n" set dimensions starting at "first".
3676 * "type" should be equal to isl_dim_set.
3678 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3679 enum isl_dim_type type
, unsigned first
, unsigned n
)
3687 if (type
!= isl_dim_set
)
3688 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3689 "only set dimensions can be projected out", goto error
);
3690 dim
= isl_set_dim(set
, isl_dim_set
);
3691 if (first
+ n
> dim
|| first
+ n
< first
)
3692 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3693 "index out of bounds", goto error
);
3695 map
= isl_map_from_domain(set
);
3696 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3697 for (i
= 0; i
< n
; ++i
)
3698 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3706 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3710 for (i
= 0; i
< n
; ++i
) {
3711 j
= isl_basic_map_alloc_div(bmap
);
3714 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3718 isl_basic_map_free(bmap
);
3722 struct isl_basic_map
*isl_basic_map_apply_range(
3723 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3725 isl_space
*dim_result
= NULL
;
3726 struct isl_basic_map
*bmap
;
3727 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3728 struct isl_dim_map
*dim_map1
, *dim_map2
;
3730 if (!bmap1
|| !bmap2
)
3732 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3733 bmap2
->dim
, isl_dim_param
))
3734 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3735 "parameters don't match", goto error
);
3736 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3737 bmap2
->dim
, isl_dim_in
))
3738 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3739 "spaces don't match", goto error
);
3741 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3742 isl_space_copy(bmap2
->dim
));
3744 n_in
= isl_basic_map_n_in(bmap1
);
3745 n_out
= isl_basic_map_n_out(bmap2
);
3746 n
= isl_basic_map_n_out(bmap1
);
3747 nparam
= isl_basic_map_n_param(bmap1
);
3749 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3750 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3751 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3752 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3753 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3754 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3755 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3756 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3757 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3758 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3759 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3761 bmap
= isl_basic_map_alloc_space(dim_result
,
3762 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3763 bmap1
->n_eq
+ bmap2
->n_eq
,
3764 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3765 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3766 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3767 bmap
= add_divs(bmap
, n
);
3768 bmap
= isl_basic_map_simplify(bmap
);
3769 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3770 return isl_basic_map_finalize(bmap
);
3772 isl_basic_map_free(bmap1
);
3773 isl_basic_map_free(bmap2
);
3777 struct isl_basic_set
*isl_basic_set_apply(
3778 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3783 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3786 return (struct isl_basic_set
*)
3787 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3789 isl_basic_set_free(bset
);
3790 isl_basic_map_free(bmap
);
3794 struct isl_basic_map
*isl_basic_map_apply_domain(
3795 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3797 if (!bmap1
|| !bmap2
)
3800 isl_assert(bmap1
->ctx
,
3801 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3802 isl_assert(bmap1
->ctx
,
3803 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3806 bmap1
= isl_basic_map_reverse(bmap1
);
3807 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3808 return isl_basic_map_reverse(bmap1
);
3810 isl_basic_map_free(bmap1
);
3811 isl_basic_map_free(bmap2
);
3815 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3816 * A \cap B -> f(A) + f(B)
3818 struct isl_basic_map
*isl_basic_map_sum(
3819 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3821 unsigned n_in
, n_out
, nparam
, total
, pos
;
3822 struct isl_basic_map
*bmap
= NULL
;
3823 struct isl_dim_map
*dim_map1
, *dim_map2
;
3826 if (!bmap1
|| !bmap2
)
3829 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3832 nparam
= isl_basic_map_n_param(bmap1
);
3833 n_in
= isl_basic_map_n_in(bmap1
);
3834 n_out
= isl_basic_map_n_out(bmap1
);
3836 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3837 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3838 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3839 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3840 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3841 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3842 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3843 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3844 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3845 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3846 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3848 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3849 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3850 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3851 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3852 for (i
= 0; i
< n_out
; ++i
) {
3853 int j
= isl_basic_map_alloc_equality(bmap
);
3856 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3857 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3858 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3859 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3861 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3862 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3863 bmap
= add_divs(bmap
, 2 * n_out
);
3865 bmap
= isl_basic_map_simplify(bmap
);
3866 return isl_basic_map_finalize(bmap
);
3868 isl_basic_map_free(bmap
);
3869 isl_basic_map_free(bmap1
);
3870 isl_basic_map_free(bmap2
);
3874 /* Given two maps A -> f(A) and B -> g(B), construct a map
3875 * A \cap B -> f(A) + f(B)
3877 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3879 struct isl_map
*result
;
3885 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3887 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3888 map1
->n
* map2
->n
, 0);
3891 for (i
= 0; i
< map1
->n
; ++i
)
3892 for (j
= 0; j
< map2
->n
; ++j
) {
3893 struct isl_basic_map
*part
;
3894 part
= isl_basic_map_sum(
3895 isl_basic_map_copy(map1
->p
[i
]),
3896 isl_basic_map_copy(map2
->p
[j
]));
3897 if (isl_basic_map_is_empty(part
))
3898 isl_basic_map_free(part
);
3900 result
= isl_map_add_basic_map(result
, part
);
3913 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3914 __isl_take isl_set
*set2
)
3916 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3919 /* Given a basic map A -> f(A), construct A -> -f(A).
3921 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3926 bmap
= isl_basic_map_cow(bmap
);
3930 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3931 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3932 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3933 for (j
= 0; j
< n
; ++j
)
3934 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3935 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3936 for (j
= 0; j
< n
; ++j
)
3937 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3938 for (i
= 0; i
< bmap
->n_div
; ++i
)
3939 for (j
= 0; j
< n
; ++j
)
3940 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3941 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3942 return isl_basic_map_finalize(bmap
);
3945 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3947 return isl_basic_map_neg(bset
);
3950 /* Given a map A -> f(A), construct A -> -f(A).
3952 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3956 map
= isl_map_cow(map
);
3960 for (i
= 0; i
< map
->n
; ++i
) {
3961 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3972 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3974 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3977 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3978 * A -> floor(f(A)/d).
3980 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3983 unsigned n_in
, n_out
, nparam
, total
, pos
;
3984 struct isl_basic_map
*result
= NULL
;
3985 struct isl_dim_map
*dim_map
;
3991 nparam
= isl_basic_map_n_param(bmap
);
3992 n_in
= isl_basic_map_n_in(bmap
);
3993 n_out
= isl_basic_map_n_out(bmap
);
3995 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3996 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3997 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3998 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3999 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4000 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4002 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4003 bmap
->n_div
+ n_out
,
4004 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4005 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4006 result
= add_divs(result
, n_out
);
4007 for (i
= 0; i
< n_out
; ++i
) {
4009 j
= isl_basic_map_alloc_inequality(result
);
4012 isl_seq_clr(result
->ineq
[j
], 1+total
);
4013 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4014 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4015 j
= isl_basic_map_alloc_inequality(result
);
4018 isl_seq_clr(result
->ineq
[j
], 1+total
);
4019 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4020 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4021 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4024 result
= isl_basic_map_simplify(result
);
4025 return isl_basic_map_finalize(result
);
4027 isl_basic_map_free(result
);
4031 /* Given a map A -> f(A) and an integer d, construct a map
4032 * A -> floor(f(A)/d).
4034 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4038 map
= isl_map_cow(map
);
4042 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4043 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4044 for (i
= 0; i
< map
->n
; ++i
) {
4045 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4056 /* Given a map A -> f(A) and an integer d, construct a map
4057 * A -> floor(f(A)/d).
4059 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4060 __isl_take isl_val
*d
)
4064 if (!isl_val_is_int(d
))
4065 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4066 "expecting integer denominator", goto error
);
4067 map
= isl_map_floordiv(map
, d
->n
);
4076 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4082 i
= isl_basic_map_alloc_equality(bmap
);
4085 nparam
= isl_basic_map_n_param(bmap
);
4086 n_in
= isl_basic_map_n_in(bmap
);
4087 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4088 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4089 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4090 return isl_basic_map_finalize(bmap
);
4092 isl_basic_map_free(bmap
);
4096 /* Add a constraints to "bmap" expressing i_pos < o_pos
4098 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4104 i
= isl_basic_map_alloc_inequality(bmap
);
4107 nparam
= isl_basic_map_n_param(bmap
);
4108 n_in
= isl_basic_map_n_in(bmap
);
4109 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4110 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4111 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4112 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4113 return isl_basic_map_finalize(bmap
);
4115 isl_basic_map_free(bmap
);
4119 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4121 static __isl_give isl_basic_map
*var_less_or_equal(
4122 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4128 i
= isl_basic_map_alloc_inequality(bmap
);
4131 nparam
= isl_basic_map_n_param(bmap
);
4132 n_in
= isl_basic_map_n_in(bmap
);
4133 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4134 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4135 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4136 return isl_basic_map_finalize(bmap
);
4138 isl_basic_map_free(bmap
);
4142 /* Add a constraints to "bmap" expressing i_pos > o_pos
4144 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4150 i
= isl_basic_map_alloc_inequality(bmap
);
4153 nparam
= isl_basic_map_n_param(bmap
);
4154 n_in
= isl_basic_map_n_in(bmap
);
4155 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4156 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4157 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4158 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4159 return isl_basic_map_finalize(bmap
);
4161 isl_basic_map_free(bmap
);
4165 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4167 static __isl_give isl_basic_map
*var_more_or_equal(
4168 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4174 i
= isl_basic_map_alloc_inequality(bmap
);
4177 nparam
= isl_basic_map_n_param(bmap
);
4178 n_in
= isl_basic_map_n_in(bmap
);
4179 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4180 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4181 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4182 return isl_basic_map_finalize(bmap
);
4184 isl_basic_map_free(bmap
);
4188 __isl_give isl_basic_map
*isl_basic_map_equal(
4189 __isl_take isl_space
*dim
, unsigned n_equal
)
4192 struct isl_basic_map
*bmap
;
4193 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4196 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4197 bmap
= var_equal(bmap
, i
);
4198 return isl_basic_map_finalize(bmap
);
4201 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4203 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4207 struct isl_basic_map
*bmap
;
4208 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4211 for (i
= 0; i
< pos
&& bmap
; ++i
)
4212 bmap
= var_equal(bmap
, i
);
4214 bmap
= var_less(bmap
, pos
);
4215 return isl_basic_map_finalize(bmap
);
4218 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4220 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4221 __isl_take isl_space
*dim
, unsigned pos
)
4224 isl_basic_map
*bmap
;
4226 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4227 for (i
= 0; i
< pos
; ++i
)
4228 bmap
= var_equal(bmap
, i
);
4229 bmap
= var_less_or_equal(bmap
, pos
);
4230 return isl_basic_map_finalize(bmap
);
4233 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4235 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4239 struct isl_basic_map
*bmap
;
4240 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4243 for (i
= 0; i
< pos
&& bmap
; ++i
)
4244 bmap
= var_equal(bmap
, i
);
4246 bmap
= var_more(bmap
, pos
);
4247 return isl_basic_map_finalize(bmap
);
4250 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4252 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4253 __isl_take isl_space
*dim
, unsigned pos
)
4256 isl_basic_map
*bmap
;
4258 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4259 for (i
= 0; i
< pos
; ++i
)
4260 bmap
= var_equal(bmap
, i
);
4261 bmap
= var_more_or_equal(bmap
, pos
);
4262 return isl_basic_map_finalize(bmap
);
4265 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4266 unsigned n
, int equal
)
4268 struct isl_map
*map
;
4271 if (n
== 0 && equal
)
4272 return isl_map_universe(dims
);
4274 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4276 for (i
= 0; i
+ 1 < n
; ++i
)
4277 map
= isl_map_add_basic_map(map
,
4278 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4281 map
= isl_map_add_basic_map(map
,
4282 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4284 map
= isl_map_add_basic_map(map
,
4285 isl_basic_map_less_at(dims
, n
- 1));
4287 isl_space_free(dims
);
4292 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4296 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4299 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4301 return map_lex_lte_first(dim
, n
, 0);
4304 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4306 return map_lex_lte_first(dim
, n
, 1);
4309 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4311 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4314 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4316 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4319 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4320 unsigned n
, int equal
)
4322 struct isl_map
*map
;
4325 if (n
== 0 && equal
)
4326 return isl_map_universe(dims
);
4328 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4330 for (i
= 0; i
+ 1 < n
; ++i
)
4331 map
= isl_map_add_basic_map(map
,
4332 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4335 map
= isl_map_add_basic_map(map
,
4336 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4338 map
= isl_map_add_basic_map(map
,
4339 isl_basic_map_more_at(dims
, n
- 1));
4341 isl_space_free(dims
);
4346 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4350 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4353 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4355 return map_lex_gte_first(dim
, n
, 0);
4358 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4360 return map_lex_gte_first(dim
, n
, 1);
4363 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4365 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4368 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4370 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4373 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4374 __isl_take isl_set
*set2
)
4377 map
= isl_map_lex_le(isl_set_get_space(set1
));
4378 map
= isl_map_intersect_domain(map
, set1
);
4379 map
= isl_map_intersect_range(map
, set2
);
4383 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4384 __isl_take isl_set
*set2
)
4387 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4388 map
= isl_map_intersect_domain(map
, set1
);
4389 map
= isl_map_intersect_range(map
, set2
);
4393 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4394 __isl_take isl_set
*set2
)
4397 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4398 map
= isl_map_intersect_domain(map
, set1
);
4399 map
= isl_map_intersect_range(map
, set2
);
4403 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4404 __isl_take isl_set
*set2
)
4407 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4408 map
= isl_map_intersect_domain(map
, set1
);
4409 map
= isl_map_intersect_range(map
, set2
);
4413 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4414 __isl_take isl_map
*map2
)
4417 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4418 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4419 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4423 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4424 __isl_take isl_map
*map2
)
4427 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4428 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4429 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4433 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4434 __isl_take isl_map
*map2
)
4437 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4438 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4439 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4443 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4444 __isl_take isl_map
*map2
)
4447 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4448 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4449 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4453 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4454 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4456 struct isl_basic_map
*bmap
;
4458 bset
= isl_basic_set_cow(bset
);
4462 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4463 isl_space_free(bset
->dim
);
4464 bmap
= (struct isl_basic_map
*) bset
;
4466 return isl_basic_map_finalize(bmap
);
4468 isl_basic_set_free(bset
);
4469 isl_space_free(dim
);
4473 /* For a div d = floor(f/m), add the constraint
4477 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4478 unsigned pos
, isl_int
*div
)
4481 unsigned total
= isl_basic_map_total_dim(bmap
);
4483 i
= isl_basic_map_alloc_inequality(bmap
);
4486 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4487 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4492 /* For a div d = floor(f/m), add the constraint
4494 * -(f-(m-1)) + m d >= 0
4496 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4497 unsigned pos
, isl_int
*div
)
4500 unsigned total
= isl_basic_map_total_dim(bmap
);
4502 i
= isl_basic_map_alloc_inequality(bmap
);
4505 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4506 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4507 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4508 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4513 /* For a div d = floor(f/m), add the constraints
4516 * -(f-(m-1)) + m d >= 0
4518 * Note that the second constraint is the negation of
4522 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4523 unsigned pos
, isl_int
*div
)
4525 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4527 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4532 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4533 unsigned pos
, isl_int
*div
)
4535 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4539 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4541 unsigned total
= isl_basic_map_total_dim(bmap
);
4542 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4544 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4548 /* For each known div d = floor(f/m), add the constraints
4551 * -(f-(m-1)) + m d >= 0
4553 * Remove duplicate constraints in case of some these div constraints
4554 * already appear in "bmap".
4556 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4557 __isl_take isl_basic_map
*bmap
)
4563 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4567 bmap
= add_known_div_constraints(bmap
);
4568 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4569 bmap
= isl_basic_map_finalize(bmap
);
4573 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4575 * In particular, if this div is of the form d = floor(f/m),
4576 * then add the constraint
4580 * if sign < 0 or the constraint
4582 * -(f-(m-1)) + m d >= 0
4586 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4587 unsigned div
, int sign
)
4595 total
= isl_basic_map_total_dim(bmap
);
4596 div_pos
= total
- bmap
->n_div
+ div
;
4599 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4601 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4604 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4606 return isl_basic_map_add_div_constraints(bset
, div
);
4609 struct isl_basic_set
*isl_basic_map_underlying_set(
4610 struct isl_basic_map
*bmap
)
4614 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4616 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4617 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4618 return (struct isl_basic_set
*)bmap
;
4619 bmap
= isl_basic_map_cow(bmap
);
4622 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4625 bmap
->extra
-= bmap
->n_div
;
4627 bmap
= isl_basic_map_finalize(bmap
);
4628 return (struct isl_basic_set
*)bmap
;
4630 isl_basic_map_free(bmap
);
4634 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4635 __isl_take isl_basic_set
*bset
)
4637 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4640 /* Replace each element in "list" by the result of applying
4641 * isl_basic_map_underlying_set to the element.
4643 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4644 __isl_take isl_basic_map_list
*list
)
4651 n
= isl_basic_map_list_n_basic_map(list
);
4652 for (i
= 0; i
< n
; ++i
) {
4653 isl_basic_map
*bmap
;
4654 isl_basic_set
*bset
;
4656 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4657 bset
= isl_basic_set_underlying_set(bmap
);
4658 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4664 struct isl_basic_map
*isl_basic_map_overlying_set(
4665 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4667 struct isl_basic_map
*bmap
;
4668 struct isl_ctx
*ctx
;
4675 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4676 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4677 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4679 if (like
->n_div
== 0) {
4680 isl_space
*space
= isl_basic_map_get_space(like
);
4681 isl_basic_map_free(like
);
4682 return isl_basic_map_reset_space(bset
, space
);
4684 bset
= isl_basic_set_cow(bset
);
4687 total
= bset
->dim
->n_out
+ bset
->extra
;
4688 bmap
= (struct isl_basic_map
*)bset
;
4689 isl_space_free(bmap
->dim
);
4690 bmap
->dim
= isl_space_copy(like
->dim
);
4693 bmap
->n_div
= like
->n_div
;
4694 bmap
->extra
+= like
->n_div
;
4698 ltotal
= total
- bmap
->extra
+ like
->extra
;
4701 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4702 bmap
->extra
* (1 + 1 + total
));
4703 if (isl_blk_is_error(bmap
->block2
))
4705 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4709 for (i
= 0; i
< bmap
->extra
; ++i
)
4710 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4711 for (i
= 0; i
< like
->n_div
; ++i
) {
4712 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4713 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4715 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4717 isl_basic_map_free(like
);
4718 bmap
= isl_basic_map_simplify(bmap
);
4719 bmap
= isl_basic_map_finalize(bmap
);
4722 isl_basic_map_free(like
);
4723 isl_basic_set_free(bset
);
4727 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4728 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4730 return (struct isl_basic_set
*)
4731 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4734 struct isl_set
*isl_set_from_underlying_set(
4735 struct isl_set
*set
, struct isl_basic_set
*like
)
4741 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4743 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4744 isl_basic_set_free(like
);
4747 set
= isl_set_cow(set
);
4750 for (i
= 0; i
< set
->n
; ++i
) {
4751 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4752 isl_basic_set_copy(like
));
4756 isl_space_free(set
->dim
);
4757 set
->dim
= isl_space_copy(like
->dim
);
4760 isl_basic_set_free(like
);
4763 isl_basic_set_free(like
);
4768 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4772 map
= isl_map_cow(map
);
4775 map
->dim
= isl_space_cow(map
->dim
);
4779 for (i
= 1; i
< map
->n
; ++i
)
4780 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4782 for (i
= 0; i
< map
->n
; ++i
) {
4783 map
->p
[i
] = (struct isl_basic_map
*)
4784 isl_basic_map_underlying_set(map
->p
[i
]);
4789 map
->dim
= isl_space_underlying(map
->dim
, 0);
4791 isl_space_free(map
->dim
);
4792 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4796 return (struct isl_set
*)map
;
4802 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4804 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4807 /* Replace the space of "bmap" by "space".
4809 * If the space of "bmap" is identical to "space" (including the identifiers
4810 * of the input and output dimensions), then simply return the original input.
4812 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4813 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4819 equal
= isl_space_is_equal(bmap
->dim
, space
);
4820 if (equal
>= 0 && equal
)
4821 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4823 if (equal
>= 0 && equal
)
4824 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4825 space
, isl_dim_out
);
4829 isl_space_free(space
);
4832 bmap
= isl_basic_map_cow(bmap
);
4833 if (!bmap
|| !space
)
4836 isl_space_free(bmap
->dim
);
4839 bmap
= isl_basic_map_finalize(bmap
);
4843 isl_basic_map_free(bmap
);
4844 isl_space_free(space
);
4848 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4849 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4851 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4855 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4856 __isl_take isl_space
*dim
)
4860 map
= isl_map_cow(map
);
4864 for (i
= 0; i
< map
->n
; ++i
) {
4865 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4866 isl_space_copy(dim
));
4870 isl_space_free(map
->dim
);
4876 isl_space_free(dim
);
4880 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4881 __isl_take isl_space
*dim
)
4883 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4886 /* Compute the parameter domain of the given basic set.
4888 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4893 if (isl_basic_set_is_params(bset
))
4896 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4897 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4898 space
= isl_basic_set_get_space(bset
);
4899 space
= isl_space_params(space
);
4900 bset
= isl_basic_set_reset_space(bset
, space
);
4904 /* Construct a zero-dimensional basic set with the given parameter domain.
4906 __isl_give isl_basic_set
*isl_basic_set_from_params(
4907 __isl_take isl_basic_set
*bset
)
4910 space
= isl_basic_set_get_space(bset
);
4911 space
= isl_space_set_from_params(space
);
4912 bset
= isl_basic_set_reset_space(bset
, space
);
4916 /* Compute the parameter domain of the given set.
4918 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4923 if (isl_set_is_params(set
))
4926 n
= isl_set_dim(set
, isl_dim_set
);
4927 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4928 space
= isl_set_get_space(set
);
4929 space
= isl_space_params(space
);
4930 set
= isl_set_reset_space(set
, space
);
4934 /* Construct a zero-dimensional set with the given parameter domain.
4936 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4939 space
= isl_set_get_space(set
);
4940 space
= isl_space_set_from_params(space
);
4941 set
= isl_set_reset_space(set
, space
);
4945 /* Compute the parameter domain of the given map.
4947 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4952 n
= isl_map_dim(map
, isl_dim_in
);
4953 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4954 n
= isl_map_dim(map
, isl_dim_out
);
4955 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4956 space
= isl_map_get_space(map
);
4957 space
= isl_space_params(space
);
4958 map
= isl_map_reset_space(map
, space
);
4962 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4969 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4971 n_out
= isl_basic_map_n_out(bmap
);
4972 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4974 return isl_basic_map_reset_space(bmap
, space
);
4977 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4981 return isl_space_may_be_set(bmap
->dim
);
4984 /* Is this basic map actually a set?
4985 * Users should never call this function. Outside of isl,
4986 * the type should indicate whether something is a set or a map.
4988 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4992 return isl_space_is_set(bmap
->dim
);
4995 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4999 if (isl_basic_map_is_set(bmap
))
5001 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5004 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5005 __isl_take isl_basic_map
*bmap
)
5009 isl_basic_map
*domain
;
5010 int nparam
, n_in
, n_out
;
5013 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5014 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5015 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5017 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5018 domain
= isl_basic_map_universe(dim
);
5020 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5021 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5022 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5024 total
= isl_basic_map_total_dim(bmap
);
5026 for (i
= 0; i
< n_in
; ++i
) {
5027 k
= isl_basic_map_alloc_equality(bmap
);
5030 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5031 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5032 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5035 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5036 return isl_basic_map_finalize(bmap
);
5038 isl_basic_map_free(bmap
);
5042 __isl_give isl_basic_map
*isl_basic_map_range_map(
5043 __isl_take isl_basic_map
*bmap
)
5047 isl_basic_map
*range
;
5048 int nparam
, n_in
, n_out
;
5051 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5052 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5053 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5055 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5056 range
= isl_basic_map_universe(dim
);
5058 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5059 bmap
= isl_basic_map_apply_range(bmap
, range
);
5060 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5062 total
= isl_basic_map_total_dim(bmap
);
5064 for (i
= 0; i
< n_out
; ++i
) {
5065 k
= isl_basic_map_alloc_equality(bmap
);
5068 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5069 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5070 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5073 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5074 return isl_basic_map_finalize(bmap
);
5076 isl_basic_map_free(bmap
);
5080 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5084 return isl_space_may_be_set(map
->dim
);
5087 /* Is this map actually a set?
5088 * Users should never call this function. Outside of isl,
5089 * the type should indicate whether something is a set or a map.
5091 int isl_map_is_set(__isl_keep isl_map
*map
)
5095 return isl_space_is_set(map
->dim
);
5098 struct isl_set
*isl_map_range(struct isl_map
*map
)
5101 struct isl_set
*set
;
5105 if (isl_map_is_set(map
))
5106 return (isl_set
*)map
;
5108 map
= isl_map_cow(map
);
5112 set
= (struct isl_set
*) map
;
5113 set
->dim
= isl_space_range(set
->dim
);
5116 for (i
= 0; i
< map
->n
; ++i
) {
5117 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5121 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5122 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5129 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5133 map
= isl_map_cow(map
);
5137 map
->dim
= isl_space_domain_map(map
->dim
);
5140 for (i
= 0; i
< map
->n
; ++i
) {
5141 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5145 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5146 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5153 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5156 isl_space
*range_dim
;
5158 map
= isl_map_cow(map
);
5162 range_dim
= isl_space_range(isl_map_get_space(map
));
5163 range_dim
= isl_space_from_range(range_dim
);
5164 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5165 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5168 for (i
= 0; i
< map
->n
; ++i
) {
5169 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5173 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5174 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5181 /* Given a wrapped map of the form A[B -> C],
5182 * return the map A[B -> C] -> B.
5184 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5191 if (!isl_set_has_tuple_id(set
))
5192 return isl_map_domain_map(isl_set_unwrap(set
));
5194 id
= isl_set_get_tuple_id(set
);
5195 map
= isl_map_domain_map(isl_set_unwrap(set
));
5196 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5201 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5202 __isl_take isl_space
*dim
)
5205 struct isl_map
*map
= NULL
;
5207 set
= isl_set_cow(set
);
5210 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5211 map
= (struct isl_map
*)set
;
5212 for (i
= 0; i
< set
->n
; ++i
) {
5213 map
->p
[i
] = isl_basic_map_from_basic_set(
5214 set
->p
[i
], isl_space_copy(dim
));
5218 isl_space_free(map
->dim
);
5222 isl_space_free(dim
);
5227 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5228 __isl_take isl_basic_set
*bset
)
5230 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5233 __isl_give isl_basic_map
*isl_basic_map_from_range(
5234 __isl_take isl_basic_set
*bset
)
5237 space
= isl_basic_set_get_space(bset
);
5238 space
= isl_space_from_range(space
);
5239 bset
= isl_basic_set_reset_space(bset
, space
);
5240 return (isl_basic_map
*)bset
;
5243 /* Create a relation with the given set as range.
5244 * The domain of the created relation is a zero-dimensional
5245 * flat anonymous space.
5247 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5250 space
= isl_set_get_space(set
);
5251 space
= isl_space_from_range(space
);
5252 set
= isl_set_reset_space(set
, space
);
5253 return (struct isl_map
*)set
;
5256 /* Create a relation with the given set as domain.
5257 * The range of the created relation is a zero-dimensional
5258 * flat anonymous space.
5260 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5262 return isl_map_reverse(isl_map_from_range(set
));
5265 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5266 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5268 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5271 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5272 __isl_take isl_set
*range
)
5274 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5277 /* Return a newly allocated isl_map with given space and flags and
5278 * room for "n" basic maps.
5279 * Make sure that all cached information is cleared.
5281 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5284 struct isl_map
*map
;
5289 isl_die(space
->ctx
, isl_error_internal
,
5290 "negative number of basic maps", goto error
);
5291 map
= isl_calloc(space
->ctx
, struct isl_map
,
5292 sizeof(struct isl_map
) +
5293 (n
- 1) * sizeof(struct isl_basic_map
*));
5297 map
->ctx
= space
->ctx
;
5298 isl_ctx_ref(map
->ctx
);
5306 isl_space_free(space
);
5310 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5311 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5314 struct isl_map
*map
;
5317 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5321 map
= isl_map_alloc_space(dims
, n
, flags
);
5325 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5327 struct isl_basic_map
*bmap
;
5328 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5329 bmap
= isl_basic_map_set_to_empty(bmap
);
5333 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5335 struct isl_basic_set
*bset
;
5336 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5337 bset
= isl_basic_set_set_to_empty(bset
);
5341 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5343 struct isl_basic_map
*bmap
;
5344 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5345 bmap
= isl_basic_map_finalize(bmap
);
5349 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5351 struct isl_basic_set
*bset
;
5352 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5353 bset
= isl_basic_set_finalize(bset
);
5357 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5360 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5361 isl_basic_map
*bmap
;
5363 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5364 for (i
= 0; i
< total
; ++i
) {
5365 int k
= isl_basic_map_alloc_inequality(bmap
);
5368 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5369 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5373 isl_basic_map_free(bmap
);
5377 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5379 return isl_basic_map_nat_universe(dim
);
5382 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5384 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5387 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5389 return isl_map_nat_universe(dim
);
5392 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5394 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5397 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5399 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5402 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5404 struct isl_map
*map
;
5407 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5408 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5412 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5414 struct isl_set
*set
;
5417 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5418 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5422 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5425 struct isl_map
*dup
;
5429 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5430 for (i
= 0; i
< map
->n
; ++i
)
5431 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5435 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5436 __isl_take isl_basic_map
*bmap
)
5440 if (isl_basic_map_plain_is_empty(bmap
)) {
5441 isl_basic_map_free(bmap
);
5444 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5445 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5446 map
->p
[map
->n
] = bmap
;
5448 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5454 isl_basic_map_free(bmap
);
5458 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5469 isl_ctx_deref(map
->ctx
);
5470 for (i
= 0; i
< map
->n
; ++i
)
5471 isl_basic_map_free(map
->p
[i
]);
5472 isl_space_free(map
->dim
);
5478 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5479 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5483 bmap
= isl_basic_map_cow(bmap
);
5484 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5485 j
= isl_basic_map_alloc_equality(bmap
);
5488 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5489 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5490 isl_int_set_si(bmap
->eq
[j
][0], value
);
5491 bmap
= isl_basic_map_simplify(bmap
);
5492 return isl_basic_map_finalize(bmap
);
5494 isl_basic_map_free(bmap
);
5498 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5499 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5503 bmap
= isl_basic_map_cow(bmap
);
5504 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5505 j
= isl_basic_map_alloc_equality(bmap
);
5508 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5509 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5510 isl_int_set(bmap
->eq
[j
][0], value
);
5511 bmap
= isl_basic_map_simplify(bmap
);
5512 return isl_basic_map_finalize(bmap
);
5514 isl_basic_map_free(bmap
);
5518 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5519 enum isl_dim_type type
, unsigned pos
, int value
)
5523 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5524 return isl_basic_map_fix_pos_si(bmap
,
5525 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5527 isl_basic_map_free(bmap
);
5531 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5532 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5536 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5537 return isl_basic_map_fix_pos(bmap
,
5538 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5540 isl_basic_map_free(bmap
);
5544 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5545 * to be equal to "v".
5547 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5548 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5552 if (!isl_val_is_int(v
))
5553 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5554 "expecting integer value", goto error
);
5555 if (pos
>= isl_basic_map_dim(bmap
, type
))
5556 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5557 "index out of bounds", goto error
);
5558 pos
+= isl_basic_map_offset(bmap
, type
);
5559 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5563 isl_basic_map_free(bmap
);
5568 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5569 * to be equal to "v".
5571 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5572 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5574 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5577 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5578 enum isl_dim_type type
, unsigned pos
, int value
)
5580 return (struct isl_basic_set
*)
5581 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5585 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5586 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5588 return (struct isl_basic_set
*)
5589 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5593 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5594 unsigned input
, int value
)
5596 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5599 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5600 unsigned dim
, int value
)
5602 return (struct isl_basic_set
*)
5603 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5604 isl_dim_set
, dim
, value
);
5607 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5609 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5616 isl_basic_map_free(map
->p
[i
]);
5617 if (i
!= map
->n
- 1) {
5618 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5619 map
->p
[i
] = map
->p
[map
->n
- 1];
5626 /* Perform "fn" on each basic map of "map", where we may not be holding
5627 * the only reference to "map".
5628 * In particular, "fn" should be a semantics preserving operation
5629 * that we want to apply to all copies of "map". We therefore need
5630 * to be careful not to modify "map" in a way that breaks "map"
5631 * in case anything goes wrong.
5633 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5634 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5636 struct isl_basic_map
*bmap
;
5642 for (i
= map
->n
- 1; i
>= 0; --i
) {
5643 bmap
= isl_basic_map_copy(map
->p
[i
]);
5647 isl_basic_map_free(map
->p
[i
]);
5649 if (remove_if_empty(map
, i
) < 0)
5659 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5660 enum isl_dim_type type
, unsigned pos
, int value
)
5664 map
= isl_map_cow(map
);
5668 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5669 for (i
= map
->n
- 1; i
>= 0; --i
) {
5670 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5671 if (remove_if_empty(map
, i
) < 0)
5674 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5681 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5682 enum isl_dim_type type
, unsigned pos
, int value
)
5684 return (struct isl_set
*)
5685 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5688 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5689 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5693 map
= isl_map_cow(map
);
5697 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5698 for (i
= 0; i
< map
->n
; ++i
) {
5699 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5703 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5710 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5711 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5713 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5716 /* Fix the value of the variable at position "pos" of type "type" of "map"
5717 * to be equal to "v".
5719 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5720 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5724 map
= isl_map_cow(map
);
5728 if (!isl_val_is_int(v
))
5729 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5730 "expecting integer value", goto error
);
5731 if (pos
>= isl_map_dim(map
, type
))
5732 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5733 "index out of bounds", goto error
);
5734 for (i
= map
->n
- 1; i
>= 0; --i
) {
5735 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5737 if (remove_if_empty(map
, i
) < 0)
5740 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5749 /* Fix the value of the variable at position "pos" of type "type" of "set"
5750 * to be equal to "v".
5752 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5753 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5755 return isl_map_fix_val(set
, type
, pos
, v
);
5758 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5759 unsigned input
, int value
)
5761 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5764 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5766 return (struct isl_set
*)
5767 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5770 static __isl_give isl_basic_map
*basic_map_bound_si(
5771 __isl_take isl_basic_map
*bmap
,
5772 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5778 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5779 pos
+= isl_basic_map_offset(bmap
, type
);
5780 bmap
= isl_basic_map_cow(bmap
);
5781 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5782 j
= isl_basic_map_alloc_inequality(bmap
);
5785 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5787 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5788 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5790 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5791 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5793 bmap
= isl_basic_map_simplify(bmap
);
5794 return isl_basic_map_finalize(bmap
);
5796 isl_basic_map_free(bmap
);
5800 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5801 __isl_take isl_basic_map
*bmap
,
5802 enum isl_dim_type type
, unsigned pos
, int value
)
5804 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5807 /* Constrain the values of the given dimension to be no greater than "value".
5809 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5810 __isl_take isl_basic_map
*bmap
,
5811 enum isl_dim_type type
, unsigned pos
, int value
)
5813 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5816 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5817 unsigned dim
, isl_int value
)
5821 bset
= isl_basic_set_cow(bset
);
5822 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5823 j
= isl_basic_set_alloc_inequality(bset
);
5826 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5827 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5828 isl_int_neg(bset
->ineq
[j
][0], value
);
5829 bset
= isl_basic_set_simplify(bset
);
5830 return isl_basic_set_finalize(bset
);
5832 isl_basic_set_free(bset
);
5836 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5837 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5841 map
= isl_map_cow(map
);
5845 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5846 for (i
= 0; i
< map
->n
; ++i
) {
5847 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5848 type
, pos
, value
, upper
);
5852 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5859 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5860 enum isl_dim_type type
, unsigned pos
, int value
)
5862 return map_bound_si(map
, type
, pos
, value
, 0);
5865 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5866 enum isl_dim_type type
, unsigned pos
, int value
)
5868 return map_bound_si(map
, type
, pos
, value
, 1);
5871 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5872 enum isl_dim_type type
, unsigned pos
, int value
)
5874 return (struct isl_set
*)
5875 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5878 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5879 enum isl_dim_type type
, unsigned pos
, int value
)
5881 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5884 /* Bound the given variable of "bmap" from below (or above is "upper"
5885 * is set) to "value".
5887 static __isl_give isl_basic_map
*basic_map_bound(
5888 __isl_take isl_basic_map
*bmap
,
5889 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5895 if (pos
>= isl_basic_map_dim(bmap
, type
))
5896 isl_die(bmap
->ctx
, isl_error_invalid
,
5897 "index out of bounds", goto error
);
5898 pos
+= isl_basic_map_offset(bmap
, type
);
5899 bmap
= isl_basic_map_cow(bmap
);
5900 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5901 j
= isl_basic_map_alloc_inequality(bmap
);
5904 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5906 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5907 isl_int_set(bmap
->ineq
[j
][0], value
);
5909 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5910 isl_int_neg(bmap
->ineq
[j
][0], value
);
5912 bmap
= isl_basic_map_simplify(bmap
);
5913 return isl_basic_map_finalize(bmap
);
5915 isl_basic_map_free(bmap
);
5919 /* Bound the given variable of "map" from below (or above is "upper"
5920 * is set) to "value".
5922 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5923 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5927 map
= isl_map_cow(map
);
5931 if (pos
>= isl_map_dim(map
, type
))
5932 isl_die(map
->ctx
, isl_error_invalid
,
5933 "index out of bounds", goto error
);
5934 for (i
= map
->n
- 1; i
>= 0; --i
) {
5935 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5936 if (remove_if_empty(map
, i
) < 0)
5939 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5946 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5947 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5949 return map_bound(map
, type
, pos
, value
, 0);
5952 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5953 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5955 return map_bound(map
, type
, pos
, value
, 1);
5958 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5959 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5961 return isl_map_lower_bound(set
, type
, pos
, value
);
5964 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5965 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5967 return isl_map_upper_bound(set
, type
, pos
, value
);
5970 /* Force the values of the variable at position "pos" of type "type" of "set"
5971 * to be no smaller than "value".
5973 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5974 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5978 if (!isl_val_is_int(value
))
5979 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5980 "expecting integer value", goto error
);
5981 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5982 isl_val_free(value
);
5985 isl_val_free(value
);
5990 /* Force the values of the variable at position "pos" of type "type" of "set"
5991 * to be no greater than "value".
5993 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5994 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5998 if (!isl_val_is_int(value
))
5999 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6000 "expecting integer value", goto error
);
6001 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6002 isl_val_free(value
);
6005 isl_val_free(value
);
6010 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6015 set
= isl_set_cow(set
);
6019 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6020 for (i
= 0; i
< set
->n
; ++i
) {
6021 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6031 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6035 map
= isl_map_cow(map
);
6039 map
->dim
= isl_space_reverse(map
->dim
);
6042 for (i
= 0; i
< map
->n
; ++i
) {
6043 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6047 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6054 static struct isl_map
*isl_basic_map_partial_lexopt(
6055 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6056 struct isl_set
**empty
, int max
)
6058 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6061 struct isl_map
*isl_basic_map_partial_lexmax(
6062 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6063 struct isl_set
**empty
)
6065 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6068 struct isl_map
*isl_basic_map_partial_lexmin(
6069 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6070 struct isl_set
**empty
)
6072 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6075 struct isl_set
*isl_basic_set_partial_lexmin(
6076 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6077 struct isl_set
**empty
)
6079 return (struct isl_set
*)
6080 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6084 struct isl_set
*isl_basic_set_partial_lexmax(
6085 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6086 struct isl_set
**empty
)
6088 return (struct isl_set
*)
6089 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6093 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6094 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6095 __isl_give isl_set
**empty
)
6097 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6100 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6101 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6102 __isl_give isl_set
**empty
)
6104 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6107 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6108 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6109 __isl_give isl_set
**empty
)
6111 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6114 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6115 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6116 __isl_give isl_set
**empty
)
6118 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6121 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6122 __isl_take isl_basic_map
*bmap
, int max
)
6124 isl_basic_set
*dom
= NULL
;
6125 isl_space
*dom_space
;
6129 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6130 dom
= isl_basic_set_universe(dom_space
);
6131 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6133 isl_basic_map_free(bmap
);
6137 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6138 __isl_take isl_basic_map
*bmap
)
6140 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6144 #define TYPE isl_pw_multi_aff
6146 #define SUFFIX _pw_multi_aff
6148 #define EMPTY isl_pw_multi_aff_empty
6150 #define ADD isl_pw_multi_aff_union_add
6151 #include "isl_map_lexopt_templ.c"
6153 /* Given a map "map", compute the lexicographically minimal
6154 * (or maximal) image element for each domain element in dom,
6155 * in the form of an isl_pw_multi_aff.
6156 * If "empty" is not NULL, then set *empty to those elements in dom that
6157 * do not have an image element.
6159 * We first compute the lexicographically minimal or maximal element
6160 * in the first basic map. This results in a partial solution "res"
6161 * and a subset "todo" of dom that still need to be handled.
6162 * We then consider each of the remaining maps in "map" and successively
6163 * update both "res" and "todo".
6164 * If "empty" is NULL, then the todo sets are not needed and therefore
6165 * also not computed.
6167 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6168 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6169 __isl_give isl_set
**empty
, int max
)
6172 isl_pw_multi_aff
*res
;
6178 if (isl_map_plain_is_empty(map
)) {
6183 return isl_pw_multi_aff_from_map(map
);
6186 res
= basic_map_partial_lexopt_pw_multi_aff(
6187 isl_basic_map_copy(map
->p
[0]),
6188 isl_set_copy(dom
), empty
, max
);
6192 for (i
= 1; i
< map
->n
; ++i
) {
6193 isl_pw_multi_aff
*res_i
;
6195 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6196 isl_basic_map_copy(map
->p
[i
]),
6197 isl_set_copy(dom
), empty
, max
);
6200 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6202 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6205 todo
= isl_set_intersect(todo
, *empty
);
6224 #define TYPE isl_map
6228 #define EMPTY isl_map_empty
6230 #define ADD isl_map_union_disjoint
6231 #include "isl_map_lexopt_templ.c"
6233 /* Given a map "map", compute the lexicographically minimal
6234 * (or maximal) image element for each domain element in "dom",
6235 * in the form of an isl_map.
6236 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6237 * do not have an image element.
6239 * If the input consists of more than one disjunct, then first
6240 * compute the desired result in the form of an isl_pw_multi_aff and
6241 * then convert that into an isl_map.
6243 * This function used to have an explicit implementation in terms
6244 * of isl_maps, but it would continually intersect the domains of
6245 * partial results with the complement of the domain of the next
6246 * partial solution, potentially leading to an explosion in the number
6247 * of disjuncts if there are several disjuncts in the input.
6248 * An even earlier implementation of this function would look for
6249 * better results in the domain of the partial result and for extra
6250 * results in the complement of this domain, which would lead to
6251 * even more splintering.
6253 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6254 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6255 __isl_give isl_set
**empty
, int max
)
6257 struct isl_map
*res
;
6258 isl_pw_multi_aff
*pma
;
6263 if (isl_map_plain_is_empty(map
)) {
6272 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6278 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
, max
);
6279 return isl_map_from_pw_multi_aff(pma
);
6288 __isl_give isl_map
*isl_map_partial_lexmax(
6289 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6290 __isl_give isl_set
**empty
)
6292 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6295 __isl_give isl_map
*isl_map_partial_lexmin(
6296 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6297 __isl_give isl_set
**empty
)
6299 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6302 __isl_give isl_set
*isl_set_partial_lexmin(
6303 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6304 __isl_give isl_set
**empty
)
6306 return (struct isl_set
*)
6307 isl_map_partial_lexmin((struct isl_map
*)set
,
6311 __isl_give isl_set
*isl_set_partial_lexmax(
6312 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6313 __isl_give isl_set
**empty
)
6315 return (struct isl_set
*)
6316 isl_map_partial_lexmax((struct isl_map
*)set
,
6320 /* Compute the lexicographic minimum (or maximum if "max" is set)
6321 * of "bmap" over its domain.
6323 * Since we are not interested in the part of the domain space where
6324 * there is no solution, we initialize the domain to those constraints
6325 * of "bmap" that only involve the parameters and the input dimensions.
6326 * This relieves the parametric programming engine from detecting those
6327 * inequalities and transferring them to the context. More importantly,
6328 * it ensures that those inequalities are transferred first and not
6329 * intermixed with inequalities that actually split the domain.
6331 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6335 isl_basic_map
*copy
;
6338 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6339 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6340 copy
= isl_basic_map_copy(bmap
);
6341 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6342 isl_dim_div
, 0, n_div
);
6343 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6344 isl_dim_out
, 0, n_out
);
6345 dom
= isl_basic_map_domain(copy
);
6346 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6349 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6351 return isl_basic_map_lexopt(bmap
, 0);
6354 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6356 return isl_basic_map_lexopt(bmap
, 1);
6359 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6361 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6364 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6366 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6369 /* Extract the first and only affine expression from list
6370 * and then add it to *pwaff with the given dom.
6371 * This domain is known to be disjoint from other domains
6372 * because of the way isl_basic_map_foreach_lexmax works.
6374 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6375 __isl_take isl_aff_list
*list
, void *user
)
6377 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6379 isl_pw_aff
**pwaff
= user
;
6380 isl_pw_aff
*pwaff_i
;
6384 if (isl_aff_list_n_aff(list
) != 1)
6385 isl_die(ctx
, isl_error_internal
,
6386 "expecting single element list", goto error
);
6388 aff
= isl_aff_list_get_aff(list
, 0);
6389 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6391 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6393 isl_aff_list_free(list
);
6397 isl_basic_set_free(dom
);
6398 isl_aff_list_free(list
);
6402 /* Given a basic map with one output dimension, compute the minimum or
6403 * maximum of that dimension as an isl_pw_aff.
6405 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6406 * call update_dim_opt on each leaf of the result.
6408 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6411 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6415 dim
= isl_space_from_domain(isl_space_domain(dim
));
6416 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6417 pwaff
= isl_pw_aff_empty(dim
);
6419 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6421 return isl_pw_aff_free(pwaff
);
6426 /* Compute the minimum or maximum of the given output dimension
6427 * as a function of the parameters and the input dimensions,
6428 * but independently of the other output dimensions.
6430 * We first project out the other output dimension and then compute
6431 * the "lexicographic" maximum in each basic map, combining the results
6432 * using isl_pw_aff_union_max.
6434 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6441 n_out
= isl_map_dim(map
, isl_dim_out
);
6442 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6443 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6448 isl_space
*dim
= isl_map_get_space(map
);
6450 return isl_pw_aff_empty(dim
);
6453 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6454 for (i
= 1; i
< map
->n
; ++i
) {
6455 isl_pw_aff
*pwaff_i
;
6457 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6458 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6466 /* Compute the maximum of the given output dimension as a function of the
6467 * parameters and input dimensions, but independently of
6468 * the other output dimensions.
6470 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6472 return map_dim_opt(map
, pos
, 1);
6475 /* Compute the minimum or maximum of the given set dimension
6476 * as a function of the parameters,
6477 * but independently of the other set dimensions.
6479 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6482 return map_dim_opt(set
, pos
, max
);
6485 /* Compute the maximum 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_max(__isl_take isl_set
*set
, int pos
)
6490 return set_dim_opt(set
, pos
, 1);
6493 /* Compute the minimum of the given set dimension as a function of the
6494 * parameters, but independently of the other set dimensions.
6496 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6498 return set_dim_opt(set
, pos
, 0);
6501 /* Apply a preimage specified by "mat" on the parameters of "bset".
6502 * bset is assumed to have only parameters and divs.
6504 static struct isl_basic_set
*basic_set_parameter_preimage(
6505 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6512 bset
->dim
= isl_space_cow(bset
->dim
);
6516 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6518 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6520 bset
->dim
->nparam
= 0;
6521 bset
->dim
->n_out
= nparam
;
6522 bset
= isl_basic_set_preimage(bset
, mat
);
6524 bset
->dim
->nparam
= bset
->dim
->n_out
;
6525 bset
->dim
->n_out
= 0;
6530 isl_basic_set_free(bset
);
6534 /* Apply a preimage specified by "mat" on the parameters of "set".
6535 * set is assumed to have only parameters and divs.
6537 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6538 __isl_take isl_mat
*mat
)
6546 nparam
= isl_set_dim(set
, isl_dim_param
);
6548 if (mat
->n_row
!= 1 + nparam
)
6549 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6550 "unexpected number of rows", goto error
);
6552 space
= isl_set_get_space(set
);
6553 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6554 isl_dim_param
, 0, nparam
);
6555 set
= isl_set_reset_space(set
, space
);
6556 set
= isl_set_preimage(set
, mat
);
6557 nparam
= isl_set_dim(set
, isl_dim_out
);
6558 space
= isl_set_get_space(set
);
6559 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6560 isl_dim_out
, 0, nparam
);
6561 set
= isl_set_reset_space(set
, space
);
6569 /* Intersect the basic set "bset" with the affine space specified by the
6570 * equalities in "eq".
6572 static struct isl_basic_set
*basic_set_append_equalities(
6573 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6581 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6586 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6587 for (i
= 0; i
< eq
->n_row
; ++i
) {
6588 k
= isl_basic_set_alloc_equality(bset
);
6591 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6592 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6596 bset
= isl_basic_set_gauss(bset
, NULL
);
6597 bset
= isl_basic_set_finalize(bset
);
6602 isl_basic_set_free(bset
);
6606 /* Intersect the set "set" with the affine space specified by the
6607 * equalities in "eq".
6609 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6617 for (i
= 0; i
< set
->n
; ++i
) {
6618 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6631 /* Given a basic set "bset" that only involves parameters and existentially
6632 * quantified variables, return the index of the first equality
6633 * that only involves parameters. If there is no such equality then
6634 * return bset->n_eq.
6636 * This function assumes that isl_basic_set_gauss has been called on "bset".
6638 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6641 unsigned nparam
, n_div
;
6646 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6647 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6649 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6650 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6657 /* Compute an explicit representation for the existentially quantified
6658 * variables in "bset" by computing the "minimal value" of the set
6659 * variables. Since there are no set variables, the computation of
6660 * the minimal value essentially computes an explicit representation
6661 * of the non-empty part(s) of "bset".
6663 * The input only involves parameters and existentially quantified variables.
6664 * All equalities among parameters have been removed.
6666 * Since the existentially quantified variables in the result are in general
6667 * going to be different from those in the input, we first replace
6668 * them by the minimal number of variables based on their equalities.
6669 * This should simplify the parametric integer programming.
6671 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6673 isl_morph
*morph1
, *morph2
;
6679 if (bset
->n_eq
== 0)
6680 return isl_basic_set_lexmin(bset
);
6682 morph1
= isl_basic_set_parameter_compression(bset
);
6683 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6684 bset
= isl_basic_set_lift(bset
);
6685 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6686 bset
= isl_morph_basic_set(morph2
, bset
);
6687 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6688 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6690 set
= isl_basic_set_lexmin(bset
);
6692 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6697 /* Project the given basic set onto its parameter domain, possibly introducing
6698 * new, explicit, existential variables in the constraints.
6699 * The input has parameters and (possibly implicit) existential variables.
6700 * The output has the same parameters, but only
6701 * explicit existentially quantified variables.
6703 * The actual projection is performed by pip, but pip doesn't seem
6704 * to like equalities very much, so we first remove the equalities
6705 * among the parameters by performing a variable compression on
6706 * the parameters. Afterward, an inverse transformation is performed
6707 * and the equalities among the parameters are inserted back in.
6709 * The variable compression on the parameters may uncover additional
6710 * equalities that were only implicit before. We therefore check
6711 * if there are any new parameter equalities in the result and
6712 * if so recurse. The removal of parameter equalities is required
6713 * for the parameter compression performed by base_compute_divs.
6715 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6719 struct isl_mat
*T
, *T2
;
6720 struct isl_set
*set
;
6723 bset
= isl_basic_set_cow(bset
);
6727 if (bset
->n_eq
== 0)
6728 return base_compute_divs(bset
);
6730 bset
= isl_basic_set_gauss(bset
, NULL
);
6733 if (isl_basic_set_plain_is_empty(bset
))
6734 return isl_set_from_basic_set(bset
);
6736 i
= first_parameter_equality(bset
);
6737 if (i
== bset
->n_eq
)
6738 return base_compute_divs(bset
);
6740 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6741 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6743 eq
= isl_mat_cow(eq
);
6744 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6745 if (T
&& T
->n_col
== 0) {
6749 bset
= isl_basic_set_set_to_empty(bset
);
6750 return isl_set_from_basic_set(bset
);
6752 bset
= basic_set_parameter_preimage(bset
, T
);
6754 i
= first_parameter_equality(bset
);
6757 else if (i
== bset
->n_eq
)
6758 set
= base_compute_divs(bset
);
6760 set
= parameter_compute_divs(bset
);
6761 set
= set_parameter_preimage(set
, T2
);
6762 set
= set_append_equalities(set
, eq
);
6766 /* Insert the divs from "ls" before those of "bmap".
6768 * The number of columns is not changed, which means that the last
6769 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6770 * The caller is responsible for removing the same number of dimensions
6771 * from the space of "bmap".
6773 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6774 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6780 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6784 old_n_div
= bmap
->n_div
;
6785 bmap
= insert_div_rows(bmap
, n_div
);
6789 for (i
= 0; i
< n_div
; ++i
) {
6790 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6791 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6797 /* Replace the space of "bmap" by the space and divs of "ls".
6799 * If "ls" has any divs, then we simplify the result since we may
6800 * have discovered some additional equalities that could simplify
6801 * the div expressions.
6803 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6804 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6808 bmap
= isl_basic_map_cow(bmap
);
6812 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6813 bmap
= insert_divs_from_local_space(bmap
, ls
);
6817 isl_space_free(bmap
->dim
);
6818 bmap
->dim
= isl_local_space_get_space(ls
);
6822 isl_local_space_free(ls
);
6824 bmap
= isl_basic_map_simplify(bmap
);
6825 bmap
= isl_basic_map_finalize(bmap
);
6828 isl_basic_map_free(bmap
);
6829 isl_local_space_free(ls
);
6833 /* Replace the space of "map" by the space and divs of "ls".
6835 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6836 __isl_take isl_local_space
*ls
)
6840 map
= isl_map_cow(map
);
6844 for (i
= 0; i
< map
->n
; ++i
) {
6845 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6846 isl_local_space_copy(ls
));
6850 isl_space_free(map
->dim
);
6851 map
->dim
= isl_local_space_get_space(ls
);
6855 isl_local_space_free(ls
);
6858 isl_local_space_free(ls
);
6863 /* Compute an explicit representation for the existentially
6864 * quantified variables for which do not know any explicit representation yet.
6866 * We first sort the existentially quantified variables so that the
6867 * existentially quantified variables for which we already have an explicit
6868 * representation are placed before those for which we do not.
6869 * The input dimensions, the output dimensions and the existentially
6870 * quantified variables for which we already have an explicit
6871 * representation are then turned into parameters.
6872 * compute_divs returns a map with the same parameters and
6873 * no input or output dimensions and the dimension specification
6874 * is reset to that of the input, including the existentially quantified
6875 * variables for which we already had an explicit representation.
6877 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6879 struct isl_basic_set
*bset
;
6880 struct isl_set
*set
;
6881 struct isl_map
*map
;
6883 isl_local_space
*ls
;
6890 bmap
= isl_basic_map_sort_divs(bmap
);
6891 bmap
= isl_basic_map_cow(bmap
);
6895 n_known
= isl_basic_map_first_unknown_div(bmap
);
6897 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6899 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6900 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6901 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6902 dim
= isl_space_set_alloc(bmap
->ctx
,
6903 nparam
+ n_in
+ n_out
+ n_known
, 0);
6907 ls
= isl_basic_map_get_local_space(bmap
);
6908 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6909 n_known
, bmap
->n_div
- n_known
);
6911 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6912 swap_div(bmap
, i
- n_known
, i
);
6913 bmap
->n_div
-= n_known
;
6914 bmap
->extra
-= n_known
;
6916 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6917 bset
= (struct isl_basic_set
*)bmap
;
6919 set
= parameter_compute_divs(bset
);
6920 map
= (struct isl_map
*)set
;
6921 map
= replace_space_by_local_space(map
, ls
);
6925 isl_basic_map_free(bmap
);
6929 /* Remove the explicit representation of local variable "div",
6932 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6933 __isl_take isl_basic_map
*bmap
, int div
)
6937 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6939 return isl_basic_map_free(bmap
);
6943 bmap
= isl_basic_map_cow(bmap
);
6946 isl_int_set_si(bmap
->div
[div
][0], 0);
6950 /* Is local variable "div" of "bmap" marked as not having an explicit
6952 * Note that even if "div" is not marked in this way and therefore
6953 * has an explicit representation, this representation may still
6954 * depend (indirectly) on other local variables that do not
6955 * have an explicit representation.
6957 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6961 return isl_bool_error
;
6962 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6963 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6964 "position out of bounds", return isl_bool_error
);
6965 return isl_int_is_zero(bmap
->div
[div
][0]);
6968 /* Return the position of the first local variable that does not
6969 * have an explicit representation.
6970 * Return the total number of local variables if they all have
6971 * an explicit representation.
6972 * Return -1 on error.
6974 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6981 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6982 if (!isl_basic_map_div_is_known(bmap
, i
))
6988 /* Does "bmap" have an explicit representation for all local variables?
6990 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6994 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
6995 first
= isl_basic_map_first_unknown_div(bmap
);
6997 return isl_bool_error
;
7001 /* Do all basic maps in "map" have an explicit representation
7002 * for all local variables?
7004 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7009 return isl_bool_error
;
7011 for (i
= 0; i
< map
->n
; ++i
) {
7012 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7017 return isl_bool_true
;
7020 /* If bmap contains any unknown divs, then compute explicit
7021 * expressions for them. However, this computation may be
7022 * quite expensive, so first try to remove divs that aren't
7025 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7028 struct isl_map
*map
;
7030 known
= isl_basic_map_divs_known(bmap
);
7034 return isl_map_from_basic_map(bmap
);
7036 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7038 known
= isl_basic_map_divs_known(bmap
);
7042 return isl_map_from_basic_map(bmap
);
7044 map
= compute_divs(bmap
);
7047 isl_basic_map_free(bmap
);
7051 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7055 struct isl_map
*res
;
7062 known
= isl_map_divs_known(map
);
7070 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7071 for (i
= 1 ; i
< map
->n
; ++i
) {
7073 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7074 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7075 res
= isl_map_union_disjoint(res
, r2
);
7077 res
= isl_map_union(res
, r2
);
7084 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7086 return (struct isl_set
*)
7087 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7090 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7092 return (struct isl_set
*)
7093 isl_map_compute_divs((struct isl_map
*)set
);
7096 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7099 struct isl_set
*set
;
7104 map
= isl_map_cow(map
);
7108 set
= (struct isl_set
*)map
;
7109 set
->dim
= isl_space_domain(set
->dim
);
7112 for (i
= 0; i
< map
->n
; ++i
) {
7113 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7117 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7118 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7125 /* Return the union of "map1" and "map2", where we assume for now that
7126 * "map1" and "map2" are disjoint. Note that the basic maps inside
7127 * "map1" or "map2" may not be disjoint from each other.
7128 * Also note that this function is also called from isl_map_union,
7129 * which takes care of handling the situation where "map1" and "map2"
7130 * may not be disjoint.
7132 * If one of the inputs is empty, we can simply return the other input.
7133 * Similarly, if one of the inputs is universal, then it is equal to the union.
7135 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7136 __isl_take isl_map
*map2
)
7140 struct isl_map
*map
= NULL
;
7146 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7147 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7148 "spaces don't match", goto error
);
7159 is_universe
= isl_map_plain_is_universe(map1
);
7160 if (is_universe
< 0)
7167 is_universe
= isl_map_plain_is_universe(map2
);
7168 if (is_universe
< 0)
7175 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7176 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7177 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7179 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7180 map1
->n
+ map2
->n
, flags
);
7183 for (i
= 0; i
< map1
->n
; ++i
) {
7184 map
= isl_map_add_basic_map(map
,
7185 isl_basic_map_copy(map1
->p
[i
]));
7189 for (i
= 0; i
< map2
->n
; ++i
) {
7190 map
= isl_map_add_basic_map(map
,
7191 isl_basic_map_copy(map2
->p
[i
]));
7205 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7206 * guaranteed to be disjoint by the caller.
7208 * Note that this functions is called from within isl_map_make_disjoint,
7209 * so we have to be careful not to touch the constraints of the inputs
7212 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7213 __isl_take isl_map
*map2
)
7215 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7218 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7219 * not be disjoint. The parameters are assumed to have been aligned.
7221 * We currently simply call map_union_disjoint, the internal operation
7222 * of which does not really depend on the inputs being disjoint.
7223 * If the result contains more than one basic map, then we clear
7224 * the disjoint flag since the result may contain basic maps from
7225 * both inputs and these are not guaranteed to be disjoint.
7227 * As a special case, if "map1" and "map2" are obviously equal,
7228 * then we simply return "map1".
7230 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7231 __isl_take isl_map
*map2
)
7238 equal
= isl_map_plain_is_equal(map1
, map2
);
7246 map1
= map_union_disjoint(map1
, map2
);
7250 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7258 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7261 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7262 __isl_take isl_map
*map2
)
7264 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7267 struct isl_set
*isl_set_union_disjoint(
7268 struct isl_set
*set1
, struct isl_set
*set2
)
7270 return (struct isl_set
*)
7271 isl_map_union_disjoint(
7272 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7275 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7277 return (struct isl_set
*)
7278 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7281 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7284 * "map" and "set" are assumed to be compatible and non-NULL.
7286 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7287 __isl_take isl_set
*set
,
7288 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7289 __isl_take isl_basic_set
*bset
))
7292 struct isl_map
*result
;
7295 if (isl_set_plain_is_universe(set
)) {
7300 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7301 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7302 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7304 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7305 map
->n
* set
->n
, flags
);
7306 for (i
= 0; result
&& i
< map
->n
; ++i
)
7307 for (j
= 0; j
< set
->n
; ++j
) {
7308 result
= isl_map_add_basic_map(result
,
7309 fn(isl_basic_map_copy(map
->p
[i
]),
7310 isl_basic_set_copy(set
->p
[j
])));
7320 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7321 __isl_take isl_set
*set
)
7326 if (!isl_map_compatible_range(map
, set
))
7327 isl_die(set
->ctx
, isl_error_invalid
,
7328 "incompatible spaces", goto error
);
7330 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7337 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7338 __isl_take isl_set
*set
)
7340 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7343 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7344 __isl_take isl_set
*set
)
7349 if (!isl_map_compatible_domain(map
, set
))
7350 isl_die(set
->ctx
, isl_error_invalid
,
7351 "incompatible spaces", goto error
);
7353 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7360 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7361 __isl_take isl_set
*set
)
7363 return isl_map_align_params_map_map_and(map
, set
,
7364 &map_intersect_domain
);
7367 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7368 __isl_take isl_map
*map2
)
7372 map1
= isl_map_reverse(map1
);
7373 map1
= isl_map_apply_range(map1
, map2
);
7374 return isl_map_reverse(map1
);
7381 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7382 __isl_take isl_map
*map2
)
7384 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7387 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7388 __isl_take isl_map
*map2
)
7390 isl_space
*dim_result
;
7391 struct isl_map
*result
;
7397 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7398 isl_space_copy(map2
->dim
));
7400 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7403 for (i
= 0; i
< map1
->n
; ++i
)
7404 for (j
= 0; j
< map2
->n
; ++j
) {
7405 result
= isl_map_add_basic_map(result
,
7406 isl_basic_map_apply_range(
7407 isl_basic_map_copy(map1
->p
[i
]),
7408 isl_basic_map_copy(map2
->p
[j
])));
7414 if (result
&& result
->n
<= 1)
7415 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7423 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7424 __isl_take isl_map
*map2
)
7426 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7430 * returns range - domain
7432 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7434 isl_space
*target_space
;
7435 struct isl_basic_set
*bset
;
7442 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7443 bmap
->dim
, isl_dim_out
),
7445 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7446 dim
= isl_basic_map_n_in(bmap
);
7447 nparam
= isl_basic_map_n_param(bmap
);
7448 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7449 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7450 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7451 for (i
= 0; i
< dim
; ++i
) {
7452 int j
= isl_basic_map_alloc_equality(bmap
);
7454 bmap
= isl_basic_map_free(bmap
);
7457 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7458 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7459 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7460 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7462 bset
= isl_basic_map_domain(bmap
);
7463 bset
= isl_basic_set_reset_space(bset
, target_space
);
7466 isl_basic_map_free(bmap
);
7471 * returns range - domain
7473 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7477 struct isl_set
*result
;
7482 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7483 map
->dim
, isl_dim_out
),
7485 dim
= isl_map_get_space(map
);
7486 dim
= isl_space_domain(dim
);
7487 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7490 for (i
= 0; i
< map
->n
; ++i
)
7491 result
= isl_set_add_basic_set(result
,
7492 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7501 * returns [domain -> range] -> range - domain
7503 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7504 __isl_take isl_basic_map
*bmap
)
7508 isl_basic_map
*domain
;
7512 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7513 bmap
->dim
, isl_dim_out
))
7514 isl_die(bmap
->ctx
, isl_error_invalid
,
7515 "domain and range don't match", goto error
);
7517 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7518 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7520 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7521 domain
= isl_basic_map_universe(dim
);
7523 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7524 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7525 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7527 total
= isl_basic_map_total_dim(bmap
);
7529 for (i
= 0; i
< n
; ++i
) {
7530 k
= isl_basic_map_alloc_equality(bmap
);
7533 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7534 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7535 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7536 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7539 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7540 return isl_basic_map_finalize(bmap
);
7542 isl_basic_map_free(bmap
);
7547 * returns [domain -> range] -> range - domain
7549 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7552 isl_space
*domain_dim
;
7557 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7558 map
->dim
, isl_dim_out
))
7559 isl_die(map
->ctx
, isl_error_invalid
,
7560 "domain and range don't match", goto error
);
7562 map
= isl_map_cow(map
);
7566 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7567 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7568 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7571 for (i
= 0; i
< map
->n
; ++i
) {
7572 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7576 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7583 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7585 struct isl_basic_map
*bmap
;
7593 nparam
= dims
->nparam
;
7595 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7599 for (i
= 0; i
< dim
; ++i
) {
7600 int j
= isl_basic_map_alloc_equality(bmap
);
7603 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7604 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7605 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7607 return isl_basic_map_finalize(bmap
);
7609 isl_basic_map_free(bmap
);
7613 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7617 if (dim
->n_in
!= dim
->n_out
)
7618 isl_die(dim
->ctx
, isl_error_invalid
,
7619 "number of input and output dimensions needs to be "
7620 "the same", goto error
);
7621 return basic_map_identity(dim
);
7623 isl_space_free(dim
);
7627 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7629 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7632 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7634 isl_space
*dim
= isl_set_get_space(set
);
7636 id
= isl_map_identity(isl_space_map_from_set(dim
));
7637 return isl_map_intersect_range(id
, set
);
7640 /* Construct a basic set with all set dimensions having only non-negative
7643 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7644 __isl_take isl_space
*space
)
7649 struct isl_basic_set
*bset
;
7653 nparam
= space
->nparam
;
7655 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7658 for (i
= 0; i
< dim
; ++i
) {
7659 int k
= isl_basic_set_alloc_inequality(bset
);
7662 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7663 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7667 isl_basic_set_free(bset
);
7671 /* Construct the half-space x_pos >= 0.
7673 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7677 isl_basic_set
*nonneg
;
7679 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7680 k
= isl_basic_set_alloc_inequality(nonneg
);
7683 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7684 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7686 return isl_basic_set_finalize(nonneg
);
7688 isl_basic_set_free(nonneg
);
7692 /* Construct the half-space x_pos <= -1.
7694 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7699 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7700 k
= isl_basic_set_alloc_inequality(neg
);
7703 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7704 isl_int_set_si(neg
->ineq
[k
][0], -1);
7705 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7707 return isl_basic_set_finalize(neg
);
7709 isl_basic_set_free(neg
);
7713 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7714 enum isl_dim_type type
, unsigned first
, unsigned n
)
7718 isl_basic_set
*nonneg
;
7726 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7728 offset
= pos(set
->dim
, type
);
7729 for (i
= 0; i
< n
; ++i
) {
7730 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7731 offset
+ first
+ i
);
7732 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7734 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7743 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7744 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7751 if (isl_set_plain_is_empty(set
)) {
7756 return fn(set
, signs
, user
);
7759 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7761 half
= isl_set_intersect(half
, isl_set_copy(set
));
7762 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7766 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7768 half
= isl_set_intersect(half
, set
);
7769 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7775 /* Call "fn" on the intersections of "set" with each of the orthants
7776 * (except for obviously empty intersections). The orthant is identified
7777 * by the signs array, with each entry having value 1 or -1 according
7778 * to the sign of the corresponding variable.
7780 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7781 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7791 if (isl_set_plain_is_empty(set
))
7794 nparam
= isl_set_dim(set
, isl_dim_param
);
7795 nvar
= isl_set_dim(set
, isl_dim_set
);
7797 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7799 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7807 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7809 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7812 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7813 __isl_keep isl_basic_map
*bmap2
)
7816 struct isl_map
*map1
;
7817 struct isl_map
*map2
;
7819 if (!bmap1
|| !bmap2
)
7820 return isl_bool_error
;
7822 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7823 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7825 is_subset
= isl_map_is_subset(map1
, map2
);
7833 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7834 __isl_keep isl_basic_set
*bset2
)
7836 return isl_basic_map_is_subset(bset1
, bset2
);
7839 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7840 __isl_keep isl_basic_map
*bmap2
)
7844 if (!bmap1
|| !bmap2
)
7845 return isl_bool_error
;
7846 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7847 if (is_subset
!= isl_bool_true
)
7849 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7853 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7854 __isl_keep isl_basic_set
*bset2
)
7856 return isl_basic_map_is_equal(
7857 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7860 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7866 return isl_bool_error
;
7867 for (i
= 0; i
< map
->n
; ++i
) {
7868 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7870 return isl_bool_error
;
7872 return isl_bool_false
;
7874 return isl_bool_true
;
7877 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7879 return map
? map
->n
== 0 : isl_bool_error
;
7882 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7884 return set
? set
->n
== 0 : isl_bool_error
;
7887 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7889 return isl_map_is_empty((struct isl_map
*)set
);
7892 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7897 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7900 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7905 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7908 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7913 return isl_bool_error
;
7914 is_subset
= isl_map_is_subset(map1
, map2
);
7915 if (is_subset
!= isl_bool_true
)
7917 is_subset
= isl_map_is_subset(map2
, map1
);
7921 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7923 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7926 isl_bool
isl_basic_map_is_strict_subset(
7927 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7931 if (!bmap1
|| !bmap2
)
7932 return isl_bool_error
;
7933 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7934 if (is_subset
!= isl_bool_true
)
7936 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7937 if (is_subset
== isl_bool_error
)
7942 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7943 __isl_keep isl_map
*map2
)
7948 return isl_bool_error
;
7949 is_subset
= isl_map_is_subset(map1
, map2
);
7950 if (is_subset
!= isl_bool_true
)
7952 is_subset
= isl_map_is_subset(map2
, map1
);
7953 if (is_subset
== isl_bool_error
)
7958 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7959 __isl_keep isl_set
*set2
)
7961 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7964 /* Is "bmap" obviously equal to the universe with the same space?
7966 * That is, does it not have any constraints?
7968 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7971 return isl_bool_error
;
7972 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7975 /* Is "bset" obviously equal to the universe with the same space?
7977 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7979 return isl_basic_map_plain_is_universe(bset
);
7982 /* If "c" does not involve any existentially quantified variables,
7983 * then set *univ to false and abort
7985 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7987 isl_bool
*univ
= user
;
7990 n
= isl_constraint_dim(c
, isl_dim_div
);
7991 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
7992 isl_constraint_free(c
);
7993 if (*univ
< 0 || !*univ
)
7994 return isl_stat_error
;
7998 /* Is "bmap" equal to the universe with the same space?
8000 * First check if it is obviously equal to the universe.
8001 * If not and if there are any constraints not involving
8002 * existentially quantified variables, then it is certainly
8003 * not equal to the universe.
8004 * Otherwise, check if the universe is a subset of "bmap".
8006 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8009 isl_basic_map
*test
;
8011 univ
= isl_basic_map_plain_is_universe(bmap
);
8012 if (univ
< 0 || univ
)
8014 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8015 return isl_bool_false
;
8016 univ
= isl_bool_true
;
8017 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8019 return isl_bool_error
;
8020 if (univ
< 0 || !univ
)
8022 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8023 univ
= isl_basic_map_is_subset(test
, bmap
);
8024 isl_basic_map_free(test
);
8028 /* Is "bset" equal to the universe with the same space?
8030 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8032 return isl_basic_map_is_universe(bset
);
8035 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8040 return isl_bool_error
;
8042 for (i
= 0; i
< map
->n
; ++i
) {
8043 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8048 return isl_bool_false
;
8051 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8053 return isl_map_plain_is_universe((isl_map
*) set
);
8056 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8058 struct isl_basic_set
*bset
= NULL
;
8059 struct isl_vec
*sample
= NULL
;
8060 isl_bool empty
, non_empty
;
8063 return isl_bool_error
;
8065 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8066 return isl_bool_true
;
8068 if (isl_basic_map_plain_is_universe(bmap
))
8069 return isl_bool_false
;
8071 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8072 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8073 copy
= isl_basic_map_remove_redundancies(copy
);
8074 empty
= isl_basic_map_plain_is_empty(copy
);
8075 isl_basic_map_free(copy
);
8079 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8081 return isl_bool_error
;
8083 return isl_bool_false
;
8084 isl_vec_free(bmap
->sample
);
8085 bmap
->sample
= NULL
;
8086 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8088 return isl_bool_error
;
8089 sample
= isl_basic_set_sample_vec(bset
);
8091 return isl_bool_error
;
8092 empty
= sample
->size
== 0;
8093 isl_vec_free(bmap
->sample
);
8094 bmap
->sample
= sample
;
8096 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8101 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8104 return isl_bool_error
;
8105 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8108 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8111 return isl_bool_error
;
8112 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8115 /* Is "bmap" known to be non-empty?
8117 * That is, is the cached sample still valid?
8119 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8124 return isl_bool_error
;
8126 return isl_bool_false
;
8127 total
= 1 + isl_basic_map_total_dim(bmap
);
8128 if (bmap
->sample
->size
!= total
)
8129 return isl_bool_false
;
8130 return isl_basic_map_contains(bmap
, bmap
->sample
);
8133 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8135 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8138 struct isl_map
*isl_basic_map_union(
8139 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8141 struct isl_map
*map
;
8142 if (!bmap1
|| !bmap2
)
8145 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8147 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8150 map
= isl_map_add_basic_map(map
, bmap1
);
8151 map
= isl_map_add_basic_map(map
, bmap2
);
8154 isl_basic_map_free(bmap1
);
8155 isl_basic_map_free(bmap2
);
8159 struct isl_set
*isl_basic_set_union(
8160 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8162 return (struct isl_set
*)isl_basic_map_union(
8163 (struct isl_basic_map
*)bset1
,
8164 (struct isl_basic_map
*)bset2
);
8167 /* Order divs such that any div only depends on previous divs */
8168 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8176 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8178 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8180 if (isl_int_is_zero(bmap
->div
[i
][0]))
8182 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8187 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8188 "integer division depends on itself",
8189 return isl_basic_map_free(bmap
));
8190 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8196 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8198 return (struct isl_basic_set
*)
8199 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8202 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8209 for (i
= 0; i
< map
->n
; ++i
) {
8210 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8221 /* Apply the expansion computed by isl_merge_divs.
8222 * The expansion itself is given by "exp" while the resulting
8223 * list of divs is given by "div".
8225 * Move the integer divisions of "bmap" into the right position
8226 * according to "exp" and then introduce the additional integer
8227 * divisions, adding div constraints.
8228 * The moving should be done first to avoid moving coefficients
8229 * in the definitions of the extra integer divisions.
8231 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8232 __isl_take isl_basic_set
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8237 bmap
= isl_basic_map_cow(bmap
);
8241 if (div
->n_row
< bmap
->n_div
)
8242 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8243 "not an expansion", goto error
);
8245 n_div
= bmap
->n_div
;
8246 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8247 div
->n_row
- n_div
, 0,
8248 2 * (div
->n_row
- n_div
));
8250 for (i
= n_div
; i
< div
->n_row
; ++i
)
8251 if (isl_basic_map_alloc_div(bmap
) < 0)
8254 for (j
= n_div
- 1; j
>= 0; --j
) {
8257 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8260 for (i
= 0; i
< div
->n_row
; ++i
) {
8261 if (j
< n_div
&& exp
[j
] == i
) {
8264 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8265 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8267 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8275 isl_basic_map_free(bmap
);
8280 /* Apply the expansion computed by isl_merge_divs.
8281 * The expansion itself is given by "exp" while the resulting
8282 * list of divs is given by "div".
8284 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8285 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8287 return isl_basic_map_expand_divs(bset
, div
, exp
);
8290 /* Look for a div in dst that corresponds to the div "div" in src.
8291 * The divs before "div" in src and dst are assumed to be the same.
8293 * Returns -1 if no corresponding div was found and the position
8294 * of the corresponding div in dst otherwise.
8296 static int find_div(struct isl_basic_map
*dst
,
8297 struct isl_basic_map
*src
, unsigned div
)
8301 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8303 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8304 for (i
= div
; i
< dst
->n_div
; ++i
)
8305 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8306 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8307 dst
->n_div
- div
) == -1)
8312 /* Align the divs of "dst" to those of "src", adding divs from "src"
8313 * if needed. That is, make sure that the first src->n_div divs
8314 * of the result are equal to those of src.
8316 * The result is not finalized as by design it will have redundant
8317 * divs if any divs from "src" were copied.
8319 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8320 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8323 int known
, extended
;
8327 return isl_basic_map_free(dst
);
8329 if (src
->n_div
== 0)
8332 known
= isl_basic_map_divs_known(src
);
8334 return isl_basic_map_free(dst
);
8336 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8337 "some src divs are unknown",
8338 return isl_basic_map_free(dst
));
8340 src
= isl_basic_map_order_divs(src
);
8343 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8344 for (i
= 0; i
< src
->n_div
; ++i
) {
8345 int j
= find_div(dst
, src
, i
);
8348 int extra
= src
->n_div
- i
;
8349 dst
= isl_basic_map_cow(dst
);
8352 dst
= isl_basic_map_extend_space(dst
,
8353 isl_space_copy(dst
->dim
),
8354 extra
, 0, 2 * extra
);
8357 j
= isl_basic_map_alloc_div(dst
);
8359 return isl_basic_map_free(dst
);
8360 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8361 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8362 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8363 return isl_basic_map_free(dst
);
8366 isl_basic_map_swap_div(dst
, i
, j
);
8371 struct isl_basic_set
*isl_basic_set_align_divs(
8372 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8374 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8375 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8378 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8386 map
= isl_map_compute_divs(map
);
8387 map
= isl_map_cow(map
);
8391 for (i
= 1; i
< map
->n
; ++i
)
8392 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8393 for (i
= 1; i
< map
->n
; ++i
) {
8394 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8396 return isl_map_free(map
);
8399 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8403 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8405 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8408 /* Align the divs of the basic maps in "map" to those
8409 * of the basic maps in "list", as well as to the other basic maps in "map".
8410 * The elements in "list" are assumed to have known divs.
8412 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8413 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8417 map
= isl_map_compute_divs(map
);
8418 map
= isl_map_cow(map
);
8420 return isl_map_free(map
);
8424 n
= isl_basic_map_list_n_basic_map(list
);
8425 for (i
= 0; i
< n
; ++i
) {
8426 isl_basic_map
*bmap
;
8428 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8429 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8430 isl_basic_map_free(bmap
);
8433 return isl_map_free(map
);
8435 return isl_map_align_divs(map
);
8438 /* Align the divs of each element of "list" to those of "bmap".
8439 * Both "bmap" and the elements of "list" are assumed to have known divs.
8441 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8442 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8447 return isl_basic_map_list_free(list
);
8449 n
= isl_basic_map_list_n_basic_map(list
);
8450 for (i
= 0; i
< n
; ++i
) {
8451 isl_basic_map
*bmap_i
;
8453 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8454 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8455 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8461 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8462 __isl_take isl_map
*map
)
8466 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8467 map
= isl_map_intersect_domain(map
, set
);
8468 set
= isl_map_range(map
);
8476 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8477 __isl_take isl_map
*map
)
8479 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8482 /* There is no need to cow as removing empty parts doesn't change
8483 * the meaning of the set.
8485 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8492 for (i
= map
->n
- 1; i
>= 0; --i
)
8493 remove_if_empty(map
, i
);
8498 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8500 return (struct isl_set
*)
8501 isl_map_remove_empty_parts((struct isl_map
*)set
);
8504 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8506 struct isl_basic_map
*bmap
;
8507 if (!map
|| map
->n
== 0)
8509 bmap
= map
->p
[map
->n
-1];
8510 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8511 return isl_basic_map_copy(bmap
);
8514 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8516 return (struct isl_basic_set
*)
8517 isl_map_copy_basic_map((struct isl_map
*)set
);
8520 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8521 __isl_keep isl_basic_map
*bmap
)
8527 for (i
= map
->n
-1; i
>= 0; --i
) {
8528 if (map
->p
[i
] != bmap
)
8530 map
= isl_map_cow(map
);
8533 isl_basic_map_free(map
->p
[i
]);
8534 if (i
!= map
->n
-1) {
8535 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8536 map
->p
[i
] = map
->p
[map
->n
-1];
8547 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8548 struct isl_basic_set
*bset
)
8550 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8551 (struct isl_basic_map
*)bset
);
8554 /* Given two basic sets bset1 and bset2, compute the maximal difference
8555 * between the values of dimension pos in bset1 and those in bset2
8556 * for any common value of the parameters and dimensions preceding pos.
8558 static enum isl_lp_result
basic_set_maximal_difference_at(
8559 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8560 int pos
, isl_int
*opt
)
8563 struct isl_basic_map
*bmap1
= NULL
;
8564 struct isl_basic_map
*bmap2
= NULL
;
8565 struct isl_ctx
*ctx
;
8566 struct isl_vec
*obj
;
8569 unsigned dim1
, dim2
;
8570 enum isl_lp_result res
;
8572 if (!bset1
|| !bset2
)
8573 return isl_lp_error
;
8575 nparam
= isl_basic_set_n_param(bset1
);
8576 dim1
= isl_basic_set_n_dim(bset1
);
8577 dim2
= isl_basic_set_n_dim(bset2
);
8578 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8579 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8580 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8581 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8582 if (!bmap1
|| !bmap2
)
8584 bmap1
= isl_basic_map_cow(bmap1
);
8585 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8586 pos
, (dim1
- pos
) + (dim2
- pos
),
8587 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8588 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8591 total
= isl_basic_map_total_dim(bmap1
);
8593 obj
= isl_vec_alloc(ctx
, 1 + total
);
8596 isl_seq_clr(obj
->block
.data
, 1 + total
);
8597 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8598 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8599 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8601 isl_basic_map_free(bmap1
);
8605 isl_basic_map_free(bmap2
);
8607 isl_basic_map_free(bmap1
);
8608 return isl_lp_error
;
8611 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8612 * for any common value of the parameters and dimensions preceding pos
8613 * in both basic sets, the values of dimension pos in bset1 are
8614 * smaller or larger than those in bset2.
8617 * 1 if bset1 follows bset2
8618 * -1 if bset1 precedes bset2
8619 * 0 if bset1 and bset2 are incomparable
8620 * -2 if some error occurred.
8622 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8623 struct isl_basic_set
*bset2
, int pos
)
8626 enum isl_lp_result res
;
8631 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8633 if (res
== isl_lp_empty
)
8635 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8636 res
== isl_lp_unbounded
)
8638 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8647 /* Given two basic sets bset1 and bset2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * there is a value of dimension pos in bset1 that is larger
8650 * than a value of the same dimension in bset2.
8653 * 1 if there exists such a pair
8654 * 0 if there is no such pair, but there is a pair of equal values
8656 * -2 if some error occurred.
8658 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8659 __isl_keep isl_basic_set
*bset2
, int pos
)
8662 enum isl_lp_result res
;
8667 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8669 if (res
== isl_lp_empty
)
8671 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8672 res
== isl_lp_unbounded
)
8674 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8676 else if (res
== isl_lp_ok
)
8685 /* Given two sets set1 and set2, check whether
8686 * for any common value of the parameters and dimensions preceding pos
8687 * there is a value of dimension pos in set1 that is larger
8688 * than a value of the same dimension in set2.
8691 * 1 if there exists such a pair
8692 * 0 if there is no such pair, but there is a pair of equal values
8694 * -2 if some error occurred.
8696 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8697 __isl_keep isl_set
*set2
, int pos
)
8705 for (i
= 0; i
< set1
->n
; ++i
)
8706 for (j
= 0; j
< set2
->n
; ++j
) {
8708 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8709 if (f
== 1 || f
== -2)
8718 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8719 unsigned pos
, isl_int
*val
)
8727 total
= isl_basic_map_total_dim(bmap
);
8728 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8729 for (; d
+1 > pos
; --d
)
8730 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8734 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8736 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8738 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8741 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8747 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8748 unsigned pos
, isl_int
*val
)
8760 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8763 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8764 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8765 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8766 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8770 isl_int_set(*val
, v
);
8776 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8777 unsigned pos
, isl_int
*val
)
8779 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8783 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8786 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8789 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8790 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8792 if (pos
>= isl_basic_map_dim(bmap
, type
))
8794 return isl_basic_map_plain_has_fixed_var(bmap
,
8795 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8798 /* If "bmap" obviously lies on a hyperplane where the given dimension
8799 * has a fixed value, then return that value.
8800 * Otherwise return NaN.
8802 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8803 __isl_keep isl_basic_map
*bmap
,
8804 enum isl_dim_type type
, unsigned pos
)
8812 ctx
= isl_basic_map_get_ctx(bmap
);
8813 v
= isl_val_alloc(ctx
);
8816 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8818 return isl_val_free(v
);
8820 isl_int_set_si(v
->d
, 1);
8824 return isl_val_nan(ctx
);
8827 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8828 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8830 if (pos
>= isl_map_dim(map
, type
))
8832 return isl_map_plain_has_fixed_var(map
,
8833 map_offset(map
, type
) - 1 + pos
, val
);
8836 /* If "map" obviously lies on a hyperplane where the given dimension
8837 * has a fixed value, then return that value.
8838 * Otherwise return NaN.
8840 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8841 enum isl_dim_type type
, unsigned pos
)
8849 ctx
= isl_map_get_ctx(map
);
8850 v
= isl_val_alloc(ctx
);
8853 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8855 return isl_val_free(v
);
8857 isl_int_set_si(v
->d
, 1);
8861 return isl_val_nan(ctx
);
8864 /* If "set" obviously lies on a hyperplane where the given dimension
8865 * has a fixed value, then return that value.
8866 * Otherwise return NaN.
8868 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8869 enum isl_dim_type type
, unsigned pos
)
8871 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8874 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8875 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8877 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8880 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8881 * then return this fixed value in *val.
8883 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8884 unsigned dim
, isl_int
*val
)
8886 return isl_basic_set_plain_has_fixed_var(bset
,
8887 isl_basic_set_n_param(bset
) + dim
, val
);
8890 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8891 * then return this fixed value in *val.
8893 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8894 unsigned dim
, isl_int
*val
)
8896 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8899 /* Check if input variable in has fixed value and if so and if val is not NULL,
8900 * then return this fixed value in *val.
8902 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8903 unsigned in
, isl_int
*val
)
8905 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8908 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8909 * and if val is not NULL, then return this lower bound in *val.
8911 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8912 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8914 int i
, i_eq
= -1, i_ineq
= -1;
8921 total
= isl_basic_set_total_dim(bset
);
8922 nparam
= isl_basic_set_n_param(bset
);
8923 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8924 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8930 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8931 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8933 if (i_eq
!= -1 || i_ineq
!= -1)
8937 if (i_eq
== -1 && i_ineq
== -1)
8939 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8940 /* The coefficient should always be one due to normalization. */
8941 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8943 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8945 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8946 total
- nparam
- dim
- 1) != -1)
8949 isl_int_neg(*val
, c
[0]);
8953 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8954 unsigned dim
, isl_int
*val
)
8966 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8970 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8972 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8973 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8975 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8979 isl_int_set(*val
, v
);
8985 /* Return -1 if the constraint "c1" should be sorted before "c2"
8986 * and 1 if it should be sorted after "c2".
8987 * Return 0 if the two constraints are the same (up to the constant term).
8989 * In particular, if a constraint involves later variables than another
8990 * then it is sorted after this other constraint.
8991 * uset_gist depends on constraints without existentially quantified
8992 * variables sorting first.
8994 * For constraints that have the same latest variable, those
8995 * with the same coefficient for this latest variable (first in absolute value
8996 * and then in actual value) are grouped together.
8997 * This is useful for detecting pairs of constraints that can
8998 * be chained in their printed representation.
9000 * Finally, within a group, constraints are sorted according to
9001 * their coefficients (excluding the constant term).
9003 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9005 isl_int
**c1
= (isl_int
**) p1
;
9006 isl_int
**c2
= (isl_int
**) p2
;
9008 unsigned size
= *(unsigned *) arg
;
9011 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9012 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9017 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9020 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9024 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9027 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9028 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9029 * and 0 if the two constraints are the same (up to the constant term).
9031 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9032 isl_int
*c1
, isl_int
*c2
)
9038 total
= isl_basic_map_total_dim(bmap
);
9039 return sort_constraint_cmp(&c1
, &c2
, &total
);
9042 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9043 __isl_take isl_basic_map
*bmap
)
9049 if (bmap
->n_ineq
== 0)
9051 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9053 total
= isl_basic_map_total_dim(bmap
);
9054 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9055 &sort_constraint_cmp
, &total
) < 0)
9056 return isl_basic_map_free(bmap
);
9060 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9061 __isl_take isl_basic_set
*bset
)
9063 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9064 (struct isl_basic_map
*)bset
);
9067 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9071 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9073 bmap
= isl_basic_map_remove_redundancies(bmap
);
9074 bmap
= isl_basic_map_sort_constraints(bmap
);
9076 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9080 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9082 return (struct isl_basic_set
*)isl_basic_map_normalize(
9083 (struct isl_basic_map
*)bset
);
9086 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9087 const __isl_keep isl_basic_map
*bmap2
)
9092 if (!bmap1
|| !bmap2
)
9097 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9098 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9099 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9100 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9101 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9102 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9103 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9104 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9105 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9106 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9107 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9109 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9111 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9113 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9114 return bmap1
->n_eq
- bmap2
->n_eq
;
9115 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9116 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9117 if (bmap1
->n_div
!= bmap2
->n_div
)
9118 return bmap1
->n_div
- bmap2
->n_div
;
9119 total
= isl_basic_map_total_dim(bmap1
);
9120 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9121 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9125 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9126 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9130 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9131 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9138 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9139 const __isl_keep isl_basic_set
*bset2
)
9141 return isl_basic_map_plain_cmp(bset1
, bset2
);
9144 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9150 if (set1
->n
!= set2
->n
)
9151 return set1
->n
- set2
->n
;
9153 for (i
= 0; i
< set1
->n
; ++i
) {
9154 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9162 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9163 __isl_keep isl_basic_map
*bmap2
)
9165 if (!bmap1
|| !bmap2
)
9166 return isl_bool_error
;
9167 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9170 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9171 __isl_keep isl_basic_set
*bset2
)
9173 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9174 (isl_basic_map
*)bset2
);
9177 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9179 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9180 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9182 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9185 /* Sort the basic maps of "map" and remove duplicate basic maps.
9187 * While removing basic maps, we make sure that the basic maps remain
9188 * sorted because isl_map_normalize expects the basic maps of the result
9191 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9195 map
= isl_map_remove_empty_parts(map
);
9198 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9199 for (i
= map
->n
- 1; i
>= 1; --i
) {
9200 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9202 isl_basic_map_free(map
->p
[i
-1]);
9203 for (j
= i
; j
< map
->n
; ++j
)
9204 map
->p
[j
- 1] = map
->p
[j
];
9211 /* Remove obvious duplicates among the basic maps of "map".
9213 * Unlike isl_map_normalize, this function does not remove redundant
9214 * constraints and only removes duplicates that have exactly the same
9215 * constraints in the input. It does sort the constraints and
9216 * the basic maps to ease the detection of duplicates.
9218 * If "map" has already been normalized or if the basic maps are
9219 * disjoint, then there can be no duplicates.
9221 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9224 isl_basic_map
*bmap
;
9230 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9232 for (i
= 0; i
< map
->n
; ++i
) {
9233 bmap
= isl_basic_map_copy(map
->p
[i
]);
9234 bmap
= isl_basic_map_sort_constraints(bmap
);
9236 return isl_map_free(map
);
9237 isl_basic_map_free(map
->p
[i
]);
9241 map
= sort_and_remove_duplicates(map
);
9245 /* We normalize in place, but if anything goes wrong we need
9246 * to return NULL, so we need to make sure we don't change the
9247 * meaning of any possible other copies of map.
9249 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9252 struct isl_basic_map
*bmap
;
9256 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9258 for (i
= 0; i
< map
->n
; ++i
) {
9259 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9262 isl_basic_map_free(map
->p
[i
]);
9266 map
= sort_and_remove_duplicates(map
);
9268 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9275 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9277 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9280 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9281 __isl_keep isl_map
*map2
)
9287 return isl_bool_error
;
9290 return isl_bool_true
;
9291 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9292 return isl_bool_false
;
9294 map1
= isl_map_copy(map1
);
9295 map2
= isl_map_copy(map2
);
9296 map1
= isl_map_normalize(map1
);
9297 map2
= isl_map_normalize(map2
);
9300 equal
= map1
->n
== map2
->n
;
9301 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9302 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9312 return isl_bool_error
;
9315 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9316 __isl_keep isl_set
*set2
)
9318 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9319 (struct isl_map
*)set2
);
9322 /* Return an interval that ranges from min to max (inclusive)
9324 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9325 isl_int min
, isl_int max
)
9328 struct isl_basic_set
*bset
= NULL
;
9330 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9334 k
= isl_basic_set_alloc_inequality(bset
);
9337 isl_int_set_si(bset
->ineq
[k
][1], 1);
9338 isl_int_neg(bset
->ineq
[k
][0], min
);
9340 k
= isl_basic_set_alloc_inequality(bset
);
9343 isl_int_set_si(bset
->ineq
[k
][1], -1);
9344 isl_int_set(bset
->ineq
[k
][0], max
);
9348 isl_basic_set_free(bset
);
9352 /* Return the basic maps in "map" as a list.
9354 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9355 __isl_keep isl_map
*map
)
9359 isl_basic_map_list
*list
;
9363 ctx
= isl_map_get_ctx(map
);
9364 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9366 for (i
= 0; i
< map
->n
; ++i
) {
9367 isl_basic_map
*bmap
;
9369 bmap
= isl_basic_map_copy(map
->p
[i
]);
9370 list
= isl_basic_map_list_add(list
, bmap
);
9376 /* Return the intersection of the elements in the non-empty list "list".
9377 * All elements are assumed to live in the same space.
9379 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9380 __isl_take isl_basic_map_list
*list
)
9383 isl_basic_map
*bmap
;
9387 n
= isl_basic_map_list_n_basic_map(list
);
9389 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9390 "expecting non-empty list", goto error
);
9392 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9393 for (i
= 1; i
< n
; ++i
) {
9394 isl_basic_map
*bmap_i
;
9396 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9397 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9400 isl_basic_map_list_free(list
);
9403 isl_basic_map_list_free(list
);
9407 /* Return the intersection of the elements in the non-empty list "list".
9408 * All elements are assumed to live in the same space.
9410 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9411 __isl_take isl_basic_set_list
*list
)
9413 return isl_basic_map_list_intersect(list
);
9416 /* Return the union of the elements in the non-empty list "list".
9417 * All elements are assumed to live in the same space.
9419 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9426 n
= isl_set_list_n_set(list
);
9428 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9429 "expecting non-empty list", goto error
);
9431 set
= isl_set_list_get_set(list
, 0);
9432 for (i
= 1; i
< n
; ++i
) {
9435 set_i
= isl_set_list_get_set(list
, i
);
9436 set
= isl_set_union(set
, set_i
);
9439 isl_set_list_free(list
);
9442 isl_set_list_free(list
);
9446 /* Return the Cartesian product of the basic sets in list (in the given order).
9448 __isl_give isl_basic_set
*isl_basic_set_list_product(
9449 __isl_take
struct isl_basic_set_list
*list
)
9457 struct isl_basic_set
*product
= NULL
;
9461 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9462 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9463 nparam
= isl_basic_set_n_param(list
->p
[0]);
9464 dim
= isl_basic_set_n_dim(list
->p
[0]);
9465 extra
= list
->p
[0]->n_div
;
9466 n_eq
= list
->p
[0]->n_eq
;
9467 n_ineq
= list
->p
[0]->n_ineq
;
9468 for (i
= 1; i
< list
->n
; ++i
) {
9469 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9470 isl_assert(list
->ctx
,
9471 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9472 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9473 extra
+= list
->p
[i
]->n_div
;
9474 n_eq
+= list
->p
[i
]->n_eq
;
9475 n_ineq
+= list
->p
[i
]->n_ineq
;
9477 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9482 for (i
= 0; i
< list
->n
; ++i
) {
9483 isl_basic_set_add_constraints(product
,
9484 isl_basic_set_copy(list
->p
[i
]), dim
);
9485 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9487 isl_basic_set_list_free(list
);
9490 isl_basic_set_free(product
);
9491 isl_basic_set_list_free(list
);
9495 struct isl_basic_map
*isl_basic_map_product(
9496 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9498 isl_space
*dim_result
= NULL
;
9499 struct isl_basic_map
*bmap
;
9500 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9501 struct isl_dim_map
*dim_map1
, *dim_map2
;
9503 if (!bmap1
|| !bmap2
)
9506 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9507 bmap2
->dim
, isl_dim_param
), goto error
);
9508 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9509 isl_space_copy(bmap2
->dim
));
9511 in1
= isl_basic_map_n_in(bmap1
);
9512 in2
= isl_basic_map_n_in(bmap2
);
9513 out1
= isl_basic_map_n_out(bmap1
);
9514 out2
= isl_basic_map_n_out(bmap2
);
9515 nparam
= isl_basic_map_n_param(bmap1
);
9517 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9518 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9519 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9520 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9521 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9522 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9523 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9524 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9525 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9526 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9527 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9529 bmap
= isl_basic_map_alloc_space(dim_result
,
9530 bmap1
->n_div
+ bmap2
->n_div
,
9531 bmap1
->n_eq
+ bmap2
->n_eq
,
9532 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9533 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9534 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9535 bmap
= isl_basic_map_simplify(bmap
);
9536 return isl_basic_map_finalize(bmap
);
9538 isl_basic_map_free(bmap1
);
9539 isl_basic_map_free(bmap2
);
9543 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9544 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9546 isl_basic_map
*prod
;
9548 prod
= isl_basic_map_product(bmap1
, bmap2
);
9549 prod
= isl_basic_map_flatten(prod
);
9553 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9554 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9556 return isl_basic_map_flat_range_product(bset1
, bset2
);
9559 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9560 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9562 isl_space
*space_result
= NULL
;
9563 isl_basic_map
*bmap
;
9564 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9565 struct isl_dim_map
*dim_map1
, *dim_map2
;
9567 if (!bmap1
|| !bmap2
)
9570 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9571 isl_space_copy(bmap2
->dim
));
9573 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9574 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9575 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9576 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9578 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9579 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9580 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9581 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9582 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9583 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9584 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9585 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9586 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9587 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9588 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9590 bmap
= isl_basic_map_alloc_space(space_result
,
9591 bmap1
->n_div
+ bmap2
->n_div
,
9592 bmap1
->n_eq
+ bmap2
->n_eq
,
9593 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9594 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9595 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9596 bmap
= isl_basic_map_simplify(bmap
);
9597 return isl_basic_map_finalize(bmap
);
9599 isl_basic_map_free(bmap1
);
9600 isl_basic_map_free(bmap2
);
9604 __isl_give isl_basic_map
*isl_basic_map_range_product(
9605 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9607 isl_space
*dim_result
= NULL
;
9608 isl_basic_map
*bmap
;
9609 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9610 struct isl_dim_map
*dim_map1
, *dim_map2
;
9612 if (!bmap1
|| !bmap2
)
9615 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9616 bmap2
->dim
, isl_dim_param
))
9617 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9618 "parameters don't match", goto error
);
9620 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9621 isl_space_copy(bmap2
->dim
));
9623 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9624 out1
= isl_basic_map_n_out(bmap1
);
9625 out2
= isl_basic_map_n_out(bmap2
);
9626 nparam
= isl_basic_map_n_param(bmap1
);
9628 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9629 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9630 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9631 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9632 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9633 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9634 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9635 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9636 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9637 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9638 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9640 bmap
= isl_basic_map_alloc_space(dim_result
,
9641 bmap1
->n_div
+ bmap2
->n_div
,
9642 bmap1
->n_eq
+ bmap2
->n_eq
,
9643 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9644 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9645 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9646 bmap
= isl_basic_map_simplify(bmap
);
9647 return isl_basic_map_finalize(bmap
);
9649 isl_basic_map_free(bmap1
);
9650 isl_basic_map_free(bmap2
);
9654 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9655 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9657 isl_basic_map
*prod
;
9659 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9660 prod
= isl_basic_map_flatten_range(prod
);
9664 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9665 * and collect the results.
9666 * The result live in the space obtained by calling "space_product"
9667 * on the spaces of "map1" and "map2".
9668 * If "remove_duplicates" is set then the result may contain duplicates
9669 * (even if the inputs do not) and so we try and remove the obvious
9672 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9673 __isl_take isl_map
*map2
,
9674 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9675 __isl_take isl_space
*right
),
9676 __isl_give isl_basic_map
*(*basic_map_product
)(
9677 __isl_take isl_basic_map
*left
,
9678 __isl_take isl_basic_map
*right
),
9679 int remove_duplicates
)
9682 struct isl_map
*result
;
9688 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9689 map2
->dim
, isl_dim_param
), goto error
);
9691 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9692 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9693 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9695 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9696 isl_space_copy(map2
->dim
)),
9697 map1
->n
* map2
->n
, flags
);
9700 for (i
= 0; i
< map1
->n
; ++i
)
9701 for (j
= 0; j
< map2
->n
; ++j
) {
9702 struct isl_basic_map
*part
;
9703 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9704 isl_basic_map_copy(map2
->p
[j
]));
9705 if (isl_basic_map_is_empty(part
))
9706 isl_basic_map_free(part
);
9708 result
= isl_map_add_basic_map(result
, part
);
9712 if (remove_duplicates
)
9713 result
= isl_map_remove_obvious_duplicates(result
);
9723 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9725 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9726 __isl_take isl_map
*map2
)
9728 return map_product(map1
, map2
, &isl_space_product
,
9729 &isl_basic_map_product
, 0);
9732 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9733 __isl_take isl_map
*map2
)
9735 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9738 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9740 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9741 __isl_take isl_map
*map2
)
9745 prod
= isl_map_product(map1
, map2
);
9746 prod
= isl_map_flatten(prod
);
9750 /* Given two set A and B, construct its Cartesian product A x B.
9752 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9754 return isl_map_range_product(set1
, set2
);
9757 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9758 __isl_take isl_set
*set2
)
9760 return isl_map_flat_range_product(set1
, set2
);
9763 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9765 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9766 __isl_take isl_map
*map2
)
9768 return map_product(map1
, map2
, &isl_space_domain_product
,
9769 &isl_basic_map_domain_product
, 1);
9772 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9774 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9775 __isl_take isl_map
*map2
)
9777 return map_product(map1
, map2
, &isl_space_range_product
,
9778 &isl_basic_map_range_product
, 1);
9781 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9782 __isl_take isl_map
*map2
)
9784 return isl_map_align_params_map_map_and(map1
, map2
,
9785 &map_domain_product_aligned
);
9788 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9789 __isl_take isl_map
*map2
)
9791 return isl_map_align_params_map_map_and(map1
, map2
,
9792 &map_range_product_aligned
);
9795 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9797 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9800 int total1
, keep1
, total2
, keep2
;
9804 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9805 !isl_space_range_is_wrapping(map
->dim
))
9806 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9807 "not a product", return isl_map_free(map
));
9809 space
= isl_map_get_space(map
);
9810 total1
= isl_space_dim(space
, isl_dim_in
);
9811 total2
= isl_space_dim(space
, isl_dim_out
);
9812 space
= isl_space_factor_domain(space
);
9813 keep1
= isl_space_dim(space
, isl_dim_in
);
9814 keep2
= isl_space_dim(space
, isl_dim_out
);
9815 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9816 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9817 map
= isl_map_reset_space(map
, space
);
9822 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9824 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9827 int total1
, keep1
, total2
, keep2
;
9831 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9832 !isl_space_range_is_wrapping(map
->dim
))
9833 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9834 "not a product", return isl_map_free(map
));
9836 space
= isl_map_get_space(map
);
9837 total1
= isl_space_dim(space
, isl_dim_in
);
9838 total2
= isl_space_dim(space
, isl_dim_out
);
9839 space
= isl_space_factor_range(space
);
9840 keep1
= isl_space_dim(space
, isl_dim_in
);
9841 keep2
= isl_space_dim(space
, isl_dim_out
);
9842 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9843 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9844 map
= isl_map_reset_space(map
, space
);
9849 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9851 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9858 if (!isl_space_domain_is_wrapping(map
->dim
))
9859 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9860 "domain is not a product", return isl_map_free(map
));
9862 space
= isl_map_get_space(map
);
9863 total
= isl_space_dim(space
, isl_dim_in
);
9864 space
= isl_space_domain_factor_domain(space
);
9865 keep
= isl_space_dim(space
, isl_dim_in
);
9866 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9867 map
= isl_map_reset_space(map
, space
);
9872 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9874 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9881 if (!isl_space_domain_is_wrapping(map
->dim
))
9882 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9883 "domain is not a product", return isl_map_free(map
));
9885 space
= isl_map_get_space(map
);
9886 total
= isl_space_dim(space
, isl_dim_in
);
9887 space
= isl_space_domain_factor_range(space
);
9888 keep
= isl_space_dim(space
, isl_dim_in
);
9889 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9890 map
= isl_map_reset_space(map
, space
);
9895 /* Given a map A -> [B -> C], extract the map A -> B.
9897 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9904 if (!isl_space_range_is_wrapping(map
->dim
))
9905 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9906 "range is not a product", return isl_map_free(map
));
9908 space
= isl_map_get_space(map
);
9909 total
= isl_space_dim(space
, isl_dim_out
);
9910 space
= isl_space_range_factor_domain(space
);
9911 keep
= isl_space_dim(space
, isl_dim_out
);
9912 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9913 map
= isl_map_reset_space(map
, space
);
9918 /* Given a map A -> [B -> C], extract the map A -> C.
9920 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9927 if (!isl_space_range_is_wrapping(map
->dim
))
9928 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9929 "range is not a product", return isl_map_free(map
));
9931 space
= isl_map_get_space(map
);
9932 total
= isl_space_dim(space
, isl_dim_out
);
9933 space
= isl_space_range_factor_range(space
);
9934 keep
= isl_space_dim(space
, isl_dim_out
);
9935 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9936 map
= isl_map_reset_space(map
, space
);
9941 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9943 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9944 __isl_take isl_map
*map2
)
9948 prod
= isl_map_domain_product(map1
, map2
);
9949 prod
= isl_map_flatten_domain(prod
);
9953 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9955 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9956 __isl_take isl_map
*map2
)
9960 prod
= isl_map_range_product(map1
, map2
);
9961 prod
= isl_map_flatten_range(prod
);
9965 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9968 uint32_t hash
= isl_hash_init();
9973 bmap
= isl_basic_map_copy(bmap
);
9974 bmap
= isl_basic_map_normalize(bmap
);
9977 total
= isl_basic_map_total_dim(bmap
);
9978 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9979 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9981 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9982 isl_hash_hash(hash
, c_hash
);
9984 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9985 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9987 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9988 isl_hash_hash(hash
, c_hash
);
9990 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9991 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9993 if (isl_int_is_zero(bmap
->div
[i
][0]))
9995 isl_hash_byte(hash
, i
& 0xFF);
9996 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9997 isl_hash_hash(hash
, c_hash
);
9999 isl_basic_map_free(bmap
);
10003 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10005 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
10008 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10015 map
= isl_map_copy(map
);
10016 map
= isl_map_normalize(map
);
10020 hash
= isl_hash_init();
10021 for (i
= 0; i
< map
->n
; ++i
) {
10022 uint32_t bmap_hash
;
10023 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10024 isl_hash_hash(hash
, bmap_hash
);
10032 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10034 return isl_map_get_hash((isl_map
*)set
);
10037 /* Check if the value for dimension dim is completely determined
10038 * by the values of the other parameters and variables.
10039 * That is, check if dimension dim is involved in an equality.
10041 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10048 nparam
= isl_basic_set_n_param(bset
);
10049 for (i
= 0; i
< bset
->n_eq
; ++i
)
10050 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10055 /* Check if the value for dimension dim is completely determined
10056 * by the values of the other parameters and variables.
10057 * That is, check if dimension dim is involved in an equality
10058 * for each of the subsets.
10060 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10066 for (i
= 0; i
< set
->n
; ++i
) {
10068 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10075 /* Return the number of basic maps in the (current) representation of "map".
10077 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10079 return map
? map
->n
: 0;
10082 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10084 return set
? set
->n
: 0;
10087 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10088 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10093 return isl_stat_error
;
10095 for (i
= 0; i
< map
->n
; ++i
)
10096 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10097 return isl_stat_error
;
10099 return isl_stat_ok
;
10102 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10103 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10108 return isl_stat_error
;
10110 for (i
= 0; i
< set
->n
; ++i
)
10111 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10112 return isl_stat_error
;
10114 return isl_stat_ok
;
10117 /* Return a list of basic sets, the union of which is equal to "set".
10119 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10120 __isl_keep isl_set
*set
)
10123 isl_basic_set_list
*list
;
10128 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10129 for (i
= 0; i
< set
->n
; ++i
) {
10130 isl_basic_set
*bset
;
10132 bset
= isl_basic_set_copy(set
->p
[i
]);
10133 list
= isl_basic_set_list_add(list
, bset
);
10139 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10146 bset
= isl_basic_set_cow(bset
);
10150 dim
= isl_basic_set_get_space(bset
);
10151 dim
= isl_space_lift(dim
, bset
->n_div
);
10154 isl_space_free(bset
->dim
);
10156 bset
->extra
-= bset
->n_div
;
10159 bset
= isl_basic_set_finalize(bset
);
10163 isl_basic_set_free(bset
);
10167 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10173 set
= isl_set_align_divs(set
);
10178 set
= isl_set_cow(set
);
10182 n_div
= set
->p
[0]->n_div
;
10183 dim
= isl_set_get_space(set
);
10184 dim
= isl_space_lift(dim
, n_div
);
10187 isl_space_free(set
->dim
);
10190 for (i
= 0; i
< set
->n
; ++i
) {
10191 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10202 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10205 struct isl_basic_map
*bmap
;
10212 set
= isl_set_align_divs(set
);
10217 dim
= isl_set_get_space(set
);
10218 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10220 return isl_map_identity(isl_space_map_from_set(dim
));
10223 n_div
= set
->p
[0]->n_div
;
10224 dim
= isl_space_map_from_set(dim
);
10225 n_param
= isl_space_dim(dim
, isl_dim_param
);
10226 n_set
= isl_space_dim(dim
, isl_dim_in
);
10227 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10228 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10229 for (i
= 0; i
< n_set
; ++i
)
10230 bmap
= var_equal(bmap
, i
);
10232 total
= n_param
+ n_set
+ n_set
+ n_div
;
10233 for (i
= 0; i
< n_div
; ++i
) {
10234 k
= isl_basic_map_alloc_inequality(bmap
);
10237 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10238 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10239 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10240 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10241 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10242 set
->p
[0]->div
[i
][0]);
10244 l
= isl_basic_map_alloc_inequality(bmap
);
10247 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10248 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10249 set
->p
[0]->div
[i
][0]);
10250 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10254 bmap
= isl_basic_map_simplify(bmap
);
10255 bmap
= isl_basic_map_finalize(bmap
);
10256 return isl_map_from_basic_map(bmap
);
10259 isl_basic_map_free(bmap
);
10263 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10271 dim
= isl_basic_set_total_dim(bset
);
10272 size
+= bset
->n_eq
* (1 + dim
);
10273 size
+= bset
->n_ineq
* (1 + dim
);
10274 size
+= bset
->n_div
* (2 + dim
);
10279 int isl_set_size(__isl_keep isl_set
*set
)
10287 for (i
= 0; i
< set
->n
; ++i
)
10288 size
+= isl_basic_set_size(set
->p
[i
]);
10293 /* Check if there is any lower bound (if lower == 0) and/or upper
10294 * bound (if upper == 0) on the specified dim.
10296 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10297 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10302 return isl_bool_error
;
10304 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10305 return isl_bool_error
);
10307 pos
+= isl_basic_map_offset(bmap
, type
);
10309 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10310 if (isl_int_is_zero(bmap
->div
[i
][0]))
10312 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10313 return isl_bool_true
;
10316 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10317 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10318 return isl_bool_true
;
10320 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10321 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10328 return lower
&& upper
;
10331 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10332 enum isl_dim_type type
, unsigned pos
)
10334 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10337 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10338 enum isl_dim_type type
, unsigned pos
)
10340 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10343 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10344 enum isl_dim_type type
, unsigned pos
)
10346 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10349 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10350 enum isl_dim_type type
, unsigned pos
)
10357 for (i
= 0; i
< map
->n
; ++i
) {
10359 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10360 if (bounded
< 0 || !bounded
)
10367 /* Return 1 if the specified dim is involved in both an upper bound
10368 * and a lower bound.
10370 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10371 enum isl_dim_type type
, unsigned pos
)
10373 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10376 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10378 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10379 enum isl_dim_type type
, unsigned pos
,
10380 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10381 enum isl_dim_type type
, unsigned pos
))
10386 return isl_bool_error
;
10388 for (i
= 0; i
< map
->n
; ++i
) {
10390 bounded
= fn(map
->p
[i
], type
, pos
);
10391 if (bounded
< 0 || bounded
)
10395 return isl_bool_false
;
10398 /* Return 1 if the specified dim is involved in any lower bound.
10400 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10401 enum isl_dim_type type
, unsigned pos
)
10403 return has_any_bound(set
, type
, pos
,
10404 &isl_basic_map_dim_has_lower_bound
);
10407 /* Return 1 if the specified dim is involved in any upper bound.
10409 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10410 enum isl_dim_type type
, unsigned pos
)
10412 return has_any_bound(set
, type
, pos
,
10413 &isl_basic_map_dim_has_upper_bound
);
10416 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10418 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10419 enum isl_dim_type type
, unsigned pos
,
10420 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10421 enum isl_dim_type type
, unsigned pos
))
10426 return isl_bool_error
;
10428 for (i
= 0; i
< map
->n
; ++i
) {
10430 bounded
= fn(map
->p
[i
], type
, pos
);
10431 if (bounded
< 0 || !bounded
)
10435 return isl_bool_true
;
10438 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10440 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10441 enum isl_dim_type type
, unsigned pos
)
10443 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10446 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10448 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10449 enum isl_dim_type type
, unsigned pos
)
10451 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10454 /* For each of the "n" variables starting at "first", determine
10455 * the sign of the variable and put the results in the first "n"
10456 * elements of the array "signs".
10458 * 1 means that the variable is non-negative
10459 * -1 means that the variable is non-positive
10460 * 0 means the variable attains both positive and negative values.
10462 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10463 unsigned first
, unsigned n
, int *signs
)
10465 isl_vec
*bound
= NULL
;
10466 struct isl_tab
*tab
= NULL
;
10467 struct isl_tab_undo
*snap
;
10470 if (!bset
|| !signs
)
10473 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10474 tab
= isl_tab_from_basic_set(bset
, 0);
10475 if (!bound
|| !tab
)
10478 isl_seq_clr(bound
->el
, bound
->size
);
10479 isl_int_set_si(bound
->el
[0], -1);
10481 snap
= isl_tab_snap(tab
);
10482 for (i
= 0; i
< n
; ++i
) {
10485 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10486 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10488 empty
= tab
->empty
;
10489 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10490 if (isl_tab_rollback(tab
, snap
) < 0)
10498 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10499 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10501 empty
= tab
->empty
;
10502 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10503 if (isl_tab_rollback(tab
, snap
) < 0)
10506 signs
[i
] = empty
? -1 : 0;
10510 isl_vec_free(bound
);
10514 isl_vec_free(bound
);
10518 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10519 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10521 if (!bset
|| !signs
)
10523 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10526 first
+= pos(bset
->dim
, type
) - 1;
10527 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10530 /* Is it possible for the integer division "div" to depend (possibly
10531 * indirectly) on any output dimensions?
10533 * If the div is undefined, then we conservatively assume that it
10534 * may depend on them.
10535 * Otherwise, we check if it actually depends on them or on any integer
10536 * divisions that may depend on them.
10538 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10541 unsigned n_out
, o_out
;
10542 unsigned n_div
, o_div
;
10544 if (isl_int_is_zero(bmap
->div
[div
][0]))
10547 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10548 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10550 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10553 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10554 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10556 for (i
= 0; i
< n_div
; ++i
) {
10557 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10559 if (div_may_involve_output(bmap
, i
))
10566 /* Return the first integer division of "bmap" in the range
10567 * [first, first + n[ that may depend on any output dimensions and
10568 * that has a non-zero coefficient in "c" (where the first coefficient
10569 * in "c" corresponds to integer division "first").
10571 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10572 isl_int
*c
, int first
, int n
)
10579 for (k
= first
; k
< first
+ n
; ++k
) {
10580 if (isl_int_is_zero(c
[k
]))
10582 if (div_may_involve_output(bmap
, k
))
10589 /* Look for a pair of inequality constraints in "bmap" of the form
10591 * -l + i >= 0 or i >= l
10593 * n + l - i >= 0 or i <= l + n
10595 * with n < "m" and i the output dimension at position "pos".
10596 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10597 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10598 * and earlier output dimensions, as well as integer divisions that do
10599 * not involve any of the output dimensions.
10601 * Return the index of the first inequality constraint or bmap->n_ineq
10602 * if no such pair can be found.
10604 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10605 int pos
, isl_int m
)
10610 unsigned n_div
, o_div
;
10611 unsigned n_out
, o_out
;
10617 ctx
= isl_basic_map_get_ctx(bmap
);
10618 total
= isl_basic_map_total_dim(bmap
);
10619 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10620 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10621 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10622 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10623 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10624 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10626 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10627 n_out
- (pos
+ 1)) != -1)
10629 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10632 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10633 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10636 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10637 bmap
->ineq
[j
] + 1, total
))
10641 if (j
>= bmap
->n_ineq
)
10643 isl_int_add(bmap
->ineq
[i
][0],
10644 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10645 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10646 isl_int_sub(bmap
->ineq
[i
][0],
10647 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10650 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10656 return bmap
->n_ineq
;
10659 /* Return the index of the equality of "bmap" that defines
10660 * the output dimension "pos" in terms of earlier dimensions.
10661 * The equality may also involve integer divisions, as long
10662 * as those integer divisions are defined in terms of
10663 * parameters or input dimensions.
10664 * In this case, *div is set to the number of integer divisions and
10665 * *ineq is set to the number of inequality constraints (provided
10666 * div and ineq are not NULL).
10668 * The equality may also involve a single integer division involving
10669 * the output dimensions (typically only output dimension "pos") as
10670 * long as the coefficient of output dimension "pos" is 1 or -1 and
10671 * there is a pair of constraints i >= l and i <= l + n, with i referring
10672 * to output dimension "pos", l an expression involving only earlier
10673 * dimensions and n smaller than the coefficient of the integer division
10674 * in the equality. In this case, the output dimension can be defined
10675 * in terms of a modulo expression that does not involve the integer division.
10676 * *div is then set to this single integer division and
10677 * *ineq is set to the index of constraint i >= l.
10679 * Return bmap->n_eq if there is no such equality.
10680 * Return -1 on error.
10682 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10683 int pos
, int *div
, int *ineq
)
10686 unsigned n_out
, o_out
;
10687 unsigned n_div
, o_div
;
10692 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10693 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10694 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10695 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10698 *ineq
= bmap
->n_ineq
;
10701 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10702 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10704 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10705 n_out
- (pos
+ 1)) != -1)
10707 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10711 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10712 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10714 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10715 k
+ 1, n_div
- (k
+1)) < n_div
)
10717 l
= find_modulo_constraint_pair(bmap
, pos
,
10718 bmap
->eq
[j
][o_div
+ k
]);
10721 if (l
>= bmap
->n_ineq
)
10733 /* Check if the given basic map is obviously single-valued.
10734 * In particular, for each output dimension, check that there is
10735 * an equality that defines the output dimension in terms of
10736 * earlier dimensions.
10738 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10744 return isl_bool_error
;
10746 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10748 for (i
= 0; i
< n_out
; ++i
) {
10751 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10754 return isl_bool_error
;
10755 if (eq
>= bmap
->n_eq
)
10756 return isl_bool_false
;
10759 return isl_bool_true
;
10762 /* Check if the given basic map is single-valued.
10763 * We simply compute
10767 * and check if the result is a subset of the identity mapping.
10769 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10772 isl_basic_map
*test
;
10776 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10780 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10781 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10783 space
= isl_basic_map_get_space(bmap
);
10784 space
= isl_space_map_from_set(isl_space_range(space
));
10785 id
= isl_basic_map_identity(space
);
10787 sv
= isl_basic_map_is_subset(test
, id
);
10789 isl_basic_map_free(test
);
10790 isl_basic_map_free(id
);
10795 /* Check if the given map is obviously single-valued.
10797 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10800 return isl_bool_error
;
10802 return isl_bool_true
;
10804 return isl_bool_false
;
10806 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10809 /* Check if the given map is single-valued.
10810 * We simply compute
10814 * and check if the result is a subset of the identity mapping.
10816 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10823 sv
= isl_map_plain_is_single_valued(map
);
10827 test
= isl_map_reverse(isl_map_copy(map
));
10828 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10830 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10831 id
= isl_map_identity(dim
);
10833 sv
= isl_map_is_subset(test
, id
);
10835 isl_map_free(test
);
10841 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10845 map
= isl_map_copy(map
);
10846 map
= isl_map_reverse(map
);
10847 in
= isl_map_is_single_valued(map
);
10853 /* Check if the given map is obviously injective.
10855 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10859 map
= isl_map_copy(map
);
10860 map
= isl_map_reverse(map
);
10861 in
= isl_map_plain_is_single_valued(map
);
10867 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10871 sv
= isl_map_is_single_valued(map
);
10875 return isl_map_is_injective(map
);
10878 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10880 return isl_map_is_single_valued((isl_map
*)set
);
10883 /* Does "map" only map elements to themselves?
10885 * If the domain and range spaces are different, then "map"
10886 * is considered not to be an identity relation, even if it is empty.
10887 * Otherwise, construct the maximal identity relation and
10888 * check whether "map" is a subset of this relation.
10890 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10894 isl_bool equal
, is_identity
;
10896 space
= isl_map_get_space(map
);
10897 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10898 isl_space_free(space
);
10899 if (equal
< 0 || !equal
)
10902 id
= isl_map_identity(isl_map_get_space(map
));
10903 is_identity
= isl_map_is_subset(map
, id
);
10906 return is_identity
;
10909 int isl_map_is_translation(__isl_keep isl_map
*map
)
10914 delta
= isl_map_deltas(isl_map_copy(map
));
10915 ok
= isl_set_is_singleton(delta
);
10916 isl_set_free(delta
);
10921 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10923 if (isl_seq_first_non_zero(p
, pos
) != -1)
10925 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10930 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10939 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10942 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10943 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10944 for (j
= 0; j
< nvar
; ++j
) {
10945 int lower
= 0, upper
= 0;
10946 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10947 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10949 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10953 if (i
< bset
->n_eq
)
10955 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10956 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10958 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10960 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10965 if (!lower
|| !upper
)
10972 int isl_set_is_box(__isl_keep isl_set
*set
)
10979 return isl_basic_set_is_box(set
->p
[0]);
10982 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10985 return isl_bool_error
;
10987 return isl_space_is_wrapping(bset
->dim
);
10990 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10993 return isl_bool_error
;
10995 return isl_space_is_wrapping(set
->dim
);
10998 /* Modify the space of "map" through a call to "change".
10999 * If "can_change" is set (not NULL), then first call it to check
11000 * if the modification is allowed, printing the error message "cannot_change"
11003 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11004 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11005 const char *cannot_change
,
11006 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11014 ok
= can_change
? can_change(map
) : isl_bool_true
;
11016 return isl_map_free(map
);
11018 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11019 return isl_map_free(map
));
11021 space
= change(isl_map_get_space(map
));
11022 map
= isl_map_reset_space(map
, space
);
11027 /* Is the domain of "map" a wrapped relation?
11029 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11032 return isl_bool_error
;
11034 return isl_space_domain_is_wrapping(map
->dim
);
11037 /* Is the range of "map" a wrapped relation?
11039 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11042 return isl_bool_error
;
11044 return isl_space_range_is_wrapping(map
->dim
);
11047 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11049 bmap
= isl_basic_map_cow(bmap
);
11053 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11057 bmap
= isl_basic_map_finalize(bmap
);
11059 return (isl_basic_set
*)bmap
;
11061 isl_basic_map_free(bmap
);
11065 /* Given a map A -> B, return the set (A -> B).
11067 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11069 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11072 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11074 bset
= isl_basic_set_cow(bset
);
11078 bset
->dim
= isl_space_unwrap(bset
->dim
);
11082 bset
= isl_basic_set_finalize(bset
);
11084 return (isl_basic_map
*)bset
;
11086 isl_basic_set_free(bset
);
11090 /* Given a set (A -> B), return the map A -> B.
11091 * Error out if "set" is not of the form (A -> B).
11093 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11095 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11096 "not a wrapping set", &isl_space_unwrap
);
11099 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11100 enum isl_dim_type type
)
11105 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11108 bmap
= isl_basic_map_cow(bmap
);
11112 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11116 bmap
= isl_basic_map_finalize(bmap
);
11120 isl_basic_map_free(bmap
);
11124 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11125 enum isl_dim_type type
)
11132 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11135 map
= isl_map_cow(map
);
11139 for (i
= 0; i
< map
->n
; ++i
) {
11140 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11144 map
->dim
= isl_space_reset(map
->dim
, type
);
11154 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11159 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11162 bmap
= isl_basic_map_cow(bmap
);
11166 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11170 bmap
= isl_basic_map_finalize(bmap
);
11174 isl_basic_map_free(bmap
);
11178 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11180 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11183 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11184 __isl_take isl_basic_map
*bmap
)
11189 if (!bmap
->dim
->nested
[0])
11192 bmap
= isl_basic_map_cow(bmap
);
11196 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11200 bmap
= isl_basic_map_finalize(bmap
);
11204 isl_basic_map_free(bmap
);
11208 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11209 __isl_take isl_basic_map
*bmap
)
11214 if (!bmap
->dim
->nested
[1])
11217 bmap
= isl_basic_map_cow(bmap
);
11221 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11225 bmap
= isl_basic_map_finalize(bmap
);
11229 isl_basic_map_free(bmap
);
11233 /* Remove any internal structure from the spaces of domain and range of "map".
11235 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11240 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11243 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11246 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11248 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11251 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11253 isl_space
*dim
, *flat_dim
;
11256 dim
= isl_set_get_space(set
);
11257 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11258 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11259 map
= isl_map_intersect_domain(map
, set
);
11264 /* Remove any internal structure from the space of the domain of "map".
11266 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11271 if (!map
->dim
->nested
[0])
11274 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11277 /* Remove any internal structure from the space of the range of "map".
11279 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11284 if (!map
->dim
->nested
[1])
11287 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11290 /* Reorder the dimensions of "bmap" according to the given dim_map
11291 * and set the dimension specification to "dim" and
11292 * perform Gaussian elimination on the result.
11294 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11295 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11297 isl_basic_map
*res
;
11300 bmap
= isl_basic_map_cow(bmap
);
11301 if (!bmap
|| !dim
|| !dim_map
)
11304 flags
= bmap
->flags
;
11305 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11306 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11307 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11308 res
= isl_basic_map_alloc_space(dim
,
11309 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11310 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11312 res
->flags
= flags
;
11313 res
= isl_basic_map_gauss(res
, NULL
);
11314 res
= isl_basic_map_finalize(res
);
11318 isl_basic_map_free(bmap
);
11319 isl_space_free(dim
);
11323 /* Reorder the dimensions of "map" according to given reordering.
11325 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11326 __isl_take isl_reordering
*r
)
11329 struct isl_dim_map
*dim_map
;
11331 map
= isl_map_cow(map
);
11332 dim_map
= isl_dim_map_from_reordering(r
);
11333 if (!map
|| !r
|| !dim_map
)
11336 for (i
= 0; i
< map
->n
; ++i
) {
11337 struct isl_dim_map
*dim_map_i
;
11339 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11341 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11342 isl_space_copy(r
->dim
), dim_map_i
);
11348 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11350 isl_reordering_free(r
);
11356 isl_reordering_free(r
);
11360 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11361 __isl_take isl_reordering
*r
)
11363 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11366 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11367 __isl_take isl_space
*model
)
11371 if (!map
|| !model
)
11374 ctx
= isl_space_get_ctx(model
);
11375 if (!isl_space_has_named_params(model
))
11376 isl_die(ctx
, isl_error_invalid
,
11377 "model has unnamed parameters", goto error
);
11378 if (!isl_space_has_named_params(map
->dim
))
11379 isl_die(ctx
, isl_error_invalid
,
11380 "relation has unnamed parameters", goto error
);
11381 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11382 isl_reordering
*exp
;
11384 model
= isl_space_drop_dims(model
, isl_dim_in
,
11385 0, isl_space_dim(model
, isl_dim_in
));
11386 model
= isl_space_drop_dims(model
, isl_dim_out
,
11387 0, isl_space_dim(model
, isl_dim_out
));
11388 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11389 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11390 map
= isl_map_realign(map
, exp
);
11393 isl_space_free(model
);
11396 isl_space_free(model
);
11401 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11402 __isl_take isl_space
*model
)
11404 return isl_map_align_params(set
, model
);
11407 /* Align the parameters of "bmap" to those of "model", introducing
11408 * additional parameters if needed.
11410 __isl_give isl_basic_map
*isl_basic_map_align_params(
11411 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11415 if (!bmap
|| !model
)
11418 ctx
= isl_space_get_ctx(model
);
11419 if (!isl_space_has_named_params(model
))
11420 isl_die(ctx
, isl_error_invalid
,
11421 "model has unnamed parameters", goto error
);
11422 if (!isl_space_has_named_params(bmap
->dim
))
11423 isl_die(ctx
, isl_error_invalid
,
11424 "relation has unnamed parameters", goto error
);
11425 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11426 isl_reordering
*exp
;
11427 struct isl_dim_map
*dim_map
;
11429 model
= isl_space_drop_dims(model
, isl_dim_in
,
11430 0, isl_space_dim(model
, isl_dim_in
));
11431 model
= isl_space_drop_dims(model
, isl_dim_out
,
11432 0, isl_space_dim(model
, isl_dim_out
));
11433 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11434 exp
= isl_reordering_extend_space(exp
,
11435 isl_basic_map_get_space(bmap
));
11436 dim_map
= isl_dim_map_from_reordering(exp
);
11437 bmap
= isl_basic_map_realign(bmap
,
11438 exp
? isl_space_copy(exp
->dim
) : NULL
,
11439 isl_dim_map_extend(dim_map
, bmap
));
11440 isl_reordering_free(exp
);
11444 isl_space_free(model
);
11447 isl_space_free(model
);
11448 isl_basic_map_free(bmap
);
11452 /* Align the parameters of "bset" to those of "model", introducing
11453 * additional parameters if needed.
11455 __isl_give isl_basic_set
*isl_basic_set_align_params(
11456 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11458 return isl_basic_map_align_params(bset
, model
);
11461 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11462 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11463 enum isl_dim_type c2
, enum isl_dim_type c3
,
11464 enum isl_dim_type c4
, enum isl_dim_type c5
)
11466 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11467 struct isl_mat
*mat
;
11473 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11474 isl_basic_map_total_dim(bmap
) + 1);
11477 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11478 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11479 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11480 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11481 isl_int_set(mat
->row
[i
][pos
],
11482 bmap
->eq
[i
][off
+ k
]);
11490 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11491 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11492 enum isl_dim_type c2
, enum isl_dim_type c3
,
11493 enum isl_dim_type c4
, enum isl_dim_type c5
)
11495 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11496 struct isl_mat
*mat
;
11502 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11503 isl_basic_map_total_dim(bmap
) + 1);
11506 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11507 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11508 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11509 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11510 isl_int_set(mat
->row
[i
][pos
],
11511 bmap
->ineq
[i
][off
+ k
]);
11519 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11520 __isl_take isl_space
*dim
,
11521 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11522 enum isl_dim_type c2
, enum isl_dim_type c3
,
11523 enum isl_dim_type c4
, enum isl_dim_type c5
)
11525 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11526 isl_basic_map
*bmap
;
11532 if (!dim
|| !eq
|| !ineq
)
11535 if (eq
->n_col
!= ineq
->n_col
)
11536 isl_die(dim
->ctx
, isl_error_invalid
,
11537 "equalities and inequalities matrices should have "
11538 "same number of columns", goto error
);
11540 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11542 if (eq
->n_col
< total
)
11543 isl_die(dim
->ctx
, isl_error_invalid
,
11544 "number of columns too small", goto error
);
11546 extra
= eq
->n_col
- total
;
11548 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11549 eq
->n_row
, ineq
->n_row
);
11552 for (i
= 0; i
< extra
; ++i
) {
11553 k
= isl_basic_map_alloc_div(bmap
);
11556 isl_int_set_si(bmap
->div
[k
][0], 0);
11558 for (i
= 0; i
< eq
->n_row
; ++i
) {
11559 l
= isl_basic_map_alloc_equality(bmap
);
11562 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11563 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11564 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11565 isl_int_set(bmap
->eq
[l
][off
+ k
],
11571 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11572 l
= isl_basic_map_alloc_inequality(bmap
);
11575 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11576 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11577 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11578 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11579 ineq
->row
[i
][pos
]);
11585 isl_space_free(dim
);
11587 isl_mat_free(ineq
);
11589 bmap
= isl_basic_map_simplify(bmap
);
11590 return isl_basic_map_finalize(bmap
);
11592 isl_space_free(dim
);
11594 isl_mat_free(ineq
);
11598 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11599 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11600 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11602 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11603 c1
, c2
, c3
, c4
, isl_dim_in
);
11606 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11607 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11608 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11610 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11611 c1
, c2
, c3
, c4
, isl_dim_in
);
11614 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11615 __isl_take isl_space
*dim
,
11616 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11617 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11619 return (isl_basic_set
*)
11620 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11621 c1
, c2
, c3
, c4
, isl_dim_in
);
11624 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11627 return isl_bool_error
;
11629 return isl_space_can_zip(bmap
->dim
);
11632 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11635 return isl_bool_error
;
11637 return isl_space_can_zip(map
->dim
);
11640 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11641 * (A -> C) -> (B -> D).
11643 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11652 if (!isl_basic_map_can_zip(bmap
))
11653 isl_die(bmap
->ctx
, isl_error_invalid
,
11654 "basic map cannot be zipped", goto error
);
11655 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11656 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11657 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11658 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11659 bmap
= isl_basic_map_cow(bmap
);
11660 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11663 bmap
->dim
= isl_space_zip(bmap
->dim
);
11666 bmap
= isl_basic_map_mark_final(bmap
);
11669 isl_basic_map_free(bmap
);
11673 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11674 * (A -> C) -> (B -> D).
11676 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11683 if (!isl_map_can_zip(map
))
11684 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11687 map
= isl_map_cow(map
);
11691 for (i
= 0; i
< map
->n
; ++i
) {
11692 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11697 map
->dim
= isl_space_zip(map
->dim
);
11707 /* Can we apply isl_basic_map_curry to "bmap"?
11708 * That is, does it have a nested relation in its domain?
11710 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11713 return isl_bool_error
;
11715 return isl_space_can_curry(bmap
->dim
);
11718 /* Can we apply isl_map_curry to "map"?
11719 * That is, does it have a nested relation in its domain?
11721 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11724 return isl_bool_error
;
11726 return isl_space_can_curry(map
->dim
);
11729 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11732 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11738 if (!isl_basic_map_can_curry(bmap
))
11739 isl_die(bmap
->ctx
, isl_error_invalid
,
11740 "basic map cannot be curried", goto error
);
11741 bmap
= isl_basic_map_cow(bmap
);
11744 bmap
->dim
= isl_space_curry(bmap
->dim
);
11747 bmap
= isl_basic_map_mark_final(bmap
);
11750 isl_basic_map_free(bmap
);
11754 /* Given a map (A -> B) -> C, return the corresponding map
11757 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11759 return isl_map_change_space(map
, &isl_map_can_curry
,
11760 "map cannot be curried", &isl_space_curry
);
11763 /* Can isl_map_range_curry be applied to "map"?
11764 * That is, does it have a nested relation in its range,
11765 * the domain of which is itself a nested relation?
11767 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11770 return isl_bool_error
;
11772 return isl_space_can_range_curry(map
->dim
);
11775 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11776 * A -> (B -> (C -> D)).
11778 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11780 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11781 "map range cannot be curried",
11782 &isl_space_range_curry
);
11785 /* Can we apply isl_basic_map_uncurry to "bmap"?
11786 * That is, does it have a nested relation in its domain?
11788 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11791 return isl_bool_error
;
11793 return isl_space_can_uncurry(bmap
->dim
);
11796 /* Can we apply isl_map_uncurry to "map"?
11797 * That is, does it have a nested relation in its domain?
11799 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11802 return isl_bool_error
;
11804 return isl_space_can_uncurry(map
->dim
);
11807 /* Given a basic map A -> (B -> C), return the corresponding basic map
11810 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11816 if (!isl_basic_map_can_uncurry(bmap
))
11817 isl_die(bmap
->ctx
, isl_error_invalid
,
11818 "basic map cannot be uncurried",
11819 return isl_basic_map_free(bmap
));
11820 bmap
= isl_basic_map_cow(bmap
);
11823 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11825 return isl_basic_map_free(bmap
);
11826 bmap
= isl_basic_map_mark_final(bmap
);
11830 /* Given a map A -> (B -> C), return the corresponding map
11833 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11835 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11836 "map cannot be uncurried", &isl_space_uncurry
);
11839 /* Construct a basic map mapping the domain of the affine expression
11840 * to a one-dimensional range prescribed by the affine expression.
11842 * A NaN affine expression cannot be converted to a basic map.
11844 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11849 isl_local_space
*ls
;
11850 isl_basic_map
*bmap
= NULL
;
11854 is_nan
= isl_aff_is_nan(aff
);
11858 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11859 "cannot convert NaN", goto error
);
11861 ls
= isl_aff_get_local_space(aff
);
11862 bmap
= isl_basic_map_from_local_space(ls
);
11863 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11864 k
= isl_basic_map_alloc_equality(bmap
);
11868 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11869 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11870 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11871 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11872 aff
->v
->size
- (pos
+ 1));
11875 bmap
= isl_basic_map_finalize(bmap
);
11879 isl_basic_map_free(bmap
);
11883 /* Construct a map mapping the domain of the affine expression
11884 * to a one-dimensional range prescribed by the affine expression.
11886 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11888 isl_basic_map
*bmap
;
11890 bmap
= isl_basic_map_from_aff(aff
);
11891 return isl_map_from_basic_map(bmap
);
11894 /* Construct a basic map mapping the domain the multi-affine expression
11895 * to its range, with each dimension in the range equated to the
11896 * corresponding affine expression.
11898 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11899 __isl_take isl_multi_aff
*maff
)
11903 isl_basic_map
*bmap
;
11908 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11909 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11910 "invalid space", goto error
);
11912 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11913 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11915 for (i
= 0; i
< maff
->n
; ++i
) {
11917 isl_basic_map
*bmap_i
;
11919 aff
= isl_aff_copy(maff
->p
[i
]);
11920 bmap_i
= isl_basic_map_from_aff(aff
);
11922 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11925 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11927 isl_multi_aff_free(maff
);
11930 isl_multi_aff_free(maff
);
11934 /* Construct a map mapping the domain the multi-affine expression
11935 * to its range, with each dimension in the range equated to the
11936 * corresponding affine expression.
11938 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11940 isl_basic_map
*bmap
;
11942 bmap
= isl_basic_map_from_multi_aff(maff
);
11943 return isl_map_from_basic_map(bmap
);
11946 /* Construct a basic map mapping a domain in the given space to
11947 * to an n-dimensional range, with n the number of elements in the list,
11948 * where each coordinate in the range is prescribed by the
11949 * corresponding affine expression.
11950 * The domains of all affine expressions in the list are assumed to match
11953 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11954 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11958 isl_basic_map
*bmap
;
11963 dim
= isl_space_from_domain(domain_dim
);
11964 bmap
= isl_basic_map_universe(dim
);
11966 for (i
= 0; i
< list
->n
; ++i
) {
11968 isl_basic_map
*bmap_i
;
11970 aff
= isl_aff_copy(list
->p
[i
]);
11971 bmap_i
= isl_basic_map_from_aff(aff
);
11973 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11976 isl_aff_list_free(list
);
11980 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11981 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11983 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11986 /* Construct a basic map where the given dimensions are equal to each other.
11988 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11989 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11991 isl_basic_map
*bmap
= NULL
;
11997 if (pos1
>= isl_space_dim(space
, type1
))
11998 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11999 "index out of bounds", goto error
);
12000 if (pos2
>= isl_space_dim(space
, type2
))
12001 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12002 "index out of bounds", goto error
);
12004 if (type1
== type2
&& pos1
== pos2
)
12005 return isl_basic_map_universe(space
);
12007 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12008 i
= isl_basic_map_alloc_equality(bmap
);
12011 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12012 pos1
+= isl_basic_map_offset(bmap
, type1
);
12013 pos2
+= isl_basic_map_offset(bmap
, type2
);
12014 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12015 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12016 bmap
= isl_basic_map_finalize(bmap
);
12017 isl_space_free(space
);
12020 isl_space_free(space
);
12021 isl_basic_map_free(bmap
);
12025 /* Add a constraint imposing that the given two dimensions are equal.
12027 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12028 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12032 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12034 bmap
= isl_basic_map_intersect(bmap
, eq
);
12039 /* Add a constraint imposing that the given two dimensions are equal.
12041 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12042 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12044 isl_basic_map
*bmap
;
12046 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12048 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12053 /* Add a constraint imposing that the given two dimensions have opposite values.
12055 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12056 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12058 isl_basic_map
*bmap
= NULL
;
12064 if (pos1
>= isl_map_dim(map
, type1
))
12065 isl_die(map
->ctx
, isl_error_invalid
,
12066 "index out of bounds", goto error
);
12067 if (pos2
>= isl_map_dim(map
, type2
))
12068 isl_die(map
->ctx
, isl_error_invalid
,
12069 "index out of bounds", goto error
);
12071 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12072 i
= isl_basic_map_alloc_equality(bmap
);
12075 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12076 pos1
+= isl_basic_map_offset(bmap
, type1
);
12077 pos2
+= isl_basic_map_offset(bmap
, type2
);
12078 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12079 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12080 bmap
= isl_basic_map_finalize(bmap
);
12082 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12086 isl_basic_map_free(bmap
);
12091 /* Construct a constraint imposing that the value of the first dimension is
12092 * greater than or equal to that of the second.
12094 static __isl_give isl_constraint
*constraint_order_ge(
12095 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12096 enum isl_dim_type type2
, int pos2
)
12103 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12105 if (pos1
>= isl_constraint_dim(c
, type1
))
12106 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12107 "index out of bounds", return isl_constraint_free(c
));
12108 if (pos2
>= isl_constraint_dim(c
, type2
))
12109 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12110 "index out of bounds", return isl_constraint_free(c
));
12112 if (type1
== type2
&& pos1
== pos2
)
12115 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12116 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12121 /* Add a constraint imposing that the value of the first dimension is
12122 * greater than or equal to that of the second.
12124 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12125 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12130 if (type1
== type2
&& pos1
== pos2
)
12132 space
= isl_basic_map_get_space(bmap
);
12133 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12134 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12139 /* Add a constraint imposing that the value of the first dimension is
12140 * greater than or equal to that of the second.
12142 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12143 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12148 if (type1
== type2
&& pos1
== pos2
)
12150 space
= isl_map_get_space(map
);
12151 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12152 map
= isl_map_add_constraint(map
, c
);
12157 /* Add a constraint imposing that the value of the first dimension is
12158 * less than or equal to that of the second.
12160 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12161 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12163 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12166 /* Construct a basic map where the value of the first dimension is
12167 * greater than that of the second.
12169 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12170 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12172 isl_basic_map
*bmap
= NULL
;
12178 if (pos1
>= isl_space_dim(space
, type1
))
12179 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12180 "index out of bounds", goto error
);
12181 if (pos2
>= isl_space_dim(space
, type2
))
12182 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12183 "index out of bounds", goto error
);
12185 if (type1
== type2
&& pos1
== pos2
)
12186 return isl_basic_map_empty(space
);
12188 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12189 i
= isl_basic_map_alloc_inequality(bmap
);
12191 return isl_basic_map_free(bmap
);
12192 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12193 pos1
+= isl_basic_map_offset(bmap
, type1
);
12194 pos2
+= isl_basic_map_offset(bmap
, type2
);
12195 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12196 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12197 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12198 bmap
= isl_basic_map_finalize(bmap
);
12202 isl_space_free(space
);
12203 isl_basic_map_free(bmap
);
12207 /* Add a constraint imposing that the value of the first dimension is
12208 * greater than that of the second.
12210 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12211 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12215 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12217 bmap
= isl_basic_map_intersect(bmap
, gt
);
12222 /* Add a constraint imposing that the value of the first dimension is
12223 * greater than that of the second.
12225 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12226 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12228 isl_basic_map
*bmap
;
12230 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12232 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12237 /* Add a constraint imposing that the value of the first dimension is
12238 * smaller than that of the second.
12240 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12241 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12243 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12246 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12250 isl_local_space
*ls
;
12255 if (!isl_basic_map_divs_known(bmap
))
12256 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12257 "some divs are unknown", return NULL
);
12259 ls
= isl_basic_map_get_local_space(bmap
);
12260 div
= isl_local_space_get_div(ls
, pos
);
12261 isl_local_space_free(ls
);
12266 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12269 return isl_basic_map_get_div(bset
, pos
);
12272 /* Plug in "subs" for dimension "type", "pos" of "bset".
12274 * Let i be the dimension to replace and let "subs" be of the form
12278 * Any integer division with a non-zero coefficient for i,
12280 * floor((a i + g)/m)
12284 * floor((a f + d g)/(m d))
12286 * Constraints of the form
12294 * We currently require that "subs" is an integral expression.
12295 * Handling rational expressions may require us to add stride constraints
12296 * as we do in isl_basic_set_preimage_multi_aff.
12298 __isl_give isl_basic_set
*isl_basic_set_substitute(
12299 __isl_take isl_basic_set
*bset
,
12300 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12306 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12309 bset
= isl_basic_set_cow(bset
);
12310 if (!bset
|| !subs
)
12313 ctx
= isl_basic_set_get_ctx(bset
);
12314 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12315 isl_die(ctx
, isl_error_invalid
,
12316 "spaces don't match", goto error
);
12317 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12318 isl_die(ctx
, isl_error_unsupported
,
12319 "cannot handle divs yet", goto error
);
12320 if (!isl_int_is_one(subs
->v
->el
[0]))
12321 isl_die(ctx
, isl_error_invalid
,
12322 "can only substitute integer expressions", goto error
);
12324 pos
+= isl_basic_set_offset(bset
, type
);
12328 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12329 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12331 isl_int_set(v
, bset
->eq
[i
][pos
]);
12332 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12333 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12334 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12337 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12338 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12340 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12341 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12342 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12343 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12346 for (i
= 0; i
< bset
->n_div
; ++i
) {
12347 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12349 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12350 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12351 isl_seq_combine(bset
->div
[i
] + 1,
12352 subs
->v
->el
[0], bset
->div
[i
] + 1,
12353 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12354 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12359 bset
= isl_basic_set_simplify(bset
);
12360 return isl_basic_set_finalize(bset
);
12362 isl_basic_set_free(bset
);
12366 /* Plug in "subs" for dimension "type", "pos" of "set".
12368 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12369 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12373 if (set
&& isl_set_plain_is_empty(set
))
12376 set
= isl_set_cow(set
);
12380 for (i
= set
->n
- 1; i
>= 0; --i
) {
12381 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12382 if (remove_if_empty(set
, i
) < 0)
12392 /* Check if the range of "ma" is compatible with the domain or range
12393 * (depending on "type") of "bmap".
12394 * Return -1 if anything is wrong.
12396 static int check_basic_map_compatible_range_multi_aff(
12397 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12398 __isl_keep isl_multi_aff
*ma
)
12401 isl_space
*ma_space
;
12403 ma_space
= isl_multi_aff_get_space(ma
);
12405 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12409 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12410 "parameters don't match", goto error
);
12411 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12415 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12416 "spaces don't match", goto error
);
12418 isl_space_free(ma_space
);
12421 isl_space_free(ma_space
);
12425 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12426 * coefficients before the transformed range of dimensions,
12427 * the "n_after" coefficients after the transformed range of dimensions
12428 * and the coefficients of the other divs in "bmap".
12430 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12431 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12436 isl_local_space
*ls
;
12441 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12445 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12446 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12447 for (i
= 0; i
< n_div
; ++i
) {
12448 int o_bmap
= 0, o_ls
= 0;
12450 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12451 o_bmap
+= 1 + 1 + n_param
;
12452 o_ls
+= 1 + 1 + n_param
;
12453 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12454 o_bmap
+= n_before
;
12455 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12456 ls
->div
->row
[i
] + o_ls
, n_set
);
12459 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12461 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12462 ls
->div
->row
[i
] + o_ls
, n_div
);
12465 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12466 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12470 isl_local_space_free(ls
);
12473 isl_local_space_free(ls
);
12477 /* How many stride constraints does "ma" enforce?
12478 * That is, how many of the affine expressions have a denominator
12479 * different from one?
12481 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12486 for (i
= 0; i
< ma
->n
; ++i
)
12487 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12493 /* For each affine expression in ma of the form
12495 * x_i = (f_i y + h_i)/m_i
12497 * with m_i different from one, add a constraint to "bmap"
12500 * f_i y + h_i = m_i alpha_i
12502 * with alpha_i an additional existentially quantified variable.
12504 * The input variables of "ma" correspond to a subset of the variables
12505 * of "bmap". There are "n_before" variables in "bmap" before this
12506 * subset and "n_after" variables after this subset.
12507 * The integer divisions of the affine expressions in "ma" are assumed
12508 * to have been aligned. There are "n_div_ma" of them and
12509 * they appear first in "bmap", straight after the "n_after" variables.
12511 static __isl_give isl_basic_map
*add_ma_strides(
12512 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12513 int n_before
, int n_after
, int n_div_ma
)
12521 total
= isl_basic_map_total_dim(bmap
);
12522 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12523 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12524 for (i
= 0; i
< ma
->n
; ++i
) {
12525 int o_bmap
= 0, o_ma
= 1;
12527 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12529 div
= isl_basic_map_alloc_div(bmap
);
12530 k
= isl_basic_map_alloc_equality(bmap
);
12531 if (div
< 0 || k
< 0)
12533 isl_int_set_si(bmap
->div
[div
][0], 0);
12534 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12535 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12536 o_bmap
+= 1 + n_param
;
12537 o_ma
+= 1 + n_param
;
12538 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12539 o_bmap
+= n_before
;
12540 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12541 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12544 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12546 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12547 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12548 o_bmap
+= n_div_ma
;
12550 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12551 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12557 isl_basic_map_free(bmap
);
12561 /* Replace the domain or range space (depending on "type) of "space" by "set".
12563 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12564 enum isl_dim_type type
, __isl_take isl_space
*set
)
12566 if (type
== isl_dim_in
) {
12567 space
= isl_space_range(space
);
12568 space
= isl_space_map_from_domain_and_range(set
, space
);
12570 space
= isl_space_domain(space
);
12571 space
= isl_space_map_from_domain_and_range(space
, set
);
12577 /* Compute the preimage of the domain or range (depending on "type")
12578 * of "bmap" under the function represented by "ma".
12579 * In other words, plug in "ma" in the domain or range of "bmap".
12580 * The result is a basic map that lives in the same space as "bmap"
12581 * except that the domain or range has been replaced by
12582 * the domain space of "ma".
12584 * If bmap is represented by
12586 * A(p) + S u + B x + T v + C(divs) >= 0,
12588 * where u and x are input and output dimensions if type == isl_dim_out
12589 * while x and v are input and output dimensions if type == isl_dim_in,
12590 * and ma is represented by
12592 * x = D(p) + F(y) + G(divs')
12594 * then the result is
12596 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12598 * The divs in the input set are similarly adjusted.
12601 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12605 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12606 * B_i G(divs') + c_i(divs))/n_i)
12608 * If bmap is not a rational map and if F(y) involves any denominators
12610 * x_i = (f_i y + h_i)/m_i
12612 * then additional constraints are added to ensure that we only
12613 * map back integer points. That is we enforce
12615 * f_i y + h_i = m_i alpha_i
12617 * with alpha_i an additional existentially quantified variable.
12619 * We first copy over the divs from "ma".
12620 * Then we add the modified constraints and divs from "bmap".
12621 * Finally, we add the stride constraints, if needed.
12623 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12624 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12625 __isl_take isl_multi_aff
*ma
)
12629 isl_basic_map
*res
= NULL
;
12630 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12631 isl_int f
, c1
, c2
, g
;
12632 int rational
, strides
;
12639 ma
= isl_multi_aff_align_divs(ma
);
12642 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12645 if (type
== isl_dim_in
) {
12647 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12649 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12652 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12653 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12655 space
= isl_multi_aff_get_domain_space(ma
);
12656 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12657 rational
= isl_basic_map_is_rational(bmap
);
12658 strides
= rational
? 0 : multi_aff_strides(ma
);
12659 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12660 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12662 res
= isl_basic_map_set_rational(res
);
12664 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12665 if (isl_basic_map_alloc_div(res
) < 0)
12668 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12671 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12672 k
= isl_basic_map_alloc_equality(res
);
12675 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12676 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12679 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12680 k
= isl_basic_map_alloc_inequality(res
);
12683 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12684 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12687 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12688 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12689 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12692 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12693 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12698 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12704 isl_basic_map_free(bmap
);
12705 isl_multi_aff_free(ma
);
12706 res
= isl_basic_set_simplify(res
);
12707 return isl_basic_map_finalize(res
);
12713 isl_basic_map_free(bmap
);
12714 isl_multi_aff_free(ma
);
12715 isl_basic_map_free(res
);
12719 /* Compute the preimage of "bset" under the function represented by "ma".
12720 * In other words, plug in "ma" in "bset". The result is a basic set
12721 * that lives in the domain space of "ma".
12723 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12724 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12726 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12729 /* Compute the preimage of the domain of "bmap" under the function
12730 * represented by "ma".
12731 * In other words, plug in "ma" in the domain of "bmap".
12732 * The result is a basic map that lives in the same space as "bmap"
12733 * except that the domain has been replaced by the domain space of "ma".
12735 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12736 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12738 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12741 /* Compute the preimage of the range of "bmap" under the function
12742 * represented by "ma".
12743 * In other words, plug in "ma" in the range of "bmap".
12744 * The result is a basic map that lives in the same space as "bmap"
12745 * except that the range has been replaced by the domain space of "ma".
12747 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12748 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12750 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12753 /* Check if the range of "ma" is compatible with the domain or range
12754 * (depending on "type") of "map".
12755 * Return -1 if anything is wrong.
12757 static int check_map_compatible_range_multi_aff(
12758 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12759 __isl_keep isl_multi_aff
*ma
)
12762 isl_space
*ma_space
;
12764 ma_space
= isl_multi_aff_get_space(ma
);
12765 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12766 isl_space_free(ma_space
);
12768 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12769 "spaces don't match", return -1);
12773 /* Compute the preimage of the domain or range (depending on "type")
12774 * of "map" under the function represented by "ma".
12775 * In other words, plug in "ma" in the domain or range of "map".
12776 * The result is a map that lives in the same space as "map"
12777 * except that the domain or range has been replaced by
12778 * the domain space of "ma".
12780 * The parameters are assumed to have been aligned.
12782 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12783 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12788 map
= isl_map_cow(map
);
12789 ma
= isl_multi_aff_align_divs(ma
);
12792 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12795 for (i
= 0; i
< map
->n
; ++i
) {
12796 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12797 isl_multi_aff_copy(ma
));
12802 space
= isl_multi_aff_get_domain_space(ma
);
12803 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12805 isl_space_free(map
->dim
);
12810 isl_multi_aff_free(ma
);
12812 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12813 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12816 isl_multi_aff_free(ma
);
12821 /* Compute the preimage of the domain or range (depending on "type")
12822 * of "map" under the function represented by "ma".
12823 * In other words, plug in "ma" in the domain or range of "map".
12824 * The result is a map that lives in the same space as "map"
12825 * except that the domain or range has been replaced by
12826 * the domain space of "ma".
12828 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12829 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12834 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12835 return map_preimage_multi_aff(map
, type
, ma
);
12837 if (!isl_space_has_named_params(map
->dim
) ||
12838 !isl_space_has_named_params(ma
->space
))
12839 isl_die(map
->ctx
, isl_error_invalid
,
12840 "unaligned unnamed parameters", goto error
);
12841 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12842 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12844 return map_preimage_multi_aff(map
, type
, ma
);
12846 isl_multi_aff_free(ma
);
12847 return isl_map_free(map
);
12850 /* Compute the preimage of "set" under the function represented by "ma".
12851 * In other words, plug in "ma" in "set". The result is a set
12852 * that lives in the domain space of "ma".
12854 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12855 __isl_take isl_multi_aff
*ma
)
12857 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12860 /* Compute the preimage of the domain of "map" under the function
12861 * represented by "ma".
12862 * In other words, plug in "ma" in the domain of "map".
12863 * The result is a map that lives in the same space as "map"
12864 * except that the domain has been replaced by the domain space of "ma".
12866 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12867 __isl_take isl_multi_aff
*ma
)
12869 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12872 /* Compute the preimage of the range of "map" under the function
12873 * represented by "ma".
12874 * In other words, plug in "ma" in the range of "map".
12875 * The result is a map that lives in the same space as "map"
12876 * except that the range has been replaced by the domain space of "ma".
12878 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12879 __isl_take isl_multi_aff
*ma
)
12881 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12884 /* Compute the preimage of "map" under the function represented by "pma".
12885 * In other words, plug in "pma" in the domain or range of "map".
12886 * The result is a map that lives in the same space as "map",
12887 * except that the space of type "type" has been replaced by
12888 * the domain space of "pma".
12890 * The parameters of "map" and "pma" are assumed to have been aligned.
12892 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12893 __isl_take isl_map
*map
, enum isl_dim_type type
,
12894 __isl_take isl_pw_multi_aff
*pma
)
12903 isl_pw_multi_aff_free(pma
);
12904 res
= isl_map_empty(isl_map_get_space(map
));
12909 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12910 isl_multi_aff_copy(pma
->p
[0].maff
));
12911 if (type
== isl_dim_in
)
12912 res
= isl_map_intersect_domain(res
,
12913 isl_map_copy(pma
->p
[0].set
));
12915 res
= isl_map_intersect_range(res
,
12916 isl_map_copy(pma
->p
[0].set
));
12918 for (i
= 1; i
< pma
->n
; ++i
) {
12921 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12922 isl_multi_aff_copy(pma
->p
[i
].maff
));
12923 if (type
== isl_dim_in
)
12924 res_i
= isl_map_intersect_domain(res_i
,
12925 isl_map_copy(pma
->p
[i
].set
));
12927 res_i
= isl_map_intersect_range(res_i
,
12928 isl_map_copy(pma
->p
[i
].set
));
12929 res
= isl_map_union(res
, res_i
);
12932 isl_pw_multi_aff_free(pma
);
12936 isl_pw_multi_aff_free(pma
);
12941 /* Compute the preimage of "map" under the function represented by "pma".
12942 * In other words, plug in "pma" in the domain or range of "map".
12943 * The result is a map that lives in the same space as "map",
12944 * except that the space of type "type" has been replaced by
12945 * the domain space of "pma".
12947 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12948 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12953 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12954 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12956 if (!isl_space_has_named_params(map
->dim
) ||
12957 !isl_space_has_named_params(pma
->dim
))
12958 isl_die(map
->ctx
, isl_error_invalid
,
12959 "unaligned unnamed parameters", goto error
);
12960 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12961 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12963 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12965 isl_pw_multi_aff_free(pma
);
12966 return isl_map_free(map
);
12969 /* Compute the preimage of "set" under the function represented by "pma".
12970 * In other words, plug in "pma" in "set". The result is a set
12971 * that lives in the domain space of "pma".
12973 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12974 __isl_take isl_pw_multi_aff
*pma
)
12976 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12979 /* Compute the preimage of the domain of "map" under the function
12980 * represented by "pma".
12981 * In other words, plug in "pma" in the domain of "map".
12982 * The result is a map that lives in the same space as "map",
12983 * except that domain space has been replaced by the domain space of "pma".
12985 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12986 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12988 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12991 /* Compute the preimage of the range of "map" under the function
12992 * represented by "pma".
12993 * In other words, plug in "pma" in the range of "map".
12994 * The result is a map that lives in the same space as "map",
12995 * except that range space has been replaced by the domain space of "pma".
12997 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12998 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13000 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13003 /* Compute the preimage of "map" under the function represented by "mpa".
13004 * In other words, plug in "mpa" in the domain or range of "map".
13005 * The result is a map that lives in the same space as "map",
13006 * except that the space of type "type" has been replaced by
13007 * the domain space of "mpa".
13009 * If the map does not involve any constraints that refer to the
13010 * dimensions of the substituted space, then the only possible
13011 * effect of "mpa" on the map is to map the space to a different space.
13012 * We create a separate isl_multi_aff to effectuate this change
13013 * in order to avoid spurious splitting of the map along the pieces
13016 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13017 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13020 isl_pw_multi_aff
*pma
;
13025 n
= isl_map_dim(map
, type
);
13026 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13030 space
= isl_multi_pw_aff_get_space(mpa
);
13031 isl_multi_pw_aff_free(mpa
);
13032 ma
= isl_multi_aff_zero(space
);
13033 return isl_map_preimage_multi_aff(map
, type
, ma
);
13036 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13037 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13040 isl_multi_pw_aff_free(mpa
);
13044 /* Compute the preimage of "map" under the function represented by "mpa".
13045 * In other words, plug in "mpa" in the domain "map".
13046 * The result is a map that lives in the same space as "map",
13047 * except that domain space has been replaced by the domain space of "mpa".
13049 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13050 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13052 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13055 /* Compute the preimage of "set" by the function represented by "mpa".
13056 * In other words, plug in "mpa" in "set".
13058 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13059 __isl_take isl_multi_pw_aff
*mpa
)
13061 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);