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>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
47 #include <isl_printer_private.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 /* Treat "bset" as a basic map.
55 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
56 * this function performs a redundant cast.
58 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
59 __isl_keep
const isl_basic_set
*bset
)
61 return (const isl_basic_map
*) bset
;
65 #define TYPE isl_basic_map
66 #include "has_single_reference_templ.c"
68 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
71 case isl_dim_param
: return 1;
72 case isl_dim_in
: return 1 + dim
->nparam
;
73 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
78 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
79 enum isl_dim_type type
)
82 return isl_size_error
;
84 case isl_dim_cst
: return 1;
87 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
88 case isl_dim_div
: return bmap
->n_div
;
89 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
94 /* Return the space of "map".
96 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
98 return map
? map
->dim
: NULL
;
101 /* Return the space of "set".
103 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
105 return isl_map_peek_space(set_to_map(set
));
108 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
110 return isl_space_dim(isl_map_peek_space(map
), type
);
113 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
115 return isl_map_dim(set_to_map(set
), type
);
118 /* Return the position of the variables of the given type
119 * within the sequence of variables of "bmap".
121 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
122 enum isl_dim_type type
)
126 space
= isl_basic_map_peek_space(bmap
);
128 return isl_size_error
;
133 case isl_dim_out
: return isl_space_offset(space
, type
);
134 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
137 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
138 "invalid dimension type", return isl_size_error
);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bset".
145 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
146 enum isl_dim_type type
)
148 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
151 /* Return the position of the coefficients of the variables of the given type
152 * within the sequence of coefficients of "bmap".
154 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
155 enum isl_dim_type type
)
158 case isl_dim_cst
: return 0;
162 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
167 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
168 enum isl_dim_type type
)
170 return isl_basic_map_offset(bset
, type
);
173 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
175 return pos(map
->dim
, type
);
178 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
179 enum isl_dim_type type
)
181 return isl_basic_map_dim(bset
, type
);
184 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
186 return isl_basic_set_dim(bset
, isl_dim_set
);
189 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
191 return isl_basic_set_dim(bset
, isl_dim_param
);
194 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
196 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
199 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
201 return isl_set_dim(set
, isl_dim_set
);
204 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
206 return isl_set_dim(set
, isl_dim_param
);
209 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
214 return isl_size_error
;
215 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
217 return isl_size_error
;
218 return dim
+ bmap
->n_div
;
221 /* Return the number of equality constraints in the description of "bmap".
222 * Return -1 on error.
224 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
231 /* Return the number of equality constraints in the description of "bset".
232 * Return -1 on error.
234 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
236 return isl_basic_map_n_equality(bset_to_bmap(bset
));
239 /* Return the number of inequality constraints in the description of "bmap".
240 * Return -1 on error.
242 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
249 /* Return the number of inequality constraints in the description of "bset".
250 * Return -1 on error.
252 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
254 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
257 /* Do "bmap1" and "bmap2" have the same parameters?
259 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
260 __isl_keep isl_basic_map
*bmap2
)
262 isl_space
*space1
, *space2
;
264 space1
= isl_basic_map_peek_space(bmap1
);
265 space2
= isl_basic_map_peek_space(bmap2
);
266 return isl_space_has_equal_params(space1
, space2
);
269 /* Do "map1" and "map2" have the same parameters?
271 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
272 __isl_keep isl_map
*map2
)
274 isl_space
*space1
, *space2
;
276 space1
= isl_map_peek_space(map1
);
277 space2
= isl_map_peek_space(map2
);
278 return isl_space_has_equal_params(space1
, space2
);
281 /* Do "map" and "set" have the same parameters?
283 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
284 __isl_keep isl_set
*set
)
286 return isl_map_has_equal_params(map
, set_to_map(set
));
289 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
290 __isl_keep isl_set
*set
)
294 return isl_bool_error
;
295 m
= isl_map_has_equal_params(map
, set_to_map(set
));
298 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
299 set
->dim
, isl_dim_set
);
302 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
303 __isl_keep isl_basic_set
*bset
)
307 return isl_bool_error
;
308 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
311 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
312 bset
->dim
, isl_dim_set
);
315 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
316 __isl_keep isl_set
*set
)
320 return isl_bool_error
;
321 m
= isl_map_has_equal_params(map
, set_to_map(set
));
324 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
325 set
->dim
, isl_dim_set
);
328 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
329 __isl_keep isl_basic_set
*bset
)
333 return isl_bool_error
;
334 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
337 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
338 bset
->dim
, isl_dim_set
);
341 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
343 return bmap
? bmap
->ctx
: NULL
;
346 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
348 return bset
? bset
->ctx
: NULL
;
351 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
353 return map
? map
->ctx
: NULL
;
356 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
358 return set
? set
->ctx
: NULL
;
361 /* Return the space of "bmap".
363 __isl_keep isl_space
*isl_basic_map_peek_space(
364 __isl_keep
const isl_basic_map
*bmap
)
366 return bmap
? bmap
->dim
: NULL
;
369 /* Return the space of "bset".
371 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
373 return isl_basic_map_peek_space(bset_to_bmap(bset
));
376 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
378 return isl_space_copy(isl_basic_map_peek_space(bmap
));
381 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
383 return isl_basic_map_get_space(bset_to_bmap(bset
));
386 /* Extract the divs in "bmap" as a matrix.
388 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
396 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
400 ctx
= isl_basic_map_get_ctx(bmap
);
401 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
402 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
406 for (i
= 0; i
< bmap
->n_div
; ++i
)
407 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
412 /* Extract the divs in "bset" as a matrix.
414 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
416 return isl_basic_map_get_divs(bset
);
419 __isl_give isl_local_space
*isl_basic_map_get_local_space(
420 __isl_keep isl_basic_map
*bmap
)
427 div
= isl_basic_map_get_divs(bmap
);
428 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
431 __isl_give isl_local_space
*isl_basic_set_get_local_space(
432 __isl_keep isl_basic_set
*bset
)
434 return isl_basic_map_get_local_space(bset
);
437 /* For each known div d = floor(f/m), add the constraints
440 * -(f-(m-1)) + m d >= 0
442 * Do not finalize the result.
444 static __isl_give isl_basic_map
*add_known_div_constraints(
445 __isl_take isl_basic_map
*bmap
)
450 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
452 return isl_basic_map_free(bmap
);
455 bmap
= isl_basic_map_cow(bmap
);
456 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
459 for (i
= 0; i
< n_div
; ++i
) {
460 if (isl_int_is_zero(bmap
->div
[i
][0]))
462 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
468 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
469 __isl_take isl_local_space
*ls
)
475 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
477 ls
= isl_local_space_free(ls
);
481 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
482 n_div
, 0, 2 * n_div
);
484 for (i
= 0; i
< n_div
; ++i
)
485 if (isl_basic_map_alloc_div(bmap
) < 0)
488 for (i
= 0; i
< n_div
; ++i
)
489 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
490 bmap
= add_known_div_constraints(bmap
);
492 isl_local_space_free(ls
);
495 isl_local_space_free(ls
);
496 isl_basic_map_free(bmap
);
500 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
501 __isl_take isl_local_space
*ls
)
503 return isl_basic_map_from_local_space(ls
);
506 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
508 return isl_space_copy(isl_map_peek_space(map
));
511 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
515 return isl_space_copy(set
->dim
);
518 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
519 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
521 bmap
= isl_basic_map_cow(bmap
);
524 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
527 bmap
= isl_basic_map_finalize(bmap
);
530 isl_basic_map_free(bmap
);
534 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
535 __isl_take isl_basic_set
*bset
, const char *s
)
537 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
540 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
541 enum isl_dim_type type
)
543 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
546 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
547 enum isl_dim_type type
, const char *s
)
551 map
= isl_map_cow(map
);
555 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
559 for (i
= 0; i
< map
->n
; ++i
) {
560 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
571 /* Replace the identifier of the tuple of type "type" by "id".
573 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
574 __isl_take isl_basic_map
*bmap
,
575 enum isl_dim_type type
, __isl_take isl_id
*id
)
577 bmap
= isl_basic_map_cow(bmap
);
580 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
582 return isl_basic_map_free(bmap
);
583 bmap
= isl_basic_map_finalize(bmap
);
590 /* Replace the identifier of the tuple by "id".
592 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
593 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
595 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
598 /* Does the input or output tuple have a name?
600 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
602 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
605 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
606 enum isl_dim_type type
)
608 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
611 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
614 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
618 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
619 enum isl_dim_type type
, __isl_take isl_id
*id
)
621 map
= isl_map_cow(map
);
625 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
627 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
633 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
634 __isl_take isl_id
*id
)
636 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
639 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
640 enum isl_dim_type type
)
642 map
= isl_map_cow(map
);
646 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
648 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
651 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
653 return isl_map_reset_tuple_id(set
, isl_dim_set
);
656 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
658 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
661 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
662 enum isl_dim_type type
)
664 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
667 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
669 return isl_map_has_tuple_id(set
, isl_dim_set
);
672 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
674 return isl_map_get_tuple_id(set
, isl_dim_set
);
677 /* Does the set tuple have a name?
679 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
682 return isl_bool_error
;
683 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
687 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
689 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
692 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
694 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
697 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
698 enum isl_dim_type type
, unsigned pos
)
700 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
703 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
704 enum isl_dim_type type
, unsigned pos
)
706 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
709 /* Does the given dimension have a name?
711 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
712 enum isl_dim_type type
, unsigned pos
)
715 return isl_bool_error
;
716 return isl_space_has_dim_name(map
->dim
, type
, pos
);
719 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
720 enum isl_dim_type type
, unsigned pos
)
722 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
725 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
726 enum isl_dim_type type
, unsigned pos
)
728 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
731 /* Does the given dimension have a name?
733 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
734 enum isl_dim_type type
, unsigned pos
)
737 return isl_bool_error
;
738 return isl_space_has_dim_name(set
->dim
, type
, pos
);
741 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
742 __isl_take isl_basic_map
*bmap
,
743 enum isl_dim_type type
, unsigned pos
, const char *s
)
745 bmap
= isl_basic_map_cow(bmap
);
748 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
751 return isl_basic_map_finalize(bmap
);
753 isl_basic_map_free(bmap
);
757 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
758 enum isl_dim_type type
, unsigned pos
, const char *s
)
762 map
= isl_map_cow(map
);
766 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
770 for (i
= 0; i
< map
->n
; ++i
) {
771 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
782 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
783 __isl_take isl_basic_set
*bset
,
784 enum isl_dim_type type
, unsigned pos
, const char *s
)
786 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
790 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
791 enum isl_dim_type type
, unsigned pos
, const char *s
)
793 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
797 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
798 enum isl_dim_type type
, unsigned pos
)
801 return isl_bool_error
;
802 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
805 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
806 enum isl_dim_type type
, unsigned pos
)
808 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
811 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
812 enum isl_dim_type type
, unsigned pos
)
814 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
817 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
818 enum isl_dim_type type
, unsigned pos
)
820 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
823 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
824 enum isl_dim_type type
, unsigned pos
)
826 return isl_map_has_dim_id(set
, type
, pos
);
829 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
830 enum isl_dim_type type
, unsigned pos
)
832 return isl_map_get_dim_id(set
, type
, pos
);
835 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
836 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
838 map
= isl_map_cow(map
);
842 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
844 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
850 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
851 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
853 return isl_map_set_dim_id(set
, type
, pos
, id
);
856 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
857 __isl_keep isl_id
*id
)
861 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
864 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
865 __isl_keep isl_id
*id
)
867 return isl_map_find_dim_by_id(set
, type
, id
);
870 /* Return the position of the dimension of the given type and name
872 * Return -1 if no such dimension can be found.
874 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
875 enum isl_dim_type type
, const char *name
)
879 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
882 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
887 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
890 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
893 return isl_map_find_dim_by_name(set
, type
, name
);
896 /* Check whether equality i of bset is a pure stride constraint
897 * on a single dimension, i.e., of the form
901 * with k a constant and e an existentially quantified variable.
903 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
911 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
912 d
= isl_basic_set_dim(bset
, isl_dim_set
);
913 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
914 if (nparam
< 0 || d
< 0 || n_div
< 0)
915 return isl_bool_error
;
917 if (!isl_int_is_zero(bset
->eq
[i
][0]))
918 return isl_bool_false
;
920 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
921 return isl_bool_false
;
922 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
924 return isl_bool_false
;
925 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
927 return isl_bool_false
;
929 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
931 return isl_bool_false
;
932 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
933 n_div
- pos2
- 1) != -1)
934 return isl_bool_false
;
935 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
936 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
937 return isl_bool_false
;
939 return isl_bool_true
;
942 /* Reset the user pointer on all identifiers of parameters and tuples
943 * of the space of "map".
945 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
949 space
= isl_map_get_space(map
);
950 space
= isl_space_reset_user(space
);
951 map
= isl_map_reset_space(map
, space
);
956 /* Reset the user pointer on all identifiers of parameters and tuples
957 * of the space of "set".
959 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
961 return isl_map_reset_user(set
);
964 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
967 return isl_bool_error
;
968 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
971 /* Has "map" been marked as a rational map?
972 * In particular, have all basic maps in "map" been marked this way?
973 * An empty map is not considered to be rational.
974 * Maps where only some of the basic maps are marked rational
977 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
983 return isl_bool_error
;
985 return isl_bool_false
;
986 rational
= isl_basic_map_is_rational(map
->p
[0]);
989 for (i
= 1; i
< map
->n
; ++i
) {
992 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
995 if (rational
!= rational_i
)
996 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
997 "mixed rational and integer basic maps "
998 "not supported", return isl_bool_error
);
1004 /* Has "set" been marked as a rational set?
1005 * In particular, have all basic set in "set" been marked this way?
1006 * An empty set is not considered to be rational.
1007 * Sets where only some of the basic sets are marked rational
1010 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1012 return isl_map_is_rational(set
);
1015 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1017 return isl_basic_map_is_rational(bset
);
1020 /* Does "bmap" contain any rational points?
1022 * If "bmap" has an equality for each dimension, equating the dimension
1023 * to an integer constant, then it has no rational points, even if it
1024 * is marked as rational.
1026 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1028 isl_bool has_rational
= isl_bool_true
;
1032 return isl_bool_error
;
1033 if (isl_basic_map_plain_is_empty(bmap
))
1034 return isl_bool_false
;
1035 if (!isl_basic_map_is_rational(bmap
))
1036 return isl_bool_false
;
1037 bmap
= isl_basic_map_copy(bmap
);
1038 bmap
= isl_basic_map_implicit_equalities(bmap
);
1039 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1041 return isl_bool_error
;
1042 if (bmap
->n_eq
== total
) {
1044 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1045 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1048 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1049 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1051 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1056 if (i
== bmap
->n_eq
)
1057 has_rational
= isl_bool_false
;
1059 isl_basic_map_free(bmap
);
1061 return has_rational
;
1064 /* Does "map" contain any rational points?
1066 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1069 isl_bool has_rational
;
1072 return isl_bool_error
;
1073 for (i
= 0; i
< map
->n
; ++i
) {
1074 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1075 if (has_rational
< 0 || has_rational
)
1076 return has_rational
;
1078 return isl_bool_false
;
1081 /* Does "set" contain any rational points?
1083 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1085 return isl_map_has_rational(set
);
1088 /* Is this basic set a parameter domain?
1090 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1093 return isl_bool_error
;
1094 return isl_space_is_params(bset
->dim
);
1097 /* Is this set a parameter domain?
1099 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1102 return isl_bool_error
;
1103 return isl_space_is_params(set
->dim
);
1106 /* Is this map actually a parameter domain?
1107 * Users should never call this function. Outside of isl,
1108 * a map can never be a parameter domain.
1110 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1113 return isl_bool_error
;
1114 return isl_space_is_params(map
->dim
);
1117 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1118 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1119 unsigned n_eq
, unsigned n_ineq
)
1122 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1123 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1124 size_t row_size
= 1 + n_var
+ extra
;
1130 return isl_basic_map_free(bmap
);
1132 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1133 if (isl_blk_is_error(bmap
->block
))
1136 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1137 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1141 bmap
->block2
= isl_blk_empty();
1144 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1145 if (isl_blk_is_error(bmap
->block2
))
1148 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1153 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1154 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1156 for (i
= 0; i
< extra
; ++i
)
1157 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1161 bmap
->c_size
= n_eq
+ n_ineq
;
1162 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1163 bmap
->extra
= extra
;
1167 bmap
->sample
= NULL
;
1171 isl_basic_map_free(bmap
);
1175 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1176 unsigned nparam
, unsigned dim
, unsigned extra
,
1177 unsigned n_eq
, unsigned n_ineq
)
1179 struct isl_basic_map
*bmap
;
1182 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1186 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1187 return bset_from_bmap(bmap
);
1190 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1191 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1193 struct isl_basic_map
*bmap
;
1196 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1197 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1198 return bset_from_bmap(bmap
);
1200 isl_space_free(dim
);
1204 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1205 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1207 struct isl_basic_map
*bmap
;
1211 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1216 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1218 isl_space_free(space
);
1222 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1223 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1224 unsigned n_eq
, unsigned n_ineq
)
1226 struct isl_basic_map
*bmap
;
1229 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1233 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1237 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1238 __isl_keep isl_basic_map
*src
)
1241 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1243 if (!dst
|| total
< 0)
1244 return isl_basic_map_free(dst
);
1246 for (i
= 0; i
< src
->n_eq
; ++i
) {
1247 int j
= isl_basic_map_alloc_equality(dst
);
1249 return isl_basic_map_free(dst
);
1250 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1253 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1254 int j
= isl_basic_map_alloc_inequality(dst
);
1256 return isl_basic_map_free(dst
);
1257 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1260 for (i
= 0; i
< src
->n_div
; ++i
) {
1261 int j
= isl_basic_map_alloc_div(dst
);
1263 return isl_basic_map_free(dst
);
1264 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1266 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1270 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1272 struct isl_basic_map
*dup
;
1276 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1277 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1278 dup
= dup_constraints(dup
, bmap
);
1281 dup
->flags
= bmap
->flags
;
1282 dup
->sample
= isl_vec_copy(bmap
->sample
);
1286 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1288 struct isl_basic_map
*dup
;
1290 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1291 return bset_from_bmap(dup
);
1294 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1299 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1303 return isl_basic_set_dup(bset
);
1306 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1315 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1320 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1324 bmap
= isl_basic_map_dup(bmap
);
1326 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1330 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1339 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1344 if (--bmap
->ref
> 0)
1347 isl_ctx_deref(bmap
->ctx
);
1349 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1351 isl_blk_free(bmap
->ctx
, bmap
->block
);
1352 isl_vec_free(bmap
->sample
);
1353 isl_space_free(bmap
->dim
);
1359 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1361 return isl_basic_map_free(bset_to_bmap(bset
));
1364 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1366 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1369 /* Check that "bset" does not involve any parameters.
1371 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1375 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1377 return isl_stat_error
;
1379 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1380 "basic set should not have any parameters",
1381 return isl_stat_error
);
1385 /* Check that "bset" does not involve any local variables.
1387 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1391 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1393 return isl_stat_error
;
1395 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1396 "basic set should not have any local variables",
1397 return isl_stat_error
);
1401 /* Check that "map" has only named parameters, reporting an error
1404 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1406 return isl_space_check_named_params(isl_map_peek_space(map
));
1409 /* Check that "bmap" has only named parameters, reporting an error
1412 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1414 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1417 /* Check that "bmap1" and "bmap2" have the same parameters,
1418 * reporting an error if they do not.
1420 static isl_stat
isl_basic_map_check_equal_params(
1421 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1425 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1427 return isl_stat_error
;
1429 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1430 "parameters don't match", return isl_stat_error
);
1434 __isl_give isl_map
*isl_map_align_params_map_map_and(
1435 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1436 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1437 __isl_take isl_map
*map2
))
1441 if (isl_map_has_equal_params(map1
, map2
))
1442 return fn(map1
, map2
);
1443 if (isl_map_check_named_params(map1
) < 0)
1445 if (isl_map_check_named_params(map2
) < 0)
1447 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1448 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1449 return fn(map1
, map2
);
1456 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1457 __isl_keep isl_map
*map2
,
1458 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1463 return isl_bool_error
;
1464 if (isl_map_has_equal_params(map1
, map2
))
1465 return fn(map1
, map2
);
1466 if (isl_map_check_named_params(map1
) < 0)
1467 return isl_bool_error
;
1468 if (isl_map_check_named_params(map2
) < 0)
1469 return isl_bool_error
;
1470 map1
= isl_map_copy(map1
);
1471 map2
= isl_map_copy(map2
);
1472 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1473 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1480 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1483 struct isl_ctx
*ctx
;
1485 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1489 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1490 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1492 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1493 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1494 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1495 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1496 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1498 int j
= isl_basic_map_alloc_inequality(bmap
);
1502 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1503 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1510 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1511 bmap
->extra
- bmap
->n_div
);
1512 return bmap
->n_eq
++;
1515 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1517 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1520 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1524 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1529 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1531 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1534 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1539 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1541 if (pos
!= bmap
->n_eq
- 1) {
1543 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1544 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1550 /* Turn inequality "pos" of "bmap" into an equality.
1552 * In particular, we move the inequality in front of the equalities
1553 * and move the last inequality in the position of the moved inequality.
1554 * Note that isl_tab_make_equalities_explicit depends on this particular
1555 * change in the ordering of the constraints.
1557 void isl_basic_map_inequality_to_equality(
1558 struct isl_basic_map
*bmap
, unsigned pos
)
1562 t
= bmap
->ineq
[pos
];
1563 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1564 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1569 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1570 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1571 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1572 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1575 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1577 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1580 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1583 struct isl_ctx
*ctx
;
1585 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1589 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1590 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1591 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1594 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1595 bmap
->extra
- bmap
->n_div
);
1596 return bmap
->n_ineq
++;
1599 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1601 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1604 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1608 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1613 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1615 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1618 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1623 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1625 if (pos
!= bmap
->n_ineq
- 1) {
1626 t
= bmap
->ineq
[pos
];
1627 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1628 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1629 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1635 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1637 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1640 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1647 empty
= isl_basic_map_plain_is_empty(bmap
);
1649 return isl_basic_map_free(bmap
);
1653 bmap
= isl_basic_map_cow(bmap
);
1654 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1655 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1657 return isl_basic_map_free(bmap
);
1658 k
= isl_basic_map_alloc_equality(bmap
);
1661 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1664 isl_basic_map_free(bmap
);
1668 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1671 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1674 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1680 bmap
= isl_basic_map_cow(bmap
);
1681 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1682 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1684 return isl_basic_map_free(bmap
);
1685 k
= isl_basic_map_alloc_inequality(bmap
);
1688 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1691 isl_basic_map_free(bmap
);
1695 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1698 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1701 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1705 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1708 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1709 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1710 bmap
->extra
- bmap
->n_div
);
1711 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1712 return bmap
->n_div
++;
1715 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1717 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1721 #define TYPE isl_basic_map
1722 #include "check_type_range_templ.c"
1724 /* Check that there are "n" dimensions of type "type" starting at "first"
1727 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1728 enum isl_dim_type type
, unsigned first
, unsigned n
)
1730 return isl_basic_map_check_range(bset_to_bmap(bset
),
1734 /* Insert an extra integer division, prescribed by "div", to "bmap"
1735 * at (integer division) position "pos".
1737 * The integer division is first added at the end and then moved
1738 * into the right position.
1740 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1741 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1746 bmap
= isl_basic_map_cow(bmap
);
1747 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1748 if (total
< 0 || !div
)
1749 return isl_basic_map_free(bmap
);
1751 if (div
->size
!= 1 + 1 + total
)
1752 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1753 "unexpected size", return isl_basic_map_free(bmap
));
1754 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1755 return isl_basic_map_free(bmap
);
1757 bmap
= isl_basic_map_extend_space(bmap
,
1758 isl_basic_map_get_space(bmap
), 1, 0, 2);
1759 k
= isl_basic_map_alloc_div(bmap
);
1761 return isl_basic_map_free(bmap
);
1762 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1763 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1765 for (i
= k
; i
> pos
; --i
)
1766 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1771 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1774 return isl_stat_error
;
1775 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1780 static __isl_give isl_basic_map
*add_constraints(
1781 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1782 unsigned i_pos
, unsigned o_pos
)
1784 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1785 unsigned o_in
, o_out
;
1788 struct isl_dim_map
*dim_map
;
1790 space
= isl_basic_map_peek_space(bmap2
);
1791 if (!bmap1
|| !space
)
1794 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1795 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1796 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1797 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1798 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1799 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1800 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1801 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1803 ctx
= isl_basic_map_get_ctx(bmap1
);
1804 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1805 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1806 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1807 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1808 isl_dim_map_div(dim_map
, bmap2
, total
);
1810 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1812 isl_basic_map_free(bmap1
);
1813 isl_basic_map_free(bmap2
);
1817 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1818 struct isl_basic_set
*bset2
, unsigned pos
)
1820 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1821 bset_to_bmap(bset2
), 0, pos
));
1824 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1825 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1826 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1828 struct isl_basic_map
*ext
;
1838 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1839 base
->extra
>= base
->n_div
+ extra
;
1841 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1842 room_for_ineq(base
, n_ineq
)) {
1843 isl_space_free(space
);
1847 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1848 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1849 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1850 extra
+= base
->extra
;
1852 n_ineq
+= base
->n_ineq
;
1854 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1860 ext
->sample
= isl_vec_copy(base
->sample
);
1861 flags
= base
->flags
;
1862 ext
= add_constraints(ext
, base
, 0, 0);
1865 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1871 isl_space_free(space
);
1872 isl_basic_map_free(base
);
1876 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1877 __isl_take isl_basic_set
*base
,
1878 __isl_take isl_space
*dim
, unsigned extra
,
1879 unsigned n_eq
, unsigned n_ineq
)
1881 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1882 dim
, extra
, n_eq
, n_ineq
));
1885 struct isl_basic_map
*isl_basic_map_extend_constraints(
1886 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1890 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1894 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1895 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1896 unsigned n_eq
, unsigned n_ineq
)
1898 struct isl_basic_map
*bmap
;
1903 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1907 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1910 isl_basic_map_free(base
);
1914 struct isl_basic_set
*isl_basic_set_extend_constraints(
1915 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1917 isl_basic_map
*bmap
= bset_to_bmap(base
);
1918 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1919 return bset_from_bmap(bmap
);
1922 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1924 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1927 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1932 if (bmap
->ref
> 1) {
1934 bmap
= isl_basic_map_dup(bmap
);
1937 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1938 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1943 /* Clear all cached information in "map", either because it is about
1944 * to be modified or because it is being freed.
1945 * Always return the same pointer that is passed in.
1946 * This is needed for the use in isl_map_free.
1948 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1950 isl_basic_map_free(map
->cached_simple_hull
[0]);
1951 isl_basic_map_free(map
->cached_simple_hull
[1]);
1952 map
->cached_simple_hull
[0] = NULL
;
1953 map
->cached_simple_hull
[1] = NULL
;
1957 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1959 return isl_map_cow(set
);
1962 /* Return an isl_map that is equal to "map" and that has only
1963 * a single reference.
1965 * If the original input already has only one reference, then
1966 * simply return it, but clear all cached information, since
1967 * it may be rendered invalid by the operations that will be
1968 * performed on the result.
1970 * Otherwise, create a duplicate (without any cached information).
1972 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1978 return clear_caches(map
);
1980 return isl_map_dup(map
);
1983 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1984 unsigned a_len
, unsigned b_len
)
1986 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1987 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1988 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1991 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1992 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1997 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2000 if (n1
== 0 || n2
== 0)
2003 bmap
= isl_basic_map_cow(bmap
);
2007 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2008 if (isl_blk_is_error(blk
))
2011 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2013 bmap
->eq
[i
] + pos
, n1
, n2
);
2015 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2017 bmap
->ineq
[i
] + pos
, n1
, n2
);
2019 for (i
= 0; i
< bmap
->n_div
; ++i
)
2021 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2023 isl_blk_free(bmap
->ctx
, blk
);
2025 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2026 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2027 return isl_basic_map_finalize(bmap
);
2029 isl_basic_map_free(bmap
);
2033 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2034 __isl_take isl_basic_map
*bmap
)
2039 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2041 return isl_basic_map_free(bmap
);
2042 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2043 return isl_basic_map_free(bmap
);
2044 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2046 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2048 i
= isl_basic_map_alloc_equality(bmap
);
2052 isl_int_set_si(bmap
->eq
[i
][0], 1);
2053 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2054 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2055 isl_vec_free(bmap
->sample
);
2056 bmap
->sample
= NULL
;
2057 return isl_basic_map_finalize(bmap
);
2059 isl_basic_map_free(bmap
);
2063 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2064 __isl_take isl_basic_set
*bset
)
2066 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2069 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2070 __isl_take isl_basic_map
*bmap
)
2075 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2078 bmap
= isl_basic_map_cow(bmap
);
2082 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2084 return isl_basic_map_finalize(bmap
);
2087 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2088 __isl_take isl_basic_set
*bset
)
2090 return isl_basic_map_set_rational(bset
);
2093 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2094 __isl_take isl_basic_set
*bset
)
2099 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2102 bset
= isl_basic_set_cow(bset
);
2106 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2108 return isl_basic_set_finalize(bset
);
2111 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2115 map
= isl_map_cow(map
);
2118 for (i
= 0; i
< map
->n
; ++i
) {
2119 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2129 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2131 return isl_map_set_rational(set
);
2134 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2137 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2139 isl_int
*t
= bmap
->div
[a
];
2140 bmap
->div
[a
] = bmap
->div
[b
];
2144 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2145 * div definitions accordingly.
2147 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2153 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2155 return isl_basic_map_free(bmap
);
2157 swap_div(bmap
, a
, b
);
2159 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2160 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2162 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2163 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2165 for (i
= 0; i
< bmap
->n_div
; ++i
)
2166 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2167 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2172 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2174 isl_seq_cpy(c
, c
+ n
, rem
);
2175 isl_seq_clr(c
+ rem
, n
);
2178 /* Drop n dimensions starting at first.
2180 * In principle, this frees up some extra variables as the number
2181 * of columns remains constant, but we would have to extend
2182 * the div array too as the number of rows in this array is assumed
2183 * to be equal to extra.
2185 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2186 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2188 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2191 /* Move "n" divs starting at "first" to the end of the list of divs.
2193 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2194 unsigned first
, unsigned n
)
2199 if (first
+ n
== bmap
->n_div
)
2202 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2205 for (i
= 0; i
< n
; ++i
)
2206 div
[i
] = bmap
->div
[first
+ i
];
2207 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2208 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2209 for (i
= 0; i
< n
; ++i
)
2210 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2214 isl_basic_map_free(bmap
);
2219 #define TYPE isl_map
2221 #include "check_type_range_templ.c"
2223 /* Check that there are "n" dimensions of type "type" starting at "first"
2226 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2227 enum isl_dim_type type
, unsigned first
, unsigned n
)
2229 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2232 /* Drop "n" dimensions of type "type" starting at "first".
2233 * Perform the core computation, without cowing or
2234 * simplifying and finalizing the result.
2236 * In principle, this frees up some extra variables as the number
2237 * of columns remains constant, but we would have to extend
2238 * the div array too as the number of rows in this array is assumed
2239 * to be equal to extra.
2241 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2242 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2243 unsigned first
, unsigned n
)
2250 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2251 return isl_basic_map_free(bmap
);
2253 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2255 return isl_basic_map_free(bmap
);
2257 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2258 left
= total
- (offset
- 1) - n
;
2259 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2260 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2262 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2263 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2265 for (i
= 0; i
< bmap
->n_div
; ++i
)
2266 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2268 if (type
== isl_dim_div
) {
2269 bmap
= move_divs_last(bmap
, first
, n
);
2272 if (isl_basic_map_free_div(bmap
, n
) < 0)
2273 return isl_basic_map_free(bmap
);
2275 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2277 return isl_basic_map_free(bmap
);
2279 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2280 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2284 /* Drop "n" dimensions of type "type" starting at "first".
2286 * In principle, this frees up some extra variables as the number
2287 * of columns remains constant, but we would have to extend
2288 * the div array too as the number of rows in this array is assumed
2289 * to be equal to extra.
2291 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2292 enum isl_dim_type type
, unsigned first
, unsigned n
)
2296 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2299 bmap
= isl_basic_map_cow(bmap
);
2303 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2305 bmap
= isl_basic_map_simplify(bmap
);
2306 return isl_basic_map_finalize(bmap
);
2309 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2310 enum isl_dim_type type
, unsigned first
, unsigned n
)
2312 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2316 /* No longer consider "map" to be normalized.
2318 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2322 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2326 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2327 enum isl_dim_type type
, unsigned first
, unsigned n
)
2331 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2332 return isl_map_free(map
);
2334 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2336 map
= isl_map_cow(map
);
2339 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2343 for (i
= 0; i
< map
->n
; ++i
) {
2344 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2348 map
= isl_map_unmark_normalized(map
);
2356 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2357 enum isl_dim_type type
, unsigned first
, unsigned n
)
2359 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2362 /* Drop the integer division at position "div", which is assumed
2363 * not to appear in any of the constraints or
2364 * in any of the other integer divisions.
2366 * Since the integer division is redundant, there is no need to cow.
2368 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2369 __isl_take isl_basic_map
*bmap
, unsigned div
)
2371 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2374 /* Eliminate the specified n dimensions starting at first from the
2375 * constraints, without removing the dimensions from the space.
2376 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2378 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2379 enum isl_dim_type type
, unsigned first
, unsigned n
)
2386 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2387 return isl_map_free(map
);
2389 map
= isl_map_cow(map
);
2393 for (i
= 0; i
< map
->n
; ++i
) {
2394 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2404 /* Eliminate the specified n dimensions starting at first from the
2405 * constraints, without removing the dimensions from the space.
2406 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2408 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2409 enum isl_dim_type type
, unsigned first
, unsigned n
)
2411 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2414 /* Eliminate the specified n dimensions starting at first from the
2415 * constraints, without removing the dimensions from the space.
2416 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2418 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2419 unsigned first
, unsigned n
)
2421 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2424 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2425 __isl_take isl_basic_map
*bmap
)
2429 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2431 return isl_basic_map_free(bmap
);
2432 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2436 return isl_basic_map_finalize(bmap
);
2439 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2440 __isl_take isl_basic_set
*bset
)
2442 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2445 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2454 map
= isl_map_cow(map
);
2458 for (i
= 0; i
< map
->n
; ++i
) {
2459 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2469 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2471 return isl_map_remove_divs(set
);
2474 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2475 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2476 unsigned first
, unsigned n
)
2478 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2479 return isl_basic_map_free(bmap
);
2480 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2482 bmap
= isl_basic_map_eliminate_vars(bmap
,
2483 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2486 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2488 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2492 /* Return true if the definition of the given div (recursively) involves
2493 * any of the given variables.
2495 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2496 unsigned first
, unsigned n
)
2499 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2501 if (isl_int_is_zero(bmap
->div
[div
][0]))
2502 return isl_bool_false
;
2503 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2504 return isl_bool_true
;
2506 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2509 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2511 involves
= div_involves_vars(bmap
, i
, first
, n
);
2512 if (involves
< 0 || involves
)
2516 return isl_bool_false
;
2519 /* Try and add a lower and/or upper bound on "div" to "bmap"
2520 * based on inequality "i".
2521 * "total" is the total number of variables (excluding the divs).
2522 * "v" is a temporary object that can be used during the calculations.
2523 * If "lb" is set, then a lower bound should be constructed.
2524 * If "ub" is set, then an upper bound should be constructed.
2526 * The calling function has already checked that the inequality does not
2527 * reference "div", but we still need to check that the inequality is
2528 * of the right form. We'll consider the case where we want to construct
2529 * a lower bound. The construction of upper bounds is similar.
2531 * Let "div" be of the form
2533 * q = floor((a + f(x))/d)
2535 * We essentially check if constraint "i" is of the form
2539 * so that we can use it to derive a lower bound on "div".
2540 * However, we allow a slightly more general form
2544 * with the condition that the coefficients of g(x) - f(x) are all
2546 * Rewriting this constraint as
2550 * adding a + f(x) to both sides and dividing by d, we obtain
2552 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2554 * Taking the floor on both sides, we obtain
2556 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2560 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2562 * In the case of an upper bound, we construct the constraint
2564 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2567 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2568 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2569 unsigned total
, isl_int v
, int lb
, int ub
)
2573 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2575 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2576 bmap
->div
[div
][1 + 1 + j
]);
2577 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2580 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2581 bmap
->div
[div
][1 + 1 + j
]);
2582 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2588 bmap
= isl_basic_map_cow(bmap
);
2589 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2591 int k
= isl_basic_map_alloc_inequality(bmap
);
2594 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2595 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2596 bmap
->div
[div
][1 + j
]);
2597 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2598 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2600 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2603 int k
= isl_basic_map_alloc_inequality(bmap
);
2606 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2607 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2608 bmap
->div
[div
][1 + j
]);
2609 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2610 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2612 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2617 isl_basic_map_free(bmap
);
2621 /* This function is called right before "div" is eliminated from "bmap"
2622 * using Fourier-Motzkin.
2623 * Look through the constraints of "bmap" for constraints on the argument
2624 * of the integer division and use them to construct constraints on the
2625 * integer division itself. These constraints can then be combined
2626 * during the Fourier-Motzkin elimination.
2627 * Note that it is only useful to introduce lower bounds on "div"
2628 * if "bmap" already contains upper bounds on "div" as the newly
2629 * introduce lower bounds can then be combined with the pre-existing
2630 * upper bounds. Similarly for upper bounds.
2631 * We therefore first check if "bmap" contains any lower and/or upper bounds
2634 * It is interesting to note that the introduction of these constraints
2635 * can indeed lead to more accurate results, even when compared to
2636 * deriving constraints on the argument of "div" from constraints on "div".
2637 * Consider, for example, the set
2639 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2641 * The second constraint can be rewritten as
2643 * 2 * [(-i-2j+3)/4] + k >= 0
2645 * from which we can derive
2647 * -i - 2j + 3 >= -2k
2653 * Combined with the first constraint, we obtain
2655 * -3 <= 3 + 2k or k >= -3
2657 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2658 * the first constraint, we obtain
2660 * [(i + 2j)/4] >= [-3/4] = -1
2662 * Combining this constraint with the second constraint, we obtain
2666 static __isl_give isl_basic_map
*insert_bounds_on_div(
2667 __isl_take isl_basic_map
*bmap
, int div
)
2670 int check_lb
, check_ub
;
2677 if (isl_int_is_zero(bmap
->div
[div
][0]))
2680 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2682 return isl_basic_map_free(bmap
);
2686 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2687 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2694 if (!check_lb
&& !check_ub
)
2699 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2700 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2703 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2704 check_lb
, check_ub
);
2712 /* Remove all divs (recursively) involving any of the given dimensions
2713 * in their definitions.
2715 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2716 __isl_take isl_basic_map
*bmap
,
2717 enum isl_dim_type type
, unsigned first
, unsigned n
)
2721 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2722 return isl_basic_map_free(bmap
);
2723 first
+= isl_basic_map_offset(bmap
, type
);
2725 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2728 involves
= div_involves_vars(bmap
, i
, first
, n
);
2730 return isl_basic_map_free(bmap
);
2733 bmap
= insert_bounds_on_div(bmap
, i
);
2734 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2743 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2744 __isl_take isl_basic_set
*bset
,
2745 enum isl_dim_type type
, unsigned first
, unsigned n
)
2747 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2750 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2751 enum isl_dim_type type
, unsigned first
, unsigned n
)
2760 map
= isl_map_cow(map
);
2764 for (i
= 0; i
< map
->n
; ++i
) {
2765 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2776 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2777 enum isl_dim_type type
, unsigned first
, unsigned n
)
2779 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2783 /* Does the description of "bmap" depend on the specified dimensions?
2784 * We also check whether the dimensions appear in any of the div definitions.
2785 * In principle there is no need for this check. If the dimensions appear
2786 * in a div definition, they also appear in the defining constraints of that
2789 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2790 enum isl_dim_type type
, unsigned first
, unsigned n
)
2794 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2795 return isl_bool_error
;
2797 first
+= isl_basic_map_offset(bmap
, type
);
2798 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2799 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2800 return isl_bool_true
;
2801 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2802 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2803 return isl_bool_true
;
2804 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2805 if (isl_int_is_zero(bmap
->div
[i
][0]))
2807 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2808 return isl_bool_true
;
2811 return isl_bool_false
;
2814 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2815 enum isl_dim_type type
, unsigned first
, unsigned n
)
2819 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2820 return isl_bool_error
;
2822 for (i
= 0; i
< map
->n
; ++i
) {
2823 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2825 if (involves
< 0 || involves
)
2829 return isl_bool_false
;
2832 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2833 enum isl_dim_type type
, unsigned first
, unsigned n
)
2835 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2838 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2839 enum isl_dim_type type
, unsigned first
, unsigned n
)
2841 return isl_map_involves_dims(set
, type
, first
, n
);
2844 /* Drop all constraints in bmap that involve any of the dimensions
2845 * first to first+n-1.
2846 * This function only performs the actual removal of constraints.
2848 * This function should not call finalize since it is used by
2849 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2851 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2852 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2859 bmap
= isl_basic_map_cow(bmap
);
2864 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2865 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2867 isl_basic_map_drop_equality(bmap
, i
);
2870 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2871 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2873 isl_basic_map_drop_inequality(bmap
, i
);
2879 /* Drop all constraints in bset that involve any of the dimensions
2880 * first to first+n-1.
2881 * This function only performs the actual removal of constraints.
2883 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2884 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2886 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2889 /* Drop all constraints in bmap that do not involve any of the dimensions
2890 * first to first + n - 1 of the given type.
2892 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2893 __isl_take isl_basic_map
*bmap
,
2894 enum isl_dim_type type
, unsigned first
, unsigned n
)
2899 isl_space
*space
= isl_basic_map_get_space(bmap
);
2900 isl_basic_map_free(bmap
);
2901 return isl_basic_map_universe(space
);
2903 bmap
= isl_basic_map_cow(bmap
);
2907 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2908 return isl_basic_map_free(bmap
);
2910 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2912 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2913 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2915 isl_basic_map_drop_equality(bmap
, i
);
2918 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2919 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2921 isl_basic_map_drop_inequality(bmap
, i
);
2924 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2928 /* Drop all constraints in bset that do not involve any of the dimensions
2929 * first to first + n - 1 of the given type.
2931 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2932 __isl_take isl_basic_set
*bset
,
2933 enum isl_dim_type type
, unsigned first
, unsigned n
)
2935 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2939 /* Drop all constraints in bmap that involve any of the dimensions
2940 * first to first + n - 1 of the given type.
2942 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2943 __isl_take isl_basic_map
*bmap
,
2944 enum isl_dim_type type
, unsigned first
, unsigned n
)
2951 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2952 return isl_basic_map_free(bmap
);
2954 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2955 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2956 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2957 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2961 /* Drop all constraints in bset that involve any of the dimensions
2962 * first to first + n - 1 of the given type.
2964 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2965 __isl_take isl_basic_set
*bset
,
2966 enum isl_dim_type type
, unsigned first
, unsigned n
)
2968 return isl_basic_map_drop_constraints_involving_dims(bset
,
2972 /* Drop constraints from "map" by applying "drop" to each basic map.
2974 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2975 enum isl_dim_type type
, unsigned first
, unsigned n
,
2976 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2977 enum isl_dim_type type
, unsigned first
, unsigned n
))
2981 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2982 return isl_map_free(map
);
2984 map
= isl_map_cow(map
);
2988 for (i
= 0; i
< map
->n
; ++i
) {
2989 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2991 return isl_map_free(map
);
2995 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3000 /* Drop all constraints in map that involve any of the dimensions
3001 * first to first + n - 1 of the given type.
3003 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3004 __isl_take isl_map
*map
,
3005 enum isl_dim_type type
, unsigned first
, unsigned n
)
3009 return drop_constraints(map
, type
, first
, n
,
3010 &isl_basic_map_drop_constraints_involving_dims
);
3013 /* Drop all constraints in "map" that do not involve any of the dimensions
3014 * first to first + n - 1 of the given type.
3016 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3017 __isl_take isl_map
*map
,
3018 enum isl_dim_type type
, unsigned first
, unsigned n
)
3021 isl_space
*space
= isl_map_get_space(map
);
3023 return isl_map_universe(space
);
3025 return drop_constraints(map
, type
, first
, n
,
3026 &isl_basic_map_drop_constraints_not_involving_dims
);
3029 /* Drop all constraints in set that involve any of the dimensions
3030 * first to first + n - 1 of the given type.
3032 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3033 __isl_take isl_set
*set
,
3034 enum isl_dim_type type
, unsigned first
, unsigned n
)
3036 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3039 /* Drop all constraints in "set" that do not involve any of the dimensions
3040 * first to first + n - 1 of the given type.
3042 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3043 __isl_take isl_set
*set
,
3044 enum isl_dim_type type
, unsigned first
, unsigned n
)
3046 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3049 /* Does local variable "div" of "bmap" have a complete explicit representation?
3050 * Having a complete explicit representation requires not only
3051 * an explicit representation, but also that all local variables
3052 * that appear in this explicit representation in turn have
3053 * a complete explicit representation.
3055 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3058 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3061 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3062 if (marked
< 0 || marked
)
3063 return isl_bool_not(marked
);
3065 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3068 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3070 known
= isl_basic_map_div_is_known(bmap
, i
);
3071 if (known
< 0 || !known
)
3075 return isl_bool_true
;
3078 /* Remove all divs that are unknown or defined in terms of unknown divs.
3080 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3081 __isl_take isl_basic_map
*bmap
)
3088 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3089 if (isl_basic_map_div_is_known(bmap
, i
))
3091 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3100 /* Remove all divs that are unknown or defined in terms of unknown divs.
3102 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3103 __isl_take isl_basic_set
*bset
)
3105 return isl_basic_map_remove_unknown_divs(bset
);
3108 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3117 map
= isl_map_cow(map
);
3121 for (i
= 0; i
< map
->n
; ++i
) {
3122 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3132 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3134 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3137 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3138 __isl_take isl_basic_set
*bset
,
3139 enum isl_dim_type type
, unsigned first
, unsigned n
)
3141 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3142 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3143 return bset_from_bmap(bmap
);
3146 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3147 enum isl_dim_type type
, unsigned first
, unsigned n
)
3154 map
= isl_map_cow(map
);
3155 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3156 return isl_map_free(map
);
3158 for (i
= 0; i
< map
->n
; ++i
) {
3159 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3160 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3164 map
= isl_map_drop(map
, type
, first
, n
);
3171 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3172 enum isl_dim_type type
, unsigned first
, unsigned n
)
3174 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3178 /* Project out n inputs starting at first using Fourier-Motzkin */
3179 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3180 unsigned first
, unsigned n
)
3182 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3185 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3186 FILE *out
, int indent
)
3191 fprintf(out
, "null basic set\n");
3195 fprintf(out
, "%*s", indent
, "");
3196 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3197 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3198 bset
->extra
, bset
->flags
);
3200 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3201 p
= isl_printer_set_dump(p
, 1);
3202 p
= isl_printer_set_indent(p
, indent
);
3203 p
= isl_printer_start_line(p
);
3204 p
= isl_printer_print_basic_set(p
, bset
);
3205 p
= isl_printer_end_line(p
);
3206 isl_printer_free(p
);
3209 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3210 FILE *out
, int indent
)
3215 fprintf(out
, "null basic map\n");
3219 fprintf(out
, "%*s", indent
, "");
3220 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3221 "flags: %x, n_name: %d\n",
3223 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3224 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3226 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3227 p
= isl_printer_set_dump(p
, 1);
3228 p
= isl_printer_set_indent(p
, indent
);
3229 p
= isl_printer_start_line(p
);
3230 p
= isl_printer_print_basic_map(p
, bmap
);
3231 p
= isl_printer_end_line(p
);
3232 isl_printer_free(p
);
3235 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3240 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3242 return isl_basic_map_free(bmap
);
3243 if (pos
>= bmap
->n_ineq
)
3244 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3245 "invalid position", return isl_basic_map_free(bmap
));
3246 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3247 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3248 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3249 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3253 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3256 if (isl_space_check_is_set(space
) < 0)
3258 return isl_map_alloc_space(space
, n
, flags
);
3260 isl_space_free(space
);
3264 /* Make sure "map" has room for at least "n" more basic maps.
3266 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3269 struct isl_map
*grown
= NULL
;
3273 isl_assert(map
->ctx
, n
>= 0, goto error
);
3274 if (map
->n
+ n
<= map
->size
)
3276 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3279 for (i
= 0; i
< map
->n
; ++i
) {
3280 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3288 isl_map_free(grown
);
3293 /* Make sure "set" has room for at least "n" more basic sets.
3295 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3297 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3300 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3302 return isl_map_from_basic_map(bset
);
3305 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3307 struct isl_map
*map
;
3312 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3313 return isl_map_add_basic_map(map
, bmap
);
3316 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3317 __isl_take isl_basic_set
*bset
)
3319 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3320 bset_to_bmap(bset
)));
3323 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3325 return isl_map_free(set
);
3328 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3333 fprintf(out
, "null set\n");
3337 fprintf(out
, "%*s", indent
, "");
3338 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3339 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3341 for (i
= 0; i
< set
->n
; ++i
) {
3342 fprintf(out
, "%*s", indent
, "");
3343 fprintf(out
, "basic set %d:\n", i
);
3344 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3348 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3353 fprintf(out
, "null map\n");
3357 fprintf(out
, "%*s", indent
, "");
3358 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3359 "flags: %x, n_name: %d\n",
3360 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3361 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3362 for (i
= 0; i
< map
->n
; ++i
) {
3363 fprintf(out
, "%*s", indent
, "");
3364 fprintf(out
, "basic map %d:\n", i
);
3365 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3369 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3370 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3372 struct isl_basic_map
*bmap_domain
;
3375 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3378 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3382 isl_assert(bset
->ctx
,
3383 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3385 bmap
= isl_basic_map_cow(bmap
);
3388 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3389 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3390 bmap_domain
= isl_basic_map_from_domain(bset
);
3391 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3393 bmap
= isl_basic_map_simplify(bmap
);
3394 return isl_basic_map_finalize(bmap
);
3396 isl_basic_map_free(bmap
);
3397 isl_basic_set_free(bset
);
3401 /* Check that the space of "bset" is the same as that of the range of "bmap".
3403 static isl_stat
isl_basic_map_check_compatible_range(
3404 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3408 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3410 return isl_stat_error
;
3412 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3413 "incompatible spaces", return isl_stat_error
);
3418 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3419 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3421 struct isl_basic_map
*bmap_range
;
3424 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3427 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3430 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3433 if (isl_basic_set_plain_is_universe(bset
)) {
3434 isl_basic_set_free(bset
);
3438 bmap
= isl_basic_map_cow(bmap
);
3441 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3442 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3443 bmap_range
= bset_to_bmap(bset
);
3444 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3446 bmap
= isl_basic_map_simplify(bmap
);
3447 return isl_basic_map_finalize(bmap
);
3449 isl_basic_map_free(bmap
);
3450 isl_basic_set_free(bset
);
3454 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3455 __isl_keep isl_vec
*vec
)
3461 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3462 if (total
< 0 || !vec
)
3463 return isl_bool_error
;
3465 if (1 + total
!= vec
->size
)
3466 return isl_bool_false
;
3470 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3471 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3472 if (!isl_int_is_zero(s
)) {
3474 return isl_bool_false
;
3478 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3479 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3480 if (isl_int_is_neg(s
)) {
3482 return isl_bool_false
;
3488 return isl_bool_true
;
3491 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3492 __isl_keep isl_vec
*vec
)
3494 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3497 __isl_give isl_basic_map
*isl_basic_map_intersect(
3498 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3500 struct isl_vec
*sample
= NULL
;
3501 isl_space
*space1
, *space2
;
3502 isl_size dim1
, dim2
, nparam1
, nparam2
;
3504 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3506 space1
= isl_basic_map_peek_space(bmap1
);
3507 space2
= isl_basic_map_peek_space(bmap2
);
3508 dim1
= isl_space_dim(space1
, isl_dim_all
);
3509 dim2
= isl_space_dim(space2
, isl_dim_all
);
3510 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3511 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3512 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3514 if (dim1
== nparam1
&& dim2
!= nparam2
)
3515 return isl_basic_map_intersect(bmap2
, bmap1
);
3517 if (dim2
!= nparam2
)
3518 isl_assert(bmap1
->ctx
,
3519 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3521 if (isl_basic_map_plain_is_empty(bmap1
)) {
3522 isl_basic_map_free(bmap2
);
3525 if (isl_basic_map_plain_is_empty(bmap2
)) {
3526 isl_basic_map_free(bmap1
);
3530 if (bmap1
->sample
&&
3531 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3532 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3533 sample
= isl_vec_copy(bmap1
->sample
);
3534 else if (bmap2
->sample
&&
3535 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3536 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3537 sample
= isl_vec_copy(bmap2
->sample
);
3539 bmap1
= isl_basic_map_cow(bmap1
);
3542 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3543 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3544 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3547 isl_vec_free(sample
);
3549 isl_vec_free(bmap1
->sample
);
3550 bmap1
->sample
= sample
;
3553 bmap1
= isl_basic_map_simplify(bmap1
);
3554 return isl_basic_map_finalize(bmap1
);
3557 isl_vec_free(sample
);
3558 isl_basic_map_free(bmap1
);
3559 isl_basic_map_free(bmap2
);
3563 struct isl_basic_set
*isl_basic_set_intersect(
3564 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3566 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3567 bset_to_bmap(bset2
)));
3570 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3571 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3573 return isl_basic_set_intersect(bset1
, bset2
);
3576 /* Special case of isl_map_intersect, where both map1 and map2
3577 * are convex, without any divs and such that either map1 or map2
3578 * contains a single constraint. This constraint is then simply
3579 * added to the other map.
3581 static __isl_give isl_map
*map_intersect_add_constraint(
3582 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3584 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3585 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3586 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3587 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3589 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3590 return isl_map_intersect(map2
, map1
);
3592 map1
= isl_map_cow(map1
);
3595 if (isl_map_plain_is_empty(map1
)) {
3599 if (map2
->p
[0]->n_eq
== 1)
3600 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3602 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3603 map2
->p
[0]->ineq
[0]);
3605 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3606 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3610 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3611 isl_basic_map_free(map1
->p
[0]);
3617 map1
= isl_map_unmark_normalized(map1
);
3625 /* map2 may be either a parameter domain or a map living in the same
3628 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3629 __isl_take isl_map
*map2
)
3635 isl_size dim2
, nparam2
;
3640 if ((isl_map_plain_is_empty(map1
) ||
3641 isl_map_plain_is_universe(map2
)) &&
3642 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3646 if ((isl_map_plain_is_empty(map2
) ||
3647 isl_map_plain_is_universe(map1
)) &&
3648 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3653 if (map1
->n
== 1 && map2
->n
== 1 &&
3654 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3655 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3656 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3657 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3658 return map_intersect_add_constraint(map1
, map2
);
3660 equal
= isl_map_plain_is_equal(map1
, map2
);
3668 dim2
= isl_map_dim(map2
, isl_dim_all
);
3669 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3670 if (dim2
< 0 || nparam2
< 0)
3672 if (dim2
!= nparam2
)
3673 isl_assert(map1
->ctx
,
3674 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3676 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3677 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3678 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3680 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3681 map1
->n
* map2
->n
, flags
);
3684 for (i
= 0; i
< map1
->n
; ++i
)
3685 for (j
= 0; j
< map2
->n
; ++j
) {
3686 struct isl_basic_map
*part
;
3687 part
= isl_basic_map_intersect(
3688 isl_basic_map_copy(map1
->p
[i
]),
3689 isl_basic_map_copy(map2
->p
[j
]));
3690 if (isl_basic_map_is_empty(part
) < 0)
3691 part
= isl_basic_map_free(part
);
3692 result
= isl_map_add_basic_map(result
, part
);
3705 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3706 __isl_take isl_map
*map2
)
3710 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3711 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3712 "spaces don't match", goto error
);
3713 return map_intersect_internal(map1
, map2
);
3720 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3721 __isl_take isl_map
*map2
)
3723 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3726 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3728 return set_from_map(isl_map_intersect(set_to_map(set1
),
3732 /* map_intersect_internal accepts intersections
3733 * with parameter domains, so we can just call that function.
3735 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3736 __isl_take isl_set
*params
)
3738 return map_intersect_internal(map
, params
);
3741 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3742 __isl_take isl_map
*map2
)
3744 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3747 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3748 __isl_take isl_set
*params
)
3750 return isl_map_intersect_params(set
, params
);
3753 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3761 bmap
= isl_basic_map_cow(bmap
);
3764 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3765 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3766 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3767 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3768 if (n1
< 0 || n2
< 0)
3769 bmap
= isl_basic_map_free(bmap
);
3770 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3771 return isl_basic_map_reset_space(bmap
, space
);
3774 static __isl_give isl_basic_map
*basic_map_space_reset(
3775 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3781 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3784 space
= isl_basic_map_get_space(bmap
);
3785 space
= isl_space_reset(space
, type
);
3786 bmap
= isl_basic_map_reset_space(bmap
, space
);
3790 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3791 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3792 unsigned pos
, unsigned n
)
3794 isl_bool rational
, is_empty
;
3795 isl_space
*res_space
;
3796 struct isl_basic_map
*res
;
3797 struct isl_dim_map
*dim_map
;
3800 enum isl_dim_type t
;
3803 return basic_map_space_reset(bmap
, type
);
3805 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3806 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3807 if (is_empty
< 0 || total
< 0)
3808 return isl_basic_map_free(bmap
);
3809 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3812 return isl_basic_map_free(bmap
);
3814 isl_basic_map_free(bmap
);
3815 return isl_basic_map_empty(res_space
);
3818 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
3820 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3824 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3826 isl_size size
= isl_basic_map_dim(bmap
, t
);
3828 dim_map
= isl_dim_map_free(dim_map
);
3829 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3831 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3832 pos
, size
- pos
, off
+ pos
+ n
);
3834 dim
= isl_space_dim(res_space
, t
);
3836 dim_map
= isl_dim_map_free(dim_map
);
3839 isl_dim_map_div(dim_map
, bmap
, off
);
3841 res
= isl_basic_map_alloc_space(res_space
,
3842 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3843 rational
= isl_basic_map_is_rational(bmap
);
3845 res
= isl_basic_map_free(res
);
3847 res
= isl_basic_map_set_rational(res
);
3848 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3849 return isl_basic_map_finalize(res
);
3852 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3853 __isl_take isl_basic_set
*bset
,
3854 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3856 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3859 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3860 enum isl_dim_type type
, unsigned n
)
3864 dim
= isl_basic_map_dim(bmap
, type
);
3866 return isl_basic_map_free(bmap
);
3867 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
3870 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3871 enum isl_dim_type type
, unsigned n
)
3875 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3876 return isl_basic_map_add_dims(bset
, type
, n
);
3878 isl_basic_set_free(bset
);
3882 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3883 enum isl_dim_type type
)
3887 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3890 space
= isl_map_get_space(map
);
3891 space
= isl_space_reset(space
, type
);
3892 map
= isl_map_reset_space(map
, space
);
3896 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3897 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3902 return map_space_reset(map
, type
);
3904 map
= isl_map_cow(map
);
3908 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3912 for (i
= 0; i
< map
->n
; ++i
) {
3913 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3924 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3925 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3927 return isl_map_insert_dims(set
, type
, pos
, n
);
3930 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3931 enum isl_dim_type type
, unsigned n
)
3935 dim
= isl_map_dim(map
, type
);
3937 return isl_map_free(map
);
3938 return isl_map_insert_dims(map
, type
, dim
, n
);
3941 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3942 enum isl_dim_type type
, unsigned n
)
3946 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3947 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3953 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3954 __isl_take isl_basic_map
*bmap
,
3955 enum isl_dim_type dst_type
, unsigned dst_pos
,
3956 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3959 struct isl_dim_map
*dim_map
;
3960 struct isl_basic_map
*res
;
3961 enum isl_dim_type t
;
3968 bmap
= isl_basic_map_reset(bmap
, src_type
);
3969 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3973 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3974 return isl_basic_map_free(bmap
);
3976 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3979 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3981 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3982 pos(bmap
->dim
, src_type
) + src_pos
+
3983 ((src_type
< dst_type
) ? n
: 0)) {
3984 bmap
= isl_basic_map_cow(bmap
);
3988 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3989 src_type
, src_pos
, n
);
3993 bmap
= isl_basic_map_finalize(bmap
);
3998 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4000 return isl_basic_map_free(bmap
);
4001 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4004 space
= isl_basic_map_peek_space(bmap
);
4005 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4006 isl_size size
= isl_space_dim(space
, t
);
4008 dim_map
= isl_dim_map_free(dim_map
);
4009 if (t
== dst_type
) {
4010 isl_dim_map_dim_range(dim_map
, space
, t
,
4013 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4016 isl_dim_map_dim_range(dim_map
, space
, t
,
4017 dst_pos
, size
- dst_pos
, off
);
4018 off
+= size
- dst_pos
;
4019 } else if (t
== src_type
) {
4020 isl_dim_map_dim_range(dim_map
, space
, t
,
4023 isl_dim_map_dim_range(dim_map
, space
, t
,
4024 src_pos
+ n
, size
- src_pos
- n
, off
);
4025 off
+= size
- src_pos
- n
;
4027 isl_dim_map_dim(dim_map
, space
, t
, off
);
4031 isl_dim_map_div(dim_map
, bmap
, off
);
4033 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4034 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4035 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4039 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4040 src_type
, src_pos
, n
);
4044 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4045 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4046 bmap
= isl_basic_map_finalize(bmap
);
4050 isl_basic_map_free(bmap
);
4054 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4055 enum isl_dim_type dst_type
, unsigned dst_pos
,
4056 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4058 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4059 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4060 src_type
, src_pos
, n
);
4061 return bset_from_bmap(bmap
);
4064 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4065 enum isl_dim_type dst_type
, unsigned dst_pos
,
4066 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4070 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4071 return set_from_map(isl_map_move_dims(set_to_map(set
),
4072 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4078 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4079 enum isl_dim_type dst_type
, unsigned dst_pos
,
4080 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4085 map
= isl_map_reset(map
, src_type
);
4086 map
= isl_map_reset(map
, dst_type
);
4090 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4091 return isl_map_free(map
);
4093 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4096 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4098 map
= isl_map_cow(map
);
4102 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4106 for (i
= 0; i
< map
->n
; ++i
) {
4107 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4109 src_type
, src_pos
, n
);
4120 /* Move the specified dimensions to the last columns right before
4121 * the divs. Don't change the dimension specification of bmap.
4122 * That's the responsibility of the caller.
4124 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4125 enum isl_dim_type type
, unsigned first
, unsigned n
)
4128 struct isl_dim_map
*dim_map
;
4129 struct isl_basic_map
*res
;
4130 enum isl_dim_type t
;
4136 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4137 isl_basic_map_offset(bmap
, isl_dim_div
))
4140 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4142 return isl_basic_map_free(bmap
);
4143 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4146 space
= isl_basic_map_peek_space(bmap
);
4147 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4148 isl_size size
= isl_space_dim(space
, t
);
4150 dim_map
= isl_dim_map_free(dim_map
);
4152 isl_dim_map_dim_range(dim_map
, space
, t
,
4155 isl_dim_map_dim_range(dim_map
, space
, t
,
4156 first
, n
, total
- bmap
->n_div
- n
);
4157 isl_dim_map_dim_range(dim_map
, space
, t
,
4158 first
+ n
, size
- (first
+ n
), off
);
4159 off
+= size
- (first
+ n
);
4161 isl_dim_map_dim(dim_map
, space
, t
, off
);
4165 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4167 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4168 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4169 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4173 /* Insert "n" rows in the divs of "bmap".
4175 * The number of columns is not changed, which means that the last
4176 * dimensions of "bmap" are being reintepreted as the new divs.
4177 * The space of "bmap" is not adjusted, however, which means
4178 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4179 * from the space of "bmap" is the responsibility of the caller.
4181 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4189 bmap
= isl_basic_map_cow(bmap
);
4193 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4194 old
= bmap
->block2
.data
;
4195 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4196 (bmap
->extra
+ n
) * (1 + row_size
));
4197 if (!bmap
->block2
.data
)
4198 return isl_basic_map_free(bmap
);
4199 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4201 return isl_basic_map_free(bmap
);
4202 for (i
= 0; i
< n
; ++i
) {
4203 new_div
[i
] = bmap
->block2
.data
+
4204 (bmap
->extra
+ i
) * (1 + row_size
);
4205 isl_seq_clr(new_div
[i
], 1 + row_size
);
4207 for (i
= 0; i
< bmap
->extra
; ++i
)
4208 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4210 bmap
->div
= new_div
;
4217 /* Drop constraints from "bmap" that only involve the variables
4218 * of "type" in the range [first, first + n] that are not related
4219 * to any of the variables outside that interval.
4220 * These constraints cannot influence the values for the variables
4221 * outside the interval, except in case they cause "bmap" to be empty.
4222 * Only drop the constraints if "bmap" is known to be non-empty.
4224 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4225 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4226 unsigned first
, unsigned n
)
4230 isl_size dim
, n_div
;
4233 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4235 return isl_basic_map_free(bmap
);
4239 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4240 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4241 if (dim
< 0 || n_div
< 0)
4242 return isl_basic_map_free(bmap
);
4243 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4245 return isl_basic_map_free(bmap
);
4246 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4247 for (i
= 0; i
< first
; ++i
)
4249 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4252 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4257 /* Turn the n dimensions of type type, starting at first
4258 * into existentially quantified variables.
4260 * If a subset of the projected out variables are unrelated
4261 * to any of the variables that remain, then the constraints
4262 * involving this subset are simply dropped first.
4264 __isl_give isl_basic_map
*isl_basic_map_project_out(
4265 __isl_take isl_basic_map
*bmap
,
4266 enum isl_dim_type type
, unsigned first
, unsigned n
)
4271 return basic_map_space_reset(bmap
, type
);
4272 if (type
== isl_dim_div
)
4273 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4274 "cannot project out existentially quantified variables",
4275 return isl_basic_map_free(bmap
));
4277 empty
= isl_basic_map_plain_is_empty(bmap
);
4279 return isl_basic_map_free(bmap
);
4281 bmap
= isl_basic_map_set_to_empty(bmap
);
4283 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4287 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4288 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4290 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4291 return isl_basic_map_free(bmap
);
4293 bmap
= move_last(bmap
, type
, first
, n
);
4294 bmap
= isl_basic_map_cow(bmap
);
4295 bmap
= insert_div_rows(bmap
, n
);
4299 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4302 bmap
= isl_basic_map_simplify(bmap
);
4303 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4304 return isl_basic_map_finalize(bmap
);
4306 isl_basic_map_free(bmap
);
4310 /* Turn the n dimensions of type type, starting at first
4311 * into existentially quantified variables.
4313 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4314 enum isl_dim_type type
, unsigned first
, unsigned n
)
4316 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4320 /* Turn the n dimensions of type type, starting at first
4321 * into existentially quantified variables.
4323 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4324 enum isl_dim_type type
, unsigned first
, unsigned n
)
4329 return map_space_reset(map
, type
);
4331 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4332 return isl_map_free(map
);
4334 map
= isl_map_cow(map
);
4338 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4342 for (i
= 0; i
< map
->n
; ++i
) {
4343 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4354 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4355 * into existentially quantified variables.
4357 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4358 enum isl_dim_type type
, unsigned first
, unsigned n
)
4362 dim
= isl_map_dim(map
, type
);
4363 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4364 return isl_map_free(map
);
4365 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4366 map
= isl_map_project_out(map
, type
, 0, first
);
4370 /* Turn the n dimensions of type type, starting at first
4371 * into existentially quantified variables.
4373 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4374 enum isl_dim_type type
, unsigned first
, unsigned n
)
4376 return set_from_map(isl_map_project_out(set_to_map(set
),
4380 /* Return a map that projects the elements in "set" onto their
4381 * "n" set dimensions starting at "first".
4382 * "type" should be equal to isl_dim_set.
4384 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4385 enum isl_dim_type type
, unsigned first
, unsigned n
)
4390 if (type
!= isl_dim_set
)
4391 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4392 "only set dimensions can be projected out", goto error
);
4393 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4394 return isl_set_free(set
);
4396 map
= isl_map_from_domain(set
);
4397 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4398 for (i
= 0; i
< n
; ++i
)
4399 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4407 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4413 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4415 return isl_basic_map_free(bmap
);
4416 for (i
= 0; i
< n
; ++i
) {
4417 j
= isl_basic_map_alloc_div(bmap
);
4420 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4424 isl_basic_map_free(bmap
);
4428 struct isl_basic_map
*isl_basic_map_apply_range(
4429 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4431 isl_space
*space_result
= NULL
;
4432 struct isl_basic_map
*bmap
;
4433 isl_size n_in
, n_out
, n
, nparam
;
4434 unsigned total
, pos
;
4435 struct isl_dim_map
*dim_map1
, *dim_map2
;
4437 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4439 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4440 bmap2
->dim
, isl_dim_in
))
4441 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4442 "spaces don't match", goto error
);
4444 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4445 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4446 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4447 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4448 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4451 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4452 isl_basic_map_get_space(bmap2
));
4454 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4455 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4456 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4457 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4458 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4459 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4460 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4461 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4462 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4463 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4464 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4466 bmap
= isl_basic_map_alloc_space(space_result
,
4467 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4468 bmap1
->n_eq
+ bmap2
->n_eq
,
4469 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4470 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4471 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4472 bmap
= add_divs(bmap
, n
);
4473 bmap
= isl_basic_map_simplify(bmap
);
4474 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4475 return isl_basic_map_finalize(bmap
);
4477 isl_basic_map_free(bmap1
);
4478 isl_basic_map_free(bmap2
);
4482 struct isl_basic_set
*isl_basic_set_apply(
4483 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4488 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4491 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4494 isl_basic_set_free(bset
);
4495 isl_basic_map_free(bmap
);
4499 struct isl_basic_map
*isl_basic_map_apply_domain(
4500 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4502 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4504 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4505 bmap2
->dim
, isl_dim_in
))
4506 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4507 "spaces don't match", goto error
);
4509 bmap1
= isl_basic_map_reverse(bmap1
);
4510 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4511 return isl_basic_map_reverse(bmap1
);
4513 isl_basic_map_free(bmap1
);
4514 isl_basic_map_free(bmap2
);
4518 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4519 * A \cap B -> f(A) + f(B)
4521 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4522 __isl_take isl_basic_map
*bmap2
)
4524 isl_size n_in
, n_out
, nparam
;
4525 unsigned total
, pos
;
4526 struct isl_basic_map
*bmap
= NULL
;
4527 struct isl_dim_map
*dim_map1
, *dim_map2
;
4530 if (!bmap1
|| !bmap2
)
4533 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4536 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4537 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4538 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4539 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4542 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4543 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4544 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4545 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4546 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4547 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4548 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4549 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4550 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4551 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4552 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4554 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4555 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4556 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4557 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4558 for (i
= 0; i
< n_out
; ++i
) {
4559 int j
= isl_basic_map_alloc_equality(bmap
);
4562 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4563 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4564 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4565 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4567 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4568 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4569 bmap
= add_divs(bmap
, 2 * n_out
);
4571 bmap
= isl_basic_map_simplify(bmap
);
4572 return isl_basic_map_finalize(bmap
);
4574 isl_basic_map_free(bmap
);
4575 isl_basic_map_free(bmap1
);
4576 isl_basic_map_free(bmap2
);
4580 /* Given two maps A -> f(A) and B -> g(B), construct a map
4581 * A \cap B -> f(A) + f(B)
4583 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4584 __isl_take isl_map
*map2
)
4586 struct isl_map
*result
;
4592 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4594 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4595 map1
->n
* map2
->n
, 0);
4598 for (i
= 0; i
< map1
->n
; ++i
)
4599 for (j
= 0; j
< map2
->n
; ++j
) {
4600 struct isl_basic_map
*part
;
4601 part
= isl_basic_map_sum(
4602 isl_basic_map_copy(map1
->p
[i
]),
4603 isl_basic_map_copy(map2
->p
[j
]));
4604 if (isl_basic_map_is_empty(part
))
4605 isl_basic_map_free(part
);
4607 result
= isl_map_add_basic_map(result
, part
);
4620 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4621 __isl_take isl_set
*set2
)
4623 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4626 /* Given a basic map A -> f(A), construct A -> -f(A).
4628 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4634 bmap
= isl_basic_map_cow(bmap
);
4635 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4637 return isl_basic_map_free(bmap
);
4639 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4640 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4641 for (j
= 0; j
< n
; ++j
)
4642 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4643 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4644 for (j
= 0; j
< n
; ++j
)
4645 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4646 for (i
= 0; i
< bmap
->n_div
; ++i
)
4647 for (j
= 0; j
< n
; ++j
)
4648 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4649 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4650 return isl_basic_map_finalize(bmap
);
4653 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4655 return isl_basic_map_neg(bset
);
4658 /* Given a map A -> f(A), construct A -> -f(A).
4660 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4664 map
= isl_map_cow(map
);
4668 for (i
= 0; i
< map
->n
; ++i
) {
4669 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4680 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4682 return set_from_map(isl_map_neg(set_to_map(set
)));
4685 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4686 * A -> floor(f(A)/d).
4688 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4691 isl_size n_in
, n_out
, nparam
;
4692 unsigned total
, pos
;
4693 struct isl_basic_map
*result
= NULL
;
4694 struct isl_dim_map
*dim_map
;
4697 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4698 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4699 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4700 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4701 return isl_basic_map_free(bmap
);
4703 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4704 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4705 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4706 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4707 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4708 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4710 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4711 bmap
->n_div
+ n_out
,
4712 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4713 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4714 result
= add_divs(result
, n_out
);
4715 for (i
= 0; i
< n_out
; ++i
) {
4717 j
= isl_basic_map_alloc_inequality(result
);
4720 isl_seq_clr(result
->ineq
[j
], 1+total
);
4721 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4722 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4723 j
= isl_basic_map_alloc_inequality(result
);
4726 isl_seq_clr(result
->ineq
[j
], 1+total
);
4727 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4728 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4729 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4732 result
= isl_basic_map_simplify(result
);
4733 return isl_basic_map_finalize(result
);
4735 isl_basic_map_free(result
);
4739 /* Given a map A -> f(A) and an integer d, construct a map
4740 * A -> floor(f(A)/d).
4742 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4746 map
= isl_map_cow(map
);
4750 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4751 for (i
= 0; i
< map
->n
; ++i
) {
4752 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4756 map
= isl_map_unmark_normalized(map
);
4764 /* Given a map A -> f(A) and an integer d, construct a map
4765 * A -> floor(f(A)/d).
4767 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4768 __isl_take isl_val
*d
)
4772 if (!isl_val_is_int(d
))
4773 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4774 "expecting integer denominator", goto error
);
4775 map
= isl_map_floordiv(map
, d
->n
);
4784 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4792 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4793 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4794 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4795 if (total
< 0 || nparam
< 0 || n_in
< 0)
4796 return isl_basic_map_free(bmap
);
4797 i
= isl_basic_map_alloc_equality(bmap
);
4800 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
4801 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4802 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4803 return isl_basic_map_finalize(bmap
);
4805 isl_basic_map_free(bmap
);
4809 /* Add a constraint to "bmap" expressing i_pos < o_pos
4811 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4819 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4820 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4821 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4822 if (total
< 0 || nparam
< 0 || n_in
< 0)
4823 return isl_basic_map_free(bmap
);
4824 i
= isl_basic_map_alloc_inequality(bmap
);
4827 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4828 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4829 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4830 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4831 return isl_basic_map_finalize(bmap
);
4833 isl_basic_map_free(bmap
);
4837 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4839 static __isl_give isl_basic_map
*var_less_or_equal(
4840 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4847 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4848 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4849 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4850 if (total
< 0 || nparam
< 0 || n_in
< 0)
4851 return isl_basic_map_free(bmap
);
4852 i
= isl_basic_map_alloc_inequality(bmap
);
4855 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4856 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4857 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4858 return isl_basic_map_finalize(bmap
);
4860 isl_basic_map_free(bmap
);
4864 /* Add a constraint to "bmap" expressing i_pos > o_pos
4866 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4874 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4875 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4876 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4877 if (total
< 0 || nparam
< 0 || n_in
< 0)
4878 return isl_basic_map_free(bmap
);
4879 i
= isl_basic_map_alloc_inequality(bmap
);
4882 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4883 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4884 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4885 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4886 return isl_basic_map_finalize(bmap
);
4888 isl_basic_map_free(bmap
);
4892 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4894 static __isl_give isl_basic_map
*var_more_or_equal(
4895 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4902 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4903 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4904 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4905 if (total
< 0 || nparam
< 0 || n_in
< 0)
4906 return isl_basic_map_free(bmap
);
4907 i
= isl_basic_map_alloc_inequality(bmap
);
4910 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4911 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4912 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4913 return isl_basic_map_finalize(bmap
);
4915 isl_basic_map_free(bmap
);
4919 __isl_give isl_basic_map
*isl_basic_map_equal(
4920 __isl_take isl_space
*space
, unsigned n_equal
)
4923 struct isl_basic_map
*bmap
;
4924 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4927 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4928 bmap
= var_equal(bmap
, i
);
4929 return isl_basic_map_finalize(bmap
);
4932 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4934 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4938 struct isl_basic_map
*bmap
;
4939 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4942 for (i
= 0; i
< pos
&& bmap
; ++i
)
4943 bmap
= var_equal(bmap
, i
);
4945 bmap
= var_less(bmap
, pos
);
4946 return isl_basic_map_finalize(bmap
);
4949 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4951 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4952 __isl_take isl_space
*space
, unsigned pos
)
4955 isl_basic_map
*bmap
;
4957 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4958 for (i
= 0; i
< pos
; ++i
)
4959 bmap
= var_equal(bmap
, i
);
4960 bmap
= var_less_or_equal(bmap
, pos
);
4961 return isl_basic_map_finalize(bmap
);
4964 /* Return a relation on "space" expressing i_pos > o_pos
4966 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4970 struct isl_basic_map
*bmap
;
4971 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4974 for (i
= 0; i
< pos
&& bmap
; ++i
)
4975 bmap
= var_equal(bmap
, i
);
4977 bmap
= var_more(bmap
, pos
);
4978 return isl_basic_map_finalize(bmap
);
4981 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4983 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4984 __isl_take isl_space
*space
, unsigned pos
)
4987 isl_basic_map
*bmap
;
4989 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4990 for (i
= 0; i
< pos
; ++i
)
4991 bmap
= var_equal(bmap
, i
);
4992 bmap
= var_more_or_equal(bmap
, pos
);
4993 return isl_basic_map_finalize(bmap
);
4996 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4997 unsigned n
, int equal
)
4999 struct isl_map
*map
;
5002 if (n
== 0 && equal
)
5003 return isl_map_universe(space
);
5005 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5007 for (i
= 0; i
+ 1 < n
; ++i
)
5008 map
= isl_map_add_basic_map(map
,
5009 isl_basic_map_less_at(isl_space_copy(space
), i
));
5012 map
= isl_map_add_basic_map(map
,
5013 isl_basic_map_less_or_equal_at(space
, n
- 1));
5015 map
= isl_map_add_basic_map(map
,
5016 isl_basic_map_less_at(space
, n
- 1));
5018 isl_space_free(space
);
5023 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5027 return map_lex_lte_first(space
, space
->n_out
, equal
);
5030 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5032 return map_lex_lte_first(dim
, n
, 0);
5035 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5037 return map_lex_lte_first(dim
, n
, 1);
5040 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5042 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5045 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5047 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5050 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5051 unsigned n
, int equal
)
5053 struct isl_map
*map
;
5056 if (n
== 0 && equal
)
5057 return isl_map_universe(space
);
5059 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5061 for (i
= 0; i
+ 1 < n
; ++i
)
5062 map
= isl_map_add_basic_map(map
,
5063 isl_basic_map_more_at(isl_space_copy(space
), i
));
5066 map
= isl_map_add_basic_map(map
,
5067 isl_basic_map_more_or_equal_at(space
, n
- 1));
5069 map
= isl_map_add_basic_map(map
,
5070 isl_basic_map_more_at(space
, n
- 1));
5072 isl_space_free(space
);
5077 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5081 return map_lex_gte_first(space
, space
->n_out
, equal
);
5084 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5086 return map_lex_gte_first(dim
, n
, 0);
5089 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5091 return map_lex_gte_first(dim
, n
, 1);
5094 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5096 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5099 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5101 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5104 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5105 __isl_take isl_set
*set2
)
5108 map
= isl_map_lex_le(isl_set_get_space(set1
));
5109 map
= isl_map_intersect_domain(map
, set1
);
5110 map
= isl_map_intersect_range(map
, set2
);
5114 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5115 __isl_take isl_set
*set2
)
5118 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5119 map
= isl_map_intersect_domain(map
, set1
);
5120 map
= isl_map_intersect_range(map
, set2
);
5124 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5125 __isl_take isl_set
*set2
)
5128 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5129 map
= isl_map_intersect_domain(map
, set1
);
5130 map
= isl_map_intersect_range(map
, set2
);
5134 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5135 __isl_take isl_set
*set2
)
5138 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5139 map
= isl_map_intersect_domain(map
, set1
);
5140 map
= isl_map_intersect_range(map
, set2
);
5144 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5145 __isl_take isl_map
*map2
)
5148 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5149 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5150 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5154 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5155 __isl_take isl_map
*map2
)
5158 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5159 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5160 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5164 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5165 __isl_take isl_map
*map2
)
5168 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5169 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5170 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5174 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5175 __isl_take isl_map
*map2
)
5178 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5179 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5180 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5184 /* For the div d = floor(f/m) at position "div", add the constraint
5188 static __isl_give isl_basic_map
*add_upper_div_constraint(
5189 __isl_take isl_basic_map
*bmap
, unsigned div
)
5192 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5196 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5197 if (v_div
< 0 || n_div
< 0)
5198 return isl_basic_map_free(bmap
);
5200 i
= isl_basic_map_alloc_inequality(bmap
);
5202 return isl_basic_map_free(bmap
);
5203 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5204 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5209 /* For the div d = floor(f/m) at position "div", add the constraint
5211 * -(f-(m-1)) + m d >= 0
5213 static __isl_give isl_basic_map
*add_lower_div_constraint(
5214 __isl_take isl_basic_map
*bmap
, unsigned div
)
5217 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5221 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5222 if (v_div
< 0 || n_div
< 0)
5223 return isl_basic_map_free(bmap
);
5225 i
= isl_basic_map_alloc_inequality(bmap
);
5227 return isl_basic_map_free(bmap
);
5228 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5229 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5230 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5231 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5236 /* For the div d = floor(f/m) at position "pos", add the constraints
5239 * -(f-(m-1)) + m d >= 0
5241 * Note that the second constraint is the negation of
5245 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5246 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5248 bmap
= add_upper_div_constraint(bmap
, pos
);
5249 bmap
= add_lower_div_constraint(bmap
, pos
);
5253 /* For each known div d = floor(f/m), add the constraints
5256 * -(f-(m-1)) + m d >= 0
5258 * Remove duplicate constraints in case of some these div constraints
5259 * already appear in "bmap".
5261 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5262 __isl_take isl_basic_map
*bmap
)
5266 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5268 return isl_basic_map_free(bmap
);
5272 bmap
= add_known_div_constraints(bmap
);
5273 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5274 bmap
= isl_basic_map_finalize(bmap
);
5278 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5280 * In particular, if this div is of the form d = floor(f/m),
5281 * then add the constraint
5285 * if sign < 0 or the constraint
5287 * -(f-(m-1)) + m d >= 0
5291 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5292 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5295 return add_upper_div_constraint(bmap
, div
);
5297 return add_lower_div_constraint(bmap
, div
);
5300 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5301 __isl_take isl_basic_map
*bmap
)
5305 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5307 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5308 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5309 return bset_from_bmap(bmap
);
5310 bmap
= isl_basic_map_cow(bmap
);
5313 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5316 bmap
->extra
-= bmap
->n_div
;
5318 bmap
= isl_basic_map_finalize(bmap
);
5319 return bset_from_bmap(bmap
);
5321 isl_basic_map_free(bmap
);
5325 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5326 __isl_take isl_basic_set
*bset
)
5328 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5331 /* Replace each element in "list" by the result of applying
5332 * isl_basic_map_underlying_set to the element.
5334 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5335 __isl_take isl_basic_map_list
*list
)
5340 n
= isl_basic_map_list_n_basic_map(list
);
5344 for (i
= 0; i
< n
; ++i
) {
5345 isl_basic_map
*bmap
;
5346 isl_basic_set
*bset
;
5348 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5349 bset
= isl_basic_set_underlying_set(bmap
);
5350 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5355 isl_basic_map_list_free(list
);
5359 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5360 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5362 struct isl_basic_map
*bmap
;
5363 struct isl_ctx
*ctx
;
5364 isl_size dim
, bmap_total
;
5371 if (isl_basic_set_check_no_params(bset
) < 0 ||
5372 isl_basic_set_check_no_locals(bset
) < 0)
5374 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5375 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5376 if (dim
< 0 || bmap_total
< 0)
5378 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5379 if (like
->n_div
== 0) {
5380 isl_space
*space
= isl_basic_map_get_space(like
);
5381 isl_basic_map_free(like
);
5382 return isl_basic_map_reset_space(bset
, space
);
5384 bset
= isl_basic_set_cow(bset
);
5387 total
= dim
+ bset
->extra
;
5388 bmap
= bset_to_bmap(bset
);
5389 isl_space_free(bmap
->dim
);
5390 bmap
->dim
= isl_space_copy(like
->dim
);
5393 bmap
->n_div
= like
->n_div
;
5394 bmap
->extra
+= like
->n_div
;
5398 ltotal
= total
- bmap
->extra
+ like
->extra
;
5401 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5402 bmap
->extra
* (1 + 1 + total
));
5403 if (isl_blk_is_error(bmap
->block2
))
5405 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5409 for (i
= 0; i
< bmap
->extra
; ++i
)
5410 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5411 for (i
= 0; i
< like
->n_div
; ++i
) {
5412 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5413 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5415 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5417 isl_basic_map_free(like
);
5418 bmap
= isl_basic_map_simplify(bmap
);
5419 bmap
= isl_basic_map_finalize(bmap
);
5422 isl_basic_map_free(like
);
5423 isl_basic_set_free(bset
);
5427 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5428 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5430 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5431 bset_to_bmap(like
)));
5434 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5438 map
= isl_map_cow(map
);
5441 map
->dim
= isl_space_cow(map
->dim
);
5445 for (i
= 1; i
< map
->n
; ++i
)
5446 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5448 for (i
= 0; i
< map
->n
; ++i
) {
5449 map
->p
[i
] = bset_to_bmap(
5450 isl_basic_map_underlying_set(map
->p
[i
]));
5455 map
->dim
= isl_space_underlying(map
->dim
, 0);
5457 isl_space_free(map
->dim
);
5458 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5462 return set_from_map(map
);
5468 /* Replace the space of "bmap" by "space".
5470 * If the space of "bmap" is identical to "space" (including the identifiers
5471 * of the input and output dimensions), then simply return the original input.
5473 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5474 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5477 isl_space
*bmap_space
;
5479 bmap_space
= isl_basic_map_peek_space(bmap
);
5480 equal
= isl_space_is_equal(bmap_space
, space
);
5481 if (equal
>= 0 && equal
)
5482 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5486 isl_space_free(space
);
5489 bmap
= isl_basic_map_cow(bmap
);
5490 if (!bmap
|| !space
)
5493 isl_space_free(bmap
->dim
);
5496 bmap
= isl_basic_map_finalize(bmap
);
5500 isl_basic_map_free(bmap
);
5501 isl_space_free(space
);
5505 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5506 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5508 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5512 /* Check that the total dimensions of "map" and "space" are the same.
5514 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5515 __isl_keep isl_space
*space
)
5517 isl_size dim1
, dim2
;
5519 dim1
= isl_map_dim(map
, isl_dim_all
);
5520 dim2
= isl_space_dim(space
, isl_dim_all
);
5521 if (dim1
< 0 || dim2
< 0)
5522 return isl_stat_error
;
5525 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5526 "total dimensions do not match", return isl_stat_error
);
5529 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5530 __isl_take isl_space
*space
)
5534 map
= isl_map_cow(map
);
5538 for (i
= 0; i
< map
->n
; ++i
) {
5539 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5540 isl_space_copy(space
));
5544 isl_space_free(map
->dim
);
5550 isl_space_free(space
);
5554 /* Replace the space of "map" by "space", without modifying
5555 * the dimension of "map".
5557 * If the space of "map" is identical to "space" (including the identifiers
5558 * of the input and output dimensions), then simply return the original input.
5560 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5561 __isl_take isl_space
*space
)
5564 isl_space
*map_space
;
5566 map_space
= isl_map_peek_space(map
);
5567 equal
= isl_space_is_equal(map_space
, space
);
5568 if (equal
>= 0 && equal
)
5569 equal
= isl_space_has_equal_ids(map_space
, space
);
5573 isl_space_free(space
);
5576 if (check_map_space_equal_total_dim(map
, space
) < 0)
5578 return isl_map_reset_space(map
, space
);
5581 isl_space_free(space
);
5585 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5586 __isl_take isl_space
*dim
)
5588 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5591 /* Compute the parameter domain of the given basic set.
5593 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5599 is_params
= isl_basic_set_is_params(bset
);
5601 return isl_basic_set_free(bset
);
5605 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5607 return isl_basic_set_free(bset
);
5608 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5609 space
= isl_basic_set_get_space(bset
);
5610 space
= isl_space_params(space
);
5611 bset
= isl_basic_set_reset_space(bset
, space
);
5615 /* Construct a zero-dimensional basic set with the given parameter domain.
5617 __isl_give isl_basic_set
*isl_basic_set_from_params(
5618 __isl_take isl_basic_set
*bset
)
5621 space
= isl_basic_set_get_space(bset
);
5622 space
= isl_space_set_from_params(space
);
5623 bset
= isl_basic_set_reset_space(bset
, space
);
5627 /* Compute the parameter domain of the given set.
5629 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5631 return isl_map_params(set_to_map(set
));
5634 /* Construct a zero-dimensional set with the given parameter domain.
5636 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5639 space
= isl_set_get_space(set
);
5640 space
= isl_space_set_from_params(space
);
5641 set
= isl_set_reset_space(set
, space
);
5645 /* Compute the parameter domain of the given map.
5647 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5650 isl_size n_in
, n_out
;
5652 n_in
= isl_map_dim(map
, isl_dim_in
);
5653 n_out
= isl_map_dim(map
, isl_dim_out
);
5654 if (n_in
< 0 || n_out
< 0)
5655 return isl_map_free(map
);
5656 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5657 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5658 space
= isl_map_get_space(map
);
5659 space
= isl_space_params(space
);
5660 map
= isl_map_reset_space(map
, space
);
5664 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5669 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5671 return isl_basic_map_free(bmap
);
5672 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5674 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5676 return isl_basic_map_reset_space(bmap
, space
);
5679 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5682 return isl_bool_error
;
5683 return isl_space_may_be_set(bmap
->dim
);
5686 /* Is this basic map actually a set?
5687 * Users should never call this function. Outside of isl,
5688 * the type should indicate whether something is a set or a map.
5690 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5693 return isl_bool_error
;
5694 return isl_space_is_set(bmap
->dim
);
5697 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5701 is_set
= isl_basic_map_is_set(bmap
);
5706 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5708 isl_basic_map_free(bmap
);
5712 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5713 __isl_take isl_basic_map
*bmap
)
5717 isl_basic_map
*domain
;
5718 isl_size nparam
, n_in
, n_out
;
5720 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5721 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5722 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5723 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5724 return isl_basic_map_free(bmap
);
5726 space
= isl_basic_map_get_space(bmap
);
5727 space
= isl_space_from_range(isl_space_domain(space
));
5728 domain
= isl_basic_map_universe(space
);
5730 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5731 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5732 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5734 for (i
= 0; i
< n_in
; ++i
)
5735 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5738 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5739 return isl_basic_map_finalize(bmap
);
5742 __isl_give isl_basic_map
*isl_basic_map_range_map(
5743 __isl_take isl_basic_map
*bmap
)
5747 isl_basic_map
*range
;
5748 isl_size nparam
, n_in
, n_out
;
5750 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5751 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5752 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5753 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5754 return isl_basic_map_free(bmap
);
5756 space
= isl_basic_map_get_space(bmap
);
5757 space
= isl_space_from_range(isl_space_range(space
));
5758 range
= isl_basic_map_universe(space
);
5760 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5761 bmap
= isl_basic_map_apply_range(bmap
, range
);
5762 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5764 for (i
= 0; i
< n_out
; ++i
)
5765 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5768 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5769 return isl_basic_map_finalize(bmap
);
5772 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5776 return isl_space_may_be_set(map
->dim
);
5779 /* Is this map actually a set?
5780 * Users should never call this function. Outside of isl,
5781 * the type should indicate whether something is a set or a map.
5783 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5786 return isl_bool_error
;
5787 return isl_space_is_set(map
->dim
);
5790 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5794 struct isl_set
*set
;
5796 is_set
= isl_map_is_set(map
);
5800 return set_from_map(map
);
5802 map
= isl_map_cow(map
);
5806 set
= set_from_map(map
);
5807 set
->dim
= isl_space_range(set
->dim
);
5810 for (i
= 0; i
< map
->n
; ++i
) {
5811 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5815 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5816 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5823 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5827 map
= isl_map_cow(map
);
5831 map
->dim
= isl_space_domain_map(map
->dim
);
5834 for (i
= 0; i
< map
->n
; ++i
) {
5835 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5839 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5840 map
= isl_map_unmark_normalized(map
);
5847 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5850 isl_space
*range_dim
;
5852 map
= isl_map_cow(map
);
5856 range_dim
= isl_space_range(isl_map_get_space(map
));
5857 range_dim
= isl_space_from_range(range_dim
);
5858 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5859 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5862 for (i
= 0; i
< map
->n
; ++i
) {
5863 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5867 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5868 map
= isl_map_unmark_normalized(map
);
5875 /* Given a wrapped map of the form A[B -> C],
5876 * return the map A[B -> C] -> B.
5878 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5885 if (!isl_set_has_tuple_id(set
))
5886 return isl_map_domain_map(isl_set_unwrap(set
));
5888 id
= isl_set_get_tuple_id(set
);
5889 map
= isl_map_domain_map(isl_set_unwrap(set
));
5890 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5895 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5896 __isl_take isl_basic_set
*bset
)
5898 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5901 __isl_give isl_basic_map
*isl_basic_map_from_range(
5902 __isl_take isl_basic_set
*bset
)
5905 space
= isl_basic_set_get_space(bset
);
5906 space
= isl_space_from_range(space
);
5907 bset
= isl_basic_set_reset_space(bset
, space
);
5908 return bset_to_bmap(bset
);
5911 /* Create a relation with the given set as range.
5912 * The domain of the created relation is a zero-dimensional
5913 * flat anonymous space.
5915 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5918 space
= isl_set_get_space(set
);
5919 space
= isl_space_from_range(space
);
5920 set
= isl_set_reset_space(set
, space
);
5921 return set_to_map(set
);
5924 /* Create a relation with the given set as domain.
5925 * The range of the created relation is a zero-dimensional
5926 * flat anonymous space.
5928 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5930 return isl_map_reverse(isl_map_from_range(set
));
5933 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5934 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5936 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5939 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5940 __isl_take isl_set
*range
)
5942 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5945 /* Return a newly allocated isl_map with given space and flags and
5946 * room for "n" basic maps.
5947 * Make sure that all cached information is cleared.
5949 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5952 struct isl_map
*map
;
5957 isl_die(space
->ctx
, isl_error_internal
,
5958 "negative number of basic maps", goto error
);
5959 map
= isl_calloc(space
->ctx
, struct isl_map
,
5960 sizeof(struct isl_map
) +
5961 (n
- 1) * sizeof(struct isl_basic_map
*));
5965 map
->ctx
= space
->ctx
;
5966 isl_ctx_ref(map
->ctx
);
5974 isl_space_free(space
);
5978 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5980 struct isl_basic_map
*bmap
;
5981 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5982 bmap
= isl_basic_map_set_to_empty(bmap
);
5986 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5988 struct isl_basic_set
*bset
;
5989 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5990 bset
= isl_basic_set_set_to_empty(bset
);
5994 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5996 struct isl_basic_map
*bmap
;
5997 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5998 bmap
= isl_basic_map_finalize(bmap
);
6002 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6004 struct isl_basic_set
*bset
;
6005 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6006 bset
= isl_basic_set_finalize(bset
);
6010 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6011 __isl_take isl_space
*space
)
6014 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6015 isl_basic_map
*bmap
;
6018 space
= isl_space_free(space
);
6019 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6020 for (i
= 0; i
< total
; ++i
) {
6021 int k
= isl_basic_map_alloc_inequality(bmap
);
6024 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6025 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6029 isl_basic_map_free(bmap
);
6033 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6034 __isl_take isl_space
*space
)
6036 return isl_basic_map_nat_universe(space
);
6039 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6041 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6044 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6046 return isl_map_nat_universe(dim
);
6049 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6051 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6054 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6056 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6059 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6061 struct isl_map
*map
;
6064 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6065 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6069 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6071 struct isl_set
*set
;
6074 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6075 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6079 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6082 struct isl_map
*dup
;
6086 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6087 for (i
= 0; i
< map
->n
; ++i
)
6088 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6092 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6093 __isl_take isl_basic_map
*bmap
)
6097 if (isl_basic_map_plain_is_empty(bmap
)) {
6098 isl_basic_map_free(bmap
);
6101 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6102 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6103 map
->p
[map
->n
] = bmap
;
6105 map
= isl_map_unmark_normalized(map
);
6111 isl_basic_map_free(bmap
);
6115 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6126 isl_ctx_deref(map
->ctx
);
6127 for (i
= 0; i
< map
->n
; ++i
)
6128 isl_basic_map_free(map
->p
[i
]);
6129 isl_space_free(map
->dim
);
6135 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6136 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6141 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6143 return isl_basic_map_free(bmap
);
6145 bmap
= isl_basic_map_cow(bmap
);
6146 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6147 j
= isl_basic_map_alloc_equality(bmap
);
6150 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6151 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6152 isl_int_set_si(bmap
->eq
[j
][0], value
);
6153 bmap
= isl_basic_map_simplify(bmap
);
6154 return isl_basic_map_finalize(bmap
);
6156 isl_basic_map_free(bmap
);
6160 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6161 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6166 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6168 return isl_basic_map_free(bmap
);
6170 bmap
= isl_basic_map_cow(bmap
);
6171 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6172 j
= isl_basic_map_alloc_equality(bmap
);
6175 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6176 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6177 isl_int_set(bmap
->eq
[j
][0], value
);
6178 bmap
= isl_basic_map_simplify(bmap
);
6179 return isl_basic_map_finalize(bmap
);
6181 isl_basic_map_free(bmap
);
6185 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6186 enum isl_dim_type type
, unsigned pos
, int value
)
6188 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6189 return isl_basic_map_free(bmap
);
6190 return isl_basic_map_fix_pos_si(bmap
,
6191 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6194 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6195 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6197 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6198 return isl_basic_map_free(bmap
);
6199 return isl_basic_map_fix_pos(bmap
,
6200 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6203 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6204 * to be equal to "v".
6206 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6207 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6211 if (!isl_val_is_int(v
))
6212 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6213 "expecting integer value", goto error
);
6214 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6216 pos
+= isl_basic_map_offset(bmap
, type
);
6217 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6221 isl_basic_map_free(bmap
);
6226 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6227 * to be equal to "v".
6229 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6230 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6232 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6235 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6236 enum isl_dim_type type
, unsigned pos
, int value
)
6238 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6242 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6243 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6245 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6249 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6250 unsigned input
, int value
)
6252 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6255 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6256 unsigned dim
, int value
)
6258 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6259 isl_dim_set
, dim
, value
));
6262 /* Remove the basic map at position "i" from "map" if this basic map
6263 * is (obviously) empty.
6265 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6272 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6274 return isl_map_free(map
);
6278 isl_basic_map_free(map
->p
[i
]);
6281 map
->p
[i
] = map
->p
[map
->n
];
6282 map
= isl_map_unmark_normalized(map
);
6289 /* Perform "fn" on each basic map of "map", where we may not be holding
6290 * the only reference to "map".
6291 * In particular, "fn" should be a semantics preserving operation
6292 * that we want to apply to all copies of "map". We therefore need
6293 * to be careful not to modify "map" in a way that breaks "map"
6294 * in case anything goes wrong.
6296 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6297 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6299 struct isl_basic_map
*bmap
;
6305 for (i
= map
->n
- 1; i
>= 0; --i
) {
6306 bmap
= isl_basic_map_copy(map
->p
[i
]);
6310 isl_basic_map_free(map
->p
[i
]);
6312 map
= remove_if_empty(map
, i
);
6323 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6324 enum isl_dim_type type
, unsigned pos
, int value
)
6328 map
= isl_map_cow(map
);
6329 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6330 return isl_map_free(map
);
6331 for (i
= map
->n
- 1; i
>= 0; --i
) {
6332 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6333 map
= remove_if_empty(map
, i
);
6337 map
= isl_map_unmark_normalized(map
);
6341 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6342 enum isl_dim_type type
, unsigned pos
, int value
)
6344 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6347 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6348 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6352 map
= isl_map_cow(map
);
6353 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6354 return isl_map_free(map
);
6355 for (i
= 0; i
< map
->n
; ++i
) {
6356 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6360 map
= isl_map_unmark_normalized(map
);
6367 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6368 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6370 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6373 /* Fix the value of the variable at position "pos" of type "type" of "map"
6374 * to be equal to "v".
6376 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6377 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6381 map
= isl_map_cow(map
);
6385 if (!isl_val_is_int(v
))
6386 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6387 "expecting integer value", goto error
);
6388 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6390 for (i
= map
->n
- 1; i
>= 0; --i
) {
6391 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6393 map
= remove_if_empty(map
, i
);
6397 map
= isl_map_unmark_normalized(map
);
6406 /* Fix the value of the variable at position "pos" of type "type" of "set"
6407 * to be equal to "v".
6409 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6410 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6412 return isl_map_fix_val(set
, type
, pos
, v
);
6415 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6416 unsigned input
, int value
)
6418 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6421 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6423 return set_from_map(isl_map_fix_si(set_to_map(set
),
6424 isl_dim_set
, dim
, value
));
6427 static __isl_give isl_basic_map
*basic_map_bound_si(
6428 __isl_take isl_basic_map
*bmap
,
6429 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6434 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6435 return isl_basic_map_free(bmap
);
6436 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6438 return isl_basic_map_free(bmap
);
6439 pos
+= isl_basic_map_offset(bmap
, type
);
6440 bmap
= isl_basic_map_cow(bmap
);
6441 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6442 j
= isl_basic_map_alloc_inequality(bmap
);
6445 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6447 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6448 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6450 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6451 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6453 bmap
= isl_basic_map_simplify(bmap
);
6454 return isl_basic_map_finalize(bmap
);
6456 isl_basic_map_free(bmap
);
6460 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6461 __isl_take isl_basic_map
*bmap
,
6462 enum isl_dim_type type
, unsigned pos
, int value
)
6464 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6467 /* Constrain the values of the given dimension to be no greater than "value".
6469 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6470 __isl_take isl_basic_map
*bmap
,
6471 enum isl_dim_type type
, unsigned pos
, int value
)
6473 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6476 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6477 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6481 map
= isl_map_cow(map
);
6482 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6483 return isl_map_free(map
);
6484 for (i
= 0; i
< map
->n
; ++i
) {
6485 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6486 type
, pos
, value
, upper
);
6490 map
= isl_map_unmark_normalized(map
);
6497 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6498 enum isl_dim_type type
, unsigned pos
, int value
)
6500 return map_bound_si(map
, type
, pos
, value
, 0);
6503 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6504 enum isl_dim_type type
, unsigned pos
, int value
)
6506 return map_bound_si(map
, type
, pos
, value
, 1);
6509 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6510 enum isl_dim_type type
, unsigned pos
, int value
)
6512 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6516 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6517 enum isl_dim_type type
, unsigned pos
, int value
)
6519 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6522 /* Bound the given variable of "bmap" from below (or above is "upper"
6523 * is set) to "value".
6525 static __isl_give isl_basic_map
*basic_map_bound(
6526 __isl_take isl_basic_map
*bmap
,
6527 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6532 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6533 return isl_basic_map_free(bmap
);
6534 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6536 return isl_basic_map_free(bmap
);
6537 pos
+= isl_basic_map_offset(bmap
, type
);
6538 bmap
= isl_basic_map_cow(bmap
);
6539 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6540 j
= isl_basic_map_alloc_inequality(bmap
);
6543 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6545 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6546 isl_int_set(bmap
->ineq
[j
][0], value
);
6548 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6549 isl_int_neg(bmap
->ineq
[j
][0], value
);
6551 bmap
= isl_basic_map_simplify(bmap
);
6552 return isl_basic_map_finalize(bmap
);
6554 isl_basic_map_free(bmap
);
6558 /* Bound the given variable of "map" from below (or above is "upper"
6559 * is set) to "value".
6561 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6562 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6566 map
= isl_map_cow(map
);
6567 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6568 return isl_map_free(map
);
6569 for (i
= map
->n
- 1; i
>= 0; --i
) {
6570 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6571 map
= remove_if_empty(map
, i
);
6575 map
= isl_map_unmark_normalized(map
);
6579 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6580 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6582 return map_bound(map
, type
, pos
, value
, 0);
6585 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6586 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6588 return map_bound(map
, type
, pos
, value
, 1);
6591 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6592 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6594 return isl_map_lower_bound(set
, type
, pos
, value
);
6597 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6598 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6600 return isl_map_upper_bound(set
, type
, pos
, value
);
6603 /* Force the values of the variable at position "pos" of type "type" of "set"
6604 * to be no smaller than "value".
6606 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6607 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6611 if (!isl_val_is_int(value
))
6612 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6613 "expecting integer value", goto error
);
6614 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6615 isl_val_free(value
);
6618 isl_val_free(value
);
6623 /* Force the values of the variable at position "pos" of type "type" of "set"
6624 * to be no greater than "value".
6626 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6627 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6631 if (!isl_val_is_int(value
))
6632 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6633 "expecting integer value", goto error
);
6634 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6635 isl_val_free(value
);
6638 isl_val_free(value
);
6643 /* Bound the given variable of "bset" from below (or above is "upper"
6644 * is set) to "value".
6646 static __isl_give isl_basic_set
*isl_basic_set_bound(
6647 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6648 isl_int value
, int upper
)
6650 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6651 type
, pos
, value
, upper
));
6654 /* Bound the given variable of "bset" from below (or above is "upper"
6655 * is set) to "value".
6657 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6658 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6659 __isl_take isl_val
*value
, int upper
)
6663 if (!isl_val_is_int(value
))
6664 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6665 "expecting integer value", goto error
);
6666 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6667 isl_val_free(value
);
6670 isl_val_free(value
);
6671 isl_basic_set_free(bset
);
6675 /* Bound the given variable of "bset" from below to "value".
6677 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6678 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6679 __isl_take isl_val
*value
)
6681 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6684 /* Bound the given variable of "bset" from above to "value".
6686 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6687 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6688 __isl_take isl_val
*value
)
6690 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6693 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6697 map
= isl_map_cow(map
);
6701 map
->dim
= isl_space_reverse(map
->dim
);
6704 for (i
= 0; i
< map
->n
; ++i
) {
6705 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6709 map
= isl_map_unmark_normalized(map
);
6717 #define TYPE isl_pw_multi_aff
6719 #define SUFFIX _pw_multi_aff
6721 #define EMPTY isl_pw_multi_aff_empty
6723 #define ADD isl_pw_multi_aff_union_add
6724 #include "isl_map_lexopt_templ.c"
6726 /* Given a map "map", compute the lexicographically minimal
6727 * (or maximal) image element for each domain element in dom,
6728 * in the form of an isl_pw_multi_aff.
6729 * If "empty" is not NULL, then set *empty to those elements in dom that
6730 * do not have an image element.
6731 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6732 * should be computed over the domain of "map". "empty" is also NULL
6735 * We first compute the lexicographically minimal or maximal element
6736 * in the first basic map. This results in a partial solution "res"
6737 * and a subset "todo" of dom that still need to be handled.
6738 * We then consider each of the remaining maps in "map" and successively
6739 * update both "res" and "todo".
6740 * If "empty" is NULL, then the todo sets are not needed and therefore
6741 * also not computed.
6743 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6744 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6745 __isl_give isl_set
**empty
, unsigned flags
)
6749 isl_pw_multi_aff
*res
;
6752 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6753 if (!map
|| (!full
&& !dom
))
6756 if (isl_map_plain_is_empty(map
)) {
6761 return isl_pw_multi_aff_from_map(map
);
6764 res
= basic_map_partial_lexopt_pw_multi_aff(
6765 isl_basic_map_copy(map
->p
[0]),
6766 isl_set_copy(dom
), empty
, flags
);
6770 for (i
= 1; i
< map
->n
; ++i
) {
6771 isl_pw_multi_aff
*res_i
;
6773 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6774 isl_basic_map_copy(map
->p
[i
]),
6775 isl_set_copy(dom
), empty
, flags
);
6777 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6778 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6780 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6783 todo
= isl_set_intersect(todo
, *empty
);
6802 #define TYPE isl_map
6806 #define EMPTY isl_map_empty
6808 #define ADD isl_map_union_disjoint
6809 #include "isl_map_lexopt_templ.c"
6811 /* Given a map "map", compute the lexicographically minimal
6812 * (or maximal) image element for each domain element in "dom",
6813 * in the form of an isl_map.
6814 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6815 * do not have an image element.
6816 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6817 * should be computed over the domain of "map". "empty" is also NULL
6820 * If the input consists of more than one disjunct, then first
6821 * compute the desired result in the form of an isl_pw_multi_aff and
6822 * then convert that into an isl_map.
6824 * This function used to have an explicit implementation in terms
6825 * of isl_maps, but it would continually intersect the domains of
6826 * partial results with the complement of the domain of the next
6827 * partial solution, potentially leading to an explosion in the number
6828 * of disjuncts if there are several disjuncts in the input.
6829 * An even earlier implementation of this function would look for
6830 * better results in the domain of the partial result and for extra
6831 * results in the complement of this domain, which would lead to
6832 * even more splintering.
6834 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6835 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6836 __isl_give isl_set
**empty
, unsigned flags
)
6839 struct isl_map
*res
;
6840 isl_pw_multi_aff
*pma
;
6842 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6843 if (!map
|| (!full
&& !dom
))
6846 if (isl_map_plain_is_empty(map
)) {
6855 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6861 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6863 return isl_map_from_pw_multi_aff(pma
);
6872 __isl_give isl_map
*isl_map_partial_lexmax(
6873 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6874 __isl_give isl_set
**empty
)
6876 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6879 __isl_give isl_map
*isl_map_partial_lexmin(
6880 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6881 __isl_give isl_set
**empty
)
6883 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6886 __isl_give isl_set
*isl_set_partial_lexmin(
6887 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6888 __isl_give isl_set
**empty
)
6890 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6894 __isl_give isl_set
*isl_set_partial_lexmax(
6895 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6896 __isl_give isl_set
**empty
)
6898 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6902 /* Compute the lexicographic minimum (or maximum if "flags" includes
6903 * ISL_OPT_MAX) of "bset" over its parametric domain.
6905 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6908 return isl_basic_map_lexopt(bset
, flags
);
6911 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6913 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6916 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6918 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6921 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6923 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6926 /* Compute the lexicographic minimum of "bset" over its parametric domain
6927 * for the purpose of quantifier elimination.
6928 * That is, find an explicit representation for all the existentially
6929 * quantified variables in "bset" by computing their lexicographic
6932 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6933 __isl_take isl_basic_set
*bset
)
6935 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6938 /* Given a basic map with one output dimension, compute the minimum or
6939 * maximum of that dimension as an isl_pw_aff.
6941 * Compute the optimum as a lexicographic optimum over the single
6942 * output dimension and extract the single isl_pw_aff from the result.
6944 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6947 isl_pw_multi_aff
*pma
;
6950 bmap
= isl_basic_map_copy(bmap
);
6951 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6952 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6953 isl_pw_multi_aff_free(pma
);
6958 /* Compute the minimum or maximum of the given output dimension
6959 * as a function of the parameters and the input dimensions,
6960 * but independently of the other output dimensions.
6962 * We first project out the other output dimension and then compute
6963 * the "lexicographic" maximum in each basic map, combining the results
6964 * using isl_pw_aff_union_max.
6966 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6973 n_out
= isl_map_dim(map
, isl_dim_out
);
6975 map
= isl_map_free(map
);
6976 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6977 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6982 isl_space
*space
= isl_map_get_space(map
);
6984 return isl_pw_aff_empty(space
);
6987 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6988 for (i
= 1; i
< map
->n
; ++i
) {
6989 isl_pw_aff
*pwaff_i
;
6991 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6992 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7000 /* Compute the minimum of the given output dimension as a function of the
7001 * parameters and input dimensions, but independently of
7002 * the other output dimensions.
7004 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7006 return map_dim_opt(map
, pos
, 0);
7009 /* Compute the maximum of the given output dimension as a function of the
7010 * parameters and input dimensions, but independently of
7011 * the other output dimensions.
7013 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7015 return map_dim_opt(map
, pos
, 1);
7018 /* Compute the minimum or maximum of the given set dimension
7019 * as a function of the parameters,
7020 * but independently of the other set dimensions.
7022 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7025 return map_dim_opt(set
, pos
, max
);
7028 /* Compute the maximum of the given set dimension as a function of the
7029 * parameters, but independently of the other set dimensions.
7031 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7033 return set_dim_opt(set
, pos
, 1);
7036 /* Compute the minimum of the given set dimension as a function of the
7037 * parameters, but independently of the other set dimensions.
7039 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7041 return set_dim_opt(set
, pos
, 0);
7044 /* Apply a preimage specified by "mat" on the parameters of "bset".
7045 * bset is assumed to have only parameters and divs.
7047 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7048 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7052 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7053 if (nparam
< 0 || !mat
)
7056 bset
->dim
= isl_space_cow(bset
->dim
);
7060 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7062 bset
->dim
->nparam
= 0;
7063 bset
->dim
->n_out
= nparam
;
7064 bset
= isl_basic_set_preimage(bset
, mat
);
7066 bset
->dim
->nparam
= bset
->dim
->n_out
;
7067 bset
->dim
->n_out
= 0;
7072 isl_basic_set_free(bset
);
7076 /* Apply a preimage specified by "mat" on the parameters of "set".
7077 * set is assumed to have only parameters and divs.
7079 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7080 __isl_take isl_mat
*mat
)
7085 nparam
= isl_set_dim(set
, isl_dim_param
);
7086 if (nparam
< 0 || !mat
)
7089 if (mat
->n_row
!= 1 + nparam
)
7090 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7091 "unexpected number of rows", goto error
);
7093 space
= isl_set_get_space(set
);
7094 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7095 isl_dim_param
, 0, nparam
);
7096 set
= isl_set_reset_space(set
, space
);
7097 set
= isl_set_preimage(set
, mat
);
7098 nparam
= isl_set_dim(set
, isl_dim_out
);
7100 set
= isl_set_free(set
);
7101 space
= isl_set_get_space(set
);
7102 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7103 isl_dim_out
, 0, nparam
);
7104 set
= isl_set_reset_space(set
, space
);
7112 /* Intersect the basic set "bset" with the affine space specified by the
7113 * equalities in "eq".
7115 static __isl_give isl_basic_set
*basic_set_append_equalities(
7116 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7124 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7129 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7130 for (i
= 0; i
< eq
->n_row
; ++i
) {
7131 k
= isl_basic_set_alloc_equality(bset
);
7134 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7135 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7139 bset
= isl_basic_set_gauss(bset
, NULL
);
7140 bset
= isl_basic_set_finalize(bset
);
7145 isl_basic_set_free(bset
);
7149 /* Intersect the set "set" with the affine space specified by the
7150 * equalities in "eq".
7152 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7160 for (i
= 0; i
< set
->n
; ++i
) {
7161 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7174 /* Given a basic set "bset" that only involves parameters and existentially
7175 * quantified variables, return the index of the first equality
7176 * that only involves parameters. If there is no such equality then
7177 * return bset->n_eq.
7179 * This function assumes that isl_basic_set_gauss has been called on "bset".
7181 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7184 isl_size nparam
, n_div
;
7186 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7187 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7188 if (nparam
< 0 || n_div
< 0)
7191 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7192 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7199 /* Compute an explicit representation for the existentially quantified
7200 * variables in "bset" by computing the "minimal value" of the set
7201 * variables. Since there are no set variables, the computation of
7202 * the minimal value essentially computes an explicit representation
7203 * of the non-empty part(s) of "bset".
7205 * The input only involves parameters and existentially quantified variables.
7206 * All equalities among parameters have been removed.
7208 * Since the existentially quantified variables in the result are in general
7209 * going to be different from those in the input, we first replace
7210 * them by the minimal number of variables based on their equalities.
7211 * This should simplify the parametric integer programming.
7213 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7215 isl_morph
*morph1
, *morph2
;
7221 if (bset
->n_eq
== 0)
7222 return isl_basic_set_lexmin_compute_divs(bset
);
7224 morph1
= isl_basic_set_parameter_compression(bset
);
7225 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7226 bset
= isl_basic_set_lift(bset
);
7227 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7228 bset
= isl_morph_basic_set(morph2
, bset
);
7229 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7231 bset
= isl_basic_set_free(bset
);
7232 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7234 set
= isl_basic_set_lexmin_compute_divs(bset
);
7236 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7241 /* Project the given basic set onto its parameter domain, possibly introducing
7242 * new, explicit, existential variables in the constraints.
7243 * The input has parameters and (possibly implicit) existential variables.
7244 * The output has the same parameters, but only
7245 * explicit existentially quantified variables.
7247 * The actual projection is performed by pip, but pip doesn't seem
7248 * to like equalities very much, so we first remove the equalities
7249 * among the parameters by performing a variable compression on
7250 * the parameters. Afterward, an inverse transformation is performed
7251 * and the equalities among the parameters are inserted back in.
7253 * The variable compression on the parameters may uncover additional
7254 * equalities that were only implicit before. We therefore check
7255 * if there are any new parameter equalities in the result and
7256 * if so recurse. The removal of parameter equalities is required
7257 * for the parameter compression performed by base_compute_divs.
7259 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7263 struct isl_mat
*T
, *T2
;
7264 struct isl_set
*set
;
7267 bset
= isl_basic_set_cow(bset
);
7271 if (bset
->n_eq
== 0)
7272 return base_compute_divs(bset
);
7274 bset
= isl_basic_set_gauss(bset
, NULL
);
7277 if (isl_basic_set_plain_is_empty(bset
))
7278 return isl_set_from_basic_set(bset
);
7280 i
= first_parameter_equality(bset
);
7281 if (i
== bset
->n_eq
)
7282 return base_compute_divs(bset
);
7284 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7286 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7287 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7289 eq
= isl_mat_cow(eq
);
7290 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7291 if (T
&& T
->n_col
== 0) {
7295 bset
= isl_basic_set_set_to_empty(bset
);
7296 return isl_set_from_basic_set(bset
);
7298 bset
= basic_set_parameter_preimage(bset
, T
);
7300 i
= first_parameter_equality(bset
);
7303 else if (i
== bset
->n_eq
)
7304 set
= base_compute_divs(bset
);
7306 set
= parameter_compute_divs(bset
);
7307 set
= set_parameter_preimage(set
, T2
);
7308 set
= set_append_equalities(set
, eq
);
7312 /* Insert the divs from "ls" before those of "bmap".
7314 * The number of columns is not changed, which means that the last
7315 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7316 * The caller is responsible for removing the same number of dimensions
7317 * from the space of "bmap".
7319 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7320 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7326 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7328 return isl_basic_map_free(bmap
);
7332 old_n_div
= bmap
->n_div
;
7333 bmap
= insert_div_rows(bmap
, n_div
);
7337 for (i
= 0; i
< n_div
; ++i
) {
7338 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7339 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7345 /* Replace the space of "bmap" by the space and divs of "ls".
7347 * If "ls" has any divs, then we simplify the result since we may
7348 * have discovered some additional equalities that could simplify
7349 * the div expressions.
7351 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7352 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7356 bmap
= isl_basic_map_cow(bmap
);
7357 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7358 if (!bmap
|| n_div
< 0)
7361 bmap
= insert_divs_from_local_space(bmap
, ls
);
7365 isl_space_free(bmap
->dim
);
7366 bmap
->dim
= isl_local_space_get_space(ls
);
7370 isl_local_space_free(ls
);
7372 bmap
= isl_basic_map_simplify(bmap
);
7373 bmap
= isl_basic_map_finalize(bmap
);
7376 isl_basic_map_free(bmap
);
7377 isl_local_space_free(ls
);
7381 /* Replace the space of "map" by the space and divs of "ls".
7383 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7384 __isl_take isl_local_space
*ls
)
7388 map
= isl_map_cow(map
);
7392 for (i
= 0; i
< map
->n
; ++i
) {
7393 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7394 isl_local_space_copy(ls
));
7398 isl_space_free(map
->dim
);
7399 map
->dim
= isl_local_space_get_space(ls
);
7403 isl_local_space_free(ls
);
7406 isl_local_space_free(ls
);
7411 /* Compute an explicit representation for the existentially
7412 * quantified variables for which do not know any explicit representation yet.
7414 * We first sort the existentially quantified variables so that the
7415 * existentially quantified variables for which we already have an explicit
7416 * representation are placed before those for which we do not.
7417 * The input dimensions, the output dimensions and the existentially
7418 * quantified variables for which we already have an explicit
7419 * representation are then turned into parameters.
7420 * compute_divs returns a map with the same parameters and
7421 * no input or output dimensions and the dimension specification
7422 * is reset to that of the input, including the existentially quantified
7423 * variables for which we already had an explicit representation.
7425 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7427 struct isl_basic_set
*bset
;
7428 struct isl_set
*set
;
7429 struct isl_map
*map
;
7431 isl_local_space
*ls
;
7438 bmap
= isl_basic_map_sort_divs(bmap
);
7439 bmap
= isl_basic_map_cow(bmap
);
7443 n_known
= isl_basic_map_first_unknown_div(bmap
);
7444 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7445 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7446 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7447 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7448 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7450 space
= isl_space_set_alloc(bmap
->ctx
,
7451 nparam
+ n_in
+ n_out
+ n_known
, 0);
7455 ls
= isl_basic_map_get_local_space(bmap
);
7456 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7457 n_known
, bmap
->n_div
- n_known
);
7459 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7460 swap_div(bmap
, i
- n_known
, i
);
7461 bmap
->n_div
-= n_known
;
7462 bmap
->extra
-= n_known
;
7464 bmap
= isl_basic_map_reset_space(bmap
, space
);
7465 bset
= bset_from_bmap(bmap
);
7467 set
= parameter_compute_divs(bset
);
7468 map
= set_to_map(set
);
7469 map
= replace_space_by_local_space(map
, ls
);
7473 isl_basic_map_free(bmap
);
7477 /* Remove the explicit representation of local variable "div",
7480 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7481 __isl_take isl_basic_map
*bmap
, int div
)
7485 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7487 return isl_basic_map_free(bmap
);
7491 bmap
= isl_basic_map_cow(bmap
);
7494 isl_int_set_si(bmap
->div
[div
][0], 0);
7498 /* Is local variable "div" of "bmap" marked as not having an explicit
7500 * Note that even if "div" is not marked in this way and therefore
7501 * has an explicit representation, this representation may still
7502 * depend (indirectly) on other local variables that do not
7503 * have an explicit representation.
7505 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7508 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7509 return isl_bool_error
;
7510 return isl_int_is_zero(bmap
->div
[div
][0]);
7513 /* Return the position of the first local variable that does not
7514 * have an explicit representation.
7515 * Return the total number of local variables if they all have
7516 * an explicit representation.
7517 * Return -1 on error.
7519 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7526 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7527 if (!isl_basic_map_div_is_known(bmap
, i
))
7533 /* Return the position of the first local variable that does not
7534 * have an explicit representation.
7535 * Return the total number of local variables if they all have
7536 * an explicit representation.
7537 * Return -1 on error.
7539 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7541 return isl_basic_map_first_unknown_div(bset
);
7544 /* Does "bmap" have an explicit representation for all local variables?
7546 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7551 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7552 first
= isl_basic_map_first_unknown_div(bmap
);
7553 if (n
< 0 || first
< 0)
7554 return isl_bool_error
;
7558 /* Do all basic maps in "map" have an explicit representation
7559 * for all local variables?
7561 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7566 return isl_bool_error
;
7568 for (i
= 0; i
< map
->n
; ++i
) {
7569 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7574 return isl_bool_true
;
7577 /* If bmap contains any unknown divs, then compute explicit
7578 * expressions for them. However, this computation may be
7579 * quite expensive, so first try to remove divs that aren't
7582 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7585 struct isl_map
*map
;
7587 known
= isl_basic_map_divs_known(bmap
);
7591 return isl_map_from_basic_map(bmap
);
7593 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7595 known
= isl_basic_map_divs_known(bmap
);
7599 return isl_map_from_basic_map(bmap
);
7601 map
= compute_divs(bmap
);
7604 isl_basic_map_free(bmap
);
7608 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7612 struct isl_map
*res
;
7619 known
= isl_map_divs_known(map
);
7627 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7628 for (i
= 1 ; i
< map
->n
; ++i
) {
7630 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7631 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7632 res
= isl_map_union_disjoint(res
, r2
);
7634 res
= isl_map_union(res
, r2
);
7641 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7643 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7646 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7648 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7651 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7654 struct isl_set
*set
;
7659 map
= isl_map_cow(map
);
7663 set
= set_from_map(map
);
7664 set
->dim
= isl_space_domain(set
->dim
);
7667 for (i
= 0; i
< map
->n
; ++i
) {
7668 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7672 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7673 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7680 /* Return the union of "map1" and "map2", where we assume for now that
7681 * "map1" and "map2" are disjoint. Note that the basic maps inside
7682 * "map1" or "map2" may not be disjoint from each other.
7683 * Also note that this function is also called from isl_map_union,
7684 * which takes care of handling the situation where "map1" and "map2"
7685 * may not be disjoint.
7687 * If one of the inputs is empty, we can simply return the other input.
7688 * Similarly, if one of the inputs is universal, then it is equal to the union.
7690 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7691 __isl_take isl_map
*map2
)
7695 struct isl_map
*map
= NULL
;
7701 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7702 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7703 "spaces don't match", goto error
);
7714 is_universe
= isl_map_plain_is_universe(map1
);
7715 if (is_universe
< 0)
7722 is_universe
= isl_map_plain_is_universe(map2
);
7723 if (is_universe
< 0)
7730 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7731 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7732 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7734 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7735 map1
->n
+ map2
->n
, flags
);
7738 for (i
= 0; i
< map1
->n
; ++i
) {
7739 map
= isl_map_add_basic_map(map
,
7740 isl_basic_map_copy(map1
->p
[i
]));
7744 for (i
= 0; i
< map2
->n
; ++i
) {
7745 map
= isl_map_add_basic_map(map
,
7746 isl_basic_map_copy(map2
->p
[i
]));
7760 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7761 * guaranteed to be disjoint by the caller.
7763 * Note that this functions is called from within isl_map_make_disjoint,
7764 * so we have to be careful not to touch the constraints of the inputs
7767 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7768 __isl_take isl_map
*map2
)
7770 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7773 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7774 * not be disjoint. The parameters are assumed to have been aligned.
7776 * We currently simply call map_union_disjoint, the internal operation
7777 * of which does not really depend on the inputs being disjoint.
7778 * If the result contains more than one basic map, then we clear
7779 * the disjoint flag since the result may contain basic maps from
7780 * both inputs and these are not guaranteed to be disjoint.
7782 * As a special case, if "map1" and "map2" are obviously equal,
7783 * then we simply return "map1".
7785 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7786 __isl_take isl_map
*map2
)
7793 equal
= isl_map_plain_is_equal(map1
, map2
);
7801 map1
= map_union_disjoint(map1
, map2
);
7805 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7813 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7816 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7817 __isl_take isl_map
*map2
)
7819 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7822 __isl_give isl_set
*isl_set_union_disjoint(
7823 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7825 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7829 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7831 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7834 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7837 * "map" and "set" are assumed to be compatible and non-NULL.
7839 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7840 __isl_take isl_set
*set
,
7841 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7842 __isl_take isl_basic_set
*bset
))
7845 struct isl_map
*result
;
7848 if (isl_set_plain_is_universe(set
)) {
7853 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7854 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7855 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7857 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7858 map
->n
* set
->n
, flags
);
7859 for (i
= 0; result
&& i
< map
->n
; ++i
)
7860 for (j
= 0; j
< set
->n
; ++j
) {
7861 result
= isl_map_add_basic_map(result
,
7862 fn(isl_basic_map_copy(map
->p
[i
]),
7863 isl_basic_set_copy(set
->p
[j
])));
7873 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7874 __isl_take isl_set
*set
)
7878 ok
= isl_map_compatible_range(map
, set
);
7882 isl_die(set
->ctx
, isl_error_invalid
,
7883 "incompatible spaces", goto error
);
7885 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7892 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7893 __isl_take isl_set
*set
)
7895 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7898 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7899 __isl_take isl_set
*set
)
7903 ok
= isl_map_compatible_domain(map
, set
);
7907 isl_die(set
->ctx
, isl_error_invalid
,
7908 "incompatible spaces", goto error
);
7910 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7917 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7918 __isl_take isl_set
*set
)
7920 return isl_map_align_params_map_map_and(map
, set
,
7921 &map_intersect_domain
);
7924 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7925 * in the space B -> C, return the intersection.
7926 * The parameters are assumed to have been aligned.
7928 * The map "factor" is first extended to a map living in the space
7929 * [A -> B] -> C and then a regular intersection is computed.
7931 static __isl_give isl_map
*map_intersect_domain_factor_range(
7932 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7935 isl_map
*ext_factor
;
7937 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7938 ext_factor
= isl_map_universe(space
);
7939 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7940 return map_intersect(map
, ext_factor
);
7943 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7944 * in the space B -> C, return the intersection.
7946 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7947 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7949 return isl_map_align_params_map_map_and(map
, factor
,
7950 &map_intersect_domain_factor_range
);
7953 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7954 * in the space A -> C, return the intersection.
7956 * The map "factor" is first extended to a map living in the space
7957 * A -> [B -> C] and then a regular intersection is computed.
7959 static __isl_give isl_map
*map_intersect_range_factor_range(
7960 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7963 isl_map
*ext_factor
;
7965 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7966 ext_factor
= isl_map_universe(space
);
7967 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7968 return isl_map_intersect(map
, ext_factor
);
7971 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7972 * in the space A -> C, return the intersection.
7974 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7975 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7977 return isl_map_align_params_map_map_and(map
, factor
,
7978 &map_intersect_range_factor_range
);
7981 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7982 __isl_take isl_map
*map2
)
7986 map1
= isl_map_reverse(map1
);
7987 map1
= isl_map_apply_range(map1
, map2
);
7988 return isl_map_reverse(map1
);
7995 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7996 __isl_take isl_map
*map2
)
7998 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
8001 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
8002 __isl_take isl_map
*map2
)
8005 struct isl_map
*result
;
8011 space
= isl_space_join(isl_space_copy(map1
->dim
),
8012 isl_space_copy(map2
->dim
));
8014 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8017 for (i
= 0; i
< map1
->n
; ++i
)
8018 for (j
= 0; j
< map2
->n
; ++j
) {
8019 result
= isl_map_add_basic_map(result
,
8020 isl_basic_map_apply_range(
8021 isl_basic_map_copy(map1
->p
[i
]),
8022 isl_basic_map_copy(map2
->p
[j
])));
8028 if (result
&& result
->n
<= 1)
8029 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8037 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8038 __isl_take isl_map
*map2
)
8040 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8044 * returns range - domain
8046 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8048 isl_space
*target_space
;
8049 struct isl_basic_set
*bset
;
8057 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8058 bmap
->dim
, isl_dim_out
),
8060 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8061 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8062 if (dim
< 0 || nparam
< 0)
8064 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8065 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8066 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8067 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8069 bmap
= isl_basic_map_free(bmap
);
8070 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8071 for (i
= 0; i
< dim
; ++i
) {
8072 int j
= isl_basic_map_alloc_equality(bmap
);
8074 bmap
= isl_basic_map_free(bmap
);
8077 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8078 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8079 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8080 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8082 bset
= isl_basic_map_domain(bmap
);
8083 bset
= isl_basic_set_reset_space(bset
, target_space
);
8086 isl_basic_map_free(bmap
);
8090 /* Check that domain and range of "map" are the same.
8092 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
8097 space
= isl_map_peek_space(map
);
8098 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8100 return isl_stat_error
;
8102 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8103 "domain and range don't match", return isl_stat_error
);
8108 * returns range - domain
8110 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8114 struct isl_set
*result
;
8119 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8120 map
->dim
, isl_dim_out
),
8122 dim
= isl_map_get_space(map
);
8123 dim
= isl_space_domain(dim
);
8124 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8127 for (i
= 0; i
< map
->n
; ++i
)
8128 result
= isl_set_add_basic_set(result
,
8129 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8138 * returns [domain -> range] -> range - domain
8140 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8141 __isl_take isl_basic_map
*bmap
)
8145 isl_basic_map
*domain
;
8149 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8150 bmap
->dim
, isl_dim_out
))
8151 isl_die(bmap
->ctx
, isl_error_invalid
,
8152 "domain and range don't match", goto error
);
8154 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8155 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8156 if (nparam
< 0 || n
)
8157 return isl_basic_map_free(bmap
);
8159 space
= isl_basic_map_get_space(bmap
);
8160 space
= isl_space_from_range(isl_space_domain(space
));
8161 domain
= isl_basic_map_universe(space
);
8163 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8164 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8165 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8167 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8169 return isl_basic_map_free(bmap
);
8171 for (i
= 0; i
< n
; ++i
) {
8172 k
= isl_basic_map_alloc_equality(bmap
);
8175 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8176 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8177 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8178 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8181 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8182 return isl_basic_map_finalize(bmap
);
8184 isl_basic_map_free(bmap
);
8189 * returns [domain -> range] -> range - domain
8191 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8194 isl_space
*domain_space
;
8196 if (isl_map_check_equal_tuples(map
) < 0)
8197 return isl_map_free(map
);
8199 map
= isl_map_cow(map
);
8203 domain_space
= isl_space_domain(isl_map_get_space(map
));
8204 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8205 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8208 for (i
= 0; i
< map
->n
; ++i
) {
8209 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8213 map
= isl_map_unmark_normalized(map
);
8220 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8222 isl_size n_in
, n_out
;
8224 n_in
= isl_space_dim(space
, isl_dim_in
);
8225 n_out
= isl_space_dim(space
, isl_dim_out
);
8226 if (n_in
< 0 || n_out
< 0)
8229 isl_die(space
->ctx
, isl_error_invalid
,
8230 "number of input and output dimensions needs to be "
8231 "the same", goto error
);
8232 return isl_basic_map_equal(space
, n_in
);
8234 isl_space_free(space
);
8238 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8240 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8243 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8245 isl_space
*dim
= isl_set_get_space(set
);
8247 id
= isl_map_identity(isl_space_map_from_set(dim
));
8248 return isl_map_intersect_range(id
, set
);
8251 /* Construct a basic set with all set dimensions having only non-negative
8254 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8255 __isl_take isl_space
*space
)
8261 struct isl_basic_set
*bset
;
8263 nparam
= isl_space_dim(space
, isl_dim_param
);
8264 dim
= isl_space_dim(space
, isl_dim_set
);
8265 total
= isl_space_dim(space
, isl_dim_all
);
8266 if (nparam
< 0 || dim
< 0 || total
< 0)
8267 space
= isl_space_free(space
);
8268 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8271 for (i
= 0; i
< dim
; ++i
) {
8272 int k
= isl_basic_set_alloc_inequality(bset
);
8275 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8276 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8280 isl_basic_set_free(bset
);
8284 /* Construct the half-space x_pos >= 0.
8286 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8291 isl_basic_set
*nonneg
;
8293 total
= isl_space_dim(space
, isl_dim_all
);
8295 space
= isl_space_free(space
);
8296 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8297 k
= isl_basic_set_alloc_inequality(nonneg
);
8300 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8301 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8303 return isl_basic_set_finalize(nonneg
);
8305 isl_basic_set_free(nonneg
);
8309 /* Construct the half-space x_pos <= -1.
8311 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8318 total
= isl_space_dim(space
, isl_dim_all
);
8320 space
= isl_space_free(space
);
8321 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8322 k
= isl_basic_set_alloc_inequality(neg
);
8325 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8326 isl_int_set_si(neg
->ineq
[k
][0], -1);
8327 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8329 return isl_basic_set_finalize(neg
);
8331 isl_basic_set_free(neg
);
8335 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8336 enum isl_dim_type type
, unsigned first
, unsigned n
)
8340 isl_basic_set
*nonneg
;
8346 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8347 return isl_set_free(set
);
8349 offset
= pos(set
->dim
, type
);
8350 for (i
= 0; i
< n
; ++i
) {
8351 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8352 offset
+ first
+ i
);
8353 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8355 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8361 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8363 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8369 return isl_stat_error
;
8370 if (isl_set_plain_is_empty(set
)) {
8375 return fn(set
, signs
, user
);
8378 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8380 half
= isl_set_intersect(half
, isl_set_copy(set
));
8381 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8385 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8387 half
= isl_set_intersect(half
, set
);
8388 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8391 return isl_stat_error
;
8394 /* Call "fn" on the intersections of "set" with each of the orthants
8395 * (except for obviously empty intersections). The orthant is identified
8396 * by the signs array, with each entry having value 1 or -1 according
8397 * to the sign of the corresponding variable.
8399 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8400 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8409 return isl_stat_error
;
8410 if (isl_set_plain_is_empty(set
))
8413 nparam
= isl_set_dim(set
, isl_dim_param
);
8414 nvar
= isl_set_dim(set
, isl_dim_set
);
8415 if (nparam
< 0 || nvar
< 0)
8416 return isl_stat_error
;
8418 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8420 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8428 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8430 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8433 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8434 __isl_keep isl_basic_map
*bmap2
)
8437 struct isl_map
*map1
;
8438 struct isl_map
*map2
;
8440 if (!bmap1
|| !bmap2
)
8441 return isl_bool_error
;
8443 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8444 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8446 is_subset
= isl_map_is_subset(map1
, map2
);
8454 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8455 __isl_keep isl_basic_set
*bset2
)
8457 return isl_basic_map_is_subset(bset1
, bset2
);
8460 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8461 __isl_keep isl_basic_map
*bmap2
)
8465 if (!bmap1
|| !bmap2
)
8466 return isl_bool_error
;
8467 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8468 if (is_subset
!= isl_bool_true
)
8470 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8474 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8475 __isl_keep isl_basic_set
*bset2
)
8477 return isl_basic_map_is_equal(
8478 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8481 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8487 return isl_bool_error
;
8488 for (i
= 0; i
< map
->n
; ++i
) {
8489 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8491 return isl_bool_error
;
8493 return isl_bool_false
;
8495 return isl_bool_true
;
8498 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8500 return map
? map
->n
== 0 : isl_bool_error
;
8503 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8505 return set
? set
->n
== 0 : isl_bool_error
;
8508 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8510 return isl_map_is_empty(set_to_map(set
));
8513 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8514 __isl_keep isl_map
*map2
)
8517 return isl_bool_error
;
8519 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8522 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8523 __isl_keep isl_set
*set2
)
8526 return isl_bool_error
;
8528 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8531 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8536 return isl_bool_error
;
8537 is_subset
= isl_map_is_subset(map1
, map2
);
8538 if (is_subset
!= isl_bool_true
)
8540 is_subset
= isl_map_is_subset(map2
, map1
);
8544 /* Is "map1" equal to "map2"?
8546 * First check if they are obviously equal.
8547 * If not, then perform a more detailed analysis.
8549 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8553 equal
= isl_map_plain_is_equal(map1
, map2
);
8554 if (equal
< 0 || equal
)
8556 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8559 isl_bool
isl_basic_map_is_strict_subset(
8560 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8564 if (!bmap1
|| !bmap2
)
8565 return isl_bool_error
;
8566 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8567 if (is_subset
!= isl_bool_true
)
8569 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8570 return isl_bool_not(is_subset
);
8573 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8574 __isl_keep isl_map
*map2
)
8579 return isl_bool_error
;
8580 is_subset
= isl_map_is_subset(map1
, map2
);
8581 if (is_subset
!= isl_bool_true
)
8583 is_subset
= isl_map_is_subset(map2
, map1
);
8584 return isl_bool_not(is_subset
);
8587 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8588 __isl_keep isl_set
*set2
)
8590 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8593 /* Is "bmap" obviously equal to the universe with the same space?
8595 * That is, does it not have any constraints?
8597 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8600 return isl_bool_error
;
8601 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8604 /* Is "bset" obviously equal to the universe with the same space?
8606 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8608 return isl_basic_map_plain_is_universe(bset
);
8611 /* If "c" does not involve any existentially quantified variables,
8612 * then set *univ to false and abort
8614 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8616 isl_bool
*univ
= user
;
8619 n
= isl_constraint_dim(c
, isl_dim_div
);
8621 c
= isl_constraint_free(c
);
8622 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8623 isl_constraint_free(c
);
8624 if (*univ
< 0 || !*univ
)
8625 return isl_stat_error
;
8629 /* Is "bmap" equal to the universe with the same space?
8631 * First check if it is obviously equal to the universe.
8632 * If not and if there are any constraints not involving
8633 * existentially quantified variables, then it is certainly
8634 * not equal to the universe.
8635 * Otherwise, check if the universe is a subset of "bmap".
8637 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8641 isl_basic_map
*test
;
8643 univ
= isl_basic_map_plain_is_universe(bmap
);
8644 if (univ
< 0 || univ
)
8646 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
8648 return isl_bool_error
;
8650 return isl_bool_false
;
8651 univ
= isl_bool_true
;
8652 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8654 return isl_bool_error
;
8655 if (univ
< 0 || !univ
)
8657 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8658 univ
= isl_basic_map_is_subset(test
, bmap
);
8659 isl_basic_map_free(test
);
8663 /* Is "bset" equal to the universe with the same space?
8665 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8667 return isl_basic_map_is_universe(bset
);
8670 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8675 return isl_bool_error
;
8677 for (i
= 0; i
< map
->n
; ++i
) {
8678 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8683 return isl_bool_false
;
8686 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8688 return isl_map_plain_is_universe(set_to_map(set
));
8691 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8693 struct isl_basic_set
*bset
= NULL
;
8694 struct isl_vec
*sample
= NULL
;
8695 isl_bool empty
, non_empty
;
8698 return isl_bool_error
;
8700 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8701 return isl_bool_true
;
8703 if (isl_basic_map_plain_is_universe(bmap
))
8704 return isl_bool_false
;
8706 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8707 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8708 copy
= isl_basic_map_remove_redundancies(copy
);
8709 empty
= isl_basic_map_plain_is_empty(copy
);
8710 isl_basic_map_free(copy
);
8714 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8716 return isl_bool_error
;
8718 return isl_bool_false
;
8719 isl_vec_free(bmap
->sample
);
8720 bmap
->sample
= NULL
;
8721 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8723 return isl_bool_error
;
8724 sample
= isl_basic_set_sample_vec(bset
);
8726 return isl_bool_error
;
8727 empty
= sample
->size
== 0;
8728 isl_vec_free(bmap
->sample
);
8729 bmap
->sample
= sample
;
8731 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8736 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8739 return isl_bool_error
;
8740 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8743 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8746 return isl_bool_error
;
8747 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8750 /* Is "bmap" known to be non-empty?
8752 * That is, is the cached sample still valid?
8754 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8759 return isl_bool_error
;
8761 return isl_bool_false
;
8762 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8764 return isl_bool_error
;
8765 if (bmap
->sample
->size
!= 1 + total
)
8766 return isl_bool_false
;
8767 return isl_basic_map_contains(bmap
, bmap
->sample
);
8770 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8772 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8775 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8776 __isl_take isl_basic_map
*bmap2
)
8778 struct isl_map
*map
;
8779 if (!bmap1
|| !bmap2
)
8782 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8784 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8787 map
= isl_map_add_basic_map(map
, bmap1
);
8788 map
= isl_map_add_basic_map(map
, bmap2
);
8791 isl_basic_map_free(bmap1
);
8792 isl_basic_map_free(bmap2
);
8796 struct isl_set
*isl_basic_set_union(
8797 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8799 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8800 bset_to_bmap(bset2
)));
8803 /* Order divs such that any div only depends on previous divs */
8804 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8805 __isl_take isl_basic_map
*bmap
)
8810 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8812 return isl_basic_map_free(bmap
);
8814 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8816 if (isl_int_is_zero(bmap
->div
[i
][0]))
8818 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8823 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8824 "integer division depends on itself",
8825 return isl_basic_map_free(bmap
));
8826 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8834 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8836 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8839 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8846 for (i
= 0; i
< map
->n
; ++i
) {
8847 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8858 /* Sort the local variables of "bset".
8860 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8861 __isl_take isl_basic_set
*bset
)
8863 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8866 /* Apply the expansion computed by isl_merge_divs.
8867 * The expansion itself is given by "exp" while the resulting
8868 * list of divs is given by "div".
8870 * Move the integer divisions of "bmap" into the right position
8871 * according to "exp" and then introduce the additional integer
8872 * divisions, adding div constraints.
8873 * The moving should be done first to avoid moving coefficients
8874 * in the definitions of the extra integer divisions.
8876 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8877 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8882 bmap
= isl_basic_map_cow(bmap
);
8886 if (div
->n_row
< bmap
->n_div
)
8887 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8888 "not an expansion", goto error
);
8890 n_div
= bmap
->n_div
;
8891 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8892 div
->n_row
- n_div
, 0,
8893 2 * (div
->n_row
- n_div
));
8895 for (i
= n_div
; i
< div
->n_row
; ++i
)
8896 if (isl_basic_map_alloc_div(bmap
) < 0)
8899 for (j
= n_div
- 1; j
>= 0; --j
) {
8902 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8907 for (i
= 0; i
< div
->n_row
; ++i
) {
8908 if (j
< n_div
&& exp
[j
] == i
) {
8911 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8912 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8914 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8923 isl_basic_map_free(bmap
);
8928 /* Apply the expansion computed by isl_merge_divs.
8929 * The expansion itself is given by "exp" while the resulting
8930 * list of divs is given by "div".
8932 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8933 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8935 return isl_basic_map_expand_divs(bset
, div
, exp
);
8938 /* Look for a div in dst that corresponds to the div "div" in src.
8939 * The divs before "div" in src and dst are assumed to be the same.
8941 * Return the position of the corresponding div in dst
8942 * if there is one. Otherwise, return a position beyond the integer divisions.
8943 * Return -1 on error.
8945 static int find_div(__isl_keep isl_basic_map
*dst
,
8946 __isl_keep isl_basic_map
*src
, unsigned div
)
8952 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8953 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8954 if (n_div
< 0 || v_div
< 0)
8956 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8957 for (i
= div
; i
< n_div
; ++i
)
8958 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8959 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8965 /* Align the divs of "dst" to those of "src", adding divs from "src"
8966 * if needed. That is, make sure that the first src->n_div divs
8967 * of the result are equal to those of src.
8969 * The result is not finalized as by design it will have redundant
8970 * divs if any divs from "src" were copied.
8972 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8973 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8982 return isl_basic_map_free(dst
);
8984 if (src
->n_div
== 0)
8987 known
= isl_basic_map_divs_known(src
);
8989 return isl_basic_map_free(dst
);
8991 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8992 "some src divs are unknown",
8993 return isl_basic_map_free(dst
));
8995 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8997 return isl_basic_map_free(dst
);
8999 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
9001 return isl_basic_map_free(dst
);
9004 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9006 dst
= isl_basic_map_free(dst
);
9007 for (i
= 0; i
< src
->n_div
; ++i
) {
9008 int j
= find_div(dst
, src
, i
);
9010 dst
= isl_basic_map_free(dst
);
9011 if (j
== dst_n_div
) {
9013 int extra
= src
->n_div
- i
;
9014 dst
= isl_basic_map_cow(dst
);
9017 dst
= isl_basic_map_extend_space(dst
,
9018 isl_space_copy(dst
->dim
),
9019 extra
, 0, 2 * extra
);
9022 j
= isl_basic_map_alloc_div(dst
);
9025 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9026 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9028 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9033 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9037 isl_basic_map_free(src
);
9040 isl_basic_map_free(src
);
9041 isl_basic_map_free(dst
);
9045 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9053 map
= isl_map_compute_divs(map
);
9054 map
= isl_map_cow(map
);
9058 for (i
= 1; i
< map
->n
; ++i
)
9059 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9060 for (i
= 1; i
< map
->n
; ++i
) {
9061 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9063 return isl_map_free(map
);
9066 map
= isl_map_unmark_normalized(map
);
9070 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9072 return isl_map_align_divs_internal(map
);
9075 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
9077 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9080 /* Align the divs of the basic maps in "map" to those
9081 * of the basic maps in "list", as well as to the other basic maps in "map".
9082 * The elements in "list" are assumed to have known divs.
9084 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9085 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9090 n
= isl_basic_map_list_n_basic_map(list
);
9091 map
= isl_map_compute_divs(map
);
9092 map
= isl_map_cow(map
);
9094 return isl_map_free(map
);
9098 for (i
= 0; i
< n
; ++i
) {
9099 isl_basic_map
*bmap
;
9101 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9102 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9103 isl_basic_map_free(bmap
);
9106 return isl_map_free(map
);
9108 return isl_map_align_divs_internal(map
);
9111 /* Align the divs of each element of "list" to those of "bmap".
9112 * Both "bmap" and the elements of "list" are assumed to have known divs.
9114 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9115 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9120 n
= isl_basic_map_list_n_basic_map(list
);
9122 return isl_basic_map_list_free(list
);
9124 for (i
= 0; i
< n
; ++i
) {
9125 isl_basic_map
*bmap_i
;
9127 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9128 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9129 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9135 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9136 __isl_take isl_map
*map
)
9140 ok
= isl_map_compatible_domain(map
, set
);
9144 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9145 "incompatible spaces", goto error
);
9146 map
= isl_map_intersect_domain(map
, set
);
9147 set
= isl_map_range(map
);
9155 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9156 __isl_take isl_map
*map
)
9158 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9161 /* There is no need to cow as removing empty parts doesn't change
9162 * the meaning of the set.
9164 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9171 for (i
= map
->n
- 1; i
>= 0; --i
)
9172 map
= remove_if_empty(map
, i
);
9177 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9179 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9182 /* Create a binary relation that maps the shared initial "pos" dimensions
9183 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9185 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9186 __isl_keep isl_basic_set
*bset2
, int pos
)
9188 isl_basic_map
*bmap1
;
9189 isl_basic_map
*bmap2
;
9191 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9192 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9193 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9194 isl_dim_out
, 0, pos
);
9195 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9196 isl_dim_out
, 0, pos
);
9197 return isl_basic_map_range_product(bmap1
, bmap2
);
9200 /* Given two basic sets bset1 and bset2, compute the maximal difference
9201 * between the values of dimension pos in bset1 and those in bset2
9202 * for any common value of the parameters and dimensions preceding pos.
9204 static enum isl_lp_result
basic_set_maximal_difference_at(
9205 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9206 int pos
, isl_int
*opt
)
9208 isl_basic_map
*bmap1
;
9209 struct isl_ctx
*ctx
;
9210 struct isl_vec
*obj
;
9214 enum isl_lp_result res
;
9216 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9217 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9218 if (nparam
< 0 || dim1
< 0 || !bset2
)
9219 return isl_lp_error
;
9221 bmap1
= join_initial(bset1
, bset2
, pos
);
9222 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9224 return isl_lp_error
;
9227 obj
= isl_vec_alloc(ctx
, 1 + total
);
9230 isl_seq_clr(obj
->block
.data
, 1 + total
);
9231 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9232 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9233 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9235 isl_basic_map_free(bmap1
);
9239 isl_basic_map_free(bmap1
);
9240 return isl_lp_error
;
9243 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9244 * for any common value of the parameters and dimensions preceding pos
9245 * in both basic sets, the values of dimension pos in bset1 are
9246 * smaller or larger than those in bset2.
9249 * 1 if bset1 follows bset2
9250 * -1 if bset1 precedes bset2
9251 * 0 if bset1 and bset2 are incomparable
9252 * -2 if some error occurred.
9254 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9255 __isl_keep isl_basic_set
*bset2
, int pos
)
9258 enum isl_lp_result res
;
9263 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9265 if (res
== isl_lp_empty
)
9267 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9268 res
== isl_lp_unbounded
)
9270 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9279 /* Given two basic sets bset1 and bset2, check whether
9280 * for any common value of the parameters and dimensions preceding pos
9281 * there is a value of dimension pos in bset1 that is larger
9282 * than a value of the same dimension in bset2.
9285 * 1 if there exists such a pair
9286 * 0 if there is no such pair, but there is a pair of equal values
9288 * -2 if some error occurred.
9290 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9291 __isl_keep isl_basic_set
*bset2
, int pos
)
9294 isl_basic_map
*bmap
;
9297 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9300 bmap
= join_initial(bset1
, bset2
, pos
);
9301 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9302 isl_dim_out
, dim1
- pos
);
9303 empty
= isl_basic_map_is_empty(bmap
);
9307 isl_basic_map_free(bmap
);
9310 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9311 isl_dim_out
, dim1
- pos
);
9312 empty
= isl_basic_map_is_empty(bmap
);
9315 isl_basic_map_free(bmap
);
9320 isl_basic_map_free(bmap
);
9324 /* Given two sets set1 and set2, check whether
9325 * for any common value of the parameters and dimensions preceding pos
9326 * there is a value of dimension pos in set1 that is larger
9327 * than a value of the same dimension in set2.
9330 * 1 if there exists such a pair
9331 * 0 if there is no such pair, but there is a pair of equal values
9333 * -2 if some error occurred.
9335 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9336 __isl_keep isl_set
*set2
, int pos
)
9344 for (i
= 0; i
< set1
->n
; ++i
)
9345 for (j
= 0; j
< set2
->n
; ++j
) {
9347 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9348 if (f
== 1 || f
== -2)
9357 static isl_bool
isl_basic_map_plain_has_fixed_var(
9358 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9364 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9366 return isl_bool_error
;
9367 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9368 for (; d
+1 > pos
; --d
)
9369 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9373 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9374 return isl_bool_false
;
9375 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9376 return isl_bool_false
;
9377 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9378 return isl_bool_false
;
9380 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9381 return isl_bool_true
;
9383 return isl_bool_false
;
9386 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9387 unsigned pos
, isl_int
*val
)
9395 return isl_bool_error
;
9397 return isl_bool_false
;
9399 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9402 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9403 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9404 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9405 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9406 fixed
= isl_bool_false
;
9409 isl_int_set(*val
, v
);
9415 static isl_bool
isl_basic_set_plain_has_fixed_var(
9416 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9418 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9422 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9423 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9425 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9426 return isl_bool_error
;
9427 return isl_basic_map_plain_has_fixed_var(bmap
,
9428 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9431 /* If "bmap" obviously lies on a hyperplane where the given dimension
9432 * has a fixed value, then return that value.
9433 * Otherwise return NaN.
9435 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9436 __isl_keep isl_basic_map
*bmap
,
9437 enum isl_dim_type type
, unsigned pos
)
9445 ctx
= isl_basic_map_get_ctx(bmap
);
9446 v
= isl_val_alloc(ctx
);
9449 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9451 return isl_val_free(v
);
9453 isl_int_set_si(v
->d
, 1);
9457 return isl_val_nan(ctx
);
9460 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9461 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9463 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9464 return isl_bool_error
;
9465 return isl_map_plain_has_fixed_var(map
,
9466 map_offset(map
, type
) - 1 + pos
, val
);
9469 /* If "map" obviously lies on a hyperplane where the given dimension
9470 * has a fixed value, then return that value.
9471 * Otherwise return NaN.
9473 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9474 enum isl_dim_type type
, unsigned pos
)
9482 ctx
= isl_map_get_ctx(map
);
9483 v
= isl_val_alloc(ctx
);
9486 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9488 return isl_val_free(v
);
9490 isl_int_set_si(v
->d
, 1);
9494 return isl_val_nan(ctx
);
9497 /* If "set" obviously lies on a hyperplane where the given dimension
9498 * has a fixed value, then return that value.
9499 * Otherwise return NaN.
9501 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9502 enum isl_dim_type type
, unsigned pos
)
9504 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9507 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9508 * then return this fixed value in *val.
9510 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9511 unsigned dim
, isl_int
*val
)
9515 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
9517 return isl_bool_error
;
9518 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
9521 /* Return -1 if the constraint "c1" should be sorted before "c2"
9522 * and 1 if it should be sorted after "c2".
9523 * Return 0 if the two constraints are the same (up to the constant term).
9525 * In particular, if a constraint involves later variables than another
9526 * then it is sorted after this other constraint.
9527 * uset_gist depends on constraints without existentially quantified
9528 * variables sorting first.
9530 * For constraints that have the same latest variable, those
9531 * with the same coefficient for this latest variable (first in absolute value
9532 * and then in actual value) are grouped together.
9533 * This is useful for detecting pairs of constraints that can
9534 * be chained in their printed representation.
9536 * Finally, within a group, constraints are sorted according to
9537 * their coefficients (excluding the constant term).
9539 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9541 isl_int
**c1
= (isl_int
**) p1
;
9542 isl_int
**c2
= (isl_int
**) p2
;
9544 unsigned size
= *(unsigned *) arg
;
9547 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9548 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9553 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9556 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9560 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9563 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9564 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9565 * and 0 if the two constraints are the same (up to the constant term).
9567 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9568 isl_int
*c1
, isl_int
*c2
)
9573 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9577 return sort_constraint_cmp(&c1
, &c2
, &size
);
9580 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9581 __isl_take isl_basic_map
*bmap
)
9588 if (bmap
->n_ineq
== 0)
9590 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9592 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9594 return isl_basic_map_free(bmap
);
9596 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9597 &sort_constraint_cmp
, &size
) < 0)
9598 return isl_basic_map_free(bmap
);
9599 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9603 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9604 __isl_take isl_basic_set
*bset
)
9606 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9607 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9610 __isl_give isl_basic_map
*isl_basic_map_normalize(
9611 __isl_take isl_basic_map
*bmap
)
9613 bmap
= isl_basic_map_remove_redundancies(bmap
);
9614 bmap
= isl_basic_map_sort_constraints(bmap
);
9617 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9618 __isl_keep isl_basic_map
*bmap2
)
9622 isl_space
*space1
, *space2
;
9624 if (!bmap1
|| !bmap2
)
9629 space1
= isl_basic_map_peek_space(bmap1
);
9630 space2
= isl_basic_map_peek_space(bmap2
);
9631 cmp
= isl_space_cmp(space1
, space2
);
9634 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9635 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9636 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9637 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9638 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9640 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9642 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9644 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9645 return bmap1
->n_eq
- bmap2
->n_eq
;
9646 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9647 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9648 if (bmap1
->n_div
!= bmap2
->n_div
)
9649 return bmap1
->n_div
- bmap2
->n_div
;
9650 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9653 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9654 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9658 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9659 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9663 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9664 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9671 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9672 __isl_keep isl_basic_set
*bset2
)
9674 return isl_basic_map_plain_cmp(bset1
, bset2
);
9677 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9683 if (set1
->n
!= set2
->n
)
9684 return set1
->n
- set2
->n
;
9686 for (i
= 0; i
< set1
->n
; ++i
) {
9687 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9695 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9696 __isl_keep isl_basic_map
*bmap2
)
9698 if (!bmap1
|| !bmap2
)
9699 return isl_bool_error
;
9700 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9703 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9704 __isl_keep isl_basic_set
*bset2
)
9706 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9707 bset_to_bmap(bset2
));
9710 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9712 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9713 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9715 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9718 /* Sort the basic maps of "map" and remove duplicate basic maps.
9720 * While removing basic maps, we make sure that the basic maps remain
9721 * sorted because isl_map_normalize expects the basic maps of the result
9724 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9728 map
= isl_map_remove_empty_parts(map
);
9731 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9732 for (i
= map
->n
- 1; i
>= 1; --i
) {
9733 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9735 isl_basic_map_free(map
->p
[i
-1]);
9736 for (j
= i
; j
< map
->n
; ++j
)
9737 map
->p
[j
- 1] = map
->p
[j
];
9744 /* Remove obvious duplicates among the basic maps of "map".
9746 * Unlike isl_map_normalize, this function does not remove redundant
9747 * constraints and only removes duplicates that have exactly the same
9748 * constraints in the input. It does sort the constraints and
9749 * the basic maps to ease the detection of duplicates.
9751 * If "map" has already been normalized or if the basic maps are
9752 * disjoint, then there can be no duplicates.
9754 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9757 isl_basic_map
*bmap
;
9763 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9765 for (i
= 0; i
< map
->n
; ++i
) {
9766 bmap
= isl_basic_map_copy(map
->p
[i
]);
9767 bmap
= isl_basic_map_sort_constraints(bmap
);
9769 return isl_map_free(map
);
9770 isl_basic_map_free(map
->p
[i
]);
9774 map
= sort_and_remove_duplicates(map
);
9778 /* We normalize in place, but if anything goes wrong we need
9779 * to return NULL, so we need to make sure we don't change the
9780 * meaning of any possible other copies of map.
9782 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9785 struct isl_basic_map
*bmap
;
9789 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9791 for (i
= 0; i
< map
->n
; ++i
) {
9792 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9795 isl_basic_map_free(map
->p
[i
]);
9799 map
= sort_and_remove_duplicates(map
);
9801 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9808 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9810 return set_from_map(isl_map_normalize(set_to_map(set
)));
9813 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9814 __isl_keep isl_map
*map2
)
9820 return isl_bool_error
;
9823 return isl_bool_true
;
9824 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9825 return isl_bool_false
;
9827 map1
= isl_map_copy(map1
);
9828 map2
= isl_map_copy(map2
);
9829 map1
= isl_map_normalize(map1
);
9830 map2
= isl_map_normalize(map2
);
9833 equal
= map1
->n
== map2
->n
;
9834 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9835 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9845 return isl_bool_error
;
9848 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9849 __isl_keep isl_set
*set2
)
9851 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9854 /* Return the basic maps in "map" as a list.
9856 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9857 __isl_keep isl_map
*map
)
9861 isl_basic_map_list
*list
;
9865 ctx
= isl_map_get_ctx(map
);
9866 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9868 for (i
= 0; i
< map
->n
; ++i
) {
9869 isl_basic_map
*bmap
;
9871 bmap
= isl_basic_map_copy(map
->p
[i
]);
9872 list
= isl_basic_map_list_add(list
, bmap
);
9878 /* Return the intersection of the elements in the non-empty list "list".
9879 * All elements are assumed to live in the same space.
9881 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9882 __isl_take isl_basic_map_list
*list
)
9886 isl_basic_map
*bmap
;
9888 n
= isl_basic_map_list_n_basic_map(list
);
9892 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9893 "expecting non-empty list", goto error
);
9895 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9896 for (i
= 1; i
< n
; ++i
) {
9897 isl_basic_map
*bmap_i
;
9899 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9900 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9903 isl_basic_map_list_free(list
);
9906 isl_basic_map_list_free(list
);
9910 /* Return the intersection of the elements in the non-empty list "list".
9911 * All elements are assumed to live in the same space.
9913 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9914 __isl_take isl_basic_set_list
*list
)
9916 return isl_basic_map_list_intersect(list
);
9919 /* Return the union of the elements of "list".
9920 * The list is required to have at least one element.
9922 __isl_give isl_set
*isl_basic_set_list_union(
9923 __isl_take isl_basic_set_list
*list
)
9928 isl_basic_set
*bset
;
9931 n
= isl_basic_set_list_n_basic_set(list
);
9935 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9936 "expecting non-empty list", goto error
);
9938 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9939 space
= isl_basic_set_get_space(bset
);
9940 isl_basic_set_free(bset
);
9942 set
= isl_set_alloc_space(space
, n
, 0);
9943 for (i
= 0; i
< n
; ++i
) {
9944 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9945 set
= isl_set_add_basic_set(set
, bset
);
9948 isl_basic_set_list_free(list
);
9951 isl_basic_set_list_free(list
);
9955 /* Return the union of the elements in the non-empty list "list".
9956 * All elements are assumed to live in the same space.
9958 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9964 n
= isl_set_list_n_set(list
);
9968 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9969 "expecting non-empty list", goto error
);
9971 set
= isl_set_list_get_set(list
, 0);
9972 for (i
= 1; i
< n
; ++i
) {
9975 set_i
= isl_set_list_get_set(list
, i
);
9976 set
= isl_set_union(set
, set_i
);
9979 isl_set_list_free(list
);
9982 isl_set_list_free(list
);
9986 __isl_give isl_basic_map
*isl_basic_map_product(
9987 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9989 isl_space
*space_result
= NULL
;
9990 struct isl_basic_map
*bmap
;
9991 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9992 struct isl_dim_map
*dim_map1
, *dim_map2
;
9994 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9996 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9997 isl_space_copy(bmap2
->dim
));
9999 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10000 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10001 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10002 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10003 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10005 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10006 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10007 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10008 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10009 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10010 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10011 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10012 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10013 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10014 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10015 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10017 bmap
= isl_basic_map_alloc_space(space_result
,
10018 bmap1
->n_div
+ bmap2
->n_div
,
10019 bmap1
->n_eq
+ bmap2
->n_eq
,
10020 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10021 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10022 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10023 bmap
= isl_basic_map_simplify(bmap
);
10024 return isl_basic_map_finalize(bmap
);
10026 isl_basic_map_free(bmap1
);
10027 isl_basic_map_free(bmap2
);
10031 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10032 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10034 isl_basic_map
*prod
;
10036 prod
= isl_basic_map_product(bmap1
, bmap2
);
10037 prod
= isl_basic_map_flatten(prod
);
10041 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10042 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10044 return isl_basic_map_flat_range_product(bset1
, bset2
);
10047 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10048 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10050 isl_space
*space1
, *space2
;
10051 isl_space
*space_result
= NULL
;
10052 isl_basic_map
*bmap
;
10053 isl_size in1
, in2
, out
, nparam
;
10054 unsigned total
, pos
;
10055 struct isl_dim_map
*dim_map1
, *dim_map2
;
10057 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10058 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10059 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10060 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10061 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10064 space1
= isl_basic_map_get_space(bmap1
);
10065 space2
= isl_basic_map_get_space(bmap2
);
10066 space_result
= isl_space_domain_product(space1
, space2
);
10068 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10069 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10070 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10071 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10072 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10073 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10074 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10075 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10076 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10077 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10078 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10080 bmap
= isl_basic_map_alloc_space(space_result
,
10081 bmap1
->n_div
+ bmap2
->n_div
,
10082 bmap1
->n_eq
+ bmap2
->n_eq
,
10083 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10084 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10085 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10086 bmap
= isl_basic_map_simplify(bmap
);
10087 return isl_basic_map_finalize(bmap
);
10089 isl_basic_map_free(bmap1
);
10090 isl_basic_map_free(bmap2
);
10094 __isl_give isl_basic_map
*isl_basic_map_range_product(
10095 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10098 isl_space
*space_result
= NULL
;
10099 isl_basic_map
*bmap
;
10100 isl_size in
, out1
, out2
, nparam
;
10101 unsigned total
, pos
;
10102 struct isl_dim_map
*dim_map1
, *dim_map2
;
10104 rational
= isl_basic_map_is_rational(bmap1
);
10105 if (rational
>= 0 && rational
)
10106 rational
= isl_basic_map_is_rational(bmap2
);
10107 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10108 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10109 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10110 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10111 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10114 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10117 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10118 isl_space_copy(bmap2
->dim
));
10120 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10121 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10122 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10123 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10124 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10125 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10126 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10127 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10128 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10129 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10130 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10132 bmap
= isl_basic_map_alloc_space(space_result
,
10133 bmap1
->n_div
+ bmap2
->n_div
,
10134 bmap1
->n_eq
+ bmap2
->n_eq
,
10135 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10136 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10137 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10139 bmap
= isl_basic_map_set_rational(bmap
);
10140 bmap
= isl_basic_map_simplify(bmap
);
10141 return isl_basic_map_finalize(bmap
);
10143 isl_basic_map_free(bmap1
);
10144 isl_basic_map_free(bmap2
);
10148 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10149 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10151 isl_basic_map
*prod
;
10153 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10154 prod
= isl_basic_map_flatten_range(prod
);
10158 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10159 * and collect the results.
10160 * The result live in the space obtained by calling "space_product"
10161 * on the spaces of "map1" and "map2".
10162 * If "remove_duplicates" is set then the result may contain duplicates
10163 * (even if the inputs do not) and so we try and remove the obvious
10166 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10167 __isl_take isl_map
*map2
,
10168 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10169 __isl_take isl_space
*right
),
10170 __isl_give isl_basic_map
*(*basic_map_product
)(
10171 __isl_take isl_basic_map
*left
,
10172 __isl_take isl_basic_map
*right
),
10173 int remove_duplicates
)
10175 unsigned flags
= 0;
10176 struct isl_map
*result
;
10180 m
= isl_map_has_equal_params(map1
, map2
);
10184 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10185 "parameters don't match", goto error
);
10187 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10188 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10189 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10191 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10192 isl_space_copy(map2
->dim
)),
10193 map1
->n
* map2
->n
, flags
);
10196 for (i
= 0; i
< map1
->n
; ++i
)
10197 for (j
= 0; j
< map2
->n
; ++j
) {
10198 struct isl_basic_map
*part
;
10199 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10200 isl_basic_map_copy(map2
->p
[j
]));
10201 if (isl_basic_map_is_empty(part
))
10202 isl_basic_map_free(part
);
10204 result
= isl_map_add_basic_map(result
, part
);
10208 if (remove_duplicates
)
10209 result
= isl_map_remove_obvious_duplicates(result
);
10210 isl_map_free(map1
);
10211 isl_map_free(map2
);
10214 isl_map_free(map1
);
10215 isl_map_free(map2
);
10219 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10221 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10222 __isl_take isl_map
*map2
)
10224 return map_product(map1
, map2
, &isl_space_product
,
10225 &isl_basic_map_product
, 0);
10228 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10229 __isl_take isl_map
*map2
)
10231 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10234 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10236 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10237 __isl_take isl_map
*map2
)
10241 prod
= isl_map_product(map1
, map2
);
10242 prod
= isl_map_flatten(prod
);
10246 /* Given two set A and B, construct its Cartesian product A x B.
10248 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10250 return isl_map_range_product(set1
, set2
);
10253 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10254 __isl_take isl_set
*set2
)
10256 return isl_map_flat_range_product(set1
, set2
);
10259 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10261 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10262 __isl_take isl_map
*map2
)
10264 return map_product(map1
, map2
, &isl_space_domain_product
,
10265 &isl_basic_map_domain_product
, 1);
10268 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10270 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10271 __isl_take isl_map
*map2
)
10273 return map_product(map1
, map2
, &isl_space_range_product
,
10274 &isl_basic_map_range_product
, 1);
10277 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10278 __isl_take isl_map
*map2
)
10280 return isl_map_align_params_map_map_and(map1
, map2
,
10281 &map_domain_product_aligned
);
10284 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10285 __isl_take isl_map
*map2
)
10287 return isl_map_align_params_map_map_and(map1
, map2
,
10288 &map_range_product_aligned
);
10291 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10293 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10296 isl_size total1
, keep1
, total2
, keep2
;
10298 total1
= isl_map_dim(map
, isl_dim_in
);
10299 total2
= isl_map_dim(map
, isl_dim_out
);
10300 if (total1
< 0 || total2
< 0)
10301 return isl_map_free(map
);
10302 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10303 !isl_space_range_is_wrapping(map
->dim
))
10304 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10305 "not a product", return isl_map_free(map
));
10307 space
= isl_map_get_space(map
);
10308 space
= isl_space_factor_domain(space
);
10309 keep1
= isl_space_dim(space
, isl_dim_in
);
10310 keep2
= isl_space_dim(space
, isl_dim_out
);
10311 if (keep1
< 0 || keep2
< 0)
10312 map
= isl_map_free(map
);
10313 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10314 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10315 map
= isl_map_reset_space(map
, space
);
10320 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10322 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10325 isl_size total1
, keep1
, total2
, keep2
;
10327 total1
= isl_map_dim(map
, isl_dim_in
);
10328 total2
= isl_map_dim(map
, isl_dim_out
);
10329 if (total1
< 0 || total2
< 0)
10330 return isl_map_free(map
);
10331 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10332 !isl_space_range_is_wrapping(map
->dim
))
10333 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10334 "not a product", return isl_map_free(map
));
10336 space
= isl_map_get_space(map
);
10337 space
= isl_space_factor_range(space
);
10338 keep1
= isl_space_dim(space
, isl_dim_in
);
10339 keep2
= isl_space_dim(space
, isl_dim_out
);
10340 if (keep1
< 0 || keep2
< 0)
10341 map
= isl_map_free(map
);
10342 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10343 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10344 map
= isl_map_reset_space(map
, space
);
10349 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10351 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10354 isl_size total
, keep
;
10356 total
= isl_map_dim(map
, isl_dim_in
);
10358 return isl_map_free(map
);
10359 if (!isl_space_domain_is_wrapping(map
->dim
))
10360 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10361 "domain is not a product", return isl_map_free(map
));
10363 space
= isl_map_get_space(map
);
10364 space
= isl_space_domain_factor_domain(space
);
10365 keep
= isl_space_dim(space
, isl_dim_in
);
10367 map
= isl_map_free(map
);
10368 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10369 map
= isl_map_reset_space(map
, space
);
10374 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10376 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10379 isl_size total
, keep
;
10381 total
= isl_map_dim(map
, isl_dim_in
);
10383 return isl_map_free(map
);
10384 if (!isl_space_domain_is_wrapping(map
->dim
))
10385 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10386 "domain is not a product", return isl_map_free(map
));
10388 space
= isl_map_get_space(map
);
10389 space
= isl_space_domain_factor_range(space
);
10390 keep
= isl_space_dim(space
, isl_dim_in
);
10392 map
= isl_map_free(map
);
10393 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10394 map
= isl_map_reset_space(map
, space
);
10399 /* Given a map A -> [B -> C], extract the map A -> B.
10401 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10404 isl_size total
, keep
;
10406 total
= isl_map_dim(map
, isl_dim_out
);
10408 return isl_map_free(map
);
10409 if (!isl_space_range_is_wrapping(map
->dim
))
10410 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10411 "range is not a product", return isl_map_free(map
));
10413 space
= isl_map_get_space(map
);
10414 space
= isl_space_range_factor_domain(space
);
10415 keep
= isl_space_dim(space
, isl_dim_out
);
10417 map
= isl_map_free(map
);
10418 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10419 map
= isl_map_reset_space(map
, space
);
10424 /* Given a map A -> [B -> C], extract the map A -> C.
10426 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10429 isl_size total
, keep
;
10431 total
= isl_map_dim(map
, isl_dim_out
);
10433 return isl_map_free(map
);
10434 if (!isl_space_range_is_wrapping(map
->dim
))
10435 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10436 "range is not a product", return isl_map_free(map
));
10438 space
= isl_map_get_space(map
);
10439 space
= isl_space_range_factor_range(space
);
10440 keep
= isl_space_dim(space
, isl_dim_out
);
10442 map
= isl_map_free(map
);
10443 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10444 map
= isl_map_reset_space(map
, space
);
10449 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10451 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10452 __isl_take isl_map
*map2
)
10456 prod
= isl_map_domain_product(map1
, map2
);
10457 prod
= isl_map_flatten_domain(prod
);
10461 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10463 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10464 __isl_take isl_map
*map2
)
10468 prod
= isl_map_range_product(map1
, map2
);
10469 prod
= isl_map_flatten_range(prod
);
10473 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10476 uint32_t hash
= isl_hash_init();
10481 bmap
= isl_basic_map_copy(bmap
);
10482 bmap
= isl_basic_map_normalize(bmap
);
10483 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10486 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10487 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10489 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10490 isl_hash_hash(hash
, c_hash
);
10492 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10493 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10495 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10496 isl_hash_hash(hash
, c_hash
);
10498 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10499 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10501 if (isl_int_is_zero(bmap
->div
[i
][0]))
10503 isl_hash_byte(hash
, i
& 0xFF);
10504 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10505 isl_hash_hash(hash
, c_hash
);
10507 isl_basic_map_free(bmap
);
10511 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10513 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10516 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10523 map
= isl_map_copy(map
);
10524 map
= isl_map_normalize(map
);
10528 hash
= isl_hash_init();
10529 for (i
= 0; i
< map
->n
; ++i
) {
10530 uint32_t bmap_hash
;
10531 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10532 isl_hash_hash(hash
, bmap_hash
);
10540 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10542 return isl_map_get_hash(set_to_map(set
));
10545 /* Return the number of basic maps in the (current) representation of "map".
10547 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
10549 return map
? map
->n
: isl_size_error
;
10552 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
10554 return set
? set
->n
: isl_size_error
;
10557 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10558 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10563 return isl_stat_error
;
10565 for (i
= 0; i
< map
->n
; ++i
)
10566 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10567 return isl_stat_error
;
10569 return isl_stat_ok
;
10572 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10573 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10578 return isl_stat_error
;
10580 for (i
= 0; i
< set
->n
; ++i
)
10581 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10582 return isl_stat_error
;
10584 return isl_stat_ok
;
10587 /* Return a list of basic sets, the union of which is equal to "set".
10589 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10590 __isl_keep isl_set
*set
)
10593 isl_basic_set_list
*list
;
10598 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10599 for (i
= 0; i
< set
->n
; ++i
) {
10600 isl_basic_set
*bset
;
10602 bset
= isl_basic_set_copy(set
->p
[i
]);
10603 list
= isl_basic_set_list_add(list
, bset
);
10609 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10616 bset
= isl_basic_set_cow(bset
);
10620 space
= isl_basic_set_get_space(bset
);
10621 space
= isl_space_lift(space
, bset
->n_div
);
10624 isl_space_free(bset
->dim
);
10626 bset
->extra
-= bset
->n_div
;
10629 bset
= isl_basic_set_finalize(bset
);
10633 isl_basic_set_free(bset
);
10637 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10643 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10648 set
= isl_set_cow(set
);
10652 n_div
= set
->p
[0]->n_div
;
10653 space
= isl_set_get_space(set
);
10654 space
= isl_space_lift(space
, n_div
);
10657 isl_space_free(set
->dim
);
10660 for (i
= 0; i
< set
->n
; ++i
) {
10661 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10672 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10677 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
10680 size
+= bset
->n_eq
* (1 + dim
);
10681 size
+= bset
->n_ineq
* (1 + dim
);
10682 size
+= bset
->n_div
* (2 + dim
);
10687 int isl_set_size(__isl_keep isl_set
*set
)
10695 for (i
= 0; i
< set
->n
; ++i
)
10696 size
+= isl_basic_set_size(set
->p
[i
]);
10701 /* Check if there is any lower bound (if lower == 0) and/or upper
10702 * bound (if upper == 0) on the specified dim.
10704 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10705 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10709 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10710 return isl_bool_error
;
10712 pos
+= isl_basic_map_offset(bmap
, type
);
10714 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10715 if (isl_int_is_zero(bmap
->div
[i
][0]))
10717 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10718 return isl_bool_true
;
10721 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10722 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10723 return isl_bool_true
;
10725 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10726 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10733 return lower
&& upper
;
10736 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10737 enum isl_dim_type type
, unsigned pos
)
10739 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10742 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10743 enum isl_dim_type type
, unsigned pos
)
10745 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10748 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10749 enum isl_dim_type type
, unsigned pos
)
10751 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10754 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10755 enum isl_dim_type type
, unsigned pos
)
10760 return isl_bool_error
;
10762 for (i
= 0; i
< map
->n
; ++i
) {
10764 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10765 if (bounded
< 0 || !bounded
)
10769 return isl_bool_true
;
10772 /* Return true if the specified dim is involved in both an upper bound
10773 * and a lower bound.
10775 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10776 enum isl_dim_type type
, unsigned pos
)
10778 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10781 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10783 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10784 enum isl_dim_type type
, unsigned pos
,
10785 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10786 enum isl_dim_type type
, unsigned pos
))
10791 return isl_bool_error
;
10793 for (i
= 0; i
< map
->n
; ++i
) {
10795 bounded
= fn(map
->p
[i
], type
, pos
);
10796 if (bounded
< 0 || bounded
)
10800 return isl_bool_false
;
10803 /* Return 1 if the specified dim is involved in any lower bound.
10805 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10806 enum isl_dim_type type
, unsigned pos
)
10808 return has_any_bound(set
, type
, pos
,
10809 &isl_basic_map_dim_has_lower_bound
);
10812 /* Return 1 if the specified dim is involved in any upper bound.
10814 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10815 enum isl_dim_type type
, unsigned pos
)
10817 return has_any_bound(set
, type
, pos
,
10818 &isl_basic_map_dim_has_upper_bound
);
10821 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10823 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10824 enum isl_dim_type type
, unsigned pos
,
10825 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10826 enum isl_dim_type type
, unsigned pos
))
10831 return isl_bool_error
;
10833 for (i
= 0; i
< map
->n
; ++i
) {
10835 bounded
= fn(map
->p
[i
], type
, pos
);
10836 if (bounded
< 0 || !bounded
)
10840 return isl_bool_true
;
10843 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10845 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10846 enum isl_dim_type type
, unsigned pos
)
10848 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10851 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10853 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10854 enum isl_dim_type type
, unsigned pos
)
10856 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10859 /* For each of the "n" variables starting at "first", determine
10860 * the sign of the variable and put the results in the first "n"
10861 * elements of the array "signs".
10863 * 1 means that the variable is non-negative
10864 * -1 means that the variable is non-positive
10865 * 0 means the variable attains both positive and negative values.
10867 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10868 unsigned first
, unsigned n
, int *signs
)
10870 isl_vec
*bound
= NULL
;
10871 struct isl_tab
*tab
= NULL
;
10872 struct isl_tab_undo
*snap
;
10876 total
= isl_basic_set_dim(bset
, isl_dim_all
);
10877 if (total
< 0 || !signs
)
10878 return isl_stat_error
;
10880 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
10881 tab
= isl_tab_from_basic_set(bset
, 0);
10882 if (!bound
|| !tab
)
10885 isl_seq_clr(bound
->el
, bound
->size
);
10886 isl_int_set_si(bound
->el
[0], -1);
10888 snap
= isl_tab_snap(tab
);
10889 for (i
= 0; i
< n
; ++i
) {
10892 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10893 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10895 empty
= tab
->empty
;
10896 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10897 if (isl_tab_rollback(tab
, snap
) < 0)
10905 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10906 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10908 empty
= tab
->empty
;
10909 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10910 if (isl_tab_rollback(tab
, snap
) < 0)
10913 signs
[i
] = empty
? -1 : 0;
10917 isl_vec_free(bound
);
10918 return isl_stat_ok
;
10921 isl_vec_free(bound
);
10922 return isl_stat_error
;
10925 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10926 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10928 if (!bset
|| !signs
)
10929 return isl_stat_error
;
10930 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10931 return isl_stat_error
;
10933 first
+= pos(bset
->dim
, type
) - 1;
10934 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10937 /* Is it possible for the integer division "div" to depend (possibly
10938 * indirectly) on any output dimensions?
10940 * If the div is undefined, then we conservatively assume that it
10941 * may depend on them.
10942 * Otherwise, we check if it actually depends on them or on any integer
10943 * divisions that may depend on them.
10945 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10948 isl_size n_out
, n_div
;
10949 unsigned o_out
, o_div
;
10951 if (isl_int_is_zero(bmap
->div
[div
][0]))
10952 return isl_bool_true
;
10954 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10956 return isl_bool_error
;
10957 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10959 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10960 return isl_bool_true
;
10962 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10964 return isl_bool_error
;
10965 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10967 for (i
= 0; i
< n_div
; ++i
) {
10968 isl_bool may_involve
;
10970 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10972 may_involve
= div_may_involve_output(bmap
, i
);
10973 if (may_involve
< 0 || may_involve
)
10974 return may_involve
;
10977 return isl_bool_false
;
10980 /* Return the first integer division of "bmap" in the range
10981 * [first, first + n[ that may depend on any output dimensions and
10982 * that has a non-zero coefficient in "c" (where the first coefficient
10983 * in "c" corresponds to integer division "first").
10985 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10986 isl_int
*c
, int first
, int n
)
10993 for (k
= first
; k
< first
+ n
; ++k
) {
10994 isl_bool may_involve
;
10996 if (isl_int_is_zero(c
[k
]))
10998 may_involve
= div_may_involve_output(bmap
, k
);
10999 if (may_involve
< 0)
11008 /* Look for a pair of inequality constraints in "bmap" of the form
11010 * -l + i >= 0 or i >= l
11012 * n + l - i >= 0 or i <= l + n
11014 * with n < "m" and i the output dimension at position "pos".
11015 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11016 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11017 * and earlier output dimensions, as well as integer divisions that do
11018 * not involve any of the output dimensions.
11020 * Return the index of the first inequality constraint or bmap->n_ineq
11021 * if no such pair can be found.
11023 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11024 int pos
, isl_int m
)
11029 isl_size n_div
, n_out
;
11030 unsigned o_div
, o_out
;
11033 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11034 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11035 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11036 if (total
< 0 || n_out
< 0 || n_div
< 0)
11039 ctx
= isl_basic_map_get_ctx(bmap
);
11040 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11041 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11042 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11043 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11045 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11046 n_out
- (pos
+ 1)) != -1)
11048 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11051 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11052 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11055 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11056 bmap
->ineq
[j
] + 1, total
))
11060 if (j
>= bmap
->n_ineq
)
11062 isl_int_add(bmap
->ineq
[i
][0],
11063 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11064 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11065 isl_int_sub(bmap
->ineq
[i
][0],
11066 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11069 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11075 return bmap
->n_ineq
;
11078 /* Return the index of the equality of "bmap" that defines
11079 * the output dimension "pos" in terms of earlier dimensions.
11080 * The equality may also involve integer divisions, as long
11081 * as those integer divisions are defined in terms of
11082 * parameters or input dimensions.
11083 * In this case, *div is set to the number of integer divisions and
11084 * *ineq is set to the number of inequality constraints (provided
11085 * div and ineq are not NULL).
11087 * The equality may also involve a single integer division involving
11088 * the output dimensions (typically only output dimension "pos") as
11089 * long as the coefficient of output dimension "pos" is 1 or -1 and
11090 * there is a pair of constraints i >= l and i <= l + n, with i referring
11091 * to output dimension "pos", l an expression involving only earlier
11092 * dimensions and n smaller than the coefficient of the integer division
11093 * in the equality. In this case, the output dimension can be defined
11094 * in terms of a modulo expression that does not involve the integer division.
11095 * *div is then set to this single integer division and
11096 * *ineq is set to the index of constraint i >= l.
11098 * Return bmap->n_eq if there is no such equality.
11099 * Return -1 on error.
11101 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11102 int pos
, int *div
, int *ineq
)
11105 isl_size n_div
, n_out
;
11106 unsigned o_div
, o_out
;
11108 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11109 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11110 if (n_out
< 0 || n_div
< 0)
11113 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11114 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11117 *ineq
= bmap
->n_ineq
;
11120 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11121 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11123 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11124 n_out
- (pos
+ 1)) != -1)
11126 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11130 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11131 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11133 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11134 k
+ 1, n_div
- (k
+1)) < n_div
)
11136 l
= find_modulo_constraint_pair(bmap
, pos
,
11137 bmap
->eq
[j
][o_div
+ k
]);
11140 if (l
>= bmap
->n_ineq
)
11152 /* Check if the given basic map is obviously single-valued.
11153 * In particular, for each output dimension, check that there is
11154 * an equality that defines the output dimension in terms of
11155 * earlier dimensions.
11157 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11162 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11164 return isl_bool_error
;
11166 for (i
= 0; i
< n_out
; ++i
) {
11169 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11172 return isl_bool_error
;
11173 if (eq
>= bmap
->n_eq
)
11174 return isl_bool_false
;
11177 return isl_bool_true
;
11180 /* Check if the given basic map is single-valued.
11181 * We simply compute
11185 * and check if the result is a subset of the identity mapping.
11187 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11190 isl_basic_map
*test
;
11194 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11198 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11199 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11201 space
= isl_basic_map_get_space(bmap
);
11202 space
= isl_space_map_from_set(isl_space_range(space
));
11203 id
= isl_basic_map_identity(space
);
11205 sv
= isl_basic_map_is_subset(test
, id
);
11207 isl_basic_map_free(test
);
11208 isl_basic_map_free(id
);
11213 /* Check if the given map is obviously single-valued.
11215 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11218 return isl_bool_error
;
11220 return isl_bool_true
;
11222 return isl_bool_false
;
11224 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11227 /* Check if the given map is single-valued.
11228 * We simply compute
11232 * and check if the result is a subset of the identity mapping.
11234 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11241 sv
= isl_map_plain_is_single_valued(map
);
11245 test
= isl_map_reverse(isl_map_copy(map
));
11246 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11248 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11249 id
= isl_map_identity(dim
);
11251 sv
= isl_map_is_subset(test
, id
);
11253 isl_map_free(test
);
11259 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11263 map
= isl_map_copy(map
);
11264 map
= isl_map_reverse(map
);
11265 in
= isl_map_is_single_valued(map
);
11271 /* Check if the given map is obviously injective.
11273 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11277 map
= isl_map_copy(map
);
11278 map
= isl_map_reverse(map
);
11279 in
= isl_map_plain_is_single_valued(map
);
11285 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11289 sv
= isl_map_is_single_valued(map
);
11293 return isl_map_is_injective(map
);
11296 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11298 return isl_map_is_single_valued(set_to_map(set
));
11301 /* Does "map" only map elements to themselves?
11303 * If the domain and range spaces are different, then "map"
11304 * is considered not to be an identity relation, even if it is empty.
11305 * Otherwise, construct the maximal identity relation and
11306 * check whether "map" is a subset of this relation.
11308 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11312 isl_bool equal
, is_identity
;
11314 space
= isl_map_get_space(map
);
11315 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11316 isl_space_free(space
);
11317 if (equal
< 0 || !equal
)
11320 id
= isl_map_identity(isl_map_get_space(map
));
11321 is_identity
= isl_map_is_subset(map
, id
);
11324 return is_identity
;
11327 int isl_map_is_translation(__isl_keep isl_map
*map
)
11332 delta
= isl_map_deltas(isl_map_copy(map
));
11333 ok
= isl_set_is_singleton(delta
);
11334 isl_set_free(delta
);
11339 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11341 if (isl_seq_first_non_zero(p
, pos
) != -1)
11343 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11348 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11351 isl_size nvar
, n_div
;
11354 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11356 return isl_bool_error
;
11358 return isl_bool_false
;
11360 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11362 return isl_bool_error
;
11363 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11364 for (j
= 0; j
< nvar
; ++j
) {
11365 int lower
= 0, upper
= 0;
11366 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11367 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11369 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11370 return isl_bool_false
;
11373 if (i
< bset
->n_eq
)
11375 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11376 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11378 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11379 return isl_bool_false
;
11380 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11385 if (!lower
|| !upper
)
11386 return isl_bool_false
;
11389 return isl_bool_true
;
11392 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11395 return isl_bool_error
;
11397 return isl_bool_false
;
11399 return isl_basic_set_is_box(set
->p
[0]);
11402 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11405 return isl_bool_error
;
11407 return isl_space_is_wrapping(bset
->dim
);
11410 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11413 return isl_bool_error
;
11415 return isl_space_is_wrapping(set
->dim
);
11418 /* Modify the space of "map" through a call to "change".
11419 * If "can_change" is set (not NULL), then first call it to check
11420 * if the modification is allowed, printing the error message "cannot_change"
11423 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11424 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11425 const char *cannot_change
,
11426 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11434 ok
= can_change
? can_change(map
) : isl_bool_true
;
11436 return isl_map_free(map
);
11438 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11439 return isl_map_free(map
));
11441 space
= change(isl_map_get_space(map
));
11442 map
= isl_map_reset_space(map
, space
);
11447 /* Is the domain of "map" a wrapped relation?
11449 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11452 return isl_bool_error
;
11454 return isl_space_domain_is_wrapping(map
->dim
);
11457 /* Does "map" have a wrapped relation in both domain and range?
11459 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11461 return isl_space_is_product(isl_map_peek_space(map
));
11464 /* Is the range of "map" a wrapped relation?
11466 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11469 return isl_bool_error
;
11471 return isl_space_range_is_wrapping(map
->dim
);
11474 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11476 bmap
= isl_basic_map_cow(bmap
);
11480 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11484 bmap
= isl_basic_map_finalize(bmap
);
11486 return bset_from_bmap(bmap
);
11488 isl_basic_map_free(bmap
);
11492 /* Given a map A -> B, return the set (A -> B).
11494 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11496 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11499 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11501 bset
= isl_basic_set_cow(bset
);
11505 bset
->dim
= isl_space_unwrap(bset
->dim
);
11509 bset
= isl_basic_set_finalize(bset
);
11511 return bset_to_bmap(bset
);
11513 isl_basic_set_free(bset
);
11517 /* Given a set (A -> B), return the map A -> B.
11518 * Error out if "set" is not of the form (A -> B).
11520 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11522 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11523 "not a wrapping set", &isl_space_unwrap
);
11526 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11527 enum isl_dim_type type
)
11532 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11535 bmap
= isl_basic_map_cow(bmap
);
11539 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11543 bmap
= isl_basic_map_finalize(bmap
);
11547 isl_basic_map_free(bmap
);
11551 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11552 enum isl_dim_type type
)
11559 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11562 map
= isl_map_cow(map
);
11566 for (i
= 0; i
< map
->n
; ++i
) {
11567 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11571 map
->dim
= isl_space_reset(map
->dim
, type
);
11581 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11586 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11589 bmap
= isl_basic_map_cow(bmap
);
11593 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11597 bmap
= isl_basic_map_finalize(bmap
);
11601 isl_basic_map_free(bmap
);
11605 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11607 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11610 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11611 __isl_take isl_basic_map
*bmap
)
11616 if (!bmap
->dim
->nested
[0])
11619 bmap
= isl_basic_map_cow(bmap
);
11623 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11627 bmap
= isl_basic_map_finalize(bmap
);
11631 isl_basic_map_free(bmap
);
11635 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11636 __isl_take isl_basic_map
*bmap
)
11641 if (!bmap
->dim
->nested
[1])
11644 bmap
= isl_basic_map_cow(bmap
);
11648 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11652 bmap
= isl_basic_map_finalize(bmap
);
11656 isl_basic_map_free(bmap
);
11660 /* Remove any internal structure from the spaces of domain and range of "map".
11662 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11667 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11670 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11673 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11675 return set_from_map(isl_map_flatten(set_to_map(set
)));
11678 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11680 isl_space
*space
, *flat_space
;
11683 space
= isl_set_get_space(set
);
11684 flat_space
= isl_space_flatten(isl_space_copy(space
));
11685 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11687 map
= isl_map_intersect_domain(map
, set
);
11692 /* Remove any internal structure from the space of the domain of "map".
11694 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11699 if (!map
->dim
->nested
[0])
11702 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11705 /* Remove any internal structure from the space of the range of "map".
11707 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11712 if (!map
->dim
->nested
[1])
11715 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11718 /* Reorder the dimensions of "bmap" according to the given dim_map
11719 * and set the dimension specification to "space" and
11720 * perform Gaussian elimination on the result.
11722 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11723 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11725 isl_basic_map
*res
;
11729 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11730 if (n_div
< 0 || !space
|| !dim_map
)
11733 flags
= bmap
->flags
;
11734 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11735 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11736 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11737 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11738 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11740 res
->flags
= flags
;
11741 res
= isl_basic_map_gauss(res
, NULL
);
11742 res
= isl_basic_map_finalize(res
);
11745 isl_dim_map_free(dim_map
);
11746 isl_basic_map_free(bmap
);
11747 isl_space_free(space
);
11751 /* Reorder the dimensions of "map" according to given reordering.
11753 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11754 __isl_take isl_reordering
*r
)
11757 struct isl_dim_map
*dim_map
;
11759 map
= isl_map_cow(map
);
11760 dim_map
= isl_dim_map_from_reordering(r
);
11761 if (!map
|| !r
|| !dim_map
)
11764 for (i
= 0; i
< map
->n
; ++i
) {
11765 struct isl_dim_map
*dim_map_i
;
11768 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11770 space
= isl_reordering_get_space(r
);
11771 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11777 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11778 map
= isl_map_unmark_normalized(map
);
11780 isl_reordering_free(r
);
11781 isl_dim_map_free(dim_map
);
11784 isl_dim_map_free(dim_map
);
11786 isl_reordering_free(r
);
11790 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11791 __isl_take isl_reordering
*r
)
11793 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11796 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11797 __isl_take isl_space
*model
)
11802 if (!map
|| !model
)
11805 ctx
= isl_space_get_ctx(model
);
11806 if (!isl_space_has_named_params(model
))
11807 isl_die(ctx
, isl_error_invalid
,
11808 "model has unnamed parameters", goto error
);
11809 if (isl_map_check_named_params(map
) < 0)
11811 aligned
= isl_map_space_has_equal_params(map
, model
);
11815 isl_reordering
*exp
;
11817 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11818 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11819 map
= isl_map_realign(map
, exp
);
11822 isl_space_free(model
);
11825 isl_space_free(model
);
11830 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11831 __isl_take isl_space
*model
)
11833 return isl_map_align_params(set
, model
);
11836 /* Align the parameters of "bmap" to those of "model", introducing
11837 * additional parameters if needed.
11839 __isl_give isl_basic_map
*isl_basic_map_align_params(
11840 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11843 isl_bool equal_params
;
11845 if (!bmap
|| !model
)
11848 ctx
= isl_space_get_ctx(model
);
11849 if (!isl_space_has_named_params(model
))
11850 isl_die(ctx
, isl_error_invalid
,
11851 "model has unnamed parameters", goto error
);
11852 if (isl_basic_map_check_named_params(bmap
) < 0)
11854 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11855 if (equal_params
< 0)
11857 if (!equal_params
) {
11858 isl_reordering
*exp
;
11859 struct isl_dim_map
*dim_map
;
11861 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11862 exp
= isl_reordering_extend_space(exp
,
11863 isl_basic_map_get_space(bmap
));
11864 dim_map
= isl_dim_map_from_reordering(exp
);
11865 bmap
= isl_basic_map_realign(bmap
,
11866 isl_reordering_get_space(exp
),
11867 isl_dim_map_extend(dim_map
, bmap
));
11868 isl_reordering_free(exp
);
11869 isl_dim_map_free(dim_map
);
11872 isl_space_free(model
);
11875 isl_space_free(model
);
11876 isl_basic_map_free(bmap
);
11880 /* Do "bset" and "space" have the same parameters?
11882 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11883 __isl_keep isl_space
*space
)
11885 isl_space
*bset_space
;
11887 bset_space
= isl_basic_set_peek_space(bset
);
11888 return isl_space_has_equal_params(bset_space
, space
);
11891 /* Do "map" and "space" have the same parameters?
11893 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11894 __isl_keep isl_space
*space
)
11896 isl_space
*map_space
;
11898 map_space
= isl_map_peek_space(map
);
11899 return isl_space_has_equal_params(map_space
, space
);
11902 /* Do "set" and "space" have the same parameters?
11904 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11905 __isl_keep isl_space
*space
)
11907 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11910 /* Align the parameters of "bset" to those of "model", introducing
11911 * additional parameters if needed.
11913 __isl_give isl_basic_set
*isl_basic_set_align_params(
11914 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11916 return isl_basic_map_align_params(bset
, model
);
11919 /* Drop all parameters not referenced by "map".
11921 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11926 n
= isl_map_dim(map
, isl_dim_param
);
11927 if (isl_map_check_named_params(map
) < 0 || n
< 0)
11928 return isl_map_free(map
);
11930 for (i
= n
- 1; i
>= 0; i
--) {
11933 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11935 return isl_map_free(map
);
11937 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11943 /* Drop all parameters not referenced by "set".
11945 __isl_give isl_set
*isl_set_drop_unused_params(
11946 __isl_take isl_set
*set
)
11948 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11951 /* Drop all parameters not referenced by "bmap".
11953 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11954 __isl_take isl_basic_map
*bmap
)
11959 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
11960 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
11961 return isl_basic_map_free(bmap
);
11963 for (i
= nparam
- 1; i
>= 0; i
--) {
11966 involves
= isl_basic_map_involves_dims(bmap
,
11967 isl_dim_param
, i
, 1);
11969 return isl_basic_map_free(bmap
);
11971 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11977 /* Drop all parameters not referenced by "bset".
11979 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11980 __isl_take isl_basic_set
*bset
)
11982 return bset_from_bmap(isl_basic_map_drop_unused_params(
11983 bset_to_bmap(bset
)));
11986 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11987 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11988 enum isl_dim_type c2
, enum isl_dim_type c3
,
11989 enum isl_dim_type c4
, enum isl_dim_type c5
)
11991 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11992 struct isl_mat
*mat
;
11997 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12000 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12003 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12004 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12005 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12006 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12008 return isl_mat_free(mat
);
12009 for (k
= 0; k
< dim
; ++k
) {
12010 isl_int_set(mat
->row
[i
][pos
],
12011 bmap
->eq
[i
][off
+ k
]);
12019 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12020 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12021 enum isl_dim_type c2
, enum isl_dim_type c3
,
12022 enum isl_dim_type c4
, enum isl_dim_type c5
)
12024 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12025 struct isl_mat
*mat
;
12030 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12033 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12036 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12037 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12038 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12039 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12041 return isl_mat_free(mat
);
12042 for (k
= 0; k
< dim
; ++k
) {
12043 isl_int_set(mat
->row
[i
][pos
],
12044 bmap
->ineq
[i
][off
+ k
]);
12052 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12053 __isl_take isl_space
*space
,
12054 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12055 enum isl_dim_type c2
, enum isl_dim_type c3
,
12056 enum isl_dim_type c4
, enum isl_dim_type c5
)
12058 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12059 isl_basic_map
*bmap
= NULL
;
12066 dim
= isl_space_dim(space
, isl_dim_all
);
12067 if (dim
< 0 || !eq
|| !ineq
)
12070 if (eq
->n_col
!= ineq
->n_col
)
12071 isl_die(space
->ctx
, isl_error_invalid
,
12072 "equalities and inequalities matrices should have "
12073 "same number of columns", goto error
);
12077 if (eq
->n_col
< total
)
12078 isl_die(space
->ctx
, isl_error_invalid
,
12079 "number of columns too small", goto error
);
12081 extra
= eq
->n_col
- total
;
12083 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12084 eq
->n_row
, ineq
->n_row
);
12087 for (i
= 0; i
< extra
; ++i
) {
12088 k
= isl_basic_map_alloc_div(bmap
);
12091 isl_int_set_si(bmap
->div
[k
][0], 0);
12093 for (i
= 0; i
< eq
->n_row
; ++i
) {
12094 l
= isl_basic_map_alloc_equality(bmap
);
12097 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12098 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12099 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12102 for (k
= 0; k
< dim
; ++k
) {
12103 isl_int_set(bmap
->eq
[l
][off
+ k
],
12109 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12110 l
= isl_basic_map_alloc_inequality(bmap
);
12113 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12114 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12115 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12118 for (k
= 0; k
< dim
; ++k
) {
12119 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12120 ineq
->row
[i
][pos
]);
12126 isl_space_free(space
);
12128 isl_mat_free(ineq
);
12130 bmap
= isl_basic_map_simplify(bmap
);
12131 return isl_basic_map_finalize(bmap
);
12133 isl_space_free(space
);
12135 isl_mat_free(ineq
);
12136 isl_basic_map_free(bmap
);
12140 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12141 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12142 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12144 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12145 c1
, c2
, c3
, c4
, isl_dim_in
);
12148 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12149 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12150 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12152 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12153 c1
, c2
, c3
, c4
, isl_dim_in
);
12156 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12157 __isl_take isl_space
*dim
,
12158 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12159 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12161 isl_basic_map
*bmap
;
12162 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12163 c1
, c2
, c3
, c4
, isl_dim_in
);
12164 return bset_from_bmap(bmap
);
12167 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12170 return isl_bool_error
;
12172 return isl_space_can_zip(bmap
->dim
);
12175 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12178 return isl_bool_error
;
12180 return isl_space_can_zip(map
->dim
);
12183 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12184 * (A -> C) -> (B -> D).
12186 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12196 if (!isl_basic_map_can_zip(bmap
))
12197 isl_die(bmap
->ctx
, isl_error_invalid
,
12198 "basic map cannot be zipped", goto error
);
12199 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12200 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12201 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12202 if (n_in
< 0 || n1
< 0 || n2
< 0)
12203 return isl_basic_map_free(bmap
);
12204 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12205 bmap
= isl_basic_map_cow(bmap
);
12206 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12209 bmap
->dim
= isl_space_zip(bmap
->dim
);
12212 bmap
= isl_basic_map_mark_final(bmap
);
12215 isl_basic_map_free(bmap
);
12219 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12220 * (A -> C) -> (B -> D).
12222 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12229 if (!isl_map_can_zip(map
))
12230 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12233 map
= isl_map_cow(map
);
12237 for (i
= 0; i
< map
->n
; ++i
) {
12238 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12243 map
->dim
= isl_space_zip(map
->dim
);
12253 /* Can we apply isl_basic_map_curry to "bmap"?
12254 * That is, does it have a nested relation in its domain?
12256 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12259 return isl_bool_error
;
12261 return isl_space_can_curry(bmap
->dim
);
12264 /* Can we apply isl_map_curry to "map"?
12265 * That is, does it have a nested relation in its domain?
12267 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12270 return isl_bool_error
;
12272 return isl_space_can_curry(map
->dim
);
12275 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12278 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12284 if (!isl_basic_map_can_curry(bmap
))
12285 isl_die(bmap
->ctx
, isl_error_invalid
,
12286 "basic map cannot be curried", goto error
);
12287 bmap
= isl_basic_map_cow(bmap
);
12290 bmap
->dim
= isl_space_curry(bmap
->dim
);
12293 bmap
= isl_basic_map_mark_final(bmap
);
12296 isl_basic_map_free(bmap
);
12300 /* Given a map (A -> B) -> C, return the corresponding map
12303 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12305 return isl_map_change_space(map
, &isl_map_can_curry
,
12306 "map cannot be curried", &isl_space_curry
);
12309 /* Can isl_map_range_curry be applied to "map"?
12310 * That is, does it have a nested relation in its range,
12311 * the domain of which is itself a nested relation?
12313 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12316 return isl_bool_error
;
12318 return isl_space_can_range_curry(map
->dim
);
12321 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12322 * A -> (B -> (C -> D)).
12324 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12326 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12327 "map range cannot be curried",
12328 &isl_space_range_curry
);
12331 /* Can we apply isl_basic_map_uncurry to "bmap"?
12332 * That is, does it have a nested relation in its domain?
12334 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12337 return isl_bool_error
;
12339 return isl_space_can_uncurry(bmap
->dim
);
12342 /* Can we apply isl_map_uncurry to "map"?
12343 * That is, does it have a nested relation in its domain?
12345 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12348 return isl_bool_error
;
12350 return isl_space_can_uncurry(map
->dim
);
12353 /* Given a basic map A -> (B -> C), return the corresponding basic map
12356 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12362 if (!isl_basic_map_can_uncurry(bmap
))
12363 isl_die(bmap
->ctx
, isl_error_invalid
,
12364 "basic map cannot be uncurried",
12365 return isl_basic_map_free(bmap
));
12366 bmap
= isl_basic_map_cow(bmap
);
12369 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12371 return isl_basic_map_free(bmap
);
12372 bmap
= isl_basic_map_mark_final(bmap
);
12376 /* Given a map A -> (B -> C), return the corresponding map
12379 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12381 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12382 "map cannot be uncurried", &isl_space_uncurry
);
12385 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12386 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12388 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12391 /* Construct a basic map where the given dimensions are equal to each other.
12393 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12394 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12396 isl_basic_map
*bmap
= NULL
;
12400 total
= isl_space_dim(space
, isl_dim_all
);
12402 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12403 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12406 if (type1
== type2
&& pos1
== pos2
)
12407 return isl_basic_map_universe(space
);
12409 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12410 i
= isl_basic_map_alloc_equality(bmap
);
12413 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12414 pos1
+= isl_basic_map_offset(bmap
, type1
);
12415 pos2
+= isl_basic_map_offset(bmap
, type2
);
12416 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12417 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12418 bmap
= isl_basic_map_finalize(bmap
);
12419 isl_space_free(space
);
12422 isl_space_free(space
);
12423 isl_basic_map_free(bmap
);
12427 /* Add a constraint imposing that the given two dimensions are equal.
12429 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12430 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12434 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12436 bmap
= isl_basic_map_intersect(bmap
, eq
);
12441 /* Add a constraint imposing that the given two dimensions are equal.
12443 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12444 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12446 isl_basic_map
*bmap
;
12448 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12450 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12455 /* Add a constraint imposing that the given two dimensions have opposite values.
12457 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12458 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12460 isl_basic_map
*bmap
= NULL
;
12464 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12465 return isl_map_free(map
);
12466 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12467 return isl_map_free(map
);
12469 total
= isl_map_dim(map
, isl_dim_all
);
12471 return isl_map_free(map
);
12472 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12473 i
= isl_basic_map_alloc_equality(bmap
);
12476 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12477 pos1
+= isl_basic_map_offset(bmap
, type1
);
12478 pos2
+= isl_basic_map_offset(bmap
, type2
);
12479 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12480 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12481 bmap
= isl_basic_map_finalize(bmap
);
12483 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12487 isl_basic_map_free(bmap
);
12492 /* Construct a constraint imposing that the value of the first dimension is
12493 * greater than or equal to that of the second.
12495 static __isl_give isl_constraint
*constraint_order_ge(
12496 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12497 enum isl_dim_type type2
, int pos2
)
12501 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12502 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12503 space
= isl_space_free(space
);
12507 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12509 if (type1
== type2
&& pos1
== pos2
)
12512 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12513 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12518 /* Add a constraint imposing that the value of the first dimension is
12519 * greater than or equal to that of the second.
12521 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12522 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12527 if (type1
== type2
&& pos1
== pos2
)
12529 space
= isl_basic_map_get_space(bmap
);
12530 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12531 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12536 /* Add a constraint imposing that the value of the first dimension is
12537 * greater than or equal to that of the second.
12539 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12540 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12545 if (type1
== type2
&& pos1
== pos2
)
12547 space
= isl_map_get_space(map
);
12548 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12549 map
= isl_map_add_constraint(map
, c
);
12554 /* Add a constraint imposing that the value of the first dimension is
12555 * less than or equal to that of the second.
12557 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12558 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12560 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12563 /* Construct a basic map where the value of the first dimension is
12564 * greater than that of the second.
12566 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12567 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12569 isl_basic_map
*bmap
= NULL
;
12573 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12574 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12577 if (type1
== type2
&& pos1
== pos2
)
12578 return isl_basic_map_empty(space
);
12580 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12581 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12582 i
= isl_basic_map_alloc_inequality(bmap
);
12583 if (total
< 0 || i
< 0)
12584 return isl_basic_map_free(bmap
);
12585 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
12586 pos1
+= isl_basic_map_offset(bmap
, type1
);
12587 pos2
+= isl_basic_map_offset(bmap
, type2
);
12588 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12589 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12590 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12591 bmap
= isl_basic_map_finalize(bmap
);
12595 isl_space_free(space
);
12596 isl_basic_map_free(bmap
);
12600 /* Add a constraint imposing that the value of the first dimension is
12601 * greater than that of the second.
12603 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12604 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12608 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12610 bmap
= isl_basic_map_intersect(bmap
, gt
);
12615 /* Add a constraint imposing that the value of the first dimension is
12616 * greater than that of the second.
12618 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12619 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12621 isl_basic_map
*bmap
;
12623 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12625 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12630 /* Add a constraint imposing that the value of the first dimension is
12631 * smaller than that of the second.
12633 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12634 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12636 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12639 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12643 isl_local_space
*ls
;
12648 if (!isl_basic_map_divs_known(bmap
))
12649 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12650 "some divs are unknown", return NULL
);
12652 ls
= isl_basic_map_get_local_space(bmap
);
12653 div
= isl_local_space_get_div(ls
, pos
);
12654 isl_local_space_free(ls
);
12659 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12662 return isl_basic_map_get_div(bset
, pos
);
12665 /* Plug in "subs" for dimension "type", "pos" of "bset".
12667 * Let i be the dimension to replace and let "subs" be of the form
12671 * Any integer division with a non-zero coefficient for i,
12673 * floor((a i + g)/m)
12677 * floor((a f + d g)/(m d))
12679 * Constraints of the form
12687 * We currently require that "subs" is an integral expression.
12688 * Handling rational expressions may require us to add stride constraints
12689 * as we do in isl_basic_set_preimage_multi_aff.
12691 __isl_give isl_basic_set
*isl_basic_set_substitute(
12692 __isl_take isl_basic_set
*bset
,
12693 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12700 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12703 bset
= isl_basic_set_cow(bset
);
12704 if (!bset
|| !subs
)
12707 ctx
= isl_basic_set_get_ctx(bset
);
12708 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12709 isl_die(ctx
, isl_error_invalid
,
12710 "spaces don't match", goto error
);
12711 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
12715 isl_die(ctx
, isl_error_unsupported
,
12716 "cannot handle divs yet", goto error
);
12717 if (!isl_int_is_one(subs
->v
->el
[0]))
12718 isl_die(ctx
, isl_error_invalid
,
12719 "can only substitute integer expressions", goto error
);
12721 pos
+= isl_basic_set_offset(bset
, type
);
12725 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12726 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12728 isl_int_set(v
, bset
->eq
[i
][pos
]);
12729 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12730 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12731 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12734 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12735 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12737 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12738 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12739 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12740 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12743 for (i
= 0; i
< bset
->n_div
; ++i
) {
12744 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12746 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12747 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12748 isl_seq_combine(bset
->div
[i
] + 1,
12749 subs
->v
->el
[0], bset
->div
[i
] + 1,
12750 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12751 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12756 bset
= isl_basic_set_simplify(bset
);
12757 return isl_basic_set_finalize(bset
);
12759 isl_basic_set_free(bset
);
12763 /* Plug in "subs" for dimension "type", "pos" of "set".
12765 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12766 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12770 if (set
&& isl_set_plain_is_empty(set
))
12773 set
= isl_set_cow(set
);
12777 for (i
= set
->n
- 1; i
>= 0; --i
) {
12778 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12779 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12790 /* Check if the range of "ma" is compatible with the domain or range
12791 * (depending on "type") of "bmap".
12793 static isl_stat
check_basic_map_compatible_range_multi_aff(
12794 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12795 __isl_keep isl_multi_aff
*ma
)
12798 isl_space
*ma_space
;
12800 ma_space
= isl_multi_aff_get_space(ma
);
12802 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12806 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12807 "parameters don't match", goto error
);
12808 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12812 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12813 "spaces don't match", goto error
);
12815 isl_space_free(ma_space
);
12816 return isl_stat_ok
;
12818 isl_space_free(ma_space
);
12819 return isl_stat_error
;
12822 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12823 * coefficients before the transformed range of dimensions,
12824 * the "n_after" coefficients after the transformed range of dimensions
12825 * and the coefficients of the other divs in "bmap".
12827 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12828 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12833 isl_local_space
*ls
;
12838 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12839 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12840 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12841 if (n_param
< 0 || n_set
< 0)
12842 return isl_basic_map_free(bmap
);
12844 for (i
= 0; i
< n_div
; ++i
) {
12845 int o_bmap
= 0, o_ls
= 0;
12847 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12848 o_bmap
+= 1 + 1 + n_param
;
12849 o_ls
+= 1 + 1 + n_param
;
12850 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12851 o_bmap
+= n_before
;
12852 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12853 ls
->div
->row
[i
] + o_ls
, n_set
);
12856 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12858 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12859 ls
->div
->row
[i
] + o_ls
, n_div
);
12862 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12863 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12868 isl_local_space_free(ls
);
12871 isl_local_space_free(ls
);
12872 return isl_basic_map_free(bmap
);
12875 /* How many stride constraints does "ma" enforce?
12876 * That is, how many of the affine expressions have a denominator
12877 * different from one?
12879 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12884 for (i
= 0; i
< ma
->n
; ++i
)
12885 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12891 /* For each affine expression in ma of the form
12893 * x_i = (f_i y + h_i)/m_i
12895 * with m_i different from one, add a constraint to "bmap"
12898 * f_i y + h_i = m_i alpha_i
12900 * with alpha_i an additional existentially quantified variable.
12902 * The input variables of "ma" correspond to a subset of the variables
12903 * of "bmap". There are "n_before" variables in "bmap" before this
12904 * subset and "n_after" variables after this subset.
12905 * The integer divisions of the affine expressions in "ma" are assumed
12906 * to have been aligned. There are "n_div_ma" of them and
12907 * they appear first in "bmap", straight after the "n_after" variables.
12909 static __isl_give isl_basic_map
*add_ma_strides(
12910 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12911 int n_before
, int n_after
, int n_div_ma
)
12919 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12920 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12921 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12922 if (total
< 0 || n_param
< 0 || n_in
< 0)
12923 return isl_basic_map_free(bmap
);
12924 for (i
= 0; i
< ma
->n
; ++i
) {
12925 int o_bmap
= 0, o_ma
= 1;
12927 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12929 div
= isl_basic_map_alloc_div(bmap
);
12930 k
= isl_basic_map_alloc_equality(bmap
);
12931 if (div
< 0 || k
< 0)
12933 isl_int_set_si(bmap
->div
[div
][0], 0);
12934 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12935 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12936 o_bmap
+= 1 + n_param
;
12937 o_ma
+= 1 + n_param
;
12938 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12939 o_bmap
+= n_before
;
12940 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12941 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12944 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12946 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12947 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12948 o_bmap
+= n_div_ma
;
12950 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12951 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12957 isl_basic_map_free(bmap
);
12961 /* Replace the domain or range space (depending on "type) of "space" by "set".
12963 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12964 enum isl_dim_type type
, __isl_take isl_space
*set
)
12966 if (type
== isl_dim_in
) {
12967 space
= isl_space_range(space
);
12968 space
= isl_space_map_from_domain_and_range(set
, space
);
12970 space
= isl_space_domain(space
);
12971 space
= isl_space_map_from_domain_and_range(space
, set
);
12977 /* Compute the preimage of the domain or range (depending on "type")
12978 * of "bmap" under the function represented by "ma".
12979 * In other words, plug in "ma" in the domain or range of "bmap".
12980 * The result is a basic map that lives in the same space as "bmap"
12981 * except that the domain or range has been replaced by
12982 * the domain space of "ma".
12984 * If bmap is represented by
12986 * A(p) + S u + B x + T v + C(divs) >= 0,
12988 * where u and x are input and output dimensions if type == isl_dim_out
12989 * while x and v are input and output dimensions if type == isl_dim_in,
12990 * and ma is represented by
12992 * x = D(p) + F(y) + G(divs')
12994 * then the result is
12996 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12998 * The divs in the input set are similarly adjusted.
13001 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13005 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13006 * B_i G(divs') + c_i(divs))/n_i)
13008 * If bmap is not a rational map and if F(y) involves any denominators
13010 * x_i = (f_i y + h_i)/m_i
13012 * then additional constraints are added to ensure that we only
13013 * map back integer points. That is we enforce
13015 * f_i y + h_i = m_i alpha_i
13017 * with alpha_i an additional existentially quantified variable.
13019 * We first copy over the divs from "ma".
13020 * Then we add the modified constraints and divs from "bmap".
13021 * Finally, we add the stride constraints, if needed.
13023 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13024 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13025 __isl_take isl_multi_aff
*ma
)
13029 isl_basic_map
*res
= NULL
;
13030 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13031 isl_int f
, c1
, c2
, g
;
13040 ma
= isl_multi_aff_align_divs(ma
);
13043 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13046 if (type
== isl_dim_in
) {
13048 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13050 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13053 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13054 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13055 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13058 space
= isl_multi_aff_get_domain_space(ma
);
13059 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13060 rational
= isl_basic_map_is_rational(bmap
);
13061 strides
= rational
? 0 : multi_aff_strides(ma
);
13062 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13063 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13065 res
= isl_basic_map_set_rational(res
);
13067 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13068 if (isl_basic_map_alloc_div(res
) < 0)
13071 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13075 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13076 k
= isl_basic_map_alloc_equality(res
);
13079 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13080 n_after
, n_div_ma
, n_div_bmap
,
13081 f
, c1
, c2
, g
, 0) < 0)
13085 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13086 k
= isl_basic_map_alloc_inequality(res
);
13089 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13090 n_after
, n_div_ma
, n_div_bmap
,
13091 f
, c1
, c2
, g
, 0) < 0)
13095 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13096 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13097 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13100 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13101 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13102 f
, c1
, c2
, g
, 1) < 0)
13107 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13113 isl_basic_map_free(bmap
);
13114 isl_multi_aff_free(ma
);
13115 res
= isl_basic_map_simplify(res
);
13116 return isl_basic_map_finalize(res
);
13122 isl_basic_map_free(bmap
);
13123 isl_multi_aff_free(ma
);
13124 isl_basic_map_free(res
);
13128 /* Compute the preimage of "bset" under the function represented by "ma".
13129 * In other words, plug in "ma" in "bset". The result is a basic set
13130 * that lives in the domain space of "ma".
13132 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13133 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13135 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13138 /* Compute the preimage of the domain of "bmap" under the function
13139 * represented by "ma".
13140 * In other words, plug in "ma" in the domain of "bmap".
13141 * The result is a basic map that lives in the same space as "bmap"
13142 * except that the domain has been replaced by the domain space of "ma".
13144 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13145 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13147 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13150 /* Compute the preimage of the range of "bmap" under the function
13151 * represented by "ma".
13152 * In other words, plug in "ma" in the range of "bmap".
13153 * The result is a basic map that lives in the same space as "bmap"
13154 * except that the range has been replaced by the domain space of "ma".
13156 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13157 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13159 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13162 /* Check if the range of "ma" is compatible with the domain or range
13163 * (depending on "type") of "map".
13164 * Return isl_stat_error if anything is wrong.
13166 static isl_stat
check_map_compatible_range_multi_aff(
13167 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13168 __isl_keep isl_multi_aff
*ma
)
13171 isl_space
*ma_space
;
13173 ma_space
= isl_multi_aff_get_space(ma
);
13174 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13175 isl_space_free(ma_space
);
13177 return isl_stat_error
;
13179 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13180 "spaces don't match", return isl_stat_error
);
13181 return isl_stat_ok
;
13184 /* Compute the preimage of the domain or range (depending on "type")
13185 * of "map" under the function represented by "ma".
13186 * In other words, plug in "ma" in the domain or range of "map".
13187 * The result is a map that lives in the same space as "map"
13188 * except that the domain or range has been replaced by
13189 * the domain space of "ma".
13191 * The parameters are assumed to have been aligned.
13193 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13194 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13199 map
= isl_map_cow(map
);
13200 ma
= isl_multi_aff_align_divs(ma
);
13203 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13206 for (i
= 0; i
< map
->n
; ++i
) {
13207 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13208 isl_multi_aff_copy(ma
));
13213 space
= isl_multi_aff_get_domain_space(ma
);
13214 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13216 isl_space_free(map
->dim
);
13221 isl_multi_aff_free(ma
);
13223 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13224 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13227 isl_multi_aff_free(ma
);
13232 /* Compute the preimage of the domain or range (depending on "type")
13233 * of "map" under the function represented by "ma".
13234 * In other words, plug in "ma" in the domain or range of "map".
13235 * The result is a map that lives in the same space as "map"
13236 * except that the domain or range has been replaced by
13237 * the domain space of "ma".
13239 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13240 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13247 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13251 return map_preimage_multi_aff(map
, type
, ma
);
13253 if (isl_map_check_named_params(map
) < 0)
13255 if (!isl_space_has_named_params(ma
->space
))
13256 isl_die(map
->ctx
, isl_error_invalid
,
13257 "unaligned unnamed parameters", goto error
);
13258 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13259 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13261 return map_preimage_multi_aff(map
, type
, ma
);
13263 isl_multi_aff_free(ma
);
13264 return isl_map_free(map
);
13267 /* Compute the preimage of "set" under the function represented by "ma".
13268 * In other words, plug in "ma" in "set". The result is a set
13269 * that lives in the domain space of "ma".
13271 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13272 __isl_take isl_multi_aff
*ma
)
13274 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13277 /* Compute the preimage of the domain of "map" under the function
13278 * represented by "ma".
13279 * In other words, plug in "ma" in the domain of "map".
13280 * The result is a map that lives in the same space as "map"
13281 * except that the domain has been replaced by the domain space of "ma".
13283 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13284 __isl_take isl_multi_aff
*ma
)
13286 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13289 /* Compute the preimage of the range of "map" under the function
13290 * represented by "ma".
13291 * In other words, plug in "ma" in the range of "map".
13292 * The result is a map that lives in the same space as "map"
13293 * except that the range has been replaced by the domain space of "ma".
13295 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13296 __isl_take isl_multi_aff
*ma
)
13298 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13301 /* Compute the preimage of "map" under the function represented by "pma".
13302 * In other words, plug in "pma" in the domain or range of "map".
13303 * The result is a map that lives in the same space as "map",
13304 * except that the space of type "type" has been replaced by
13305 * the domain space of "pma".
13307 * The parameters of "map" and "pma" are assumed to have been aligned.
13309 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13310 __isl_take isl_map
*map
, enum isl_dim_type type
,
13311 __isl_take isl_pw_multi_aff
*pma
)
13320 isl_pw_multi_aff_free(pma
);
13321 res
= isl_map_empty(isl_map_get_space(map
));
13326 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13327 isl_multi_aff_copy(pma
->p
[0].maff
));
13328 if (type
== isl_dim_in
)
13329 res
= isl_map_intersect_domain(res
,
13330 isl_map_copy(pma
->p
[0].set
));
13332 res
= isl_map_intersect_range(res
,
13333 isl_map_copy(pma
->p
[0].set
));
13335 for (i
= 1; i
< pma
->n
; ++i
) {
13338 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13339 isl_multi_aff_copy(pma
->p
[i
].maff
));
13340 if (type
== isl_dim_in
)
13341 res_i
= isl_map_intersect_domain(res_i
,
13342 isl_map_copy(pma
->p
[i
].set
));
13344 res_i
= isl_map_intersect_range(res_i
,
13345 isl_map_copy(pma
->p
[i
].set
));
13346 res
= isl_map_union(res
, res_i
);
13349 isl_pw_multi_aff_free(pma
);
13353 isl_pw_multi_aff_free(pma
);
13358 /* Compute the preimage of "map" under the function represented by "pma".
13359 * In other words, plug in "pma" in the domain or range of "map".
13360 * The result is a map that lives in the same space as "map",
13361 * except that the space of type "type" has been replaced by
13362 * the domain space of "pma".
13364 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13365 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13372 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13376 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13378 if (isl_map_check_named_params(map
) < 0)
13380 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13382 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13383 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13385 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13387 isl_pw_multi_aff_free(pma
);
13388 return isl_map_free(map
);
13391 /* Compute the preimage of "set" under the function represented by "pma".
13392 * In other words, plug in "pma" in "set". The result is a set
13393 * that lives in the domain space of "pma".
13395 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13396 __isl_take isl_pw_multi_aff
*pma
)
13398 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13401 /* Compute the preimage of the domain of "map" under the function
13402 * represented by "pma".
13403 * In other words, plug in "pma" in the domain of "map".
13404 * The result is a map that lives in the same space as "map",
13405 * except that domain space has been replaced by the domain space of "pma".
13407 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13408 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13410 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13413 /* Compute the preimage of the range of "map" under the function
13414 * represented by "pma".
13415 * In other words, plug in "pma" in the range of "map".
13416 * The result is a map that lives in the same space as "map",
13417 * except that range space has been replaced by the domain space of "pma".
13419 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13420 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13422 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13425 /* Compute the preimage of "map" under the function represented by "mpa".
13426 * In other words, plug in "mpa" in the domain or range of "map".
13427 * The result is a map that lives in the same space as "map",
13428 * except that the space of type "type" has been replaced by
13429 * the domain space of "mpa".
13431 * If the map does not involve any constraints that refer to the
13432 * dimensions of the substituted space, then the only possible
13433 * effect of "mpa" on the map is to map the space to a different space.
13434 * We create a separate isl_multi_aff to effectuate this change
13435 * in order to avoid spurious splitting of the map along the pieces
13437 * If "mpa" has a non-trivial explicit domain, however,
13438 * then the full substitution should be performed.
13440 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13441 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13445 isl_pw_multi_aff
*pma
;
13447 n
= isl_map_dim(map
, type
);
13451 full
= isl_map_involves_dims(map
, type
, 0, n
);
13452 if (full
>= 0 && !full
)
13453 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13460 space
= isl_multi_pw_aff_get_space(mpa
);
13461 isl_multi_pw_aff_free(mpa
);
13462 ma
= isl_multi_aff_zero(space
);
13463 return isl_map_preimage_multi_aff(map
, type
, ma
);
13466 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13467 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13470 isl_multi_pw_aff_free(mpa
);
13474 /* Compute the preimage of "map" under the function represented by "mpa".
13475 * In other words, plug in "mpa" in the domain "map".
13476 * The result is a map that lives in the same space as "map",
13477 * except that domain space has been replaced by the domain space of "mpa".
13479 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13480 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13482 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13485 /* Compute the preimage of "set" by the function represented by "mpa".
13486 * In other words, plug in "mpa" in "set".
13488 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13489 __isl_take isl_multi_pw_aff
*mpa
)
13491 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13494 /* Return a copy of the equality constraints of "bset" as a matrix.
13496 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13497 __isl_keep isl_basic_set
*bset
)
13502 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13506 ctx
= isl_basic_set_get_ctx(bset
);
13507 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
13510 /* Are the "n" "coefficients" starting at "first" of the integer division
13511 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13513 * The "coefficient" at position 0 is the denominator.
13514 * The "coefficient" at position 1 is the constant term.
13516 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13517 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13518 unsigned first
, unsigned n
)
13520 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13521 return isl_bool_error
;
13522 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13523 return isl_bool_error
;
13524 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13525 bmap2
->div
[pos2
] + first
, n
);
13528 /* Are the integer division expressions at position "pos1" in "bmap1" and
13529 * "pos2" in "bmap2" equal to each other, except that the constant terms
13532 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13533 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13534 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13537 isl_size total
, total2
;
13539 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
13540 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
13541 if (total
< 0 || total2
< 0)
13542 return isl_bool_error
;
13543 if (total
!= total2
)
13544 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13545 "incomparable div expressions", return isl_bool_error
);
13546 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13548 if (equal
< 0 || !equal
)
13550 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13552 if (equal
< 0 || equal
)
13553 return isl_bool_not(equal
);
13554 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13558 /* Replace the numerator of the constant term of the integer division
13559 * expression at position "div" in "bmap" by "value".
13560 * The caller guarantees that this does not change the meaning
13563 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13564 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13566 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13567 return isl_basic_map_free(bmap
);
13569 isl_int_set_si(bmap
->div
[div
][1], value
);
13574 /* Is the point "inner" internal to inequality constraint "ineq"
13576 * The point is considered to be internal to the inequality constraint,
13577 * if it strictly lies on the positive side of the inequality constraint,
13578 * or if it lies on the constraint and the constraint is lexico-positive.
13580 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13581 __isl_keep isl_basic_set
*bset
, int ineq
)
13587 if (!inner
|| !bset
)
13588 return isl_bool_error
;
13590 ctx
= isl_basic_set_get_ctx(bset
);
13591 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13592 &ctx
->normalize_gcd
);
13593 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13594 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13596 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13598 return isl_bool_error
;
13599 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13600 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13603 /* Tighten the inequality constraints of "bset" that are outward with respect
13604 * to the point "vec".
13605 * That is, tighten the constraints that are not satisfied by "vec".
13607 * "vec" is a point internal to some superset S of "bset" that is used
13608 * to make the subsets of S disjoint, by tightening one half of the constraints
13609 * that separate two subsets. In particular, the constraints of S
13610 * are all satisfied by "vec" and should not be tightened.
13611 * Of the internal constraints, those that have "vec" on the outside
13612 * are tightened. The shared facet is included in the adjacent subset
13613 * with the opposite constraint.
13614 * For constraints that saturate "vec", this criterion cannot be used
13615 * to determine which of the two sides should be tightened.
13616 * Instead, the sign of the first non-zero coefficient is used
13617 * to make this choice. Note that this second criterion is never used
13618 * on the constraints of S since "vec" is interior to "S".
13620 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13621 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13625 bset
= isl_basic_set_cow(bset
);
13628 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13631 internal
= is_internal(vec
, bset
, j
);
13633 return isl_basic_set_free(bset
);
13636 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13642 /* Replace the variables x of type "type" starting at "first" in "bmap"
13643 * by x' with x = M x' with M the matrix trans.
13644 * That is, replace the corresponding coefficients c by c M.
13646 * The transformation matrix should be a square matrix.
13648 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13649 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13650 __isl_take isl_mat
*trans
)
13654 bmap
= isl_basic_map_cow(bmap
);
13655 if (!bmap
|| !trans
)
13658 if (trans
->n_row
!= trans
->n_col
)
13659 isl_die(trans
->ctx
, isl_error_invalid
,
13660 "expecting square transformation matrix", goto error
);
13661 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13664 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13666 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13667 isl_mat_copy(trans
)) < 0)
13669 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13670 isl_mat_copy(trans
)) < 0)
13672 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13673 isl_mat_copy(trans
)) < 0)
13676 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13677 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13679 isl_mat_free(trans
);
13682 isl_mat_free(trans
);
13683 isl_basic_map_free(bmap
);
13687 /* Replace the variables x of type "type" starting at "first" in "bset"
13688 * by x' with x = M x' with M the matrix trans.
13689 * That is, replace the corresponding coefficients c by c M.
13691 * The transformation matrix should be a square matrix.
13693 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13694 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13695 __isl_take isl_mat
*trans
)
13697 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);