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
*dim
)
5929 struct isl_basic_map
*bmap
;
5930 bmap
= isl_basic_map_alloc_space(dim
, 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
*dim
)
5937 struct isl_basic_set
*bset
;
5938 bset
= isl_basic_set_alloc_space(dim
, 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
*dim
)
5945 struct isl_basic_map
*bmap
;
5946 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5947 bmap
= isl_basic_map_finalize(bmap
);
5951 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5953 struct isl_basic_set
*bset
;
5954 bset
= isl_basic_set_alloc_space(dim
, 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
*dim
)
5996 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5999 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
6001 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
6004 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
6006 struct isl_map
*map
;
6009 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
6010 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
6014 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
6016 struct isl_set
*set
;
6019 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
6020 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
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 /* Extract the first and only affine expression from list
6871 * and then add it to *pwaff with the given dom.
6872 * This domain is known to be disjoint from other domains
6873 * because of the way isl_basic_map_foreach_lexmax works.
6875 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6876 __isl_take isl_aff_list
*list
, void *user
)
6878 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6880 isl_pw_aff
**pwaff
= user
;
6881 isl_pw_aff
*pwaff_i
;
6885 if (isl_aff_list_n_aff(list
) != 1)
6886 isl_die(ctx
, isl_error_internal
,
6887 "expecting single element list", goto error
);
6889 aff
= isl_aff_list_get_aff(list
, 0);
6890 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6892 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6894 isl_aff_list_free(list
);
6898 isl_basic_set_free(dom
);
6899 isl_aff_list_free(list
);
6900 return isl_stat_error
;
6903 /* Given a basic map with one output dimension, compute the minimum or
6904 * maximum of that dimension as an isl_pw_aff.
6906 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6907 * call update_dim_opt on each leaf of the result.
6909 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6912 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6916 dim
= isl_space_from_domain(isl_space_domain(dim
));
6917 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6918 pwaff
= isl_pw_aff_empty(dim
);
6920 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6922 return isl_pw_aff_free(pwaff
);
6927 /* Compute the minimum or maximum of the given output dimension
6928 * as a function of the parameters and the input dimensions,
6929 * but independently of the other output dimensions.
6931 * We first project out the other output dimension and then compute
6932 * the "lexicographic" maximum in each basic map, combining the results
6933 * using isl_pw_aff_union_max.
6935 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6942 n_out
= isl_map_dim(map
, isl_dim_out
);
6943 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6944 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6949 isl_space
*dim
= isl_map_get_space(map
);
6951 return isl_pw_aff_empty(dim
);
6954 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6955 for (i
= 1; i
< map
->n
; ++i
) {
6956 isl_pw_aff
*pwaff_i
;
6958 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6959 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6967 /* Compute the minimum of the given output dimension as a function of the
6968 * parameters and input dimensions, but independently of
6969 * the other output dimensions.
6971 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6973 return map_dim_opt(map
, pos
, 0);
6976 /* Compute the maximum of the given output dimension as a function of the
6977 * parameters and input dimensions, but independently of
6978 * the other output dimensions.
6980 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6982 return map_dim_opt(map
, pos
, 1);
6985 /* Compute the minimum or maximum of the given set dimension
6986 * as a function of the parameters,
6987 * but independently of the other set dimensions.
6989 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6992 return map_dim_opt(set
, pos
, max
);
6995 /* Compute the maximum of the given set dimension as a function of the
6996 * parameters, but independently of the other set dimensions.
6998 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7000 return set_dim_opt(set
, pos
, 1);
7003 /* Compute the minimum of the given set dimension as a function of the
7004 * parameters, but independently of the other set dimensions.
7006 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7008 return set_dim_opt(set
, pos
, 0);
7011 /* Apply a preimage specified by "mat" on the parameters of "bset".
7012 * bset is assumed to have only parameters and divs.
7014 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7015 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7022 bset
->dim
= isl_space_cow(bset
->dim
);
7026 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7028 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7030 bset
->dim
->nparam
= 0;
7031 bset
->dim
->n_out
= nparam
;
7032 bset
= isl_basic_set_preimage(bset
, mat
);
7034 bset
->dim
->nparam
= bset
->dim
->n_out
;
7035 bset
->dim
->n_out
= 0;
7040 isl_basic_set_free(bset
);
7044 /* Apply a preimage specified by "mat" on the parameters of "set".
7045 * set is assumed to have only parameters and divs.
7047 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7048 __isl_take isl_mat
*mat
)
7056 nparam
= isl_set_dim(set
, isl_dim_param
);
7058 if (mat
->n_row
!= 1 + nparam
)
7059 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7060 "unexpected number of rows", goto error
);
7062 space
= isl_set_get_space(set
);
7063 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7064 isl_dim_param
, 0, nparam
);
7065 set
= isl_set_reset_space(set
, space
);
7066 set
= isl_set_preimage(set
, mat
);
7067 nparam
= isl_set_dim(set
, isl_dim_out
);
7068 space
= isl_set_get_space(set
);
7069 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7070 isl_dim_out
, 0, nparam
);
7071 set
= isl_set_reset_space(set
, space
);
7079 /* Intersect the basic set "bset" with the affine space specified by the
7080 * equalities in "eq".
7082 static __isl_give isl_basic_set
*basic_set_append_equalities(
7083 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7091 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7096 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7097 for (i
= 0; i
< eq
->n_row
; ++i
) {
7098 k
= isl_basic_set_alloc_equality(bset
);
7101 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7102 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7106 bset
= isl_basic_set_gauss(bset
, NULL
);
7107 bset
= isl_basic_set_finalize(bset
);
7112 isl_basic_set_free(bset
);
7116 /* Intersect the set "set" with the affine space specified by the
7117 * equalities in "eq".
7119 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7127 for (i
= 0; i
< set
->n
; ++i
) {
7128 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7141 /* Given a basic set "bset" that only involves parameters and existentially
7142 * quantified variables, return the index of the first equality
7143 * that only involves parameters. If there is no such equality then
7144 * return bset->n_eq.
7146 * This function assumes that isl_basic_set_gauss has been called on "bset".
7148 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7151 unsigned nparam
, n_div
;
7156 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7157 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7159 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7160 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7167 /* Compute an explicit representation for the existentially quantified
7168 * variables in "bset" by computing the "minimal value" of the set
7169 * variables. Since there are no set variables, the computation of
7170 * the minimal value essentially computes an explicit representation
7171 * of the non-empty part(s) of "bset".
7173 * The input only involves parameters and existentially quantified variables.
7174 * All equalities among parameters have been removed.
7176 * Since the existentially quantified variables in the result are in general
7177 * going to be different from those in the input, we first replace
7178 * them by the minimal number of variables based on their equalities.
7179 * This should simplify the parametric integer programming.
7181 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7183 isl_morph
*morph1
, *morph2
;
7189 if (bset
->n_eq
== 0)
7190 return isl_basic_set_lexmin_compute_divs(bset
);
7192 morph1
= isl_basic_set_parameter_compression(bset
);
7193 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7194 bset
= isl_basic_set_lift(bset
);
7195 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7196 bset
= isl_morph_basic_set(morph2
, bset
);
7197 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7198 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7200 set
= isl_basic_set_lexmin_compute_divs(bset
);
7202 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7207 /* Project the given basic set onto its parameter domain, possibly introducing
7208 * new, explicit, existential variables in the constraints.
7209 * The input has parameters and (possibly implicit) existential variables.
7210 * The output has the same parameters, but only
7211 * explicit existentially quantified variables.
7213 * The actual projection is performed by pip, but pip doesn't seem
7214 * to like equalities very much, so we first remove the equalities
7215 * among the parameters by performing a variable compression on
7216 * the parameters. Afterward, an inverse transformation is performed
7217 * and the equalities among the parameters are inserted back in.
7219 * The variable compression on the parameters may uncover additional
7220 * equalities that were only implicit before. We therefore check
7221 * if there are any new parameter equalities in the result and
7222 * if so recurse. The removal of parameter equalities is required
7223 * for the parameter compression performed by base_compute_divs.
7225 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7229 struct isl_mat
*T
, *T2
;
7230 struct isl_set
*set
;
7233 bset
= isl_basic_set_cow(bset
);
7237 if (bset
->n_eq
== 0)
7238 return base_compute_divs(bset
);
7240 bset
= isl_basic_set_gauss(bset
, NULL
);
7243 if (isl_basic_set_plain_is_empty(bset
))
7244 return isl_set_from_basic_set(bset
);
7246 i
= first_parameter_equality(bset
);
7247 if (i
== bset
->n_eq
)
7248 return base_compute_divs(bset
);
7250 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7251 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7253 eq
= isl_mat_cow(eq
);
7254 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7255 if (T
&& T
->n_col
== 0) {
7259 bset
= isl_basic_set_set_to_empty(bset
);
7260 return isl_set_from_basic_set(bset
);
7262 bset
= basic_set_parameter_preimage(bset
, T
);
7264 i
= first_parameter_equality(bset
);
7267 else if (i
== bset
->n_eq
)
7268 set
= base_compute_divs(bset
);
7270 set
= parameter_compute_divs(bset
);
7271 set
= set_parameter_preimage(set
, T2
);
7272 set
= set_append_equalities(set
, eq
);
7276 /* Insert the divs from "ls" before those of "bmap".
7278 * The number of columns is not changed, which means that the last
7279 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7280 * The caller is responsible for removing the same number of dimensions
7281 * from the space of "bmap".
7283 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7284 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7290 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7294 old_n_div
= bmap
->n_div
;
7295 bmap
= insert_div_rows(bmap
, n_div
);
7299 for (i
= 0; i
< n_div
; ++i
) {
7300 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7301 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7307 /* Replace the space of "bmap" by the space and divs of "ls".
7309 * If "ls" has any divs, then we simplify the result since we may
7310 * have discovered some additional equalities that could simplify
7311 * the div expressions.
7313 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7314 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7318 bmap
= isl_basic_map_cow(bmap
);
7322 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7323 bmap
= insert_divs_from_local_space(bmap
, ls
);
7327 isl_space_free(bmap
->dim
);
7328 bmap
->dim
= isl_local_space_get_space(ls
);
7332 isl_local_space_free(ls
);
7334 bmap
= isl_basic_map_simplify(bmap
);
7335 bmap
= isl_basic_map_finalize(bmap
);
7338 isl_basic_map_free(bmap
);
7339 isl_local_space_free(ls
);
7343 /* Replace the space of "map" by the space and divs of "ls".
7345 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7346 __isl_take isl_local_space
*ls
)
7350 map
= isl_map_cow(map
);
7354 for (i
= 0; i
< map
->n
; ++i
) {
7355 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7356 isl_local_space_copy(ls
));
7360 isl_space_free(map
->dim
);
7361 map
->dim
= isl_local_space_get_space(ls
);
7365 isl_local_space_free(ls
);
7368 isl_local_space_free(ls
);
7373 /* Compute an explicit representation for the existentially
7374 * quantified variables for which do not know any explicit representation yet.
7376 * We first sort the existentially quantified variables so that the
7377 * existentially quantified variables for which we already have an explicit
7378 * representation are placed before those for which we do not.
7379 * The input dimensions, the output dimensions and the existentially
7380 * quantified variables for which we already have an explicit
7381 * representation are then turned into parameters.
7382 * compute_divs returns a map with the same parameters and
7383 * no input or output dimensions and the dimension specification
7384 * is reset to that of the input, including the existentially quantified
7385 * variables for which we already had an explicit representation.
7387 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7389 struct isl_basic_set
*bset
;
7390 struct isl_set
*set
;
7391 struct isl_map
*map
;
7393 isl_local_space
*ls
;
7400 bmap
= isl_basic_map_sort_divs(bmap
);
7401 bmap
= isl_basic_map_cow(bmap
);
7405 n_known
= isl_basic_map_first_unknown_div(bmap
);
7407 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7409 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7410 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7411 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7412 dim
= isl_space_set_alloc(bmap
->ctx
,
7413 nparam
+ n_in
+ n_out
+ n_known
, 0);
7417 ls
= isl_basic_map_get_local_space(bmap
);
7418 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7419 n_known
, bmap
->n_div
- n_known
);
7421 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7422 swap_div(bmap
, i
- n_known
, i
);
7423 bmap
->n_div
-= n_known
;
7424 bmap
->extra
-= n_known
;
7426 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7427 bset
= bset_from_bmap(bmap
);
7429 set
= parameter_compute_divs(bset
);
7430 map
= set_to_map(set
);
7431 map
= replace_space_by_local_space(map
, ls
);
7435 isl_basic_map_free(bmap
);
7439 /* Remove the explicit representation of local variable "div",
7442 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7443 __isl_take isl_basic_map
*bmap
, int div
)
7447 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7449 return isl_basic_map_free(bmap
);
7453 bmap
= isl_basic_map_cow(bmap
);
7456 isl_int_set_si(bmap
->div
[div
][0], 0);
7460 /* Is local variable "div" of "bmap" marked as not having an explicit
7462 * Note that even if "div" is not marked in this way and therefore
7463 * has an explicit representation, this representation may still
7464 * depend (indirectly) on other local variables that do not
7465 * have an explicit representation.
7467 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7470 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7471 return isl_bool_error
;
7472 return isl_int_is_zero(bmap
->div
[div
][0]);
7475 /* Return the position of the first local variable that does not
7476 * have an explicit representation.
7477 * Return the total number of local variables if they all have
7478 * an explicit representation.
7479 * Return -1 on error.
7481 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7488 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7489 if (!isl_basic_map_div_is_known(bmap
, i
))
7495 /* Return the position of the first local variable that does not
7496 * have an explicit representation.
7497 * Return the total number of local variables if they all have
7498 * an explicit representation.
7499 * Return -1 on error.
7501 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7503 return isl_basic_map_first_unknown_div(bset
);
7506 /* Does "bmap" have an explicit representation for all local variables?
7508 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7512 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7513 first
= isl_basic_map_first_unknown_div(bmap
);
7515 return isl_bool_error
;
7519 /* Do all basic maps in "map" have an explicit representation
7520 * for all local variables?
7522 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7527 return isl_bool_error
;
7529 for (i
= 0; i
< map
->n
; ++i
) {
7530 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7535 return isl_bool_true
;
7538 /* If bmap contains any unknown divs, then compute explicit
7539 * expressions for them. However, this computation may be
7540 * quite expensive, so first try to remove divs that aren't
7543 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7546 struct isl_map
*map
;
7548 known
= isl_basic_map_divs_known(bmap
);
7552 return isl_map_from_basic_map(bmap
);
7554 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7556 known
= isl_basic_map_divs_known(bmap
);
7560 return isl_map_from_basic_map(bmap
);
7562 map
= compute_divs(bmap
);
7565 isl_basic_map_free(bmap
);
7569 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7573 struct isl_map
*res
;
7580 known
= isl_map_divs_known(map
);
7588 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7589 for (i
= 1 ; i
< map
->n
; ++i
) {
7591 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7592 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7593 res
= isl_map_union_disjoint(res
, r2
);
7595 res
= isl_map_union(res
, r2
);
7602 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7604 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7607 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7609 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7612 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7615 struct isl_set
*set
;
7620 map
= isl_map_cow(map
);
7624 set
= set_from_map(map
);
7625 set
->dim
= isl_space_domain(set
->dim
);
7628 for (i
= 0; i
< map
->n
; ++i
) {
7629 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7633 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7634 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7641 /* Return the union of "map1" and "map2", where we assume for now that
7642 * "map1" and "map2" are disjoint. Note that the basic maps inside
7643 * "map1" or "map2" may not be disjoint from each other.
7644 * Also note that this function is also called from isl_map_union,
7645 * which takes care of handling the situation where "map1" and "map2"
7646 * may not be disjoint.
7648 * If one of the inputs is empty, we can simply return the other input.
7649 * Similarly, if one of the inputs is universal, then it is equal to the union.
7651 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7652 __isl_take isl_map
*map2
)
7656 struct isl_map
*map
= NULL
;
7662 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7663 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7664 "spaces don't match", goto error
);
7675 is_universe
= isl_map_plain_is_universe(map1
);
7676 if (is_universe
< 0)
7683 is_universe
= isl_map_plain_is_universe(map2
);
7684 if (is_universe
< 0)
7691 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7692 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7693 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7695 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7696 map1
->n
+ map2
->n
, flags
);
7699 for (i
= 0; i
< map1
->n
; ++i
) {
7700 map
= isl_map_add_basic_map(map
,
7701 isl_basic_map_copy(map1
->p
[i
]));
7705 for (i
= 0; i
< map2
->n
; ++i
) {
7706 map
= isl_map_add_basic_map(map
,
7707 isl_basic_map_copy(map2
->p
[i
]));
7721 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7722 * guaranteed to be disjoint by the caller.
7724 * Note that this functions is called from within isl_map_make_disjoint,
7725 * so we have to be careful not to touch the constraints of the inputs
7728 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7729 __isl_take isl_map
*map2
)
7731 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7734 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7735 * not be disjoint. The parameters are assumed to have been aligned.
7737 * We currently simply call map_union_disjoint, the internal operation
7738 * of which does not really depend on the inputs being disjoint.
7739 * If the result contains more than one basic map, then we clear
7740 * the disjoint flag since the result may contain basic maps from
7741 * both inputs and these are not guaranteed to be disjoint.
7743 * As a special case, if "map1" and "map2" are obviously equal,
7744 * then we simply return "map1".
7746 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7747 __isl_take isl_map
*map2
)
7754 equal
= isl_map_plain_is_equal(map1
, map2
);
7762 map1
= map_union_disjoint(map1
, map2
);
7766 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7774 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7777 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7778 __isl_take isl_map
*map2
)
7780 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7783 __isl_give isl_set
*isl_set_union_disjoint(
7784 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7786 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7790 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7792 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7795 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7798 * "map" and "set" are assumed to be compatible and non-NULL.
7800 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7801 __isl_take isl_set
*set
,
7802 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7803 __isl_take isl_basic_set
*bset
))
7806 struct isl_map
*result
;
7809 if (isl_set_plain_is_universe(set
)) {
7814 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7815 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7816 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7818 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7819 map
->n
* set
->n
, flags
);
7820 for (i
= 0; result
&& i
< map
->n
; ++i
)
7821 for (j
= 0; j
< set
->n
; ++j
) {
7822 result
= isl_map_add_basic_map(result
,
7823 fn(isl_basic_map_copy(map
->p
[i
]),
7824 isl_basic_set_copy(set
->p
[j
])));
7834 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7835 __isl_take isl_set
*set
)
7839 ok
= isl_map_compatible_range(map
, set
);
7843 isl_die(set
->ctx
, isl_error_invalid
,
7844 "incompatible spaces", goto error
);
7846 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7853 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7854 __isl_take isl_set
*set
)
7856 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7859 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7860 __isl_take isl_set
*set
)
7864 ok
= isl_map_compatible_domain(map
, set
);
7868 isl_die(set
->ctx
, isl_error_invalid
,
7869 "incompatible spaces", goto error
);
7871 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7878 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7879 __isl_take isl_set
*set
)
7881 return isl_map_align_params_map_map_and(map
, set
,
7882 &map_intersect_domain
);
7885 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7886 * in the space B -> C, return the intersection.
7887 * The parameters are assumed to have been aligned.
7889 * The map "factor" is first extended to a map living in the space
7890 * [A -> B] -> C and then a regular intersection is computed.
7892 static __isl_give isl_map
*map_intersect_domain_factor_range(
7893 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7896 isl_map
*ext_factor
;
7898 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7899 ext_factor
= isl_map_universe(space
);
7900 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7901 return map_intersect(map
, ext_factor
);
7904 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7905 * in the space B -> C, return the intersection.
7907 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7908 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7910 return isl_map_align_params_map_map_and(map
, factor
,
7911 &map_intersect_domain_factor_range
);
7914 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7915 * in the space A -> C, return the intersection.
7917 * The map "factor" is first extended to a map living in the space
7918 * A -> [B -> C] and then a regular intersection is computed.
7920 static __isl_give isl_map
*map_intersect_range_factor_range(
7921 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7924 isl_map
*ext_factor
;
7926 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7927 ext_factor
= isl_map_universe(space
);
7928 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7929 return isl_map_intersect(map
, ext_factor
);
7932 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7933 * in the space A -> C, return the intersection.
7935 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7936 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7938 return isl_map_align_params_map_map_and(map
, factor
,
7939 &map_intersect_range_factor_range
);
7942 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7943 __isl_take isl_map
*map2
)
7947 map1
= isl_map_reverse(map1
);
7948 map1
= isl_map_apply_range(map1
, map2
);
7949 return isl_map_reverse(map1
);
7956 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7957 __isl_take isl_map
*map2
)
7959 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7962 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7963 __isl_take isl_map
*map2
)
7965 isl_space
*dim_result
;
7966 struct isl_map
*result
;
7972 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7973 isl_space_copy(map2
->dim
));
7975 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7978 for (i
= 0; i
< map1
->n
; ++i
)
7979 for (j
= 0; j
< map2
->n
; ++j
) {
7980 result
= isl_map_add_basic_map(result
,
7981 isl_basic_map_apply_range(
7982 isl_basic_map_copy(map1
->p
[i
]),
7983 isl_basic_map_copy(map2
->p
[j
])));
7989 if (result
&& result
->n
<= 1)
7990 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7998 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7999 __isl_take isl_map
*map2
)
8001 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8005 * returns range - domain
8007 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8009 isl_space
*target_space
;
8010 struct isl_basic_set
*bset
;
8017 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8018 bmap
->dim
, isl_dim_out
),
8020 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8021 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8022 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8023 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8024 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8025 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8026 for (i
= 0; i
< dim
; ++i
) {
8027 int j
= isl_basic_map_alloc_equality(bmap
);
8029 bmap
= isl_basic_map_free(bmap
);
8032 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8033 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8034 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8035 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8037 bset
= isl_basic_map_domain(bmap
);
8038 bset
= isl_basic_set_reset_space(bset
, target_space
);
8041 isl_basic_map_free(bmap
);
8046 * returns range - domain
8048 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8052 struct isl_set
*result
;
8057 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8058 map
->dim
, isl_dim_out
),
8060 dim
= isl_map_get_space(map
);
8061 dim
= isl_space_domain(dim
);
8062 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8065 for (i
= 0; i
< map
->n
; ++i
)
8066 result
= isl_set_add_basic_set(result
,
8067 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8076 * returns [domain -> range] -> range - domain
8078 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8079 __isl_take isl_basic_map
*bmap
)
8083 isl_basic_map
*domain
;
8087 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8088 bmap
->dim
, isl_dim_out
))
8089 isl_die(bmap
->ctx
, isl_error_invalid
,
8090 "domain and range don't match", goto error
);
8092 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8093 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8095 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
8096 domain
= isl_basic_map_universe(dim
);
8098 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8099 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8100 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8102 total
= isl_basic_map_total_dim(bmap
);
8104 for (i
= 0; i
< n
; ++i
) {
8105 k
= isl_basic_map_alloc_equality(bmap
);
8108 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8109 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8110 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8111 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8114 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8115 return isl_basic_map_finalize(bmap
);
8117 isl_basic_map_free(bmap
);
8122 * returns [domain -> range] -> range - domain
8124 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8127 isl_space
*domain_dim
;
8132 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8133 map
->dim
, isl_dim_out
))
8134 isl_die(map
->ctx
, isl_error_invalid
,
8135 "domain and range don't match", goto error
);
8137 map
= isl_map_cow(map
);
8141 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
8142 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8143 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
8146 for (i
= 0; i
< map
->n
; ++i
) {
8147 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8151 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8158 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
8160 struct isl_basic_map
*bmap
;
8168 nparam
= dims
->nparam
;
8170 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
8174 for (i
= 0; i
< dim
; ++i
) {
8175 int j
= isl_basic_map_alloc_equality(bmap
);
8178 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8179 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8180 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
8182 return isl_basic_map_finalize(bmap
);
8184 isl_basic_map_free(bmap
);
8188 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
8192 if (dim
->n_in
!= dim
->n_out
)
8193 isl_die(dim
->ctx
, isl_error_invalid
,
8194 "number of input and output dimensions needs to be "
8195 "the same", goto error
);
8196 return basic_map_identity(dim
);
8198 isl_space_free(dim
);
8202 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8204 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8207 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8209 isl_space
*dim
= isl_set_get_space(set
);
8211 id
= isl_map_identity(isl_space_map_from_set(dim
));
8212 return isl_map_intersect_range(id
, set
);
8215 /* Construct a basic set with all set dimensions having only non-negative
8218 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8219 __isl_take isl_space
*space
)
8224 struct isl_basic_set
*bset
;
8228 nparam
= space
->nparam
;
8230 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8233 for (i
= 0; i
< dim
; ++i
) {
8234 int k
= isl_basic_set_alloc_inequality(bset
);
8237 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8238 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8242 isl_basic_set_free(bset
);
8246 /* Construct the half-space x_pos >= 0.
8248 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
8252 isl_basic_set
*nonneg
;
8254 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8255 k
= isl_basic_set_alloc_inequality(nonneg
);
8258 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8259 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8261 return isl_basic_set_finalize(nonneg
);
8263 isl_basic_set_free(nonneg
);
8267 /* Construct the half-space x_pos <= -1.
8269 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
8274 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8275 k
= isl_basic_set_alloc_inequality(neg
);
8278 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8279 isl_int_set_si(neg
->ineq
[k
][0], -1);
8280 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8282 return isl_basic_set_finalize(neg
);
8284 isl_basic_set_free(neg
);
8288 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8289 enum isl_dim_type type
, unsigned first
, unsigned n
)
8293 isl_basic_set
*nonneg
;
8301 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8303 offset
= pos(set
->dim
, type
);
8304 for (i
= 0; i
< n
; ++i
) {
8305 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8306 offset
+ first
+ i
);
8307 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8309 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8318 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8320 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8326 return isl_stat_error
;
8327 if (isl_set_plain_is_empty(set
)) {
8332 return fn(set
, signs
, user
);
8335 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8337 half
= isl_set_intersect(half
, isl_set_copy(set
));
8338 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8342 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8344 half
= isl_set_intersect(half
, set
);
8345 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8348 return isl_stat_error
;
8351 /* Call "fn" on the intersections of "set" with each of the orthants
8352 * (except for obviously empty intersections). The orthant is identified
8353 * by the signs array, with each entry having value 1 or -1 according
8354 * to the sign of the corresponding variable.
8356 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8357 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8366 return isl_stat_error
;
8367 if (isl_set_plain_is_empty(set
))
8370 nparam
= isl_set_dim(set
, isl_dim_param
);
8371 nvar
= isl_set_dim(set
, isl_dim_set
);
8373 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8375 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8383 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8385 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8388 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8389 __isl_keep isl_basic_map
*bmap2
)
8392 struct isl_map
*map1
;
8393 struct isl_map
*map2
;
8395 if (!bmap1
|| !bmap2
)
8396 return isl_bool_error
;
8398 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8399 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8401 is_subset
= isl_map_is_subset(map1
, map2
);
8409 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8410 __isl_keep isl_basic_set
*bset2
)
8412 return isl_basic_map_is_subset(bset1
, bset2
);
8415 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8416 __isl_keep isl_basic_map
*bmap2
)
8420 if (!bmap1
|| !bmap2
)
8421 return isl_bool_error
;
8422 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8423 if (is_subset
!= isl_bool_true
)
8425 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8429 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8430 __isl_keep isl_basic_set
*bset2
)
8432 return isl_basic_map_is_equal(
8433 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8436 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8442 return isl_bool_error
;
8443 for (i
= 0; i
< map
->n
; ++i
) {
8444 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8446 return isl_bool_error
;
8448 return isl_bool_false
;
8450 return isl_bool_true
;
8453 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8455 return map
? map
->n
== 0 : isl_bool_error
;
8458 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8460 return set
? set
->n
== 0 : isl_bool_error
;
8463 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8465 return isl_map_is_empty(set_to_map(set
));
8468 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8469 __isl_keep isl_map
*map2
)
8472 return isl_bool_error
;
8474 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8477 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8478 __isl_keep isl_set
*set2
)
8481 return isl_bool_error
;
8483 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8486 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8491 return isl_bool_error
;
8492 is_subset
= isl_map_is_subset(map1
, map2
);
8493 if (is_subset
!= isl_bool_true
)
8495 is_subset
= isl_map_is_subset(map2
, map1
);
8499 /* Is "map1" equal to "map2"?
8501 * First check if they are obviously equal.
8502 * If not, then perform a more detailed analysis.
8504 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8508 equal
= isl_map_plain_is_equal(map1
, map2
);
8509 if (equal
< 0 || equal
)
8511 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8514 isl_bool
isl_basic_map_is_strict_subset(
8515 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8519 if (!bmap1
|| !bmap2
)
8520 return isl_bool_error
;
8521 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8522 if (is_subset
!= isl_bool_true
)
8524 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8525 if (is_subset
== isl_bool_error
)
8530 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8531 __isl_keep isl_map
*map2
)
8536 return isl_bool_error
;
8537 is_subset
= isl_map_is_subset(map1
, map2
);
8538 if (is_subset
!= isl_bool_true
)
8540 is_subset
= isl_map_is_subset(map2
, map1
);
8541 if (is_subset
== isl_bool_error
)
8546 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8547 __isl_keep isl_set
*set2
)
8549 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8552 /* Is "bmap" obviously equal to the universe with the same space?
8554 * That is, does it not have any constraints?
8556 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8559 return isl_bool_error
;
8560 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8563 /* Is "bset" obviously equal to the universe with the same space?
8565 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8567 return isl_basic_map_plain_is_universe(bset
);
8570 /* If "c" does not involve any existentially quantified variables,
8571 * then set *univ to false and abort
8573 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8575 isl_bool
*univ
= user
;
8578 n
= isl_constraint_dim(c
, isl_dim_div
);
8579 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8580 isl_constraint_free(c
);
8581 if (*univ
< 0 || !*univ
)
8582 return isl_stat_error
;
8586 /* Is "bmap" equal to the universe with the same space?
8588 * First check if it is obviously equal to the universe.
8589 * If not and if there are any constraints not involving
8590 * existentially quantified variables, then it is certainly
8591 * not equal to the universe.
8592 * Otherwise, check if the universe is a subset of "bmap".
8594 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8597 isl_basic_map
*test
;
8599 univ
= isl_basic_map_plain_is_universe(bmap
);
8600 if (univ
< 0 || univ
)
8602 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8603 return isl_bool_false
;
8604 univ
= isl_bool_true
;
8605 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8607 return isl_bool_error
;
8608 if (univ
< 0 || !univ
)
8610 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8611 univ
= isl_basic_map_is_subset(test
, bmap
);
8612 isl_basic_map_free(test
);
8616 /* Is "bset" equal to the universe with the same space?
8618 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8620 return isl_basic_map_is_universe(bset
);
8623 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8628 return isl_bool_error
;
8630 for (i
= 0; i
< map
->n
; ++i
) {
8631 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8636 return isl_bool_false
;
8639 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8641 return isl_map_plain_is_universe(set_to_map(set
));
8644 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8646 struct isl_basic_set
*bset
= NULL
;
8647 struct isl_vec
*sample
= NULL
;
8648 isl_bool empty
, non_empty
;
8651 return isl_bool_error
;
8653 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8654 return isl_bool_true
;
8656 if (isl_basic_map_plain_is_universe(bmap
))
8657 return isl_bool_false
;
8659 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8660 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8661 copy
= isl_basic_map_remove_redundancies(copy
);
8662 empty
= isl_basic_map_plain_is_empty(copy
);
8663 isl_basic_map_free(copy
);
8667 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8669 return isl_bool_error
;
8671 return isl_bool_false
;
8672 isl_vec_free(bmap
->sample
);
8673 bmap
->sample
= NULL
;
8674 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8676 return isl_bool_error
;
8677 sample
= isl_basic_set_sample_vec(bset
);
8679 return isl_bool_error
;
8680 empty
= sample
->size
== 0;
8681 isl_vec_free(bmap
->sample
);
8682 bmap
->sample
= sample
;
8684 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8689 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8692 return isl_bool_error
;
8693 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8696 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8699 return isl_bool_error
;
8700 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8703 /* Is "bmap" known to be non-empty?
8705 * That is, is the cached sample still valid?
8707 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8712 return isl_bool_error
;
8714 return isl_bool_false
;
8715 total
= 1 + isl_basic_map_total_dim(bmap
);
8716 if (bmap
->sample
->size
!= total
)
8717 return isl_bool_false
;
8718 return isl_basic_map_contains(bmap
, bmap
->sample
);
8721 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8723 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8726 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8727 __isl_take isl_basic_map
*bmap2
)
8729 struct isl_map
*map
;
8730 if (!bmap1
|| !bmap2
)
8733 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8735 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8738 map
= isl_map_add_basic_map(map
, bmap1
);
8739 map
= isl_map_add_basic_map(map
, bmap2
);
8742 isl_basic_map_free(bmap1
);
8743 isl_basic_map_free(bmap2
);
8747 struct isl_set
*isl_basic_set_union(
8748 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8750 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8751 bset_to_bmap(bset2
)));
8754 /* Order divs such that any div only depends on previous divs */
8755 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8756 __isl_take isl_basic_map
*bmap
)
8764 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8766 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8768 if (isl_int_is_zero(bmap
->div
[i
][0]))
8770 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8775 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8776 "integer division depends on itself",
8777 return isl_basic_map_free(bmap
));
8778 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8784 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8786 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8789 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8796 for (i
= 0; i
< map
->n
; ++i
) {
8797 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8808 /* Sort the local variables of "bset".
8810 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8811 __isl_take isl_basic_set
*bset
)
8813 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8816 /* Apply the expansion computed by isl_merge_divs.
8817 * The expansion itself is given by "exp" while the resulting
8818 * list of divs is given by "div".
8820 * Move the integer divisions of "bmap" into the right position
8821 * according to "exp" and then introduce the additional integer
8822 * divisions, adding div constraints.
8823 * The moving should be done first to avoid moving coefficients
8824 * in the definitions of the extra integer divisions.
8826 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8827 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8832 bmap
= isl_basic_map_cow(bmap
);
8836 if (div
->n_row
< bmap
->n_div
)
8837 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8838 "not an expansion", goto error
);
8840 n_div
= bmap
->n_div
;
8841 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8842 div
->n_row
- n_div
, 0,
8843 2 * (div
->n_row
- n_div
));
8845 for (i
= n_div
; i
< div
->n_row
; ++i
)
8846 if (isl_basic_map_alloc_div(bmap
) < 0)
8849 for (j
= n_div
- 1; j
>= 0; --j
) {
8852 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8855 for (i
= 0; i
< div
->n_row
; ++i
) {
8856 if (j
< n_div
&& exp
[j
] == i
) {
8859 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8860 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8862 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8870 isl_basic_map_free(bmap
);
8875 /* Apply the expansion computed by isl_merge_divs.
8876 * The expansion itself is given by "exp" while the resulting
8877 * list of divs is given by "div".
8879 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8880 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8882 return isl_basic_map_expand_divs(bset
, div
, exp
);
8885 /* Look for a div in dst that corresponds to the div "div" in src.
8886 * The divs before "div" in src and dst are assumed to be the same.
8888 * Returns -1 if no corresponding div was found and the position
8889 * of the corresponding div in dst otherwise.
8891 static int find_div(__isl_keep isl_basic_map
*dst
,
8892 __isl_keep isl_basic_map
*src
, unsigned div
)
8896 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8898 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8899 for (i
= div
; i
< dst
->n_div
; ++i
)
8900 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8901 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8902 dst
->n_div
- div
) == -1)
8907 /* Align the divs of "dst" to those of "src", adding divs from "src"
8908 * if needed. That is, make sure that the first src->n_div divs
8909 * of the result are equal to those of src.
8911 * The result is not finalized as by design it will have redundant
8912 * divs if any divs from "src" were copied.
8914 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8915 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8918 int known
, extended
;
8922 return isl_basic_map_free(dst
);
8924 if (src
->n_div
== 0)
8927 known
= isl_basic_map_divs_known(src
);
8929 return isl_basic_map_free(dst
);
8931 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8932 "some src divs are unknown",
8933 return isl_basic_map_free(dst
));
8935 src
= isl_basic_map_order_divs(src
);
8938 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8939 for (i
= 0; i
< src
->n_div
; ++i
) {
8940 int j
= find_div(dst
, src
, i
);
8943 int extra
= src
->n_div
- i
;
8944 dst
= isl_basic_map_cow(dst
);
8947 dst
= isl_basic_map_extend_space(dst
,
8948 isl_space_copy(dst
->dim
),
8949 extra
, 0, 2 * extra
);
8952 j
= isl_basic_map_alloc_div(dst
);
8954 return isl_basic_map_free(dst
);
8955 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8956 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8957 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8958 return isl_basic_map_free(dst
);
8961 isl_basic_map_swap_div(dst
, i
, j
);
8966 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8974 map
= isl_map_compute_divs(map
);
8975 map
= isl_map_cow(map
);
8979 for (i
= 1; i
< map
->n
; ++i
)
8980 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8981 for (i
= 1; i
< map
->n
; ++i
) {
8982 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8984 return isl_map_free(map
);
8987 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8991 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8993 return isl_map_align_divs_internal(map
);
8996 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8998 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9001 /* Align the divs of the basic maps in "map" to those
9002 * of the basic maps in "list", as well as to the other basic maps in "map".
9003 * The elements in "list" are assumed to have known divs.
9005 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9006 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9010 map
= isl_map_compute_divs(map
);
9011 map
= isl_map_cow(map
);
9013 return isl_map_free(map
);
9017 n
= isl_basic_map_list_n_basic_map(list
);
9018 for (i
= 0; i
< n
; ++i
) {
9019 isl_basic_map
*bmap
;
9021 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9022 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9023 isl_basic_map_free(bmap
);
9026 return isl_map_free(map
);
9028 return isl_map_align_divs_internal(map
);
9031 /* Align the divs of each element of "list" to those of "bmap".
9032 * Both "bmap" and the elements of "list" are assumed to have known divs.
9034 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9035 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9040 return isl_basic_map_list_free(list
);
9042 n
= isl_basic_map_list_n_basic_map(list
);
9043 for (i
= 0; i
< n
; ++i
) {
9044 isl_basic_map
*bmap_i
;
9046 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9047 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9048 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9054 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9055 __isl_take isl_map
*map
)
9059 ok
= isl_map_compatible_domain(map
, set
);
9063 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9064 "incompatible spaces", goto error
);
9065 map
= isl_map_intersect_domain(map
, set
);
9066 set
= isl_map_range(map
);
9074 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9075 __isl_take isl_map
*map
)
9077 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9080 /* There is no need to cow as removing empty parts doesn't change
9081 * the meaning of the set.
9083 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9090 for (i
= map
->n
- 1; i
>= 0; --i
)
9091 remove_if_empty(map
, i
);
9096 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9098 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9101 /* Given two basic sets bset1 and bset2, compute the maximal difference
9102 * between the values of dimension pos in bset1 and those in bset2
9103 * for any common value of the parameters and dimensions preceding pos.
9105 static enum isl_lp_result
basic_set_maximal_difference_at(
9106 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9107 int pos
, isl_int
*opt
)
9109 isl_basic_map
*bmap1
;
9110 isl_basic_map
*bmap2
;
9111 struct isl_ctx
*ctx
;
9112 struct isl_vec
*obj
;
9116 enum isl_lp_result res
;
9118 if (!bset1
|| !bset2
)
9119 return isl_lp_error
;
9121 nparam
= isl_basic_set_n_param(bset1
);
9122 dim1
= isl_basic_set_n_dim(bset1
);
9124 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9125 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9126 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9127 isl_dim_out
, 0, pos
);
9128 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9129 isl_dim_out
, 0, pos
);
9130 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
9132 return isl_lp_error
;
9134 total
= isl_basic_map_total_dim(bmap1
);
9136 obj
= isl_vec_alloc(ctx
, 1 + total
);
9139 isl_seq_clr(obj
->block
.data
, 1 + total
);
9140 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9141 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9142 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9144 isl_basic_map_free(bmap1
);
9148 isl_basic_map_free(bmap1
);
9149 return isl_lp_error
;
9152 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9153 * for any common value of the parameters and dimensions preceding pos
9154 * in both basic sets, the values of dimension pos in bset1 are
9155 * smaller or larger than those in bset2.
9158 * 1 if bset1 follows bset2
9159 * -1 if bset1 precedes bset2
9160 * 0 if bset1 and bset2 are incomparable
9161 * -2 if some error occurred.
9163 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
9164 struct isl_basic_set
*bset2
, int pos
)
9167 enum isl_lp_result res
;
9172 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9174 if (res
== isl_lp_empty
)
9176 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9177 res
== isl_lp_unbounded
)
9179 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9188 /* Given two basic sets bset1 and bset2, check whether
9189 * for any common value of the parameters and dimensions preceding pos
9190 * there is a value of dimension pos in bset1 that is larger
9191 * than a value of the same dimension in bset2.
9194 * 1 if there exists such a pair
9195 * 0 if there is no such pair, but there is a pair of equal values
9197 * -2 if some error occurred.
9199 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9200 __isl_keep isl_basic_set
*bset2
, int pos
)
9203 enum isl_lp_result res
;
9208 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9210 if (res
== isl_lp_empty
)
9212 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9213 res
== isl_lp_unbounded
)
9215 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9217 else if (res
== isl_lp_ok
)
9226 /* Given two sets set1 and set2, check whether
9227 * for any common value of the parameters and dimensions preceding pos
9228 * there is a value of dimension pos in set1 that is larger
9229 * than a value of the same dimension in set2.
9232 * 1 if there exists such a pair
9233 * 0 if there is no such pair, but there is a pair of equal values
9235 * -2 if some error occurred.
9237 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9238 __isl_keep isl_set
*set2
, int pos
)
9246 for (i
= 0; i
< set1
->n
; ++i
)
9247 for (j
= 0; j
< set2
->n
; ++j
) {
9249 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9250 if (f
== 1 || f
== -2)
9259 static isl_bool
isl_basic_map_plain_has_fixed_var(
9260 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9267 return isl_bool_error
;
9268 total
= isl_basic_map_total_dim(bmap
);
9269 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9270 for (; d
+1 > pos
; --d
)
9271 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9275 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9276 return isl_bool_false
;
9277 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9278 return isl_bool_false
;
9279 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9280 return isl_bool_false
;
9282 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9283 return isl_bool_true
;
9285 return isl_bool_false
;
9288 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9289 unsigned pos
, isl_int
*val
)
9297 return isl_bool_error
;
9299 return isl_bool_false
;
9301 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9304 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9305 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9306 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9307 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9308 fixed
= isl_bool_false
;
9311 isl_int_set(*val
, v
);
9317 static isl_bool
isl_basic_set_plain_has_fixed_var(
9318 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9320 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9324 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9325 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9327 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9328 return isl_bool_error
;
9329 return isl_basic_map_plain_has_fixed_var(bmap
,
9330 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9333 /* If "bmap" obviously lies on a hyperplane where the given dimension
9334 * has a fixed value, then return that value.
9335 * Otherwise return NaN.
9337 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9338 __isl_keep isl_basic_map
*bmap
,
9339 enum isl_dim_type type
, unsigned pos
)
9347 ctx
= isl_basic_map_get_ctx(bmap
);
9348 v
= isl_val_alloc(ctx
);
9351 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9353 return isl_val_free(v
);
9355 isl_int_set_si(v
->d
, 1);
9359 return isl_val_nan(ctx
);
9362 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9363 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9365 if (pos
>= isl_map_dim(map
, type
))
9366 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9367 "position out of bounds", return isl_bool_error
);
9368 return isl_map_plain_has_fixed_var(map
,
9369 map_offset(map
, type
) - 1 + pos
, val
);
9372 /* If "map" obviously lies on a hyperplane where the given dimension
9373 * has a fixed value, then return that value.
9374 * Otherwise return NaN.
9376 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9377 enum isl_dim_type type
, unsigned pos
)
9385 ctx
= isl_map_get_ctx(map
);
9386 v
= isl_val_alloc(ctx
);
9389 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9391 return isl_val_free(v
);
9393 isl_int_set_si(v
->d
, 1);
9397 return isl_val_nan(ctx
);
9400 /* If "set" obviously lies on a hyperplane where the given dimension
9401 * has a fixed value, then return that value.
9402 * Otherwise return NaN.
9404 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9405 enum isl_dim_type type
, unsigned pos
)
9407 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9410 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9411 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9413 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9416 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9417 * then return this fixed value in *val.
9419 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9420 unsigned dim
, isl_int
*val
)
9422 return isl_basic_set_plain_has_fixed_var(bset
,
9423 isl_basic_set_n_param(bset
) + dim
, val
);
9426 /* Return -1 if the constraint "c1" should be sorted before "c2"
9427 * and 1 if it should be sorted after "c2".
9428 * Return 0 if the two constraints are the same (up to the constant term).
9430 * In particular, if a constraint involves later variables than another
9431 * then it is sorted after this other constraint.
9432 * uset_gist depends on constraints without existentially quantified
9433 * variables sorting first.
9435 * For constraints that have the same latest variable, those
9436 * with the same coefficient for this latest variable (first in absolute value
9437 * and then in actual value) are grouped together.
9438 * This is useful for detecting pairs of constraints that can
9439 * be chained in their printed representation.
9441 * Finally, within a group, constraints are sorted according to
9442 * their coefficients (excluding the constant term).
9444 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9446 isl_int
**c1
= (isl_int
**) p1
;
9447 isl_int
**c2
= (isl_int
**) p2
;
9449 unsigned size
= *(unsigned *) arg
;
9452 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9453 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9458 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9461 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9465 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9468 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9469 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9470 * and 0 if the two constraints are the same (up to the constant term).
9472 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9473 isl_int
*c1
, isl_int
*c2
)
9479 total
= isl_basic_map_total_dim(bmap
);
9480 return sort_constraint_cmp(&c1
, &c2
, &total
);
9483 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9484 __isl_take isl_basic_map
*bmap
)
9490 if (bmap
->n_ineq
== 0)
9492 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9494 total
= isl_basic_map_total_dim(bmap
);
9495 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9496 &sort_constraint_cmp
, &total
) < 0)
9497 return isl_basic_map_free(bmap
);
9501 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9502 __isl_take isl_basic_set
*bset
)
9504 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9505 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9508 __isl_give isl_basic_map
*isl_basic_map_normalize(
9509 __isl_take isl_basic_map
*bmap
)
9513 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9515 bmap
= isl_basic_map_remove_redundancies(bmap
);
9516 bmap
= isl_basic_map_sort_constraints(bmap
);
9518 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9521 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9522 __isl_keep isl_basic_map
*bmap2
)
9526 isl_space
*space1
, *space2
;
9528 if (!bmap1
|| !bmap2
)
9533 space1
= isl_basic_map_peek_space(bmap1
);
9534 space2
= isl_basic_map_peek_space(bmap2
);
9535 cmp
= isl_space_cmp(space1
, space2
);
9538 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9539 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9540 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9541 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9542 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9544 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9546 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9548 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9549 return bmap1
->n_eq
- bmap2
->n_eq
;
9550 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9551 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9552 if (bmap1
->n_div
!= bmap2
->n_div
)
9553 return bmap1
->n_div
- bmap2
->n_div
;
9554 total
= isl_basic_map_total_dim(bmap1
);
9555 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9556 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9560 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9561 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9565 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9566 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9573 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9574 __isl_keep isl_basic_set
*bset2
)
9576 return isl_basic_map_plain_cmp(bset1
, bset2
);
9579 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9585 if (set1
->n
!= set2
->n
)
9586 return set1
->n
- set2
->n
;
9588 for (i
= 0; i
< set1
->n
; ++i
) {
9589 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9597 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9598 __isl_keep isl_basic_map
*bmap2
)
9600 if (!bmap1
|| !bmap2
)
9601 return isl_bool_error
;
9602 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9605 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9606 __isl_keep isl_basic_set
*bset2
)
9608 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9609 bset_to_bmap(bset2
));
9612 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9614 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9615 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9617 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9620 /* Sort the basic maps of "map" and remove duplicate basic maps.
9622 * While removing basic maps, we make sure that the basic maps remain
9623 * sorted because isl_map_normalize expects the basic maps of the result
9626 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9630 map
= isl_map_remove_empty_parts(map
);
9633 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9634 for (i
= map
->n
- 1; i
>= 1; --i
) {
9635 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9637 isl_basic_map_free(map
->p
[i
-1]);
9638 for (j
= i
; j
< map
->n
; ++j
)
9639 map
->p
[j
- 1] = map
->p
[j
];
9646 /* Remove obvious duplicates among the basic maps of "map".
9648 * Unlike isl_map_normalize, this function does not remove redundant
9649 * constraints and only removes duplicates that have exactly the same
9650 * constraints in the input. It does sort the constraints and
9651 * the basic maps to ease the detection of duplicates.
9653 * If "map" has already been normalized or if the basic maps are
9654 * disjoint, then there can be no duplicates.
9656 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9659 isl_basic_map
*bmap
;
9665 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9667 for (i
= 0; i
< map
->n
; ++i
) {
9668 bmap
= isl_basic_map_copy(map
->p
[i
]);
9669 bmap
= isl_basic_map_sort_constraints(bmap
);
9671 return isl_map_free(map
);
9672 isl_basic_map_free(map
->p
[i
]);
9676 map
= sort_and_remove_duplicates(map
);
9680 /* We normalize in place, but if anything goes wrong we need
9681 * to return NULL, so we need to make sure we don't change the
9682 * meaning of any possible other copies of map.
9684 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9687 struct isl_basic_map
*bmap
;
9691 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9693 for (i
= 0; i
< map
->n
; ++i
) {
9694 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9697 isl_basic_map_free(map
->p
[i
]);
9701 map
= sort_and_remove_duplicates(map
);
9703 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9710 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9712 return set_from_map(isl_map_normalize(set_to_map(set
)));
9715 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9716 __isl_keep isl_map
*map2
)
9722 return isl_bool_error
;
9725 return isl_bool_true
;
9726 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9727 return isl_bool_false
;
9729 map1
= isl_map_copy(map1
);
9730 map2
= isl_map_copy(map2
);
9731 map1
= isl_map_normalize(map1
);
9732 map2
= isl_map_normalize(map2
);
9735 equal
= map1
->n
== map2
->n
;
9736 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9737 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9747 return isl_bool_error
;
9750 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9751 __isl_keep isl_set
*set2
)
9753 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9756 /* Return the basic maps in "map" as a list.
9758 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9759 __isl_keep isl_map
*map
)
9763 isl_basic_map_list
*list
;
9767 ctx
= isl_map_get_ctx(map
);
9768 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9770 for (i
= 0; i
< map
->n
; ++i
) {
9771 isl_basic_map
*bmap
;
9773 bmap
= isl_basic_map_copy(map
->p
[i
]);
9774 list
= isl_basic_map_list_add(list
, bmap
);
9780 /* Return the intersection of the elements in the non-empty list "list".
9781 * All elements are assumed to live in the same space.
9783 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9784 __isl_take isl_basic_map_list
*list
)
9787 isl_basic_map
*bmap
;
9791 n
= isl_basic_map_list_n_basic_map(list
);
9793 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9794 "expecting non-empty list", goto error
);
9796 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9797 for (i
= 1; i
< n
; ++i
) {
9798 isl_basic_map
*bmap_i
;
9800 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9801 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9804 isl_basic_map_list_free(list
);
9807 isl_basic_map_list_free(list
);
9811 /* Return the intersection of the elements in the non-empty list "list".
9812 * All elements are assumed to live in the same space.
9814 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9815 __isl_take isl_basic_set_list
*list
)
9817 return isl_basic_map_list_intersect(list
);
9820 /* Return the union of the elements of "list".
9821 * The list is required to have at least one element.
9823 __isl_give isl_set
*isl_basic_set_list_union(
9824 __isl_take isl_basic_set_list
*list
)
9828 isl_basic_set
*bset
;
9833 n
= isl_basic_set_list_n_basic_set(list
);
9835 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9836 "expecting non-empty list", goto error
);
9838 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9839 space
= isl_basic_set_get_space(bset
);
9840 isl_basic_set_free(bset
);
9842 set
= isl_set_alloc_space(space
, n
, 0);
9843 for (i
= 0; i
< n
; ++i
) {
9844 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9845 set
= isl_set_add_basic_set(set
, bset
);
9848 isl_basic_set_list_free(list
);
9851 isl_basic_set_list_free(list
);
9855 /* Return the union of the elements in the non-empty list "list".
9856 * All elements are assumed to live in the same space.
9858 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9865 n
= isl_set_list_n_set(list
);
9867 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9868 "expecting non-empty list", goto error
);
9870 set
= isl_set_list_get_set(list
, 0);
9871 for (i
= 1; i
< n
; ++i
) {
9874 set_i
= isl_set_list_get_set(list
, i
);
9875 set
= isl_set_union(set
, set_i
);
9878 isl_set_list_free(list
);
9881 isl_set_list_free(list
);
9885 __isl_give isl_basic_map
*isl_basic_map_product(
9886 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9888 isl_space
*dim_result
= NULL
;
9889 struct isl_basic_map
*bmap
;
9890 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9891 struct isl_dim_map
*dim_map1
, *dim_map2
;
9893 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9895 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9896 isl_space_copy(bmap2
->dim
));
9898 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9899 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9900 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9901 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9902 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9904 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9905 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9906 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9907 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9908 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9909 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9910 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9911 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9912 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9913 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9914 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9916 bmap
= isl_basic_map_alloc_space(dim_result
,
9917 bmap1
->n_div
+ bmap2
->n_div
,
9918 bmap1
->n_eq
+ bmap2
->n_eq
,
9919 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9920 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9921 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9922 bmap
= isl_basic_map_simplify(bmap
);
9923 return isl_basic_map_finalize(bmap
);
9925 isl_basic_map_free(bmap1
);
9926 isl_basic_map_free(bmap2
);
9930 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9931 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9933 isl_basic_map
*prod
;
9935 prod
= isl_basic_map_product(bmap1
, bmap2
);
9936 prod
= isl_basic_map_flatten(prod
);
9940 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9941 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9943 return isl_basic_map_flat_range_product(bset1
, bset2
);
9946 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9947 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9949 isl_space
*space_result
= NULL
;
9950 isl_basic_map
*bmap
;
9951 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9952 struct isl_dim_map
*dim_map1
, *dim_map2
;
9954 if (!bmap1
|| !bmap2
)
9957 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9958 isl_space_copy(bmap2
->dim
));
9960 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9961 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9962 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9963 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9965 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9966 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9967 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9968 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9969 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9970 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9971 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9972 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9973 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9974 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9975 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9977 bmap
= isl_basic_map_alloc_space(space_result
,
9978 bmap1
->n_div
+ bmap2
->n_div
,
9979 bmap1
->n_eq
+ bmap2
->n_eq
,
9980 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9981 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9982 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9983 bmap
= isl_basic_map_simplify(bmap
);
9984 return isl_basic_map_finalize(bmap
);
9986 isl_basic_map_free(bmap1
);
9987 isl_basic_map_free(bmap2
);
9991 __isl_give isl_basic_map
*isl_basic_map_range_product(
9992 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9995 isl_space
*dim_result
= NULL
;
9996 isl_basic_map
*bmap
;
9997 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9998 struct isl_dim_map
*dim_map1
, *dim_map2
;
10000 rational
= isl_basic_map_is_rational(bmap1
);
10001 if (rational
>= 0 && rational
)
10002 rational
= isl_basic_map_is_rational(bmap2
);
10003 if (!bmap1
|| !bmap2
|| rational
< 0)
10006 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10009 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10010 isl_space_copy(bmap2
->dim
));
10012 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10013 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10014 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10015 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10017 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10018 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10019 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10020 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10021 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10022 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10023 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10024 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10025 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10026 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10027 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10029 bmap
= isl_basic_map_alloc_space(dim_result
,
10030 bmap1
->n_div
+ bmap2
->n_div
,
10031 bmap1
->n_eq
+ bmap2
->n_eq
,
10032 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10033 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10034 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10036 bmap
= isl_basic_map_set_rational(bmap
);
10037 bmap
= isl_basic_map_simplify(bmap
);
10038 return isl_basic_map_finalize(bmap
);
10040 isl_basic_map_free(bmap1
);
10041 isl_basic_map_free(bmap2
);
10045 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10046 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10048 isl_basic_map
*prod
;
10050 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10051 prod
= isl_basic_map_flatten_range(prod
);
10055 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10056 * and collect the results.
10057 * The result live in the space obtained by calling "space_product"
10058 * on the spaces of "map1" and "map2".
10059 * If "remove_duplicates" is set then the result may contain duplicates
10060 * (even if the inputs do not) and so we try and remove the obvious
10063 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10064 __isl_take isl_map
*map2
,
10065 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10066 __isl_take isl_space
*right
),
10067 __isl_give isl_basic_map
*(*basic_map_product
)(
10068 __isl_take isl_basic_map
*left
,
10069 __isl_take isl_basic_map
*right
),
10070 int remove_duplicates
)
10072 unsigned flags
= 0;
10073 struct isl_map
*result
;
10077 m
= isl_map_has_equal_params(map1
, map2
);
10081 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10082 "parameters don't match", goto error
);
10084 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10085 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10086 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10088 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10089 isl_space_copy(map2
->dim
)),
10090 map1
->n
* map2
->n
, flags
);
10093 for (i
= 0; i
< map1
->n
; ++i
)
10094 for (j
= 0; j
< map2
->n
; ++j
) {
10095 struct isl_basic_map
*part
;
10096 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10097 isl_basic_map_copy(map2
->p
[j
]));
10098 if (isl_basic_map_is_empty(part
))
10099 isl_basic_map_free(part
);
10101 result
= isl_map_add_basic_map(result
, part
);
10105 if (remove_duplicates
)
10106 result
= isl_map_remove_obvious_duplicates(result
);
10107 isl_map_free(map1
);
10108 isl_map_free(map2
);
10111 isl_map_free(map1
);
10112 isl_map_free(map2
);
10116 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10118 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10119 __isl_take isl_map
*map2
)
10121 return map_product(map1
, map2
, &isl_space_product
,
10122 &isl_basic_map_product
, 0);
10125 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10126 __isl_take isl_map
*map2
)
10128 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10131 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10133 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10134 __isl_take isl_map
*map2
)
10138 prod
= isl_map_product(map1
, map2
);
10139 prod
= isl_map_flatten(prod
);
10143 /* Given two set A and B, construct its Cartesian product A x B.
10145 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10147 return isl_map_range_product(set1
, set2
);
10150 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10151 __isl_take isl_set
*set2
)
10153 return isl_map_flat_range_product(set1
, set2
);
10156 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10158 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10159 __isl_take isl_map
*map2
)
10161 return map_product(map1
, map2
, &isl_space_domain_product
,
10162 &isl_basic_map_domain_product
, 1);
10165 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10167 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10168 __isl_take isl_map
*map2
)
10170 return map_product(map1
, map2
, &isl_space_range_product
,
10171 &isl_basic_map_range_product
, 1);
10174 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10175 __isl_take isl_map
*map2
)
10177 return isl_map_align_params_map_map_and(map1
, map2
,
10178 &map_domain_product_aligned
);
10181 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10182 __isl_take isl_map
*map2
)
10184 return isl_map_align_params_map_map_and(map1
, map2
,
10185 &map_range_product_aligned
);
10188 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10190 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10193 int total1
, keep1
, total2
, keep2
;
10197 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10198 !isl_space_range_is_wrapping(map
->dim
))
10199 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10200 "not a product", return isl_map_free(map
));
10202 space
= isl_map_get_space(map
);
10203 total1
= isl_space_dim(space
, isl_dim_in
);
10204 total2
= isl_space_dim(space
, isl_dim_out
);
10205 space
= isl_space_factor_domain(space
);
10206 keep1
= isl_space_dim(space
, isl_dim_in
);
10207 keep2
= isl_space_dim(space
, isl_dim_out
);
10208 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10209 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10210 map
= isl_map_reset_space(map
, space
);
10215 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10217 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10220 int total1
, keep1
, total2
, keep2
;
10224 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10225 !isl_space_range_is_wrapping(map
->dim
))
10226 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10227 "not a product", return isl_map_free(map
));
10229 space
= isl_map_get_space(map
);
10230 total1
= isl_space_dim(space
, isl_dim_in
);
10231 total2
= isl_space_dim(space
, isl_dim_out
);
10232 space
= isl_space_factor_range(space
);
10233 keep1
= isl_space_dim(space
, isl_dim_in
);
10234 keep2
= isl_space_dim(space
, isl_dim_out
);
10235 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10236 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10237 map
= isl_map_reset_space(map
, space
);
10242 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10244 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10251 if (!isl_space_domain_is_wrapping(map
->dim
))
10252 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10253 "domain is not a product", return isl_map_free(map
));
10255 space
= isl_map_get_space(map
);
10256 total
= isl_space_dim(space
, isl_dim_in
);
10257 space
= isl_space_domain_factor_domain(space
);
10258 keep
= isl_space_dim(space
, isl_dim_in
);
10259 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10260 map
= isl_map_reset_space(map
, space
);
10265 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10267 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10274 if (!isl_space_domain_is_wrapping(map
->dim
))
10275 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10276 "domain is not a product", return isl_map_free(map
));
10278 space
= isl_map_get_space(map
);
10279 total
= isl_space_dim(space
, isl_dim_in
);
10280 space
= isl_space_domain_factor_range(space
);
10281 keep
= isl_space_dim(space
, isl_dim_in
);
10282 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10283 map
= isl_map_reset_space(map
, space
);
10288 /* Given a map A -> [B -> C], extract the map A -> B.
10290 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10297 if (!isl_space_range_is_wrapping(map
->dim
))
10298 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10299 "range is not a product", return isl_map_free(map
));
10301 space
= isl_map_get_space(map
);
10302 total
= isl_space_dim(space
, isl_dim_out
);
10303 space
= isl_space_range_factor_domain(space
);
10304 keep
= isl_space_dim(space
, isl_dim_out
);
10305 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10306 map
= isl_map_reset_space(map
, space
);
10311 /* Given a map A -> [B -> C], extract the map A -> C.
10313 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10320 if (!isl_space_range_is_wrapping(map
->dim
))
10321 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10322 "range is not a product", return isl_map_free(map
));
10324 space
= isl_map_get_space(map
);
10325 total
= isl_space_dim(space
, isl_dim_out
);
10326 space
= isl_space_range_factor_range(space
);
10327 keep
= isl_space_dim(space
, isl_dim_out
);
10328 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10329 map
= isl_map_reset_space(map
, space
);
10334 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10336 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10337 __isl_take isl_map
*map2
)
10341 prod
= isl_map_domain_product(map1
, map2
);
10342 prod
= isl_map_flatten_domain(prod
);
10346 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10348 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10349 __isl_take isl_map
*map2
)
10353 prod
= isl_map_range_product(map1
, map2
);
10354 prod
= isl_map_flatten_range(prod
);
10358 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10361 uint32_t hash
= isl_hash_init();
10366 bmap
= isl_basic_map_copy(bmap
);
10367 bmap
= isl_basic_map_normalize(bmap
);
10370 total
= isl_basic_map_total_dim(bmap
);
10371 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10372 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10374 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10375 isl_hash_hash(hash
, c_hash
);
10377 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10378 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10380 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10381 isl_hash_hash(hash
, c_hash
);
10383 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10384 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10386 if (isl_int_is_zero(bmap
->div
[i
][0]))
10388 isl_hash_byte(hash
, i
& 0xFF);
10389 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10390 isl_hash_hash(hash
, c_hash
);
10392 isl_basic_map_free(bmap
);
10396 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10398 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10401 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10408 map
= isl_map_copy(map
);
10409 map
= isl_map_normalize(map
);
10413 hash
= isl_hash_init();
10414 for (i
= 0; i
< map
->n
; ++i
) {
10415 uint32_t bmap_hash
;
10416 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10417 isl_hash_hash(hash
, bmap_hash
);
10425 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10427 return isl_map_get_hash(set_to_map(set
));
10430 /* Return the number of basic maps in the (current) representation of "map".
10432 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10434 return map
? map
->n
: 0;
10437 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10439 return set
? set
->n
: 0;
10442 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10443 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10448 return isl_stat_error
;
10450 for (i
= 0; i
< map
->n
; ++i
)
10451 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10452 return isl_stat_error
;
10454 return isl_stat_ok
;
10457 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10458 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10463 return isl_stat_error
;
10465 for (i
= 0; i
< set
->n
; ++i
)
10466 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10467 return isl_stat_error
;
10469 return isl_stat_ok
;
10472 /* Return a list of basic sets, the union of which is equal to "set".
10474 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10475 __isl_keep isl_set
*set
)
10478 isl_basic_set_list
*list
;
10483 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10484 for (i
= 0; i
< set
->n
; ++i
) {
10485 isl_basic_set
*bset
;
10487 bset
= isl_basic_set_copy(set
->p
[i
]);
10488 list
= isl_basic_set_list_add(list
, bset
);
10494 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10501 bset
= isl_basic_set_cow(bset
);
10505 dim
= isl_basic_set_get_space(bset
);
10506 dim
= isl_space_lift(dim
, bset
->n_div
);
10509 isl_space_free(bset
->dim
);
10511 bset
->extra
-= bset
->n_div
;
10514 bset
= isl_basic_set_finalize(bset
);
10518 isl_basic_set_free(bset
);
10522 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10528 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10533 set
= isl_set_cow(set
);
10537 n_div
= set
->p
[0]->n_div
;
10538 dim
= isl_set_get_space(set
);
10539 dim
= isl_space_lift(dim
, n_div
);
10542 isl_space_free(set
->dim
);
10545 for (i
= 0; i
< set
->n
; ++i
) {
10546 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10557 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10565 dim
= isl_basic_set_total_dim(bset
);
10566 size
+= bset
->n_eq
* (1 + dim
);
10567 size
+= bset
->n_ineq
* (1 + dim
);
10568 size
+= bset
->n_div
* (2 + dim
);
10573 int isl_set_size(__isl_keep isl_set
*set
)
10581 for (i
= 0; i
< set
->n
; ++i
)
10582 size
+= isl_basic_set_size(set
->p
[i
]);
10587 /* Check if there is any lower bound (if lower == 0) and/or upper
10588 * bound (if upper == 0) on the specified dim.
10590 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10591 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10595 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10596 return isl_bool_error
;
10598 pos
+= isl_basic_map_offset(bmap
, type
);
10600 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10601 if (isl_int_is_zero(bmap
->div
[i
][0]))
10603 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10604 return isl_bool_true
;
10607 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10608 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10609 return isl_bool_true
;
10611 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10612 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10619 return lower
&& upper
;
10622 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10623 enum isl_dim_type type
, unsigned pos
)
10625 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10628 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10629 enum isl_dim_type type
, unsigned pos
)
10631 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10634 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10635 enum isl_dim_type type
, unsigned pos
)
10637 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10640 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10641 enum isl_dim_type type
, unsigned pos
)
10646 return isl_bool_error
;
10648 for (i
= 0; i
< map
->n
; ++i
) {
10650 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10651 if (bounded
< 0 || !bounded
)
10655 return isl_bool_true
;
10658 /* Return true if the specified dim is involved in both an upper bound
10659 * and a lower bound.
10661 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10662 enum isl_dim_type type
, unsigned pos
)
10664 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10667 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10669 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10670 enum isl_dim_type type
, unsigned pos
,
10671 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10672 enum isl_dim_type type
, unsigned pos
))
10677 return isl_bool_error
;
10679 for (i
= 0; i
< map
->n
; ++i
) {
10681 bounded
= fn(map
->p
[i
], type
, pos
);
10682 if (bounded
< 0 || bounded
)
10686 return isl_bool_false
;
10689 /* Return 1 if the specified dim is involved in any lower bound.
10691 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10692 enum isl_dim_type type
, unsigned pos
)
10694 return has_any_bound(set
, type
, pos
,
10695 &isl_basic_map_dim_has_lower_bound
);
10698 /* Return 1 if the specified dim is involved in any upper bound.
10700 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10701 enum isl_dim_type type
, unsigned pos
)
10703 return has_any_bound(set
, type
, pos
,
10704 &isl_basic_map_dim_has_upper_bound
);
10707 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10709 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10710 enum isl_dim_type type
, unsigned pos
,
10711 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10712 enum isl_dim_type type
, unsigned pos
))
10717 return isl_bool_error
;
10719 for (i
= 0; i
< map
->n
; ++i
) {
10721 bounded
= fn(map
->p
[i
], type
, pos
);
10722 if (bounded
< 0 || !bounded
)
10726 return isl_bool_true
;
10729 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10731 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10732 enum isl_dim_type type
, unsigned pos
)
10734 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10737 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10739 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10740 enum isl_dim_type type
, unsigned pos
)
10742 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10745 /* For each of the "n" variables starting at "first", determine
10746 * the sign of the variable and put the results in the first "n"
10747 * elements of the array "signs".
10749 * 1 means that the variable is non-negative
10750 * -1 means that the variable is non-positive
10751 * 0 means the variable attains both positive and negative values.
10753 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10754 unsigned first
, unsigned n
, int *signs
)
10756 isl_vec
*bound
= NULL
;
10757 struct isl_tab
*tab
= NULL
;
10758 struct isl_tab_undo
*snap
;
10761 if (!bset
|| !signs
)
10762 return isl_stat_error
;
10764 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10765 tab
= isl_tab_from_basic_set(bset
, 0);
10766 if (!bound
|| !tab
)
10769 isl_seq_clr(bound
->el
, bound
->size
);
10770 isl_int_set_si(bound
->el
[0], -1);
10772 snap
= isl_tab_snap(tab
);
10773 for (i
= 0; i
< n
; ++i
) {
10776 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10777 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10779 empty
= tab
->empty
;
10780 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10781 if (isl_tab_rollback(tab
, snap
) < 0)
10789 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10790 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10792 empty
= tab
->empty
;
10793 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10794 if (isl_tab_rollback(tab
, snap
) < 0)
10797 signs
[i
] = empty
? -1 : 0;
10801 isl_vec_free(bound
);
10802 return isl_stat_ok
;
10805 isl_vec_free(bound
);
10806 return isl_stat_error
;
10809 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10810 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10812 if (!bset
|| !signs
)
10813 return isl_stat_error
;
10814 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10815 return isl_stat_error
);
10817 first
+= pos(bset
->dim
, type
) - 1;
10818 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10821 /* Is it possible for the integer division "div" to depend (possibly
10822 * indirectly) on any output dimensions?
10824 * If the div is undefined, then we conservatively assume that it
10825 * may depend on them.
10826 * Otherwise, we check if it actually depends on them or on any integer
10827 * divisions that may depend on them.
10829 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10832 unsigned n_out
, o_out
;
10833 unsigned n_div
, o_div
;
10835 if (isl_int_is_zero(bmap
->div
[div
][0]))
10836 return isl_bool_true
;
10838 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10839 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10841 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10842 return isl_bool_true
;
10844 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10845 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10847 for (i
= 0; i
< n_div
; ++i
) {
10848 isl_bool may_involve
;
10850 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10852 may_involve
= div_may_involve_output(bmap
, i
);
10853 if (may_involve
< 0 || may_involve
)
10854 return may_involve
;
10857 return isl_bool_false
;
10860 /* Return the first integer division of "bmap" in the range
10861 * [first, first + n[ that may depend on any output dimensions and
10862 * that has a non-zero coefficient in "c" (where the first coefficient
10863 * in "c" corresponds to integer division "first").
10865 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10866 isl_int
*c
, int first
, int n
)
10873 for (k
= first
; k
< first
+ n
; ++k
) {
10874 isl_bool may_involve
;
10876 if (isl_int_is_zero(c
[k
]))
10878 may_involve
= div_may_involve_output(bmap
, k
);
10879 if (may_involve
< 0)
10888 /* Look for a pair of inequality constraints in "bmap" of the form
10890 * -l + i >= 0 or i >= l
10892 * n + l - i >= 0 or i <= l + n
10894 * with n < "m" and i the output dimension at position "pos".
10895 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10896 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10897 * and earlier output dimensions, as well as integer divisions that do
10898 * not involve any of the output dimensions.
10900 * Return the index of the first inequality constraint or bmap->n_ineq
10901 * if no such pair can be found.
10903 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10904 int pos
, isl_int m
)
10909 unsigned n_div
, o_div
;
10910 unsigned n_out
, o_out
;
10916 ctx
= isl_basic_map_get_ctx(bmap
);
10917 total
= isl_basic_map_total_dim(bmap
);
10918 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10919 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10920 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10921 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10922 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10923 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10925 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10926 n_out
- (pos
+ 1)) != -1)
10928 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10931 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10932 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10935 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10936 bmap
->ineq
[j
] + 1, total
))
10940 if (j
>= bmap
->n_ineq
)
10942 isl_int_add(bmap
->ineq
[i
][0],
10943 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10944 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10945 isl_int_sub(bmap
->ineq
[i
][0],
10946 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10949 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10955 return bmap
->n_ineq
;
10958 /* Return the index of the equality of "bmap" that defines
10959 * the output dimension "pos" in terms of earlier dimensions.
10960 * The equality may also involve integer divisions, as long
10961 * as those integer divisions are defined in terms of
10962 * parameters or input dimensions.
10963 * In this case, *div is set to the number of integer divisions and
10964 * *ineq is set to the number of inequality constraints (provided
10965 * div and ineq are not NULL).
10967 * The equality may also involve a single integer division involving
10968 * the output dimensions (typically only output dimension "pos") as
10969 * long as the coefficient of output dimension "pos" is 1 or -1 and
10970 * there is a pair of constraints i >= l and i <= l + n, with i referring
10971 * to output dimension "pos", l an expression involving only earlier
10972 * dimensions and n smaller than the coefficient of the integer division
10973 * in the equality. In this case, the output dimension can be defined
10974 * in terms of a modulo expression that does not involve the integer division.
10975 * *div is then set to this single integer division and
10976 * *ineq is set to the index of constraint i >= l.
10978 * Return bmap->n_eq if there is no such equality.
10979 * Return -1 on error.
10981 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10982 int pos
, int *div
, int *ineq
)
10985 unsigned n_out
, o_out
;
10986 unsigned n_div
, o_div
;
10991 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10992 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10993 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10994 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10997 *ineq
= bmap
->n_ineq
;
11000 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11001 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11003 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11004 n_out
- (pos
+ 1)) != -1)
11006 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11010 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11011 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11013 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11014 k
+ 1, n_div
- (k
+1)) < n_div
)
11016 l
= find_modulo_constraint_pair(bmap
, pos
,
11017 bmap
->eq
[j
][o_div
+ k
]);
11020 if (l
>= bmap
->n_ineq
)
11032 /* Check if the given basic map is obviously single-valued.
11033 * In particular, for each output dimension, check that there is
11034 * an equality that defines the output dimension in terms of
11035 * earlier dimensions.
11037 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11043 return isl_bool_error
;
11045 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11047 for (i
= 0; i
< n_out
; ++i
) {
11050 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11053 return isl_bool_error
;
11054 if (eq
>= bmap
->n_eq
)
11055 return isl_bool_false
;
11058 return isl_bool_true
;
11061 /* Check if the given basic map is single-valued.
11062 * We simply compute
11066 * and check if the result is a subset of the identity mapping.
11068 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11071 isl_basic_map
*test
;
11075 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11079 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11080 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11082 space
= isl_basic_map_get_space(bmap
);
11083 space
= isl_space_map_from_set(isl_space_range(space
));
11084 id
= isl_basic_map_identity(space
);
11086 sv
= isl_basic_map_is_subset(test
, id
);
11088 isl_basic_map_free(test
);
11089 isl_basic_map_free(id
);
11094 /* Check if the given map is obviously single-valued.
11096 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11099 return isl_bool_error
;
11101 return isl_bool_true
;
11103 return isl_bool_false
;
11105 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11108 /* Check if the given map is single-valued.
11109 * We simply compute
11113 * and check if the result is a subset of the identity mapping.
11115 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11122 sv
= isl_map_plain_is_single_valued(map
);
11126 test
= isl_map_reverse(isl_map_copy(map
));
11127 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11129 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11130 id
= isl_map_identity(dim
);
11132 sv
= isl_map_is_subset(test
, id
);
11134 isl_map_free(test
);
11140 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11144 map
= isl_map_copy(map
);
11145 map
= isl_map_reverse(map
);
11146 in
= isl_map_is_single_valued(map
);
11152 /* Check if the given map is obviously injective.
11154 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11158 map
= isl_map_copy(map
);
11159 map
= isl_map_reverse(map
);
11160 in
= isl_map_plain_is_single_valued(map
);
11166 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11170 sv
= isl_map_is_single_valued(map
);
11174 return isl_map_is_injective(map
);
11177 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11179 return isl_map_is_single_valued(set_to_map(set
));
11182 /* Does "map" only map elements to themselves?
11184 * If the domain and range spaces are different, then "map"
11185 * is considered not to be an identity relation, even if it is empty.
11186 * Otherwise, construct the maximal identity relation and
11187 * check whether "map" is a subset of this relation.
11189 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11193 isl_bool equal
, is_identity
;
11195 space
= isl_map_get_space(map
);
11196 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11197 isl_space_free(space
);
11198 if (equal
< 0 || !equal
)
11201 id
= isl_map_identity(isl_map_get_space(map
));
11202 is_identity
= isl_map_is_subset(map
, id
);
11205 return is_identity
;
11208 int isl_map_is_translation(__isl_keep isl_map
*map
)
11213 delta
= isl_map_deltas(isl_map_copy(map
));
11214 ok
= isl_set_is_singleton(delta
);
11215 isl_set_free(delta
);
11220 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11222 if (isl_seq_first_non_zero(p
, pos
) != -1)
11224 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11229 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11236 return isl_bool_error
;
11238 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11239 return isl_bool_false
;
11241 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11242 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11243 for (j
= 0; j
< nvar
; ++j
) {
11244 int lower
= 0, upper
= 0;
11245 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11246 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11248 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11249 return isl_bool_false
;
11252 if (i
< bset
->n_eq
)
11254 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11255 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11257 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11258 return isl_bool_false
;
11259 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11264 if (!lower
|| !upper
)
11265 return isl_bool_false
;
11268 return isl_bool_true
;
11271 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11274 return isl_bool_error
;
11276 return isl_bool_false
;
11278 return isl_basic_set_is_box(set
->p
[0]);
11281 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11284 return isl_bool_error
;
11286 return isl_space_is_wrapping(bset
->dim
);
11289 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11292 return isl_bool_error
;
11294 return isl_space_is_wrapping(set
->dim
);
11297 /* Modify the space of "map" through a call to "change".
11298 * If "can_change" is set (not NULL), then first call it to check
11299 * if the modification is allowed, printing the error message "cannot_change"
11302 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11303 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11304 const char *cannot_change
,
11305 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11313 ok
= can_change
? can_change(map
) : isl_bool_true
;
11315 return isl_map_free(map
);
11317 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11318 return isl_map_free(map
));
11320 space
= change(isl_map_get_space(map
));
11321 map
= isl_map_reset_space(map
, space
);
11326 /* Is the domain of "map" a wrapped relation?
11328 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11331 return isl_bool_error
;
11333 return isl_space_domain_is_wrapping(map
->dim
);
11336 /* Does "map" have a wrapped relation in both domain and range?
11338 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11340 return isl_space_is_product(isl_map_peek_space(map
));
11343 /* Is the range of "map" a wrapped relation?
11345 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11348 return isl_bool_error
;
11350 return isl_space_range_is_wrapping(map
->dim
);
11353 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11355 bmap
= isl_basic_map_cow(bmap
);
11359 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11363 bmap
= isl_basic_map_finalize(bmap
);
11365 return bset_from_bmap(bmap
);
11367 isl_basic_map_free(bmap
);
11371 /* Given a map A -> B, return the set (A -> B).
11373 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11375 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11378 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11380 bset
= isl_basic_set_cow(bset
);
11384 bset
->dim
= isl_space_unwrap(bset
->dim
);
11388 bset
= isl_basic_set_finalize(bset
);
11390 return bset_to_bmap(bset
);
11392 isl_basic_set_free(bset
);
11396 /* Given a set (A -> B), return the map A -> B.
11397 * Error out if "set" is not of the form (A -> B).
11399 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11401 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11402 "not a wrapping set", &isl_space_unwrap
);
11405 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11406 enum isl_dim_type type
)
11411 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11414 bmap
= isl_basic_map_cow(bmap
);
11418 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11422 bmap
= isl_basic_map_finalize(bmap
);
11426 isl_basic_map_free(bmap
);
11430 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11431 enum isl_dim_type type
)
11438 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11441 map
= isl_map_cow(map
);
11445 for (i
= 0; i
< map
->n
; ++i
) {
11446 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11450 map
->dim
= isl_space_reset(map
->dim
, type
);
11460 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11465 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11468 bmap
= isl_basic_map_cow(bmap
);
11472 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11476 bmap
= isl_basic_map_finalize(bmap
);
11480 isl_basic_map_free(bmap
);
11484 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11486 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11489 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11490 __isl_take isl_basic_map
*bmap
)
11495 if (!bmap
->dim
->nested
[0])
11498 bmap
= isl_basic_map_cow(bmap
);
11502 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11506 bmap
= isl_basic_map_finalize(bmap
);
11510 isl_basic_map_free(bmap
);
11514 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11515 __isl_take isl_basic_map
*bmap
)
11520 if (!bmap
->dim
->nested
[1])
11523 bmap
= isl_basic_map_cow(bmap
);
11527 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11531 bmap
= isl_basic_map_finalize(bmap
);
11535 isl_basic_map_free(bmap
);
11539 /* Remove any internal structure from the spaces of domain and range of "map".
11541 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11546 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11549 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11552 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11554 return set_from_map(isl_map_flatten(set_to_map(set
)));
11557 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11559 isl_space
*dim
, *flat_dim
;
11562 dim
= isl_set_get_space(set
);
11563 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11564 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11565 map
= isl_map_intersect_domain(map
, set
);
11570 /* Remove any internal structure from the space of the domain of "map".
11572 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11577 if (!map
->dim
->nested
[0])
11580 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11583 /* Remove any internal structure from the space of the range of "map".
11585 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11590 if (!map
->dim
->nested
[1])
11593 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11596 /* Reorder the dimensions of "bmap" according to the given dim_map
11597 * and set the dimension specification to "dim" and
11598 * perform Gaussian elimination on the result.
11600 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11601 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11603 isl_basic_map
*res
;
11606 bmap
= isl_basic_map_cow(bmap
);
11607 if (!bmap
|| !dim
|| !dim_map
)
11610 flags
= bmap
->flags
;
11611 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11612 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11613 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11614 res
= isl_basic_map_alloc_space(dim
,
11615 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11616 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11618 res
->flags
= flags
;
11619 res
= isl_basic_map_gauss(res
, NULL
);
11620 res
= isl_basic_map_finalize(res
);
11624 isl_basic_map_free(bmap
);
11625 isl_space_free(dim
);
11629 /* Reorder the dimensions of "map" according to given reordering.
11631 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11632 __isl_take isl_reordering
*r
)
11635 struct isl_dim_map
*dim_map
;
11637 map
= isl_map_cow(map
);
11638 dim_map
= isl_dim_map_from_reordering(r
);
11639 if (!map
|| !r
|| !dim_map
)
11642 for (i
= 0; i
< map
->n
; ++i
) {
11643 struct isl_dim_map
*dim_map_i
;
11645 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11647 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11648 isl_space_copy(r
->dim
), dim_map_i
);
11654 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11656 isl_reordering_free(r
);
11662 isl_reordering_free(r
);
11666 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11667 __isl_take isl_reordering
*r
)
11669 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11672 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11673 __isl_take isl_space
*model
)
11678 if (!map
|| !model
)
11681 ctx
= isl_space_get_ctx(model
);
11682 if (!isl_space_has_named_params(model
))
11683 isl_die(ctx
, isl_error_invalid
,
11684 "model has unnamed parameters", goto error
);
11685 if (isl_map_check_named_params(map
) < 0)
11687 aligned
= isl_map_space_has_equal_params(map
, model
);
11691 isl_reordering
*exp
;
11693 model
= isl_space_drop_dims(model
, isl_dim_in
,
11694 0, isl_space_dim(model
, isl_dim_in
));
11695 model
= isl_space_drop_dims(model
, isl_dim_out
,
11696 0, isl_space_dim(model
, isl_dim_out
));
11697 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11698 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11699 map
= isl_map_realign(map
, exp
);
11702 isl_space_free(model
);
11705 isl_space_free(model
);
11710 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11711 __isl_take isl_space
*model
)
11713 return isl_map_align_params(set
, model
);
11716 /* Align the parameters of "bmap" to those of "model", introducing
11717 * additional parameters if needed.
11719 __isl_give isl_basic_map
*isl_basic_map_align_params(
11720 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11723 isl_bool equal_params
;
11725 if (!bmap
|| !model
)
11728 ctx
= isl_space_get_ctx(model
);
11729 if (!isl_space_has_named_params(model
))
11730 isl_die(ctx
, isl_error_invalid
,
11731 "model has unnamed parameters", goto error
);
11732 if (!isl_space_has_named_params(bmap
->dim
))
11733 isl_die(ctx
, isl_error_invalid
,
11734 "relation has unnamed parameters", goto error
);
11735 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11736 if (equal_params
< 0)
11738 if (!equal_params
) {
11739 isl_reordering
*exp
;
11740 struct isl_dim_map
*dim_map
;
11742 model
= isl_space_drop_dims(model
, isl_dim_in
,
11743 0, isl_space_dim(model
, isl_dim_in
));
11744 model
= isl_space_drop_dims(model
, isl_dim_out
,
11745 0, isl_space_dim(model
, isl_dim_out
));
11746 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11747 exp
= isl_reordering_extend_space(exp
,
11748 isl_basic_map_get_space(bmap
));
11749 dim_map
= isl_dim_map_from_reordering(exp
);
11750 bmap
= isl_basic_map_realign(bmap
,
11751 exp
? isl_space_copy(exp
->dim
) : NULL
,
11752 isl_dim_map_extend(dim_map
, bmap
));
11753 isl_reordering_free(exp
);
11757 isl_space_free(model
);
11760 isl_space_free(model
);
11761 isl_basic_map_free(bmap
);
11765 /* Do "bset" and "space" have the same parameters?
11767 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11768 __isl_keep isl_space
*space
)
11770 isl_space
*bset_space
;
11772 bset_space
= isl_basic_set_peek_space(bset
);
11773 return isl_space_has_equal_params(bset_space
, space
);
11776 /* Do "map" and "space" have the same parameters?
11778 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11779 __isl_keep isl_space
*space
)
11781 isl_space
*map_space
;
11783 map_space
= isl_map_peek_space(map
);
11784 return isl_space_has_equal_params(map_space
, space
);
11787 /* Do "set" and "space" have the same parameters?
11789 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11790 __isl_keep isl_space
*space
)
11792 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11795 /* Align the parameters of "bset" to those of "model", introducing
11796 * additional parameters if needed.
11798 __isl_give isl_basic_set
*isl_basic_set_align_params(
11799 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11801 return isl_basic_map_align_params(bset
, model
);
11804 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11805 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11806 enum isl_dim_type c2
, enum isl_dim_type c3
,
11807 enum isl_dim_type c4
, enum isl_dim_type c5
)
11809 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11810 struct isl_mat
*mat
;
11816 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11817 isl_basic_map_total_dim(bmap
) + 1);
11820 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11821 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11822 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11823 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11824 isl_int_set(mat
->row
[i
][pos
],
11825 bmap
->eq
[i
][off
+ k
]);
11833 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11834 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11835 enum isl_dim_type c2
, enum isl_dim_type c3
,
11836 enum isl_dim_type c4
, enum isl_dim_type c5
)
11838 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11839 struct isl_mat
*mat
;
11845 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11846 isl_basic_map_total_dim(bmap
) + 1);
11849 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11850 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11851 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11852 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11853 isl_int_set(mat
->row
[i
][pos
],
11854 bmap
->ineq
[i
][off
+ k
]);
11862 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11863 __isl_take isl_space
*dim
,
11864 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11865 enum isl_dim_type c2
, enum isl_dim_type c3
,
11866 enum isl_dim_type c4
, enum isl_dim_type c5
)
11868 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11869 isl_basic_map
*bmap
;
11875 if (!dim
|| !eq
|| !ineq
)
11878 if (eq
->n_col
!= ineq
->n_col
)
11879 isl_die(dim
->ctx
, isl_error_invalid
,
11880 "equalities and inequalities matrices should have "
11881 "same number of columns", goto error
);
11883 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11885 if (eq
->n_col
< total
)
11886 isl_die(dim
->ctx
, isl_error_invalid
,
11887 "number of columns too small", goto error
);
11889 extra
= eq
->n_col
- total
;
11891 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11892 eq
->n_row
, ineq
->n_row
);
11895 for (i
= 0; i
< extra
; ++i
) {
11896 k
= isl_basic_map_alloc_div(bmap
);
11899 isl_int_set_si(bmap
->div
[k
][0], 0);
11901 for (i
= 0; i
< eq
->n_row
; ++i
) {
11902 l
= isl_basic_map_alloc_equality(bmap
);
11905 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11906 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11907 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11908 isl_int_set(bmap
->eq
[l
][off
+ k
],
11914 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11915 l
= isl_basic_map_alloc_inequality(bmap
);
11918 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11919 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11920 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11921 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11922 ineq
->row
[i
][pos
]);
11928 isl_space_free(dim
);
11930 isl_mat_free(ineq
);
11932 bmap
= isl_basic_map_simplify(bmap
);
11933 return isl_basic_map_finalize(bmap
);
11935 isl_space_free(dim
);
11937 isl_mat_free(ineq
);
11941 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11942 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11943 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11945 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11946 c1
, c2
, c3
, c4
, isl_dim_in
);
11949 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11950 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11951 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11953 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11954 c1
, c2
, c3
, c4
, isl_dim_in
);
11957 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11958 __isl_take isl_space
*dim
,
11959 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11960 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11962 isl_basic_map
*bmap
;
11963 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11964 c1
, c2
, c3
, c4
, isl_dim_in
);
11965 return bset_from_bmap(bmap
);
11968 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11971 return isl_bool_error
;
11973 return isl_space_can_zip(bmap
->dim
);
11976 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11979 return isl_bool_error
;
11981 return isl_space_can_zip(map
->dim
);
11984 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11985 * (A -> C) -> (B -> D).
11987 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11996 if (!isl_basic_map_can_zip(bmap
))
11997 isl_die(bmap
->ctx
, isl_error_invalid
,
11998 "basic map cannot be zipped", goto error
);
11999 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12000 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12001 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12002 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12003 bmap
= isl_basic_map_cow(bmap
);
12004 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12007 bmap
->dim
= isl_space_zip(bmap
->dim
);
12010 bmap
= isl_basic_map_mark_final(bmap
);
12013 isl_basic_map_free(bmap
);
12017 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12018 * (A -> C) -> (B -> D).
12020 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12027 if (!isl_map_can_zip(map
))
12028 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12031 map
= isl_map_cow(map
);
12035 for (i
= 0; i
< map
->n
; ++i
) {
12036 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12041 map
->dim
= isl_space_zip(map
->dim
);
12051 /* Can we apply isl_basic_map_curry to "bmap"?
12052 * That is, does it have a nested relation in its domain?
12054 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12057 return isl_bool_error
;
12059 return isl_space_can_curry(bmap
->dim
);
12062 /* Can we apply isl_map_curry to "map"?
12063 * That is, does it have a nested relation in its domain?
12065 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12068 return isl_bool_error
;
12070 return isl_space_can_curry(map
->dim
);
12073 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12076 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12082 if (!isl_basic_map_can_curry(bmap
))
12083 isl_die(bmap
->ctx
, isl_error_invalid
,
12084 "basic map cannot be curried", goto error
);
12085 bmap
= isl_basic_map_cow(bmap
);
12088 bmap
->dim
= isl_space_curry(bmap
->dim
);
12091 bmap
= isl_basic_map_mark_final(bmap
);
12094 isl_basic_map_free(bmap
);
12098 /* Given a map (A -> B) -> C, return the corresponding map
12101 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12103 return isl_map_change_space(map
, &isl_map_can_curry
,
12104 "map cannot be curried", &isl_space_curry
);
12107 /* Can isl_map_range_curry be applied to "map"?
12108 * That is, does it have a nested relation in its range,
12109 * the domain of which is itself a nested relation?
12111 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12114 return isl_bool_error
;
12116 return isl_space_can_range_curry(map
->dim
);
12119 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12120 * A -> (B -> (C -> D)).
12122 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12124 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12125 "map range cannot be curried",
12126 &isl_space_range_curry
);
12129 /* Can we apply isl_basic_map_uncurry to "bmap"?
12130 * That is, does it have a nested relation in its domain?
12132 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12135 return isl_bool_error
;
12137 return isl_space_can_uncurry(bmap
->dim
);
12140 /* Can we apply isl_map_uncurry to "map"?
12141 * That is, does it have a nested relation in its domain?
12143 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12146 return isl_bool_error
;
12148 return isl_space_can_uncurry(map
->dim
);
12151 /* Given a basic map A -> (B -> C), return the corresponding basic map
12154 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12160 if (!isl_basic_map_can_uncurry(bmap
))
12161 isl_die(bmap
->ctx
, isl_error_invalid
,
12162 "basic map cannot be uncurried",
12163 return isl_basic_map_free(bmap
));
12164 bmap
= isl_basic_map_cow(bmap
);
12167 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12169 return isl_basic_map_free(bmap
);
12170 bmap
= isl_basic_map_mark_final(bmap
);
12174 /* Given a map A -> (B -> C), return the corresponding map
12177 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12179 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12180 "map cannot be uncurried", &isl_space_uncurry
);
12183 /* Construct a basic map mapping the domain of the affine expression
12184 * to a one-dimensional range prescribed by the affine expression.
12185 * If "rational" is set, then construct a rational basic map.
12187 * A NaN affine expression cannot be converted to a basic map.
12189 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12190 __isl_take isl_aff
*aff
, int rational
)
12195 isl_local_space
*ls
;
12196 isl_basic_map
*bmap
= NULL
;
12200 is_nan
= isl_aff_is_nan(aff
);
12204 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12205 "cannot convert NaN", goto error
);
12207 ls
= isl_aff_get_local_space(aff
);
12208 bmap
= isl_basic_map_from_local_space(ls
);
12209 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12210 k
= isl_basic_map_alloc_equality(bmap
);
12214 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12215 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12216 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12217 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12218 aff
->v
->size
- (pos
+ 1));
12222 bmap
= isl_basic_map_set_rational(bmap
);
12223 bmap
= isl_basic_map_gauss(bmap
, NULL
);
12224 bmap
= isl_basic_map_finalize(bmap
);
12228 isl_basic_map_free(bmap
);
12232 /* Construct a basic map mapping the domain of the affine expression
12233 * to a one-dimensional range prescribed by the affine expression.
12235 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12237 return isl_basic_map_from_aff2(aff
, 0);
12240 /* Construct a map mapping the domain of the affine expression
12241 * to a one-dimensional range prescribed by the affine expression.
12243 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12245 isl_basic_map
*bmap
;
12247 bmap
= isl_basic_map_from_aff(aff
);
12248 return isl_map_from_basic_map(bmap
);
12251 /* Construct a basic map mapping the domain the multi-affine expression
12252 * to its range, with each dimension in the range equated to the
12253 * corresponding affine expression.
12254 * If "rational" is set, then construct a rational basic map.
12256 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12257 __isl_take isl_multi_aff
*maff
, int rational
)
12261 isl_basic_map
*bmap
;
12266 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12267 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12268 "invalid space", goto error
);
12270 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12271 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12273 bmap
= isl_basic_map_set_rational(bmap
);
12275 for (i
= 0; i
< maff
->n
; ++i
) {
12277 isl_basic_map
*bmap_i
;
12279 aff
= isl_aff_copy(maff
->p
[i
]);
12280 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12282 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12285 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12287 isl_multi_aff_free(maff
);
12290 isl_multi_aff_free(maff
);
12294 /* Construct a basic map mapping the domain the multi-affine expression
12295 * to its range, with each dimension in the range equated to the
12296 * corresponding affine expression.
12298 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12299 __isl_take isl_multi_aff
*ma
)
12301 return isl_basic_map_from_multi_aff2(ma
, 0);
12304 /* Construct a map mapping the domain the multi-affine expression
12305 * to its range, with each dimension in the range equated to the
12306 * corresponding affine expression.
12308 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12310 isl_basic_map
*bmap
;
12312 bmap
= isl_basic_map_from_multi_aff(maff
);
12313 return isl_map_from_basic_map(bmap
);
12316 /* Construct a basic map mapping a domain in the given space to
12317 * to an n-dimensional range, with n the number of elements in the list,
12318 * where each coordinate in the range is prescribed by the
12319 * corresponding affine expression.
12320 * The domains of all affine expressions in the list are assumed to match
12323 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12324 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12328 isl_basic_map
*bmap
;
12333 dim
= isl_space_from_domain(domain_dim
);
12334 bmap
= isl_basic_map_universe(dim
);
12336 for (i
= 0; i
< list
->n
; ++i
) {
12338 isl_basic_map
*bmap_i
;
12340 aff
= isl_aff_copy(list
->p
[i
]);
12341 bmap_i
= isl_basic_map_from_aff(aff
);
12343 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12346 isl_aff_list_free(list
);
12350 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12351 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12353 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12356 /* Construct a basic map where the given dimensions are equal to each other.
12358 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12359 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12361 isl_basic_map
*bmap
= NULL
;
12367 if (pos1
>= isl_space_dim(space
, type1
))
12368 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12369 "index out of bounds", goto error
);
12370 if (pos2
>= isl_space_dim(space
, type2
))
12371 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12372 "index out of bounds", goto error
);
12374 if (type1
== type2
&& pos1
== pos2
)
12375 return isl_basic_map_universe(space
);
12377 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12378 i
= isl_basic_map_alloc_equality(bmap
);
12381 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12382 pos1
+= isl_basic_map_offset(bmap
, type1
);
12383 pos2
+= isl_basic_map_offset(bmap
, type2
);
12384 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12385 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12386 bmap
= isl_basic_map_finalize(bmap
);
12387 isl_space_free(space
);
12390 isl_space_free(space
);
12391 isl_basic_map_free(bmap
);
12395 /* Add a constraint imposing that the given two dimensions are equal.
12397 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12398 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12402 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12404 bmap
= isl_basic_map_intersect(bmap
, eq
);
12409 /* Add a constraint imposing that the given two dimensions are equal.
12411 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12412 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12414 isl_basic_map
*bmap
;
12416 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12418 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12423 /* Add a constraint imposing that the given two dimensions have opposite values.
12425 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12426 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12428 isl_basic_map
*bmap
= NULL
;
12434 if (pos1
>= isl_map_dim(map
, type1
))
12435 isl_die(map
->ctx
, isl_error_invalid
,
12436 "index out of bounds", goto error
);
12437 if (pos2
>= isl_map_dim(map
, type2
))
12438 isl_die(map
->ctx
, isl_error_invalid
,
12439 "index out of bounds", goto error
);
12441 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12442 i
= isl_basic_map_alloc_equality(bmap
);
12445 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12446 pos1
+= isl_basic_map_offset(bmap
, type1
);
12447 pos2
+= isl_basic_map_offset(bmap
, type2
);
12448 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12449 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12450 bmap
= isl_basic_map_finalize(bmap
);
12452 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12456 isl_basic_map_free(bmap
);
12461 /* Construct a constraint imposing that the value of the first dimension is
12462 * greater than or equal to that of the second.
12464 static __isl_give isl_constraint
*constraint_order_ge(
12465 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12466 enum isl_dim_type type2
, int pos2
)
12473 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12475 if (pos1
>= isl_constraint_dim(c
, type1
))
12476 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12477 "index out of bounds", return isl_constraint_free(c
));
12478 if (pos2
>= isl_constraint_dim(c
, type2
))
12479 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12480 "index out of bounds", return isl_constraint_free(c
));
12482 if (type1
== type2
&& pos1
== pos2
)
12485 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12486 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12491 /* Add a constraint imposing that the value of the first dimension is
12492 * greater than or equal to that of the second.
12494 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12495 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12500 if (type1
== type2
&& pos1
== pos2
)
12502 space
= isl_basic_map_get_space(bmap
);
12503 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12504 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12509 /* Add a constraint imposing that the value of the first dimension is
12510 * greater than or equal to that of the second.
12512 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12513 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12518 if (type1
== type2
&& pos1
== pos2
)
12520 space
= isl_map_get_space(map
);
12521 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12522 map
= isl_map_add_constraint(map
, c
);
12527 /* Add a constraint imposing that the value of the first dimension is
12528 * less than or equal to that of the second.
12530 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12531 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12533 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12536 /* Construct a basic map where the value of the first dimension is
12537 * greater than that of the second.
12539 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12540 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12542 isl_basic_map
*bmap
= NULL
;
12548 if (pos1
>= isl_space_dim(space
, type1
))
12549 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12550 "index out of bounds", goto error
);
12551 if (pos2
>= isl_space_dim(space
, type2
))
12552 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12553 "index out of bounds", goto error
);
12555 if (type1
== type2
&& pos1
== pos2
)
12556 return isl_basic_map_empty(space
);
12558 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12559 i
= isl_basic_map_alloc_inequality(bmap
);
12561 return isl_basic_map_free(bmap
);
12562 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12563 pos1
+= isl_basic_map_offset(bmap
, type1
);
12564 pos2
+= isl_basic_map_offset(bmap
, type2
);
12565 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12566 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12567 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12568 bmap
= isl_basic_map_finalize(bmap
);
12572 isl_space_free(space
);
12573 isl_basic_map_free(bmap
);
12577 /* Add a constraint imposing that the value of the first dimension is
12578 * greater than that of the second.
12580 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12581 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12585 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12587 bmap
= isl_basic_map_intersect(bmap
, gt
);
12592 /* Add a constraint imposing that the value of the first dimension is
12593 * greater than that of the second.
12595 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12596 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12598 isl_basic_map
*bmap
;
12600 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12602 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12607 /* Add a constraint imposing that the value of the first dimension is
12608 * smaller than that of the second.
12610 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12611 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12613 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12616 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12620 isl_local_space
*ls
;
12625 if (!isl_basic_map_divs_known(bmap
))
12626 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12627 "some divs are unknown", return NULL
);
12629 ls
= isl_basic_map_get_local_space(bmap
);
12630 div
= isl_local_space_get_div(ls
, pos
);
12631 isl_local_space_free(ls
);
12636 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12639 return isl_basic_map_get_div(bset
, pos
);
12642 /* Plug in "subs" for dimension "type", "pos" of "bset".
12644 * Let i be the dimension to replace and let "subs" be of the form
12648 * Any integer division with a non-zero coefficient for i,
12650 * floor((a i + g)/m)
12654 * floor((a f + d g)/(m d))
12656 * Constraints of the form
12664 * We currently require that "subs" is an integral expression.
12665 * Handling rational expressions may require us to add stride constraints
12666 * as we do in isl_basic_set_preimage_multi_aff.
12668 __isl_give isl_basic_set
*isl_basic_set_substitute(
12669 __isl_take isl_basic_set
*bset
,
12670 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12676 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12679 bset
= isl_basic_set_cow(bset
);
12680 if (!bset
|| !subs
)
12683 ctx
= isl_basic_set_get_ctx(bset
);
12684 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12685 isl_die(ctx
, isl_error_invalid
,
12686 "spaces don't match", goto error
);
12687 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12688 isl_die(ctx
, isl_error_unsupported
,
12689 "cannot handle divs yet", goto error
);
12690 if (!isl_int_is_one(subs
->v
->el
[0]))
12691 isl_die(ctx
, isl_error_invalid
,
12692 "can only substitute integer expressions", goto error
);
12694 pos
+= isl_basic_set_offset(bset
, type
);
12698 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12699 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12701 isl_int_set(v
, bset
->eq
[i
][pos
]);
12702 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12703 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12704 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12707 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12708 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12710 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12711 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12712 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12713 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12716 for (i
= 0; i
< bset
->n_div
; ++i
) {
12717 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12719 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12720 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12721 isl_seq_combine(bset
->div
[i
] + 1,
12722 subs
->v
->el
[0], bset
->div
[i
] + 1,
12723 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12724 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12729 bset
= isl_basic_set_simplify(bset
);
12730 return isl_basic_set_finalize(bset
);
12732 isl_basic_set_free(bset
);
12736 /* Plug in "subs" for dimension "type", "pos" of "set".
12738 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12739 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12743 if (set
&& isl_set_plain_is_empty(set
))
12746 set
= isl_set_cow(set
);
12750 for (i
= set
->n
- 1; i
>= 0; --i
) {
12751 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12752 if (remove_if_empty(set
, i
) < 0)
12762 /* Check if the range of "ma" is compatible with the domain or range
12763 * (depending on "type") of "bmap".
12765 static isl_stat
check_basic_map_compatible_range_multi_aff(
12766 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12767 __isl_keep isl_multi_aff
*ma
)
12770 isl_space
*ma_space
;
12772 ma_space
= isl_multi_aff_get_space(ma
);
12774 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12778 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12779 "parameters don't match", goto error
);
12780 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12784 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12785 "spaces don't match", goto error
);
12787 isl_space_free(ma_space
);
12788 return isl_stat_ok
;
12790 isl_space_free(ma_space
);
12791 return isl_stat_error
;
12794 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12795 * coefficients before the transformed range of dimensions,
12796 * the "n_after" coefficients after the transformed range of dimensions
12797 * and the coefficients of the other divs in "bmap".
12799 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12800 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12805 isl_local_space
*ls
;
12810 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12814 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12815 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12816 for (i
= 0; i
< n_div
; ++i
) {
12817 int o_bmap
= 0, o_ls
= 0;
12819 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12820 o_bmap
+= 1 + 1 + n_param
;
12821 o_ls
+= 1 + 1 + n_param
;
12822 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12823 o_bmap
+= n_before
;
12824 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12825 ls
->div
->row
[i
] + o_ls
, n_set
);
12828 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12830 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12831 ls
->div
->row
[i
] + o_ls
, n_div
);
12834 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12835 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12839 isl_local_space_free(ls
);
12842 isl_local_space_free(ls
);
12846 /* How many stride constraints does "ma" enforce?
12847 * That is, how many of the affine expressions have a denominator
12848 * different from one?
12850 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12855 for (i
= 0; i
< ma
->n
; ++i
)
12856 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12862 /* For each affine expression in ma of the form
12864 * x_i = (f_i y + h_i)/m_i
12866 * with m_i different from one, add a constraint to "bmap"
12869 * f_i y + h_i = m_i alpha_i
12871 * with alpha_i an additional existentially quantified variable.
12873 * The input variables of "ma" correspond to a subset of the variables
12874 * of "bmap". There are "n_before" variables in "bmap" before this
12875 * subset and "n_after" variables after this subset.
12876 * The integer divisions of the affine expressions in "ma" are assumed
12877 * to have been aligned. There are "n_div_ma" of them and
12878 * they appear first in "bmap", straight after the "n_after" variables.
12880 static __isl_give isl_basic_map
*add_ma_strides(
12881 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12882 int n_before
, int n_after
, int n_div_ma
)
12890 total
= isl_basic_map_total_dim(bmap
);
12891 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12892 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12893 for (i
= 0; i
< ma
->n
; ++i
) {
12894 int o_bmap
= 0, o_ma
= 1;
12896 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12898 div
= isl_basic_map_alloc_div(bmap
);
12899 k
= isl_basic_map_alloc_equality(bmap
);
12900 if (div
< 0 || k
< 0)
12902 isl_int_set_si(bmap
->div
[div
][0], 0);
12903 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12904 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12905 o_bmap
+= 1 + n_param
;
12906 o_ma
+= 1 + n_param
;
12907 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12908 o_bmap
+= n_before
;
12909 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12910 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12913 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12915 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12916 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12917 o_bmap
+= n_div_ma
;
12919 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12920 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12926 isl_basic_map_free(bmap
);
12930 /* Replace the domain or range space (depending on "type) of "space" by "set".
12932 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12933 enum isl_dim_type type
, __isl_take isl_space
*set
)
12935 if (type
== isl_dim_in
) {
12936 space
= isl_space_range(space
);
12937 space
= isl_space_map_from_domain_and_range(set
, space
);
12939 space
= isl_space_domain(space
);
12940 space
= isl_space_map_from_domain_and_range(space
, set
);
12946 /* Compute the preimage of the domain or range (depending on "type")
12947 * of "bmap" under the function represented by "ma".
12948 * In other words, plug in "ma" in the domain or range of "bmap".
12949 * The result is a basic map that lives in the same space as "bmap"
12950 * except that the domain or range has been replaced by
12951 * the domain space of "ma".
12953 * If bmap is represented by
12955 * A(p) + S u + B x + T v + C(divs) >= 0,
12957 * where u and x are input and output dimensions if type == isl_dim_out
12958 * while x and v are input and output dimensions if type == isl_dim_in,
12959 * and ma is represented by
12961 * x = D(p) + F(y) + G(divs')
12963 * then the result is
12965 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12967 * The divs in the input set are similarly adjusted.
12970 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12974 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12975 * B_i G(divs') + c_i(divs))/n_i)
12977 * If bmap is not a rational map and if F(y) involves any denominators
12979 * x_i = (f_i y + h_i)/m_i
12981 * then additional constraints are added to ensure that we only
12982 * map back integer points. That is we enforce
12984 * f_i y + h_i = m_i alpha_i
12986 * with alpha_i an additional existentially quantified variable.
12988 * We first copy over the divs from "ma".
12989 * Then we add the modified constraints and divs from "bmap".
12990 * Finally, we add the stride constraints, if needed.
12992 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12993 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12994 __isl_take isl_multi_aff
*ma
)
12998 isl_basic_map
*res
= NULL
;
12999 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
13000 isl_int f
, c1
, c2
, g
;
13009 ma
= isl_multi_aff_align_divs(ma
);
13012 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13015 if (type
== isl_dim_in
) {
13017 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13019 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13022 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13023 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
13025 space
= isl_multi_aff_get_domain_space(ma
);
13026 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13027 rational
= isl_basic_map_is_rational(bmap
);
13028 strides
= rational
? 0 : multi_aff_strides(ma
);
13029 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13030 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13032 res
= isl_basic_map_set_rational(res
);
13034 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13035 if (isl_basic_map_alloc_div(res
) < 0)
13038 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
13041 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13042 k
= isl_basic_map_alloc_equality(res
);
13045 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13046 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13049 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13050 k
= isl_basic_map_alloc_inequality(res
);
13053 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13054 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13057 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13058 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13059 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13062 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13063 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13068 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13074 isl_basic_map_free(bmap
);
13075 isl_multi_aff_free(ma
);
13076 res
= isl_basic_map_simplify(res
);
13077 return isl_basic_map_finalize(res
);
13083 isl_basic_map_free(bmap
);
13084 isl_multi_aff_free(ma
);
13085 isl_basic_map_free(res
);
13089 /* Compute the preimage of "bset" under the function represented by "ma".
13090 * In other words, plug in "ma" in "bset". The result is a basic set
13091 * that lives in the domain space of "ma".
13093 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13094 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13096 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13099 /* Compute the preimage of the domain of "bmap" under the function
13100 * represented by "ma".
13101 * In other words, plug in "ma" in the domain of "bmap".
13102 * The result is a basic map that lives in the same space as "bmap"
13103 * except that the domain has been replaced by the domain space of "ma".
13105 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13106 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13108 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13111 /* Compute the preimage of the range of "bmap" under the function
13112 * represented by "ma".
13113 * In other words, plug in "ma" in the range of "bmap".
13114 * The result is a basic map that lives in the same space as "bmap"
13115 * except that the range has been replaced by the domain space of "ma".
13117 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13118 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13120 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13123 /* Check if the range of "ma" is compatible with the domain or range
13124 * (depending on "type") of "map".
13125 * Return isl_stat_error if anything is wrong.
13127 static isl_stat
check_map_compatible_range_multi_aff(
13128 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13129 __isl_keep isl_multi_aff
*ma
)
13132 isl_space
*ma_space
;
13134 ma_space
= isl_multi_aff_get_space(ma
);
13135 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13136 isl_space_free(ma_space
);
13138 return isl_stat_error
;
13140 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13141 "spaces don't match", return isl_stat_error
);
13142 return isl_stat_ok
;
13145 /* Compute the preimage of the domain or range (depending on "type")
13146 * of "map" under the function represented by "ma".
13147 * In other words, plug in "ma" in the domain or range of "map".
13148 * The result is a map that lives in the same space as "map"
13149 * except that the domain or range has been replaced by
13150 * the domain space of "ma".
13152 * The parameters are assumed to have been aligned.
13154 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13155 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13160 map
= isl_map_cow(map
);
13161 ma
= isl_multi_aff_align_divs(ma
);
13164 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13167 for (i
= 0; i
< map
->n
; ++i
) {
13168 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13169 isl_multi_aff_copy(ma
));
13174 space
= isl_multi_aff_get_domain_space(ma
);
13175 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13177 isl_space_free(map
->dim
);
13182 isl_multi_aff_free(ma
);
13184 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13185 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13188 isl_multi_aff_free(ma
);
13193 /* Compute the preimage of the domain or range (depending on "type")
13194 * of "map" under the function represented by "ma".
13195 * In other words, plug in "ma" in the domain or range of "map".
13196 * The result is a map that lives in the same space as "map"
13197 * except that the domain or range has been replaced by
13198 * the domain space of "ma".
13200 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13201 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13208 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13212 return map_preimage_multi_aff(map
, type
, ma
);
13214 if (isl_map_check_named_params(map
) < 0)
13216 if (!isl_space_has_named_params(ma
->space
))
13217 isl_die(map
->ctx
, isl_error_invalid
,
13218 "unaligned unnamed parameters", goto error
);
13219 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13220 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13222 return map_preimage_multi_aff(map
, type
, ma
);
13224 isl_multi_aff_free(ma
);
13225 return isl_map_free(map
);
13228 /* Compute the preimage of "set" under the function represented by "ma".
13229 * In other words, plug in "ma" in "set". The result is a set
13230 * that lives in the domain space of "ma".
13232 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13233 __isl_take isl_multi_aff
*ma
)
13235 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13238 /* Compute the preimage of the domain of "map" under the function
13239 * represented by "ma".
13240 * In other words, plug in "ma" in the domain of "map".
13241 * The result is a map that lives in the same space as "map"
13242 * except that the domain has been replaced by the domain space of "ma".
13244 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13245 __isl_take isl_multi_aff
*ma
)
13247 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13250 /* Compute the preimage of the range of "map" under the function
13251 * represented by "ma".
13252 * In other words, plug in "ma" in the range of "map".
13253 * The result is a map that lives in the same space as "map"
13254 * except that the range has been replaced by the domain space of "ma".
13256 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13257 __isl_take isl_multi_aff
*ma
)
13259 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13262 /* Compute the preimage of "map" under the function represented by "pma".
13263 * In other words, plug in "pma" in the domain or range of "map".
13264 * The result is a map that lives in the same space as "map",
13265 * except that the space of type "type" has been replaced by
13266 * the domain space of "pma".
13268 * The parameters of "map" and "pma" are assumed to have been aligned.
13270 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13271 __isl_take isl_map
*map
, enum isl_dim_type type
,
13272 __isl_take isl_pw_multi_aff
*pma
)
13281 isl_pw_multi_aff_free(pma
);
13282 res
= isl_map_empty(isl_map_get_space(map
));
13287 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13288 isl_multi_aff_copy(pma
->p
[0].maff
));
13289 if (type
== isl_dim_in
)
13290 res
= isl_map_intersect_domain(res
,
13291 isl_map_copy(pma
->p
[0].set
));
13293 res
= isl_map_intersect_range(res
,
13294 isl_map_copy(pma
->p
[0].set
));
13296 for (i
= 1; i
< pma
->n
; ++i
) {
13299 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13300 isl_multi_aff_copy(pma
->p
[i
].maff
));
13301 if (type
== isl_dim_in
)
13302 res_i
= isl_map_intersect_domain(res_i
,
13303 isl_map_copy(pma
->p
[i
].set
));
13305 res_i
= isl_map_intersect_range(res_i
,
13306 isl_map_copy(pma
->p
[i
].set
));
13307 res
= isl_map_union(res
, res_i
);
13310 isl_pw_multi_aff_free(pma
);
13314 isl_pw_multi_aff_free(pma
);
13319 /* Compute the preimage of "map" under the function represented by "pma".
13320 * In other words, plug in "pma" in the domain or range of "map".
13321 * The result is a map that lives in the same space as "map",
13322 * except that the space of type "type" has been replaced by
13323 * the domain space of "pma".
13325 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13326 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13333 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13337 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13339 if (isl_map_check_named_params(map
) < 0)
13341 if (!isl_space_has_named_params(pma
->dim
))
13342 isl_die(map
->ctx
, isl_error_invalid
,
13343 "unaligned unnamed parameters", goto error
);
13344 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13345 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13347 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13349 isl_pw_multi_aff_free(pma
);
13350 return isl_map_free(map
);
13353 /* Compute the preimage of "set" under the function represented by "pma".
13354 * In other words, plug in "pma" in "set". The result is a set
13355 * that lives in the domain space of "pma".
13357 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13358 __isl_take isl_pw_multi_aff
*pma
)
13360 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13363 /* Compute the preimage of the domain of "map" under the function
13364 * represented by "pma".
13365 * In other words, plug in "pma" in the domain of "map".
13366 * The result is a map that lives in the same space as "map",
13367 * except that domain space has been replaced by the domain space of "pma".
13369 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13370 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13372 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13375 /* Compute the preimage of the range of "map" under the function
13376 * represented by "pma".
13377 * In other words, plug in "pma" in the range of "map".
13378 * The result is a map that lives in the same space as "map",
13379 * except that range space has been replaced by the domain space of "pma".
13381 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13382 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13384 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13387 /* Compute the preimage of "map" under the function represented by "mpa".
13388 * In other words, plug in "mpa" in the domain or range of "map".
13389 * The result is a map that lives in the same space as "map",
13390 * except that the space of type "type" has been replaced by
13391 * the domain space of "mpa".
13393 * If the map does not involve any constraints that refer to the
13394 * dimensions of the substituted space, then the only possible
13395 * effect of "mpa" on the map is to map the space to a different space.
13396 * We create a separate isl_multi_aff to effectuate this change
13397 * in order to avoid spurious splitting of the map along the pieces
13400 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13401 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13404 isl_pw_multi_aff
*pma
;
13409 n
= isl_map_dim(map
, type
);
13410 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13414 space
= isl_multi_pw_aff_get_space(mpa
);
13415 isl_multi_pw_aff_free(mpa
);
13416 ma
= isl_multi_aff_zero(space
);
13417 return isl_map_preimage_multi_aff(map
, type
, ma
);
13420 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13421 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13424 isl_multi_pw_aff_free(mpa
);
13428 /* Compute the preimage of "map" under the function represented by "mpa".
13429 * In other words, plug in "mpa" in the domain "map".
13430 * The result is a map that lives in the same space as "map",
13431 * except that domain space has been replaced by the domain space of "mpa".
13433 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13434 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13436 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13439 /* Compute the preimage of "set" by the function represented by "mpa".
13440 * In other words, plug in "mpa" in "set".
13442 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13443 __isl_take isl_multi_pw_aff
*mpa
)
13445 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13448 /* Return a copy of the equality constraints of "bset" as a matrix.
13450 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13451 __isl_keep isl_basic_set
*bset
)
13459 ctx
= isl_basic_set_get_ctx(bset
);
13460 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13461 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13464 /* Are the "n" "coefficients" starting at "first" of the integer division
13465 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13467 * The "coefficient" at position 0 is the denominator.
13468 * The "coefficient" at position 1 is the constant term.
13470 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13471 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13472 unsigned first
, unsigned n
)
13474 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13475 return isl_bool_error
;
13476 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13477 return isl_bool_error
;
13478 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13479 bmap2
->div
[pos2
] + first
, n
);
13482 /* Are the integer division expressions at position "pos1" in "bmap1" and
13483 * "pos2" in "bmap2" equal to each other, except that the constant terms
13486 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13487 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13488 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13493 if (!bmap1
|| !bmap2
)
13494 return isl_bool_error
;
13495 total
= isl_basic_map_total_dim(bmap1
);
13496 if (total
!= isl_basic_map_total_dim(bmap2
))
13497 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13498 "incomparable div expressions", return isl_bool_error
);
13499 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13501 if (equal
< 0 || !equal
)
13503 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13505 if (equal
< 0 || equal
)
13506 return isl_bool_not(equal
);
13507 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13511 /* Replace the numerator of the constant term of the integer division
13512 * expression at position "div" in "bmap" by "value".
13513 * The caller guarantees that this does not change the meaning
13516 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13517 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13519 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13520 return isl_basic_map_free(bmap
);
13522 isl_int_set_si(bmap
->div
[div
][1], value
);
13527 /* Is the point "inner" internal to inequality constraint "ineq"
13529 * The point is considered to be internal to the inequality constraint,
13530 * if it strictly lies on the positive side of the inequality constraint,
13531 * or if it lies on the constraint and the constraint is lexico-positive.
13533 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13534 __isl_keep isl_basic_set
*bset
, int ineq
)
13540 if (!inner
|| !bset
)
13541 return isl_bool_error
;
13543 ctx
= isl_basic_set_get_ctx(bset
);
13544 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13545 &ctx
->normalize_gcd
);
13546 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13547 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13549 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13550 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13551 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13554 /* Tighten the inequality constraints of "bset" that are outward with respect
13555 * to the point "vec".
13556 * That is, tighten the constraints that are not satisfied by "vec".
13558 * "vec" is a point internal to some superset S of "bset" that is used
13559 * to make the subsets of S disjoint, by tightening one half of the constraints
13560 * that separate two subsets. In particular, the constraints of S
13561 * are all satisfied by "vec" and should not be tightened.
13562 * Of the internal constraints, those that have "vec" on the outside
13563 * are tightened. The shared facet is included in the adjacent subset
13564 * with the opposite constraint.
13565 * For constraints that saturate "vec", this criterion cannot be used
13566 * to determine which of the two sides should be tightened.
13567 * Instead, the sign of the first non-zero coefficient is used
13568 * to make this choice. Note that this second criterion is never used
13569 * on the constraints of S since "vec" is interior to "S".
13571 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13572 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13576 bset
= isl_basic_set_cow(bset
);
13579 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13582 internal
= is_internal(vec
, bset
, j
);
13584 return isl_basic_set_free(bset
);
13587 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13593 /* Replace the variables x of type "type" starting at "first" in "bmap"
13594 * by x' with x = M x' with M the matrix trans.
13595 * That is, replace the corresponding coefficients c by c M.
13597 * The transformation matrix should be a square matrix.
13599 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13600 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13601 __isl_take isl_mat
*trans
)
13605 bmap
= isl_basic_map_cow(bmap
);
13606 if (!bmap
|| !trans
)
13609 if (trans
->n_row
!= trans
->n_col
)
13610 isl_die(trans
->ctx
, isl_error_invalid
,
13611 "expecting square transformation matrix", goto error
);
13612 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13613 isl_die(trans
->ctx
, isl_error_invalid
,
13614 "oversized transformation matrix", goto error
);
13616 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13618 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13619 isl_mat_copy(trans
)) < 0)
13621 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13622 isl_mat_copy(trans
)) < 0)
13624 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13625 isl_mat_copy(trans
)) < 0)
13628 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
13629 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13631 isl_mat_free(trans
);
13634 isl_mat_free(trans
);
13635 isl_basic_map_free(bmap
);
13639 /* Replace the variables x of type "type" starting at "first" in "bset"
13640 * by x' with x = M x' with M the matrix trans.
13641 * That is, replace the corresponding coefficients c by c M.
13643 * The transformation matrix should be a square matrix.
13645 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13646 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13647 __isl_take isl_mat
*trans
)
13649 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);