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 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1834 * div definitions accordingly.
1836 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1838 isl_basic_map_swap_div(bset
, a
, b
);
1841 /* Eliminate the specified n dimensions starting at first from the
1842 * constraints, without removing the dimensions from the space.
1843 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1845 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1846 enum isl_dim_type type
, unsigned first
, unsigned n
)
1855 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1856 isl_die(map
->ctx
, isl_error_invalid
,
1857 "index out of bounds", goto error
);
1859 map
= isl_map_cow(map
);
1863 for (i
= 0; i
< map
->n
; ++i
) {
1864 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1874 /* Eliminate the specified n dimensions starting at first from the
1875 * constraints, without removing the dimensions from the space.
1876 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1878 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1879 enum isl_dim_type type
, unsigned first
, unsigned n
)
1881 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1884 /* Eliminate the specified n dimensions starting at first from the
1885 * constraints, without removing the dimensions from the space.
1886 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1888 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1889 unsigned first
, unsigned n
)
1891 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1894 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1895 __isl_take isl_basic_map
*bmap
)
1899 bmap
= isl_basic_map_eliminate_vars(bmap
,
1900 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1904 return isl_basic_map_finalize(bmap
);
1907 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1908 __isl_take isl_basic_set
*bset
)
1910 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1911 (struct isl_basic_map
*)bset
);
1914 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1923 map
= isl_map_cow(map
);
1927 for (i
= 0; i
< map
->n
; ++i
) {
1928 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1938 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1940 return isl_map_remove_divs(set
);
1943 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1944 enum isl_dim_type type
, unsigned first
, unsigned n
)
1948 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1950 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1952 bmap
= isl_basic_map_eliminate_vars(bmap
,
1953 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1956 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1958 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1961 isl_basic_map_free(bmap
);
1965 /* Return true if the definition of the given div (recursively) involves
1966 * any of the given variables.
1968 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1969 unsigned first
, unsigned n
)
1972 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1974 if (isl_int_is_zero(bmap
->div
[div
][0]))
1976 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1979 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1980 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1982 if (div_involves_vars(bmap
, i
, first
, n
))
1989 /* Try and add a lower and/or upper bound on "div" to "bmap"
1990 * based on inequality "i".
1991 * "total" is the total number of variables (excluding the divs).
1992 * "v" is a temporary object that can be used during the calculations.
1993 * If "lb" is set, then a lower bound should be constructed.
1994 * If "ub" is set, then an upper bound should be constructed.
1996 * The calling function has already checked that the inequality does not
1997 * reference "div", but we still need to check that the inequality is
1998 * of the right form. We'll consider the case where we want to construct
1999 * a lower bound. The construction of upper bounds is similar.
2001 * Let "div" be of the form
2003 * q = floor((a + f(x))/d)
2005 * We essentially check if constraint "i" is of the form
2009 * so that we can use it to derive a lower bound on "div".
2010 * However, we allow a slightly more general form
2014 * with the condition that the coefficients of g(x) - f(x) are all
2016 * Rewriting this constraint as
2020 * adding a + f(x) to both sides and dividing by d, we obtain
2022 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2024 * Taking the floor on both sides, we obtain
2026 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2030 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2032 * In the case of an upper bound, we construct the constraint
2034 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2037 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2038 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2039 unsigned total
, isl_int v
, int lb
, int ub
)
2043 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2045 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2046 bmap
->div
[div
][1 + 1 + j
]);
2047 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2050 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2051 bmap
->div
[div
][1 + 1 + j
]);
2052 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2058 bmap
= isl_basic_map_cow(bmap
);
2059 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2061 int k
= isl_basic_map_alloc_inequality(bmap
);
2064 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2065 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2066 bmap
->div
[div
][1 + j
]);
2067 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2068 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2070 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2073 int k
= isl_basic_map_alloc_inequality(bmap
);
2076 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2077 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2078 bmap
->div
[div
][1 + j
]);
2079 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2080 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2082 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2085 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2088 isl_basic_map_free(bmap
);
2092 /* This function is called right before "div" is eliminated from "bmap"
2093 * using Fourier-Motzkin.
2094 * Look through the constraints of "bmap" for constraints on the argument
2095 * of the integer division and use them to construct constraints on the
2096 * integer division itself. These constraints can then be combined
2097 * during the Fourier-Motzkin elimination.
2098 * Note that it is only useful to introduce lower bounds on "div"
2099 * if "bmap" already contains upper bounds on "div" as the newly
2100 * introduce lower bounds can then be combined with the pre-existing
2101 * upper bounds. Similarly for upper bounds.
2102 * We therefore first check if "bmap" contains any lower and/or upper bounds
2105 * It is interesting to note that the introduction of these constraints
2106 * can indeed lead to more accurate results, even when compared to
2107 * deriving constraints on the argument of "div" from constraints on "div".
2108 * Consider, for example, the set
2110 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2112 * The second constraint can be rewritten as
2114 * 2 * [(-i-2j+3)/4] + k >= 0
2116 * from which we can derive
2118 * -i - 2j + 3 >= -2k
2124 * Combined with the first constraint, we obtain
2126 * -3 <= 3 + 2k or k >= -3
2128 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2129 * the first constraint, we obtain
2131 * [(i + 2j)/4] >= [-3/4] = -1
2133 * Combining this constraint with the second constraint, we obtain
2137 static __isl_give isl_basic_map
*insert_bounds_on_div(
2138 __isl_take isl_basic_map
*bmap
, int div
)
2141 int check_lb
, check_ub
;
2148 if (isl_int_is_zero(bmap
->div
[div
][0]))
2151 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2155 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2156 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2163 if (!check_lb
&& !check_ub
)
2168 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2169 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2172 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2173 check_lb
, check_ub
);
2181 /* Remove all divs (recursively) involving any of the given dimensions
2182 * in their definitions.
2184 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2185 __isl_take isl_basic_map
*bmap
,
2186 enum isl_dim_type type
, unsigned first
, unsigned n
)
2192 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2194 first
+= isl_basic_map_offset(bmap
, type
);
2196 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2197 if (!div_involves_vars(bmap
, i
, first
, n
))
2199 bmap
= insert_bounds_on_div(bmap
, i
);
2200 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2208 isl_basic_map_free(bmap
);
2212 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2213 __isl_take isl_basic_set
*bset
,
2214 enum isl_dim_type type
, unsigned first
, unsigned n
)
2216 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2219 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2220 enum isl_dim_type type
, unsigned first
, unsigned n
)
2229 map
= isl_map_cow(map
);
2233 for (i
= 0; i
< map
->n
; ++i
) {
2234 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2245 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2246 enum isl_dim_type type
, unsigned first
, unsigned n
)
2248 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2252 /* Does the desciption of "bmap" depend on the specified dimensions?
2253 * We also check whether the dimensions appear in any of the div definitions.
2254 * In principle there is no need for this check. If the dimensions appear
2255 * in a div definition, they also appear in the defining constraints of that
2258 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2259 enum isl_dim_type type
, unsigned first
, unsigned n
)
2264 return isl_bool_error
;
2266 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2267 isl_die(bmap
->ctx
, isl_error_invalid
,
2268 "index out of bounds", return isl_bool_error
);
2270 first
+= isl_basic_map_offset(bmap
, type
);
2271 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2272 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2273 return isl_bool_true
;
2274 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2275 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2276 return isl_bool_true
;
2277 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2278 if (isl_int_is_zero(bmap
->div
[i
][0]))
2280 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2281 return isl_bool_true
;
2284 return isl_bool_false
;
2287 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2288 enum isl_dim_type type
, unsigned first
, unsigned n
)
2293 return isl_bool_error
;
2295 if (first
+ n
> isl_map_dim(map
, type
))
2296 isl_die(map
->ctx
, isl_error_invalid
,
2297 "index out of bounds", return isl_bool_error
);
2299 for (i
= 0; i
< map
->n
; ++i
) {
2300 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2302 if (involves
< 0 || involves
)
2306 return isl_bool_false
;
2309 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2310 enum isl_dim_type type
, unsigned first
, unsigned n
)
2312 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2315 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2316 enum isl_dim_type type
, unsigned first
, unsigned n
)
2318 return isl_map_involves_dims(set
, type
, first
, n
);
2321 /* Does local variable "div" of "bmap" have a complete explicit representation?
2322 * Having a complete explicit representation requires not only
2323 * an explicit representation, but also that all local variables
2324 * that appear in this explicit representation in turn have
2325 * a complete explicit representation.
2327 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2330 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2333 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2334 if (marked
< 0 || marked
)
2335 return isl_bool_not(marked
);
2337 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2340 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2342 known
= isl_basic_map_div_is_known(bmap
, i
);
2343 if (known
< 0 || !known
)
2347 return isl_bool_true
;
2350 /* Does local variable "div" of "bset" have a complete explicit representation?
2352 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2354 return isl_basic_map_div_is_known(bset
, div
);
2357 /* Remove all divs that are unknown or defined in terms of unknown divs.
2359 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2360 __isl_take isl_basic_map
*bmap
)
2367 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2368 if (isl_basic_map_div_is_known(bmap
, i
))
2370 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2379 /* Remove all divs that are unknown or defined in terms of unknown divs.
2381 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2382 __isl_take isl_basic_set
*bset
)
2384 return isl_basic_map_remove_unknown_divs(bset
);
2387 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2396 map
= isl_map_cow(map
);
2400 for (i
= 0; i
< map
->n
; ++i
) {
2401 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2411 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2413 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2416 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2417 __isl_take isl_basic_set
*bset
,
2418 enum isl_dim_type type
, unsigned first
, unsigned n
)
2420 return (isl_basic_set
*)
2421 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2424 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2425 enum isl_dim_type type
, unsigned first
, unsigned n
)
2432 map
= isl_map_cow(map
);
2435 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2437 for (i
= 0; i
< map
->n
; ++i
) {
2438 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2439 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2443 map
= isl_map_drop(map
, type
, first
, n
);
2450 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2451 enum isl_dim_type type
, unsigned first
, unsigned n
)
2453 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2456 /* Project out n inputs starting at first using Fourier-Motzkin */
2457 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2458 unsigned first
, unsigned n
)
2460 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2463 static void dump_term(struct isl_basic_map
*bmap
,
2464 isl_int c
, int pos
, FILE *out
)
2467 unsigned in
= isl_basic_map_n_in(bmap
);
2468 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2469 unsigned nparam
= isl_basic_map_n_param(bmap
);
2471 isl_int_print(out
, c
, 0);
2473 if (!isl_int_is_one(c
))
2474 isl_int_print(out
, c
, 0);
2475 if (pos
< 1 + nparam
) {
2476 name
= isl_space_get_dim_name(bmap
->dim
,
2477 isl_dim_param
, pos
- 1);
2479 fprintf(out
, "%s", name
);
2481 fprintf(out
, "p%d", pos
- 1);
2482 } else if (pos
< 1 + nparam
+ in
)
2483 fprintf(out
, "i%d", pos
- 1 - nparam
);
2484 else if (pos
< 1 + nparam
+ dim
)
2485 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2487 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2491 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2492 int sign
, FILE *out
)
2496 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2500 for (i
= 0, first
= 1; i
< len
; ++i
) {
2501 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2504 fprintf(out
, " + ");
2506 isl_int_abs(v
, c
[i
]);
2507 dump_term(bmap
, v
, i
, out
);
2514 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2515 const char *op
, FILE *out
, int indent
)
2519 fprintf(out
, "%*s", indent
, "");
2521 dump_constraint_sign(bmap
, c
, 1, out
);
2522 fprintf(out
, " %s ", op
);
2523 dump_constraint_sign(bmap
, c
, -1, out
);
2527 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2528 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2530 fprintf(out
, "%*s", indent
, "");
2531 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2536 static void dump_constraints(struct isl_basic_map
*bmap
,
2537 isl_int
**c
, unsigned n
,
2538 const char *op
, FILE *out
, int indent
)
2542 for (i
= 0; i
< n
; ++i
)
2543 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2546 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2550 unsigned total
= isl_basic_map_total_dim(bmap
);
2552 for (j
= 0; j
< 1 + total
; ++j
) {
2553 if (isl_int_is_zero(exp
[j
]))
2555 if (!first
&& isl_int_is_pos(exp
[j
]))
2557 dump_term(bmap
, exp
[j
], j
, out
);
2562 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2566 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2567 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2569 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2570 fprintf(out
, "%*s", indent
, "");
2571 fprintf(out
, "e%d = [(", i
);
2572 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2574 isl_int_print(out
, bmap
->div
[i
][0], 0);
2575 fprintf(out
, "]\n");
2579 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2580 FILE *out
, int indent
)
2583 fprintf(out
, "null basic set\n");
2587 fprintf(out
, "%*s", indent
, "");
2588 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2589 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2590 bset
->extra
, bset
->flags
);
2591 dump((struct isl_basic_map
*)bset
, out
, indent
);
2594 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2595 FILE *out
, int indent
)
2598 fprintf(out
, "null basic map\n");
2602 fprintf(out
, "%*s", indent
, "");
2603 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2604 "flags: %x, n_name: %d\n",
2606 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2607 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2608 dump(bmap
, out
, indent
);
2611 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2616 total
= isl_basic_map_total_dim(bmap
);
2617 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2618 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2619 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2620 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2624 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2629 if (isl_space_dim(space
, isl_dim_in
) != 0)
2630 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2631 "set cannot have input dimensions", goto error
);
2632 return isl_map_alloc_space(space
, n
, flags
);
2634 isl_space_free(space
);
2638 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2639 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2641 struct isl_set
*set
;
2644 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2648 set
= isl_set_alloc_space(dims
, n
, flags
);
2652 /* Make sure "map" has room for at least "n" more basic maps.
2654 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2657 struct isl_map
*grown
= NULL
;
2661 isl_assert(map
->ctx
, n
>= 0, goto error
);
2662 if (map
->n
+ n
<= map
->size
)
2664 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2667 for (i
= 0; i
< map
->n
; ++i
) {
2668 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2676 isl_map_free(grown
);
2681 /* Make sure "set" has room for at least "n" more basic sets.
2683 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2685 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2688 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2691 struct isl_set
*dup
;
2696 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2699 for (i
= 0; i
< set
->n
; ++i
)
2700 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2704 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2706 return isl_map_from_basic_map(bset
);
2709 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2711 struct isl_map
*map
;
2716 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2717 return isl_map_add_basic_map(map
, bmap
);
2720 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2721 __isl_take isl_basic_set
*bset
)
2723 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2724 (struct isl_basic_map
*)bset
);
2727 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2729 return isl_map_free(set
);
2732 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2737 fprintf(out
, "null set\n");
2741 fprintf(out
, "%*s", indent
, "");
2742 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2743 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2745 for (i
= 0; i
< set
->n
; ++i
) {
2746 fprintf(out
, "%*s", indent
, "");
2747 fprintf(out
, "basic set %d:\n", i
);
2748 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2752 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2757 fprintf(out
, "null map\n");
2761 fprintf(out
, "%*s", indent
, "");
2762 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2763 "flags: %x, n_name: %d\n",
2764 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2765 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2766 for (i
= 0; i
< map
->n
; ++i
) {
2767 fprintf(out
, "%*s", indent
, "");
2768 fprintf(out
, "basic map %d:\n", i
);
2769 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2773 struct isl_basic_map
*isl_basic_map_intersect_domain(
2774 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2776 struct isl_basic_map
*bmap_domain
;
2781 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2782 bset
->dim
, isl_dim_param
), goto error
);
2784 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2785 isl_assert(bset
->ctx
,
2786 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2788 bmap
= isl_basic_map_cow(bmap
);
2791 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2792 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2793 bmap_domain
= isl_basic_map_from_domain(bset
);
2794 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2796 bmap
= isl_basic_map_simplify(bmap
);
2797 return isl_basic_map_finalize(bmap
);
2799 isl_basic_map_free(bmap
);
2800 isl_basic_set_free(bset
);
2804 struct isl_basic_map
*isl_basic_map_intersect_range(
2805 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2807 struct isl_basic_map
*bmap_range
;
2812 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2813 bset
->dim
, isl_dim_param
), goto error
);
2815 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2816 isl_assert(bset
->ctx
,
2817 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2819 if (isl_basic_set_plain_is_universe(bset
)) {
2820 isl_basic_set_free(bset
);
2824 bmap
= isl_basic_map_cow(bmap
);
2827 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2828 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2829 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2830 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2832 bmap
= isl_basic_map_simplify(bmap
);
2833 return isl_basic_map_finalize(bmap
);
2835 isl_basic_map_free(bmap
);
2836 isl_basic_set_free(bset
);
2840 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2841 __isl_keep isl_vec
*vec
)
2848 return isl_bool_error
;
2850 total
= 1 + isl_basic_map_total_dim(bmap
);
2851 if (total
!= vec
->size
)
2852 return isl_bool_error
;
2856 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2857 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2858 if (!isl_int_is_zero(s
)) {
2860 return isl_bool_false
;
2864 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2865 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2866 if (isl_int_is_neg(s
)) {
2868 return isl_bool_false
;
2874 return isl_bool_true
;
2877 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2878 __isl_keep isl_vec
*vec
)
2880 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2883 struct isl_basic_map
*isl_basic_map_intersect(
2884 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2886 struct isl_vec
*sample
= NULL
;
2888 if (!bmap1
|| !bmap2
)
2891 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2892 bmap2
->dim
, isl_dim_param
), goto error
);
2893 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2894 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2895 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2896 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2897 return isl_basic_map_intersect(bmap2
, bmap1
);
2899 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2900 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2901 isl_assert(bmap1
->ctx
,
2902 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2904 if (isl_basic_map_plain_is_empty(bmap1
)) {
2905 isl_basic_map_free(bmap2
);
2908 if (isl_basic_map_plain_is_empty(bmap2
)) {
2909 isl_basic_map_free(bmap1
);
2913 if (bmap1
->sample
&&
2914 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2915 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2916 sample
= isl_vec_copy(bmap1
->sample
);
2917 else if (bmap2
->sample
&&
2918 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2919 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2920 sample
= isl_vec_copy(bmap2
->sample
);
2922 bmap1
= isl_basic_map_cow(bmap1
);
2925 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2926 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2927 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2930 isl_vec_free(sample
);
2932 isl_vec_free(bmap1
->sample
);
2933 bmap1
->sample
= sample
;
2936 bmap1
= isl_basic_map_simplify(bmap1
);
2937 return isl_basic_map_finalize(bmap1
);
2940 isl_vec_free(sample
);
2941 isl_basic_map_free(bmap1
);
2942 isl_basic_map_free(bmap2
);
2946 struct isl_basic_set
*isl_basic_set_intersect(
2947 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2949 return (struct isl_basic_set
*)
2950 isl_basic_map_intersect(
2951 (struct isl_basic_map
*)bset1
,
2952 (struct isl_basic_map
*)bset2
);
2955 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2956 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2958 return isl_basic_set_intersect(bset1
, bset2
);
2961 /* Special case of isl_map_intersect, where both map1 and map2
2962 * are convex, without any divs and such that either map1 or map2
2963 * contains a single constraint. This constraint is then simply
2964 * added to the other map.
2966 static __isl_give isl_map
*map_intersect_add_constraint(
2967 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2969 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2970 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2971 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2972 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2974 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2975 return isl_map_intersect(map2
, map1
);
2977 isl_assert(map2
->ctx
,
2978 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2980 map1
= isl_map_cow(map1
);
2983 if (isl_map_plain_is_empty(map1
)) {
2987 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2988 if (map2
->p
[0]->n_eq
== 1)
2989 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2991 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2992 map2
->p
[0]->ineq
[0]);
2994 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2995 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2999 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3000 isl_basic_map_free(map1
->p
[0]);
3013 /* map2 may be either a parameter domain or a map living in the same
3016 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3017 __isl_take isl_map
*map2
)
3026 if ((isl_map_plain_is_empty(map1
) ||
3027 isl_map_plain_is_universe(map2
)) &&
3028 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3032 if ((isl_map_plain_is_empty(map2
) ||
3033 isl_map_plain_is_universe(map1
)) &&
3034 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3039 if (map1
->n
== 1 && map2
->n
== 1 &&
3040 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3041 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3042 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3043 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3044 return map_intersect_add_constraint(map1
, map2
);
3046 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3047 isl_space_dim(map2
->dim
, isl_dim_param
))
3048 isl_assert(map1
->ctx
,
3049 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3051 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3052 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3053 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3055 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3056 map1
->n
* map2
->n
, flags
);
3059 for (i
= 0; i
< map1
->n
; ++i
)
3060 for (j
= 0; j
< map2
->n
; ++j
) {
3061 struct isl_basic_map
*part
;
3062 part
= isl_basic_map_intersect(
3063 isl_basic_map_copy(map1
->p
[i
]),
3064 isl_basic_map_copy(map2
->p
[j
]));
3065 if (isl_basic_map_is_empty(part
) < 0)
3066 part
= isl_basic_map_free(part
);
3067 result
= isl_map_add_basic_map(result
, part
);
3080 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3081 __isl_take isl_map
*map2
)
3085 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3086 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3087 "spaces don't match", goto error
);
3088 return map_intersect_internal(map1
, map2
);
3095 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3096 __isl_take isl_map
*map2
)
3098 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3101 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3103 return (struct isl_set
*)
3104 isl_map_intersect((struct isl_map
*)set1
,
3105 (struct isl_map
*)set2
);
3108 /* map_intersect_internal accepts intersections
3109 * with parameter domains, so we can just call that function.
3111 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3112 __isl_take isl_set
*params
)
3114 return map_intersect_internal(map
, params
);
3117 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3118 __isl_take isl_map
*map2
)
3120 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3123 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3124 __isl_take isl_set
*params
)
3126 return isl_map_intersect_params(set
, params
);
3129 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3132 unsigned pos
, n1
, n2
;
3136 bmap
= isl_basic_map_cow(bmap
);
3139 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3140 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3141 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3142 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3143 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3144 return isl_basic_map_reset_space(bmap
, space
);
3147 static __isl_give isl_basic_map
*basic_map_space_reset(
3148 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3154 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3157 space
= isl_basic_map_get_space(bmap
);
3158 space
= isl_space_reset(space
, type
);
3159 bmap
= isl_basic_map_reset_space(bmap
, space
);
3163 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3164 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3165 unsigned pos
, unsigned n
)
3168 struct isl_basic_map
*res
;
3169 struct isl_dim_map
*dim_map
;
3170 unsigned total
, off
;
3171 enum isl_dim_type t
;
3174 return basic_map_space_reset(bmap
, type
);
3179 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3181 total
= isl_basic_map_total_dim(bmap
) + n
;
3182 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3184 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3186 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3188 unsigned size
= isl_basic_map_dim(bmap
, t
);
3189 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3191 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3192 pos
, size
- pos
, off
+ pos
+ n
);
3194 off
+= isl_space_dim(res_dim
, t
);
3196 isl_dim_map_div(dim_map
, bmap
, off
);
3198 res
= isl_basic_map_alloc_space(res_dim
,
3199 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3200 if (isl_basic_map_is_rational(bmap
))
3201 res
= isl_basic_map_set_rational(res
);
3202 if (isl_basic_map_plain_is_empty(bmap
)) {
3203 isl_basic_map_free(bmap
);
3205 return isl_basic_map_set_to_empty(res
);
3207 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3208 return isl_basic_map_finalize(res
);
3211 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3212 __isl_take isl_basic_set
*bset
,
3213 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3215 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3218 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3219 enum isl_dim_type type
, unsigned n
)
3223 return isl_basic_map_insert_dims(bmap
, type
,
3224 isl_basic_map_dim(bmap
, type
), n
);
3227 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3228 enum isl_dim_type type
, unsigned n
)
3232 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3233 return isl_basic_map_add_dims(bset
, type
, n
);
3235 isl_basic_set_free(bset
);
3239 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3240 enum isl_dim_type type
)
3244 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3247 space
= isl_map_get_space(map
);
3248 space
= isl_space_reset(space
, type
);
3249 map
= isl_map_reset_space(map
, space
);
3253 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3254 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3259 return map_space_reset(map
, type
);
3261 map
= isl_map_cow(map
);
3265 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3269 for (i
= 0; i
< map
->n
; ++i
) {
3270 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3281 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3282 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3284 return isl_map_insert_dims(set
, type
, pos
, n
);
3287 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3288 enum isl_dim_type type
, unsigned n
)
3292 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3295 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3296 enum isl_dim_type type
, unsigned n
)
3300 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3301 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3307 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3308 __isl_take isl_basic_map
*bmap
,
3309 enum isl_dim_type dst_type
, unsigned dst_pos
,
3310 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3312 struct isl_dim_map
*dim_map
;
3313 struct isl_basic_map
*res
;
3314 enum isl_dim_type t
;
3315 unsigned total
, off
;
3322 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3325 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3328 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3330 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3331 pos(bmap
->dim
, src_type
) + src_pos
+
3332 ((src_type
< dst_type
) ? n
: 0)) {
3333 bmap
= isl_basic_map_cow(bmap
);
3337 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3338 src_type
, src_pos
, n
);
3342 bmap
= isl_basic_map_finalize(bmap
);
3347 total
= isl_basic_map_total_dim(bmap
);
3348 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3351 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3352 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3353 if (t
== dst_type
) {
3354 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3357 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3360 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3361 dst_pos
, size
- dst_pos
, off
);
3362 off
+= size
- dst_pos
;
3363 } else if (t
== src_type
) {
3364 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3367 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3368 src_pos
+ n
, size
- src_pos
- n
, off
);
3369 off
+= size
- src_pos
- n
;
3371 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3375 isl_dim_map_div(dim_map
, bmap
, off
);
3377 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3378 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3379 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3383 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3384 src_type
, src_pos
, n
);
3388 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3389 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3390 bmap
= isl_basic_map_finalize(bmap
);
3394 isl_basic_map_free(bmap
);
3398 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3399 enum isl_dim_type dst_type
, unsigned dst_pos
,
3400 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3402 return (isl_basic_set
*)isl_basic_map_move_dims(
3403 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3406 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3407 enum isl_dim_type dst_type
, unsigned dst_pos
,
3408 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3412 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3413 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3414 src_type
, src_pos
, n
);
3420 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3421 enum isl_dim_type dst_type
, unsigned dst_pos
,
3422 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3431 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3434 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3437 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3439 map
= isl_map_cow(map
);
3443 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3447 for (i
= 0; i
< map
->n
; ++i
) {
3448 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3450 src_type
, src_pos
, n
);
3461 /* Move the specified dimensions to the last columns right before
3462 * the divs. Don't change the dimension specification of bmap.
3463 * That's the responsibility of the caller.
3465 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3466 enum isl_dim_type type
, unsigned first
, unsigned n
)
3468 struct isl_dim_map
*dim_map
;
3469 struct isl_basic_map
*res
;
3470 enum isl_dim_type t
;
3471 unsigned total
, off
;
3475 if (pos(bmap
->dim
, type
) + first
+ n
==
3476 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3479 total
= isl_basic_map_total_dim(bmap
);
3480 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3483 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3484 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3486 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3489 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3490 first
, n
, total
- bmap
->n_div
- n
);
3491 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3492 first
+ n
, size
- (first
+ n
), off
);
3493 off
+= size
- (first
+ n
);
3495 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3499 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3501 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3502 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3503 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3507 /* Insert "n" rows in the divs of "bmap".
3509 * The number of columns is not changed, which means that the last
3510 * dimensions of "bmap" are being reintepreted as the new divs.
3511 * The space of "bmap" is not adjusted, however, which means
3512 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3513 * from the space of "bmap" is the responsibility of the caller.
3515 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3523 bmap
= isl_basic_map_cow(bmap
);
3527 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3528 old
= bmap
->block2
.data
;
3529 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3530 (bmap
->extra
+ n
) * (1 + row_size
));
3531 if (!bmap
->block2
.data
)
3532 return isl_basic_map_free(bmap
);
3533 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3535 return isl_basic_map_free(bmap
);
3536 for (i
= 0; i
< n
; ++i
) {
3537 new_div
[i
] = bmap
->block2
.data
+
3538 (bmap
->extra
+ i
) * (1 + row_size
);
3539 isl_seq_clr(new_div
[i
], 1 + row_size
);
3541 for (i
= 0; i
< bmap
->extra
; ++i
)
3542 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3544 bmap
->div
= new_div
;
3551 /* Drop constraints from "bmap" that only involve the variables
3552 * of "type" in the range [first, first + n] that are not related
3553 * to any of the variables outside that interval.
3554 * These constraints cannot influence the values for the variables
3555 * outside the interval, except in case they cause "bmap" to be empty.
3556 * Only drop the constraints if "bmap" is known to be non-empty.
3558 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3559 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3560 unsigned first
, unsigned n
)
3564 unsigned dim
, n_div
;
3567 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3569 return isl_basic_map_free(bmap
);
3573 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3574 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3575 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3577 return isl_basic_map_free(bmap
);
3578 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3579 for (i
= 0; i
< first
; ++i
)
3581 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3584 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3589 /* Turn the n dimensions of type type, starting at first
3590 * into existentially quantified variables.
3592 * If a subset of the projected out variables are unrelated
3593 * to any of the variables that remain, then the constraints
3594 * involving this subset are simply dropped first.
3596 __isl_give isl_basic_map
*isl_basic_map_project_out(
3597 __isl_take isl_basic_map
*bmap
,
3598 enum isl_dim_type type
, unsigned first
, unsigned n
)
3601 return basic_map_space_reset(bmap
, type
);
3602 if (type
== isl_dim_div
)
3603 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3604 "cannot project out existentially quantified variables",
3605 return isl_basic_map_free(bmap
));
3607 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3611 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3612 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3614 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3617 bmap
= move_last(bmap
, type
, first
, n
);
3618 bmap
= isl_basic_map_cow(bmap
);
3619 bmap
= insert_div_rows(bmap
, n
);
3623 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3626 bmap
= isl_basic_map_simplify(bmap
);
3627 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3628 return isl_basic_map_finalize(bmap
);
3630 isl_basic_map_free(bmap
);
3634 /* Turn the n dimensions of type type, starting at first
3635 * into existentially quantified variables.
3637 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3638 enum isl_dim_type type
, unsigned first
, unsigned n
)
3640 return (isl_basic_set
*)isl_basic_map_project_out(
3641 (isl_basic_map
*)bset
, type
, first
, n
);
3644 /* Turn the n dimensions of type type, starting at first
3645 * into existentially quantified variables.
3647 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3648 enum isl_dim_type type
, unsigned first
, unsigned n
)
3656 return map_space_reset(map
, type
);
3658 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3660 map
= isl_map_cow(map
);
3664 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3668 for (i
= 0; i
< map
->n
; ++i
) {
3669 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3680 /* Turn the n dimensions of type type, starting at first
3681 * into existentially quantified variables.
3683 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3684 enum isl_dim_type type
, unsigned first
, unsigned n
)
3686 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3689 /* Return a map that projects the elements in "set" onto their
3690 * "n" set dimensions starting at "first".
3691 * "type" should be equal to isl_dim_set.
3693 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3694 enum isl_dim_type type
, unsigned first
, unsigned n
)
3702 if (type
!= isl_dim_set
)
3703 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3704 "only set dimensions can be projected out", goto error
);
3705 dim
= isl_set_dim(set
, isl_dim_set
);
3706 if (first
+ n
> dim
|| first
+ n
< first
)
3707 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3708 "index out of bounds", goto error
);
3710 map
= isl_map_from_domain(set
);
3711 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3712 for (i
= 0; i
< n
; ++i
)
3713 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3721 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3725 for (i
= 0; i
< n
; ++i
) {
3726 j
= isl_basic_map_alloc_div(bmap
);
3729 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3733 isl_basic_map_free(bmap
);
3737 struct isl_basic_map
*isl_basic_map_apply_range(
3738 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3740 isl_space
*dim_result
= NULL
;
3741 struct isl_basic_map
*bmap
;
3742 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3743 struct isl_dim_map
*dim_map1
, *dim_map2
;
3745 if (!bmap1
|| !bmap2
)
3747 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3748 bmap2
->dim
, isl_dim_param
))
3749 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3750 "parameters don't match", goto error
);
3751 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3752 bmap2
->dim
, isl_dim_in
))
3753 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3754 "spaces don't match", goto error
);
3756 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3757 isl_space_copy(bmap2
->dim
));
3759 n_in
= isl_basic_map_n_in(bmap1
);
3760 n_out
= isl_basic_map_n_out(bmap2
);
3761 n
= isl_basic_map_n_out(bmap1
);
3762 nparam
= isl_basic_map_n_param(bmap1
);
3764 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3765 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3766 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3767 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3768 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3769 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3770 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3771 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3772 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3773 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3774 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3776 bmap
= isl_basic_map_alloc_space(dim_result
,
3777 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3778 bmap1
->n_eq
+ bmap2
->n_eq
,
3779 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3780 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3781 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3782 bmap
= add_divs(bmap
, n
);
3783 bmap
= isl_basic_map_simplify(bmap
);
3784 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3785 return isl_basic_map_finalize(bmap
);
3787 isl_basic_map_free(bmap1
);
3788 isl_basic_map_free(bmap2
);
3792 struct isl_basic_set
*isl_basic_set_apply(
3793 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3798 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3801 return (struct isl_basic_set
*)
3802 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3804 isl_basic_set_free(bset
);
3805 isl_basic_map_free(bmap
);
3809 struct isl_basic_map
*isl_basic_map_apply_domain(
3810 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3812 if (!bmap1
|| !bmap2
)
3815 isl_assert(bmap1
->ctx
,
3816 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3817 isl_assert(bmap1
->ctx
,
3818 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3821 bmap1
= isl_basic_map_reverse(bmap1
);
3822 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3823 return isl_basic_map_reverse(bmap1
);
3825 isl_basic_map_free(bmap1
);
3826 isl_basic_map_free(bmap2
);
3830 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3831 * A \cap B -> f(A) + f(B)
3833 struct isl_basic_map
*isl_basic_map_sum(
3834 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3836 unsigned n_in
, n_out
, nparam
, total
, pos
;
3837 struct isl_basic_map
*bmap
= NULL
;
3838 struct isl_dim_map
*dim_map1
, *dim_map2
;
3841 if (!bmap1
|| !bmap2
)
3844 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3847 nparam
= isl_basic_map_n_param(bmap1
);
3848 n_in
= isl_basic_map_n_in(bmap1
);
3849 n_out
= isl_basic_map_n_out(bmap1
);
3851 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3852 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3853 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3854 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3855 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3856 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3857 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3858 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3859 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3860 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3861 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3863 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3864 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3865 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3866 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3867 for (i
= 0; i
< n_out
; ++i
) {
3868 int j
= isl_basic_map_alloc_equality(bmap
);
3871 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3872 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3873 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3874 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3876 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3877 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3878 bmap
= add_divs(bmap
, 2 * n_out
);
3880 bmap
= isl_basic_map_simplify(bmap
);
3881 return isl_basic_map_finalize(bmap
);
3883 isl_basic_map_free(bmap
);
3884 isl_basic_map_free(bmap1
);
3885 isl_basic_map_free(bmap2
);
3889 /* Given two maps A -> f(A) and B -> g(B), construct a map
3890 * A \cap B -> f(A) + f(B)
3892 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3894 struct isl_map
*result
;
3900 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3902 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3903 map1
->n
* map2
->n
, 0);
3906 for (i
= 0; i
< map1
->n
; ++i
)
3907 for (j
= 0; j
< map2
->n
; ++j
) {
3908 struct isl_basic_map
*part
;
3909 part
= isl_basic_map_sum(
3910 isl_basic_map_copy(map1
->p
[i
]),
3911 isl_basic_map_copy(map2
->p
[j
]));
3912 if (isl_basic_map_is_empty(part
))
3913 isl_basic_map_free(part
);
3915 result
= isl_map_add_basic_map(result
, part
);
3928 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3929 __isl_take isl_set
*set2
)
3931 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3934 /* Given a basic map A -> f(A), construct A -> -f(A).
3936 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3941 bmap
= isl_basic_map_cow(bmap
);
3945 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3946 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3947 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3948 for (j
= 0; j
< n
; ++j
)
3949 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3950 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3951 for (j
= 0; j
< n
; ++j
)
3952 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3953 for (i
= 0; i
< bmap
->n_div
; ++i
)
3954 for (j
= 0; j
< n
; ++j
)
3955 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3956 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3957 return isl_basic_map_finalize(bmap
);
3960 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3962 return isl_basic_map_neg(bset
);
3965 /* Given a map A -> f(A), construct A -> -f(A).
3967 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3971 map
= isl_map_cow(map
);
3975 for (i
= 0; i
< map
->n
; ++i
) {
3976 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3987 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3989 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3992 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3993 * A -> floor(f(A)/d).
3995 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3998 unsigned n_in
, n_out
, nparam
, total
, pos
;
3999 struct isl_basic_map
*result
= NULL
;
4000 struct isl_dim_map
*dim_map
;
4006 nparam
= isl_basic_map_n_param(bmap
);
4007 n_in
= isl_basic_map_n_in(bmap
);
4008 n_out
= isl_basic_map_n_out(bmap
);
4010 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4011 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4012 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4013 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4014 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4015 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4017 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4018 bmap
->n_div
+ n_out
,
4019 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4020 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4021 result
= add_divs(result
, n_out
);
4022 for (i
= 0; i
< n_out
; ++i
) {
4024 j
= isl_basic_map_alloc_inequality(result
);
4027 isl_seq_clr(result
->ineq
[j
], 1+total
);
4028 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4029 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4030 j
= isl_basic_map_alloc_inequality(result
);
4033 isl_seq_clr(result
->ineq
[j
], 1+total
);
4034 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4035 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4036 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4039 result
= isl_basic_map_simplify(result
);
4040 return isl_basic_map_finalize(result
);
4042 isl_basic_map_free(result
);
4046 /* Given a map A -> f(A) and an integer d, construct a map
4047 * A -> floor(f(A)/d).
4049 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4053 map
= isl_map_cow(map
);
4057 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4058 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4059 for (i
= 0; i
< map
->n
; ++i
) {
4060 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4071 /* Given a map A -> f(A) and an integer d, construct a map
4072 * A -> floor(f(A)/d).
4074 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4075 __isl_take isl_val
*d
)
4079 if (!isl_val_is_int(d
))
4080 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4081 "expecting integer denominator", goto error
);
4082 map
= isl_map_floordiv(map
, d
->n
);
4091 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4097 i
= isl_basic_map_alloc_equality(bmap
);
4100 nparam
= isl_basic_map_n_param(bmap
);
4101 n_in
= isl_basic_map_n_in(bmap
);
4102 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4103 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4104 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4105 return isl_basic_map_finalize(bmap
);
4107 isl_basic_map_free(bmap
);
4111 /* Add a constraints to "bmap" expressing i_pos < o_pos
4113 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4119 i
= isl_basic_map_alloc_inequality(bmap
);
4122 nparam
= isl_basic_map_n_param(bmap
);
4123 n_in
= isl_basic_map_n_in(bmap
);
4124 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4125 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4126 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4127 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4128 return isl_basic_map_finalize(bmap
);
4130 isl_basic_map_free(bmap
);
4134 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4136 static __isl_give isl_basic_map
*var_less_or_equal(
4137 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4143 i
= isl_basic_map_alloc_inequality(bmap
);
4146 nparam
= isl_basic_map_n_param(bmap
);
4147 n_in
= isl_basic_map_n_in(bmap
);
4148 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4149 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4150 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4151 return isl_basic_map_finalize(bmap
);
4153 isl_basic_map_free(bmap
);
4157 /* Add a constraints to "bmap" expressing i_pos > o_pos
4159 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4165 i
= isl_basic_map_alloc_inequality(bmap
);
4168 nparam
= isl_basic_map_n_param(bmap
);
4169 n_in
= isl_basic_map_n_in(bmap
);
4170 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4171 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4172 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4173 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4174 return isl_basic_map_finalize(bmap
);
4176 isl_basic_map_free(bmap
);
4180 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4182 static __isl_give isl_basic_map
*var_more_or_equal(
4183 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4189 i
= isl_basic_map_alloc_inequality(bmap
);
4192 nparam
= isl_basic_map_n_param(bmap
);
4193 n_in
= isl_basic_map_n_in(bmap
);
4194 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4195 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4196 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4197 return isl_basic_map_finalize(bmap
);
4199 isl_basic_map_free(bmap
);
4203 __isl_give isl_basic_map
*isl_basic_map_equal(
4204 __isl_take isl_space
*dim
, unsigned n_equal
)
4207 struct isl_basic_map
*bmap
;
4208 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4211 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4212 bmap
= var_equal(bmap
, i
);
4213 return isl_basic_map_finalize(bmap
);
4216 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4218 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4222 struct isl_basic_map
*bmap
;
4223 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4226 for (i
= 0; i
< pos
&& bmap
; ++i
)
4227 bmap
= var_equal(bmap
, i
);
4229 bmap
= var_less(bmap
, pos
);
4230 return isl_basic_map_finalize(bmap
);
4233 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4235 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4236 __isl_take isl_space
*dim
, unsigned pos
)
4239 isl_basic_map
*bmap
;
4241 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4242 for (i
= 0; i
< pos
; ++i
)
4243 bmap
= var_equal(bmap
, i
);
4244 bmap
= var_less_or_equal(bmap
, pos
);
4245 return isl_basic_map_finalize(bmap
);
4248 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4250 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4254 struct isl_basic_map
*bmap
;
4255 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4258 for (i
= 0; i
< pos
&& bmap
; ++i
)
4259 bmap
= var_equal(bmap
, i
);
4261 bmap
= var_more(bmap
, pos
);
4262 return isl_basic_map_finalize(bmap
);
4265 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4267 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4268 __isl_take isl_space
*dim
, unsigned pos
)
4271 isl_basic_map
*bmap
;
4273 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4274 for (i
= 0; i
< pos
; ++i
)
4275 bmap
= var_equal(bmap
, i
);
4276 bmap
= var_more_or_equal(bmap
, pos
);
4277 return isl_basic_map_finalize(bmap
);
4280 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4281 unsigned n
, int equal
)
4283 struct isl_map
*map
;
4286 if (n
== 0 && equal
)
4287 return isl_map_universe(dims
);
4289 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4291 for (i
= 0; i
+ 1 < n
; ++i
)
4292 map
= isl_map_add_basic_map(map
,
4293 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4296 map
= isl_map_add_basic_map(map
,
4297 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4299 map
= isl_map_add_basic_map(map
,
4300 isl_basic_map_less_at(dims
, n
- 1));
4302 isl_space_free(dims
);
4307 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4311 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4314 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4316 return map_lex_lte_first(dim
, n
, 0);
4319 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4321 return map_lex_lte_first(dim
, n
, 1);
4324 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4326 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4329 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4331 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4334 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4335 unsigned n
, int equal
)
4337 struct isl_map
*map
;
4340 if (n
== 0 && equal
)
4341 return isl_map_universe(dims
);
4343 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4345 for (i
= 0; i
+ 1 < n
; ++i
)
4346 map
= isl_map_add_basic_map(map
,
4347 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4350 map
= isl_map_add_basic_map(map
,
4351 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4353 map
= isl_map_add_basic_map(map
,
4354 isl_basic_map_more_at(dims
, n
- 1));
4356 isl_space_free(dims
);
4361 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4365 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4368 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4370 return map_lex_gte_first(dim
, n
, 0);
4373 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4375 return map_lex_gte_first(dim
, n
, 1);
4378 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4380 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4383 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4385 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4388 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4389 __isl_take isl_set
*set2
)
4392 map
= isl_map_lex_le(isl_set_get_space(set1
));
4393 map
= isl_map_intersect_domain(map
, set1
);
4394 map
= isl_map_intersect_range(map
, set2
);
4398 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4399 __isl_take isl_set
*set2
)
4402 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4403 map
= isl_map_intersect_domain(map
, set1
);
4404 map
= isl_map_intersect_range(map
, set2
);
4408 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4409 __isl_take isl_set
*set2
)
4412 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4413 map
= isl_map_intersect_domain(map
, set1
);
4414 map
= isl_map_intersect_range(map
, set2
);
4418 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4419 __isl_take isl_set
*set2
)
4422 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4423 map
= isl_map_intersect_domain(map
, set1
);
4424 map
= isl_map_intersect_range(map
, set2
);
4428 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4429 __isl_take isl_map
*map2
)
4432 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4433 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4434 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4438 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4439 __isl_take isl_map
*map2
)
4442 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4443 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4444 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4448 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4449 __isl_take isl_map
*map2
)
4452 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4453 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4454 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4458 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4459 __isl_take isl_map
*map2
)
4462 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4463 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4464 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4468 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4469 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4471 struct isl_basic_map
*bmap
;
4473 bset
= isl_basic_set_cow(bset
);
4477 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4478 isl_space_free(bset
->dim
);
4479 bmap
= (struct isl_basic_map
*) bset
;
4481 return isl_basic_map_finalize(bmap
);
4483 isl_basic_set_free(bset
);
4484 isl_space_free(dim
);
4488 /* For a div d = floor(f/m), add the constraint
4492 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4493 unsigned pos
, isl_int
*div
)
4496 unsigned total
= isl_basic_map_total_dim(bmap
);
4498 i
= isl_basic_map_alloc_inequality(bmap
);
4501 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4502 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4507 /* For a div d = floor(f/m), add the constraint
4509 * -(f-(m-1)) + m d >= 0
4511 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4512 unsigned pos
, isl_int
*div
)
4515 unsigned total
= isl_basic_map_total_dim(bmap
);
4517 i
= isl_basic_map_alloc_inequality(bmap
);
4520 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4521 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4522 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4523 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4528 /* For a div d = floor(f/m), add the constraints
4531 * -(f-(m-1)) + m d >= 0
4533 * Note that the second constraint is the negation of
4537 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4538 unsigned pos
, isl_int
*div
)
4540 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4542 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4547 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4548 unsigned pos
, isl_int
*div
)
4550 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4554 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4556 unsigned total
= isl_basic_map_total_dim(bmap
);
4557 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4559 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4563 /* For each known div d = floor(f/m), add the constraints
4566 * -(f-(m-1)) + m d >= 0
4568 * Remove duplicate constraints in case of some these div constraints
4569 * already appear in "bmap".
4571 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4572 __isl_take isl_basic_map
*bmap
)
4578 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4582 bmap
= add_known_div_constraints(bmap
);
4583 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4584 bmap
= isl_basic_map_finalize(bmap
);
4588 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4590 * In particular, if this div is of the form d = floor(f/m),
4591 * then add the constraint
4595 * if sign < 0 or the constraint
4597 * -(f-(m-1)) + m d >= 0
4601 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4602 unsigned div
, int sign
)
4610 total
= isl_basic_map_total_dim(bmap
);
4611 div_pos
= total
- bmap
->n_div
+ div
;
4614 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4616 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4619 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4621 return isl_basic_map_add_div_constraints(bset
, div
);
4624 struct isl_basic_set
*isl_basic_map_underlying_set(
4625 struct isl_basic_map
*bmap
)
4629 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4631 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4632 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4633 return (struct isl_basic_set
*)bmap
;
4634 bmap
= isl_basic_map_cow(bmap
);
4637 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4640 bmap
->extra
-= bmap
->n_div
;
4642 bmap
= isl_basic_map_finalize(bmap
);
4643 return (struct isl_basic_set
*)bmap
;
4645 isl_basic_map_free(bmap
);
4649 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4650 __isl_take isl_basic_set
*bset
)
4652 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4655 /* Replace each element in "list" by the result of applying
4656 * isl_basic_map_underlying_set to the element.
4658 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4659 __isl_take isl_basic_map_list
*list
)
4666 n
= isl_basic_map_list_n_basic_map(list
);
4667 for (i
= 0; i
< n
; ++i
) {
4668 isl_basic_map
*bmap
;
4669 isl_basic_set
*bset
;
4671 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4672 bset
= isl_basic_set_underlying_set(bmap
);
4673 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4679 struct isl_basic_map
*isl_basic_map_overlying_set(
4680 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4682 struct isl_basic_map
*bmap
;
4683 struct isl_ctx
*ctx
;
4690 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4691 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4692 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4694 if (like
->n_div
== 0) {
4695 isl_space
*space
= isl_basic_map_get_space(like
);
4696 isl_basic_map_free(like
);
4697 return isl_basic_map_reset_space(bset
, space
);
4699 bset
= isl_basic_set_cow(bset
);
4702 total
= bset
->dim
->n_out
+ bset
->extra
;
4703 bmap
= (struct isl_basic_map
*)bset
;
4704 isl_space_free(bmap
->dim
);
4705 bmap
->dim
= isl_space_copy(like
->dim
);
4708 bmap
->n_div
= like
->n_div
;
4709 bmap
->extra
+= like
->n_div
;
4713 ltotal
= total
- bmap
->extra
+ like
->extra
;
4716 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4717 bmap
->extra
* (1 + 1 + total
));
4718 if (isl_blk_is_error(bmap
->block2
))
4720 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4724 for (i
= 0; i
< bmap
->extra
; ++i
)
4725 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4726 for (i
= 0; i
< like
->n_div
; ++i
) {
4727 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4728 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4730 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4732 isl_basic_map_free(like
);
4733 bmap
= isl_basic_map_simplify(bmap
);
4734 bmap
= isl_basic_map_finalize(bmap
);
4737 isl_basic_map_free(like
);
4738 isl_basic_set_free(bset
);
4742 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4743 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4745 return (struct isl_basic_set
*)
4746 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4749 struct isl_set
*isl_set_from_underlying_set(
4750 struct isl_set
*set
, struct isl_basic_set
*like
)
4756 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4758 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4759 isl_basic_set_free(like
);
4762 set
= isl_set_cow(set
);
4765 for (i
= 0; i
< set
->n
; ++i
) {
4766 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4767 isl_basic_set_copy(like
));
4771 isl_space_free(set
->dim
);
4772 set
->dim
= isl_space_copy(like
->dim
);
4775 isl_basic_set_free(like
);
4778 isl_basic_set_free(like
);
4783 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4787 map
= isl_map_cow(map
);
4790 map
->dim
= isl_space_cow(map
->dim
);
4794 for (i
= 1; i
< map
->n
; ++i
)
4795 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4797 for (i
= 0; i
< map
->n
; ++i
) {
4798 map
->p
[i
] = (struct isl_basic_map
*)
4799 isl_basic_map_underlying_set(map
->p
[i
]);
4804 map
->dim
= isl_space_underlying(map
->dim
, 0);
4806 isl_space_free(map
->dim
);
4807 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4811 return (struct isl_set
*)map
;
4817 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4819 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4822 /* Replace the space of "bmap" by "space".
4824 * If the space of "bmap" is identical to "space" (including the identifiers
4825 * of the input and output dimensions), then simply return the original input.
4827 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4828 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4834 equal
= isl_space_is_equal(bmap
->dim
, space
);
4835 if (equal
>= 0 && equal
)
4836 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4838 if (equal
>= 0 && equal
)
4839 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4840 space
, isl_dim_out
);
4844 isl_space_free(space
);
4847 bmap
= isl_basic_map_cow(bmap
);
4848 if (!bmap
|| !space
)
4851 isl_space_free(bmap
->dim
);
4854 bmap
= isl_basic_map_finalize(bmap
);
4858 isl_basic_map_free(bmap
);
4859 isl_space_free(space
);
4863 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4864 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4866 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4870 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4871 __isl_take isl_space
*dim
)
4875 map
= isl_map_cow(map
);
4879 for (i
= 0; i
< map
->n
; ++i
) {
4880 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4881 isl_space_copy(dim
));
4885 isl_space_free(map
->dim
);
4891 isl_space_free(dim
);
4895 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4896 __isl_take isl_space
*dim
)
4898 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4901 /* Compute the parameter domain of the given basic set.
4903 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4908 if (isl_basic_set_is_params(bset
))
4911 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4912 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4913 space
= isl_basic_set_get_space(bset
);
4914 space
= isl_space_params(space
);
4915 bset
= isl_basic_set_reset_space(bset
, space
);
4919 /* Construct a zero-dimensional basic set with the given parameter domain.
4921 __isl_give isl_basic_set
*isl_basic_set_from_params(
4922 __isl_take isl_basic_set
*bset
)
4925 space
= isl_basic_set_get_space(bset
);
4926 space
= isl_space_set_from_params(space
);
4927 bset
= isl_basic_set_reset_space(bset
, space
);
4931 /* Compute the parameter domain of the given set.
4933 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4938 if (isl_set_is_params(set
))
4941 n
= isl_set_dim(set
, isl_dim_set
);
4942 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4943 space
= isl_set_get_space(set
);
4944 space
= isl_space_params(space
);
4945 set
= isl_set_reset_space(set
, space
);
4949 /* Construct a zero-dimensional set with the given parameter domain.
4951 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4954 space
= isl_set_get_space(set
);
4955 space
= isl_space_set_from_params(space
);
4956 set
= isl_set_reset_space(set
, space
);
4960 /* Compute the parameter domain of the given map.
4962 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4967 n
= isl_map_dim(map
, isl_dim_in
);
4968 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4969 n
= isl_map_dim(map
, isl_dim_out
);
4970 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4971 space
= isl_map_get_space(map
);
4972 space
= isl_space_params(space
);
4973 map
= isl_map_reset_space(map
, space
);
4977 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4984 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4986 n_out
= isl_basic_map_n_out(bmap
);
4987 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4989 return isl_basic_map_reset_space(bmap
, space
);
4992 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4996 return isl_space_may_be_set(bmap
->dim
);
4999 /* Is this basic map actually a set?
5000 * Users should never call this function. Outside of isl,
5001 * the type should indicate whether something is a set or a map.
5003 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5007 return isl_space_is_set(bmap
->dim
);
5010 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5014 if (isl_basic_map_is_set(bmap
))
5016 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5019 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5020 __isl_take isl_basic_map
*bmap
)
5024 isl_basic_map
*domain
;
5025 int nparam
, n_in
, n_out
;
5028 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5029 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5030 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5032 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5033 domain
= isl_basic_map_universe(dim
);
5035 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5036 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5037 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5039 total
= isl_basic_map_total_dim(bmap
);
5041 for (i
= 0; i
< n_in
; ++i
) {
5042 k
= isl_basic_map_alloc_equality(bmap
);
5045 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5046 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5047 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5050 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5051 return isl_basic_map_finalize(bmap
);
5053 isl_basic_map_free(bmap
);
5057 __isl_give isl_basic_map
*isl_basic_map_range_map(
5058 __isl_take isl_basic_map
*bmap
)
5062 isl_basic_map
*range
;
5063 int nparam
, n_in
, n_out
;
5066 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5067 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5068 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5070 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5071 range
= isl_basic_map_universe(dim
);
5073 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5074 bmap
= isl_basic_map_apply_range(bmap
, range
);
5075 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5077 total
= isl_basic_map_total_dim(bmap
);
5079 for (i
= 0; i
< n_out
; ++i
) {
5080 k
= isl_basic_map_alloc_equality(bmap
);
5083 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5084 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5085 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5088 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5089 return isl_basic_map_finalize(bmap
);
5091 isl_basic_map_free(bmap
);
5095 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5099 return isl_space_may_be_set(map
->dim
);
5102 /* Is this map actually a set?
5103 * Users should never call this function. Outside of isl,
5104 * the type should indicate whether something is a set or a map.
5106 int isl_map_is_set(__isl_keep isl_map
*map
)
5110 return isl_space_is_set(map
->dim
);
5113 struct isl_set
*isl_map_range(struct isl_map
*map
)
5116 struct isl_set
*set
;
5120 if (isl_map_is_set(map
))
5121 return (isl_set
*)map
;
5123 map
= isl_map_cow(map
);
5127 set
= (struct isl_set
*) map
;
5128 set
->dim
= isl_space_range(set
->dim
);
5131 for (i
= 0; i
< map
->n
; ++i
) {
5132 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5136 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5137 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5144 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5148 map
= isl_map_cow(map
);
5152 map
->dim
= isl_space_domain_map(map
->dim
);
5155 for (i
= 0; i
< map
->n
; ++i
) {
5156 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5160 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5161 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5168 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5171 isl_space
*range_dim
;
5173 map
= isl_map_cow(map
);
5177 range_dim
= isl_space_range(isl_map_get_space(map
));
5178 range_dim
= isl_space_from_range(range_dim
);
5179 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5180 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5183 for (i
= 0; i
< map
->n
; ++i
) {
5184 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5188 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5189 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5196 /* Given a wrapped map of the form A[B -> C],
5197 * return the map A[B -> C] -> B.
5199 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5206 if (!isl_set_has_tuple_id(set
))
5207 return isl_map_domain_map(isl_set_unwrap(set
));
5209 id
= isl_set_get_tuple_id(set
);
5210 map
= isl_map_domain_map(isl_set_unwrap(set
));
5211 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5216 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5217 __isl_take isl_space
*dim
)
5220 struct isl_map
*map
= NULL
;
5222 set
= isl_set_cow(set
);
5225 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5226 map
= (struct isl_map
*)set
;
5227 for (i
= 0; i
< set
->n
; ++i
) {
5228 map
->p
[i
] = isl_basic_map_from_basic_set(
5229 set
->p
[i
], isl_space_copy(dim
));
5233 isl_space_free(map
->dim
);
5237 isl_space_free(dim
);
5242 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5243 __isl_take isl_basic_set
*bset
)
5245 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5248 __isl_give isl_basic_map
*isl_basic_map_from_range(
5249 __isl_take isl_basic_set
*bset
)
5252 space
= isl_basic_set_get_space(bset
);
5253 space
= isl_space_from_range(space
);
5254 bset
= isl_basic_set_reset_space(bset
, space
);
5255 return (isl_basic_map
*)bset
;
5258 /* Create a relation with the given set as range.
5259 * The domain of the created relation is a zero-dimensional
5260 * flat anonymous space.
5262 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5265 space
= isl_set_get_space(set
);
5266 space
= isl_space_from_range(space
);
5267 set
= isl_set_reset_space(set
, space
);
5268 return (struct isl_map
*)set
;
5271 /* Create a relation with the given set as domain.
5272 * The range of the created relation is a zero-dimensional
5273 * flat anonymous space.
5275 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5277 return isl_map_reverse(isl_map_from_range(set
));
5280 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5281 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5283 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5286 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5287 __isl_take isl_set
*range
)
5289 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5292 /* Return a newly allocated isl_map with given space and flags and
5293 * room for "n" basic maps.
5294 * Make sure that all cached information is cleared.
5296 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5299 struct isl_map
*map
;
5304 isl_die(space
->ctx
, isl_error_internal
,
5305 "negative number of basic maps", goto error
);
5306 map
= isl_calloc(space
->ctx
, struct isl_map
,
5307 sizeof(struct isl_map
) +
5308 (n
- 1) * sizeof(struct isl_basic_map
*));
5312 map
->ctx
= space
->ctx
;
5313 isl_ctx_ref(map
->ctx
);
5321 isl_space_free(space
);
5325 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5326 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5329 struct isl_map
*map
;
5332 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5336 map
= isl_map_alloc_space(dims
, n
, flags
);
5340 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5342 struct isl_basic_map
*bmap
;
5343 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5344 bmap
= isl_basic_map_set_to_empty(bmap
);
5348 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5350 struct isl_basic_set
*bset
;
5351 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5352 bset
= isl_basic_set_set_to_empty(bset
);
5356 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5358 struct isl_basic_map
*bmap
;
5359 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5360 bmap
= isl_basic_map_finalize(bmap
);
5364 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5366 struct isl_basic_set
*bset
;
5367 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5368 bset
= isl_basic_set_finalize(bset
);
5372 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5375 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5376 isl_basic_map
*bmap
;
5378 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5379 for (i
= 0; i
< total
; ++i
) {
5380 int k
= isl_basic_map_alloc_inequality(bmap
);
5383 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5384 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5388 isl_basic_map_free(bmap
);
5392 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5394 return isl_basic_map_nat_universe(dim
);
5397 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5399 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5402 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5404 return isl_map_nat_universe(dim
);
5407 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5409 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5412 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5414 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5417 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5419 struct isl_map
*map
;
5422 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5423 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5427 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5429 struct isl_set
*set
;
5432 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5433 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5437 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5440 struct isl_map
*dup
;
5444 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5445 for (i
= 0; i
< map
->n
; ++i
)
5446 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5450 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5451 __isl_take isl_basic_map
*bmap
)
5455 if (isl_basic_map_plain_is_empty(bmap
)) {
5456 isl_basic_map_free(bmap
);
5459 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5460 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5461 map
->p
[map
->n
] = bmap
;
5463 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5469 isl_basic_map_free(bmap
);
5473 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5484 isl_ctx_deref(map
->ctx
);
5485 for (i
= 0; i
< map
->n
; ++i
)
5486 isl_basic_map_free(map
->p
[i
]);
5487 isl_space_free(map
->dim
);
5493 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5494 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5498 bmap
= isl_basic_map_cow(bmap
);
5499 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5500 j
= isl_basic_map_alloc_equality(bmap
);
5503 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5504 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5505 isl_int_set_si(bmap
->eq
[j
][0], value
);
5506 bmap
= isl_basic_map_simplify(bmap
);
5507 return isl_basic_map_finalize(bmap
);
5509 isl_basic_map_free(bmap
);
5513 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5514 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5518 bmap
= isl_basic_map_cow(bmap
);
5519 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5520 j
= isl_basic_map_alloc_equality(bmap
);
5523 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5524 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5525 isl_int_set(bmap
->eq
[j
][0], value
);
5526 bmap
= isl_basic_map_simplify(bmap
);
5527 return isl_basic_map_finalize(bmap
);
5529 isl_basic_map_free(bmap
);
5533 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5534 enum isl_dim_type type
, unsigned pos
, int value
)
5538 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5539 return isl_basic_map_fix_pos_si(bmap
,
5540 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5542 isl_basic_map_free(bmap
);
5546 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5547 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5551 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5552 return isl_basic_map_fix_pos(bmap
,
5553 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5555 isl_basic_map_free(bmap
);
5559 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5560 * to be equal to "v".
5562 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5563 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5567 if (!isl_val_is_int(v
))
5568 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5569 "expecting integer value", goto error
);
5570 if (pos
>= isl_basic_map_dim(bmap
, type
))
5571 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5572 "index out of bounds", goto error
);
5573 pos
+= isl_basic_map_offset(bmap
, type
);
5574 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5578 isl_basic_map_free(bmap
);
5583 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5584 * to be equal to "v".
5586 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5587 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5589 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5592 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5593 enum isl_dim_type type
, unsigned pos
, int value
)
5595 return (struct isl_basic_set
*)
5596 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5600 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5601 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5603 return (struct isl_basic_set
*)
5604 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5608 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5609 unsigned input
, int value
)
5611 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5614 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5615 unsigned dim
, int value
)
5617 return (struct isl_basic_set
*)
5618 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5619 isl_dim_set
, dim
, value
);
5622 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5624 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5631 isl_basic_map_free(map
->p
[i
]);
5632 if (i
!= map
->n
- 1) {
5633 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5634 map
->p
[i
] = map
->p
[map
->n
- 1];
5641 /* Perform "fn" on each basic map of "map", where we may not be holding
5642 * the only reference to "map".
5643 * In particular, "fn" should be a semantics preserving operation
5644 * that we want to apply to all copies of "map". We therefore need
5645 * to be careful not to modify "map" in a way that breaks "map"
5646 * in case anything goes wrong.
5648 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5649 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5651 struct isl_basic_map
*bmap
;
5657 for (i
= map
->n
- 1; i
>= 0; --i
) {
5658 bmap
= isl_basic_map_copy(map
->p
[i
]);
5662 isl_basic_map_free(map
->p
[i
]);
5664 if (remove_if_empty(map
, i
) < 0)
5674 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5675 enum isl_dim_type type
, unsigned pos
, int value
)
5679 map
= isl_map_cow(map
);
5683 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5684 for (i
= map
->n
- 1; i
>= 0; --i
) {
5685 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5686 if (remove_if_empty(map
, i
) < 0)
5689 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5696 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5697 enum isl_dim_type type
, unsigned pos
, int value
)
5699 return (struct isl_set
*)
5700 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5703 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5704 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5708 map
= isl_map_cow(map
);
5712 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5713 for (i
= 0; i
< map
->n
; ++i
) {
5714 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5718 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5725 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5726 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5728 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5731 /* Fix the value of the variable at position "pos" of type "type" of "map"
5732 * to be equal to "v".
5734 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5735 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5739 map
= isl_map_cow(map
);
5743 if (!isl_val_is_int(v
))
5744 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5745 "expecting integer value", goto error
);
5746 if (pos
>= isl_map_dim(map
, type
))
5747 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5748 "index out of bounds", goto error
);
5749 for (i
= map
->n
- 1; i
>= 0; --i
) {
5750 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5752 if (remove_if_empty(map
, i
) < 0)
5755 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5764 /* Fix the value of the variable at position "pos" of type "type" of "set"
5765 * to be equal to "v".
5767 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5768 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5770 return isl_map_fix_val(set
, type
, pos
, v
);
5773 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5774 unsigned input
, int value
)
5776 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5779 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5781 return (struct isl_set
*)
5782 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5785 static __isl_give isl_basic_map
*basic_map_bound_si(
5786 __isl_take isl_basic_map
*bmap
,
5787 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5793 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5794 pos
+= isl_basic_map_offset(bmap
, type
);
5795 bmap
= isl_basic_map_cow(bmap
);
5796 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5797 j
= isl_basic_map_alloc_inequality(bmap
);
5800 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5802 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5803 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5805 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5806 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5808 bmap
= isl_basic_map_simplify(bmap
);
5809 return isl_basic_map_finalize(bmap
);
5811 isl_basic_map_free(bmap
);
5815 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5816 __isl_take isl_basic_map
*bmap
,
5817 enum isl_dim_type type
, unsigned pos
, int value
)
5819 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5822 /* Constrain the values of the given dimension to be no greater than "value".
5824 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5825 __isl_take isl_basic_map
*bmap
,
5826 enum isl_dim_type type
, unsigned pos
, int value
)
5828 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5831 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5832 unsigned dim
, isl_int value
)
5836 bset
= isl_basic_set_cow(bset
);
5837 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5838 j
= isl_basic_set_alloc_inequality(bset
);
5841 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5842 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5843 isl_int_neg(bset
->ineq
[j
][0], value
);
5844 bset
= isl_basic_set_simplify(bset
);
5845 return isl_basic_set_finalize(bset
);
5847 isl_basic_set_free(bset
);
5851 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5852 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5856 map
= isl_map_cow(map
);
5860 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5861 for (i
= 0; i
< map
->n
; ++i
) {
5862 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5863 type
, pos
, value
, upper
);
5867 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5874 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5875 enum isl_dim_type type
, unsigned pos
, int value
)
5877 return map_bound_si(map
, type
, pos
, value
, 0);
5880 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5881 enum isl_dim_type type
, unsigned pos
, int value
)
5883 return map_bound_si(map
, type
, pos
, value
, 1);
5886 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5887 enum isl_dim_type type
, unsigned pos
, int value
)
5889 return (struct isl_set
*)
5890 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5893 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5894 enum isl_dim_type type
, unsigned pos
, int value
)
5896 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5899 /* Bound the given variable of "bmap" from below (or above is "upper"
5900 * is set) to "value".
5902 static __isl_give isl_basic_map
*basic_map_bound(
5903 __isl_take isl_basic_map
*bmap
,
5904 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5910 if (pos
>= isl_basic_map_dim(bmap
, type
))
5911 isl_die(bmap
->ctx
, isl_error_invalid
,
5912 "index out of bounds", goto error
);
5913 pos
+= isl_basic_map_offset(bmap
, type
);
5914 bmap
= isl_basic_map_cow(bmap
);
5915 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5916 j
= isl_basic_map_alloc_inequality(bmap
);
5919 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5921 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5922 isl_int_set(bmap
->ineq
[j
][0], value
);
5924 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5925 isl_int_neg(bmap
->ineq
[j
][0], value
);
5927 bmap
= isl_basic_map_simplify(bmap
);
5928 return isl_basic_map_finalize(bmap
);
5930 isl_basic_map_free(bmap
);
5934 /* Bound the given variable of "map" from below (or above is "upper"
5935 * is set) to "value".
5937 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5938 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5942 map
= isl_map_cow(map
);
5946 if (pos
>= isl_map_dim(map
, type
))
5947 isl_die(map
->ctx
, isl_error_invalid
,
5948 "index out of bounds", goto error
);
5949 for (i
= map
->n
- 1; i
>= 0; --i
) {
5950 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5951 if (remove_if_empty(map
, i
) < 0)
5954 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5961 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5962 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5964 return map_bound(map
, type
, pos
, value
, 0);
5967 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5968 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5970 return map_bound(map
, type
, pos
, value
, 1);
5973 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5974 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5976 return isl_map_lower_bound(set
, type
, pos
, value
);
5979 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5980 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5982 return isl_map_upper_bound(set
, type
, pos
, value
);
5985 /* Force the values of the variable at position "pos" of type "type" of "set"
5986 * to be no smaller than "value".
5988 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5989 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5993 if (!isl_val_is_int(value
))
5994 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5995 "expecting integer value", goto error
);
5996 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5997 isl_val_free(value
);
6000 isl_val_free(value
);
6005 /* Force the values of the variable at position "pos" of type "type" of "set"
6006 * to be no greater than "value".
6008 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6009 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6013 if (!isl_val_is_int(value
))
6014 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6015 "expecting integer value", goto error
);
6016 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6017 isl_val_free(value
);
6020 isl_val_free(value
);
6025 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6030 set
= isl_set_cow(set
);
6034 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6035 for (i
= 0; i
< set
->n
; ++i
) {
6036 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6046 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6050 map
= isl_map_cow(map
);
6054 map
->dim
= isl_space_reverse(map
->dim
);
6057 for (i
= 0; i
< map
->n
; ++i
) {
6058 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6062 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6069 static __isl_give isl_map
*isl_basic_map_partial_lexopt(
6070 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6071 __isl_give isl_set
**empty
, int max
)
6073 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6076 __isl_give isl_map
*isl_basic_map_partial_lexmax(
6077 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6078 __isl_give isl_set
**empty
)
6080 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6083 __isl_give isl_map
*isl_basic_map_partial_lexmin(
6084 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6085 __isl_give isl_set
**empty
)
6087 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6090 __isl_give isl_set
*isl_basic_set_partial_lexmin(
6091 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6092 __isl_give isl_set
**empty
)
6094 return isl_basic_map_partial_lexmin(bset
, dom
, empty
);
6097 __isl_give isl_set
*isl_basic_set_partial_lexmax(
6098 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6099 __isl_give isl_set
**empty
)
6101 return isl_basic_map_partial_lexmax(bset
, dom
, empty
);
6104 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6105 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6106 __isl_give isl_set
**empty
)
6108 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6111 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6112 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6113 __isl_give isl_set
**empty
)
6115 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6118 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6119 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6120 __isl_give isl_set
**empty
)
6122 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6125 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6126 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6127 __isl_give isl_set
**empty
)
6129 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6132 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6133 __isl_take isl_basic_map
*bmap
, int max
)
6135 isl_basic_set
*dom
= NULL
;
6136 isl_space
*dom_space
;
6140 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6141 dom
= isl_basic_set_universe(dom_space
);
6142 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6144 isl_basic_map_free(bmap
);
6148 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6149 __isl_take isl_basic_map
*bmap
)
6151 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6155 #define TYPE isl_pw_multi_aff
6157 #define SUFFIX _pw_multi_aff
6159 #define EMPTY isl_pw_multi_aff_empty
6161 #define ADD isl_pw_multi_aff_union_add
6162 #include "isl_map_lexopt_templ.c"
6164 /* Given a map "map", compute the lexicographically minimal
6165 * (or maximal) image element for each domain element in dom,
6166 * in the form of an isl_pw_multi_aff.
6167 * If "empty" is not NULL, then set *empty to those elements in dom that
6168 * do not have an image element.
6170 * We first compute the lexicographically minimal or maximal element
6171 * in the first basic map. This results in a partial solution "res"
6172 * and a subset "todo" of dom that still need to be handled.
6173 * We then consider each of the remaining maps in "map" and successively
6174 * update both "res" and "todo".
6175 * If "empty" is NULL, then the todo sets are not needed and therefore
6176 * also not computed.
6178 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6179 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6180 __isl_give isl_set
**empty
, int max
)
6183 isl_pw_multi_aff
*res
;
6189 if (isl_map_plain_is_empty(map
)) {
6194 return isl_pw_multi_aff_from_map(map
);
6197 res
= basic_map_partial_lexopt_pw_multi_aff(
6198 isl_basic_map_copy(map
->p
[0]),
6199 isl_set_copy(dom
), empty
, max
);
6203 for (i
= 1; i
< map
->n
; ++i
) {
6204 isl_pw_multi_aff
*res_i
;
6206 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6207 isl_basic_map_copy(map
->p
[i
]),
6208 isl_set_copy(dom
), empty
, max
);
6211 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6213 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6216 todo
= isl_set_intersect(todo
, *empty
);
6235 #define TYPE isl_map
6239 #define EMPTY isl_map_empty
6241 #define ADD isl_map_union_disjoint
6242 #include "isl_map_lexopt_templ.c"
6244 /* Given a map "map", compute the lexicographically minimal
6245 * (or maximal) image element for each domain element in "dom",
6246 * in the form of an isl_map.
6247 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6248 * do not have an image element.
6250 * If the input consists of more than one disjunct, then first
6251 * compute the desired result in the form of an isl_pw_multi_aff and
6252 * then convert that into an isl_map.
6254 * This function used to have an explicit implementation in terms
6255 * of isl_maps, but it would continually intersect the domains of
6256 * partial results with the complement of the domain of the next
6257 * partial solution, potentially leading to an explosion in the number
6258 * of disjuncts if there are several disjuncts in the input.
6259 * An even earlier implementation of this function would look for
6260 * better results in the domain of the partial result and for extra
6261 * results in the complement of this domain, which would lead to
6262 * even more splintering.
6264 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6265 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6266 __isl_give isl_set
**empty
, int max
)
6268 struct isl_map
*res
;
6269 isl_pw_multi_aff
*pma
;
6274 if (isl_map_plain_is_empty(map
)) {
6283 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6289 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
, max
);
6290 return isl_map_from_pw_multi_aff(pma
);
6299 __isl_give isl_map
*isl_map_partial_lexmax(
6300 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6301 __isl_give isl_set
**empty
)
6303 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6306 __isl_give isl_map
*isl_map_partial_lexmin(
6307 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6308 __isl_give isl_set
**empty
)
6310 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6313 __isl_give isl_set
*isl_set_partial_lexmin(
6314 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6315 __isl_give isl_set
**empty
)
6317 return (struct isl_set
*)
6318 isl_map_partial_lexmin((struct isl_map
*)set
,
6322 __isl_give isl_set
*isl_set_partial_lexmax(
6323 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6324 __isl_give isl_set
**empty
)
6326 return (struct isl_set
*)
6327 isl_map_partial_lexmax((struct isl_map
*)set
,
6331 /* Compute the lexicographic minimum (or maximum if "max" is set)
6332 * of "bmap" over its domain.
6334 * Since we are not interested in the part of the domain space where
6335 * there is no solution, we initialize the domain to those constraints
6336 * of "bmap" that only involve the parameters and the input dimensions.
6337 * This relieves the parametric programming engine from detecting those
6338 * inequalities and transferring them to the context. More importantly,
6339 * it ensures that those inequalities are transferred first and not
6340 * intermixed with inequalities that actually split the domain.
6342 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6346 isl_basic_map
*copy
;
6349 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6350 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6351 copy
= isl_basic_map_copy(bmap
);
6352 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6353 isl_dim_div
, 0, n_div
);
6354 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6355 isl_dim_out
, 0, n_out
);
6356 dom
= isl_basic_map_domain(copy
);
6357 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6360 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6362 return isl_basic_map_lexopt(bmap
, 0);
6365 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6367 return isl_basic_map_lexopt(bmap
, 1);
6370 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6372 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6375 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6377 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6380 /* Extract the first and only affine expression from list
6381 * and then add it to *pwaff with the given dom.
6382 * This domain is known to be disjoint from other domains
6383 * because of the way isl_basic_map_foreach_lexmax works.
6385 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6386 __isl_take isl_aff_list
*list
, void *user
)
6388 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6390 isl_pw_aff
**pwaff
= user
;
6391 isl_pw_aff
*pwaff_i
;
6395 if (isl_aff_list_n_aff(list
) != 1)
6396 isl_die(ctx
, isl_error_internal
,
6397 "expecting single element list", goto error
);
6399 aff
= isl_aff_list_get_aff(list
, 0);
6400 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6402 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6404 isl_aff_list_free(list
);
6408 isl_basic_set_free(dom
);
6409 isl_aff_list_free(list
);
6413 /* Given a basic map with one output dimension, compute the minimum or
6414 * maximum of that dimension as an isl_pw_aff.
6416 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6417 * call update_dim_opt on each leaf of the result.
6419 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6422 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6426 dim
= isl_space_from_domain(isl_space_domain(dim
));
6427 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6428 pwaff
= isl_pw_aff_empty(dim
);
6430 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6432 return isl_pw_aff_free(pwaff
);
6437 /* Compute the minimum or maximum of the given output dimension
6438 * as a function of the parameters and the input dimensions,
6439 * but independently of the other output dimensions.
6441 * We first project out the other output dimension and then compute
6442 * the "lexicographic" maximum in each basic map, combining the results
6443 * using isl_pw_aff_union_max.
6445 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6452 n_out
= isl_map_dim(map
, isl_dim_out
);
6453 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6454 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6459 isl_space
*dim
= isl_map_get_space(map
);
6461 return isl_pw_aff_empty(dim
);
6464 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6465 for (i
= 1; i
< map
->n
; ++i
) {
6466 isl_pw_aff
*pwaff_i
;
6468 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6469 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6477 /* Compute the maximum of the given output dimension as a function of the
6478 * parameters and input dimensions, but independently of
6479 * the other output dimensions.
6481 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6483 return map_dim_opt(map
, pos
, 1);
6486 /* Compute the minimum or maximum of the given set dimension
6487 * as a function of the parameters,
6488 * but independently of the other set dimensions.
6490 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6493 return map_dim_opt(set
, pos
, max
);
6496 /* Compute the maximum of the given set dimension as a function of the
6497 * parameters, but independently of the other set dimensions.
6499 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6501 return set_dim_opt(set
, pos
, 1);
6504 /* Compute the minimum of the given set dimension as a function of the
6505 * parameters, but independently of the other set dimensions.
6507 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6509 return set_dim_opt(set
, pos
, 0);
6512 /* Apply a preimage specified by "mat" on the parameters of "bset".
6513 * bset is assumed to have only parameters and divs.
6515 static struct isl_basic_set
*basic_set_parameter_preimage(
6516 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6523 bset
->dim
= isl_space_cow(bset
->dim
);
6527 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6529 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6531 bset
->dim
->nparam
= 0;
6532 bset
->dim
->n_out
= nparam
;
6533 bset
= isl_basic_set_preimage(bset
, mat
);
6535 bset
->dim
->nparam
= bset
->dim
->n_out
;
6536 bset
->dim
->n_out
= 0;
6541 isl_basic_set_free(bset
);
6545 /* Apply a preimage specified by "mat" on the parameters of "set".
6546 * set is assumed to have only parameters and divs.
6548 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6549 __isl_take isl_mat
*mat
)
6557 nparam
= isl_set_dim(set
, isl_dim_param
);
6559 if (mat
->n_row
!= 1 + nparam
)
6560 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6561 "unexpected number of rows", goto error
);
6563 space
= isl_set_get_space(set
);
6564 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6565 isl_dim_param
, 0, nparam
);
6566 set
= isl_set_reset_space(set
, space
);
6567 set
= isl_set_preimage(set
, mat
);
6568 nparam
= isl_set_dim(set
, isl_dim_out
);
6569 space
= isl_set_get_space(set
);
6570 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6571 isl_dim_out
, 0, nparam
);
6572 set
= isl_set_reset_space(set
, space
);
6580 /* Intersect the basic set "bset" with the affine space specified by the
6581 * equalities in "eq".
6583 static struct isl_basic_set
*basic_set_append_equalities(
6584 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6592 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6597 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6598 for (i
= 0; i
< eq
->n_row
; ++i
) {
6599 k
= isl_basic_set_alloc_equality(bset
);
6602 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6603 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6607 bset
= isl_basic_set_gauss(bset
, NULL
);
6608 bset
= isl_basic_set_finalize(bset
);
6613 isl_basic_set_free(bset
);
6617 /* Intersect the set "set" with the affine space specified by the
6618 * equalities in "eq".
6620 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6628 for (i
= 0; i
< set
->n
; ++i
) {
6629 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6642 /* Given a basic set "bset" that only involves parameters and existentially
6643 * quantified variables, return the index of the first equality
6644 * that only involves parameters. If there is no such equality then
6645 * return bset->n_eq.
6647 * This function assumes that isl_basic_set_gauss has been called on "bset".
6649 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6652 unsigned nparam
, n_div
;
6657 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6658 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6660 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6661 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6668 /* Compute an explicit representation for the existentially quantified
6669 * variables in "bset" by computing the "minimal value" of the set
6670 * variables. Since there are no set variables, the computation of
6671 * the minimal value essentially computes an explicit representation
6672 * of the non-empty part(s) of "bset".
6674 * The input only involves parameters and existentially quantified variables.
6675 * All equalities among parameters have been removed.
6677 * Since the existentially quantified variables in the result are in general
6678 * going to be different from those in the input, we first replace
6679 * them by the minimal number of variables based on their equalities.
6680 * This should simplify the parametric integer programming.
6682 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6684 isl_morph
*morph1
, *morph2
;
6690 if (bset
->n_eq
== 0)
6691 return isl_basic_set_lexmin(bset
);
6693 morph1
= isl_basic_set_parameter_compression(bset
);
6694 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6695 bset
= isl_basic_set_lift(bset
);
6696 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6697 bset
= isl_morph_basic_set(morph2
, bset
);
6698 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6699 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6701 set
= isl_basic_set_lexmin(bset
);
6703 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6708 /* Project the given basic set onto its parameter domain, possibly introducing
6709 * new, explicit, existential variables in the constraints.
6710 * The input has parameters and (possibly implicit) existential variables.
6711 * The output has the same parameters, but only
6712 * explicit existentially quantified variables.
6714 * The actual projection is performed by pip, but pip doesn't seem
6715 * to like equalities very much, so we first remove the equalities
6716 * among the parameters by performing a variable compression on
6717 * the parameters. Afterward, an inverse transformation is performed
6718 * and the equalities among the parameters are inserted back in.
6720 * The variable compression on the parameters may uncover additional
6721 * equalities that were only implicit before. We therefore check
6722 * if there are any new parameter equalities in the result and
6723 * if so recurse. The removal of parameter equalities is required
6724 * for the parameter compression performed by base_compute_divs.
6726 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6730 struct isl_mat
*T
, *T2
;
6731 struct isl_set
*set
;
6734 bset
= isl_basic_set_cow(bset
);
6738 if (bset
->n_eq
== 0)
6739 return base_compute_divs(bset
);
6741 bset
= isl_basic_set_gauss(bset
, NULL
);
6744 if (isl_basic_set_plain_is_empty(bset
))
6745 return isl_set_from_basic_set(bset
);
6747 i
= first_parameter_equality(bset
);
6748 if (i
== bset
->n_eq
)
6749 return base_compute_divs(bset
);
6751 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6752 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6754 eq
= isl_mat_cow(eq
);
6755 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6756 if (T
&& T
->n_col
== 0) {
6760 bset
= isl_basic_set_set_to_empty(bset
);
6761 return isl_set_from_basic_set(bset
);
6763 bset
= basic_set_parameter_preimage(bset
, T
);
6765 i
= first_parameter_equality(bset
);
6768 else if (i
== bset
->n_eq
)
6769 set
= base_compute_divs(bset
);
6771 set
= parameter_compute_divs(bset
);
6772 set
= set_parameter_preimage(set
, T2
);
6773 set
= set_append_equalities(set
, eq
);
6777 /* Insert the divs from "ls" before those of "bmap".
6779 * The number of columns is not changed, which means that the last
6780 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6781 * The caller is responsible for removing the same number of dimensions
6782 * from the space of "bmap".
6784 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6785 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6791 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6795 old_n_div
= bmap
->n_div
;
6796 bmap
= insert_div_rows(bmap
, n_div
);
6800 for (i
= 0; i
< n_div
; ++i
) {
6801 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6802 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6808 /* Replace the space of "bmap" by the space and divs of "ls".
6810 * If "ls" has any divs, then we simplify the result since we may
6811 * have discovered some additional equalities that could simplify
6812 * the div expressions.
6814 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6815 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6819 bmap
= isl_basic_map_cow(bmap
);
6823 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6824 bmap
= insert_divs_from_local_space(bmap
, ls
);
6828 isl_space_free(bmap
->dim
);
6829 bmap
->dim
= isl_local_space_get_space(ls
);
6833 isl_local_space_free(ls
);
6835 bmap
= isl_basic_map_simplify(bmap
);
6836 bmap
= isl_basic_map_finalize(bmap
);
6839 isl_basic_map_free(bmap
);
6840 isl_local_space_free(ls
);
6844 /* Replace the space of "map" by the space and divs of "ls".
6846 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6847 __isl_take isl_local_space
*ls
)
6851 map
= isl_map_cow(map
);
6855 for (i
= 0; i
< map
->n
; ++i
) {
6856 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6857 isl_local_space_copy(ls
));
6861 isl_space_free(map
->dim
);
6862 map
->dim
= isl_local_space_get_space(ls
);
6866 isl_local_space_free(ls
);
6869 isl_local_space_free(ls
);
6874 /* Compute an explicit representation for the existentially
6875 * quantified variables for which do not know any explicit representation yet.
6877 * We first sort the existentially quantified variables so that the
6878 * existentially quantified variables for which we already have an explicit
6879 * representation are placed before those for which we do not.
6880 * The input dimensions, the output dimensions and the existentially
6881 * quantified variables for which we already have an explicit
6882 * representation are then turned into parameters.
6883 * compute_divs returns a map with the same parameters and
6884 * no input or output dimensions and the dimension specification
6885 * is reset to that of the input, including the existentially quantified
6886 * variables for which we already had an explicit representation.
6888 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6890 struct isl_basic_set
*bset
;
6891 struct isl_set
*set
;
6892 struct isl_map
*map
;
6894 isl_local_space
*ls
;
6901 bmap
= isl_basic_map_sort_divs(bmap
);
6902 bmap
= isl_basic_map_cow(bmap
);
6906 n_known
= isl_basic_map_first_unknown_div(bmap
);
6908 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6910 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6911 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6912 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6913 dim
= isl_space_set_alloc(bmap
->ctx
,
6914 nparam
+ n_in
+ n_out
+ n_known
, 0);
6918 ls
= isl_basic_map_get_local_space(bmap
);
6919 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6920 n_known
, bmap
->n_div
- n_known
);
6922 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6923 swap_div(bmap
, i
- n_known
, i
);
6924 bmap
->n_div
-= n_known
;
6925 bmap
->extra
-= n_known
;
6927 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6928 bset
= (struct isl_basic_set
*)bmap
;
6930 set
= parameter_compute_divs(bset
);
6931 map
= (struct isl_map
*)set
;
6932 map
= replace_space_by_local_space(map
, ls
);
6936 isl_basic_map_free(bmap
);
6940 /* Remove the explicit representation of local variable "div",
6943 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6944 __isl_take isl_basic_map
*bmap
, int div
)
6948 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6950 return isl_basic_map_free(bmap
);
6954 bmap
= isl_basic_map_cow(bmap
);
6957 isl_int_set_si(bmap
->div
[div
][0], 0);
6961 /* Is local variable "div" of "bmap" marked as not having an explicit
6963 * Note that even if "div" is not marked in this way and therefore
6964 * has an explicit representation, this representation may still
6965 * depend (indirectly) on other local variables that do not
6966 * have an explicit representation.
6968 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6972 return isl_bool_error
;
6973 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6974 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6975 "position out of bounds", return isl_bool_error
);
6976 return isl_int_is_zero(bmap
->div
[div
][0]);
6979 /* Return the position of the first local variable that does not
6980 * have an explicit representation.
6981 * Return the total number of local variables if they all have
6982 * an explicit representation.
6983 * Return -1 on error.
6985 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6992 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6993 if (!isl_basic_map_div_is_known(bmap
, i
))
6999 /* Does "bmap" have an explicit representation for all local variables?
7001 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7005 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7006 first
= isl_basic_map_first_unknown_div(bmap
);
7008 return isl_bool_error
;
7012 /* Do all basic maps in "map" have an explicit representation
7013 * for all local variables?
7015 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7020 return isl_bool_error
;
7022 for (i
= 0; i
< map
->n
; ++i
) {
7023 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7028 return isl_bool_true
;
7031 /* If bmap contains any unknown divs, then compute explicit
7032 * expressions for them. However, this computation may be
7033 * quite expensive, so first try to remove divs that aren't
7036 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7039 struct isl_map
*map
;
7041 known
= isl_basic_map_divs_known(bmap
);
7045 return isl_map_from_basic_map(bmap
);
7047 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7049 known
= isl_basic_map_divs_known(bmap
);
7053 return isl_map_from_basic_map(bmap
);
7055 map
= compute_divs(bmap
);
7058 isl_basic_map_free(bmap
);
7062 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7066 struct isl_map
*res
;
7073 known
= isl_map_divs_known(map
);
7081 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7082 for (i
= 1 ; i
< map
->n
; ++i
) {
7084 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7085 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7086 res
= isl_map_union_disjoint(res
, r2
);
7088 res
= isl_map_union(res
, r2
);
7095 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7097 return (struct isl_set
*)
7098 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7101 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7103 return (struct isl_set
*)
7104 isl_map_compute_divs((struct isl_map
*)set
);
7107 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7110 struct isl_set
*set
;
7115 map
= isl_map_cow(map
);
7119 set
= (struct isl_set
*)map
;
7120 set
->dim
= isl_space_domain(set
->dim
);
7123 for (i
= 0; i
< map
->n
; ++i
) {
7124 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7128 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7129 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7136 /* Return the union of "map1" and "map2", where we assume for now that
7137 * "map1" and "map2" are disjoint. Note that the basic maps inside
7138 * "map1" or "map2" may not be disjoint from each other.
7139 * Also note that this function is also called from isl_map_union,
7140 * which takes care of handling the situation where "map1" and "map2"
7141 * may not be disjoint.
7143 * If one of the inputs is empty, we can simply return the other input.
7144 * Similarly, if one of the inputs is universal, then it is equal to the union.
7146 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7147 __isl_take isl_map
*map2
)
7151 struct isl_map
*map
= NULL
;
7157 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7158 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7159 "spaces don't match", goto error
);
7170 is_universe
= isl_map_plain_is_universe(map1
);
7171 if (is_universe
< 0)
7178 is_universe
= isl_map_plain_is_universe(map2
);
7179 if (is_universe
< 0)
7186 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7187 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7188 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7190 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7191 map1
->n
+ map2
->n
, flags
);
7194 for (i
= 0; i
< map1
->n
; ++i
) {
7195 map
= isl_map_add_basic_map(map
,
7196 isl_basic_map_copy(map1
->p
[i
]));
7200 for (i
= 0; i
< map2
->n
; ++i
) {
7201 map
= isl_map_add_basic_map(map
,
7202 isl_basic_map_copy(map2
->p
[i
]));
7216 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7217 * guaranteed to be disjoint by the caller.
7219 * Note that this functions is called from within isl_map_make_disjoint,
7220 * so we have to be careful not to touch the constraints of the inputs
7223 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7224 __isl_take isl_map
*map2
)
7226 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7229 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7230 * not be disjoint. The parameters are assumed to have been aligned.
7232 * We currently simply call map_union_disjoint, the internal operation
7233 * of which does not really depend on the inputs being disjoint.
7234 * If the result contains more than one basic map, then we clear
7235 * the disjoint flag since the result may contain basic maps from
7236 * both inputs and these are not guaranteed to be disjoint.
7238 * As a special case, if "map1" and "map2" are obviously equal,
7239 * then we simply return "map1".
7241 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7242 __isl_take isl_map
*map2
)
7249 equal
= isl_map_plain_is_equal(map1
, map2
);
7257 map1
= map_union_disjoint(map1
, map2
);
7261 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7269 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7272 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7273 __isl_take isl_map
*map2
)
7275 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7278 struct isl_set
*isl_set_union_disjoint(
7279 struct isl_set
*set1
, struct isl_set
*set2
)
7281 return (struct isl_set
*)
7282 isl_map_union_disjoint(
7283 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7286 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7288 return (struct isl_set
*)
7289 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7292 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7295 * "map" and "set" are assumed to be compatible and non-NULL.
7297 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7298 __isl_take isl_set
*set
,
7299 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7300 __isl_take isl_basic_set
*bset
))
7303 struct isl_map
*result
;
7306 if (isl_set_plain_is_universe(set
)) {
7311 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7312 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7313 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7315 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7316 map
->n
* set
->n
, flags
);
7317 for (i
= 0; result
&& i
< map
->n
; ++i
)
7318 for (j
= 0; j
< set
->n
; ++j
) {
7319 result
= isl_map_add_basic_map(result
,
7320 fn(isl_basic_map_copy(map
->p
[i
]),
7321 isl_basic_set_copy(set
->p
[j
])));
7331 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7332 __isl_take isl_set
*set
)
7337 if (!isl_map_compatible_range(map
, set
))
7338 isl_die(set
->ctx
, isl_error_invalid
,
7339 "incompatible spaces", goto error
);
7341 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7348 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7349 __isl_take isl_set
*set
)
7351 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7354 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7355 __isl_take isl_set
*set
)
7360 if (!isl_map_compatible_domain(map
, set
))
7361 isl_die(set
->ctx
, isl_error_invalid
,
7362 "incompatible spaces", goto error
);
7364 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7371 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7372 __isl_take isl_set
*set
)
7374 return isl_map_align_params_map_map_and(map
, set
,
7375 &map_intersect_domain
);
7378 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7379 __isl_take isl_map
*map2
)
7383 map1
= isl_map_reverse(map1
);
7384 map1
= isl_map_apply_range(map1
, map2
);
7385 return isl_map_reverse(map1
);
7392 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7393 __isl_take isl_map
*map2
)
7395 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7398 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7399 __isl_take isl_map
*map2
)
7401 isl_space
*dim_result
;
7402 struct isl_map
*result
;
7408 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7409 isl_space_copy(map2
->dim
));
7411 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7414 for (i
= 0; i
< map1
->n
; ++i
)
7415 for (j
= 0; j
< map2
->n
; ++j
) {
7416 result
= isl_map_add_basic_map(result
,
7417 isl_basic_map_apply_range(
7418 isl_basic_map_copy(map1
->p
[i
]),
7419 isl_basic_map_copy(map2
->p
[j
])));
7425 if (result
&& result
->n
<= 1)
7426 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7434 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7435 __isl_take isl_map
*map2
)
7437 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7441 * returns range - domain
7443 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7445 isl_space
*target_space
;
7446 struct isl_basic_set
*bset
;
7453 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7454 bmap
->dim
, isl_dim_out
),
7456 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7457 dim
= isl_basic_map_n_in(bmap
);
7458 nparam
= isl_basic_map_n_param(bmap
);
7459 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7460 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7461 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7462 for (i
= 0; i
< dim
; ++i
) {
7463 int j
= isl_basic_map_alloc_equality(bmap
);
7465 bmap
= isl_basic_map_free(bmap
);
7468 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7469 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7470 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7471 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7473 bset
= isl_basic_map_domain(bmap
);
7474 bset
= isl_basic_set_reset_space(bset
, target_space
);
7477 isl_basic_map_free(bmap
);
7482 * returns range - domain
7484 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7488 struct isl_set
*result
;
7493 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7494 map
->dim
, isl_dim_out
),
7496 dim
= isl_map_get_space(map
);
7497 dim
= isl_space_domain(dim
);
7498 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7501 for (i
= 0; i
< map
->n
; ++i
)
7502 result
= isl_set_add_basic_set(result
,
7503 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7512 * returns [domain -> range] -> range - domain
7514 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7515 __isl_take isl_basic_map
*bmap
)
7519 isl_basic_map
*domain
;
7523 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7524 bmap
->dim
, isl_dim_out
))
7525 isl_die(bmap
->ctx
, isl_error_invalid
,
7526 "domain and range don't match", goto error
);
7528 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7529 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7531 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7532 domain
= isl_basic_map_universe(dim
);
7534 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7535 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7536 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7538 total
= isl_basic_map_total_dim(bmap
);
7540 for (i
= 0; i
< n
; ++i
) {
7541 k
= isl_basic_map_alloc_equality(bmap
);
7544 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7545 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7546 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7547 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7550 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7551 return isl_basic_map_finalize(bmap
);
7553 isl_basic_map_free(bmap
);
7558 * returns [domain -> range] -> range - domain
7560 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7563 isl_space
*domain_dim
;
7568 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7569 map
->dim
, isl_dim_out
))
7570 isl_die(map
->ctx
, isl_error_invalid
,
7571 "domain and range don't match", goto error
);
7573 map
= isl_map_cow(map
);
7577 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7578 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7579 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7582 for (i
= 0; i
< map
->n
; ++i
) {
7583 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7587 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7594 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7596 struct isl_basic_map
*bmap
;
7604 nparam
= dims
->nparam
;
7606 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7610 for (i
= 0; i
< dim
; ++i
) {
7611 int j
= isl_basic_map_alloc_equality(bmap
);
7614 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7615 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7616 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7618 return isl_basic_map_finalize(bmap
);
7620 isl_basic_map_free(bmap
);
7624 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7628 if (dim
->n_in
!= dim
->n_out
)
7629 isl_die(dim
->ctx
, isl_error_invalid
,
7630 "number of input and output dimensions needs to be "
7631 "the same", goto error
);
7632 return basic_map_identity(dim
);
7634 isl_space_free(dim
);
7638 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7640 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7643 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7645 isl_space
*dim
= isl_set_get_space(set
);
7647 id
= isl_map_identity(isl_space_map_from_set(dim
));
7648 return isl_map_intersect_range(id
, set
);
7651 /* Construct a basic set with all set dimensions having only non-negative
7654 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7655 __isl_take isl_space
*space
)
7660 struct isl_basic_set
*bset
;
7664 nparam
= space
->nparam
;
7666 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7669 for (i
= 0; i
< dim
; ++i
) {
7670 int k
= isl_basic_set_alloc_inequality(bset
);
7673 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7674 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7678 isl_basic_set_free(bset
);
7682 /* Construct the half-space x_pos >= 0.
7684 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7688 isl_basic_set
*nonneg
;
7690 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7691 k
= isl_basic_set_alloc_inequality(nonneg
);
7694 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7695 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7697 return isl_basic_set_finalize(nonneg
);
7699 isl_basic_set_free(nonneg
);
7703 /* Construct the half-space x_pos <= -1.
7705 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7710 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7711 k
= isl_basic_set_alloc_inequality(neg
);
7714 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7715 isl_int_set_si(neg
->ineq
[k
][0], -1);
7716 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7718 return isl_basic_set_finalize(neg
);
7720 isl_basic_set_free(neg
);
7724 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7725 enum isl_dim_type type
, unsigned first
, unsigned n
)
7729 isl_basic_set
*nonneg
;
7737 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7739 offset
= pos(set
->dim
, type
);
7740 for (i
= 0; i
< n
; ++i
) {
7741 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7742 offset
+ first
+ i
);
7743 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7745 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7754 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7755 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7762 if (isl_set_plain_is_empty(set
)) {
7767 return fn(set
, signs
, user
);
7770 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7772 half
= isl_set_intersect(half
, isl_set_copy(set
));
7773 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7777 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7779 half
= isl_set_intersect(half
, set
);
7780 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7786 /* Call "fn" on the intersections of "set" with each of the orthants
7787 * (except for obviously empty intersections). The orthant is identified
7788 * by the signs array, with each entry having value 1 or -1 according
7789 * to the sign of the corresponding variable.
7791 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7792 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7802 if (isl_set_plain_is_empty(set
))
7805 nparam
= isl_set_dim(set
, isl_dim_param
);
7806 nvar
= isl_set_dim(set
, isl_dim_set
);
7808 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7810 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7818 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7820 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7823 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7824 __isl_keep isl_basic_map
*bmap2
)
7827 struct isl_map
*map1
;
7828 struct isl_map
*map2
;
7830 if (!bmap1
|| !bmap2
)
7831 return isl_bool_error
;
7833 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7834 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7836 is_subset
= isl_map_is_subset(map1
, map2
);
7844 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7845 __isl_keep isl_basic_set
*bset2
)
7847 return isl_basic_map_is_subset(bset1
, bset2
);
7850 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7851 __isl_keep isl_basic_map
*bmap2
)
7855 if (!bmap1
|| !bmap2
)
7856 return isl_bool_error
;
7857 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7858 if (is_subset
!= isl_bool_true
)
7860 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7864 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7865 __isl_keep isl_basic_set
*bset2
)
7867 return isl_basic_map_is_equal(
7868 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7871 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7877 return isl_bool_error
;
7878 for (i
= 0; i
< map
->n
; ++i
) {
7879 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7881 return isl_bool_error
;
7883 return isl_bool_false
;
7885 return isl_bool_true
;
7888 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7890 return map
? map
->n
== 0 : isl_bool_error
;
7893 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7895 return set
? set
->n
== 0 : isl_bool_error
;
7898 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7900 return isl_map_is_empty((struct isl_map
*)set
);
7903 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7908 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7911 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7916 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7919 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7924 return isl_bool_error
;
7925 is_subset
= isl_map_is_subset(map1
, map2
);
7926 if (is_subset
!= isl_bool_true
)
7928 is_subset
= isl_map_is_subset(map2
, map1
);
7932 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7934 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7937 isl_bool
isl_basic_map_is_strict_subset(
7938 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7942 if (!bmap1
|| !bmap2
)
7943 return isl_bool_error
;
7944 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7945 if (is_subset
!= isl_bool_true
)
7947 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7948 if (is_subset
== isl_bool_error
)
7953 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7954 __isl_keep isl_map
*map2
)
7959 return isl_bool_error
;
7960 is_subset
= isl_map_is_subset(map1
, map2
);
7961 if (is_subset
!= isl_bool_true
)
7963 is_subset
= isl_map_is_subset(map2
, map1
);
7964 if (is_subset
== isl_bool_error
)
7969 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7970 __isl_keep isl_set
*set2
)
7972 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7975 /* Is "bmap" obviously equal to the universe with the same space?
7977 * That is, does it not have any constraints?
7979 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7982 return isl_bool_error
;
7983 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7986 /* Is "bset" obviously equal to the universe with the same space?
7988 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7990 return isl_basic_map_plain_is_universe(bset
);
7993 /* If "c" does not involve any existentially quantified variables,
7994 * then set *univ to false and abort
7996 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7998 isl_bool
*univ
= user
;
8001 n
= isl_constraint_dim(c
, isl_dim_div
);
8002 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8003 isl_constraint_free(c
);
8004 if (*univ
< 0 || !*univ
)
8005 return isl_stat_error
;
8009 /* Is "bmap" equal to the universe with the same space?
8011 * First check if it is obviously equal to the universe.
8012 * If not and if there are any constraints not involving
8013 * existentially quantified variables, then it is certainly
8014 * not equal to the universe.
8015 * Otherwise, check if the universe is a subset of "bmap".
8017 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8020 isl_basic_map
*test
;
8022 univ
= isl_basic_map_plain_is_universe(bmap
);
8023 if (univ
< 0 || univ
)
8025 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8026 return isl_bool_false
;
8027 univ
= isl_bool_true
;
8028 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8030 return isl_bool_error
;
8031 if (univ
< 0 || !univ
)
8033 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8034 univ
= isl_basic_map_is_subset(test
, bmap
);
8035 isl_basic_map_free(test
);
8039 /* Is "bset" equal to the universe with the same space?
8041 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8043 return isl_basic_map_is_universe(bset
);
8046 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8051 return isl_bool_error
;
8053 for (i
= 0; i
< map
->n
; ++i
) {
8054 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8059 return isl_bool_false
;
8062 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8064 return isl_map_plain_is_universe((isl_map
*) set
);
8067 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8069 struct isl_basic_set
*bset
= NULL
;
8070 struct isl_vec
*sample
= NULL
;
8071 isl_bool empty
, non_empty
;
8074 return isl_bool_error
;
8076 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8077 return isl_bool_true
;
8079 if (isl_basic_map_plain_is_universe(bmap
))
8080 return isl_bool_false
;
8082 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8083 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8084 copy
= isl_basic_map_remove_redundancies(copy
);
8085 empty
= isl_basic_map_plain_is_empty(copy
);
8086 isl_basic_map_free(copy
);
8090 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8092 return isl_bool_error
;
8094 return isl_bool_false
;
8095 isl_vec_free(bmap
->sample
);
8096 bmap
->sample
= NULL
;
8097 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8099 return isl_bool_error
;
8100 sample
= isl_basic_set_sample_vec(bset
);
8102 return isl_bool_error
;
8103 empty
= sample
->size
== 0;
8104 isl_vec_free(bmap
->sample
);
8105 bmap
->sample
= sample
;
8107 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8112 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8115 return isl_bool_error
;
8116 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8119 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8122 return isl_bool_error
;
8123 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8126 /* Is "bmap" known to be non-empty?
8128 * That is, is the cached sample still valid?
8130 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8135 return isl_bool_error
;
8137 return isl_bool_false
;
8138 total
= 1 + isl_basic_map_total_dim(bmap
);
8139 if (bmap
->sample
->size
!= total
)
8140 return isl_bool_false
;
8141 return isl_basic_map_contains(bmap
, bmap
->sample
);
8144 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8146 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8149 struct isl_map
*isl_basic_map_union(
8150 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8152 struct isl_map
*map
;
8153 if (!bmap1
|| !bmap2
)
8156 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8158 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8161 map
= isl_map_add_basic_map(map
, bmap1
);
8162 map
= isl_map_add_basic_map(map
, bmap2
);
8165 isl_basic_map_free(bmap1
);
8166 isl_basic_map_free(bmap2
);
8170 struct isl_set
*isl_basic_set_union(
8171 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8173 return (struct isl_set
*)isl_basic_map_union(
8174 (struct isl_basic_map
*)bset1
,
8175 (struct isl_basic_map
*)bset2
);
8178 /* Order divs such that any div only depends on previous divs */
8179 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8187 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8189 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8191 if (isl_int_is_zero(bmap
->div
[i
][0]))
8193 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8198 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8199 "integer division depends on itself",
8200 return isl_basic_map_free(bmap
));
8201 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8207 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8209 return (struct isl_basic_set
*)
8210 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8213 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8220 for (i
= 0; i
< map
->n
; ++i
) {
8221 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8232 /* Apply the expansion computed by isl_merge_divs.
8233 * The expansion itself is given by "exp" while the resulting
8234 * list of divs is given by "div".
8236 * Move the integer divisions of "bmap" into the right position
8237 * according to "exp" and then introduce the additional integer
8238 * divisions, adding div constraints.
8239 * The moving should be done first to avoid moving coefficients
8240 * in the definitions of the extra integer divisions.
8242 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8243 __isl_take isl_basic_set
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8248 bmap
= isl_basic_map_cow(bmap
);
8252 if (div
->n_row
< bmap
->n_div
)
8253 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8254 "not an expansion", goto error
);
8256 n_div
= bmap
->n_div
;
8257 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8258 div
->n_row
- n_div
, 0,
8259 2 * (div
->n_row
- n_div
));
8261 for (i
= n_div
; i
< div
->n_row
; ++i
)
8262 if (isl_basic_map_alloc_div(bmap
) < 0)
8265 for (j
= n_div
- 1; j
>= 0; --j
) {
8268 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8271 for (i
= 0; i
< div
->n_row
; ++i
) {
8272 if (j
< n_div
&& exp
[j
] == i
) {
8275 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8276 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8278 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8286 isl_basic_map_free(bmap
);
8291 /* Apply the expansion computed by isl_merge_divs.
8292 * The expansion itself is given by "exp" while the resulting
8293 * list of divs is given by "div".
8295 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8296 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8298 return isl_basic_map_expand_divs(bset
, div
, exp
);
8301 /* Look for a div in dst that corresponds to the div "div" in src.
8302 * The divs before "div" in src and dst are assumed to be the same.
8304 * Returns -1 if no corresponding div was found and the position
8305 * of the corresponding div in dst otherwise.
8307 static int find_div(struct isl_basic_map
*dst
,
8308 struct isl_basic_map
*src
, unsigned div
)
8312 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8314 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8315 for (i
= div
; i
< dst
->n_div
; ++i
)
8316 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8317 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8318 dst
->n_div
- div
) == -1)
8323 /* Align the divs of "dst" to those of "src", adding divs from "src"
8324 * if needed. That is, make sure that the first src->n_div divs
8325 * of the result are equal to those of src.
8327 * The result is not finalized as by design it will have redundant
8328 * divs if any divs from "src" were copied.
8330 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8331 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8334 int known
, extended
;
8338 return isl_basic_map_free(dst
);
8340 if (src
->n_div
== 0)
8343 known
= isl_basic_map_divs_known(src
);
8345 return isl_basic_map_free(dst
);
8347 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8348 "some src divs are unknown",
8349 return isl_basic_map_free(dst
));
8351 src
= isl_basic_map_order_divs(src
);
8354 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8355 for (i
= 0; i
< src
->n_div
; ++i
) {
8356 int j
= find_div(dst
, src
, i
);
8359 int extra
= src
->n_div
- i
;
8360 dst
= isl_basic_map_cow(dst
);
8363 dst
= isl_basic_map_extend_space(dst
,
8364 isl_space_copy(dst
->dim
),
8365 extra
, 0, 2 * extra
);
8368 j
= isl_basic_map_alloc_div(dst
);
8370 return isl_basic_map_free(dst
);
8371 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8372 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8373 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8374 return isl_basic_map_free(dst
);
8377 isl_basic_map_swap_div(dst
, i
, j
);
8382 struct isl_basic_set
*isl_basic_set_align_divs(
8383 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8385 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8386 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8389 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8397 map
= isl_map_compute_divs(map
);
8398 map
= isl_map_cow(map
);
8402 for (i
= 1; i
< map
->n
; ++i
)
8403 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8404 for (i
= 1; i
< map
->n
; ++i
) {
8405 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8407 return isl_map_free(map
);
8410 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8414 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8416 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8419 /* Align the divs of the basic maps in "map" to those
8420 * of the basic maps in "list", as well as to the other basic maps in "map".
8421 * The elements in "list" are assumed to have known divs.
8423 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8424 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8428 map
= isl_map_compute_divs(map
);
8429 map
= isl_map_cow(map
);
8431 return isl_map_free(map
);
8435 n
= isl_basic_map_list_n_basic_map(list
);
8436 for (i
= 0; i
< n
; ++i
) {
8437 isl_basic_map
*bmap
;
8439 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8440 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8441 isl_basic_map_free(bmap
);
8444 return isl_map_free(map
);
8446 return isl_map_align_divs(map
);
8449 /* Align the divs of each element of "list" to those of "bmap".
8450 * Both "bmap" and the elements of "list" are assumed to have known divs.
8452 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8453 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8458 return isl_basic_map_list_free(list
);
8460 n
= isl_basic_map_list_n_basic_map(list
);
8461 for (i
= 0; i
< n
; ++i
) {
8462 isl_basic_map
*bmap_i
;
8464 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8465 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8466 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8472 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8473 __isl_take isl_map
*map
)
8477 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8478 map
= isl_map_intersect_domain(map
, set
);
8479 set
= isl_map_range(map
);
8487 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8488 __isl_take isl_map
*map
)
8490 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8493 /* There is no need to cow as removing empty parts doesn't change
8494 * the meaning of the set.
8496 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8503 for (i
= map
->n
- 1; i
>= 0; --i
)
8504 remove_if_empty(map
, i
);
8509 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8511 return (struct isl_set
*)
8512 isl_map_remove_empty_parts((struct isl_map
*)set
);
8515 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8517 struct isl_basic_map
*bmap
;
8518 if (!map
|| map
->n
== 0)
8520 bmap
= map
->p
[map
->n
-1];
8521 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8522 return isl_basic_map_copy(bmap
);
8525 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8527 return (struct isl_basic_set
*)
8528 isl_map_copy_basic_map((struct isl_map
*)set
);
8531 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8532 __isl_keep isl_basic_map
*bmap
)
8538 for (i
= map
->n
-1; i
>= 0; --i
) {
8539 if (map
->p
[i
] != bmap
)
8541 map
= isl_map_cow(map
);
8544 isl_basic_map_free(map
->p
[i
]);
8545 if (i
!= map
->n
-1) {
8546 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8547 map
->p
[i
] = map
->p
[map
->n
-1];
8558 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8559 struct isl_basic_set
*bset
)
8561 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8562 (struct isl_basic_map
*)bset
);
8565 /* Given two basic sets bset1 and bset2, compute the maximal difference
8566 * between the values of dimension pos in bset1 and those in bset2
8567 * for any common value of the parameters and dimensions preceding pos.
8569 static enum isl_lp_result
basic_set_maximal_difference_at(
8570 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8571 int pos
, isl_int
*opt
)
8574 struct isl_basic_map
*bmap1
= NULL
;
8575 struct isl_basic_map
*bmap2
= NULL
;
8576 struct isl_ctx
*ctx
;
8577 struct isl_vec
*obj
;
8580 unsigned dim1
, dim2
;
8581 enum isl_lp_result res
;
8583 if (!bset1
|| !bset2
)
8584 return isl_lp_error
;
8586 nparam
= isl_basic_set_n_param(bset1
);
8587 dim1
= isl_basic_set_n_dim(bset1
);
8588 dim2
= isl_basic_set_n_dim(bset2
);
8589 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8590 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8591 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8592 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8593 if (!bmap1
|| !bmap2
)
8595 bmap1
= isl_basic_map_cow(bmap1
);
8596 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8597 pos
, (dim1
- pos
) + (dim2
- pos
),
8598 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8599 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8602 total
= isl_basic_map_total_dim(bmap1
);
8604 obj
= isl_vec_alloc(ctx
, 1 + total
);
8607 isl_seq_clr(obj
->block
.data
, 1 + total
);
8608 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8609 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8610 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8612 isl_basic_map_free(bmap1
);
8616 isl_basic_map_free(bmap2
);
8618 isl_basic_map_free(bmap1
);
8619 return isl_lp_error
;
8622 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8623 * for any common value of the parameters and dimensions preceding pos
8624 * in both basic sets, the values of dimension pos in bset1 are
8625 * smaller or larger than those in bset2.
8628 * 1 if bset1 follows bset2
8629 * -1 if bset1 precedes bset2
8630 * 0 if bset1 and bset2 are incomparable
8631 * -2 if some error occurred.
8633 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8634 struct isl_basic_set
*bset2
, int pos
)
8637 enum isl_lp_result res
;
8642 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8644 if (res
== isl_lp_empty
)
8646 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8647 res
== isl_lp_unbounded
)
8649 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8658 /* Given two basic sets bset1 and bset2, check whether
8659 * for any common value of the parameters and dimensions preceding pos
8660 * there is a value of dimension pos in bset1 that is larger
8661 * than a value of the same dimension in bset2.
8664 * 1 if there exists such a pair
8665 * 0 if there is no such pair, but there is a pair of equal values
8667 * -2 if some error occurred.
8669 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8670 __isl_keep isl_basic_set
*bset2
, int pos
)
8673 enum isl_lp_result res
;
8678 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8680 if (res
== isl_lp_empty
)
8682 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8683 res
== isl_lp_unbounded
)
8685 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8687 else if (res
== isl_lp_ok
)
8696 /* Given two sets set1 and set2, check whether
8697 * for any common value of the parameters and dimensions preceding pos
8698 * there is a value of dimension pos in set1 that is larger
8699 * than a value of the same dimension in set2.
8702 * 1 if there exists such a pair
8703 * 0 if there is no such pair, but there is a pair of equal values
8705 * -2 if some error occurred.
8707 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8708 __isl_keep isl_set
*set2
, int pos
)
8716 for (i
= 0; i
< set1
->n
; ++i
)
8717 for (j
= 0; j
< set2
->n
; ++j
) {
8719 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8720 if (f
== 1 || f
== -2)
8729 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8730 unsigned pos
, isl_int
*val
)
8738 total
= isl_basic_map_total_dim(bmap
);
8739 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8740 for (; d
+1 > pos
; --d
)
8741 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8745 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8747 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8749 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8752 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8758 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8759 unsigned pos
, isl_int
*val
)
8771 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8774 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8775 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8776 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8777 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8781 isl_int_set(*val
, v
);
8787 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8788 unsigned pos
, isl_int
*val
)
8790 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8794 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8797 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8800 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8801 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8803 if (pos
>= isl_basic_map_dim(bmap
, type
))
8805 return isl_basic_map_plain_has_fixed_var(bmap
,
8806 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8809 /* If "bmap" obviously lies on a hyperplane where the given dimension
8810 * has a fixed value, then return that value.
8811 * Otherwise return NaN.
8813 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8814 __isl_keep isl_basic_map
*bmap
,
8815 enum isl_dim_type type
, unsigned pos
)
8823 ctx
= isl_basic_map_get_ctx(bmap
);
8824 v
= isl_val_alloc(ctx
);
8827 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8829 return isl_val_free(v
);
8831 isl_int_set_si(v
->d
, 1);
8835 return isl_val_nan(ctx
);
8838 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8839 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8841 if (pos
>= isl_map_dim(map
, type
))
8843 return isl_map_plain_has_fixed_var(map
,
8844 map_offset(map
, type
) - 1 + pos
, val
);
8847 /* If "map" obviously lies on a hyperplane where the given dimension
8848 * has a fixed value, then return that value.
8849 * Otherwise return NaN.
8851 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8852 enum isl_dim_type type
, unsigned pos
)
8860 ctx
= isl_map_get_ctx(map
);
8861 v
= isl_val_alloc(ctx
);
8864 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8866 return isl_val_free(v
);
8868 isl_int_set_si(v
->d
, 1);
8872 return isl_val_nan(ctx
);
8875 /* If "set" obviously lies on a hyperplane where the given dimension
8876 * has a fixed value, then return that value.
8877 * Otherwise return NaN.
8879 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8880 enum isl_dim_type type
, unsigned pos
)
8882 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8885 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8886 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8888 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8891 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8892 * then return this fixed value in *val.
8894 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8895 unsigned dim
, isl_int
*val
)
8897 return isl_basic_set_plain_has_fixed_var(bset
,
8898 isl_basic_set_n_param(bset
) + dim
, val
);
8901 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8902 * then return this fixed value in *val.
8904 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8905 unsigned dim
, isl_int
*val
)
8907 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8910 /* Check if input variable in has fixed value and if so and if val is not NULL,
8911 * then return this fixed value in *val.
8913 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8914 unsigned in
, isl_int
*val
)
8916 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8919 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8920 * and if val is not NULL, then return this lower bound in *val.
8922 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8923 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8925 int i
, i_eq
= -1, i_ineq
= -1;
8932 total
= isl_basic_set_total_dim(bset
);
8933 nparam
= isl_basic_set_n_param(bset
);
8934 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8935 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8941 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8942 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8944 if (i_eq
!= -1 || i_ineq
!= -1)
8948 if (i_eq
== -1 && i_ineq
== -1)
8950 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8951 /* The coefficient should always be one due to normalization. */
8952 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8954 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8956 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8957 total
- nparam
- dim
- 1) != -1)
8960 isl_int_neg(*val
, c
[0]);
8964 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8965 unsigned dim
, isl_int
*val
)
8977 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8981 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8983 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8984 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8986 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8990 isl_int_set(*val
, v
);
8996 /* Return -1 if the constraint "c1" should be sorted before "c2"
8997 * and 1 if it should be sorted after "c2".
8998 * Return 0 if the two constraints are the same (up to the constant term).
9000 * In particular, if a constraint involves later variables than another
9001 * then it is sorted after this other constraint.
9002 * uset_gist depends on constraints without existentially quantified
9003 * variables sorting first.
9005 * For constraints that have the same latest variable, those
9006 * with the same coefficient for this latest variable (first in absolute value
9007 * and then in actual value) are grouped together.
9008 * This is useful for detecting pairs of constraints that can
9009 * be chained in their printed representation.
9011 * Finally, within a group, constraints are sorted according to
9012 * their coefficients (excluding the constant term).
9014 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9016 isl_int
**c1
= (isl_int
**) p1
;
9017 isl_int
**c2
= (isl_int
**) p2
;
9019 unsigned size
= *(unsigned *) arg
;
9022 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9023 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9028 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9031 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9035 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9038 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9039 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9040 * and 0 if the two constraints are the same (up to the constant term).
9042 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9043 isl_int
*c1
, isl_int
*c2
)
9049 total
= isl_basic_map_total_dim(bmap
);
9050 return sort_constraint_cmp(&c1
, &c2
, &total
);
9053 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9054 __isl_take isl_basic_map
*bmap
)
9060 if (bmap
->n_ineq
== 0)
9062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9064 total
= isl_basic_map_total_dim(bmap
);
9065 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9066 &sort_constraint_cmp
, &total
) < 0)
9067 return isl_basic_map_free(bmap
);
9071 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9072 __isl_take isl_basic_set
*bset
)
9074 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9075 (struct isl_basic_map
*)bset
);
9078 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9082 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9084 bmap
= isl_basic_map_remove_redundancies(bmap
);
9085 bmap
= isl_basic_map_sort_constraints(bmap
);
9087 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9091 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9093 return (struct isl_basic_set
*)isl_basic_map_normalize(
9094 (struct isl_basic_map
*)bset
);
9097 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9098 const __isl_keep isl_basic_map
*bmap2
)
9103 if (!bmap1
|| !bmap2
)
9108 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9109 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9110 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9111 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9112 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9113 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9114 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9115 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9116 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9117 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9118 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9120 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9122 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9124 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9125 return bmap1
->n_eq
- bmap2
->n_eq
;
9126 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9127 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9128 if (bmap1
->n_div
!= bmap2
->n_div
)
9129 return bmap1
->n_div
- bmap2
->n_div
;
9130 total
= isl_basic_map_total_dim(bmap1
);
9131 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9132 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9136 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9137 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9141 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9142 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9149 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9150 const __isl_keep isl_basic_set
*bset2
)
9152 return isl_basic_map_plain_cmp(bset1
, bset2
);
9155 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9161 if (set1
->n
!= set2
->n
)
9162 return set1
->n
- set2
->n
;
9164 for (i
= 0; i
< set1
->n
; ++i
) {
9165 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9173 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9174 __isl_keep isl_basic_map
*bmap2
)
9176 if (!bmap1
|| !bmap2
)
9177 return isl_bool_error
;
9178 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9181 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9182 __isl_keep isl_basic_set
*bset2
)
9184 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9185 (isl_basic_map
*)bset2
);
9188 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9190 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9191 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9193 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9196 /* Sort the basic maps of "map" and remove duplicate basic maps.
9198 * While removing basic maps, we make sure that the basic maps remain
9199 * sorted because isl_map_normalize expects the basic maps of the result
9202 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9206 map
= isl_map_remove_empty_parts(map
);
9209 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9210 for (i
= map
->n
- 1; i
>= 1; --i
) {
9211 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9213 isl_basic_map_free(map
->p
[i
-1]);
9214 for (j
= i
; j
< map
->n
; ++j
)
9215 map
->p
[j
- 1] = map
->p
[j
];
9222 /* Remove obvious duplicates among the basic maps of "map".
9224 * Unlike isl_map_normalize, this function does not remove redundant
9225 * constraints and only removes duplicates that have exactly the same
9226 * constraints in the input. It does sort the constraints and
9227 * the basic maps to ease the detection of duplicates.
9229 * If "map" has already been normalized or if the basic maps are
9230 * disjoint, then there can be no duplicates.
9232 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9235 isl_basic_map
*bmap
;
9241 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9243 for (i
= 0; i
< map
->n
; ++i
) {
9244 bmap
= isl_basic_map_copy(map
->p
[i
]);
9245 bmap
= isl_basic_map_sort_constraints(bmap
);
9247 return isl_map_free(map
);
9248 isl_basic_map_free(map
->p
[i
]);
9252 map
= sort_and_remove_duplicates(map
);
9256 /* We normalize in place, but if anything goes wrong we need
9257 * to return NULL, so we need to make sure we don't change the
9258 * meaning of any possible other copies of map.
9260 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9263 struct isl_basic_map
*bmap
;
9267 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9269 for (i
= 0; i
< map
->n
; ++i
) {
9270 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9273 isl_basic_map_free(map
->p
[i
]);
9277 map
= sort_and_remove_duplicates(map
);
9279 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9286 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9288 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9291 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9292 __isl_keep isl_map
*map2
)
9298 return isl_bool_error
;
9301 return isl_bool_true
;
9302 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9303 return isl_bool_false
;
9305 map1
= isl_map_copy(map1
);
9306 map2
= isl_map_copy(map2
);
9307 map1
= isl_map_normalize(map1
);
9308 map2
= isl_map_normalize(map2
);
9311 equal
= map1
->n
== map2
->n
;
9312 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9313 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9323 return isl_bool_error
;
9326 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9327 __isl_keep isl_set
*set2
)
9329 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9330 (struct isl_map
*)set2
);
9333 /* Return an interval that ranges from min to max (inclusive)
9335 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9336 isl_int min
, isl_int max
)
9339 struct isl_basic_set
*bset
= NULL
;
9341 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9345 k
= isl_basic_set_alloc_inequality(bset
);
9348 isl_int_set_si(bset
->ineq
[k
][1], 1);
9349 isl_int_neg(bset
->ineq
[k
][0], min
);
9351 k
= isl_basic_set_alloc_inequality(bset
);
9354 isl_int_set_si(bset
->ineq
[k
][1], -1);
9355 isl_int_set(bset
->ineq
[k
][0], max
);
9359 isl_basic_set_free(bset
);
9363 /* Return the basic maps in "map" as a list.
9365 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9366 __isl_keep isl_map
*map
)
9370 isl_basic_map_list
*list
;
9374 ctx
= isl_map_get_ctx(map
);
9375 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9377 for (i
= 0; i
< map
->n
; ++i
) {
9378 isl_basic_map
*bmap
;
9380 bmap
= isl_basic_map_copy(map
->p
[i
]);
9381 list
= isl_basic_map_list_add(list
, bmap
);
9387 /* Return the intersection of the elements in the non-empty list "list".
9388 * All elements are assumed to live in the same space.
9390 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9391 __isl_take isl_basic_map_list
*list
)
9394 isl_basic_map
*bmap
;
9398 n
= isl_basic_map_list_n_basic_map(list
);
9400 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9401 "expecting non-empty list", goto error
);
9403 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9404 for (i
= 1; i
< n
; ++i
) {
9405 isl_basic_map
*bmap_i
;
9407 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9408 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9411 isl_basic_map_list_free(list
);
9414 isl_basic_map_list_free(list
);
9418 /* Return the intersection of the elements in the non-empty list "list".
9419 * All elements are assumed to live in the same space.
9421 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9422 __isl_take isl_basic_set_list
*list
)
9424 return isl_basic_map_list_intersect(list
);
9427 /* Return the union of the elements in the non-empty list "list".
9428 * All elements are assumed to live in the same space.
9430 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9437 n
= isl_set_list_n_set(list
);
9439 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9440 "expecting non-empty list", goto error
);
9442 set
= isl_set_list_get_set(list
, 0);
9443 for (i
= 1; i
< n
; ++i
) {
9446 set_i
= isl_set_list_get_set(list
, i
);
9447 set
= isl_set_union(set
, set_i
);
9450 isl_set_list_free(list
);
9453 isl_set_list_free(list
);
9457 /* Return the Cartesian product of the basic sets in list (in the given order).
9459 __isl_give isl_basic_set
*isl_basic_set_list_product(
9460 __isl_take
struct isl_basic_set_list
*list
)
9468 struct isl_basic_set
*product
= NULL
;
9472 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9473 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9474 nparam
= isl_basic_set_n_param(list
->p
[0]);
9475 dim
= isl_basic_set_n_dim(list
->p
[0]);
9476 extra
= list
->p
[0]->n_div
;
9477 n_eq
= list
->p
[0]->n_eq
;
9478 n_ineq
= list
->p
[0]->n_ineq
;
9479 for (i
= 1; i
< list
->n
; ++i
) {
9480 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9481 isl_assert(list
->ctx
,
9482 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9483 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9484 extra
+= list
->p
[i
]->n_div
;
9485 n_eq
+= list
->p
[i
]->n_eq
;
9486 n_ineq
+= list
->p
[i
]->n_ineq
;
9488 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9493 for (i
= 0; i
< list
->n
; ++i
) {
9494 isl_basic_set_add_constraints(product
,
9495 isl_basic_set_copy(list
->p
[i
]), dim
);
9496 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9498 isl_basic_set_list_free(list
);
9501 isl_basic_set_free(product
);
9502 isl_basic_set_list_free(list
);
9506 struct isl_basic_map
*isl_basic_map_product(
9507 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9509 isl_space
*dim_result
= NULL
;
9510 struct isl_basic_map
*bmap
;
9511 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9512 struct isl_dim_map
*dim_map1
, *dim_map2
;
9514 if (!bmap1
|| !bmap2
)
9517 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9518 bmap2
->dim
, isl_dim_param
), goto error
);
9519 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9520 isl_space_copy(bmap2
->dim
));
9522 in1
= isl_basic_map_n_in(bmap1
);
9523 in2
= isl_basic_map_n_in(bmap2
);
9524 out1
= isl_basic_map_n_out(bmap1
);
9525 out2
= isl_basic_map_n_out(bmap2
);
9526 nparam
= isl_basic_map_n_param(bmap1
);
9528 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9529 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9530 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9531 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9532 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9533 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9534 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9535 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9536 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9537 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9538 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9540 bmap
= isl_basic_map_alloc_space(dim_result
,
9541 bmap1
->n_div
+ bmap2
->n_div
,
9542 bmap1
->n_eq
+ bmap2
->n_eq
,
9543 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9544 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9545 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9546 bmap
= isl_basic_map_simplify(bmap
);
9547 return isl_basic_map_finalize(bmap
);
9549 isl_basic_map_free(bmap1
);
9550 isl_basic_map_free(bmap2
);
9554 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9555 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9557 isl_basic_map
*prod
;
9559 prod
= isl_basic_map_product(bmap1
, bmap2
);
9560 prod
= isl_basic_map_flatten(prod
);
9564 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9565 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9567 return isl_basic_map_flat_range_product(bset1
, bset2
);
9570 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9571 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9573 isl_space
*space_result
= NULL
;
9574 isl_basic_map
*bmap
;
9575 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9576 struct isl_dim_map
*dim_map1
, *dim_map2
;
9578 if (!bmap1
|| !bmap2
)
9581 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9582 isl_space_copy(bmap2
->dim
));
9584 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9585 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9586 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9587 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9589 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9590 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9591 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9592 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9593 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9594 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9595 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9596 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9597 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9598 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9599 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9601 bmap
= isl_basic_map_alloc_space(space_result
,
9602 bmap1
->n_div
+ bmap2
->n_div
,
9603 bmap1
->n_eq
+ bmap2
->n_eq
,
9604 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9605 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9606 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9607 bmap
= isl_basic_map_simplify(bmap
);
9608 return isl_basic_map_finalize(bmap
);
9610 isl_basic_map_free(bmap1
);
9611 isl_basic_map_free(bmap2
);
9615 __isl_give isl_basic_map
*isl_basic_map_range_product(
9616 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9618 isl_space
*dim_result
= NULL
;
9619 isl_basic_map
*bmap
;
9620 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9621 struct isl_dim_map
*dim_map1
, *dim_map2
;
9623 if (!bmap1
|| !bmap2
)
9626 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9627 bmap2
->dim
, isl_dim_param
))
9628 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9629 "parameters don't match", goto error
);
9631 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9632 isl_space_copy(bmap2
->dim
));
9634 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9635 out1
= isl_basic_map_n_out(bmap1
);
9636 out2
= isl_basic_map_n_out(bmap2
);
9637 nparam
= isl_basic_map_n_param(bmap1
);
9639 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9640 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9641 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9642 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9643 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9644 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9645 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9646 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9647 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9648 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9649 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9651 bmap
= isl_basic_map_alloc_space(dim_result
,
9652 bmap1
->n_div
+ bmap2
->n_div
,
9653 bmap1
->n_eq
+ bmap2
->n_eq
,
9654 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9655 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9656 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9657 bmap
= isl_basic_map_simplify(bmap
);
9658 return isl_basic_map_finalize(bmap
);
9660 isl_basic_map_free(bmap1
);
9661 isl_basic_map_free(bmap2
);
9665 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9666 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9668 isl_basic_map
*prod
;
9670 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9671 prod
= isl_basic_map_flatten_range(prod
);
9675 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9676 * and collect the results.
9677 * The result live in the space obtained by calling "space_product"
9678 * on the spaces of "map1" and "map2".
9679 * If "remove_duplicates" is set then the result may contain duplicates
9680 * (even if the inputs do not) and so we try and remove the obvious
9683 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9684 __isl_take isl_map
*map2
,
9685 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9686 __isl_take isl_space
*right
),
9687 __isl_give isl_basic_map
*(*basic_map_product
)(
9688 __isl_take isl_basic_map
*left
,
9689 __isl_take isl_basic_map
*right
),
9690 int remove_duplicates
)
9693 struct isl_map
*result
;
9699 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9700 map2
->dim
, isl_dim_param
), goto error
);
9702 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9703 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9704 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9706 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9707 isl_space_copy(map2
->dim
)),
9708 map1
->n
* map2
->n
, flags
);
9711 for (i
= 0; i
< map1
->n
; ++i
)
9712 for (j
= 0; j
< map2
->n
; ++j
) {
9713 struct isl_basic_map
*part
;
9714 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9715 isl_basic_map_copy(map2
->p
[j
]));
9716 if (isl_basic_map_is_empty(part
))
9717 isl_basic_map_free(part
);
9719 result
= isl_map_add_basic_map(result
, part
);
9723 if (remove_duplicates
)
9724 result
= isl_map_remove_obvious_duplicates(result
);
9734 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9736 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9737 __isl_take isl_map
*map2
)
9739 return map_product(map1
, map2
, &isl_space_product
,
9740 &isl_basic_map_product
, 0);
9743 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9744 __isl_take isl_map
*map2
)
9746 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9749 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9751 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9752 __isl_take isl_map
*map2
)
9756 prod
= isl_map_product(map1
, map2
);
9757 prod
= isl_map_flatten(prod
);
9761 /* Given two set A and B, construct its Cartesian product A x B.
9763 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9765 return isl_map_range_product(set1
, set2
);
9768 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9769 __isl_take isl_set
*set2
)
9771 return isl_map_flat_range_product(set1
, set2
);
9774 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9776 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9777 __isl_take isl_map
*map2
)
9779 return map_product(map1
, map2
, &isl_space_domain_product
,
9780 &isl_basic_map_domain_product
, 1);
9783 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9785 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9786 __isl_take isl_map
*map2
)
9788 return map_product(map1
, map2
, &isl_space_range_product
,
9789 &isl_basic_map_range_product
, 1);
9792 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9793 __isl_take isl_map
*map2
)
9795 return isl_map_align_params_map_map_and(map1
, map2
,
9796 &map_domain_product_aligned
);
9799 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9800 __isl_take isl_map
*map2
)
9802 return isl_map_align_params_map_map_and(map1
, map2
,
9803 &map_range_product_aligned
);
9806 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9808 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9811 int total1
, keep1
, total2
, keep2
;
9815 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9816 !isl_space_range_is_wrapping(map
->dim
))
9817 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9818 "not a product", return isl_map_free(map
));
9820 space
= isl_map_get_space(map
);
9821 total1
= isl_space_dim(space
, isl_dim_in
);
9822 total2
= isl_space_dim(space
, isl_dim_out
);
9823 space
= isl_space_factor_domain(space
);
9824 keep1
= isl_space_dim(space
, isl_dim_in
);
9825 keep2
= isl_space_dim(space
, isl_dim_out
);
9826 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9827 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9828 map
= isl_map_reset_space(map
, space
);
9833 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9835 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9838 int total1
, keep1
, total2
, keep2
;
9842 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9843 !isl_space_range_is_wrapping(map
->dim
))
9844 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9845 "not a product", return isl_map_free(map
));
9847 space
= isl_map_get_space(map
);
9848 total1
= isl_space_dim(space
, isl_dim_in
);
9849 total2
= isl_space_dim(space
, isl_dim_out
);
9850 space
= isl_space_factor_range(space
);
9851 keep1
= isl_space_dim(space
, isl_dim_in
);
9852 keep2
= isl_space_dim(space
, isl_dim_out
);
9853 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9854 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9855 map
= isl_map_reset_space(map
, space
);
9860 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9862 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9869 if (!isl_space_domain_is_wrapping(map
->dim
))
9870 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9871 "domain is not a product", return isl_map_free(map
));
9873 space
= isl_map_get_space(map
);
9874 total
= isl_space_dim(space
, isl_dim_in
);
9875 space
= isl_space_domain_factor_domain(space
);
9876 keep
= isl_space_dim(space
, isl_dim_in
);
9877 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9878 map
= isl_map_reset_space(map
, space
);
9883 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9885 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9892 if (!isl_space_domain_is_wrapping(map
->dim
))
9893 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9894 "domain is not a product", return isl_map_free(map
));
9896 space
= isl_map_get_space(map
);
9897 total
= isl_space_dim(space
, isl_dim_in
);
9898 space
= isl_space_domain_factor_range(space
);
9899 keep
= isl_space_dim(space
, isl_dim_in
);
9900 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9901 map
= isl_map_reset_space(map
, space
);
9906 /* Given a map A -> [B -> C], extract the map A -> B.
9908 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9915 if (!isl_space_range_is_wrapping(map
->dim
))
9916 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9917 "range is not a product", return isl_map_free(map
));
9919 space
= isl_map_get_space(map
);
9920 total
= isl_space_dim(space
, isl_dim_out
);
9921 space
= isl_space_range_factor_domain(space
);
9922 keep
= isl_space_dim(space
, isl_dim_out
);
9923 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9924 map
= isl_map_reset_space(map
, space
);
9929 /* Given a map A -> [B -> C], extract the map A -> C.
9931 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9938 if (!isl_space_range_is_wrapping(map
->dim
))
9939 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9940 "range is not a product", return isl_map_free(map
));
9942 space
= isl_map_get_space(map
);
9943 total
= isl_space_dim(space
, isl_dim_out
);
9944 space
= isl_space_range_factor_range(space
);
9945 keep
= isl_space_dim(space
, isl_dim_out
);
9946 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9947 map
= isl_map_reset_space(map
, space
);
9952 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9954 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9955 __isl_take isl_map
*map2
)
9959 prod
= isl_map_domain_product(map1
, map2
);
9960 prod
= isl_map_flatten_domain(prod
);
9964 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9966 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9967 __isl_take isl_map
*map2
)
9971 prod
= isl_map_range_product(map1
, map2
);
9972 prod
= isl_map_flatten_range(prod
);
9976 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9979 uint32_t hash
= isl_hash_init();
9984 bmap
= isl_basic_map_copy(bmap
);
9985 bmap
= isl_basic_map_normalize(bmap
);
9988 total
= isl_basic_map_total_dim(bmap
);
9989 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9990 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9992 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9993 isl_hash_hash(hash
, c_hash
);
9995 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9996 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9998 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9999 isl_hash_hash(hash
, c_hash
);
10001 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10002 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10004 if (isl_int_is_zero(bmap
->div
[i
][0]))
10006 isl_hash_byte(hash
, i
& 0xFF);
10007 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10008 isl_hash_hash(hash
, c_hash
);
10010 isl_basic_map_free(bmap
);
10014 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10016 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
10019 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10026 map
= isl_map_copy(map
);
10027 map
= isl_map_normalize(map
);
10031 hash
= isl_hash_init();
10032 for (i
= 0; i
< map
->n
; ++i
) {
10033 uint32_t bmap_hash
;
10034 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10035 isl_hash_hash(hash
, bmap_hash
);
10043 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10045 return isl_map_get_hash((isl_map
*)set
);
10048 /* Check if the value for dimension dim is completely determined
10049 * by the values of the other parameters and variables.
10050 * That is, check if dimension dim is involved in an equality.
10052 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10059 nparam
= isl_basic_set_n_param(bset
);
10060 for (i
= 0; i
< bset
->n_eq
; ++i
)
10061 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10066 /* Check if the value for dimension dim is completely determined
10067 * by the values of the other parameters and variables.
10068 * That is, check if dimension dim is involved in an equality
10069 * for each of the subsets.
10071 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10077 for (i
= 0; i
< set
->n
; ++i
) {
10079 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10086 /* Return the number of basic maps in the (current) representation of "map".
10088 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10090 return map
? map
->n
: 0;
10093 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10095 return set
? set
->n
: 0;
10098 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10099 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10104 return isl_stat_error
;
10106 for (i
= 0; i
< map
->n
; ++i
)
10107 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10108 return isl_stat_error
;
10110 return isl_stat_ok
;
10113 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10114 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10119 return isl_stat_error
;
10121 for (i
= 0; i
< set
->n
; ++i
)
10122 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10123 return isl_stat_error
;
10125 return isl_stat_ok
;
10128 /* Return a list of basic sets, the union of which is equal to "set".
10130 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10131 __isl_keep isl_set
*set
)
10134 isl_basic_set_list
*list
;
10139 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10140 for (i
= 0; i
< set
->n
; ++i
) {
10141 isl_basic_set
*bset
;
10143 bset
= isl_basic_set_copy(set
->p
[i
]);
10144 list
= isl_basic_set_list_add(list
, bset
);
10150 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10157 bset
= isl_basic_set_cow(bset
);
10161 dim
= isl_basic_set_get_space(bset
);
10162 dim
= isl_space_lift(dim
, bset
->n_div
);
10165 isl_space_free(bset
->dim
);
10167 bset
->extra
-= bset
->n_div
;
10170 bset
= isl_basic_set_finalize(bset
);
10174 isl_basic_set_free(bset
);
10178 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10184 set
= isl_set_align_divs(set
);
10189 set
= isl_set_cow(set
);
10193 n_div
= set
->p
[0]->n_div
;
10194 dim
= isl_set_get_space(set
);
10195 dim
= isl_space_lift(dim
, n_div
);
10198 isl_space_free(set
->dim
);
10201 for (i
= 0; i
< set
->n
; ++i
) {
10202 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10213 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10216 struct isl_basic_map
*bmap
;
10223 set
= isl_set_align_divs(set
);
10228 dim
= isl_set_get_space(set
);
10229 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10231 return isl_map_identity(isl_space_map_from_set(dim
));
10234 n_div
= set
->p
[0]->n_div
;
10235 dim
= isl_space_map_from_set(dim
);
10236 n_param
= isl_space_dim(dim
, isl_dim_param
);
10237 n_set
= isl_space_dim(dim
, isl_dim_in
);
10238 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10239 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10240 for (i
= 0; i
< n_set
; ++i
)
10241 bmap
= var_equal(bmap
, i
);
10243 total
= n_param
+ n_set
+ n_set
+ n_div
;
10244 for (i
= 0; i
< n_div
; ++i
) {
10245 k
= isl_basic_map_alloc_inequality(bmap
);
10248 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10249 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10250 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10251 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10252 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10253 set
->p
[0]->div
[i
][0]);
10255 l
= isl_basic_map_alloc_inequality(bmap
);
10258 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10259 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10260 set
->p
[0]->div
[i
][0]);
10261 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10265 bmap
= isl_basic_map_simplify(bmap
);
10266 bmap
= isl_basic_map_finalize(bmap
);
10267 return isl_map_from_basic_map(bmap
);
10270 isl_basic_map_free(bmap
);
10274 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10282 dim
= isl_basic_set_total_dim(bset
);
10283 size
+= bset
->n_eq
* (1 + dim
);
10284 size
+= bset
->n_ineq
* (1 + dim
);
10285 size
+= bset
->n_div
* (2 + dim
);
10290 int isl_set_size(__isl_keep isl_set
*set
)
10298 for (i
= 0; i
< set
->n
; ++i
)
10299 size
+= isl_basic_set_size(set
->p
[i
]);
10304 /* Check if there is any lower bound (if lower == 0) and/or upper
10305 * bound (if upper == 0) on the specified dim.
10307 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10308 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10313 return isl_bool_error
;
10315 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10316 return isl_bool_error
);
10318 pos
+= isl_basic_map_offset(bmap
, type
);
10320 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10321 if (isl_int_is_zero(bmap
->div
[i
][0]))
10323 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10324 return isl_bool_true
;
10327 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10328 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10329 return isl_bool_true
;
10331 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10332 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10339 return lower
&& upper
;
10342 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10343 enum isl_dim_type type
, unsigned pos
)
10345 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10348 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10349 enum isl_dim_type type
, unsigned pos
)
10351 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10354 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10355 enum isl_dim_type type
, unsigned pos
)
10357 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10360 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10361 enum isl_dim_type type
, unsigned pos
)
10368 for (i
= 0; i
< map
->n
; ++i
) {
10370 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10371 if (bounded
< 0 || !bounded
)
10378 /* Return 1 if the specified dim is involved in both an upper bound
10379 * and a lower bound.
10381 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10382 enum isl_dim_type type
, unsigned pos
)
10384 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10387 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10389 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10390 enum isl_dim_type type
, unsigned pos
,
10391 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10392 enum isl_dim_type type
, unsigned pos
))
10397 return isl_bool_error
;
10399 for (i
= 0; i
< map
->n
; ++i
) {
10401 bounded
= fn(map
->p
[i
], type
, pos
);
10402 if (bounded
< 0 || bounded
)
10406 return isl_bool_false
;
10409 /* Return 1 if the specified dim is involved in any lower bound.
10411 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10412 enum isl_dim_type type
, unsigned pos
)
10414 return has_any_bound(set
, type
, pos
,
10415 &isl_basic_map_dim_has_lower_bound
);
10418 /* Return 1 if the specified dim is involved in any upper bound.
10420 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10421 enum isl_dim_type type
, unsigned pos
)
10423 return has_any_bound(set
, type
, pos
,
10424 &isl_basic_map_dim_has_upper_bound
);
10427 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10429 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10430 enum isl_dim_type type
, unsigned pos
,
10431 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10432 enum isl_dim_type type
, unsigned pos
))
10437 return isl_bool_error
;
10439 for (i
= 0; i
< map
->n
; ++i
) {
10441 bounded
= fn(map
->p
[i
], type
, pos
);
10442 if (bounded
< 0 || !bounded
)
10446 return isl_bool_true
;
10449 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10451 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10452 enum isl_dim_type type
, unsigned pos
)
10454 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10457 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10459 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10460 enum isl_dim_type type
, unsigned pos
)
10462 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10465 /* For each of the "n" variables starting at "first", determine
10466 * the sign of the variable and put the results in the first "n"
10467 * elements of the array "signs".
10469 * 1 means that the variable is non-negative
10470 * -1 means that the variable is non-positive
10471 * 0 means the variable attains both positive and negative values.
10473 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10474 unsigned first
, unsigned n
, int *signs
)
10476 isl_vec
*bound
= NULL
;
10477 struct isl_tab
*tab
= NULL
;
10478 struct isl_tab_undo
*snap
;
10481 if (!bset
|| !signs
)
10484 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10485 tab
= isl_tab_from_basic_set(bset
, 0);
10486 if (!bound
|| !tab
)
10489 isl_seq_clr(bound
->el
, bound
->size
);
10490 isl_int_set_si(bound
->el
[0], -1);
10492 snap
= isl_tab_snap(tab
);
10493 for (i
= 0; i
< n
; ++i
) {
10496 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10497 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10499 empty
= tab
->empty
;
10500 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10501 if (isl_tab_rollback(tab
, snap
) < 0)
10509 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10510 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10512 empty
= tab
->empty
;
10513 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10514 if (isl_tab_rollback(tab
, snap
) < 0)
10517 signs
[i
] = empty
? -1 : 0;
10521 isl_vec_free(bound
);
10525 isl_vec_free(bound
);
10529 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10530 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10532 if (!bset
|| !signs
)
10534 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10537 first
+= pos(bset
->dim
, type
) - 1;
10538 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10541 /* Is it possible for the integer division "div" to depend (possibly
10542 * indirectly) on any output dimensions?
10544 * If the div is undefined, then we conservatively assume that it
10545 * may depend on them.
10546 * Otherwise, we check if it actually depends on them or on any integer
10547 * divisions that may depend on them.
10549 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10552 unsigned n_out
, o_out
;
10553 unsigned n_div
, o_div
;
10555 if (isl_int_is_zero(bmap
->div
[div
][0]))
10558 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10559 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10561 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10564 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10565 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10567 for (i
= 0; i
< n_div
; ++i
) {
10568 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10570 if (div_may_involve_output(bmap
, i
))
10577 /* Return the first integer division of "bmap" in the range
10578 * [first, first + n[ that may depend on any output dimensions and
10579 * that has a non-zero coefficient in "c" (where the first coefficient
10580 * in "c" corresponds to integer division "first").
10582 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10583 isl_int
*c
, int first
, int n
)
10590 for (k
= first
; k
< first
+ n
; ++k
) {
10591 if (isl_int_is_zero(c
[k
]))
10593 if (div_may_involve_output(bmap
, k
))
10600 /* Look for a pair of inequality constraints in "bmap" of the form
10602 * -l + i >= 0 or i >= l
10604 * n + l - i >= 0 or i <= l + n
10606 * with n < "m" and i the output dimension at position "pos".
10607 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10608 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10609 * and earlier output dimensions, as well as integer divisions that do
10610 * not involve any of the output dimensions.
10612 * Return the index of the first inequality constraint or bmap->n_ineq
10613 * if no such pair can be found.
10615 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10616 int pos
, isl_int m
)
10621 unsigned n_div
, o_div
;
10622 unsigned n_out
, o_out
;
10628 ctx
= isl_basic_map_get_ctx(bmap
);
10629 total
= isl_basic_map_total_dim(bmap
);
10630 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10631 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10632 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10633 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10634 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10635 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10637 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10638 n_out
- (pos
+ 1)) != -1)
10640 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10643 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10644 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10647 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10648 bmap
->ineq
[j
] + 1, total
))
10652 if (j
>= bmap
->n_ineq
)
10654 isl_int_add(bmap
->ineq
[i
][0],
10655 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10656 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10657 isl_int_sub(bmap
->ineq
[i
][0],
10658 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10661 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10667 return bmap
->n_ineq
;
10670 /* Return the index of the equality of "bmap" that defines
10671 * the output dimension "pos" in terms of earlier dimensions.
10672 * The equality may also involve integer divisions, as long
10673 * as those integer divisions are defined in terms of
10674 * parameters or input dimensions.
10675 * In this case, *div is set to the number of integer divisions and
10676 * *ineq is set to the number of inequality constraints (provided
10677 * div and ineq are not NULL).
10679 * The equality may also involve a single integer division involving
10680 * the output dimensions (typically only output dimension "pos") as
10681 * long as the coefficient of output dimension "pos" is 1 or -1 and
10682 * there is a pair of constraints i >= l and i <= l + n, with i referring
10683 * to output dimension "pos", l an expression involving only earlier
10684 * dimensions and n smaller than the coefficient of the integer division
10685 * in the equality. In this case, the output dimension can be defined
10686 * in terms of a modulo expression that does not involve the integer division.
10687 * *div is then set to this single integer division and
10688 * *ineq is set to the index of constraint i >= l.
10690 * Return bmap->n_eq if there is no such equality.
10691 * Return -1 on error.
10693 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10694 int pos
, int *div
, int *ineq
)
10697 unsigned n_out
, o_out
;
10698 unsigned n_div
, o_div
;
10703 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10704 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10705 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10706 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10709 *ineq
= bmap
->n_ineq
;
10712 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10713 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10715 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10716 n_out
- (pos
+ 1)) != -1)
10718 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10722 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10723 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10725 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10726 k
+ 1, n_div
- (k
+1)) < n_div
)
10728 l
= find_modulo_constraint_pair(bmap
, pos
,
10729 bmap
->eq
[j
][o_div
+ k
]);
10732 if (l
>= bmap
->n_ineq
)
10744 /* Check if the given basic map is obviously single-valued.
10745 * In particular, for each output dimension, check that there is
10746 * an equality that defines the output dimension in terms of
10747 * earlier dimensions.
10749 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10755 return isl_bool_error
;
10757 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10759 for (i
= 0; i
< n_out
; ++i
) {
10762 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10765 return isl_bool_error
;
10766 if (eq
>= bmap
->n_eq
)
10767 return isl_bool_false
;
10770 return isl_bool_true
;
10773 /* Check if the given basic map is single-valued.
10774 * We simply compute
10778 * and check if the result is a subset of the identity mapping.
10780 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10783 isl_basic_map
*test
;
10787 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10791 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10792 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10794 space
= isl_basic_map_get_space(bmap
);
10795 space
= isl_space_map_from_set(isl_space_range(space
));
10796 id
= isl_basic_map_identity(space
);
10798 sv
= isl_basic_map_is_subset(test
, id
);
10800 isl_basic_map_free(test
);
10801 isl_basic_map_free(id
);
10806 /* Check if the given map is obviously single-valued.
10808 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10811 return isl_bool_error
;
10813 return isl_bool_true
;
10815 return isl_bool_false
;
10817 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10820 /* Check if the given map is single-valued.
10821 * We simply compute
10825 * and check if the result is a subset of the identity mapping.
10827 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10834 sv
= isl_map_plain_is_single_valued(map
);
10838 test
= isl_map_reverse(isl_map_copy(map
));
10839 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10841 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10842 id
= isl_map_identity(dim
);
10844 sv
= isl_map_is_subset(test
, id
);
10846 isl_map_free(test
);
10852 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10856 map
= isl_map_copy(map
);
10857 map
= isl_map_reverse(map
);
10858 in
= isl_map_is_single_valued(map
);
10864 /* Check if the given map is obviously injective.
10866 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10870 map
= isl_map_copy(map
);
10871 map
= isl_map_reverse(map
);
10872 in
= isl_map_plain_is_single_valued(map
);
10878 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10882 sv
= isl_map_is_single_valued(map
);
10886 return isl_map_is_injective(map
);
10889 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10891 return isl_map_is_single_valued((isl_map
*)set
);
10894 /* Does "map" only map elements to themselves?
10896 * If the domain and range spaces are different, then "map"
10897 * is considered not to be an identity relation, even if it is empty.
10898 * Otherwise, construct the maximal identity relation and
10899 * check whether "map" is a subset of this relation.
10901 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10905 isl_bool equal
, is_identity
;
10907 space
= isl_map_get_space(map
);
10908 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10909 isl_space_free(space
);
10910 if (equal
< 0 || !equal
)
10913 id
= isl_map_identity(isl_map_get_space(map
));
10914 is_identity
= isl_map_is_subset(map
, id
);
10917 return is_identity
;
10920 int isl_map_is_translation(__isl_keep isl_map
*map
)
10925 delta
= isl_map_deltas(isl_map_copy(map
));
10926 ok
= isl_set_is_singleton(delta
);
10927 isl_set_free(delta
);
10932 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10934 if (isl_seq_first_non_zero(p
, pos
) != -1)
10936 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10941 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10950 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10953 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10954 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10955 for (j
= 0; j
< nvar
; ++j
) {
10956 int lower
= 0, upper
= 0;
10957 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10958 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10960 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10964 if (i
< bset
->n_eq
)
10966 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10967 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10969 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10971 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10976 if (!lower
|| !upper
)
10983 int isl_set_is_box(__isl_keep isl_set
*set
)
10990 return isl_basic_set_is_box(set
->p
[0]);
10993 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10996 return isl_bool_error
;
10998 return isl_space_is_wrapping(bset
->dim
);
11001 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11004 return isl_bool_error
;
11006 return isl_space_is_wrapping(set
->dim
);
11009 /* Modify the space of "map" through a call to "change".
11010 * If "can_change" is set (not NULL), then first call it to check
11011 * if the modification is allowed, printing the error message "cannot_change"
11014 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11015 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11016 const char *cannot_change
,
11017 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11025 ok
= can_change
? can_change(map
) : isl_bool_true
;
11027 return isl_map_free(map
);
11029 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11030 return isl_map_free(map
));
11032 space
= change(isl_map_get_space(map
));
11033 map
= isl_map_reset_space(map
, space
);
11038 /* Is the domain of "map" a wrapped relation?
11040 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11043 return isl_bool_error
;
11045 return isl_space_domain_is_wrapping(map
->dim
);
11048 /* Is the range of "map" a wrapped relation?
11050 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11053 return isl_bool_error
;
11055 return isl_space_range_is_wrapping(map
->dim
);
11058 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11060 bmap
= isl_basic_map_cow(bmap
);
11064 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11068 bmap
= isl_basic_map_finalize(bmap
);
11070 return (isl_basic_set
*)bmap
;
11072 isl_basic_map_free(bmap
);
11076 /* Given a map A -> B, return the set (A -> B).
11078 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11080 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11083 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11085 bset
= isl_basic_set_cow(bset
);
11089 bset
->dim
= isl_space_unwrap(bset
->dim
);
11093 bset
= isl_basic_set_finalize(bset
);
11095 return (isl_basic_map
*)bset
;
11097 isl_basic_set_free(bset
);
11101 /* Given a set (A -> B), return the map A -> B.
11102 * Error out if "set" is not of the form (A -> B).
11104 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11106 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11107 "not a wrapping set", &isl_space_unwrap
);
11110 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11111 enum isl_dim_type type
)
11116 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11119 bmap
= isl_basic_map_cow(bmap
);
11123 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11127 bmap
= isl_basic_map_finalize(bmap
);
11131 isl_basic_map_free(bmap
);
11135 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11136 enum isl_dim_type type
)
11143 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11146 map
= isl_map_cow(map
);
11150 for (i
= 0; i
< map
->n
; ++i
) {
11151 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11155 map
->dim
= isl_space_reset(map
->dim
, type
);
11165 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11170 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11173 bmap
= isl_basic_map_cow(bmap
);
11177 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11181 bmap
= isl_basic_map_finalize(bmap
);
11185 isl_basic_map_free(bmap
);
11189 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11191 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11194 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11195 __isl_take isl_basic_map
*bmap
)
11200 if (!bmap
->dim
->nested
[0])
11203 bmap
= isl_basic_map_cow(bmap
);
11207 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11211 bmap
= isl_basic_map_finalize(bmap
);
11215 isl_basic_map_free(bmap
);
11219 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11220 __isl_take isl_basic_map
*bmap
)
11225 if (!bmap
->dim
->nested
[1])
11228 bmap
= isl_basic_map_cow(bmap
);
11232 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11236 bmap
= isl_basic_map_finalize(bmap
);
11240 isl_basic_map_free(bmap
);
11244 /* Remove any internal structure from the spaces of domain and range of "map".
11246 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11251 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11254 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11257 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11259 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11262 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11264 isl_space
*dim
, *flat_dim
;
11267 dim
= isl_set_get_space(set
);
11268 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11269 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11270 map
= isl_map_intersect_domain(map
, set
);
11275 /* Remove any internal structure from the space of the domain of "map".
11277 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11282 if (!map
->dim
->nested
[0])
11285 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11288 /* Remove any internal structure from the space of the range of "map".
11290 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11295 if (!map
->dim
->nested
[1])
11298 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11301 /* Reorder the dimensions of "bmap" according to the given dim_map
11302 * and set the dimension specification to "dim" and
11303 * perform Gaussian elimination on the result.
11305 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11306 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11308 isl_basic_map
*res
;
11311 bmap
= isl_basic_map_cow(bmap
);
11312 if (!bmap
|| !dim
|| !dim_map
)
11315 flags
= bmap
->flags
;
11316 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11317 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11318 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11319 res
= isl_basic_map_alloc_space(dim
,
11320 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11321 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11323 res
->flags
= flags
;
11324 res
= isl_basic_map_gauss(res
, NULL
);
11325 res
= isl_basic_map_finalize(res
);
11329 isl_basic_map_free(bmap
);
11330 isl_space_free(dim
);
11334 /* Reorder the dimensions of "map" according to given reordering.
11336 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11337 __isl_take isl_reordering
*r
)
11340 struct isl_dim_map
*dim_map
;
11342 map
= isl_map_cow(map
);
11343 dim_map
= isl_dim_map_from_reordering(r
);
11344 if (!map
|| !r
|| !dim_map
)
11347 for (i
= 0; i
< map
->n
; ++i
) {
11348 struct isl_dim_map
*dim_map_i
;
11350 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11352 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11353 isl_space_copy(r
->dim
), dim_map_i
);
11359 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11361 isl_reordering_free(r
);
11367 isl_reordering_free(r
);
11371 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11372 __isl_take isl_reordering
*r
)
11374 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11377 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11378 __isl_take isl_space
*model
)
11382 if (!map
|| !model
)
11385 ctx
= isl_space_get_ctx(model
);
11386 if (!isl_space_has_named_params(model
))
11387 isl_die(ctx
, isl_error_invalid
,
11388 "model has unnamed parameters", goto error
);
11389 if (!isl_space_has_named_params(map
->dim
))
11390 isl_die(ctx
, isl_error_invalid
,
11391 "relation has unnamed parameters", goto error
);
11392 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11393 isl_reordering
*exp
;
11395 model
= isl_space_drop_dims(model
, isl_dim_in
,
11396 0, isl_space_dim(model
, isl_dim_in
));
11397 model
= isl_space_drop_dims(model
, isl_dim_out
,
11398 0, isl_space_dim(model
, isl_dim_out
));
11399 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11400 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11401 map
= isl_map_realign(map
, exp
);
11404 isl_space_free(model
);
11407 isl_space_free(model
);
11412 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11413 __isl_take isl_space
*model
)
11415 return isl_map_align_params(set
, model
);
11418 /* Align the parameters of "bmap" to those of "model", introducing
11419 * additional parameters if needed.
11421 __isl_give isl_basic_map
*isl_basic_map_align_params(
11422 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11426 if (!bmap
|| !model
)
11429 ctx
= isl_space_get_ctx(model
);
11430 if (!isl_space_has_named_params(model
))
11431 isl_die(ctx
, isl_error_invalid
,
11432 "model has unnamed parameters", goto error
);
11433 if (!isl_space_has_named_params(bmap
->dim
))
11434 isl_die(ctx
, isl_error_invalid
,
11435 "relation has unnamed parameters", goto error
);
11436 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11437 isl_reordering
*exp
;
11438 struct isl_dim_map
*dim_map
;
11440 model
= isl_space_drop_dims(model
, isl_dim_in
,
11441 0, isl_space_dim(model
, isl_dim_in
));
11442 model
= isl_space_drop_dims(model
, isl_dim_out
,
11443 0, isl_space_dim(model
, isl_dim_out
));
11444 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11445 exp
= isl_reordering_extend_space(exp
,
11446 isl_basic_map_get_space(bmap
));
11447 dim_map
= isl_dim_map_from_reordering(exp
);
11448 bmap
= isl_basic_map_realign(bmap
,
11449 exp
? isl_space_copy(exp
->dim
) : NULL
,
11450 isl_dim_map_extend(dim_map
, bmap
));
11451 isl_reordering_free(exp
);
11455 isl_space_free(model
);
11458 isl_space_free(model
);
11459 isl_basic_map_free(bmap
);
11463 /* Align the parameters of "bset" to those of "model", introducing
11464 * additional parameters if needed.
11466 __isl_give isl_basic_set
*isl_basic_set_align_params(
11467 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11469 return isl_basic_map_align_params(bset
, model
);
11472 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11473 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11474 enum isl_dim_type c2
, enum isl_dim_type c3
,
11475 enum isl_dim_type c4
, enum isl_dim_type c5
)
11477 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11478 struct isl_mat
*mat
;
11484 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11485 isl_basic_map_total_dim(bmap
) + 1);
11488 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11489 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11490 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11491 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11492 isl_int_set(mat
->row
[i
][pos
],
11493 bmap
->eq
[i
][off
+ k
]);
11501 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11502 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11503 enum isl_dim_type c2
, enum isl_dim_type c3
,
11504 enum isl_dim_type c4
, enum isl_dim_type c5
)
11506 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11507 struct isl_mat
*mat
;
11513 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11514 isl_basic_map_total_dim(bmap
) + 1);
11517 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11518 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11519 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11520 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11521 isl_int_set(mat
->row
[i
][pos
],
11522 bmap
->ineq
[i
][off
+ k
]);
11530 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11531 __isl_take isl_space
*dim
,
11532 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11533 enum isl_dim_type c2
, enum isl_dim_type c3
,
11534 enum isl_dim_type c4
, enum isl_dim_type c5
)
11536 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11537 isl_basic_map
*bmap
;
11543 if (!dim
|| !eq
|| !ineq
)
11546 if (eq
->n_col
!= ineq
->n_col
)
11547 isl_die(dim
->ctx
, isl_error_invalid
,
11548 "equalities and inequalities matrices should have "
11549 "same number of columns", goto error
);
11551 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11553 if (eq
->n_col
< total
)
11554 isl_die(dim
->ctx
, isl_error_invalid
,
11555 "number of columns too small", goto error
);
11557 extra
= eq
->n_col
- total
;
11559 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11560 eq
->n_row
, ineq
->n_row
);
11563 for (i
= 0; i
< extra
; ++i
) {
11564 k
= isl_basic_map_alloc_div(bmap
);
11567 isl_int_set_si(bmap
->div
[k
][0], 0);
11569 for (i
= 0; i
< eq
->n_row
; ++i
) {
11570 l
= isl_basic_map_alloc_equality(bmap
);
11573 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11574 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11575 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11576 isl_int_set(bmap
->eq
[l
][off
+ k
],
11582 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11583 l
= isl_basic_map_alloc_inequality(bmap
);
11586 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11587 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11588 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11589 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11590 ineq
->row
[i
][pos
]);
11596 isl_space_free(dim
);
11598 isl_mat_free(ineq
);
11600 bmap
= isl_basic_map_simplify(bmap
);
11601 return isl_basic_map_finalize(bmap
);
11603 isl_space_free(dim
);
11605 isl_mat_free(ineq
);
11609 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11610 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11611 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11613 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11614 c1
, c2
, c3
, c4
, isl_dim_in
);
11617 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11618 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11619 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11621 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11622 c1
, c2
, c3
, c4
, isl_dim_in
);
11625 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11626 __isl_take isl_space
*dim
,
11627 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11628 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11630 return (isl_basic_set
*)
11631 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11632 c1
, c2
, c3
, c4
, isl_dim_in
);
11635 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11638 return isl_bool_error
;
11640 return isl_space_can_zip(bmap
->dim
);
11643 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11646 return isl_bool_error
;
11648 return isl_space_can_zip(map
->dim
);
11651 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11652 * (A -> C) -> (B -> D).
11654 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11663 if (!isl_basic_map_can_zip(bmap
))
11664 isl_die(bmap
->ctx
, isl_error_invalid
,
11665 "basic map cannot be zipped", goto error
);
11666 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11667 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11668 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11669 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11670 bmap
= isl_basic_map_cow(bmap
);
11671 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11674 bmap
->dim
= isl_space_zip(bmap
->dim
);
11677 bmap
= isl_basic_map_mark_final(bmap
);
11680 isl_basic_map_free(bmap
);
11684 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11685 * (A -> C) -> (B -> D).
11687 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11694 if (!isl_map_can_zip(map
))
11695 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11698 map
= isl_map_cow(map
);
11702 for (i
= 0; i
< map
->n
; ++i
) {
11703 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11708 map
->dim
= isl_space_zip(map
->dim
);
11718 /* Can we apply isl_basic_map_curry to "bmap"?
11719 * That is, does it have a nested relation in its domain?
11721 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11724 return isl_bool_error
;
11726 return isl_space_can_curry(bmap
->dim
);
11729 /* Can we apply isl_map_curry to "map"?
11730 * That is, does it have a nested relation in its domain?
11732 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11735 return isl_bool_error
;
11737 return isl_space_can_curry(map
->dim
);
11740 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11743 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11749 if (!isl_basic_map_can_curry(bmap
))
11750 isl_die(bmap
->ctx
, isl_error_invalid
,
11751 "basic map cannot be curried", goto error
);
11752 bmap
= isl_basic_map_cow(bmap
);
11755 bmap
->dim
= isl_space_curry(bmap
->dim
);
11758 bmap
= isl_basic_map_mark_final(bmap
);
11761 isl_basic_map_free(bmap
);
11765 /* Given a map (A -> B) -> C, return the corresponding map
11768 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11770 return isl_map_change_space(map
, &isl_map_can_curry
,
11771 "map cannot be curried", &isl_space_curry
);
11774 /* Can isl_map_range_curry be applied to "map"?
11775 * That is, does it have a nested relation in its range,
11776 * the domain of which is itself a nested relation?
11778 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11781 return isl_bool_error
;
11783 return isl_space_can_range_curry(map
->dim
);
11786 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11787 * A -> (B -> (C -> D)).
11789 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11791 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11792 "map range cannot be curried",
11793 &isl_space_range_curry
);
11796 /* Can we apply isl_basic_map_uncurry to "bmap"?
11797 * That is, does it have a nested relation in its domain?
11799 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11802 return isl_bool_error
;
11804 return isl_space_can_uncurry(bmap
->dim
);
11807 /* Can we apply isl_map_uncurry to "map"?
11808 * That is, does it have a nested relation in its domain?
11810 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11813 return isl_bool_error
;
11815 return isl_space_can_uncurry(map
->dim
);
11818 /* Given a basic map A -> (B -> C), return the corresponding basic map
11821 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11827 if (!isl_basic_map_can_uncurry(bmap
))
11828 isl_die(bmap
->ctx
, isl_error_invalid
,
11829 "basic map cannot be uncurried",
11830 return isl_basic_map_free(bmap
));
11831 bmap
= isl_basic_map_cow(bmap
);
11834 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11836 return isl_basic_map_free(bmap
);
11837 bmap
= isl_basic_map_mark_final(bmap
);
11841 /* Given a map A -> (B -> C), return the corresponding map
11844 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11846 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11847 "map cannot be uncurried", &isl_space_uncurry
);
11850 /* Construct a basic map mapping the domain of the affine expression
11851 * to a one-dimensional range prescribed by the affine expression.
11853 * A NaN affine expression cannot be converted to a basic map.
11855 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11860 isl_local_space
*ls
;
11861 isl_basic_map
*bmap
= NULL
;
11865 is_nan
= isl_aff_is_nan(aff
);
11869 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11870 "cannot convert NaN", goto error
);
11872 ls
= isl_aff_get_local_space(aff
);
11873 bmap
= isl_basic_map_from_local_space(ls
);
11874 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11875 k
= isl_basic_map_alloc_equality(bmap
);
11879 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11880 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11881 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11882 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11883 aff
->v
->size
- (pos
+ 1));
11886 bmap
= isl_basic_map_finalize(bmap
);
11890 isl_basic_map_free(bmap
);
11894 /* Construct a map mapping the domain of the affine expression
11895 * to a one-dimensional range prescribed by the affine expression.
11897 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11899 isl_basic_map
*bmap
;
11901 bmap
= isl_basic_map_from_aff(aff
);
11902 return isl_map_from_basic_map(bmap
);
11905 /* Construct a basic map mapping the domain the multi-affine expression
11906 * to its range, with each dimension in the range equated to the
11907 * corresponding affine expression.
11909 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11910 __isl_take isl_multi_aff
*maff
)
11914 isl_basic_map
*bmap
;
11919 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11920 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11921 "invalid space", goto error
);
11923 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11924 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11926 for (i
= 0; i
< maff
->n
; ++i
) {
11928 isl_basic_map
*bmap_i
;
11930 aff
= isl_aff_copy(maff
->p
[i
]);
11931 bmap_i
= isl_basic_map_from_aff(aff
);
11933 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11936 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11938 isl_multi_aff_free(maff
);
11941 isl_multi_aff_free(maff
);
11945 /* Construct a map mapping the domain the multi-affine expression
11946 * to its range, with each dimension in the range equated to the
11947 * corresponding affine expression.
11949 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11951 isl_basic_map
*bmap
;
11953 bmap
= isl_basic_map_from_multi_aff(maff
);
11954 return isl_map_from_basic_map(bmap
);
11957 /* Construct a basic map mapping a domain in the given space to
11958 * to an n-dimensional range, with n the number of elements in the list,
11959 * where each coordinate in the range is prescribed by the
11960 * corresponding affine expression.
11961 * The domains of all affine expressions in the list are assumed to match
11964 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11965 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11969 isl_basic_map
*bmap
;
11974 dim
= isl_space_from_domain(domain_dim
);
11975 bmap
= isl_basic_map_universe(dim
);
11977 for (i
= 0; i
< list
->n
; ++i
) {
11979 isl_basic_map
*bmap_i
;
11981 aff
= isl_aff_copy(list
->p
[i
]);
11982 bmap_i
= isl_basic_map_from_aff(aff
);
11984 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11987 isl_aff_list_free(list
);
11991 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11992 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11994 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11997 /* Construct a basic map where the given dimensions are equal to each other.
11999 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12000 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12002 isl_basic_map
*bmap
= NULL
;
12008 if (pos1
>= isl_space_dim(space
, type1
))
12009 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12010 "index out of bounds", goto error
);
12011 if (pos2
>= isl_space_dim(space
, type2
))
12012 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12013 "index out of bounds", goto error
);
12015 if (type1
== type2
&& pos1
== pos2
)
12016 return isl_basic_map_universe(space
);
12018 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12019 i
= isl_basic_map_alloc_equality(bmap
);
12022 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12023 pos1
+= isl_basic_map_offset(bmap
, type1
);
12024 pos2
+= isl_basic_map_offset(bmap
, type2
);
12025 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12026 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12027 bmap
= isl_basic_map_finalize(bmap
);
12028 isl_space_free(space
);
12031 isl_space_free(space
);
12032 isl_basic_map_free(bmap
);
12036 /* Add a constraint imposing that the given two dimensions are equal.
12038 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12039 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12043 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12045 bmap
= isl_basic_map_intersect(bmap
, eq
);
12050 /* Add a constraint imposing that the given two dimensions are equal.
12052 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12053 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12055 isl_basic_map
*bmap
;
12057 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12059 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12064 /* Add a constraint imposing that the given two dimensions have opposite values.
12066 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12067 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12069 isl_basic_map
*bmap
= NULL
;
12075 if (pos1
>= isl_map_dim(map
, type1
))
12076 isl_die(map
->ctx
, isl_error_invalid
,
12077 "index out of bounds", goto error
);
12078 if (pos2
>= isl_map_dim(map
, type2
))
12079 isl_die(map
->ctx
, isl_error_invalid
,
12080 "index out of bounds", goto error
);
12082 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12083 i
= isl_basic_map_alloc_equality(bmap
);
12086 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12087 pos1
+= isl_basic_map_offset(bmap
, type1
);
12088 pos2
+= isl_basic_map_offset(bmap
, type2
);
12089 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12090 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12091 bmap
= isl_basic_map_finalize(bmap
);
12093 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12097 isl_basic_map_free(bmap
);
12102 /* Construct a constraint imposing that the value of the first dimension is
12103 * greater than or equal to that of the second.
12105 static __isl_give isl_constraint
*constraint_order_ge(
12106 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12107 enum isl_dim_type type2
, int pos2
)
12114 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12116 if (pos1
>= isl_constraint_dim(c
, type1
))
12117 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12118 "index out of bounds", return isl_constraint_free(c
));
12119 if (pos2
>= isl_constraint_dim(c
, type2
))
12120 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12121 "index out of bounds", return isl_constraint_free(c
));
12123 if (type1
== type2
&& pos1
== pos2
)
12126 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12127 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12132 /* Add a constraint imposing that the value of the first dimension is
12133 * greater than or equal to that of the second.
12135 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12136 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12141 if (type1
== type2
&& pos1
== pos2
)
12143 space
= isl_basic_map_get_space(bmap
);
12144 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12145 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12150 /* Add a constraint imposing that the value of the first dimension is
12151 * greater than or equal to that of the second.
12153 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12154 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12159 if (type1
== type2
&& pos1
== pos2
)
12161 space
= isl_map_get_space(map
);
12162 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12163 map
= isl_map_add_constraint(map
, c
);
12168 /* Add a constraint imposing that the value of the first dimension is
12169 * less than or equal to that of the second.
12171 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12172 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12174 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12177 /* Construct a basic map where the value of the first dimension is
12178 * greater than that of the second.
12180 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12181 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12183 isl_basic_map
*bmap
= NULL
;
12189 if (pos1
>= isl_space_dim(space
, type1
))
12190 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12191 "index out of bounds", goto error
);
12192 if (pos2
>= isl_space_dim(space
, type2
))
12193 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12194 "index out of bounds", goto error
);
12196 if (type1
== type2
&& pos1
== pos2
)
12197 return isl_basic_map_empty(space
);
12199 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12200 i
= isl_basic_map_alloc_inequality(bmap
);
12202 return isl_basic_map_free(bmap
);
12203 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12204 pos1
+= isl_basic_map_offset(bmap
, type1
);
12205 pos2
+= isl_basic_map_offset(bmap
, type2
);
12206 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12207 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12208 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12209 bmap
= isl_basic_map_finalize(bmap
);
12213 isl_space_free(space
);
12214 isl_basic_map_free(bmap
);
12218 /* Add a constraint imposing that the value of the first dimension is
12219 * greater than that of the second.
12221 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12222 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12226 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12228 bmap
= isl_basic_map_intersect(bmap
, gt
);
12233 /* Add a constraint imposing that the value of the first dimension is
12234 * greater than that of the second.
12236 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12237 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12239 isl_basic_map
*bmap
;
12241 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12243 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12248 /* Add a constraint imposing that the value of the first dimension is
12249 * smaller than that of the second.
12251 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12252 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12254 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12257 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12261 isl_local_space
*ls
;
12266 if (!isl_basic_map_divs_known(bmap
))
12267 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12268 "some divs are unknown", return NULL
);
12270 ls
= isl_basic_map_get_local_space(bmap
);
12271 div
= isl_local_space_get_div(ls
, pos
);
12272 isl_local_space_free(ls
);
12277 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12280 return isl_basic_map_get_div(bset
, pos
);
12283 /* Plug in "subs" for dimension "type", "pos" of "bset".
12285 * Let i be the dimension to replace and let "subs" be of the form
12289 * Any integer division with a non-zero coefficient for i,
12291 * floor((a i + g)/m)
12295 * floor((a f + d g)/(m d))
12297 * Constraints of the form
12305 * We currently require that "subs" is an integral expression.
12306 * Handling rational expressions may require us to add stride constraints
12307 * as we do in isl_basic_set_preimage_multi_aff.
12309 __isl_give isl_basic_set
*isl_basic_set_substitute(
12310 __isl_take isl_basic_set
*bset
,
12311 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12317 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12320 bset
= isl_basic_set_cow(bset
);
12321 if (!bset
|| !subs
)
12324 ctx
= isl_basic_set_get_ctx(bset
);
12325 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12326 isl_die(ctx
, isl_error_invalid
,
12327 "spaces don't match", goto error
);
12328 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12329 isl_die(ctx
, isl_error_unsupported
,
12330 "cannot handle divs yet", goto error
);
12331 if (!isl_int_is_one(subs
->v
->el
[0]))
12332 isl_die(ctx
, isl_error_invalid
,
12333 "can only substitute integer expressions", goto error
);
12335 pos
+= isl_basic_set_offset(bset
, type
);
12339 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12340 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12342 isl_int_set(v
, bset
->eq
[i
][pos
]);
12343 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12344 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12345 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12348 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12349 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12351 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12352 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12353 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12354 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12357 for (i
= 0; i
< bset
->n_div
; ++i
) {
12358 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12360 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12361 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12362 isl_seq_combine(bset
->div
[i
] + 1,
12363 subs
->v
->el
[0], bset
->div
[i
] + 1,
12364 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12365 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12370 bset
= isl_basic_set_simplify(bset
);
12371 return isl_basic_set_finalize(bset
);
12373 isl_basic_set_free(bset
);
12377 /* Plug in "subs" for dimension "type", "pos" of "set".
12379 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12380 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12384 if (set
&& isl_set_plain_is_empty(set
))
12387 set
= isl_set_cow(set
);
12391 for (i
= set
->n
- 1; i
>= 0; --i
) {
12392 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12393 if (remove_if_empty(set
, i
) < 0)
12403 /* Check if the range of "ma" is compatible with the domain or range
12404 * (depending on "type") of "bmap".
12405 * Return -1 if anything is wrong.
12407 static int check_basic_map_compatible_range_multi_aff(
12408 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12409 __isl_keep isl_multi_aff
*ma
)
12412 isl_space
*ma_space
;
12414 ma_space
= isl_multi_aff_get_space(ma
);
12416 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12420 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12421 "parameters don't match", goto error
);
12422 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12426 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12427 "spaces don't match", goto error
);
12429 isl_space_free(ma_space
);
12432 isl_space_free(ma_space
);
12436 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12437 * coefficients before the transformed range of dimensions,
12438 * the "n_after" coefficients after the transformed range of dimensions
12439 * and the coefficients of the other divs in "bmap".
12441 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12442 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12447 isl_local_space
*ls
;
12452 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12456 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12457 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12458 for (i
= 0; i
< n_div
; ++i
) {
12459 int o_bmap
= 0, o_ls
= 0;
12461 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12462 o_bmap
+= 1 + 1 + n_param
;
12463 o_ls
+= 1 + 1 + n_param
;
12464 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12465 o_bmap
+= n_before
;
12466 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12467 ls
->div
->row
[i
] + o_ls
, n_set
);
12470 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12472 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12473 ls
->div
->row
[i
] + o_ls
, n_div
);
12476 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12477 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12481 isl_local_space_free(ls
);
12484 isl_local_space_free(ls
);
12488 /* How many stride constraints does "ma" enforce?
12489 * That is, how many of the affine expressions have a denominator
12490 * different from one?
12492 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12497 for (i
= 0; i
< ma
->n
; ++i
)
12498 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12504 /* For each affine expression in ma of the form
12506 * x_i = (f_i y + h_i)/m_i
12508 * with m_i different from one, add a constraint to "bmap"
12511 * f_i y + h_i = m_i alpha_i
12513 * with alpha_i an additional existentially quantified variable.
12515 * The input variables of "ma" correspond to a subset of the variables
12516 * of "bmap". There are "n_before" variables in "bmap" before this
12517 * subset and "n_after" variables after this subset.
12518 * The integer divisions of the affine expressions in "ma" are assumed
12519 * to have been aligned. There are "n_div_ma" of them and
12520 * they appear first in "bmap", straight after the "n_after" variables.
12522 static __isl_give isl_basic_map
*add_ma_strides(
12523 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12524 int n_before
, int n_after
, int n_div_ma
)
12532 total
= isl_basic_map_total_dim(bmap
);
12533 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12534 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12535 for (i
= 0; i
< ma
->n
; ++i
) {
12536 int o_bmap
= 0, o_ma
= 1;
12538 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12540 div
= isl_basic_map_alloc_div(bmap
);
12541 k
= isl_basic_map_alloc_equality(bmap
);
12542 if (div
< 0 || k
< 0)
12544 isl_int_set_si(bmap
->div
[div
][0], 0);
12545 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12546 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12547 o_bmap
+= 1 + n_param
;
12548 o_ma
+= 1 + n_param
;
12549 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12550 o_bmap
+= n_before
;
12551 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12552 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12555 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12557 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12558 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12559 o_bmap
+= n_div_ma
;
12561 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12562 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12568 isl_basic_map_free(bmap
);
12572 /* Replace the domain or range space (depending on "type) of "space" by "set".
12574 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12575 enum isl_dim_type type
, __isl_take isl_space
*set
)
12577 if (type
== isl_dim_in
) {
12578 space
= isl_space_range(space
);
12579 space
= isl_space_map_from_domain_and_range(set
, space
);
12581 space
= isl_space_domain(space
);
12582 space
= isl_space_map_from_domain_and_range(space
, set
);
12588 /* Compute the preimage of the domain or range (depending on "type")
12589 * of "bmap" under the function represented by "ma".
12590 * In other words, plug in "ma" in the domain or range of "bmap".
12591 * The result is a basic map that lives in the same space as "bmap"
12592 * except that the domain or range has been replaced by
12593 * the domain space of "ma".
12595 * If bmap is represented by
12597 * A(p) + S u + B x + T v + C(divs) >= 0,
12599 * where u and x are input and output dimensions if type == isl_dim_out
12600 * while x and v are input and output dimensions if type == isl_dim_in,
12601 * and ma is represented by
12603 * x = D(p) + F(y) + G(divs')
12605 * then the result is
12607 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12609 * The divs in the input set are similarly adjusted.
12612 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12616 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12617 * B_i G(divs') + c_i(divs))/n_i)
12619 * If bmap is not a rational map and if F(y) involves any denominators
12621 * x_i = (f_i y + h_i)/m_i
12623 * then additional constraints are added to ensure that we only
12624 * map back integer points. That is we enforce
12626 * f_i y + h_i = m_i alpha_i
12628 * with alpha_i an additional existentially quantified variable.
12630 * We first copy over the divs from "ma".
12631 * Then we add the modified constraints and divs from "bmap".
12632 * Finally, we add the stride constraints, if needed.
12634 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12635 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12636 __isl_take isl_multi_aff
*ma
)
12640 isl_basic_map
*res
= NULL
;
12641 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12642 isl_int f
, c1
, c2
, g
;
12643 int rational
, strides
;
12650 ma
= isl_multi_aff_align_divs(ma
);
12653 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12656 if (type
== isl_dim_in
) {
12658 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12660 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12663 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12664 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12666 space
= isl_multi_aff_get_domain_space(ma
);
12667 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12668 rational
= isl_basic_map_is_rational(bmap
);
12669 strides
= rational
? 0 : multi_aff_strides(ma
);
12670 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12671 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12673 res
= isl_basic_map_set_rational(res
);
12675 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12676 if (isl_basic_map_alloc_div(res
) < 0)
12679 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12682 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12683 k
= isl_basic_map_alloc_equality(res
);
12686 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12687 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12690 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12691 k
= isl_basic_map_alloc_inequality(res
);
12694 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12695 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12698 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12699 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12700 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12703 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12704 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12709 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12715 isl_basic_map_free(bmap
);
12716 isl_multi_aff_free(ma
);
12717 res
= isl_basic_set_simplify(res
);
12718 return isl_basic_map_finalize(res
);
12724 isl_basic_map_free(bmap
);
12725 isl_multi_aff_free(ma
);
12726 isl_basic_map_free(res
);
12730 /* Compute the preimage of "bset" under the function represented by "ma".
12731 * In other words, plug in "ma" in "bset". The result is a basic set
12732 * that lives in the domain space of "ma".
12734 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12735 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12737 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12740 /* Compute the preimage of the domain of "bmap" under the function
12741 * represented by "ma".
12742 * In other words, plug in "ma" in the domain of "bmap".
12743 * The result is a basic map that lives in the same space as "bmap"
12744 * except that the domain has been replaced by the domain space of "ma".
12746 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12747 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12749 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12752 /* Compute the preimage of the range of "bmap" under the function
12753 * represented by "ma".
12754 * In other words, plug in "ma" in the range of "bmap".
12755 * The result is a basic map that lives in the same space as "bmap"
12756 * except that the range has been replaced by the domain space of "ma".
12758 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12759 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12761 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12764 /* Check if the range of "ma" is compatible with the domain or range
12765 * (depending on "type") of "map".
12766 * Return -1 if anything is wrong.
12768 static int check_map_compatible_range_multi_aff(
12769 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12770 __isl_keep isl_multi_aff
*ma
)
12773 isl_space
*ma_space
;
12775 ma_space
= isl_multi_aff_get_space(ma
);
12776 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12777 isl_space_free(ma_space
);
12779 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12780 "spaces don't match", return -1);
12784 /* Compute the preimage of the domain or range (depending on "type")
12785 * of "map" under the function represented by "ma".
12786 * In other words, plug in "ma" in the domain or range of "map".
12787 * The result is a map that lives in the same space as "map"
12788 * except that the domain or range has been replaced by
12789 * the domain space of "ma".
12791 * The parameters are assumed to have been aligned.
12793 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12794 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12799 map
= isl_map_cow(map
);
12800 ma
= isl_multi_aff_align_divs(ma
);
12803 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12806 for (i
= 0; i
< map
->n
; ++i
) {
12807 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12808 isl_multi_aff_copy(ma
));
12813 space
= isl_multi_aff_get_domain_space(ma
);
12814 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12816 isl_space_free(map
->dim
);
12821 isl_multi_aff_free(ma
);
12823 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12824 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12827 isl_multi_aff_free(ma
);
12832 /* Compute the preimage of the domain or range (depending on "type")
12833 * of "map" under the function represented by "ma".
12834 * In other words, plug in "ma" in the domain or range of "map".
12835 * The result is a map that lives in the same space as "map"
12836 * except that the domain or range has been replaced by
12837 * the domain space of "ma".
12839 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12840 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12845 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12846 return map_preimage_multi_aff(map
, type
, ma
);
12848 if (!isl_space_has_named_params(map
->dim
) ||
12849 !isl_space_has_named_params(ma
->space
))
12850 isl_die(map
->ctx
, isl_error_invalid
,
12851 "unaligned unnamed parameters", goto error
);
12852 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12853 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12855 return map_preimage_multi_aff(map
, type
, ma
);
12857 isl_multi_aff_free(ma
);
12858 return isl_map_free(map
);
12861 /* Compute the preimage of "set" under the function represented by "ma".
12862 * In other words, plug in "ma" in "set". The result is a set
12863 * that lives in the domain space of "ma".
12865 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12866 __isl_take isl_multi_aff
*ma
)
12868 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12871 /* Compute the preimage of the domain of "map" under the function
12872 * represented by "ma".
12873 * In other words, plug in "ma" in the domain of "map".
12874 * The result is a map that lives in the same space as "map"
12875 * except that the domain has been replaced by the domain space of "ma".
12877 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12878 __isl_take isl_multi_aff
*ma
)
12880 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12883 /* Compute the preimage of the range of "map" under the function
12884 * represented by "ma".
12885 * In other words, plug in "ma" in the range of "map".
12886 * The result is a map that lives in the same space as "map"
12887 * except that the range has been replaced by the domain space of "ma".
12889 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12890 __isl_take isl_multi_aff
*ma
)
12892 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12895 /* Compute the preimage of "map" under the function represented by "pma".
12896 * In other words, plug in "pma" in the domain or range of "map".
12897 * The result is a map that lives in the same space as "map",
12898 * except that the space of type "type" has been replaced by
12899 * the domain space of "pma".
12901 * The parameters of "map" and "pma" are assumed to have been aligned.
12903 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12904 __isl_take isl_map
*map
, enum isl_dim_type type
,
12905 __isl_take isl_pw_multi_aff
*pma
)
12914 isl_pw_multi_aff_free(pma
);
12915 res
= isl_map_empty(isl_map_get_space(map
));
12920 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12921 isl_multi_aff_copy(pma
->p
[0].maff
));
12922 if (type
== isl_dim_in
)
12923 res
= isl_map_intersect_domain(res
,
12924 isl_map_copy(pma
->p
[0].set
));
12926 res
= isl_map_intersect_range(res
,
12927 isl_map_copy(pma
->p
[0].set
));
12929 for (i
= 1; i
< pma
->n
; ++i
) {
12932 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12933 isl_multi_aff_copy(pma
->p
[i
].maff
));
12934 if (type
== isl_dim_in
)
12935 res_i
= isl_map_intersect_domain(res_i
,
12936 isl_map_copy(pma
->p
[i
].set
));
12938 res_i
= isl_map_intersect_range(res_i
,
12939 isl_map_copy(pma
->p
[i
].set
));
12940 res
= isl_map_union(res
, res_i
);
12943 isl_pw_multi_aff_free(pma
);
12947 isl_pw_multi_aff_free(pma
);
12952 /* Compute the preimage of "map" under the function represented by "pma".
12953 * In other words, plug in "pma" in the domain or range of "map".
12954 * The result is a map that lives in the same space as "map",
12955 * except that the space of type "type" has been replaced by
12956 * the domain space of "pma".
12958 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12959 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12964 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12965 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12967 if (!isl_space_has_named_params(map
->dim
) ||
12968 !isl_space_has_named_params(pma
->dim
))
12969 isl_die(map
->ctx
, isl_error_invalid
,
12970 "unaligned unnamed parameters", goto error
);
12971 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12972 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12974 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12976 isl_pw_multi_aff_free(pma
);
12977 return isl_map_free(map
);
12980 /* Compute the preimage of "set" under the function represented by "pma".
12981 * In other words, plug in "pma" in "set". The result is a set
12982 * that lives in the domain space of "pma".
12984 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12985 __isl_take isl_pw_multi_aff
*pma
)
12987 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12990 /* Compute the preimage of the domain of "map" under the function
12991 * represented by "pma".
12992 * In other words, plug in "pma" in the domain of "map".
12993 * The result is a map that lives in the same space as "map",
12994 * except that domain space has been replaced by the domain space of "pma".
12996 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12997 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12999 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13002 /* Compute the preimage of the range of "map" under the function
13003 * represented by "pma".
13004 * In other words, plug in "pma" in the range of "map".
13005 * The result is a map that lives in the same space as "map",
13006 * except that range space has been replaced by the domain space of "pma".
13008 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13009 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13011 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13014 /* Compute the preimage of "map" under the function represented by "mpa".
13015 * In other words, plug in "mpa" in the domain or range of "map".
13016 * The result is a map that lives in the same space as "map",
13017 * except that the space of type "type" has been replaced by
13018 * the domain space of "mpa".
13020 * If the map does not involve any constraints that refer to the
13021 * dimensions of the substituted space, then the only possible
13022 * effect of "mpa" on the map is to map the space to a different space.
13023 * We create a separate isl_multi_aff to effectuate this change
13024 * in order to avoid spurious splitting of the map along the pieces
13027 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13028 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13031 isl_pw_multi_aff
*pma
;
13036 n
= isl_map_dim(map
, type
);
13037 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13041 space
= isl_multi_pw_aff_get_space(mpa
);
13042 isl_multi_pw_aff_free(mpa
);
13043 ma
= isl_multi_aff_zero(space
);
13044 return isl_map_preimage_multi_aff(map
, type
, ma
);
13047 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13048 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13051 isl_multi_pw_aff_free(mpa
);
13055 /* Compute the preimage of "map" under the function represented by "mpa".
13056 * In other words, plug in "mpa" in the domain "map".
13057 * The result is a map that lives in the same space as "map",
13058 * except that domain space has been replaced by the domain space of "mpa".
13060 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13061 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13063 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13066 /* Compute the preimage of "set" by the function represented by "mpa".
13067 * In other words, plug in "mpa" in "set".
13069 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13070 __isl_take isl_multi_pw_aff
*mpa
)
13072 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);