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(struct isl_basic_set
*bset
,
129 enum isl_dim_type type
)
131 return isl_basic_map_offset(bset
, type
);
134 static unsigned map_offset(struct 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(const struct 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(const struct isl_basic_map
*bmap
)
174 return bmap
? bmap
->dim
->n_in
: 0;
177 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
179 return bmap
? bmap
->dim
->n_out
: 0;
182 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
184 return bmap
? bmap
->dim
->nparam
: 0;
187 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
189 return bmap
? bmap
->n_div
: 0;
192 unsigned isl_basic_map_total_dim(const struct 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(const struct isl_map
*map
)
199 return map
? map
->dim
->n_in
: 0;
202 unsigned isl_map_n_out(const struct isl_map
*map
)
204 return map
? map
->dim
->n_out
: 0;
207 unsigned isl_map_n_param(const struct isl_map
*map
)
209 return map
? map
->dim
->nparam
: 0;
212 /* Do "bmap1" and "bmap2" have the same parameters?
214 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
215 __isl_keep isl_basic_map
*bmap2
)
217 isl_space
*space1
, *space2
;
219 space1
= isl_basic_map_peek_space(bmap1
);
220 space2
= isl_basic_map_peek_space(bmap2
);
221 return isl_space_match(space1
, isl_dim_param
, space2
, isl_dim_param
);
224 /* Do "map1" and "map2" have the same parameters?
226 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
227 __isl_keep isl_map
*map2
)
229 isl_space
*space1
, *space2
;
231 space1
= isl_map_peek_space(map1
);
232 space2
= isl_map_peek_space(map2
);
233 return isl_space_match(space1
, isl_dim_param
, space2
, isl_dim_param
);
236 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
237 __isl_keep isl_set
*set
)
241 return isl_bool_error
;
242 m
= isl_map_has_equal_params(map
, set_to_map(set
));
245 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
246 set
->dim
, isl_dim_set
);
249 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
250 __isl_keep isl_basic_set
*bset
)
254 return isl_bool_error
;
255 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
258 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
259 bset
->dim
, isl_dim_set
);
262 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
263 __isl_keep isl_set
*set
)
267 return isl_bool_error
;
268 m
= isl_map_has_equal_params(map
, set_to_map(set
));
271 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
272 set
->dim
, isl_dim_set
);
275 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
276 __isl_keep isl_basic_set
*bset
)
280 return isl_bool_error
;
281 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
284 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
285 bset
->dim
, isl_dim_set
);
288 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
290 return bmap
? bmap
->ctx
: NULL
;
293 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
295 return bset
? bset
->ctx
: NULL
;
298 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
300 return map
? map
->ctx
: NULL
;
303 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
305 return set
? set
->ctx
: NULL
;
308 /* Return the space of "bmap".
310 __isl_keep isl_space
*isl_basic_map_peek_space(
311 __isl_keep
const isl_basic_map
*bmap
)
313 return bmap
? bmap
->dim
: NULL
;
316 /* Return the space of "bset".
318 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
320 return isl_basic_map_peek_space(bset_to_bmap(bset
));
323 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
325 return isl_space_copy(isl_basic_map_peek_space(bmap
));
328 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
330 return isl_basic_map_get_space(bset_to_bmap(bset
));
333 /* Extract the divs in "bmap" as a matrix.
335 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
346 ctx
= isl_basic_map_get_ctx(bmap
);
347 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
348 cols
= 1 + 1 + total
+ bmap
->n_div
;
349 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
353 for (i
= 0; i
< bmap
->n_div
; ++i
)
354 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
359 /* Extract the divs in "bset" as a matrix.
361 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
363 return isl_basic_map_get_divs(bset
);
366 __isl_give isl_local_space
*isl_basic_map_get_local_space(
367 __isl_keep isl_basic_map
*bmap
)
374 div
= isl_basic_map_get_divs(bmap
);
375 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
378 __isl_give isl_local_space
*isl_basic_set_get_local_space(
379 __isl_keep isl_basic_set
*bset
)
381 return isl_basic_map_get_local_space(bset
);
384 /* For each known div d = floor(f/m), add the constraints
387 * -(f-(m-1)) + m d >= 0
389 * Do not finalize the result.
391 static __isl_give isl_basic_map
*add_known_div_constraints(
392 __isl_take isl_basic_map
*bmap
)
399 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
402 bmap
= isl_basic_map_cow(bmap
);
403 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
406 for (i
= 0; i
< n_div
; ++i
) {
407 if (isl_int_is_zero(bmap
->div
[i
][0]))
409 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
410 return isl_basic_map_free(bmap
);
416 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
417 __isl_take isl_local_space
*ls
)
426 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
427 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
428 n_div
, 0, 2 * n_div
);
430 for (i
= 0; i
< n_div
; ++i
)
431 if (isl_basic_map_alloc_div(bmap
) < 0)
434 for (i
= 0; i
< n_div
; ++i
)
435 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
436 bmap
= add_known_div_constraints(bmap
);
438 isl_local_space_free(ls
);
441 isl_local_space_free(ls
);
442 isl_basic_map_free(bmap
);
446 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
447 __isl_take isl_local_space
*ls
)
449 return isl_basic_map_from_local_space(ls
);
452 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
454 return isl_space_copy(isl_map_peek_space(map
));
457 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
461 return isl_space_copy(set
->dim
);
464 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
465 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
467 bmap
= isl_basic_map_cow(bmap
);
470 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
473 bmap
= isl_basic_map_finalize(bmap
);
476 isl_basic_map_free(bmap
);
480 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
481 __isl_take isl_basic_set
*bset
, const char *s
)
483 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
486 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
487 enum isl_dim_type type
)
489 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
492 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
493 enum isl_dim_type type
, const char *s
)
497 map
= isl_map_cow(map
);
501 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
505 for (i
= 0; i
< map
->n
; ++i
) {
506 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
517 /* Replace the identifier of the tuple of type "type" by "id".
519 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
520 __isl_take isl_basic_map
*bmap
,
521 enum isl_dim_type type
, __isl_take isl_id
*id
)
523 bmap
= isl_basic_map_cow(bmap
);
526 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
528 return isl_basic_map_free(bmap
);
529 bmap
= isl_basic_map_finalize(bmap
);
536 /* Replace the identifier of the tuple by "id".
538 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
539 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
541 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
544 /* Does the input or output tuple have a name?
546 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
548 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
551 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
552 enum isl_dim_type type
)
554 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
557 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
560 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
564 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
565 enum isl_dim_type type
, __isl_take isl_id
*id
)
567 map
= isl_map_cow(map
);
571 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
573 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
579 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
580 __isl_take isl_id
*id
)
582 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
585 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
586 enum isl_dim_type type
)
588 map
= isl_map_cow(map
);
592 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
594 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
597 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
599 return isl_map_reset_tuple_id(set
, isl_dim_set
);
602 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
604 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
607 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
608 enum isl_dim_type type
)
610 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
613 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
615 return isl_map_has_tuple_id(set
, isl_dim_set
);
618 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
620 return isl_map_get_tuple_id(set
, isl_dim_set
);
623 /* Does the set tuple have a name?
625 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
628 return isl_bool_error
;
629 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
633 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
635 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
638 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
640 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
643 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
644 enum isl_dim_type type
, unsigned pos
)
646 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
649 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
650 enum isl_dim_type type
, unsigned pos
)
652 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
655 /* Does the given dimension have a name?
657 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
658 enum isl_dim_type type
, unsigned pos
)
661 return isl_bool_error
;
662 return isl_space_has_dim_name(map
->dim
, type
, pos
);
665 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
666 enum isl_dim_type type
, unsigned pos
)
668 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
671 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
672 enum isl_dim_type type
, unsigned pos
)
674 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
677 /* Does the given dimension have a name?
679 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
680 enum isl_dim_type type
, unsigned pos
)
683 return isl_bool_error
;
684 return isl_space_has_dim_name(set
->dim
, type
, pos
);
687 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
688 __isl_take isl_basic_map
*bmap
,
689 enum isl_dim_type type
, unsigned pos
, const char *s
)
691 bmap
= isl_basic_map_cow(bmap
);
694 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
697 return isl_basic_map_finalize(bmap
);
699 isl_basic_map_free(bmap
);
703 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
704 enum isl_dim_type type
, unsigned pos
, const char *s
)
708 map
= isl_map_cow(map
);
712 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
716 for (i
= 0; i
< map
->n
; ++i
) {
717 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
728 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
729 __isl_take isl_basic_set
*bset
,
730 enum isl_dim_type type
, unsigned pos
, const char *s
)
732 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
736 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
737 enum isl_dim_type type
, unsigned pos
, const char *s
)
739 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
743 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
744 enum isl_dim_type type
, unsigned pos
)
747 return isl_bool_error
;
748 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
751 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
752 enum isl_dim_type type
, unsigned pos
)
754 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
757 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
758 enum isl_dim_type type
, unsigned pos
)
760 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
763 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
764 enum isl_dim_type type
, unsigned pos
)
766 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
769 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
770 enum isl_dim_type type
, unsigned pos
)
772 return isl_map_has_dim_id(set
, type
, pos
);
775 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
776 enum isl_dim_type type
, unsigned pos
)
778 return isl_map_get_dim_id(set
, type
, pos
);
781 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
782 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
784 map
= isl_map_cow(map
);
788 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
790 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
796 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
797 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
799 return isl_map_set_dim_id(set
, type
, pos
, id
);
802 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
803 __isl_keep isl_id
*id
)
807 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
810 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
811 __isl_keep isl_id
*id
)
813 return isl_map_find_dim_by_id(set
, type
, id
);
816 /* Return the position of the dimension of the given type and name
818 * Return -1 if no such dimension can be found.
820 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
821 enum isl_dim_type type
, const char *name
)
825 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
828 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
833 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
836 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
839 return isl_map_find_dim_by_name(set
, type
, name
);
842 /* Reset the user pointer on all identifiers of parameters and tuples
843 * of the space of "map".
845 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
849 space
= isl_map_get_space(map
);
850 space
= isl_space_reset_user(space
);
851 map
= isl_map_reset_space(map
, space
);
856 /* Reset the user pointer on all identifiers of parameters and tuples
857 * of the space of "set".
859 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
861 return isl_map_reset_user(set
);
864 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
867 return isl_bool_error
;
868 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
871 /* Has "map" been marked as a rational map?
872 * In particular, have all basic maps in "map" been marked this way?
873 * An empty map is not considered to be rational.
874 * Maps where only some of the basic maps are marked rational
877 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
883 return isl_bool_error
;
885 return isl_bool_false
;
886 rational
= isl_basic_map_is_rational(map
->p
[0]);
889 for (i
= 1; i
< map
->n
; ++i
) {
892 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
895 if (rational
!= rational_i
)
896 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
897 "mixed rational and integer basic maps "
898 "not supported", return isl_bool_error
);
904 /* Has "set" been marked as a rational set?
905 * In particular, have all basic set in "set" been marked this way?
906 * An empty set is not considered to be rational.
907 * Sets where only some of the basic sets are marked rational
910 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
912 return isl_map_is_rational(set
);
915 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
917 return isl_basic_map_is_rational(bset
);
920 /* Does "bmap" contain any rational points?
922 * If "bmap" has an equality for each dimension, equating the dimension
923 * to an integer constant, then it has no rational points, even if it
924 * is marked as rational.
926 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
928 isl_bool has_rational
= isl_bool_true
;
932 return isl_bool_error
;
933 if (isl_basic_map_plain_is_empty(bmap
))
934 return isl_bool_false
;
935 if (!isl_basic_map_is_rational(bmap
))
936 return isl_bool_false
;
937 bmap
= isl_basic_map_copy(bmap
);
938 bmap
= isl_basic_map_implicit_equalities(bmap
);
940 return isl_bool_error
;
941 total
= isl_basic_map_total_dim(bmap
);
942 if (bmap
->n_eq
== total
) {
944 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
945 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
948 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
949 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
951 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
957 has_rational
= isl_bool_false
;
959 isl_basic_map_free(bmap
);
964 /* Does "map" contain any rational points?
966 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
969 isl_bool has_rational
;
972 return isl_bool_error
;
973 for (i
= 0; i
< map
->n
; ++i
) {
974 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
975 if (has_rational
< 0 || has_rational
)
978 return isl_bool_false
;
981 /* Does "set" contain any rational points?
983 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
985 return isl_map_has_rational(set
);
988 /* Is this basic set a parameter domain?
990 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
993 return isl_bool_error
;
994 return isl_space_is_params(bset
->dim
);
997 /* Is this set a parameter domain?
999 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1002 return isl_bool_error
;
1003 return isl_space_is_params(set
->dim
);
1006 /* Is this map actually a parameter domain?
1007 * Users should never call this function. Outside of isl,
1008 * a map can never be a parameter domain.
1010 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1013 return isl_bool_error
;
1014 return isl_space_is_params(map
->dim
);
1017 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
1018 struct isl_basic_map
*bmap
, unsigned extra
,
1019 unsigned n_eq
, unsigned n_ineq
)
1022 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1027 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1028 if (isl_blk_is_error(bmap
->block
))
1031 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1032 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1036 bmap
->block2
= isl_blk_empty();
1039 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1040 if (isl_blk_is_error(bmap
->block2
))
1043 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1048 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1049 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1051 for (i
= 0; i
< extra
; ++i
)
1052 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1056 bmap
->c_size
= n_eq
+ n_ineq
;
1057 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1058 bmap
->extra
= extra
;
1062 bmap
->sample
= NULL
;
1066 isl_basic_map_free(bmap
);
1070 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1071 unsigned nparam
, unsigned dim
, unsigned extra
,
1072 unsigned n_eq
, unsigned n_ineq
)
1074 struct isl_basic_map
*bmap
;
1077 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1081 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1082 return bset_from_bmap(bmap
);
1085 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1086 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1088 struct isl_basic_map
*bmap
;
1091 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1092 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1093 return bset_from_bmap(bmap
);
1095 isl_space_free(dim
);
1099 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1100 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1102 struct isl_basic_map
*bmap
;
1106 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1111 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1113 isl_space_free(dim
);
1117 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1118 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1119 unsigned n_eq
, unsigned n_ineq
)
1121 struct isl_basic_map
*bmap
;
1124 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1128 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1132 static void dup_constraints(
1133 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1136 unsigned total
= isl_basic_map_total_dim(src
);
1138 for (i
= 0; i
< src
->n_eq
; ++i
) {
1139 int j
= isl_basic_map_alloc_equality(dst
);
1140 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1143 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1144 int j
= isl_basic_map_alloc_inequality(dst
);
1145 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1148 for (i
= 0; i
< src
->n_div
; ++i
) {
1149 int j
= isl_basic_map_alloc_div(dst
);
1150 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1152 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1155 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1157 struct isl_basic_map
*dup
;
1161 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1162 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1165 dup_constraints(dup
, bmap
);
1166 dup
->flags
= bmap
->flags
;
1167 dup
->sample
= isl_vec_copy(bmap
->sample
);
1171 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1173 struct isl_basic_map
*dup
;
1175 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1176 return bset_from_bmap(dup
);
1179 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1184 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1188 return isl_basic_set_dup(bset
);
1191 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1200 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1205 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1209 bmap
= isl_basic_map_dup(bmap
);
1211 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1215 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1224 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1229 if (--bmap
->ref
> 0)
1232 isl_ctx_deref(bmap
->ctx
);
1234 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1236 isl_blk_free(bmap
->ctx
, bmap
->block
);
1237 isl_vec_free(bmap
->sample
);
1238 isl_space_free(bmap
->dim
);
1244 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1246 return isl_basic_map_free(bset_to_bmap(bset
));
1249 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1251 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1254 /* Check that "map" has only named parameters, reporting an error
1257 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1259 return isl_space_check_named_params(isl_map_peek_space(map
));
1262 /* Check that "bmap1" and "bmap2" have the same parameters,
1263 * reporting an error if they do not.
1265 static isl_stat
isl_basic_map_check_equal_params(
1266 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1270 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1272 return isl_stat_error
;
1274 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1275 "parameters don't match", return isl_stat_error
);
1279 __isl_give isl_map
*isl_map_align_params_map_map_and(
1280 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1281 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1282 __isl_take isl_map
*map2
))
1286 if (isl_map_has_equal_params(map1
, map2
))
1287 return fn(map1
, map2
);
1288 if (isl_map_check_named_params(map1
) < 0)
1290 if (isl_map_check_named_params(map2
) < 0)
1292 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1293 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1294 return fn(map1
, map2
);
1301 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1302 __isl_keep isl_map
*map2
,
1303 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1308 return isl_bool_error
;
1309 if (isl_map_has_equal_params(map1
, map2
))
1310 return fn(map1
, map2
);
1311 if (isl_map_check_named_params(map1
) < 0)
1312 return isl_bool_error
;
1313 if (isl_map_check_named_params(map2
) < 0)
1314 return isl_bool_error
;
1315 map1
= isl_map_copy(map1
);
1316 map2
= isl_map_copy(map2
);
1317 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1318 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1325 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1327 struct isl_ctx
*ctx
;
1331 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1332 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1334 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1335 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1336 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1337 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1338 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1339 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1341 int j
= isl_basic_map_alloc_inequality(bmap
);
1345 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1346 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1353 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1354 bmap
->extra
- bmap
->n_div
);
1355 return bmap
->n_eq
++;
1358 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1360 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1363 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1367 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1372 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1374 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1377 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1382 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1384 if (pos
!= bmap
->n_eq
- 1) {
1386 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1387 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1393 /* Turn inequality "pos" of "bmap" into an equality.
1395 * In particular, we move the inequality in front of the equalities
1396 * and move the last inequality in the position of the moved inequality.
1397 * Note that isl_tab_make_equalities_explicit depends on this particular
1398 * change in the ordering of the constraints.
1400 void isl_basic_map_inequality_to_equality(
1401 struct isl_basic_map
*bmap
, unsigned pos
)
1405 t
= bmap
->ineq
[pos
];
1406 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1407 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1412 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1413 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1414 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1415 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1418 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1420 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1423 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1425 struct isl_ctx
*ctx
;
1429 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1430 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1431 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1432 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1433 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1434 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1435 1 + isl_basic_map_total_dim(bmap
),
1436 bmap
->extra
- bmap
->n_div
);
1437 return bmap
->n_ineq
++;
1440 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1442 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1445 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1449 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1454 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1456 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1459 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1464 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1466 if (pos
!= bmap
->n_ineq
- 1) {
1467 t
= bmap
->ineq
[pos
];
1468 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1469 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1470 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1476 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1478 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1481 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1486 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1489 k
= isl_basic_map_alloc_equality(bmap
);
1492 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1495 isl_basic_map_free(bmap
);
1499 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1502 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1505 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1510 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1513 k
= isl_basic_map_alloc_inequality(bmap
);
1516 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1519 isl_basic_map_free(bmap
);
1523 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1526 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1529 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1533 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1534 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1535 1 + 1 + isl_basic_map_total_dim(bmap
),
1536 bmap
->extra
- bmap
->n_div
);
1537 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1538 return bmap
->n_div
++;
1541 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1543 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1546 /* Check that there are "n" dimensions of type "type" starting at "first"
1549 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1550 enum isl_dim_type type
, unsigned first
, unsigned n
)
1555 return isl_stat_error
;
1556 dim
= isl_basic_map_dim(bmap
, type
);
1557 if (first
+ n
> dim
|| first
+ n
< first
)
1558 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1559 "position or range out of bounds",
1560 return isl_stat_error
);
1564 /* Insert an extra integer division, prescribed by "div", to "bmap"
1565 * at (integer division) position "pos".
1567 * The integer division is first added at the end and then moved
1568 * into the right position.
1570 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1571 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1575 bmap
= isl_basic_map_cow(bmap
);
1577 return isl_basic_map_free(bmap
);
1579 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1580 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1581 "unexpected size", return isl_basic_map_free(bmap
));
1582 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1583 return isl_basic_map_free(bmap
);
1585 bmap
= isl_basic_map_extend_space(bmap
,
1586 isl_basic_map_get_space(bmap
), 1, 0, 2);
1587 k
= isl_basic_map_alloc_div(bmap
);
1589 return isl_basic_map_free(bmap
);
1590 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1591 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1593 for (i
= k
; i
> pos
; --i
)
1594 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1599 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1602 return isl_stat_error
;
1603 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1608 /* Copy constraint from src to dst, putting the vars of src at offset
1609 * dim_off in dst and the divs of src at offset div_off in dst.
1610 * If both sets are actually map, then dim_off applies to the input
1613 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1614 struct isl_basic_map
*src_map
, isl_int
*src
,
1615 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1617 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1618 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1619 unsigned src_in
= isl_basic_map_n_in(src_map
);
1620 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1621 unsigned src_out
= isl_basic_map_n_out(src_map
);
1622 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1623 isl_int_set(dst
[0], src
[0]);
1624 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1625 if (dst_nparam
> src_nparam
)
1626 isl_seq_clr(dst
+1+src_nparam
,
1627 dst_nparam
- src_nparam
);
1628 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1629 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1631 isl_min(dst_in
-in_off
, src_in
));
1632 if (dst_in
-in_off
> src_in
)
1633 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1634 dst_in
- in_off
- src_in
);
1635 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1636 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1637 src
+1+src_nparam
+src_in
,
1638 isl_min(dst_out
-out_off
, src_out
));
1639 if (dst_out
-out_off
> src_out
)
1640 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1641 dst_out
- out_off
- src_out
);
1642 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1643 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1644 src
+1+src_nparam
+src_in
+src_out
,
1645 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1646 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1647 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1648 div_off
+src_map
->n_div
,
1649 dst_map
->n_div
- div_off
- src_map
->n_div
);
1652 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1653 struct isl_basic_map
*src_map
, isl_int
*src
,
1654 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1656 isl_int_set(dst
[0], src
[0]);
1657 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1660 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1661 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1666 if (!bmap1
|| !bmap2
)
1669 div_off
= bmap1
->n_div
;
1671 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1672 int i1
= isl_basic_map_alloc_equality(bmap1
);
1675 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1676 i_pos
, o_pos
, div_off
);
1679 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1680 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1683 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1684 i_pos
, o_pos
, div_off
);
1687 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1688 int i1
= isl_basic_map_alloc_div(bmap1
);
1691 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1692 i_pos
, o_pos
, div_off
);
1695 isl_basic_map_free(bmap2
);
1700 isl_basic_map_free(bmap1
);
1701 isl_basic_map_free(bmap2
);
1705 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1706 struct isl_basic_set
*bset2
, unsigned pos
)
1708 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1709 bset_to_bmap(bset2
), 0, pos
));
1712 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1713 __isl_take isl_space
*dim
, unsigned extra
,
1714 unsigned n_eq
, unsigned n_ineq
)
1716 struct isl_basic_map
*ext
;
1726 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1727 base
->extra
>= base
->n_div
+ extra
;
1729 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1730 room_for_ineq(base
, n_ineq
)) {
1731 isl_space_free(dim
);
1735 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1736 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1737 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1738 extra
+= base
->extra
;
1740 n_ineq
+= base
->n_ineq
;
1742 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1748 ext
->sample
= isl_vec_copy(base
->sample
);
1749 flags
= base
->flags
;
1750 ext
= add_constraints(ext
, base
, 0, 0);
1753 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1759 isl_space_free(dim
);
1760 isl_basic_map_free(base
);
1764 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1765 __isl_take isl_space
*dim
, unsigned extra
,
1766 unsigned n_eq
, unsigned n_ineq
)
1768 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1769 dim
, extra
, n_eq
, n_ineq
));
1772 struct isl_basic_map
*isl_basic_map_extend_constraints(
1773 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1777 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1781 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1782 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1783 unsigned n_eq
, unsigned n_ineq
)
1785 struct isl_basic_map
*bmap
;
1790 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1794 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1797 isl_basic_map_free(base
);
1801 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1802 unsigned nparam
, unsigned dim
, unsigned extra
,
1803 unsigned n_eq
, unsigned n_ineq
)
1805 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1806 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1809 struct isl_basic_set
*isl_basic_set_extend_constraints(
1810 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1812 isl_basic_map
*bmap
= bset_to_bmap(base
);
1813 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1814 return bset_from_bmap(bmap
);
1817 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1819 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1822 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1827 if (bmap
->ref
> 1) {
1829 bmap
= isl_basic_map_dup(bmap
);
1832 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1833 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1838 /* Clear all cached information in "map", either because it is about
1839 * to be modified or because it is being freed.
1840 * Always return the same pointer that is passed in.
1841 * This is needed for the use in isl_map_free.
1843 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1845 isl_basic_map_free(map
->cached_simple_hull
[0]);
1846 isl_basic_map_free(map
->cached_simple_hull
[1]);
1847 map
->cached_simple_hull
[0] = NULL
;
1848 map
->cached_simple_hull
[1] = NULL
;
1852 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1854 return isl_map_cow(set
);
1857 /* Return an isl_map that is equal to "map" and that has only
1858 * a single reference.
1860 * If the original input already has only one reference, then
1861 * simply return it, but clear all cached information, since
1862 * it may be rendered invalid by the operations that will be
1863 * performed on the result.
1865 * Otherwise, create a duplicate (without any cached information).
1867 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1873 return clear_caches(map
);
1875 return isl_map_dup(map
);
1878 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1879 unsigned a_len
, unsigned b_len
)
1881 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1882 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1883 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1886 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1887 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1895 isl_assert(bmap
->ctx
,
1896 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1898 if (n1
== 0 || n2
== 0)
1901 bmap
= isl_basic_map_cow(bmap
);
1905 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1906 if (isl_blk_is_error(blk
))
1909 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1911 bmap
->eq
[i
] + pos
, n1
, n2
);
1913 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1915 bmap
->ineq
[i
] + pos
, n1
, n2
);
1917 for (i
= 0; i
< bmap
->n_div
; ++i
)
1919 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1921 isl_blk_free(bmap
->ctx
, blk
);
1923 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1924 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1925 return isl_basic_map_finalize(bmap
);
1927 isl_basic_map_free(bmap
);
1931 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1937 total
= isl_basic_map_total_dim(bmap
);
1938 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1939 return isl_basic_map_free(bmap
);
1940 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1942 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1944 i
= isl_basic_map_alloc_equality(bmap
);
1948 isl_int_set_si(bmap
->eq
[i
][0], 1);
1949 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1950 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1951 isl_vec_free(bmap
->sample
);
1952 bmap
->sample
= NULL
;
1953 return isl_basic_map_finalize(bmap
);
1955 isl_basic_map_free(bmap
);
1959 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1961 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1964 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1967 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1969 isl_int
*t
= bmap
->div
[a
];
1970 bmap
->div
[a
] = bmap
->div
[b
];
1974 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1975 * div definitions accordingly.
1977 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1980 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1982 swap_div(bmap
, a
, b
);
1984 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1985 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1987 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1988 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1990 for (i
= 0; i
< bmap
->n_div
; ++i
)
1991 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1992 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1995 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1996 * div definitions accordingly.
1998 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
2000 isl_basic_map_swap_div(bset
, a
, b
);
2003 /* Eliminate the specified n dimensions starting at first from the
2004 * constraints, without removing the dimensions from the space.
2005 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2007 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2008 enum isl_dim_type type
, unsigned first
, unsigned n
)
2017 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2018 isl_die(map
->ctx
, isl_error_invalid
,
2019 "index out of bounds", goto error
);
2021 map
= isl_map_cow(map
);
2025 for (i
= 0; i
< map
->n
; ++i
) {
2026 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2036 /* Eliminate the specified n dimensions starting at first from the
2037 * constraints, without removing the dimensions from the space.
2038 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2040 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2041 enum isl_dim_type type
, unsigned first
, unsigned n
)
2043 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2046 /* Eliminate the specified n dimensions starting at first from the
2047 * constraints, without removing the dimensions from the space.
2048 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2050 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2051 unsigned first
, unsigned n
)
2053 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2056 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2057 __isl_take isl_basic_map
*bmap
)
2061 bmap
= isl_basic_map_eliminate_vars(bmap
,
2062 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2066 return isl_basic_map_finalize(bmap
);
2069 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2070 __isl_take isl_basic_set
*bset
)
2072 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2075 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2084 map
= isl_map_cow(map
);
2088 for (i
= 0; i
< map
->n
; ++i
) {
2089 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2099 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2101 return isl_map_remove_divs(set
);
2104 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2105 enum isl_dim_type type
, unsigned first
, unsigned n
)
2107 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2108 return isl_basic_map_free(bmap
);
2109 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2111 bmap
= isl_basic_map_eliminate_vars(bmap
,
2112 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2115 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2117 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2121 /* Return true if the definition of the given div (recursively) involves
2122 * any of the given variables.
2124 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2125 unsigned first
, unsigned n
)
2128 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2130 if (isl_int_is_zero(bmap
->div
[div
][0]))
2131 return isl_bool_false
;
2132 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2133 return isl_bool_true
;
2135 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2138 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2140 involves
= div_involves_vars(bmap
, i
, first
, n
);
2141 if (involves
< 0 || involves
)
2145 return isl_bool_false
;
2148 /* Try and add a lower and/or upper bound on "div" to "bmap"
2149 * based on inequality "i".
2150 * "total" is the total number of variables (excluding the divs).
2151 * "v" is a temporary object that can be used during the calculations.
2152 * If "lb" is set, then a lower bound should be constructed.
2153 * If "ub" is set, then an upper bound should be constructed.
2155 * The calling function has already checked that the inequality does not
2156 * reference "div", but we still need to check that the inequality is
2157 * of the right form. We'll consider the case where we want to construct
2158 * a lower bound. The construction of upper bounds is similar.
2160 * Let "div" be of the form
2162 * q = floor((a + f(x))/d)
2164 * We essentially check if constraint "i" is of the form
2168 * so that we can use it to derive a lower bound on "div".
2169 * However, we allow a slightly more general form
2173 * with the condition that the coefficients of g(x) - f(x) are all
2175 * Rewriting this constraint as
2179 * adding a + f(x) to both sides and dividing by d, we obtain
2181 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2183 * Taking the floor on both sides, we obtain
2185 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2189 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2191 * In the case of an upper bound, we construct the constraint
2193 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2196 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2197 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2198 unsigned total
, isl_int v
, int lb
, int ub
)
2202 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2204 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2205 bmap
->div
[div
][1 + 1 + j
]);
2206 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2209 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2210 bmap
->div
[div
][1 + 1 + j
]);
2211 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2217 bmap
= isl_basic_map_cow(bmap
);
2218 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2220 int k
= isl_basic_map_alloc_inequality(bmap
);
2223 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2224 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2225 bmap
->div
[div
][1 + j
]);
2226 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2227 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2229 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2232 int k
= isl_basic_map_alloc_inequality(bmap
);
2235 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2236 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2237 bmap
->div
[div
][1 + j
]);
2238 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2239 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2241 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2244 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2247 isl_basic_map_free(bmap
);
2251 /* This function is called right before "div" is eliminated from "bmap"
2252 * using Fourier-Motzkin.
2253 * Look through the constraints of "bmap" for constraints on the argument
2254 * of the integer division and use them to construct constraints on the
2255 * integer division itself. These constraints can then be combined
2256 * during the Fourier-Motzkin elimination.
2257 * Note that it is only useful to introduce lower bounds on "div"
2258 * if "bmap" already contains upper bounds on "div" as the newly
2259 * introduce lower bounds can then be combined with the pre-existing
2260 * upper bounds. Similarly for upper bounds.
2261 * We therefore first check if "bmap" contains any lower and/or upper bounds
2264 * It is interesting to note that the introduction of these constraints
2265 * can indeed lead to more accurate results, even when compared to
2266 * deriving constraints on the argument of "div" from constraints on "div".
2267 * Consider, for example, the set
2269 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2271 * The second constraint can be rewritten as
2273 * 2 * [(-i-2j+3)/4] + k >= 0
2275 * from which we can derive
2277 * -i - 2j + 3 >= -2k
2283 * Combined with the first constraint, we obtain
2285 * -3 <= 3 + 2k or k >= -3
2287 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2288 * the first constraint, we obtain
2290 * [(i + 2j)/4] >= [-3/4] = -1
2292 * Combining this constraint with the second constraint, we obtain
2296 static __isl_give isl_basic_map
*insert_bounds_on_div(
2297 __isl_take isl_basic_map
*bmap
, int div
)
2300 int check_lb
, check_ub
;
2307 if (isl_int_is_zero(bmap
->div
[div
][0]))
2310 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2314 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2315 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2322 if (!check_lb
&& !check_ub
)
2327 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2328 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2331 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2332 check_lb
, check_ub
);
2340 /* Remove all divs (recursively) involving any of the given dimensions
2341 * in their definitions.
2343 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2344 __isl_take isl_basic_map
*bmap
,
2345 enum isl_dim_type type
, unsigned first
, unsigned n
)
2349 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2350 return isl_basic_map_free(bmap
);
2351 first
+= isl_basic_map_offset(bmap
, type
);
2353 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2356 involves
= div_involves_vars(bmap
, i
, first
, n
);
2358 return isl_basic_map_free(bmap
);
2361 bmap
= insert_bounds_on_div(bmap
, i
);
2362 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2371 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2372 __isl_take isl_basic_set
*bset
,
2373 enum isl_dim_type type
, unsigned first
, unsigned n
)
2375 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2378 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2379 enum isl_dim_type type
, unsigned first
, unsigned n
)
2388 map
= isl_map_cow(map
);
2392 for (i
= 0; i
< map
->n
; ++i
) {
2393 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2404 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2405 enum isl_dim_type type
, unsigned first
, unsigned n
)
2407 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2411 /* Does the description of "bmap" depend on the specified dimensions?
2412 * We also check whether the dimensions appear in any of the div definitions.
2413 * In principle there is no need for this check. If the dimensions appear
2414 * in a div definition, they also appear in the defining constraints of that
2417 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2418 enum isl_dim_type type
, unsigned first
, unsigned n
)
2422 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2423 return isl_bool_error
;
2425 first
+= isl_basic_map_offset(bmap
, type
);
2426 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2427 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2428 return isl_bool_true
;
2429 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2430 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2431 return isl_bool_true
;
2432 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2433 if (isl_int_is_zero(bmap
->div
[i
][0]))
2435 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2436 return isl_bool_true
;
2439 return isl_bool_false
;
2442 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2443 enum isl_dim_type type
, unsigned first
, unsigned n
)
2448 return isl_bool_error
;
2450 if (first
+ n
> isl_map_dim(map
, type
))
2451 isl_die(map
->ctx
, isl_error_invalid
,
2452 "index out of bounds", return isl_bool_error
);
2454 for (i
= 0; i
< map
->n
; ++i
) {
2455 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2457 if (involves
< 0 || involves
)
2461 return isl_bool_false
;
2464 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2465 enum isl_dim_type type
, unsigned first
, unsigned n
)
2467 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2470 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2471 enum isl_dim_type type
, unsigned first
, unsigned n
)
2473 return isl_map_involves_dims(set
, type
, first
, n
);
2476 /* Drop all constraints in bmap that involve any of the dimensions
2477 * first to first+n-1.
2479 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2480 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2487 bmap
= isl_basic_map_cow(bmap
);
2492 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2493 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2495 isl_basic_map_drop_equality(bmap
, i
);
2498 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2499 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2501 isl_basic_map_drop_inequality(bmap
, i
);
2504 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2508 /* Drop all constraints in bset that involve any of the dimensions
2509 * first to first+n-1.
2511 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2512 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2514 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2517 /* Drop all constraints in bmap that do not involve any of the dimensions
2518 * first to first + n - 1 of the given type.
2520 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2521 __isl_take isl_basic_map
*bmap
,
2522 enum isl_dim_type type
, unsigned first
, unsigned n
)
2527 isl_space
*space
= isl_basic_map_get_space(bmap
);
2528 isl_basic_map_free(bmap
);
2529 return isl_basic_map_universe(space
);
2531 bmap
= isl_basic_map_cow(bmap
);
2535 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2536 return isl_basic_map_free(bmap
);
2538 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2540 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2541 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2543 isl_basic_map_drop_equality(bmap
, i
);
2546 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2547 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2549 isl_basic_map_drop_inequality(bmap
, i
);
2552 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2556 /* Drop all constraints in bset that do not involve any of the dimensions
2557 * first to first + n - 1 of the given type.
2559 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2560 __isl_take isl_basic_set
*bset
,
2561 enum isl_dim_type type
, unsigned first
, unsigned n
)
2563 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2567 /* Drop all constraints in bmap that involve any of the dimensions
2568 * first to first + n - 1 of the given type.
2570 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2571 __isl_take isl_basic_map
*bmap
,
2572 enum isl_dim_type type
, unsigned first
, unsigned n
)
2579 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2580 return isl_basic_map_free(bmap
);
2582 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2583 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2584 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2587 /* Drop all constraints in bset that involve any of the dimensions
2588 * first to first + n - 1 of the given type.
2590 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2591 __isl_take isl_basic_set
*bset
,
2592 enum isl_dim_type type
, unsigned first
, unsigned n
)
2594 return isl_basic_map_drop_constraints_involving_dims(bset
,
2598 /* Drop constraints from "map" by applying "drop" to each basic map.
2600 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2601 enum isl_dim_type type
, unsigned first
, unsigned n
,
2602 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2603 enum isl_dim_type type
, unsigned first
, unsigned n
))
2611 dim
= isl_map_dim(map
, type
);
2612 if (first
+ n
> dim
|| first
+ n
< first
)
2613 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2614 "index out of bounds", return isl_map_free(map
));
2616 map
= isl_map_cow(map
);
2620 for (i
= 0; i
< map
->n
; ++i
) {
2621 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2623 return isl_map_free(map
);
2627 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2632 /* Drop all constraints in map that involve any of the dimensions
2633 * first to first + n - 1 of the given type.
2635 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2636 __isl_take isl_map
*map
,
2637 enum isl_dim_type type
, unsigned first
, unsigned n
)
2641 return drop_constraints(map
, type
, first
, n
,
2642 &isl_basic_map_drop_constraints_involving_dims
);
2645 /* Drop all constraints in "map" that do not involve any of the dimensions
2646 * first to first + n - 1 of the given type.
2648 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2649 __isl_take isl_map
*map
,
2650 enum isl_dim_type type
, unsigned first
, unsigned n
)
2653 isl_space
*space
= isl_map_get_space(map
);
2655 return isl_map_universe(space
);
2657 return drop_constraints(map
, type
, first
, n
,
2658 &isl_basic_map_drop_constraints_not_involving_dims
);
2661 /* Drop all constraints in set that involve any of the dimensions
2662 * first to first + n - 1 of the given type.
2664 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2665 __isl_take isl_set
*set
,
2666 enum isl_dim_type type
, unsigned first
, unsigned n
)
2668 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2671 /* Drop all constraints in "set" that do not involve any of the dimensions
2672 * first to first + n - 1 of the given type.
2674 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2675 __isl_take isl_set
*set
,
2676 enum isl_dim_type type
, unsigned first
, unsigned n
)
2678 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2681 /* Does local variable "div" of "bmap" have a complete explicit representation?
2682 * Having a complete explicit representation requires not only
2683 * an explicit representation, but also that all local variables
2684 * that appear in this explicit representation in turn have
2685 * a complete explicit representation.
2687 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2690 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2693 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2694 if (marked
< 0 || marked
)
2695 return isl_bool_not(marked
);
2697 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2700 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2702 known
= isl_basic_map_div_is_known(bmap
, i
);
2703 if (known
< 0 || !known
)
2707 return isl_bool_true
;
2710 /* Remove all divs that are unknown or defined in terms of unknown divs.
2712 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2713 __isl_take isl_basic_map
*bmap
)
2720 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2721 if (isl_basic_map_div_is_known(bmap
, i
))
2723 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2732 /* Remove all divs that are unknown or defined in terms of unknown divs.
2734 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2735 __isl_take isl_basic_set
*bset
)
2737 return isl_basic_map_remove_unknown_divs(bset
);
2740 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2749 map
= isl_map_cow(map
);
2753 for (i
= 0; i
< map
->n
; ++i
) {
2754 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2764 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2766 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2769 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2770 __isl_take isl_basic_set
*bset
,
2771 enum isl_dim_type type
, unsigned first
, unsigned n
)
2773 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2774 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2775 return bset_from_bmap(bmap
);
2778 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2779 enum isl_dim_type type
, unsigned first
, unsigned n
)
2786 map
= isl_map_cow(map
);
2789 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2791 for (i
= 0; i
< map
->n
; ++i
) {
2792 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2793 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2797 map
= isl_map_drop(map
, type
, first
, n
);
2804 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2805 enum isl_dim_type type
, unsigned first
, unsigned n
)
2807 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2811 /* Project out n inputs starting at first using Fourier-Motzkin */
2812 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2813 unsigned first
, unsigned n
)
2815 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2818 static void dump_term(struct isl_basic_map
*bmap
,
2819 isl_int c
, int pos
, FILE *out
)
2822 unsigned in
= isl_basic_map_n_in(bmap
);
2823 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2824 unsigned nparam
= isl_basic_map_n_param(bmap
);
2826 isl_int_print(out
, c
, 0);
2828 if (!isl_int_is_one(c
))
2829 isl_int_print(out
, c
, 0);
2830 if (pos
< 1 + nparam
) {
2831 name
= isl_space_get_dim_name(bmap
->dim
,
2832 isl_dim_param
, pos
- 1);
2834 fprintf(out
, "%s", name
);
2836 fprintf(out
, "p%d", pos
- 1);
2837 } else if (pos
< 1 + nparam
+ in
)
2838 fprintf(out
, "i%d", pos
- 1 - nparam
);
2839 else if (pos
< 1 + nparam
+ dim
)
2840 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2842 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2846 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2847 int sign
, FILE *out
)
2851 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2855 for (i
= 0, first
= 1; i
< len
; ++i
) {
2856 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2859 fprintf(out
, " + ");
2861 isl_int_abs(v
, c
[i
]);
2862 dump_term(bmap
, v
, i
, out
);
2869 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2870 const char *op
, FILE *out
, int indent
)
2874 fprintf(out
, "%*s", indent
, "");
2876 dump_constraint_sign(bmap
, c
, 1, out
);
2877 fprintf(out
, " %s ", op
);
2878 dump_constraint_sign(bmap
, c
, -1, out
);
2882 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2883 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2885 fprintf(out
, "%*s", indent
, "");
2886 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2891 static void dump_constraints(struct isl_basic_map
*bmap
,
2892 isl_int
**c
, unsigned n
,
2893 const char *op
, FILE *out
, int indent
)
2897 for (i
= 0; i
< n
; ++i
)
2898 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2901 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2905 unsigned total
= isl_basic_map_total_dim(bmap
);
2907 for (j
= 0; j
< 1 + total
; ++j
) {
2908 if (isl_int_is_zero(exp
[j
]))
2910 if (!first
&& isl_int_is_pos(exp
[j
]))
2912 dump_term(bmap
, exp
[j
], j
, out
);
2917 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2921 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2922 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2924 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2925 fprintf(out
, "%*s", indent
, "");
2926 fprintf(out
, "e%d = [(", i
);
2927 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2929 isl_int_print(out
, bmap
->div
[i
][0], 0);
2930 fprintf(out
, "]\n");
2934 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2935 FILE *out
, int indent
)
2938 fprintf(out
, "null basic set\n");
2942 fprintf(out
, "%*s", indent
, "");
2943 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2944 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2945 bset
->extra
, bset
->flags
);
2946 dump(bset_to_bmap(bset
), out
, indent
);
2949 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2950 FILE *out
, int indent
)
2953 fprintf(out
, "null basic map\n");
2957 fprintf(out
, "%*s", indent
, "");
2958 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2959 "flags: %x, n_name: %d\n",
2961 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2962 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2963 dump(bmap
, out
, indent
);
2966 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2971 total
= isl_basic_map_total_dim(bmap
);
2972 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2973 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2974 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2975 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2979 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2984 if (isl_space_dim(space
, isl_dim_in
) != 0)
2985 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2986 "set cannot have input dimensions", goto error
);
2987 return isl_map_alloc_space(space
, n
, flags
);
2989 isl_space_free(space
);
2993 /* Make sure "map" has room for at least "n" more basic maps.
2995 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2998 struct isl_map
*grown
= NULL
;
3002 isl_assert(map
->ctx
, n
>= 0, goto error
);
3003 if (map
->n
+ n
<= map
->size
)
3005 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3008 for (i
= 0; i
< map
->n
; ++i
) {
3009 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3017 isl_map_free(grown
);
3022 /* Make sure "set" has room for at least "n" more basic sets.
3024 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3026 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3029 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
3031 return isl_map_from_basic_map(bset
);
3034 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
3036 struct isl_map
*map
;
3041 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3042 return isl_map_add_basic_map(map
, bmap
);
3045 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3046 __isl_take isl_basic_set
*bset
)
3048 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3049 bset_to_bmap(bset
)));
3052 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3054 return isl_map_free(set
);
3057 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3062 fprintf(out
, "null set\n");
3066 fprintf(out
, "%*s", indent
, "");
3067 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3068 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3070 for (i
= 0; i
< set
->n
; ++i
) {
3071 fprintf(out
, "%*s", indent
, "");
3072 fprintf(out
, "basic set %d:\n", i
);
3073 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3077 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3082 fprintf(out
, "null map\n");
3086 fprintf(out
, "%*s", indent
, "");
3087 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3088 "flags: %x, n_name: %d\n",
3089 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3090 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3091 for (i
= 0; i
< map
->n
; ++i
) {
3092 fprintf(out
, "%*s", indent
, "");
3093 fprintf(out
, "basic map %d:\n", i
);
3094 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3098 struct isl_basic_map
*isl_basic_map_intersect_domain(
3099 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3101 struct isl_basic_map
*bmap_domain
;
3103 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3106 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3107 isl_assert(bset
->ctx
,
3108 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3110 bmap
= isl_basic_map_cow(bmap
);
3113 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3114 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3115 bmap_domain
= isl_basic_map_from_domain(bset
);
3116 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3118 bmap
= isl_basic_map_simplify(bmap
);
3119 return isl_basic_map_finalize(bmap
);
3121 isl_basic_map_free(bmap
);
3122 isl_basic_set_free(bset
);
3126 /* Check that the space of "bset" is the same as that of the range of "bmap".
3128 static isl_stat
isl_basic_map_check_compatible_range(
3129 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3133 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3135 return isl_stat_error
;
3137 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3138 "incompatible spaces", return isl_stat_error
);
3143 struct isl_basic_map
*isl_basic_map_intersect_range(
3144 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3146 struct isl_basic_map
*bmap_range
;
3148 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3151 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3152 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3155 if (isl_basic_set_plain_is_universe(bset
)) {
3156 isl_basic_set_free(bset
);
3160 bmap
= isl_basic_map_cow(bmap
);
3163 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3164 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3165 bmap_range
= bset_to_bmap(bset
);
3166 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3168 bmap
= isl_basic_map_simplify(bmap
);
3169 return isl_basic_map_finalize(bmap
);
3171 isl_basic_map_free(bmap
);
3172 isl_basic_set_free(bset
);
3176 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3177 __isl_keep isl_vec
*vec
)
3184 return isl_bool_error
;
3186 total
= 1 + isl_basic_map_total_dim(bmap
);
3187 if (total
!= vec
->size
)
3188 return isl_bool_false
;
3192 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3193 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3194 if (!isl_int_is_zero(s
)) {
3196 return isl_bool_false
;
3200 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3201 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3202 if (isl_int_is_neg(s
)) {
3204 return isl_bool_false
;
3210 return isl_bool_true
;
3213 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3214 __isl_keep isl_vec
*vec
)
3216 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3219 struct isl_basic_map
*isl_basic_map_intersect(
3220 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3222 struct isl_vec
*sample
= NULL
;
3224 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3226 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3227 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3228 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3229 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3230 return isl_basic_map_intersect(bmap2
, bmap1
);
3232 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3233 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3234 isl_assert(bmap1
->ctx
,
3235 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3237 if (isl_basic_map_plain_is_empty(bmap1
)) {
3238 isl_basic_map_free(bmap2
);
3241 if (isl_basic_map_plain_is_empty(bmap2
)) {
3242 isl_basic_map_free(bmap1
);
3246 if (bmap1
->sample
&&
3247 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3248 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3249 sample
= isl_vec_copy(bmap1
->sample
);
3250 else if (bmap2
->sample
&&
3251 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3252 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3253 sample
= isl_vec_copy(bmap2
->sample
);
3255 bmap1
= isl_basic_map_cow(bmap1
);
3258 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3259 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3260 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3263 isl_vec_free(sample
);
3265 isl_vec_free(bmap1
->sample
);
3266 bmap1
->sample
= sample
;
3269 bmap1
= isl_basic_map_simplify(bmap1
);
3270 return isl_basic_map_finalize(bmap1
);
3273 isl_vec_free(sample
);
3274 isl_basic_map_free(bmap1
);
3275 isl_basic_map_free(bmap2
);
3279 struct isl_basic_set
*isl_basic_set_intersect(
3280 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3282 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3283 bset_to_bmap(bset2
)));
3286 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3287 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3289 return isl_basic_set_intersect(bset1
, bset2
);
3292 /* Special case of isl_map_intersect, where both map1 and map2
3293 * are convex, without any divs and such that either map1 or map2
3294 * contains a single constraint. This constraint is then simply
3295 * added to the other map.
3297 static __isl_give isl_map
*map_intersect_add_constraint(
3298 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3300 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3301 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3302 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3303 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3305 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3306 return isl_map_intersect(map2
, map1
);
3308 map1
= isl_map_cow(map1
);
3311 if (isl_map_plain_is_empty(map1
)) {
3315 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3316 if (map2
->p
[0]->n_eq
== 1)
3317 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3319 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3320 map2
->p
[0]->ineq
[0]);
3322 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3323 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3327 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3328 isl_basic_map_free(map1
->p
[0]);
3341 /* map2 may be either a parameter domain or a map living in the same
3344 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3345 __isl_take isl_map
*map2
)
3354 if ((isl_map_plain_is_empty(map1
) ||
3355 isl_map_plain_is_universe(map2
)) &&
3356 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3360 if ((isl_map_plain_is_empty(map2
) ||
3361 isl_map_plain_is_universe(map1
)) &&
3362 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3367 if (map1
->n
== 1 && map2
->n
== 1 &&
3368 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3369 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3370 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3371 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3372 return map_intersect_add_constraint(map1
, map2
);
3374 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3375 isl_space_dim(map2
->dim
, isl_dim_param
))
3376 isl_assert(map1
->ctx
,
3377 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3379 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3380 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3381 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3383 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3384 map1
->n
* map2
->n
, flags
);
3387 for (i
= 0; i
< map1
->n
; ++i
)
3388 for (j
= 0; j
< map2
->n
; ++j
) {
3389 struct isl_basic_map
*part
;
3390 part
= isl_basic_map_intersect(
3391 isl_basic_map_copy(map1
->p
[i
]),
3392 isl_basic_map_copy(map2
->p
[j
]));
3393 if (isl_basic_map_is_empty(part
) < 0)
3394 part
= isl_basic_map_free(part
);
3395 result
= isl_map_add_basic_map(result
, part
);
3408 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3409 __isl_take isl_map
*map2
)
3413 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3414 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3415 "spaces don't match", goto error
);
3416 return map_intersect_internal(map1
, map2
);
3423 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3424 __isl_take isl_map
*map2
)
3426 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3429 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3431 return set_from_map(isl_map_intersect(set_to_map(set1
),
3435 /* map_intersect_internal accepts intersections
3436 * with parameter domains, so we can just call that function.
3438 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3439 __isl_take isl_set
*params
)
3441 return map_intersect_internal(map
, params
);
3444 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3445 __isl_take isl_map
*map2
)
3447 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3450 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3451 __isl_take isl_set
*params
)
3453 return isl_map_intersect_params(set
, params
);
3456 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3459 unsigned pos
, n1
, n2
;
3463 bmap
= isl_basic_map_cow(bmap
);
3466 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3467 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3468 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3469 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3470 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3471 return isl_basic_map_reset_space(bmap
, space
);
3474 static __isl_give isl_basic_map
*basic_map_space_reset(
3475 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3481 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3484 space
= isl_basic_map_get_space(bmap
);
3485 space
= isl_space_reset(space
, type
);
3486 bmap
= isl_basic_map_reset_space(bmap
, space
);
3490 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3491 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3492 unsigned pos
, unsigned n
)
3496 struct isl_basic_map
*res
;
3497 struct isl_dim_map
*dim_map
;
3498 unsigned total
, off
;
3499 enum isl_dim_type t
;
3502 return basic_map_space_reset(bmap
, type
);
3507 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3509 total
= isl_basic_map_total_dim(bmap
) + n
;
3510 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3512 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3514 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3516 unsigned size
= isl_basic_map_dim(bmap
, t
);
3517 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3519 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3520 pos
, size
- pos
, off
+ pos
+ n
);
3522 off
+= isl_space_dim(res_dim
, t
);
3524 isl_dim_map_div(dim_map
, bmap
, off
);
3526 res
= isl_basic_map_alloc_space(res_dim
,
3527 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3528 rational
= isl_basic_map_is_rational(bmap
);
3530 res
= isl_basic_map_free(res
);
3532 res
= isl_basic_map_set_rational(res
);
3533 if (isl_basic_map_plain_is_empty(bmap
)) {
3534 isl_basic_map_free(bmap
);
3536 return isl_basic_map_set_to_empty(res
);
3538 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3539 return isl_basic_map_finalize(res
);
3542 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3543 __isl_take isl_basic_set
*bset
,
3544 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3546 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3549 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3550 enum isl_dim_type type
, unsigned n
)
3554 return isl_basic_map_insert_dims(bmap
, type
,
3555 isl_basic_map_dim(bmap
, type
), n
);
3558 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3559 enum isl_dim_type type
, unsigned n
)
3563 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3564 return isl_basic_map_add_dims(bset
, type
, n
);
3566 isl_basic_set_free(bset
);
3570 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3571 enum isl_dim_type type
)
3575 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3578 space
= isl_map_get_space(map
);
3579 space
= isl_space_reset(space
, type
);
3580 map
= isl_map_reset_space(map
, space
);
3584 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3585 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3590 return map_space_reset(map
, type
);
3592 map
= isl_map_cow(map
);
3596 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3600 for (i
= 0; i
< map
->n
; ++i
) {
3601 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3612 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3613 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3615 return isl_map_insert_dims(set
, type
, pos
, n
);
3618 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3619 enum isl_dim_type type
, unsigned n
)
3623 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3626 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3627 enum isl_dim_type type
, unsigned n
)
3631 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3632 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3638 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3639 __isl_take isl_basic_map
*bmap
,
3640 enum isl_dim_type dst_type
, unsigned dst_pos
,
3641 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3643 struct isl_dim_map
*dim_map
;
3644 struct isl_basic_map
*res
;
3645 enum isl_dim_type t
;
3646 unsigned total
, off
;
3653 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3654 return isl_basic_map_free(bmap
);
3656 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3659 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3661 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3662 pos(bmap
->dim
, src_type
) + src_pos
+
3663 ((src_type
< dst_type
) ? n
: 0)) {
3664 bmap
= isl_basic_map_cow(bmap
);
3668 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3669 src_type
, src_pos
, n
);
3673 bmap
= isl_basic_map_finalize(bmap
);
3678 total
= isl_basic_map_total_dim(bmap
);
3679 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3682 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3683 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3684 if (t
== dst_type
) {
3685 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3688 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3691 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3692 dst_pos
, size
- dst_pos
, off
);
3693 off
+= size
- dst_pos
;
3694 } else if (t
== src_type
) {
3695 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3698 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3699 src_pos
+ n
, size
- src_pos
- n
, off
);
3700 off
+= size
- src_pos
- n
;
3702 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3706 isl_dim_map_div(dim_map
, bmap
, off
);
3708 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3709 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3710 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3714 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3715 src_type
, src_pos
, n
);
3719 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3720 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3721 bmap
= isl_basic_map_finalize(bmap
);
3725 isl_basic_map_free(bmap
);
3729 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3730 enum isl_dim_type dst_type
, unsigned dst_pos
,
3731 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3733 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3734 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3735 src_type
, src_pos
, n
);
3736 return bset_from_bmap(bmap
);
3739 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3740 enum isl_dim_type dst_type
, unsigned dst_pos
,
3741 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3745 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3746 return set_from_map(isl_map_move_dims(set_to_map(set
),
3747 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3753 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3754 enum isl_dim_type dst_type
, unsigned dst_pos
,
3755 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3764 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3767 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3770 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3772 map
= isl_map_cow(map
);
3776 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3780 for (i
= 0; i
< map
->n
; ++i
) {
3781 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3783 src_type
, src_pos
, n
);
3794 /* Move the specified dimensions to the last columns right before
3795 * the divs. Don't change the dimension specification of bmap.
3796 * That's the responsibility of the caller.
3798 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3799 enum isl_dim_type type
, unsigned first
, unsigned n
)
3801 struct isl_dim_map
*dim_map
;
3802 struct isl_basic_map
*res
;
3803 enum isl_dim_type t
;
3804 unsigned total
, off
;
3808 if (pos(bmap
->dim
, type
) + first
+ n
==
3809 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3812 total
= isl_basic_map_total_dim(bmap
);
3813 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3816 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3817 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3819 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3822 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3823 first
, n
, total
- bmap
->n_div
- n
);
3824 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3825 first
+ n
, size
- (first
+ n
), off
);
3826 off
+= size
- (first
+ n
);
3828 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3832 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3834 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3835 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3836 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3840 /* Insert "n" rows in the divs of "bmap".
3842 * The number of columns is not changed, which means that the last
3843 * dimensions of "bmap" are being reintepreted as the new divs.
3844 * The space of "bmap" is not adjusted, however, which means
3845 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3846 * from the space of "bmap" is the responsibility of the caller.
3848 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3856 bmap
= isl_basic_map_cow(bmap
);
3860 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3861 old
= bmap
->block2
.data
;
3862 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3863 (bmap
->extra
+ n
) * (1 + row_size
));
3864 if (!bmap
->block2
.data
)
3865 return isl_basic_map_free(bmap
);
3866 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3868 return isl_basic_map_free(bmap
);
3869 for (i
= 0; i
< n
; ++i
) {
3870 new_div
[i
] = bmap
->block2
.data
+
3871 (bmap
->extra
+ i
) * (1 + row_size
);
3872 isl_seq_clr(new_div
[i
], 1 + row_size
);
3874 for (i
= 0; i
< bmap
->extra
; ++i
)
3875 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3877 bmap
->div
= new_div
;
3884 /* Drop constraints from "bmap" that only involve the variables
3885 * of "type" in the range [first, first + n] that are not related
3886 * to any of the variables outside that interval.
3887 * These constraints cannot influence the values for the variables
3888 * outside the interval, except in case they cause "bmap" to be empty.
3889 * Only drop the constraints if "bmap" is known to be non-empty.
3891 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3892 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3893 unsigned first
, unsigned n
)
3897 unsigned dim
, n_div
;
3900 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3902 return isl_basic_map_free(bmap
);
3906 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3907 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3908 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3910 return isl_basic_map_free(bmap
);
3911 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3912 for (i
= 0; i
< first
; ++i
)
3914 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3917 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3922 /* Turn the n dimensions of type type, starting at first
3923 * into existentially quantified variables.
3925 * If a subset of the projected out variables are unrelated
3926 * to any of the variables that remain, then the constraints
3927 * involving this subset are simply dropped first.
3929 __isl_give isl_basic_map
*isl_basic_map_project_out(
3930 __isl_take isl_basic_map
*bmap
,
3931 enum isl_dim_type type
, unsigned first
, unsigned n
)
3934 return basic_map_space_reset(bmap
, type
);
3935 if (type
== isl_dim_div
)
3936 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3937 "cannot project out existentially quantified variables",
3938 return isl_basic_map_free(bmap
));
3940 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3944 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3945 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3947 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3948 return isl_basic_map_free(bmap
);
3950 bmap
= move_last(bmap
, type
, first
, n
);
3951 bmap
= isl_basic_map_cow(bmap
);
3952 bmap
= insert_div_rows(bmap
, n
);
3956 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3959 bmap
= isl_basic_map_simplify(bmap
);
3960 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3961 return isl_basic_map_finalize(bmap
);
3963 isl_basic_map_free(bmap
);
3967 /* Turn the n dimensions of type type, starting at first
3968 * into existentially quantified variables.
3970 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3971 enum isl_dim_type type
, unsigned first
, unsigned n
)
3973 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3977 /* Turn the n dimensions of type type, starting at first
3978 * into existentially quantified variables.
3980 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3981 enum isl_dim_type type
, unsigned first
, unsigned n
)
3989 return map_space_reset(map
, type
);
3991 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3993 map
= isl_map_cow(map
);
3997 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4001 for (i
= 0; i
< map
->n
; ++i
) {
4002 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4013 /* Turn the n dimensions of type type, starting at first
4014 * into existentially quantified variables.
4016 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4017 enum isl_dim_type type
, unsigned first
, unsigned n
)
4019 return set_from_map(isl_map_project_out(set_to_map(set
),
4023 /* Return a map that projects the elements in "set" onto their
4024 * "n" set dimensions starting at "first".
4025 * "type" should be equal to isl_dim_set.
4027 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4028 enum isl_dim_type type
, unsigned first
, unsigned n
)
4036 if (type
!= isl_dim_set
)
4037 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4038 "only set dimensions can be projected out", goto error
);
4039 dim
= isl_set_dim(set
, isl_dim_set
);
4040 if (first
+ n
> dim
|| first
+ n
< first
)
4041 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4042 "index out of bounds", goto error
);
4044 map
= isl_map_from_domain(set
);
4045 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4046 for (i
= 0; i
< n
; ++i
)
4047 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4055 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4059 for (i
= 0; i
< n
; ++i
) {
4060 j
= isl_basic_map_alloc_div(bmap
);
4063 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4067 isl_basic_map_free(bmap
);
4071 struct isl_basic_map
*isl_basic_map_apply_range(
4072 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4074 isl_space
*dim_result
= NULL
;
4075 struct isl_basic_map
*bmap
;
4076 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4077 struct isl_dim_map
*dim_map1
, *dim_map2
;
4079 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4081 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4082 bmap2
->dim
, isl_dim_in
))
4083 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4084 "spaces don't match", goto error
);
4086 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4087 isl_space_copy(bmap2
->dim
));
4089 n_in
= isl_basic_map_n_in(bmap1
);
4090 n_out
= isl_basic_map_n_out(bmap2
);
4091 n
= isl_basic_map_n_out(bmap1
);
4092 nparam
= isl_basic_map_n_param(bmap1
);
4094 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4095 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4096 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4097 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4098 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4099 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4100 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4101 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4102 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4103 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4104 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4106 bmap
= isl_basic_map_alloc_space(dim_result
,
4107 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4108 bmap1
->n_eq
+ bmap2
->n_eq
,
4109 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4110 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4111 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4112 bmap
= add_divs(bmap
, n
);
4113 bmap
= isl_basic_map_simplify(bmap
);
4114 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4115 return isl_basic_map_finalize(bmap
);
4117 isl_basic_map_free(bmap1
);
4118 isl_basic_map_free(bmap2
);
4122 struct isl_basic_set
*isl_basic_set_apply(
4123 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4128 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4131 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4134 isl_basic_set_free(bset
);
4135 isl_basic_map_free(bmap
);
4139 struct isl_basic_map
*isl_basic_map_apply_domain(
4140 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4142 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4144 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4145 bmap2
->dim
, isl_dim_in
))
4146 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4147 "spaces don't match", goto error
);
4149 bmap1
= isl_basic_map_reverse(bmap1
);
4150 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4151 return isl_basic_map_reverse(bmap1
);
4153 isl_basic_map_free(bmap1
);
4154 isl_basic_map_free(bmap2
);
4158 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4159 * A \cap B -> f(A) + f(B)
4161 struct isl_basic_map
*isl_basic_map_sum(
4162 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4164 unsigned n_in
, n_out
, nparam
, total
, pos
;
4165 struct isl_basic_map
*bmap
= NULL
;
4166 struct isl_dim_map
*dim_map1
, *dim_map2
;
4169 if (!bmap1
|| !bmap2
)
4172 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4175 nparam
= isl_basic_map_n_param(bmap1
);
4176 n_in
= isl_basic_map_n_in(bmap1
);
4177 n_out
= isl_basic_map_n_out(bmap1
);
4179 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4180 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4181 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4182 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4183 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4184 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4185 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4186 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4187 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4188 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4189 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4191 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4192 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4193 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4194 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4195 for (i
= 0; i
< n_out
; ++i
) {
4196 int j
= isl_basic_map_alloc_equality(bmap
);
4199 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4200 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4201 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4202 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4204 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4205 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4206 bmap
= add_divs(bmap
, 2 * n_out
);
4208 bmap
= isl_basic_map_simplify(bmap
);
4209 return isl_basic_map_finalize(bmap
);
4211 isl_basic_map_free(bmap
);
4212 isl_basic_map_free(bmap1
);
4213 isl_basic_map_free(bmap2
);
4217 /* Given two maps A -> f(A) and B -> g(B), construct a map
4218 * A \cap B -> f(A) + f(B)
4220 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4222 struct isl_map
*result
;
4228 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4230 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4231 map1
->n
* map2
->n
, 0);
4234 for (i
= 0; i
< map1
->n
; ++i
)
4235 for (j
= 0; j
< map2
->n
; ++j
) {
4236 struct isl_basic_map
*part
;
4237 part
= isl_basic_map_sum(
4238 isl_basic_map_copy(map1
->p
[i
]),
4239 isl_basic_map_copy(map2
->p
[j
]));
4240 if (isl_basic_map_is_empty(part
))
4241 isl_basic_map_free(part
);
4243 result
= isl_map_add_basic_map(result
, part
);
4256 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4257 __isl_take isl_set
*set2
)
4259 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4262 /* Given a basic map A -> f(A), construct A -> -f(A).
4264 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4269 bmap
= isl_basic_map_cow(bmap
);
4273 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4274 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4275 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4276 for (j
= 0; j
< n
; ++j
)
4277 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4278 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4279 for (j
= 0; j
< n
; ++j
)
4280 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4281 for (i
= 0; i
< bmap
->n_div
; ++i
)
4282 for (j
= 0; j
< n
; ++j
)
4283 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4284 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4285 return isl_basic_map_finalize(bmap
);
4288 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4290 return isl_basic_map_neg(bset
);
4293 /* Given a map A -> f(A), construct A -> -f(A).
4295 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4299 map
= isl_map_cow(map
);
4303 for (i
= 0; i
< map
->n
; ++i
) {
4304 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4315 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4317 return set_from_map(isl_map_neg(set_to_map(set
)));
4320 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4321 * A -> floor(f(A)/d).
4323 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4326 unsigned n_in
, n_out
, nparam
, total
, pos
;
4327 struct isl_basic_map
*result
= NULL
;
4328 struct isl_dim_map
*dim_map
;
4334 nparam
= isl_basic_map_n_param(bmap
);
4335 n_in
= isl_basic_map_n_in(bmap
);
4336 n_out
= isl_basic_map_n_out(bmap
);
4338 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4339 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4340 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4341 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4342 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4343 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4345 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4346 bmap
->n_div
+ n_out
,
4347 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4348 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4349 result
= add_divs(result
, n_out
);
4350 for (i
= 0; i
< n_out
; ++i
) {
4352 j
= isl_basic_map_alloc_inequality(result
);
4355 isl_seq_clr(result
->ineq
[j
], 1+total
);
4356 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4357 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4358 j
= isl_basic_map_alloc_inequality(result
);
4361 isl_seq_clr(result
->ineq
[j
], 1+total
);
4362 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4363 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4364 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4367 result
= isl_basic_map_simplify(result
);
4368 return isl_basic_map_finalize(result
);
4370 isl_basic_map_free(result
);
4374 /* Given a map A -> f(A) and an integer d, construct a map
4375 * A -> floor(f(A)/d).
4377 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4381 map
= isl_map_cow(map
);
4385 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4386 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4387 for (i
= 0; i
< map
->n
; ++i
) {
4388 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4399 /* Given a map A -> f(A) and an integer d, construct a map
4400 * A -> floor(f(A)/d).
4402 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4403 __isl_take isl_val
*d
)
4407 if (!isl_val_is_int(d
))
4408 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4409 "expecting integer denominator", goto error
);
4410 map
= isl_map_floordiv(map
, d
->n
);
4419 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4425 i
= isl_basic_map_alloc_equality(bmap
);
4428 nparam
= isl_basic_map_n_param(bmap
);
4429 n_in
= isl_basic_map_n_in(bmap
);
4430 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4431 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4432 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4433 return isl_basic_map_finalize(bmap
);
4435 isl_basic_map_free(bmap
);
4439 /* Add a constraint to "bmap" expressing i_pos < o_pos
4441 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4447 i
= isl_basic_map_alloc_inequality(bmap
);
4450 nparam
= isl_basic_map_n_param(bmap
);
4451 n_in
= isl_basic_map_n_in(bmap
);
4452 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4453 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4454 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4455 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4456 return isl_basic_map_finalize(bmap
);
4458 isl_basic_map_free(bmap
);
4462 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4464 static __isl_give isl_basic_map
*var_less_or_equal(
4465 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4471 i
= isl_basic_map_alloc_inequality(bmap
);
4474 nparam
= isl_basic_map_n_param(bmap
);
4475 n_in
= isl_basic_map_n_in(bmap
);
4476 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4477 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4478 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4479 return isl_basic_map_finalize(bmap
);
4481 isl_basic_map_free(bmap
);
4485 /* Add a constraint to "bmap" expressing i_pos > o_pos
4487 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4493 i
= isl_basic_map_alloc_inequality(bmap
);
4496 nparam
= isl_basic_map_n_param(bmap
);
4497 n_in
= isl_basic_map_n_in(bmap
);
4498 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4499 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4500 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4501 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4502 return isl_basic_map_finalize(bmap
);
4504 isl_basic_map_free(bmap
);
4508 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4510 static __isl_give isl_basic_map
*var_more_or_equal(
4511 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4517 i
= isl_basic_map_alloc_inequality(bmap
);
4520 nparam
= isl_basic_map_n_param(bmap
);
4521 n_in
= isl_basic_map_n_in(bmap
);
4522 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4523 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4524 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4525 return isl_basic_map_finalize(bmap
);
4527 isl_basic_map_free(bmap
);
4531 __isl_give isl_basic_map
*isl_basic_map_equal(
4532 __isl_take isl_space
*dim
, unsigned n_equal
)
4535 struct isl_basic_map
*bmap
;
4536 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4539 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4540 bmap
= var_equal(bmap
, i
);
4541 return isl_basic_map_finalize(bmap
);
4544 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4546 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4550 struct isl_basic_map
*bmap
;
4551 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4554 for (i
= 0; i
< pos
&& bmap
; ++i
)
4555 bmap
= var_equal(bmap
, i
);
4557 bmap
= var_less(bmap
, pos
);
4558 return isl_basic_map_finalize(bmap
);
4561 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4563 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4564 __isl_take isl_space
*dim
, unsigned pos
)
4567 isl_basic_map
*bmap
;
4569 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4570 for (i
= 0; i
< pos
; ++i
)
4571 bmap
= var_equal(bmap
, i
);
4572 bmap
= var_less_or_equal(bmap
, pos
);
4573 return isl_basic_map_finalize(bmap
);
4576 /* Return a relation on "dim" expressing i_pos > o_pos
4578 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4582 struct isl_basic_map
*bmap
;
4583 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4586 for (i
= 0; i
< pos
&& bmap
; ++i
)
4587 bmap
= var_equal(bmap
, i
);
4589 bmap
= var_more(bmap
, pos
);
4590 return isl_basic_map_finalize(bmap
);
4593 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4595 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4596 __isl_take isl_space
*dim
, unsigned pos
)
4599 isl_basic_map
*bmap
;
4601 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4602 for (i
= 0; i
< pos
; ++i
)
4603 bmap
= var_equal(bmap
, i
);
4604 bmap
= var_more_or_equal(bmap
, pos
);
4605 return isl_basic_map_finalize(bmap
);
4608 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4609 unsigned n
, int equal
)
4611 struct isl_map
*map
;
4614 if (n
== 0 && equal
)
4615 return isl_map_universe(dims
);
4617 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4619 for (i
= 0; i
+ 1 < n
; ++i
)
4620 map
= isl_map_add_basic_map(map
,
4621 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4624 map
= isl_map_add_basic_map(map
,
4625 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4627 map
= isl_map_add_basic_map(map
,
4628 isl_basic_map_less_at(dims
, n
- 1));
4630 isl_space_free(dims
);
4635 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4639 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4642 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4644 return map_lex_lte_first(dim
, n
, 0);
4647 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4649 return map_lex_lte_first(dim
, n
, 1);
4652 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4654 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4657 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4659 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4662 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4663 unsigned n
, int equal
)
4665 struct isl_map
*map
;
4668 if (n
== 0 && equal
)
4669 return isl_map_universe(dims
);
4671 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4673 for (i
= 0; i
+ 1 < n
; ++i
)
4674 map
= isl_map_add_basic_map(map
,
4675 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4678 map
= isl_map_add_basic_map(map
,
4679 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4681 map
= isl_map_add_basic_map(map
,
4682 isl_basic_map_more_at(dims
, n
- 1));
4684 isl_space_free(dims
);
4689 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4693 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4696 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4698 return map_lex_gte_first(dim
, n
, 0);
4701 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4703 return map_lex_gte_first(dim
, n
, 1);
4706 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4708 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4711 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4713 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4716 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4717 __isl_take isl_set
*set2
)
4720 map
= isl_map_lex_le(isl_set_get_space(set1
));
4721 map
= isl_map_intersect_domain(map
, set1
);
4722 map
= isl_map_intersect_range(map
, set2
);
4726 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4727 __isl_take isl_set
*set2
)
4730 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4731 map
= isl_map_intersect_domain(map
, set1
);
4732 map
= isl_map_intersect_range(map
, set2
);
4736 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4737 __isl_take isl_set
*set2
)
4740 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4741 map
= isl_map_intersect_domain(map
, set1
);
4742 map
= isl_map_intersect_range(map
, set2
);
4746 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4747 __isl_take isl_set
*set2
)
4750 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4751 map
= isl_map_intersect_domain(map
, set1
);
4752 map
= isl_map_intersect_range(map
, set2
);
4756 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4757 __isl_take isl_map
*map2
)
4760 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4761 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4762 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4766 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4767 __isl_take isl_map
*map2
)
4770 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4771 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4772 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4776 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4777 __isl_take isl_map
*map2
)
4780 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4781 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4782 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4786 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4787 __isl_take isl_map
*map2
)
4790 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4791 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4792 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4796 /* For a div d = floor(f/m), add the constraint
4800 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4801 unsigned pos
, isl_int
*div
)
4804 unsigned total
= isl_basic_map_total_dim(bmap
);
4806 i
= isl_basic_map_alloc_inequality(bmap
);
4808 return isl_stat_error
;
4809 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4810 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4815 /* For a div d = floor(f/m), add the constraint
4817 * -(f-(m-1)) + m d >= 0
4819 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4820 unsigned pos
, isl_int
*div
)
4823 unsigned total
= isl_basic_map_total_dim(bmap
);
4825 i
= isl_basic_map_alloc_inequality(bmap
);
4827 return isl_stat_error
;
4828 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4829 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4830 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4831 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4836 /* For a div d = floor(f/m), add the constraints
4839 * -(f-(m-1)) + m d >= 0
4841 * Note that the second constraint is the negation of
4845 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4846 unsigned pos
, isl_int
*div
)
4848 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4850 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4855 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4856 unsigned pos
, isl_int
*div
)
4858 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4862 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4864 unsigned total
= isl_basic_map_total_dim(bmap
);
4865 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4867 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4871 /* For each known div d = floor(f/m), add the constraints
4874 * -(f-(m-1)) + m d >= 0
4876 * Remove duplicate constraints in case of some these div constraints
4877 * already appear in "bmap".
4879 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4880 __isl_take isl_basic_map
*bmap
)
4886 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4890 bmap
= add_known_div_constraints(bmap
);
4891 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4892 bmap
= isl_basic_map_finalize(bmap
);
4896 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4898 * In particular, if this div is of the form d = floor(f/m),
4899 * then add the constraint
4903 * if sign < 0 or the constraint
4905 * -(f-(m-1)) + m d >= 0
4909 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4910 unsigned div
, int sign
)
4918 total
= isl_basic_map_total_dim(bmap
);
4919 div_pos
= total
- bmap
->n_div
+ div
;
4922 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4924 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4927 struct isl_basic_set
*isl_basic_map_underlying_set(
4928 struct isl_basic_map
*bmap
)
4932 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4934 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4935 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4936 return bset_from_bmap(bmap
);
4937 bmap
= isl_basic_map_cow(bmap
);
4940 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4943 bmap
->extra
-= bmap
->n_div
;
4945 bmap
= isl_basic_map_finalize(bmap
);
4946 return bset_from_bmap(bmap
);
4948 isl_basic_map_free(bmap
);
4952 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4953 __isl_take isl_basic_set
*bset
)
4955 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4958 /* Replace each element in "list" by the result of applying
4959 * isl_basic_map_underlying_set to the element.
4961 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4962 __isl_take isl_basic_map_list
*list
)
4969 n
= isl_basic_map_list_n_basic_map(list
);
4970 for (i
= 0; i
< n
; ++i
) {
4971 isl_basic_map
*bmap
;
4972 isl_basic_set
*bset
;
4974 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4975 bset
= isl_basic_set_underlying_set(bmap
);
4976 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4982 struct isl_basic_map
*isl_basic_map_overlying_set(
4983 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4985 struct isl_basic_map
*bmap
;
4986 struct isl_ctx
*ctx
;
4993 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4994 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4995 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4997 if (like
->n_div
== 0) {
4998 isl_space
*space
= isl_basic_map_get_space(like
);
4999 isl_basic_map_free(like
);
5000 return isl_basic_map_reset_space(bset
, space
);
5002 bset
= isl_basic_set_cow(bset
);
5005 total
= bset
->dim
->n_out
+ bset
->extra
;
5006 bmap
= bset_to_bmap(bset
);
5007 isl_space_free(bmap
->dim
);
5008 bmap
->dim
= isl_space_copy(like
->dim
);
5011 bmap
->n_div
= like
->n_div
;
5012 bmap
->extra
+= like
->n_div
;
5016 ltotal
= total
- bmap
->extra
+ like
->extra
;
5019 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5020 bmap
->extra
* (1 + 1 + total
));
5021 if (isl_blk_is_error(bmap
->block2
))
5023 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5027 for (i
= 0; i
< bmap
->extra
; ++i
)
5028 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5029 for (i
= 0; i
< like
->n_div
; ++i
) {
5030 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5031 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5033 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5035 isl_basic_map_free(like
);
5036 bmap
= isl_basic_map_simplify(bmap
);
5037 bmap
= isl_basic_map_finalize(bmap
);
5040 isl_basic_map_free(like
);
5041 isl_basic_set_free(bset
);
5045 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5046 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5048 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5049 bset_to_bmap(like
)));
5052 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5056 map
= isl_map_cow(map
);
5059 map
->dim
= isl_space_cow(map
->dim
);
5063 for (i
= 1; i
< map
->n
; ++i
)
5064 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5066 for (i
= 0; i
< map
->n
; ++i
) {
5067 map
->p
[i
] = bset_to_bmap(
5068 isl_basic_map_underlying_set(map
->p
[i
]));
5073 map
->dim
= isl_space_underlying(map
->dim
, 0);
5075 isl_space_free(map
->dim
);
5076 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5080 return set_from_map(map
);
5086 /* Replace the space of "bmap" by "space".
5088 * If the space of "bmap" is identical to "space" (including the identifiers
5089 * of the input and output dimensions), then simply return the original input.
5091 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5092 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5095 isl_space
*bmap_space
;
5097 bmap_space
= isl_basic_map_peek_space(bmap
);
5098 equal
= isl_space_is_equal(bmap_space
, space
);
5099 if (equal
>= 0 && equal
)
5100 equal
= isl_space_match(bmap_space
, isl_dim_in
,
5102 if (equal
>= 0 && equal
)
5103 equal
= isl_space_match(bmap_space
, isl_dim_out
,
5104 space
, isl_dim_out
);
5108 isl_space_free(space
);
5111 bmap
= isl_basic_map_cow(bmap
);
5112 if (!bmap
|| !space
)
5115 isl_space_free(bmap
->dim
);
5118 bmap
= isl_basic_map_finalize(bmap
);
5122 isl_basic_map_free(bmap
);
5123 isl_space_free(space
);
5127 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5128 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5130 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5134 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5135 __isl_take isl_space
*dim
)
5139 map
= isl_map_cow(map
);
5143 for (i
= 0; i
< map
->n
; ++i
) {
5144 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5145 isl_space_copy(dim
));
5149 isl_space_free(map
->dim
);
5155 isl_space_free(dim
);
5159 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5160 __isl_take isl_space
*dim
)
5162 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5165 /* Compute the parameter domain of the given basic set.
5167 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5173 is_params
= isl_basic_set_is_params(bset
);
5175 return isl_basic_set_free(bset
);
5179 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5180 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5181 space
= isl_basic_set_get_space(bset
);
5182 space
= isl_space_params(space
);
5183 bset
= isl_basic_set_reset_space(bset
, space
);
5187 /* Construct a zero-dimensional basic set with the given parameter domain.
5189 __isl_give isl_basic_set
*isl_basic_set_from_params(
5190 __isl_take isl_basic_set
*bset
)
5193 space
= isl_basic_set_get_space(bset
);
5194 space
= isl_space_set_from_params(space
);
5195 bset
= isl_basic_set_reset_space(bset
, space
);
5199 /* Compute the parameter domain of the given set.
5201 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5206 if (isl_set_is_params(set
))
5209 n
= isl_set_dim(set
, isl_dim_set
);
5210 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5211 space
= isl_set_get_space(set
);
5212 space
= isl_space_params(space
);
5213 set
= isl_set_reset_space(set
, space
);
5217 /* Construct a zero-dimensional set with the given parameter domain.
5219 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5222 space
= isl_set_get_space(set
);
5223 space
= isl_space_set_from_params(space
);
5224 set
= isl_set_reset_space(set
, space
);
5228 /* Compute the parameter domain of the given map.
5230 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5235 n
= isl_map_dim(map
, isl_dim_in
);
5236 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5237 n
= isl_map_dim(map
, isl_dim_out
);
5238 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5239 space
= isl_map_get_space(map
);
5240 space
= isl_space_params(space
);
5241 map
= isl_map_reset_space(map
, space
);
5245 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5252 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5254 n_out
= isl_basic_map_n_out(bmap
);
5255 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5257 return isl_basic_map_reset_space(bmap
, space
);
5260 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5263 return isl_bool_error
;
5264 return isl_space_may_be_set(bmap
->dim
);
5267 /* Is this basic map actually a set?
5268 * Users should never call this function. Outside of isl,
5269 * the type should indicate whether something is a set or a map.
5271 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5274 return isl_bool_error
;
5275 return isl_space_is_set(bmap
->dim
);
5278 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5282 is_set
= isl_basic_map_is_set(bmap
);
5287 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5289 isl_basic_map_free(bmap
);
5293 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5294 __isl_take isl_basic_map
*bmap
)
5298 isl_basic_map
*domain
;
5299 int nparam
, n_in
, n_out
;
5301 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5302 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5303 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5305 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5306 domain
= isl_basic_map_universe(dim
);
5308 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5309 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5310 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5312 for (i
= 0; i
< n_in
; ++i
)
5313 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5316 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5317 return isl_basic_map_finalize(bmap
);
5320 __isl_give isl_basic_map
*isl_basic_map_range_map(
5321 __isl_take isl_basic_map
*bmap
)
5325 isl_basic_map
*range
;
5326 int nparam
, n_in
, n_out
;
5328 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5329 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5330 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5332 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5333 range
= isl_basic_map_universe(dim
);
5335 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5336 bmap
= isl_basic_map_apply_range(bmap
, range
);
5337 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5339 for (i
= 0; i
< n_out
; ++i
)
5340 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5343 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5344 return isl_basic_map_finalize(bmap
);
5347 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5351 return isl_space_may_be_set(map
->dim
);
5354 /* Is this map actually a set?
5355 * Users should never call this function. Outside of isl,
5356 * the type should indicate whether something is a set or a map.
5358 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5361 return isl_bool_error
;
5362 return isl_space_is_set(map
->dim
);
5365 struct isl_set
*isl_map_range(struct isl_map
*map
)
5369 struct isl_set
*set
;
5371 is_set
= isl_map_is_set(map
);
5375 return set_from_map(map
);
5377 map
= isl_map_cow(map
);
5381 set
= set_from_map(map
);
5382 set
->dim
= isl_space_range(set
->dim
);
5385 for (i
= 0; i
< map
->n
; ++i
) {
5386 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5390 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5391 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5398 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5402 map
= isl_map_cow(map
);
5406 map
->dim
= isl_space_domain_map(map
->dim
);
5409 for (i
= 0; i
< map
->n
; ++i
) {
5410 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5414 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5415 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5422 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5425 isl_space
*range_dim
;
5427 map
= isl_map_cow(map
);
5431 range_dim
= isl_space_range(isl_map_get_space(map
));
5432 range_dim
= isl_space_from_range(range_dim
);
5433 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5434 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5437 for (i
= 0; i
< map
->n
; ++i
) {
5438 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5442 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5443 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5450 /* Given a wrapped map of the form A[B -> C],
5451 * return the map A[B -> C] -> B.
5453 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5460 if (!isl_set_has_tuple_id(set
))
5461 return isl_map_domain_map(isl_set_unwrap(set
));
5463 id
= isl_set_get_tuple_id(set
);
5464 map
= isl_map_domain_map(isl_set_unwrap(set
));
5465 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5470 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5471 __isl_take isl_basic_set
*bset
)
5473 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5476 __isl_give isl_basic_map
*isl_basic_map_from_range(
5477 __isl_take isl_basic_set
*bset
)
5480 space
= isl_basic_set_get_space(bset
);
5481 space
= isl_space_from_range(space
);
5482 bset
= isl_basic_set_reset_space(bset
, space
);
5483 return bset_to_bmap(bset
);
5486 /* Create a relation with the given set as range.
5487 * The domain of the created relation is a zero-dimensional
5488 * flat anonymous space.
5490 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5493 space
= isl_set_get_space(set
);
5494 space
= isl_space_from_range(space
);
5495 set
= isl_set_reset_space(set
, space
);
5496 return set_to_map(set
);
5499 /* Create a relation with the given set as domain.
5500 * The range of the created relation is a zero-dimensional
5501 * flat anonymous space.
5503 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5505 return isl_map_reverse(isl_map_from_range(set
));
5508 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5509 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5511 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5514 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5515 __isl_take isl_set
*range
)
5517 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5520 /* Return a newly allocated isl_map with given space and flags and
5521 * room for "n" basic maps.
5522 * Make sure that all cached information is cleared.
5524 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5527 struct isl_map
*map
;
5532 isl_die(space
->ctx
, isl_error_internal
,
5533 "negative number of basic maps", goto error
);
5534 map
= isl_calloc(space
->ctx
, struct isl_map
,
5535 sizeof(struct isl_map
) +
5536 (n
- 1) * sizeof(struct isl_basic_map
*));
5540 map
->ctx
= space
->ctx
;
5541 isl_ctx_ref(map
->ctx
);
5549 isl_space_free(space
);
5553 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5555 struct isl_basic_map
*bmap
;
5556 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5557 bmap
= isl_basic_map_set_to_empty(bmap
);
5561 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5563 struct isl_basic_set
*bset
;
5564 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5565 bset
= isl_basic_set_set_to_empty(bset
);
5569 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5571 struct isl_basic_map
*bmap
;
5572 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5573 bmap
= isl_basic_map_finalize(bmap
);
5577 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5579 struct isl_basic_set
*bset
;
5580 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5581 bset
= isl_basic_set_finalize(bset
);
5585 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5588 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5589 isl_basic_map
*bmap
;
5591 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5592 for (i
= 0; i
< total
; ++i
) {
5593 int k
= isl_basic_map_alloc_inequality(bmap
);
5596 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5597 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5601 isl_basic_map_free(bmap
);
5605 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5607 return isl_basic_map_nat_universe(dim
);
5610 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5612 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5615 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5617 return isl_map_nat_universe(dim
);
5620 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5622 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5625 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5627 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5630 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5632 struct isl_map
*map
;
5635 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5636 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5640 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5642 struct isl_set
*set
;
5645 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5646 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5650 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5653 struct isl_map
*dup
;
5657 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5658 for (i
= 0; i
< map
->n
; ++i
)
5659 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5663 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5664 __isl_take isl_basic_map
*bmap
)
5668 if (isl_basic_map_plain_is_empty(bmap
)) {
5669 isl_basic_map_free(bmap
);
5672 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5673 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5674 map
->p
[map
->n
] = bmap
;
5676 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5682 isl_basic_map_free(bmap
);
5686 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5697 isl_ctx_deref(map
->ctx
);
5698 for (i
= 0; i
< map
->n
; ++i
)
5699 isl_basic_map_free(map
->p
[i
]);
5700 isl_space_free(map
->dim
);
5706 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5707 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5711 bmap
= isl_basic_map_cow(bmap
);
5712 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5713 j
= isl_basic_map_alloc_equality(bmap
);
5716 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5717 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5718 isl_int_set_si(bmap
->eq
[j
][0], value
);
5719 bmap
= isl_basic_map_simplify(bmap
);
5720 return isl_basic_map_finalize(bmap
);
5722 isl_basic_map_free(bmap
);
5726 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5727 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5731 bmap
= isl_basic_map_cow(bmap
);
5732 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5733 j
= isl_basic_map_alloc_equality(bmap
);
5736 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5737 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5738 isl_int_set(bmap
->eq
[j
][0], value
);
5739 bmap
= isl_basic_map_simplify(bmap
);
5740 return isl_basic_map_finalize(bmap
);
5742 isl_basic_map_free(bmap
);
5746 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5747 enum isl_dim_type type
, unsigned pos
, int value
)
5749 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5750 return isl_basic_map_free(bmap
);
5751 return isl_basic_map_fix_pos_si(bmap
,
5752 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5755 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5756 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5758 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5759 return isl_basic_map_free(bmap
);
5760 return isl_basic_map_fix_pos(bmap
,
5761 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5764 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5765 * to be equal to "v".
5767 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5768 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5772 if (!isl_val_is_int(v
))
5773 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5774 "expecting integer value", goto error
);
5775 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5777 pos
+= isl_basic_map_offset(bmap
, type
);
5778 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5782 isl_basic_map_free(bmap
);
5787 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5788 * to be equal to "v".
5790 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5791 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5793 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5796 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5797 enum isl_dim_type type
, unsigned pos
, int value
)
5799 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5803 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5804 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5806 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5810 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5811 unsigned input
, int value
)
5813 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5816 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5817 unsigned dim
, int value
)
5819 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5820 isl_dim_set
, dim
, value
));
5823 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5825 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5832 isl_basic_map_free(map
->p
[i
]);
5833 if (i
!= map
->n
- 1) {
5834 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5835 map
->p
[i
] = map
->p
[map
->n
- 1];
5842 /* Perform "fn" on each basic map of "map", where we may not be holding
5843 * the only reference to "map".
5844 * In particular, "fn" should be a semantics preserving operation
5845 * that we want to apply to all copies of "map". We therefore need
5846 * to be careful not to modify "map" in a way that breaks "map"
5847 * in case anything goes wrong.
5849 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5850 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5852 struct isl_basic_map
*bmap
;
5858 for (i
= map
->n
- 1; i
>= 0; --i
) {
5859 bmap
= isl_basic_map_copy(map
->p
[i
]);
5863 isl_basic_map_free(map
->p
[i
]);
5865 if (remove_if_empty(map
, i
) < 0)
5875 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5876 enum isl_dim_type type
, unsigned pos
, int value
)
5880 map
= isl_map_cow(map
);
5884 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5885 for (i
= map
->n
- 1; i
>= 0; --i
) {
5886 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5887 if (remove_if_empty(map
, i
) < 0)
5890 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5897 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5898 enum isl_dim_type type
, unsigned pos
, int value
)
5900 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5903 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5904 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5908 map
= isl_map_cow(map
);
5912 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5913 for (i
= 0; i
< map
->n
; ++i
) {
5914 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5918 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5925 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5926 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5928 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5931 /* Fix the value of the variable at position "pos" of type "type" of "map"
5932 * to be equal to "v".
5934 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5935 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5939 map
= isl_map_cow(map
);
5943 if (!isl_val_is_int(v
))
5944 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5945 "expecting integer value", goto error
);
5946 if (pos
>= isl_map_dim(map
, type
))
5947 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5948 "index out of bounds", goto error
);
5949 for (i
= map
->n
- 1; i
>= 0; --i
) {
5950 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5952 if (remove_if_empty(map
, i
) < 0)
5955 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5964 /* Fix the value of the variable at position "pos" of type "type" of "set"
5965 * to be equal to "v".
5967 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5968 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5970 return isl_map_fix_val(set
, type
, pos
, v
);
5973 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5974 unsigned input
, int value
)
5976 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5979 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5981 return set_from_map(isl_map_fix_si(set_to_map(set
),
5982 isl_dim_set
, dim
, value
));
5985 static __isl_give isl_basic_map
*basic_map_bound_si(
5986 __isl_take isl_basic_map
*bmap
,
5987 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5991 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5992 return isl_basic_map_free(bmap
);
5993 pos
+= isl_basic_map_offset(bmap
, type
);
5994 bmap
= isl_basic_map_cow(bmap
);
5995 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5996 j
= isl_basic_map_alloc_inequality(bmap
);
5999 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6001 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6002 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6004 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6005 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6007 bmap
= isl_basic_map_simplify(bmap
);
6008 return isl_basic_map_finalize(bmap
);
6010 isl_basic_map_free(bmap
);
6014 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6015 __isl_take isl_basic_map
*bmap
,
6016 enum isl_dim_type type
, unsigned pos
, int value
)
6018 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6021 /* Constrain the values of the given dimension to be no greater than "value".
6023 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6024 __isl_take isl_basic_map
*bmap
,
6025 enum isl_dim_type type
, unsigned pos
, int value
)
6027 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6030 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6031 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6035 map
= isl_map_cow(map
);
6039 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6040 for (i
= 0; i
< map
->n
; ++i
) {
6041 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6042 type
, pos
, value
, upper
);
6046 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6053 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6054 enum isl_dim_type type
, unsigned pos
, int value
)
6056 return map_bound_si(map
, type
, pos
, value
, 0);
6059 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6060 enum isl_dim_type type
, unsigned pos
, int value
)
6062 return map_bound_si(map
, type
, pos
, value
, 1);
6065 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6066 enum isl_dim_type type
, unsigned pos
, int value
)
6068 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6072 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6073 enum isl_dim_type type
, unsigned pos
, int value
)
6075 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6078 /* Bound the given variable of "bmap" from below (or above is "upper"
6079 * is set) to "value".
6081 static __isl_give isl_basic_map
*basic_map_bound(
6082 __isl_take isl_basic_map
*bmap
,
6083 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6087 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6088 return isl_basic_map_free(bmap
);
6089 pos
+= isl_basic_map_offset(bmap
, type
);
6090 bmap
= isl_basic_map_cow(bmap
);
6091 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6092 j
= isl_basic_map_alloc_inequality(bmap
);
6095 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6097 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6098 isl_int_set(bmap
->ineq
[j
][0], value
);
6100 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6101 isl_int_neg(bmap
->ineq
[j
][0], value
);
6103 bmap
= isl_basic_map_simplify(bmap
);
6104 return isl_basic_map_finalize(bmap
);
6106 isl_basic_map_free(bmap
);
6110 /* Bound the given variable of "map" from below (or above is "upper"
6111 * is set) to "value".
6113 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6114 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6118 map
= isl_map_cow(map
);
6122 if (pos
>= isl_map_dim(map
, type
))
6123 isl_die(map
->ctx
, isl_error_invalid
,
6124 "index out of bounds", goto error
);
6125 for (i
= map
->n
- 1; i
>= 0; --i
) {
6126 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6127 if (remove_if_empty(map
, i
) < 0)
6130 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6137 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6138 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6140 return map_bound(map
, type
, pos
, value
, 0);
6143 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6144 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6146 return map_bound(map
, type
, pos
, value
, 1);
6149 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6150 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6152 return isl_map_lower_bound(set
, type
, pos
, value
);
6155 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6156 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6158 return isl_map_upper_bound(set
, type
, pos
, value
);
6161 /* Force the values of the variable at position "pos" of type "type" of "set"
6162 * to be no smaller than "value".
6164 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6165 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6169 if (!isl_val_is_int(value
))
6170 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6171 "expecting integer value", goto error
);
6172 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6173 isl_val_free(value
);
6176 isl_val_free(value
);
6181 /* Force the values of the variable at position "pos" of type "type" of "set"
6182 * to be no greater than "value".
6184 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6185 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6189 if (!isl_val_is_int(value
))
6190 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6191 "expecting integer value", goto error
);
6192 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6193 isl_val_free(value
);
6196 isl_val_free(value
);
6201 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6205 map
= isl_map_cow(map
);
6209 map
->dim
= isl_space_reverse(map
->dim
);
6212 for (i
= 0; i
< map
->n
; ++i
) {
6213 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6217 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6225 #define TYPE isl_pw_multi_aff
6227 #define SUFFIX _pw_multi_aff
6229 #define EMPTY isl_pw_multi_aff_empty
6231 #define ADD isl_pw_multi_aff_union_add
6232 #include "isl_map_lexopt_templ.c"
6234 /* Given a map "map", compute the lexicographically minimal
6235 * (or maximal) image element for each domain element in dom,
6236 * in the form of an isl_pw_multi_aff.
6237 * If "empty" is not NULL, then set *empty to those elements in dom that
6238 * do not have an image element.
6239 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6240 * should be computed over the domain of "map". "empty" is also NULL
6243 * We first compute the lexicographically minimal or maximal element
6244 * in the first basic map. This results in a partial solution "res"
6245 * and a subset "todo" of dom that still need to be handled.
6246 * We then consider each of the remaining maps in "map" and successively
6247 * update both "res" and "todo".
6248 * If "empty" is NULL, then the todo sets are not needed and therefore
6249 * also not computed.
6251 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6252 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6253 __isl_give isl_set
**empty
, unsigned flags
)
6257 isl_pw_multi_aff
*res
;
6260 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6261 if (!map
|| (!full
&& !dom
))
6264 if (isl_map_plain_is_empty(map
)) {
6269 return isl_pw_multi_aff_from_map(map
);
6272 res
= basic_map_partial_lexopt_pw_multi_aff(
6273 isl_basic_map_copy(map
->p
[0]),
6274 isl_set_copy(dom
), empty
, flags
);
6278 for (i
= 1; i
< map
->n
; ++i
) {
6279 isl_pw_multi_aff
*res_i
;
6281 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6282 isl_basic_map_copy(map
->p
[i
]),
6283 isl_set_copy(dom
), empty
, flags
);
6285 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6286 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6288 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6291 todo
= isl_set_intersect(todo
, *empty
);
6310 #define TYPE isl_map
6314 #define EMPTY isl_map_empty
6316 #define ADD isl_map_union_disjoint
6317 #include "isl_map_lexopt_templ.c"
6319 /* Given a map "map", compute the lexicographically minimal
6320 * (or maximal) image element for each domain element in "dom",
6321 * in the form of an isl_map.
6322 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6323 * do not have an image element.
6324 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6325 * should be computed over the domain of "map". "empty" is also NULL
6328 * If the input consists of more than one disjunct, then first
6329 * compute the desired result in the form of an isl_pw_multi_aff and
6330 * then convert that into an isl_map.
6332 * This function used to have an explicit implementation in terms
6333 * of isl_maps, but it would continually intersect the domains of
6334 * partial results with the complement of the domain of the next
6335 * partial solution, potentially leading to an explosion in the number
6336 * of disjuncts if there are several disjuncts in the input.
6337 * An even earlier implementation of this function would look for
6338 * better results in the domain of the partial result and for extra
6339 * results in the complement of this domain, which would lead to
6340 * even more splintering.
6342 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6343 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6344 __isl_give isl_set
**empty
, unsigned flags
)
6347 struct isl_map
*res
;
6348 isl_pw_multi_aff
*pma
;
6350 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6351 if (!map
|| (!full
&& !dom
))
6354 if (isl_map_plain_is_empty(map
)) {
6363 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6369 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6371 return isl_map_from_pw_multi_aff(pma
);
6380 __isl_give isl_map
*isl_map_partial_lexmax(
6381 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6382 __isl_give isl_set
**empty
)
6384 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6387 __isl_give isl_map
*isl_map_partial_lexmin(
6388 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6389 __isl_give isl_set
**empty
)
6391 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6394 __isl_give isl_set
*isl_set_partial_lexmin(
6395 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6396 __isl_give isl_set
**empty
)
6398 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6402 __isl_give isl_set
*isl_set_partial_lexmax(
6403 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6404 __isl_give isl_set
**empty
)
6406 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6410 /* Compute the lexicographic minimum (or maximum if "flags" includes
6411 * ISL_OPT_MAX) of "bset" over its parametric domain.
6413 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6416 return isl_basic_map_lexopt(bset
, flags
);
6419 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6421 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6424 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6426 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6429 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6431 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6434 /* Compute the lexicographic minimum of "bset" over its parametric domain
6435 * for the purpose of quantifier elimination.
6436 * That is, find an explicit representation for all the existentially
6437 * quantified variables in "bset" by computing their lexicographic
6440 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6441 __isl_take isl_basic_set
*bset
)
6443 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6446 /* Extract the first and only affine expression from list
6447 * and then add it to *pwaff with the given dom.
6448 * This domain is known to be disjoint from other domains
6449 * because of the way isl_basic_map_foreach_lexmax works.
6451 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6452 __isl_take isl_aff_list
*list
, void *user
)
6454 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6456 isl_pw_aff
**pwaff
= user
;
6457 isl_pw_aff
*pwaff_i
;
6461 if (isl_aff_list_n_aff(list
) != 1)
6462 isl_die(ctx
, isl_error_internal
,
6463 "expecting single element list", goto error
);
6465 aff
= isl_aff_list_get_aff(list
, 0);
6466 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6468 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6470 isl_aff_list_free(list
);
6474 isl_basic_set_free(dom
);
6475 isl_aff_list_free(list
);
6476 return isl_stat_error
;
6479 /* Given a basic map with one output dimension, compute the minimum or
6480 * maximum of that dimension as an isl_pw_aff.
6482 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6483 * call update_dim_opt on each leaf of the result.
6485 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6488 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6492 dim
= isl_space_from_domain(isl_space_domain(dim
));
6493 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6494 pwaff
= isl_pw_aff_empty(dim
);
6496 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6498 return isl_pw_aff_free(pwaff
);
6503 /* Compute the minimum or maximum of the given output dimension
6504 * as a function of the parameters and the input dimensions,
6505 * but independently of the other output dimensions.
6507 * We first project out the other output dimension and then compute
6508 * the "lexicographic" maximum in each basic map, combining the results
6509 * using isl_pw_aff_union_max.
6511 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6518 n_out
= isl_map_dim(map
, isl_dim_out
);
6519 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6520 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6525 isl_space
*dim
= isl_map_get_space(map
);
6527 return isl_pw_aff_empty(dim
);
6530 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6531 for (i
= 1; i
< map
->n
; ++i
) {
6532 isl_pw_aff
*pwaff_i
;
6534 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6535 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6543 /* Compute the minimum of the given output dimension as a function of the
6544 * parameters and input dimensions, but independently of
6545 * the other output dimensions.
6547 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6549 return map_dim_opt(map
, pos
, 0);
6552 /* Compute the maximum of the given output dimension as a function of the
6553 * parameters and input dimensions, but independently of
6554 * the other output dimensions.
6556 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6558 return map_dim_opt(map
, pos
, 1);
6561 /* Compute the minimum or maximum of the given set dimension
6562 * as a function of the parameters,
6563 * but independently of the other set dimensions.
6565 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6568 return map_dim_opt(set
, pos
, max
);
6571 /* Compute the maximum of the given set dimension as a function of the
6572 * parameters, but independently of the other set dimensions.
6574 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6576 return set_dim_opt(set
, pos
, 1);
6579 /* Compute the minimum of the given set dimension as a function of the
6580 * parameters, but independently of the other set dimensions.
6582 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6584 return set_dim_opt(set
, pos
, 0);
6587 /* Apply a preimage specified by "mat" on the parameters of "bset".
6588 * bset is assumed to have only parameters and divs.
6590 static struct isl_basic_set
*basic_set_parameter_preimage(
6591 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6598 bset
->dim
= isl_space_cow(bset
->dim
);
6602 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6604 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6606 bset
->dim
->nparam
= 0;
6607 bset
->dim
->n_out
= nparam
;
6608 bset
= isl_basic_set_preimage(bset
, mat
);
6610 bset
->dim
->nparam
= bset
->dim
->n_out
;
6611 bset
->dim
->n_out
= 0;
6616 isl_basic_set_free(bset
);
6620 /* Apply a preimage specified by "mat" on the parameters of "set".
6621 * set is assumed to have only parameters and divs.
6623 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6624 __isl_take isl_mat
*mat
)
6632 nparam
= isl_set_dim(set
, isl_dim_param
);
6634 if (mat
->n_row
!= 1 + nparam
)
6635 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6636 "unexpected number of rows", goto error
);
6638 space
= isl_set_get_space(set
);
6639 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6640 isl_dim_param
, 0, nparam
);
6641 set
= isl_set_reset_space(set
, space
);
6642 set
= isl_set_preimage(set
, mat
);
6643 nparam
= isl_set_dim(set
, isl_dim_out
);
6644 space
= isl_set_get_space(set
);
6645 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6646 isl_dim_out
, 0, nparam
);
6647 set
= isl_set_reset_space(set
, space
);
6655 /* Intersect the basic set "bset" with the affine space specified by the
6656 * equalities in "eq".
6658 static struct isl_basic_set
*basic_set_append_equalities(
6659 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6667 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6672 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6673 for (i
= 0; i
< eq
->n_row
; ++i
) {
6674 k
= isl_basic_set_alloc_equality(bset
);
6677 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6678 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6682 bset
= isl_basic_set_gauss(bset
, NULL
);
6683 bset
= isl_basic_set_finalize(bset
);
6688 isl_basic_set_free(bset
);
6692 /* Intersect the set "set" with the affine space specified by the
6693 * equalities in "eq".
6695 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6703 for (i
= 0; i
< set
->n
; ++i
) {
6704 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6717 /* Given a basic set "bset" that only involves parameters and existentially
6718 * quantified variables, return the index of the first equality
6719 * that only involves parameters. If there is no such equality then
6720 * return bset->n_eq.
6722 * This function assumes that isl_basic_set_gauss has been called on "bset".
6724 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6727 unsigned nparam
, n_div
;
6732 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6733 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6735 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6736 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6743 /* Compute an explicit representation for the existentially quantified
6744 * variables in "bset" by computing the "minimal value" of the set
6745 * variables. Since there are no set variables, the computation of
6746 * the minimal value essentially computes an explicit representation
6747 * of the non-empty part(s) of "bset".
6749 * The input only involves parameters and existentially quantified variables.
6750 * All equalities among parameters have been removed.
6752 * Since the existentially quantified variables in the result are in general
6753 * going to be different from those in the input, we first replace
6754 * them by the minimal number of variables based on their equalities.
6755 * This should simplify the parametric integer programming.
6757 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6759 isl_morph
*morph1
, *morph2
;
6765 if (bset
->n_eq
== 0)
6766 return isl_basic_set_lexmin_compute_divs(bset
);
6768 morph1
= isl_basic_set_parameter_compression(bset
);
6769 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6770 bset
= isl_basic_set_lift(bset
);
6771 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6772 bset
= isl_morph_basic_set(morph2
, bset
);
6773 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6774 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6776 set
= isl_basic_set_lexmin_compute_divs(bset
);
6778 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6783 /* Project the given basic set onto its parameter domain, possibly introducing
6784 * new, explicit, existential variables in the constraints.
6785 * The input has parameters and (possibly implicit) existential variables.
6786 * The output has the same parameters, but only
6787 * explicit existentially quantified variables.
6789 * The actual projection is performed by pip, but pip doesn't seem
6790 * to like equalities very much, so we first remove the equalities
6791 * among the parameters by performing a variable compression on
6792 * the parameters. Afterward, an inverse transformation is performed
6793 * and the equalities among the parameters are inserted back in.
6795 * The variable compression on the parameters may uncover additional
6796 * equalities that were only implicit before. We therefore check
6797 * if there are any new parameter equalities in the result and
6798 * if so recurse. The removal of parameter equalities is required
6799 * for the parameter compression performed by base_compute_divs.
6801 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6805 struct isl_mat
*T
, *T2
;
6806 struct isl_set
*set
;
6809 bset
= isl_basic_set_cow(bset
);
6813 if (bset
->n_eq
== 0)
6814 return base_compute_divs(bset
);
6816 bset
= isl_basic_set_gauss(bset
, NULL
);
6819 if (isl_basic_set_plain_is_empty(bset
))
6820 return isl_set_from_basic_set(bset
);
6822 i
= first_parameter_equality(bset
);
6823 if (i
== bset
->n_eq
)
6824 return base_compute_divs(bset
);
6826 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6827 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6829 eq
= isl_mat_cow(eq
);
6830 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6831 if (T
&& T
->n_col
== 0) {
6835 bset
= isl_basic_set_set_to_empty(bset
);
6836 return isl_set_from_basic_set(bset
);
6838 bset
= basic_set_parameter_preimage(bset
, T
);
6840 i
= first_parameter_equality(bset
);
6843 else if (i
== bset
->n_eq
)
6844 set
= base_compute_divs(bset
);
6846 set
= parameter_compute_divs(bset
);
6847 set
= set_parameter_preimage(set
, T2
);
6848 set
= set_append_equalities(set
, eq
);
6852 /* Insert the divs from "ls" before those of "bmap".
6854 * The number of columns is not changed, which means that the last
6855 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6856 * The caller is responsible for removing the same number of dimensions
6857 * from the space of "bmap".
6859 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6860 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6866 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6870 old_n_div
= bmap
->n_div
;
6871 bmap
= insert_div_rows(bmap
, n_div
);
6875 for (i
= 0; i
< n_div
; ++i
) {
6876 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6877 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6883 /* Replace the space of "bmap" by the space and divs of "ls".
6885 * If "ls" has any divs, then we simplify the result since we may
6886 * have discovered some additional equalities that could simplify
6887 * the div expressions.
6889 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6890 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6894 bmap
= isl_basic_map_cow(bmap
);
6898 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6899 bmap
= insert_divs_from_local_space(bmap
, ls
);
6903 isl_space_free(bmap
->dim
);
6904 bmap
->dim
= isl_local_space_get_space(ls
);
6908 isl_local_space_free(ls
);
6910 bmap
= isl_basic_map_simplify(bmap
);
6911 bmap
= isl_basic_map_finalize(bmap
);
6914 isl_basic_map_free(bmap
);
6915 isl_local_space_free(ls
);
6919 /* Replace the space of "map" by the space and divs of "ls".
6921 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6922 __isl_take isl_local_space
*ls
)
6926 map
= isl_map_cow(map
);
6930 for (i
= 0; i
< map
->n
; ++i
) {
6931 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6932 isl_local_space_copy(ls
));
6936 isl_space_free(map
->dim
);
6937 map
->dim
= isl_local_space_get_space(ls
);
6941 isl_local_space_free(ls
);
6944 isl_local_space_free(ls
);
6949 /* Compute an explicit representation for the existentially
6950 * quantified variables for which do not know any explicit representation yet.
6952 * We first sort the existentially quantified variables so that the
6953 * existentially quantified variables for which we already have an explicit
6954 * representation are placed before those for which we do not.
6955 * The input dimensions, the output dimensions and the existentially
6956 * quantified variables for which we already have an explicit
6957 * representation are then turned into parameters.
6958 * compute_divs returns a map with the same parameters and
6959 * no input or output dimensions and the dimension specification
6960 * is reset to that of the input, including the existentially quantified
6961 * variables for which we already had an explicit representation.
6963 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6965 struct isl_basic_set
*bset
;
6966 struct isl_set
*set
;
6967 struct isl_map
*map
;
6969 isl_local_space
*ls
;
6976 bmap
= isl_basic_map_sort_divs(bmap
);
6977 bmap
= isl_basic_map_cow(bmap
);
6981 n_known
= isl_basic_map_first_unknown_div(bmap
);
6983 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6985 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6986 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6987 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6988 dim
= isl_space_set_alloc(bmap
->ctx
,
6989 nparam
+ n_in
+ n_out
+ n_known
, 0);
6993 ls
= isl_basic_map_get_local_space(bmap
);
6994 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6995 n_known
, bmap
->n_div
- n_known
);
6997 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6998 swap_div(bmap
, i
- n_known
, i
);
6999 bmap
->n_div
-= n_known
;
7000 bmap
->extra
-= n_known
;
7002 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7003 bset
= bset_from_bmap(bmap
);
7005 set
= parameter_compute_divs(bset
);
7006 map
= set_to_map(set
);
7007 map
= replace_space_by_local_space(map
, ls
);
7011 isl_basic_map_free(bmap
);
7015 /* Remove the explicit representation of local variable "div",
7018 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7019 __isl_take isl_basic_map
*bmap
, int div
)
7023 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7025 return isl_basic_map_free(bmap
);
7029 bmap
= isl_basic_map_cow(bmap
);
7032 isl_int_set_si(bmap
->div
[div
][0], 0);
7036 /* Is local variable "div" of "bmap" marked as not having an explicit
7038 * Note that even if "div" is not marked in this way and therefore
7039 * has an explicit representation, this representation may still
7040 * depend (indirectly) on other local variables that do not
7041 * have an explicit representation.
7043 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7046 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7047 return isl_bool_error
;
7048 return isl_int_is_zero(bmap
->div
[div
][0]);
7051 /* Return the position of the first local variable that does not
7052 * have an explicit representation.
7053 * Return the total number of local variables if they all have
7054 * an explicit representation.
7055 * Return -1 on error.
7057 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7064 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7065 if (!isl_basic_map_div_is_known(bmap
, i
))
7071 /* Return the position of the first local variable that does not
7072 * have an explicit representation.
7073 * Return the total number of local variables if they all have
7074 * an explicit representation.
7075 * Return -1 on error.
7077 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7079 return isl_basic_map_first_unknown_div(bset
);
7082 /* Does "bmap" have an explicit representation for all local variables?
7084 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7088 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7089 first
= isl_basic_map_first_unknown_div(bmap
);
7091 return isl_bool_error
;
7095 /* Do all basic maps in "map" have an explicit representation
7096 * for all local variables?
7098 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7103 return isl_bool_error
;
7105 for (i
= 0; i
< map
->n
; ++i
) {
7106 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7111 return isl_bool_true
;
7114 /* If bmap contains any unknown divs, then compute explicit
7115 * expressions for them. However, this computation may be
7116 * quite expensive, so first try to remove divs that aren't
7119 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7122 struct isl_map
*map
;
7124 known
= isl_basic_map_divs_known(bmap
);
7128 return isl_map_from_basic_map(bmap
);
7130 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7132 known
= isl_basic_map_divs_known(bmap
);
7136 return isl_map_from_basic_map(bmap
);
7138 map
= compute_divs(bmap
);
7141 isl_basic_map_free(bmap
);
7145 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7149 struct isl_map
*res
;
7156 known
= isl_map_divs_known(map
);
7164 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7165 for (i
= 1 ; i
< map
->n
; ++i
) {
7167 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7168 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7169 res
= isl_map_union_disjoint(res
, r2
);
7171 res
= isl_map_union(res
, r2
);
7178 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7180 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7183 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7185 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7188 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7191 struct isl_set
*set
;
7196 map
= isl_map_cow(map
);
7200 set
= set_from_map(map
);
7201 set
->dim
= isl_space_domain(set
->dim
);
7204 for (i
= 0; i
< map
->n
; ++i
) {
7205 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7209 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7210 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7217 /* Return the union of "map1" and "map2", where we assume for now that
7218 * "map1" and "map2" are disjoint. Note that the basic maps inside
7219 * "map1" or "map2" may not be disjoint from each other.
7220 * Also note that this function is also called from isl_map_union,
7221 * which takes care of handling the situation where "map1" and "map2"
7222 * may not be disjoint.
7224 * If one of the inputs is empty, we can simply return the other input.
7225 * Similarly, if one of the inputs is universal, then it is equal to the union.
7227 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7228 __isl_take isl_map
*map2
)
7232 struct isl_map
*map
= NULL
;
7238 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7239 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7240 "spaces don't match", goto error
);
7251 is_universe
= isl_map_plain_is_universe(map1
);
7252 if (is_universe
< 0)
7259 is_universe
= isl_map_plain_is_universe(map2
);
7260 if (is_universe
< 0)
7267 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7268 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7269 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7271 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7272 map1
->n
+ map2
->n
, flags
);
7275 for (i
= 0; i
< map1
->n
; ++i
) {
7276 map
= isl_map_add_basic_map(map
,
7277 isl_basic_map_copy(map1
->p
[i
]));
7281 for (i
= 0; i
< map2
->n
; ++i
) {
7282 map
= isl_map_add_basic_map(map
,
7283 isl_basic_map_copy(map2
->p
[i
]));
7297 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7298 * guaranteed to be disjoint by the caller.
7300 * Note that this functions is called from within isl_map_make_disjoint,
7301 * so we have to be careful not to touch the constraints of the inputs
7304 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7305 __isl_take isl_map
*map2
)
7307 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7310 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7311 * not be disjoint. The parameters are assumed to have been aligned.
7313 * We currently simply call map_union_disjoint, the internal operation
7314 * of which does not really depend on the inputs being disjoint.
7315 * If the result contains more than one basic map, then we clear
7316 * the disjoint flag since the result may contain basic maps from
7317 * both inputs and these are not guaranteed to be disjoint.
7319 * As a special case, if "map1" and "map2" are obviously equal,
7320 * then we simply return "map1".
7322 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7323 __isl_take isl_map
*map2
)
7330 equal
= isl_map_plain_is_equal(map1
, map2
);
7338 map1
= map_union_disjoint(map1
, map2
);
7342 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7350 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7353 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7354 __isl_take isl_map
*map2
)
7356 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7359 struct isl_set
*isl_set_union_disjoint(
7360 struct isl_set
*set1
, struct isl_set
*set2
)
7362 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7366 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7368 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7371 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7374 * "map" and "set" are assumed to be compatible and non-NULL.
7376 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7377 __isl_take isl_set
*set
,
7378 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7379 __isl_take isl_basic_set
*bset
))
7382 struct isl_map
*result
;
7385 if (isl_set_plain_is_universe(set
)) {
7390 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7391 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7392 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7394 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7395 map
->n
* set
->n
, flags
);
7396 for (i
= 0; result
&& i
< map
->n
; ++i
)
7397 for (j
= 0; j
< set
->n
; ++j
) {
7398 result
= isl_map_add_basic_map(result
,
7399 fn(isl_basic_map_copy(map
->p
[i
]),
7400 isl_basic_set_copy(set
->p
[j
])));
7410 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7411 __isl_take isl_set
*set
)
7415 ok
= isl_map_compatible_range(map
, set
);
7419 isl_die(set
->ctx
, isl_error_invalid
,
7420 "incompatible spaces", goto error
);
7422 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7429 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7430 __isl_take isl_set
*set
)
7432 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7435 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7436 __isl_take isl_set
*set
)
7440 ok
= isl_map_compatible_domain(map
, set
);
7444 isl_die(set
->ctx
, isl_error_invalid
,
7445 "incompatible spaces", goto error
);
7447 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7454 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7455 __isl_take isl_set
*set
)
7457 return isl_map_align_params_map_map_and(map
, set
,
7458 &map_intersect_domain
);
7461 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7462 __isl_take isl_map
*map2
)
7466 map1
= isl_map_reverse(map1
);
7467 map1
= isl_map_apply_range(map1
, map2
);
7468 return isl_map_reverse(map1
);
7475 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7476 __isl_take isl_map
*map2
)
7478 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7481 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7482 __isl_take isl_map
*map2
)
7484 isl_space
*dim_result
;
7485 struct isl_map
*result
;
7491 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7492 isl_space_copy(map2
->dim
));
7494 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7497 for (i
= 0; i
< map1
->n
; ++i
)
7498 for (j
= 0; j
< map2
->n
; ++j
) {
7499 result
= isl_map_add_basic_map(result
,
7500 isl_basic_map_apply_range(
7501 isl_basic_map_copy(map1
->p
[i
]),
7502 isl_basic_map_copy(map2
->p
[j
])));
7508 if (result
&& result
->n
<= 1)
7509 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7517 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7518 __isl_take isl_map
*map2
)
7520 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7524 * returns range - domain
7526 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7528 isl_space
*target_space
;
7529 struct isl_basic_set
*bset
;
7536 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7537 bmap
->dim
, isl_dim_out
),
7539 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7540 dim
= isl_basic_map_n_in(bmap
);
7541 nparam
= isl_basic_map_n_param(bmap
);
7542 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7543 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7544 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7545 for (i
= 0; i
< dim
; ++i
) {
7546 int j
= isl_basic_map_alloc_equality(bmap
);
7548 bmap
= isl_basic_map_free(bmap
);
7551 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7552 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7553 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7554 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7556 bset
= isl_basic_map_domain(bmap
);
7557 bset
= isl_basic_set_reset_space(bset
, target_space
);
7560 isl_basic_map_free(bmap
);
7565 * returns range - domain
7567 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7571 struct isl_set
*result
;
7576 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7577 map
->dim
, isl_dim_out
),
7579 dim
= isl_map_get_space(map
);
7580 dim
= isl_space_domain(dim
);
7581 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7584 for (i
= 0; i
< map
->n
; ++i
)
7585 result
= isl_set_add_basic_set(result
,
7586 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7595 * returns [domain -> range] -> range - domain
7597 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7598 __isl_take isl_basic_map
*bmap
)
7602 isl_basic_map
*domain
;
7606 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7607 bmap
->dim
, isl_dim_out
))
7608 isl_die(bmap
->ctx
, isl_error_invalid
,
7609 "domain and range don't match", goto error
);
7611 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7612 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7614 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7615 domain
= isl_basic_map_universe(dim
);
7617 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7618 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7619 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7621 total
= isl_basic_map_total_dim(bmap
);
7623 for (i
= 0; i
< n
; ++i
) {
7624 k
= isl_basic_map_alloc_equality(bmap
);
7627 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7628 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7629 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7630 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7633 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7634 return isl_basic_map_finalize(bmap
);
7636 isl_basic_map_free(bmap
);
7641 * returns [domain -> range] -> range - domain
7643 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7646 isl_space
*domain_dim
;
7651 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7652 map
->dim
, isl_dim_out
))
7653 isl_die(map
->ctx
, isl_error_invalid
,
7654 "domain and range don't match", goto error
);
7656 map
= isl_map_cow(map
);
7660 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7661 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7662 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7665 for (i
= 0; i
< map
->n
; ++i
) {
7666 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7670 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7677 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7679 struct isl_basic_map
*bmap
;
7687 nparam
= dims
->nparam
;
7689 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7693 for (i
= 0; i
< dim
; ++i
) {
7694 int j
= isl_basic_map_alloc_equality(bmap
);
7697 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7698 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7699 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7701 return isl_basic_map_finalize(bmap
);
7703 isl_basic_map_free(bmap
);
7707 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7711 if (dim
->n_in
!= dim
->n_out
)
7712 isl_die(dim
->ctx
, isl_error_invalid
,
7713 "number of input and output dimensions needs to be "
7714 "the same", goto error
);
7715 return basic_map_identity(dim
);
7717 isl_space_free(dim
);
7721 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7723 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7726 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7728 isl_space
*dim
= isl_set_get_space(set
);
7730 id
= isl_map_identity(isl_space_map_from_set(dim
));
7731 return isl_map_intersect_range(id
, set
);
7734 /* Construct a basic set with all set dimensions having only non-negative
7737 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7738 __isl_take isl_space
*space
)
7743 struct isl_basic_set
*bset
;
7747 nparam
= space
->nparam
;
7749 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7752 for (i
= 0; i
< dim
; ++i
) {
7753 int k
= isl_basic_set_alloc_inequality(bset
);
7756 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7757 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7761 isl_basic_set_free(bset
);
7765 /* Construct the half-space x_pos >= 0.
7767 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7771 isl_basic_set
*nonneg
;
7773 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7774 k
= isl_basic_set_alloc_inequality(nonneg
);
7777 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7778 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7780 return isl_basic_set_finalize(nonneg
);
7782 isl_basic_set_free(nonneg
);
7786 /* Construct the half-space x_pos <= -1.
7788 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7793 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7794 k
= isl_basic_set_alloc_inequality(neg
);
7797 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7798 isl_int_set_si(neg
->ineq
[k
][0], -1);
7799 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7801 return isl_basic_set_finalize(neg
);
7803 isl_basic_set_free(neg
);
7807 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7808 enum isl_dim_type type
, unsigned first
, unsigned n
)
7812 isl_basic_set
*nonneg
;
7820 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7822 offset
= pos(set
->dim
, type
);
7823 for (i
= 0; i
< n
; ++i
) {
7824 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7825 offset
+ first
+ i
);
7826 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7828 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7837 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7839 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7845 return isl_stat_error
;
7846 if (isl_set_plain_is_empty(set
)) {
7851 return fn(set
, signs
, user
);
7854 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7856 half
= isl_set_intersect(half
, isl_set_copy(set
));
7857 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7861 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7863 half
= isl_set_intersect(half
, set
);
7864 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7867 return isl_stat_error
;
7870 /* Call "fn" on the intersections of "set" with each of the orthants
7871 * (except for obviously empty intersections). The orthant is identified
7872 * by the signs array, with each entry having value 1 or -1 according
7873 * to the sign of the corresponding variable.
7875 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7876 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7885 return isl_stat_error
;
7886 if (isl_set_plain_is_empty(set
))
7889 nparam
= isl_set_dim(set
, isl_dim_param
);
7890 nvar
= isl_set_dim(set
, isl_dim_set
);
7892 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7894 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7902 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7904 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7907 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7908 __isl_keep isl_basic_map
*bmap2
)
7911 struct isl_map
*map1
;
7912 struct isl_map
*map2
;
7914 if (!bmap1
|| !bmap2
)
7915 return isl_bool_error
;
7917 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7918 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7920 is_subset
= isl_map_is_subset(map1
, map2
);
7928 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7929 __isl_keep isl_basic_set
*bset2
)
7931 return isl_basic_map_is_subset(bset1
, bset2
);
7934 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7935 __isl_keep isl_basic_map
*bmap2
)
7939 if (!bmap1
|| !bmap2
)
7940 return isl_bool_error
;
7941 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7942 if (is_subset
!= isl_bool_true
)
7944 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7948 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7949 __isl_keep isl_basic_set
*bset2
)
7951 return isl_basic_map_is_equal(
7952 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7955 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7961 return isl_bool_error
;
7962 for (i
= 0; i
< map
->n
; ++i
) {
7963 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7965 return isl_bool_error
;
7967 return isl_bool_false
;
7969 return isl_bool_true
;
7972 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7974 return map
? map
->n
== 0 : isl_bool_error
;
7977 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7979 return set
? set
->n
== 0 : isl_bool_error
;
7982 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7984 return isl_map_is_empty(set_to_map(set
));
7987 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
7988 __isl_keep isl_map
*map2
)
7991 return isl_bool_error
;
7993 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7996 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
7997 __isl_keep isl_set
*set2
)
8000 return isl_bool_error
;
8002 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8005 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8010 return isl_bool_error
;
8011 is_subset
= isl_map_is_subset(map1
, map2
);
8012 if (is_subset
!= isl_bool_true
)
8014 is_subset
= isl_map_is_subset(map2
, map1
);
8018 /* Is "map1" equal to "map2"?
8020 * First check if they are obviously equal.
8021 * If not, then perform a more detailed analysis.
8023 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8027 equal
= isl_map_plain_is_equal(map1
, map2
);
8028 if (equal
< 0 || equal
)
8030 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8033 isl_bool
isl_basic_map_is_strict_subset(
8034 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8038 if (!bmap1
|| !bmap2
)
8039 return isl_bool_error
;
8040 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8041 if (is_subset
!= isl_bool_true
)
8043 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8044 if (is_subset
== isl_bool_error
)
8049 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8050 __isl_keep isl_map
*map2
)
8055 return isl_bool_error
;
8056 is_subset
= isl_map_is_subset(map1
, map2
);
8057 if (is_subset
!= isl_bool_true
)
8059 is_subset
= isl_map_is_subset(map2
, map1
);
8060 if (is_subset
== isl_bool_error
)
8065 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8066 __isl_keep isl_set
*set2
)
8068 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8071 /* Is "bmap" obviously equal to the universe with the same space?
8073 * That is, does it not have any constraints?
8075 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8078 return isl_bool_error
;
8079 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8082 /* Is "bset" obviously equal to the universe with the same space?
8084 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8086 return isl_basic_map_plain_is_universe(bset
);
8089 /* If "c" does not involve any existentially quantified variables,
8090 * then set *univ to false and abort
8092 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8094 isl_bool
*univ
= user
;
8097 n
= isl_constraint_dim(c
, isl_dim_div
);
8098 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8099 isl_constraint_free(c
);
8100 if (*univ
< 0 || !*univ
)
8101 return isl_stat_error
;
8105 /* Is "bmap" equal to the universe with the same space?
8107 * First check if it is obviously equal to the universe.
8108 * If not and if there are any constraints not involving
8109 * existentially quantified variables, then it is certainly
8110 * not equal to the universe.
8111 * Otherwise, check if the universe is a subset of "bmap".
8113 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8116 isl_basic_map
*test
;
8118 univ
= isl_basic_map_plain_is_universe(bmap
);
8119 if (univ
< 0 || univ
)
8121 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8122 return isl_bool_false
;
8123 univ
= isl_bool_true
;
8124 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8126 return isl_bool_error
;
8127 if (univ
< 0 || !univ
)
8129 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8130 univ
= isl_basic_map_is_subset(test
, bmap
);
8131 isl_basic_map_free(test
);
8135 /* Is "bset" equal to the universe with the same space?
8137 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8139 return isl_basic_map_is_universe(bset
);
8142 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8147 return isl_bool_error
;
8149 for (i
= 0; i
< map
->n
; ++i
) {
8150 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8155 return isl_bool_false
;
8158 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8160 return isl_map_plain_is_universe(set_to_map(set
));
8163 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8165 struct isl_basic_set
*bset
= NULL
;
8166 struct isl_vec
*sample
= NULL
;
8167 isl_bool empty
, non_empty
;
8170 return isl_bool_error
;
8172 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8173 return isl_bool_true
;
8175 if (isl_basic_map_plain_is_universe(bmap
))
8176 return isl_bool_false
;
8178 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8179 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8180 copy
= isl_basic_map_remove_redundancies(copy
);
8181 empty
= isl_basic_map_plain_is_empty(copy
);
8182 isl_basic_map_free(copy
);
8186 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8188 return isl_bool_error
;
8190 return isl_bool_false
;
8191 isl_vec_free(bmap
->sample
);
8192 bmap
->sample
= NULL
;
8193 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8195 return isl_bool_error
;
8196 sample
= isl_basic_set_sample_vec(bset
);
8198 return isl_bool_error
;
8199 empty
= sample
->size
== 0;
8200 isl_vec_free(bmap
->sample
);
8201 bmap
->sample
= sample
;
8203 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8208 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8211 return isl_bool_error
;
8212 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8215 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8218 return isl_bool_error
;
8219 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8222 /* Is "bmap" known to be non-empty?
8224 * That is, is the cached sample still valid?
8226 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8231 return isl_bool_error
;
8233 return isl_bool_false
;
8234 total
= 1 + isl_basic_map_total_dim(bmap
);
8235 if (bmap
->sample
->size
!= total
)
8236 return isl_bool_false
;
8237 return isl_basic_map_contains(bmap
, bmap
->sample
);
8240 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8242 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8245 struct isl_map
*isl_basic_map_union(
8246 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8248 struct isl_map
*map
;
8249 if (!bmap1
|| !bmap2
)
8252 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8254 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8257 map
= isl_map_add_basic_map(map
, bmap1
);
8258 map
= isl_map_add_basic_map(map
, bmap2
);
8261 isl_basic_map_free(bmap1
);
8262 isl_basic_map_free(bmap2
);
8266 struct isl_set
*isl_basic_set_union(
8267 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8269 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8270 bset_to_bmap(bset2
)));
8273 /* Order divs such that any div only depends on previous divs */
8274 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8282 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8284 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8286 if (isl_int_is_zero(bmap
->div
[i
][0]))
8288 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8293 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8294 "integer division depends on itself",
8295 return isl_basic_map_free(bmap
));
8296 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8302 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8304 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8307 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8314 for (i
= 0; i
< map
->n
; ++i
) {
8315 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8326 /* Sort the local variables of "bset".
8328 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8329 __isl_take isl_basic_set
*bset
)
8331 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8334 /* Apply the expansion computed by isl_merge_divs.
8335 * The expansion itself is given by "exp" while the resulting
8336 * list of divs is given by "div".
8338 * Move the integer divisions of "bmap" into the right position
8339 * according to "exp" and then introduce the additional integer
8340 * divisions, adding div constraints.
8341 * The moving should be done first to avoid moving coefficients
8342 * in the definitions of the extra integer divisions.
8344 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8345 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8350 bmap
= isl_basic_map_cow(bmap
);
8354 if (div
->n_row
< bmap
->n_div
)
8355 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8356 "not an expansion", goto error
);
8358 n_div
= bmap
->n_div
;
8359 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8360 div
->n_row
- n_div
, 0,
8361 2 * (div
->n_row
- n_div
));
8363 for (i
= n_div
; i
< div
->n_row
; ++i
)
8364 if (isl_basic_map_alloc_div(bmap
) < 0)
8367 for (j
= n_div
- 1; j
>= 0; --j
) {
8370 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8373 for (i
= 0; i
< div
->n_row
; ++i
) {
8374 if (j
< n_div
&& exp
[j
] == i
) {
8377 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8378 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8380 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8388 isl_basic_map_free(bmap
);
8393 /* Apply the expansion computed by isl_merge_divs.
8394 * The expansion itself is given by "exp" while the resulting
8395 * list of divs is given by "div".
8397 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8398 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8400 return isl_basic_map_expand_divs(bset
, div
, exp
);
8403 /* Look for a div in dst that corresponds to the div "div" in src.
8404 * The divs before "div" in src and dst are assumed to be the same.
8406 * Returns -1 if no corresponding div was found and the position
8407 * of the corresponding div in dst otherwise.
8409 static int find_div(struct isl_basic_map
*dst
,
8410 struct isl_basic_map
*src
, unsigned div
)
8414 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8416 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8417 for (i
= div
; i
< dst
->n_div
; ++i
)
8418 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8419 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8420 dst
->n_div
- div
) == -1)
8425 /* Align the divs of "dst" to those of "src", adding divs from "src"
8426 * if needed. That is, make sure that the first src->n_div divs
8427 * of the result are equal to those of src.
8429 * The result is not finalized as by design it will have redundant
8430 * divs if any divs from "src" were copied.
8432 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8433 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8436 int known
, extended
;
8440 return isl_basic_map_free(dst
);
8442 if (src
->n_div
== 0)
8445 known
= isl_basic_map_divs_known(src
);
8447 return isl_basic_map_free(dst
);
8449 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8450 "some src divs are unknown",
8451 return isl_basic_map_free(dst
));
8453 src
= isl_basic_map_order_divs(src
);
8456 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8457 for (i
= 0; i
< src
->n_div
; ++i
) {
8458 int j
= find_div(dst
, src
, i
);
8461 int extra
= src
->n_div
- i
;
8462 dst
= isl_basic_map_cow(dst
);
8465 dst
= isl_basic_map_extend_space(dst
,
8466 isl_space_copy(dst
->dim
),
8467 extra
, 0, 2 * extra
);
8470 j
= isl_basic_map_alloc_div(dst
);
8472 return isl_basic_map_free(dst
);
8473 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8474 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8475 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8476 return isl_basic_map_free(dst
);
8479 isl_basic_map_swap_div(dst
, i
, j
);
8484 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8492 map
= isl_map_compute_divs(map
);
8493 map
= isl_map_cow(map
);
8497 for (i
= 1; i
< map
->n
; ++i
)
8498 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8499 for (i
= 1; i
< map
->n
; ++i
) {
8500 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8502 return isl_map_free(map
);
8505 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8509 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8511 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8514 /* Align the divs of the basic maps in "map" to those
8515 * of the basic maps in "list", as well as to the other basic maps in "map".
8516 * The elements in "list" are assumed to have known divs.
8518 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8519 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8523 map
= isl_map_compute_divs(map
);
8524 map
= isl_map_cow(map
);
8526 return isl_map_free(map
);
8530 n
= isl_basic_map_list_n_basic_map(list
);
8531 for (i
= 0; i
< n
; ++i
) {
8532 isl_basic_map
*bmap
;
8534 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8535 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8536 isl_basic_map_free(bmap
);
8539 return isl_map_free(map
);
8541 return isl_map_align_divs(map
);
8544 /* Align the divs of each element of "list" to those of "bmap".
8545 * Both "bmap" and the elements of "list" are assumed to have known divs.
8547 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8548 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8553 return isl_basic_map_list_free(list
);
8555 n
= isl_basic_map_list_n_basic_map(list
);
8556 for (i
= 0; i
< n
; ++i
) {
8557 isl_basic_map
*bmap_i
;
8559 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8560 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8561 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8567 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8568 __isl_take isl_map
*map
)
8572 ok
= isl_map_compatible_domain(map
, set
);
8576 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8577 "incompatible spaces", goto error
);
8578 map
= isl_map_intersect_domain(map
, set
);
8579 set
= isl_map_range(map
);
8587 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8588 __isl_take isl_map
*map
)
8590 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8593 /* There is no need to cow as removing empty parts doesn't change
8594 * the meaning of the set.
8596 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8603 for (i
= map
->n
- 1; i
>= 0; --i
)
8604 remove_if_empty(map
, i
);
8609 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8611 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8614 /* Given two basic sets bset1 and bset2, compute the maximal difference
8615 * between the values of dimension pos in bset1 and those in bset2
8616 * for any common value of the parameters and dimensions preceding pos.
8618 static enum isl_lp_result
basic_set_maximal_difference_at(
8619 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8620 int pos
, isl_int
*opt
)
8622 isl_basic_map
*bmap1
;
8623 isl_basic_map
*bmap2
;
8624 struct isl_ctx
*ctx
;
8625 struct isl_vec
*obj
;
8629 enum isl_lp_result res
;
8631 if (!bset1
|| !bset2
)
8632 return isl_lp_error
;
8634 nparam
= isl_basic_set_n_param(bset1
);
8635 dim1
= isl_basic_set_n_dim(bset1
);
8637 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8638 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8639 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8640 isl_dim_out
, 0, pos
);
8641 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8642 isl_dim_out
, 0, pos
);
8643 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8645 return isl_lp_error
;
8647 total
= isl_basic_map_total_dim(bmap1
);
8649 obj
= isl_vec_alloc(ctx
, 1 + total
);
8652 isl_seq_clr(obj
->block
.data
, 1 + total
);
8653 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8654 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8655 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8657 isl_basic_map_free(bmap1
);
8661 isl_basic_map_free(bmap1
);
8662 return isl_lp_error
;
8665 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8666 * for any common value of the parameters and dimensions preceding pos
8667 * in both basic sets, the values of dimension pos in bset1 are
8668 * smaller or larger than those in bset2.
8671 * 1 if bset1 follows bset2
8672 * -1 if bset1 precedes bset2
8673 * 0 if bset1 and bset2 are incomparable
8674 * -2 if some error occurred.
8676 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8677 struct isl_basic_set
*bset2
, int pos
)
8680 enum isl_lp_result res
;
8685 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8687 if (res
== isl_lp_empty
)
8689 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8690 res
== isl_lp_unbounded
)
8692 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8701 /* Given two basic sets bset1 and bset2, check whether
8702 * for any common value of the parameters and dimensions preceding pos
8703 * there is a value of dimension pos in bset1 that is larger
8704 * than a value of the same dimension in bset2.
8707 * 1 if there exists such a pair
8708 * 0 if there is no such pair, but there is a pair of equal values
8710 * -2 if some error occurred.
8712 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8713 __isl_keep isl_basic_set
*bset2
, int pos
)
8716 enum isl_lp_result res
;
8721 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8723 if (res
== isl_lp_empty
)
8725 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8726 res
== isl_lp_unbounded
)
8728 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8730 else if (res
== isl_lp_ok
)
8739 /* Given two sets set1 and set2, check whether
8740 * for any common value of the parameters and dimensions preceding pos
8741 * there is a value of dimension pos in set1 that is larger
8742 * than a value of the same dimension in set2.
8745 * 1 if there exists such a pair
8746 * 0 if there is no such pair, but there is a pair of equal values
8748 * -2 if some error occurred.
8750 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8751 __isl_keep isl_set
*set2
, int pos
)
8759 for (i
= 0; i
< set1
->n
; ++i
)
8760 for (j
= 0; j
< set2
->n
; ++j
) {
8762 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8763 if (f
== 1 || f
== -2)
8772 static isl_bool
isl_basic_map_plain_has_fixed_var(
8773 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8780 return isl_bool_error
;
8781 total
= isl_basic_map_total_dim(bmap
);
8782 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8783 for (; d
+1 > pos
; --d
)
8784 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8788 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8789 return isl_bool_false
;
8790 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8791 return isl_bool_false
;
8792 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8793 return isl_bool_false
;
8795 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8796 return isl_bool_true
;
8798 return isl_bool_false
;
8801 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8802 unsigned pos
, isl_int
*val
)
8810 return isl_bool_error
;
8812 return isl_bool_false
;
8814 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8817 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8818 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
8819 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8820 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
8821 fixed
= isl_bool_false
;
8824 isl_int_set(*val
, v
);
8830 static isl_bool
isl_basic_set_plain_has_fixed_var(
8831 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
8833 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8837 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8838 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8840 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
8841 return isl_bool_error
;
8842 return isl_basic_map_plain_has_fixed_var(bmap
,
8843 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8846 /* If "bmap" obviously lies on a hyperplane where the given dimension
8847 * has a fixed value, then return that value.
8848 * Otherwise return NaN.
8850 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8851 __isl_keep isl_basic_map
*bmap
,
8852 enum isl_dim_type type
, unsigned pos
)
8860 ctx
= isl_basic_map_get_ctx(bmap
);
8861 v
= isl_val_alloc(ctx
);
8864 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8866 return isl_val_free(v
);
8868 isl_int_set_si(v
->d
, 1);
8872 return isl_val_nan(ctx
);
8875 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8876 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8878 if (pos
>= isl_map_dim(map
, type
))
8879 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8880 "position out of bounds", return isl_bool_error
);
8881 return isl_map_plain_has_fixed_var(map
,
8882 map_offset(map
, type
) - 1 + pos
, val
);
8885 /* If "map" obviously lies on a hyperplane where the given dimension
8886 * has a fixed value, then return that value.
8887 * Otherwise return NaN.
8889 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8890 enum isl_dim_type type
, unsigned pos
)
8898 ctx
= isl_map_get_ctx(map
);
8899 v
= isl_val_alloc(ctx
);
8902 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8904 return isl_val_free(v
);
8906 isl_int_set_si(v
->d
, 1);
8910 return isl_val_nan(ctx
);
8913 /* If "set" obviously lies on a hyperplane where the given dimension
8914 * has a fixed value, then return that value.
8915 * Otherwise return NaN.
8917 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8918 enum isl_dim_type type
, unsigned pos
)
8920 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8923 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8924 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8926 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8929 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8930 * then return this fixed value in *val.
8932 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8933 unsigned dim
, isl_int
*val
)
8935 return isl_basic_set_plain_has_fixed_var(bset
,
8936 isl_basic_set_n_param(bset
) + dim
, val
);
8939 /* Return -1 if the constraint "c1" should be sorted before "c2"
8940 * and 1 if it should be sorted after "c2".
8941 * Return 0 if the two constraints are the same (up to the constant term).
8943 * In particular, if a constraint involves later variables than another
8944 * then it is sorted after this other constraint.
8945 * uset_gist depends on constraints without existentially quantified
8946 * variables sorting first.
8948 * For constraints that have the same latest variable, those
8949 * with the same coefficient for this latest variable (first in absolute value
8950 * and then in actual value) are grouped together.
8951 * This is useful for detecting pairs of constraints that can
8952 * be chained in their printed representation.
8954 * Finally, within a group, constraints are sorted according to
8955 * their coefficients (excluding the constant term).
8957 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8959 isl_int
**c1
= (isl_int
**) p1
;
8960 isl_int
**c2
= (isl_int
**) p2
;
8962 unsigned size
= *(unsigned *) arg
;
8965 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8966 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8971 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8974 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8978 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8981 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8982 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8983 * and 0 if the two constraints are the same (up to the constant term).
8985 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
8986 isl_int
*c1
, isl_int
*c2
)
8992 total
= isl_basic_map_total_dim(bmap
);
8993 return sort_constraint_cmp(&c1
, &c2
, &total
);
8996 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
8997 __isl_take isl_basic_map
*bmap
)
9003 if (bmap
->n_ineq
== 0)
9005 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9007 total
= isl_basic_map_total_dim(bmap
);
9008 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9009 &sort_constraint_cmp
, &total
) < 0)
9010 return isl_basic_map_free(bmap
);
9014 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9015 __isl_take isl_basic_set
*bset
)
9017 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9018 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9021 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9025 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9027 bmap
= isl_basic_map_remove_redundancies(bmap
);
9028 bmap
= isl_basic_map_sort_constraints(bmap
);
9030 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9033 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9034 __isl_keep isl_basic_map
*bmap2
)
9039 if (!bmap1
|| !bmap2
)
9044 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9045 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9046 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9047 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9048 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9049 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9050 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9051 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9052 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9053 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9054 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9056 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9058 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9060 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9061 return bmap1
->n_eq
- bmap2
->n_eq
;
9062 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9063 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9064 if (bmap1
->n_div
!= bmap2
->n_div
)
9065 return bmap1
->n_div
- bmap2
->n_div
;
9066 total
= isl_basic_map_total_dim(bmap1
);
9067 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9068 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9072 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9073 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9077 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9078 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9085 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9086 __isl_keep isl_basic_set
*bset2
)
9088 return isl_basic_map_plain_cmp(bset1
, bset2
);
9091 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9097 if (set1
->n
!= set2
->n
)
9098 return set1
->n
- set2
->n
;
9100 for (i
= 0; i
< set1
->n
; ++i
) {
9101 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9109 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9110 __isl_keep isl_basic_map
*bmap2
)
9112 if (!bmap1
|| !bmap2
)
9113 return isl_bool_error
;
9114 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9117 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9118 __isl_keep isl_basic_set
*bset2
)
9120 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9121 bset_to_bmap(bset2
));
9124 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9126 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9127 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9129 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9132 /* Sort the basic maps of "map" and remove duplicate basic maps.
9134 * While removing basic maps, we make sure that the basic maps remain
9135 * sorted because isl_map_normalize expects the basic maps of the result
9138 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9142 map
= isl_map_remove_empty_parts(map
);
9145 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9146 for (i
= map
->n
- 1; i
>= 1; --i
) {
9147 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9149 isl_basic_map_free(map
->p
[i
-1]);
9150 for (j
= i
; j
< map
->n
; ++j
)
9151 map
->p
[j
- 1] = map
->p
[j
];
9158 /* Remove obvious duplicates among the basic maps of "map".
9160 * Unlike isl_map_normalize, this function does not remove redundant
9161 * constraints and only removes duplicates that have exactly the same
9162 * constraints in the input. It does sort the constraints and
9163 * the basic maps to ease the detection of duplicates.
9165 * If "map" has already been normalized or if the basic maps are
9166 * disjoint, then there can be no duplicates.
9168 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9171 isl_basic_map
*bmap
;
9177 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9179 for (i
= 0; i
< map
->n
; ++i
) {
9180 bmap
= isl_basic_map_copy(map
->p
[i
]);
9181 bmap
= isl_basic_map_sort_constraints(bmap
);
9183 return isl_map_free(map
);
9184 isl_basic_map_free(map
->p
[i
]);
9188 map
= sort_and_remove_duplicates(map
);
9192 /* We normalize in place, but if anything goes wrong we need
9193 * to return NULL, so we need to make sure we don't change the
9194 * meaning of any possible other copies of map.
9196 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9199 struct isl_basic_map
*bmap
;
9203 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9205 for (i
= 0; i
< map
->n
; ++i
) {
9206 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9209 isl_basic_map_free(map
->p
[i
]);
9213 map
= sort_and_remove_duplicates(map
);
9215 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9222 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9224 return set_from_map(isl_map_normalize(set_to_map(set
)));
9227 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9228 __isl_keep isl_map
*map2
)
9234 return isl_bool_error
;
9237 return isl_bool_true
;
9238 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9239 return isl_bool_false
;
9241 map1
= isl_map_copy(map1
);
9242 map2
= isl_map_copy(map2
);
9243 map1
= isl_map_normalize(map1
);
9244 map2
= isl_map_normalize(map2
);
9247 equal
= map1
->n
== map2
->n
;
9248 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9249 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9259 return isl_bool_error
;
9262 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9263 __isl_keep isl_set
*set2
)
9265 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9268 /* Return the basic maps in "map" as a list.
9270 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9271 __isl_keep isl_map
*map
)
9275 isl_basic_map_list
*list
;
9279 ctx
= isl_map_get_ctx(map
);
9280 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9282 for (i
= 0; i
< map
->n
; ++i
) {
9283 isl_basic_map
*bmap
;
9285 bmap
= isl_basic_map_copy(map
->p
[i
]);
9286 list
= isl_basic_map_list_add(list
, bmap
);
9292 /* Return the intersection of the elements in the non-empty list "list".
9293 * All elements are assumed to live in the same space.
9295 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9296 __isl_take isl_basic_map_list
*list
)
9299 isl_basic_map
*bmap
;
9303 n
= isl_basic_map_list_n_basic_map(list
);
9305 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9306 "expecting non-empty list", goto error
);
9308 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9309 for (i
= 1; i
< n
; ++i
) {
9310 isl_basic_map
*bmap_i
;
9312 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9313 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9316 isl_basic_map_list_free(list
);
9319 isl_basic_map_list_free(list
);
9323 /* Return the intersection of the elements in the non-empty list "list".
9324 * All elements are assumed to live in the same space.
9326 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9327 __isl_take isl_basic_set_list
*list
)
9329 return isl_basic_map_list_intersect(list
);
9332 /* Return the union of the elements of "list".
9333 * The list is required to have at least one element.
9335 __isl_give isl_set
*isl_basic_set_list_union(
9336 __isl_take isl_basic_set_list
*list
)
9340 isl_basic_set
*bset
;
9345 n
= isl_basic_set_list_n_basic_set(list
);
9347 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9348 "expecting non-empty list", goto error
);
9350 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9351 space
= isl_basic_set_get_space(bset
);
9352 isl_basic_set_free(bset
);
9354 set
= isl_set_alloc_space(space
, n
, 0);
9355 for (i
= 0; i
< n
; ++i
) {
9356 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9357 set
= isl_set_add_basic_set(set
, bset
);
9360 isl_basic_set_list_free(list
);
9363 isl_basic_set_list_free(list
);
9367 /* Return the union of the elements in the non-empty list "list".
9368 * All elements are assumed to live in the same space.
9370 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9377 n
= isl_set_list_n_set(list
);
9379 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9380 "expecting non-empty list", goto error
);
9382 set
= isl_set_list_get_set(list
, 0);
9383 for (i
= 1; i
< n
; ++i
) {
9386 set_i
= isl_set_list_get_set(list
, i
);
9387 set
= isl_set_union(set
, set_i
);
9390 isl_set_list_free(list
);
9393 isl_set_list_free(list
);
9397 struct isl_basic_map
*isl_basic_map_product(
9398 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9400 isl_space
*dim_result
= NULL
;
9401 struct isl_basic_map
*bmap
;
9402 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9403 struct isl_dim_map
*dim_map1
, *dim_map2
;
9405 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9407 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9408 isl_space_copy(bmap2
->dim
));
9410 in1
= isl_basic_map_n_in(bmap1
);
9411 in2
= isl_basic_map_n_in(bmap2
);
9412 out1
= isl_basic_map_n_out(bmap1
);
9413 out2
= isl_basic_map_n_out(bmap2
);
9414 nparam
= isl_basic_map_n_param(bmap1
);
9416 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9417 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9418 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9419 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9420 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9421 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9422 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9423 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9424 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9425 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9426 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9428 bmap
= isl_basic_map_alloc_space(dim_result
,
9429 bmap1
->n_div
+ bmap2
->n_div
,
9430 bmap1
->n_eq
+ bmap2
->n_eq
,
9431 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9432 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9433 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9434 bmap
= isl_basic_map_simplify(bmap
);
9435 return isl_basic_map_finalize(bmap
);
9437 isl_basic_map_free(bmap1
);
9438 isl_basic_map_free(bmap2
);
9442 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9443 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9445 isl_basic_map
*prod
;
9447 prod
= isl_basic_map_product(bmap1
, bmap2
);
9448 prod
= isl_basic_map_flatten(prod
);
9452 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9453 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9455 return isl_basic_map_flat_range_product(bset1
, bset2
);
9458 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9459 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9461 isl_space
*space_result
= NULL
;
9462 isl_basic_map
*bmap
;
9463 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9464 struct isl_dim_map
*dim_map1
, *dim_map2
;
9466 if (!bmap1
|| !bmap2
)
9469 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9470 isl_space_copy(bmap2
->dim
));
9472 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9473 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9474 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9475 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9477 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9478 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9479 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9480 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9481 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9482 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9483 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9484 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9485 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9486 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9487 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9489 bmap
= isl_basic_map_alloc_space(space_result
,
9490 bmap1
->n_div
+ bmap2
->n_div
,
9491 bmap1
->n_eq
+ bmap2
->n_eq
,
9492 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9493 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9494 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9495 bmap
= isl_basic_map_simplify(bmap
);
9496 return isl_basic_map_finalize(bmap
);
9498 isl_basic_map_free(bmap1
);
9499 isl_basic_map_free(bmap2
);
9503 __isl_give isl_basic_map
*isl_basic_map_range_product(
9504 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9507 isl_space
*dim_result
= NULL
;
9508 isl_basic_map
*bmap
;
9509 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9510 struct isl_dim_map
*dim_map1
, *dim_map2
;
9512 rational
= isl_basic_map_is_rational(bmap1
);
9513 if (rational
>= 0 && rational
)
9514 rational
= isl_basic_map_is_rational(bmap2
);
9515 if (!bmap1
|| !bmap2
|| rational
< 0)
9518 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9521 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9522 isl_space_copy(bmap2
->dim
));
9524 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9525 out1
= isl_basic_map_n_out(bmap1
);
9526 out2
= isl_basic_map_n_out(bmap2
);
9527 nparam
= isl_basic_map_n_param(bmap1
);
9529 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9530 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9531 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9532 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9533 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9534 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9535 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9536 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9537 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9538 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9539 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9541 bmap
= isl_basic_map_alloc_space(dim_result
,
9542 bmap1
->n_div
+ bmap2
->n_div
,
9543 bmap1
->n_eq
+ bmap2
->n_eq
,
9544 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9545 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9546 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9548 bmap
= isl_basic_map_set_rational(bmap
);
9549 bmap
= isl_basic_map_simplify(bmap
);
9550 return isl_basic_map_finalize(bmap
);
9552 isl_basic_map_free(bmap1
);
9553 isl_basic_map_free(bmap2
);
9557 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9558 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9560 isl_basic_map
*prod
;
9562 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9563 prod
= isl_basic_map_flatten_range(prod
);
9567 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9568 * and collect the results.
9569 * The result live in the space obtained by calling "space_product"
9570 * on the spaces of "map1" and "map2".
9571 * If "remove_duplicates" is set then the result may contain duplicates
9572 * (even if the inputs do not) and so we try and remove the obvious
9575 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9576 __isl_take isl_map
*map2
,
9577 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9578 __isl_take isl_space
*right
),
9579 __isl_give isl_basic_map
*(*basic_map_product
)(
9580 __isl_take isl_basic_map
*left
,
9581 __isl_take isl_basic_map
*right
),
9582 int remove_duplicates
)
9585 struct isl_map
*result
;
9589 m
= isl_map_has_equal_params(map1
, map2
);
9593 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
9594 "parameters don't match", goto error
);
9596 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9597 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9598 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9600 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9601 isl_space_copy(map2
->dim
)),
9602 map1
->n
* map2
->n
, flags
);
9605 for (i
= 0; i
< map1
->n
; ++i
)
9606 for (j
= 0; j
< map2
->n
; ++j
) {
9607 struct isl_basic_map
*part
;
9608 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9609 isl_basic_map_copy(map2
->p
[j
]));
9610 if (isl_basic_map_is_empty(part
))
9611 isl_basic_map_free(part
);
9613 result
= isl_map_add_basic_map(result
, part
);
9617 if (remove_duplicates
)
9618 result
= isl_map_remove_obvious_duplicates(result
);
9628 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9630 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9631 __isl_take isl_map
*map2
)
9633 return map_product(map1
, map2
, &isl_space_product
,
9634 &isl_basic_map_product
, 0);
9637 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9638 __isl_take isl_map
*map2
)
9640 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9643 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9645 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9646 __isl_take isl_map
*map2
)
9650 prod
= isl_map_product(map1
, map2
);
9651 prod
= isl_map_flatten(prod
);
9655 /* Given two set A and B, construct its Cartesian product A x B.
9657 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9659 return isl_map_range_product(set1
, set2
);
9662 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9663 __isl_take isl_set
*set2
)
9665 return isl_map_flat_range_product(set1
, set2
);
9668 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9670 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9671 __isl_take isl_map
*map2
)
9673 return map_product(map1
, map2
, &isl_space_domain_product
,
9674 &isl_basic_map_domain_product
, 1);
9677 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9679 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9680 __isl_take isl_map
*map2
)
9682 return map_product(map1
, map2
, &isl_space_range_product
,
9683 &isl_basic_map_range_product
, 1);
9686 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9687 __isl_take isl_map
*map2
)
9689 return isl_map_align_params_map_map_and(map1
, map2
,
9690 &map_domain_product_aligned
);
9693 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9694 __isl_take isl_map
*map2
)
9696 return isl_map_align_params_map_map_and(map1
, map2
,
9697 &map_range_product_aligned
);
9700 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9702 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9705 int total1
, keep1
, total2
, keep2
;
9709 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9710 !isl_space_range_is_wrapping(map
->dim
))
9711 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9712 "not a product", return isl_map_free(map
));
9714 space
= isl_map_get_space(map
);
9715 total1
= isl_space_dim(space
, isl_dim_in
);
9716 total2
= isl_space_dim(space
, isl_dim_out
);
9717 space
= isl_space_factor_domain(space
);
9718 keep1
= isl_space_dim(space
, isl_dim_in
);
9719 keep2
= isl_space_dim(space
, isl_dim_out
);
9720 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9721 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9722 map
= isl_map_reset_space(map
, space
);
9727 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9729 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9732 int total1
, keep1
, total2
, keep2
;
9736 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9737 !isl_space_range_is_wrapping(map
->dim
))
9738 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9739 "not a product", return isl_map_free(map
));
9741 space
= isl_map_get_space(map
);
9742 total1
= isl_space_dim(space
, isl_dim_in
);
9743 total2
= isl_space_dim(space
, isl_dim_out
);
9744 space
= isl_space_factor_range(space
);
9745 keep1
= isl_space_dim(space
, isl_dim_in
);
9746 keep2
= isl_space_dim(space
, isl_dim_out
);
9747 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9748 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9749 map
= isl_map_reset_space(map
, space
);
9754 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9756 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9763 if (!isl_space_domain_is_wrapping(map
->dim
))
9764 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9765 "domain is not a product", return isl_map_free(map
));
9767 space
= isl_map_get_space(map
);
9768 total
= isl_space_dim(space
, isl_dim_in
);
9769 space
= isl_space_domain_factor_domain(space
);
9770 keep
= isl_space_dim(space
, isl_dim_in
);
9771 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9772 map
= isl_map_reset_space(map
, space
);
9777 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9779 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9786 if (!isl_space_domain_is_wrapping(map
->dim
))
9787 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9788 "domain is not a product", return isl_map_free(map
));
9790 space
= isl_map_get_space(map
);
9791 total
= isl_space_dim(space
, isl_dim_in
);
9792 space
= isl_space_domain_factor_range(space
);
9793 keep
= isl_space_dim(space
, isl_dim_in
);
9794 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9795 map
= isl_map_reset_space(map
, space
);
9800 /* Given a map A -> [B -> C], extract the map A -> B.
9802 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9809 if (!isl_space_range_is_wrapping(map
->dim
))
9810 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9811 "range is not a product", return isl_map_free(map
));
9813 space
= isl_map_get_space(map
);
9814 total
= isl_space_dim(space
, isl_dim_out
);
9815 space
= isl_space_range_factor_domain(space
);
9816 keep
= isl_space_dim(space
, isl_dim_out
);
9817 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9818 map
= isl_map_reset_space(map
, space
);
9823 /* Given a map A -> [B -> C], extract the map A -> C.
9825 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9832 if (!isl_space_range_is_wrapping(map
->dim
))
9833 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9834 "range is not a product", return isl_map_free(map
));
9836 space
= isl_map_get_space(map
);
9837 total
= isl_space_dim(space
, isl_dim_out
);
9838 space
= isl_space_range_factor_range(space
);
9839 keep
= isl_space_dim(space
, isl_dim_out
);
9840 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9841 map
= isl_map_reset_space(map
, space
);
9846 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9848 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9849 __isl_take isl_map
*map2
)
9853 prod
= isl_map_domain_product(map1
, map2
);
9854 prod
= isl_map_flatten_domain(prod
);
9858 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9860 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9861 __isl_take isl_map
*map2
)
9865 prod
= isl_map_range_product(map1
, map2
);
9866 prod
= isl_map_flatten_range(prod
);
9870 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9873 uint32_t hash
= isl_hash_init();
9878 bmap
= isl_basic_map_copy(bmap
);
9879 bmap
= isl_basic_map_normalize(bmap
);
9882 total
= isl_basic_map_total_dim(bmap
);
9883 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9884 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9886 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9887 isl_hash_hash(hash
, c_hash
);
9889 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9890 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9892 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9893 isl_hash_hash(hash
, c_hash
);
9895 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9896 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9898 if (isl_int_is_zero(bmap
->div
[i
][0]))
9900 isl_hash_byte(hash
, i
& 0xFF);
9901 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9902 isl_hash_hash(hash
, c_hash
);
9904 isl_basic_map_free(bmap
);
9908 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9910 return isl_basic_map_get_hash(bset_to_bmap(bset
));
9913 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9920 map
= isl_map_copy(map
);
9921 map
= isl_map_normalize(map
);
9925 hash
= isl_hash_init();
9926 for (i
= 0; i
< map
->n
; ++i
) {
9928 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9929 isl_hash_hash(hash
, bmap_hash
);
9937 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9939 return isl_map_get_hash(set_to_map(set
));
9942 /* Return the number of basic maps in the (current) representation of "map".
9944 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9946 return map
? map
->n
: 0;
9949 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9951 return set
? set
->n
: 0;
9954 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9955 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9960 return isl_stat_error
;
9962 for (i
= 0; i
< map
->n
; ++i
)
9963 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9964 return isl_stat_error
;
9969 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9970 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9975 return isl_stat_error
;
9977 for (i
= 0; i
< set
->n
; ++i
)
9978 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9979 return isl_stat_error
;
9984 /* Return a list of basic sets, the union of which is equal to "set".
9986 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9987 __isl_keep isl_set
*set
)
9990 isl_basic_set_list
*list
;
9995 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
9996 for (i
= 0; i
< set
->n
; ++i
) {
9997 isl_basic_set
*bset
;
9999 bset
= isl_basic_set_copy(set
->p
[i
]);
10000 list
= isl_basic_set_list_add(list
, bset
);
10006 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10013 bset
= isl_basic_set_cow(bset
);
10017 dim
= isl_basic_set_get_space(bset
);
10018 dim
= isl_space_lift(dim
, bset
->n_div
);
10021 isl_space_free(bset
->dim
);
10023 bset
->extra
-= bset
->n_div
;
10026 bset
= isl_basic_set_finalize(bset
);
10030 isl_basic_set_free(bset
);
10034 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10040 set
= isl_set_align_divs(set
);
10045 set
= isl_set_cow(set
);
10049 n_div
= set
->p
[0]->n_div
;
10050 dim
= isl_set_get_space(set
);
10051 dim
= isl_space_lift(dim
, n_div
);
10054 isl_space_free(set
->dim
);
10057 for (i
= 0; i
< set
->n
; ++i
) {
10058 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10069 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10077 dim
= isl_basic_set_total_dim(bset
);
10078 size
+= bset
->n_eq
* (1 + dim
);
10079 size
+= bset
->n_ineq
* (1 + dim
);
10080 size
+= bset
->n_div
* (2 + dim
);
10085 int isl_set_size(__isl_keep isl_set
*set
)
10093 for (i
= 0; i
< set
->n
; ++i
)
10094 size
+= isl_basic_set_size(set
->p
[i
]);
10099 /* Check if there is any lower bound (if lower == 0) and/or upper
10100 * bound (if upper == 0) on the specified dim.
10102 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10103 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10107 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10108 return isl_bool_error
;
10110 pos
+= isl_basic_map_offset(bmap
, type
);
10112 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10113 if (isl_int_is_zero(bmap
->div
[i
][0]))
10115 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10116 return isl_bool_true
;
10119 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10120 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10121 return isl_bool_true
;
10123 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10124 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10131 return lower
&& upper
;
10134 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10135 enum isl_dim_type type
, unsigned pos
)
10137 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10140 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10141 enum isl_dim_type type
, unsigned pos
)
10143 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10146 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10147 enum isl_dim_type type
, unsigned pos
)
10149 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10152 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10153 enum isl_dim_type type
, unsigned pos
)
10158 return isl_bool_error
;
10160 for (i
= 0; i
< map
->n
; ++i
) {
10162 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10163 if (bounded
< 0 || !bounded
)
10167 return isl_bool_true
;
10170 /* Return true if the specified dim is involved in both an upper bound
10171 * and a lower bound.
10173 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10174 enum isl_dim_type type
, unsigned pos
)
10176 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10179 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10181 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10182 enum isl_dim_type type
, unsigned pos
,
10183 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10184 enum isl_dim_type type
, unsigned pos
))
10189 return isl_bool_error
;
10191 for (i
= 0; i
< map
->n
; ++i
) {
10193 bounded
= fn(map
->p
[i
], type
, pos
);
10194 if (bounded
< 0 || bounded
)
10198 return isl_bool_false
;
10201 /* Return 1 if the specified dim is involved in any lower bound.
10203 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10204 enum isl_dim_type type
, unsigned pos
)
10206 return has_any_bound(set
, type
, pos
,
10207 &isl_basic_map_dim_has_lower_bound
);
10210 /* Return 1 if the specified dim is involved in any upper bound.
10212 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10213 enum isl_dim_type type
, unsigned pos
)
10215 return has_any_bound(set
, type
, pos
,
10216 &isl_basic_map_dim_has_upper_bound
);
10219 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10221 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10222 enum isl_dim_type type
, unsigned pos
,
10223 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10224 enum isl_dim_type type
, unsigned pos
))
10229 return isl_bool_error
;
10231 for (i
= 0; i
< map
->n
; ++i
) {
10233 bounded
= fn(map
->p
[i
], type
, pos
);
10234 if (bounded
< 0 || !bounded
)
10238 return isl_bool_true
;
10241 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10243 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10244 enum isl_dim_type type
, unsigned pos
)
10246 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10249 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10251 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10252 enum isl_dim_type type
, unsigned pos
)
10254 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10257 /* For each of the "n" variables starting at "first", determine
10258 * the sign of the variable and put the results in the first "n"
10259 * elements of the array "signs".
10261 * 1 means that the variable is non-negative
10262 * -1 means that the variable is non-positive
10263 * 0 means the variable attains both positive and negative values.
10265 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10266 unsigned first
, unsigned n
, int *signs
)
10268 isl_vec
*bound
= NULL
;
10269 struct isl_tab
*tab
= NULL
;
10270 struct isl_tab_undo
*snap
;
10273 if (!bset
|| !signs
)
10274 return isl_stat_error
;
10276 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10277 tab
= isl_tab_from_basic_set(bset
, 0);
10278 if (!bound
|| !tab
)
10281 isl_seq_clr(bound
->el
, bound
->size
);
10282 isl_int_set_si(bound
->el
[0], -1);
10284 snap
= isl_tab_snap(tab
);
10285 for (i
= 0; i
< n
; ++i
) {
10288 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10289 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10291 empty
= tab
->empty
;
10292 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10293 if (isl_tab_rollback(tab
, snap
) < 0)
10301 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10302 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10304 empty
= tab
->empty
;
10305 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10306 if (isl_tab_rollback(tab
, snap
) < 0)
10309 signs
[i
] = empty
? -1 : 0;
10313 isl_vec_free(bound
);
10314 return isl_stat_ok
;
10317 isl_vec_free(bound
);
10318 return isl_stat_error
;
10321 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10322 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10324 if (!bset
|| !signs
)
10325 return isl_stat_error
;
10326 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10327 return isl_stat_error
);
10329 first
+= pos(bset
->dim
, type
) - 1;
10330 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10333 /* Is it possible for the integer division "div" to depend (possibly
10334 * indirectly) on any output dimensions?
10336 * If the div is undefined, then we conservatively assume that it
10337 * may depend on them.
10338 * Otherwise, we check if it actually depends on them or on any integer
10339 * divisions that may depend on them.
10341 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10344 unsigned n_out
, o_out
;
10345 unsigned n_div
, o_div
;
10347 if (isl_int_is_zero(bmap
->div
[div
][0]))
10348 return isl_bool_true
;
10350 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10351 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10353 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10354 return isl_bool_true
;
10356 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10357 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10359 for (i
= 0; i
< n_div
; ++i
) {
10360 isl_bool may_involve
;
10362 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10364 may_involve
= div_may_involve_output(bmap
, i
);
10365 if (may_involve
< 0 || may_involve
)
10366 return may_involve
;
10369 return isl_bool_false
;
10372 /* Return the first integer division of "bmap" in the range
10373 * [first, first + n[ that may depend on any output dimensions and
10374 * that has a non-zero coefficient in "c" (where the first coefficient
10375 * in "c" corresponds to integer division "first").
10377 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10378 isl_int
*c
, int first
, int n
)
10385 for (k
= first
; k
< first
+ n
; ++k
) {
10386 isl_bool may_involve
;
10388 if (isl_int_is_zero(c
[k
]))
10390 may_involve
= div_may_involve_output(bmap
, k
);
10391 if (may_involve
< 0)
10400 /* Look for a pair of inequality constraints in "bmap" of the form
10402 * -l + i >= 0 or i >= l
10404 * n + l - i >= 0 or i <= l + n
10406 * with n < "m" and i the output dimension at position "pos".
10407 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10408 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10409 * and earlier output dimensions, as well as integer divisions that do
10410 * not involve any of the output dimensions.
10412 * Return the index of the first inequality constraint or bmap->n_ineq
10413 * if no such pair can be found.
10415 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10416 int pos
, isl_int m
)
10421 unsigned n_div
, o_div
;
10422 unsigned n_out
, o_out
;
10428 ctx
= isl_basic_map_get_ctx(bmap
);
10429 total
= isl_basic_map_total_dim(bmap
);
10430 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10431 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10432 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10433 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10434 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10435 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10437 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10438 n_out
- (pos
+ 1)) != -1)
10440 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10443 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10444 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10447 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10448 bmap
->ineq
[j
] + 1, total
))
10452 if (j
>= bmap
->n_ineq
)
10454 isl_int_add(bmap
->ineq
[i
][0],
10455 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10456 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10457 isl_int_sub(bmap
->ineq
[i
][0],
10458 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10461 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10467 return bmap
->n_ineq
;
10470 /* Return the index of the equality of "bmap" that defines
10471 * the output dimension "pos" in terms of earlier dimensions.
10472 * The equality may also involve integer divisions, as long
10473 * as those integer divisions are defined in terms of
10474 * parameters or input dimensions.
10475 * In this case, *div is set to the number of integer divisions and
10476 * *ineq is set to the number of inequality constraints (provided
10477 * div and ineq are not NULL).
10479 * The equality may also involve a single integer division involving
10480 * the output dimensions (typically only output dimension "pos") as
10481 * long as the coefficient of output dimension "pos" is 1 or -1 and
10482 * there is a pair of constraints i >= l and i <= l + n, with i referring
10483 * to output dimension "pos", l an expression involving only earlier
10484 * dimensions and n smaller than the coefficient of the integer division
10485 * in the equality. In this case, the output dimension can be defined
10486 * in terms of a modulo expression that does not involve the integer division.
10487 * *div is then set to this single integer division and
10488 * *ineq is set to the index of constraint i >= l.
10490 * Return bmap->n_eq if there is no such equality.
10491 * Return -1 on error.
10493 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10494 int pos
, int *div
, int *ineq
)
10497 unsigned n_out
, o_out
;
10498 unsigned n_div
, o_div
;
10503 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10504 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10505 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10506 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10509 *ineq
= bmap
->n_ineq
;
10512 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10513 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10515 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10516 n_out
- (pos
+ 1)) != -1)
10518 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10522 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10523 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10525 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10526 k
+ 1, n_div
- (k
+1)) < n_div
)
10528 l
= find_modulo_constraint_pair(bmap
, pos
,
10529 bmap
->eq
[j
][o_div
+ k
]);
10532 if (l
>= bmap
->n_ineq
)
10544 /* Check if the given basic map is obviously single-valued.
10545 * In particular, for each output dimension, check that there is
10546 * an equality that defines the output dimension in terms of
10547 * earlier dimensions.
10549 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10555 return isl_bool_error
;
10557 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10559 for (i
= 0; i
< n_out
; ++i
) {
10562 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10565 return isl_bool_error
;
10566 if (eq
>= bmap
->n_eq
)
10567 return isl_bool_false
;
10570 return isl_bool_true
;
10573 /* Check if the given basic map is single-valued.
10574 * We simply compute
10578 * and check if the result is a subset of the identity mapping.
10580 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10583 isl_basic_map
*test
;
10587 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10591 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10592 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10594 space
= isl_basic_map_get_space(bmap
);
10595 space
= isl_space_map_from_set(isl_space_range(space
));
10596 id
= isl_basic_map_identity(space
);
10598 sv
= isl_basic_map_is_subset(test
, id
);
10600 isl_basic_map_free(test
);
10601 isl_basic_map_free(id
);
10606 /* Check if the given map is obviously single-valued.
10608 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10611 return isl_bool_error
;
10613 return isl_bool_true
;
10615 return isl_bool_false
;
10617 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10620 /* Check if the given map is single-valued.
10621 * We simply compute
10625 * and check if the result is a subset of the identity mapping.
10627 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10634 sv
= isl_map_plain_is_single_valued(map
);
10638 test
= isl_map_reverse(isl_map_copy(map
));
10639 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10641 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10642 id
= isl_map_identity(dim
);
10644 sv
= isl_map_is_subset(test
, id
);
10646 isl_map_free(test
);
10652 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10656 map
= isl_map_copy(map
);
10657 map
= isl_map_reverse(map
);
10658 in
= isl_map_is_single_valued(map
);
10664 /* Check if the given map is obviously injective.
10666 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10670 map
= isl_map_copy(map
);
10671 map
= isl_map_reverse(map
);
10672 in
= isl_map_plain_is_single_valued(map
);
10678 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10682 sv
= isl_map_is_single_valued(map
);
10686 return isl_map_is_injective(map
);
10689 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10691 return isl_map_is_single_valued(set_to_map(set
));
10694 /* Does "map" only map elements to themselves?
10696 * If the domain and range spaces are different, then "map"
10697 * is considered not to be an identity relation, even if it is empty.
10698 * Otherwise, construct the maximal identity relation and
10699 * check whether "map" is a subset of this relation.
10701 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10705 isl_bool equal
, is_identity
;
10707 space
= isl_map_get_space(map
);
10708 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10709 isl_space_free(space
);
10710 if (equal
< 0 || !equal
)
10713 id
= isl_map_identity(isl_map_get_space(map
));
10714 is_identity
= isl_map_is_subset(map
, id
);
10717 return is_identity
;
10720 int isl_map_is_translation(__isl_keep isl_map
*map
)
10725 delta
= isl_map_deltas(isl_map_copy(map
));
10726 ok
= isl_set_is_singleton(delta
);
10727 isl_set_free(delta
);
10732 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10734 if (isl_seq_first_non_zero(p
, pos
) != -1)
10736 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10741 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10748 return isl_bool_error
;
10750 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10751 return isl_bool_false
;
10753 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10754 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10755 for (j
= 0; j
< nvar
; ++j
) {
10756 int lower
= 0, upper
= 0;
10757 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10758 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10760 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10761 return isl_bool_false
;
10764 if (i
< bset
->n_eq
)
10766 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10767 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10769 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10770 return isl_bool_false
;
10771 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10776 if (!lower
|| !upper
)
10777 return isl_bool_false
;
10780 return isl_bool_true
;
10783 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
10786 return isl_bool_error
;
10788 return isl_bool_false
;
10790 return isl_basic_set_is_box(set
->p
[0]);
10793 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10796 return isl_bool_error
;
10798 return isl_space_is_wrapping(bset
->dim
);
10801 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10804 return isl_bool_error
;
10806 return isl_space_is_wrapping(set
->dim
);
10809 /* Modify the space of "map" through a call to "change".
10810 * If "can_change" is set (not NULL), then first call it to check
10811 * if the modification is allowed, printing the error message "cannot_change"
10814 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10815 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10816 const char *cannot_change
,
10817 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10825 ok
= can_change
? can_change(map
) : isl_bool_true
;
10827 return isl_map_free(map
);
10829 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10830 return isl_map_free(map
));
10832 space
= change(isl_map_get_space(map
));
10833 map
= isl_map_reset_space(map
, space
);
10838 /* Is the domain of "map" a wrapped relation?
10840 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10843 return isl_bool_error
;
10845 return isl_space_domain_is_wrapping(map
->dim
);
10848 /* Is the range of "map" a wrapped relation?
10850 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10853 return isl_bool_error
;
10855 return isl_space_range_is_wrapping(map
->dim
);
10858 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10860 bmap
= isl_basic_map_cow(bmap
);
10864 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10868 bmap
= isl_basic_map_finalize(bmap
);
10870 return bset_from_bmap(bmap
);
10872 isl_basic_map_free(bmap
);
10876 /* Given a map A -> B, return the set (A -> B).
10878 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10880 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
10883 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10885 bset
= isl_basic_set_cow(bset
);
10889 bset
->dim
= isl_space_unwrap(bset
->dim
);
10893 bset
= isl_basic_set_finalize(bset
);
10895 return bset_to_bmap(bset
);
10897 isl_basic_set_free(bset
);
10901 /* Given a set (A -> B), return the map A -> B.
10902 * Error out if "set" is not of the form (A -> B).
10904 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10906 return isl_map_change_space(set
, &isl_set_is_wrapping
,
10907 "not a wrapping set", &isl_space_unwrap
);
10910 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10911 enum isl_dim_type type
)
10916 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10919 bmap
= isl_basic_map_cow(bmap
);
10923 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10927 bmap
= isl_basic_map_finalize(bmap
);
10931 isl_basic_map_free(bmap
);
10935 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10936 enum isl_dim_type type
)
10943 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10946 map
= isl_map_cow(map
);
10950 for (i
= 0; i
< map
->n
; ++i
) {
10951 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10955 map
->dim
= isl_space_reset(map
->dim
, type
);
10965 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10970 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10973 bmap
= isl_basic_map_cow(bmap
);
10977 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10981 bmap
= isl_basic_map_finalize(bmap
);
10985 isl_basic_map_free(bmap
);
10989 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10991 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
10994 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10995 __isl_take isl_basic_map
*bmap
)
11000 if (!bmap
->dim
->nested
[0])
11003 bmap
= isl_basic_map_cow(bmap
);
11007 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11011 bmap
= isl_basic_map_finalize(bmap
);
11015 isl_basic_map_free(bmap
);
11019 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11020 __isl_take isl_basic_map
*bmap
)
11025 if (!bmap
->dim
->nested
[1])
11028 bmap
= isl_basic_map_cow(bmap
);
11032 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11036 bmap
= isl_basic_map_finalize(bmap
);
11040 isl_basic_map_free(bmap
);
11044 /* Remove any internal structure from the spaces of domain and range of "map".
11046 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11051 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11054 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11057 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11059 return set_from_map(isl_map_flatten(set_to_map(set
)));
11062 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11064 isl_space
*dim
, *flat_dim
;
11067 dim
= isl_set_get_space(set
);
11068 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11069 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11070 map
= isl_map_intersect_domain(map
, set
);
11075 /* Remove any internal structure from the space of the domain of "map".
11077 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11082 if (!map
->dim
->nested
[0])
11085 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11088 /* Remove any internal structure from the space of the range of "map".
11090 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11095 if (!map
->dim
->nested
[1])
11098 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11101 /* Reorder the dimensions of "bmap" according to the given dim_map
11102 * and set the dimension specification to "dim" and
11103 * perform Gaussian elimination on the result.
11105 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11106 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11108 isl_basic_map
*res
;
11111 bmap
= isl_basic_map_cow(bmap
);
11112 if (!bmap
|| !dim
|| !dim_map
)
11115 flags
= bmap
->flags
;
11116 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11117 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11118 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11119 res
= isl_basic_map_alloc_space(dim
,
11120 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11121 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11123 res
->flags
= flags
;
11124 res
= isl_basic_map_gauss(res
, NULL
);
11125 res
= isl_basic_map_finalize(res
);
11129 isl_basic_map_free(bmap
);
11130 isl_space_free(dim
);
11134 /* Reorder the dimensions of "map" according to given reordering.
11136 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11137 __isl_take isl_reordering
*r
)
11140 struct isl_dim_map
*dim_map
;
11142 map
= isl_map_cow(map
);
11143 dim_map
= isl_dim_map_from_reordering(r
);
11144 if (!map
|| !r
|| !dim_map
)
11147 for (i
= 0; i
< map
->n
; ++i
) {
11148 struct isl_dim_map
*dim_map_i
;
11150 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11152 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11153 isl_space_copy(r
->dim
), dim_map_i
);
11159 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11161 isl_reordering_free(r
);
11167 isl_reordering_free(r
);
11171 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11172 __isl_take isl_reordering
*r
)
11174 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11177 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11178 __isl_take isl_space
*model
)
11183 if (!map
|| !model
)
11186 ctx
= isl_space_get_ctx(model
);
11187 if (!isl_space_has_named_params(model
))
11188 isl_die(ctx
, isl_error_invalid
,
11189 "model has unnamed parameters", goto error
);
11190 if (isl_map_check_named_params(map
) < 0)
11192 aligned
= isl_map_space_has_equal_params(map
, model
);
11196 isl_reordering
*exp
;
11198 model
= isl_space_drop_dims(model
, isl_dim_in
,
11199 0, isl_space_dim(model
, isl_dim_in
));
11200 model
= isl_space_drop_dims(model
, isl_dim_out
,
11201 0, isl_space_dim(model
, isl_dim_out
));
11202 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11203 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11204 map
= isl_map_realign(map
, exp
);
11207 isl_space_free(model
);
11210 isl_space_free(model
);
11215 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11216 __isl_take isl_space
*model
)
11218 return isl_map_align_params(set
, model
);
11221 /* Align the parameters of "bmap" to those of "model", introducing
11222 * additional parameters if needed.
11224 __isl_give isl_basic_map
*isl_basic_map_align_params(
11225 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11229 if (!bmap
|| !model
)
11232 ctx
= isl_space_get_ctx(model
);
11233 if (!isl_space_has_named_params(model
))
11234 isl_die(ctx
, isl_error_invalid
,
11235 "model has unnamed parameters", goto error
);
11236 if (!isl_space_has_named_params(bmap
->dim
))
11237 isl_die(ctx
, isl_error_invalid
,
11238 "relation has unnamed parameters", goto error
);
11239 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11240 isl_reordering
*exp
;
11241 struct isl_dim_map
*dim_map
;
11243 model
= isl_space_drop_dims(model
, isl_dim_in
,
11244 0, isl_space_dim(model
, isl_dim_in
));
11245 model
= isl_space_drop_dims(model
, isl_dim_out
,
11246 0, isl_space_dim(model
, isl_dim_out
));
11247 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11248 exp
= isl_reordering_extend_space(exp
,
11249 isl_basic_map_get_space(bmap
));
11250 dim_map
= isl_dim_map_from_reordering(exp
);
11251 bmap
= isl_basic_map_realign(bmap
,
11252 exp
? isl_space_copy(exp
->dim
) : NULL
,
11253 isl_dim_map_extend(dim_map
, bmap
));
11254 isl_reordering_free(exp
);
11258 isl_space_free(model
);
11261 isl_space_free(model
);
11262 isl_basic_map_free(bmap
);
11266 /* Do "bset" and "space" have the same parameters?
11268 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11269 __isl_keep isl_space
*space
)
11271 isl_space
*bset_space
;
11273 bset_space
= isl_basic_set_peek_space(bset
);
11274 return isl_space_match(bset_space
, isl_dim_param
, space
, isl_dim_param
);
11277 /* Do "map" and "space" have the same parameters?
11279 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11280 __isl_keep isl_space
*space
)
11282 isl_space
*map_space
;
11284 map_space
= isl_map_peek_space(map
);
11285 return isl_space_match(map_space
, isl_dim_param
, space
, isl_dim_param
);
11288 /* Do "set" and "space" have the same parameters?
11290 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11291 __isl_keep isl_space
*space
)
11293 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11296 /* Align the parameters of "bset" to those of "model", introducing
11297 * additional parameters if needed.
11299 __isl_give isl_basic_set
*isl_basic_set_align_params(
11300 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11302 return isl_basic_map_align_params(bset
, model
);
11305 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11306 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11307 enum isl_dim_type c2
, enum isl_dim_type c3
,
11308 enum isl_dim_type c4
, enum isl_dim_type c5
)
11310 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11311 struct isl_mat
*mat
;
11317 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11318 isl_basic_map_total_dim(bmap
) + 1);
11321 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11322 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11323 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11324 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11325 isl_int_set(mat
->row
[i
][pos
],
11326 bmap
->eq
[i
][off
+ k
]);
11334 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11335 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11336 enum isl_dim_type c2
, enum isl_dim_type c3
,
11337 enum isl_dim_type c4
, enum isl_dim_type c5
)
11339 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11340 struct isl_mat
*mat
;
11346 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11347 isl_basic_map_total_dim(bmap
) + 1);
11350 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11351 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11352 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11353 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11354 isl_int_set(mat
->row
[i
][pos
],
11355 bmap
->ineq
[i
][off
+ k
]);
11363 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11364 __isl_take isl_space
*dim
,
11365 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11366 enum isl_dim_type c2
, enum isl_dim_type c3
,
11367 enum isl_dim_type c4
, enum isl_dim_type c5
)
11369 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11370 isl_basic_map
*bmap
;
11376 if (!dim
|| !eq
|| !ineq
)
11379 if (eq
->n_col
!= ineq
->n_col
)
11380 isl_die(dim
->ctx
, isl_error_invalid
,
11381 "equalities and inequalities matrices should have "
11382 "same number of columns", goto error
);
11384 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11386 if (eq
->n_col
< total
)
11387 isl_die(dim
->ctx
, isl_error_invalid
,
11388 "number of columns too small", goto error
);
11390 extra
= eq
->n_col
- total
;
11392 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11393 eq
->n_row
, ineq
->n_row
);
11396 for (i
= 0; i
< extra
; ++i
) {
11397 k
= isl_basic_map_alloc_div(bmap
);
11400 isl_int_set_si(bmap
->div
[k
][0], 0);
11402 for (i
= 0; i
< eq
->n_row
; ++i
) {
11403 l
= isl_basic_map_alloc_equality(bmap
);
11406 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11407 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11408 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11409 isl_int_set(bmap
->eq
[l
][off
+ k
],
11415 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11416 l
= isl_basic_map_alloc_inequality(bmap
);
11419 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11420 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11421 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11422 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11423 ineq
->row
[i
][pos
]);
11429 isl_space_free(dim
);
11431 isl_mat_free(ineq
);
11433 bmap
= isl_basic_map_simplify(bmap
);
11434 return isl_basic_map_finalize(bmap
);
11436 isl_space_free(dim
);
11438 isl_mat_free(ineq
);
11442 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11443 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11444 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11446 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11447 c1
, c2
, c3
, c4
, isl_dim_in
);
11450 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11451 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11452 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11454 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11455 c1
, c2
, c3
, c4
, isl_dim_in
);
11458 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11459 __isl_take isl_space
*dim
,
11460 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11461 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11463 isl_basic_map
*bmap
;
11464 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11465 c1
, c2
, c3
, c4
, isl_dim_in
);
11466 return bset_from_bmap(bmap
);
11469 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11472 return isl_bool_error
;
11474 return isl_space_can_zip(bmap
->dim
);
11477 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11480 return isl_bool_error
;
11482 return isl_space_can_zip(map
->dim
);
11485 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11486 * (A -> C) -> (B -> D).
11488 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11497 if (!isl_basic_map_can_zip(bmap
))
11498 isl_die(bmap
->ctx
, isl_error_invalid
,
11499 "basic map cannot be zipped", goto error
);
11500 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11501 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11502 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11503 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11504 bmap
= isl_basic_map_cow(bmap
);
11505 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11508 bmap
->dim
= isl_space_zip(bmap
->dim
);
11511 bmap
= isl_basic_map_mark_final(bmap
);
11514 isl_basic_map_free(bmap
);
11518 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11519 * (A -> C) -> (B -> D).
11521 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11528 if (!isl_map_can_zip(map
))
11529 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11532 map
= isl_map_cow(map
);
11536 for (i
= 0; i
< map
->n
; ++i
) {
11537 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11542 map
->dim
= isl_space_zip(map
->dim
);
11552 /* Can we apply isl_basic_map_curry to "bmap"?
11553 * That is, does it have a nested relation in its domain?
11555 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11558 return isl_bool_error
;
11560 return isl_space_can_curry(bmap
->dim
);
11563 /* Can we apply isl_map_curry to "map"?
11564 * That is, does it have a nested relation in its domain?
11566 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11569 return isl_bool_error
;
11571 return isl_space_can_curry(map
->dim
);
11574 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11577 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11583 if (!isl_basic_map_can_curry(bmap
))
11584 isl_die(bmap
->ctx
, isl_error_invalid
,
11585 "basic map cannot be curried", goto error
);
11586 bmap
= isl_basic_map_cow(bmap
);
11589 bmap
->dim
= isl_space_curry(bmap
->dim
);
11592 bmap
= isl_basic_map_mark_final(bmap
);
11595 isl_basic_map_free(bmap
);
11599 /* Given a map (A -> B) -> C, return the corresponding map
11602 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11604 return isl_map_change_space(map
, &isl_map_can_curry
,
11605 "map cannot be curried", &isl_space_curry
);
11608 /* Can isl_map_range_curry be applied to "map"?
11609 * That is, does it have a nested relation in its range,
11610 * the domain of which is itself a nested relation?
11612 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11615 return isl_bool_error
;
11617 return isl_space_can_range_curry(map
->dim
);
11620 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11621 * A -> (B -> (C -> D)).
11623 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11625 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11626 "map range cannot be curried",
11627 &isl_space_range_curry
);
11630 /* Can we apply isl_basic_map_uncurry to "bmap"?
11631 * That is, does it have a nested relation in its domain?
11633 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11636 return isl_bool_error
;
11638 return isl_space_can_uncurry(bmap
->dim
);
11641 /* Can we apply isl_map_uncurry to "map"?
11642 * That is, does it have a nested relation in its domain?
11644 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11647 return isl_bool_error
;
11649 return isl_space_can_uncurry(map
->dim
);
11652 /* Given a basic map A -> (B -> C), return the corresponding basic map
11655 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11661 if (!isl_basic_map_can_uncurry(bmap
))
11662 isl_die(bmap
->ctx
, isl_error_invalid
,
11663 "basic map cannot be uncurried",
11664 return isl_basic_map_free(bmap
));
11665 bmap
= isl_basic_map_cow(bmap
);
11668 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11670 return isl_basic_map_free(bmap
);
11671 bmap
= isl_basic_map_mark_final(bmap
);
11675 /* Given a map A -> (B -> C), return the corresponding map
11678 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11680 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11681 "map cannot be uncurried", &isl_space_uncurry
);
11684 /* Construct a basic map mapping the domain of the affine expression
11685 * to a one-dimensional range prescribed by the affine expression.
11686 * If "rational" is set, then construct a rational basic map.
11688 * A NaN affine expression cannot be converted to a basic map.
11690 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11691 __isl_take isl_aff
*aff
, int rational
)
11696 isl_local_space
*ls
;
11697 isl_basic_map
*bmap
= NULL
;
11701 is_nan
= isl_aff_is_nan(aff
);
11705 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11706 "cannot convert NaN", goto error
);
11708 ls
= isl_aff_get_local_space(aff
);
11709 bmap
= isl_basic_map_from_local_space(ls
);
11710 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11711 k
= isl_basic_map_alloc_equality(bmap
);
11715 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11716 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11717 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11718 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11719 aff
->v
->size
- (pos
+ 1));
11723 bmap
= isl_basic_map_set_rational(bmap
);
11724 bmap
= isl_basic_map_finalize(bmap
);
11728 isl_basic_map_free(bmap
);
11732 /* Construct a basic map mapping the domain of the affine expression
11733 * to a one-dimensional range prescribed by the affine expression.
11735 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11737 return isl_basic_map_from_aff2(aff
, 0);
11740 /* Construct a map mapping the domain of the affine expression
11741 * to a one-dimensional range prescribed by the affine expression.
11743 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11745 isl_basic_map
*bmap
;
11747 bmap
= isl_basic_map_from_aff(aff
);
11748 return isl_map_from_basic_map(bmap
);
11751 /* Construct a basic map mapping the domain the multi-affine expression
11752 * to its range, with each dimension in the range equated to the
11753 * corresponding affine expression.
11754 * If "rational" is set, then construct a rational basic map.
11756 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11757 __isl_take isl_multi_aff
*maff
, int rational
)
11761 isl_basic_map
*bmap
;
11766 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11767 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11768 "invalid space", goto error
);
11770 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11771 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11773 bmap
= isl_basic_map_set_rational(bmap
);
11775 for (i
= 0; i
< maff
->n
; ++i
) {
11777 isl_basic_map
*bmap_i
;
11779 aff
= isl_aff_copy(maff
->p
[i
]);
11780 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11782 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11785 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11787 isl_multi_aff_free(maff
);
11790 isl_multi_aff_free(maff
);
11794 /* Construct a basic map mapping the domain the multi-affine expression
11795 * to its range, with each dimension in the range equated to the
11796 * corresponding affine expression.
11798 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11799 __isl_take isl_multi_aff
*ma
)
11801 return isl_basic_map_from_multi_aff2(ma
, 0);
11804 /* Construct a map mapping the domain the multi-affine expression
11805 * to its range, with each dimension in the range equated to the
11806 * corresponding affine expression.
11808 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11810 isl_basic_map
*bmap
;
11812 bmap
= isl_basic_map_from_multi_aff(maff
);
11813 return isl_map_from_basic_map(bmap
);
11816 /* Construct a basic map mapping a domain in the given space to
11817 * to an n-dimensional range, with n the number of elements in the list,
11818 * where each coordinate in the range is prescribed by the
11819 * corresponding affine expression.
11820 * The domains of all affine expressions in the list are assumed to match
11823 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11824 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11828 isl_basic_map
*bmap
;
11833 dim
= isl_space_from_domain(domain_dim
);
11834 bmap
= isl_basic_map_universe(dim
);
11836 for (i
= 0; i
< list
->n
; ++i
) {
11838 isl_basic_map
*bmap_i
;
11840 aff
= isl_aff_copy(list
->p
[i
]);
11841 bmap_i
= isl_basic_map_from_aff(aff
);
11843 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11846 isl_aff_list_free(list
);
11850 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11851 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11853 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11856 /* Construct a basic map where the given dimensions are equal to each other.
11858 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11859 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11861 isl_basic_map
*bmap
= NULL
;
11867 if (pos1
>= isl_space_dim(space
, type1
))
11868 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11869 "index out of bounds", goto error
);
11870 if (pos2
>= isl_space_dim(space
, type2
))
11871 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11872 "index out of bounds", goto error
);
11874 if (type1
== type2
&& pos1
== pos2
)
11875 return isl_basic_map_universe(space
);
11877 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11878 i
= isl_basic_map_alloc_equality(bmap
);
11881 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11882 pos1
+= isl_basic_map_offset(bmap
, type1
);
11883 pos2
+= isl_basic_map_offset(bmap
, type2
);
11884 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11885 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11886 bmap
= isl_basic_map_finalize(bmap
);
11887 isl_space_free(space
);
11890 isl_space_free(space
);
11891 isl_basic_map_free(bmap
);
11895 /* Add a constraint imposing that the given two dimensions are equal.
11897 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11898 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11902 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11904 bmap
= isl_basic_map_intersect(bmap
, eq
);
11909 /* Add a constraint imposing that the given two dimensions are equal.
11911 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11912 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11914 isl_basic_map
*bmap
;
11916 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11918 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11923 /* Add a constraint imposing that the given two dimensions have opposite values.
11925 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11926 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11928 isl_basic_map
*bmap
= NULL
;
11934 if (pos1
>= isl_map_dim(map
, type1
))
11935 isl_die(map
->ctx
, isl_error_invalid
,
11936 "index out of bounds", goto error
);
11937 if (pos2
>= isl_map_dim(map
, type2
))
11938 isl_die(map
->ctx
, isl_error_invalid
,
11939 "index out of bounds", goto error
);
11941 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11942 i
= isl_basic_map_alloc_equality(bmap
);
11945 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11946 pos1
+= isl_basic_map_offset(bmap
, type1
);
11947 pos2
+= isl_basic_map_offset(bmap
, type2
);
11948 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11949 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11950 bmap
= isl_basic_map_finalize(bmap
);
11952 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11956 isl_basic_map_free(bmap
);
11961 /* Construct a constraint imposing that the value of the first dimension is
11962 * greater than or equal to that of the second.
11964 static __isl_give isl_constraint
*constraint_order_ge(
11965 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11966 enum isl_dim_type type2
, int pos2
)
11973 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
11975 if (pos1
>= isl_constraint_dim(c
, type1
))
11976 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11977 "index out of bounds", return isl_constraint_free(c
));
11978 if (pos2
>= isl_constraint_dim(c
, type2
))
11979 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11980 "index out of bounds", return isl_constraint_free(c
));
11982 if (type1
== type2
&& pos1
== pos2
)
11985 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11986 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11991 /* Add a constraint imposing that the value of the first dimension is
11992 * greater than or equal to that of the second.
11994 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11995 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12000 if (type1
== type2
&& pos1
== pos2
)
12002 space
= isl_basic_map_get_space(bmap
);
12003 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12004 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12009 /* Add a constraint imposing that the value of the first dimension is
12010 * greater than or equal to that of the second.
12012 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12013 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12018 if (type1
== type2
&& pos1
== pos2
)
12020 space
= isl_map_get_space(map
);
12021 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12022 map
= isl_map_add_constraint(map
, c
);
12027 /* Add a constraint imposing that the value of the first dimension is
12028 * less than or equal to that of the second.
12030 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12031 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12033 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12036 /* Construct a basic map where the value of the first dimension is
12037 * greater than that of the second.
12039 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12040 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12042 isl_basic_map
*bmap
= NULL
;
12048 if (pos1
>= isl_space_dim(space
, type1
))
12049 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12050 "index out of bounds", goto error
);
12051 if (pos2
>= isl_space_dim(space
, type2
))
12052 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12053 "index out of bounds", goto error
);
12055 if (type1
== type2
&& pos1
== pos2
)
12056 return isl_basic_map_empty(space
);
12058 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12059 i
= isl_basic_map_alloc_inequality(bmap
);
12061 return isl_basic_map_free(bmap
);
12062 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12063 pos1
+= isl_basic_map_offset(bmap
, type1
);
12064 pos2
+= isl_basic_map_offset(bmap
, type2
);
12065 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12066 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12067 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12068 bmap
= isl_basic_map_finalize(bmap
);
12072 isl_space_free(space
);
12073 isl_basic_map_free(bmap
);
12077 /* Add a constraint imposing that the value of the first dimension is
12078 * greater than that of the second.
12080 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12081 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12085 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12087 bmap
= isl_basic_map_intersect(bmap
, gt
);
12092 /* Add a constraint imposing that the value of the first dimension is
12093 * greater than that of the second.
12095 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12096 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12098 isl_basic_map
*bmap
;
12100 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12102 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12107 /* Add a constraint imposing that the value of the first dimension is
12108 * smaller than that of the second.
12110 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12111 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12113 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12116 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12120 isl_local_space
*ls
;
12125 if (!isl_basic_map_divs_known(bmap
))
12126 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12127 "some divs are unknown", return NULL
);
12129 ls
= isl_basic_map_get_local_space(bmap
);
12130 div
= isl_local_space_get_div(ls
, pos
);
12131 isl_local_space_free(ls
);
12136 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12139 return isl_basic_map_get_div(bset
, pos
);
12142 /* Plug in "subs" for dimension "type", "pos" of "bset".
12144 * Let i be the dimension to replace and let "subs" be of the form
12148 * Any integer division with a non-zero coefficient for i,
12150 * floor((a i + g)/m)
12154 * floor((a f + d g)/(m d))
12156 * Constraints of the form
12164 * We currently require that "subs" is an integral expression.
12165 * Handling rational expressions may require us to add stride constraints
12166 * as we do in isl_basic_set_preimage_multi_aff.
12168 __isl_give isl_basic_set
*isl_basic_set_substitute(
12169 __isl_take isl_basic_set
*bset
,
12170 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12176 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12179 bset
= isl_basic_set_cow(bset
);
12180 if (!bset
|| !subs
)
12183 ctx
= isl_basic_set_get_ctx(bset
);
12184 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12185 isl_die(ctx
, isl_error_invalid
,
12186 "spaces don't match", goto error
);
12187 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12188 isl_die(ctx
, isl_error_unsupported
,
12189 "cannot handle divs yet", goto error
);
12190 if (!isl_int_is_one(subs
->v
->el
[0]))
12191 isl_die(ctx
, isl_error_invalid
,
12192 "can only substitute integer expressions", goto error
);
12194 pos
+= isl_basic_set_offset(bset
, type
);
12198 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12199 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12201 isl_int_set(v
, bset
->eq
[i
][pos
]);
12202 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12203 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12204 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12207 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12208 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12210 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12211 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12212 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12213 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12216 for (i
= 0; i
< bset
->n_div
; ++i
) {
12217 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12219 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12220 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12221 isl_seq_combine(bset
->div
[i
] + 1,
12222 subs
->v
->el
[0], bset
->div
[i
] + 1,
12223 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12224 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12229 bset
= isl_basic_set_simplify(bset
);
12230 return isl_basic_set_finalize(bset
);
12232 isl_basic_set_free(bset
);
12236 /* Plug in "subs" for dimension "type", "pos" of "set".
12238 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12239 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12243 if (set
&& isl_set_plain_is_empty(set
))
12246 set
= isl_set_cow(set
);
12250 for (i
= set
->n
- 1; i
>= 0; --i
) {
12251 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12252 if (remove_if_empty(set
, i
) < 0)
12262 /* Check if the range of "ma" is compatible with the domain or range
12263 * (depending on "type") of "bmap".
12265 static isl_stat
check_basic_map_compatible_range_multi_aff(
12266 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12267 __isl_keep isl_multi_aff
*ma
)
12270 isl_space
*ma_space
;
12272 ma_space
= isl_multi_aff_get_space(ma
);
12274 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12278 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12279 "parameters don't match", goto error
);
12280 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12284 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12285 "spaces don't match", goto error
);
12287 isl_space_free(ma_space
);
12288 return isl_stat_ok
;
12290 isl_space_free(ma_space
);
12291 return isl_stat_error
;
12294 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12295 * coefficients before the transformed range of dimensions,
12296 * the "n_after" coefficients after the transformed range of dimensions
12297 * and the coefficients of the other divs in "bmap".
12299 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12300 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12305 isl_local_space
*ls
;
12310 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12314 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12315 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12316 for (i
= 0; i
< n_div
; ++i
) {
12317 int o_bmap
= 0, o_ls
= 0;
12319 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12320 o_bmap
+= 1 + 1 + n_param
;
12321 o_ls
+= 1 + 1 + n_param
;
12322 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12323 o_bmap
+= n_before
;
12324 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12325 ls
->div
->row
[i
] + o_ls
, n_set
);
12328 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12330 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12331 ls
->div
->row
[i
] + o_ls
, n_div
);
12334 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12335 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12339 isl_local_space_free(ls
);
12342 isl_local_space_free(ls
);
12346 /* How many stride constraints does "ma" enforce?
12347 * That is, how many of the affine expressions have a denominator
12348 * different from one?
12350 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12355 for (i
= 0; i
< ma
->n
; ++i
)
12356 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12362 /* For each affine expression in ma of the form
12364 * x_i = (f_i y + h_i)/m_i
12366 * with m_i different from one, add a constraint to "bmap"
12369 * f_i y + h_i = m_i alpha_i
12371 * with alpha_i an additional existentially quantified variable.
12373 * The input variables of "ma" correspond to a subset of the variables
12374 * of "bmap". There are "n_before" variables in "bmap" before this
12375 * subset and "n_after" variables after this subset.
12376 * The integer divisions of the affine expressions in "ma" are assumed
12377 * to have been aligned. There are "n_div_ma" of them and
12378 * they appear first in "bmap", straight after the "n_after" variables.
12380 static __isl_give isl_basic_map
*add_ma_strides(
12381 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12382 int n_before
, int n_after
, int n_div_ma
)
12390 total
= isl_basic_map_total_dim(bmap
);
12391 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12392 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12393 for (i
= 0; i
< ma
->n
; ++i
) {
12394 int o_bmap
= 0, o_ma
= 1;
12396 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12398 div
= isl_basic_map_alloc_div(bmap
);
12399 k
= isl_basic_map_alloc_equality(bmap
);
12400 if (div
< 0 || k
< 0)
12402 isl_int_set_si(bmap
->div
[div
][0], 0);
12403 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12404 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12405 o_bmap
+= 1 + n_param
;
12406 o_ma
+= 1 + n_param
;
12407 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12408 o_bmap
+= n_before
;
12409 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12410 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12413 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12415 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12416 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12417 o_bmap
+= n_div_ma
;
12419 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12420 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12426 isl_basic_map_free(bmap
);
12430 /* Replace the domain or range space (depending on "type) of "space" by "set".
12432 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12433 enum isl_dim_type type
, __isl_take isl_space
*set
)
12435 if (type
== isl_dim_in
) {
12436 space
= isl_space_range(space
);
12437 space
= isl_space_map_from_domain_and_range(set
, space
);
12439 space
= isl_space_domain(space
);
12440 space
= isl_space_map_from_domain_and_range(space
, set
);
12446 /* Compute the preimage of the domain or range (depending on "type")
12447 * of "bmap" under the function represented by "ma".
12448 * In other words, plug in "ma" in the domain or range of "bmap".
12449 * The result is a basic map that lives in the same space as "bmap"
12450 * except that the domain or range has been replaced by
12451 * the domain space of "ma".
12453 * If bmap is represented by
12455 * A(p) + S u + B x + T v + C(divs) >= 0,
12457 * where u and x are input and output dimensions if type == isl_dim_out
12458 * while x and v are input and output dimensions if type == isl_dim_in,
12459 * and ma is represented by
12461 * x = D(p) + F(y) + G(divs')
12463 * then the result is
12465 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12467 * The divs in the input set are similarly adjusted.
12470 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12474 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12475 * B_i G(divs') + c_i(divs))/n_i)
12477 * If bmap is not a rational map and if F(y) involves any denominators
12479 * x_i = (f_i y + h_i)/m_i
12481 * then additional constraints are added to ensure that we only
12482 * map back integer points. That is we enforce
12484 * f_i y + h_i = m_i alpha_i
12486 * with alpha_i an additional existentially quantified variable.
12488 * We first copy over the divs from "ma".
12489 * Then we add the modified constraints and divs from "bmap".
12490 * Finally, we add the stride constraints, if needed.
12492 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12493 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12494 __isl_take isl_multi_aff
*ma
)
12498 isl_basic_map
*res
= NULL
;
12499 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12500 isl_int f
, c1
, c2
, g
;
12509 ma
= isl_multi_aff_align_divs(ma
);
12512 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12515 if (type
== isl_dim_in
) {
12517 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12519 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12522 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12523 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12525 space
= isl_multi_aff_get_domain_space(ma
);
12526 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12527 rational
= isl_basic_map_is_rational(bmap
);
12528 strides
= rational
? 0 : multi_aff_strides(ma
);
12529 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12530 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12532 res
= isl_basic_map_set_rational(res
);
12534 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12535 if (isl_basic_map_alloc_div(res
) < 0)
12538 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12541 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12542 k
= isl_basic_map_alloc_equality(res
);
12545 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12546 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12549 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12550 k
= isl_basic_map_alloc_inequality(res
);
12553 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12554 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12557 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12558 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12559 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12562 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12563 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12568 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12574 isl_basic_map_free(bmap
);
12575 isl_multi_aff_free(ma
);
12576 res
= isl_basic_map_simplify(res
);
12577 return isl_basic_map_finalize(res
);
12583 isl_basic_map_free(bmap
);
12584 isl_multi_aff_free(ma
);
12585 isl_basic_map_free(res
);
12589 /* Compute the preimage of "bset" under the function represented by "ma".
12590 * In other words, plug in "ma" in "bset". The result is a basic set
12591 * that lives in the domain space of "ma".
12593 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12594 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12596 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12599 /* Compute the preimage of the domain of "bmap" under the function
12600 * represented by "ma".
12601 * In other words, plug in "ma" in the domain of "bmap".
12602 * The result is a basic map that lives in the same space as "bmap"
12603 * except that the domain has been replaced by the domain space of "ma".
12605 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12606 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12608 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12611 /* Compute the preimage of the range of "bmap" under the function
12612 * represented by "ma".
12613 * In other words, plug in "ma" in the range of "bmap".
12614 * The result is a basic map that lives in the same space as "bmap"
12615 * except that the range has been replaced by the domain space of "ma".
12617 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12618 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12620 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12623 /* Check if the range of "ma" is compatible with the domain or range
12624 * (depending on "type") of "map".
12625 * Return isl_stat_error if anything is wrong.
12627 static isl_stat
check_map_compatible_range_multi_aff(
12628 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12629 __isl_keep isl_multi_aff
*ma
)
12632 isl_space
*ma_space
;
12634 ma_space
= isl_multi_aff_get_space(ma
);
12635 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12636 isl_space_free(ma_space
);
12638 return isl_stat_error
;
12640 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12641 "spaces don't match", return isl_stat_error
);
12642 return isl_stat_ok
;
12645 /* Compute the preimage of the domain or range (depending on "type")
12646 * of "map" under the function represented by "ma".
12647 * In other words, plug in "ma" in the domain or range of "map".
12648 * The result is a map that lives in the same space as "map"
12649 * except that the domain or range has been replaced by
12650 * the domain space of "ma".
12652 * The parameters are assumed to have been aligned.
12654 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12655 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12660 map
= isl_map_cow(map
);
12661 ma
= isl_multi_aff_align_divs(ma
);
12664 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12667 for (i
= 0; i
< map
->n
; ++i
) {
12668 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12669 isl_multi_aff_copy(ma
));
12674 space
= isl_multi_aff_get_domain_space(ma
);
12675 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12677 isl_space_free(map
->dim
);
12682 isl_multi_aff_free(ma
);
12684 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12685 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12688 isl_multi_aff_free(ma
);
12693 /* Compute the preimage of the domain or range (depending on "type")
12694 * of "map" under the function represented by "ma".
12695 * In other words, plug in "ma" in the domain or range of "map".
12696 * The result is a map that lives in the same space as "map"
12697 * except that the domain or range has been replaced by
12698 * the domain space of "ma".
12700 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12701 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12708 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
12712 return map_preimage_multi_aff(map
, type
, ma
);
12714 if (isl_map_check_named_params(map
) < 0)
12716 if (!isl_space_has_named_params(ma
->space
))
12717 isl_die(map
->ctx
, isl_error_invalid
,
12718 "unaligned unnamed parameters", goto error
);
12719 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12720 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12722 return map_preimage_multi_aff(map
, type
, ma
);
12724 isl_multi_aff_free(ma
);
12725 return isl_map_free(map
);
12728 /* Compute the preimage of "set" under the function represented by "ma".
12729 * In other words, plug in "ma" in "set". The result is a set
12730 * that lives in the domain space of "ma".
12732 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12733 __isl_take isl_multi_aff
*ma
)
12735 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12738 /* Compute the preimage of the domain of "map" under the function
12739 * represented by "ma".
12740 * In other words, plug in "ma" in the domain of "map".
12741 * The result is a map that lives in the same space as "map"
12742 * except that the domain has been replaced by the domain space of "ma".
12744 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12745 __isl_take isl_multi_aff
*ma
)
12747 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12750 /* Compute the preimage of the range of "map" under the function
12751 * represented by "ma".
12752 * In other words, plug in "ma" in the range of "map".
12753 * The result is a map that lives in the same space as "map"
12754 * except that the range has been replaced by the domain space of "ma".
12756 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12757 __isl_take isl_multi_aff
*ma
)
12759 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12762 /* Compute the preimage of "map" under the function represented by "pma".
12763 * In other words, plug in "pma" in the domain or range of "map".
12764 * The result is a map that lives in the same space as "map",
12765 * except that the space of type "type" has been replaced by
12766 * the domain space of "pma".
12768 * The parameters of "map" and "pma" are assumed to have been aligned.
12770 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12771 __isl_take isl_map
*map
, enum isl_dim_type type
,
12772 __isl_take isl_pw_multi_aff
*pma
)
12781 isl_pw_multi_aff_free(pma
);
12782 res
= isl_map_empty(isl_map_get_space(map
));
12787 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12788 isl_multi_aff_copy(pma
->p
[0].maff
));
12789 if (type
== isl_dim_in
)
12790 res
= isl_map_intersect_domain(res
,
12791 isl_map_copy(pma
->p
[0].set
));
12793 res
= isl_map_intersect_range(res
,
12794 isl_map_copy(pma
->p
[0].set
));
12796 for (i
= 1; i
< pma
->n
; ++i
) {
12799 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12800 isl_multi_aff_copy(pma
->p
[i
].maff
));
12801 if (type
== isl_dim_in
)
12802 res_i
= isl_map_intersect_domain(res_i
,
12803 isl_map_copy(pma
->p
[i
].set
));
12805 res_i
= isl_map_intersect_range(res_i
,
12806 isl_map_copy(pma
->p
[i
].set
));
12807 res
= isl_map_union(res
, res_i
);
12810 isl_pw_multi_aff_free(pma
);
12814 isl_pw_multi_aff_free(pma
);
12819 /* Compute the preimage of "map" under the function represented by "pma".
12820 * In other words, plug in "pma" in the domain or range of "map".
12821 * The result is a map that lives in the same space as "map",
12822 * except that the space of type "type" has been replaced by
12823 * the domain space of "pma".
12825 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12826 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12833 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
12837 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12839 if (isl_map_check_named_params(map
) < 0)
12841 if (!isl_space_has_named_params(pma
->dim
))
12842 isl_die(map
->ctx
, isl_error_invalid
,
12843 "unaligned unnamed parameters", goto error
);
12844 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12845 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12847 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12849 isl_pw_multi_aff_free(pma
);
12850 return isl_map_free(map
);
12853 /* Compute the preimage of "set" under the function represented by "pma".
12854 * In other words, plug in "pma" in "set". The result is a set
12855 * that lives in the domain space of "pma".
12857 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12858 __isl_take isl_pw_multi_aff
*pma
)
12860 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12863 /* Compute the preimage of the domain of "map" under the function
12864 * represented by "pma".
12865 * In other words, plug in "pma" in the domain of "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that domain space has been replaced by the domain space of "pma".
12869 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12870 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12872 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12875 /* Compute the preimage of the range of "map" under the function
12876 * represented by "pma".
12877 * In other words, plug in "pma" in the range of "map".
12878 * The result is a map that lives in the same space as "map",
12879 * except that range space has been replaced by the domain space of "pma".
12881 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12882 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12884 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12887 /* Compute the preimage of "map" under the function represented by "mpa".
12888 * In other words, plug in "mpa" in the domain or range of "map".
12889 * The result is a map that lives in the same space as "map",
12890 * except that the space of type "type" has been replaced by
12891 * the domain space of "mpa".
12893 * If the map does not involve any constraints that refer to the
12894 * dimensions of the substituted space, then the only possible
12895 * effect of "mpa" on the map is to map the space to a different space.
12896 * We create a separate isl_multi_aff to effectuate this change
12897 * in order to avoid spurious splitting of the map along the pieces
12900 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12901 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12904 isl_pw_multi_aff
*pma
;
12909 n
= isl_map_dim(map
, type
);
12910 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12914 space
= isl_multi_pw_aff_get_space(mpa
);
12915 isl_multi_pw_aff_free(mpa
);
12916 ma
= isl_multi_aff_zero(space
);
12917 return isl_map_preimage_multi_aff(map
, type
, ma
);
12920 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12921 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12924 isl_multi_pw_aff_free(mpa
);
12928 /* Compute the preimage of "map" under the function represented by "mpa".
12929 * In other words, plug in "mpa" in the domain "map".
12930 * The result is a map that lives in the same space as "map",
12931 * except that domain space has been replaced by the domain space of "mpa".
12933 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12934 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12936 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12939 /* Compute the preimage of "set" by the function represented by "mpa".
12940 * In other words, plug in "mpa" in "set".
12942 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12943 __isl_take isl_multi_pw_aff
*mpa
)
12945 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
12948 /* Are the "n" "coefficients" starting at "first" of the integer division
12949 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12951 * The "coefficient" at position 0 is the denominator.
12952 * The "coefficient" at position 1 is the constant term.
12954 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
12955 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
12956 unsigned first
, unsigned n
)
12958 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
12959 return isl_bool_error
;
12960 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
12961 return isl_bool_error
;
12962 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
12963 bmap2
->div
[pos2
] + first
, n
);
12966 /* Are the integer division expressions at position "pos1" in "bmap1" and
12967 * "pos2" in "bmap2" equal to each other, except that the constant terms
12970 isl_bool
isl_basic_map_equal_div_expr_except_constant(
12971 __isl_keep isl_basic_map
*bmap1
, int pos1
,
12972 __isl_keep isl_basic_map
*bmap2
, int pos2
)
12977 if (!bmap1
|| !bmap2
)
12978 return isl_bool_error
;
12979 total
= isl_basic_map_total_dim(bmap1
);
12980 if (total
!= isl_basic_map_total_dim(bmap2
))
12981 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
12982 "incomparable div expressions", return isl_bool_error
);
12983 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12985 if (equal
< 0 || !equal
)
12987 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12989 if (equal
< 0 || equal
)
12990 return isl_bool_not(equal
);
12991 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12995 /* Replace the numerator of the constant term of the integer division
12996 * expression at position "div" in "bmap" by "value".
12997 * The caller guarantees that this does not change the meaning
13000 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13001 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13003 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13004 return isl_basic_map_free(bmap
);
13006 isl_int_set_si(bmap
->div
[div
][1], value
);
13011 /* Is the point "inner" internal to inequality constraint "ineq"
13013 * The point is considered to be internal to the inequality constraint,
13014 * if it strictly lies on the positive side of the inequality constraint,
13015 * or if it lies on the constraint and the constraint is lexico-positive.
13017 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13018 __isl_keep isl_basic_set
*bset
, int ineq
)
13024 if (!inner
|| !bset
)
13025 return isl_bool_error
;
13027 ctx
= isl_basic_set_get_ctx(bset
);
13028 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13029 &ctx
->normalize_gcd
);
13030 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13031 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13033 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13034 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13035 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13038 /* Tighten the inequality constraints of "bset" that are outward with respect
13039 * to the point "vec".
13040 * That is, tighten the constraints that are not satisfied by "vec".
13042 * "vec" is a point internal to some superset S of "bset" that is used
13043 * to make the subsets of S disjoint, by tightening one half of the constraints
13044 * that separate two subsets. In particular, the constraints of S
13045 * are all satisfied by "vec" and should not be tightened.
13046 * Of the internal constraints, those that have "vec" on the outside
13047 * are tightened. The shared facet is included in the adjacent subset
13048 * with the opposite constraint.
13049 * For constraints that saturate "vec", this criterion cannot be used
13050 * to determine which of the two sides should be tightened.
13051 * Instead, the sign of the first non-zero coefficient is used
13052 * to make this choice. Note that this second criterion is never used
13053 * on the constraints of S since "vec" is interior to "S".
13055 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13056 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13060 bset
= isl_basic_set_cow(bset
);
13063 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13066 internal
= is_internal(vec
, bset
, j
);
13068 return isl_basic_set_free(bset
);
13071 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);