2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return dim
->nparam
;
50 case isl_dim_in
: return dim
->n_in
;
51 case isl_dim_out
: return dim
->n_out
;
52 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
57 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
60 case isl_dim_param
: return 1;
61 case isl_dim_in
: return 1 + dim
->nparam
;
62 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
67 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
68 enum isl_dim_type type
)
73 case isl_dim_cst
: return 1;
76 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
77 case isl_dim_div
: return bmap
->n_div
;
78 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
83 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
85 return map
? n(map
->dim
, type
) : 0;
88 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
90 return set
? n(set
->dim
, type
) : 0;
93 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
94 enum isl_dim_type type
)
96 isl_space
*dim
= bmap
->dim
;
98 case isl_dim_cst
: return 0;
99 case isl_dim_param
: return 1;
100 case isl_dim_in
: return 1 + dim
->nparam
;
101 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
102 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
107 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
108 enum isl_dim_type type
)
110 return isl_basic_map_offset(bset
, type
);
113 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
115 return pos(map
->dim
, type
);
118 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
119 enum isl_dim_type type
)
121 return isl_basic_map_dim(bset
, type
);
124 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
126 return isl_basic_set_dim(bset
, isl_dim_set
);
129 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
131 return isl_basic_set_dim(bset
, isl_dim_param
);
134 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
138 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
141 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
143 return isl_set_dim(set
, isl_dim_set
);
146 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
148 return isl_set_dim(set
, isl_dim_param
);
151 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
153 return bmap
? bmap
->dim
->n_in
: 0;
156 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
158 return bmap
? bmap
->dim
->n_out
: 0;
161 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
163 return bmap
? bmap
->dim
->nparam
: 0;
166 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
168 return bmap
? bmap
->n_div
: 0;
171 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
173 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
176 unsigned isl_map_n_in(const struct isl_map
*map
)
178 return map
? map
->dim
->n_in
: 0;
181 unsigned isl_map_n_out(const struct isl_map
*map
)
183 return map
? map
->dim
->n_out
: 0;
186 unsigned isl_map_n_param(const struct isl_map
*map
)
188 return map
? map
->dim
->nparam
: 0;
191 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
196 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
199 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
200 set
->dim
, isl_dim_set
);
203 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
204 struct isl_basic_set
*bset
)
209 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
212 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
213 bset
->dim
, isl_dim_set
);
216 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
221 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
224 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
225 set
->dim
, isl_dim_set
);
228 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
229 struct isl_basic_set
*bset
)
234 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
237 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
238 bset
->dim
, isl_dim_set
);
241 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
243 return bmap
? bmap
->ctx
: NULL
;
246 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
248 return bset
? bset
->ctx
: NULL
;
251 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
253 return map
? map
->ctx
: NULL
;
256 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
258 return set
? set
->ctx
: NULL
;
261 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
265 return isl_space_copy(bmap
->dim
);
268 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
272 return isl_space_copy(bset
->dim
);
275 /* Extract the divs in "bmap" as a matrix.
277 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
288 ctx
= isl_basic_map_get_ctx(bmap
);
289 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
290 cols
= 1 + 1 + total
+ bmap
->n_div
;
291 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
295 for (i
= 0; i
< bmap
->n_div
; ++i
)
296 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
301 /* Extract the divs in "bset" as a matrix.
303 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
305 return isl_basic_map_get_divs(bset
);
308 __isl_give isl_local_space
*isl_basic_map_get_local_space(
309 __isl_keep isl_basic_map
*bmap
)
316 div
= isl_basic_map_get_divs(bmap
);
317 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
320 __isl_give isl_local_space
*isl_basic_set_get_local_space(
321 __isl_keep isl_basic_set
*bset
)
323 return isl_basic_map_get_local_space(bset
);
326 /* For each known div d = floor(f/m), add the constraints
329 * -(f-(n-1)) + m d >= 0
331 * Do not finalize the result.
333 static __isl_give isl_basic_map
*add_known_div_constraints(
334 __isl_take isl_basic_map
*bmap
)
341 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
344 bmap
= isl_basic_map_cow(bmap
);
345 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
348 for (i
= 0; i
< n_div
; ++i
) {
349 if (isl_int_is_zero(bmap
->div
[i
][0]))
351 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
352 return isl_basic_map_free(bmap
);
358 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
359 __isl_take isl_local_space
*ls
)
368 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
369 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
370 n_div
, 0, 2 * n_div
);
372 for (i
= 0; i
< n_div
; ++i
)
373 if (isl_basic_map_alloc_div(bmap
) < 0)
376 for (i
= 0; i
< n_div
; ++i
)
377 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
378 bmap
= add_known_div_constraints(bmap
);
380 isl_local_space_free(ls
);
383 isl_local_space_free(ls
);
384 isl_basic_map_free(bmap
);
388 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
389 __isl_take isl_local_space
*ls
)
391 return isl_basic_map_from_local_space(ls
);
394 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
398 return isl_space_copy(map
->dim
);
401 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
405 return isl_space_copy(set
->dim
);
408 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
409 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
411 bmap
= isl_basic_map_cow(bmap
);
414 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
417 bmap
= isl_basic_map_finalize(bmap
);
420 isl_basic_map_free(bmap
);
424 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
425 __isl_take isl_basic_set
*bset
, const char *s
)
427 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
430 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
431 enum isl_dim_type type
)
433 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
436 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
437 enum isl_dim_type type
, const char *s
)
441 map
= isl_map_cow(map
);
445 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
449 for (i
= 0; i
< map
->n
; ++i
) {
450 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
461 /* Replace the identifier of the tuple of type "type" by "id".
463 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
464 __isl_take isl_basic_map
*bmap
,
465 enum isl_dim_type type
, __isl_take isl_id
*id
)
467 bmap
= isl_basic_map_cow(bmap
);
470 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
472 return isl_basic_map_free(bmap
);
473 bmap
= isl_basic_map_finalize(bmap
);
480 /* Replace the identifier of the tuple by "id".
482 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
483 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
485 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
488 /* Does the input or output tuple have a name?
490 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
492 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
495 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
496 enum isl_dim_type type
)
498 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
501 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
504 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
507 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
508 enum isl_dim_type type
, __isl_take isl_id
*id
)
510 map
= isl_map_cow(map
);
514 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
516 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
522 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
523 __isl_take isl_id
*id
)
525 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
528 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
529 enum isl_dim_type type
)
531 map
= isl_map_cow(map
);
535 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
537 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
540 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
542 return isl_map_reset_tuple_id(set
, isl_dim_set
);
545 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
547 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
550 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
551 enum isl_dim_type type
)
553 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
556 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
558 return isl_map_has_tuple_id(set
, isl_dim_set
);
561 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
563 return isl_map_get_tuple_id(set
, isl_dim_set
);
566 /* Does the set tuple have a name?
568 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
571 return isl_bool_error
;
572 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
576 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
578 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
581 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
583 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
586 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
587 enum isl_dim_type type
, unsigned pos
)
589 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
592 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
593 enum isl_dim_type type
, unsigned pos
)
595 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
598 /* Does the given dimension have a name?
600 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
601 enum isl_dim_type type
, unsigned pos
)
604 return isl_bool_error
;
605 return isl_space_has_dim_name(map
->dim
, type
, pos
);
608 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
609 enum isl_dim_type type
, unsigned pos
)
611 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
614 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
615 enum isl_dim_type type
, unsigned pos
)
617 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
620 /* Does the given dimension have a name?
622 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
623 enum isl_dim_type type
, unsigned pos
)
626 return isl_bool_error
;
627 return isl_space_has_dim_name(set
->dim
, type
, pos
);
630 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
631 __isl_take isl_basic_map
*bmap
,
632 enum isl_dim_type type
, unsigned pos
, const char *s
)
634 bmap
= isl_basic_map_cow(bmap
);
637 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
640 return isl_basic_map_finalize(bmap
);
642 isl_basic_map_free(bmap
);
646 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
647 enum isl_dim_type type
, unsigned pos
, const char *s
)
651 map
= isl_map_cow(map
);
655 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
659 for (i
= 0; i
< map
->n
; ++i
) {
660 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
671 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
672 __isl_take isl_basic_set
*bset
,
673 enum isl_dim_type type
, unsigned pos
, const char *s
)
675 return (isl_basic_set
*)isl_basic_map_set_dim_name(
676 (isl_basic_map
*)bset
, type
, pos
, s
);
679 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
680 enum isl_dim_type type
, unsigned pos
, const char *s
)
682 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
685 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
686 enum isl_dim_type type
, unsigned pos
)
689 return isl_bool_error
;
690 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
693 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
694 enum isl_dim_type type
, unsigned pos
)
696 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
699 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
700 enum isl_dim_type type
, unsigned pos
)
702 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
705 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
706 enum isl_dim_type type
, unsigned pos
)
708 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
711 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
712 enum isl_dim_type type
, unsigned pos
)
714 return isl_map_has_dim_id(set
, type
, pos
);
717 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
718 enum isl_dim_type type
, unsigned pos
)
720 return isl_map_get_dim_id(set
, type
, pos
);
723 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
724 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
726 map
= isl_map_cow(map
);
730 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
732 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
738 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
739 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
741 return isl_map_set_dim_id(set
, type
, pos
, id
);
744 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
745 __isl_keep isl_id
*id
)
749 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
752 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
753 __isl_keep isl_id
*id
)
755 return isl_map_find_dim_by_id(set
, type
, id
);
758 /* Return the position of the dimension of the given type and name
760 * Return -1 if no such dimension can be found.
762 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
763 enum isl_dim_type type
, const char *name
)
767 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
770 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
775 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
778 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
781 return isl_map_find_dim_by_name(set
, type
, name
);
784 /* Reset the user pointer on all identifiers of parameters and tuples
785 * of the space of "map".
787 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
791 space
= isl_map_get_space(map
);
792 space
= isl_space_reset_user(space
);
793 map
= isl_map_reset_space(map
, space
);
798 /* Reset the user pointer on all identifiers of parameters and tuples
799 * of the space of "set".
801 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
803 return isl_map_reset_user(set
);
806 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
810 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
813 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
815 return isl_basic_map_is_rational(bset
);
818 /* Does "bmap" contain any rational points?
820 * If "bmap" has an equality for each dimension, equating the dimension
821 * to an integer constant, then it has no rational points, even if it
822 * is marked as rational.
824 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
826 int has_rational
= 1;
831 if (isl_basic_map_plain_is_empty(bmap
))
833 if (!isl_basic_map_is_rational(bmap
))
835 bmap
= isl_basic_map_copy(bmap
);
836 bmap
= isl_basic_map_implicit_equalities(bmap
);
839 total
= isl_basic_map_total_dim(bmap
);
840 if (bmap
->n_eq
== total
) {
842 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
843 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
846 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
847 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
849 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
857 isl_basic_map_free(bmap
);
862 /* Does "map" contain any rational points?
864 int isl_map_has_rational(__isl_keep isl_map
*map
)
871 for (i
= 0; i
< map
->n
; ++i
) {
872 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
873 if (has_rational
< 0)
881 /* Does "set" contain any rational points?
883 int isl_set_has_rational(__isl_keep isl_set
*set
)
885 return isl_map_has_rational(set
);
888 /* Is this basic set a parameter domain?
890 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
894 return isl_space_is_params(bset
->dim
);
897 /* Is this set a parameter domain?
899 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
902 return isl_bool_error
;
903 return isl_space_is_params(set
->dim
);
906 /* Is this map actually a parameter domain?
907 * Users should never call this function. Outside of isl,
908 * a map can never be a parameter domain.
910 int isl_map_is_params(__isl_keep isl_map
*map
)
914 return isl_space_is_params(map
->dim
);
917 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
918 struct isl_basic_map
*bmap
, unsigned extra
,
919 unsigned n_eq
, unsigned n_ineq
)
922 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
927 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
928 if (isl_blk_is_error(bmap
->block
))
931 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
932 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
936 bmap
->block2
= isl_blk_empty();
939 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
940 if (isl_blk_is_error(bmap
->block2
))
943 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
948 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
949 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
951 for (i
= 0; i
< extra
; ++i
)
952 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
956 bmap
->c_size
= n_eq
+ n_ineq
;
957 bmap
->eq
= bmap
->ineq
+ n_ineq
;
966 isl_basic_map_free(bmap
);
970 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
971 unsigned nparam
, unsigned dim
, unsigned extra
,
972 unsigned n_eq
, unsigned n_ineq
)
974 struct isl_basic_map
*bmap
;
977 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
981 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
982 return (struct isl_basic_set
*)bmap
;
985 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
986 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
988 struct isl_basic_map
*bmap
;
991 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
992 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
993 return (struct isl_basic_set
*)bmap
;
999 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1000 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1002 struct isl_basic_map
*bmap
;
1006 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1011 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1013 isl_space_free(dim
);
1017 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1018 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1019 unsigned n_eq
, unsigned n_ineq
)
1021 struct isl_basic_map
*bmap
;
1024 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1028 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1032 static void dup_constraints(
1033 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1036 unsigned total
= isl_basic_map_total_dim(src
);
1038 for (i
= 0; i
< src
->n_eq
; ++i
) {
1039 int j
= isl_basic_map_alloc_equality(dst
);
1040 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1043 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1044 int j
= isl_basic_map_alloc_inequality(dst
);
1045 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1048 for (i
= 0; i
< src
->n_div
; ++i
) {
1049 int j
= isl_basic_map_alloc_div(dst
);
1050 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1052 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1055 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1057 struct isl_basic_map
*dup
;
1061 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1062 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1065 dup_constraints(dup
, bmap
);
1066 dup
->flags
= bmap
->flags
;
1067 dup
->sample
= isl_vec_copy(bmap
->sample
);
1071 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1073 struct isl_basic_map
*dup
;
1075 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1076 return (struct isl_basic_set
*)dup
;
1079 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1084 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1088 return isl_basic_set_dup(bset
);
1091 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1100 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1105 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1109 bmap
= isl_basic_map_dup(bmap
);
1111 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1115 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1124 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1129 if (--bmap
->ref
> 0)
1132 isl_ctx_deref(bmap
->ctx
);
1134 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1136 isl_blk_free(bmap
->ctx
, bmap
->block
);
1137 isl_vec_free(bmap
->sample
);
1138 isl_space_free(bmap
->dim
);
1144 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1146 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1149 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1151 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1154 __isl_give isl_map
*isl_map_align_params_map_map_and(
1155 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1156 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1157 __isl_take isl_map
*map2
))
1161 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1162 return fn(map1
, map2
);
1163 if (!isl_space_has_named_params(map1
->dim
) ||
1164 !isl_space_has_named_params(map2
->dim
))
1165 isl_die(map1
->ctx
, isl_error_invalid
,
1166 "unaligned unnamed parameters", goto error
);
1167 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1168 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1169 return fn(map1
, map2
);
1176 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1177 __isl_keep isl_map
*map2
,
1178 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1183 return isl_bool_error
;
1184 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1185 return fn(map1
, map2
);
1186 if (!isl_space_has_named_params(map1
->dim
) ||
1187 !isl_space_has_named_params(map2
->dim
))
1188 isl_die(map1
->ctx
, isl_error_invalid
,
1189 "unaligned unnamed parameters", return isl_bool_error
);
1190 map1
= isl_map_copy(map1
);
1191 map2
= isl_map_copy(map2
);
1192 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1193 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1200 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1202 struct isl_ctx
*ctx
;
1206 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1207 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1210 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1211 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1212 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1213 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1214 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1216 int j
= isl_basic_map_alloc_inequality(bmap
);
1220 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1221 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1228 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1229 bmap
->extra
- bmap
->n_div
);
1230 return bmap
->n_eq
++;
1233 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1235 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1238 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1242 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1247 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1249 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1252 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1257 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1259 if (pos
!= bmap
->n_eq
- 1) {
1261 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1262 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1268 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1270 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1273 /* Turn inequality "pos" of "bmap" into an equality.
1275 * In particular, we move the inequality in front of the equalities
1276 * and move the last inequality in the position of the moved inequality.
1277 * Note that isl_tab_make_equalities_explicit depends on this particular
1278 * change in the ordering of the constraints.
1280 void isl_basic_map_inequality_to_equality(
1281 struct isl_basic_map
*bmap
, unsigned pos
)
1285 t
= bmap
->ineq
[pos
];
1286 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1287 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1292 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1293 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1294 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1295 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1298 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1300 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1303 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1305 struct isl_ctx
*ctx
;
1309 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1310 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1311 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1312 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1313 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1314 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1315 1 + isl_basic_map_total_dim(bmap
),
1316 bmap
->extra
- bmap
->n_div
);
1317 return bmap
->n_ineq
++;
1320 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1322 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1325 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1329 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1334 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1336 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1339 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1344 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1346 if (pos
!= bmap
->n_ineq
- 1) {
1347 t
= bmap
->ineq
[pos
];
1348 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1349 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1350 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1356 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1358 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1361 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1366 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1369 k
= isl_basic_map_alloc_equality(bmap
);
1372 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1375 isl_basic_map_free(bmap
);
1379 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1382 return (isl_basic_set
*)
1383 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1386 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1391 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1394 k
= isl_basic_map_alloc_inequality(bmap
);
1397 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1400 isl_basic_map_free(bmap
);
1404 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1407 return (isl_basic_set
*)
1408 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1411 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1415 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1416 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1417 1 + 1 + isl_basic_map_total_dim(bmap
),
1418 bmap
->extra
- bmap
->n_div
);
1419 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1420 return bmap
->n_div
++;
1423 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1425 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1428 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1432 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1437 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1439 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1442 /* Copy constraint from src to dst, putting the vars of src at offset
1443 * dim_off in dst and the divs of src at offset div_off in dst.
1444 * If both sets are actually map, then dim_off applies to the input
1447 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1448 struct isl_basic_map
*src_map
, isl_int
*src
,
1449 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1451 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1452 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1453 unsigned src_in
= isl_basic_map_n_in(src_map
);
1454 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1455 unsigned src_out
= isl_basic_map_n_out(src_map
);
1456 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1457 isl_int_set(dst
[0], src
[0]);
1458 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1459 if (dst_nparam
> src_nparam
)
1460 isl_seq_clr(dst
+1+src_nparam
,
1461 dst_nparam
- src_nparam
);
1462 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1463 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1465 isl_min(dst_in
-in_off
, src_in
));
1466 if (dst_in
-in_off
> src_in
)
1467 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1468 dst_in
- in_off
- src_in
);
1469 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1470 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1471 src
+1+src_nparam
+src_in
,
1472 isl_min(dst_out
-out_off
, src_out
));
1473 if (dst_out
-out_off
> src_out
)
1474 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1475 dst_out
- out_off
- src_out
);
1476 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1477 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1478 src
+1+src_nparam
+src_in
+src_out
,
1479 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1480 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1481 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1482 div_off
+src_map
->n_div
,
1483 dst_map
->n_div
- div_off
- src_map
->n_div
);
1486 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1487 struct isl_basic_map
*src_map
, isl_int
*src
,
1488 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1490 isl_int_set(dst
[0], src
[0]);
1491 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1494 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1495 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1500 if (!bmap1
|| !bmap2
)
1503 div_off
= bmap1
->n_div
;
1505 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1506 int i1
= isl_basic_map_alloc_equality(bmap1
);
1509 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1510 i_pos
, o_pos
, div_off
);
1513 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1514 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1517 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1518 i_pos
, o_pos
, div_off
);
1521 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1522 int i1
= isl_basic_map_alloc_div(bmap1
);
1525 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1526 i_pos
, o_pos
, div_off
);
1529 isl_basic_map_free(bmap2
);
1534 isl_basic_map_free(bmap1
);
1535 isl_basic_map_free(bmap2
);
1539 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1540 struct isl_basic_set
*bset2
, unsigned pos
)
1542 return (struct isl_basic_set
*)
1543 add_constraints((struct isl_basic_map
*)bset1
,
1544 (struct isl_basic_map
*)bset2
, 0, pos
);
1547 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1548 __isl_take isl_space
*dim
, unsigned extra
,
1549 unsigned n_eq
, unsigned n_ineq
)
1551 struct isl_basic_map
*ext
;
1561 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1562 base
->extra
>= base
->n_div
+ extra
;
1564 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1565 room_for_ineq(base
, n_ineq
)) {
1566 isl_space_free(dim
);
1570 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1571 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1572 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1573 extra
+= base
->extra
;
1575 n_ineq
+= base
->n_ineq
;
1577 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1583 ext
->sample
= isl_vec_copy(base
->sample
);
1584 flags
= base
->flags
;
1585 ext
= add_constraints(ext
, base
, 0, 0);
1588 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1594 isl_space_free(dim
);
1595 isl_basic_map_free(base
);
1599 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1600 __isl_take isl_space
*dim
, unsigned extra
,
1601 unsigned n_eq
, unsigned n_ineq
)
1603 return (struct isl_basic_set
*)
1604 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1605 extra
, n_eq
, n_ineq
);
1608 struct isl_basic_map
*isl_basic_map_extend_constraints(
1609 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1613 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1617 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1618 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1619 unsigned n_eq
, unsigned n_ineq
)
1621 struct isl_basic_map
*bmap
;
1626 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1630 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1633 isl_basic_map_free(base
);
1637 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1638 unsigned nparam
, unsigned dim
, unsigned extra
,
1639 unsigned n_eq
, unsigned n_ineq
)
1641 return (struct isl_basic_set
*)
1642 isl_basic_map_extend((struct isl_basic_map
*)base
,
1643 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1646 struct isl_basic_set
*isl_basic_set_extend_constraints(
1647 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1649 return (struct isl_basic_set
*)
1650 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1654 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1656 return (struct isl_basic_set
*)
1657 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1660 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1665 if (bmap
->ref
> 1) {
1667 bmap
= isl_basic_map_dup(bmap
);
1670 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1671 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1676 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1684 return isl_set_dup(set
);
1687 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1695 return isl_map_dup(map
);
1698 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1699 unsigned a_len
, unsigned b_len
)
1701 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1702 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1703 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1706 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1707 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1715 isl_assert(bmap
->ctx
,
1716 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1718 if (n1
== 0 || n2
== 0)
1721 bmap
= isl_basic_map_cow(bmap
);
1725 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1726 if (isl_blk_is_error(blk
))
1729 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1731 bmap
->eq
[i
] + pos
, n1
, n2
);
1733 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1735 bmap
->ineq
[i
] + pos
, n1
, n2
);
1737 for (i
= 0; i
< bmap
->n_div
; ++i
)
1739 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1741 isl_blk_free(bmap
->ctx
, blk
);
1743 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1744 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1745 return isl_basic_map_finalize(bmap
);
1747 isl_basic_map_free(bmap
);
1751 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1757 total
= isl_basic_map_total_dim(bmap
);
1758 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1759 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1761 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1763 i
= isl_basic_map_alloc_equality(bmap
);
1767 isl_int_set_si(bmap
->eq
[i
][0], 1);
1768 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1769 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1770 isl_vec_free(bmap
->sample
);
1771 bmap
->sample
= NULL
;
1772 return isl_basic_map_finalize(bmap
);
1774 isl_basic_map_free(bmap
);
1778 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1780 return (struct isl_basic_set
*)
1781 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1784 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1787 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1789 isl_int
*t
= bmap
->div
[a
];
1790 bmap
->div
[a
] = bmap
->div
[b
];
1794 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1795 * div definitions accordingly.
1797 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1800 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1802 swap_div(bmap
, a
, b
);
1804 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1805 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1807 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1808 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1810 for (i
= 0; i
< bmap
->n_div
; ++i
)
1811 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1812 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1815 /* Eliminate the specified n dimensions starting at first from the
1816 * constraints, without removing the dimensions from the space.
1817 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1819 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1820 enum isl_dim_type type
, unsigned first
, unsigned n
)
1829 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1830 isl_die(map
->ctx
, isl_error_invalid
,
1831 "index out of bounds", goto error
);
1833 map
= isl_map_cow(map
);
1837 for (i
= 0; i
< map
->n
; ++i
) {
1838 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1848 /* Eliminate the specified n dimensions starting at first from the
1849 * constraints, without removing the dimensions from the space.
1850 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1852 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1853 enum isl_dim_type type
, unsigned first
, unsigned n
)
1855 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1858 /* Eliminate the specified n dimensions starting at first from the
1859 * constraints, without removing the dimensions from the space.
1860 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1862 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1863 unsigned first
, unsigned n
)
1865 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1868 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1869 __isl_take isl_basic_map
*bmap
)
1873 bmap
= isl_basic_map_eliminate_vars(bmap
,
1874 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1878 return isl_basic_map_finalize(bmap
);
1881 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1882 __isl_take isl_basic_set
*bset
)
1884 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1885 (struct isl_basic_map
*)bset
);
1888 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1897 map
= isl_map_cow(map
);
1901 for (i
= 0; i
< map
->n
; ++i
) {
1902 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1912 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1914 return isl_map_remove_divs(set
);
1917 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1918 enum isl_dim_type type
, unsigned first
, unsigned n
)
1922 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1924 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1926 bmap
= isl_basic_map_eliminate_vars(bmap
,
1927 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1930 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1932 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1935 isl_basic_map_free(bmap
);
1939 /* Return true if the definition of the given div (recursively) involves
1940 * any of the given variables.
1942 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1943 unsigned first
, unsigned n
)
1946 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1948 if (isl_int_is_zero(bmap
->div
[div
][0]))
1950 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1953 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1954 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1956 if (div_involves_vars(bmap
, i
, first
, n
))
1963 /* Try and add a lower and/or upper bound on "div" to "bmap"
1964 * based on inequality "i".
1965 * "total" is the total number of variables (excluding the divs).
1966 * "v" is a temporary object that can be used during the calculations.
1967 * If "lb" is set, then a lower bound should be constructed.
1968 * If "ub" is set, then an upper bound should be constructed.
1970 * The calling function has already checked that the inequality does not
1971 * reference "div", but we still need to check that the inequality is
1972 * of the right form. We'll consider the case where we want to construct
1973 * a lower bound. The construction of upper bounds is similar.
1975 * Let "div" be of the form
1977 * q = floor((a + f(x))/d)
1979 * We essentially check if constraint "i" is of the form
1983 * so that we can use it to derive a lower bound on "div".
1984 * However, we allow a slightly more general form
1988 * with the condition that the coefficients of g(x) - f(x) are all
1990 * Rewriting this constraint as
1994 * adding a + f(x) to both sides and dividing by d, we obtain
1996 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1998 * Taking the floor on both sides, we obtain
2000 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2004 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2006 * In the case of an upper bound, we construct the constraint
2008 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2011 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2012 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2013 unsigned total
, isl_int v
, int lb
, int ub
)
2017 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2019 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2020 bmap
->div
[div
][1 + 1 + j
]);
2021 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2024 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2025 bmap
->div
[div
][1 + 1 + j
]);
2026 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2032 bmap
= isl_basic_map_cow(bmap
);
2033 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2035 int k
= isl_basic_map_alloc_inequality(bmap
);
2038 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2039 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2040 bmap
->div
[div
][1 + j
]);
2041 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2042 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2044 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2047 int k
= isl_basic_map_alloc_inequality(bmap
);
2050 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2051 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2052 bmap
->div
[div
][1 + j
]);
2053 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2054 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2056 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2059 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2062 isl_basic_map_free(bmap
);
2066 /* This function is called right before "div" is eliminated from "bmap"
2067 * using Fourier-Motzkin.
2068 * Look through the constraints of "bmap" for constraints on the argument
2069 * of the integer division and use them to construct constraints on the
2070 * integer division itself. These constraints can then be combined
2071 * during the Fourier-Motzkin elimination.
2072 * Note that it is only useful to introduce lower bounds on "div"
2073 * if "bmap" already contains upper bounds on "div" as the newly
2074 * introduce lower bounds can then be combined with the pre-existing
2075 * upper bounds. Similarly for upper bounds.
2076 * We therefore first check if "bmap" contains any lower and/or upper bounds
2079 * It is interesting to note that the introduction of these constraints
2080 * can indeed lead to more accurate results, even when compared to
2081 * deriving constraints on the argument of "div" from constraints on "div".
2082 * Consider, for example, the set
2084 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2086 * The second constraint can be rewritten as
2088 * 2 * [(-i-2j+3)/4] + k >= 0
2090 * from which we can derive
2092 * -i - 2j + 3 >= -2k
2098 * Combined with the first constraint, we obtain
2100 * -3 <= 3 + 2k or k >= -3
2102 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2103 * the first constraint, we obtain
2105 * [(i + 2j)/4] >= [-3/4] = -1
2107 * Combining this constraint with the second constraint, we obtain
2111 static __isl_give isl_basic_map
*insert_bounds_on_div(
2112 __isl_take isl_basic_map
*bmap
, int div
)
2115 int check_lb
, check_ub
;
2122 if (isl_int_is_zero(bmap
->div
[div
][0]))
2125 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2129 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2130 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2137 if (!check_lb
&& !check_ub
)
2142 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2143 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2146 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2147 check_lb
, check_ub
);
2155 /* Remove all divs (recursively) involving any of the given dimensions
2156 * in their definitions.
2158 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2159 __isl_take isl_basic_map
*bmap
,
2160 enum isl_dim_type type
, unsigned first
, unsigned n
)
2166 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2168 first
+= isl_basic_map_offset(bmap
, type
);
2170 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2171 if (!div_involves_vars(bmap
, i
, first
, n
))
2173 bmap
= insert_bounds_on_div(bmap
, i
);
2174 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2182 isl_basic_map_free(bmap
);
2186 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2187 __isl_take isl_basic_set
*bset
,
2188 enum isl_dim_type type
, unsigned first
, unsigned n
)
2190 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2193 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2194 enum isl_dim_type type
, unsigned first
, unsigned n
)
2203 map
= isl_map_cow(map
);
2207 for (i
= 0; i
< map
->n
; ++i
) {
2208 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2219 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2220 enum isl_dim_type type
, unsigned first
, unsigned n
)
2222 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2226 /* Does the desciption of "bmap" depend on the specified dimensions?
2227 * We also check whether the dimensions appear in any of the div definitions.
2228 * In principle there is no need for this check. If the dimensions appear
2229 * in a div definition, they also appear in the defining constraints of that
2232 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2233 enum isl_dim_type type
, unsigned first
, unsigned n
)
2238 return isl_bool_error
;
2240 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2241 isl_die(bmap
->ctx
, isl_error_invalid
,
2242 "index out of bounds", return isl_bool_error
);
2244 first
+= isl_basic_map_offset(bmap
, type
);
2245 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2246 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2247 return isl_bool_true
;
2248 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2249 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2250 return isl_bool_true
;
2251 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2252 if (isl_int_is_zero(bmap
->div
[i
][0]))
2254 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2255 return isl_bool_true
;
2258 return isl_bool_false
;
2261 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2262 enum isl_dim_type type
, unsigned first
, unsigned n
)
2267 return isl_bool_error
;
2269 if (first
+ n
> isl_map_dim(map
, type
))
2270 isl_die(map
->ctx
, isl_error_invalid
,
2271 "index out of bounds", return isl_bool_error
);
2273 for (i
= 0; i
< map
->n
; ++i
) {
2274 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2276 if (involves
< 0 || involves
)
2280 return isl_bool_false
;
2283 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2284 enum isl_dim_type type
, unsigned first
, unsigned n
)
2286 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2289 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2290 enum isl_dim_type type
, unsigned first
, unsigned n
)
2292 return isl_map_involves_dims(set
, type
, first
, n
);
2295 /* Return true if the definition of the given div is unknown or depends
2298 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2301 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2303 if (isl_int_is_zero(bmap
->div
[div
][0]))
2306 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2307 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2309 if (div_is_unknown(bmap
, i
))
2316 /* Remove all divs that are unknown or defined in terms of unknown divs.
2318 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2319 __isl_take isl_basic_map
*bmap
)
2326 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2327 if (!div_is_unknown(bmap
, i
))
2329 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2338 /* Remove all divs that are unknown or defined in terms of unknown divs.
2340 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2341 __isl_take isl_basic_set
*bset
)
2343 return isl_basic_map_remove_unknown_divs(bset
);
2346 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2355 map
= isl_map_cow(map
);
2359 for (i
= 0; i
< map
->n
; ++i
) {
2360 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2370 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2372 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2375 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2376 __isl_take isl_basic_set
*bset
,
2377 enum isl_dim_type type
, unsigned first
, unsigned n
)
2379 return (isl_basic_set
*)
2380 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2383 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2384 enum isl_dim_type type
, unsigned first
, unsigned n
)
2391 map
= isl_map_cow(map
);
2394 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2396 for (i
= 0; i
< map
->n
; ++i
) {
2397 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2398 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2402 map
= isl_map_drop(map
, type
, first
, n
);
2409 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2410 enum isl_dim_type type
, unsigned first
, unsigned n
)
2412 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2415 /* Project out n inputs starting at first using Fourier-Motzkin */
2416 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2417 unsigned first
, unsigned n
)
2419 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2422 static void dump_term(struct isl_basic_map
*bmap
,
2423 isl_int c
, int pos
, FILE *out
)
2426 unsigned in
= isl_basic_map_n_in(bmap
);
2427 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2428 unsigned nparam
= isl_basic_map_n_param(bmap
);
2430 isl_int_print(out
, c
, 0);
2432 if (!isl_int_is_one(c
))
2433 isl_int_print(out
, c
, 0);
2434 if (pos
< 1 + nparam
) {
2435 name
= isl_space_get_dim_name(bmap
->dim
,
2436 isl_dim_param
, pos
- 1);
2438 fprintf(out
, "%s", name
);
2440 fprintf(out
, "p%d", pos
- 1);
2441 } else if (pos
< 1 + nparam
+ in
)
2442 fprintf(out
, "i%d", pos
- 1 - nparam
);
2443 else if (pos
< 1 + nparam
+ dim
)
2444 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2446 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2450 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2451 int sign
, FILE *out
)
2455 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2459 for (i
= 0, first
= 1; i
< len
; ++i
) {
2460 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2463 fprintf(out
, " + ");
2465 isl_int_abs(v
, c
[i
]);
2466 dump_term(bmap
, v
, i
, out
);
2473 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2474 const char *op
, FILE *out
, int indent
)
2478 fprintf(out
, "%*s", indent
, "");
2480 dump_constraint_sign(bmap
, c
, 1, out
);
2481 fprintf(out
, " %s ", op
);
2482 dump_constraint_sign(bmap
, c
, -1, out
);
2486 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2487 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2489 fprintf(out
, "%*s", indent
, "");
2490 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2495 static void dump_constraints(struct isl_basic_map
*bmap
,
2496 isl_int
**c
, unsigned n
,
2497 const char *op
, FILE *out
, int indent
)
2501 for (i
= 0; i
< n
; ++i
)
2502 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2505 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2509 unsigned total
= isl_basic_map_total_dim(bmap
);
2511 for (j
= 0; j
< 1 + total
; ++j
) {
2512 if (isl_int_is_zero(exp
[j
]))
2514 if (!first
&& isl_int_is_pos(exp
[j
]))
2516 dump_term(bmap
, exp
[j
], j
, out
);
2521 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2525 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2526 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2528 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2529 fprintf(out
, "%*s", indent
, "");
2530 fprintf(out
, "e%d = [(", i
);
2531 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2533 isl_int_print(out
, bmap
->div
[i
][0], 0);
2534 fprintf(out
, "]\n");
2538 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2539 FILE *out
, int indent
)
2542 fprintf(out
, "null basic set\n");
2546 fprintf(out
, "%*s", indent
, "");
2547 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2548 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2549 bset
->extra
, bset
->flags
);
2550 dump((struct isl_basic_map
*)bset
, out
, indent
);
2553 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2554 FILE *out
, int indent
)
2557 fprintf(out
, "null basic map\n");
2561 fprintf(out
, "%*s", indent
, "");
2562 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2563 "flags: %x, n_name: %d\n",
2565 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2566 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2567 dump(bmap
, out
, indent
);
2570 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2575 total
= isl_basic_map_total_dim(bmap
);
2576 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2577 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2578 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2579 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2583 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2586 struct isl_set
*set
;
2590 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2591 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2592 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2593 sizeof(struct isl_set
) +
2594 (n
- 1) * sizeof(struct isl_basic_set
*));
2598 set
->ctx
= dim
->ctx
;
2599 isl_ctx_ref(set
->ctx
);
2607 isl_space_free(dim
);
2611 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2612 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2614 struct isl_set
*set
;
2617 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2621 set
= isl_set_alloc_space(dims
, n
, flags
);
2625 /* Make sure "map" has room for at least "n" more basic maps.
2627 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2630 struct isl_map
*grown
= NULL
;
2634 isl_assert(map
->ctx
, n
>= 0, goto error
);
2635 if (map
->n
+ n
<= map
->size
)
2637 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2640 for (i
= 0; i
< map
->n
; ++i
) {
2641 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2649 isl_map_free(grown
);
2654 /* Make sure "set" has room for at least "n" more basic sets.
2656 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2658 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2661 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2664 struct isl_set
*dup
;
2669 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2672 for (i
= 0; i
< set
->n
; ++i
)
2673 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2677 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2679 return isl_map_from_basic_map(bset
);
2682 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2684 struct isl_map
*map
;
2689 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2690 return isl_map_add_basic_map(map
, bmap
);
2693 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2694 __isl_take isl_basic_set
*bset
)
2696 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2697 (struct isl_basic_map
*)bset
);
2700 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2710 isl_ctx_deref(set
->ctx
);
2711 for (i
= 0; i
< set
->n
; ++i
)
2712 isl_basic_set_free(set
->p
[i
]);
2713 isl_space_free(set
->dim
);
2719 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2724 fprintf(out
, "null set\n");
2728 fprintf(out
, "%*s", indent
, "");
2729 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2730 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2732 for (i
= 0; i
< set
->n
; ++i
) {
2733 fprintf(out
, "%*s", indent
, "");
2734 fprintf(out
, "basic set %d:\n", i
);
2735 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2739 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2744 fprintf(out
, "null map\n");
2748 fprintf(out
, "%*s", indent
, "");
2749 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2750 "flags: %x, n_name: %d\n",
2751 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2752 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2753 for (i
= 0; i
< map
->n
; ++i
) {
2754 fprintf(out
, "%*s", indent
, "");
2755 fprintf(out
, "basic map %d:\n", i
);
2756 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2760 struct isl_basic_map
*isl_basic_map_intersect_domain(
2761 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2763 struct isl_basic_map
*bmap_domain
;
2768 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2769 bset
->dim
, isl_dim_param
), goto error
);
2771 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2772 isl_assert(bset
->ctx
,
2773 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2775 bmap
= isl_basic_map_cow(bmap
);
2778 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2779 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2780 bmap_domain
= isl_basic_map_from_domain(bset
);
2781 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2783 bmap
= isl_basic_map_simplify(bmap
);
2784 return isl_basic_map_finalize(bmap
);
2786 isl_basic_map_free(bmap
);
2787 isl_basic_set_free(bset
);
2791 struct isl_basic_map
*isl_basic_map_intersect_range(
2792 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2794 struct isl_basic_map
*bmap_range
;
2799 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2800 bset
->dim
, isl_dim_param
), goto error
);
2802 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2803 isl_assert(bset
->ctx
,
2804 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2806 if (isl_basic_set_plain_is_universe(bset
)) {
2807 isl_basic_set_free(bset
);
2811 bmap
= isl_basic_map_cow(bmap
);
2814 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2815 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2816 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2817 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2819 bmap
= isl_basic_map_simplify(bmap
);
2820 return isl_basic_map_finalize(bmap
);
2822 isl_basic_map_free(bmap
);
2823 isl_basic_set_free(bset
);
2827 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2828 __isl_keep isl_vec
*vec
)
2835 return isl_bool_error
;
2837 total
= 1 + isl_basic_map_total_dim(bmap
);
2838 if (total
!= vec
->size
)
2839 return isl_bool_error
;
2843 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2844 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2845 if (!isl_int_is_zero(s
)) {
2847 return isl_bool_false
;
2851 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2852 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2853 if (isl_int_is_neg(s
)) {
2855 return isl_bool_false
;
2861 return isl_bool_true
;
2864 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2865 __isl_keep isl_vec
*vec
)
2867 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2870 struct isl_basic_map
*isl_basic_map_intersect(
2871 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2873 struct isl_vec
*sample
= NULL
;
2875 if (!bmap1
|| !bmap2
)
2878 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2879 bmap2
->dim
, isl_dim_param
), goto error
);
2880 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2881 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2882 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2883 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2884 return isl_basic_map_intersect(bmap2
, bmap1
);
2886 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2887 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2888 isl_assert(bmap1
->ctx
,
2889 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2891 if (isl_basic_map_plain_is_empty(bmap1
)) {
2892 isl_basic_map_free(bmap2
);
2895 if (isl_basic_map_plain_is_empty(bmap2
)) {
2896 isl_basic_map_free(bmap1
);
2900 if (bmap1
->sample
&&
2901 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2902 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2903 sample
= isl_vec_copy(bmap1
->sample
);
2904 else if (bmap2
->sample
&&
2905 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2906 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2907 sample
= isl_vec_copy(bmap2
->sample
);
2909 bmap1
= isl_basic_map_cow(bmap1
);
2912 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2913 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2914 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2917 isl_vec_free(sample
);
2919 isl_vec_free(bmap1
->sample
);
2920 bmap1
->sample
= sample
;
2923 bmap1
= isl_basic_map_simplify(bmap1
);
2924 return isl_basic_map_finalize(bmap1
);
2927 isl_vec_free(sample
);
2928 isl_basic_map_free(bmap1
);
2929 isl_basic_map_free(bmap2
);
2933 struct isl_basic_set
*isl_basic_set_intersect(
2934 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2936 return (struct isl_basic_set
*)
2937 isl_basic_map_intersect(
2938 (struct isl_basic_map
*)bset1
,
2939 (struct isl_basic_map
*)bset2
);
2942 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2943 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2945 return isl_basic_set_intersect(bset1
, bset2
);
2948 /* Special case of isl_map_intersect, where both map1 and map2
2949 * are convex, without any divs and such that either map1 or map2
2950 * contains a single constraint. This constraint is then simply
2951 * added to the other map.
2953 static __isl_give isl_map
*map_intersect_add_constraint(
2954 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2956 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2957 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2958 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2959 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2961 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2962 return isl_map_intersect(map2
, map1
);
2964 isl_assert(map2
->ctx
,
2965 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2967 map1
= isl_map_cow(map1
);
2970 if (isl_map_plain_is_empty(map1
)) {
2974 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2975 if (map2
->p
[0]->n_eq
== 1)
2976 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2978 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2979 map2
->p
[0]->ineq
[0]);
2981 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2982 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2986 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2987 isl_basic_map_free(map1
->p
[0]);
3000 /* map2 may be either a parameter domain or a map living in the same
3003 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3004 __isl_take isl_map
*map2
)
3013 if ((isl_map_plain_is_empty(map1
) ||
3014 isl_map_plain_is_universe(map2
)) &&
3015 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3019 if ((isl_map_plain_is_empty(map2
) ||
3020 isl_map_plain_is_universe(map1
)) &&
3021 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3026 if (map1
->n
== 1 && map2
->n
== 1 &&
3027 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3028 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3029 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3030 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3031 return map_intersect_add_constraint(map1
, map2
);
3033 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3034 isl_space_dim(map2
->dim
, isl_dim_param
))
3035 isl_assert(map1
->ctx
,
3036 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3038 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3039 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3040 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3042 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3043 map1
->n
* map2
->n
, flags
);
3046 for (i
= 0; i
< map1
->n
; ++i
)
3047 for (j
= 0; j
< map2
->n
; ++j
) {
3048 struct isl_basic_map
*part
;
3049 part
= isl_basic_map_intersect(
3050 isl_basic_map_copy(map1
->p
[i
]),
3051 isl_basic_map_copy(map2
->p
[j
]));
3052 if (isl_basic_map_is_empty(part
) < 0)
3053 part
= isl_basic_map_free(part
);
3054 result
= isl_map_add_basic_map(result
, part
);
3067 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3068 __isl_take isl_map
*map2
)
3072 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3073 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3074 "spaces don't match", goto error
);
3075 return map_intersect_internal(map1
, map2
);
3082 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3083 __isl_take isl_map
*map2
)
3085 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3088 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3090 return (struct isl_set
*)
3091 isl_map_intersect((struct isl_map
*)set1
,
3092 (struct isl_map
*)set2
);
3095 /* map_intersect_internal accepts intersections
3096 * with parameter domains, so we can just call that function.
3098 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3099 __isl_take isl_set
*params
)
3101 return map_intersect_internal(map
, params
);
3104 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3105 __isl_take isl_map
*map2
)
3107 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3110 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3111 __isl_take isl_set
*params
)
3113 return isl_map_intersect_params(set
, params
);
3116 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3119 unsigned pos
, n1
, n2
;
3123 bmap
= isl_basic_map_cow(bmap
);
3126 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3127 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3128 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3129 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3130 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3131 return isl_basic_map_reset_space(bmap
, space
);
3134 static __isl_give isl_basic_map
*basic_map_space_reset(
3135 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3141 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3144 space
= isl_basic_map_get_space(bmap
);
3145 space
= isl_space_reset(space
, type
);
3146 bmap
= isl_basic_map_reset_space(bmap
, space
);
3150 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3151 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3152 unsigned pos
, unsigned n
)
3155 struct isl_basic_map
*res
;
3156 struct isl_dim_map
*dim_map
;
3157 unsigned total
, off
;
3158 enum isl_dim_type t
;
3161 return basic_map_space_reset(bmap
, type
);
3166 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3168 total
= isl_basic_map_total_dim(bmap
) + n
;
3169 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3171 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3173 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3175 unsigned size
= isl_basic_map_dim(bmap
, t
);
3176 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3178 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3179 pos
, size
- pos
, off
+ pos
+ n
);
3181 off
+= isl_space_dim(res_dim
, t
);
3183 isl_dim_map_div(dim_map
, bmap
, off
);
3185 res
= isl_basic_map_alloc_space(res_dim
,
3186 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3187 if (isl_basic_map_is_rational(bmap
))
3188 res
= isl_basic_map_set_rational(res
);
3189 if (isl_basic_map_plain_is_empty(bmap
)) {
3190 isl_basic_map_free(bmap
);
3192 return isl_basic_map_set_to_empty(res
);
3194 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3195 return isl_basic_map_finalize(res
);
3198 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3199 __isl_take isl_basic_set
*bset
,
3200 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3202 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3205 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3206 enum isl_dim_type type
, unsigned n
)
3210 return isl_basic_map_insert_dims(bmap
, type
,
3211 isl_basic_map_dim(bmap
, type
), n
);
3214 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3215 enum isl_dim_type type
, unsigned n
)
3219 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3220 return isl_basic_map_add_dims(bset
, type
, n
);
3222 isl_basic_set_free(bset
);
3226 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3227 enum isl_dim_type type
)
3231 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3234 space
= isl_map_get_space(map
);
3235 space
= isl_space_reset(space
, type
);
3236 map
= isl_map_reset_space(map
, space
);
3240 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3241 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3246 return map_space_reset(map
, type
);
3248 map
= isl_map_cow(map
);
3252 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3256 for (i
= 0; i
< map
->n
; ++i
) {
3257 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3268 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3269 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3271 return isl_map_insert_dims(set
, type
, pos
, n
);
3274 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3275 enum isl_dim_type type
, unsigned n
)
3279 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3282 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3283 enum isl_dim_type type
, unsigned n
)
3287 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3288 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3294 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3295 __isl_take isl_basic_map
*bmap
,
3296 enum isl_dim_type dst_type
, unsigned dst_pos
,
3297 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3299 struct isl_dim_map
*dim_map
;
3300 struct isl_basic_map
*res
;
3301 enum isl_dim_type t
;
3302 unsigned total
, off
;
3309 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3312 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3315 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3317 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3318 pos(bmap
->dim
, src_type
) + src_pos
+
3319 ((src_type
< dst_type
) ? n
: 0)) {
3320 bmap
= isl_basic_map_cow(bmap
);
3324 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3325 src_type
, src_pos
, n
);
3329 bmap
= isl_basic_map_finalize(bmap
);
3334 total
= isl_basic_map_total_dim(bmap
);
3335 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3338 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3339 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3340 if (t
== dst_type
) {
3341 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3344 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3347 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3348 dst_pos
, size
- dst_pos
, off
);
3349 off
+= size
- dst_pos
;
3350 } else if (t
== src_type
) {
3351 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3354 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3355 src_pos
+ n
, size
- src_pos
- n
, off
);
3356 off
+= size
- src_pos
- n
;
3358 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3362 isl_dim_map_div(dim_map
, bmap
, off
);
3364 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3365 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3366 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3370 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3371 src_type
, src_pos
, n
);
3375 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3376 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3377 bmap
= isl_basic_map_finalize(bmap
);
3381 isl_basic_map_free(bmap
);
3385 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3386 enum isl_dim_type dst_type
, unsigned dst_pos
,
3387 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3389 return (isl_basic_set
*)isl_basic_map_move_dims(
3390 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3393 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3394 enum isl_dim_type dst_type
, unsigned dst_pos
,
3395 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3399 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3400 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3401 src_type
, src_pos
, n
);
3407 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3408 enum isl_dim_type dst_type
, unsigned dst_pos
,
3409 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3418 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3421 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3424 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3426 map
= isl_map_cow(map
);
3430 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3434 for (i
= 0; i
< map
->n
; ++i
) {
3435 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3437 src_type
, src_pos
, n
);
3448 /* Move the specified dimensions to the last columns right before
3449 * the divs. Don't change the dimension specification of bmap.
3450 * That's the responsibility of the caller.
3452 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3453 enum isl_dim_type type
, unsigned first
, unsigned n
)
3455 struct isl_dim_map
*dim_map
;
3456 struct isl_basic_map
*res
;
3457 enum isl_dim_type t
;
3458 unsigned total
, off
;
3462 if (pos(bmap
->dim
, type
) + first
+ n
==
3463 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3466 total
= isl_basic_map_total_dim(bmap
);
3467 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3470 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3471 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3473 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3476 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3477 first
, n
, total
- bmap
->n_div
- n
);
3478 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3479 first
+ n
, size
- (first
+ n
), off
);
3480 off
+= size
- (first
+ n
);
3482 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3486 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3488 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3489 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3490 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3494 /* Insert "n" rows in the divs of "bmap".
3496 * The number of columns is not changed, which means that the last
3497 * dimensions of "bmap" are being reintepreted as the new divs.
3498 * The space of "bmap" is not adjusted, however, which means
3499 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3500 * from the space of "bmap" is the responsibility of the caller.
3502 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3510 bmap
= isl_basic_map_cow(bmap
);
3514 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3515 old
= bmap
->block2
.data
;
3516 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3517 (bmap
->extra
+ n
) * (1 + row_size
));
3518 if (!bmap
->block2
.data
)
3519 return isl_basic_map_free(bmap
);
3520 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3522 return isl_basic_map_free(bmap
);
3523 for (i
= 0; i
< n
; ++i
) {
3524 new_div
[i
] = bmap
->block2
.data
+
3525 (bmap
->extra
+ i
) * (1 + row_size
);
3526 isl_seq_clr(new_div
[i
], 1 + row_size
);
3528 for (i
= 0; i
< bmap
->extra
; ++i
)
3529 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3531 bmap
->div
= new_div
;
3538 /* Drop constraints from "bmap" that only involve the variables
3539 * of "type" in the range [first, first + n] that are not related
3540 * to any of the variables outside that interval.
3541 * These constraints cannot influence the values for the variables
3542 * outside the interval, except in case they cause "bmap" to be empty.
3543 * Only drop the constraints if "bmap" is known to be non-empty.
3545 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3546 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3547 unsigned first
, unsigned n
)
3551 unsigned dim
, n_div
;
3554 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3556 return isl_basic_map_free(bmap
);
3560 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3561 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3562 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3564 return isl_basic_map_free(bmap
);
3565 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3566 for (i
= 0; i
< first
; ++i
)
3568 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3571 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3576 /* Turn the n dimensions of type type, starting at first
3577 * into existentially quantified variables.
3579 * If a subset of the projected out variables are unrelated
3580 * to any of the variables that remain, then the constraints
3581 * involving this subset are simply dropped first.
3583 __isl_give isl_basic_map
*isl_basic_map_project_out(
3584 __isl_take isl_basic_map
*bmap
,
3585 enum isl_dim_type type
, unsigned first
, unsigned n
)
3588 return basic_map_space_reset(bmap
, type
);
3589 if (type
== isl_dim_div
)
3590 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3591 "cannot project out existentially quantified variables",
3592 return isl_basic_map_free(bmap
));
3594 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3598 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3599 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3601 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3604 bmap
= move_last(bmap
, type
, first
, n
);
3605 bmap
= isl_basic_map_cow(bmap
);
3606 bmap
= insert_div_rows(bmap
, n
);
3610 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3613 bmap
= isl_basic_map_simplify(bmap
);
3614 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3615 return isl_basic_map_finalize(bmap
);
3617 isl_basic_map_free(bmap
);
3621 /* Turn the n dimensions of type type, starting at first
3622 * into existentially quantified variables.
3624 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3625 enum isl_dim_type type
, unsigned first
, unsigned n
)
3627 return (isl_basic_set
*)isl_basic_map_project_out(
3628 (isl_basic_map
*)bset
, type
, first
, n
);
3631 /* Turn the n dimensions of type type, starting at first
3632 * into existentially quantified variables.
3634 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3635 enum isl_dim_type type
, unsigned first
, unsigned n
)
3643 return map_space_reset(map
, type
);
3645 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3647 map
= isl_map_cow(map
);
3651 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3655 for (i
= 0; i
< map
->n
; ++i
) {
3656 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3667 /* Turn the n dimensions of type type, starting at first
3668 * into existentially quantified variables.
3670 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3671 enum isl_dim_type type
, unsigned first
, unsigned n
)
3673 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3676 /* Return a map that projects the elements in "set" onto their
3677 * "n" set dimensions starting at "first".
3678 * "type" should be equal to isl_dim_set.
3680 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3681 enum isl_dim_type type
, unsigned first
, unsigned n
)
3689 if (type
!= isl_dim_set
)
3690 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3691 "only set dimensions can be projected out", goto error
);
3692 dim
= isl_set_dim(set
, isl_dim_set
);
3693 if (first
+ n
> dim
|| first
+ n
< first
)
3694 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3695 "index out of bounds", goto error
);
3697 map
= isl_map_from_domain(set
);
3698 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3699 for (i
= 0; i
< n
; ++i
)
3700 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3708 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3712 for (i
= 0; i
< n
; ++i
) {
3713 j
= isl_basic_map_alloc_div(bmap
);
3716 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3720 isl_basic_map_free(bmap
);
3724 struct isl_basic_map
*isl_basic_map_apply_range(
3725 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3727 isl_space
*dim_result
= NULL
;
3728 struct isl_basic_map
*bmap
;
3729 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3730 struct isl_dim_map
*dim_map1
, *dim_map2
;
3732 if (!bmap1
|| !bmap2
)
3734 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3735 bmap2
->dim
, isl_dim_param
))
3736 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3737 "parameters don't match", goto error
);
3738 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3739 bmap2
->dim
, isl_dim_in
))
3740 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3741 "spaces don't match", goto error
);
3743 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3744 isl_space_copy(bmap2
->dim
));
3746 n_in
= isl_basic_map_n_in(bmap1
);
3747 n_out
= isl_basic_map_n_out(bmap2
);
3748 n
= isl_basic_map_n_out(bmap1
);
3749 nparam
= isl_basic_map_n_param(bmap1
);
3751 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3752 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3753 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3754 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3755 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3756 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3757 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3758 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3759 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3760 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3761 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3763 bmap
= isl_basic_map_alloc_space(dim_result
,
3764 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3765 bmap1
->n_eq
+ bmap2
->n_eq
,
3766 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3767 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3768 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3769 bmap
= add_divs(bmap
, n
);
3770 bmap
= isl_basic_map_simplify(bmap
);
3771 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3772 return isl_basic_map_finalize(bmap
);
3774 isl_basic_map_free(bmap1
);
3775 isl_basic_map_free(bmap2
);
3779 struct isl_basic_set
*isl_basic_set_apply(
3780 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3785 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3788 return (struct isl_basic_set
*)
3789 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3791 isl_basic_set_free(bset
);
3792 isl_basic_map_free(bmap
);
3796 struct isl_basic_map
*isl_basic_map_apply_domain(
3797 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3799 if (!bmap1
|| !bmap2
)
3802 isl_assert(bmap1
->ctx
,
3803 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3804 isl_assert(bmap1
->ctx
,
3805 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3808 bmap1
= isl_basic_map_reverse(bmap1
);
3809 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3810 return isl_basic_map_reverse(bmap1
);
3812 isl_basic_map_free(bmap1
);
3813 isl_basic_map_free(bmap2
);
3817 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3818 * A \cap B -> f(A) + f(B)
3820 struct isl_basic_map
*isl_basic_map_sum(
3821 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3823 unsigned n_in
, n_out
, nparam
, total
, pos
;
3824 struct isl_basic_map
*bmap
= NULL
;
3825 struct isl_dim_map
*dim_map1
, *dim_map2
;
3828 if (!bmap1
|| !bmap2
)
3831 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3834 nparam
= isl_basic_map_n_param(bmap1
);
3835 n_in
= isl_basic_map_n_in(bmap1
);
3836 n_out
= isl_basic_map_n_out(bmap1
);
3838 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3839 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3840 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3841 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3842 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3843 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3844 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3845 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3846 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3847 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3848 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3850 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3851 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3852 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3853 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3854 for (i
= 0; i
< n_out
; ++i
) {
3855 int j
= isl_basic_map_alloc_equality(bmap
);
3858 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3859 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3860 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3861 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3863 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3864 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3865 bmap
= add_divs(bmap
, 2 * n_out
);
3867 bmap
= isl_basic_map_simplify(bmap
);
3868 return isl_basic_map_finalize(bmap
);
3870 isl_basic_map_free(bmap
);
3871 isl_basic_map_free(bmap1
);
3872 isl_basic_map_free(bmap2
);
3876 /* Given two maps A -> f(A) and B -> g(B), construct a map
3877 * A \cap B -> f(A) + f(B)
3879 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3881 struct isl_map
*result
;
3887 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3889 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3890 map1
->n
* map2
->n
, 0);
3893 for (i
= 0; i
< map1
->n
; ++i
)
3894 for (j
= 0; j
< map2
->n
; ++j
) {
3895 struct isl_basic_map
*part
;
3896 part
= isl_basic_map_sum(
3897 isl_basic_map_copy(map1
->p
[i
]),
3898 isl_basic_map_copy(map2
->p
[j
]));
3899 if (isl_basic_map_is_empty(part
))
3900 isl_basic_map_free(part
);
3902 result
= isl_map_add_basic_map(result
, part
);
3915 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3916 __isl_take isl_set
*set2
)
3918 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3921 /* Given a basic map A -> f(A), construct A -> -f(A).
3923 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3928 bmap
= isl_basic_map_cow(bmap
);
3932 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3933 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3934 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3935 for (j
= 0; j
< n
; ++j
)
3936 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3937 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3938 for (j
= 0; j
< n
; ++j
)
3939 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3940 for (i
= 0; i
< bmap
->n_div
; ++i
)
3941 for (j
= 0; j
< n
; ++j
)
3942 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3943 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3944 return isl_basic_map_finalize(bmap
);
3947 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3949 return isl_basic_map_neg(bset
);
3952 /* Given a map A -> f(A), construct A -> -f(A).
3954 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3958 map
= isl_map_cow(map
);
3962 for (i
= 0; i
< map
->n
; ++i
) {
3963 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3974 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3976 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3979 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3980 * A -> floor(f(A)/d).
3982 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3985 unsigned n_in
, n_out
, nparam
, total
, pos
;
3986 struct isl_basic_map
*result
= NULL
;
3987 struct isl_dim_map
*dim_map
;
3993 nparam
= isl_basic_map_n_param(bmap
);
3994 n_in
= isl_basic_map_n_in(bmap
);
3995 n_out
= isl_basic_map_n_out(bmap
);
3997 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3998 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3999 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4000 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4001 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4002 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4004 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4005 bmap
->n_div
+ n_out
,
4006 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4007 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4008 result
= add_divs(result
, n_out
);
4009 for (i
= 0; i
< n_out
; ++i
) {
4011 j
= isl_basic_map_alloc_inequality(result
);
4014 isl_seq_clr(result
->ineq
[j
], 1+total
);
4015 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4016 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4017 j
= isl_basic_map_alloc_inequality(result
);
4020 isl_seq_clr(result
->ineq
[j
], 1+total
);
4021 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4022 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4023 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4026 result
= isl_basic_map_simplify(result
);
4027 return isl_basic_map_finalize(result
);
4029 isl_basic_map_free(result
);
4033 /* Given a map A -> f(A) and an integer d, construct a map
4034 * A -> floor(f(A)/d).
4036 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4040 map
= isl_map_cow(map
);
4044 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4045 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4046 for (i
= 0; i
< map
->n
; ++i
) {
4047 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4058 /* Given a map A -> f(A) and an integer d, construct a map
4059 * A -> floor(f(A)/d).
4061 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4062 __isl_take isl_val
*d
)
4066 if (!isl_val_is_int(d
))
4067 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4068 "expecting integer denominator", goto error
);
4069 map
= isl_map_floordiv(map
, d
->n
);
4078 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4084 i
= isl_basic_map_alloc_equality(bmap
);
4087 nparam
= isl_basic_map_n_param(bmap
);
4088 n_in
= isl_basic_map_n_in(bmap
);
4089 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4090 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4091 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4092 return isl_basic_map_finalize(bmap
);
4094 isl_basic_map_free(bmap
);
4098 /* Add a constraints to "bmap" expressing i_pos < o_pos
4100 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4106 i
= isl_basic_map_alloc_inequality(bmap
);
4109 nparam
= isl_basic_map_n_param(bmap
);
4110 n_in
= isl_basic_map_n_in(bmap
);
4111 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4112 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4113 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4114 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4115 return isl_basic_map_finalize(bmap
);
4117 isl_basic_map_free(bmap
);
4121 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4123 static __isl_give isl_basic_map
*var_less_or_equal(
4124 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4130 i
= isl_basic_map_alloc_inequality(bmap
);
4133 nparam
= isl_basic_map_n_param(bmap
);
4134 n_in
= isl_basic_map_n_in(bmap
);
4135 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4136 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4137 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4138 return isl_basic_map_finalize(bmap
);
4140 isl_basic_map_free(bmap
);
4144 /* Add a constraints to "bmap" expressing i_pos > o_pos
4146 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4152 i
= isl_basic_map_alloc_inequality(bmap
);
4155 nparam
= isl_basic_map_n_param(bmap
);
4156 n_in
= isl_basic_map_n_in(bmap
);
4157 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4158 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4159 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4160 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4161 return isl_basic_map_finalize(bmap
);
4163 isl_basic_map_free(bmap
);
4167 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4169 static __isl_give isl_basic_map
*var_more_or_equal(
4170 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4176 i
= isl_basic_map_alloc_inequality(bmap
);
4179 nparam
= isl_basic_map_n_param(bmap
);
4180 n_in
= isl_basic_map_n_in(bmap
);
4181 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4182 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4183 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4184 return isl_basic_map_finalize(bmap
);
4186 isl_basic_map_free(bmap
);
4190 __isl_give isl_basic_map
*isl_basic_map_equal(
4191 __isl_take isl_space
*dim
, unsigned n_equal
)
4194 struct isl_basic_map
*bmap
;
4195 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4198 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4199 bmap
= var_equal(bmap
, i
);
4200 return isl_basic_map_finalize(bmap
);
4203 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4205 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4209 struct isl_basic_map
*bmap
;
4210 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4213 for (i
= 0; i
< pos
&& bmap
; ++i
)
4214 bmap
= var_equal(bmap
, i
);
4216 bmap
= var_less(bmap
, pos
);
4217 return isl_basic_map_finalize(bmap
);
4220 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4222 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4223 __isl_take isl_space
*dim
, unsigned pos
)
4226 isl_basic_map
*bmap
;
4228 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4229 for (i
= 0; i
< pos
; ++i
)
4230 bmap
= var_equal(bmap
, i
);
4231 bmap
= var_less_or_equal(bmap
, pos
);
4232 return isl_basic_map_finalize(bmap
);
4235 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4237 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4241 struct isl_basic_map
*bmap
;
4242 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4245 for (i
= 0; i
< pos
&& bmap
; ++i
)
4246 bmap
= var_equal(bmap
, i
);
4248 bmap
= var_more(bmap
, pos
);
4249 return isl_basic_map_finalize(bmap
);
4252 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4254 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4255 __isl_take isl_space
*dim
, unsigned pos
)
4258 isl_basic_map
*bmap
;
4260 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4261 for (i
= 0; i
< pos
; ++i
)
4262 bmap
= var_equal(bmap
, i
);
4263 bmap
= var_more_or_equal(bmap
, pos
);
4264 return isl_basic_map_finalize(bmap
);
4267 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4268 unsigned n
, int equal
)
4270 struct isl_map
*map
;
4273 if (n
== 0 && equal
)
4274 return isl_map_universe(dims
);
4276 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4278 for (i
= 0; i
+ 1 < n
; ++i
)
4279 map
= isl_map_add_basic_map(map
,
4280 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4283 map
= isl_map_add_basic_map(map
,
4284 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4286 map
= isl_map_add_basic_map(map
,
4287 isl_basic_map_less_at(dims
, n
- 1));
4289 isl_space_free(dims
);
4294 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4298 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4301 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4303 return map_lex_lte_first(dim
, n
, 0);
4306 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4308 return map_lex_lte_first(dim
, n
, 1);
4311 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4313 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4316 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4318 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4321 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4322 unsigned n
, int equal
)
4324 struct isl_map
*map
;
4327 if (n
== 0 && equal
)
4328 return isl_map_universe(dims
);
4330 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4332 for (i
= 0; i
+ 1 < n
; ++i
)
4333 map
= isl_map_add_basic_map(map
,
4334 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4337 map
= isl_map_add_basic_map(map
,
4338 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4340 map
= isl_map_add_basic_map(map
,
4341 isl_basic_map_more_at(dims
, n
- 1));
4343 isl_space_free(dims
);
4348 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4352 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4355 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4357 return map_lex_gte_first(dim
, n
, 0);
4360 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4362 return map_lex_gte_first(dim
, n
, 1);
4365 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4367 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4370 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4372 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4375 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4376 __isl_take isl_set
*set2
)
4379 map
= isl_map_lex_le(isl_set_get_space(set1
));
4380 map
= isl_map_intersect_domain(map
, set1
);
4381 map
= isl_map_intersect_range(map
, set2
);
4385 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4386 __isl_take isl_set
*set2
)
4389 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4390 map
= isl_map_intersect_domain(map
, set1
);
4391 map
= isl_map_intersect_range(map
, set2
);
4395 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4396 __isl_take isl_set
*set2
)
4399 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4400 map
= isl_map_intersect_domain(map
, set1
);
4401 map
= isl_map_intersect_range(map
, set2
);
4405 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4406 __isl_take isl_set
*set2
)
4409 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4410 map
= isl_map_intersect_domain(map
, set1
);
4411 map
= isl_map_intersect_range(map
, set2
);
4415 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4416 __isl_take isl_map
*map2
)
4419 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4420 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4421 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4425 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4426 __isl_take isl_map
*map2
)
4429 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4430 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4431 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4435 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4436 __isl_take isl_map
*map2
)
4439 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4440 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4441 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4445 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4446 __isl_take isl_map
*map2
)
4449 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4450 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4451 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4455 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4456 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4458 struct isl_basic_map
*bmap
;
4460 bset
= isl_basic_set_cow(bset
);
4464 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4465 isl_space_free(bset
->dim
);
4466 bmap
= (struct isl_basic_map
*) bset
;
4468 return isl_basic_map_finalize(bmap
);
4470 isl_basic_set_free(bset
);
4471 isl_space_free(dim
);
4475 /* For a div d = floor(f/m), add the constraint
4479 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4480 unsigned pos
, isl_int
*div
)
4483 unsigned total
= isl_basic_map_total_dim(bmap
);
4485 i
= isl_basic_map_alloc_inequality(bmap
);
4488 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4489 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4494 /* For a div d = floor(f/m), add the constraint
4496 * -(f-(n-1)) + m d >= 0
4498 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4499 unsigned pos
, isl_int
*div
)
4502 unsigned total
= isl_basic_map_total_dim(bmap
);
4504 i
= isl_basic_map_alloc_inequality(bmap
);
4507 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4508 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4509 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4510 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4515 /* For a div d = floor(f/m), add the constraints
4518 * -(f-(n-1)) + m d >= 0
4520 * Note that the second constraint is the negation of
4524 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4525 unsigned pos
, isl_int
*div
)
4527 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4529 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4534 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4535 unsigned pos
, isl_int
*div
)
4537 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4541 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4543 unsigned total
= isl_basic_map_total_dim(bmap
);
4544 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4546 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4550 /* For each known div d = floor(f/m), add the constraints
4553 * -(f-(n-1)) + m d >= 0
4555 * Remove duplicate constraints in case of some these div constraints
4556 * already appear in "bmap".
4558 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4559 __isl_take isl_basic_map
*bmap
)
4565 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4569 bmap
= add_known_div_constraints(bmap
);
4570 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4571 bmap
= isl_basic_map_finalize(bmap
);
4575 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4577 * In particular, if this div is of the form d = floor(f/m),
4578 * then add the constraint
4582 * if sign < 0 or the constraint
4584 * -(f-(n-1)) + m d >= 0
4588 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4589 unsigned div
, int sign
)
4597 total
= isl_basic_map_total_dim(bmap
);
4598 div_pos
= total
- bmap
->n_div
+ div
;
4601 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4603 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4606 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4608 return isl_basic_map_add_div_constraints(bset
, div
);
4611 struct isl_basic_set
*isl_basic_map_underlying_set(
4612 struct isl_basic_map
*bmap
)
4616 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4618 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4619 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4620 return (struct isl_basic_set
*)bmap
;
4621 bmap
= isl_basic_map_cow(bmap
);
4624 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4627 bmap
->extra
-= bmap
->n_div
;
4629 bmap
= isl_basic_map_finalize(bmap
);
4630 return (struct isl_basic_set
*)bmap
;
4632 isl_basic_map_free(bmap
);
4636 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4637 __isl_take isl_basic_set
*bset
)
4639 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4642 /* Replace each element in "list" by the result of applying
4643 * isl_basic_map_underlying_set to the element.
4645 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4646 __isl_take isl_basic_map_list
*list
)
4653 n
= isl_basic_map_list_n_basic_map(list
);
4654 for (i
= 0; i
< n
; ++i
) {
4655 isl_basic_map
*bmap
;
4656 isl_basic_set
*bset
;
4658 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4659 bset
= isl_basic_set_underlying_set(bmap
);
4660 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4666 struct isl_basic_map
*isl_basic_map_overlying_set(
4667 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4669 struct isl_basic_map
*bmap
;
4670 struct isl_ctx
*ctx
;
4677 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4678 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4679 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4681 if (like
->n_div
== 0) {
4682 isl_space
*space
= isl_basic_map_get_space(like
);
4683 isl_basic_map_free(like
);
4684 return isl_basic_map_reset_space(bset
, space
);
4686 bset
= isl_basic_set_cow(bset
);
4689 total
= bset
->dim
->n_out
+ bset
->extra
;
4690 bmap
= (struct isl_basic_map
*)bset
;
4691 isl_space_free(bmap
->dim
);
4692 bmap
->dim
= isl_space_copy(like
->dim
);
4695 bmap
->n_div
= like
->n_div
;
4696 bmap
->extra
+= like
->n_div
;
4700 ltotal
= total
- bmap
->extra
+ like
->extra
;
4703 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4704 bmap
->extra
* (1 + 1 + total
));
4705 if (isl_blk_is_error(bmap
->block2
))
4707 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4711 for (i
= 0; i
< bmap
->extra
; ++i
)
4712 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4713 for (i
= 0; i
< like
->n_div
; ++i
) {
4714 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4715 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4717 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4719 isl_basic_map_free(like
);
4720 bmap
= isl_basic_map_simplify(bmap
);
4721 bmap
= isl_basic_map_finalize(bmap
);
4724 isl_basic_map_free(like
);
4725 isl_basic_set_free(bset
);
4729 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4730 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4732 return (struct isl_basic_set
*)
4733 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4736 struct isl_set
*isl_set_from_underlying_set(
4737 struct isl_set
*set
, struct isl_basic_set
*like
)
4743 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4745 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4746 isl_basic_set_free(like
);
4749 set
= isl_set_cow(set
);
4752 for (i
= 0; i
< set
->n
; ++i
) {
4753 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4754 isl_basic_set_copy(like
));
4758 isl_space_free(set
->dim
);
4759 set
->dim
= isl_space_copy(like
->dim
);
4762 isl_basic_set_free(like
);
4765 isl_basic_set_free(like
);
4770 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4774 map
= isl_map_cow(map
);
4777 map
->dim
= isl_space_cow(map
->dim
);
4781 for (i
= 1; i
< map
->n
; ++i
)
4782 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4784 for (i
= 0; i
< map
->n
; ++i
) {
4785 map
->p
[i
] = (struct isl_basic_map
*)
4786 isl_basic_map_underlying_set(map
->p
[i
]);
4791 map
->dim
= isl_space_underlying(map
->dim
, 0);
4793 isl_space_free(map
->dim
);
4794 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4798 return (struct isl_set
*)map
;
4804 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4806 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4809 /* Replace the space of "bmap" by "space".
4811 * If the space of "bmap" is identical to "space" (including the identifiers
4812 * of the input and output dimensions), then simply return the original input.
4814 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4815 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4821 equal
= isl_space_is_equal(bmap
->dim
, space
);
4822 if (equal
>= 0 && equal
)
4823 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4825 if (equal
>= 0 && equal
)
4826 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4827 space
, isl_dim_out
);
4831 isl_space_free(space
);
4834 bmap
= isl_basic_map_cow(bmap
);
4835 if (!bmap
|| !space
)
4838 isl_space_free(bmap
->dim
);
4841 bmap
= isl_basic_map_finalize(bmap
);
4845 isl_basic_map_free(bmap
);
4846 isl_space_free(space
);
4850 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4851 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4853 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4857 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4858 __isl_take isl_space
*dim
)
4862 map
= isl_map_cow(map
);
4866 for (i
= 0; i
< map
->n
; ++i
) {
4867 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4868 isl_space_copy(dim
));
4872 isl_space_free(map
->dim
);
4878 isl_space_free(dim
);
4882 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4883 __isl_take isl_space
*dim
)
4885 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4888 /* Compute the parameter domain of the given basic set.
4890 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4895 if (isl_basic_set_is_params(bset
))
4898 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4899 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4900 space
= isl_basic_set_get_space(bset
);
4901 space
= isl_space_params(space
);
4902 bset
= isl_basic_set_reset_space(bset
, space
);
4906 /* Construct a zero-dimensional basic set with the given parameter domain.
4908 __isl_give isl_basic_set
*isl_basic_set_from_params(
4909 __isl_take isl_basic_set
*bset
)
4912 space
= isl_basic_set_get_space(bset
);
4913 space
= isl_space_set_from_params(space
);
4914 bset
= isl_basic_set_reset_space(bset
, space
);
4918 /* Compute the parameter domain of the given set.
4920 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4925 if (isl_set_is_params(set
))
4928 n
= isl_set_dim(set
, isl_dim_set
);
4929 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4930 space
= isl_set_get_space(set
);
4931 space
= isl_space_params(space
);
4932 set
= isl_set_reset_space(set
, space
);
4936 /* Construct a zero-dimensional set with the given parameter domain.
4938 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4941 space
= isl_set_get_space(set
);
4942 space
= isl_space_set_from_params(space
);
4943 set
= isl_set_reset_space(set
, space
);
4947 /* Compute the parameter domain of the given map.
4949 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4954 n
= isl_map_dim(map
, isl_dim_in
);
4955 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4956 n
= isl_map_dim(map
, isl_dim_out
);
4957 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4958 space
= isl_map_get_space(map
);
4959 space
= isl_space_params(space
);
4960 map
= isl_map_reset_space(map
, space
);
4964 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4971 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4973 n_out
= isl_basic_map_n_out(bmap
);
4974 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4976 return isl_basic_map_reset_space(bmap
, space
);
4979 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4983 return isl_space_may_be_set(bmap
->dim
);
4986 /* Is this basic map actually a set?
4987 * Users should never call this function. Outside of isl,
4988 * the type should indicate whether something is a set or a map.
4990 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4994 return isl_space_is_set(bmap
->dim
);
4997 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5001 if (isl_basic_map_is_set(bmap
))
5003 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5006 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5007 __isl_take isl_basic_map
*bmap
)
5011 isl_basic_map
*domain
;
5012 int nparam
, n_in
, n_out
;
5015 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5016 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5017 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5019 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5020 domain
= isl_basic_map_universe(dim
);
5022 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5023 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5024 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5026 total
= isl_basic_map_total_dim(bmap
);
5028 for (i
= 0; i
< n_in
; ++i
) {
5029 k
= isl_basic_map_alloc_equality(bmap
);
5032 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5033 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5034 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5037 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5038 return isl_basic_map_finalize(bmap
);
5040 isl_basic_map_free(bmap
);
5044 __isl_give isl_basic_map
*isl_basic_map_range_map(
5045 __isl_take isl_basic_map
*bmap
)
5049 isl_basic_map
*range
;
5050 int nparam
, n_in
, n_out
;
5053 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5054 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5055 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5057 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5058 range
= isl_basic_map_universe(dim
);
5060 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5061 bmap
= isl_basic_map_apply_range(bmap
, range
);
5062 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5064 total
= isl_basic_map_total_dim(bmap
);
5066 for (i
= 0; i
< n_out
; ++i
) {
5067 k
= isl_basic_map_alloc_equality(bmap
);
5070 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5071 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5072 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5075 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5076 return isl_basic_map_finalize(bmap
);
5078 isl_basic_map_free(bmap
);
5082 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5086 return isl_space_may_be_set(map
->dim
);
5089 /* Is this map actually a set?
5090 * Users should never call this function. Outside of isl,
5091 * the type should indicate whether something is a set or a map.
5093 int isl_map_is_set(__isl_keep isl_map
*map
)
5097 return isl_space_is_set(map
->dim
);
5100 struct isl_set
*isl_map_range(struct isl_map
*map
)
5103 struct isl_set
*set
;
5107 if (isl_map_is_set(map
))
5108 return (isl_set
*)map
;
5110 map
= isl_map_cow(map
);
5114 set
= (struct isl_set
*) map
;
5115 set
->dim
= isl_space_range(set
->dim
);
5118 for (i
= 0; i
< map
->n
; ++i
) {
5119 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5123 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5124 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5131 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5135 map
= isl_map_cow(map
);
5139 map
->dim
= isl_space_domain_map(map
->dim
);
5142 for (i
= 0; i
< map
->n
; ++i
) {
5143 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5147 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5148 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5155 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5158 isl_space
*range_dim
;
5160 map
= isl_map_cow(map
);
5164 range_dim
= isl_space_range(isl_map_get_space(map
));
5165 range_dim
= isl_space_from_range(range_dim
);
5166 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5167 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5170 for (i
= 0; i
< map
->n
; ++i
) {
5171 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5175 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5176 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5183 /* Given a wrapped map of the form A[B -> C],
5184 * return the map A[B -> C] -> B.
5186 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5193 if (!isl_set_has_tuple_id(set
))
5194 return isl_map_domain_map(isl_set_unwrap(set
));
5196 id
= isl_set_get_tuple_id(set
);
5197 map
= isl_map_domain_map(isl_set_unwrap(set
));
5198 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5203 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5204 __isl_take isl_space
*dim
)
5207 struct isl_map
*map
= NULL
;
5209 set
= isl_set_cow(set
);
5212 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5213 map
= (struct isl_map
*)set
;
5214 for (i
= 0; i
< set
->n
; ++i
) {
5215 map
->p
[i
] = isl_basic_map_from_basic_set(
5216 set
->p
[i
], isl_space_copy(dim
));
5220 isl_space_free(map
->dim
);
5224 isl_space_free(dim
);
5229 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5230 __isl_take isl_basic_set
*bset
)
5232 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5235 __isl_give isl_basic_map
*isl_basic_map_from_range(
5236 __isl_take isl_basic_set
*bset
)
5239 space
= isl_basic_set_get_space(bset
);
5240 space
= isl_space_from_range(space
);
5241 bset
= isl_basic_set_reset_space(bset
, space
);
5242 return (isl_basic_map
*)bset
;
5245 /* Create a relation with the given set as range.
5246 * The domain of the created relation is a zero-dimensional
5247 * flat anonymous space.
5249 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5252 space
= isl_set_get_space(set
);
5253 space
= isl_space_from_range(space
);
5254 set
= isl_set_reset_space(set
, space
);
5255 return (struct isl_map
*)set
;
5258 /* Create a relation with the given set as domain.
5259 * The range of the created relation is a zero-dimensional
5260 * flat anonymous space.
5262 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5264 return isl_map_reverse(isl_map_from_range(set
));
5267 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5268 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5270 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5273 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5274 __isl_take isl_set
*range
)
5276 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5279 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5282 struct isl_map
*map
;
5287 isl_die(dim
->ctx
, isl_error_internal
,
5288 "negative number of basic maps", goto error
);
5289 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5290 sizeof(struct isl_map
) +
5291 (n
- 1) * sizeof(struct isl_basic_map
*));
5295 map
->ctx
= dim
->ctx
;
5296 isl_ctx_ref(map
->ctx
);
5304 isl_space_free(dim
);
5308 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5309 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5312 struct isl_map
*map
;
5315 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5319 map
= isl_map_alloc_space(dims
, n
, flags
);
5323 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5325 struct isl_basic_map
*bmap
;
5326 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5327 bmap
= isl_basic_map_set_to_empty(bmap
);
5331 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5333 struct isl_basic_set
*bset
;
5334 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5335 bset
= isl_basic_set_set_to_empty(bset
);
5339 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5341 struct isl_basic_map
*bmap
;
5342 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5343 bmap
= isl_basic_map_finalize(bmap
);
5347 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5349 struct isl_basic_set
*bset
;
5350 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5351 bset
= isl_basic_set_finalize(bset
);
5355 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5358 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5359 isl_basic_map
*bmap
;
5361 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5362 for (i
= 0; i
< total
; ++i
) {
5363 int k
= isl_basic_map_alloc_inequality(bmap
);
5366 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5367 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5371 isl_basic_map_free(bmap
);
5375 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5377 return isl_basic_map_nat_universe(dim
);
5380 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5382 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5385 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5387 return isl_map_nat_universe(dim
);
5390 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5392 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5395 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5397 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5400 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5402 struct isl_map
*map
;
5405 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5406 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5410 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5412 struct isl_set
*set
;
5415 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5416 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5420 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5423 struct isl_map
*dup
;
5427 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5428 for (i
= 0; i
< map
->n
; ++i
)
5429 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5433 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5434 __isl_take isl_basic_map
*bmap
)
5438 if (isl_basic_map_plain_is_empty(bmap
)) {
5439 isl_basic_map_free(bmap
);
5442 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5443 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5444 map
->p
[map
->n
] = bmap
;
5446 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5452 isl_basic_map_free(bmap
);
5456 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5466 isl_ctx_deref(map
->ctx
);
5467 for (i
= 0; i
< map
->n
; ++i
)
5468 isl_basic_map_free(map
->p
[i
]);
5469 isl_space_free(map
->dim
);
5475 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5476 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5480 bmap
= isl_basic_map_cow(bmap
);
5481 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5482 j
= isl_basic_map_alloc_equality(bmap
);
5485 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5486 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5487 isl_int_set_si(bmap
->eq
[j
][0], value
);
5488 bmap
= isl_basic_map_simplify(bmap
);
5489 return isl_basic_map_finalize(bmap
);
5491 isl_basic_map_free(bmap
);
5495 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5496 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5500 bmap
= isl_basic_map_cow(bmap
);
5501 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5502 j
= isl_basic_map_alloc_equality(bmap
);
5505 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5506 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5507 isl_int_set(bmap
->eq
[j
][0], value
);
5508 bmap
= isl_basic_map_simplify(bmap
);
5509 return isl_basic_map_finalize(bmap
);
5511 isl_basic_map_free(bmap
);
5515 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5516 enum isl_dim_type type
, unsigned pos
, int value
)
5520 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5521 return isl_basic_map_fix_pos_si(bmap
,
5522 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5524 isl_basic_map_free(bmap
);
5528 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5529 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5533 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5534 return isl_basic_map_fix_pos(bmap
,
5535 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5537 isl_basic_map_free(bmap
);
5541 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5542 * to be equal to "v".
5544 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5545 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5549 if (!isl_val_is_int(v
))
5550 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5551 "expecting integer value", goto error
);
5552 if (pos
>= isl_basic_map_dim(bmap
, type
))
5553 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5554 "index out of bounds", goto error
);
5555 pos
+= isl_basic_map_offset(bmap
, type
);
5556 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5560 isl_basic_map_free(bmap
);
5565 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5566 * to be equal to "v".
5568 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5569 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5571 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5574 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5575 enum isl_dim_type type
, unsigned pos
, int value
)
5577 return (struct isl_basic_set
*)
5578 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5582 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5583 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5585 return (struct isl_basic_set
*)
5586 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5590 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5591 unsigned input
, int value
)
5593 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5596 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5597 unsigned dim
, int value
)
5599 return (struct isl_basic_set
*)
5600 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5601 isl_dim_set
, dim
, value
);
5604 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5606 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5613 isl_basic_map_free(map
->p
[i
]);
5614 if (i
!= map
->n
- 1) {
5615 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5616 map
->p
[i
] = map
->p
[map
->n
- 1];
5623 /* Perform "fn" on each basic map of "map", where we may not be holding
5624 * the only reference to "map".
5625 * In particular, "fn" should be a semantics preserving operation
5626 * that we want to apply to all copies of "map". We therefore need
5627 * to be careful not to modify "map" in a way that breaks "map"
5628 * in case anything goes wrong.
5630 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5631 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5633 struct isl_basic_map
*bmap
;
5639 for (i
= map
->n
- 1; i
>= 0; --i
) {
5640 bmap
= isl_basic_map_copy(map
->p
[i
]);
5644 isl_basic_map_free(map
->p
[i
]);
5646 if (remove_if_empty(map
, i
) < 0)
5656 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5657 enum isl_dim_type type
, unsigned pos
, int value
)
5661 map
= isl_map_cow(map
);
5665 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5666 for (i
= map
->n
- 1; i
>= 0; --i
) {
5667 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5668 if (remove_if_empty(map
, i
) < 0)
5671 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5678 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5679 enum isl_dim_type type
, unsigned pos
, int value
)
5681 return (struct isl_set
*)
5682 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5685 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5686 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5690 map
= isl_map_cow(map
);
5694 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5695 for (i
= 0; i
< map
->n
; ++i
) {
5696 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5700 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5707 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5708 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5710 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5713 /* Fix the value of the variable at position "pos" of type "type" of "map"
5714 * to be equal to "v".
5716 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5717 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5721 map
= isl_map_cow(map
);
5725 if (!isl_val_is_int(v
))
5726 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5727 "expecting integer value", goto error
);
5728 if (pos
>= isl_map_dim(map
, type
))
5729 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5730 "index out of bounds", goto error
);
5731 for (i
= map
->n
- 1; i
>= 0; --i
) {
5732 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5734 if (remove_if_empty(map
, i
) < 0)
5737 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5746 /* Fix the value of the variable at position "pos" of type "type" of "set"
5747 * to be equal to "v".
5749 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5750 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5752 return isl_map_fix_val(set
, type
, pos
, v
);
5755 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5756 unsigned input
, int value
)
5758 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5761 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5763 return (struct isl_set
*)
5764 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5767 static __isl_give isl_basic_map
*basic_map_bound_si(
5768 __isl_take isl_basic_map
*bmap
,
5769 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5775 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5776 pos
+= isl_basic_map_offset(bmap
, type
);
5777 bmap
= isl_basic_map_cow(bmap
);
5778 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5779 j
= isl_basic_map_alloc_inequality(bmap
);
5782 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5784 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5785 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5787 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5788 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5790 bmap
= isl_basic_map_simplify(bmap
);
5791 return isl_basic_map_finalize(bmap
);
5793 isl_basic_map_free(bmap
);
5797 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5798 __isl_take isl_basic_map
*bmap
,
5799 enum isl_dim_type type
, unsigned pos
, int value
)
5801 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5804 /* Constrain the values of the given dimension to be no greater than "value".
5806 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5807 __isl_take isl_basic_map
*bmap
,
5808 enum isl_dim_type type
, unsigned pos
, int value
)
5810 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5813 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5814 unsigned dim
, isl_int value
)
5818 bset
= isl_basic_set_cow(bset
);
5819 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5820 j
= isl_basic_set_alloc_inequality(bset
);
5823 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5824 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5825 isl_int_neg(bset
->ineq
[j
][0], value
);
5826 bset
= isl_basic_set_simplify(bset
);
5827 return isl_basic_set_finalize(bset
);
5829 isl_basic_set_free(bset
);
5833 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5834 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5838 map
= isl_map_cow(map
);
5842 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5843 for (i
= 0; i
< map
->n
; ++i
) {
5844 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5845 type
, pos
, value
, upper
);
5849 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5856 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5857 enum isl_dim_type type
, unsigned pos
, int value
)
5859 return map_bound_si(map
, type
, pos
, value
, 0);
5862 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5863 enum isl_dim_type type
, unsigned pos
, int value
)
5865 return map_bound_si(map
, type
, pos
, value
, 1);
5868 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5869 enum isl_dim_type type
, unsigned pos
, int value
)
5871 return (struct isl_set
*)
5872 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5875 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5876 enum isl_dim_type type
, unsigned pos
, int value
)
5878 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5881 /* Bound the given variable of "bmap" from below (or above is "upper"
5882 * is set) to "value".
5884 static __isl_give isl_basic_map
*basic_map_bound(
5885 __isl_take isl_basic_map
*bmap
,
5886 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5892 if (pos
>= isl_basic_map_dim(bmap
, type
))
5893 isl_die(bmap
->ctx
, isl_error_invalid
,
5894 "index out of bounds", goto error
);
5895 pos
+= isl_basic_map_offset(bmap
, type
);
5896 bmap
= isl_basic_map_cow(bmap
);
5897 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5898 j
= isl_basic_map_alloc_inequality(bmap
);
5901 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5903 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5904 isl_int_set(bmap
->ineq
[j
][0], value
);
5906 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5907 isl_int_neg(bmap
->ineq
[j
][0], value
);
5909 bmap
= isl_basic_map_simplify(bmap
);
5910 return isl_basic_map_finalize(bmap
);
5912 isl_basic_map_free(bmap
);
5916 /* Bound the given variable of "map" from below (or above is "upper"
5917 * is set) to "value".
5919 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5920 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5924 map
= isl_map_cow(map
);
5928 if (pos
>= isl_map_dim(map
, type
))
5929 isl_die(map
->ctx
, isl_error_invalid
,
5930 "index out of bounds", goto error
);
5931 for (i
= map
->n
- 1; i
>= 0; --i
) {
5932 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5933 if (remove_if_empty(map
, i
) < 0)
5936 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5943 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5944 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5946 return map_bound(map
, type
, pos
, value
, 0);
5949 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5950 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5952 return map_bound(map
, type
, pos
, value
, 1);
5955 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5956 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5958 return isl_map_lower_bound(set
, type
, pos
, value
);
5961 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5962 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5964 return isl_map_upper_bound(set
, type
, pos
, value
);
5967 /* Force the values of the variable at position "pos" of type "type" of "set"
5968 * to be no smaller than "value".
5970 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5971 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5975 if (!isl_val_is_int(value
))
5976 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5977 "expecting integer value", goto error
);
5978 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5979 isl_val_free(value
);
5982 isl_val_free(value
);
5987 /* Force the values of the variable at position "pos" of type "type" of "set"
5988 * to be no greater than "value".
5990 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5991 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5995 if (!isl_val_is_int(value
))
5996 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5997 "expecting integer value", goto error
);
5998 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5999 isl_val_free(value
);
6002 isl_val_free(value
);
6007 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6012 set
= isl_set_cow(set
);
6016 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6017 for (i
= 0; i
< set
->n
; ++i
) {
6018 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6028 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6032 map
= isl_map_cow(map
);
6036 map
->dim
= isl_space_reverse(map
->dim
);
6039 for (i
= 0; i
< map
->n
; ++i
) {
6040 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6044 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6051 static struct isl_map
*isl_basic_map_partial_lexopt(
6052 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6053 struct isl_set
**empty
, int max
)
6055 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6058 struct isl_map
*isl_basic_map_partial_lexmax(
6059 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6060 struct isl_set
**empty
)
6062 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6065 struct isl_map
*isl_basic_map_partial_lexmin(
6066 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6067 struct isl_set
**empty
)
6069 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6072 struct isl_set
*isl_basic_set_partial_lexmin(
6073 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6074 struct isl_set
**empty
)
6076 return (struct isl_set
*)
6077 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6081 struct isl_set
*isl_basic_set_partial_lexmax(
6082 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6083 struct isl_set
**empty
)
6085 return (struct isl_set
*)
6086 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6090 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6091 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6092 __isl_give isl_set
**empty
)
6094 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6097 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6098 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6099 __isl_give isl_set
**empty
)
6101 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6104 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6105 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6106 __isl_give isl_set
**empty
)
6108 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6111 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6112 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6113 __isl_give isl_set
**empty
)
6115 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6118 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6119 __isl_take isl_basic_map
*bmap
, int max
)
6121 isl_basic_set
*dom
= NULL
;
6122 isl_space
*dom_space
;
6126 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6127 dom
= isl_basic_set_universe(dom_space
);
6128 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6130 isl_basic_map_free(bmap
);
6134 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6135 __isl_take isl_basic_map
*bmap
)
6137 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6141 #define TYPE isl_pw_multi_aff
6143 #define SUFFIX _pw_multi_aff
6145 #define EMPTY isl_pw_multi_aff_empty
6147 #define ADD isl_pw_multi_aff_union_add
6148 #include "isl_map_lexopt_templ.c"
6150 /* Given a map "map", compute the lexicographically minimal
6151 * (or maximal) image element for each domain element in dom,
6152 * in the form of an isl_pw_multi_aff.
6153 * If "empty" is not NULL, then set *empty to those elements in dom that
6154 * do not have an image element.
6156 * We first compute the lexicographically minimal or maximal element
6157 * in the first basic map. This results in a partial solution "res"
6158 * and a subset "todo" of dom that still need to be handled.
6159 * We then consider each of the remaining maps in "map" and successively
6160 * update both "res" and "todo".
6161 * If "empty" is NULL, then the todo sets are not needed and therefore
6162 * also not computed.
6164 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6165 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6166 __isl_give isl_set
**empty
, int max
)
6169 isl_pw_multi_aff
*res
;
6175 if (isl_map_plain_is_empty(map
)) {
6180 return isl_pw_multi_aff_from_map(map
);
6183 res
= basic_map_partial_lexopt_pw_multi_aff(
6184 isl_basic_map_copy(map
->p
[0]),
6185 isl_set_copy(dom
), empty
, max
);
6189 for (i
= 1; i
< map
->n
; ++i
) {
6190 isl_pw_multi_aff
*res_i
;
6192 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6193 isl_basic_map_copy(map
->p
[i
]),
6194 isl_set_copy(dom
), empty
, max
);
6197 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6199 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6202 todo
= isl_set_intersect(todo
, *empty
);
6221 #define TYPE isl_map
6225 #define EMPTY isl_map_empty
6227 #define ADD isl_map_union_disjoint
6228 #include "isl_map_lexopt_templ.c"
6230 /* Given a map "map", compute the lexicographically minimal
6231 * (or maximal) image element for each domain element in "dom",
6232 * in the form of an isl_map.
6233 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6234 * do not have an image element.
6236 * If the input consists of more than one disjunct, then first
6237 * compute the desired result in the form of an isl_pw_multi_aff and
6238 * then convert that into an isl_map.
6240 * This function used to have an explicit implementation in terms
6241 * of isl_maps, but it would continually intersect the domains of
6242 * partial results with the complement of the domain of the next
6243 * partial solution, potentially leading to an explosion in the number
6244 * of disjuncts if there are several disjuncts in the input.
6245 * An even earlier implementation of this function would look for
6246 * better results in the domain of the partial result and for extra
6247 * results in the complement of this domain, which would lead to
6248 * even more splintering.
6250 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6251 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6252 __isl_give isl_set
**empty
, int max
)
6254 struct isl_map
*res
;
6255 isl_pw_multi_aff
*pma
;
6260 if (isl_map_plain_is_empty(map
)) {
6269 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6275 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
, max
);
6276 return isl_map_from_pw_multi_aff(pma
);
6285 __isl_give isl_map
*isl_map_partial_lexmax(
6286 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6287 __isl_give isl_set
**empty
)
6289 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6292 __isl_give isl_map
*isl_map_partial_lexmin(
6293 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6294 __isl_give isl_set
**empty
)
6296 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6299 __isl_give isl_set
*isl_set_partial_lexmin(
6300 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6301 __isl_give isl_set
**empty
)
6303 return (struct isl_set
*)
6304 isl_map_partial_lexmin((struct isl_map
*)set
,
6308 __isl_give isl_set
*isl_set_partial_lexmax(
6309 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6310 __isl_give isl_set
**empty
)
6312 return (struct isl_set
*)
6313 isl_map_partial_lexmax((struct isl_map
*)set
,
6317 /* Compute the lexicographic minimum (or maximum if "max" is set)
6318 * of "bmap" over its domain.
6320 * Since we are not interested in the part of the domain space where
6321 * there is no solution, we initialize the domain to those constraints
6322 * of "bmap" that only involve the parameters and the input dimensions.
6323 * This relieves the parametric programming engine from detecting those
6324 * inequalities and transferring them to the context. More importantly,
6325 * it ensures that those inequalities are transferred first and not
6326 * intermixed with inequalities that actually split the domain.
6328 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6332 isl_basic_map
*copy
;
6335 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6336 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6337 copy
= isl_basic_map_copy(bmap
);
6338 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6339 isl_dim_div
, 0, n_div
);
6340 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6341 isl_dim_out
, 0, n_out
);
6342 dom
= isl_basic_map_domain(copy
);
6343 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6346 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6348 return isl_basic_map_lexopt(bmap
, 0);
6351 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6353 return isl_basic_map_lexopt(bmap
, 1);
6356 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6358 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6361 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6363 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6366 /* Extract the first and only affine expression from list
6367 * and then add it to *pwaff with the given dom.
6368 * This domain is known to be disjoint from other domains
6369 * because of the way isl_basic_map_foreach_lexmax works.
6371 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6372 __isl_take isl_aff_list
*list
, void *user
)
6374 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6376 isl_pw_aff
**pwaff
= user
;
6377 isl_pw_aff
*pwaff_i
;
6381 if (isl_aff_list_n_aff(list
) != 1)
6382 isl_die(ctx
, isl_error_internal
,
6383 "expecting single element list", goto error
);
6385 aff
= isl_aff_list_get_aff(list
, 0);
6386 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6388 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6390 isl_aff_list_free(list
);
6394 isl_basic_set_free(dom
);
6395 isl_aff_list_free(list
);
6399 /* Given a basic map with one output dimension, compute the minimum or
6400 * maximum of that dimension as an isl_pw_aff.
6402 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6403 * call update_dim_opt on each leaf of the result.
6405 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6408 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6412 dim
= isl_space_from_domain(isl_space_domain(dim
));
6413 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6414 pwaff
= isl_pw_aff_empty(dim
);
6416 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6418 return isl_pw_aff_free(pwaff
);
6423 /* Compute the minimum or maximum of the given output dimension
6424 * as a function of the parameters and the input dimensions,
6425 * but independently of the other output dimensions.
6427 * We first project out the other output dimension and then compute
6428 * the "lexicographic" maximum in each basic map, combining the results
6429 * using isl_pw_aff_union_max.
6431 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6438 n_out
= isl_map_dim(map
, isl_dim_out
);
6439 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6440 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6445 isl_space
*dim
= isl_map_get_space(map
);
6447 return isl_pw_aff_empty(dim
);
6450 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6451 for (i
= 1; i
< map
->n
; ++i
) {
6452 isl_pw_aff
*pwaff_i
;
6454 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6455 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6463 /* Compute the maximum of the given output dimension as a function of the
6464 * parameters and input dimensions, but independently of
6465 * the other output dimensions.
6467 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6469 return map_dim_opt(map
, pos
, 1);
6472 /* Compute the minimum or maximum of the given set dimension
6473 * as a function of the parameters,
6474 * but independently of the other set dimensions.
6476 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6479 return map_dim_opt(set
, pos
, max
);
6482 /* Compute the maximum of the given set dimension as a function of the
6483 * parameters, but independently of the other set dimensions.
6485 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6487 return set_dim_opt(set
, pos
, 1);
6490 /* Compute the minimum of the given set dimension as a function of the
6491 * parameters, but independently of the other set dimensions.
6493 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6495 return set_dim_opt(set
, pos
, 0);
6498 /* Apply a preimage specified by "mat" on the parameters of "bset".
6499 * bset is assumed to have only parameters and divs.
6501 static struct isl_basic_set
*basic_set_parameter_preimage(
6502 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6509 bset
->dim
= isl_space_cow(bset
->dim
);
6513 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6515 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6517 bset
->dim
->nparam
= 0;
6518 bset
->dim
->n_out
= nparam
;
6519 bset
= isl_basic_set_preimage(bset
, mat
);
6521 bset
->dim
->nparam
= bset
->dim
->n_out
;
6522 bset
->dim
->n_out
= 0;
6527 isl_basic_set_free(bset
);
6531 /* Apply a preimage specified by "mat" on the parameters of "set".
6532 * set is assumed to have only parameters and divs.
6534 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6535 __isl_take isl_mat
*mat
)
6543 nparam
= isl_set_dim(set
, isl_dim_param
);
6545 if (mat
->n_row
!= 1 + nparam
)
6546 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6547 "unexpected number of rows", goto error
);
6549 space
= isl_set_get_space(set
);
6550 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6551 isl_dim_param
, 0, nparam
);
6552 set
= isl_set_reset_space(set
, space
);
6553 set
= isl_set_preimage(set
, mat
);
6554 nparam
= isl_set_dim(set
, isl_dim_out
);
6555 space
= isl_set_get_space(set
);
6556 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6557 isl_dim_out
, 0, nparam
);
6558 set
= isl_set_reset_space(set
, space
);
6566 /* Intersect the basic set "bset" with the affine space specified by the
6567 * equalities in "eq".
6569 static struct isl_basic_set
*basic_set_append_equalities(
6570 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6578 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6583 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6584 for (i
= 0; i
< eq
->n_row
; ++i
) {
6585 k
= isl_basic_set_alloc_equality(bset
);
6588 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6589 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6593 bset
= isl_basic_set_gauss(bset
, NULL
);
6594 bset
= isl_basic_set_finalize(bset
);
6599 isl_basic_set_free(bset
);
6603 /* Intersect the set "set" with the affine space specified by the
6604 * equalities in "eq".
6606 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6614 for (i
= 0; i
< set
->n
; ++i
) {
6615 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6628 /* Given a basic set "bset" that only involves parameters and existentially
6629 * quantified variables, return the index of the first equality
6630 * that only involves parameters. If there is no such equality then
6631 * return bset->n_eq.
6633 * This function assumes that isl_basic_set_gauss has been called on "bset".
6635 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6638 unsigned nparam
, n_div
;
6643 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6644 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6646 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6647 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6654 /* Compute an explicit representation for the existentially quantified
6655 * variables in "bset" by computing the "minimal value" of the set
6656 * variables. Since there are no set variables, the computation of
6657 * the minimal value essentially computes an explicit representation
6658 * of the non-empty part(s) of "bset".
6660 * The input only involves parameters and existentially quantified variables.
6661 * All equalities among parameters have been removed.
6663 * Since the existentially quantified variables in the result are in general
6664 * going to be different from those in the input, we first replace
6665 * them by the minimal number of variables based on their equalities.
6666 * This should simplify the parametric integer programming.
6668 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6670 isl_morph
*morph1
, *morph2
;
6676 if (bset
->n_eq
== 0)
6677 return isl_basic_set_lexmin(bset
);
6679 morph1
= isl_basic_set_parameter_compression(bset
);
6680 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6681 bset
= isl_basic_set_lift(bset
);
6682 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6683 bset
= isl_morph_basic_set(morph2
, bset
);
6684 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6685 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6687 set
= isl_basic_set_lexmin(bset
);
6689 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6694 /* Project the given basic set onto its parameter domain, possibly introducing
6695 * new, explicit, existential variables in the constraints.
6696 * The input has parameters and (possibly implicit) existential variables.
6697 * The output has the same parameters, but only
6698 * explicit existentially quantified variables.
6700 * The actual projection is performed by pip, but pip doesn't seem
6701 * to like equalities very much, so we first remove the equalities
6702 * among the parameters by performing a variable compression on
6703 * the parameters. Afterward, an inverse transformation is performed
6704 * and the equalities among the parameters are inserted back in.
6706 * The variable compression on the parameters may uncover additional
6707 * equalities that were only implicit before. We therefore check
6708 * if there are any new parameter equalities in the result and
6709 * if so recurse. The removal of parameter equalities is required
6710 * for the parameter compression performed by base_compute_divs.
6712 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6716 struct isl_mat
*T
, *T2
;
6717 struct isl_set
*set
;
6720 bset
= isl_basic_set_cow(bset
);
6724 if (bset
->n_eq
== 0)
6725 return base_compute_divs(bset
);
6727 bset
= isl_basic_set_gauss(bset
, NULL
);
6730 if (isl_basic_set_plain_is_empty(bset
))
6731 return isl_set_from_basic_set(bset
);
6733 i
= first_parameter_equality(bset
);
6734 if (i
== bset
->n_eq
)
6735 return base_compute_divs(bset
);
6737 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6738 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6740 eq
= isl_mat_cow(eq
);
6741 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6742 if (T
&& T
->n_col
== 0) {
6746 bset
= isl_basic_set_set_to_empty(bset
);
6747 return isl_set_from_basic_set(bset
);
6749 bset
= basic_set_parameter_preimage(bset
, T
);
6751 i
= first_parameter_equality(bset
);
6754 else if (i
== bset
->n_eq
)
6755 set
= base_compute_divs(bset
);
6757 set
= parameter_compute_divs(bset
);
6758 set
= set_parameter_preimage(set
, T2
);
6759 set
= set_append_equalities(set
, eq
);
6763 /* Insert the divs from "ls" before those of "bmap".
6765 * The number of columns is not changed, which means that the last
6766 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6767 * The caller is responsible for removing the same number of dimensions
6768 * from the space of "bmap".
6770 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6771 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6777 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6781 old_n_div
= bmap
->n_div
;
6782 bmap
= insert_div_rows(bmap
, n_div
);
6786 for (i
= 0; i
< n_div
; ++i
) {
6787 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6788 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6794 /* Replace the space of "bmap" by the space and divs of "ls".
6796 * If "ls" has any divs, then we simplify the result since we may
6797 * have discovered some additional equalities that could simplify
6798 * the div expressions.
6800 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6801 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6805 bmap
= isl_basic_map_cow(bmap
);
6809 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6810 bmap
= insert_divs_from_local_space(bmap
, ls
);
6814 isl_space_free(bmap
->dim
);
6815 bmap
->dim
= isl_local_space_get_space(ls
);
6819 isl_local_space_free(ls
);
6821 bmap
= isl_basic_map_simplify(bmap
);
6822 bmap
= isl_basic_map_finalize(bmap
);
6825 isl_basic_map_free(bmap
);
6826 isl_local_space_free(ls
);
6830 /* Replace the space of "map" by the space and divs of "ls".
6832 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6833 __isl_take isl_local_space
*ls
)
6837 map
= isl_map_cow(map
);
6841 for (i
= 0; i
< map
->n
; ++i
) {
6842 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6843 isl_local_space_copy(ls
));
6847 isl_space_free(map
->dim
);
6848 map
->dim
= isl_local_space_get_space(ls
);
6852 isl_local_space_free(ls
);
6855 isl_local_space_free(ls
);
6860 /* Compute an explicit representation for the existentially
6861 * quantified variables for which do not know any explicit representation yet.
6863 * We first sort the existentially quantified variables so that the
6864 * existentially quantified variables for which we already have an explicit
6865 * representation are placed before those for which we do not.
6866 * The input dimensions, the output dimensions and the existentially
6867 * quantified variables for which we already have an explicit
6868 * representation are then turned into parameters.
6869 * compute_divs returns a map with the same parameters and
6870 * no input or output dimensions and the dimension specification
6871 * is reset to that of the input, including the existentially quantified
6872 * variables for which we already had an explicit representation.
6874 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6876 struct isl_basic_set
*bset
;
6877 struct isl_set
*set
;
6878 struct isl_map
*map
;
6880 isl_local_space
*ls
;
6887 bmap
= isl_basic_map_sort_divs(bmap
);
6888 bmap
= isl_basic_map_cow(bmap
);
6892 n_known
= isl_basic_map_first_unknown_div(bmap
);
6894 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6896 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6897 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6898 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6899 dim
= isl_space_set_alloc(bmap
->ctx
,
6900 nparam
+ n_in
+ n_out
+ n_known
, 0);
6904 ls
= isl_basic_map_get_local_space(bmap
);
6905 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6906 n_known
, bmap
->n_div
- n_known
);
6908 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6909 swap_div(bmap
, i
- n_known
, i
);
6910 bmap
->n_div
-= n_known
;
6911 bmap
->extra
-= n_known
;
6913 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6914 bset
= (struct isl_basic_set
*)bmap
;
6916 set
= parameter_compute_divs(bset
);
6917 map
= (struct isl_map
*)set
;
6918 map
= replace_space_by_local_space(map
, ls
);
6922 isl_basic_map_free(bmap
);
6926 /* Remove the explicit representation of local variable "div",
6929 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6930 __isl_take isl_basic_map
*bmap
, int div
)
6934 known
= isl_basic_map_div_is_known(bmap
, div
);
6936 return isl_basic_map_free(bmap
);
6940 bmap
= isl_basic_map_cow(bmap
);
6943 isl_int_set_si(bmap
->div
[div
][0], 0);
6947 /* Does local variable "div" of "bmap" have an explicit representation?
6949 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
6952 return isl_bool_error
;
6953 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6954 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6955 "position out of bounds", return isl_bool_error
);
6956 return !isl_int_is_zero(bmap
->div
[div
][0]);
6959 /* Return the position of the first local variable that does not
6960 * have an explicit representation.
6961 * Return the total number of local variables if they all have
6962 * an explicit representation.
6963 * Return -1 on error.
6965 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
6972 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6973 if (!isl_basic_map_div_is_known(bmap
, i
))
6979 /* Does "bmap" have an explicit representation for all local variables?
6981 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6985 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
6986 first
= isl_basic_map_first_unknown_div(bmap
);
6988 return isl_bool_error
;
6992 /* Do all basic maps in "map" have an explicit representation
6993 * for all local variables?
6995 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7000 return isl_bool_error
;
7002 for (i
= 0; i
< map
->n
; ++i
) {
7003 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7008 return isl_bool_true
;
7011 /* If bmap contains any unknown divs, then compute explicit
7012 * expressions for them. However, this computation may be
7013 * quite expensive, so first try to remove divs that aren't
7016 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7019 struct isl_map
*map
;
7021 known
= isl_basic_map_divs_known(bmap
);
7025 return isl_map_from_basic_map(bmap
);
7027 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7029 known
= isl_basic_map_divs_known(bmap
);
7033 return isl_map_from_basic_map(bmap
);
7035 map
= compute_divs(bmap
);
7038 isl_basic_map_free(bmap
);
7042 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7046 struct isl_map
*res
;
7053 known
= isl_map_divs_known(map
);
7061 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7062 for (i
= 1 ; i
< map
->n
; ++i
) {
7064 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7065 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7066 res
= isl_map_union_disjoint(res
, r2
);
7068 res
= isl_map_union(res
, r2
);
7075 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7077 return (struct isl_set
*)
7078 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7081 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7083 return (struct isl_set
*)
7084 isl_map_compute_divs((struct isl_map
*)set
);
7087 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7090 struct isl_set
*set
;
7095 map
= isl_map_cow(map
);
7099 set
= (struct isl_set
*)map
;
7100 set
->dim
= isl_space_domain(set
->dim
);
7103 for (i
= 0; i
< map
->n
; ++i
) {
7104 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7108 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7109 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7116 /* Return the union of "map1" and "map2", where we assume for now that
7117 * "map1" and "map2" are disjoint. Note that the basic maps inside
7118 * "map1" or "map2" may not be disjoint from each other.
7119 * Also note that this function is also called from isl_map_union,
7120 * which takes care of handling the situation where "map1" and "map2"
7121 * may not be disjoint.
7123 * If one of the inputs is empty, we can simply return the other input.
7124 * Similarly, if one of the inputs is universal, then it is equal to the union.
7126 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7127 __isl_take isl_map
*map2
)
7131 struct isl_map
*map
= NULL
;
7137 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7138 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7139 "spaces don't match", goto error
);
7150 is_universe
= isl_map_plain_is_universe(map1
);
7151 if (is_universe
< 0)
7158 is_universe
= isl_map_plain_is_universe(map2
);
7159 if (is_universe
< 0)
7166 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7167 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7168 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7170 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7171 map1
->n
+ map2
->n
, flags
);
7174 for (i
= 0; i
< map1
->n
; ++i
) {
7175 map
= isl_map_add_basic_map(map
,
7176 isl_basic_map_copy(map1
->p
[i
]));
7180 for (i
= 0; i
< map2
->n
; ++i
) {
7181 map
= isl_map_add_basic_map(map
,
7182 isl_basic_map_copy(map2
->p
[i
]));
7196 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7197 * guaranteed to be disjoint by the caller.
7199 * Note that this functions is called from within isl_map_make_disjoint,
7200 * so we have to be careful not to touch the constraints of the inputs
7203 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7204 __isl_take isl_map
*map2
)
7206 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7209 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7210 * not be disjoint. The parameters are assumed to have been aligned.
7212 * We currently simply call map_union_disjoint, the internal operation
7213 * of which does not really depend on the inputs being disjoint.
7214 * If the result contains more than one basic map, then we clear
7215 * the disjoint flag since the result may contain basic maps from
7216 * both inputs and these are not guaranteed to be disjoint.
7218 * As a special case, if "map1" and "map2" are obviously equal,
7219 * then we simply return "map1".
7221 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7222 __isl_take isl_map
*map2
)
7229 equal
= isl_map_plain_is_equal(map1
, map2
);
7237 map1
= map_union_disjoint(map1
, map2
);
7241 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7249 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7252 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7253 __isl_take isl_map
*map2
)
7255 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7258 struct isl_set
*isl_set_union_disjoint(
7259 struct isl_set
*set1
, struct isl_set
*set2
)
7261 return (struct isl_set
*)
7262 isl_map_union_disjoint(
7263 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7266 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7268 return (struct isl_set
*)
7269 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7272 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7275 * "map" and "set" are assumed to be compatible and non-NULL.
7277 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7278 __isl_take isl_set
*set
,
7279 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7280 __isl_take isl_basic_set
*bset
))
7283 struct isl_map
*result
;
7286 if (isl_set_plain_is_universe(set
)) {
7291 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7292 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7293 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7295 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7296 map
->n
* set
->n
, flags
);
7297 for (i
= 0; result
&& i
< map
->n
; ++i
)
7298 for (j
= 0; j
< set
->n
; ++j
) {
7299 result
= isl_map_add_basic_map(result
,
7300 fn(isl_basic_map_copy(map
->p
[i
]),
7301 isl_basic_set_copy(set
->p
[j
])));
7311 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7312 __isl_take isl_set
*set
)
7317 if (!isl_map_compatible_range(map
, set
))
7318 isl_die(set
->ctx
, isl_error_invalid
,
7319 "incompatible spaces", goto error
);
7321 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7328 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7329 __isl_take isl_set
*set
)
7331 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7334 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7335 __isl_take isl_set
*set
)
7340 if (!isl_map_compatible_domain(map
, set
))
7341 isl_die(set
->ctx
, isl_error_invalid
,
7342 "incompatible spaces", goto error
);
7344 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7351 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7352 __isl_take isl_set
*set
)
7354 return isl_map_align_params_map_map_and(map
, set
,
7355 &map_intersect_domain
);
7358 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7359 __isl_take isl_map
*map2
)
7363 map1
= isl_map_reverse(map1
);
7364 map1
= isl_map_apply_range(map1
, map2
);
7365 return isl_map_reverse(map1
);
7372 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7373 __isl_take isl_map
*map2
)
7375 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7378 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7379 __isl_take isl_map
*map2
)
7381 isl_space
*dim_result
;
7382 struct isl_map
*result
;
7388 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7389 isl_space_copy(map2
->dim
));
7391 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7394 for (i
= 0; i
< map1
->n
; ++i
)
7395 for (j
= 0; j
< map2
->n
; ++j
) {
7396 result
= isl_map_add_basic_map(result
,
7397 isl_basic_map_apply_range(
7398 isl_basic_map_copy(map1
->p
[i
]),
7399 isl_basic_map_copy(map2
->p
[j
])));
7405 if (result
&& result
->n
<= 1)
7406 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7414 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7415 __isl_take isl_map
*map2
)
7417 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7421 * returns range - domain
7423 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7425 isl_space
*target_space
;
7426 struct isl_basic_set
*bset
;
7433 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7434 bmap
->dim
, isl_dim_out
),
7436 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7437 dim
= isl_basic_map_n_in(bmap
);
7438 nparam
= isl_basic_map_n_param(bmap
);
7439 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7440 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7441 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7442 for (i
= 0; i
< dim
; ++i
) {
7443 int j
= isl_basic_map_alloc_equality(bmap
);
7445 bmap
= isl_basic_map_free(bmap
);
7448 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7449 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7450 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7451 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7453 bset
= isl_basic_map_domain(bmap
);
7454 bset
= isl_basic_set_reset_space(bset
, target_space
);
7457 isl_basic_map_free(bmap
);
7462 * returns range - domain
7464 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7468 struct isl_set
*result
;
7473 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7474 map
->dim
, isl_dim_out
),
7476 dim
= isl_map_get_space(map
);
7477 dim
= isl_space_domain(dim
);
7478 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7481 for (i
= 0; i
< map
->n
; ++i
)
7482 result
= isl_set_add_basic_set(result
,
7483 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7492 * returns [domain -> range] -> range - domain
7494 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7495 __isl_take isl_basic_map
*bmap
)
7499 isl_basic_map
*domain
;
7503 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7504 bmap
->dim
, isl_dim_out
))
7505 isl_die(bmap
->ctx
, isl_error_invalid
,
7506 "domain and range don't match", goto error
);
7508 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7509 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7511 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7512 domain
= isl_basic_map_universe(dim
);
7514 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7515 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7516 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7518 total
= isl_basic_map_total_dim(bmap
);
7520 for (i
= 0; i
< n
; ++i
) {
7521 k
= isl_basic_map_alloc_equality(bmap
);
7524 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7525 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7526 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7527 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7530 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7531 return isl_basic_map_finalize(bmap
);
7533 isl_basic_map_free(bmap
);
7538 * returns [domain -> range] -> range - domain
7540 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7543 isl_space
*domain_dim
;
7548 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7549 map
->dim
, isl_dim_out
))
7550 isl_die(map
->ctx
, isl_error_invalid
,
7551 "domain and range don't match", goto error
);
7553 map
= isl_map_cow(map
);
7557 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7558 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7559 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7562 for (i
= 0; i
< map
->n
; ++i
) {
7563 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7567 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7574 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7576 struct isl_basic_map
*bmap
;
7584 nparam
= dims
->nparam
;
7586 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7590 for (i
= 0; i
< dim
; ++i
) {
7591 int j
= isl_basic_map_alloc_equality(bmap
);
7594 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7595 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7596 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7598 return isl_basic_map_finalize(bmap
);
7600 isl_basic_map_free(bmap
);
7604 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7608 if (dim
->n_in
!= dim
->n_out
)
7609 isl_die(dim
->ctx
, isl_error_invalid
,
7610 "number of input and output dimensions needs to be "
7611 "the same", goto error
);
7612 return basic_map_identity(dim
);
7614 isl_space_free(dim
);
7618 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7620 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7623 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7625 isl_space
*dim
= isl_set_get_space(set
);
7627 id
= isl_map_identity(isl_space_map_from_set(dim
));
7628 return isl_map_intersect_range(id
, set
);
7631 /* Construct a basic set with all set dimensions having only non-negative
7634 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7635 __isl_take isl_space
*space
)
7640 struct isl_basic_set
*bset
;
7644 nparam
= space
->nparam
;
7646 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7649 for (i
= 0; i
< dim
; ++i
) {
7650 int k
= isl_basic_set_alloc_inequality(bset
);
7653 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7654 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7658 isl_basic_set_free(bset
);
7662 /* Construct the half-space x_pos >= 0.
7664 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7668 isl_basic_set
*nonneg
;
7670 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7671 k
= isl_basic_set_alloc_inequality(nonneg
);
7674 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7675 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7677 return isl_basic_set_finalize(nonneg
);
7679 isl_basic_set_free(nonneg
);
7683 /* Construct the half-space x_pos <= -1.
7685 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7690 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7691 k
= isl_basic_set_alloc_inequality(neg
);
7694 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7695 isl_int_set_si(neg
->ineq
[k
][0], -1);
7696 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7698 return isl_basic_set_finalize(neg
);
7700 isl_basic_set_free(neg
);
7704 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7705 enum isl_dim_type type
, unsigned first
, unsigned n
)
7709 isl_basic_set
*nonneg
;
7717 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7719 offset
= pos(set
->dim
, type
);
7720 for (i
= 0; i
< n
; ++i
) {
7721 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7722 offset
+ first
+ i
);
7723 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7725 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7734 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7735 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7742 if (isl_set_plain_is_empty(set
)) {
7747 return fn(set
, signs
, user
);
7750 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7752 half
= isl_set_intersect(half
, isl_set_copy(set
));
7753 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7757 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7759 half
= isl_set_intersect(half
, set
);
7760 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7766 /* Call "fn" on the intersections of "set" with each of the orthants
7767 * (except for obviously empty intersections). The orthant is identified
7768 * by the signs array, with each entry having value 1 or -1 according
7769 * to the sign of the corresponding variable.
7771 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7772 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7782 if (isl_set_plain_is_empty(set
))
7785 nparam
= isl_set_dim(set
, isl_dim_param
);
7786 nvar
= isl_set_dim(set
, isl_dim_set
);
7788 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7790 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7798 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7800 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7803 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7804 __isl_keep isl_basic_map
*bmap2
)
7807 struct isl_map
*map1
;
7808 struct isl_map
*map2
;
7810 if (!bmap1
|| !bmap2
)
7811 return isl_bool_error
;
7813 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7814 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7816 is_subset
= isl_map_is_subset(map1
, map2
);
7824 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7825 __isl_keep isl_basic_set
*bset2
)
7827 return isl_basic_map_is_subset(bset1
, bset2
);
7830 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7831 __isl_keep isl_basic_map
*bmap2
)
7835 if (!bmap1
|| !bmap2
)
7836 return isl_bool_error
;
7837 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7838 if (is_subset
!= isl_bool_true
)
7840 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7844 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7845 __isl_keep isl_basic_set
*bset2
)
7847 return isl_basic_map_is_equal(
7848 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7851 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7857 return isl_bool_error
;
7858 for (i
= 0; i
< map
->n
; ++i
) {
7859 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7861 return isl_bool_error
;
7863 return isl_bool_false
;
7865 return isl_bool_true
;
7868 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7870 return map
? map
->n
== 0 : isl_bool_error
;
7873 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7875 return set
? set
->n
== 0 : isl_bool_error
;
7878 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7880 return isl_map_is_empty((struct isl_map
*)set
);
7883 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7888 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7891 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7896 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7899 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7904 return isl_bool_error
;
7905 is_subset
= isl_map_is_subset(map1
, map2
);
7906 if (is_subset
!= isl_bool_true
)
7908 is_subset
= isl_map_is_subset(map2
, map1
);
7912 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7914 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7917 isl_bool
isl_basic_map_is_strict_subset(
7918 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7922 if (!bmap1
|| !bmap2
)
7923 return isl_bool_error
;
7924 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7925 if (is_subset
!= isl_bool_true
)
7927 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7928 if (is_subset
== isl_bool_error
)
7933 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7934 __isl_keep isl_map
*map2
)
7939 return isl_bool_error
;
7940 is_subset
= isl_map_is_subset(map1
, map2
);
7941 if (is_subset
!= isl_bool_true
)
7943 is_subset
= isl_map_is_subset(map2
, map1
);
7944 if (is_subset
== isl_bool_error
)
7949 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7950 __isl_keep isl_set
*set2
)
7952 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7955 /* Is "bmap" obviously equal to the universe with the same space?
7957 * That is, does it not have any constraints?
7959 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
7962 return isl_bool_error
;
7963 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7966 /* Is "bset" obviously equal to the universe with the same space?
7968 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
7970 return isl_basic_map_plain_is_universe(bset
);
7973 /* If "c" does not involve any existentially quantified variables,
7974 * then set *univ to false and abort
7976 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
7978 isl_bool
*univ
= user
;
7981 n
= isl_constraint_dim(c
, isl_dim_div
);
7982 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
7983 isl_constraint_free(c
);
7984 if (*univ
< 0 || !*univ
)
7985 return isl_stat_error
;
7989 /* Is "bmap" equal to the universe with the same space?
7991 * First check if it is obviously equal to the universe.
7992 * If not and if there are any constraints not involving
7993 * existentially quantified variables, then it is certainly
7994 * not equal to the universe.
7995 * Otherwise, check if the universe is a subset of "bmap".
7997 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8000 isl_basic_map
*test
;
8002 univ
= isl_basic_map_plain_is_universe(bmap
);
8003 if (univ
< 0 || univ
)
8005 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8006 return isl_bool_false
;
8007 univ
= isl_bool_true
;
8008 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8010 return isl_bool_error
;
8011 if (univ
< 0 || !univ
)
8013 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8014 univ
= isl_basic_map_is_subset(test
, bmap
);
8015 isl_basic_map_free(test
);
8019 /* Is "bset" equal to the universe with the same space?
8021 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8023 return isl_basic_map_is_universe(bset
);
8026 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8031 return isl_bool_error
;
8033 for (i
= 0; i
< map
->n
; ++i
) {
8034 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8039 return isl_bool_false
;
8042 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8044 return isl_map_plain_is_universe((isl_map
*) set
);
8047 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8049 struct isl_basic_set
*bset
= NULL
;
8050 struct isl_vec
*sample
= NULL
;
8051 isl_bool empty
, non_empty
;
8054 return isl_bool_error
;
8056 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8057 return isl_bool_true
;
8059 if (isl_basic_map_plain_is_universe(bmap
))
8060 return isl_bool_false
;
8062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8063 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8064 copy
= isl_basic_map_remove_redundancies(copy
);
8065 empty
= isl_basic_map_plain_is_empty(copy
);
8066 isl_basic_map_free(copy
);
8070 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8072 return isl_bool_error
;
8074 return isl_bool_false
;
8075 isl_vec_free(bmap
->sample
);
8076 bmap
->sample
= NULL
;
8077 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8079 return isl_bool_error
;
8080 sample
= isl_basic_set_sample_vec(bset
);
8082 return isl_bool_error
;
8083 empty
= sample
->size
== 0;
8084 isl_vec_free(bmap
->sample
);
8085 bmap
->sample
= sample
;
8087 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8092 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8095 return isl_bool_error
;
8096 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8099 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8102 return isl_bool_error
;
8103 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8106 /* Is "bmap" known to be non-empty?
8108 * That is, is the cached sample still valid?
8110 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8115 return isl_bool_error
;
8117 return isl_bool_false
;
8118 total
= 1 + isl_basic_map_total_dim(bmap
);
8119 if (bmap
->sample
->size
!= total
)
8120 return isl_bool_false
;
8121 return isl_basic_map_contains(bmap
, bmap
->sample
);
8124 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8126 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8129 struct isl_map
*isl_basic_map_union(
8130 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8132 struct isl_map
*map
;
8133 if (!bmap1
|| !bmap2
)
8136 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8138 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8141 map
= isl_map_add_basic_map(map
, bmap1
);
8142 map
= isl_map_add_basic_map(map
, bmap2
);
8145 isl_basic_map_free(bmap1
);
8146 isl_basic_map_free(bmap2
);
8150 struct isl_set
*isl_basic_set_union(
8151 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8153 return (struct isl_set
*)isl_basic_map_union(
8154 (struct isl_basic_map
*)bset1
,
8155 (struct isl_basic_map
*)bset2
);
8158 /* Order divs such that any div only depends on previous divs */
8159 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8167 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8169 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8171 if (isl_int_is_zero(bmap
->div
[i
][0]))
8173 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8178 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8179 "integer division depends on itself",
8180 return isl_basic_map_free(bmap
));
8181 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8187 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8189 return (struct isl_basic_set
*)
8190 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8193 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8200 for (i
= 0; i
< map
->n
; ++i
) {
8201 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8212 /* Apply the expansion computed by isl_merge_divs.
8213 * The expansion itself is given by "exp" while the resulting
8214 * list of divs is given by "div".
8216 * Move the integer divisions of "bset" into the right position
8217 * according to "exp" and then introduce the additional integer
8218 * divisions, adding div constraints.
8219 * The moving should be done first to avoid moving coefficients
8220 * in the definitions of the extra integer divisions.
8222 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8223 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8228 bset
= isl_basic_set_cow(bset
);
8232 if (div
->n_row
< bset
->n_div
)
8233 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8234 "not an expansion", goto error
);
8236 n_div
= bset
->n_div
;
8237 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8238 div
->n_row
- n_div
, 0,
8239 2 * (div
->n_row
- n_div
));
8241 for (i
= n_div
; i
< div
->n_row
; ++i
)
8242 if (isl_basic_set_alloc_div(bset
) < 0)
8245 for (j
= n_div
- 1; j
>= 0; --j
) {
8248 isl_basic_map_swap_div(bset
, j
, exp
[j
]);
8251 for (i
= 0; i
< div
->n_row
; ++i
) {
8252 if (j
< n_div
&& exp
[j
] == i
) {
8255 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8256 if (!isl_basic_map_div_is_known(bset
, i
))
8258 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8266 isl_basic_set_free(bset
);
8271 /* Look for a div in dst that corresponds to the div "div" in src.
8272 * The divs before "div" in src and dst are assumed to be the same.
8274 * Returns -1 if no corresponding div was found and the position
8275 * of the corresponding div in dst otherwise.
8277 static int find_div(struct isl_basic_map
*dst
,
8278 struct isl_basic_map
*src
, unsigned div
)
8282 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8284 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8285 for (i
= div
; i
< dst
->n_div
; ++i
)
8286 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8287 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8288 dst
->n_div
- div
) == -1)
8293 /* Align the divs of "dst" to those of "src", adding divs from "src"
8294 * if needed. That is, make sure that the first src->n_div divs
8295 * of the result are equal to those of src.
8297 * The result is not finalized as by design it will have redundant
8298 * divs if any divs from "src" were copied.
8300 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8301 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8304 int known
, extended
;
8308 return isl_basic_map_free(dst
);
8310 if (src
->n_div
== 0)
8313 known
= isl_basic_map_divs_known(src
);
8315 return isl_basic_map_free(dst
);
8317 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8318 "some src divs are unknown",
8319 return isl_basic_map_free(dst
));
8321 src
= isl_basic_map_order_divs(src
);
8324 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8325 for (i
= 0; i
< src
->n_div
; ++i
) {
8326 int j
= find_div(dst
, src
, i
);
8329 int extra
= src
->n_div
- i
;
8330 dst
= isl_basic_map_cow(dst
);
8333 dst
= isl_basic_map_extend_space(dst
,
8334 isl_space_copy(dst
->dim
),
8335 extra
, 0, 2 * extra
);
8338 j
= isl_basic_map_alloc_div(dst
);
8340 return isl_basic_map_free(dst
);
8341 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8342 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8343 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8344 return isl_basic_map_free(dst
);
8347 isl_basic_map_swap_div(dst
, i
, j
);
8352 struct isl_basic_set
*isl_basic_set_align_divs(
8353 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8355 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8356 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8359 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8367 map
= isl_map_compute_divs(map
);
8368 map
= isl_map_cow(map
);
8372 for (i
= 1; i
< map
->n
; ++i
)
8373 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8374 for (i
= 1; i
< map
->n
; ++i
) {
8375 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8377 return isl_map_free(map
);
8380 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8384 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8386 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8389 /* Align the divs of the basic maps in "map" to those
8390 * of the basic maps in "list", as well as to the other basic maps in "map".
8391 * The elements in "list" are assumed to have known divs.
8393 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8394 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8398 map
= isl_map_compute_divs(map
);
8399 map
= isl_map_cow(map
);
8401 return isl_map_free(map
);
8405 n
= isl_basic_map_list_n_basic_map(list
);
8406 for (i
= 0; i
< n
; ++i
) {
8407 isl_basic_map
*bmap
;
8409 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8410 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8411 isl_basic_map_free(bmap
);
8414 return isl_map_free(map
);
8416 return isl_map_align_divs(map
);
8419 /* Align the divs of each element of "list" to those of "bmap".
8420 * Both "bmap" and the elements of "list" are assumed to have known divs.
8422 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8423 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8428 return isl_basic_map_list_free(list
);
8430 n
= isl_basic_map_list_n_basic_map(list
);
8431 for (i
= 0; i
< n
; ++i
) {
8432 isl_basic_map
*bmap_i
;
8434 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8435 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8436 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8442 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8443 __isl_take isl_map
*map
)
8447 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8448 map
= isl_map_intersect_domain(map
, set
);
8449 set
= isl_map_range(map
);
8457 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8458 __isl_take isl_map
*map
)
8460 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8463 /* There is no need to cow as removing empty parts doesn't change
8464 * the meaning of the set.
8466 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8473 for (i
= map
->n
- 1; i
>= 0; --i
)
8474 remove_if_empty(map
, i
);
8479 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8481 return (struct isl_set
*)
8482 isl_map_remove_empty_parts((struct isl_map
*)set
);
8485 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8487 struct isl_basic_map
*bmap
;
8488 if (!map
|| map
->n
== 0)
8490 bmap
= map
->p
[map
->n
-1];
8491 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8492 return isl_basic_map_copy(bmap
);
8495 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8497 return (struct isl_basic_set
*)
8498 isl_map_copy_basic_map((struct isl_map
*)set
);
8501 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8502 __isl_keep isl_basic_map
*bmap
)
8508 for (i
= map
->n
-1; i
>= 0; --i
) {
8509 if (map
->p
[i
] != bmap
)
8511 map
= isl_map_cow(map
);
8514 isl_basic_map_free(map
->p
[i
]);
8515 if (i
!= map
->n
-1) {
8516 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8517 map
->p
[i
] = map
->p
[map
->n
-1];
8528 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8529 struct isl_basic_set
*bset
)
8531 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8532 (struct isl_basic_map
*)bset
);
8535 /* Given two basic sets bset1 and bset2, compute the maximal difference
8536 * between the values of dimension pos in bset1 and those in bset2
8537 * for any common value of the parameters and dimensions preceding pos.
8539 static enum isl_lp_result
basic_set_maximal_difference_at(
8540 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8541 int pos
, isl_int
*opt
)
8544 struct isl_basic_map
*bmap1
= NULL
;
8545 struct isl_basic_map
*bmap2
= NULL
;
8546 struct isl_ctx
*ctx
;
8547 struct isl_vec
*obj
;
8550 unsigned dim1
, dim2
;
8551 enum isl_lp_result res
;
8553 if (!bset1
|| !bset2
)
8554 return isl_lp_error
;
8556 nparam
= isl_basic_set_n_param(bset1
);
8557 dim1
= isl_basic_set_n_dim(bset1
);
8558 dim2
= isl_basic_set_n_dim(bset2
);
8559 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8560 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8561 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8562 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8563 if (!bmap1
|| !bmap2
)
8565 bmap1
= isl_basic_map_cow(bmap1
);
8566 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8567 pos
, (dim1
- pos
) + (dim2
- pos
),
8568 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8569 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8572 total
= isl_basic_map_total_dim(bmap1
);
8574 obj
= isl_vec_alloc(ctx
, 1 + total
);
8577 isl_seq_clr(obj
->block
.data
, 1 + total
);
8578 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8579 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8580 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8582 isl_basic_map_free(bmap1
);
8586 isl_basic_map_free(bmap2
);
8588 isl_basic_map_free(bmap1
);
8589 return isl_lp_error
;
8592 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8593 * for any common value of the parameters and dimensions preceding pos
8594 * in both basic sets, the values of dimension pos in bset1 are
8595 * smaller or larger than those in bset2.
8598 * 1 if bset1 follows bset2
8599 * -1 if bset1 precedes bset2
8600 * 0 if bset1 and bset2 are incomparable
8601 * -2 if some error occurred.
8603 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8604 struct isl_basic_set
*bset2
, int pos
)
8607 enum isl_lp_result res
;
8612 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8614 if (res
== isl_lp_empty
)
8616 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8617 res
== isl_lp_unbounded
)
8619 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8628 /* Given two basic sets bset1 and bset2, check whether
8629 * for any common value of the parameters and dimensions preceding pos
8630 * there is a value of dimension pos in bset1 that is larger
8631 * than a value of the same dimension in bset2.
8634 * 1 if there exists such a pair
8635 * 0 if there is no such pair, but there is a pair of equal values
8637 * -2 if some error occurred.
8639 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8640 __isl_keep isl_basic_set
*bset2
, int pos
)
8643 enum isl_lp_result res
;
8648 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8650 if (res
== isl_lp_empty
)
8652 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8653 res
== isl_lp_unbounded
)
8655 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8657 else if (res
== isl_lp_ok
)
8666 /* Given two sets set1 and set2, check whether
8667 * for any common value of the parameters and dimensions preceding pos
8668 * there is a value of dimension pos in set1 that is larger
8669 * than a value of the same dimension in set2.
8672 * 1 if there exists such a pair
8673 * 0 if there is no such pair, but there is a pair of equal values
8675 * -2 if some error occurred.
8677 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8678 __isl_keep isl_set
*set2
, int pos
)
8686 for (i
= 0; i
< set1
->n
; ++i
)
8687 for (j
= 0; j
< set2
->n
; ++j
) {
8689 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8690 if (f
== 1 || f
== -2)
8699 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8700 unsigned pos
, isl_int
*val
)
8708 total
= isl_basic_map_total_dim(bmap
);
8709 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8710 for (; d
+1 > pos
; --d
)
8711 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8715 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8717 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8719 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8722 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8728 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8729 unsigned pos
, isl_int
*val
)
8741 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8744 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8745 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8746 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8747 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8751 isl_int_set(*val
, v
);
8757 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8758 unsigned pos
, isl_int
*val
)
8760 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8764 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8767 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8770 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8771 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8773 if (pos
>= isl_basic_map_dim(bmap
, type
))
8775 return isl_basic_map_plain_has_fixed_var(bmap
,
8776 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8779 /* If "bmap" obviously lies on a hyperplane where the given dimension
8780 * has a fixed value, then return that value.
8781 * Otherwise return NaN.
8783 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8784 __isl_keep isl_basic_map
*bmap
,
8785 enum isl_dim_type type
, unsigned pos
)
8793 ctx
= isl_basic_map_get_ctx(bmap
);
8794 v
= isl_val_alloc(ctx
);
8797 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8799 return isl_val_free(v
);
8801 isl_int_set_si(v
->d
, 1);
8805 return isl_val_nan(ctx
);
8808 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8809 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8811 if (pos
>= isl_map_dim(map
, type
))
8813 return isl_map_plain_has_fixed_var(map
,
8814 map_offset(map
, type
) - 1 + pos
, val
);
8817 /* If "map" obviously lies on a hyperplane where the given dimension
8818 * has a fixed value, then return that value.
8819 * Otherwise return NaN.
8821 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8822 enum isl_dim_type type
, unsigned pos
)
8830 ctx
= isl_map_get_ctx(map
);
8831 v
= isl_val_alloc(ctx
);
8834 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8836 return isl_val_free(v
);
8838 isl_int_set_si(v
->d
, 1);
8842 return isl_val_nan(ctx
);
8845 /* If "set" obviously lies on a hyperplane where the given dimension
8846 * has a fixed value, then return that value.
8847 * Otherwise return NaN.
8849 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8850 enum isl_dim_type type
, unsigned pos
)
8852 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8855 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8856 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8858 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8861 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8862 * then return this fixed value in *val.
8864 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8865 unsigned dim
, isl_int
*val
)
8867 return isl_basic_set_plain_has_fixed_var(bset
,
8868 isl_basic_set_n_param(bset
) + dim
, val
);
8871 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8872 * then return this fixed value in *val.
8874 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8875 unsigned dim
, isl_int
*val
)
8877 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8880 /* Check if input variable in has fixed value and if so and if val is not NULL,
8881 * then return this fixed value in *val.
8883 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8884 unsigned in
, isl_int
*val
)
8886 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8889 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8890 * and if val is not NULL, then return this lower bound in *val.
8892 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8893 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8895 int i
, i_eq
= -1, i_ineq
= -1;
8902 total
= isl_basic_set_total_dim(bset
);
8903 nparam
= isl_basic_set_n_param(bset
);
8904 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8905 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8911 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8912 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8914 if (i_eq
!= -1 || i_ineq
!= -1)
8918 if (i_eq
== -1 && i_ineq
== -1)
8920 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8921 /* The coefficient should always be one due to normalization. */
8922 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8924 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8926 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8927 total
- nparam
- dim
- 1) != -1)
8930 isl_int_neg(*val
, c
[0]);
8934 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8935 unsigned dim
, isl_int
*val
)
8947 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8951 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8953 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8954 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8956 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8960 isl_int_set(*val
, v
);
8966 /* Return -1 if the constraint "c1" should be sorted before "c2"
8967 * and 1 if it should be sorted after "c2".
8968 * Return 0 if the two constraints are the same (up to the constant term).
8970 * In particular, if a constraint involves later variables than another
8971 * then it is sorted after this other constraint.
8972 * uset_gist depends on constraints without existentially quantified
8973 * variables sorting first.
8975 * For constraints that have the same latest variable, those
8976 * with the same coefficient for this latest variable (first in absolute value
8977 * and then in actual value) are grouped together.
8978 * This is useful for detecting pairs of constraints that can
8979 * be chained in their printed representation.
8981 * Finally, within a group, constraints are sorted according to
8982 * their coefficients (excluding the constant term).
8984 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8986 isl_int
**c1
= (isl_int
**) p1
;
8987 isl_int
**c2
= (isl_int
**) p2
;
8989 unsigned size
= *(unsigned *) arg
;
8992 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8993 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8998 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9001 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9005 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9008 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9009 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9010 * and 0 if the two constraints are the same (up to the constant term).
9012 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9013 isl_int
*c1
, isl_int
*c2
)
9019 total
= isl_basic_map_total_dim(bmap
);
9020 return sort_constraint_cmp(&c1
, &c2
, &total
);
9023 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9024 __isl_take isl_basic_map
*bmap
)
9030 if (bmap
->n_ineq
== 0)
9032 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9034 total
= isl_basic_map_total_dim(bmap
);
9035 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9036 &sort_constraint_cmp
, &total
) < 0)
9037 return isl_basic_map_free(bmap
);
9041 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9042 __isl_take isl_basic_set
*bset
)
9044 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9045 (struct isl_basic_map
*)bset
);
9048 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9052 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9054 bmap
= isl_basic_map_remove_redundancies(bmap
);
9055 bmap
= isl_basic_map_sort_constraints(bmap
);
9057 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9061 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9063 return (struct isl_basic_set
*)isl_basic_map_normalize(
9064 (struct isl_basic_map
*)bset
);
9067 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9068 const __isl_keep isl_basic_map
*bmap2
)
9073 if (!bmap1
|| !bmap2
)
9078 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9079 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9080 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9081 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9082 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9083 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9084 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9085 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9086 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9087 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9088 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9090 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9092 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9094 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9095 return bmap1
->n_eq
- bmap2
->n_eq
;
9096 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9097 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9098 if (bmap1
->n_div
!= bmap2
->n_div
)
9099 return bmap1
->n_div
- bmap2
->n_div
;
9100 total
= isl_basic_map_total_dim(bmap1
);
9101 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9102 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9106 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9107 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9111 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9112 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9119 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9120 const __isl_keep isl_basic_set
*bset2
)
9122 return isl_basic_map_plain_cmp(bset1
, bset2
);
9125 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9131 if (set1
->n
!= set2
->n
)
9132 return set1
->n
- set2
->n
;
9134 for (i
= 0; i
< set1
->n
; ++i
) {
9135 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9143 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9144 __isl_keep isl_basic_map
*bmap2
)
9146 if (!bmap1
|| !bmap2
)
9147 return isl_bool_error
;
9148 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9151 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9152 __isl_keep isl_basic_set
*bset2
)
9154 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9155 (isl_basic_map
*)bset2
);
9158 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9160 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9161 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9163 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9166 /* Sort the basic maps of "map" and remove duplicate basic maps.
9168 * While removing basic maps, we make sure that the basic maps remain
9169 * sorted because isl_map_normalize expects the basic maps of the result
9172 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9176 map
= isl_map_remove_empty_parts(map
);
9179 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9180 for (i
= map
->n
- 1; i
>= 1; --i
) {
9181 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9183 isl_basic_map_free(map
->p
[i
-1]);
9184 for (j
= i
; j
< map
->n
; ++j
)
9185 map
->p
[j
- 1] = map
->p
[j
];
9192 /* Remove obvious duplicates among the basic maps of "map".
9194 * Unlike isl_map_normalize, this function does not remove redundant
9195 * constraints and only removes duplicates that have exactly the same
9196 * constraints in the input. It does sort the constraints and
9197 * the basic maps to ease the detection of duplicates.
9199 * If "map" has already been normalized or if the basic maps are
9200 * disjoint, then there can be no duplicates.
9202 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9205 isl_basic_map
*bmap
;
9211 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9213 for (i
= 0; i
< map
->n
; ++i
) {
9214 bmap
= isl_basic_map_copy(map
->p
[i
]);
9215 bmap
= isl_basic_map_sort_constraints(bmap
);
9217 return isl_map_free(map
);
9218 isl_basic_map_free(map
->p
[i
]);
9222 map
= sort_and_remove_duplicates(map
);
9226 /* We normalize in place, but if anything goes wrong we need
9227 * to return NULL, so we need to make sure we don't change the
9228 * meaning of any possible other copies of map.
9230 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9233 struct isl_basic_map
*bmap
;
9237 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9239 for (i
= 0; i
< map
->n
; ++i
) {
9240 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9243 isl_basic_map_free(map
->p
[i
]);
9247 map
= sort_and_remove_duplicates(map
);
9249 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9256 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9258 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9261 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9262 __isl_keep isl_map
*map2
)
9268 return isl_bool_error
;
9271 return isl_bool_true
;
9272 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9273 return isl_bool_false
;
9275 map1
= isl_map_copy(map1
);
9276 map2
= isl_map_copy(map2
);
9277 map1
= isl_map_normalize(map1
);
9278 map2
= isl_map_normalize(map2
);
9281 equal
= map1
->n
== map2
->n
;
9282 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9283 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9293 return isl_bool_error
;
9296 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9297 __isl_keep isl_set
*set2
)
9299 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9300 (struct isl_map
*)set2
);
9303 /* Return an interval that ranges from min to max (inclusive)
9305 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9306 isl_int min
, isl_int max
)
9309 struct isl_basic_set
*bset
= NULL
;
9311 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9315 k
= isl_basic_set_alloc_inequality(bset
);
9318 isl_int_set_si(bset
->ineq
[k
][1], 1);
9319 isl_int_neg(bset
->ineq
[k
][0], min
);
9321 k
= isl_basic_set_alloc_inequality(bset
);
9324 isl_int_set_si(bset
->ineq
[k
][1], -1);
9325 isl_int_set(bset
->ineq
[k
][0], max
);
9329 isl_basic_set_free(bset
);
9333 /* Return the basic maps in "map" as a list.
9335 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9336 __isl_keep isl_map
*map
)
9340 isl_basic_map_list
*list
;
9344 ctx
= isl_map_get_ctx(map
);
9345 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9347 for (i
= 0; i
< map
->n
; ++i
) {
9348 isl_basic_map
*bmap
;
9350 bmap
= isl_basic_map_copy(map
->p
[i
]);
9351 list
= isl_basic_map_list_add(list
, bmap
);
9357 /* Return the intersection of the elements in the non-empty list "list".
9358 * All elements are assumed to live in the same space.
9360 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9361 __isl_take isl_basic_map_list
*list
)
9364 isl_basic_map
*bmap
;
9368 n
= isl_basic_map_list_n_basic_map(list
);
9370 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9371 "expecting non-empty list", goto error
);
9373 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9374 for (i
= 1; i
< n
; ++i
) {
9375 isl_basic_map
*bmap_i
;
9377 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9378 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9381 isl_basic_map_list_free(list
);
9384 isl_basic_map_list_free(list
);
9388 /* Return the intersection of the elements in the non-empty list "list".
9389 * All elements are assumed to live in the same space.
9391 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9392 __isl_take isl_basic_set_list
*list
)
9394 return isl_basic_map_list_intersect(list
);
9397 /* Return the union of the elements in the non-empty list "list".
9398 * All elements are assumed to live in the same space.
9400 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9407 n
= isl_set_list_n_set(list
);
9409 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9410 "expecting non-empty list", goto error
);
9412 set
= isl_set_list_get_set(list
, 0);
9413 for (i
= 1; i
< n
; ++i
) {
9416 set_i
= isl_set_list_get_set(list
, i
);
9417 set
= isl_set_union(set
, set_i
);
9420 isl_set_list_free(list
);
9423 isl_set_list_free(list
);
9427 /* Return the Cartesian product of the basic sets in list (in the given order).
9429 __isl_give isl_basic_set
*isl_basic_set_list_product(
9430 __isl_take
struct isl_basic_set_list
*list
)
9438 struct isl_basic_set
*product
= NULL
;
9442 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9443 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9444 nparam
= isl_basic_set_n_param(list
->p
[0]);
9445 dim
= isl_basic_set_n_dim(list
->p
[0]);
9446 extra
= list
->p
[0]->n_div
;
9447 n_eq
= list
->p
[0]->n_eq
;
9448 n_ineq
= list
->p
[0]->n_ineq
;
9449 for (i
= 1; i
< list
->n
; ++i
) {
9450 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9451 isl_assert(list
->ctx
,
9452 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9453 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9454 extra
+= list
->p
[i
]->n_div
;
9455 n_eq
+= list
->p
[i
]->n_eq
;
9456 n_ineq
+= list
->p
[i
]->n_ineq
;
9458 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9463 for (i
= 0; i
< list
->n
; ++i
) {
9464 isl_basic_set_add_constraints(product
,
9465 isl_basic_set_copy(list
->p
[i
]), dim
);
9466 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9468 isl_basic_set_list_free(list
);
9471 isl_basic_set_free(product
);
9472 isl_basic_set_list_free(list
);
9476 struct isl_basic_map
*isl_basic_map_product(
9477 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9479 isl_space
*dim_result
= NULL
;
9480 struct isl_basic_map
*bmap
;
9481 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9482 struct isl_dim_map
*dim_map1
, *dim_map2
;
9484 if (!bmap1
|| !bmap2
)
9487 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9488 bmap2
->dim
, isl_dim_param
), goto error
);
9489 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9490 isl_space_copy(bmap2
->dim
));
9492 in1
= isl_basic_map_n_in(bmap1
);
9493 in2
= isl_basic_map_n_in(bmap2
);
9494 out1
= isl_basic_map_n_out(bmap1
);
9495 out2
= isl_basic_map_n_out(bmap2
);
9496 nparam
= isl_basic_map_n_param(bmap1
);
9498 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9499 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9500 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9501 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9502 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9503 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9504 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9505 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9506 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9507 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9508 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9510 bmap
= isl_basic_map_alloc_space(dim_result
,
9511 bmap1
->n_div
+ bmap2
->n_div
,
9512 bmap1
->n_eq
+ bmap2
->n_eq
,
9513 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9514 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9515 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9516 bmap
= isl_basic_map_simplify(bmap
);
9517 return isl_basic_map_finalize(bmap
);
9519 isl_basic_map_free(bmap1
);
9520 isl_basic_map_free(bmap2
);
9524 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9525 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9527 isl_basic_map
*prod
;
9529 prod
= isl_basic_map_product(bmap1
, bmap2
);
9530 prod
= isl_basic_map_flatten(prod
);
9534 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9535 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9537 return isl_basic_map_flat_range_product(bset1
, bset2
);
9540 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9541 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9543 isl_space
*space_result
= NULL
;
9544 isl_basic_map
*bmap
;
9545 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9546 struct isl_dim_map
*dim_map1
, *dim_map2
;
9548 if (!bmap1
|| !bmap2
)
9551 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9552 isl_space_copy(bmap2
->dim
));
9554 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9555 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9556 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9557 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9559 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9560 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9561 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9562 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9563 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9564 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9565 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9566 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9567 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9568 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9569 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9571 bmap
= isl_basic_map_alloc_space(space_result
,
9572 bmap1
->n_div
+ bmap2
->n_div
,
9573 bmap1
->n_eq
+ bmap2
->n_eq
,
9574 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9575 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9576 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9577 bmap
= isl_basic_map_simplify(bmap
);
9578 return isl_basic_map_finalize(bmap
);
9580 isl_basic_map_free(bmap1
);
9581 isl_basic_map_free(bmap2
);
9585 __isl_give isl_basic_map
*isl_basic_map_range_product(
9586 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9588 isl_space
*dim_result
= NULL
;
9589 isl_basic_map
*bmap
;
9590 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9591 struct isl_dim_map
*dim_map1
, *dim_map2
;
9593 if (!bmap1
|| !bmap2
)
9596 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9597 bmap2
->dim
, isl_dim_param
))
9598 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9599 "parameters don't match", goto error
);
9601 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9602 isl_space_copy(bmap2
->dim
));
9604 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9605 out1
= isl_basic_map_n_out(bmap1
);
9606 out2
= isl_basic_map_n_out(bmap2
);
9607 nparam
= isl_basic_map_n_param(bmap1
);
9609 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9610 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9611 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9612 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9613 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9614 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9615 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9616 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9617 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9618 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9619 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9621 bmap
= isl_basic_map_alloc_space(dim_result
,
9622 bmap1
->n_div
+ bmap2
->n_div
,
9623 bmap1
->n_eq
+ bmap2
->n_eq
,
9624 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9625 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9626 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9627 bmap
= isl_basic_map_simplify(bmap
);
9628 return isl_basic_map_finalize(bmap
);
9630 isl_basic_map_free(bmap1
);
9631 isl_basic_map_free(bmap2
);
9635 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9636 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9638 isl_basic_map
*prod
;
9640 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9641 prod
= isl_basic_map_flatten_range(prod
);
9645 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9646 * and collect the results.
9647 * The result live in the space obtained by calling "space_product"
9648 * on the spaces of "map1" and "map2".
9649 * If "remove_duplicates" is set then the result may contain duplicates
9650 * (even if the inputs do not) and so we try and remove the obvious
9653 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9654 __isl_take isl_map
*map2
,
9655 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9656 __isl_take isl_space
*right
),
9657 __isl_give isl_basic_map
*(*basic_map_product
)(
9658 __isl_take isl_basic_map
*left
,
9659 __isl_take isl_basic_map
*right
),
9660 int remove_duplicates
)
9663 struct isl_map
*result
;
9669 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9670 map2
->dim
, isl_dim_param
), goto error
);
9672 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9673 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9674 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9676 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9677 isl_space_copy(map2
->dim
)),
9678 map1
->n
* map2
->n
, flags
);
9681 for (i
= 0; i
< map1
->n
; ++i
)
9682 for (j
= 0; j
< map2
->n
; ++j
) {
9683 struct isl_basic_map
*part
;
9684 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9685 isl_basic_map_copy(map2
->p
[j
]));
9686 if (isl_basic_map_is_empty(part
))
9687 isl_basic_map_free(part
);
9689 result
= isl_map_add_basic_map(result
, part
);
9693 if (remove_duplicates
)
9694 result
= isl_map_remove_obvious_duplicates(result
);
9704 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9706 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9707 __isl_take isl_map
*map2
)
9709 return map_product(map1
, map2
, &isl_space_product
,
9710 &isl_basic_map_product
, 0);
9713 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9714 __isl_take isl_map
*map2
)
9716 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9719 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9721 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9722 __isl_take isl_map
*map2
)
9726 prod
= isl_map_product(map1
, map2
);
9727 prod
= isl_map_flatten(prod
);
9731 /* Given two set A and B, construct its Cartesian product A x B.
9733 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9735 return isl_map_range_product(set1
, set2
);
9738 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9739 __isl_take isl_set
*set2
)
9741 return isl_map_flat_range_product(set1
, set2
);
9744 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9746 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9747 __isl_take isl_map
*map2
)
9749 return map_product(map1
, map2
, &isl_space_domain_product
,
9750 &isl_basic_map_domain_product
, 1);
9753 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9755 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9756 __isl_take isl_map
*map2
)
9758 return map_product(map1
, map2
, &isl_space_range_product
,
9759 &isl_basic_map_range_product
, 1);
9762 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9763 __isl_take isl_map
*map2
)
9765 return isl_map_align_params_map_map_and(map1
, map2
,
9766 &map_domain_product_aligned
);
9769 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9770 __isl_take isl_map
*map2
)
9772 return isl_map_align_params_map_map_and(map1
, map2
,
9773 &map_range_product_aligned
);
9776 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9778 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9781 int total1
, keep1
, total2
, keep2
;
9785 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9786 !isl_space_range_is_wrapping(map
->dim
))
9787 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9788 "not a product", return isl_map_free(map
));
9790 space
= isl_map_get_space(map
);
9791 total1
= isl_space_dim(space
, isl_dim_in
);
9792 total2
= isl_space_dim(space
, isl_dim_out
);
9793 space
= isl_space_factor_domain(space
);
9794 keep1
= isl_space_dim(space
, isl_dim_in
);
9795 keep2
= isl_space_dim(space
, isl_dim_out
);
9796 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9797 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9798 map
= isl_map_reset_space(map
, space
);
9803 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9805 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9808 int total1
, keep1
, total2
, keep2
;
9812 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9813 !isl_space_range_is_wrapping(map
->dim
))
9814 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9815 "not a product", return isl_map_free(map
));
9817 space
= isl_map_get_space(map
);
9818 total1
= isl_space_dim(space
, isl_dim_in
);
9819 total2
= isl_space_dim(space
, isl_dim_out
);
9820 space
= isl_space_factor_range(space
);
9821 keep1
= isl_space_dim(space
, isl_dim_in
);
9822 keep2
= isl_space_dim(space
, isl_dim_out
);
9823 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9824 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9825 map
= isl_map_reset_space(map
, space
);
9830 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9832 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9839 if (!isl_space_domain_is_wrapping(map
->dim
))
9840 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9841 "domain is not a product", return isl_map_free(map
));
9843 space
= isl_map_get_space(map
);
9844 total
= isl_space_dim(space
, isl_dim_in
);
9845 space
= isl_space_domain_factor_domain(space
);
9846 keep
= isl_space_dim(space
, isl_dim_in
);
9847 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9848 map
= isl_map_reset_space(map
, space
);
9853 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9855 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9862 if (!isl_space_domain_is_wrapping(map
->dim
))
9863 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9864 "domain is not a product", return isl_map_free(map
));
9866 space
= isl_map_get_space(map
);
9867 total
= isl_space_dim(space
, isl_dim_in
);
9868 space
= isl_space_domain_factor_range(space
);
9869 keep
= isl_space_dim(space
, isl_dim_in
);
9870 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9871 map
= isl_map_reset_space(map
, space
);
9876 /* Given a map A -> [B -> C], extract the map A -> B.
9878 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9885 if (!isl_space_range_is_wrapping(map
->dim
))
9886 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9887 "range is not a product", return isl_map_free(map
));
9889 space
= isl_map_get_space(map
);
9890 total
= isl_space_dim(space
, isl_dim_out
);
9891 space
= isl_space_range_factor_domain(space
);
9892 keep
= isl_space_dim(space
, isl_dim_out
);
9893 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9894 map
= isl_map_reset_space(map
, space
);
9899 /* Given a map A -> [B -> C], extract the map A -> C.
9901 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9908 if (!isl_space_range_is_wrapping(map
->dim
))
9909 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9910 "range is not a product", return isl_map_free(map
));
9912 space
= isl_map_get_space(map
);
9913 total
= isl_space_dim(space
, isl_dim_out
);
9914 space
= isl_space_range_factor_range(space
);
9915 keep
= isl_space_dim(space
, isl_dim_out
);
9916 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9917 map
= isl_map_reset_space(map
, space
);
9922 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9924 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9925 __isl_take isl_map
*map2
)
9929 prod
= isl_map_domain_product(map1
, map2
);
9930 prod
= isl_map_flatten_domain(prod
);
9934 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9936 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9937 __isl_take isl_map
*map2
)
9941 prod
= isl_map_range_product(map1
, map2
);
9942 prod
= isl_map_flatten_range(prod
);
9946 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9949 uint32_t hash
= isl_hash_init();
9954 bmap
= isl_basic_map_copy(bmap
);
9955 bmap
= isl_basic_map_normalize(bmap
);
9958 total
= isl_basic_map_total_dim(bmap
);
9959 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9960 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9962 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9963 isl_hash_hash(hash
, c_hash
);
9965 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9966 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9968 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9969 isl_hash_hash(hash
, c_hash
);
9971 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9972 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9974 if (isl_int_is_zero(bmap
->div
[i
][0]))
9976 isl_hash_byte(hash
, i
& 0xFF);
9977 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9978 isl_hash_hash(hash
, c_hash
);
9980 isl_basic_map_free(bmap
);
9984 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9986 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9989 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9996 map
= isl_map_copy(map
);
9997 map
= isl_map_normalize(map
);
10001 hash
= isl_hash_init();
10002 for (i
= 0; i
< map
->n
; ++i
) {
10003 uint32_t bmap_hash
;
10004 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10005 isl_hash_hash(hash
, bmap_hash
);
10013 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10015 return isl_map_get_hash((isl_map
*)set
);
10018 /* Check if the value for dimension dim is completely determined
10019 * by the values of the other parameters and variables.
10020 * That is, check if dimension dim is involved in an equality.
10022 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10029 nparam
= isl_basic_set_n_param(bset
);
10030 for (i
= 0; i
< bset
->n_eq
; ++i
)
10031 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10036 /* Check if the value for dimension dim is completely determined
10037 * by the values of the other parameters and variables.
10038 * That is, check if dimension dim is involved in an equality
10039 * for each of the subsets.
10041 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10047 for (i
= 0; i
< set
->n
; ++i
) {
10049 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10056 /* Return the number of basic maps in the (current) representation of "map".
10058 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10060 return map
? map
->n
: 0;
10063 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10065 return set
? set
->n
: 0;
10068 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10069 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10074 return isl_stat_error
;
10076 for (i
= 0; i
< map
->n
; ++i
)
10077 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10078 return isl_stat_error
;
10080 return isl_stat_ok
;
10083 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10084 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10089 return isl_stat_error
;
10091 for (i
= 0; i
< set
->n
; ++i
)
10092 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10093 return isl_stat_error
;
10095 return isl_stat_ok
;
10098 /* Return a list of basic sets, the union of which is equal to "set".
10100 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10101 __isl_keep isl_set
*set
)
10104 isl_basic_set_list
*list
;
10109 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10110 for (i
= 0; i
< set
->n
; ++i
) {
10111 isl_basic_set
*bset
;
10113 bset
= isl_basic_set_copy(set
->p
[i
]);
10114 list
= isl_basic_set_list_add(list
, bset
);
10120 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10127 bset
= isl_basic_set_cow(bset
);
10131 dim
= isl_basic_set_get_space(bset
);
10132 dim
= isl_space_lift(dim
, bset
->n_div
);
10135 isl_space_free(bset
->dim
);
10137 bset
->extra
-= bset
->n_div
;
10140 bset
= isl_basic_set_finalize(bset
);
10144 isl_basic_set_free(bset
);
10148 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10154 set
= isl_set_align_divs(set
);
10159 set
= isl_set_cow(set
);
10163 n_div
= set
->p
[0]->n_div
;
10164 dim
= isl_set_get_space(set
);
10165 dim
= isl_space_lift(dim
, n_div
);
10168 isl_space_free(set
->dim
);
10171 for (i
= 0; i
< set
->n
; ++i
) {
10172 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10183 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10186 struct isl_basic_map
*bmap
;
10193 set
= isl_set_align_divs(set
);
10198 dim
= isl_set_get_space(set
);
10199 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10201 return isl_map_identity(isl_space_map_from_set(dim
));
10204 n_div
= set
->p
[0]->n_div
;
10205 dim
= isl_space_map_from_set(dim
);
10206 n_param
= isl_space_dim(dim
, isl_dim_param
);
10207 n_set
= isl_space_dim(dim
, isl_dim_in
);
10208 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10209 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10210 for (i
= 0; i
< n_set
; ++i
)
10211 bmap
= var_equal(bmap
, i
);
10213 total
= n_param
+ n_set
+ n_set
+ n_div
;
10214 for (i
= 0; i
< n_div
; ++i
) {
10215 k
= isl_basic_map_alloc_inequality(bmap
);
10218 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10219 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10220 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10221 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10222 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10223 set
->p
[0]->div
[i
][0]);
10225 l
= isl_basic_map_alloc_inequality(bmap
);
10228 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10229 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10230 set
->p
[0]->div
[i
][0]);
10231 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10235 bmap
= isl_basic_map_simplify(bmap
);
10236 bmap
= isl_basic_map_finalize(bmap
);
10237 return isl_map_from_basic_map(bmap
);
10240 isl_basic_map_free(bmap
);
10244 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10252 dim
= isl_basic_set_total_dim(bset
);
10253 size
+= bset
->n_eq
* (1 + dim
);
10254 size
+= bset
->n_ineq
* (1 + dim
);
10255 size
+= bset
->n_div
* (2 + dim
);
10260 int isl_set_size(__isl_keep isl_set
*set
)
10268 for (i
= 0; i
< set
->n
; ++i
)
10269 size
+= isl_basic_set_size(set
->p
[i
]);
10274 /* Check if there is any lower bound (if lower == 0) and/or upper
10275 * bound (if upper == 0) on the specified dim.
10277 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10278 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10283 return isl_bool_error
;
10285 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10286 return isl_bool_error
);
10288 pos
+= isl_basic_map_offset(bmap
, type
);
10290 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10291 if (isl_int_is_zero(bmap
->div
[i
][0]))
10293 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10294 return isl_bool_true
;
10297 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10298 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10299 return isl_bool_true
;
10301 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10302 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10309 return lower
&& upper
;
10312 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10313 enum isl_dim_type type
, unsigned pos
)
10315 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10318 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10319 enum isl_dim_type type
, unsigned pos
)
10321 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10324 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10325 enum isl_dim_type type
, unsigned pos
)
10327 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10330 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10331 enum isl_dim_type type
, unsigned pos
)
10338 for (i
= 0; i
< map
->n
; ++i
) {
10340 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10341 if (bounded
< 0 || !bounded
)
10348 /* Return 1 if the specified dim is involved in both an upper bound
10349 * and a lower bound.
10351 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10352 enum isl_dim_type type
, unsigned pos
)
10354 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10357 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10359 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10360 enum isl_dim_type type
, unsigned pos
,
10361 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10362 enum isl_dim_type type
, unsigned pos
))
10367 return isl_bool_error
;
10369 for (i
= 0; i
< map
->n
; ++i
) {
10371 bounded
= fn(map
->p
[i
], type
, pos
);
10372 if (bounded
< 0 || bounded
)
10376 return isl_bool_false
;
10379 /* Return 1 if the specified dim is involved in any lower bound.
10381 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10382 enum isl_dim_type type
, unsigned pos
)
10384 return has_any_bound(set
, type
, pos
,
10385 &isl_basic_map_dim_has_lower_bound
);
10388 /* Return 1 if the specified dim is involved in any upper bound.
10390 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10391 enum isl_dim_type type
, unsigned pos
)
10393 return has_any_bound(set
, type
, pos
,
10394 &isl_basic_map_dim_has_upper_bound
);
10397 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10399 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10400 enum isl_dim_type type
, unsigned pos
,
10401 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10402 enum isl_dim_type type
, unsigned pos
))
10407 return isl_bool_error
;
10409 for (i
= 0; i
< map
->n
; ++i
) {
10411 bounded
= fn(map
->p
[i
], type
, pos
);
10412 if (bounded
< 0 || !bounded
)
10416 return isl_bool_true
;
10419 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10421 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10422 enum isl_dim_type type
, unsigned pos
)
10424 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10427 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10429 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10430 enum isl_dim_type type
, unsigned pos
)
10432 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10435 /* For each of the "n" variables starting at "first", determine
10436 * the sign of the variable and put the results in the first "n"
10437 * elements of the array "signs".
10439 * 1 means that the variable is non-negative
10440 * -1 means that the variable is non-positive
10441 * 0 means the variable attains both positive and negative values.
10443 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10444 unsigned first
, unsigned n
, int *signs
)
10446 isl_vec
*bound
= NULL
;
10447 struct isl_tab
*tab
= NULL
;
10448 struct isl_tab_undo
*snap
;
10451 if (!bset
|| !signs
)
10454 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10455 tab
= isl_tab_from_basic_set(bset
, 0);
10456 if (!bound
|| !tab
)
10459 isl_seq_clr(bound
->el
, bound
->size
);
10460 isl_int_set_si(bound
->el
[0], -1);
10462 snap
= isl_tab_snap(tab
);
10463 for (i
= 0; i
< n
; ++i
) {
10466 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10467 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10469 empty
= tab
->empty
;
10470 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10471 if (isl_tab_rollback(tab
, snap
) < 0)
10479 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10480 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10482 empty
= tab
->empty
;
10483 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10484 if (isl_tab_rollback(tab
, snap
) < 0)
10487 signs
[i
] = empty
? -1 : 0;
10491 isl_vec_free(bound
);
10495 isl_vec_free(bound
);
10499 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10500 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10502 if (!bset
|| !signs
)
10504 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10507 first
+= pos(bset
->dim
, type
) - 1;
10508 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10511 /* Is it possible for the integer division "div" to depend (possibly
10512 * indirectly) on any output dimensions?
10514 * If the div is undefined, then we conservatively assume that it
10515 * may depend on them.
10516 * Otherwise, we check if it actually depends on them or on any integer
10517 * divisions that may depend on them.
10519 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10522 unsigned n_out
, o_out
;
10523 unsigned n_div
, o_div
;
10525 if (isl_int_is_zero(bmap
->div
[div
][0]))
10528 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10529 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10531 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10534 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10535 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10537 for (i
= 0; i
< n_div
; ++i
) {
10538 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10540 if (div_may_involve_output(bmap
, i
))
10547 /* Return the first integer division of "bmap" in the range
10548 * [first, first + n[ that may depend on any output dimensions and
10549 * that has a non-zero coefficient in "c" (where the first coefficient
10550 * in "c" corresponds to integer division "first").
10552 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10553 isl_int
*c
, int first
, int n
)
10560 for (k
= first
; k
< first
+ n
; ++k
) {
10561 if (isl_int_is_zero(c
[k
]))
10563 if (div_may_involve_output(bmap
, k
))
10570 /* Look for a pair of inequality constraints in "bmap" of the form
10572 * -l + i >= 0 or i >= l
10574 * n + l - i >= 0 or i <= l + n
10576 * with n < "m" and i the output dimension at position "pos".
10577 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10578 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10579 * and earlier output dimensions, as well as integer divisions that do
10580 * not involve any of the output dimensions.
10582 * Return the index of the first inequality constraint or bmap->n_ineq
10583 * if no such pair can be found.
10585 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10586 int pos
, isl_int m
)
10591 unsigned n_div
, o_div
;
10592 unsigned n_out
, o_out
;
10598 ctx
= isl_basic_map_get_ctx(bmap
);
10599 total
= isl_basic_map_total_dim(bmap
);
10600 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10601 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10602 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10603 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10604 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10605 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10607 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10608 n_out
- (pos
+ 1)) != -1)
10610 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10613 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10614 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10617 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10618 bmap
->ineq
[j
] + 1, total
))
10622 if (j
>= bmap
->n_ineq
)
10624 isl_int_add(bmap
->ineq
[i
][0],
10625 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10626 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10627 isl_int_sub(bmap
->ineq
[i
][0],
10628 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10631 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10637 return bmap
->n_ineq
;
10640 /* Return the index of the equality of "bmap" that defines
10641 * the output dimension "pos" in terms of earlier dimensions.
10642 * The equality may also involve integer divisions, as long
10643 * as those integer divisions are defined in terms of
10644 * parameters or input dimensions.
10645 * In this case, *div is set to the number of integer divisions and
10646 * *ineq is set to the number of inequality constraints (provided
10647 * div and ineq are not NULL).
10649 * The equality may also involve a single integer division involving
10650 * the output dimensions (typically only output dimension "pos") as
10651 * long as the coefficient of output dimension "pos" is 1 or -1 and
10652 * there is a pair of constraints i >= l and i <= l + n, with i referring
10653 * to output dimension "pos", l an expression involving only earlier
10654 * dimensions and n smaller than the coefficient of the integer division
10655 * in the equality. In this case, the output dimension can be defined
10656 * in terms of a modulo expression that does not involve the integer division.
10657 * *div is then set to this single integer division and
10658 * *ineq is set to the index of constraint i >= l.
10660 * Return bmap->n_eq if there is no such equality.
10661 * Return -1 on error.
10663 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10664 int pos
, int *div
, int *ineq
)
10667 unsigned n_out
, o_out
;
10668 unsigned n_div
, o_div
;
10673 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10674 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10675 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10676 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10679 *ineq
= bmap
->n_ineq
;
10682 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10683 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10685 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10686 n_out
- (pos
+ 1)) != -1)
10688 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10692 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10693 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10695 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10696 k
+ 1, n_div
- (k
+1)) < n_div
)
10698 l
= find_modulo_constraint_pair(bmap
, pos
,
10699 bmap
->eq
[j
][o_div
+ k
]);
10702 if (l
>= bmap
->n_ineq
)
10714 /* Check if the given basic map is obviously single-valued.
10715 * In particular, for each output dimension, check that there is
10716 * an equality that defines the output dimension in terms of
10717 * earlier dimensions.
10719 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10725 return isl_bool_error
;
10727 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10729 for (i
= 0; i
< n_out
; ++i
) {
10732 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10735 return isl_bool_error
;
10736 if (eq
>= bmap
->n_eq
)
10737 return isl_bool_false
;
10740 return isl_bool_true
;
10743 /* Check if the given basic map is single-valued.
10744 * We simply compute
10748 * and check if the result is a subset of the identity mapping.
10750 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10753 isl_basic_map
*test
;
10757 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10761 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10762 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10764 space
= isl_basic_map_get_space(bmap
);
10765 space
= isl_space_map_from_set(isl_space_range(space
));
10766 id
= isl_basic_map_identity(space
);
10768 sv
= isl_basic_map_is_subset(test
, id
);
10770 isl_basic_map_free(test
);
10771 isl_basic_map_free(id
);
10776 /* Check if the given map is obviously single-valued.
10778 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10781 return isl_bool_error
;
10783 return isl_bool_true
;
10785 return isl_bool_false
;
10787 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10790 /* Check if the given map is single-valued.
10791 * We simply compute
10795 * and check if the result is a subset of the identity mapping.
10797 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10804 sv
= isl_map_plain_is_single_valued(map
);
10808 test
= isl_map_reverse(isl_map_copy(map
));
10809 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10811 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10812 id
= isl_map_identity(dim
);
10814 sv
= isl_map_is_subset(test
, id
);
10816 isl_map_free(test
);
10822 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10826 map
= isl_map_copy(map
);
10827 map
= isl_map_reverse(map
);
10828 in
= isl_map_is_single_valued(map
);
10834 /* Check if the given map is obviously injective.
10836 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10840 map
= isl_map_copy(map
);
10841 map
= isl_map_reverse(map
);
10842 in
= isl_map_plain_is_single_valued(map
);
10848 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10852 sv
= isl_map_is_single_valued(map
);
10856 return isl_map_is_injective(map
);
10859 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10861 return isl_map_is_single_valued((isl_map
*)set
);
10864 /* Does "map" only map elements to themselves?
10866 * If the domain and range spaces are different, then "map"
10867 * is considered not to be an identity relation, even if it is empty.
10868 * Otherwise, construct the maximal identity relation and
10869 * check whether "map" is a subset of this relation.
10871 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10875 isl_bool equal
, is_identity
;
10877 space
= isl_map_get_space(map
);
10878 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10879 isl_space_free(space
);
10880 if (equal
< 0 || !equal
)
10883 id
= isl_map_identity(isl_map_get_space(map
));
10884 is_identity
= isl_map_is_subset(map
, id
);
10887 return is_identity
;
10890 int isl_map_is_translation(__isl_keep isl_map
*map
)
10895 delta
= isl_map_deltas(isl_map_copy(map
));
10896 ok
= isl_set_is_singleton(delta
);
10897 isl_set_free(delta
);
10902 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10904 if (isl_seq_first_non_zero(p
, pos
) != -1)
10906 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10911 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10920 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10923 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10924 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10925 for (j
= 0; j
< nvar
; ++j
) {
10926 int lower
= 0, upper
= 0;
10927 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10928 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10930 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10934 if (i
< bset
->n_eq
)
10936 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10937 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10939 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10941 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10946 if (!lower
|| !upper
)
10953 int isl_set_is_box(__isl_keep isl_set
*set
)
10960 return isl_basic_set_is_box(set
->p
[0]);
10963 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10966 return isl_bool_error
;
10968 return isl_space_is_wrapping(bset
->dim
);
10971 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10974 return isl_bool_error
;
10976 return isl_space_is_wrapping(set
->dim
);
10979 /* Modify the space of "map" through a call to "change".
10980 * If "can_change" is set (not NULL), then first call it to check
10981 * if the modification is allowed, printing the error message "cannot_change"
10984 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10985 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10986 const char *cannot_change
,
10987 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10995 ok
= can_change
? can_change(map
) : isl_bool_true
;
10997 return isl_map_free(map
);
10999 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11000 return isl_map_free(map
));
11002 space
= change(isl_map_get_space(map
));
11003 map
= isl_map_reset_space(map
, space
);
11008 /* Is the domain of "map" a wrapped relation?
11010 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11013 return isl_bool_error
;
11015 return isl_space_domain_is_wrapping(map
->dim
);
11018 /* Is the range of "map" a wrapped relation?
11020 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11023 return isl_bool_error
;
11025 return isl_space_range_is_wrapping(map
->dim
);
11028 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11030 bmap
= isl_basic_map_cow(bmap
);
11034 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11038 bmap
= isl_basic_map_finalize(bmap
);
11040 return (isl_basic_set
*)bmap
;
11042 isl_basic_map_free(bmap
);
11046 /* Given a map A -> B, return the set (A -> B).
11048 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11050 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11053 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11055 bset
= isl_basic_set_cow(bset
);
11059 bset
->dim
= isl_space_unwrap(bset
->dim
);
11063 bset
= isl_basic_set_finalize(bset
);
11065 return (isl_basic_map
*)bset
;
11067 isl_basic_set_free(bset
);
11071 /* Given a set (A -> B), return the map A -> B.
11072 * Error out if "set" is not of the form (A -> B).
11074 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11076 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11077 "not a wrapping set", &isl_space_unwrap
);
11080 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11081 enum isl_dim_type type
)
11086 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11089 bmap
= isl_basic_map_cow(bmap
);
11093 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11097 bmap
= isl_basic_map_finalize(bmap
);
11101 isl_basic_map_free(bmap
);
11105 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11106 enum isl_dim_type type
)
11113 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11116 map
= isl_map_cow(map
);
11120 for (i
= 0; i
< map
->n
; ++i
) {
11121 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11125 map
->dim
= isl_space_reset(map
->dim
, type
);
11135 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11140 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11143 bmap
= isl_basic_map_cow(bmap
);
11147 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11151 bmap
= isl_basic_map_finalize(bmap
);
11155 isl_basic_map_free(bmap
);
11159 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11161 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
11164 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11165 __isl_take isl_basic_map
*bmap
)
11170 if (!bmap
->dim
->nested
[0])
11173 bmap
= isl_basic_map_cow(bmap
);
11177 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11181 bmap
= isl_basic_map_finalize(bmap
);
11185 isl_basic_map_free(bmap
);
11189 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11190 __isl_take isl_basic_map
*bmap
)
11195 if (!bmap
->dim
->nested
[1])
11198 bmap
= isl_basic_map_cow(bmap
);
11202 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11206 bmap
= isl_basic_map_finalize(bmap
);
11210 isl_basic_map_free(bmap
);
11214 /* Remove any internal structure from the spaces of domain and range of "map".
11216 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11221 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11224 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11227 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11229 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11232 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11234 isl_space
*dim
, *flat_dim
;
11237 dim
= isl_set_get_space(set
);
11238 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11239 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11240 map
= isl_map_intersect_domain(map
, set
);
11245 /* Remove any internal structure from the space of the domain of "map".
11247 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11252 if (!map
->dim
->nested
[0])
11255 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11258 /* Remove any internal structure from the space of the range of "map".
11260 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11265 if (!map
->dim
->nested
[1])
11268 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11271 /* Reorder the dimensions of "bmap" according to the given dim_map
11272 * and set the dimension specification to "dim" and
11273 * perform Gaussian elimination on the result.
11275 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11276 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11278 isl_basic_map
*res
;
11281 bmap
= isl_basic_map_cow(bmap
);
11282 if (!bmap
|| !dim
|| !dim_map
)
11285 flags
= bmap
->flags
;
11286 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11287 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11288 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11289 res
= isl_basic_map_alloc_space(dim
,
11290 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11291 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11293 res
->flags
= flags
;
11294 res
= isl_basic_map_gauss(res
, NULL
);
11295 res
= isl_basic_map_finalize(res
);
11299 isl_basic_map_free(bmap
);
11300 isl_space_free(dim
);
11304 /* Reorder the dimensions of "map" according to given reordering.
11306 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11307 __isl_take isl_reordering
*r
)
11310 struct isl_dim_map
*dim_map
;
11312 map
= isl_map_cow(map
);
11313 dim_map
= isl_dim_map_from_reordering(r
);
11314 if (!map
|| !r
|| !dim_map
)
11317 for (i
= 0; i
< map
->n
; ++i
) {
11318 struct isl_dim_map
*dim_map_i
;
11320 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11322 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11323 isl_space_copy(r
->dim
), dim_map_i
);
11329 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11331 isl_reordering_free(r
);
11337 isl_reordering_free(r
);
11341 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11342 __isl_take isl_reordering
*r
)
11344 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11347 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11348 __isl_take isl_space
*model
)
11352 if (!map
|| !model
)
11355 ctx
= isl_space_get_ctx(model
);
11356 if (!isl_space_has_named_params(model
))
11357 isl_die(ctx
, isl_error_invalid
,
11358 "model has unnamed parameters", goto error
);
11359 if (!isl_space_has_named_params(map
->dim
))
11360 isl_die(ctx
, isl_error_invalid
,
11361 "relation has unnamed parameters", goto error
);
11362 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11363 isl_reordering
*exp
;
11365 model
= isl_space_drop_dims(model
, isl_dim_in
,
11366 0, isl_space_dim(model
, isl_dim_in
));
11367 model
= isl_space_drop_dims(model
, isl_dim_out
,
11368 0, isl_space_dim(model
, isl_dim_out
));
11369 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11370 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11371 map
= isl_map_realign(map
, exp
);
11374 isl_space_free(model
);
11377 isl_space_free(model
);
11382 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11383 __isl_take isl_space
*model
)
11385 return isl_map_align_params(set
, model
);
11388 /* Align the parameters of "bmap" to those of "model", introducing
11389 * additional parameters if needed.
11391 __isl_give isl_basic_map
*isl_basic_map_align_params(
11392 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11396 if (!bmap
|| !model
)
11399 ctx
= isl_space_get_ctx(model
);
11400 if (!isl_space_has_named_params(model
))
11401 isl_die(ctx
, isl_error_invalid
,
11402 "model has unnamed parameters", goto error
);
11403 if (!isl_space_has_named_params(bmap
->dim
))
11404 isl_die(ctx
, isl_error_invalid
,
11405 "relation has unnamed parameters", goto error
);
11406 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11407 isl_reordering
*exp
;
11408 struct isl_dim_map
*dim_map
;
11410 model
= isl_space_drop_dims(model
, isl_dim_in
,
11411 0, isl_space_dim(model
, isl_dim_in
));
11412 model
= isl_space_drop_dims(model
, isl_dim_out
,
11413 0, isl_space_dim(model
, isl_dim_out
));
11414 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11415 exp
= isl_reordering_extend_space(exp
,
11416 isl_basic_map_get_space(bmap
));
11417 dim_map
= isl_dim_map_from_reordering(exp
);
11418 bmap
= isl_basic_map_realign(bmap
,
11419 exp
? isl_space_copy(exp
->dim
) : NULL
,
11420 isl_dim_map_extend(dim_map
, bmap
));
11421 isl_reordering_free(exp
);
11425 isl_space_free(model
);
11428 isl_space_free(model
);
11429 isl_basic_map_free(bmap
);
11433 /* Align the parameters of "bset" to those of "model", introducing
11434 * additional parameters if needed.
11436 __isl_give isl_basic_set
*isl_basic_set_align_params(
11437 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11439 return isl_basic_map_align_params(bset
, model
);
11442 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11443 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11444 enum isl_dim_type c2
, enum isl_dim_type c3
,
11445 enum isl_dim_type c4
, enum isl_dim_type c5
)
11447 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11448 struct isl_mat
*mat
;
11454 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11455 isl_basic_map_total_dim(bmap
) + 1);
11458 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11459 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11460 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11461 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11462 isl_int_set(mat
->row
[i
][pos
],
11463 bmap
->eq
[i
][off
+ k
]);
11471 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11472 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11473 enum isl_dim_type c2
, enum isl_dim_type c3
,
11474 enum isl_dim_type c4
, enum isl_dim_type c5
)
11476 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11477 struct isl_mat
*mat
;
11483 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11484 isl_basic_map_total_dim(bmap
) + 1);
11487 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11488 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11489 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11490 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11491 isl_int_set(mat
->row
[i
][pos
],
11492 bmap
->ineq
[i
][off
+ k
]);
11500 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11501 __isl_take isl_space
*dim
,
11502 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, 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 isl_basic_map
*bmap
;
11513 if (!dim
|| !eq
|| !ineq
)
11516 if (eq
->n_col
!= ineq
->n_col
)
11517 isl_die(dim
->ctx
, isl_error_invalid
,
11518 "equalities and inequalities matrices should have "
11519 "same number of columns", goto error
);
11521 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11523 if (eq
->n_col
< total
)
11524 isl_die(dim
->ctx
, isl_error_invalid
,
11525 "number of columns too small", goto error
);
11527 extra
= eq
->n_col
- total
;
11529 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11530 eq
->n_row
, ineq
->n_row
);
11533 for (i
= 0; i
< extra
; ++i
) {
11534 k
= isl_basic_map_alloc_div(bmap
);
11537 isl_int_set_si(bmap
->div
[k
][0], 0);
11539 for (i
= 0; i
< eq
->n_row
; ++i
) {
11540 l
= isl_basic_map_alloc_equality(bmap
);
11543 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11544 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11545 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11546 isl_int_set(bmap
->eq
[l
][off
+ k
],
11552 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11553 l
= isl_basic_map_alloc_inequality(bmap
);
11556 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11557 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11558 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11559 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11560 ineq
->row
[i
][pos
]);
11566 isl_space_free(dim
);
11568 isl_mat_free(ineq
);
11570 bmap
= isl_basic_map_simplify(bmap
);
11571 return isl_basic_map_finalize(bmap
);
11573 isl_space_free(dim
);
11575 isl_mat_free(ineq
);
11579 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11580 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11581 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11583 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11584 c1
, c2
, c3
, c4
, isl_dim_in
);
11587 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11588 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11589 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11591 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11592 c1
, c2
, c3
, c4
, isl_dim_in
);
11595 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11596 __isl_take isl_space
*dim
,
11597 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11598 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11600 return (isl_basic_set
*)
11601 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11602 c1
, c2
, c3
, c4
, isl_dim_in
);
11605 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11608 return isl_bool_error
;
11610 return isl_space_can_zip(bmap
->dim
);
11613 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11616 return isl_bool_error
;
11618 return isl_space_can_zip(map
->dim
);
11621 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11622 * (A -> C) -> (B -> D).
11624 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11633 if (!isl_basic_map_can_zip(bmap
))
11634 isl_die(bmap
->ctx
, isl_error_invalid
,
11635 "basic map cannot be zipped", goto error
);
11636 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11637 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11638 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11639 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11640 bmap
= isl_basic_map_cow(bmap
);
11641 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11644 bmap
->dim
= isl_space_zip(bmap
->dim
);
11647 bmap
= isl_basic_map_mark_final(bmap
);
11650 isl_basic_map_free(bmap
);
11654 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11655 * (A -> C) -> (B -> D).
11657 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11664 if (!isl_map_can_zip(map
))
11665 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11668 map
= isl_map_cow(map
);
11672 for (i
= 0; i
< map
->n
; ++i
) {
11673 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11678 map
->dim
= isl_space_zip(map
->dim
);
11688 /* Can we apply isl_basic_map_curry to "bmap"?
11689 * That is, does it have a nested relation in its domain?
11691 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11694 return isl_bool_error
;
11696 return isl_space_can_curry(bmap
->dim
);
11699 /* Can we apply isl_map_curry to "map"?
11700 * That is, does it have a nested relation in its domain?
11702 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11705 return isl_bool_error
;
11707 return isl_space_can_curry(map
->dim
);
11710 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11713 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11719 if (!isl_basic_map_can_curry(bmap
))
11720 isl_die(bmap
->ctx
, isl_error_invalid
,
11721 "basic map cannot be curried", goto error
);
11722 bmap
= isl_basic_map_cow(bmap
);
11725 bmap
->dim
= isl_space_curry(bmap
->dim
);
11728 bmap
= isl_basic_map_mark_final(bmap
);
11731 isl_basic_map_free(bmap
);
11735 /* Given a map (A -> B) -> C, return the corresponding map
11738 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11740 return isl_map_change_space(map
, &isl_map_can_curry
,
11741 "map cannot be curried", &isl_space_curry
);
11744 /* Can isl_map_range_curry be applied to "map"?
11745 * That is, does it have a nested relation in its range,
11746 * the domain of which is itself a nested relation?
11748 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11751 return isl_bool_error
;
11753 return isl_space_can_range_curry(map
->dim
);
11756 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11757 * A -> (B -> (C -> D)).
11759 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11761 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11762 "map range cannot be curried",
11763 &isl_space_range_curry
);
11766 /* Can we apply isl_basic_map_uncurry to "bmap"?
11767 * That is, does it have a nested relation in its domain?
11769 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11772 return isl_bool_error
;
11774 return isl_space_can_uncurry(bmap
->dim
);
11777 /* Can we apply isl_map_uncurry to "map"?
11778 * That is, does it have a nested relation in its domain?
11780 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11783 return isl_bool_error
;
11785 return isl_space_can_uncurry(map
->dim
);
11788 /* Given a basic map A -> (B -> C), return the corresponding basic map
11791 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11797 if (!isl_basic_map_can_uncurry(bmap
))
11798 isl_die(bmap
->ctx
, isl_error_invalid
,
11799 "basic map cannot be uncurried",
11800 return isl_basic_map_free(bmap
));
11801 bmap
= isl_basic_map_cow(bmap
);
11804 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11806 return isl_basic_map_free(bmap
);
11807 bmap
= isl_basic_map_mark_final(bmap
);
11811 /* Given a map A -> (B -> C), return the corresponding map
11814 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11816 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11817 "map cannot be uncurried", &isl_space_uncurry
);
11820 /* Construct a basic map mapping the domain of the affine expression
11821 * to a one-dimensional range prescribed by the affine expression.
11823 * A NaN affine expression cannot be converted to a basic map.
11825 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11830 isl_local_space
*ls
;
11831 isl_basic_map
*bmap
= NULL
;
11835 is_nan
= isl_aff_is_nan(aff
);
11839 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11840 "cannot convert NaN", goto error
);
11842 ls
= isl_aff_get_local_space(aff
);
11843 bmap
= isl_basic_map_from_local_space(ls
);
11844 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11845 k
= isl_basic_map_alloc_equality(bmap
);
11849 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11850 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11851 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11852 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11853 aff
->v
->size
- (pos
+ 1));
11856 bmap
= isl_basic_map_finalize(bmap
);
11860 isl_basic_map_free(bmap
);
11864 /* Construct a map mapping the domain of the affine expression
11865 * to a one-dimensional range prescribed by the affine expression.
11867 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11869 isl_basic_map
*bmap
;
11871 bmap
= isl_basic_map_from_aff(aff
);
11872 return isl_map_from_basic_map(bmap
);
11875 /* Construct a basic map mapping the domain the multi-affine expression
11876 * to its range, with each dimension in the range equated to the
11877 * corresponding affine expression.
11879 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11880 __isl_take isl_multi_aff
*maff
)
11884 isl_basic_map
*bmap
;
11889 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11890 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11891 "invalid space", goto error
);
11893 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11894 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11896 for (i
= 0; i
< maff
->n
; ++i
) {
11898 isl_basic_map
*bmap_i
;
11900 aff
= isl_aff_copy(maff
->p
[i
]);
11901 bmap_i
= isl_basic_map_from_aff(aff
);
11903 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11906 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11908 isl_multi_aff_free(maff
);
11911 isl_multi_aff_free(maff
);
11915 /* Construct a map mapping the domain the multi-affine expression
11916 * to its range, with each dimension in the range equated to the
11917 * corresponding affine expression.
11919 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11921 isl_basic_map
*bmap
;
11923 bmap
= isl_basic_map_from_multi_aff(maff
);
11924 return isl_map_from_basic_map(bmap
);
11927 /* Construct a basic map mapping a domain in the given space to
11928 * to an n-dimensional range, with n the number of elements in the list,
11929 * where each coordinate in the range is prescribed by the
11930 * corresponding affine expression.
11931 * The domains of all affine expressions in the list are assumed to match
11934 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11935 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11939 isl_basic_map
*bmap
;
11944 dim
= isl_space_from_domain(domain_dim
);
11945 bmap
= isl_basic_map_universe(dim
);
11947 for (i
= 0; i
< list
->n
; ++i
) {
11949 isl_basic_map
*bmap_i
;
11951 aff
= isl_aff_copy(list
->p
[i
]);
11952 bmap_i
= isl_basic_map_from_aff(aff
);
11954 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11957 isl_aff_list_free(list
);
11961 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11962 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11964 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11967 /* Construct a basic map where the given dimensions are equal to each other.
11969 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11970 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11972 isl_basic_map
*bmap
= NULL
;
11978 if (pos1
>= isl_space_dim(space
, type1
))
11979 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11980 "index out of bounds", goto error
);
11981 if (pos2
>= isl_space_dim(space
, type2
))
11982 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11983 "index out of bounds", goto error
);
11985 if (type1
== type2
&& pos1
== pos2
)
11986 return isl_basic_map_universe(space
);
11988 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11989 i
= isl_basic_map_alloc_equality(bmap
);
11992 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11993 pos1
+= isl_basic_map_offset(bmap
, type1
);
11994 pos2
+= isl_basic_map_offset(bmap
, type2
);
11995 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11996 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11997 bmap
= isl_basic_map_finalize(bmap
);
11998 isl_space_free(space
);
12001 isl_space_free(space
);
12002 isl_basic_map_free(bmap
);
12006 /* Add a constraint imposing that the given two dimensions are equal.
12008 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12009 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12013 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12015 bmap
= isl_basic_map_intersect(bmap
, eq
);
12020 /* Add a constraint imposing that the given two dimensions are equal.
12022 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12023 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12025 isl_basic_map
*bmap
;
12027 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12029 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12034 /* Add a constraint imposing that the given two dimensions have opposite values.
12036 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12037 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12039 isl_basic_map
*bmap
= NULL
;
12045 if (pos1
>= isl_map_dim(map
, type1
))
12046 isl_die(map
->ctx
, isl_error_invalid
,
12047 "index out of bounds", goto error
);
12048 if (pos2
>= isl_map_dim(map
, type2
))
12049 isl_die(map
->ctx
, isl_error_invalid
,
12050 "index out of bounds", goto error
);
12052 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12053 i
= isl_basic_map_alloc_equality(bmap
);
12056 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12057 pos1
+= isl_basic_map_offset(bmap
, type1
);
12058 pos2
+= isl_basic_map_offset(bmap
, type2
);
12059 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12060 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12061 bmap
= isl_basic_map_finalize(bmap
);
12063 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12067 isl_basic_map_free(bmap
);
12072 /* Construct a constraint imposing that the value of the first dimension is
12073 * greater than or equal to that of the second.
12075 static __isl_give isl_constraint
*constraint_order_ge(
12076 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12077 enum isl_dim_type type2
, int pos2
)
12084 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12086 if (pos1
>= isl_constraint_dim(c
, type1
))
12087 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12088 "index out of bounds", return isl_constraint_free(c
));
12089 if (pos2
>= isl_constraint_dim(c
, type2
))
12090 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12091 "index out of bounds", return isl_constraint_free(c
));
12093 if (type1
== type2
&& pos1
== pos2
)
12096 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12097 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12102 /* Add a constraint imposing that the value of the first dimension is
12103 * greater than or equal to that of the second.
12105 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12106 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12111 if (type1
== type2
&& pos1
== pos2
)
12113 space
= isl_basic_map_get_space(bmap
);
12114 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12115 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12120 /* Add a constraint imposing that the value of the first dimension is
12121 * greater than or equal to that of the second.
12123 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12124 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12129 if (type1
== type2
&& pos1
== pos2
)
12131 space
= isl_map_get_space(map
);
12132 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12133 map
= isl_map_add_constraint(map
, c
);
12138 /* Add a constraint imposing that the value of the first dimension is
12139 * less than or equal to that of the second.
12141 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12142 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12144 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12147 /* Construct a basic map where the value of the first dimension is
12148 * greater than that of the second.
12150 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12151 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12153 isl_basic_map
*bmap
= NULL
;
12159 if (pos1
>= isl_space_dim(space
, type1
))
12160 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12161 "index out of bounds", goto error
);
12162 if (pos2
>= isl_space_dim(space
, type2
))
12163 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12164 "index out of bounds", goto error
);
12166 if (type1
== type2
&& pos1
== pos2
)
12167 return isl_basic_map_empty(space
);
12169 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12170 i
= isl_basic_map_alloc_inequality(bmap
);
12172 return isl_basic_map_free(bmap
);
12173 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12174 pos1
+= isl_basic_map_offset(bmap
, type1
);
12175 pos2
+= isl_basic_map_offset(bmap
, type2
);
12176 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12177 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12178 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12179 bmap
= isl_basic_map_finalize(bmap
);
12183 isl_space_free(space
);
12184 isl_basic_map_free(bmap
);
12188 /* Add a constraint imposing that the value of the first dimension is
12189 * greater than that of the second.
12191 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12192 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12196 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12198 bmap
= isl_basic_map_intersect(bmap
, gt
);
12203 /* Add a constraint imposing that the value of the first dimension is
12204 * greater than that of the second.
12206 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12207 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12209 isl_basic_map
*bmap
;
12211 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12213 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12218 /* Add a constraint imposing that the value of the first dimension is
12219 * smaller than that of the second.
12221 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12222 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12224 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12227 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12231 isl_local_space
*ls
;
12236 if (!isl_basic_map_divs_known(bmap
))
12237 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12238 "some divs are unknown", return NULL
);
12240 ls
= isl_basic_map_get_local_space(bmap
);
12241 div
= isl_local_space_get_div(ls
, pos
);
12242 isl_local_space_free(ls
);
12247 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12250 return isl_basic_map_get_div(bset
, pos
);
12253 /* Plug in "subs" for dimension "type", "pos" of "bset".
12255 * Let i be the dimension to replace and let "subs" be of the form
12259 * Any integer division with a non-zero coefficient for i,
12261 * floor((a i + g)/m)
12265 * floor((a f + d g)/(m d))
12267 * Constraints of the form
12275 * We currently require that "subs" is an integral expression.
12276 * Handling rational expressions may require us to add stride constraints
12277 * as we do in isl_basic_set_preimage_multi_aff.
12279 __isl_give isl_basic_set
*isl_basic_set_substitute(
12280 __isl_take isl_basic_set
*bset
,
12281 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12287 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12290 bset
= isl_basic_set_cow(bset
);
12291 if (!bset
|| !subs
)
12294 ctx
= isl_basic_set_get_ctx(bset
);
12295 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12296 isl_die(ctx
, isl_error_invalid
,
12297 "spaces don't match", goto error
);
12298 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12299 isl_die(ctx
, isl_error_unsupported
,
12300 "cannot handle divs yet", goto error
);
12301 if (!isl_int_is_one(subs
->v
->el
[0]))
12302 isl_die(ctx
, isl_error_invalid
,
12303 "can only substitute integer expressions", goto error
);
12305 pos
+= isl_basic_set_offset(bset
, type
);
12309 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12310 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12312 isl_int_set(v
, bset
->eq
[i
][pos
]);
12313 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12314 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12315 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12318 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12319 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12321 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12322 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12323 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12324 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12327 for (i
= 0; i
< bset
->n_div
; ++i
) {
12328 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12330 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12331 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12332 isl_seq_combine(bset
->div
[i
] + 1,
12333 subs
->v
->el
[0], bset
->div
[i
] + 1,
12334 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12335 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12340 bset
= isl_basic_set_simplify(bset
);
12341 return isl_basic_set_finalize(bset
);
12343 isl_basic_set_free(bset
);
12347 /* Plug in "subs" for dimension "type", "pos" of "set".
12349 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12350 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12354 if (set
&& isl_set_plain_is_empty(set
))
12357 set
= isl_set_cow(set
);
12361 for (i
= set
->n
- 1; i
>= 0; --i
) {
12362 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12363 if (remove_if_empty(set
, i
) < 0)
12373 /* Check if the range of "ma" is compatible with the domain or range
12374 * (depending on "type") of "bmap".
12375 * Return -1 if anything is wrong.
12377 static int check_basic_map_compatible_range_multi_aff(
12378 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12379 __isl_keep isl_multi_aff
*ma
)
12382 isl_space
*ma_space
;
12384 ma_space
= isl_multi_aff_get_space(ma
);
12386 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12390 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12391 "parameters don't match", goto error
);
12392 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12396 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12397 "spaces don't match", goto error
);
12399 isl_space_free(ma_space
);
12402 isl_space_free(ma_space
);
12406 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12407 * coefficients before the transformed range of dimensions,
12408 * the "n_after" coefficients after the transformed range of dimensions
12409 * and the coefficients of the other divs in "bmap".
12411 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12412 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12417 isl_local_space
*ls
;
12422 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12426 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12427 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12428 for (i
= 0; i
< n_div
; ++i
) {
12429 int o_bmap
= 0, o_ls
= 0;
12431 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12432 o_bmap
+= 1 + 1 + n_param
;
12433 o_ls
+= 1 + 1 + n_param
;
12434 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12435 o_bmap
+= n_before
;
12436 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12437 ls
->div
->row
[i
] + o_ls
, n_set
);
12440 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12442 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12443 ls
->div
->row
[i
] + o_ls
, n_div
);
12446 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12447 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12451 isl_local_space_free(ls
);
12454 isl_local_space_free(ls
);
12458 /* How many stride constraints does "ma" enforce?
12459 * That is, how many of the affine expressions have a denominator
12460 * different from one?
12462 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12467 for (i
= 0; i
< ma
->n
; ++i
)
12468 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12474 /* For each affine expression in ma of the form
12476 * x_i = (f_i y + h_i)/m_i
12478 * with m_i different from one, add a constraint to "bmap"
12481 * f_i y + h_i = m_i alpha_i
12483 * with alpha_i an additional existentially quantified variable.
12485 * The input variables of "ma" correspond to a subset of the variables
12486 * of "bmap". There are "n_before" variables in "bmap" before this
12487 * subset and "n_after" variables after this subset.
12488 * The integer divisions of the affine expressions in "ma" are assumed
12489 * to have been aligned. There are "n_div_ma" of them and
12490 * they appear first in "bmap", straight after the "n_after" variables.
12492 static __isl_give isl_basic_map
*add_ma_strides(
12493 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12494 int n_before
, int n_after
, int n_div_ma
)
12502 total
= isl_basic_map_total_dim(bmap
);
12503 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12504 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12505 for (i
= 0; i
< ma
->n
; ++i
) {
12506 int o_bmap
= 0, o_ma
= 1;
12508 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12510 div
= isl_basic_map_alloc_div(bmap
);
12511 k
= isl_basic_map_alloc_equality(bmap
);
12512 if (div
< 0 || k
< 0)
12514 isl_int_set_si(bmap
->div
[div
][0], 0);
12515 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12516 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12517 o_bmap
+= 1 + n_param
;
12518 o_ma
+= 1 + n_param
;
12519 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12520 o_bmap
+= n_before
;
12521 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12522 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12525 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12527 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12528 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12529 o_bmap
+= n_div_ma
;
12531 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12532 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12538 isl_basic_map_free(bmap
);
12542 /* Replace the domain or range space (depending on "type) of "space" by "set".
12544 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12545 enum isl_dim_type type
, __isl_take isl_space
*set
)
12547 if (type
== isl_dim_in
) {
12548 space
= isl_space_range(space
);
12549 space
= isl_space_map_from_domain_and_range(set
, space
);
12551 space
= isl_space_domain(space
);
12552 space
= isl_space_map_from_domain_and_range(space
, set
);
12558 /* Compute the preimage of the domain or range (depending on "type")
12559 * of "bmap" under the function represented by "ma".
12560 * In other words, plug in "ma" in the domain or range of "bmap".
12561 * The result is a basic map that lives in the same space as "bmap"
12562 * except that the domain or range has been replaced by
12563 * the domain space of "ma".
12565 * If bmap is represented by
12567 * A(p) + S u + B x + T v + C(divs) >= 0,
12569 * where u and x are input and output dimensions if type == isl_dim_out
12570 * while x and v are input and output dimensions if type == isl_dim_in,
12571 * and ma is represented by
12573 * x = D(p) + F(y) + G(divs')
12575 * then the result is
12577 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12579 * The divs in the input set are similarly adjusted.
12582 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12586 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12587 * B_i G(divs') + c_i(divs))/n_i)
12589 * If bmap is not a rational map and if F(y) involves any denominators
12591 * x_i = (f_i y + h_i)/m_i
12593 * then additional constraints are added to ensure that we only
12594 * map back integer points. That is we enforce
12596 * f_i y + h_i = m_i alpha_i
12598 * with alpha_i an additional existentially quantified variable.
12600 * We first copy over the divs from "ma".
12601 * Then we add the modified constraints and divs from "bmap".
12602 * Finally, we add the stride constraints, if needed.
12604 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12605 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12606 __isl_take isl_multi_aff
*ma
)
12610 isl_basic_map
*res
= NULL
;
12611 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12612 isl_int f
, c1
, c2
, g
;
12613 int rational
, strides
;
12620 ma
= isl_multi_aff_align_divs(ma
);
12623 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12626 if (type
== isl_dim_in
) {
12628 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12630 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12633 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12634 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12636 space
= isl_multi_aff_get_domain_space(ma
);
12637 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12638 rational
= isl_basic_map_is_rational(bmap
);
12639 strides
= rational
? 0 : multi_aff_strides(ma
);
12640 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12641 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12643 res
= isl_basic_map_set_rational(res
);
12645 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12646 if (isl_basic_map_alloc_div(res
) < 0)
12649 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12652 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12653 k
= isl_basic_map_alloc_equality(res
);
12656 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12657 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12660 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12661 k
= isl_basic_map_alloc_inequality(res
);
12664 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12665 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12668 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12669 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12670 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12673 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12674 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12679 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12685 isl_basic_map_free(bmap
);
12686 isl_multi_aff_free(ma
);
12687 res
= isl_basic_set_simplify(res
);
12688 return isl_basic_map_finalize(res
);
12694 isl_basic_map_free(bmap
);
12695 isl_multi_aff_free(ma
);
12696 isl_basic_map_free(res
);
12700 /* Compute the preimage of "bset" under the function represented by "ma".
12701 * In other words, plug in "ma" in "bset". The result is a basic set
12702 * that lives in the domain space of "ma".
12704 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12705 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12707 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12710 /* Compute the preimage of the domain of "bmap" under the function
12711 * represented by "ma".
12712 * In other words, plug in "ma" in the domain of "bmap".
12713 * The result is a basic map that lives in the same space as "bmap"
12714 * except that the domain has been replaced by the domain space of "ma".
12716 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12717 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12719 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12722 /* Compute the preimage of the range of "bmap" under the function
12723 * represented by "ma".
12724 * In other words, plug in "ma" in the range of "bmap".
12725 * The result is a basic map that lives in the same space as "bmap"
12726 * except that the range has been replaced by the domain space of "ma".
12728 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12729 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12731 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12734 /* Check if the range of "ma" is compatible with the domain or range
12735 * (depending on "type") of "map".
12736 * Return -1 if anything is wrong.
12738 static int check_map_compatible_range_multi_aff(
12739 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12740 __isl_keep isl_multi_aff
*ma
)
12743 isl_space
*ma_space
;
12745 ma_space
= isl_multi_aff_get_space(ma
);
12746 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12747 isl_space_free(ma_space
);
12749 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12750 "spaces don't match", return -1);
12754 /* Compute the preimage of the domain or range (depending on "type")
12755 * of "map" under the function represented by "ma".
12756 * In other words, plug in "ma" in the domain or range of "map".
12757 * The result is a map that lives in the same space as "map"
12758 * except that the domain or range has been replaced by
12759 * the domain space of "ma".
12761 * The parameters are assumed to have been aligned.
12763 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12764 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12769 map
= isl_map_cow(map
);
12770 ma
= isl_multi_aff_align_divs(ma
);
12773 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12776 for (i
= 0; i
< map
->n
; ++i
) {
12777 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12778 isl_multi_aff_copy(ma
));
12783 space
= isl_multi_aff_get_domain_space(ma
);
12784 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12786 isl_space_free(map
->dim
);
12791 isl_multi_aff_free(ma
);
12793 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12794 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12797 isl_multi_aff_free(ma
);
12802 /* Compute the preimage of the domain or range (depending on "type")
12803 * of "map" under the function represented by "ma".
12804 * In other words, plug in "ma" in the domain or range of "map".
12805 * The result is a map that lives in the same space as "map"
12806 * except that the domain or range has been replaced by
12807 * the domain space of "ma".
12809 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12810 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12815 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12816 return map_preimage_multi_aff(map
, type
, ma
);
12818 if (!isl_space_has_named_params(map
->dim
) ||
12819 !isl_space_has_named_params(ma
->space
))
12820 isl_die(map
->ctx
, isl_error_invalid
,
12821 "unaligned unnamed parameters", goto error
);
12822 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12823 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12825 return map_preimage_multi_aff(map
, type
, ma
);
12827 isl_multi_aff_free(ma
);
12828 return isl_map_free(map
);
12831 /* Compute the preimage of "set" under the function represented by "ma".
12832 * In other words, plug in "ma" in "set". The result is a set
12833 * that lives in the domain space of "ma".
12835 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12836 __isl_take isl_multi_aff
*ma
)
12838 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12841 /* Compute the preimage of the domain of "map" under the function
12842 * represented by "ma".
12843 * In other words, plug in "ma" in the domain of "map".
12844 * The result is a map that lives in the same space as "map"
12845 * except that the domain has been replaced by the domain space of "ma".
12847 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12848 __isl_take isl_multi_aff
*ma
)
12850 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12853 /* Compute the preimage of the range of "map" under the function
12854 * represented by "ma".
12855 * In other words, plug in "ma" in the range of "map".
12856 * The result is a map that lives in the same space as "map"
12857 * except that the range has been replaced by the domain space of "ma".
12859 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12860 __isl_take isl_multi_aff
*ma
)
12862 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12865 /* Compute the preimage of "map" under the function represented by "pma".
12866 * In other words, plug in "pma" in the domain or range of "map".
12867 * The result is a map that lives in the same space as "map",
12868 * except that the space of type "type" has been replaced by
12869 * the domain space of "pma".
12871 * The parameters of "map" and "pma" are assumed to have been aligned.
12873 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12874 __isl_take isl_map
*map
, enum isl_dim_type type
,
12875 __isl_take isl_pw_multi_aff
*pma
)
12884 isl_pw_multi_aff_free(pma
);
12885 res
= isl_map_empty(isl_map_get_space(map
));
12890 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12891 isl_multi_aff_copy(pma
->p
[0].maff
));
12892 if (type
== isl_dim_in
)
12893 res
= isl_map_intersect_domain(res
,
12894 isl_map_copy(pma
->p
[0].set
));
12896 res
= isl_map_intersect_range(res
,
12897 isl_map_copy(pma
->p
[0].set
));
12899 for (i
= 1; i
< pma
->n
; ++i
) {
12902 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12903 isl_multi_aff_copy(pma
->p
[i
].maff
));
12904 if (type
== isl_dim_in
)
12905 res_i
= isl_map_intersect_domain(res_i
,
12906 isl_map_copy(pma
->p
[i
].set
));
12908 res_i
= isl_map_intersect_range(res_i
,
12909 isl_map_copy(pma
->p
[i
].set
));
12910 res
= isl_map_union(res
, res_i
);
12913 isl_pw_multi_aff_free(pma
);
12917 isl_pw_multi_aff_free(pma
);
12922 /* Compute the preimage of "map" under the function represented by "pma".
12923 * In other words, plug in "pma" in the domain or range of "map".
12924 * The result is a map that lives in the same space as "map",
12925 * except that the space of type "type" has been replaced by
12926 * the domain space of "pma".
12928 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12929 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12934 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12935 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12937 if (!isl_space_has_named_params(map
->dim
) ||
12938 !isl_space_has_named_params(pma
->dim
))
12939 isl_die(map
->ctx
, isl_error_invalid
,
12940 "unaligned unnamed parameters", goto error
);
12941 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12942 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12944 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12946 isl_pw_multi_aff_free(pma
);
12947 return isl_map_free(map
);
12950 /* Compute the preimage of "set" under the function represented by "pma".
12951 * In other words, plug in "pma" in "set". The result is a set
12952 * that lives in the domain space of "pma".
12954 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12955 __isl_take isl_pw_multi_aff
*pma
)
12957 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12960 /* Compute the preimage of the domain of "map" under the function
12961 * represented by "pma".
12962 * In other words, plug in "pma" in the domain of "map".
12963 * The result is a map that lives in the same space as "map",
12964 * except that domain space has been replaced by the domain space of "pma".
12966 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12967 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12969 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12972 /* Compute the preimage of the range of "map" under the function
12973 * represented by "pma".
12974 * In other words, plug in "pma" in the range of "map".
12975 * The result is a map that lives in the same space as "map",
12976 * except that range space has been replaced by the domain space of "pma".
12978 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12979 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12981 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12984 /* Compute the preimage of "map" under the function represented by "mpa".
12985 * In other words, plug in "mpa" in the domain or range of "map".
12986 * The result is a map that lives in the same space as "map",
12987 * except that the space of type "type" has been replaced by
12988 * the domain space of "mpa".
12990 * If the map does not involve any constraints that refer to the
12991 * dimensions of the substituted space, then the only possible
12992 * effect of "mpa" on the map is to map the space to a different space.
12993 * We create a separate isl_multi_aff to effectuate this change
12994 * in order to avoid spurious splitting of the map along the pieces
12997 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12998 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13001 isl_pw_multi_aff
*pma
;
13006 n
= isl_map_dim(map
, type
);
13007 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13011 space
= isl_multi_pw_aff_get_space(mpa
);
13012 isl_multi_pw_aff_free(mpa
);
13013 ma
= isl_multi_aff_zero(space
);
13014 return isl_map_preimage_multi_aff(map
, type
, ma
);
13017 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13018 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13021 isl_multi_pw_aff_free(mpa
);
13025 /* Compute the preimage of "map" under the function represented by "mpa".
13026 * In other words, plug in "mpa" in the domain "map".
13027 * The result is a map that lives in the same space as "map",
13028 * except that domain space has been replaced by the domain space of "mpa".
13030 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13031 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13033 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13036 /* Compute the preimage of "set" by the function represented by "mpa".
13037 * In other words, plug in "mpa" in "set".
13039 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13040 __isl_take isl_multi_pw_aff
*mpa
)
13042 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);