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 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 /* Return the space of "map".
93 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
95 return map
? map
->dim
: NULL
;
98 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
100 return map
? n(map
->dim
, type
) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
105 return set
? n(set
->dim
, type
) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
109 enum isl_dim_type type
)
118 case isl_dim_cst
: return 0;
119 case isl_dim_param
: return 1;
120 case isl_dim_in
: return 1 + space
->nparam
;
121 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
122 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
128 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
129 enum isl_dim_type type
)
131 return isl_basic_map_offset(bset
, type
);
134 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
136 return pos(map
->dim
, type
);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
140 enum isl_dim_type type
)
142 return isl_basic_map_dim(bset
, type
);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
147 return isl_basic_set_dim(bset
, isl_dim_set
);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
152 return isl_basic_set_dim(bset
, isl_dim_param
);
155 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
159 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
162 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
164 return isl_set_dim(set
, isl_dim_set
);
167 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
169 return isl_set_dim(set
, isl_dim_param
);
172 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
174 return bmap
? bmap
->dim
->n_in
: 0;
177 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
179 return bmap
? bmap
->dim
->n_out
: 0;
182 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
184 return bmap
? bmap
->dim
->nparam
: 0;
187 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
189 return bmap
? bmap
->n_div
: 0;
192 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
194 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
197 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
199 return map
? map
->dim
->n_in
: 0;
202 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
204 return map
? map
->dim
->n_out
: 0;
207 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
209 return map
? map
->dim
->nparam
: 0;
212 /* Return the number of equality constraints in the description of "bmap".
213 * Return -1 on error.
215 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
222 /* Return the number of equality constraints in the description of "bset".
223 * Return -1 on error.
225 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
227 return isl_basic_map_n_equality(bset_to_bmap(bset
));
230 /* Return the number of inequality constraints in the description of "bmap".
231 * Return -1 on error.
233 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
240 /* Return the number of inequality constraints in the description of "bset".
241 * Return -1 on error.
243 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
245 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
248 /* Do "bmap1" and "bmap2" have the same parameters?
250 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
251 __isl_keep isl_basic_map
*bmap2
)
253 isl_space
*space1
, *space2
;
255 space1
= isl_basic_map_peek_space(bmap1
);
256 space2
= isl_basic_map_peek_space(bmap2
);
257 return isl_space_has_equal_params(space1
, space2
);
260 /* Do "map1" and "map2" have the same parameters?
262 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
263 __isl_keep isl_map
*map2
)
265 isl_space
*space1
, *space2
;
267 space1
= isl_map_peek_space(map1
);
268 space2
= isl_map_peek_space(map2
);
269 return isl_space_has_equal_params(space1
, space2
);
272 /* Do "map" and "set" have the same parameters?
274 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
275 __isl_keep isl_set
*set
)
277 return isl_map_has_equal_params(map
, set_to_map(set
));
280 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
281 __isl_keep isl_set
*set
)
285 return isl_bool_error
;
286 m
= isl_map_has_equal_params(map
, set_to_map(set
));
289 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
290 set
->dim
, isl_dim_set
);
293 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
294 __isl_keep isl_basic_set
*bset
)
298 return isl_bool_error
;
299 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
302 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
303 bset
->dim
, isl_dim_set
);
306 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
307 __isl_keep isl_set
*set
)
311 return isl_bool_error
;
312 m
= isl_map_has_equal_params(map
, set_to_map(set
));
315 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
316 set
->dim
, isl_dim_set
);
319 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
320 __isl_keep isl_basic_set
*bset
)
324 return isl_bool_error
;
325 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
328 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
329 bset
->dim
, isl_dim_set
);
332 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
334 return bmap
? bmap
->ctx
: NULL
;
337 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
339 return bset
? bset
->ctx
: NULL
;
342 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
344 return map
? map
->ctx
: NULL
;
347 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
349 return set
? set
->ctx
: NULL
;
352 /* Return the space of "bmap".
354 __isl_keep isl_space
*isl_basic_map_peek_space(
355 __isl_keep
const isl_basic_map
*bmap
)
357 return bmap
? bmap
->dim
: NULL
;
360 /* Return the space of "bset".
362 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
364 return isl_basic_map_peek_space(bset_to_bmap(bset
));
367 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
369 return isl_space_copy(isl_basic_map_peek_space(bmap
));
372 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
374 return isl_basic_map_get_space(bset_to_bmap(bset
));
377 /* Extract the divs in "bmap" as a matrix.
379 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
390 ctx
= isl_basic_map_get_ctx(bmap
);
391 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
392 cols
= 1 + 1 + total
+ bmap
->n_div
;
393 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
397 for (i
= 0; i
< bmap
->n_div
; ++i
)
398 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
403 /* Extract the divs in "bset" as a matrix.
405 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
407 return isl_basic_map_get_divs(bset
);
410 __isl_give isl_local_space
*isl_basic_map_get_local_space(
411 __isl_keep isl_basic_map
*bmap
)
418 div
= isl_basic_map_get_divs(bmap
);
419 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
422 __isl_give isl_local_space
*isl_basic_set_get_local_space(
423 __isl_keep isl_basic_set
*bset
)
425 return isl_basic_map_get_local_space(bset
);
428 /* For each known div d = floor(f/m), add the constraints
431 * -(f-(m-1)) + m d >= 0
433 * Do not finalize the result.
435 static __isl_give isl_basic_map
*add_known_div_constraints(
436 __isl_take isl_basic_map
*bmap
)
443 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
446 bmap
= isl_basic_map_cow(bmap
);
447 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
450 for (i
= 0; i
< n_div
; ++i
) {
451 if (isl_int_is_zero(bmap
->div
[i
][0]))
453 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
454 return isl_basic_map_free(bmap
);
460 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
461 __isl_take isl_local_space
*ls
)
470 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
471 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
472 n_div
, 0, 2 * n_div
);
474 for (i
= 0; i
< n_div
; ++i
)
475 if (isl_basic_map_alloc_div(bmap
) < 0)
478 for (i
= 0; i
< n_div
; ++i
)
479 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
480 bmap
= add_known_div_constraints(bmap
);
482 isl_local_space_free(ls
);
485 isl_local_space_free(ls
);
486 isl_basic_map_free(bmap
);
490 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
491 __isl_take isl_local_space
*ls
)
493 return isl_basic_map_from_local_space(ls
);
496 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
498 return isl_space_copy(isl_map_peek_space(map
));
501 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
505 return isl_space_copy(set
->dim
);
508 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
509 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
511 bmap
= isl_basic_map_cow(bmap
);
514 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
517 bmap
= isl_basic_map_finalize(bmap
);
520 isl_basic_map_free(bmap
);
524 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
525 __isl_take isl_basic_set
*bset
, const char *s
)
527 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
530 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
531 enum isl_dim_type type
)
533 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
536 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
537 enum isl_dim_type type
, const char *s
)
541 map
= isl_map_cow(map
);
545 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
549 for (i
= 0; i
< map
->n
; ++i
) {
550 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
561 /* Replace the identifier of the tuple of type "type" by "id".
563 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
564 __isl_take isl_basic_map
*bmap
,
565 enum isl_dim_type type
, __isl_take isl_id
*id
)
567 bmap
= isl_basic_map_cow(bmap
);
570 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
572 return isl_basic_map_free(bmap
);
573 bmap
= isl_basic_map_finalize(bmap
);
580 /* Replace the identifier of the tuple by "id".
582 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
583 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
585 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
588 /* Does the input or output tuple have a name?
590 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
592 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
595 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
596 enum isl_dim_type type
)
598 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
601 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
604 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
608 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
609 enum isl_dim_type type
, __isl_take isl_id
*id
)
611 map
= isl_map_cow(map
);
615 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
617 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
623 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
624 __isl_take isl_id
*id
)
626 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
629 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
630 enum isl_dim_type type
)
632 map
= isl_map_cow(map
);
636 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
638 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
641 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
643 return isl_map_reset_tuple_id(set
, isl_dim_set
);
646 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
648 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
651 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
652 enum isl_dim_type type
)
654 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
657 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
659 return isl_map_has_tuple_id(set
, isl_dim_set
);
662 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
664 return isl_map_get_tuple_id(set
, isl_dim_set
);
667 /* Does the set tuple have a name?
669 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
672 return isl_bool_error
;
673 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
677 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
679 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
682 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
684 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
687 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
688 enum isl_dim_type type
, unsigned pos
)
690 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
693 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
694 enum isl_dim_type type
, unsigned pos
)
696 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
699 /* Does the given dimension have a name?
701 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
702 enum isl_dim_type type
, unsigned pos
)
705 return isl_bool_error
;
706 return isl_space_has_dim_name(map
->dim
, type
, pos
);
709 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
710 enum isl_dim_type type
, unsigned pos
)
712 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
715 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
716 enum isl_dim_type type
, unsigned pos
)
718 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
721 /* Does the given dimension have a name?
723 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
724 enum isl_dim_type type
, unsigned pos
)
727 return isl_bool_error
;
728 return isl_space_has_dim_name(set
->dim
, type
, pos
);
731 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
732 __isl_take isl_basic_map
*bmap
,
733 enum isl_dim_type type
, unsigned pos
, const char *s
)
735 bmap
= isl_basic_map_cow(bmap
);
738 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
741 return isl_basic_map_finalize(bmap
);
743 isl_basic_map_free(bmap
);
747 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
748 enum isl_dim_type type
, unsigned pos
, const char *s
)
752 map
= isl_map_cow(map
);
756 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
760 for (i
= 0; i
< map
->n
; ++i
) {
761 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
772 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
773 __isl_take isl_basic_set
*bset
,
774 enum isl_dim_type type
, unsigned pos
, const char *s
)
776 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
780 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
781 enum isl_dim_type type
, unsigned pos
, const char *s
)
783 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
787 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
788 enum isl_dim_type type
, unsigned pos
)
791 return isl_bool_error
;
792 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
795 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
796 enum isl_dim_type type
, unsigned pos
)
798 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
801 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
802 enum isl_dim_type type
, unsigned pos
)
804 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
807 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
808 enum isl_dim_type type
, unsigned pos
)
810 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
813 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
814 enum isl_dim_type type
, unsigned pos
)
816 return isl_map_has_dim_id(set
, type
, pos
);
819 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
820 enum isl_dim_type type
, unsigned pos
)
822 return isl_map_get_dim_id(set
, type
, pos
);
825 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
826 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
828 map
= isl_map_cow(map
);
832 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
834 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
840 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
841 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
843 return isl_map_set_dim_id(set
, type
, pos
, id
);
846 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
847 __isl_keep isl_id
*id
)
851 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
854 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
855 __isl_keep isl_id
*id
)
857 return isl_map_find_dim_by_id(set
, type
, id
);
860 /* Return the position of the dimension of the given type and name
862 * Return -1 if no such dimension can be found.
864 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
865 enum isl_dim_type type
, const char *name
)
869 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
872 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
877 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
880 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
883 return isl_map_find_dim_by_name(set
, type
, name
);
886 /* Check whether equality i of bset is a pure stride constraint
887 * on a single dimension, i.e., of the form
891 * with k a constant and e an existentially quantified variable.
893 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
902 return isl_bool_error
;
904 if (!isl_int_is_zero(bset
->eq
[i
][0]))
905 return isl_bool_false
;
907 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
908 d
= isl_basic_set_dim(bset
, isl_dim_set
);
909 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
911 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
912 return isl_bool_false
;
913 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
915 return isl_bool_false
;
916 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
918 return isl_bool_false
;
920 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
922 return isl_bool_false
;
923 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
924 n_div
- pos2
- 1) != -1)
925 return isl_bool_false
;
926 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
927 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
928 return isl_bool_false
;
930 return isl_bool_true
;
933 /* Reset the user pointer on all identifiers of parameters and tuples
934 * of the space of "map".
936 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
940 space
= isl_map_get_space(map
);
941 space
= isl_space_reset_user(space
);
942 map
= isl_map_reset_space(map
, space
);
947 /* Reset the user pointer on all identifiers of parameters and tuples
948 * of the space of "set".
950 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
952 return isl_map_reset_user(set
);
955 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
958 return isl_bool_error
;
959 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
962 /* Has "map" been marked as a rational map?
963 * In particular, have all basic maps in "map" been marked this way?
964 * An empty map is not considered to be rational.
965 * Maps where only some of the basic maps are marked rational
968 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
974 return isl_bool_error
;
976 return isl_bool_false
;
977 rational
= isl_basic_map_is_rational(map
->p
[0]);
980 for (i
= 1; i
< map
->n
; ++i
) {
983 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
986 if (rational
!= rational_i
)
987 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
988 "mixed rational and integer basic maps "
989 "not supported", return isl_bool_error
);
995 /* Has "set" been marked as a rational set?
996 * In particular, have all basic set in "set" been marked this way?
997 * An empty set is not considered to be rational.
998 * Sets where only some of the basic sets are marked rational
1001 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1003 return isl_map_is_rational(set
);
1006 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1008 return isl_basic_map_is_rational(bset
);
1011 /* Does "bmap" contain any rational points?
1013 * If "bmap" has an equality for each dimension, equating the dimension
1014 * to an integer constant, then it has no rational points, even if it
1015 * is marked as rational.
1017 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1019 isl_bool has_rational
= isl_bool_true
;
1023 return isl_bool_error
;
1024 if (isl_basic_map_plain_is_empty(bmap
))
1025 return isl_bool_false
;
1026 if (!isl_basic_map_is_rational(bmap
))
1027 return isl_bool_false
;
1028 bmap
= isl_basic_map_copy(bmap
);
1029 bmap
= isl_basic_map_implicit_equalities(bmap
);
1031 return isl_bool_error
;
1032 total
= isl_basic_map_total_dim(bmap
);
1033 if (bmap
->n_eq
== total
) {
1035 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1036 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1039 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1040 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1042 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1047 if (i
== bmap
->n_eq
)
1048 has_rational
= isl_bool_false
;
1050 isl_basic_map_free(bmap
);
1052 return has_rational
;
1055 /* Does "map" contain any rational points?
1057 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1060 isl_bool has_rational
;
1063 return isl_bool_error
;
1064 for (i
= 0; i
< map
->n
; ++i
) {
1065 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1066 if (has_rational
< 0 || has_rational
)
1067 return has_rational
;
1069 return isl_bool_false
;
1072 /* Does "set" contain any rational points?
1074 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1076 return isl_map_has_rational(set
);
1079 /* Is this basic set a parameter domain?
1081 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1084 return isl_bool_error
;
1085 return isl_space_is_params(bset
->dim
);
1088 /* Is this set a parameter domain?
1090 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1093 return isl_bool_error
;
1094 return isl_space_is_params(set
->dim
);
1097 /* Is this map actually a parameter domain?
1098 * Users should never call this function. Outside of isl,
1099 * a map can never be a parameter domain.
1101 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1104 return isl_bool_error
;
1105 return isl_space_is_params(map
->dim
);
1108 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
1109 struct isl_basic_map
*bmap
, unsigned extra
,
1110 unsigned n_eq
, unsigned n_ineq
)
1113 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1118 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1119 if (isl_blk_is_error(bmap
->block
))
1122 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1123 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1127 bmap
->block2
= isl_blk_empty();
1130 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1131 if (isl_blk_is_error(bmap
->block2
))
1134 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1139 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1140 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1142 for (i
= 0; i
< extra
; ++i
)
1143 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1147 bmap
->c_size
= n_eq
+ n_ineq
;
1148 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1149 bmap
->extra
= extra
;
1153 bmap
->sample
= NULL
;
1157 isl_basic_map_free(bmap
);
1161 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1162 unsigned nparam
, unsigned dim
, unsigned extra
,
1163 unsigned n_eq
, unsigned n_ineq
)
1165 struct isl_basic_map
*bmap
;
1168 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1172 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1173 return bset_from_bmap(bmap
);
1176 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1177 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1179 struct isl_basic_map
*bmap
;
1182 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1183 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1184 return bset_from_bmap(bmap
);
1186 isl_space_free(dim
);
1190 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1191 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1193 struct isl_basic_map
*bmap
;
1197 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1202 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1204 isl_space_free(dim
);
1208 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1209 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1210 unsigned n_eq
, unsigned n_ineq
)
1212 struct isl_basic_map
*bmap
;
1215 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1219 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1223 static void dup_constraints(
1224 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1227 unsigned total
= isl_basic_map_total_dim(src
);
1229 for (i
= 0; i
< src
->n_eq
; ++i
) {
1230 int j
= isl_basic_map_alloc_equality(dst
);
1231 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1234 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1235 int j
= isl_basic_map_alloc_inequality(dst
);
1236 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1239 for (i
= 0; i
< src
->n_div
; ++i
) {
1240 int j
= isl_basic_map_alloc_div(dst
);
1241 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1243 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1246 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1248 struct isl_basic_map
*dup
;
1252 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1253 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1256 dup_constraints(dup
, bmap
);
1257 dup
->flags
= bmap
->flags
;
1258 dup
->sample
= isl_vec_copy(bmap
->sample
);
1262 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1264 struct isl_basic_map
*dup
;
1266 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1267 return bset_from_bmap(dup
);
1270 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1275 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1279 return isl_basic_set_dup(bset
);
1282 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1291 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1296 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1300 bmap
= isl_basic_map_dup(bmap
);
1302 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1306 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1315 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1320 if (--bmap
->ref
> 0)
1323 isl_ctx_deref(bmap
->ctx
);
1325 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1327 isl_blk_free(bmap
->ctx
, bmap
->block
);
1328 isl_vec_free(bmap
->sample
);
1329 isl_space_free(bmap
->dim
);
1335 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1337 return isl_basic_map_free(bset_to_bmap(bset
));
1340 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1342 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1345 /* Check that "map" has only named parameters, reporting an error
1348 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1350 return isl_space_check_named_params(isl_map_peek_space(map
));
1353 /* Check that "bmap1" and "bmap2" have the same parameters,
1354 * reporting an error if they do not.
1356 static isl_stat
isl_basic_map_check_equal_params(
1357 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1361 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1363 return isl_stat_error
;
1365 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1366 "parameters don't match", return isl_stat_error
);
1370 __isl_give isl_map
*isl_map_align_params_map_map_and(
1371 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1372 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1373 __isl_take isl_map
*map2
))
1377 if (isl_map_has_equal_params(map1
, map2
))
1378 return fn(map1
, map2
);
1379 if (isl_map_check_named_params(map1
) < 0)
1381 if (isl_map_check_named_params(map2
) < 0)
1383 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1384 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1385 return fn(map1
, map2
);
1392 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1393 __isl_keep isl_map
*map2
,
1394 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1399 return isl_bool_error
;
1400 if (isl_map_has_equal_params(map1
, map2
))
1401 return fn(map1
, map2
);
1402 if (isl_map_check_named_params(map1
) < 0)
1403 return isl_bool_error
;
1404 if (isl_map_check_named_params(map2
) < 0)
1405 return isl_bool_error
;
1406 map1
= isl_map_copy(map1
);
1407 map2
= isl_map_copy(map2
);
1408 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1409 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1416 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1418 struct isl_ctx
*ctx
;
1422 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1423 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1425 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1426 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1427 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1428 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1429 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1430 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1432 int j
= isl_basic_map_alloc_inequality(bmap
);
1436 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1437 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1444 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1445 bmap
->extra
- bmap
->n_div
);
1446 return bmap
->n_eq
++;
1449 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1451 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1454 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1458 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1463 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1465 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1468 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1473 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1475 if (pos
!= bmap
->n_eq
- 1) {
1477 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1478 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1484 /* Turn inequality "pos" of "bmap" into an equality.
1486 * In particular, we move the inequality in front of the equalities
1487 * and move the last inequality in the position of the moved inequality.
1488 * Note that isl_tab_make_equalities_explicit depends on this particular
1489 * change in the ordering of the constraints.
1491 void isl_basic_map_inequality_to_equality(
1492 struct isl_basic_map
*bmap
, unsigned pos
)
1496 t
= bmap
->ineq
[pos
];
1497 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1498 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1504 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1505 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1506 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1509 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1511 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1514 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1516 struct isl_ctx
*ctx
;
1520 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1521 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1522 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1523 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1524 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1525 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1526 1 + isl_basic_map_total_dim(bmap
),
1527 bmap
->extra
- bmap
->n_div
);
1528 return bmap
->n_ineq
++;
1531 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1533 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1536 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1540 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1545 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1547 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1550 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1555 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1557 if (pos
!= bmap
->n_ineq
- 1) {
1558 t
= bmap
->ineq
[pos
];
1559 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1560 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1561 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1567 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1569 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1572 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1577 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1580 k
= isl_basic_map_alloc_equality(bmap
);
1583 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1586 isl_basic_map_free(bmap
);
1590 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1593 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1596 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1601 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1604 k
= isl_basic_map_alloc_inequality(bmap
);
1607 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1610 isl_basic_map_free(bmap
);
1614 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1617 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1620 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1624 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1625 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1626 1 + 1 + isl_basic_map_total_dim(bmap
),
1627 bmap
->extra
- bmap
->n_div
);
1628 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1629 return bmap
->n_div
++;
1632 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1634 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1637 /* Check that there are "n" dimensions of type "type" starting at "first"
1640 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1641 enum isl_dim_type type
, unsigned first
, unsigned n
)
1646 return isl_stat_error
;
1647 dim
= isl_basic_map_dim(bmap
, type
);
1648 if (first
+ n
> dim
|| first
+ n
< first
)
1649 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1650 "position or range out of bounds",
1651 return isl_stat_error
);
1655 /* Insert an extra integer division, prescribed by "div", to "bmap"
1656 * at (integer division) position "pos".
1658 * The integer division is first added at the end and then moved
1659 * into the right position.
1661 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1662 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1666 bmap
= isl_basic_map_cow(bmap
);
1668 return isl_basic_map_free(bmap
);
1670 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1671 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1672 "unexpected size", return isl_basic_map_free(bmap
));
1673 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1674 return isl_basic_map_free(bmap
);
1676 bmap
= isl_basic_map_extend_space(bmap
,
1677 isl_basic_map_get_space(bmap
), 1, 0, 2);
1678 k
= isl_basic_map_alloc_div(bmap
);
1680 return isl_basic_map_free(bmap
);
1681 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1682 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1684 for (i
= k
; i
> pos
; --i
)
1685 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1690 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1693 return isl_stat_error
;
1694 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1699 /* Copy constraint from src to dst, putting the vars of src at offset
1700 * dim_off in dst and the divs of src at offset div_off in dst.
1701 * If both sets are actually map, then dim_off applies to the input
1704 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1705 struct isl_basic_map
*src_map
, isl_int
*src
,
1706 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1708 unsigned src_nparam
= isl_basic_map_dim(src_map
, isl_dim_param
);
1709 unsigned dst_nparam
= isl_basic_map_dim(dst_map
, isl_dim_param
);
1710 unsigned src_in
= isl_basic_map_dim(src_map
, isl_dim_in
);
1711 unsigned dst_in
= isl_basic_map_dim(dst_map
, isl_dim_in
);
1712 unsigned src_out
= isl_basic_map_dim(src_map
, isl_dim_out
);
1713 unsigned dst_out
= isl_basic_map_dim(dst_map
, isl_dim_out
);
1714 isl_int_set(dst
[0], src
[0]);
1715 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1716 if (dst_nparam
> src_nparam
)
1717 isl_seq_clr(dst
+1+src_nparam
,
1718 dst_nparam
- src_nparam
);
1719 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1720 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1722 isl_min(dst_in
-in_off
, src_in
));
1723 if (dst_in
-in_off
> src_in
)
1724 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1725 dst_in
- in_off
- src_in
);
1726 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1727 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1728 src
+1+src_nparam
+src_in
,
1729 isl_min(dst_out
-out_off
, src_out
));
1730 if (dst_out
-out_off
> src_out
)
1731 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1732 dst_out
- out_off
- src_out
);
1733 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1734 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1735 src
+1+src_nparam
+src_in
+src_out
,
1736 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1737 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1738 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1739 div_off
+src_map
->n_div
,
1740 dst_map
->n_div
- div_off
- src_map
->n_div
);
1743 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1744 struct isl_basic_map
*src_map
, isl_int
*src
,
1745 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1747 isl_int_set(dst
[0], src
[0]);
1748 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1751 static __isl_give isl_basic_map
*add_constraints(
1752 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1753 unsigned i_pos
, unsigned o_pos
)
1758 if (!bmap1
|| !bmap2
)
1761 div_off
= bmap1
->n_div
;
1763 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1764 int i1
= isl_basic_map_alloc_equality(bmap1
);
1767 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1768 i_pos
, o_pos
, div_off
);
1771 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1772 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1775 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1776 i_pos
, o_pos
, div_off
);
1779 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1780 int i1
= isl_basic_map_alloc_div(bmap1
);
1783 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1784 i_pos
, o_pos
, div_off
);
1787 isl_basic_map_free(bmap2
);
1792 isl_basic_map_free(bmap1
);
1793 isl_basic_map_free(bmap2
);
1797 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1798 struct isl_basic_set
*bset2
, unsigned pos
)
1800 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1801 bset_to_bmap(bset2
), 0, pos
));
1804 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1805 __isl_take isl_basic_map
*base
, __isl_take isl_space
*dim
,
1806 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1808 struct isl_basic_map
*ext
;
1818 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1819 base
->extra
>= base
->n_div
+ extra
;
1821 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1822 room_for_ineq(base
, n_ineq
)) {
1823 isl_space_free(dim
);
1827 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1828 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1829 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1830 extra
+= base
->extra
;
1832 n_ineq
+= base
->n_ineq
;
1834 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1840 ext
->sample
= isl_vec_copy(base
->sample
);
1841 flags
= base
->flags
;
1842 ext
= add_constraints(ext
, base
, 0, 0);
1845 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1851 isl_space_free(dim
);
1852 isl_basic_map_free(base
);
1856 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1857 __isl_take isl_basic_set
*base
,
1858 __isl_take isl_space
*dim
, unsigned extra
,
1859 unsigned n_eq
, unsigned n_ineq
)
1861 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1862 dim
, extra
, n_eq
, n_ineq
));
1865 struct isl_basic_map
*isl_basic_map_extend_constraints(
1866 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1870 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1874 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1875 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1876 unsigned n_eq
, unsigned n_ineq
)
1878 struct isl_basic_map
*bmap
;
1883 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1887 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1890 isl_basic_map_free(base
);
1894 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1895 unsigned nparam
, unsigned dim
, unsigned extra
,
1896 unsigned n_eq
, unsigned n_ineq
)
1898 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1899 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1902 struct isl_basic_set
*isl_basic_set_extend_constraints(
1903 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1905 isl_basic_map
*bmap
= bset_to_bmap(base
);
1906 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1907 return bset_from_bmap(bmap
);
1910 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1912 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1915 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1920 if (bmap
->ref
> 1) {
1922 bmap
= isl_basic_map_dup(bmap
);
1925 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1926 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1931 /* Clear all cached information in "map", either because it is about
1932 * to be modified or because it is being freed.
1933 * Always return the same pointer that is passed in.
1934 * This is needed for the use in isl_map_free.
1936 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1938 isl_basic_map_free(map
->cached_simple_hull
[0]);
1939 isl_basic_map_free(map
->cached_simple_hull
[1]);
1940 map
->cached_simple_hull
[0] = NULL
;
1941 map
->cached_simple_hull
[1] = NULL
;
1945 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1947 return isl_map_cow(set
);
1950 /* Return an isl_map that is equal to "map" and that has only
1951 * a single reference.
1953 * If the original input already has only one reference, then
1954 * simply return it, but clear all cached information, since
1955 * it may be rendered invalid by the operations that will be
1956 * performed on the result.
1958 * Otherwise, create a duplicate (without any cached information).
1960 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1966 return clear_caches(map
);
1968 return isl_map_dup(map
);
1971 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1972 unsigned a_len
, unsigned b_len
)
1974 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1975 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1976 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1979 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1980 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1988 isl_assert(bmap
->ctx
,
1989 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1991 if (n1
== 0 || n2
== 0)
1994 bmap
= isl_basic_map_cow(bmap
);
1998 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1999 if (isl_blk_is_error(blk
))
2002 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2004 bmap
->eq
[i
] + pos
, n1
, n2
);
2006 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2008 bmap
->ineq
[i
] + pos
, n1
, n2
);
2010 for (i
= 0; i
< bmap
->n_div
; ++i
)
2012 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2014 isl_blk_free(bmap
->ctx
, blk
);
2016 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
2017 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2018 return isl_basic_map_finalize(bmap
);
2020 isl_basic_map_free(bmap
);
2024 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2025 __isl_take isl_basic_map
*bmap
)
2031 total
= isl_basic_map_total_dim(bmap
);
2032 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2033 return isl_basic_map_free(bmap
);
2034 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2036 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2038 i
= isl_basic_map_alloc_equality(bmap
);
2042 isl_int_set_si(bmap
->eq
[i
][0], 1);
2043 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2044 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2045 isl_vec_free(bmap
->sample
);
2046 bmap
->sample
= NULL
;
2047 return isl_basic_map_finalize(bmap
);
2049 isl_basic_map_free(bmap
);
2053 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2054 __isl_take isl_basic_set
*bset
)
2056 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2059 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2060 __isl_take isl_basic_map
*bmap
)
2065 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2068 bmap
= isl_basic_map_cow(bmap
);
2072 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2074 return isl_basic_map_finalize(bmap
);
2077 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2078 __isl_take isl_basic_set
*bset
)
2080 return isl_basic_map_set_rational(bset
);
2083 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2084 __isl_take isl_basic_set
*bset
)
2089 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2092 bset
= isl_basic_set_cow(bset
);
2096 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2098 return isl_basic_set_finalize(bset
);
2101 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2105 map
= isl_map_cow(map
);
2108 for (i
= 0; i
< map
->n
; ++i
) {
2109 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2119 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2121 return isl_map_set_rational(set
);
2124 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2127 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2129 isl_int
*t
= bmap
->div
[a
];
2130 bmap
->div
[a
] = bmap
->div
[b
];
2134 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2135 * div definitions accordingly.
2137 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2140 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2142 swap_div(bmap
, a
, b
);
2144 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2145 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2147 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2148 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2150 for (i
= 0; i
< bmap
->n_div
; ++i
)
2151 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2152 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2155 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2156 * div definitions accordingly.
2158 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
2160 isl_basic_map_swap_div(bset
, a
, b
);
2163 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2165 isl_seq_cpy(c
, c
+ n
, rem
);
2166 isl_seq_clr(c
+ rem
, n
);
2169 /* Drop n dimensions starting at first.
2171 * In principle, this frees up some extra variables as the number
2172 * of columns remains constant, but we would have to extend
2173 * the div array too as the number of rows in this array is assumed
2174 * to be equal to extra.
2176 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2177 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2179 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2182 /* Move "n" divs starting at "first" to the end of the list of divs.
2184 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2185 unsigned first
, unsigned n
)
2190 if (first
+ n
== bmap
->n_div
)
2193 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2196 for (i
= 0; i
< n
; ++i
)
2197 div
[i
] = bmap
->div
[first
+ i
];
2198 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2199 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2200 for (i
= 0; i
< n
; ++i
)
2201 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2205 isl_basic_map_free(bmap
);
2209 /* Drop "n" dimensions of type "type" starting at "first".
2211 * In principle, this frees up some extra variables as the number
2212 * of columns remains constant, but we would have to extend
2213 * the div array too as the number of rows in this array is assumed
2214 * to be equal to extra.
2216 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2217 enum isl_dim_type type
, unsigned first
, unsigned n
)
2227 dim
= isl_basic_map_dim(bmap
, type
);
2228 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
2230 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2233 bmap
= isl_basic_map_cow(bmap
);
2237 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2238 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2239 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2240 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2242 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2243 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2245 for (i
= 0; i
< bmap
->n_div
; ++i
)
2246 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2248 if (type
== isl_dim_div
) {
2249 bmap
= move_divs_last(bmap
, first
, n
);
2252 if (isl_basic_map_free_div(bmap
, n
) < 0)
2253 return isl_basic_map_free(bmap
);
2255 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2259 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2260 bmap
= isl_basic_map_simplify(bmap
);
2261 return isl_basic_map_finalize(bmap
);
2263 isl_basic_map_free(bmap
);
2267 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2268 enum isl_dim_type type
, unsigned first
, unsigned n
)
2270 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2274 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2275 enum isl_dim_type type
, unsigned first
, unsigned n
)
2282 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2284 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2286 map
= isl_map_cow(map
);
2289 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2293 for (i
= 0; i
< map
->n
; ++i
) {
2294 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2298 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2306 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2307 enum isl_dim_type type
, unsigned first
, unsigned n
)
2309 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2313 * We don't cow, as the div is assumed to be redundant.
2315 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2316 __isl_take isl_basic_map
*bmap
, unsigned div
)
2324 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
2326 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
2328 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2329 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2331 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2332 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
2333 isl_basic_map_drop_inequality(bmap
, i
);
2337 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2340 for (i
= 0; i
< bmap
->n_div
; ++i
)
2341 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
2343 if (div
!= bmap
->n_div
- 1) {
2345 isl_int
*t
= bmap
->div
[div
];
2347 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
2348 bmap
->div
[j
] = bmap
->div
[j
+1];
2350 bmap
->div
[bmap
->n_div
- 1] = t
;
2352 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2353 if (isl_basic_map_free_div(bmap
, 1) < 0)
2354 return isl_basic_map_free(bmap
);
2358 isl_basic_map_free(bmap
);
2362 /* Eliminate the specified n dimensions starting at first from the
2363 * constraints, without removing the dimensions from the space.
2364 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2366 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2367 enum isl_dim_type type
, unsigned first
, unsigned n
)
2376 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2377 isl_die(map
->ctx
, isl_error_invalid
,
2378 "index out of bounds", goto error
);
2380 map
= isl_map_cow(map
);
2384 for (i
= 0; i
< map
->n
; ++i
) {
2385 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2395 /* Eliminate the specified n dimensions starting at first from the
2396 * constraints, without removing the dimensions from the space.
2397 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2399 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2400 enum isl_dim_type type
, unsigned first
, unsigned n
)
2402 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2405 /* Eliminate the specified n dimensions starting at first from the
2406 * constraints, without removing the dimensions from the space.
2407 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2409 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2410 unsigned first
, unsigned n
)
2412 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2415 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2416 __isl_take isl_basic_map
*bmap
)
2420 bmap
= isl_basic_map_eliminate_vars(bmap
,
2421 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2425 return isl_basic_map_finalize(bmap
);
2428 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2429 __isl_take isl_basic_set
*bset
)
2431 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2434 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2443 map
= isl_map_cow(map
);
2447 for (i
= 0; i
< map
->n
; ++i
) {
2448 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2458 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2460 return isl_map_remove_divs(set
);
2463 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2464 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2465 unsigned first
, unsigned n
)
2467 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2468 return isl_basic_map_free(bmap
);
2469 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2471 bmap
= isl_basic_map_eliminate_vars(bmap
,
2472 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2475 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2477 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2481 /* Return true if the definition of the given div (recursively) involves
2482 * any of the given variables.
2484 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2485 unsigned first
, unsigned n
)
2488 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2490 if (isl_int_is_zero(bmap
->div
[div
][0]))
2491 return isl_bool_false
;
2492 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2493 return isl_bool_true
;
2495 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2498 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2500 involves
= div_involves_vars(bmap
, i
, first
, n
);
2501 if (involves
< 0 || involves
)
2505 return isl_bool_false
;
2508 /* Try and add a lower and/or upper bound on "div" to "bmap"
2509 * based on inequality "i".
2510 * "total" is the total number of variables (excluding the divs).
2511 * "v" is a temporary object that can be used during the calculations.
2512 * If "lb" is set, then a lower bound should be constructed.
2513 * If "ub" is set, then an upper bound should be constructed.
2515 * The calling function has already checked that the inequality does not
2516 * reference "div", but we still need to check that the inequality is
2517 * of the right form. We'll consider the case where we want to construct
2518 * a lower bound. The construction of upper bounds is similar.
2520 * Let "div" be of the form
2522 * q = floor((a + f(x))/d)
2524 * We essentially check if constraint "i" is of the form
2528 * so that we can use it to derive a lower bound on "div".
2529 * However, we allow a slightly more general form
2533 * with the condition that the coefficients of g(x) - f(x) are all
2535 * Rewriting this constraint as
2539 * adding a + f(x) to both sides and dividing by d, we obtain
2541 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2543 * Taking the floor on both sides, we obtain
2545 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2549 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2551 * In the case of an upper bound, we construct the constraint
2553 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2556 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2557 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2558 unsigned total
, isl_int v
, int lb
, int ub
)
2562 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2564 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2565 bmap
->div
[div
][1 + 1 + j
]);
2566 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2569 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2570 bmap
->div
[div
][1 + 1 + j
]);
2571 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2577 bmap
= isl_basic_map_cow(bmap
);
2578 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2580 int k
= isl_basic_map_alloc_inequality(bmap
);
2583 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2584 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2585 bmap
->div
[div
][1 + j
]);
2586 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2587 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2589 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2592 int k
= isl_basic_map_alloc_inequality(bmap
);
2595 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2596 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2597 bmap
->div
[div
][1 + j
]);
2598 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2599 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2601 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2604 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2607 isl_basic_map_free(bmap
);
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2656 static __isl_give isl_basic_map
*insert_bounds_on_div(
2657 __isl_take isl_basic_map
*bmap
, int div
)
2660 int check_lb
, check_ub
;
2667 if (isl_int_is_zero(bmap
->div
[div
][0]))
2670 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2674 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2675 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2682 if (!check_lb
&& !check_ub
)
2687 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2688 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2691 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2692 check_lb
, check_ub
);
2700 /* Remove all divs (recursively) involving any of the given dimensions
2701 * in their definitions.
2703 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2704 __isl_take isl_basic_map
*bmap
,
2705 enum isl_dim_type type
, unsigned first
, unsigned n
)
2709 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2710 return isl_basic_map_free(bmap
);
2711 first
+= isl_basic_map_offset(bmap
, type
);
2713 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2716 involves
= div_involves_vars(bmap
, i
, first
, n
);
2718 return isl_basic_map_free(bmap
);
2721 bmap
= insert_bounds_on_div(bmap
, i
);
2722 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2731 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2732 __isl_take isl_basic_set
*bset
,
2733 enum isl_dim_type type
, unsigned first
, unsigned n
)
2735 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2738 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2739 enum isl_dim_type type
, unsigned first
, unsigned n
)
2748 map
= isl_map_cow(map
);
2752 for (i
= 0; i
< map
->n
; ++i
) {
2753 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2764 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2765 enum isl_dim_type type
, unsigned first
, unsigned n
)
2767 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2771 /* Does the description of "bmap" depend on the specified dimensions?
2772 * We also check whether the dimensions appear in any of the div definitions.
2773 * In principle there is no need for this check. If the dimensions appear
2774 * in a div definition, they also appear in the defining constraints of that
2777 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2778 enum isl_dim_type type
, unsigned first
, unsigned n
)
2782 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2783 return isl_bool_error
;
2785 first
+= isl_basic_map_offset(bmap
, type
);
2786 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2787 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2788 return isl_bool_true
;
2789 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2790 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2791 return isl_bool_true
;
2792 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2793 if (isl_int_is_zero(bmap
->div
[i
][0]))
2795 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2796 return isl_bool_true
;
2799 return isl_bool_false
;
2802 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2803 enum isl_dim_type type
, unsigned first
, unsigned n
)
2808 return isl_bool_error
;
2810 if (first
+ n
> isl_map_dim(map
, type
))
2811 isl_die(map
->ctx
, isl_error_invalid
,
2812 "index out of bounds", return isl_bool_error
);
2814 for (i
= 0; i
< map
->n
; ++i
) {
2815 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2817 if (involves
< 0 || involves
)
2821 return isl_bool_false
;
2824 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2825 enum isl_dim_type type
, unsigned first
, unsigned n
)
2827 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2830 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2831 enum isl_dim_type type
, unsigned first
, unsigned n
)
2833 return isl_map_involves_dims(set
, type
, first
, n
);
2836 /* Drop all constraints in bmap that involve any of the dimensions
2837 * first to first+n-1.
2839 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2840 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2847 bmap
= isl_basic_map_cow(bmap
);
2852 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2853 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2855 isl_basic_map_drop_equality(bmap
, i
);
2858 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2859 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2861 isl_basic_map_drop_inequality(bmap
, i
);
2864 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2868 /* Drop all constraints in bset that involve any of the dimensions
2869 * first to first+n-1.
2871 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2872 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2874 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2877 /* Drop all constraints in bmap that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_map
*bmap
,
2882 enum isl_dim_type type
, unsigned first
, unsigned n
)
2887 isl_space
*space
= isl_basic_map_get_space(bmap
);
2888 isl_basic_map_free(bmap
);
2889 return isl_basic_map_universe(space
);
2891 bmap
= isl_basic_map_cow(bmap
);
2895 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2896 return isl_basic_map_free(bmap
);
2898 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2900 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2901 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2903 isl_basic_map_drop_equality(bmap
, i
);
2906 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2907 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2909 isl_basic_map_drop_inequality(bmap
, i
);
2912 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2916 /* Drop all constraints in bset that do not involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2919 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2920 __isl_take isl_basic_set
*bset
,
2921 enum isl_dim_type type
, unsigned first
, unsigned n
)
2923 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2927 /* Drop all constraints in bmap that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2931 __isl_take isl_basic_map
*bmap
,
2932 enum isl_dim_type type
, unsigned first
, unsigned n
)
2939 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2940 return isl_basic_map_free(bmap
);
2942 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2943 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2944 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2947 /* Drop all constraints in bset that involve any of the dimensions
2948 * first to first + n - 1 of the given type.
2950 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2951 __isl_take isl_basic_set
*bset
,
2952 enum isl_dim_type type
, unsigned first
, unsigned n
)
2954 return isl_basic_map_drop_constraints_involving_dims(bset
,
2958 /* Drop constraints from "map" by applying "drop" to each basic map.
2960 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2961 enum isl_dim_type type
, unsigned first
, unsigned n
,
2962 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2963 enum isl_dim_type type
, unsigned first
, unsigned n
))
2971 dim
= isl_map_dim(map
, type
);
2972 if (first
+ n
> dim
|| first
+ n
< first
)
2973 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2974 "index out of bounds", return isl_map_free(map
));
2976 map
= isl_map_cow(map
);
2980 for (i
= 0; i
< map
->n
; ++i
) {
2981 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2983 return isl_map_free(map
);
2987 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2992 /* Drop all constraints in map that involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2995 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2996 __isl_take isl_map
*map
,
2997 enum isl_dim_type type
, unsigned first
, unsigned n
)
3001 return drop_constraints(map
, type
, first
, n
,
3002 &isl_basic_map_drop_constraints_involving_dims
);
3005 /* Drop all constraints in "map" that do not involve any of the dimensions
3006 * first to first + n - 1 of the given type.
3008 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3009 __isl_take isl_map
*map
,
3010 enum isl_dim_type type
, unsigned first
, unsigned n
)
3013 isl_space
*space
= isl_map_get_space(map
);
3015 return isl_map_universe(space
);
3017 return drop_constraints(map
, type
, first
, n
,
3018 &isl_basic_map_drop_constraints_not_involving_dims
);
3021 /* Drop all constraints in set that involve any of the dimensions
3022 * first to first + n - 1 of the given type.
3024 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3025 __isl_take isl_set
*set
,
3026 enum isl_dim_type type
, unsigned first
, unsigned n
)
3028 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3031 /* Drop all constraints in "set" that do not involve any of the dimensions
3032 * first to first + n - 1 of the given type.
3034 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3035 __isl_take isl_set
*set
,
3036 enum isl_dim_type type
, unsigned first
, unsigned n
)
3038 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3041 /* Does local variable "div" of "bmap" have a complete explicit representation?
3042 * Having a complete explicit representation requires not only
3043 * an explicit representation, but also that all local variables
3044 * that appear in this explicit representation in turn have
3045 * a complete explicit representation.
3047 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3050 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3053 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3054 if (marked
< 0 || marked
)
3055 return isl_bool_not(marked
);
3057 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3060 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3062 known
= isl_basic_map_div_is_known(bmap
, i
);
3063 if (known
< 0 || !known
)
3067 return isl_bool_true
;
3070 /* Remove all divs that are unknown or defined in terms of unknown divs.
3072 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3073 __isl_take isl_basic_map
*bmap
)
3080 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3081 if (isl_basic_map_div_is_known(bmap
, i
))
3083 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3092 /* Remove all divs that are unknown or defined in terms of unknown divs.
3094 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3095 __isl_take isl_basic_set
*bset
)
3097 return isl_basic_map_remove_unknown_divs(bset
);
3100 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3109 map
= isl_map_cow(map
);
3113 for (i
= 0; i
< map
->n
; ++i
) {
3114 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3124 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3126 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3129 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3130 __isl_take isl_basic_set
*bset
,
3131 enum isl_dim_type type
, unsigned first
, unsigned n
)
3133 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3134 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3135 return bset_from_bmap(bmap
);
3138 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3139 enum isl_dim_type type
, unsigned first
, unsigned n
)
3146 map
= isl_map_cow(map
);
3149 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3151 for (i
= 0; i
< map
->n
; ++i
) {
3152 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3153 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3157 map
= isl_map_drop(map
, type
, first
, n
);
3164 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3165 enum isl_dim_type type
, unsigned first
, unsigned n
)
3167 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3171 /* Project out n inputs starting at first using Fourier-Motzkin */
3172 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3173 unsigned first
, unsigned n
)
3175 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3178 static void dump_term(struct isl_basic_map
*bmap
,
3179 isl_int c
, int pos
, FILE *out
)
3182 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3183 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3184 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3186 isl_int_print(out
, c
, 0);
3188 if (!isl_int_is_one(c
))
3189 isl_int_print(out
, c
, 0);
3190 if (pos
< 1 + nparam
) {
3191 name
= isl_space_get_dim_name(bmap
->dim
,
3192 isl_dim_param
, pos
- 1);
3194 fprintf(out
, "%s", name
);
3196 fprintf(out
, "p%d", pos
- 1);
3197 } else if (pos
< 1 + nparam
+ in
)
3198 fprintf(out
, "i%d", pos
- 1 - nparam
);
3199 else if (pos
< 1 + nparam
+ dim
)
3200 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3202 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3206 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3207 int sign
, FILE *out
)
3211 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3215 for (i
= 0, first
= 1; i
< len
; ++i
) {
3216 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3219 fprintf(out
, " + ");
3221 isl_int_abs(v
, c
[i
]);
3222 dump_term(bmap
, v
, i
, out
);
3229 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3230 const char *op
, FILE *out
, int indent
)
3234 fprintf(out
, "%*s", indent
, "");
3236 dump_constraint_sign(bmap
, c
, 1, out
);
3237 fprintf(out
, " %s ", op
);
3238 dump_constraint_sign(bmap
, c
, -1, out
);
3242 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3243 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3245 fprintf(out
, "%*s", indent
, "");
3246 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3251 static void dump_constraints(struct isl_basic_map
*bmap
,
3252 isl_int
**c
, unsigned n
,
3253 const char *op
, FILE *out
, int indent
)
3257 for (i
= 0; i
< n
; ++i
)
3258 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3261 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3265 unsigned total
= isl_basic_map_total_dim(bmap
);
3267 for (j
= 0; j
< 1 + total
; ++j
) {
3268 if (isl_int_is_zero(exp
[j
]))
3270 if (!first
&& isl_int_is_pos(exp
[j
]))
3272 dump_term(bmap
, exp
[j
], j
, out
);
3277 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3281 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3282 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3284 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3285 fprintf(out
, "%*s", indent
, "");
3286 fprintf(out
, "e%d = [(", i
);
3287 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3289 isl_int_print(out
, bmap
->div
[i
][0], 0);
3290 fprintf(out
, "]\n");
3294 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3295 FILE *out
, int indent
)
3298 fprintf(out
, "null basic set\n");
3302 fprintf(out
, "%*s", indent
, "");
3303 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3304 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3305 bset
->extra
, bset
->flags
);
3306 dump(bset_to_bmap(bset
), out
, indent
);
3309 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3310 FILE *out
, int indent
)
3313 fprintf(out
, "null basic map\n");
3317 fprintf(out
, "%*s", indent
, "");
3318 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3319 "flags: %x, n_name: %d\n",
3321 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3322 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3323 dump(bmap
, out
, indent
);
3326 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3331 total
= isl_basic_map_total_dim(bmap
);
3332 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3333 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3334 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3335 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3339 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3344 if (isl_space_dim(space
, isl_dim_in
) != 0)
3345 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3346 "set cannot have input dimensions", goto error
);
3347 return isl_map_alloc_space(space
, n
, flags
);
3349 isl_space_free(space
);
3353 /* Make sure "map" has room for at least "n" more basic maps.
3355 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3358 struct isl_map
*grown
= NULL
;
3362 isl_assert(map
->ctx
, n
>= 0, goto error
);
3363 if (map
->n
+ n
<= map
->size
)
3365 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3368 for (i
= 0; i
< map
->n
; ++i
) {
3369 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3377 isl_map_free(grown
);
3382 /* Make sure "set" has room for at least "n" more basic sets.
3384 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3386 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3389 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3391 return isl_map_from_basic_map(bset
);
3394 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3396 struct isl_map
*map
;
3401 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3402 return isl_map_add_basic_map(map
, bmap
);
3405 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3406 __isl_take isl_basic_set
*bset
)
3408 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3409 bset_to_bmap(bset
)));
3412 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3414 return isl_map_free(set
);
3417 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3422 fprintf(out
, "null set\n");
3426 fprintf(out
, "%*s", indent
, "");
3427 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3428 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3430 for (i
= 0; i
< set
->n
; ++i
) {
3431 fprintf(out
, "%*s", indent
, "");
3432 fprintf(out
, "basic set %d:\n", i
);
3433 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3437 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3442 fprintf(out
, "null map\n");
3446 fprintf(out
, "%*s", indent
, "");
3447 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3448 "flags: %x, n_name: %d\n",
3449 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3450 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3451 for (i
= 0; i
< map
->n
; ++i
) {
3452 fprintf(out
, "%*s", indent
, "");
3453 fprintf(out
, "basic map %d:\n", i
);
3454 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3458 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3459 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3461 struct isl_basic_map
*bmap_domain
;
3463 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3466 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3467 isl_assert(bset
->ctx
,
3468 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3470 bmap
= isl_basic_map_cow(bmap
);
3473 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3474 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3475 bmap_domain
= isl_basic_map_from_domain(bset
);
3476 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3478 bmap
= isl_basic_map_simplify(bmap
);
3479 return isl_basic_map_finalize(bmap
);
3481 isl_basic_map_free(bmap
);
3482 isl_basic_set_free(bset
);
3486 /* Check that the space of "bset" is the same as that of the range of "bmap".
3488 static isl_stat
isl_basic_map_check_compatible_range(
3489 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3493 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3495 return isl_stat_error
;
3497 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3498 "incompatible spaces", return isl_stat_error
);
3503 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3504 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3506 struct isl_basic_map
*bmap_range
;
3508 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3511 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3512 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3515 if (isl_basic_set_plain_is_universe(bset
)) {
3516 isl_basic_set_free(bset
);
3520 bmap
= isl_basic_map_cow(bmap
);
3523 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3524 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3525 bmap_range
= bset_to_bmap(bset
);
3526 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3528 bmap
= isl_basic_map_simplify(bmap
);
3529 return isl_basic_map_finalize(bmap
);
3531 isl_basic_map_free(bmap
);
3532 isl_basic_set_free(bset
);
3536 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3537 __isl_keep isl_vec
*vec
)
3544 return isl_bool_error
;
3546 total
= 1 + isl_basic_map_total_dim(bmap
);
3547 if (total
!= vec
->size
)
3548 return isl_bool_false
;
3552 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3553 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3554 if (!isl_int_is_zero(s
)) {
3556 return isl_bool_false
;
3560 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3561 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3562 if (isl_int_is_neg(s
)) {
3564 return isl_bool_false
;
3570 return isl_bool_true
;
3573 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3574 __isl_keep isl_vec
*vec
)
3576 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3579 __isl_give isl_basic_map
*isl_basic_map_intersect(
3580 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3582 struct isl_vec
*sample
= NULL
;
3584 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3586 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3587 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3588 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3589 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3590 return isl_basic_map_intersect(bmap2
, bmap1
);
3592 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3593 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3594 isl_assert(bmap1
->ctx
,
3595 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3597 if (isl_basic_map_plain_is_empty(bmap1
)) {
3598 isl_basic_map_free(bmap2
);
3601 if (isl_basic_map_plain_is_empty(bmap2
)) {
3602 isl_basic_map_free(bmap1
);
3606 if (bmap1
->sample
&&
3607 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3608 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3609 sample
= isl_vec_copy(bmap1
->sample
);
3610 else if (bmap2
->sample
&&
3611 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3612 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3613 sample
= isl_vec_copy(bmap2
->sample
);
3615 bmap1
= isl_basic_map_cow(bmap1
);
3618 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3619 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3620 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3623 isl_vec_free(sample
);
3625 isl_vec_free(bmap1
->sample
);
3626 bmap1
->sample
= sample
;
3629 bmap1
= isl_basic_map_simplify(bmap1
);
3630 return isl_basic_map_finalize(bmap1
);
3633 isl_vec_free(sample
);
3634 isl_basic_map_free(bmap1
);
3635 isl_basic_map_free(bmap2
);
3639 struct isl_basic_set
*isl_basic_set_intersect(
3640 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3642 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3643 bset_to_bmap(bset2
)));
3646 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3647 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3649 return isl_basic_set_intersect(bset1
, bset2
);
3652 /* Special case of isl_map_intersect, where both map1 and map2
3653 * are convex, without any divs and such that either map1 or map2
3654 * contains a single constraint. This constraint is then simply
3655 * added to the other map.
3657 static __isl_give isl_map
*map_intersect_add_constraint(
3658 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3660 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3661 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3662 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3663 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3665 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3666 return isl_map_intersect(map2
, map1
);
3668 map1
= isl_map_cow(map1
);
3671 if (isl_map_plain_is_empty(map1
)) {
3675 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3676 if (map2
->p
[0]->n_eq
== 1)
3677 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3679 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3680 map2
->p
[0]->ineq
[0]);
3682 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3683 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3687 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3688 isl_basic_map_free(map1
->p
[0]);
3701 /* map2 may be either a parameter domain or a map living in the same
3704 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3705 __isl_take isl_map
*map2
)
3714 if ((isl_map_plain_is_empty(map1
) ||
3715 isl_map_plain_is_universe(map2
)) &&
3716 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3720 if ((isl_map_plain_is_empty(map2
) ||
3721 isl_map_plain_is_universe(map1
)) &&
3722 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3727 if (map1
->n
== 1 && map2
->n
== 1 &&
3728 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3729 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3730 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3731 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3732 return map_intersect_add_constraint(map1
, map2
);
3734 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3735 isl_space_dim(map2
->dim
, isl_dim_param
))
3736 isl_assert(map1
->ctx
,
3737 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3739 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3740 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3741 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3743 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3744 map1
->n
* map2
->n
, flags
);
3747 for (i
= 0; i
< map1
->n
; ++i
)
3748 for (j
= 0; j
< map2
->n
; ++j
) {
3749 struct isl_basic_map
*part
;
3750 part
= isl_basic_map_intersect(
3751 isl_basic_map_copy(map1
->p
[i
]),
3752 isl_basic_map_copy(map2
->p
[j
]));
3753 if (isl_basic_map_is_empty(part
) < 0)
3754 part
= isl_basic_map_free(part
);
3755 result
= isl_map_add_basic_map(result
, part
);
3768 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3769 __isl_take isl_map
*map2
)
3773 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3774 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3775 "spaces don't match", goto error
);
3776 return map_intersect_internal(map1
, map2
);
3783 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3784 __isl_take isl_map
*map2
)
3786 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3789 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3791 return set_from_map(isl_map_intersect(set_to_map(set1
),
3795 /* map_intersect_internal accepts intersections
3796 * with parameter domains, so we can just call that function.
3798 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3799 __isl_take isl_set
*params
)
3801 return map_intersect_internal(map
, params
);
3804 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3805 __isl_take isl_map
*map2
)
3807 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3810 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3811 __isl_take isl_set
*params
)
3813 return isl_map_intersect_params(set
, params
);
3816 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3819 unsigned pos
, n1
, n2
;
3823 bmap
= isl_basic_map_cow(bmap
);
3826 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3827 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3828 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3829 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3830 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3831 return isl_basic_map_reset_space(bmap
, space
);
3834 static __isl_give isl_basic_map
*basic_map_space_reset(
3835 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3841 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3844 space
= isl_basic_map_get_space(bmap
);
3845 space
= isl_space_reset(space
, type
);
3846 bmap
= isl_basic_map_reset_space(bmap
, space
);
3850 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3851 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3852 unsigned pos
, unsigned n
)
3856 struct isl_basic_map
*res
;
3857 struct isl_dim_map
*dim_map
;
3858 unsigned total
, off
;
3859 enum isl_dim_type t
;
3862 return basic_map_space_reset(bmap
, type
);
3867 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3869 total
= isl_basic_map_total_dim(bmap
) + n
;
3870 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3872 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3874 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3876 unsigned size
= isl_basic_map_dim(bmap
, t
);
3877 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3879 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3880 pos
, size
- pos
, off
+ pos
+ n
);
3882 off
+= isl_space_dim(res_dim
, t
);
3884 isl_dim_map_div(dim_map
, bmap
, off
);
3886 res
= isl_basic_map_alloc_space(res_dim
,
3887 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3888 rational
= isl_basic_map_is_rational(bmap
);
3890 res
= isl_basic_map_free(res
);
3892 res
= isl_basic_map_set_rational(res
);
3893 if (isl_basic_map_plain_is_empty(bmap
)) {
3894 isl_basic_map_free(bmap
);
3896 return isl_basic_map_set_to_empty(res
);
3898 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3899 return isl_basic_map_finalize(res
);
3902 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3903 __isl_take isl_basic_set
*bset
,
3904 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3906 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3909 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3910 enum isl_dim_type type
, unsigned n
)
3914 return isl_basic_map_insert_dims(bmap
, type
,
3915 isl_basic_map_dim(bmap
, type
), n
);
3918 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3919 enum isl_dim_type type
, unsigned n
)
3923 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3924 return isl_basic_map_add_dims(bset
, type
, n
);
3926 isl_basic_set_free(bset
);
3930 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3931 enum isl_dim_type type
)
3935 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3938 space
= isl_map_get_space(map
);
3939 space
= isl_space_reset(space
, type
);
3940 map
= isl_map_reset_space(map
, space
);
3944 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3945 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3950 return map_space_reset(map
, type
);
3952 map
= isl_map_cow(map
);
3956 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3960 for (i
= 0; i
< map
->n
; ++i
) {
3961 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3972 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3973 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3975 return isl_map_insert_dims(set
, type
, pos
, n
);
3978 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3979 enum isl_dim_type type
, unsigned n
)
3983 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3986 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3987 enum isl_dim_type type
, unsigned n
)
3991 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3992 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3998 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3999 __isl_take isl_basic_map
*bmap
,
4000 enum isl_dim_type dst_type
, unsigned dst_pos
,
4001 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4003 struct isl_dim_map
*dim_map
;
4004 struct isl_basic_map
*res
;
4005 enum isl_dim_type t
;
4006 unsigned total
, off
;
4011 bmap
= isl_basic_map_reset(bmap
, src_type
);
4012 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4016 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4017 return isl_basic_map_free(bmap
);
4019 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4022 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4024 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4025 pos(bmap
->dim
, src_type
) + src_pos
+
4026 ((src_type
< dst_type
) ? n
: 0)) {
4027 bmap
= isl_basic_map_cow(bmap
);
4031 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4032 src_type
, src_pos
, n
);
4036 bmap
= isl_basic_map_finalize(bmap
);
4041 total
= isl_basic_map_total_dim(bmap
);
4042 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4045 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4046 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4047 if (t
== dst_type
) {
4048 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4051 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4054 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4055 dst_pos
, size
- dst_pos
, off
);
4056 off
+= size
- dst_pos
;
4057 } else if (t
== src_type
) {
4058 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4061 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4062 src_pos
+ n
, size
- src_pos
- n
, off
);
4063 off
+= size
- src_pos
- n
;
4065 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4069 isl_dim_map_div(dim_map
, bmap
, off
);
4071 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4072 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4073 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4077 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4078 src_type
, src_pos
, n
);
4082 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4083 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4084 bmap
= isl_basic_map_finalize(bmap
);
4088 isl_basic_map_free(bmap
);
4092 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4093 enum isl_dim_type dst_type
, unsigned dst_pos
,
4094 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4096 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4097 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4098 src_type
, src_pos
, n
);
4099 return bset_from_bmap(bmap
);
4102 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4103 enum isl_dim_type dst_type
, unsigned dst_pos
,
4104 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4108 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4109 return set_from_map(isl_map_move_dims(set_to_map(set
),
4110 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4116 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4117 enum isl_dim_type dst_type
, unsigned dst_pos
,
4118 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4125 map
= isl_map_reset(map
, src_type
);
4126 map
= isl_map_reset(map
, dst_type
);
4130 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
4133 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4136 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4138 map
= isl_map_cow(map
);
4142 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4146 for (i
= 0; i
< map
->n
; ++i
) {
4147 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4149 src_type
, src_pos
, n
);
4160 /* Move the specified dimensions to the last columns right before
4161 * the divs. Don't change the dimension specification of bmap.
4162 * That's the responsibility of the caller.
4164 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4165 enum isl_dim_type type
, unsigned first
, unsigned n
)
4167 struct isl_dim_map
*dim_map
;
4168 struct isl_basic_map
*res
;
4169 enum isl_dim_type t
;
4170 unsigned total
, off
;
4174 if (pos(bmap
->dim
, type
) + first
+ n
==
4175 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4178 total
= isl_basic_map_total_dim(bmap
);
4179 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4182 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4183 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4185 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4188 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4189 first
, n
, total
- bmap
->n_div
- n
);
4190 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4191 first
+ n
, size
- (first
+ n
), off
);
4192 off
+= size
- (first
+ n
);
4194 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4198 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4200 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4201 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4202 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4206 /* Insert "n" rows in the divs of "bmap".
4208 * The number of columns is not changed, which means that the last
4209 * dimensions of "bmap" are being reintepreted as the new divs.
4210 * The space of "bmap" is not adjusted, however, which means
4211 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4212 * from the space of "bmap" is the responsibility of the caller.
4214 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4222 bmap
= isl_basic_map_cow(bmap
);
4226 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4227 old
= bmap
->block2
.data
;
4228 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4229 (bmap
->extra
+ n
) * (1 + row_size
));
4230 if (!bmap
->block2
.data
)
4231 return isl_basic_map_free(bmap
);
4232 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4234 return isl_basic_map_free(bmap
);
4235 for (i
= 0; i
< n
; ++i
) {
4236 new_div
[i
] = bmap
->block2
.data
+
4237 (bmap
->extra
+ i
) * (1 + row_size
);
4238 isl_seq_clr(new_div
[i
], 1 + row_size
);
4240 for (i
= 0; i
< bmap
->extra
; ++i
)
4241 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4243 bmap
->div
= new_div
;
4250 /* Drop constraints from "bmap" that only involve the variables
4251 * of "type" in the range [first, first + n] that are not related
4252 * to any of the variables outside that interval.
4253 * These constraints cannot influence the values for the variables
4254 * outside the interval, except in case they cause "bmap" to be empty.
4255 * Only drop the constraints if "bmap" is known to be non-empty.
4257 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4258 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4259 unsigned first
, unsigned n
)
4263 unsigned dim
, n_div
;
4266 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4268 return isl_basic_map_free(bmap
);
4272 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4273 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4274 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4276 return isl_basic_map_free(bmap
);
4277 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4278 for (i
= 0; i
< first
; ++i
)
4280 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4283 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4288 /* Turn the n dimensions of type type, starting at first
4289 * into existentially quantified variables.
4291 * If a subset of the projected out variables are unrelated
4292 * to any of the variables that remain, then the constraints
4293 * involving this subset are simply dropped first.
4295 __isl_give isl_basic_map
*isl_basic_map_project_out(
4296 __isl_take isl_basic_map
*bmap
,
4297 enum isl_dim_type type
, unsigned first
, unsigned n
)
4302 return basic_map_space_reset(bmap
, type
);
4303 if (type
== isl_dim_div
)
4304 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4305 "cannot project out existentially quantified variables",
4306 return isl_basic_map_free(bmap
));
4308 empty
= isl_basic_map_plain_is_empty(bmap
);
4310 return isl_basic_map_free(bmap
);
4312 bmap
= isl_basic_map_set_to_empty(bmap
);
4314 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4318 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4319 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4321 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4322 return isl_basic_map_free(bmap
);
4324 bmap
= move_last(bmap
, type
, first
, n
);
4325 bmap
= isl_basic_map_cow(bmap
);
4326 bmap
= insert_div_rows(bmap
, n
);
4330 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4333 bmap
= isl_basic_map_simplify(bmap
);
4334 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4335 return isl_basic_map_finalize(bmap
);
4337 isl_basic_map_free(bmap
);
4341 /* Turn the n dimensions of type type, starting at first
4342 * into existentially quantified variables.
4344 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4345 enum isl_dim_type type
, unsigned first
, unsigned n
)
4347 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4351 /* Turn the n dimensions of type type, starting at first
4352 * into existentially quantified variables.
4354 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4355 enum isl_dim_type type
, unsigned first
, unsigned n
)
4363 return map_space_reset(map
, type
);
4365 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
4367 map
= isl_map_cow(map
);
4371 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4375 for (i
= 0; i
< map
->n
; ++i
) {
4376 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4387 /* Turn the n dimensions of type type, starting at first
4388 * into existentially quantified variables.
4390 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4391 enum isl_dim_type type
, unsigned first
, unsigned n
)
4393 return set_from_map(isl_map_project_out(set_to_map(set
),
4397 /* Return a map that projects the elements in "set" onto their
4398 * "n" set dimensions starting at "first".
4399 * "type" should be equal to isl_dim_set.
4401 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4402 enum isl_dim_type type
, unsigned first
, unsigned n
)
4410 if (type
!= isl_dim_set
)
4411 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4412 "only set dimensions can be projected out", goto error
);
4413 dim
= isl_set_dim(set
, isl_dim_set
);
4414 if (first
+ n
> dim
|| first
+ n
< first
)
4415 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4416 "index out of bounds", goto error
);
4418 map
= isl_map_from_domain(set
);
4419 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4420 for (i
= 0; i
< n
; ++i
)
4421 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4429 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4433 for (i
= 0; i
< n
; ++i
) {
4434 j
= isl_basic_map_alloc_div(bmap
);
4437 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4441 isl_basic_map_free(bmap
);
4445 struct isl_basic_map
*isl_basic_map_apply_range(
4446 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4448 isl_space
*dim_result
= NULL
;
4449 struct isl_basic_map
*bmap
;
4450 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4451 struct isl_dim_map
*dim_map1
, *dim_map2
;
4453 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4455 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4456 bmap2
->dim
, isl_dim_in
))
4457 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4458 "spaces don't match", goto error
);
4460 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4461 isl_space_copy(bmap2
->dim
));
4463 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4464 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4465 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4466 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4468 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4469 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4470 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4471 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4472 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4473 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4474 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4475 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4476 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4477 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4478 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4480 bmap
= isl_basic_map_alloc_space(dim_result
,
4481 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4482 bmap1
->n_eq
+ bmap2
->n_eq
,
4483 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4484 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4485 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4486 bmap
= add_divs(bmap
, n
);
4487 bmap
= isl_basic_map_simplify(bmap
);
4488 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4489 return isl_basic_map_finalize(bmap
);
4491 isl_basic_map_free(bmap1
);
4492 isl_basic_map_free(bmap2
);
4496 struct isl_basic_set
*isl_basic_set_apply(
4497 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4502 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4505 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4508 isl_basic_set_free(bset
);
4509 isl_basic_map_free(bmap
);
4513 struct isl_basic_map
*isl_basic_map_apply_domain(
4514 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4516 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4518 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4519 bmap2
->dim
, isl_dim_in
))
4520 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4521 "spaces don't match", goto error
);
4523 bmap1
= isl_basic_map_reverse(bmap1
);
4524 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4525 return isl_basic_map_reverse(bmap1
);
4527 isl_basic_map_free(bmap1
);
4528 isl_basic_map_free(bmap2
);
4532 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4533 * A \cap B -> f(A) + f(B)
4535 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4536 __isl_take isl_basic_map
*bmap2
)
4538 unsigned n_in
, n_out
, nparam
, total
, pos
;
4539 struct isl_basic_map
*bmap
= NULL
;
4540 struct isl_dim_map
*dim_map1
, *dim_map2
;
4543 if (!bmap1
|| !bmap2
)
4546 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4549 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4550 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4551 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4553 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4554 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4555 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4556 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4557 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4558 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4559 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4560 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4561 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4562 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4563 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4565 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4566 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4567 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4568 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4569 for (i
= 0; i
< n_out
; ++i
) {
4570 int j
= isl_basic_map_alloc_equality(bmap
);
4573 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4574 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4575 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4576 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4578 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4579 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4580 bmap
= add_divs(bmap
, 2 * n_out
);
4582 bmap
= isl_basic_map_simplify(bmap
);
4583 return isl_basic_map_finalize(bmap
);
4585 isl_basic_map_free(bmap
);
4586 isl_basic_map_free(bmap1
);
4587 isl_basic_map_free(bmap2
);
4591 /* Given two maps A -> f(A) and B -> g(B), construct a map
4592 * A \cap B -> f(A) + f(B)
4594 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4595 __isl_take isl_map
*map2
)
4597 struct isl_map
*result
;
4603 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4605 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4606 map1
->n
* map2
->n
, 0);
4609 for (i
= 0; i
< map1
->n
; ++i
)
4610 for (j
= 0; j
< map2
->n
; ++j
) {
4611 struct isl_basic_map
*part
;
4612 part
= isl_basic_map_sum(
4613 isl_basic_map_copy(map1
->p
[i
]),
4614 isl_basic_map_copy(map2
->p
[j
]));
4615 if (isl_basic_map_is_empty(part
))
4616 isl_basic_map_free(part
);
4618 result
= isl_map_add_basic_map(result
, part
);
4631 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4632 __isl_take isl_set
*set2
)
4634 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4637 /* Given a basic map A -> f(A), construct A -> -f(A).
4639 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4644 bmap
= isl_basic_map_cow(bmap
);
4648 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4649 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4650 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4651 for (j
= 0; j
< n
; ++j
)
4652 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4653 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4654 for (j
= 0; j
< n
; ++j
)
4655 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4656 for (i
= 0; i
< bmap
->n_div
; ++i
)
4657 for (j
= 0; j
< n
; ++j
)
4658 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4659 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4660 return isl_basic_map_finalize(bmap
);
4663 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4665 return isl_basic_map_neg(bset
);
4668 /* Given a map A -> f(A), construct A -> -f(A).
4670 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4674 map
= isl_map_cow(map
);
4678 for (i
= 0; i
< map
->n
; ++i
) {
4679 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4690 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4692 return set_from_map(isl_map_neg(set_to_map(set
)));
4695 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4696 * A -> floor(f(A)/d).
4698 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4701 unsigned n_in
, n_out
, nparam
, total
, pos
;
4702 struct isl_basic_map
*result
= NULL
;
4703 struct isl_dim_map
*dim_map
;
4709 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4710 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4711 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4713 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4714 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4715 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4716 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4717 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4718 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4720 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4721 bmap
->n_div
+ n_out
,
4722 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4723 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4724 result
= add_divs(result
, n_out
);
4725 for (i
= 0; i
< n_out
; ++i
) {
4727 j
= isl_basic_map_alloc_inequality(result
);
4730 isl_seq_clr(result
->ineq
[j
], 1+total
);
4731 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4732 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4733 j
= isl_basic_map_alloc_inequality(result
);
4736 isl_seq_clr(result
->ineq
[j
], 1+total
);
4737 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4738 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4739 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4742 result
= isl_basic_map_simplify(result
);
4743 return isl_basic_map_finalize(result
);
4745 isl_basic_map_free(result
);
4749 /* Given a map A -> f(A) and an integer d, construct a map
4750 * A -> floor(f(A)/d).
4752 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4756 map
= isl_map_cow(map
);
4760 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4761 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4762 for (i
= 0; i
< map
->n
; ++i
) {
4763 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4774 /* Given a map A -> f(A) and an integer d, construct a map
4775 * A -> floor(f(A)/d).
4777 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4778 __isl_take isl_val
*d
)
4782 if (!isl_val_is_int(d
))
4783 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4784 "expecting integer denominator", goto error
);
4785 map
= isl_map_floordiv(map
, d
->n
);
4794 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4801 i
= isl_basic_map_alloc_equality(bmap
);
4804 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4805 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4806 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4807 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4808 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4809 return isl_basic_map_finalize(bmap
);
4811 isl_basic_map_free(bmap
);
4815 /* Add a constraint to "bmap" expressing i_pos < o_pos
4817 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4824 i
= isl_basic_map_alloc_inequality(bmap
);
4827 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4828 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4829 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4830 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4831 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4832 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4833 return isl_basic_map_finalize(bmap
);
4835 isl_basic_map_free(bmap
);
4839 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4841 static __isl_give isl_basic_map
*var_less_or_equal(
4842 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4848 i
= isl_basic_map_alloc_inequality(bmap
);
4851 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4852 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4853 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4854 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4855 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4856 return isl_basic_map_finalize(bmap
);
4858 isl_basic_map_free(bmap
);
4862 /* Add a constraint to "bmap" expressing i_pos > o_pos
4864 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4871 i
= isl_basic_map_alloc_inequality(bmap
);
4874 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4875 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4876 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4877 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4878 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4879 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4880 return isl_basic_map_finalize(bmap
);
4882 isl_basic_map_free(bmap
);
4886 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4888 static __isl_give isl_basic_map
*var_more_or_equal(
4889 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4895 i
= isl_basic_map_alloc_inequality(bmap
);
4898 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4899 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4900 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4901 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4902 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4903 return isl_basic_map_finalize(bmap
);
4905 isl_basic_map_free(bmap
);
4909 __isl_give isl_basic_map
*isl_basic_map_equal(
4910 __isl_take isl_space
*dim
, unsigned n_equal
)
4913 struct isl_basic_map
*bmap
;
4914 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4917 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4918 bmap
= var_equal(bmap
, i
);
4919 return isl_basic_map_finalize(bmap
);
4922 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4924 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4928 struct isl_basic_map
*bmap
;
4929 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4932 for (i
= 0; i
< pos
&& bmap
; ++i
)
4933 bmap
= var_equal(bmap
, i
);
4935 bmap
= var_less(bmap
, pos
);
4936 return isl_basic_map_finalize(bmap
);
4939 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4941 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4942 __isl_take isl_space
*dim
, unsigned pos
)
4945 isl_basic_map
*bmap
;
4947 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4948 for (i
= 0; i
< pos
; ++i
)
4949 bmap
= var_equal(bmap
, i
);
4950 bmap
= var_less_or_equal(bmap
, pos
);
4951 return isl_basic_map_finalize(bmap
);
4954 /* Return a relation on "dim" expressing i_pos > o_pos
4956 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4960 struct isl_basic_map
*bmap
;
4961 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4964 for (i
= 0; i
< pos
&& bmap
; ++i
)
4965 bmap
= var_equal(bmap
, i
);
4967 bmap
= var_more(bmap
, pos
);
4968 return isl_basic_map_finalize(bmap
);
4971 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4973 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4974 __isl_take isl_space
*dim
, unsigned pos
)
4977 isl_basic_map
*bmap
;
4979 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4980 for (i
= 0; i
< pos
; ++i
)
4981 bmap
= var_equal(bmap
, i
);
4982 bmap
= var_more_or_equal(bmap
, pos
);
4983 return isl_basic_map_finalize(bmap
);
4986 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4987 unsigned n
, int equal
)
4989 struct isl_map
*map
;
4992 if (n
== 0 && equal
)
4993 return isl_map_universe(dims
);
4995 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4997 for (i
= 0; i
+ 1 < n
; ++i
)
4998 map
= isl_map_add_basic_map(map
,
4999 isl_basic_map_less_at(isl_space_copy(dims
), i
));
5002 map
= isl_map_add_basic_map(map
,
5003 isl_basic_map_less_or_equal_at(dims
, n
- 1));
5005 map
= isl_map_add_basic_map(map
,
5006 isl_basic_map_less_at(dims
, n
- 1));
5008 isl_space_free(dims
);
5013 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
5017 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
5020 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5022 return map_lex_lte_first(dim
, n
, 0);
5025 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5027 return map_lex_lte_first(dim
, n
, 1);
5030 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5032 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5035 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5037 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5040 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
5041 unsigned n
, int equal
)
5043 struct isl_map
*map
;
5046 if (n
== 0 && equal
)
5047 return isl_map_universe(dims
);
5049 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
5051 for (i
= 0; i
+ 1 < n
; ++i
)
5052 map
= isl_map_add_basic_map(map
,
5053 isl_basic_map_more_at(isl_space_copy(dims
), i
));
5056 map
= isl_map_add_basic_map(map
,
5057 isl_basic_map_more_or_equal_at(dims
, n
- 1));
5059 map
= isl_map_add_basic_map(map
,
5060 isl_basic_map_more_at(dims
, n
- 1));
5062 isl_space_free(dims
);
5067 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
5071 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
5074 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5076 return map_lex_gte_first(dim
, n
, 0);
5079 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5081 return map_lex_gte_first(dim
, n
, 1);
5084 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5086 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5089 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5091 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5094 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5095 __isl_take isl_set
*set2
)
5098 map
= isl_map_lex_le(isl_set_get_space(set1
));
5099 map
= isl_map_intersect_domain(map
, set1
);
5100 map
= isl_map_intersect_range(map
, set2
);
5104 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5105 __isl_take isl_set
*set2
)
5108 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5109 map
= isl_map_intersect_domain(map
, set1
);
5110 map
= isl_map_intersect_range(map
, set2
);
5114 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5115 __isl_take isl_set
*set2
)
5118 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5119 map
= isl_map_intersect_domain(map
, set1
);
5120 map
= isl_map_intersect_range(map
, set2
);
5124 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5125 __isl_take isl_set
*set2
)
5128 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5129 map
= isl_map_intersect_domain(map
, set1
);
5130 map
= isl_map_intersect_range(map
, set2
);
5134 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5135 __isl_take isl_map
*map2
)
5138 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5139 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5140 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5144 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5145 __isl_take isl_map
*map2
)
5148 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5149 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5150 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5154 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5155 __isl_take isl_map
*map2
)
5158 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5159 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5160 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5164 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5165 __isl_take isl_map
*map2
)
5168 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5169 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5170 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5174 /* For a div d = floor(f/m), add the constraint
5178 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5179 unsigned pos
, isl_int
*div
)
5182 unsigned total
= isl_basic_map_total_dim(bmap
);
5184 i
= isl_basic_map_alloc_inequality(bmap
);
5186 return isl_stat_error
;
5187 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5188 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5193 /* For a div d = floor(f/m), add the constraint
5195 * -(f-(m-1)) + m d >= 0
5197 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5198 unsigned pos
, isl_int
*div
)
5201 unsigned total
= isl_basic_map_total_dim(bmap
);
5203 i
= isl_basic_map_alloc_inequality(bmap
);
5205 return isl_stat_error
;
5206 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5207 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5208 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5209 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5214 /* For a div d = floor(f/m), add the constraints
5217 * -(f-(m-1)) + m d >= 0
5219 * Note that the second constraint is the negation of
5223 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5224 unsigned pos
, isl_int
*div
)
5226 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5228 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5233 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5234 unsigned pos
, isl_int
*div
)
5236 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5240 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
5242 unsigned total
= isl_basic_map_total_dim(bmap
);
5243 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5245 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5249 /* For each known div d = floor(f/m), add the constraints
5252 * -(f-(m-1)) + m d >= 0
5254 * Remove duplicate constraints in case of some these div constraints
5255 * already appear in "bmap".
5257 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5258 __isl_take isl_basic_map
*bmap
)
5264 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5268 bmap
= add_known_div_constraints(bmap
);
5269 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5270 bmap
= isl_basic_map_finalize(bmap
);
5274 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5276 * In particular, if this div is of the form d = floor(f/m),
5277 * then add the constraint
5281 * if sign < 0 or the constraint
5283 * -(f-(m-1)) + m d >= 0
5287 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5288 unsigned div
, int sign
)
5296 total
= isl_basic_map_total_dim(bmap
);
5297 div_pos
= total
- bmap
->n_div
+ div
;
5300 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5302 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5305 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5306 __isl_take isl_basic_map
*bmap
)
5310 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5312 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5313 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5314 return bset_from_bmap(bmap
);
5315 bmap
= isl_basic_map_cow(bmap
);
5318 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5321 bmap
->extra
-= bmap
->n_div
;
5323 bmap
= isl_basic_map_finalize(bmap
);
5324 return bset_from_bmap(bmap
);
5326 isl_basic_map_free(bmap
);
5330 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5331 __isl_take isl_basic_set
*bset
)
5333 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5336 /* Replace each element in "list" by the result of applying
5337 * isl_basic_map_underlying_set to the element.
5339 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5340 __isl_take isl_basic_map_list
*list
)
5347 n
= isl_basic_map_list_n_basic_map(list
);
5348 for (i
= 0; i
< n
; ++i
) {
5349 isl_basic_map
*bmap
;
5350 isl_basic_set
*bset
;
5352 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5353 bset
= isl_basic_set_underlying_set(bmap
);
5354 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5360 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5361 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5363 struct isl_basic_map
*bmap
;
5364 struct isl_ctx
*ctx
;
5371 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5372 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5373 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5375 if (like
->n_div
== 0) {
5376 isl_space
*space
= isl_basic_map_get_space(like
);
5377 isl_basic_map_free(like
);
5378 return isl_basic_map_reset_space(bset
, space
);
5380 bset
= isl_basic_set_cow(bset
);
5383 total
= bset
->dim
->n_out
+ bset
->extra
;
5384 bmap
= bset_to_bmap(bset
);
5385 isl_space_free(bmap
->dim
);
5386 bmap
->dim
= isl_space_copy(like
->dim
);
5389 bmap
->n_div
= like
->n_div
;
5390 bmap
->extra
+= like
->n_div
;
5394 ltotal
= total
- bmap
->extra
+ like
->extra
;
5397 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5398 bmap
->extra
* (1 + 1 + total
));
5399 if (isl_blk_is_error(bmap
->block2
))
5401 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5405 for (i
= 0; i
< bmap
->extra
; ++i
)
5406 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5407 for (i
= 0; i
< like
->n_div
; ++i
) {
5408 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5409 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5411 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5413 isl_basic_map_free(like
);
5414 bmap
= isl_basic_map_simplify(bmap
);
5415 bmap
= isl_basic_map_finalize(bmap
);
5418 isl_basic_map_free(like
);
5419 isl_basic_set_free(bset
);
5423 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5424 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5426 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5427 bset_to_bmap(like
)));
5430 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5434 map
= isl_map_cow(map
);
5437 map
->dim
= isl_space_cow(map
->dim
);
5441 for (i
= 1; i
< map
->n
; ++i
)
5442 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5444 for (i
= 0; i
< map
->n
; ++i
) {
5445 map
->p
[i
] = bset_to_bmap(
5446 isl_basic_map_underlying_set(map
->p
[i
]));
5451 map
->dim
= isl_space_underlying(map
->dim
, 0);
5453 isl_space_free(map
->dim
);
5454 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5458 return set_from_map(map
);
5464 /* Replace the space of "bmap" by "space".
5466 * If the space of "bmap" is identical to "space" (including the identifiers
5467 * of the input and output dimensions), then simply return the original input.
5469 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5470 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5473 isl_space
*bmap_space
;
5475 bmap_space
= isl_basic_map_peek_space(bmap
);
5476 equal
= isl_space_is_equal(bmap_space
, space
);
5477 if (equal
>= 0 && equal
)
5478 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5482 isl_space_free(space
);
5485 bmap
= isl_basic_map_cow(bmap
);
5486 if (!bmap
|| !space
)
5489 isl_space_free(bmap
->dim
);
5492 bmap
= isl_basic_map_finalize(bmap
);
5496 isl_basic_map_free(bmap
);
5497 isl_space_free(space
);
5501 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5502 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5504 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5508 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5509 __isl_take isl_space
*dim
)
5513 map
= isl_map_cow(map
);
5517 for (i
= 0; i
< map
->n
; ++i
) {
5518 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5519 isl_space_copy(dim
));
5523 isl_space_free(map
->dim
);
5529 isl_space_free(dim
);
5533 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5534 __isl_take isl_space
*dim
)
5536 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5539 /* Compute the parameter domain of the given basic set.
5541 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5547 is_params
= isl_basic_set_is_params(bset
);
5549 return isl_basic_set_free(bset
);
5553 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5554 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5555 space
= isl_basic_set_get_space(bset
);
5556 space
= isl_space_params(space
);
5557 bset
= isl_basic_set_reset_space(bset
, space
);
5561 /* Construct a zero-dimensional basic set with the given parameter domain.
5563 __isl_give isl_basic_set
*isl_basic_set_from_params(
5564 __isl_take isl_basic_set
*bset
)
5567 space
= isl_basic_set_get_space(bset
);
5568 space
= isl_space_set_from_params(space
);
5569 bset
= isl_basic_set_reset_space(bset
, space
);
5573 /* Compute the parameter domain of the given set.
5575 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5580 if (isl_set_is_params(set
))
5583 n
= isl_set_dim(set
, isl_dim_set
);
5584 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5585 space
= isl_set_get_space(set
);
5586 space
= isl_space_params(space
);
5587 set
= isl_set_reset_space(set
, space
);
5591 /* Construct a zero-dimensional set with the given parameter domain.
5593 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5596 space
= isl_set_get_space(set
);
5597 space
= isl_space_set_from_params(space
);
5598 set
= isl_set_reset_space(set
, space
);
5602 /* Compute the parameter domain of the given map.
5604 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5609 n
= isl_map_dim(map
, isl_dim_in
);
5610 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5611 n
= isl_map_dim(map
, isl_dim_out
);
5612 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5613 space
= isl_map_get_space(map
);
5614 space
= isl_space_params(space
);
5615 map
= isl_map_reset_space(map
, space
);
5619 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5626 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5628 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5629 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5631 return isl_basic_map_reset_space(bmap
, space
);
5634 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5637 return isl_bool_error
;
5638 return isl_space_may_be_set(bmap
->dim
);
5641 /* Is this basic map actually a set?
5642 * Users should never call this function. Outside of isl,
5643 * the type should indicate whether something is a set or a map.
5645 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5648 return isl_bool_error
;
5649 return isl_space_is_set(bmap
->dim
);
5652 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5656 is_set
= isl_basic_map_is_set(bmap
);
5661 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5663 isl_basic_map_free(bmap
);
5667 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5668 __isl_take isl_basic_map
*bmap
)
5672 isl_basic_map
*domain
;
5673 int nparam
, n_in
, n_out
;
5675 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5676 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5677 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5679 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5680 domain
= isl_basic_map_universe(dim
);
5682 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5683 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5684 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5686 for (i
= 0; i
< n_in
; ++i
)
5687 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5690 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5691 return isl_basic_map_finalize(bmap
);
5694 __isl_give isl_basic_map
*isl_basic_map_range_map(
5695 __isl_take isl_basic_map
*bmap
)
5699 isl_basic_map
*range
;
5700 int nparam
, n_in
, n_out
;
5702 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5703 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5704 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5706 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5707 range
= isl_basic_map_universe(dim
);
5709 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5710 bmap
= isl_basic_map_apply_range(bmap
, range
);
5711 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5713 for (i
= 0; i
< n_out
; ++i
)
5714 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5717 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5718 return isl_basic_map_finalize(bmap
);
5721 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5725 return isl_space_may_be_set(map
->dim
);
5728 /* Is this map actually a set?
5729 * Users should never call this function. Outside of isl,
5730 * the type should indicate whether something is a set or a map.
5732 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5735 return isl_bool_error
;
5736 return isl_space_is_set(map
->dim
);
5739 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5743 struct isl_set
*set
;
5745 is_set
= isl_map_is_set(map
);
5749 return set_from_map(map
);
5751 map
= isl_map_cow(map
);
5755 set
= set_from_map(map
);
5756 set
->dim
= isl_space_range(set
->dim
);
5759 for (i
= 0; i
< map
->n
; ++i
) {
5760 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5764 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5765 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5772 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5776 map
= isl_map_cow(map
);
5780 map
->dim
= isl_space_domain_map(map
->dim
);
5783 for (i
= 0; i
< map
->n
; ++i
) {
5784 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5788 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5789 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5796 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5799 isl_space
*range_dim
;
5801 map
= isl_map_cow(map
);
5805 range_dim
= isl_space_range(isl_map_get_space(map
));
5806 range_dim
= isl_space_from_range(range_dim
);
5807 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5808 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5811 for (i
= 0; i
< map
->n
; ++i
) {
5812 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5816 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5817 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5824 /* Given a wrapped map of the form A[B -> C],
5825 * return the map A[B -> C] -> B.
5827 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5834 if (!isl_set_has_tuple_id(set
))
5835 return isl_map_domain_map(isl_set_unwrap(set
));
5837 id
= isl_set_get_tuple_id(set
);
5838 map
= isl_map_domain_map(isl_set_unwrap(set
));
5839 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5844 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5845 __isl_take isl_basic_set
*bset
)
5847 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5850 __isl_give isl_basic_map
*isl_basic_map_from_range(
5851 __isl_take isl_basic_set
*bset
)
5854 space
= isl_basic_set_get_space(bset
);
5855 space
= isl_space_from_range(space
);
5856 bset
= isl_basic_set_reset_space(bset
, space
);
5857 return bset_to_bmap(bset
);
5860 /* Create a relation with the given set as range.
5861 * The domain of the created relation is a zero-dimensional
5862 * flat anonymous space.
5864 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5867 space
= isl_set_get_space(set
);
5868 space
= isl_space_from_range(space
);
5869 set
= isl_set_reset_space(set
, space
);
5870 return set_to_map(set
);
5873 /* Create a relation with the given set as domain.
5874 * The range of the created relation is a zero-dimensional
5875 * flat anonymous space.
5877 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5879 return isl_map_reverse(isl_map_from_range(set
));
5882 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5883 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5885 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5888 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5889 __isl_take isl_set
*range
)
5891 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5894 /* Return a newly allocated isl_map with given space and flags and
5895 * room for "n" basic maps.
5896 * Make sure that all cached information is cleared.
5898 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5901 struct isl_map
*map
;
5906 isl_die(space
->ctx
, isl_error_internal
,
5907 "negative number of basic maps", goto error
);
5908 map
= isl_calloc(space
->ctx
, struct isl_map
,
5909 sizeof(struct isl_map
) +
5910 (n
- 1) * sizeof(struct isl_basic_map
*));
5914 map
->ctx
= space
->ctx
;
5915 isl_ctx_ref(map
->ctx
);
5923 isl_space_free(space
);
5927 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5929 struct isl_basic_map
*bmap
;
5930 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5931 bmap
= isl_basic_map_set_to_empty(bmap
);
5935 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5937 struct isl_basic_set
*bset
;
5938 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5939 bset
= isl_basic_set_set_to_empty(bset
);
5943 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5945 struct isl_basic_map
*bmap
;
5946 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5947 bmap
= isl_basic_map_finalize(bmap
);
5951 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5953 struct isl_basic_set
*bset
;
5954 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5955 bset
= isl_basic_set_finalize(bset
);
5959 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5962 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5963 isl_basic_map
*bmap
;
5965 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5966 for (i
= 0; i
< total
; ++i
) {
5967 int k
= isl_basic_map_alloc_inequality(bmap
);
5970 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5971 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5975 isl_basic_map_free(bmap
);
5979 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5981 return isl_basic_map_nat_universe(dim
);
5984 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5986 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5989 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5991 return isl_map_nat_universe(dim
);
5994 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
5996 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5999 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6001 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6004 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6006 struct isl_map
*map
;
6009 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6010 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6014 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6016 struct isl_set
*set
;
6019 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6020 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6024 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6027 struct isl_map
*dup
;
6031 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6032 for (i
= 0; i
< map
->n
; ++i
)
6033 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6037 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6038 __isl_take isl_basic_map
*bmap
)
6042 if (isl_basic_map_plain_is_empty(bmap
)) {
6043 isl_basic_map_free(bmap
);
6046 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6047 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6048 map
->p
[map
->n
] = bmap
;
6050 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6056 isl_basic_map_free(bmap
);
6060 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6071 isl_ctx_deref(map
->ctx
);
6072 for (i
= 0; i
< map
->n
; ++i
)
6073 isl_basic_map_free(map
->p
[i
]);
6074 isl_space_free(map
->dim
);
6080 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
6081 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
6085 bmap
= isl_basic_map_cow(bmap
);
6086 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6087 j
= isl_basic_map_alloc_equality(bmap
);
6090 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6091 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6092 isl_int_set_si(bmap
->eq
[j
][0], value
);
6093 bmap
= isl_basic_map_simplify(bmap
);
6094 return isl_basic_map_finalize(bmap
);
6096 isl_basic_map_free(bmap
);
6100 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6101 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6105 bmap
= isl_basic_map_cow(bmap
);
6106 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6107 j
= isl_basic_map_alloc_equality(bmap
);
6110 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6111 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6112 isl_int_set(bmap
->eq
[j
][0], value
);
6113 bmap
= isl_basic_map_simplify(bmap
);
6114 return isl_basic_map_finalize(bmap
);
6116 isl_basic_map_free(bmap
);
6120 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6121 enum isl_dim_type type
, unsigned pos
, int value
)
6123 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6124 return isl_basic_map_free(bmap
);
6125 return isl_basic_map_fix_pos_si(bmap
,
6126 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6129 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6130 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6132 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6133 return isl_basic_map_free(bmap
);
6134 return isl_basic_map_fix_pos(bmap
,
6135 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6138 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6139 * to be equal to "v".
6141 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6142 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6146 if (!isl_val_is_int(v
))
6147 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6148 "expecting integer value", goto error
);
6149 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6151 pos
+= isl_basic_map_offset(bmap
, type
);
6152 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6156 isl_basic_map_free(bmap
);
6161 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6162 * to be equal to "v".
6164 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6165 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6167 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6170 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6171 enum isl_dim_type type
, unsigned pos
, int value
)
6173 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6177 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6178 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6180 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6184 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6185 unsigned input
, int value
)
6187 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6190 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6191 unsigned dim
, int value
)
6193 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6194 isl_dim_set
, dim
, value
));
6197 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
6199 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6206 isl_basic_map_free(map
->p
[i
]);
6207 if (i
!= map
->n
- 1) {
6208 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6209 map
->p
[i
] = map
->p
[map
->n
- 1];
6216 /* Perform "fn" on each basic map of "map", where we may not be holding
6217 * the only reference to "map".
6218 * In particular, "fn" should be a semantics preserving operation
6219 * that we want to apply to all copies of "map". We therefore need
6220 * to be careful not to modify "map" in a way that breaks "map"
6221 * in case anything goes wrong.
6223 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6224 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6226 struct isl_basic_map
*bmap
;
6232 for (i
= map
->n
- 1; i
>= 0; --i
) {
6233 bmap
= isl_basic_map_copy(map
->p
[i
]);
6237 isl_basic_map_free(map
->p
[i
]);
6239 if (remove_if_empty(map
, i
) < 0)
6249 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6250 enum isl_dim_type type
, unsigned pos
, int value
)
6254 map
= isl_map_cow(map
);
6258 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6259 for (i
= map
->n
- 1; i
>= 0; --i
) {
6260 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6261 if (remove_if_empty(map
, i
) < 0)
6264 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6271 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6272 enum isl_dim_type type
, unsigned pos
, int value
)
6274 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6277 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6278 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6282 map
= isl_map_cow(map
);
6286 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6287 for (i
= 0; i
< map
->n
; ++i
) {
6288 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6292 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6299 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6300 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6302 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6305 /* Fix the value of the variable at position "pos" of type "type" of "map"
6306 * to be equal to "v".
6308 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6309 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6313 map
= isl_map_cow(map
);
6317 if (!isl_val_is_int(v
))
6318 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6319 "expecting integer value", goto error
);
6320 if (pos
>= isl_map_dim(map
, type
))
6321 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6322 "index out of bounds", goto error
);
6323 for (i
= map
->n
- 1; i
>= 0; --i
) {
6324 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6326 if (remove_if_empty(map
, i
) < 0)
6329 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6338 /* Fix the value of the variable at position "pos" of type "type" of "set"
6339 * to be equal to "v".
6341 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6342 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6344 return isl_map_fix_val(set
, type
, pos
, v
);
6347 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6348 unsigned input
, int value
)
6350 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6353 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6355 return set_from_map(isl_map_fix_si(set_to_map(set
),
6356 isl_dim_set
, dim
, value
));
6359 static __isl_give isl_basic_map
*basic_map_bound_si(
6360 __isl_take isl_basic_map
*bmap
,
6361 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6365 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6366 return isl_basic_map_free(bmap
);
6367 pos
+= isl_basic_map_offset(bmap
, type
);
6368 bmap
= isl_basic_map_cow(bmap
);
6369 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6370 j
= isl_basic_map_alloc_inequality(bmap
);
6373 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6375 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6376 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6378 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6379 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6381 bmap
= isl_basic_map_simplify(bmap
);
6382 return isl_basic_map_finalize(bmap
);
6384 isl_basic_map_free(bmap
);
6388 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6389 __isl_take isl_basic_map
*bmap
,
6390 enum isl_dim_type type
, unsigned pos
, int value
)
6392 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6395 /* Constrain the values of the given dimension to be no greater than "value".
6397 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6398 __isl_take isl_basic_map
*bmap
,
6399 enum isl_dim_type type
, unsigned pos
, int value
)
6401 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6404 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6405 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6409 map
= isl_map_cow(map
);
6413 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6414 for (i
= 0; i
< map
->n
; ++i
) {
6415 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6416 type
, pos
, value
, upper
);
6420 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6427 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6428 enum isl_dim_type type
, unsigned pos
, int value
)
6430 return map_bound_si(map
, type
, pos
, value
, 0);
6433 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6434 enum isl_dim_type type
, unsigned pos
, int value
)
6436 return map_bound_si(map
, type
, pos
, value
, 1);
6439 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6440 enum isl_dim_type type
, unsigned pos
, int value
)
6442 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6446 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6447 enum isl_dim_type type
, unsigned pos
, int value
)
6449 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6452 /* Bound the given variable of "bmap" from below (or above is "upper"
6453 * is set) to "value".
6455 static __isl_give isl_basic_map
*basic_map_bound(
6456 __isl_take isl_basic_map
*bmap
,
6457 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6461 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6462 return isl_basic_map_free(bmap
);
6463 pos
+= isl_basic_map_offset(bmap
, type
);
6464 bmap
= isl_basic_map_cow(bmap
);
6465 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6466 j
= isl_basic_map_alloc_inequality(bmap
);
6469 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6471 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6472 isl_int_set(bmap
->ineq
[j
][0], value
);
6474 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6475 isl_int_neg(bmap
->ineq
[j
][0], value
);
6477 bmap
= isl_basic_map_simplify(bmap
);
6478 return isl_basic_map_finalize(bmap
);
6480 isl_basic_map_free(bmap
);
6484 /* Bound the given variable of "map" from below (or above is "upper"
6485 * is set) to "value".
6487 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6488 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6492 map
= isl_map_cow(map
);
6496 if (pos
>= isl_map_dim(map
, type
))
6497 isl_die(map
->ctx
, isl_error_invalid
,
6498 "index out of bounds", goto error
);
6499 for (i
= map
->n
- 1; i
>= 0; --i
) {
6500 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6501 if (remove_if_empty(map
, i
) < 0)
6504 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6511 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6512 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6514 return map_bound(map
, type
, pos
, value
, 0);
6517 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6518 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6520 return map_bound(map
, type
, pos
, value
, 1);
6523 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6524 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6526 return isl_map_lower_bound(set
, type
, pos
, value
);
6529 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6530 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6532 return isl_map_upper_bound(set
, type
, pos
, value
);
6535 /* Force the values of the variable at position "pos" of type "type" of "set"
6536 * to be no smaller than "value".
6538 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6539 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6543 if (!isl_val_is_int(value
))
6544 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6545 "expecting integer value", goto error
);
6546 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6547 isl_val_free(value
);
6550 isl_val_free(value
);
6555 /* Force the values of the variable at position "pos" of type "type" of "set"
6556 * to be no greater than "value".
6558 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6559 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6563 if (!isl_val_is_int(value
))
6564 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6565 "expecting integer value", goto error
);
6566 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6567 isl_val_free(value
);
6570 isl_val_free(value
);
6575 /* Bound the given variable of "bset" from below (or above is "upper"
6576 * is set) to "value".
6578 static __isl_give isl_basic_set
*isl_basic_set_bound(
6579 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6580 isl_int value
, int upper
)
6582 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6583 type
, pos
, value
, upper
));
6586 /* Bound the given variable of "bset" from below (or above is "upper"
6587 * is set) to "value".
6589 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6590 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6591 __isl_take isl_val
*value
, int upper
)
6595 if (!isl_val_is_int(value
))
6596 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6597 "expecting integer value", goto error
);
6598 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6599 isl_val_free(value
);
6602 isl_val_free(value
);
6603 isl_basic_set_free(bset
);
6607 /* Bound the given variable of "bset" from below to "value".
6609 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6610 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6611 __isl_take isl_val
*value
)
6613 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6616 /* Bound the given variable of "bset" from above to "value".
6618 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6619 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6620 __isl_take isl_val
*value
)
6622 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6625 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6629 map
= isl_map_cow(map
);
6633 map
->dim
= isl_space_reverse(map
->dim
);
6636 for (i
= 0; i
< map
->n
; ++i
) {
6637 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6641 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6649 #define TYPE isl_pw_multi_aff
6651 #define SUFFIX _pw_multi_aff
6653 #define EMPTY isl_pw_multi_aff_empty
6655 #define ADD isl_pw_multi_aff_union_add
6656 #include "isl_map_lexopt_templ.c"
6658 /* Given a map "map", compute the lexicographically minimal
6659 * (or maximal) image element for each domain element in dom,
6660 * in the form of an isl_pw_multi_aff.
6661 * If "empty" is not NULL, then set *empty to those elements in dom that
6662 * do not have an image element.
6663 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6664 * should be computed over the domain of "map". "empty" is also NULL
6667 * We first compute the lexicographically minimal or maximal element
6668 * in the first basic map. This results in a partial solution "res"
6669 * and a subset "todo" of dom that still need to be handled.
6670 * We then consider each of the remaining maps in "map" and successively
6671 * update both "res" and "todo".
6672 * If "empty" is NULL, then the todo sets are not needed and therefore
6673 * also not computed.
6675 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6676 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6677 __isl_give isl_set
**empty
, unsigned flags
)
6681 isl_pw_multi_aff
*res
;
6684 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6685 if (!map
|| (!full
&& !dom
))
6688 if (isl_map_plain_is_empty(map
)) {
6693 return isl_pw_multi_aff_from_map(map
);
6696 res
= basic_map_partial_lexopt_pw_multi_aff(
6697 isl_basic_map_copy(map
->p
[0]),
6698 isl_set_copy(dom
), empty
, flags
);
6702 for (i
= 1; i
< map
->n
; ++i
) {
6703 isl_pw_multi_aff
*res_i
;
6705 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6706 isl_basic_map_copy(map
->p
[i
]),
6707 isl_set_copy(dom
), empty
, flags
);
6709 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6710 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6712 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6715 todo
= isl_set_intersect(todo
, *empty
);
6734 #define TYPE isl_map
6738 #define EMPTY isl_map_empty
6740 #define ADD isl_map_union_disjoint
6741 #include "isl_map_lexopt_templ.c"
6743 /* Given a map "map", compute the lexicographically minimal
6744 * (or maximal) image element for each domain element in "dom",
6745 * in the form of an isl_map.
6746 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6747 * do not have an image element.
6748 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6749 * should be computed over the domain of "map". "empty" is also NULL
6752 * If the input consists of more than one disjunct, then first
6753 * compute the desired result in the form of an isl_pw_multi_aff and
6754 * then convert that into an isl_map.
6756 * This function used to have an explicit implementation in terms
6757 * of isl_maps, but it would continually intersect the domains of
6758 * partial results with the complement of the domain of the next
6759 * partial solution, potentially leading to an explosion in the number
6760 * of disjuncts if there are several disjuncts in the input.
6761 * An even earlier implementation of this function would look for
6762 * better results in the domain of the partial result and for extra
6763 * results in the complement of this domain, which would lead to
6764 * even more splintering.
6766 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6767 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6768 __isl_give isl_set
**empty
, unsigned flags
)
6771 struct isl_map
*res
;
6772 isl_pw_multi_aff
*pma
;
6774 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6775 if (!map
|| (!full
&& !dom
))
6778 if (isl_map_plain_is_empty(map
)) {
6787 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6793 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6795 return isl_map_from_pw_multi_aff(pma
);
6804 __isl_give isl_map
*isl_map_partial_lexmax(
6805 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6806 __isl_give isl_set
**empty
)
6808 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6811 __isl_give isl_map
*isl_map_partial_lexmin(
6812 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6813 __isl_give isl_set
**empty
)
6815 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6818 __isl_give isl_set
*isl_set_partial_lexmin(
6819 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6820 __isl_give isl_set
**empty
)
6822 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6826 __isl_give isl_set
*isl_set_partial_lexmax(
6827 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6828 __isl_give isl_set
**empty
)
6830 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6834 /* Compute the lexicographic minimum (or maximum if "flags" includes
6835 * ISL_OPT_MAX) of "bset" over its parametric domain.
6837 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6840 return isl_basic_map_lexopt(bset
, flags
);
6843 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6845 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6848 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6850 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6853 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6855 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6858 /* Compute the lexicographic minimum of "bset" over its parametric domain
6859 * for the purpose of quantifier elimination.
6860 * That is, find an explicit representation for all the existentially
6861 * quantified variables in "bset" by computing their lexicographic
6864 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6865 __isl_take isl_basic_set
*bset
)
6867 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6870 /* Given a basic map with one output dimension, compute the minimum or
6871 * maximum of that dimension as an isl_pw_aff.
6873 * Compute the optimum as a lexicographic optimum over the single
6874 * output dimension and extract the single isl_pw_aff from the result.
6876 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6879 isl_pw_multi_aff
*pma
;
6882 bmap
= isl_basic_map_copy(bmap
);
6883 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6884 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6885 isl_pw_multi_aff_free(pma
);
6890 /* Compute the minimum or maximum of the given output dimension
6891 * as a function of the parameters and the input dimensions,
6892 * but independently of the other output dimensions.
6894 * We first project out the other output dimension and then compute
6895 * the "lexicographic" maximum in each basic map, combining the results
6896 * using isl_pw_aff_union_max.
6898 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6905 n_out
= isl_map_dim(map
, isl_dim_out
);
6906 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6907 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6912 isl_space
*dim
= isl_map_get_space(map
);
6914 return isl_pw_aff_empty(dim
);
6917 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6918 for (i
= 1; i
< map
->n
; ++i
) {
6919 isl_pw_aff
*pwaff_i
;
6921 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6922 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6930 /* Compute the minimum of the given output dimension as a function of the
6931 * parameters and input dimensions, but independently of
6932 * the other output dimensions.
6934 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6936 return map_dim_opt(map
, pos
, 0);
6939 /* Compute the maximum of the given output dimension as a function of the
6940 * parameters and input dimensions, but independently of
6941 * the other output dimensions.
6943 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6945 return map_dim_opt(map
, pos
, 1);
6948 /* Compute the minimum or maximum of the given set dimension
6949 * as a function of the parameters,
6950 * but independently of the other set dimensions.
6952 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6955 return map_dim_opt(set
, pos
, max
);
6958 /* Compute the maximum of the given set dimension as a function of the
6959 * parameters, but independently of the other set dimensions.
6961 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6963 return set_dim_opt(set
, pos
, 1);
6966 /* Compute the minimum of the given set dimension as a function of the
6967 * parameters, but independently of the other set dimensions.
6969 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6971 return set_dim_opt(set
, pos
, 0);
6974 /* Apply a preimage specified by "mat" on the parameters of "bset".
6975 * bset is assumed to have only parameters and divs.
6977 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6978 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6985 bset
->dim
= isl_space_cow(bset
->dim
);
6989 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6991 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6993 bset
->dim
->nparam
= 0;
6994 bset
->dim
->n_out
= nparam
;
6995 bset
= isl_basic_set_preimage(bset
, mat
);
6997 bset
->dim
->nparam
= bset
->dim
->n_out
;
6998 bset
->dim
->n_out
= 0;
7003 isl_basic_set_free(bset
);
7007 /* Apply a preimage specified by "mat" on the parameters of "set".
7008 * set is assumed to have only parameters and divs.
7010 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7011 __isl_take isl_mat
*mat
)
7019 nparam
= isl_set_dim(set
, isl_dim_param
);
7021 if (mat
->n_row
!= 1 + nparam
)
7022 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7023 "unexpected number of rows", goto error
);
7025 space
= isl_set_get_space(set
);
7026 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7027 isl_dim_param
, 0, nparam
);
7028 set
= isl_set_reset_space(set
, space
);
7029 set
= isl_set_preimage(set
, mat
);
7030 nparam
= isl_set_dim(set
, isl_dim_out
);
7031 space
= isl_set_get_space(set
);
7032 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7033 isl_dim_out
, 0, nparam
);
7034 set
= isl_set_reset_space(set
, space
);
7042 /* Intersect the basic set "bset" with the affine space specified by the
7043 * equalities in "eq".
7045 static __isl_give isl_basic_set
*basic_set_append_equalities(
7046 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7054 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7059 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7060 for (i
= 0; i
< eq
->n_row
; ++i
) {
7061 k
= isl_basic_set_alloc_equality(bset
);
7064 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7065 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7069 bset
= isl_basic_set_gauss(bset
, NULL
);
7070 bset
= isl_basic_set_finalize(bset
);
7075 isl_basic_set_free(bset
);
7079 /* Intersect the set "set" with the affine space specified by the
7080 * equalities in "eq".
7082 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7090 for (i
= 0; i
< set
->n
; ++i
) {
7091 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7104 /* Given a basic set "bset" that only involves parameters and existentially
7105 * quantified variables, return the index of the first equality
7106 * that only involves parameters. If there is no such equality then
7107 * return bset->n_eq.
7109 * This function assumes that isl_basic_set_gauss has been called on "bset".
7111 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7114 unsigned nparam
, n_div
;
7119 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7120 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7122 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7123 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7130 /* Compute an explicit representation for the existentially quantified
7131 * variables in "bset" by computing the "minimal value" of the set
7132 * variables. Since there are no set variables, the computation of
7133 * the minimal value essentially computes an explicit representation
7134 * of the non-empty part(s) of "bset".
7136 * The input only involves parameters and existentially quantified variables.
7137 * All equalities among parameters have been removed.
7139 * Since the existentially quantified variables in the result are in general
7140 * going to be different from those in the input, we first replace
7141 * them by the minimal number of variables based on their equalities.
7142 * This should simplify the parametric integer programming.
7144 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7146 isl_morph
*morph1
, *morph2
;
7152 if (bset
->n_eq
== 0)
7153 return isl_basic_set_lexmin_compute_divs(bset
);
7155 morph1
= isl_basic_set_parameter_compression(bset
);
7156 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7157 bset
= isl_basic_set_lift(bset
);
7158 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7159 bset
= isl_morph_basic_set(morph2
, bset
);
7160 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7161 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7163 set
= isl_basic_set_lexmin_compute_divs(bset
);
7165 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7170 /* Project the given basic set onto its parameter domain, possibly introducing
7171 * new, explicit, existential variables in the constraints.
7172 * The input has parameters and (possibly implicit) existential variables.
7173 * The output has the same parameters, but only
7174 * explicit existentially quantified variables.
7176 * The actual projection is performed by pip, but pip doesn't seem
7177 * to like equalities very much, so we first remove the equalities
7178 * among the parameters by performing a variable compression on
7179 * the parameters. Afterward, an inverse transformation is performed
7180 * and the equalities among the parameters are inserted back in.
7182 * The variable compression on the parameters may uncover additional
7183 * equalities that were only implicit before. We therefore check
7184 * if there are any new parameter equalities in the result and
7185 * if so recurse. The removal of parameter equalities is required
7186 * for the parameter compression performed by base_compute_divs.
7188 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7192 struct isl_mat
*T
, *T2
;
7193 struct isl_set
*set
;
7196 bset
= isl_basic_set_cow(bset
);
7200 if (bset
->n_eq
== 0)
7201 return base_compute_divs(bset
);
7203 bset
= isl_basic_set_gauss(bset
, NULL
);
7206 if (isl_basic_set_plain_is_empty(bset
))
7207 return isl_set_from_basic_set(bset
);
7209 i
= first_parameter_equality(bset
);
7210 if (i
== bset
->n_eq
)
7211 return base_compute_divs(bset
);
7213 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7214 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7216 eq
= isl_mat_cow(eq
);
7217 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7218 if (T
&& T
->n_col
== 0) {
7222 bset
= isl_basic_set_set_to_empty(bset
);
7223 return isl_set_from_basic_set(bset
);
7225 bset
= basic_set_parameter_preimage(bset
, T
);
7227 i
= first_parameter_equality(bset
);
7230 else if (i
== bset
->n_eq
)
7231 set
= base_compute_divs(bset
);
7233 set
= parameter_compute_divs(bset
);
7234 set
= set_parameter_preimage(set
, T2
);
7235 set
= set_append_equalities(set
, eq
);
7239 /* Insert the divs from "ls" before those of "bmap".
7241 * The number of columns is not changed, which means that the last
7242 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7243 * The caller is responsible for removing the same number of dimensions
7244 * from the space of "bmap".
7246 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7247 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7253 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7257 old_n_div
= bmap
->n_div
;
7258 bmap
= insert_div_rows(bmap
, n_div
);
7262 for (i
= 0; i
< n_div
; ++i
) {
7263 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7264 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7270 /* Replace the space of "bmap" by the space and divs of "ls".
7272 * If "ls" has any divs, then we simplify the result since we may
7273 * have discovered some additional equalities that could simplify
7274 * the div expressions.
7276 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7277 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7281 bmap
= isl_basic_map_cow(bmap
);
7285 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7286 bmap
= insert_divs_from_local_space(bmap
, ls
);
7290 isl_space_free(bmap
->dim
);
7291 bmap
->dim
= isl_local_space_get_space(ls
);
7295 isl_local_space_free(ls
);
7297 bmap
= isl_basic_map_simplify(bmap
);
7298 bmap
= isl_basic_map_finalize(bmap
);
7301 isl_basic_map_free(bmap
);
7302 isl_local_space_free(ls
);
7306 /* Replace the space of "map" by the space and divs of "ls".
7308 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7309 __isl_take isl_local_space
*ls
)
7313 map
= isl_map_cow(map
);
7317 for (i
= 0; i
< map
->n
; ++i
) {
7318 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7319 isl_local_space_copy(ls
));
7323 isl_space_free(map
->dim
);
7324 map
->dim
= isl_local_space_get_space(ls
);
7328 isl_local_space_free(ls
);
7331 isl_local_space_free(ls
);
7336 /* Compute an explicit representation for the existentially
7337 * quantified variables for which do not know any explicit representation yet.
7339 * We first sort the existentially quantified variables so that the
7340 * existentially quantified variables for which we already have an explicit
7341 * representation are placed before those for which we do not.
7342 * The input dimensions, the output dimensions and the existentially
7343 * quantified variables for which we already have an explicit
7344 * representation are then turned into parameters.
7345 * compute_divs returns a map with the same parameters and
7346 * no input or output dimensions and the dimension specification
7347 * is reset to that of the input, including the existentially quantified
7348 * variables for which we already had an explicit representation.
7350 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7352 struct isl_basic_set
*bset
;
7353 struct isl_set
*set
;
7354 struct isl_map
*map
;
7356 isl_local_space
*ls
;
7363 bmap
= isl_basic_map_sort_divs(bmap
);
7364 bmap
= isl_basic_map_cow(bmap
);
7368 n_known
= isl_basic_map_first_unknown_div(bmap
);
7370 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7372 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7373 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7374 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7375 dim
= isl_space_set_alloc(bmap
->ctx
,
7376 nparam
+ n_in
+ n_out
+ n_known
, 0);
7380 ls
= isl_basic_map_get_local_space(bmap
);
7381 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7382 n_known
, bmap
->n_div
- n_known
);
7384 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7385 swap_div(bmap
, i
- n_known
, i
);
7386 bmap
->n_div
-= n_known
;
7387 bmap
->extra
-= n_known
;
7389 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7390 bset
= bset_from_bmap(bmap
);
7392 set
= parameter_compute_divs(bset
);
7393 map
= set_to_map(set
);
7394 map
= replace_space_by_local_space(map
, ls
);
7398 isl_basic_map_free(bmap
);
7402 /* Remove the explicit representation of local variable "div",
7405 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7406 __isl_take isl_basic_map
*bmap
, int div
)
7410 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7412 return isl_basic_map_free(bmap
);
7416 bmap
= isl_basic_map_cow(bmap
);
7419 isl_int_set_si(bmap
->div
[div
][0], 0);
7423 /* Is local variable "div" of "bmap" marked as not having an explicit
7425 * Note that even if "div" is not marked in this way and therefore
7426 * has an explicit representation, this representation may still
7427 * depend (indirectly) on other local variables that do not
7428 * have an explicit representation.
7430 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7433 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7434 return isl_bool_error
;
7435 return isl_int_is_zero(bmap
->div
[div
][0]);
7438 /* Return the position of the first local variable that does not
7439 * have an explicit representation.
7440 * Return the total number of local variables if they all have
7441 * an explicit representation.
7442 * Return -1 on error.
7444 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7451 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7452 if (!isl_basic_map_div_is_known(bmap
, i
))
7458 /* Return the position of the first local variable that does not
7459 * have an explicit representation.
7460 * Return the total number of local variables if they all have
7461 * an explicit representation.
7462 * Return -1 on error.
7464 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7466 return isl_basic_map_first_unknown_div(bset
);
7469 /* Does "bmap" have an explicit representation for all local variables?
7471 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7475 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7476 first
= isl_basic_map_first_unknown_div(bmap
);
7478 return isl_bool_error
;
7482 /* Do all basic maps in "map" have an explicit representation
7483 * for all local variables?
7485 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7490 return isl_bool_error
;
7492 for (i
= 0; i
< map
->n
; ++i
) {
7493 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7498 return isl_bool_true
;
7501 /* If bmap contains any unknown divs, then compute explicit
7502 * expressions for them. However, this computation may be
7503 * quite expensive, so first try to remove divs that aren't
7506 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7509 struct isl_map
*map
;
7511 known
= isl_basic_map_divs_known(bmap
);
7515 return isl_map_from_basic_map(bmap
);
7517 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7519 known
= isl_basic_map_divs_known(bmap
);
7523 return isl_map_from_basic_map(bmap
);
7525 map
= compute_divs(bmap
);
7528 isl_basic_map_free(bmap
);
7532 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7536 struct isl_map
*res
;
7543 known
= isl_map_divs_known(map
);
7551 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7552 for (i
= 1 ; i
< map
->n
; ++i
) {
7554 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7555 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7556 res
= isl_map_union_disjoint(res
, r2
);
7558 res
= isl_map_union(res
, r2
);
7565 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7567 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7570 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7572 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7575 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7578 struct isl_set
*set
;
7583 map
= isl_map_cow(map
);
7587 set
= set_from_map(map
);
7588 set
->dim
= isl_space_domain(set
->dim
);
7591 for (i
= 0; i
< map
->n
; ++i
) {
7592 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7596 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7597 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7604 /* Return the union of "map1" and "map2", where we assume for now that
7605 * "map1" and "map2" are disjoint. Note that the basic maps inside
7606 * "map1" or "map2" may not be disjoint from each other.
7607 * Also note that this function is also called from isl_map_union,
7608 * which takes care of handling the situation where "map1" and "map2"
7609 * may not be disjoint.
7611 * If one of the inputs is empty, we can simply return the other input.
7612 * Similarly, if one of the inputs is universal, then it is equal to the union.
7614 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7615 __isl_take isl_map
*map2
)
7619 struct isl_map
*map
= NULL
;
7625 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7626 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7627 "spaces don't match", goto error
);
7638 is_universe
= isl_map_plain_is_universe(map1
);
7639 if (is_universe
< 0)
7646 is_universe
= isl_map_plain_is_universe(map2
);
7647 if (is_universe
< 0)
7654 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7655 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7656 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7658 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7659 map1
->n
+ map2
->n
, flags
);
7662 for (i
= 0; i
< map1
->n
; ++i
) {
7663 map
= isl_map_add_basic_map(map
,
7664 isl_basic_map_copy(map1
->p
[i
]));
7668 for (i
= 0; i
< map2
->n
; ++i
) {
7669 map
= isl_map_add_basic_map(map
,
7670 isl_basic_map_copy(map2
->p
[i
]));
7684 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7685 * guaranteed to be disjoint by the caller.
7687 * Note that this functions is called from within isl_map_make_disjoint,
7688 * so we have to be careful not to touch the constraints of the inputs
7691 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7692 __isl_take isl_map
*map2
)
7694 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7697 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7698 * not be disjoint. The parameters are assumed to have been aligned.
7700 * We currently simply call map_union_disjoint, the internal operation
7701 * of which does not really depend on the inputs being disjoint.
7702 * If the result contains more than one basic map, then we clear
7703 * the disjoint flag since the result may contain basic maps from
7704 * both inputs and these are not guaranteed to be disjoint.
7706 * As a special case, if "map1" and "map2" are obviously equal,
7707 * then we simply return "map1".
7709 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7710 __isl_take isl_map
*map2
)
7717 equal
= isl_map_plain_is_equal(map1
, map2
);
7725 map1
= map_union_disjoint(map1
, map2
);
7729 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7737 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7740 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7741 __isl_take isl_map
*map2
)
7743 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7746 __isl_give isl_set
*isl_set_union_disjoint(
7747 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7749 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7753 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7755 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7758 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7761 * "map" and "set" are assumed to be compatible and non-NULL.
7763 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7764 __isl_take isl_set
*set
,
7765 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7766 __isl_take isl_basic_set
*bset
))
7769 struct isl_map
*result
;
7772 if (isl_set_plain_is_universe(set
)) {
7777 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7778 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7779 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7781 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7782 map
->n
* set
->n
, flags
);
7783 for (i
= 0; result
&& i
< map
->n
; ++i
)
7784 for (j
= 0; j
< set
->n
; ++j
) {
7785 result
= isl_map_add_basic_map(result
,
7786 fn(isl_basic_map_copy(map
->p
[i
]),
7787 isl_basic_set_copy(set
->p
[j
])));
7797 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7798 __isl_take isl_set
*set
)
7802 ok
= isl_map_compatible_range(map
, set
);
7806 isl_die(set
->ctx
, isl_error_invalid
,
7807 "incompatible spaces", goto error
);
7809 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7816 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7817 __isl_take isl_set
*set
)
7819 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7822 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7823 __isl_take isl_set
*set
)
7827 ok
= isl_map_compatible_domain(map
, set
);
7831 isl_die(set
->ctx
, isl_error_invalid
,
7832 "incompatible spaces", goto error
);
7834 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7841 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7842 __isl_take isl_set
*set
)
7844 return isl_map_align_params_map_map_and(map
, set
,
7845 &map_intersect_domain
);
7848 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7849 * in the space B -> C, return the intersection.
7850 * The parameters are assumed to have been aligned.
7852 * The map "factor" is first extended to a map living in the space
7853 * [A -> B] -> C and then a regular intersection is computed.
7855 static __isl_give isl_map
*map_intersect_domain_factor_range(
7856 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7859 isl_map
*ext_factor
;
7861 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7862 ext_factor
= isl_map_universe(space
);
7863 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7864 return map_intersect(map
, ext_factor
);
7867 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7868 * in the space B -> C, return the intersection.
7870 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7871 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7873 return isl_map_align_params_map_map_and(map
, factor
,
7874 &map_intersect_domain_factor_range
);
7877 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7878 * in the space A -> C, return the intersection.
7880 * The map "factor" is first extended to a map living in the space
7881 * A -> [B -> C] and then a regular intersection is computed.
7883 static __isl_give isl_map
*map_intersect_range_factor_range(
7884 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7887 isl_map
*ext_factor
;
7889 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7890 ext_factor
= isl_map_universe(space
);
7891 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7892 return isl_map_intersect(map
, ext_factor
);
7895 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7896 * in the space A -> C, return the intersection.
7898 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7899 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7901 return isl_map_align_params_map_map_and(map
, factor
,
7902 &map_intersect_range_factor_range
);
7905 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7906 __isl_take isl_map
*map2
)
7910 map1
= isl_map_reverse(map1
);
7911 map1
= isl_map_apply_range(map1
, map2
);
7912 return isl_map_reverse(map1
);
7919 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7920 __isl_take isl_map
*map2
)
7922 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7925 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7926 __isl_take isl_map
*map2
)
7928 isl_space
*dim_result
;
7929 struct isl_map
*result
;
7935 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7936 isl_space_copy(map2
->dim
));
7938 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7941 for (i
= 0; i
< map1
->n
; ++i
)
7942 for (j
= 0; j
< map2
->n
; ++j
) {
7943 result
= isl_map_add_basic_map(result
,
7944 isl_basic_map_apply_range(
7945 isl_basic_map_copy(map1
->p
[i
]),
7946 isl_basic_map_copy(map2
->p
[j
])));
7952 if (result
&& result
->n
<= 1)
7953 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7961 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7962 __isl_take isl_map
*map2
)
7964 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7968 * returns range - domain
7970 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
7972 isl_space
*target_space
;
7973 struct isl_basic_set
*bset
;
7980 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7981 bmap
->dim
, isl_dim_out
),
7983 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7984 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7985 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7986 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7987 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7988 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7989 for (i
= 0; i
< dim
; ++i
) {
7990 int j
= isl_basic_map_alloc_equality(bmap
);
7992 bmap
= isl_basic_map_free(bmap
);
7995 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7996 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7997 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7998 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8000 bset
= isl_basic_map_domain(bmap
);
8001 bset
= isl_basic_set_reset_space(bset
, target_space
);
8004 isl_basic_map_free(bmap
);
8009 * returns range - domain
8011 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8015 struct isl_set
*result
;
8020 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8021 map
->dim
, isl_dim_out
),
8023 dim
= isl_map_get_space(map
);
8024 dim
= isl_space_domain(dim
);
8025 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8028 for (i
= 0; i
< map
->n
; ++i
)
8029 result
= isl_set_add_basic_set(result
,
8030 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8039 * returns [domain -> range] -> range - domain
8041 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8042 __isl_take isl_basic_map
*bmap
)
8046 isl_basic_map
*domain
;
8050 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8051 bmap
->dim
, isl_dim_out
))
8052 isl_die(bmap
->ctx
, isl_error_invalid
,
8053 "domain and range don't match", goto error
);
8055 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8056 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8058 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
8059 domain
= isl_basic_map_universe(dim
);
8061 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8062 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8063 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8065 total
= isl_basic_map_total_dim(bmap
);
8067 for (i
= 0; i
< n
; ++i
) {
8068 k
= isl_basic_map_alloc_equality(bmap
);
8071 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8072 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8073 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8074 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8077 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8078 return isl_basic_map_finalize(bmap
);
8080 isl_basic_map_free(bmap
);
8085 * returns [domain -> range] -> range - domain
8087 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8090 isl_space
*domain_dim
;
8095 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8096 map
->dim
, isl_dim_out
))
8097 isl_die(map
->ctx
, isl_error_invalid
,
8098 "domain and range don't match", goto error
);
8100 map
= isl_map_cow(map
);
8104 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
8105 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8106 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
8109 for (i
= 0; i
< map
->n
; ++i
) {
8110 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8114 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8121 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
8123 struct isl_basic_map
*bmap
;
8131 nparam
= dims
->nparam
;
8133 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
8137 for (i
= 0; i
< dim
; ++i
) {
8138 int j
= isl_basic_map_alloc_equality(bmap
);
8141 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8142 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8143 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
8145 return isl_basic_map_finalize(bmap
);
8147 isl_basic_map_free(bmap
);
8151 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
8155 if (dim
->n_in
!= dim
->n_out
)
8156 isl_die(dim
->ctx
, isl_error_invalid
,
8157 "number of input and output dimensions needs to be "
8158 "the same", goto error
);
8159 return basic_map_identity(dim
);
8161 isl_space_free(dim
);
8165 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8167 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8170 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8172 isl_space
*dim
= isl_set_get_space(set
);
8174 id
= isl_map_identity(isl_space_map_from_set(dim
));
8175 return isl_map_intersect_range(id
, set
);
8178 /* Construct a basic set with all set dimensions having only non-negative
8181 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8182 __isl_take isl_space
*space
)
8187 struct isl_basic_set
*bset
;
8191 nparam
= space
->nparam
;
8193 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8196 for (i
= 0; i
< dim
; ++i
) {
8197 int k
= isl_basic_set_alloc_inequality(bset
);
8200 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8201 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8205 isl_basic_set_free(bset
);
8209 /* Construct the half-space x_pos >= 0.
8211 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
8215 isl_basic_set
*nonneg
;
8217 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8218 k
= isl_basic_set_alloc_inequality(nonneg
);
8221 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8222 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8224 return isl_basic_set_finalize(nonneg
);
8226 isl_basic_set_free(nonneg
);
8230 /* Construct the half-space x_pos <= -1.
8232 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
8237 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8238 k
= isl_basic_set_alloc_inequality(neg
);
8241 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8242 isl_int_set_si(neg
->ineq
[k
][0], -1);
8243 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8245 return isl_basic_set_finalize(neg
);
8247 isl_basic_set_free(neg
);
8251 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8252 enum isl_dim_type type
, unsigned first
, unsigned n
)
8256 isl_basic_set
*nonneg
;
8264 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8266 offset
= pos(set
->dim
, type
);
8267 for (i
= 0; i
< n
; ++i
) {
8268 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8269 offset
+ first
+ i
);
8270 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8272 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8281 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8283 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8289 return isl_stat_error
;
8290 if (isl_set_plain_is_empty(set
)) {
8295 return fn(set
, signs
, user
);
8298 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8300 half
= isl_set_intersect(half
, isl_set_copy(set
));
8301 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8305 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8307 half
= isl_set_intersect(half
, set
);
8308 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8311 return isl_stat_error
;
8314 /* Call "fn" on the intersections of "set" with each of the orthants
8315 * (except for obviously empty intersections). The orthant is identified
8316 * by the signs array, with each entry having value 1 or -1 according
8317 * to the sign of the corresponding variable.
8319 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8320 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8329 return isl_stat_error
;
8330 if (isl_set_plain_is_empty(set
))
8333 nparam
= isl_set_dim(set
, isl_dim_param
);
8334 nvar
= isl_set_dim(set
, isl_dim_set
);
8336 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8338 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8346 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8348 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8351 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8352 __isl_keep isl_basic_map
*bmap2
)
8355 struct isl_map
*map1
;
8356 struct isl_map
*map2
;
8358 if (!bmap1
|| !bmap2
)
8359 return isl_bool_error
;
8361 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8362 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8364 is_subset
= isl_map_is_subset(map1
, map2
);
8372 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8373 __isl_keep isl_basic_set
*bset2
)
8375 return isl_basic_map_is_subset(bset1
, bset2
);
8378 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8379 __isl_keep isl_basic_map
*bmap2
)
8383 if (!bmap1
|| !bmap2
)
8384 return isl_bool_error
;
8385 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8386 if (is_subset
!= isl_bool_true
)
8388 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8392 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8393 __isl_keep isl_basic_set
*bset2
)
8395 return isl_basic_map_is_equal(
8396 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8399 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8405 return isl_bool_error
;
8406 for (i
= 0; i
< map
->n
; ++i
) {
8407 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8409 return isl_bool_error
;
8411 return isl_bool_false
;
8413 return isl_bool_true
;
8416 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8418 return map
? map
->n
== 0 : isl_bool_error
;
8421 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8423 return set
? set
->n
== 0 : isl_bool_error
;
8426 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8428 return isl_map_is_empty(set_to_map(set
));
8431 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8432 __isl_keep isl_map
*map2
)
8435 return isl_bool_error
;
8437 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8440 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8441 __isl_keep isl_set
*set2
)
8444 return isl_bool_error
;
8446 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8449 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8454 return isl_bool_error
;
8455 is_subset
= isl_map_is_subset(map1
, map2
);
8456 if (is_subset
!= isl_bool_true
)
8458 is_subset
= isl_map_is_subset(map2
, map1
);
8462 /* Is "map1" equal to "map2"?
8464 * First check if they are obviously equal.
8465 * If not, then perform a more detailed analysis.
8467 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8471 equal
= isl_map_plain_is_equal(map1
, map2
);
8472 if (equal
< 0 || equal
)
8474 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8477 isl_bool
isl_basic_map_is_strict_subset(
8478 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8482 if (!bmap1
|| !bmap2
)
8483 return isl_bool_error
;
8484 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8485 if (is_subset
!= isl_bool_true
)
8487 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8488 if (is_subset
== isl_bool_error
)
8493 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8494 __isl_keep isl_map
*map2
)
8499 return isl_bool_error
;
8500 is_subset
= isl_map_is_subset(map1
, map2
);
8501 if (is_subset
!= isl_bool_true
)
8503 is_subset
= isl_map_is_subset(map2
, map1
);
8504 if (is_subset
== isl_bool_error
)
8509 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8510 __isl_keep isl_set
*set2
)
8512 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8515 /* Is "bmap" obviously equal to the universe with the same space?
8517 * That is, does it not have any constraints?
8519 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8522 return isl_bool_error
;
8523 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8526 /* Is "bset" obviously equal to the universe with the same space?
8528 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8530 return isl_basic_map_plain_is_universe(bset
);
8533 /* If "c" does not involve any existentially quantified variables,
8534 * then set *univ to false and abort
8536 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8538 isl_bool
*univ
= user
;
8541 n
= isl_constraint_dim(c
, isl_dim_div
);
8542 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8543 isl_constraint_free(c
);
8544 if (*univ
< 0 || !*univ
)
8545 return isl_stat_error
;
8549 /* Is "bmap" equal to the universe with the same space?
8551 * First check if it is obviously equal to the universe.
8552 * If not and if there are any constraints not involving
8553 * existentially quantified variables, then it is certainly
8554 * not equal to the universe.
8555 * Otherwise, check if the universe is a subset of "bmap".
8557 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8560 isl_basic_map
*test
;
8562 univ
= isl_basic_map_plain_is_universe(bmap
);
8563 if (univ
< 0 || univ
)
8565 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8566 return isl_bool_false
;
8567 univ
= isl_bool_true
;
8568 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8570 return isl_bool_error
;
8571 if (univ
< 0 || !univ
)
8573 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8574 univ
= isl_basic_map_is_subset(test
, bmap
);
8575 isl_basic_map_free(test
);
8579 /* Is "bset" equal to the universe with the same space?
8581 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8583 return isl_basic_map_is_universe(bset
);
8586 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8591 return isl_bool_error
;
8593 for (i
= 0; i
< map
->n
; ++i
) {
8594 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8599 return isl_bool_false
;
8602 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8604 return isl_map_plain_is_universe(set_to_map(set
));
8607 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8609 struct isl_basic_set
*bset
= NULL
;
8610 struct isl_vec
*sample
= NULL
;
8611 isl_bool empty
, non_empty
;
8614 return isl_bool_error
;
8616 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8617 return isl_bool_true
;
8619 if (isl_basic_map_plain_is_universe(bmap
))
8620 return isl_bool_false
;
8622 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8623 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8624 copy
= isl_basic_map_remove_redundancies(copy
);
8625 empty
= isl_basic_map_plain_is_empty(copy
);
8626 isl_basic_map_free(copy
);
8630 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8632 return isl_bool_error
;
8634 return isl_bool_false
;
8635 isl_vec_free(bmap
->sample
);
8636 bmap
->sample
= NULL
;
8637 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8639 return isl_bool_error
;
8640 sample
= isl_basic_set_sample_vec(bset
);
8642 return isl_bool_error
;
8643 empty
= sample
->size
== 0;
8644 isl_vec_free(bmap
->sample
);
8645 bmap
->sample
= sample
;
8647 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8652 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8655 return isl_bool_error
;
8656 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8659 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8662 return isl_bool_error
;
8663 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8666 /* Is "bmap" known to be non-empty?
8668 * That is, is the cached sample still valid?
8670 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8675 return isl_bool_error
;
8677 return isl_bool_false
;
8678 total
= 1 + isl_basic_map_total_dim(bmap
);
8679 if (bmap
->sample
->size
!= total
)
8680 return isl_bool_false
;
8681 return isl_basic_map_contains(bmap
, bmap
->sample
);
8684 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8686 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8689 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8690 __isl_take isl_basic_map
*bmap2
)
8692 struct isl_map
*map
;
8693 if (!bmap1
|| !bmap2
)
8696 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8698 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8701 map
= isl_map_add_basic_map(map
, bmap1
);
8702 map
= isl_map_add_basic_map(map
, bmap2
);
8705 isl_basic_map_free(bmap1
);
8706 isl_basic_map_free(bmap2
);
8710 struct isl_set
*isl_basic_set_union(
8711 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8713 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8714 bset_to_bmap(bset2
)));
8717 /* Order divs such that any div only depends on previous divs */
8718 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8719 __isl_take isl_basic_map
*bmap
)
8727 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8729 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8731 if (isl_int_is_zero(bmap
->div
[i
][0]))
8733 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8738 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8739 "integer division depends on itself",
8740 return isl_basic_map_free(bmap
));
8741 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8747 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8749 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8752 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8759 for (i
= 0; i
< map
->n
; ++i
) {
8760 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8771 /* Sort the local variables of "bset".
8773 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8774 __isl_take isl_basic_set
*bset
)
8776 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8779 /* Apply the expansion computed by isl_merge_divs.
8780 * The expansion itself is given by "exp" while the resulting
8781 * list of divs is given by "div".
8783 * Move the integer divisions of "bmap" into the right position
8784 * according to "exp" and then introduce the additional integer
8785 * divisions, adding div constraints.
8786 * The moving should be done first to avoid moving coefficients
8787 * in the definitions of the extra integer divisions.
8789 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8790 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8795 bmap
= isl_basic_map_cow(bmap
);
8799 if (div
->n_row
< bmap
->n_div
)
8800 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8801 "not an expansion", goto error
);
8803 n_div
= bmap
->n_div
;
8804 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8805 div
->n_row
- n_div
, 0,
8806 2 * (div
->n_row
- n_div
));
8808 for (i
= n_div
; i
< div
->n_row
; ++i
)
8809 if (isl_basic_map_alloc_div(bmap
) < 0)
8812 for (j
= n_div
- 1; j
>= 0; --j
) {
8815 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8818 for (i
= 0; i
< div
->n_row
; ++i
) {
8819 if (j
< n_div
&& exp
[j
] == i
) {
8822 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8823 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8825 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8833 isl_basic_map_free(bmap
);
8838 /* Apply the expansion computed by isl_merge_divs.
8839 * The expansion itself is given by "exp" while the resulting
8840 * list of divs is given by "div".
8842 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8843 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8845 return isl_basic_map_expand_divs(bset
, div
, exp
);
8848 /* Look for a div in dst that corresponds to the div "div" in src.
8849 * The divs before "div" in src and dst are assumed to be the same.
8851 * Returns -1 if no corresponding div was found and the position
8852 * of the corresponding div in dst otherwise.
8854 static int find_div(__isl_keep isl_basic_map
*dst
,
8855 __isl_keep isl_basic_map
*src
, unsigned div
)
8859 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8861 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8862 for (i
= div
; i
< dst
->n_div
; ++i
)
8863 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8864 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8865 dst
->n_div
- div
) == -1)
8870 /* Align the divs of "dst" to those of "src", adding divs from "src"
8871 * if needed. That is, make sure that the first src->n_div divs
8872 * of the result are equal to those of src.
8874 * The result is not finalized as by design it will have redundant
8875 * divs if any divs from "src" were copied.
8877 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8878 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8881 int known
, extended
;
8885 return isl_basic_map_free(dst
);
8887 if (src
->n_div
== 0)
8890 known
= isl_basic_map_divs_known(src
);
8892 return isl_basic_map_free(dst
);
8894 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8895 "some src divs are unknown",
8896 return isl_basic_map_free(dst
));
8898 src
= isl_basic_map_order_divs(src
);
8901 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8902 for (i
= 0; i
< src
->n_div
; ++i
) {
8903 int j
= find_div(dst
, src
, i
);
8906 int extra
= src
->n_div
- i
;
8907 dst
= isl_basic_map_cow(dst
);
8910 dst
= isl_basic_map_extend_space(dst
,
8911 isl_space_copy(dst
->dim
),
8912 extra
, 0, 2 * extra
);
8915 j
= isl_basic_map_alloc_div(dst
);
8917 return isl_basic_map_free(dst
);
8918 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8919 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8920 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8921 return isl_basic_map_free(dst
);
8924 isl_basic_map_swap_div(dst
, i
, j
);
8929 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8937 map
= isl_map_compute_divs(map
);
8938 map
= isl_map_cow(map
);
8942 for (i
= 1; i
< map
->n
; ++i
)
8943 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8944 for (i
= 1; i
< map
->n
; ++i
) {
8945 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8947 return isl_map_free(map
);
8950 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8954 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8956 return isl_map_align_divs_internal(map
);
8959 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8961 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8964 /* Align the divs of the basic maps in "map" to those
8965 * of the basic maps in "list", as well as to the other basic maps in "map".
8966 * The elements in "list" are assumed to have known divs.
8968 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8969 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8973 map
= isl_map_compute_divs(map
);
8974 map
= isl_map_cow(map
);
8976 return isl_map_free(map
);
8980 n
= isl_basic_map_list_n_basic_map(list
);
8981 for (i
= 0; i
< n
; ++i
) {
8982 isl_basic_map
*bmap
;
8984 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8985 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8986 isl_basic_map_free(bmap
);
8989 return isl_map_free(map
);
8991 return isl_map_align_divs_internal(map
);
8994 /* Align the divs of each element of "list" to those of "bmap".
8995 * Both "bmap" and the elements of "list" are assumed to have known divs.
8997 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8998 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9003 return isl_basic_map_list_free(list
);
9005 n
= isl_basic_map_list_n_basic_map(list
);
9006 for (i
= 0; i
< n
; ++i
) {
9007 isl_basic_map
*bmap_i
;
9009 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9010 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9011 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9017 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9018 __isl_take isl_map
*map
)
9022 ok
= isl_map_compatible_domain(map
, set
);
9026 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9027 "incompatible spaces", goto error
);
9028 map
= isl_map_intersect_domain(map
, set
);
9029 set
= isl_map_range(map
);
9037 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9038 __isl_take isl_map
*map
)
9040 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9043 /* There is no need to cow as removing empty parts doesn't change
9044 * the meaning of the set.
9046 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9053 for (i
= map
->n
- 1; i
>= 0; --i
)
9054 remove_if_empty(map
, i
);
9059 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9061 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9064 /* Given two basic sets bset1 and bset2, compute the maximal difference
9065 * between the values of dimension pos in bset1 and those in bset2
9066 * for any common value of the parameters and dimensions preceding pos.
9068 static enum isl_lp_result
basic_set_maximal_difference_at(
9069 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9070 int pos
, isl_int
*opt
)
9072 isl_basic_map
*bmap1
;
9073 isl_basic_map
*bmap2
;
9074 struct isl_ctx
*ctx
;
9075 struct isl_vec
*obj
;
9079 enum isl_lp_result res
;
9081 if (!bset1
|| !bset2
)
9082 return isl_lp_error
;
9084 nparam
= isl_basic_set_n_param(bset1
);
9085 dim1
= isl_basic_set_n_dim(bset1
);
9087 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9088 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9089 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9090 isl_dim_out
, 0, pos
);
9091 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9092 isl_dim_out
, 0, pos
);
9093 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
9095 return isl_lp_error
;
9097 total
= isl_basic_map_total_dim(bmap1
);
9099 obj
= isl_vec_alloc(ctx
, 1 + total
);
9102 isl_seq_clr(obj
->block
.data
, 1 + total
);
9103 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9104 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9105 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9107 isl_basic_map_free(bmap1
);
9111 isl_basic_map_free(bmap1
);
9112 return isl_lp_error
;
9115 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9116 * for any common value of the parameters and dimensions preceding pos
9117 * in both basic sets, the values of dimension pos in bset1 are
9118 * smaller or larger than those in bset2.
9121 * 1 if bset1 follows bset2
9122 * -1 if bset1 precedes bset2
9123 * 0 if bset1 and bset2 are incomparable
9124 * -2 if some error occurred.
9126 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
9127 struct isl_basic_set
*bset2
, int pos
)
9130 enum isl_lp_result res
;
9135 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9137 if (res
== isl_lp_empty
)
9139 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9140 res
== isl_lp_unbounded
)
9142 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9151 /* Given two basic sets bset1 and bset2, check whether
9152 * for any common value of the parameters and dimensions preceding pos
9153 * there is a value of dimension pos in bset1 that is larger
9154 * than a value of the same dimension in bset2.
9157 * 1 if there exists such a pair
9158 * 0 if there is no such pair, but there is a pair of equal values
9160 * -2 if some error occurred.
9162 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9163 __isl_keep isl_basic_set
*bset2
, int pos
)
9166 enum isl_lp_result res
;
9171 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9173 if (res
== isl_lp_empty
)
9175 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9176 res
== isl_lp_unbounded
)
9178 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9180 else if (res
== isl_lp_ok
)
9189 /* Given two sets set1 and set2, check whether
9190 * for any common value of the parameters and dimensions preceding pos
9191 * there is a value of dimension pos in set1 that is larger
9192 * than a value of the same dimension in set2.
9195 * 1 if there exists such a pair
9196 * 0 if there is no such pair, but there is a pair of equal values
9198 * -2 if some error occurred.
9200 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9201 __isl_keep isl_set
*set2
, int pos
)
9209 for (i
= 0; i
< set1
->n
; ++i
)
9210 for (j
= 0; j
< set2
->n
; ++j
) {
9212 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9213 if (f
== 1 || f
== -2)
9222 static isl_bool
isl_basic_map_plain_has_fixed_var(
9223 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9230 return isl_bool_error
;
9231 total
= isl_basic_map_total_dim(bmap
);
9232 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9233 for (; d
+1 > pos
; --d
)
9234 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9238 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9239 return isl_bool_false
;
9240 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9241 return isl_bool_false
;
9242 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9243 return isl_bool_false
;
9245 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9246 return isl_bool_true
;
9248 return isl_bool_false
;
9251 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9252 unsigned pos
, isl_int
*val
)
9260 return isl_bool_error
;
9262 return isl_bool_false
;
9264 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9267 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9268 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9269 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9270 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9271 fixed
= isl_bool_false
;
9274 isl_int_set(*val
, v
);
9280 static isl_bool
isl_basic_set_plain_has_fixed_var(
9281 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9283 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9287 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9288 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9290 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9291 return isl_bool_error
;
9292 return isl_basic_map_plain_has_fixed_var(bmap
,
9293 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9296 /* If "bmap" obviously lies on a hyperplane where the given dimension
9297 * has a fixed value, then return that value.
9298 * Otherwise return NaN.
9300 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9301 __isl_keep isl_basic_map
*bmap
,
9302 enum isl_dim_type type
, unsigned pos
)
9310 ctx
= isl_basic_map_get_ctx(bmap
);
9311 v
= isl_val_alloc(ctx
);
9314 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9316 return isl_val_free(v
);
9318 isl_int_set_si(v
->d
, 1);
9322 return isl_val_nan(ctx
);
9325 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9326 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9328 if (pos
>= isl_map_dim(map
, type
))
9329 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9330 "position out of bounds", return isl_bool_error
);
9331 return isl_map_plain_has_fixed_var(map
,
9332 map_offset(map
, type
) - 1 + pos
, val
);
9335 /* If "map" obviously lies on a hyperplane where the given dimension
9336 * has a fixed value, then return that value.
9337 * Otherwise return NaN.
9339 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9340 enum isl_dim_type type
, unsigned pos
)
9348 ctx
= isl_map_get_ctx(map
);
9349 v
= isl_val_alloc(ctx
);
9352 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9354 return isl_val_free(v
);
9356 isl_int_set_si(v
->d
, 1);
9360 return isl_val_nan(ctx
);
9363 /* If "set" obviously lies on a hyperplane where the given dimension
9364 * has a fixed value, then return that value.
9365 * Otherwise return NaN.
9367 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9368 enum isl_dim_type type
, unsigned pos
)
9370 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9373 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9374 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9376 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9379 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9380 * then return this fixed value in *val.
9382 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9383 unsigned dim
, isl_int
*val
)
9385 return isl_basic_set_plain_has_fixed_var(bset
,
9386 isl_basic_set_n_param(bset
) + dim
, val
);
9389 /* Return -1 if the constraint "c1" should be sorted before "c2"
9390 * and 1 if it should be sorted after "c2".
9391 * Return 0 if the two constraints are the same (up to the constant term).
9393 * In particular, if a constraint involves later variables than another
9394 * then it is sorted after this other constraint.
9395 * uset_gist depends on constraints without existentially quantified
9396 * variables sorting first.
9398 * For constraints that have the same latest variable, those
9399 * with the same coefficient for this latest variable (first in absolute value
9400 * and then in actual value) are grouped together.
9401 * This is useful for detecting pairs of constraints that can
9402 * be chained in their printed representation.
9404 * Finally, within a group, constraints are sorted according to
9405 * their coefficients (excluding the constant term).
9407 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9409 isl_int
**c1
= (isl_int
**) p1
;
9410 isl_int
**c2
= (isl_int
**) p2
;
9412 unsigned size
= *(unsigned *) arg
;
9415 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9416 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9421 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9424 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9428 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9431 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9432 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9433 * and 0 if the two constraints are the same (up to the constant term).
9435 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9436 isl_int
*c1
, isl_int
*c2
)
9442 total
= isl_basic_map_total_dim(bmap
);
9443 return sort_constraint_cmp(&c1
, &c2
, &total
);
9446 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9447 __isl_take isl_basic_map
*bmap
)
9453 if (bmap
->n_ineq
== 0)
9455 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9457 total
= isl_basic_map_total_dim(bmap
);
9458 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9459 &sort_constraint_cmp
, &total
) < 0)
9460 return isl_basic_map_free(bmap
);
9464 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9465 __isl_take isl_basic_set
*bset
)
9467 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9468 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9471 __isl_give isl_basic_map
*isl_basic_map_normalize(
9472 __isl_take isl_basic_map
*bmap
)
9476 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9478 bmap
= isl_basic_map_remove_redundancies(bmap
);
9479 bmap
= isl_basic_map_sort_constraints(bmap
);
9481 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9484 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9485 __isl_keep isl_basic_map
*bmap2
)
9489 isl_space
*space1
, *space2
;
9491 if (!bmap1
|| !bmap2
)
9496 space1
= isl_basic_map_peek_space(bmap1
);
9497 space2
= isl_basic_map_peek_space(bmap2
);
9498 cmp
= isl_space_cmp(space1
, space2
);
9501 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9502 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9503 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9504 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9505 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9507 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9509 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9511 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9512 return bmap1
->n_eq
- bmap2
->n_eq
;
9513 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9514 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9515 if (bmap1
->n_div
!= bmap2
->n_div
)
9516 return bmap1
->n_div
- bmap2
->n_div
;
9517 total
= isl_basic_map_total_dim(bmap1
);
9518 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9519 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9523 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9524 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9528 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9529 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9536 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9537 __isl_keep isl_basic_set
*bset2
)
9539 return isl_basic_map_plain_cmp(bset1
, bset2
);
9542 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9548 if (set1
->n
!= set2
->n
)
9549 return set1
->n
- set2
->n
;
9551 for (i
= 0; i
< set1
->n
; ++i
) {
9552 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9560 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9561 __isl_keep isl_basic_map
*bmap2
)
9563 if (!bmap1
|| !bmap2
)
9564 return isl_bool_error
;
9565 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9568 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9569 __isl_keep isl_basic_set
*bset2
)
9571 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9572 bset_to_bmap(bset2
));
9575 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9577 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9578 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9580 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9583 /* Sort the basic maps of "map" and remove duplicate basic maps.
9585 * While removing basic maps, we make sure that the basic maps remain
9586 * sorted because isl_map_normalize expects the basic maps of the result
9589 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9593 map
= isl_map_remove_empty_parts(map
);
9596 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9597 for (i
= map
->n
- 1; i
>= 1; --i
) {
9598 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9600 isl_basic_map_free(map
->p
[i
-1]);
9601 for (j
= i
; j
< map
->n
; ++j
)
9602 map
->p
[j
- 1] = map
->p
[j
];
9609 /* Remove obvious duplicates among the basic maps of "map".
9611 * Unlike isl_map_normalize, this function does not remove redundant
9612 * constraints and only removes duplicates that have exactly the same
9613 * constraints in the input. It does sort the constraints and
9614 * the basic maps to ease the detection of duplicates.
9616 * If "map" has already been normalized or if the basic maps are
9617 * disjoint, then there can be no duplicates.
9619 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9622 isl_basic_map
*bmap
;
9628 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9630 for (i
= 0; i
< map
->n
; ++i
) {
9631 bmap
= isl_basic_map_copy(map
->p
[i
]);
9632 bmap
= isl_basic_map_sort_constraints(bmap
);
9634 return isl_map_free(map
);
9635 isl_basic_map_free(map
->p
[i
]);
9639 map
= sort_and_remove_duplicates(map
);
9643 /* We normalize in place, but if anything goes wrong we need
9644 * to return NULL, so we need to make sure we don't change the
9645 * meaning of any possible other copies of map.
9647 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9650 struct isl_basic_map
*bmap
;
9654 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9656 for (i
= 0; i
< map
->n
; ++i
) {
9657 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9660 isl_basic_map_free(map
->p
[i
]);
9664 map
= sort_and_remove_duplicates(map
);
9666 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9673 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9675 return set_from_map(isl_map_normalize(set_to_map(set
)));
9678 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9679 __isl_keep isl_map
*map2
)
9685 return isl_bool_error
;
9688 return isl_bool_true
;
9689 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9690 return isl_bool_false
;
9692 map1
= isl_map_copy(map1
);
9693 map2
= isl_map_copy(map2
);
9694 map1
= isl_map_normalize(map1
);
9695 map2
= isl_map_normalize(map2
);
9698 equal
= map1
->n
== map2
->n
;
9699 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9700 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9710 return isl_bool_error
;
9713 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9714 __isl_keep isl_set
*set2
)
9716 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9719 /* Return the basic maps in "map" as a list.
9721 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9722 __isl_keep isl_map
*map
)
9726 isl_basic_map_list
*list
;
9730 ctx
= isl_map_get_ctx(map
);
9731 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9733 for (i
= 0; i
< map
->n
; ++i
) {
9734 isl_basic_map
*bmap
;
9736 bmap
= isl_basic_map_copy(map
->p
[i
]);
9737 list
= isl_basic_map_list_add(list
, bmap
);
9743 /* Return the intersection of the elements in the non-empty list "list".
9744 * All elements are assumed to live in the same space.
9746 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9747 __isl_take isl_basic_map_list
*list
)
9750 isl_basic_map
*bmap
;
9754 n
= isl_basic_map_list_n_basic_map(list
);
9756 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9757 "expecting non-empty list", goto error
);
9759 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9760 for (i
= 1; i
< n
; ++i
) {
9761 isl_basic_map
*bmap_i
;
9763 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9764 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9767 isl_basic_map_list_free(list
);
9770 isl_basic_map_list_free(list
);
9774 /* Return the intersection of the elements in the non-empty list "list".
9775 * All elements are assumed to live in the same space.
9777 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9778 __isl_take isl_basic_set_list
*list
)
9780 return isl_basic_map_list_intersect(list
);
9783 /* Return the union of the elements of "list".
9784 * The list is required to have at least one element.
9786 __isl_give isl_set
*isl_basic_set_list_union(
9787 __isl_take isl_basic_set_list
*list
)
9791 isl_basic_set
*bset
;
9796 n
= isl_basic_set_list_n_basic_set(list
);
9798 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9799 "expecting non-empty list", goto error
);
9801 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9802 space
= isl_basic_set_get_space(bset
);
9803 isl_basic_set_free(bset
);
9805 set
= isl_set_alloc_space(space
, n
, 0);
9806 for (i
= 0; i
< n
; ++i
) {
9807 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9808 set
= isl_set_add_basic_set(set
, bset
);
9811 isl_basic_set_list_free(list
);
9814 isl_basic_set_list_free(list
);
9818 /* Return the union of the elements in the non-empty list "list".
9819 * All elements are assumed to live in the same space.
9821 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9828 n
= isl_set_list_n_set(list
);
9830 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9831 "expecting non-empty list", goto error
);
9833 set
= isl_set_list_get_set(list
, 0);
9834 for (i
= 1; i
< n
; ++i
) {
9837 set_i
= isl_set_list_get_set(list
, i
);
9838 set
= isl_set_union(set
, set_i
);
9841 isl_set_list_free(list
);
9844 isl_set_list_free(list
);
9848 __isl_give isl_basic_map
*isl_basic_map_product(
9849 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9851 isl_space
*dim_result
= NULL
;
9852 struct isl_basic_map
*bmap
;
9853 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9854 struct isl_dim_map
*dim_map1
, *dim_map2
;
9856 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9858 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9859 isl_space_copy(bmap2
->dim
));
9861 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9862 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9863 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9864 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9865 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9867 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9868 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9869 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9870 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9871 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9872 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9873 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9874 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9875 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9876 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9877 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9879 bmap
= isl_basic_map_alloc_space(dim_result
,
9880 bmap1
->n_div
+ bmap2
->n_div
,
9881 bmap1
->n_eq
+ bmap2
->n_eq
,
9882 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9883 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9884 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9885 bmap
= isl_basic_map_simplify(bmap
);
9886 return isl_basic_map_finalize(bmap
);
9888 isl_basic_map_free(bmap1
);
9889 isl_basic_map_free(bmap2
);
9893 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9894 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9896 isl_basic_map
*prod
;
9898 prod
= isl_basic_map_product(bmap1
, bmap2
);
9899 prod
= isl_basic_map_flatten(prod
);
9903 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9904 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9906 return isl_basic_map_flat_range_product(bset1
, bset2
);
9909 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9910 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9912 isl_space
*space_result
= NULL
;
9913 isl_basic_map
*bmap
;
9914 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9915 struct isl_dim_map
*dim_map1
, *dim_map2
;
9917 if (!bmap1
|| !bmap2
)
9920 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9921 isl_space_copy(bmap2
->dim
));
9923 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9924 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9925 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9926 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9928 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9929 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9930 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9931 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9932 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9933 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9934 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9935 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9936 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9937 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9938 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9940 bmap
= isl_basic_map_alloc_space(space_result
,
9941 bmap1
->n_div
+ bmap2
->n_div
,
9942 bmap1
->n_eq
+ bmap2
->n_eq
,
9943 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9944 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9945 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9946 bmap
= isl_basic_map_simplify(bmap
);
9947 return isl_basic_map_finalize(bmap
);
9949 isl_basic_map_free(bmap1
);
9950 isl_basic_map_free(bmap2
);
9954 __isl_give isl_basic_map
*isl_basic_map_range_product(
9955 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9958 isl_space
*dim_result
= NULL
;
9959 isl_basic_map
*bmap
;
9960 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9961 struct isl_dim_map
*dim_map1
, *dim_map2
;
9963 rational
= isl_basic_map_is_rational(bmap1
);
9964 if (rational
>= 0 && rational
)
9965 rational
= isl_basic_map_is_rational(bmap2
);
9966 if (!bmap1
|| !bmap2
|| rational
< 0)
9969 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9972 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9973 isl_space_copy(bmap2
->dim
));
9975 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9976 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9977 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9978 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9980 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9981 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9982 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9983 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9984 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9985 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9986 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9987 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9988 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9989 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9990 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9992 bmap
= isl_basic_map_alloc_space(dim_result
,
9993 bmap1
->n_div
+ bmap2
->n_div
,
9994 bmap1
->n_eq
+ bmap2
->n_eq
,
9995 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9996 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9997 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9999 bmap
= isl_basic_map_set_rational(bmap
);
10000 bmap
= isl_basic_map_simplify(bmap
);
10001 return isl_basic_map_finalize(bmap
);
10003 isl_basic_map_free(bmap1
);
10004 isl_basic_map_free(bmap2
);
10008 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10009 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10011 isl_basic_map
*prod
;
10013 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10014 prod
= isl_basic_map_flatten_range(prod
);
10018 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10019 * and collect the results.
10020 * The result live in the space obtained by calling "space_product"
10021 * on the spaces of "map1" and "map2".
10022 * If "remove_duplicates" is set then the result may contain duplicates
10023 * (even if the inputs do not) and so we try and remove the obvious
10026 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10027 __isl_take isl_map
*map2
,
10028 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10029 __isl_take isl_space
*right
),
10030 __isl_give isl_basic_map
*(*basic_map_product
)(
10031 __isl_take isl_basic_map
*left
,
10032 __isl_take isl_basic_map
*right
),
10033 int remove_duplicates
)
10035 unsigned flags
= 0;
10036 struct isl_map
*result
;
10040 m
= isl_map_has_equal_params(map1
, map2
);
10044 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10045 "parameters don't match", goto error
);
10047 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10048 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10049 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10051 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10052 isl_space_copy(map2
->dim
)),
10053 map1
->n
* map2
->n
, flags
);
10056 for (i
= 0; i
< map1
->n
; ++i
)
10057 for (j
= 0; j
< map2
->n
; ++j
) {
10058 struct isl_basic_map
*part
;
10059 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10060 isl_basic_map_copy(map2
->p
[j
]));
10061 if (isl_basic_map_is_empty(part
))
10062 isl_basic_map_free(part
);
10064 result
= isl_map_add_basic_map(result
, part
);
10068 if (remove_duplicates
)
10069 result
= isl_map_remove_obvious_duplicates(result
);
10070 isl_map_free(map1
);
10071 isl_map_free(map2
);
10074 isl_map_free(map1
);
10075 isl_map_free(map2
);
10079 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10081 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10082 __isl_take isl_map
*map2
)
10084 return map_product(map1
, map2
, &isl_space_product
,
10085 &isl_basic_map_product
, 0);
10088 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10089 __isl_take isl_map
*map2
)
10091 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10094 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10096 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10097 __isl_take isl_map
*map2
)
10101 prod
= isl_map_product(map1
, map2
);
10102 prod
= isl_map_flatten(prod
);
10106 /* Given two set A and B, construct its Cartesian product A x B.
10108 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10110 return isl_map_range_product(set1
, set2
);
10113 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10114 __isl_take isl_set
*set2
)
10116 return isl_map_flat_range_product(set1
, set2
);
10119 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10121 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10122 __isl_take isl_map
*map2
)
10124 return map_product(map1
, map2
, &isl_space_domain_product
,
10125 &isl_basic_map_domain_product
, 1);
10128 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10130 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10131 __isl_take isl_map
*map2
)
10133 return map_product(map1
, map2
, &isl_space_range_product
,
10134 &isl_basic_map_range_product
, 1);
10137 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10138 __isl_take isl_map
*map2
)
10140 return isl_map_align_params_map_map_and(map1
, map2
,
10141 &map_domain_product_aligned
);
10144 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10145 __isl_take isl_map
*map2
)
10147 return isl_map_align_params_map_map_and(map1
, map2
,
10148 &map_range_product_aligned
);
10151 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10153 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10156 int total1
, keep1
, total2
, keep2
;
10160 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10161 !isl_space_range_is_wrapping(map
->dim
))
10162 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10163 "not a product", return isl_map_free(map
));
10165 space
= isl_map_get_space(map
);
10166 total1
= isl_space_dim(space
, isl_dim_in
);
10167 total2
= isl_space_dim(space
, isl_dim_out
);
10168 space
= isl_space_factor_domain(space
);
10169 keep1
= isl_space_dim(space
, isl_dim_in
);
10170 keep2
= isl_space_dim(space
, isl_dim_out
);
10171 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10172 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10173 map
= isl_map_reset_space(map
, space
);
10178 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10180 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10183 int total1
, keep1
, total2
, keep2
;
10187 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10188 !isl_space_range_is_wrapping(map
->dim
))
10189 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10190 "not a product", return isl_map_free(map
));
10192 space
= isl_map_get_space(map
);
10193 total1
= isl_space_dim(space
, isl_dim_in
);
10194 total2
= isl_space_dim(space
, isl_dim_out
);
10195 space
= isl_space_factor_range(space
);
10196 keep1
= isl_space_dim(space
, isl_dim_in
);
10197 keep2
= isl_space_dim(space
, isl_dim_out
);
10198 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10199 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10200 map
= isl_map_reset_space(map
, space
);
10205 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10207 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10214 if (!isl_space_domain_is_wrapping(map
->dim
))
10215 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10216 "domain is not a product", return isl_map_free(map
));
10218 space
= isl_map_get_space(map
);
10219 total
= isl_space_dim(space
, isl_dim_in
);
10220 space
= isl_space_domain_factor_domain(space
);
10221 keep
= isl_space_dim(space
, isl_dim_in
);
10222 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10223 map
= isl_map_reset_space(map
, space
);
10228 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10230 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10237 if (!isl_space_domain_is_wrapping(map
->dim
))
10238 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10239 "domain is not a product", return isl_map_free(map
));
10241 space
= isl_map_get_space(map
);
10242 total
= isl_space_dim(space
, isl_dim_in
);
10243 space
= isl_space_domain_factor_range(space
);
10244 keep
= isl_space_dim(space
, isl_dim_in
);
10245 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10246 map
= isl_map_reset_space(map
, space
);
10251 /* Given a map A -> [B -> C], extract the map A -> B.
10253 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10260 if (!isl_space_range_is_wrapping(map
->dim
))
10261 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10262 "range is not a product", return isl_map_free(map
));
10264 space
= isl_map_get_space(map
);
10265 total
= isl_space_dim(space
, isl_dim_out
);
10266 space
= isl_space_range_factor_domain(space
);
10267 keep
= isl_space_dim(space
, isl_dim_out
);
10268 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10269 map
= isl_map_reset_space(map
, space
);
10274 /* Given a map A -> [B -> C], extract the map A -> C.
10276 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10283 if (!isl_space_range_is_wrapping(map
->dim
))
10284 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10285 "range is not a product", return isl_map_free(map
));
10287 space
= isl_map_get_space(map
);
10288 total
= isl_space_dim(space
, isl_dim_out
);
10289 space
= isl_space_range_factor_range(space
);
10290 keep
= isl_space_dim(space
, isl_dim_out
);
10291 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10292 map
= isl_map_reset_space(map
, space
);
10297 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10299 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10300 __isl_take isl_map
*map2
)
10304 prod
= isl_map_domain_product(map1
, map2
);
10305 prod
= isl_map_flatten_domain(prod
);
10309 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10311 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10312 __isl_take isl_map
*map2
)
10316 prod
= isl_map_range_product(map1
, map2
);
10317 prod
= isl_map_flatten_range(prod
);
10321 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10324 uint32_t hash
= isl_hash_init();
10329 bmap
= isl_basic_map_copy(bmap
);
10330 bmap
= isl_basic_map_normalize(bmap
);
10333 total
= isl_basic_map_total_dim(bmap
);
10334 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10335 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10337 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10338 isl_hash_hash(hash
, c_hash
);
10340 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10341 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10343 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10344 isl_hash_hash(hash
, c_hash
);
10346 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10347 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10349 if (isl_int_is_zero(bmap
->div
[i
][0]))
10351 isl_hash_byte(hash
, i
& 0xFF);
10352 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10353 isl_hash_hash(hash
, c_hash
);
10355 isl_basic_map_free(bmap
);
10359 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10361 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10364 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10371 map
= isl_map_copy(map
);
10372 map
= isl_map_normalize(map
);
10376 hash
= isl_hash_init();
10377 for (i
= 0; i
< map
->n
; ++i
) {
10378 uint32_t bmap_hash
;
10379 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10380 isl_hash_hash(hash
, bmap_hash
);
10388 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10390 return isl_map_get_hash(set_to_map(set
));
10393 /* Return the number of basic maps in the (current) representation of "map".
10395 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10397 return map
? map
->n
: 0;
10400 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10402 return set
? set
->n
: 0;
10405 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10406 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10411 return isl_stat_error
;
10413 for (i
= 0; i
< map
->n
; ++i
)
10414 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10415 return isl_stat_error
;
10417 return isl_stat_ok
;
10420 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10421 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10426 return isl_stat_error
;
10428 for (i
= 0; i
< set
->n
; ++i
)
10429 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10430 return isl_stat_error
;
10432 return isl_stat_ok
;
10435 /* Return a list of basic sets, the union of which is equal to "set".
10437 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10438 __isl_keep isl_set
*set
)
10441 isl_basic_set_list
*list
;
10446 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10447 for (i
= 0; i
< set
->n
; ++i
) {
10448 isl_basic_set
*bset
;
10450 bset
= isl_basic_set_copy(set
->p
[i
]);
10451 list
= isl_basic_set_list_add(list
, bset
);
10457 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10464 bset
= isl_basic_set_cow(bset
);
10468 dim
= isl_basic_set_get_space(bset
);
10469 dim
= isl_space_lift(dim
, bset
->n_div
);
10472 isl_space_free(bset
->dim
);
10474 bset
->extra
-= bset
->n_div
;
10477 bset
= isl_basic_set_finalize(bset
);
10481 isl_basic_set_free(bset
);
10485 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10491 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10496 set
= isl_set_cow(set
);
10500 n_div
= set
->p
[0]->n_div
;
10501 dim
= isl_set_get_space(set
);
10502 dim
= isl_space_lift(dim
, n_div
);
10505 isl_space_free(set
->dim
);
10508 for (i
= 0; i
< set
->n
; ++i
) {
10509 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10520 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10528 dim
= isl_basic_set_total_dim(bset
);
10529 size
+= bset
->n_eq
* (1 + dim
);
10530 size
+= bset
->n_ineq
* (1 + dim
);
10531 size
+= bset
->n_div
* (2 + dim
);
10536 int isl_set_size(__isl_keep isl_set
*set
)
10544 for (i
= 0; i
< set
->n
; ++i
)
10545 size
+= isl_basic_set_size(set
->p
[i
]);
10550 /* Check if there is any lower bound (if lower == 0) and/or upper
10551 * bound (if upper == 0) on the specified dim.
10553 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10554 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10558 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10559 return isl_bool_error
;
10561 pos
+= isl_basic_map_offset(bmap
, type
);
10563 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10564 if (isl_int_is_zero(bmap
->div
[i
][0]))
10566 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10567 return isl_bool_true
;
10570 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10571 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10572 return isl_bool_true
;
10574 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10575 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10582 return lower
&& upper
;
10585 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10586 enum isl_dim_type type
, unsigned pos
)
10588 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10591 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10592 enum isl_dim_type type
, unsigned pos
)
10594 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10597 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10598 enum isl_dim_type type
, unsigned pos
)
10600 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10603 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10604 enum isl_dim_type type
, unsigned pos
)
10609 return isl_bool_error
;
10611 for (i
= 0; i
< map
->n
; ++i
) {
10613 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10614 if (bounded
< 0 || !bounded
)
10618 return isl_bool_true
;
10621 /* Return true if the specified dim is involved in both an upper bound
10622 * and a lower bound.
10624 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10625 enum isl_dim_type type
, unsigned pos
)
10627 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10630 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10632 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10633 enum isl_dim_type type
, unsigned pos
,
10634 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10635 enum isl_dim_type type
, unsigned pos
))
10640 return isl_bool_error
;
10642 for (i
= 0; i
< map
->n
; ++i
) {
10644 bounded
= fn(map
->p
[i
], type
, pos
);
10645 if (bounded
< 0 || bounded
)
10649 return isl_bool_false
;
10652 /* Return 1 if the specified dim is involved in any lower bound.
10654 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10655 enum isl_dim_type type
, unsigned pos
)
10657 return has_any_bound(set
, type
, pos
,
10658 &isl_basic_map_dim_has_lower_bound
);
10661 /* Return 1 if the specified dim is involved in any upper bound.
10663 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10664 enum isl_dim_type type
, unsigned pos
)
10666 return has_any_bound(set
, type
, pos
,
10667 &isl_basic_map_dim_has_upper_bound
);
10670 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10672 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10673 enum isl_dim_type type
, unsigned pos
,
10674 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10675 enum isl_dim_type type
, unsigned pos
))
10680 return isl_bool_error
;
10682 for (i
= 0; i
< map
->n
; ++i
) {
10684 bounded
= fn(map
->p
[i
], type
, pos
);
10685 if (bounded
< 0 || !bounded
)
10689 return isl_bool_true
;
10692 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10694 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10695 enum isl_dim_type type
, unsigned pos
)
10697 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10700 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10702 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10703 enum isl_dim_type type
, unsigned pos
)
10705 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10708 /* For each of the "n" variables starting at "first", determine
10709 * the sign of the variable and put the results in the first "n"
10710 * elements of the array "signs".
10712 * 1 means that the variable is non-negative
10713 * -1 means that the variable is non-positive
10714 * 0 means the variable attains both positive and negative values.
10716 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10717 unsigned first
, unsigned n
, int *signs
)
10719 isl_vec
*bound
= NULL
;
10720 struct isl_tab
*tab
= NULL
;
10721 struct isl_tab_undo
*snap
;
10724 if (!bset
|| !signs
)
10725 return isl_stat_error
;
10727 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10728 tab
= isl_tab_from_basic_set(bset
, 0);
10729 if (!bound
|| !tab
)
10732 isl_seq_clr(bound
->el
, bound
->size
);
10733 isl_int_set_si(bound
->el
[0], -1);
10735 snap
= isl_tab_snap(tab
);
10736 for (i
= 0; i
< n
; ++i
) {
10739 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10740 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10742 empty
= tab
->empty
;
10743 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10744 if (isl_tab_rollback(tab
, snap
) < 0)
10752 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10753 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10755 empty
= tab
->empty
;
10756 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10757 if (isl_tab_rollback(tab
, snap
) < 0)
10760 signs
[i
] = empty
? -1 : 0;
10764 isl_vec_free(bound
);
10765 return isl_stat_ok
;
10768 isl_vec_free(bound
);
10769 return isl_stat_error
;
10772 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10773 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10775 if (!bset
|| !signs
)
10776 return isl_stat_error
;
10777 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10778 return isl_stat_error
);
10780 first
+= pos(bset
->dim
, type
) - 1;
10781 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10784 /* Is it possible for the integer division "div" to depend (possibly
10785 * indirectly) on any output dimensions?
10787 * If the div is undefined, then we conservatively assume that it
10788 * may depend on them.
10789 * Otherwise, we check if it actually depends on them or on any integer
10790 * divisions that may depend on them.
10792 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10795 unsigned n_out
, o_out
;
10796 unsigned n_div
, o_div
;
10798 if (isl_int_is_zero(bmap
->div
[div
][0]))
10799 return isl_bool_true
;
10801 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10802 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10804 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10805 return isl_bool_true
;
10807 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10808 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10810 for (i
= 0; i
< n_div
; ++i
) {
10811 isl_bool may_involve
;
10813 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10815 may_involve
= div_may_involve_output(bmap
, i
);
10816 if (may_involve
< 0 || may_involve
)
10817 return may_involve
;
10820 return isl_bool_false
;
10823 /* Return the first integer division of "bmap" in the range
10824 * [first, first + n[ that may depend on any output dimensions and
10825 * that has a non-zero coefficient in "c" (where the first coefficient
10826 * in "c" corresponds to integer division "first").
10828 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10829 isl_int
*c
, int first
, int n
)
10836 for (k
= first
; k
< first
+ n
; ++k
) {
10837 isl_bool may_involve
;
10839 if (isl_int_is_zero(c
[k
]))
10841 may_involve
= div_may_involve_output(bmap
, k
);
10842 if (may_involve
< 0)
10851 /* Look for a pair of inequality constraints in "bmap" of the form
10853 * -l + i >= 0 or i >= l
10855 * n + l - i >= 0 or i <= l + n
10857 * with n < "m" and i the output dimension at position "pos".
10858 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10859 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10860 * and earlier output dimensions, as well as integer divisions that do
10861 * not involve any of the output dimensions.
10863 * Return the index of the first inequality constraint or bmap->n_ineq
10864 * if no such pair can be found.
10866 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10867 int pos
, isl_int m
)
10872 unsigned n_div
, o_div
;
10873 unsigned n_out
, o_out
;
10879 ctx
= isl_basic_map_get_ctx(bmap
);
10880 total
= isl_basic_map_total_dim(bmap
);
10881 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10882 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10883 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10884 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10885 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10886 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10888 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10889 n_out
- (pos
+ 1)) != -1)
10891 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10894 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10895 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10898 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10899 bmap
->ineq
[j
] + 1, total
))
10903 if (j
>= bmap
->n_ineq
)
10905 isl_int_add(bmap
->ineq
[i
][0],
10906 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10907 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10908 isl_int_sub(bmap
->ineq
[i
][0],
10909 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10912 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10918 return bmap
->n_ineq
;
10921 /* Return the index of the equality of "bmap" that defines
10922 * the output dimension "pos" in terms of earlier dimensions.
10923 * The equality may also involve integer divisions, as long
10924 * as those integer divisions are defined in terms of
10925 * parameters or input dimensions.
10926 * In this case, *div is set to the number of integer divisions and
10927 * *ineq is set to the number of inequality constraints (provided
10928 * div and ineq are not NULL).
10930 * The equality may also involve a single integer division involving
10931 * the output dimensions (typically only output dimension "pos") as
10932 * long as the coefficient of output dimension "pos" is 1 or -1 and
10933 * there is a pair of constraints i >= l and i <= l + n, with i referring
10934 * to output dimension "pos", l an expression involving only earlier
10935 * dimensions and n smaller than the coefficient of the integer division
10936 * in the equality. In this case, the output dimension can be defined
10937 * in terms of a modulo expression that does not involve the integer division.
10938 * *div is then set to this single integer division and
10939 * *ineq is set to the index of constraint i >= l.
10941 * Return bmap->n_eq if there is no such equality.
10942 * Return -1 on error.
10944 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10945 int pos
, int *div
, int *ineq
)
10948 unsigned n_out
, o_out
;
10949 unsigned n_div
, o_div
;
10954 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10955 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10956 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10957 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10960 *ineq
= bmap
->n_ineq
;
10963 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10964 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10966 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10967 n_out
- (pos
+ 1)) != -1)
10969 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10973 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10974 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10976 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10977 k
+ 1, n_div
- (k
+1)) < n_div
)
10979 l
= find_modulo_constraint_pair(bmap
, pos
,
10980 bmap
->eq
[j
][o_div
+ k
]);
10983 if (l
>= bmap
->n_ineq
)
10995 /* Check if the given basic map is obviously single-valued.
10996 * In particular, for each output dimension, check that there is
10997 * an equality that defines the output dimension in terms of
10998 * earlier dimensions.
11000 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11006 return isl_bool_error
;
11008 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11010 for (i
= 0; i
< n_out
; ++i
) {
11013 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11016 return isl_bool_error
;
11017 if (eq
>= bmap
->n_eq
)
11018 return isl_bool_false
;
11021 return isl_bool_true
;
11024 /* Check if the given basic map is single-valued.
11025 * We simply compute
11029 * and check if the result is a subset of the identity mapping.
11031 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11034 isl_basic_map
*test
;
11038 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11042 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11043 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11045 space
= isl_basic_map_get_space(bmap
);
11046 space
= isl_space_map_from_set(isl_space_range(space
));
11047 id
= isl_basic_map_identity(space
);
11049 sv
= isl_basic_map_is_subset(test
, id
);
11051 isl_basic_map_free(test
);
11052 isl_basic_map_free(id
);
11057 /* Check if the given map is obviously single-valued.
11059 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11062 return isl_bool_error
;
11064 return isl_bool_true
;
11066 return isl_bool_false
;
11068 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11071 /* Check if the given map is single-valued.
11072 * We simply compute
11076 * and check if the result is a subset of the identity mapping.
11078 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11085 sv
= isl_map_plain_is_single_valued(map
);
11089 test
= isl_map_reverse(isl_map_copy(map
));
11090 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11092 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11093 id
= isl_map_identity(dim
);
11095 sv
= isl_map_is_subset(test
, id
);
11097 isl_map_free(test
);
11103 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11107 map
= isl_map_copy(map
);
11108 map
= isl_map_reverse(map
);
11109 in
= isl_map_is_single_valued(map
);
11115 /* Check if the given map is obviously injective.
11117 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11121 map
= isl_map_copy(map
);
11122 map
= isl_map_reverse(map
);
11123 in
= isl_map_plain_is_single_valued(map
);
11129 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11133 sv
= isl_map_is_single_valued(map
);
11137 return isl_map_is_injective(map
);
11140 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11142 return isl_map_is_single_valued(set_to_map(set
));
11145 /* Does "map" only map elements to themselves?
11147 * If the domain and range spaces are different, then "map"
11148 * is considered not to be an identity relation, even if it is empty.
11149 * Otherwise, construct the maximal identity relation and
11150 * check whether "map" is a subset of this relation.
11152 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11156 isl_bool equal
, is_identity
;
11158 space
= isl_map_get_space(map
);
11159 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11160 isl_space_free(space
);
11161 if (equal
< 0 || !equal
)
11164 id
= isl_map_identity(isl_map_get_space(map
));
11165 is_identity
= isl_map_is_subset(map
, id
);
11168 return is_identity
;
11171 int isl_map_is_translation(__isl_keep isl_map
*map
)
11176 delta
= isl_map_deltas(isl_map_copy(map
));
11177 ok
= isl_set_is_singleton(delta
);
11178 isl_set_free(delta
);
11183 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11185 if (isl_seq_first_non_zero(p
, pos
) != -1)
11187 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11192 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11199 return isl_bool_error
;
11201 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11202 return isl_bool_false
;
11204 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11205 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11206 for (j
= 0; j
< nvar
; ++j
) {
11207 int lower
= 0, upper
= 0;
11208 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11209 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11211 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11212 return isl_bool_false
;
11215 if (i
< bset
->n_eq
)
11217 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11218 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11220 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11221 return isl_bool_false
;
11222 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11227 if (!lower
|| !upper
)
11228 return isl_bool_false
;
11231 return isl_bool_true
;
11234 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11237 return isl_bool_error
;
11239 return isl_bool_false
;
11241 return isl_basic_set_is_box(set
->p
[0]);
11244 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11247 return isl_bool_error
;
11249 return isl_space_is_wrapping(bset
->dim
);
11252 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11255 return isl_bool_error
;
11257 return isl_space_is_wrapping(set
->dim
);
11260 /* Modify the space of "map" through a call to "change".
11261 * If "can_change" is set (not NULL), then first call it to check
11262 * if the modification is allowed, printing the error message "cannot_change"
11265 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11266 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11267 const char *cannot_change
,
11268 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11276 ok
= can_change
? can_change(map
) : isl_bool_true
;
11278 return isl_map_free(map
);
11280 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11281 return isl_map_free(map
));
11283 space
= change(isl_map_get_space(map
));
11284 map
= isl_map_reset_space(map
, space
);
11289 /* Is the domain of "map" a wrapped relation?
11291 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11294 return isl_bool_error
;
11296 return isl_space_domain_is_wrapping(map
->dim
);
11299 /* Does "map" have a wrapped relation in both domain and range?
11301 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11303 return isl_space_is_product(isl_map_peek_space(map
));
11306 /* Is the range of "map" a wrapped relation?
11308 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11311 return isl_bool_error
;
11313 return isl_space_range_is_wrapping(map
->dim
);
11316 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11318 bmap
= isl_basic_map_cow(bmap
);
11322 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11326 bmap
= isl_basic_map_finalize(bmap
);
11328 return bset_from_bmap(bmap
);
11330 isl_basic_map_free(bmap
);
11334 /* Given a map A -> B, return the set (A -> B).
11336 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11338 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11341 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11343 bset
= isl_basic_set_cow(bset
);
11347 bset
->dim
= isl_space_unwrap(bset
->dim
);
11351 bset
= isl_basic_set_finalize(bset
);
11353 return bset_to_bmap(bset
);
11355 isl_basic_set_free(bset
);
11359 /* Given a set (A -> B), return the map A -> B.
11360 * Error out if "set" is not of the form (A -> B).
11362 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11364 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11365 "not a wrapping set", &isl_space_unwrap
);
11368 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11369 enum isl_dim_type type
)
11374 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11377 bmap
= isl_basic_map_cow(bmap
);
11381 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11385 bmap
= isl_basic_map_finalize(bmap
);
11389 isl_basic_map_free(bmap
);
11393 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11394 enum isl_dim_type type
)
11401 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11404 map
= isl_map_cow(map
);
11408 for (i
= 0; i
< map
->n
; ++i
) {
11409 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11413 map
->dim
= isl_space_reset(map
->dim
, type
);
11423 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11428 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11431 bmap
= isl_basic_map_cow(bmap
);
11435 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11439 bmap
= isl_basic_map_finalize(bmap
);
11443 isl_basic_map_free(bmap
);
11447 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11449 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11452 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11453 __isl_take isl_basic_map
*bmap
)
11458 if (!bmap
->dim
->nested
[0])
11461 bmap
= isl_basic_map_cow(bmap
);
11465 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11469 bmap
= isl_basic_map_finalize(bmap
);
11473 isl_basic_map_free(bmap
);
11477 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11478 __isl_take isl_basic_map
*bmap
)
11483 if (!bmap
->dim
->nested
[1])
11486 bmap
= isl_basic_map_cow(bmap
);
11490 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11494 bmap
= isl_basic_map_finalize(bmap
);
11498 isl_basic_map_free(bmap
);
11502 /* Remove any internal structure from the spaces of domain and range of "map".
11504 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11509 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11512 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11515 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11517 return set_from_map(isl_map_flatten(set_to_map(set
)));
11520 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11522 isl_space
*dim
, *flat_dim
;
11525 dim
= isl_set_get_space(set
);
11526 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11527 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11528 map
= isl_map_intersect_domain(map
, set
);
11533 /* Remove any internal structure from the space of the domain of "map".
11535 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11540 if (!map
->dim
->nested
[0])
11543 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11546 /* Remove any internal structure from the space of the range of "map".
11548 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11553 if (!map
->dim
->nested
[1])
11556 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11559 /* Reorder the dimensions of "bmap" according to the given dim_map
11560 * and set the dimension specification to "dim" and
11561 * perform Gaussian elimination on the result.
11563 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11564 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11566 isl_basic_map
*res
;
11569 bmap
= isl_basic_map_cow(bmap
);
11570 if (!bmap
|| !dim
|| !dim_map
)
11573 flags
= bmap
->flags
;
11574 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11575 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11576 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11577 res
= isl_basic_map_alloc_space(dim
,
11578 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11579 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11581 res
->flags
= flags
;
11582 res
= isl_basic_map_gauss(res
, NULL
);
11583 res
= isl_basic_map_finalize(res
);
11587 isl_basic_map_free(bmap
);
11588 isl_space_free(dim
);
11592 /* Reorder the dimensions of "map" according to given reordering.
11594 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11595 __isl_take isl_reordering
*r
)
11598 struct isl_dim_map
*dim_map
;
11600 map
= isl_map_cow(map
);
11601 dim_map
= isl_dim_map_from_reordering(r
);
11602 if (!map
|| !r
|| !dim_map
)
11605 for (i
= 0; i
< map
->n
; ++i
) {
11606 struct isl_dim_map
*dim_map_i
;
11608 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11610 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11611 isl_space_copy(r
->dim
), dim_map_i
);
11617 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11619 isl_reordering_free(r
);
11625 isl_reordering_free(r
);
11629 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11630 __isl_take isl_reordering
*r
)
11632 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11635 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11636 __isl_take isl_space
*model
)
11641 if (!map
|| !model
)
11644 ctx
= isl_space_get_ctx(model
);
11645 if (!isl_space_has_named_params(model
))
11646 isl_die(ctx
, isl_error_invalid
,
11647 "model has unnamed parameters", goto error
);
11648 if (isl_map_check_named_params(map
) < 0)
11650 aligned
= isl_map_space_has_equal_params(map
, model
);
11654 isl_reordering
*exp
;
11656 model
= isl_space_drop_dims(model
, isl_dim_in
,
11657 0, isl_space_dim(model
, isl_dim_in
));
11658 model
= isl_space_drop_dims(model
, isl_dim_out
,
11659 0, isl_space_dim(model
, isl_dim_out
));
11660 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11661 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11662 map
= isl_map_realign(map
, exp
);
11665 isl_space_free(model
);
11668 isl_space_free(model
);
11673 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11674 __isl_take isl_space
*model
)
11676 return isl_map_align_params(set
, model
);
11679 /* Align the parameters of "bmap" to those of "model", introducing
11680 * additional parameters if needed.
11682 __isl_give isl_basic_map
*isl_basic_map_align_params(
11683 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11686 isl_bool equal_params
;
11688 if (!bmap
|| !model
)
11691 ctx
= isl_space_get_ctx(model
);
11692 if (!isl_space_has_named_params(model
))
11693 isl_die(ctx
, isl_error_invalid
,
11694 "model has unnamed parameters", goto error
);
11695 if (!isl_space_has_named_params(bmap
->dim
))
11696 isl_die(ctx
, isl_error_invalid
,
11697 "relation has unnamed parameters", goto error
);
11698 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11699 if (equal_params
< 0)
11701 if (!equal_params
) {
11702 isl_reordering
*exp
;
11703 struct isl_dim_map
*dim_map
;
11705 model
= isl_space_drop_dims(model
, isl_dim_in
,
11706 0, isl_space_dim(model
, isl_dim_in
));
11707 model
= isl_space_drop_dims(model
, isl_dim_out
,
11708 0, isl_space_dim(model
, isl_dim_out
));
11709 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11710 exp
= isl_reordering_extend_space(exp
,
11711 isl_basic_map_get_space(bmap
));
11712 dim_map
= isl_dim_map_from_reordering(exp
);
11713 bmap
= isl_basic_map_realign(bmap
,
11714 exp
? isl_space_copy(exp
->dim
) : NULL
,
11715 isl_dim_map_extend(dim_map
, bmap
));
11716 isl_reordering_free(exp
);
11720 isl_space_free(model
);
11723 isl_space_free(model
);
11724 isl_basic_map_free(bmap
);
11728 /* Do "bset" and "space" have the same parameters?
11730 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11731 __isl_keep isl_space
*space
)
11733 isl_space
*bset_space
;
11735 bset_space
= isl_basic_set_peek_space(bset
);
11736 return isl_space_has_equal_params(bset_space
, space
);
11739 /* Do "map" and "space" have the same parameters?
11741 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11742 __isl_keep isl_space
*space
)
11744 isl_space
*map_space
;
11746 map_space
= isl_map_peek_space(map
);
11747 return isl_space_has_equal_params(map_space
, space
);
11750 /* Do "set" and "space" have the same parameters?
11752 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11753 __isl_keep isl_space
*space
)
11755 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11758 /* Align the parameters of "bset" to those of "model", introducing
11759 * additional parameters if needed.
11761 __isl_give isl_basic_set
*isl_basic_set_align_params(
11762 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11764 return isl_basic_map_align_params(bset
, model
);
11767 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11768 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11769 enum isl_dim_type c2
, enum isl_dim_type c3
,
11770 enum isl_dim_type c4
, enum isl_dim_type c5
)
11772 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11773 struct isl_mat
*mat
;
11779 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11780 isl_basic_map_total_dim(bmap
) + 1);
11783 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11784 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11785 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11786 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11787 isl_int_set(mat
->row
[i
][pos
],
11788 bmap
->eq
[i
][off
+ k
]);
11796 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11797 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11798 enum isl_dim_type c2
, enum isl_dim_type c3
,
11799 enum isl_dim_type c4
, enum isl_dim_type c5
)
11801 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11802 struct isl_mat
*mat
;
11808 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11809 isl_basic_map_total_dim(bmap
) + 1);
11812 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11813 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11814 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11815 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11816 isl_int_set(mat
->row
[i
][pos
],
11817 bmap
->ineq
[i
][off
+ k
]);
11825 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11826 __isl_take isl_space
*dim
,
11827 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11828 enum isl_dim_type c2
, enum isl_dim_type c3
,
11829 enum isl_dim_type c4
, enum isl_dim_type c5
)
11831 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11832 isl_basic_map
*bmap
;
11838 if (!dim
|| !eq
|| !ineq
)
11841 if (eq
->n_col
!= ineq
->n_col
)
11842 isl_die(dim
->ctx
, isl_error_invalid
,
11843 "equalities and inequalities matrices should have "
11844 "same number of columns", goto error
);
11846 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11848 if (eq
->n_col
< total
)
11849 isl_die(dim
->ctx
, isl_error_invalid
,
11850 "number of columns too small", goto error
);
11852 extra
= eq
->n_col
- total
;
11854 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11855 eq
->n_row
, ineq
->n_row
);
11858 for (i
= 0; i
< extra
; ++i
) {
11859 k
= isl_basic_map_alloc_div(bmap
);
11862 isl_int_set_si(bmap
->div
[k
][0], 0);
11864 for (i
= 0; i
< eq
->n_row
; ++i
) {
11865 l
= isl_basic_map_alloc_equality(bmap
);
11868 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11869 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11870 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11871 isl_int_set(bmap
->eq
[l
][off
+ k
],
11877 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11878 l
= isl_basic_map_alloc_inequality(bmap
);
11881 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11882 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11883 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11884 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11885 ineq
->row
[i
][pos
]);
11891 isl_space_free(dim
);
11893 isl_mat_free(ineq
);
11895 bmap
= isl_basic_map_simplify(bmap
);
11896 return isl_basic_map_finalize(bmap
);
11898 isl_space_free(dim
);
11900 isl_mat_free(ineq
);
11904 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11905 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11906 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11908 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11909 c1
, c2
, c3
, c4
, isl_dim_in
);
11912 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11913 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11914 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11916 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11917 c1
, c2
, c3
, c4
, isl_dim_in
);
11920 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11921 __isl_take isl_space
*dim
,
11922 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11923 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11925 isl_basic_map
*bmap
;
11926 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11927 c1
, c2
, c3
, c4
, isl_dim_in
);
11928 return bset_from_bmap(bmap
);
11931 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11934 return isl_bool_error
;
11936 return isl_space_can_zip(bmap
->dim
);
11939 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11942 return isl_bool_error
;
11944 return isl_space_can_zip(map
->dim
);
11947 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11948 * (A -> C) -> (B -> D).
11950 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11959 if (!isl_basic_map_can_zip(bmap
))
11960 isl_die(bmap
->ctx
, isl_error_invalid
,
11961 "basic map cannot be zipped", goto error
);
11962 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11963 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11964 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11965 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11966 bmap
= isl_basic_map_cow(bmap
);
11967 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11970 bmap
->dim
= isl_space_zip(bmap
->dim
);
11973 bmap
= isl_basic_map_mark_final(bmap
);
11976 isl_basic_map_free(bmap
);
11980 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11981 * (A -> C) -> (B -> D).
11983 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11990 if (!isl_map_can_zip(map
))
11991 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11994 map
= isl_map_cow(map
);
11998 for (i
= 0; i
< map
->n
; ++i
) {
11999 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12004 map
->dim
= isl_space_zip(map
->dim
);
12014 /* Can we apply isl_basic_map_curry to "bmap"?
12015 * That is, does it have a nested relation in its domain?
12017 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12020 return isl_bool_error
;
12022 return isl_space_can_curry(bmap
->dim
);
12025 /* Can we apply isl_map_curry to "map"?
12026 * That is, does it have a nested relation in its domain?
12028 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12031 return isl_bool_error
;
12033 return isl_space_can_curry(map
->dim
);
12036 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12039 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12045 if (!isl_basic_map_can_curry(bmap
))
12046 isl_die(bmap
->ctx
, isl_error_invalid
,
12047 "basic map cannot be curried", goto error
);
12048 bmap
= isl_basic_map_cow(bmap
);
12051 bmap
->dim
= isl_space_curry(bmap
->dim
);
12054 bmap
= isl_basic_map_mark_final(bmap
);
12057 isl_basic_map_free(bmap
);
12061 /* Given a map (A -> B) -> C, return the corresponding map
12064 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12066 return isl_map_change_space(map
, &isl_map_can_curry
,
12067 "map cannot be curried", &isl_space_curry
);
12070 /* Can isl_map_range_curry be applied to "map"?
12071 * That is, does it have a nested relation in its range,
12072 * the domain of which is itself a nested relation?
12074 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12077 return isl_bool_error
;
12079 return isl_space_can_range_curry(map
->dim
);
12082 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12083 * A -> (B -> (C -> D)).
12085 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12087 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12088 "map range cannot be curried",
12089 &isl_space_range_curry
);
12092 /* Can we apply isl_basic_map_uncurry to "bmap"?
12093 * That is, does it have a nested relation in its domain?
12095 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12098 return isl_bool_error
;
12100 return isl_space_can_uncurry(bmap
->dim
);
12103 /* Can we apply isl_map_uncurry to "map"?
12104 * That is, does it have a nested relation in its domain?
12106 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12109 return isl_bool_error
;
12111 return isl_space_can_uncurry(map
->dim
);
12114 /* Given a basic map A -> (B -> C), return the corresponding basic map
12117 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12123 if (!isl_basic_map_can_uncurry(bmap
))
12124 isl_die(bmap
->ctx
, isl_error_invalid
,
12125 "basic map cannot be uncurried",
12126 return isl_basic_map_free(bmap
));
12127 bmap
= isl_basic_map_cow(bmap
);
12130 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12132 return isl_basic_map_free(bmap
);
12133 bmap
= isl_basic_map_mark_final(bmap
);
12137 /* Given a map A -> (B -> C), return the corresponding map
12140 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12142 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12143 "map cannot be uncurried", &isl_space_uncurry
);
12146 /* Construct a basic map mapping the domain of the affine expression
12147 * to a one-dimensional range prescribed by the affine expression.
12148 * If "rational" is set, then construct a rational basic map.
12150 * A NaN affine expression cannot be converted to a basic map.
12152 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12153 __isl_take isl_aff
*aff
, int rational
)
12158 isl_local_space
*ls
;
12159 isl_basic_map
*bmap
= NULL
;
12163 is_nan
= isl_aff_is_nan(aff
);
12167 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12168 "cannot convert NaN", goto error
);
12170 ls
= isl_aff_get_local_space(aff
);
12171 bmap
= isl_basic_map_from_local_space(ls
);
12172 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12173 k
= isl_basic_map_alloc_equality(bmap
);
12177 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12178 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12179 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12180 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12181 aff
->v
->size
- (pos
+ 1));
12185 bmap
= isl_basic_map_set_rational(bmap
);
12186 bmap
= isl_basic_map_gauss(bmap
, NULL
);
12187 bmap
= isl_basic_map_finalize(bmap
);
12191 isl_basic_map_free(bmap
);
12195 /* Construct a basic map mapping the domain of the affine expression
12196 * to a one-dimensional range prescribed by the affine expression.
12198 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12200 return isl_basic_map_from_aff2(aff
, 0);
12203 /* Construct a map mapping the domain of the affine expression
12204 * to a one-dimensional range prescribed by the affine expression.
12206 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12208 isl_basic_map
*bmap
;
12210 bmap
= isl_basic_map_from_aff(aff
);
12211 return isl_map_from_basic_map(bmap
);
12214 /* Construct a basic map mapping the domain the multi-affine expression
12215 * to its range, with each dimension in the range equated to the
12216 * corresponding affine expression.
12217 * If "rational" is set, then construct a rational basic map.
12219 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12220 __isl_take isl_multi_aff
*maff
, int rational
)
12224 isl_basic_map
*bmap
;
12229 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12230 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12231 "invalid space", goto error
);
12233 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12234 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12236 bmap
= isl_basic_map_set_rational(bmap
);
12238 for (i
= 0; i
< maff
->n
; ++i
) {
12240 isl_basic_map
*bmap_i
;
12242 aff
= isl_aff_copy(maff
->p
[i
]);
12243 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12245 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12248 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12250 isl_multi_aff_free(maff
);
12253 isl_multi_aff_free(maff
);
12257 /* Construct a basic map mapping the domain the multi-affine expression
12258 * to its range, with each dimension in the range equated to the
12259 * corresponding affine expression.
12261 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12262 __isl_take isl_multi_aff
*ma
)
12264 return isl_basic_map_from_multi_aff2(ma
, 0);
12267 /* Construct a map mapping the domain the multi-affine expression
12268 * to its range, with each dimension in the range equated to the
12269 * corresponding affine expression.
12271 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12273 isl_basic_map
*bmap
;
12275 bmap
= isl_basic_map_from_multi_aff(maff
);
12276 return isl_map_from_basic_map(bmap
);
12279 /* Construct a basic map mapping a domain in the given space to
12280 * to an n-dimensional range, with n the number of elements in the list,
12281 * where each coordinate in the range is prescribed by the
12282 * corresponding affine expression.
12283 * The domains of all affine expressions in the list are assumed to match
12286 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12287 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12291 isl_basic_map
*bmap
;
12296 dim
= isl_space_from_domain(domain_dim
);
12297 bmap
= isl_basic_map_universe(dim
);
12299 for (i
= 0; i
< list
->n
; ++i
) {
12301 isl_basic_map
*bmap_i
;
12303 aff
= isl_aff_copy(list
->p
[i
]);
12304 bmap_i
= isl_basic_map_from_aff(aff
);
12306 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12309 isl_aff_list_free(list
);
12313 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12314 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12316 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12319 /* Construct a basic map where the given dimensions are equal to each other.
12321 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12322 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12324 isl_basic_map
*bmap
= NULL
;
12330 if (pos1
>= isl_space_dim(space
, type1
))
12331 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12332 "index out of bounds", goto error
);
12333 if (pos2
>= isl_space_dim(space
, type2
))
12334 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12335 "index out of bounds", goto error
);
12337 if (type1
== type2
&& pos1
== pos2
)
12338 return isl_basic_map_universe(space
);
12340 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12341 i
= isl_basic_map_alloc_equality(bmap
);
12344 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12345 pos1
+= isl_basic_map_offset(bmap
, type1
);
12346 pos2
+= isl_basic_map_offset(bmap
, type2
);
12347 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12348 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12349 bmap
= isl_basic_map_finalize(bmap
);
12350 isl_space_free(space
);
12353 isl_space_free(space
);
12354 isl_basic_map_free(bmap
);
12358 /* Add a constraint imposing that the given two dimensions are equal.
12360 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12361 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12365 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12367 bmap
= isl_basic_map_intersect(bmap
, eq
);
12372 /* Add a constraint imposing that the given two dimensions are equal.
12374 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12375 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12377 isl_basic_map
*bmap
;
12379 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12381 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12386 /* Add a constraint imposing that the given two dimensions have opposite values.
12388 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12389 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12391 isl_basic_map
*bmap
= NULL
;
12397 if (pos1
>= isl_map_dim(map
, type1
))
12398 isl_die(map
->ctx
, isl_error_invalid
,
12399 "index out of bounds", goto error
);
12400 if (pos2
>= isl_map_dim(map
, type2
))
12401 isl_die(map
->ctx
, isl_error_invalid
,
12402 "index out of bounds", goto error
);
12404 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12405 i
= isl_basic_map_alloc_equality(bmap
);
12408 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12409 pos1
+= isl_basic_map_offset(bmap
, type1
);
12410 pos2
+= isl_basic_map_offset(bmap
, type2
);
12411 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12412 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12413 bmap
= isl_basic_map_finalize(bmap
);
12415 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12419 isl_basic_map_free(bmap
);
12424 /* Construct a constraint imposing that the value of the first dimension is
12425 * greater than or equal to that of the second.
12427 static __isl_give isl_constraint
*constraint_order_ge(
12428 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12429 enum isl_dim_type type2
, int pos2
)
12436 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12438 if (pos1
>= isl_constraint_dim(c
, type1
))
12439 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12440 "index out of bounds", return isl_constraint_free(c
));
12441 if (pos2
>= isl_constraint_dim(c
, type2
))
12442 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12443 "index out of bounds", return isl_constraint_free(c
));
12445 if (type1
== type2
&& pos1
== pos2
)
12448 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12449 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12454 /* Add a constraint imposing that the value of the first dimension is
12455 * greater than or equal to that of the second.
12457 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12458 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12463 if (type1
== type2
&& pos1
== pos2
)
12465 space
= isl_basic_map_get_space(bmap
);
12466 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12467 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12472 /* Add a constraint imposing that the value of the first dimension is
12473 * greater than or equal to that of the second.
12475 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12476 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12481 if (type1
== type2
&& pos1
== pos2
)
12483 space
= isl_map_get_space(map
);
12484 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12485 map
= isl_map_add_constraint(map
, c
);
12490 /* Add a constraint imposing that the value of the first dimension is
12491 * less than or equal to that of the second.
12493 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12494 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12496 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12499 /* Construct a basic map where the value of the first dimension is
12500 * greater than that of the second.
12502 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12503 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12505 isl_basic_map
*bmap
= NULL
;
12511 if (pos1
>= isl_space_dim(space
, type1
))
12512 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12513 "index out of bounds", goto error
);
12514 if (pos2
>= isl_space_dim(space
, type2
))
12515 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12516 "index out of bounds", goto error
);
12518 if (type1
== type2
&& pos1
== pos2
)
12519 return isl_basic_map_empty(space
);
12521 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12522 i
= isl_basic_map_alloc_inequality(bmap
);
12524 return isl_basic_map_free(bmap
);
12525 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12526 pos1
+= isl_basic_map_offset(bmap
, type1
);
12527 pos2
+= isl_basic_map_offset(bmap
, type2
);
12528 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12529 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12530 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12531 bmap
= isl_basic_map_finalize(bmap
);
12535 isl_space_free(space
);
12536 isl_basic_map_free(bmap
);
12540 /* Add a constraint imposing that the value of the first dimension is
12541 * greater than that of the second.
12543 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12544 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12548 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12550 bmap
= isl_basic_map_intersect(bmap
, gt
);
12555 /* Add a constraint imposing that the value of the first dimension is
12556 * greater than that of the second.
12558 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12559 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12561 isl_basic_map
*bmap
;
12563 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12565 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12570 /* Add a constraint imposing that the value of the first dimension is
12571 * smaller than that of the second.
12573 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12574 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12576 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12579 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12583 isl_local_space
*ls
;
12588 if (!isl_basic_map_divs_known(bmap
))
12589 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12590 "some divs are unknown", return NULL
);
12592 ls
= isl_basic_map_get_local_space(bmap
);
12593 div
= isl_local_space_get_div(ls
, pos
);
12594 isl_local_space_free(ls
);
12599 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12602 return isl_basic_map_get_div(bset
, pos
);
12605 /* Plug in "subs" for dimension "type", "pos" of "bset".
12607 * Let i be the dimension to replace and let "subs" be of the form
12611 * Any integer division with a non-zero coefficient for i,
12613 * floor((a i + g)/m)
12617 * floor((a f + d g)/(m d))
12619 * Constraints of the form
12627 * We currently require that "subs" is an integral expression.
12628 * Handling rational expressions may require us to add stride constraints
12629 * as we do in isl_basic_set_preimage_multi_aff.
12631 __isl_give isl_basic_set
*isl_basic_set_substitute(
12632 __isl_take isl_basic_set
*bset
,
12633 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12639 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12642 bset
= isl_basic_set_cow(bset
);
12643 if (!bset
|| !subs
)
12646 ctx
= isl_basic_set_get_ctx(bset
);
12647 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12648 isl_die(ctx
, isl_error_invalid
,
12649 "spaces don't match", goto error
);
12650 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12651 isl_die(ctx
, isl_error_unsupported
,
12652 "cannot handle divs yet", goto error
);
12653 if (!isl_int_is_one(subs
->v
->el
[0]))
12654 isl_die(ctx
, isl_error_invalid
,
12655 "can only substitute integer expressions", goto error
);
12657 pos
+= isl_basic_set_offset(bset
, type
);
12661 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12662 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12664 isl_int_set(v
, bset
->eq
[i
][pos
]);
12665 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12666 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12667 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12670 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12671 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12673 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12674 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12675 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12676 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12679 for (i
= 0; i
< bset
->n_div
; ++i
) {
12680 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12682 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12683 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12684 isl_seq_combine(bset
->div
[i
] + 1,
12685 subs
->v
->el
[0], bset
->div
[i
] + 1,
12686 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12687 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12692 bset
= isl_basic_set_simplify(bset
);
12693 return isl_basic_set_finalize(bset
);
12695 isl_basic_set_free(bset
);
12699 /* Plug in "subs" for dimension "type", "pos" of "set".
12701 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12702 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12706 if (set
&& isl_set_plain_is_empty(set
))
12709 set
= isl_set_cow(set
);
12713 for (i
= set
->n
- 1; i
>= 0; --i
) {
12714 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12715 if (remove_if_empty(set
, i
) < 0)
12725 /* Check if the range of "ma" is compatible with the domain or range
12726 * (depending on "type") of "bmap".
12728 static isl_stat
check_basic_map_compatible_range_multi_aff(
12729 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12730 __isl_keep isl_multi_aff
*ma
)
12733 isl_space
*ma_space
;
12735 ma_space
= isl_multi_aff_get_space(ma
);
12737 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12741 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12742 "parameters don't match", goto error
);
12743 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12747 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12748 "spaces don't match", goto error
);
12750 isl_space_free(ma_space
);
12751 return isl_stat_ok
;
12753 isl_space_free(ma_space
);
12754 return isl_stat_error
;
12757 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12758 * coefficients before the transformed range of dimensions,
12759 * the "n_after" coefficients after the transformed range of dimensions
12760 * and the coefficients of the other divs in "bmap".
12762 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12763 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12768 isl_local_space
*ls
;
12773 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12777 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12778 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12779 for (i
= 0; i
< n_div
; ++i
) {
12780 int o_bmap
= 0, o_ls
= 0;
12782 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12783 o_bmap
+= 1 + 1 + n_param
;
12784 o_ls
+= 1 + 1 + n_param
;
12785 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12786 o_bmap
+= n_before
;
12787 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12788 ls
->div
->row
[i
] + o_ls
, n_set
);
12791 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12793 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12794 ls
->div
->row
[i
] + o_ls
, n_div
);
12797 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12798 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12802 isl_local_space_free(ls
);
12805 isl_local_space_free(ls
);
12809 /* How many stride constraints does "ma" enforce?
12810 * That is, how many of the affine expressions have a denominator
12811 * different from one?
12813 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12818 for (i
= 0; i
< ma
->n
; ++i
)
12819 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12825 /* For each affine expression in ma of the form
12827 * x_i = (f_i y + h_i)/m_i
12829 * with m_i different from one, add a constraint to "bmap"
12832 * f_i y + h_i = m_i alpha_i
12834 * with alpha_i an additional existentially quantified variable.
12836 * The input variables of "ma" correspond to a subset of the variables
12837 * of "bmap". There are "n_before" variables in "bmap" before this
12838 * subset and "n_after" variables after this subset.
12839 * The integer divisions of the affine expressions in "ma" are assumed
12840 * to have been aligned. There are "n_div_ma" of them and
12841 * they appear first in "bmap", straight after the "n_after" variables.
12843 static __isl_give isl_basic_map
*add_ma_strides(
12844 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12845 int n_before
, int n_after
, int n_div_ma
)
12853 total
= isl_basic_map_total_dim(bmap
);
12854 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12855 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12856 for (i
= 0; i
< ma
->n
; ++i
) {
12857 int o_bmap
= 0, o_ma
= 1;
12859 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12861 div
= isl_basic_map_alloc_div(bmap
);
12862 k
= isl_basic_map_alloc_equality(bmap
);
12863 if (div
< 0 || k
< 0)
12865 isl_int_set_si(bmap
->div
[div
][0], 0);
12866 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12867 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12868 o_bmap
+= 1 + n_param
;
12869 o_ma
+= 1 + n_param
;
12870 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12871 o_bmap
+= n_before
;
12872 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12873 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12876 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12878 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12879 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12880 o_bmap
+= n_div_ma
;
12882 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12883 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12889 isl_basic_map_free(bmap
);
12893 /* Replace the domain or range space (depending on "type) of "space" by "set".
12895 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12896 enum isl_dim_type type
, __isl_take isl_space
*set
)
12898 if (type
== isl_dim_in
) {
12899 space
= isl_space_range(space
);
12900 space
= isl_space_map_from_domain_and_range(set
, space
);
12902 space
= isl_space_domain(space
);
12903 space
= isl_space_map_from_domain_and_range(space
, set
);
12909 /* Compute the preimage of the domain or range (depending on "type")
12910 * of "bmap" under the function represented by "ma".
12911 * In other words, plug in "ma" in the domain or range of "bmap".
12912 * The result is a basic map that lives in the same space as "bmap"
12913 * except that the domain or range has been replaced by
12914 * the domain space of "ma".
12916 * If bmap is represented by
12918 * A(p) + S u + B x + T v + C(divs) >= 0,
12920 * where u and x are input and output dimensions if type == isl_dim_out
12921 * while x and v are input and output dimensions if type == isl_dim_in,
12922 * and ma is represented by
12924 * x = D(p) + F(y) + G(divs')
12926 * then the result is
12928 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12930 * The divs in the input set are similarly adjusted.
12933 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12937 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12938 * B_i G(divs') + c_i(divs))/n_i)
12940 * If bmap is not a rational map and if F(y) involves any denominators
12942 * x_i = (f_i y + h_i)/m_i
12944 * then additional constraints are added to ensure that we only
12945 * map back integer points. That is we enforce
12947 * f_i y + h_i = m_i alpha_i
12949 * with alpha_i an additional existentially quantified variable.
12951 * We first copy over the divs from "ma".
12952 * Then we add the modified constraints and divs from "bmap".
12953 * Finally, we add the stride constraints, if needed.
12955 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12956 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12957 __isl_take isl_multi_aff
*ma
)
12961 isl_basic_map
*res
= NULL
;
12962 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12963 isl_int f
, c1
, c2
, g
;
12972 ma
= isl_multi_aff_align_divs(ma
);
12975 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12978 if (type
== isl_dim_in
) {
12980 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12982 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12985 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12986 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12988 space
= isl_multi_aff_get_domain_space(ma
);
12989 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12990 rational
= isl_basic_map_is_rational(bmap
);
12991 strides
= rational
? 0 : multi_aff_strides(ma
);
12992 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12993 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12995 res
= isl_basic_map_set_rational(res
);
12997 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12998 if (isl_basic_map_alloc_div(res
) < 0)
13001 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
13004 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13005 k
= isl_basic_map_alloc_equality(res
);
13008 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13009 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13012 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13013 k
= isl_basic_map_alloc_inequality(res
);
13016 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13017 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13020 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13021 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13022 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13025 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13026 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13031 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13037 isl_basic_map_free(bmap
);
13038 isl_multi_aff_free(ma
);
13039 res
= isl_basic_map_simplify(res
);
13040 return isl_basic_map_finalize(res
);
13046 isl_basic_map_free(bmap
);
13047 isl_multi_aff_free(ma
);
13048 isl_basic_map_free(res
);
13052 /* Compute the preimage of "bset" under the function represented by "ma".
13053 * In other words, plug in "ma" in "bset". The result is a basic set
13054 * that lives in the domain space of "ma".
13056 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13057 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13059 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13062 /* Compute the preimage of the domain of "bmap" under the function
13063 * represented by "ma".
13064 * In other words, plug in "ma" in the domain of "bmap".
13065 * The result is a basic map that lives in the same space as "bmap"
13066 * except that the domain has been replaced by the domain space of "ma".
13068 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13069 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13071 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13074 /* Compute the preimage of the range of "bmap" under the function
13075 * represented by "ma".
13076 * In other words, plug in "ma" in the range of "bmap".
13077 * The result is a basic map that lives in the same space as "bmap"
13078 * except that the range has been replaced by the domain space of "ma".
13080 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13081 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13083 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13086 /* Check if the range of "ma" is compatible with the domain or range
13087 * (depending on "type") of "map".
13088 * Return isl_stat_error if anything is wrong.
13090 static isl_stat
check_map_compatible_range_multi_aff(
13091 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13092 __isl_keep isl_multi_aff
*ma
)
13095 isl_space
*ma_space
;
13097 ma_space
= isl_multi_aff_get_space(ma
);
13098 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13099 isl_space_free(ma_space
);
13101 return isl_stat_error
;
13103 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13104 "spaces don't match", return isl_stat_error
);
13105 return isl_stat_ok
;
13108 /* Compute the preimage of the domain or range (depending on "type")
13109 * of "map" under the function represented by "ma".
13110 * In other words, plug in "ma" in the domain or range of "map".
13111 * The result is a map that lives in the same space as "map"
13112 * except that the domain or range has been replaced by
13113 * the domain space of "ma".
13115 * The parameters are assumed to have been aligned.
13117 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13118 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13123 map
= isl_map_cow(map
);
13124 ma
= isl_multi_aff_align_divs(ma
);
13127 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13130 for (i
= 0; i
< map
->n
; ++i
) {
13131 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13132 isl_multi_aff_copy(ma
));
13137 space
= isl_multi_aff_get_domain_space(ma
);
13138 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13140 isl_space_free(map
->dim
);
13145 isl_multi_aff_free(ma
);
13147 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13148 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13151 isl_multi_aff_free(ma
);
13156 /* Compute the preimage of the domain or range (depending on "type")
13157 * of "map" under the function represented by "ma".
13158 * In other words, plug in "ma" in the domain or range of "map".
13159 * The result is a map that lives in the same space as "map"
13160 * except that the domain or range has been replaced by
13161 * the domain space of "ma".
13163 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13164 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13171 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13175 return map_preimage_multi_aff(map
, type
, ma
);
13177 if (isl_map_check_named_params(map
) < 0)
13179 if (!isl_space_has_named_params(ma
->space
))
13180 isl_die(map
->ctx
, isl_error_invalid
,
13181 "unaligned unnamed parameters", goto error
);
13182 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13183 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13185 return map_preimage_multi_aff(map
, type
, ma
);
13187 isl_multi_aff_free(ma
);
13188 return isl_map_free(map
);
13191 /* Compute the preimage of "set" under the function represented by "ma".
13192 * In other words, plug in "ma" in "set". The result is a set
13193 * that lives in the domain space of "ma".
13195 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13196 __isl_take isl_multi_aff
*ma
)
13198 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13201 /* Compute the preimage of the domain of "map" under the function
13202 * represented by "ma".
13203 * In other words, plug in "ma" in the domain of "map".
13204 * The result is a map that lives in the same space as "map"
13205 * except that the domain has been replaced by the domain space of "ma".
13207 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13208 __isl_take isl_multi_aff
*ma
)
13210 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13213 /* Compute the preimage of the range of "map" under the function
13214 * represented by "ma".
13215 * In other words, plug in "ma" in the range of "map".
13216 * The result is a map that lives in the same space as "map"
13217 * except that the range has been replaced by the domain space of "ma".
13219 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13220 __isl_take isl_multi_aff
*ma
)
13222 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13225 /* Compute the preimage of "map" under the function represented by "pma".
13226 * In other words, plug in "pma" in the domain or range of "map".
13227 * The result is a map that lives in the same space as "map",
13228 * except that the space of type "type" has been replaced by
13229 * the domain space of "pma".
13231 * The parameters of "map" and "pma" are assumed to have been aligned.
13233 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13234 __isl_take isl_map
*map
, enum isl_dim_type type
,
13235 __isl_take isl_pw_multi_aff
*pma
)
13244 isl_pw_multi_aff_free(pma
);
13245 res
= isl_map_empty(isl_map_get_space(map
));
13250 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13251 isl_multi_aff_copy(pma
->p
[0].maff
));
13252 if (type
== isl_dim_in
)
13253 res
= isl_map_intersect_domain(res
,
13254 isl_map_copy(pma
->p
[0].set
));
13256 res
= isl_map_intersect_range(res
,
13257 isl_map_copy(pma
->p
[0].set
));
13259 for (i
= 1; i
< pma
->n
; ++i
) {
13262 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13263 isl_multi_aff_copy(pma
->p
[i
].maff
));
13264 if (type
== isl_dim_in
)
13265 res_i
= isl_map_intersect_domain(res_i
,
13266 isl_map_copy(pma
->p
[i
].set
));
13268 res_i
= isl_map_intersect_range(res_i
,
13269 isl_map_copy(pma
->p
[i
].set
));
13270 res
= isl_map_union(res
, res_i
);
13273 isl_pw_multi_aff_free(pma
);
13277 isl_pw_multi_aff_free(pma
);
13282 /* Compute the preimage of "map" under the function represented by "pma".
13283 * In other words, plug in "pma" in the domain or range of "map".
13284 * The result is a map that lives in the same space as "map",
13285 * except that the space of type "type" has been replaced by
13286 * the domain space of "pma".
13288 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13289 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13296 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13300 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13302 if (isl_map_check_named_params(map
) < 0)
13304 if (!isl_space_has_named_params(pma
->dim
))
13305 isl_die(map
->ctx
, isl_error_invalid
,
13306 "unaligned unnamed parameters", goto error
);
13307 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13308 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13310 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13312 isl_pw_multi_aff_free(pma
);
13313 return isl_map_free(map
);
13316 /* Compute the preimage of "set" under the function represented by "pma".
13317 * In other words, plug in "pma" in "set". The result is a set
13318 * that lives in the domain space of "pma".
13320 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13321 __isl_take isl_pw_multi_aff
*pma
)
13323 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13326 /* Compute the preimage of the domain of "map" under the function
13327 * represented by "pma".
13328 * In other words, plug in "pma" in the domain of "map".
13329 * The result is a map that lives in the same space as "map",
13330 * except that domain space has been replaced by the domain space of "pma".
13332 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13333 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13335 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13338 /* Compute the preimage of the range of "map" under the function
13339 * represented by "pma".
13340 * In other words, plug in "pma" in the range of "map".
13341 * The result is a map that lives in the same space as "map",
13342 * except that range space has been replaced by the domain space of "pma".
13344 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13345 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13347 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13350 /* Compute the preimage of "map" under the function represented by "mpa".
13351 * In other words, plug in "mpa" in the domain or range of "map".
13352 * The result is a map that lives in the same space as "map",
13353 * except that the space of type "type" has been replaced by
13354 * the domain space of "mpa".
13356 * If the map does not involve any constraints that refer to the
13357 * dimensions of the substituted space, then the only possible
13358 * effect of "mpa" on the map is to map the space to a different space.
13359 * We create a separate isl_multi_aff to effectuate this change
13360 * in order to avoid spurious splitting of the map along the pieces
13363 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13364 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13367 isl_pw_multi_aff
*pma
;
13372 n
= isl_map_dim(map
, type
);
13373 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13377 space
= isl_multi_pw_aff_get_space(mpa
);
13378 isl_multi_pw_aff_free(mpa
);
13379 ma
= isl_multi_aff_zero(space
);
13380 return isl_map_preimage_multi_aff(map
, type
, ma
);
13383 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13384 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13387 isl_multi_pw_aff_free(mpa
);
13391 /* Compute the preimage of "map" under the function represented by "mpa".
13392 * In other words, plug in "mpa" in the domain "map".
13393 * The result is a map that lives in the same space as "map",
13394 * except that domain space has been replaced by the domain space of "mpa".
13396 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13397 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13399 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13402 /* Compute the preimage of "set" by the function represented by "mpa".
13403 * In other words, plug in "mpa" in "set".
13405 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13406 __isl_take isl_multi_pw_aff
*mpa
)
13408 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13411 /* Return a copy of the equality constraints of "bset" as a matrix.
13413 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13414 __isl_keep isl_basic_set
*bset
)
13422 ctx
= isl_basic_set_get_ctx(bset
);
13423 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13424 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13427 /* Are the "n" "coefficients" starting at "first" of the integer division
13428 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13430 * The "coefficient" at position 0 is the denominator.
13431 * The "coefficient" at position 1 is the constant term.
13433 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13434 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13435 unsigned first
, unsigned n
)
13437 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13438 return isl_bool_error
;
13439 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13440 return isl_bool_error
;
13441 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13442 bmap2
->div
[pos2
] + first
, n
);
13445 /* Are the integer division expressions at position "pos1" in "bmap1" and
13446 * "pos2" in "bmap2" equal to each other, except that the constant terms
13449 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13450 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13451 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13456 if (!bmap1
|| !bmap2
)
13457 return isl_bool_error
;
13458 total
= isl_basic_map_total_dim(bmap1
);
13459 if (total
!= isl_basic_map_total_dim(bmap2
))
13460 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13461 "incomparable div expressions", return isl_bool_error
);
13462 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13464 if (equal
< 0 || !equal
)
13466 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13468 if (equal
< 0 || equal
)
13469 return isl_bool_not(equal
);
13470 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13474 /* Replace the numerator of the constant term of the integer division
13475 * expression at position "div" in "bmap" by "value".
13476 * The caller guarantees that this does not change the meaning
13479 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13480 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13482 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13483 return isl_basic_map_free(bmap
);
13485 isl_int_set_si(bmap
->div
[div
][1], value
);
13490 /* Is the point "inner" internal to inequality constraint "ineq"
13492 * The point is considered to be internal to the inequality constraint,
13493 * if it strictly lies on the positive side of the inequality constraint,
13494 * or if it lies on the constraint and the constraint is lexico-positive.
13496 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13497 __isl_keep isl_basic_set
*bset
, int ineq
)
13503 if (!inner
|| !bset
)
13504 return isl_bool_error
;
13506 ctx
= isl_basic_set_get_ctx(bset
);
13507 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13508 &ctx
->normalize_gcd
);
13509 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13510 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13512 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13513 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13514 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13517 /* Tighten the inequality constraints of "bset" that are outward with respect
13518 * to the point "vec".
13519 * That is, tighten the constraints that are not satisfied by "vec".
13521 * "vec" is a point internal to some superset S of "bset" that is used
13522 * to make the subsets of S disjoint, by tightening one half of the constraints
13523 * that separate two subsets. In particular, the constraints of S
13524 * are all satisfied by "vec" and should not be tightened.
13525 * Of the internal constraints, those that have "vec" on the outside
13526 * are tightened. The shared facet is included in the adjacent subset
13527 * with the opposite constraint.
13528 * For constraints that saturate "vec", this criterion cannot be used
13529 * to determine which of the two sides should be tightened.
13530 * Instead, the sign of the first non-zero coefficient is used
13531 * to make this choice. Note that this second criterion is never used
13532 * on the constraints of S since "vec" is interior to "S".
13534 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13535 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13539 bset
= isl_basic_set_cow(bset
);
13542 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13545 internal
= is_internal(vec
, bset
, j
);
13547 return isl_basic_set_free(bset
);
13550 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13556 /* Replace the variables x of type "type" starting at "first" in "bmap"
13557 * by x' with x = M x' with M the matrix trans.
13558 * That is, replace the corresponding coefficients c by c M.
13560 * The transformation matrix should be a square matrix.
13562 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13563 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13564 __isl_take isl_mat
*trans
)
13568 bmap
= isl_basic_map_cow(bmap
);
13569 if (!bmap
|| !trans
)
13572 if (trans
->n_row
!= trans
->n_col
)
13573 isl_die(trans
->ctx
, isl_error_invalid
,
13574 "expecting square transformation matrix", goto error
);
13575 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13576 isl_die(trans
->ctx
, isl_error_invalid
,
13577 "oversized transformation matrix", goto error
);
13579 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13581 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13582 isl_mat_copy(trans
)) < 0)
13584 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13585 isl_mat_copy(trans
)) < 0)
13587 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13588 isl_mat_copy(trans
)) < 0)
13591 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
13592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13594 isl_mat_free(trans
);
13597 isl_mat_free(trans
);
13598 isl_basic_map_free(bmap
);
13602 /* Replace the variables x of type "type" starting at "first" in "bset"
13603 * by x' with x = M x' with M the matrix trans.
13604 * That is, replace the corresponding coefficients c by c M.
13606 * The transformation matrix should be a square matrix.
13608 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13609 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13610 __isl_take isl_mat
*trans
)
13612 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);