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
;
64 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
67 case isl_dim_param
: return 1;
68 case isl_dim_in
: return 1 + dim
->nparam
;
69 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
74 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
75 enum isl_dim_type type
)
80 case isl_dim_cst
: return 1;
83 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
84 case isl_dim_div
: return bmap
->n_div
;
85 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
90 /* Return the space of "map".
92 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
94 return map
? map
->dim
: NULL
;
97 /* Return the space of "set".
99 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
101 return isl_map_peek_space(set_to_map(set
));
104 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
106 return isl_space_dim(isl_map_peek_space(map
), type
);
109 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
111 return isl_map_dim(set_to_map(set
), type
);
114 /* Return the position of the variables of the given type
115 * within the sequence of variables of "bmap".
117 int isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
118 enum isl_dim_type type
)
122 space
= isl_basic_map_peek_space(bmap
);
129 case isl_dim_out
: return isl_space_offset(space
, type
);
130 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
133 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
134 "invalid dimension type", return -1);
138 /* Return the position of the variables of the given type
139 * within the sequence of variables of "bset".
141 int isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
142 enum isl_dim_type type
)
144 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
147 /* Return the position of the coefficients of the variables of the given type
148 * within the sequence of coefficients of "bmap".
150 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
151 enum isl_dim_type type
)
154 case isl_dim_cst
: return 0;
158 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
163 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
164 enum isl_dim_type type
)
166 return isl_basic_map_offset(bset
, type
);
169 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
171 return pos(map
->dim
, type
);
174 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
175 enum isl_dim_type type
)
177 return isl_basic_map_dim(bset
, type
);
180 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
182 return isl_basic_set_dim(bset
, isl_dim_set
);
185 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
187 return isl_basic_set_dim(bset
, isl_dim_param
);
190 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
192 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
195 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
197 return isl_set_dim(set
, isl_dim_set
);
200 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
202 return isl_set_dim(set
, isl_dim_param
);
205 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
207 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
210 /* Return the number of equality constraints in the description of "bmap".
211 * Return -1 on error.
213 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
220 /* Return the number of equality constraints in the description of "bset".
221 * Return -1 on error.
223 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
225 return isl_basic_map_n_equality(bset_to_bmap(bset
));
228 /* Return the number of inequality constraints in the description of "bmap".
229 * Return -1 on error.
231 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
238 /* Return the number of inequality constraints in the description of "bset".
239 * Return -1 on error.
241 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
243 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
246 /* Do "bmap1" and "bmap2" have the same parameters?
248 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
249 __isl_keep isl_basic_map
*bmap2
)
251 isl_space
*space1
, *space2
;
253 space1
= isl_basic_map_peek_space(bmap1
);
254 space2
= isl_basic_map_peek_space(bmap2
);
255 return isl_space_has_equal_params(space1
, space2
);
258 /* Do "map1" and "map2" have the same parameters?
260 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
261 __isl_keep isl_map
*map2
)
263 isl_space
*space1
, *space2
;
265 space1
= isl_map_peek_space(map1
);
266 space2
= isl_map_peek_space(map2
);
267 return isl_space_has_equal_params(space1
, space2
);
270 /* Do "map" and "set" have the same parameters?
272 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
273 __isl_keep isl_set
*set
)
275 return isl_map_has_equal_params(map
, set_to_map(set
));
278 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
279 __isl_keep isl_set
*set
)
283 return isl_bool_error
;
284 m
= isl_map_has_equal_params(map
, set_to_map(set
));
287 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
288 set
->dim
, isl_dim_set
);
291 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
292 __isl_keep isl_basic_set
*bset
)
296 return isl_bool_error
;
297 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
300 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
301 bset
->dim
, isl_dim_set
);
304 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
305 __isl_keep isl_set
*set
)
309 return isl_bool_error
;
310 m
= isl_map_has_equal_params(map
, set_to_map(set
));
313 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
314 set
->dim
, isl_dim_set
);
317 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
318 __isl_keep isl_basic_set
*bset
)
322 return isl_bool_error
;
323 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
326 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
327 bset
->dim
, isl_dim_set
);
330 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
332 return bmap
? bmap
->ctx
: NULL
;
335 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
337 return bset
? bset
->ctx
: NULL
;
340 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
342 return map
? map
->ctx
: NULL
;
345 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
347 return set
? set
->ctx
: NULL
;
350 /* Return the space of "bmap".
352 __isl_keep isl_space
*isl_basic_map_peek_space(
353 __isl_keep
const isl_basic_map
*bmap
)
355 return bmap
? bmap
->dim
: NULL
;
358 /* Return the space of "bset".
360 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
362 return isl_basic_map_peek_space(bset_to_bmap(bset
));
365 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
367 return isl_space_copy(isl_basic_map_peek_space(bmap
));
370 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
372 return isl_basic_map_get_space(bset_to_bmap(bset
));
375 /* Extract the divs in "bmap" as a matrix.
377 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
385 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
389 ctx
= isl_basic_map_get_ctx(bmap
);
390 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
391 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
395 for (i
= 0; i
< bmap
->n_div
; ++i
)
396 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
401 /* Extract the divs in "bset" as a matrix.
403 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
405 return isl_basic_map_get_divs(bset
);
408 __isl_give isl_local_space
*isl_basic_map_get_local_space(
409 __isl_keep isl_basic_map
*bmap
)
416 div
= isl_basic_map_get_divs(bmap
);
417 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
420 __isl_give isl_local_space
*isl_basic_set_get_local_space(
421 __isl_keep isl_basic_set
*bset
)
423 return isl_basic_map_get_local_space(bset
);
426 /* For each known div d = floor(f/m), add the constraints
429 * -(f-(m-1)) + m d >= 0
431 * Do not finalize the result.
433 static __isl_give isl_basic_map
*add_known_div_constraints(
434 __isl_take isl_basic_map
*bmap
)
441 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
444 bmap
= isl_basic_map_cow(bmap
);
445 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
448 for (i
= 0; i
< n_div
; ++i
) {
449 if (isl_int_is_zero(bmap
->div
[i
][0]))
451 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
457 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
458 __isl_take isl_local_space
*ls
)
467 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
468 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
469 n_div
, 0, 2 * n_div
);
471 for (i
= 0; i
< n_div
; ++i
)
472 if (isl_basic_map_alloc_div(bmap
) < 0)
475 for (i
= 0; i
< n_div
; ++i
)
476 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
477 bmap
= add_known_div_constraints(bmap
);
479 isl_local_space_free(ls
);
482 isl_local_space_free(ls
);
483 isl_basic_map_free(bmap
);
487 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
488 __isl_take isl_local_space
*ls
)
490 return isl_basic_map_from_local_space(ls
);
493 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
495 return isl_space_copy(isl_map_peek_space(map
));
498 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
502 return isl_space_copy(set
->dim
);
505 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
506 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
508 bmap
= isl_basic_map_cow(bmap
);
511 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
514 bmap
= isl_basic_map_finalize(bmap
);
517 isl_basic_map_free(bmap
);
521 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
522 __isl_take isl_basic_set
*bset
, const char *s
)
524 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
527 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
528 enum isl_dim_type type
)
530 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
533 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
534 enum isl_dim_type type
, const char *s
)
538 map
= isl_map_cow(map
);
542 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
546 for (i
= 0; i
< map
->n
; ++i
) {
547 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
558 /* Replace the identifier of the tuple of type "type" by "id".
560 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
561 __isl_take isl_basic_map
*bmap
,
562 enum isl_dim_type type
, __isl_take isl_id
*id
)
564 bmap
= isl_basic_map_cow(bmap
);
567 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
569 return isl_basic_map_free(bmap
);
570 bmap
= isl_basic_map_finalize(bmap
);
577 /* Replace the identifier of the tuple by "id".
579 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
580 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
582 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
585 /* Does the input or output tuple have a name?
587 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
589 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
592 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
593 enum isl_dim_type type
)
595 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
598 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
601 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
605 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
606 enum isl_dim_type type
, __isl_take isl_id
*id
)
608 map
= isl_map_cow(map
);
612 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
614 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
620 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
621 __isl_take isl_id
*id
)
623 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
626 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
627 enum isl_dim_type type
)
629 map
= isl_map_cow(map
);
633 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
635 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
638 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
640 return isl_map_reset_tuple_id(set
, isl_dim_set
);
643 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
645 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
648 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
649 enum isl_dim_type type
)
651 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
654 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
656 return isl_map_has_tuple_id(set
, isl_dim_set
);
659 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
661 return isl_map_get_tuple_id(set
, isl_dim_set
);
664 /* Does the set tuple have a name?
666 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
669 return isl_bool_error
;
670 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
674 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
676 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
679 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
681 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
684 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
685 enum isl_dim_type type
, unsigned pos
)
687 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
690 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
691 enum isl_dim_type type
, unsigned pos
)
693 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
696 /* Does the given dimension have a name?
698 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
699 enum isl_dim_type type
, unsigned pos
)
702 return isl_bool_error
;
703 return isl_space_has_dim_name(map
->dim
, type
, pos
);
706 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
707 enum isl_dim_type type
, unsigned pos
)
709 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
712 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
713 enum isl_dim_type type
, unsigned pos
)
715 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
718 /* Does the given dimension have a name?
720 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
721 enum isl_dim_type type
, unsigned pos
)
724 return isl_bool_error
;
725 return isl_space_has_dim_name(set
->dim
, type
, pos
);
728 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
729 __isl_take isl_basic_map
*bmap
,
730 enum isl_dim_type type
, unsigned pos
, const char *s
)
732 bmap
= isl_basic_map_cow(bmap
);
735 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
738 return isl_basic_map_finalize(bmap
);
740 isl_basic_map_free(bmap
);
744 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
745 enum isl_dim_type type
, unsigned pos
, const char *s
)
749 map
= isl_map_cow(map
);
753 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
757 for (i
= 0; i
< map
->n
; ++i
) {
758 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
769 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
770 __isl_take isl_basic_set
*bset
,
771 enum isl_dim_type type
, unsigned pos
, const char *s
)
773 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
777 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
778 enum isl_dim_type type
, unsigned pos
, const char *s
)
780 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
784 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
785 enum isl_dim_type type
, unsigned pos
)
788 return isl_bool_error
;
789 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
792 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
793 enum isl_dim_type type
, unsigned pos
)
795 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
798 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
799 enum isl_dim_type type
, unsigned pos
)
801 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
804 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
805 enum isl_dim_type type
, unsigned pos
)
807 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
810 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
811 enum isl_dim_type type
, unsigned pos
)
813 return isl_map_has_dim_id(set
, type
, pos
);
816 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
817 enum isl_dim_type type
, unsigned pos
)
819 return isl_map_get_dim_id(set
, type
, pos
);
822 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
823 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
825 map
= isl_map_cow(map
);
829 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
831 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
837 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
838 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
840 return isl_map_set_dim_id(set
, type
, pos
, id
);
843 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
844 __isl_keep isl_id
*id
)
848 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
851 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
852 __isl_keep isl_id
*id
)
854 return isl_map_find_dim_by_id(set
, type
, id
);
857 /* Return the position of the dimension of the given type and name
859 * Return -1 if no such dimension can be found.
861 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
862 enum isl_dim_type type
, const char *name
)
866 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
869 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
874 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
877 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
880 return isl_map_find_dim_by_name(set
, type
, name
);
883 /* Check whether equality i of bset is a pure stride constraint
884 * on a single dimension, i.e., of the form
888 * with k a constant and e an existentially quantified variable.
890 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
899 return isl_bool_error
;
901 if (!isl_int_is_zero(bset
->eq
[i
][0]))
902 return isl_bool_false
;
904 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
905 d
= isl_basic_set_dim(bset
, isl_dim_set
);
906 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
908 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
909 return isl_bool_false
;
910 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
912 return isl_bool_false
;
913 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
915 return isl_bool_false
;
917 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
919 return isl_bool_false
;
920 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
921 n_div
- pos2
- 1) != -1)
922 return isl_bool_false
;
923 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
924 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
925 return isl_bool_false
;
927 return isl_bool_true
;
930 /* Reset the user pointer on all identifiers of parameters and tuples
931 * of the space of "map".
933 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
937 space
= isl_map_get_space(map
);
938 space
= isl_space_reset_user(space
);
939 map
= isl_map_reset_space(map
, space
);
944 /* Reset the user pointer on all identifiers of parameters and tuples
945 * of the space of "set".
947 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
949 return isl_map_reset_user(set
);
952 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
955 return isl_bool_error
;
956 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
959 /* Has "map" been marked as a rational map?
960 * In particular, have all basic maps in "map" been marked this way?
961 * An empty map is not considered to be rational.
962 * Maps where only some of the basic maps are marked rational
965 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
971 return isl_bool_error
;
973 return isl_bool_false
;
974 rational
= isl_basic_map_is_rational(map
->p
[0]);
977 for (i
= 1; i
< map
->n
; ++i
) {
980 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
983 if (rational
!= rational_i
)
984 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
985 "mixed rational and integer basic maps "
986 "not supported", return isl_bool_error
);
992 /* Has "set" been marked as a rational set?
993 * In particular, have all basic set in "set" been marked this way?
994 * An empty set is not considered to be rational.
995 * Sets where only some of the basic sets are marked rational
998 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1000 return isl_map_is_rational(set
);
1003 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1005 return isl_basic_map_is_rational(bset
);
1008 /* Does "bmap" contain any rational points?
1010 * If "bmap" has an equality for each dimension, equating the dimension
1011 * to an integer constant, then it has no rational points, even if it
1012 * is marked as rational.
1014 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1016 isl_bool has_rational
= isl_bool_true
;
1020 return isl_bool_error
;
1021 if (isl_basic_map_plain_is_empty(bmap
))
1022 return isl_bool_false
;
1023 if (!isl_basic_map_is_rational(bmap
))
1024 return isl_bool_false
;
1025 bmap
= isl_basic_map_copy(bmap
);
1026 bmap
= isl_basic_map_implicit_equalities(bmap
);
1028 return isl_bool_error
;
1029 total
= isl_basic_map_total_dim(bmap
);
1030 if (bmap
->n_eq
== total
) {
1032 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1033 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1036 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1037 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1039 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1044 if (i
== bmap
->n_eq
)
1045 has_rational
= isl_bool_false
;
1047 isl_basic_map_free(bmap
);
1049 return has_rational
;
1052 /* Does "map" contain any rational points?
1054 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1057 isl_bool has_rational
;
1060 return isl_bool_error
;
1061 for (i
= 0; i
< map
->n
; ++i
) {
1062 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1063 if (has_rational
< 0 || has_rational
)
1064 return has_rational
;
1066 return isl_bool_false
;
1069 /* Does "set" contain any rational points?
1071 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1073 return isl_map_has_rational(set
);
1076 /* Is this basic set a parameter domain?
1078 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1081 return isl_bool_error
;
1082 return isl_space_is_params(bset
->dim
);
1085 /* Is this set a parameter domain?
1087 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1090 return isl_bool_error
;
1091 return isl_space_is_params(set
->dim
);
1094 /* Is this map actually a parameter domain?
1095 * Users should never call this function. Outside of isl,
1096 * a map can never be a parameter domain.
1098 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1101 return isl_bool_error
;
1102 return isl_space_is_params(map
->dim
);
1105 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1106 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1107 unsigned n_eq
, unsigned n_ineq
)
1110 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1111 unsigned n_var
= isl_space_dim(space
, isl_dim_all
);
1112 size_t row_size
= 1 + n_var
+ extra
;
1118 return isl_basic_map_free(bmap
);
1120 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1121 if (isl_blk_is_error(bmap
->block
))
1124 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1125 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1129 bmap
->block2
= isl_blk_empty();
1132 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1133 if (isl_blk_is_error(bmap
->block2
))
1136 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1141 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1142 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1144 for (i
= 0; i
< extra
; ++i
)
1145 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1149 bmap
->c_size
= n_eq
+ n_ineq
;
1150 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1151 bmap
->extra
= extra
;
1155 bmap
->sample
= NULL
;
1159 isl_basic_map_free(bmap
);
1163 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1164 unsigned nparam
, unsigned dim
, unsigned extra
,
1165 unsigned n_eq
, unsigned n_ineq
)
1167 struct isl_basic_map
*bmap
;
1170 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1174 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1175 return bset_from_bmap(bmap
);
1178 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1179 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1181 struct isl_basic_map
*bmap
;
1184 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1185 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1186 return bset_from_bmap(bmap
);
1188 isl_space_free(dim
);
1192 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1193 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1195 struct isl_basic_map
*bmap
;
1199 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1204 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1206 isl_space_free(space
);
1210 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1211 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1212 unsigned n_eq
, unsigned n_ineq
)
1214 struct isl_basic_map
*bmap
;
1217 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1221 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1225 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1226 __isl_keep isl_basic_map
*src
)
1229 unsigned total
= isl_basic_map_total_dim(src
);
1234 for (i
= 0; i
< src
->n_eq
; ++i
) {
1235 int j
= isl_basic_map_alloc_equality(dst
);
1237 return isl_basic_map_free(dst
);
1238 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1241 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1242 int j
= isl_basic_map_alloc_inequality(dst
);
1244 return isl_basic_map_free(dst
);
1245 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1248 for (i
= 0; i
< src
->n_div
; ++i
) {
1249 int j
= isl_basic_map_alloc_div(dst
);
1251 return isl_basic_map_free(dst
);
1252 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1254 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1258 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1260 struct isl_basic_map
*dup
;
1264 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1265 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1266 dup
= dup_constraints(dup
, bmap
);
1269 dup
->flags
= bmap
->flags
;
1270 dup
->sample
= isl_vec_copy(bmap
->sample
);
1274 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1276 struct isl_basic_map
*dup
;
1278 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1279 return bset_from_bmap(dup
);
1282 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1287 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1291 return isl_basic_set_dup(bset
);
1294 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1303 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1308 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1312 bmap
= isl_basic_map_dup(bmap
);
1314 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1318 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1327 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1332 if (--bmap
->ref
> 0)
1335 isl_ctx_deref(bmap
->ctx
);
1337 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1339 isl_blk_free(bmap
->ctx
, bmap
->block
);
1340 isl_vec_free(bmap
->sample
);
1341 isl_space_free(bmap
->dim
);
1347 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1349 return isl_basic_map_free(bset_to_bmap(bset
));
1352 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1354 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1357 /* Check that "bset" does not involve any parameters.
1359 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1362 return isl_stat_error
;
1363 if (isl_basic_set_dim(bset
, isl_dim_param
) != 0)
1364 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1365 "basic set should not have any parameters",
1366 return isl_stat_error
);
1370 /* Check that "bset" does not involve any local variables.
1372 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1375 return isl_stat_error
;
1376 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
1377 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1378 "basic set should not have any local variables",
1379 return isl_stat_error
);
1383 /* Check that "map" has only named parameters, reporting an error
1386 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1388 return isl_space_check_named_params(isl_map_peek_space(map
));
1391 /* Check that "bmap" has only named parameters, reporting an error
1394 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1396 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1399 /* Check that "bmap1" and "bmap2" have the same parameters,
1400 * reporting an error if they do not.
1402 static isl_stat
isl_basic_map_check_equal_params(
1403 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1407 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1409 return isl_stat_error
;
1411 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1412 "parameters don't match", return isl_stat_error
);
1416 __isl_give isl_map
*isl_map_align_params_map_map_and(
1417 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1418 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1419 __isl_take isl_map
*map2
))
1423 if (isl_map_has_equal_params(map1
, map2
))
1424 return fn(map1
, map2
);
1425 if (isl_map_check_named_params(map1
) < 0)
1427 if (isl_map_check_named_params(map2
) < 0)
1429 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1430 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1431 return fn(map1
, map2
);
1438 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1439 __isl_keep isl_map
*map2
,
1440 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1445 return isl_bool_error
;
1446 if (isl_map_has_equal_params(map1
, map2
))
1447 return fn(map1
, map2
);
1448 if (isl_map_check_named_params(map1
) < 0)
1449 return isl_bool_error
;
1450 if (isl_map_check_named_params(map2
) < 0)
1451 return isl_bool_error
;
1452 map1
= isl_map_copy(map1
);
1453 map2
= isl_map_copy(map2
);
1454 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1455 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1462 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1464 struct isl_ctx
*ctx
;
1468 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1469 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1471 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1472 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1473 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1474 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1475 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1477 int j
= isl_basic_map_alloc_inequality(bmap
);
1481 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1482 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1489 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1490 bmap
->extra
- bmap
->n_div
);
1491 return bmap
->n_eq
++;
1494 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1496 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1499 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1503 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1508 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1510 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1513 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1518 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1520 if (pos
!= bmap
->n_eq
- 1) {
1522 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1523 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1529 /* Turn inequality "pos" of "bmap" into an equality.
1531 * In particular, we move the inequality in front of the equalities
1532 * and move the last inequality in the position of the moved inequality.
1533 * Note that isl_tab_make_equalities_explicit depends on this particular
1534 * change in the ordering of the constraints.
1536 void isl_basic_map_inequality_to_equality(
1537 struct isl_basic_map
*bmap
, unsigned pos
)
1541 t
= bmap
->ineq
[pos
];
1542 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1543 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1548 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1549 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1550 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1551 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1554 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1556 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1559 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1561 struct isl_ctx
*ctx
;
1565 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1566 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1567 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1568 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1569 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1570 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1571 1 + isl_basic_map_total_dim(bmap
),
1572 bmap
->extra
- bmap
->n_div
);
1573 return bmap
->n_ineq
++;
1576 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1578 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1581 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1585 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1590 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1592 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1595 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1600 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1602 if (pos
!= bmap
->n_ineq
- 1) {
1603 t
= bmap
->ineq
[pos
];
1604 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1605 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1606 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1612 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1614 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1617 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1623 empty
= isl_basic_map_plain_is_empty(bmap
);
1625 return isl_basic_map_free(bmap
);
1629 bmap
= isl_basic_map_cow(bmap
);
1630 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1633 k
= isl_basic_map_alloc_equality(bmap
);
1636 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1639 isl_basic_map_free(bmap
);
1643 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1646 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1649 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1654 bmap
= isl_basic_map_cow(bmap
);
1655 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1658 k
= isl_basic_map_alloc_inequality(bmap
);
1661 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1664 isl_basic_map_free(bmap
);
1668 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1671 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1674 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1678 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1679 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1680 1 + 1 + isl_basic_map_total_dim(bmap
),
1681 bmap
->extra
- bmap
->n_div
);
1682 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1683 return bmap
->n_div
++;
1686 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1688 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1692 #define TYPE isl_basic_map
1693 #include "check_type_range_templ.c"
1695 /* Check that there are "n" dimensions of type "type" starting at "first"
1698 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1699 enum isl_dim_type type
, unsigned first
, unsigned n
)
1701 return isl_basic_map_check_range(bset_to_bmap(bset
),
1705 /* Insert an extra integer division, prescribed by "div", to "bmap"
1706 * at (integer division) position "pos".
1708 * The integer division is first added at the end and then moved
1709 * into the right position.
1711 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1712 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1716 bmap
= isl_basic_map_cow(bmap
);
1718 return isl_basic_map_free(bmap
);
1720 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1721 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1722 "unexpected size", return isl_basic_map_free(bmap
));
1723 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1724 return isl_basic_map_free(bmap
);
1726 bmap
= isl_basic_map_extend_space(bmap
,
1727 isl_basic_map_get_space(bmap
), 1, 0, 2);
1728 k
= isl_basic_map_alloc_div(bmap
);
1730 return isl_basic_map_free(bmap
);
1731 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1732 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1734 for (i
= k
; i
> pos
; --i
)
1735 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1740 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1743 return isl_stat_error
;
1744 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1749 static __isl_give isl_basic_map
*add_constraints(
1750 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1751 unsigned i_pos
, unsigned o_pos
)
1753 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1756 struct isl_dim_map
*dim_map
;
1758 space
= isl_basic_map_peek_space(bmap2
);
1759 if (!bmap1
|| !space
)
1762 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1763 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1764 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1765 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1766 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1767 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1768 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1769 ctx
= isl_basic_map_get_ctx(bmap1
);
1770 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1771 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1772 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1773 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1774 isl_dim_map_div(dim_map
, bmap2
, total
);
1776 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1778 isl_basic_map_free(bmap1
);
1779 isl_basic_map_free(bmap2
);
1783 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1784 struct isl_basic_set
*bset2
, unsigned pos
)
1786 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1787 bset_to_bmap(bset2
), 0, pos
));
1790 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1791 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1792 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1794 struct isl_basic_map
*ext
;
1804 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1805 base
->extra
>= base
->n_div
+ extra
;
1807 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1808 room_for_ineq(base
, n_ineq
)) {
1809 isl_space_free(space
);
1813 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1814 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1815 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1816 extra
+= base
->extra
;
1818 n_ineq
+= base
->n_ineq
;
1820 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1826 ext
->sample
= isl_vec_copy(base
->sample
);
1827 flags
= base
->flags
;
1828 ext
= add_constraints(ext
, base
, 0, 0);
1831 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1837 isl_space_free(space
);
1838 isl_basic_map_free(base
);
1842 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1843 __isl_take isl_basic_set
*base
,
1844 __isl_take isl_space
*dim
, unsigned extra
,
1845 unsigned n_eq
, unsigned n_ineq
)
1847 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1848 dim
, extra
, n_eq
, n_ineq
));
1851 struct isl_basic_map
*isl_basic_map_extend_constraints(
1852 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1856 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1860 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1861 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1862 unsigned n_eq
, unsigned n_ineq
)
1864 struct isl_basic_map
*bmap
;
1869 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1873 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1876 isl_basic_map_free(base
);
1880 struct isl_basic_set
*isl_basic_set_extend_constraints(
1881 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1883 isl_basic_map
*bmap
= bset_to_bmap(base
);
1884 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1885 return bset_from_bmap(bmap
);
1888 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1890 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1893 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1898 if (bmap
->ref
> 1) {
1900 bmap
= isl_basic_map_dup(bmap
);
1903 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1904 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1909 /* Clear all cached information in "map", either because it is about
1910 * to be modified or because it is being freed.
1911 * Always return the same pointer that is passed in.
1912 * This is needed for the use in isl_map_free.
1914 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1916 isl_basic_map_free(map
->cached_simple_hull
[0]);
1917 isl_basic_map_free(map
->cached_simple_hull
[1]);
1918 map
->cached_simple_hull
[0] = NULL
;
1919 map
->cached_simple_hull
[1] = NULL
;
1923 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1925 return isl_map_cow(set
);
1928 /* Return an isl_map that is equal to "map" and that has only
1929 * a single reference.
1931 * If the original input already has only one reference, then
1932 * simply return it, but clear all cached information, since
1933 * it may be rendered invalid by the operations that will be
1934 * performed on the result.
1936 * Otherwise, create a duplicate (without any cached information).
1938 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1944 return clear_caches(map
);
1946 return isl_map_dup(map
);
1949 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1950 unsigned a_len
, unsigned b_len
)
1952 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1953 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1954 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1957 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1958 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1963 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
1966 if (n1
== 0 || n2
== 0)
1969 bmap
= isl_basic_map_cow(bmap
);
1973 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1974 if (isl_blk_is_error(blk
))
1977 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1979 bmap
->eq
[i
] + pos
, n1
, n2
);
1981 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1983 bmap
->ineq
[i
] + pos
, n1
, n2
);
1985 for (i
= 0; i
< bmap
->n_div
; ++i
)
1987 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1989 isl_blk_free(bmap
->ctx
, blk
);
1991 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
1992 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1993 return isl_basic_map_finalize(bmap
);
1995 isl_basic_map_free(bmap
);
1999 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2000 __isl_take isl_basic_map
*bmap
)
2006 total
= isl_basic_map_total_dim(bmap
);
2007 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2008 return isl_basic_map_free(bmap
);
2009 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2011 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2013 i
= isl_basic_map_alloc_equality(bmap
);
2017 isl_int_set_si(bmap
->eq
[i
][0], 1);
2018 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2019 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2020 isl_vec_free(bmap
->sample
);
2021 bmap
->sample
= NULL
;
2022 return isl_basic_map_finalize(bmap
);
2024 isl_basic_map_free(bmap
);
2028 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2029 __isl_take isl_basic_set
*bset
)
2031 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2034 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2035 __isl_take isl_basic_map
*bmap
)
2040 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2043 bmap
= isl_basic_map_cow(bmap
);
2047 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2049 return isl_basic_map_finalize(bmap
);
2052 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2053 __isl_take isl_basic_set
*bset
)
2055 return isl_basic_map_set_rational(bset
);
2058 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2059 __isl_take isl_basic_set
*bset
)
2064 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2067 bset
= isl_basic_set_cow(bset
);
2071 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2073 return isl_basic_set_finalize(bset
);
2076 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2080 map
= isl_map_cow(map
);
2083 for (i
= 0; i
< map
->n
; ++i
) {
2084 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2094 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2096 return isl_map_set_rational(set
);
2099 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2102 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2104 isl_int
*t
= bmap
->div
[a
];
2105 bmap
->div
[a
] = bmap
->div
[b
];
2109 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2110 * div definitions accordingly.
2112 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2118 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2120 return isl_basic_map_free(bmap
);
2122 swap_div(bmap
, a
, b
);
2124 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2125 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2127 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2128 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2130 for (i
= 0; i
< bmap
->n_div
; ++i
)
2131 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2132 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2137 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2139 isl_seq_cpy(c
, c
+ n
, rem
);
2140 isl_seq_clr(c
+ rem
, n
);
2143 /* Drop n dimensions starting at first.
2145 * In principle, this frees up some extra variables as the number
2146 * of columns remains constant, but we would have to extend
2147 * the div array too as the number of rows in this array is assumed
2148 * to be equal to extra.
2150 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2151 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2153 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2156 /* Move "n" divs starting at "first" to the end of the list of divs.
2158 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2159 unsigned first
, unsigned n
)
2164 if (first
+ n
== bmap
->n_div
)
2167 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2170 for (i
= 0; i
< n
; ++i
)
2171 div
[i
] = bmap
->div
[first
+ i
];
2172 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2173 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2174 for (i
= 0; i
< n
; ++i
)
2175 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2179 isl_basic_map_free(bmap
);
2184 #define TYPE isl_map
2186 #include "check_type_range_templ.c"
2188 /* Check that there are "n" dimensions of type "type" starting at "first"
2191 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2192 enum isl_dim_type type
, unsigned first
, unsigned n
)
2194 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2197 /* Drop "n" dimensions of type "type" starting at "first".
2198 * Perform the core computation, without cowing or
2199 * simplifying and finalizing the result.
2201 * In principle, this frees up some extra variables as the number
2202 * of columns remains constant, but we would have to extend
2203 * the div array too as the number of rows in this array is assumed
2204 * to be equal to extra.
2206 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2207 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2208 unsigned first
, unsigned n
)
2214 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2215 return isl_basic_map_free(bmap
);
2217 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2218 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2219 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2220 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2222 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2223 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2225 for (i
= 0; i
< bmap
->n_div
; ++i
)
2226 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2228 if (type
== isl_dim_div
) {
2229 bmap
= move_divs_last(bmap
, first
, n
);
2232 if (isl_basic_map_free_div(bmap
, n
) < 0)
2233 return isl_basic_map_free(bmap
);
2235 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2237 return isl_basic_map_free(bmap
);
2239 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2240 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2244 /* Drop "n" dimensions of type "type" starting at "first".
2246 * In principle, this frees up some extra variables as the number
2247 * of columns remains constant, but we would have to extend
2248 * the div array too as the number of rows in this array is assumed
2249 * to be equal to extra.
2251 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2252 enum isl_dim_type type
, unsigned first
, unsigned n
)
2256 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2259 bmap
= isl_basic_map_cow(bmap
);
2263 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2265 bmap
= isl_basic_map_simplify(bmap
);
2266 return isl_basic_map_finalize(bmap
);
2269 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2270 enum isl_dim_type type
, unsigned first
, unsigned n
)
2272 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2276 /* No longer consider "map" to be normalized.
2278 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2282 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2286 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2287 enum isl_dim_type type
, unsigned first
, unsigned n
)
2291 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2292 return isl_map_free(map
);
2294 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2296 map
= isl_map_cow(map
);
2299 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2303 for (i
= 0; i
< map
->n
; ++i
) {
2304 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2308 map
= isl_map_unmark_normalized(map
);
2316 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2317 enum isl_dim_type type
, unsigned first
, unsigned n
)
2319 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2322 /* Drop the integer division at position "div", which is assumed
2323 * not to appear in any of the constraints or
2324 * in any of the other integer divisions.
2326 * Since the integer division is redundant, there is no need to cow.
2328 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2329 __isl_take isl_basic_map
*bmap
, unsigned div
)
2331 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2334 /* Eliminate the specified n dimensions starting at first from the
2335 * constraints, without removing the dimensions from the space.
2336 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2338 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2339 enum isl_dim_type type
, unsigned first
, unsigned n
)
2346 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2347 return isl_map_free(map
);
2349 map
= isl_map_cow(map
);
2353 for (i
= 0; i
< map
->n
; ++i
) {
2354 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2364 /* Eliminate the specified n dimensions starting at first from the
2365 * constraints, without removing the dimensions from the space.
2366 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2368 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2369 enum isl_dim_type type
, unsigned first
, unsigned n
)
2371 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
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_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2379 unsigned first
, unsigned n
)
2381 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2384 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2385 __isl_take isl_basic_map
*bmap
)
2389 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2391 return isl_basic_map_free(bmap
);
2392 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2396 return isl_basic_map_finalize(bmap
);
2399 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2400 __isl_take isl_basic_set
*bset
)
2402 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2405 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2414 map
= isl_map_cow(map
);
2418 for (i
= 0; i
< map
->n
; ++i
) {
2419 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2429 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2431 return isl_map_remove_divs(set
);
2434 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2435 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2436 unsigned first
, unsigned n
)
2438 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2439 return isl_basic_map_free(bmap
);
2440 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2442 bmap
= isl_basic_map_eliminate_vars(bmap
,
2443 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2446 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2448 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2452 /* Return true if the definition of the given div (recursively) involves
2453 * any of the given variables.
2455 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2456 unsigned first
, unsigned n
)
2459 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2461 if (isl_int_is_zero(bmap
->div
[div
][0]))
2462 return isl_bool_false
;
2463 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2464 return isl_bool_true
;
2466 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2469 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2471 involves
= div_involves_vars(bmap
, i
, first
, n
);
2472 if (involves
< 0 || involves
)
2476 return isl_bool_false
;
2479 /* Try and add a lower and/or upper bound on "div" to "bmap"
2480 * based on inequality "i".
2481 * "total" is the total number of variables (excluding the divs).
2482 * "v" is a temporary object that can be used during the calculations.
2483 * If "lb" is set, then a lower bound should be constructed.
2484 * If "ub" is set, then an upper bound should be constructed.
2486 * The calling function has already checked that the inequality does not
2487 * reference "div", but we still need to check that the inequality is
2488 * of the right form. We'll consider the case where we want to construct
2489 * a lower bound. The construction of upper bounds is similar.
2491 * Let "div" be of the form
2493 * q = floor((a + f(x))/d)
2495 * We essentially check if constraint "i" is of the form
2499 * so that we can use it to derive a lower bound on "div".
2500 * However, we allow a slightly more general form
2504 * with the condition that the coefficients of g(x) - f(x) are all
2506 * Rewriting this constraint as
2510 * adding a + f(x) to both sides and dividing by d, we obtain
2512 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2514 * Taking the floor on both sides, we obtain
2516 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2520 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2522 * In the case of an upper bound, we construct the constraint
2524 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2527 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2528 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2529 unsigned total
, isl_int v
, int lb
, int ub
)
2533 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2535 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2536 bmap
->div
[div
][1 + 1 + j
]);
2537 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2540 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2541 bmap
->div
[div
][1 + 1 + j
]);
2542 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2548 bmap
= isl_basic_map_cow(bmap
);
2549 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2551 int k
= isl_basic_map_alloc_inequality(bmap
);
2554 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2555 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2556 bmap
->div
[div
][1 + j
]);
2557 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2558 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2560 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2563 int k
= isl_basic_map_alloc_inequality(bmap
);
2566 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2567 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2568 bmap
->div
[div
][1 + j
]);
2569 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2570 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2572 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2577 isl_basic_map_free(bmap
);
2581 /* This function is called right before "div" is eliminated from "bmap"
2582 * using Fourier-Motzkin.
2583 * Look through the constraints of "bmap" for constraints on the argument
2584 * of the integer division and use them to construct constraints on the
2585 * integer division itself. These constraints can then be combined
2586 * during the Fourier-Motzkin elimination.
2587 * Note that it is only useful to introduce lower bounds on "div"
2588 * if "bmap" already contains upper bounds on "div" as the newly
2589 * introduce lower bounds can then be combined with the pre-existing
2590 * upper bounds. Similarly for upper bounds.
2591 * We therefore first check if "bmap" contains any lower and/or upper bounds
2594 * It is interesting to note that the introduction of these constraints
2595 * can indeed lead to more accurate results, even when compared to
2596 * deriving constraints on the argument of "div" from constraints on "div".
2597 * Consider, for example, the set
2599 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2601 * The second constraint can be rewritten as
2603 * 2 * [(-i-2j+3)/4] + k >= 0
2605 * from which we can derive
2607 * -i - 2j + 3 >= -2k
2613 * Combined with the first constraint, we obtain
2615 * -3 <= 3 + 2k or k >= -3
2617 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2618 * the first constraint, we obtain
2620 * [(i + 2j)/4] >= [-3/4] = -1
2622 * Combining this constraint with the second constraint, we obtain
2626 static __isl_give isl_basic_map
*insert_bounds_on_div(
2627 __isl_take isl_basic_map
*bmap
, int div
)
2630 int check_lb
, check_ub
;
2637 if (isl_int_is_zero(bmap
->div
[div
][0]))
2640 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2642 return isl_basic_map_free(bmap
);
2646 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2647 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2654 if (!check_lb
&& !check_ub
)
2659 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2660 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2663 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2664 check_lb
, check_ub
);
2672 /* Remove all divs (recursively) involving any of the given dimensions
2673 * in their definitions.
2675 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2676 __isl_take isl_basic_map
*bmap
,
2677 enum isl_dim_type type
, unsigned first
, unsigned n
)
2681 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2682 return isl_basic_map_free(bmap
);
2683 first
+= isl_basic_map_offset(bmap
, type
);
2685 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2688 involves
= div_involves_vars(bmap
, i
, first
, n
);
2690 return isl_basic_map_free(bmap
);
2693 bmap
= insert_bounds_on_div(bmap
, i
);
2694 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2703 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2704 __isl_take isl_basic_set
*bset
,
2705 enum isl_dim_type type
, unsigned first
, unsigned n
)
2707 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2710 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2711 enum isl_dim_type type
, unsigned first
, unsigned n
)
2720 map
= isl_map_cow(map
);
2724 for (i
= 0; i
< map
->n
; ++i
) {
2725 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2736 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2737 enum isl_dim_type type
, unsigned first
, unsigned n
)
2739 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2743 /* Does the description of "bmap" depend on the specified dimensions?
2744 * We also check whether the dimensions appear in any of the div definitions.
2745 * In principle there is no need for this check. If the dimensions appear
2746 * in a div definition, they also appear in the defining constraints of that
2749 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2750 enum isl_dim_type type
, unsigned first
, unsigned n
)
2754 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2755 return isl_bool_error
;
2757 first
+= isl_basic_map_offset(bmap
, type
);
2758 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2759 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2760 return isl_bool_true
;
2761 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2762 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2763 return isl_bool_true
;
2764 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2765 if (isl_int_is_zero(bmap
->div
[i
][0]))
2767 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2768 return isl_bool_true
;
2771 return isl_bool_false
;
2774 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2775 enum isl_dim_type type
, unsigned first
, unsigned n
)
2779 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2780 return isl_bool_error
;
2782 for (i
= 0; i
< map
->n
; ++i
) {
2783 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2785 if (involves
< 0 || involves
)
2789 return isl_bool_false
;
2792 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2793 enum isl_dim_type type
, unsigned first
, unsigned n
)
2795 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2798 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2799 enum isl_dim_type type
, unsigned first
, unsigned n
)
2801 return isl_map_involves_dims(set
, type
, first
, n
);
2804 /* Drop all constraints in bmap that involve any of the dimensions
2805 * first to first+n-1.
2806 * This function only performs the actual removal of constraints.
2808 * This function should not call finalize since it is used by
2809 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2811 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2812 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2819 bmap
= isl_basic_map_cow(bmap
);
2824 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2825 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2827 isl_basic_map_drop_equality(bmap
, i
);
2830 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2831 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2833 isl_basic_map_drop_inequality(bmap
, i
);
2839 /* Drop all constraints in bset that involve any of the dimensions
2840 * first to first+n-1.
2841 * This function only performs the actual removal of constraints.
2843 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2844 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2846 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2849 /* Drop all constraints in bmap that do not involve any of the dimensions
2850 * first to first + n - 1 of the given type.
2852 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2853 __isl_take isl_basic_map
*bmap
,
2854 enum isl_dim_type type
, unsigned first
, unsigned n
)
2859 isl_space
*space
= isl_basic_map_get_space(bmap
);
2860 isl_basic_map_free(bmap
);
2861 return isl_basic_map_universe(space
);
2863 bmap
= isl_basic_map_cow(bmap
);
2867 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2868 return isl_basic_map_free(bmap
);
2870 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2872 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2873 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2875 isl_basic_map_drop_equality(bmap
, i
);
2878 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2879 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2881 isl_basic_map_drop_inequality(bmap
, i
);
2884 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2888 /* Drop all constraints in bset that do not involve any of the dimensions
2889 * first to first + n - 1 of the given type.
2891 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2892 __isl_take isl_basic_set
*bset
,
2893 enum isl_dim_type type
, unsigned first
, unsigned n
)
2895 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2899 /* Drop all constraints in bmap that involve any of the dimensions
2900 * first to first + n - 1 of the given type.
2902 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2903 __isl_take isl_basic_map
*bmap
,
2904 enum isl_dim_type type
, unsigned first
, unsigned n
)
2911 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2912 return isl_basic_map_free(bmap
);
2914 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2915 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2916 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2917 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2921 /* Drop all constraints in bset that involve any of the dimensions
2922 * first to first + n - 1 of the given type.
2924 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2925 __isl_take isl_basic_set
*bset
,
2926 enum isl_dim_type type
, unsigned first
, unsigned n
)
2928 return isl_basic_map_drop_constraints_involving_dims(bset
,
2932 /* Drop constraints from "map" by applying "drop" to each basic map.
2934 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2935 enum isl_dim_type type
, unsigned first
, unsigned n
,
2936 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2937 enum isl_dim_type type
, unsigned first
, unsigned n
))
2941 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2942 return isl_map_free(map
);
2944 map
= isl_map_cow(map
);
2948 for (i
= 0; i
< map
->n
; ++i
) {
2949 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2951 return isl_map_free(map
);
2955 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2960 /* Drop all constraints in map that involve any of the dimensions
2961 * first to first + n - 1 of the given type.
2963 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2964 __isl_take isl_map
*map
,
2965 enum isl_dim_type type
, unsigned first
, unsigned n
)
2969 return drop_constraints(map
, type
, first
, n
,
2970 &isl_basic_map_drop_constraints_involving_dims
);
2973 /* Drop all constraints in "map" that do not involve any of the dimensions
2974 * first to first + n - 1 of the given type.
2976 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2977 __isl_take isl_map
*map
,
2978 enum isl_dim_type type
, unsigned first
, unsigned n
)
2981 isl_space
*space
= isl_map_get_space(map
);
2983 return isl_map_universe(space
);
2985 return drop_constraints(map
, type
, first
, n
,
2986 &isl_basic_map_drop_constraints_not_involving_dims
);
2989 /* Drop all constraints in set that involve any of the dimensions
2990 * first to first + n - 1 of the given type.
2992 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2993 __isl_take isl_set
*set
,
2994 enum isl_dim_type type
, unsigned first
, unsigned n
)
2996 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2999 /* Drop all constraints in "set" that do not involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3003 __isl_take isl_set
*set
,
3004 enum isl_dim_type type
, unsigned first
, unsigned n
)
3006 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3009 /* Does local variable "div" of "bmap" have a complete explicit representation?
3010 * Having a complete explicit representation requires not only
3011 * an explicit representation, but also that all local variables
3012 * that appear in this explicit representation in turn have
3013 * a complete explicit representation.
3015 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3018 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3021 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3022 if (marked
< 0 || marked
)
3023 return isl_bool_not(marked
);
3025 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3028 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3030 known
= isl_basic_map_div_is_known(bmap
, i
);
3031 if (known
< 0 || !known
)
3035 return isl_bool_true
;
3038 /* Remove all divs that are unknown or defined in terms of unknown divs.
3040 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3041 __isl_take isl_basic_map
*bmap
)
3048 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3049 if (isl_basic_map_div_is_known(bmap
, i
))
3051 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3060 /* Remove all divs that are unknown or defined in terms of unknown divs.
3062 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3063 __isl_take isl_basic_set
*bset
)
3065 return isl_basic_map_remove_unknown_divs(bset
);
3068 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3077 map
= isl_map_cow(map
);
3081 for (i
= 0; i
< map
->n
; ++i
) {
3082 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3092 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3094 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3097 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3098 __isl_take isl_basic_set
*bset
,
3099 enum isl_dim_type type
, unsigned first
, unsigned n
)
3101 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3102 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3103 return bset_from_bmap(bmap
);
3106 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3107 enum isl_dim_type type
, unsigned first
, unsigned n
)
3114 map
= isl_map_cow(map
);
3115 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3116 return isl_map_free(map
);
3118 for (i
= 0; i
< map
->n
; ++i
) {
3119 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3120 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3124 map
= isl_map_drop(map
, type
, first
, n
);
3131 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3132 enum isl_dim_type type
, unsigned first
, unsigned n
)
3134 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3138 /* Project out n inputs starting at first using Fourier-Motzkin */
3139 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3140 unsigned first
, unsigned n
)
3142 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3145 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3146 FILE *out
, int indent
)
3151 fprintf(out
, "null basic set\n");
3155 fprintf(out
, "%*s", indent
, "");
3156 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3157 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3158 bset
->extra
, bset
->flags
);
3160 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3161 p
= isl_printer_set_dump(p
, 1);
3162 p
= isl_printer_set_indent(p
, indent
);
3163 p
= isl_printer_start_line(p
);
3164 p
= isl_printer_print_basic_set(p
, bset
);
3165 p
= isl_printer_end_line(p
);
3166 isl_printer_free(p
);
3169 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3170 FILE *out
, int indent
)
3175 fprintf(out
, "null basic map\n");
3179 fprintf(out
, "%*s", indent
, "");
3180 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3181 "flags: %x, n_name: %d\n",
3183 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3184 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3186 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3187 p
= isl_printer_set_dump(p
, 1);
3188 p
= isl_printer_set_indent(p
, indent
);
3189 p
= isl_printer_start_line(p
);
3190 p
= isl_printer_print_basic_map(p
, bmap
);
3191 p
= isl_printer_end_line(p
);
3192 isl_printer_free(p
);
3195 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3202 total
= isl_basic_map_total_dim(bmap
);
3203 if (pos
>= bmap
->n_ineq
)
3204 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3205 "invalid position", return isl_basic_map_free(bmap
));
3206 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3207 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3208 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3213 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3216 if (isl_space_check_is_set(space
) < 0)
3218 return isl_map_alloc_space(space
, n
, flags
);
3220 isl_space_free(space
);
3224 /* Make sure "map" has room for at least "n" more basic maps.
3226 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3229 struct isl_map
*grown
= NULL
;
3233 isl_assert(map
->ctx
, n
>= 0, goto error
);
3234 if (map
->n
+ n
<= map
->size
)
3236 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3239 for (i
= 0; i
< map
->n
; ++i
) {
3240 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3248 isl_map_free(grown
);
3253 /* Make sure "set" has room for at least "n" more basic sets.
3255 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3257 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3260 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3262 return isl_map_from_basic_map(bset
);
3265 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3267 struct isl_map
*map
;
3272 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3273 return isl_map_add_basic_map(map
, bmap
);
3276 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3277 __isl_take isl_basic_set
*bset
)
3279 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3280 bset_to_bmap(bset
)));
3283 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3285 return isl_map_free(set
);
3288 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3293 fprintf(out
, "null set\n");
3297 fprintf(out
, "%*s", indent
, "");
3298 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3299 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3301 for (i
= 0; i
< set
->n
; ++i
) {
3302 fprintf(out
, "%*s", indent
, "");
3303 fprintf(out
, "basic set %d:\n", i
);
3304 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3308 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3313 fprintf(out
, "null map\n");
3317 fprintf(out
, "%*s", indent
, "");
3318 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3319 "flags: %x, n_name: %d\n",
3320 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3321 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3322 for (i
= 0; i
< map
->n
; ++i
) {
3323 fprintf(out
, "%*s", indent
, "");
3324 fprintf(out
, "basic map %d:\n", i
);
3325 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3329 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3330 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3332 struct isl_basic_map
*bmap_domain
;
3334 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3337 if (isl_basic_set_dim(bset
, isl_dim_set
) != 0)
3338 isl_assert(bset
->ctx
,
3339 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3341 bmap
= isl_basic_map_cow(bmap
);
3344 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3345 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3346 bmap_domain
= isl_basic_map_from_domain(bset
);
3347 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3349 bmap
= isl_basic_map_simplify(bmap
);
3350 return isl_basic_map_finalize(bmap
);
3352 isl_basic_map_free(bmap
);
3353 isl_basic_set_free(bset
);
3357 /* Check that the space of "bset" is the same as that of the range of "bmap".
3359 static isl_stat
isl_basic_map_check_compatible_range(
3360 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3364 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3366 return isl_stat_error
;
3368 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3369 "incompatible spaces", return isl_stat_error
);
3374 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3375 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3377 struct isl_basic_map
*bmap_range
;
3379 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3382 if (isl_basic_set_dim(bset
, isl_dim_set
) != 0 &&
3383 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3386 if (isl_basic_set_plain_is_universe(bset
)) {
3387 isl_basic_set_free(bset
);
3391 bmap
= isl_basic_map_cow(bmap
);
3394 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3395 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3396 bmap_range
= bset_to_bmap(bset
);
3397 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3399 bmap
= isl_basic_map_simplify(bmap
);
3400 return isl_basic_map_finalize(bmap
);
3402 isl_basic_map_free(bmap
);
3403 isl_basic_set_free(bset
);
3407 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3408 __isl_keep isl_vec
*vec
)
3415 return isl_bool_error
;
3417 total
= 1 + isl_basic_map_total_dim(bmap
);
3418 if (total
!= vec
->size
)
3419 return isl_bool_false
;
3423 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3424 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3425 if (!isl_int_is_zero(s
)) {
3427 return isl_bool_false
;
3431 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3432 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3433 if (isl_int_is_neg(s
)) {
3435 return isl_bool_false
;
3441 return isl_bool_true
;
3444 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3445 __isl_keep isl_vec
*vec
)
3447 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3450 __isl_give isl_basic_map
*isl_basic_map_intersect(
3451 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3453 struct isl_vec
*sample
= NULL
;
3454 isl_space
*space1
, *space2
;
3456 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3458 space1
= isl_basic_map_peek_space(bmap1
);
3459 space2
= isl_basic_map_peek_space(bmap2
);
3460 if (isl_space_dim(space1
, isl_dim_all
) ==
3461 isl_space_dim(space1
, isl_dim_param
) &&
3462 isl_space_dim(space2
, isl_dim_all
) !=
3463 isl_space_dim(space2
, isl_dim_param
))
3464 return isl_basic_map_intersect(bmap2
, bmap1
);
3466 if (isl_space_dim(space2
, isl_dim_all
) !=
3467 isl_space_dim(space2
, isl_dim_param
))
3468 isl_assert(bmap1
->ctx
,
3469 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3471 if (isl_basic_map_plain_is_empty(bmap1
)) {
3472 isl_basic_map_free(bmap2
);
3475 if (isl_basic_map_plain_is_empty(bmap2
)) {
3476 isl_basic_map_free(bmap1
);
3480 if (bmap1
->sample
&&
3481 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3482 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3483 sample
= isl_vec_copy(bmap1
->sample
);
3484 else if (bmap2
->sample
&&
3485 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3486 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3487 sample
= isl_vec_copy(bmap2
->sample
);
3489 bmap1
= isl_basic_map_cow(bmap1
);
3492 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3493 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3494 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3497 isl_vec_free(sample
);
3499 isl_vec_free(bmap1
->sample
);
3500 bmap1
->sample
= sample
;
3503 bmap1
= isl_basic_map_simplify(bmap1
);
3504 return isl_basic_map_finalize(bmap1
);
3507 isl_vec_free(sample
);
3508 isl_basic_map_free(bmap1
);
3509 isl_basic_map_free(bmap2
);
3513 struct isl_basic_set
*isl_basic_set_intersect(
3514 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3516 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3517 bset_to_bmap(bset2
)));
3520 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3521 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3523 return isl_basic_set_intersect(bset1
, bset2
);
3526 /* Special case of isl_map_intersect, where both map1 and map2
3527 * are convex, without any divs and such that either map1 or map2
3528 * contains a single constraint. This constraint is then simply
3529 * added to the other map.
3531 static __isl_give isl_map
*map_intersect_add_constraint(
3532 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3534 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3535 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3536 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3537 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3539 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3540 return isl_map_intersect(map2
, map1
);
3542 map1
= isl_map_cow(map1
);
3545 if (isl_map_plain_is_empty(map1
)) {
3549 if (map2
->p
[0]->n_eq
== 1)
3550 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3552 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3553 map2
->p
[0]->ineq
[0]);
3555 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3556 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3560 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3561 isl_basic_map_free(map1
->p
[0]);
3567 map1
= isl_map_unmark_normalized(map1
);
3575 /* map2 may be either a parameter domain or a map living in the same
3578 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3579 __isl_take isl_map
*map2
)
3589 if ((isl_map_plain_is_empty(map1
) ||
3590 isl_map_plain_is_universe(map2
)) &&
3591 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3595 if ((isl_map_plain_is_empty(map2
) ||
3596 isl_map_plain_is_universe(map1
)) &&
3597 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3602 if (map1
->n
== 1 && map2
->n
== 1 &&
3603 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3604 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3605 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3606 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3607 return map_intersect_add_constraint(map1
, map2
);
3609 equal
= isl_map_plain_is_equal(map1
, map2
);
3617 if (isl_map_dim(map2
, isl_dim_all
) != isl_map_dim(map2
, isl_dim_param
))
3618 isl_assert(map1
->ctx
,
3619 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3621 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3622 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3623 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3625 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3626 map1
->n
* map2
->n
, flags
);
3629 for (i
= 0; i
< map1
->n
; ++i
)
3630 for (j
= 0; j
< map2
->n
; ++j
) {
3631 struct isl_basic_map
*part
;
3632 part
= isl_basic_map_intersect(
3633 isl_basic_map_copy(map1
->p
[i
]),
3634 isl_basic_map_copy(map2
->p
[j
]));
3635 if (isl_basic_map_is_empty(part
) < 0)
3636 part
= isl_basic_map_free(part
);
3637 result
= isl_map_add_basic_map(result
, part
);
3650 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3651 __isl_take isl_map
*map2
)
3655 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3656 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3657 "spaces don't match", goto error
);
3658 return map_intersect_internal(map1
, map2
);
3665 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3666 __isl_take isl_map
*map2
)
3668 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3671 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3673 return set_from_map(isl_map_intersect(set_to_map(set1
),
3677 /* map_intersect_internal accepts intersections
3678 * with parameter domains, so we can just call that function.
3680 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3681 __isl_take isl_set
*params
)
3683 return map_intersect_internal(map
, params
);
3686 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3687 __isl_take isl_map
*map2
)
3689 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3692 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3693 __isl_take isl_set
*params
)
3695 return isl_map_intersect_params(set
, params
);
3698 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3701 unsigned pos
, n1
, n2
;
3705 bmap
= isl_basic_map_cow(bmap
);
3708 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3709 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3710 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3711 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3712 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3713 return isl_basic_map_reset_space(bmap
, space
);
3716 static __isl_give isl_basic_map
*basic_map_space_reset(
3717 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3723 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3726 space
= isl_basic_map_get_space(bmap
);
3727 space
= isl_space_reset(space
, type
);
3728 bmap
= isl_basic_map_reset_space(bmap
, space
);
3732 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3733 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3734 unsigned pos
, unsigned n
)
3736 isl_bool rational
, is_empty
;
3737 isl_space
*res_space
;
3738 struct isl_basic_map
*res
;
3739 struct isl_dim_map
*dim_map
;
3740 unsigned total
, off
;
3741 enum isl_dim_type t
;
3744 return basic_map_space_reset(bmap
, type
);
3746 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3748 return isl_basic_map_free(bmap
);
3749 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3752 return isl_basic_map_free(bmap
);
3754 isl_basic_map_free(bmap
);
3755 return isl_basic_map_empty(res_space
);
3758 total
= isl_basic_map_total_dim(bmap
) + n
;
3759 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3761 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3763 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3765 unsigned size
= isl_basic_map_dim(bmap
, t
);
3766 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3768 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3769 pos
, size
- pos
, off
+ pos
+ n
);
3771 off
+= isl_space_dim(res_space
, t
);
3773 isl_dim_map_div(dim_map
, bmap
, off
);
3775 res
= isl_basic_map_alloc_space(res_space
,
3776 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3777 rational
= isl_basic_map_is_rational(bmap
);
3779 res
= isl_basic_map_free(res
);
3781 res
= isl_basic_map_set_rational(res
);
3782 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3783 return isl_basic_map_finalize(res
);
3786 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3787 __isl_take isl_basic_set
*bset
,
3788 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3790 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3793 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3794 enum isl_dim_type type
, unsigned n
)
3798 return isl_basic_map_insert_dims(bmap
, type
,
3799 isl_basic_map_dim(bmap
, type
), n
);
3802 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3803 enum isl_dim_type type
, unsigned n
)
3807 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3808 return isl_basic_map_add_dims(bset
, type
, n
);
3810 isl_basic_set_free(bset
);
3814 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3815 enum isl_dim_type type
)
3819 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3822 space
= isl_map_get_space(map
);
3823 space
= isl_space_reset(space
, type
);
3824 map
= isl_map_reset_space(map
, space
);
3828 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3829 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3834 return map_space_reset(map
, type
);
3836 map
= isl_map_cow(map
);
3840 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3844 for (i
= 0; i
< map
->n
; ++i
) {
3845 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3856 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3857 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3859 return isl_map_insert_dims(set
, type
, pos
, n
);
3862 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3863 enum isl_dim_type type
, unsigned n
)
3867 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3870 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3871 enum isl_dim_type type
, unsigned n
)
3875 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3876 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3882 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3883 __isl_take isl_basic_map
*bmap
,
3884 enum isl_dim_type dst_type
, unsigned dst_pos
,
3885 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3888 struct isl_dim_map
*dim_map
;
3889 struct isl_basic_map
*res
;
3890 enum isl_dim_type t
;
3891 unsigned total
, off
;
3896 bmap
= isl_basic_map_reset(bmap
, src_type
);
3897 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3901 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3902 return isl_basic_map_free(bmap
);
3904 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3907 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3909 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3910 pos(bmap
->dim
, src_type
) + src_pos
+
3911 ((src_type
< dst_type
) ? n
: 0)) {
3912 bmap
= isl_basic_map_cow(bmap
);
3916 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3917 src_type
, src_pos
, n
);
3921 bmap
= isl_basic_map_finalize(bmap
);
3926 total
= isl_basic_map_total_dim(bmap
);
3927 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3930 space
= isl_basic_map_peek_space(bmap
);
3931 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3932 unsigned size
= isl_space_dim(space
, t
);
3933 if (t
== dst_type
) {
3934 isl_dim_map_dim_range(dim_map
, space
, t
,
3937 isl_dim_map_dim_range(dim_map
, space
, src_type
,
3940 isl_dim_map_dim_range(dim_map
, space
, t
,
3941 dst_pos
, size
- dst_pos
, off
);
3942 off
+= size
- dst_pos
;
3943 } else if (t
== src_type
) {
3944 isl_dim_map_dim_range(dim_map
, space
, t
,
3947 isl_dim_map_dim_range(dim_map
, space
, t
,
3948 src_pos
+ n
, size
- src_pos
- n
, off
);
3949 off
+= size
- src_pos
- n
;
3951 isl_dim_map_dim(dim_map
, space
, t
, off
);
3955 isl_dim_map_div(dim_map
, bmap
, off
);
3957 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3958 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3959 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3963 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3964 src_type
, src_pos
, n
);
3968 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3969 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3970 bmap
= isl_basic_map_finalize(bmap
);
3974 isl_basic_map_free(bmap
);
3978 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3979 enum isl_dim_type dst_type
, unsigned dst_pos
,
3980 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3982 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3983 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3984 src_type
, src_pos
, n
);
3985 return bset_from_bmap(bmap
);
3988 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3989 enum isl_dim_type dst_type
, unsigned dst_pos
,
3990 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3994 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3995 return set_from_map(isl_map_move_dims(set_to_map(set
),
3996 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4002 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4003 enum isl_dim_type dst_type
, unsigned dst_pos
,
4004 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4009 map
= isl_map_reset(map
, src_type
);
4010 map
= isl_map_reset(map
, dst_type
);
4014 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4015 return isl_map_free(map
);
4017 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4020 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4022 map
= isl_map_cow(map
);
4026 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4030 for (i
= 0; i
< map
->n
; ++i
) {
4031 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4033 src_type
, src_pos
, n
);
4044 /* Move the specified dimensions to the last columns right before
4045 * the divs. Don't change the dimension specification of bmap.
4046 * That's the responsibility of the caller.
4048 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4049 enum isl_dim_type type
, unsigned first
, unsigned n
)
4052 struct isl_dim_map
*dim_map
;
4053 struct isl_basic_map
*res
;
4054 enum isl_dim_type t
;
4055 unsigned total
, off
;
4059 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4060 isl_basic_map_offset(bmap
, isl_dim_div
))
4063 total
= isl_basic_map_total_dim(bmap
);
4064 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4067 space
= isl_basic_map_peek_space(bmap
);
4068 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4069 unsigned size
= isl_space_dim(space
, t
);
4071 isl_dim_map_dim_range(dim_map
, space
, t
,
4074 isl_dim_map_dim_range(dim_map
, space
, t
,
4075 first
, n
, total
- bmap
->n_div
- n
);
4076 isl_dim_map_dim_range(dim_map
, space
, t
,
4077 first
+ n
, size
- (first
+ n
), off
);
4078 off
+= size
- (first
+ n
);
4080 isl_dim_map_dim(dim_map
, space
, t
, off
);
4084 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4086 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4087 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4088 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4092 /* Insert "n" rows in the divs of "bmap".
4094 * The number of columns is not changed, which means that the last
4095 * dimensions of "bmap" are being reintepreted as the new divs.
4096 * The space of "bmap" is not adjusted, however, which means
4097 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4098 * from the space of "bmap" is the responsibility of the caller.
4100 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4108 bmap
= isl_basic_map_cow(bmap
);
4112 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4113 old
= bmap
->block2
.data
;
4114 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4115 (bmap
->extra
+ n
) * (1 + row_size
));
4116 if (!bmap
->block2
.data
)
4117 return isl_basic_map_free(bmap
);
4118 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4120 return isl_basic_map_free(bmap
);
4121 for (i
= 0; i
< n
; ++i
) {
4122 new_div
[i
] = bmap
->block2
.data
+
4123 (bmap
->extra
+ i
) * (1 + row_size
);
4124 isl_seq_clr(new_div
[i
], 1 + row_size
);
4126 for (i
= 0; i
< bmap
->extra
; ++i
)
4127 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4129 bmap
->div
= new_div
;
4136 /* Drop constraints from "bmap" that only involve the variables
4137 * of "type" in the range [first, first + n] that are not related
4138 * to any of the variables outside that interval.
4139 * These constraints cannot influence the values for the variables
4140 * outside the interval, except in case they cause "bmap" to be empty.
4141 * Only drop the constraints if "bmap" is known to be non-empty.
4143 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4144 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4145 unsigned first
, unsigned n
)
4149 unsigned dim
, n_div
;
4152 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4154 return isl_basic_map_free(bmap
);
4158 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4159 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4160 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4162 return isl_basic_map_free(bmap
);
4163 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4164 for (i
= 0; i
< first
; ++i
)
4166 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4169 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4174 /* Turn the n dimensions of type type, starting at first
4175 * into existentially quantified variables.
4177 * If a subset of the projected out variables are unrelated
4178 * to any of the variables that remain, then the constraints
4179 * involving this subset are simply dropped first.
4181 __isl_give isl_basic_map
*isl_basic_map_project_out(
4182 __isl_take isl_basic_map
*bmap
,
4183 enum isl_dim_type type
, unsigned first
, unsigned n
)
4188 return basic_map_space_reset(bmap
, type
);
4189 if (type
== isl_dim_div
)
4190 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4191 "cannot project out existentially quantified variables",
4192 return isl_basic_map_free(bmap
));
4194 empty
= isl_basic_map_plain_is_empty(bmap
);
4196 return isl_basic_map_free(bmap
);
4198 bmap
= isl_basic_map_set_to_empty(bmap
);
4200 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4204 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4205 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4207 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4208 return isl_basic_map_free(bmap
);
4210 bmap
= move_last(bmap
, type
, first
, n
);
4211 bmap
= isl_basic_map_cow(bmap
);
4212 bmap
= insert_div_rows(bmap
, n
);
4216 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4219 bmap
= isl_basic_map_simplify(bmap
);
4220 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4221 return isl_basic_map_finalize(bmap
);
4223 isl_basic_map_free(bmap
);
4227 /* Turn the n dimensions of type type, starting at first
4228 * into existentially quantified variables.
4230 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4231 enum isl_dim_type type
, unsigned first
, unsigned n
)
4233 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4237 /* Turn the n dimensions of type type, starting at first
4238 * into existentially quantified variables.
4240 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4241 enum isl_dim_type type
, unsigned first
, unsigned n
)
4246 return map_space_reset(map
, type
);
4248 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4249 return isl_map_free(map
);
4251 map
= isl_map_cow(map
);
4255 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4259 for (i
= 0; i
< map
->n
; ++i
) {
4260 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4271 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4272 * into existentially quantified variables.
4274 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4275 enum isl_dim_type type
, unsigned first
, unsigned n
)
4279 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4280 return isl_map_free(map
);
4281 dim
= isl_map_dim(map
, type
);
4282 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4283 map
= isl_map_project_out(map
, type
, 0, first
);
4287 /* Turn the n dimensions of type type, starting at first
4288 * into existentially quantified variables.
4290 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4291 enum isl_dim_type type
, unsigned first
, unsigned n
)
4293 return set_from_map(isl_map_project_out(set_to_map(set
),
4297 /* Return a map that projects the elements in "set" onto their
4298 * "n" set dimensions starting at "first".
4299 * "type" should be equal to isl_dim_set.
4301 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4302 enum isl_dim_type type
, unsigned first
, unsigned n
)
4307 if (type
!= isl_dim_set
)
4308 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4309 "only set dimensions can be projected out", goto error
);
4310 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4311 return isl_set_free(set
);
4313 map
= isl_map_from_domain(set
);
4314 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4315 for (i
= 0; i
< n
; ++i
)
4316 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4324 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4329 for (i
= 0; i
< n
; ++i
) {
4330 j
= isl_basic_map_alloc_div(bmap
);
4333 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4337 isl_basic_map_free(bmap
);
4341 struct isl_basic_map
*isl_basic_map_apply_range(
4342 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4344 isl_space
*space_result
= NULL
;
4345 struct isl_basic_map
*bmap
;
4346 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4347 struct isl_dim_map
*dim_map1
, *dim_map2
;
4349 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4351 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4352 bmap2
->dim
, isl_dim_in
))
4353 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4354 "spaces don't match", goto error
);
4356 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4357 isl_basic_map_get_space(bmap2
));
4359 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4360 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4361 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4362 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4364 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4365 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4366 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4367 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4368 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4369 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4370 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4371 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4372 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4373 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4374 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4376 bmap
= isl_basic_map_alloc_space(space_result
,
4377 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4378 bmap1
->n_eq
+ bmap2
->n_eq
,
4379 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4380 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4381 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4382 bmap
= add_divs(bmap
, n
);
4383 bmap
= isl_basic_map_simplify(bmap
);
4384 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4385 return isl_basic_map_finalize(bmap
);
4387 isl_basic_map_free(bmap1
);
4388 isl_basic_map_free(bmap2
);
4392 struct isl_basic_set
*isl_basic_set_apply(
4393 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4398 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4401 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4404 isl_basic_set_free(bset
);
4405 isl_basic_map_free(bmap
);
4409 struct isl_basic_map
*isl_basic_map_apply_domain(
4410 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4412 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4414 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4415 bmap2
->dim
, isl_dim_in
))
4416 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4417 "spaces don't match", goto error
);
4419 bmap1
= isl_basic_map_reverse(bmap1
);
4420 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4421 return isl_basic_map_reverse(bmap1
);
4423 isl_basic_map_free(bmap1
);
4424 isl_basic_map_free(bmap2
);
4428 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4429 * A \cap B -> f(A) + f(B)
4431 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4432 __isl_take isl_basic_map
*bmap2
)
4434 unsigned n_in
, n_out
, nparam
, total
, pos
;
4435 struct isl_basic_map
*bmap
= NULL
;
4436 struct isl_dim_map
*dim_map1
, *dim_map2
;
4439 if (!bmap1
|| !bmap2
)
4442 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4445 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4446 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4447 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4449 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4450 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4451 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4452 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4453 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4454 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4455 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4456 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4457 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4458 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4459 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4461 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4462 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4463 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4464 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4465 for (i
= 0; i
< n_out
; ++i
) {
4466 int j
= isl_basic_map_alloc_equality(bmap
);
4469 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4470 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4471 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4472 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4474 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4475 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4476 bmap
= add_divs(bmap
, 2 * n_out
);
4478 bmap
= isl_basic_map_simplify(bmap
);
4479 return isl_basic_map_finalize(bmap
);
4481 isl_basic_map_free(bmap
);
4482 isl_basic_map_free(bmap1
);
4483 isl_basic_map_free(bmap2
);
4487 /* Given two maps A -> f(A) and B -> g(B), construct a map
4488 * A \cap B -> f(A) + f(B)
4490 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4491 __isl_take isl_map
*map2
)
4493 struct isl_map
*result
;
4499 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4501 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4502 map1
->n
* map2
->n
, 0);
4505 for (i
= 0; i
< map1
->n
; ++i
)
4506 for (j
= 0; j
< map2
->n
; ++j
) {
4507 struct isl_basic_map
*part
;
4508 part
= isl_basic_map_sum(
4509 isl_basic_map_copy(map1
->p
[i
]),
4510 isl_basic_map_copy(map2
->p
[j
]));
4511 if (isl_basic_map_is_empty(part
))
4512 isl_basic_map_free(part
);
4514 result
= isl_map_add_basic_map(result
, part
);
4527 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4528 __isl_take isl_set
*set2
)
4530 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4533 /* Given a basic map A -> f(A), construct A -> -f(A).
4535 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4540 bmap
= isl_basic_map_cow(bmap
);
4544 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4545 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4546 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4547 for (j
= 0; j
< n
; ++j
)
4548 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4549 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4550 for (j
= 0; j
< n
; ++j
)
4551 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4552 for (i
= 0; i
< bmap
->n_div
; ++i
)
4553 for (j
= 0; j
< n
; ++j
)
4554 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4555 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4556 return isl_basic_map_finalize(bmap
);
4559 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4561 return isl_basic_map_neg(bset
);
4564 /* Given a map A -> f(A), construct A -> -f(A).
4566 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4570 map
= isl_map_cow(map
);
4574 for (i
= 0; i
< map
->n
; ++i
) {
4575 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4586 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4588 return set_from_map(isl_map_neg(set_to_map(set
)));
4591 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4592 * A -> floor(f(A)/d).
4594 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4597 unsigned n_in
, n_out
, nparam
, total
, pos
;
4598 struct isl_basic_map
*result
= NULL
;
4599 struct isl_dim_map
*dim_map
;
4605 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4606 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4607 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4609 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4610 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4611 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4612 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4613 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4614 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4616 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4617 bmap
->n_div
+ n_out
,
4618 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4619 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4620 result
= add_divs(result
, n_out
);
4621 for (i
= 0; i
< n_out
; ++i
) {
4623 j
= isl_basic_map_alloc_inequality(result
);
4626 isl_seq_clr(result
->ineq
[j
], 1+total
);
4627 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4628 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4629 j
= isl_basic_map_alloc_inequality(result
);
4632 isl_seq_clr(result
->ineq
[j
], 1+total
);
4633 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4634 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4635 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4638 result
= isl_basic_map_simplify(result
);
4639 return isl_basic_map_finalize(result
);
4641 isl_basic_map_free(result
);
4645 /* Given a map A -> f(A) and an integer d, construct a map
4646 * A -> floor(f(A)/d).
4648 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4652 map
= isl_map_cow(map
);
4656 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4657 for (i
= 0; i
< map
->n
; ++i
) {
4658 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4662 map
= isl_map_unmark_normalized(map
);
4670 /* Given a map A -> f(A) and an integer d, construct a map
4671 * A -> floor(f(A)/d).
4673 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4674 __isl_take isl_val
*d
)
4678 if (!isl_val_is_int(d
))
4679 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4680 "expecting integer denominator", goto error
);
4681 map
= isl_map_floordiv(map
, d
->n
);
4690 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4697 i
= isl_basic_map_alloc_equality(bmap
);
4700 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4701 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4702 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4703 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4704 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4705 return isl_basic_map_finalize(bmap
);
4707 isl_basic_map_free(bmap
);
4711 /* Add a constraint to "bmap" expressing i_pos < o_pos
4713 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4720 i
= isl_basic_map_alloc_inequality(bmap
);
4723 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4724 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4725 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4726 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4727 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4728 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4729 return isl_basic_map_finalize(bmap
);
4731 isl_basic_map_free(bmap
);
4735 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4737 static __isl_give isl_basic_map
*var_less_or_equal(
4738 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4744 i
= isl_basic_map_alloc_inequality(bmap
);
4747 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4748 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4749 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4750 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4751 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4752 return isl_basic_map_finalize(bmap
);
4754 isl_basic_map_free(bmap
);
4758 /* Add a constraint to "bmap" expressing i_pos > o_pos
4760 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4767 i
= isl_basic_map_alloc_inequality(bmap
);
4770 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4771 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4772 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4773 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4774 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4775 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4776 return isl_basic_map_finalize(bmap
);
4778 isl_basic_map_free(bmap
);
4782 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4784 static __isl_give isl_basic_map
*var_more_or_equal(
4785 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4791 i
= isl_basic_map_alloc_inequality(bmap
);
4794 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4795 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4796 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4797 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4798 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4799 return isl_basic_map_finalize(bmap
);
4801 isl_basic_map_free(bmap
);
4805 __isl_give isl_basic_map
*isl_basic_map_equal(
4806 __isl_take isl_space
*space
, unsigned n_equal
)
4809 struct isl_basic_map
*bmap
;
4810 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4813 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4814 bmap
= var_equal(bmap
, i
);
4815 return isl_basic_map_finalize(bmap
);
4818 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4820 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4824 struct isl_basic_map
*bmap
;
4825 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4828 for (i
= 0; i
< pos
&& bmap
; ++i
)
4829 bmap
= var_equal(bmap
, i
);
4831 bmap
= var_less(bmap
, pos
);
4832 return isl_basic_map_finalize(bmap
);
4835 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4837 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4838 __isl_take isl_space
*space
, unsigned pos
)
4841 isl_basic_map
*bmap
;
4843 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4844 for (i
= 0; i
< pos
; ++i
)
4845 bmap
= var_equal(bmap
, i
);
4846 bmap
= var_less_or_equal(bmap
, pos
);
4847 return isl_basic_map_finalize(bmap
);
4850 /* Return a relation on "space" expressing i_pos > o_pos
4852 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4856 struct isl_basic_map
*bmap
;
4857 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4860 for (i
= 0; i
< pos
&& bmap
; ++i
)
4861 bmap
= var_equal(bmap
, i
);
4863 bmap
= var_more(bmap
, pos
);
4864 return isl_basic_map_finalize(bmap
);
4867 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4869 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4870 __isl_take isl_space
*space
, unsigned pos
)
4873 isl_basic_map
*bmap
;
4875 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4876 for (i
= 0; i
< pos
; ++i
)
4877 bmap
= var_equal(bmap
, i
);
4878 bmap
= var_more_or_equal(bmap
, pos
);
4879 return isl_basic_map_finalize(bmap
);
4882 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4883 unsigned n
, int equal
)
4885 struct isl_map
*map
;
4888 if (n
== 0 && equal
)
4889 return isl_map_universe(space
);
4891 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4893 for (i
= 0; i
+ 1 < n
; ++i
)
4894 map
= isl_map_add_basic_map(map
,
4895 isl_basic_map_less_at(isl_space_copy(space
), i
));
4898 map
= isl_map_add_basic_map(map
,
4899 isl_basic_map_less_or_equal_at(space
, n
- 1));
4901 map
= isl_map_add_basic_map(map
,
4902 isl_basic_map_less_at(space
, n
- 1));
4904 isl_space_free(space
);
4909 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
4913 return map_lex_lte_first(space
, space
->n_out
, equal
);
4916 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4918 return map_lex_lte_first(dim
, n
, 0);
4921 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4923 return map_lex_lte_first(dim
, n
, 1);
4926 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4928 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4931 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4933 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4936 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
4937 unsigned n
, int equal
)
4939 struct isl_map
*map
;
4942 if (n
== 0 && equal
)
4943 return isl_map_universe(space
);
4945 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4947 for (i
= 0; i
+ 1 < n
; ++i
)
4948 map
= isl_map_add_basic_map(map
,
4949 isl_basic_map_more_at(isl_space_copy(space
), i
));
4952 map
= isl_map_add_basic_map(map
,
4953 isl_basic_map_more_or_equal_at(space
, n
- 1));
4955 map
= isl_map_add_basic_map(map
,
4956 isl_basic_map_more_at(space
, n
- 1));
4958 isl_space_free(space
);
4963 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
4967 return map_lex_gte_first(space
, space
->n_out
, equal
);
4970 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4972 return map_lex_gte_first(dim
, n
, 0);
4975 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4977 return map_lex_gte_first(dim
, n
, 1);
4980 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4982 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4985 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4987 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4990 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4991 __isl_take isl_set
*set2
)
4994 map
= isl_map_lex_le(isl_set_get_space(set1
));
4995 map
= isl_map_intersect_domain(map
, set1
);
4996 map
= isl_map_intersect_range(map
, set2
);
5000 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5001 __isl_take isl_set
*set2
)
5004 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5005 map
= isl_map_intersect_domain(map
, set1
);
5006 map
= isl_map_intersect_range(map
, set2
);
5010 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5011 __isl_take isl_set
*set2
)
5014 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5015 map
= isl_map_intersect_domain(map
, set1
);
5016 map
= isl_map_intersect_range(map
, set2
);
5020 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5021 __isl_take isl_set
*set2
)
5024 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5025 map
= isl_map_intersect_domain(map
, set1
);
5026 map
= isl_map_intersect_range(map
, set2
);
5030 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5031 __isl_take isl_map
*map2
)
5034 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5035 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5036 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5040 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5041 __isl_take isl_map
*map2
)
5044 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5045 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5046 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5050 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5051 __isl_take isl_map
*map2
)
5054 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5055 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5056 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5060 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5061 __isl_take isl_map
*map2
)
5064 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5065 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5066 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5070 /* For the div d = floor(f/m) at position "div", add the constraint
5074 static __isl_give isl_basic_map
*add_upper_div_constraint(
5075 __isl_take isl_basic_map
*bmap
, unsigned div
)
5078 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5079 unsigned n_div
, pos
;
5081 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5083 return isl_basic_map_free(bmap
);
5085 i
= isl_basic_map_alloc_inequality(bmap
);
5087 return isl_basic_map_free(bmap
);
5088 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5089 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5094 /* For the div d = floor(f/m) at position "div", add the constraint
5096 * -(f-(m-1)) + m d >= 0
5098 static __isl_give isl_basic_map
*add_lower_div_constraint(
5099 __isl_take isl_basic_map
*bmap
, unsigned div
)
5102 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5103 unsigned n_div
, pos
;
5105 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5107 return isl_basic_map_free(bmap
);
5109 i
= isl_basic_map_alloc_inequality(bmap
);
5111 return isl_basic_map_free(bmap
);
5112 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5113 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5114 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5115 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5120 /* For the div d = floor(f/m) at position "pos", add the constraints
5123 * -(f-(m-1)) + m d >= 0
5125 * Note that the second constraint is the negation of
5129 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5130 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5132 bmap
= add_upper_div_constraint(bmap
, pos
);
5133 bmap
= add_lower_div_constraint(bmap
, pos
);
5137 /* For each known div d = floor(f/m), add the constraints
5140 * -(f-(m-1)) + m d >= 0
5142 * Remove duplicate constraints in case of some these div constraints
5143 * already appear in "bmap".
5145 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5146 __isl_take isl_basic_map
*bmap
)
5152 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5156 bmap
= add_known_div_constraints(bmap
);
5157 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5158 bmap
= isl_basic_map_finalize(bmap
);
5162 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5164 * In particular, if this div is of the form d = floor(f/m),
5165 * then add the constraint
5169 * if sign < 0 or the constraint
5171 * -(f-(m-1)) + m d >= 0
5175 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5176 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5179 return add_upper_div_constraint(bmap
, div
);
5181 return add_lower_div_constraint(bmap
, div
);
5184 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5185 __isl_take isl_basic_map
*bmap
)
5189 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5191 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5192 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5193 return bset_from_bmap(bmap
);
5194 bmap
= isl_basic_map_cow(bmap
);
5197 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5200 bmap
->extra
-= bmap
->n_div
;
5202 bmap
= isl_basic_map_finalize(bmap
);
5203 return bset_from_bmap(bmap
);
5205 isl_basic_map_free(bmap
);
5209 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5210 __isl_take isl_basic_set
*bset
)
5212 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5215 /* Replace each element in "list" by the result of applying
5216 * isl_basic_map_underlying_set to the element.
5218 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5219 __isl_take isl_basic_map_list
*list
)
5226 n
= isl_basic_map_list_n_basic_map(list
);
5227 for (i
= 0; i
< n
; ++i
) {
5228 isl_basic_map
*bmap
;
5229 isl_basic_set
*bset
;
5231 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5232 bset
= isl_basic_set_underlying_set(bmap
);
5233 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5239 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5240 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5242 struct isl_basic_map
*bmap
;
5243 struct isl_ctx
*ctx
;
5244 unsigned dim
, total
;
5250 if (isl_basic_set_check_no_params(bset
) < 0 ||
5251 isl_basic_set_check_no_locals(bset
) < 0)
5253 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5254 isl_assert(ctx
, dim
== isl_basic_map_total_dim(like
), goto error
);
5255 if (like
->n_div
== 0) {
5256 isl_space
*space
= isl_basic_map_get_space(like
);
5257 isl_basic_map_free(like
);
5258 return isl_basic_map_reset_space(bset
, space
);
5260 bset
= isl_basic_set_cow(bset
);
5263 total
= dim
+ bset
->extra
;
5264 bmap
= bset_to_bmap(bset
);
5265 isl_space_free(bmap
->dim
);
5266 bmap
->dim
= isl_space_copy(like
->dim
);
5269 bmap
->n_div
= like
->n_div
;
5270 bmap
->extra
+= like
->n_div
;
5274 ltotal
= total
- bmap
->extra
+ like
->extra
;
5277 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5278 bmap
->extra
* (1 + 1 + total
));
5279 if (isl_blk_is_error(bmap
->block2
))
5281 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5285 for (i
= 0; i
< bmap
->extra
; ++i
)
5286 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5287 for (i
= 0; i
< like
->n_div
; ++i
) {
5288 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5289 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5291 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5293 isl_basic_map_free(like
);
5294 bmap
= isl_basic_map_simplify(bmap
);
5295 bmap
= isl_basic_map_finalize(bmap
);
5298 isl_basic_map_free(like
);
5299 isl_basic_set_free(bset
);
5303 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5304 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5306 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5307 bset_to_bmap(like
)));
5310 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5314 map
= isl_map_cow(map
);
5317 map
->dim
= isl_space_cow(map
->dim
);
5321 for (i
= 1; i
< map
->n
; ++i
)
5322 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5324 for (i
= 0; i
< map
->n
; ++i
) {
5325 map
->p
[i
] = bset_to_bmap(
5326 isl_basic_map_underlying_set(map
->p
[i
]));
5331 map
->dim
= isl_space_underlying(map
->dim
, 0);
5333 isl_space_free(map
->dim
);
5334 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5338 return set_from_map(map
);
5344 /* Replace the space of "bmap" by "space".
5346 * If the space of "bmap" is identical to "space" (including the identifiers
5347 * of the input and output dimensions), then simply return the original input.
5349 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5350 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5353 isl_space
*bmap_space
;
5355 bmap_space
= isl_basic_map_peek_space(bmap
);
5356 equal
= isl_space_is_equal(bmap_space
, space
);
5357 if (equal
>= 0 && equal
)
5358 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5362 isl_space_free(space
);
5365 bmap
= isl_basic_map_cow(bmap
);
5366 if (!bmap
|| !space
)
5369 isl_space_free(bmap
->dim
);
5372 bmap
= isl_basic_map_finalize(bmap
);
5376 isl_basic_map_free(bmap
);
5377 isl_space_free(space
);
5381 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5382 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5384 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5388 /* Check that the total dimensions of "map" and "space" are the same.
5390 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5391 __isl_keep isl_space
*space
)
5393 unsigned dim1
, dim2
;
5396 return isl_stat_error
;
5397 dim1
= isl_map_dim(map
, isl_dim_all
);
5398 dim2
= isl_space_dim(space
, isl_dim_all
);
5401 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5402 "total dimensions do not match", return isl_stat_error
);
5405 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5406 __isl_take isl_space
*space
)
5410 map
= isl_map_cow(map
);
5414 for (i
= 0; i
< map
->n
; ++i
) {
5415 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5416 isl_space_copy(space
));
5420 isl_space_free(map
->dim
);
5426 isl_space_free(space
);
5430 /* Replace the space of "map" by "space", without modifying
5431 * the dimension of "map".
5433 * If the space of "map" is identical to "space" (including the identifiers
5434 * of the input and output dimensions), then simply return the original input.
5436 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5437 __isl_take isl_space
*space
)
5440 isl_space
*map_space
;
5442 map_space
= isl_map_peek_space(map
);
5443 equal
= isl_space_is_equal(map_space
, space
);
5444 if (equal
>= 0 && equal
)
5445 equal
= isl_space_has_equal_ids(map_space
, space
);
5449 isl_space_free(space
);
5452 if (check_map_space_equal_total_dim(map
, space
) < 0)
5454 return isl_map_reset_space(map
, space
);
5457 isl_space_free(space
);
5461 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5462 __isl_take isl_space
*dim
)
5464 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5467 /* Compute the parameter domain of the given basic set.
5469 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5475 is_params
= isl_basic_set_is_params(bset
);
5477 return isl_basic_set_free(bset
);
5481 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5482 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5483 space
= isl_basic_set_get_space(bset
);
5484 space
= isl_space_params(space
);
5485 bset
= isl_basic_set_reset_space(bset
, space
);
5489 /* Construct a zero-dimensional basic set with the given parameter domain.
5491 __isl_give isl_basic_set
*isl_basic_set_from_params(
5492 __isl_take isl_basic_set
*bset
)
5495 space
= isl_basic_set_get_space(bset
);
5496 space
= isl_space_set_from_params(space
);
5497 bset
= isl_basic_set_reset_space(bset
, space
);
5501 /* Compute the parameter domain of the given set.
5503 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5505 return isl_map_params(set_to_map(set
));
5508 /* Construct a zero-dimensional set with the given parameter domain.
5510 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5513 space
= isl_set_get_space(set
);
5514 space
= isl_space_set_from_params(space
);
5515 set
= isl_set_reset_space(set
, space
);
5519 /* Compute the parameter domain of the given map.
5521 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5526 n
= isl_map_dim(map
, isl_dim_in
);
5527 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5528 n
= isl_map_dim(map
, isl_dim_out
);
5529 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5530 space
= isl_map_get_space(map
);
5531 space
= isl_space_params(space
);
5532 map
= isl_map_reset_space(map
, space
);
5536 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5543 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5545 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5546 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5548 return isl_basic_map_reset_space(bmap
, space
);
5551 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5554 return isl_bool_error
;
5555 return isl_space_may_be_set(bmap
->dim
);
5558 /* Is this basic map actually a set?
5559 * Users should never call this function. Outside of isl,
5560 * the type should indicate whether something is a set or a map.
5562 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5565 return isl_bool_error
;
5566 return isl_space_is_set(bmap
->dim
);
5569 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5573 is_set
= isl_basic_map_is_set(bmap
);
5578 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5580 isl_basic_map_free(bmap
);
5584 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5585 __isl_take isl_basic_map
*bmap
)
5589 isl_basic_map
*domain
;
5590 int nparam
, n_in
, n_out
;
5592 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5593 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5594 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5596 space
= isl_basic_map_get_space(bmap
);
5597 space
= isl_space_from_range(isl_space_domain(space
));
5598 domain
= isl_basic_map_universe(space
);
5600 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5601 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5602 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5604 for (i
= 0; i
< n_in
; ++i
)
5605 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5608 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5609 return isl_basic_map_finalize(bmap
);
5612 __isl_give isl_basic_map
*isl_basic_map_range_map(
5613 __isl_take isl_basic_map
*bmap
)
5617 isl_basic_map
*range
;
5618 int nparam
, n_in
, n_out
;
5620 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5621 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5622 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5624 space
= isl_basic_map_get_space(bmap
);
5625 space
= isl_space_from_range(isl_space_range(space
));
5626 range
= isl_basic_map_universe(space
);
5628 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5629 bmap
= isl_basic_map_apply_range(bmap
, range
);
5630 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5632 for (i
= 0; i
< n_out
; ++i
)
5633 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5636 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5637 return isl_basic_map_finalize(bmap
);
5640 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5644 return isl_space_may_be_set(map
->dim
);
5647 /* Is this map actually a set?
5648 * Users should never call this function. Outside of isl,
5649 * the type should indicate whether something is a set or a map.
5651 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5654 return isl_bool_error
;
5655 return isl_space_is_set(map
->dim
);
5658 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5662 struct isl_set
*set
;
5664 is_set
= isl_map_is_set(map
);
5668 return set_from_map(map
);
5670 map
= isl_map_cow(map
);
5674 set
= set_from_map(map
);
5675 set
->dim
= isl_space_range(set
->dim
);
5678 for (i
= 0; i
< map
->n
; ++i
) {
5679 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5683 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5684 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5691 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5695 map
= isl_map_cow(map
);
5699 map
->dim
= isl_space_domain_map(map
->dim
);
5702 for (i
= 0; i
< map
->n
; ++i
) {
5703 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5707 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5708 map
= isl_map_unmark_normalized(map
);
5715 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5718 isl_space
*range_dim
;
5720 map
= isl_map_cow(map
);
5724 range_dim
= isl_space_range(isl_map_get_space(map
));
5725 range_dim
= isl_space_from_range(range_dim
);
5726 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5727 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5730 for (i
= 0; i
< map
->n
; ++i
) {
5731 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5735 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5736 map
= isl_map_unmark_normalized(map
);
5743 /* Given a wrapped map of the form A[B -> C],
5744 * return the map A[B -> C] -> B.
5746 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5753 if (!isl_set_has_tuple_id(set
))
5754 return isl_map_domain_map(isl_set_unwrap(set
));
5756 id
= isl_set_get_tuple_id(set
);
5757 map
= isl_map_domain_map(isl_set_unwrap(set
));
5758 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5763 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5764 __isl_take isl_basic_set
*bset
)
5766 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5769 __isl_give isl_basic_map
*isl_basic_map_from_range(
5770 __isl_take isl_basic_set
*bset
)
5773 space
= isl_basic_set_get_space(bset
);
5774 space
= isl_space_from_range(space
);
5775 bset
= isl_basic_set_reset_space(bset
, space
);
5776 return bset_to_bmap(bset
);
5779 /* Create a relation with the given set as range.
5780 * The domain of the created relation is a zero-dimensional
5781 * flat anonymous space.
5783 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5786 space
= isl_set_get_space(set
);
5787 space
= isl_space_from_range(space
);
5788 set
= isl_set_reset_space(set
, space
);
5789 return set_to_map(set
);
5792 /* Create a relation with the given set as domain.
5793 * The range of the created relation is a zero-dimensional
5794 * flat anonymous space.
5796 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5798 return isl_map_reverse(isl_map_from_range(set
));
5801 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5802 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5804 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5807 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5808 __isl_take isl_set
*range
)
5810 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5813 /* Return a newly allocated isl_map with given space and flags and
5814 * room for "n" basic maps.
5815 * Make sure that all cached information is cleared.
5817 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5820 struct isl_map
*map
;
5825 isl_die(space
->ctx
, isl_error_internal
,
5826 "negative number of basic maps", goto error
);
5827 map
= isl_calloc(space
->ctx
, struct isl_map
,
5828 sizeof(struct isl_map
) +
5829 (n
- 1) * sizeof(struct isl_basic_map
*));
5833 map
->ctx
= space
->ctx
;
5834 isl_ctx_ref(map
->ctx
);
5842 isl_space_free(space
);
5846 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5848 struct isl_basic_map
*bmap
;
5849 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5850 bmap
= isl_basic_map_set_to_empty(bmap
);
5854 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5856 struct isl_basic_set
*bset
;
5857 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5858 bset
= isl_basic_set_set_to_empty(bset
);
5862 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5864 struct isl_basic_map
*bmap
;
5865 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5866 bmap
= isl_basic_map_finalize(bmap
);
5870 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5872 struct isl_basic_set
*bset
;
5873 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5874 bset
= isl_basic_set_finalize(bset
);
5878 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
5879 __isl_take isl_space
*space
)
5882 unsigned total
= isl_space_dim(space
, isl_dim_all
);
5883 isl_basic_map
*bmap
;
5885 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
5886 for (i
= 0; i
< total
; ++i
) {
5887 int k
= isl_basic_map_alloc_inequality(bmap
);
5890 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5891 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5895 isl_basic_map_free(bmap
);
5899 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
5900 __isl_take isl_space
*space
)
5902 return isl_basic_map_nat_universe(space
);
5905 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5907 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5910 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5912 return isl_map_nat_universe(dim
);
5915 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
5917 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5920 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
5922 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5925 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
5927 struct isl_map
*map
;
5930 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5931 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
5935 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
5937 struct isl_set
*set
;
5940 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5941 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
5945 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5948 struct isl_map
*dup
;
5952 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5953 for (i
= 0; i
< map
->n
; ++i
)
5954 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5958 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5959 __isl_take isl_basic_map
*bmap
)
5963 if (isl_basic_map_plain_is_empty(bmap
)) {
5964 isl_basic_map_free(bmap
);
5967 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5968 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5969 map
->p
[map
->n
] = bmap
;
5971 map
= isl_map_unmark_normalized(map
);
5977 isl_basic_map_free(bmap
);
5981 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5992 isl_ctx_deref(map
->ctx
);
5993 for (i
= 0; i
< map
->n
; ++i
)
5994 isl_basic_map_free(map
->p
[i
]);
5995 isl_space_free(map
->dim
);
6001 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6002 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6006 bmap
= isl_basic_map_cow(bmap
);
6007 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6008 j
= isl_basic_map_alloc_equality(bmap
);
6011 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6012 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6013 isl_int_set_si(bmap
->eq
[j
][0], value
);
6014 bmap
= isl_basic_map_simplify(bmap
);
6015 return isl_basic_map_finalize(bmap
);
6017 isl_basic_map_free(bmap
);
6021 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6022 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6026 bmap
= isl_basic_map_cow(bmap
);
6027 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6028 j
= isl_basic_map_alloc_equality(bmap
);
6031 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6032 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6033 isl_int_set(bmap
->eq
[j
][0], value
);
6034 bmap
= isl_basic_map_simplify(bmap
);
6035 return isl_basic_map_finalize(bmap
);
6037 isl_basic_map_free(bmap
);
6041 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6042 enum isl_dim_type type
, unsigned pos
, int value
)
6044 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6045 return isl_basic_map_free(bmap
);
6046 return isl_basic_map_fix_pos_si(bmap
,
6047 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6050 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6051 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6053 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6054 return isl_basic_map_free(bmap
);
6055 return isl_basic_map_fix_pos(bmap
,
6056 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6059 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6060 * to be equal to "v".
6062 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6063 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6067 if (!isl_val_is_int(v
))
6068 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6069 "expecting integer value", goto error
);
6070 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6072 pos
+= isl_basic_map_offset(bmap
, type
);
6073 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6077 isl_basic_map_free(bmap
);
6082 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6083 * to be equal to "v".
6085 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6086 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6088 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6091 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6092 enum isl_dim_type type
, unsigned pos
, int value
)
6094 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6098 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6099 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6101 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6105 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6106 unsigned input
, int value
)
6108 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6111 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6112 unsigned dim
, int value
)
6114 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6115 isl_dim_set
, dim
, value
));
6118 /* Remove the basic map at position "i" from "map" if this basic map
6119 * is (obviously) empty.
6121 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6128 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6130 return isl_map_free(map
);
6134 isl_basic_map_free(map
->p
[i
]);
6137 map
->p
[i
] = map
->p
[map
->n
];
6138 map
= isl_map_unmark_normalized(map
);
6145 /* Perform "fn" on each basic map of "map", where we may not be holding
6146 * the only reference to "map".
6147 * In particular, "fn" should be a semantics preserving operation
6148 * that we want to apply to all copies of "map". We therefore need
6149 * to be careful not to modify "map" in a way that breaks "map"
6150 * in case anything goes wrong.
6152 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6153 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6155 struct isl_basic_map
*bmap
;
6161 for (i
= map
->n
- 1; i
>= 0; --i
) {
6162 bmap
= isl_basic_map_copy(map
->p
[i
]);
6166 isl_basic_map_free(map
->p
[i
]);
6168 map
= remove_if_empty(map
, i
);
6179 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6180 enum isl_dim_type type
, unsigned pos
, int value
)
6184 map
= isl_map_cow(map
);
6185 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6186 return isl_map_free(map
);
6187 for (i
= map
->n
- 1; i
>= 0; --i
) {
6188 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6189 map
= remove_if_empty(map
, i
);
6193 map
= isl_map_unmark_normalized(map
);
6197 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6198 enum isl_dim_type type
, unsigned pos
, int value
)
6200 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6203 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6204 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6208 map
= isl_map_cow(map
);
6209 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6210 return isl_map_free(map
);
6211 for (i
= 0; i
< map
->n
; ++i
) {
6212 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6216 map
= isl_map_unmark_normalized(map
);
6223 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6224 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6226 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6229 /* Fix the value of the variable at position "pos" of type "type" of "map"
6230 * to be equal to "v".
6232 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6233 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6237 map
= isl_map_cow(map
);
6241 if (!isl_val_is_int(v
))
6242 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6243 "expecting integer value", goto error
);
6244 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6246 for (i
= map
->n
- 1; i
>= 0; --i
) {
6247 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6249 map
= remove_if_empty(map
, i
);
6253 map
= isl_map_unmark_normalized(map
);
6262 /* Fix the value of the variable at position "pos" of type "type" of "set"
6263 * to be equal to "v".
6265 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6266 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6268 return isl_map_fix_val(set
, type
, pos
, v
);
6271 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6272 unsigned input
, int value
)
6274 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6277 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6279 return set_from_map(isl_map_fix_si(set_to_map(set
),
6280 isl_dim_set
, dim
, value
));
6283 static __isl_give isl_basic_map
*basic_map_bound_si(
6284 __isl_take isl_basic_map
*bmap
,
6285 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6289 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6290 return isl_basic_map_free(bmap
);
6291 pos
+= isl_basic_map_offset(bmap
, type
);
6292 bmap
= isl_basic_map_cow(bmap
);
6293 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6294 j
= isl_basic_map_alloc_inequality(bmap
);
6297 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6299 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6300 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6302 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6303 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6305 bmap
= isl_basic_map_simplify(bmap
);
6306 return isl_basic_map_finalize(bmap
);
6308 isl_basic_map_free(bmap
);
6312 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6313 __isl_take isl_basic_map
*bmap
,
6314 enum isl_dim_type type
, unsigned pos
, int value
)
6316 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6319 /* Constrain the values of the given dimension to be no greater than "value".
6321 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6322 __isl_take isl_basic_map
*bmap
,
6323 enum isl_dim_type type
, unsigned pos
, int value
)
6325 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6328 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6329 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6333 map
= isl_map_cow(map
);
6334 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6335 return isl_map_free(map
);
6336 for (i
= 0; i
< map
->n
; ++i
) {
6337 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6338 type
, pos
, value
, upper
);
6342 map
= isl_map_unmark_normalized(map
);
6349 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6350 enum isl_dim_type type
, unsigned pos
, int value
)
6352 return map_bound_si(map
, type
, pos
, value
, 0);
6355 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6356 enum isl_dim_type type
, unsigned pos
, int value
)
6358 return map_bound_si(map
, type
, pos
, value
, 1);
6361 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6362 enum isl_dim_type type
, unsigned pos
, int value
)
6364 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6368 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6369 enum isl_dim_type type
, unsigned pos
, int value
)
6371 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6374 /* Bound the given variable of "bmap" from below (or above is "upper"
6375 * is set) to "value".
6377 static __isl_give isl_basic_map
*basic_map_bound(
6378 __isl_take isl_basic_map
*bmap
,
6379 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6383 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6384 return isl_basic_map_free(bmap
);
6385 pos
+= isl_basic_map_offset(bmap
, type
);
6386 bmap
= isl_basic_map_cow(bmap
);
6387 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6388 j
= isl_basic_map_alloc_inequality(bmap
);
6391 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6393 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6394 isl_int_set(bmap
->ineq
[j
][0], value
);
6396 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6397 isl_int_neg(bmap
->ineq
[j
][0], value
);
6399 bmap
= isl_basic_map_simplify(bmap
);
6400 return isl_basic_map_finalize(bmap
);
6402 isl_basic_map_free(bmap
);
6406 /* Bound the given variable of "map" from below (or above is "upper"
6407 * is set) to "value".
6409 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6410 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6414 map
= isl_map_cow(map
);
6415 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6416 return isl_map_free(map
);
6417 for (i
= map
->n
- 1; i
>= 0; --i
) {
6418 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6419 map
= remove_if_empty(map
, i
);
6423 map
= isl_map_unmark_normalized(map
);
6427 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6428 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6430 return map_bound(map
, type
, pos
, value
, 0);
6433 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6434 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6436 return map_bound(map
, type
, pos
, value
, 1);
6439 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6440 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6442 return isl_map_lower_bound(set
, type
, pos
, value
);
6445 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6446 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6448 return isl_map_upper_bound(set
, type
, pos
, value
);
6451 /* Force the values of the variable at position "pos" of type "type" of "set"
6452 * to be no smaller than "value".
6454 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6455 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6459 if (!isl_val_is_int(value
))
6460 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6461 "expecting integer value", goto error
);
6462 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6463 isl_val_free(value
);
6466 isl_val_free(value
);
6471 /* Force the values of the variable at position "pos" of type "type" of "set"
6472 * to be no greater than "value".
6474 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6475 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6479 if (!isl_val_is_int(value
))
6480 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6481 "expecting integer value", goto error
);
6482 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6483 isl_val_free(value
);
6486 isl_val_free(value
);
6491 /* Bound the given variable of "bset" from below (or above is "upper"
6492 * is set) to "value".
6494 static __isl_give isl_basic_set
*isl_basic_set_bound(
6495 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6496 isl_int value
, int upper
)
6498 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6499 type
, pos
, value
, upper
));
6502 /* Bound the given variable of "bset" from below (or above is "upper"
6503 * is set) to "value".
6505 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6506 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6507 __isl_take isl_val
*value
, int upper
)
6511 if (!isl_val_is_int(value
))
6512 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6513 "expecting integer value", goto error
);
6514 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6515 isl_val_free(value
);
6518 isl_val_free(value
);
6519 isl_basic_set_free(bset
);
6523 /* Bound the given variable of "bset" from below to "value".
6525 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6526 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6527 __isl_take isl_val
*value
)
6529 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6532 /* Bound the given variable of "bset" from above to "value".
6534 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6535 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6536 __isl_take isl_val
*value
)
6538 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6541 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6545 map
= isl_map_cow(map
);
6549 map
->dim
= isl_space_reverse(map
->dim
);
6552 for (i
= 0; i
< map
->n
; ++i
) {
6553 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6557 map
= isl_map_unmark_normalized(map
);
6565 #define TYPE isl_pw_multi_aff
6567 #define SUFFIX _pw_multi_aff
6569 #define EMPTY isl_pw_multi_aff_empty
6571 #define ADD isl_pw_multi_aff_union_add
6572 #include "isl_map_lexopt_templ.c"
6574 /* Given a map "map", compute the lexicographically minimal
6575 * (or maximal) image element for each domain element in dom,
6576 * in the form of an isl_pw_multi_aff.
6577 * If "empty" is not NULL, then set *empty to those elements in dom that
6578 * do not have an image element.
6579 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6580 * should be computed over the domain of "map". "empty" is also NULL
6583 * We first compute the lexicographically minimal or maximal element
6584 * in the first basic map. This results in a partial solution "res"
6585 * and a subset "todo" of dom that still need to be handled.
6586 * We then consider each of the remaining maps in "map" and successively
6587 * update both "res" and "todo".
6588 * If "empty" is NULL, then the todo sets are not needed and therefore
6589 * also not computed.
6591 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6592 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6593 __isl_give isl_set
**empty
, unsigned flags
)
6597 isl_pw_multi_aff
*res
;
6600 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6601 if (!map
|| (!full
&& !dom
))
6604 if (isl_map_plain_is_empty(map
)) {
6609 return isl_pw_multi_aff_from_map(map
);
6612 res
= basic_map_partial_lexopt_pw_multi_aff(
6613 isl_basic_map_copy(map
->p
[0]),
6614 isl_set_copy(dom
), empty
, flags
);
6618 for (i
= 1; i
< map
->n
; ++i
) {
6619 isl_pw_multi_aff
*res_i
;
6621 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6622 isl_basic_map_copy(map
->p
[i
]),
6623 isl_set_copy(dom
), empty
, flags
);
6625 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6626 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6628 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6631 todo
= isl_set_intersect(todo
, *empty
);
6650 #define TYPE isl_map
6654 #define EMPTY isl_map_empty
6656 #define ADD isl_map_union_disjoint
6657 #include "isl_map_lexopt_templ.c"
6659 /* Given a map "map", compute the lexicographically minimal
6660 * (or maximal) image element for each domain element in "dom",
6661 * in the form of an isl_map.
6662 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6663 * do not have an image element.
6664 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6665 * should be computed over the domain of "map". "empty" is also NULL
6668 * If the input consists of more than one disjunct, then first
6669 * compute the desired result in the form of an isl_pw_multi_aff and
6670 * then convert that into an isl_map.
6672 * This function used to have an explicit implementation in terms
6673 * of isl_maps, but it would continually intersect the domains of
6674 * partial results with the complement of the domain of the next
6675 * partial solution, potentially leading to an explosion in the number
6676 * of disjuncts if there are several disjuncts in the input.
6677 * An even earlier implementation of this function would look for
6678 * better results in the domain of the partial result and for extra
6679 * results in the complement of this domain, which would lead to
6680 * even more splintering.
6682 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6683 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6684 __isl_give isl_set
**empty
, unsigned flags
)
6687 struct isl_map
*res
;
6688 isl_pw_multi_aff
*pma
;
6690 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6691 if (!map
|| (!full
&& !dom
))
6694 if (isl_map_plain_is_empty(map
)) {
6703 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6709 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6711 return isl_map_from_pw_multi_aff(pma
);
6720 __isl_give isl_map
*isl_map_partial_lexmax(
6721 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6722 __isl_give isl_set
**empty
)
6724 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6727 __isl_give isl_map
*isl_map_partial_lexmin(
6728 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6729 __isl_give isl_set
**empty
)
6731 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6734 __isl_give isl_set
*isl_set_partial_lexmin(
6735 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6736 __isl_give isl_set
**empty
)
6738 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6742 __isl_give isl_set
*isl_set_partial_lexmax(
6743 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6744 __isl_give isl_set
**empty
)
6746 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6750 /* Compute the lexicographic minimum (or maximum if "flags" includes
6751 * ISL_OPT_MAX) of "bset" over its parametric domain.
6753 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6756 return isl_basic_map_lexopt(bset
, flags
);
6759 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6761 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6764 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6766 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6769 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6771 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6774 /* Compute the lexicographic minimum of "bset" over its parametric domain
6775 * for the purpose of quantifier elimination.
6776 * That is, find an explicit representation for all the existentially
6777 * quantified variables in "bset" by computing their lexicographic
6780 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6781 __isl_take isl_basic_set
*bset
)
6783 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6786 /* Given a basic map with one output dimension, compute the minimum or
6787 * maximum of that dimension as an isl_pw_aff.
6789 * Compute the optimum as a lexicographic optimum over the single
6790 * output dimension and extract the single isl_pw_aff from the result.
6792 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6795 isl_pw_multi_aff
*pma
;
6798 bmap
= isl_basic_map_copy(bmap
);
6799 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6800 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6801 isl_pw_multi_aff_free(pma
);
6806 /* Compute the minimum or maximum of the given output dimension
6807 * as a function of the parameters and the input dimensions,
6808 * but independently of the other output dimensions.
6810 * We first project out the other output dimension and then compute
6811 * the "lexicographic" maximum in each basic map, combining the results
6812 * using isl_pw_aff_union_max.
6814 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6821 n_out
= isl_map_dim(map
, isl_dim_out
);
6822 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6823 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6828 isl_space
*space
= isl_map_get_space(map
);
6830 return isl_pw_aff_empty(space
);
6833 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6834 for (i
= 1; i
< map
->n
; ++i
) {
6835 isl_pw_aff
*pwaff_i
;
6837 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6838 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6846 /* Compute the minimum of the given output dimension as a function of the
6847 * parameters and input dimensions, but independently of
6848 * the other output dimensions.
6850 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6852 return map_dim_opt(map
, pos
, 0);
6855 /* Compute the maximum of the given output dimension as a function of the
6856 * parameters and input dimensions, but independently of
6857 * the other output dimensions.
6859 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6861 return map_dim_opt(map
, pos
, 1);
6864 /* Compute the minimum or maximum of the given set dimension
6865 * as a function of the parameters,
6866 * but independently of the other set dimensions.
6868 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6871 return map_dim_opt(set
, pos
, max
);
6874 /* Compute the maximum of the given set dimension as a function of the
6875 * parameters, but independently of the other set dimensions.
6877 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6879 return set_dim_opt(set
, pos
, 1);
6882 /* Compute the minimum of the given set dimension as a function of the
6883 * parameters, but independently of the other set dimensions.
6885 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6887 return set_dim_opt(set
, pos
, 0);
6890 /* Apply a preimage specified by "mat" on the parameters of "bset".
6891 * bset is assumed to have only parameters and divs.
6893 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6894 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6901 bset
->dim
= isl_space_cow(bset
->dim
);
6905 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6907 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6909 bset
->dim
->nparam
= 0;
6910 bset
->dim
->n_out
= nparam
;
6911 bset
= isl_basic_set_preimage(bset
, mat
);
6913 bset
->dim
->nparam
= bset
->dim
->n_out
;
6914 bset
->dim
->n_out
= 0;
6919 isl_basic_set_free(bset
);
6923 /* Apply a preimage specified by "mat" on the parameters of "set".
6924 * set is assumed to have only parameters and divs.
6926 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6927 __isl_take isl_mat
*mat
)
6935 nparam
= isl_set_dim(set
, isl_dim_param
);
6937 if (mat
->n_row
!= 1 + nparam
)
6938 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6939 "unexpected number of rows", goto error
);
6941 space
= isl_set_get_space(set
);
6942 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6943 isl_dim_param
, 0, nparam
);
6944 set
= isl_set_reset_space(set
, space
);
6945 set
= isl_set_preimage(set
, mat
);
6946 nparam
= isl_set_dim(set
, isl_dim_out
);
6947 space
= isl_set_get_space(set
);
6948 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6949 isl_dim_out
, 0, nparam
);
6950 set
= isl_set_reset_space(set
, space
);
6958 /* Intersect the basic set "bset" with the affine space specified by the
6959 * equalities in "eq".
6961 static __isl_give isl_basic_set
*basic_set_append_equalities(
6962 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
6970 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6975 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
6976 for (i
= 0; i
< eq
->n_row
; ++i
) {
6977 k
= isl_basic_set_alloc_equality(bset
);
6980 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6981 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6985 bset
= isl_basic_set_gauss(bset
, NULL
);
6986 bset
= isl_basic_set_finalize(bset
);
6991 isl_basic_set_free(bset
);
6995 /* Intersect the set "set" with the affine space specified by the
6996 * equalities in "eq".
6998 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7006 for (i
= 0; i
< set
->n
; ++i
) {
7007 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7020 /* Given a basic set "bset" that only involves parameters and existentially
7021 * quantified variables, return the index of the first equality
7022 * that only involves parameters. If there is no such equality then
7023 * return bset->n_eq.
7025 * This function assumes that isl_basic_set_gauss has been called on "bset".
7027 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7030 unsigned nparam
, n_div
;
7035 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7036 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7038 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7039 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7046 /* Compute an explicit representation for the existentially quantified
7047 * variables in "bset" by computing the "minimal value" of the set
7048 * variables. Since there are no set variables, the computation of
7049 * the minimal value essentially computes an explicit representation
7050 * of the non-empty part(s) of "bset".
7052 * The input only involves parameters and existentially quantified variables.
7053 * All equalities among parameters have been removed.
7055 * Since the existentially quantified variables in the result are in general
7056 * going to be different from those in the input, we first replace
7057 * them by the minimal number of variables based on their equalities.
7058 * This should simplify the parametric integer programming.
7060 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7062 isl_morph
*morph1
, *morph2
;
7068 if (bset
->n_eq
== 0)
7069 return isl_basic_set_lexmin_compute_divs(bset
);
7071 morph1
= isl_basic_set_parameter_compression(bset
);
7072 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7073 bset
= isl_basic_set_lift(bset
);
7074 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7075 bset
= isl_morph_basic_set(morph2
, bset
);
7076 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7077 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7079 set
= isl_basic_set_lexmin_compute_divs(bset
);
7081 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7086 /* Project the given basic set onto its parameter domain, possibly introducing
7087 * new, explicit, existential variables in the constraints.
7088 * The input has parameters and (possibly implicit) existential variables.
7089 * The output has the same parameters, but only
7090 * explicit existentially quantified variables.
7092 * The actual projection is performed by pip, but pip doesn't seem
7093 * to like equalities very much, so we first remove the equalities
7094 * among the parameters by performing a variable compression on
7095 * the parameters. Afterward, an inverse transformation is performed
7096 * and the equalities among the parameters are inserted back in.
7098 * The variable compression on the parameters may uncover additional
7099 * equalities that were only implicit before. We therefore check
7100 * if there are any new parameter equalities in the result and
7101 * if so recurse. The removal of parameter equalities is required
7102 * for the parameter compression performed by base_compute_divs.
7104 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7108 struct isl_mat
*T
, *T2
;
7109 struct isl_set
*set
;
7112 bset
= isl_basic_set_cow(bset
);
7116 if (bset
->n_eq
== 0)
7117 return base_compute_divs(bset
);
7119 bset
= isl_basic_set_gauss(bset
, NULL
);
7122 if (isl_basic_set_plain_is_empty(bset
))
7123 return isl_set_from_basic_set(bset
);
7125 i
= first_parameter_equality(bset
);
7126 if (i
== bset
->n_eq
)
7127 return base_compute_divs(bset
);
7129 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7130 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7132 eq
= isl_mat_cow(eq
);
7133 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7134 if (T
&& T
->n_col
== 0) {
7138 bset
= isl_basic_set_set_to_empty(bset
);
7139 return isl_set_from_basic_set(bset
);
7141 bset
= basic_set_parameter_preimage(bset
, T
);
7143 i
= first_parameter_equality(bset
);
7146 else if (i
== bset
->n_eq
)
7147 set
= base_compute_divs(bset
);
7149 set
= parameter_compute_divs(bset
);
7150 set
= set_parameter_preimage(set
, T2
);
7151 set
= set_append_equalities(set
, eq
);
7155 /* Insert the divs from "ls" before those of "bmap".
7157 * The number of columns is not changed, which means that the last
7158 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7159 * The caller is responsible for removing the same number of dimensions
7160 * from the space of "bmap".
7162 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7163 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7169 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7173 old_n_div
= bmap
->n_div
;
7174 bmap
= insert_div_rows(bmap
, n_div
);
7178 for (i
= 0; i
< n_div
; ++i
) {
7179 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7180 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7186 /* Replace the space of "bmap" by the space and divs of "ls".
7188 * If "ls" has any divs, then we simplify the result since we may
7189 * have discovered some additional equalities that could simplify
7190 * the div expressions.
7192 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7193 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7197 bmap
= isl_basic_map_cow(bmap
);
7201 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7202 bmap
= insert_divs_from_local_space(bmap
, ls
);
7206 isl_space_free(bmap
->dim
);
7207 bmap
->dim
= isl_local_space_get_space(ls
);
7211 isl_local_space_free(ls
);
7213 bmap
= isl_basic_map_simplify(bmap
);
7214 bmap
= isl_basic_map_finalize(bmap
);
7217 isl_basic_map_free(bmap
);
7218 isl_local_space_free(ls
);
7222 /* Replace the space of "map" by the space and divs of "ls".
7224 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7225 __isl_take isl_local_space
*ls
)
7229 map
= isl_map_cow(map
);
7233 for (i
= 0; i
< map
->n
; ++i
) {
7234 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7235 isl_local_space_copy(ls
));
7239 isl_space_free(map
->dim
);
7240 map
->dim
= isl_local_space_get_space(ls
);
7244 isl_local_space_free(ls
);
7247 isl_local_space_free(ls
);
7252 /* Compute an explicit representation for the existentially
7253 * quantified variables for which do not know any explicit representation yet.
7255 * We first sort the existentially quantified variables so that the
7256 * existentially quantified variables for which we already have an explicit
7257 * representation are placed before those for which we do not.
7258 * The input dimensions, the output dimensions and the existentially
7259 * quantified variables for which we already have an explicit
7260 * representation are then turned into parameters.
7261 * compute_divs returns a map with the same parameters and
7262 * no input or output dimensions and the dimension specification
7263 * is reset to that of the input, including the existentially quantified
7264 * variables for which we already had an explicit representation.
7266 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7268 struct isl_basic_set
*bset
;
7269 struct isl_set
*set
;
7270 struct isl_map
*map
;
7272 isl_local_space
*ls
;
7279 bmap
= isl_basic_map_sort_divs(bmap
);
7280 bmap
= isl_basic_map_cow(bmap
);
7284 n_known
= isl_basic_map_first_unknown_div(bmap
);
7286 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7288 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7289 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7290 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7291 space
= isl_space_set_alloc(bmap
->ctx
,
7292 nparam
+ n_in
+ n_out
+ n_known
, 0);
7296 ls
= isl_basic_map_get_local_space(bmap
);
7297 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7298 n_known
, bmap
->n_div
- n_known
);
7300 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7301 swap_div(bmap
, i
- n_known
, i
);
7302 bmap
->n_div
-= n_known
;
7303 bmap
->extra
-= n_known
;
7305 bmap
= isl_basic_map_reset_space(bmap
, space
);
7306 bset
= bset_from_bmap(bmap
);
7308 set
= parameter_compute_divs(bset
);
7309 map
= set_to_map(set
);
7310 map
= replace_space_by_local_space(map
, ls
);
7314 isl_basic_map_free(bmap
);
7318 /* Remove the explicit representation of local variable "div",
7321 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7322 __isl_take isl_basic_map
*bmap
, int div
)
7326 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7328 return isl_basic_map_free(bmap
);
7332 bmap
= isl_basic_map_cow(bmap
);
7335 isl_int_set_si(bmap
->div
[div
][0], 0);
7339 /* Is local variable "div" of "bmap" marked as not having an explicit
7341 * Note that even if "div" is not marked in this way and therefore
7342 * has an explicit representation, this representation may still
7343 * depend (indirectly) on other local variables that do not
7344 * have an explicit representation.
7346 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7349 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7350 return isl_bool_error
;
7351 return isl_int_is_zero(bmap
->div
[div
][0]);
7354 /* Return the position of the first local variable that does not
7355 * have an explicit representation.
7356 * Return the total number of local variables if they all have
7357 * an explicit representation.
7358 * Return -1 on error.
7360 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7367 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7368 if (!isl_basic_map_div_is_known(bmap
, i
))
7374 /* Return the position of the first local variable that does not
7375 * have an explicit representation.
7376 * Return the total number of local variables if they all have
7377 * an explicit representation.
7378 * Return -1 on error.
7380 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7382 return isl_basic_map_first_unknown_div(bset
);
7385 /* Does "bmap" have an explicit representation for all local variables?
7387 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7391 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7392 first
= isl_basic_map_first_unknown_div(bmap
);
7394 return isl_bool_error
;
7398 /* Do all basic maps in "map" have an explicit representation
7399 * for all local variables?
7401 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7406 return isl_bool_error
;
7408 for (i
= 0; i
< map
->n
; ++i
) {
7409 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7414 return isl_bool_true
;
7417 /* If bmap contains any unknown divs, then compute explicit
7418 * expressions for them. However, this computation may be
7419 * quite expensive, so first try to remove divs that aren't
7422 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7425 struct isl_map
*map
;
7427 known
= isl_basic_map_divs_known(bmap
);
7431 return isl_map_from_basic_map(bmap
);
7433 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7435 known
= isl_basic_map_divs_known(bmap
);
7439 return isl_map_from_basic_map(bmap
);
7441 map
= compute_divs(bmap
);
7444 isl_basic_map_free(bmap
);
7448 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7452 struct isl_map
*res
;
7459 known
= isl_map_divs_known(map
);
7467 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7468 for (i
= 1 ; i
< map
->n
; ++i
) {
7470 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7471 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7472 res
= isl_map_union_disjoint(res
, r2
);
7474 res
= isl_map_union(res
, r2
);
7481 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7483 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7486 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7488 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7491 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7494 struct isl_set
*set
;
7499 map
= isl_map_cow(map
);
7503 set
= set_from_map(map
);
7504 set
->dim
= isl_space_domain(set
->dim
);
7507 for (i
= 0; i
< map
->n
; ++i
) {
7508 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7512 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7513 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7520 /* Return the union of "map1" and "map2", where we assume for now that
7521 * "map1" and "map2" are disjoint. Note that the basic maps inside
7522 * "map1" or "map2" may not be disjoint from each other.
7523 * Also note that this function is also called from isl_map_union,
7524 * which takes care of handling the situation where "map1" and "map2"
7525 * may not be disjoint.
7527 * If one of the inputs is empty, we can simply return the other input.
7528 * Similarly, if one of the inputs is universal, then it is equal to the union.
7530 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7531 __isl_take isl_map
*map2
)
7535 struct isl_map
*map
= NULL
;
7541 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7542 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7543 "spaces don't match", goto error
);
7554 is_universe
= isl_map_plain_is_universe(map1
);
7555 if (is_universe
< 0)
7562 is_universe
= isl_map_plain_is_universe(map2
);
7563 if (is_universe
< 0)
7570 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7571 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7572 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7574 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7575 map1
->n
+ map2
->n
, flags
);
7578 for (i
= 0; i
< map1
->n
; ++i
) {
7579 map
= isl_map_add_basic_map(map
,
7580 isl_basic_map_copy(map1
->p
[i
]));
7584 for (i
= 0; i
< map2
->n
; ++i
) {
7585 map
= isl_map_add_basic_map(map
,
7586 isl_basic_map_copy(map2
->p
[i
]));
7600 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7601 * guaranteed to be disjoint by the caller.
7603 * Note that this functions is called from within isl_map_make_disjoint,
7604 * so we have to be careful not to touch the constraints of the inputs
7607 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7608 __isl_take isl_map
*map2
)
7610 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7613 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7614 * not be disjoint. The parameters are assumed to have been aligned.
7616 * We currently simply call map_union_disjoint, the internal operation
7617 * of which does not really depend on the inputs being disjoint.
7618 * If the result contains more than one basic map, then we clear
7619 * the disjoint flag since the result may contain basic maps from
7620 * both inputs and these are not guaranteed to be disjoint.
7622 * As a special case, if "map1" and "map2" are obviously equal,
7623 * then we simply return "map1".
7625 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7626 __isl_take isl_map
*map2
)
7633 equal
= isl_map_plain_is_equal(map1
, map2
);
7641 map1
= map_union_disjoint(map1
, map2
);
7645 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7653 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7656 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7657 __isl_take isl_map
*map2
)
7659 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7662 __isl_give isl_set
*isl_set_union_disjoint(
7663 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7665 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7669 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7671 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7674 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7677 * "map" and "set" are assumed to be compatible and non-NULL.
7679 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7680 __isl_take isl_set
*set
,
7681 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7682 __isl_take isl_basic_set
*bset
))
7685 struct isl_map
*result
;
7688 if (isl_set_plain_is_universe(set
)) {
7693 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7694 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7695 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7697 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7698 map
->n
* set
->n
, flags
);
7699 for (i
= 0; result
&& i
< map
->n
; ++i
)
7700 for (j
= 0; j
< set
->n
; ++j
) {
7701 result
= isl_map_add_basic_map(result
,
7702 fn(isl_basic_map_copy(map
->p
[i
]),
7703 isl_basic_set_copy(set
->p
[j
])));
7713 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7714 __isl_take isl_set
*set
)
7718 ok
= isl_map_compatible_range(map
, set
);
7722 isl_die(set
->ctx
, isl_error_invalid
,
7723 "incompatible spaces", goto error
);
7725 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7732 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7733 __isl_take isl_set
*set
)
7735 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7738 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7739 __isl_take isl_set
*set
)
7743 ok
= isl_map_compatible_domain(map
, set
);
7747 isl_die(set
->ctx
, isl_error_invalid
,
7748 "incompatible spaces", goto error
);
7750 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7757 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7758 __isl_take isl_set
*set
)
7760 return isl_map_align_params_map_map_and(map
, set
,
7761 &map_intersect_domain
);
7764 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7765 * in the space B -> C, return the intersection.
7766 * The parameters are assumed to have been aligned.
7768 * The map "factor" is first extended to a map living in the space
7769 * [A -> B] -> C and then a regular intersection is computed.
7771 static __isl_give isl_map
*map_intersect_domain_factor_range(
7772 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7775 isl_map
*ext_factor
;
7777 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7778 ext_factor
= isl_map_universe(space
);
7779 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7780 return map_intersect(map
, ext_factor
);
7783 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7784 * in the space B -> C, return the intersection.
7786 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7787 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7789 return isl_map_align_params_map_map_and(map
, factor
,
7790 &map_intersect_domain_factor_range
);
7793 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7794 * in the space A -> C, return the intersection.
7796 * The map "factor" is first extended to a map living in the space
7797 * A -> [B -> C] and then a regular intersection is computed.
7799 static __isl_give isl_map
*map_intersect_range_factor_range(
7800 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7803 isl_map
*ext_factor
;
7805 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7806 ext_factor
= isl_map_universe(space
);
7807 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7808 return isl_map_intersect(map
, ext_factor
);
7811 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7812 * in the space A -> C, return the intersection.
7814 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7815 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7817 return isl_map_align_params_map_map_and(map
, factor
,
7818 &map_intersect_range_factor_range
);
7821 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7822 __isl_take isl_map
*map2
)
7826 map1
= isl_map_reverse(map1
);
7827 map1
= isl_map_apply_range(map1
, map2
);
7828 return isl_map_reverse(map1
);
7835 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7836 __isl_take isl_map
*map2
)
7838 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7841 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7842 __isl_take isl_map
*map2
)
7845 struct isl_map
*result
;
7851 space
= isl_space_join(isl_space_copy(map1
->dim
),
7852 isl_space_copy(map2
->dim
));
7854 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
7857 for (i
= 0; i
< map1
->n
; ++i
)
7858 for (j
= 0; j
< map2
->n
; ++j
) {
7859 result
= isl_map_add_basic_map(result
,
7860 isl_basic_map_apply_range(
7861 isl_basic_map_copy(map1
->p
[i
]),
7862 isl_basic_map_copy(map2
->p
[j
])));
7868 if (result
&& result
->n
<= 1)
7869 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7877 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7878 __isl_take isl_map
*map2
)
7880 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7884 * returns range - domain
7886 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
7888 isl_space
*target_space
;
7889 struct isl_basic_set
*bset
;
7896 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7897 bmap
->dim
, isl_dim_out
),
7899 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7900 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7901 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7902 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7903 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7904 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7905 for (i
= 0; i
< dim
; ++i
) {
7906 int j
= isl_basic_map_alloc_equality(bmap
);
7908 bmap
= isl_basic_map_free(bmap
);
7911 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7912 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7913 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7914 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7916 bset
= isl_basic_map_domain(bmap
);
7917 bset
= isl_basic_set_reset_space(bset
, target_space
);
7920 isl_basic_map_free(bmap
);
7924 /* Check that domain and range of "map" are the same.
7926 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
7931 space
= isl_map_peek_space(map
);
7932 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
7934 return isl_stat_error
;
7936 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7937 "domain and range don't match", return isl_stat_error
);
7942 * returns range - domain
7944 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7948 struct isl_set
*result
;
7953 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7954 map
->dim
, isl_dim_out
),
7956 dim
= isl_map_get_space(map
);
7957 dim
= isl_space_domain(dim
);
7958 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7961 for (i
= 0; i
< map
->n
; ++i
)
7962 result
= isl_set_add_basic_set(result
,
7963 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7972 * returns [domain -> range] -> range - domain
7974 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7975 __isl_take isl_basic_map
*bmap
)
7979 isl_basic_map
*domain
;
7983 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7984 bmap
->dim
, isl_dim_out
))
7985 isl_die(bmap
->ctx
, isl_error_invalid
,
7986 "domain and range don't match", goto error
);
7988 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7989 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7991 space
= isl_basic_map_get_space(bmap
);
7992 space
= isl_space_from_range(isl_space_domain(space
));
7993 domain
= isl_basic_map_universe(space
);
7995 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7996 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7997 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7999 total
= isl_basic_map_total_dim(bmap
);
8001 for (i
= 0; i
< n
; ++i
) {
8002 k
= isl_basic_map_alloc_equality(bmap
);
8005 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8006 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8007 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8008 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8011 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8012 return isl_basic_map_finalize(bmap
);
8014 isl_basic_map_free(bmap
);
8019 * returns [domain -> range] -> range - domain
8021 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8024 isl_space
*domain_space
;
8026 if (isl_map_check_equal_tuples(map
) < 0)
8027 return isl_map_free(map
);
8029 map
= isl_map_cow(map
);
8033 domain_space
= isl_space_domain(isl_map_get_space(map
));
8034 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8035 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8038 for (i
= 0; i
< map
->n
; ++i
) {
8039 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8043 map
= isl_map_unmark_normalized(map
);
8050 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8052 unsigned n_in
, n_out
;
8056 n_in
= isl_space_dim(space
, isl_dim_in
);
8057 n_out
= isl_space_dim(space
, isl_dim_out
);
8059 isl_die(space
->ctx
, isl_error_invalid
,
8060 "number of input and output dimensions needs to be "
8061 "the same", goto error
);
8062 return isl_basic_map_equal(space
, n_in
);
8064 isl_space_free(space
);
8068 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8070 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8073 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8075 isl_space
*dim
= isl_set_get_space(set
);
8077 id
= isl_map_identity(isl_space_map_from_set(dim
));
8078 return isl_map_intersect_range(id
, set
);
8081 /* Construct a basic set with all set dimensions having only non-negative
8084 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8085 __isl_take isl_space
*space
)
8090 struct isl_basic_set
*bset
;
8094 nparam
= isl_space_dim(space
, isl_dim_param
);
8095 dim
= isl_space_dim(space
, isl_dim_set
);
8096 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8099 for (i
= 0; i
< dim
; ++i
) {
8100 int k
= isl_basic_set_alloc_inequality(bset
);
8103 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8104 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8108 isl_basic_set_free(bset
);
8112 /* Construct the half-space x_pos >= 0.
8114 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8118 isl_basic_set
*nonneg
;
8120 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8121 k
= isl_basic_set_alloc_inequality(nonneg
);
8124 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8125 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8127 return isl_basic_set_finalize(nonneg
);
8129 isl_basic_set_free(nonneg
);
8133 /* Construct the half-space x_pos <= -1.
8135 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8141 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8142 k
= isl_basic_set_alloc_inequality(neg
);
8145 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8146 isl_int_set_si(neg
->ineq
[k
][0], -1);
8147 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8149 return isl_basic_set_finalize(neg
);
8151 isl_basic_set_free(neg
);
8155 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8156 enum isl_dim_type type
, unsigned first
, unsigned n
)
8160 isl_basic_set
*nonneg
;
8166 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8167 return isl_set_free(set
);
8169 offset
= pos(set
->dim
, type
);
8170 for (i
= 0; i
< n
; ++i
) {
8171 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8172 offset
+ first
+ i
);
8173 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8175 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8181 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8183 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8189 return isl_stat_error
;
8190 if (isl_set_plain_is_empty(set
)) {
8195 return fn(set
, signs
, user
);
8198 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8200 half
= isl_set_intersect(half
, isl_set_copy(set
));
8201 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8205 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8207 half
= isl_set_intersect(half
, set
);
8208 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8211 return isl_stat_error
;
8214 /* Call "fn" on the intersections of "set" with each of the orthants
8215 * (except for obviously empty intersections). The orthant is identified
8216 * by the signs array, with each entry having value 1 or -1 according
8217 * to the sign of the corresponding variable.
8219 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8220 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8229 return isl_stat_error
;
8230 if (isl_set_plain_is_empty(set
))
8233 nparam
= isl_set_dim(set
, isl_dim_param
);
8234 nvar
= isl_set_dim(set
, isl_dim_set
);
8236 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8238 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8246 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8248 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8251 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8252 __isl_keep isl_basic_map
*bmap2
)
8255 struct isl_map
*map1
;
8256 struct isl_map
*map2
;
8258 if (!bmap1
|| !bmap2
)
8259 return isl_bool_error
;
8261 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8262 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8264 is_subset
= isl_map_is_subset(map1
, map2
);
8272 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8273 __isl_keep isl_basic_set
*bset2
)
8275 return isl_basic_map_is_subset(bset1
, bset2
);
8278 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8279 __isl_keep isl_basic_map
*bmap2
)
8283 if (!bmap1
|| !bmap2
)
8284 return isl_bool_error
;
8285 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8286 if (is_subset
!= isl_bool_true
)
8288 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8292 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8293 __isl_keep isl_basic_set
*bset2
)
8295 return isl_basic_map_is_equal(
8296 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8299 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8305 return isl_bool_error
;
8306 for (i
= 0; i
< map
->n
; ++i
) {
8307 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8309 return isl_bool_error
;
8311 return isl_bool_false
;
8313 return isl_bool_true
;
8316 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8318 return map
? map
->n
== 0 : isl_bool_error
;
8321 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8323 return set
? set
->n
== 0 : isl_bool_error
;
8326 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8328 return isl_map_is_empty(set_to_map(set
));
8331 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8332 __isl_keep isl_map
*map2
)
8335 return isl_bool_error
;
8337 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8340 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8341 __isl_keep isl_set
*set2
)
8344 return isl_bool_error
;
8346 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8349 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8354 return isl_bool_error
;
8355 is_subset
= isl_map_is_subset(map1
, map2
);
8356 if (is_subset
!= isl_bool_true
)
8358 is_subset
= isl_map_is_subset(map2
, map1
);
8362 /* Is "map1" equal to "map2"?
8364 * First check if they are obviously equal.
8365 * If not, then perform a more detailed analysis.
8367 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8371 equal
= isl_map_plain_is_equal(map1
, map2
);
8372 if (equal
< 0 || equal
)
8374 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8377 isl_bool
isl_basic_map_is_strict_subset(
8378 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8382 if (!bmap1
|| !bmap2
)
8383 return isl_bool_error
;
8384 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8385 if (is_subset
!= isl_bool_true
)
8387 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8388 return isl_bool_not(is_subset
);
8391 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8392 __isl_keep isl_map
*map2
)
8397 return isl_bool_error
;
8398 is_subset
= isl_map_is_subset(map1
, map2
);
8399 if (is_subset
!= isl_bool_true
)
8401 is_subset
= isl_map_is_subset(map2
, map1
);
8402 return isl_bool_not(is_subset
);
8405 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8406 __isl_keep isl_set
*set2
)
8408 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8411 /* Is "bmap" obviously equal to the universe with the same space?
8413 * That is, does it not have any constraints?
8415 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8418 return isl_bool_error
;
8419 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8422 /* Is "bset" obviously equal to the universe with the same space?
8424 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8426 return isl_basic_map_plain_is_universe(bset
);
8429 /* If "c" does not involve any existentially quantified variables,
8430 * then set *univ to false and abort
8432 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8434 isl_bool
*univ
= user
;
8437 n
= isl_constraint_dim(c
, isl_dim_div
);
8438 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8439 isl_constraint_free(c
);
8440 if (*univ
< 0 || !*univ
)
8441 return isl_stat_error
;
8445 /* Is "bmap" equal to the universe with the same space?
8447 * First check if it is obviously equal to the universe.
8448 * If not and if there are any constraints not involving
8449 * existentially quantified variables, then it is certainly
8450 * not equal to the universe.
8451 * Otherwise, check if the universe is a subset of "bmap".
8453 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8456 isl_basic_map
*test
;
8458 univ
= isl_basic_map_plain_is_universe(bmap
);
8459 if (univ
< 0 || univ
)
8461 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8462 return isl_bool_false
;
8463 univ
= isl_bool_true
;
8464 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8466 return isl_bool_error
;
8467 if (univ
< 0 || !univ
)
8469 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8470 univ
= isl_basic_map_is_subset(test
, bmap
);
8471 isl_basic_map_free(test
);
8475 /* Is "bset" equal to the universe with the same space?
8477 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8479 return isl_basic_map_is_universe(bset
);
8482 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8487 return isl_bool_error
;
8489 for (i
= 0; i
< map
->n
; ++i
) {
8490 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8495 return isl_bool_false
;
8498 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8500 return isl_map_plain_is_universe(set_to_map(set
));
8503 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8505 struct isl_basic_set
*bset
= NULL
;
8506 struct isl_vec
*sample
= NULL
;
8507 isl_bool empty
, non_empty
;
8510 return isl_bool_error
;
8512 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8513 return isl_bool_true
;
8515 if (isl_basic_map_plain_is_universe(bmap
))
8516 return isl_bool_false
;
8518 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8519 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8520 copy
= isl_basic_map_remove_redundancies(copy
);
8521 empty
= isl_basic_map_plain_is_empty(copy
);
8522 isl_basic_map_free(copy
);
8526 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8528 return isl_bool_error
;
8530 return isl_bool_false
;
8531 isl_vec_free(bmap
->sample
);
8532 bmap
->sample
= NULL
;
8533 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8535 return isl_bool_error
;
8536 sample
= isl_basic_set_sample_vec(bset
);
8538 return isl_bool_error
;
8539 empty
= sample
->size
== 0;
8540 isl_vec_free(bmap
->sample
);
8541 bmap
->sample
= sample
;
8543 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8548 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8551 return isl_bool_error
;
8552 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8555 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8558 return isl_bool_error
;
8559 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8562 /* Is "bmap" known to be non-empty?
8564 * That is, is the cached sample still valid?
8566 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8571 return isl_bool_error
;
8573 return isl_bool_false
;
8574 total
= 1 + isl_basic_map_total_dim(bmap
);
8575 if (bmap
->sample
->size
!= total
)
8576 return isl_bool_false
;
8577 return isl_basic_map_contains(bmap
, bmap
->sample
);
8580 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8582 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8585 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8586 __isl_take isl_basic_map
*bmap2
)
8588 struct isl_map
*map
;
8589 if (!bmap1
|| !bmap2
)
8592 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8594 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8597 map
= isl_map_add_basic_map(map
, bmap1
);
8598 map
= isl_map_add_basic_map(map
, bmap2
);
8601 isl_basic_map_free(bmap1
);
8602 isl_basic_map_free(bmap2
);
8606 struct isl_set
*isl_basic_set_union(
8607 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8609 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8610 bset_to_bmap(bset2
)));
8613 /* Order divs such that any div only depends on previous divs */
8614 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8615 __isl_take isl_basic_map
*bmap
)
8620 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8622 return isl_basic_map_free(bmap
);
8624 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8626 if (isl_int_is_zero(bmap
->div
[i
][0]))
8628 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8633 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8634 "integer division depends on itself",
8635 return isl_basic_map_free(bmap
));
8636 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8644 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8646 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8649 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8656 for (i
= 0; i
< map
->n
; ++i
) {
8657 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8668 /* Sort the local variables of "bset".
8670 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8671 __isl_take isl_basic_set
*bset
)
8673 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8676 /* Apply the expansion computed by isl_merge_divs.
8677 * The expansion itself is given by "exp" while the resulting
8678 * list of divs is given by "div".
8680 * Move the integer divisions of "bmap" into the right position
8681 * according to "exp" and then introduce the additional integer
8682 * divisions, adding div constraints.
8683 * The moving should be done first to avoid moving coefficients
8684 * in the definitions of the extra integer divisions.
8686 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8687 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8692 bmap
= isl_basic_map_cow(bmap
);
8696 if (div
->n_row
< bmap
->n_div
)
8697 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8698 "not an expansion", goto error
);
8700 n_div
= bmap
->n_div
;
8701 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8702 div
->n_row
- n_div
, 0,
8703 2 * (div
->n_row
- n_div
));
8705 for (i
= n_div
; i
< div
->n_row
; ++i
)
8706 if (isl_basic_map_alloc_div(bmap
) < 0)
8709 for (j
= n_div
- 1; j
>= 0; --j
) {
8712 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8717 for (i
= 0; i
< div
->n_row
; ++i
) {
8718 if (j
< n_div
&& exp
[j
] == i
) {
8721 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8722 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8724 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8733 isl_basic_map_free(bmap
);
8738 /* Apply the expansion computed by isl_merge_divs.
8739 * The expansion itself is given by "exp" while the resulting
8740 * list of divs is given by "div".
8742 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8743 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8745 return isl_basic_map_expand_divs(bset
, div
, exp
);
8748 /* Look for a div in dst that corresponds to the div "div" in src.
8749 * The divs before "div" in src and dst are assumed to be the same.
8751 * Return the position of the corresponding div in dst
8752 * if there is one. Otherwise, return a position beyond the integer divisions.
8753 * Return -1 on error.
8755 static int find_div(__isl_keep isl_basic_map
*dst
,
8756 __isl_keep isl_basic_map
*src
, unsigned div
)
8762 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8763 if (!dst
|| v_div
< 0)
8766 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8767 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8768 for (i
= div
; i
< n_div
; ++i
)
8769 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8770 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8776 /* Align the divs of "dst" to those of "src", adding divs from "src"
8777 * if needed. That is, make sure that the first src->n_div divs
8778 * of the result are equal to those of src.
8780 * The result is not finalized as by design it will have redundant
8781 * divs if any divs from "src" were copied.
8783 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8784 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8793 return isl_basic_map_free(dst
);
8795 if (src
->n_div
== 0)
8798 known
= isl_basic_map_divs_known(src
);
8800 return isl_basic_map_free(dst
);
8802 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8803 "some src divs are unknown",
8804 return isl_basic_map_free(dst
));
8806 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8808 return isl_basic_map_free(dst
);
8810 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8812 return isl_basic_map_free(dst
);
8815 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8816 for (i
= 0; i
< src
->n_div
; ++i
) {
8817 int j
= find_div(dst
, src
, i
);
8819 dst
= isl_basic_map_free(dst
);
8820 if (j
== dst_n_div
) {
8822 int extra
= src
->n_div
- i
;
8823 dst
= isl_basic_map_cow(dst
);
8826 dst
= isl_basic_map_extend_space(dst
,
8827 isl_space_copy(dst
->dim
),
8828 extra
, 0, 2 * extra
);
8831 j
= isl_basic_map_alloc_div(dst
);
8834 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
8835 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
8837 dst
= isl_basic_map_add_div_constraints(dst
, j
);
8842 dst
= isl_basic_map_swap_div(dst
, i
, j
);
8846 isl_basic_map_free(src
);
8849 isl_basic_map_free(src
);
8850 isl_basic_map_free(dst
);
8854 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8862 map
= isl_map_compute_divs(map
);
8863 map
= isl_map_cow(map
);
8867 for (i
= 1; i
< map
->n
; ++i
)
8868 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8869 for (i
= 1; i
< map
->n
; ++i
) {
8870 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8872 return isl_map_free(map
);
8875 map
= isl_map_unmark_normalized(map
);
8879 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8881 return isl_map_align_divs_internal(map
);
8884 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8886 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8889 /* Align the divs of the basic maps in "map" to those
8890 * of the basic maps in "list", as well as to the other basic maps in "map".
8891 * The elements in "list" are assumed to have known divs.
8893 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8894 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8898 map
= isl_map_compute_divs(map
);
8899 map
= isl_map_cow(map
);
8901 return isl_map_free(map
);
8905 n
= isl_basic_map_list_n_basic_map(list
);
8906 for (i
= 0; i
< n
; ++i
) {
8907 isl_basic_map
*bmap
;
8909 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8910 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8911 isl_basic_map_free(bmap
);
8914 return isl_map_free(map
);
8916 return isl_map_align_divs_internal(map
);
8919 /* Align the divs of each element of "list" to those of "bmap".
8920 * Both "bmap" and the elements of "list" are assumed to have known divs.
8922 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8923 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8928 return isl_basic_map_list_free(list
);
8930 n
= isl_basic_map_list_n_basic_map(list
);
8931 for (i
= 0; i
< n
; ++i
) {
8932 isl_basic_map
*bmap_i
;
8934 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8935 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8936 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8942 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8943 __isl_take isl_map
*map
)
8947 ok
= isl_map_compatible_domain(map
, set
);
8951 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8952 "incompatible spaces", goto error
);
8953 map
= isl_map_intersect_domain(map
, set
);
8954 set
= isl_map_range(map
);
8962 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8963 __isl_take isl_map
*map
)
8965 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8968 /* There is no need to cow as removing empty parts doesn't change
8969 * the meaning of the set.
8971 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
8978 for (i
= map
->n
- 1; i
>= 0; --i
)
8979 map
= remove_if_empty(map
, i
);
8984 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8986 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8989 /* Create a binary relation that maps the shared initial "pos" dimensions
8990 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
8992 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
8993 __isl_keep isl_basic_set
*bset2
, int pos
)
8995 isl_basic_map
*bmap1
;
8996 isl_basic_map
*bmap2
;
8998 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8999 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9000 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9001 isl_dim_out
, 0, pos
);
9002 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9003 isl_dim_out
, 0, pos
);
9004 return isl_basic_map_range_product(bmap1
, bmap2
);
9007 /* Given two basic sets bset1 and bset2, compute the maximal difference
9008 * between the values of dimension pos in bset1 and those in bset2
9009 * for any common value of the parameters and dimensions preceding pos.
9011 static enum isl_lp_result
basic_set_maximal_difference_at(
9012 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9013 int pos
, isl_int
*opt
)
9015 isl_basic_map
*bmap1
;
9016 struct isl_ctx
*ctx
;
9017 struct isl_vec
*obj
;
9021 enum isl_lp_result res
;
9023 if (!bset1
|| !bset2
)
9024 return isl_lp_error
;
9026 nparam
= isl_basic_set_n_param(bset1
);
9027 dim1
= isl_basic_set_n_dim(bset1
);
9029 bmap1
= join_initial(bset1
, bset2
, pos
);
9031 return isl_lp_error
;
9033 total
= isl_basic_map_total_dim(bmap1
);
9035 obj
= isl_vec_alloc(ctx
, 1 + total
);
9038 isl_seq_clr(obj
->block
.data
, 1 + total
);
9039 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9040 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9041 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9043 isl_basic_map_free(bmap1
);
9047 isl_basic_map_free(bmap1
);
9048 return isl_lp_error
;
9051 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9052 * for any common value of the parameters and dimensions preceding pos
9053 * in both basic sets, the values of dimension pos in bset1 are
9054 * smaller or larger than those in bset2.
9057 * 1 if bset1 follows bset2
9058 * -1 if bset1 precedes bset2
9059 * 0 if bset1 and bset2 are incomparable
9060 * -2 if some error occurred.
9062 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9063 __isl_keep isl_basic_set
*bset2
, int pos
)
9066 enum isl_lp_result res
;
9071 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9073 if (res
== isl_lp_empty
)
9075 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9076 res
== isl_lp_unbounded
)
9078 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9087 /* Given two basic sets bset1 and bset2, check whether
9088 * for any common value of the parameters and dimensions preceding pos
9089 * there is a value of dimension pos in bset1 that is larger
9090 * than a value of the same dimension in bset2.
9093 * 1 if there exists such a pair
9094 * 0 if there is no such pair, but there is a pair of equal values
9096 * -2 if some error occurred.
9098 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9099 __isl_keep isl_basic_set
*bset2
, int pos
)
9102 isl_basic_map
*bmap
;
9105 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9106 bmap
= join_initial(bset1
, bset2
, pos
);
9107 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9108 isl_dim_out
, dim1
- pos
);
9109 empty
= isl_basic_map_is_empty(bmap
);
9113 isl_basic_map_free(bmap
);
9116 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9117 isl_dim_out
, dim1
- pos
);
9118 empty
= isl_basic_map_is_empty(bmap
);
9121 isl_basic_map_free(bmap
);
9126 isl_basic_map_free(bmap
);
9130 /* Given two sets set1 and set2, check whether
9131 * for any common value of the parameters and dimensions preceding pos
9132 * there is a value of dimension pos in set1 that is larger
9133 * than a value of the same dimension in set2.
9136 * 1 if there exists such a pair
9137 * 0 if there is no such pair, but there is a pair of equal values
9139 * -2 if some error occurred.
9141 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9142 __isl_keep isl_set
*set2
, int pos
)
9150 for (i
= 0; i
< set1
->n
; ++i
)
9151 for (j
= 0; j
< set2
->n
; ++j
) {
9153 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9154 if (f
== 1 || f
== -2)
9163 static isl_bool
isl_basic_map_plain_has_fixed_var(
9164 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9171 return isl_bool_error
;
9172 total
= isl_basic_map_total_dim(bmap
);
9173 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9174 for (; d
+1 > pos
; --d
)
9175 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9179 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9180 return isl_bool_false
;
9181 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9182 return isl_bool_false
;
9183 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9184 return isl_bool_false
;
9186 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9187 return isl_bool_true
;
9189 return isl_bool_false
;
9192 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9193 unsigned pos
, isl_int
*val
)
9201 return isl_bool_error
;
9203 return isl_bool_false
;
9205 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9208 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9209 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9210 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9211 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9212 fixed
= isl_bool_false
;
9215 isl_int_set(*val
, v
);
9221 static isl_bool
isl_basic_set_plain_has_fixed_var(
9222 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9224 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9228 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9229 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9231 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9232 return isl_bool_error
;
9233 return isl_basic_map_plain_has_fixed_var(bmap
,
9234 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9237 /* If "bmap" obviously lies on a hyperplane where the given dimension
9238 * has a fixed value, then return that value.
9239 * Otherwise return NaN.
9241 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9242 __isl_keep isl_basic_map
*bmap
,
9243 enum isl_dim_type type
, unsigned pos
)
9251 ctx
= isl_basic_map_get_ctx(bmap
);
9252 v
= isl_val_alloc(ctx
);
9255 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9257 return isl_val_free(v
);
9259 isl_int_set_si(v
->d
, 1);
9263 return isl_val_nan(ctx
);
9266 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9267 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9269 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9270 return isl_bool_error
;
9271 return isl_map_plain_has_fixed_var(map
,
9272 map_offset(map
, type
) - 1 + pos
, val
);
9275 /* If "map" obviously lies on a hyperplane where the given dimension
9276 * has a fixed value, then return that value.
9277 * Otherwise return NaN.
9279 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9280 enum isl_dim_type type
, unsigned pos
)
9288 ctx
= isl_map_get_ctx(map
);
9289 v
= isl_val_alloc(ctx
);
9292 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9294 return isl_val_free(v
);
9296 isl_int_set_si(v
->d
, 1);
9300 return isl_val_nan(ctx
);
9303 /* If "set" obviously lies on a hyperplane where the given dimension
9304 * has a fixed value, then return that value.
9305 * Otherwise return NaN.
9307 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9308 enum isl_dim_type type
, unsigned pos
)
9310 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9313 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9314 * then return this fixed value in *val.
9316 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9317 unsigned dim
, isl_int
*val
)
9319 return isl_basic_set_plain_has_fixed_var(bset
,
9320 isl_basic_set_n_param(bset
) + dim
, val
);
9323 /* Return -1 if the constraint "c1" should be sorted before "c2"
9324 * and 1 if it should be sorted after "c2".
9325 * Return 0 if the two constraints are the same (up to the constant term).
9327 * In particular, if a constraint involves later variables than another
9328 * then it is sorted after this other constraint.
9329 * uset_gist depends on constraints without existentially quantified
9330 * variables sorting first.
9332 * For constraints that have the same latest variable, those
9333 * with the same coefficient for this latest variable (first in absolute value
9334 * and then in actual value) are grouped together.
9335 * This is useful for detecting pairs of constraints that can
9336 * be chained in their printed representation.
9338 * Finally, within a group, constraints are sorted according to
9339 * their coefficients (excluding the constant term).
9341 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9343 isl_int
**c1
= (isl_int
**) p1
;
9344 isl_int
**c2
= (isl_int
**) p2
;
9346 unsigned size
= *(unsigned *) arg
;
9349 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9350 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9355 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9358 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9362 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9365 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9366 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9367 * and 0 if the two constraints are the same (up to the constant term).
9369 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9370 isl_int
*c1
, isl_int
*c2
)
9376 total
= isl_basic_map_total_dim(bmap
);
9377 return sort_constraint_cmp(&c1
, &c2
, &total
);
9380 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9381 __isl_take isl_basic_map
*bmap
)
9387 if (bmap
->n_ineq
== 0)
9389 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9391 total
= isl_basic_map_total_dim(bmap
);
9392 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9393 &sort_constraint_cmp
, &total
) < 0)
9394 return isl_basic_map_free(bmap
);
9395 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9399 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9400 __isl_take isl_basic_set
*bset
)
9402 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9403 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9406 __isl_give isl_basic_map
*isl_basic_map_normalize(
9407 __isl_take isl_basic_map
*bmap
)
9409 bmap
= isl_basic_map_remove_redundancies(bmap
);
9410 bmap
= isl_basic_map_sort_constraints(bmap
);
9413 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9414 __isl_keep isl_basic_map
*bmap2
)
9418 isl_space
*space1
, *space2
;
9420 if (!bmap1
|| !bmap2
)
9425 space1
= isl_basic_map_peek_space(bmap1
);
9426 space2
= isl_basic_map_peek_space(bmap2
);
9427 cmp
= isl_space_cmp(space1
, space2
);
9430 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9431 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9432 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9433 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9434 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9436 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9438 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9440 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9441 return bmap1
->n_eq
- bmap2
->n_eq
;
9442 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9443 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9444 if (bmap1
->n_div
!= bmap2
->n_div
)
9445 return bmap1
->n_div
- bmap2
->n_div
;
9446 total
= isl_basic_map_total_dim(bmap1
);
9447 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9448 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9452 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9453 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9457 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9458 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9465 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9466 __isl_keep isl_basic_set
*bset2
)
9468 return isl_basic_map_plain_cmp(bset1
, bset2
);
9471 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9477 if (set1
->n
!= set2
->n
)
9478 return set1
->n
- set2
->n
;
9480 for (i
= 0; i
< set1
->n
; ++i
) {
9481 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9489 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9490 __isl_keep isl_basic_map
*bmap2
)
9492 if (!bmap1
|| !bmap2
)
9493 return isl_bool_error
;
9494 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9497 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9498 __isl_keep isl_basic_set
*bset2
)
9500 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9501 bset_to_bmap(bset2
));
9504 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9506 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9507 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9509 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9512 /* Sort the basic maps of "map" and remove duplicate basic maps.
9514 * While removing basic maps, we make sure that the basic maps remain
9515 * sorted because isl_map_normalize expects the basic maps of the result
9518 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9522 map
= isl_map_remove_empty_parts(map
);
9525 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9526 for (i
= map
->n
- 1; i
>= 1; --i
) {
9527 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9529 isl_basic_map_free(map
->p
[i
-1]);
9530 for (j
= i
; j
< map
->n
; ++j
)
9531 map
->p
[j
- 1] = map
->p
[j
];
9538 /* Remove obvious duplicates among the basic maps of "map".
9540 * Unlike isl_map_normalize, this function does not remove redundant
9541 * constraints and only removes duplicates that have exactly the same
9542 * constraints in the input. It does sort the constraints and
9543 * the basic maps to ease the detection of duplicates.
9545 * If "map" has already been normalized or if the basic maps are
9546 * disjoint, then there can be no duplicates.
9548 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9551 isl_basic_map
*bmap
;
9557 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9559 for (i
= 0; i
< map
->n
; ++i
) {
9560 bmap
= isl_basic_map_copy(map
->p
[i
]);
9561 bmap
= isl_basic_map_sort_constraints(bmap
);
9563 return isl_map_free(map
);
9564 isl_basic_map_free(map
->p
[i
]);
9568 map
= sort_and_remove_duplicates(map
);
9572 /* We normalize in place, but if anything goes wrong we need
9573 * to return NULL, so we need to make sure we don't change the
9574 * meaning of any possible other copies of map.
9576 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9579 struct isl_basic_map
*bmap
;
9583 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9585 for (i
= 0; i
< map
->n
; ++i
) {
9586 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9589 isl_basic_map_free(map
->p
[i
]);
9593 map
= sort_and_remove_duplicates(map
);
9595 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9602 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9604 return set_from_map(isl_map_normalize(set_to_map(set
)));
9607 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9608 __isl_keep isl_map
*map2
)
9614 return isl_bool_error
;
9617 return isl_bool_true
;
9618 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9619 return isl_bool_false
;
9621 map1
= isl_map_copy(map1
);
9622 map2
= isl_map_copy(map2
);
9623 map1
= isl_map_normalize(map1
);
9624 map2
= isl_map_normalize(map2
);
9627 equal
= map1
->n
== map2
->n
;
9628 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9629 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9639 return isl_bool_error
;
9642 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9643 __isl_keep isl_set
*set2
)
9645 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9648 /* Return the basic maps in "map" as a list.
9650 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9651 __isl_keep isl_map
*map
)
9655 isl_basic_map_list
*list
;
9659 ctx
= isl_map_get_ctx(map
);
9660 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9662 for (i
= 0; i
< map
->n
; ++i
) {
9663 isl_basic_map
*bmap
;
9665 bmap
= isl_basic_map_copy(map
->p
[i
]);
9666 list
= isl_basic_map_list_add(list
, bmap
);
9672 /* Return the intersection of the elements in the non-empty list "list".
9673 * All elements are assumed to live in the same space.
9675 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9676 __isl_take isl_basic_map_list
*list
)
9679 isl_basic_map
*bmap
;
9683 n
= isl_basic_map_list_n_basic_map(list
);
9685 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9686 "expecting non-empty list", goto error
);
9688 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9689 for (i
= 1; i
< n
; ++i
) {
9690 isl_basic_map
*bmap_i
;
9692 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9693 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9696 isl_basic_map_list_free(list
);
9699 isl_basic_map_list_free(list
);
9703 /* Return the intersection of the elements in the non-empty list "list".
9704 * All elements are assumed to live in the same space.
9706 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9707 __isl_take isl_basic_set_list
*list
)
9709 return isl_basic_map_list_intersect(list
);
9712 /* Return the union of the elements of "list".
9713 * The list is required to have at least one element.
9715 __isl_give isl_set
*isl_basic_set_list_union(
9716 __isl_take isl_basic_set_list
*list
)
9720 isl_basic_set
*bset
;
9725 n
= isl_basic_set_list_n_basic_set(list
);
9727 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9728 "expecting non-empty list", goto error
);
9730 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9731 space
= isl_basic_set_get_space(bset
);
9732 isl_basic_set_free(bset
);
9734 set
= isl_set_alloc_space(space
, n
, 0);
9735 for (i
= 0; i
< n
; ++i
) {
9736 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9737 set
= isl_set_add_basic_set(set
, bset
);
9740 isl_basic_set_list_free(list
);
9743 isl_basic_set_list_free(list
);
9747 /* Return the union of the elements in the non-empty list "list".
9748 * All elements are assumed to live in the same space.
9750 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9757 n
= isl_set_list_n_set(list
);
9759 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9760 "expecting non-empty list", goto error
);
9762 set
= isl_set_list_get_set(list
, 0);
9763 for (i
= 1; i
< n
; ++i
) {
9766 set_i
= isl_set_list_get_set(list
, i
);
9767 set
= isl_set_union(set
, set_i
);
9770 isl_set_list_free(list
);
9773 isl_set_list_free(list
);
9777 __isl_give isl_basic_map
*isl_basic_map_product(
9778 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9780 isl_space
*space_result
= NULL
;
9781 struct isl_basic_map
*bmap
;
9782 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9783 struct isl_dim_map
*dim_map1
, *dim_map2
;
9785 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9787 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9788 isl_space_copy(bmap2
->dim
));
9790 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9791 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9792 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9793 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9794 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9796 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9797 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9798 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9799 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9800 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9801 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9802 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9803 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9804 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9805 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9806 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9808 bmap
= isl_basic_map_alloc_space(space_result
,
9809 bmap1
->n_div
+ bmap2
->n_div
,
9810 bmap1
->n_eq
+ bmap2
->n_eq
,
9811 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9812 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9813 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9814 bmap
= isl_basic_map_simplify(bmap
);
9815 return isl_basic_map_finalize(bmap
);
9817 isl_basic_map_free(bmap1
);
9818 isl_basic_map_free(bmap2
);
9822 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9823 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9825 isl_basic_map
*prod
;
9827 prod
= isl_basic_map_product(bmap1
, bmap2
);
9828 prod
= isl_basic_map_flatten(prod
);
9832 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9833 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9835 return isl_basic_map_flat_range_product(bset1
, bset2
);
9838 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9839 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9841 isl_space
*space1
, *space2
;
9842 isl_space
*space_result
= NULL
;
9843 isl_basic_map
*bmap
;
9844 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9845 struct isl_dim_map
*dim_map1
, *dim_map2
;
9847 if (!bmap1
|| !bmap2
)
9850 space1
= isl_basic_map_get_space(bmap1
);
9851 space2
= isl_basic_map_get_space(bmap2
);
9852 space_result
= isl_space_domain_product(space1
, space2
);
9854 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9855 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9856 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9857 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9859 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9860 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9861 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9862 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9863 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9864 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9865 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9866 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9867 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9868 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9869 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9871 bmap
= isl_basic_map_alloc_space(space_result
,
9872 bmap1
->n_div
+ bmap2
->n_div
,
9873 bmap1
->n_eq
+ bmap2
->n_eq
,
9874 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9875 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9876 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9877 bmap
= isl_basic_map_simplify(bmap
);
9878 return isl_basic_map_finalize(bmap
);
9880 isl_basic_map_free(bmap1
);
9881 isl_basic_map_free(bmap2
);
9885 __isl_give isl_basic_map
*isl_basic_map_range_product(
9886 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9889 isl_space
*space_result
= NULL
;
9890 isl_basic_map
*bmap
;
9891 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9892 struct isl_dim_map
*dim_map1
, *dim_map2
;
9894 rational
= isl_basic_map_is_rational(bmap1
);
9895 if (rational
>= 0 && rational
)
9896 rational
= isl_basic_map_is_rational(bmap2
);
9897 if (!bmap1
|| !bmap2
|| rational
< 0)
9900 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9903 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9904 isl_space_copy(bmap2
->dim
));
9906 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9907 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9908 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9909 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9911 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9912 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9913 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9914 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9915 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9916 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9917 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9918 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9919 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9920 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9921 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9923 bmap
= isl_basic_map_alloc_space(space_result
,
9924 bmap1
->n_div
+ bmap2
->n_div
,
9925 bmap1
->n_eq
+ bmap2
->n_eq
,
9926 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9927 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9928 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9930 bmap
= isl_basic_map_set_rational(bmap
);
9931 bmap
= isl_basic_map_simplify(bmap
);
9932 return isl_basic_map_finalize(bmap
);
9934 isl_basic_map_free(bmap1
);
9935 isl_basic_map_free(bmap2
);
9939 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9940 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9942 isl_basic_map
*prod
;
9944 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9945 prod
= isl_basic_map_flatten_range(prod
);
9949 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9950 * and collect the results.
9951 * The result live in the space obtained by calling "space_product"
9952 * on the spaces of "map1" and "map2".
9953 * If "remove_duplicates" is set then the result may contain duplicates
9954 * (even if the inputs do not) and so we try and remove the obvious
9957 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9958 __isl_take isl_map
*map2
,
9959 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9960 __isl_take isl_space
*right
),
9961 __isl_give isl_basic_map
*(*basic_map_product
)(
9962 __isl_take isl_basic_map
*left
,
9963 __isl_take isl_basic_map
*right
),
9964 int remove_duplicates
)
9967 struct isl_map
*result
;
9971 m
= isl_map_has_equal_params(map1
, map2
);
9975 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
9976 "parameters don't match", goto error
);
9978 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9979 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9980 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9982 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9983 isl_space_copy(map2
->dim
)),
9984 map1
->n
* map2
->n
, flags
);
9987 for (i
= 0; i
< map1
->n
; ++i
)
9988 for (j
= 0; j
< map2
->n
; ++j
) {
9989 struct isl_basic_map
*part
;
9990 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9991 isl_basic_map_copy(map2
->p
[j
]));
9992 if (isl_basic_map_is_empty(part
))
9993 isl_basic_map_free(part
);
9995 result
= isl_map_add_basic_map(result
, part
);
9999 if (remove_duplicates
)
10000 result
= isl_map_remove_obvious_duplicates(result
);
10001 isl_map_free(map1
);
10002 isl_map_free(map2
);
10005 isl_map_free(map1
);
10006 isl_map_free(map2
);
10010 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10012 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10013 __isl_take isl_map
*map2
)
10015 return map_product(map1
, map2
, &isl_space_product
,
10016 &isl_basic_map_product
, 0);
10019 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10020 __isl_take isl_map
*map2
)
10022 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10025 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10027 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10028 __isl_take isl_map
*map2
)
10032 prod
= isl_map_product(map1
, map2
);
10033 prod
= isl_map_flatten(prod
);
10037 /* Given two set A and B, construct its Cartesian product A x B.
10039 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10041 return isl_map_range_product(set1
, set2
);
10044 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10045 __isl_take isl_set
*set2
)
10047 return isl_map_flat_range_product(set1
, set2
);
10050 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10052 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10053 __isl_take isl_map
*map2
)
10055 return map_product(map1
, map2
, &isl_space_domain_product
,
10056 &isl_basic_map_domain_product
, 1);
10059 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10061 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10062 __isl_take isl_map
*map2
)
10064 return map_product(map1
, map2
, &isl_space_range_product
,
10065 &isl_basic_map_range_product
, 1);
10068 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10069 __isl_take isl_map
*map2
)
10071 return isl_map_align_params_map_map_and(map1
, map2
,
10072 &map_domain_product_aligned
);
10075 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10076 __isl_take isl_map
*map2
)
10078 return isl_map_align_params_map_map_and(map1
, map2
,
10079 &map_range_product_aligned
);
10082 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10084 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10087 int total1
, keep1
, total2
, keep2
;
10091 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10092 !isl_space_range_is_wrapping(map
->dim
))
10093 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10094 "not a product", return isl_map_free(map
));
10096 space
= isl_map_get_space(map
);
10097 total1
= isl_space_dim(space
, isl_dim_in
);
10098 total2
= isl_space_dim(space
, isl_dim_out
);
10099 space
= isl_space_factor_domain(space
);
10100 keep1
= isl_space_dim(space
, isl_dim_in
);
10101 keep2
= isl_space_dim(space
, isl_dim_out
);
10102 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10103 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10104 map
= isl_map_reset_space(map
, space
);
10109 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10111 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10114 int total1
, keep1
, total2
, keep2
;
10118 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10119 !isl_space_range_is_wrapping(map
->dim
))
10120 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10121 "not a product", return isl_map_free(map
));
10123 space
= isl_map_get_space(map
);
10124 total1
= isl_space_dim(space
, isl_dim_in
);
10125 total2
= isl_space_dim(space
, isl_dim_out
);
10126 space
= isl_space_factor_range(space
);
10127 keep1
= isl_space_dim(space
, isl_dim_in
);
10128 keep2
= isl_space_dim(space
, isl_dim_out
);
10129 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10130 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10131 map
= isl_map_reset_space(map
, space
);
10136 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10138 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10145 if (!isl_space_domain_is_wrapping(map
->dim
))
10146 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10147 "domain is not a product", return isl_map_free(map
));
10149 space
= isl_map_get_space(map
);
10150 total
= isl_space_dim(space
, isl_dim_in
);
10151 space
= isl_space_domain_factor_domain(space
);
10152 keep
= isl_space_dim(space
, isl_dim_in
);
10153 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10154 map
= isl_map_reset_space(map
, space
);
10159 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10161 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10168 if (!isl_space_domain_is_wrapping(map
->dim
))
10169 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10170 "domain is not a product", return isl_map_free(map
));
10172 space
= isl_map_get_space(map
);
10173 total
= isl_space_dim(space
, isl_dim_in
);
10174 space
= isl_space_domain_factor_range(space
);
10175 keep
= isl_space_dim(space
, isl_dim_in
);
10176 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10177 map
= isl_map_reset_space(map
, space
);
10182 /* Given a map A -> [B -> C], extract the map A -> B.
10184 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10191 if (!isl_space_range_is_wrapping(map
->dim
))
10192 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10193 "range is not a product", return isl_map_free(map
));
10195 space
= isl_map_get_space(map
);
10196 total
= isl_space_dim(space
, isl_dim_out
);
10197 space
= isl_space_range_factor_domain(space
);
10198 keep
= isl_space_dim(space
, isl_dim_out
);
10199 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10200 map
= isl_map_reset_space(map
, space
);
10205 /* Given a map A -> [B -> C], extract the map A -> C.
10207 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10214 if (!isl_space_range_is_wrapping(map
->dim
))
10215 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10216 "range is not a product", return isl_map_free(map
));
10218 space
= isl_map_get_space(map
);
10219 total
= isl_space_dim(space
, isl_dim_out
);
10220 space
= isl_space_range_factor_range(space
);
10221 keep
= isl_space_dim(space
, isl_dim_out
);
10222 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10223 map
= isl_map_reset_space(map
, space
);
10228 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10230 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10231 __isl_take isl_map
*map2
)
10235 prod
= isl_map_domain_product(map1
, map2
);
10236 prod
= isl_map_flatten_domain(prod
);
10240 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10242 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10243 __isl_take isl_map
*map2
)
10247 prod
= isl_map_range_product(map1
, map2
);
10248 prod
= isl_map_flatten_range(prod
);
10252 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10255 uint32_t hash
= isl_hash_init();
10260 bmap
= isl_basic_map_copy(bmap
);
10261 bmap
= isl_basic_map_normalize(bmap
);
10264 total
= isl_basic_map_total_dim(bmap
);
10265 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10266 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10268 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10269 isl_hash_hash(hash
, c_hash
);
10271 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10272 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10274 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10275 isl_hash_hash(hash
, c_hash
);
10277 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10278 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10280 if (isl_int_is_zero(bmap
->div
[i
][0]))
10282 isl_hash_byte(hash
, i
& 0xFF);
10283 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10284 isl_hash_hash(hash
, c_hash
);
10286 isl_basic_map_free(bmap
);
10290 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10292 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10295 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10302 map
= isl_map_copy(map
);
10303 map
= isl_map_normalize(map
);
10307 hash
= isl_hash_init();
10308 for (i
= 0; i
< map
->n
; ++i
) {
10309 uint32_t bmap_hash
;
10310 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10311 isl_hash_hash(hash
, bmap_hash
);
10319 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10321 return isl_map_get_hash(set_to_map(set
));
10324 /* Return the number of basic maps in the (current) representation of "map".
10326 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10328 return map
? map
->n
: 0;
10331 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10333 return set
? set
->n
: 0;
10336 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10337 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10342 return isl_stat_error
;
10344 for (i
= 0; i
< map
->n
; ++i
)
10345 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10346 return isl_stat_error
;
10348 return isl_stat_ok
;
10351 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10352 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10357 return isl_stat_error
;
10359 for (i
= 0; i
< set
->n
; ++i
)
10360 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10361 return isl_stat_error
;
10363 return isl_stat_ok
;
10366 /* Return a list of basic sets, the union of which is equal to "set".
10368 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10369 __isl_keep isl_set
*set
)
10372 isl_basic_set_list
*list
;
10377 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10378 for (i
= 0; i
< set
->n
; ++i
) {
10379 isl_basic_set
*bset
;
10381 bset
= isl_basic_set_copy(set
->p
[i
]);
10382 list
= isl_basic_set_list_add(list
, bset
);
10388 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10395 bset
= isl_basic_set_cow(bset
);
10399 space
= isl_basic_set_get_space(bset
);
10400 space
= isl_space_lift(space
, bset
->n_div
);
10403 isl_space_free(bset
->dim
);
10405 bset
->extra
-= bset
->n_div
;
10408 bset
= isl_basic_set_finalize(bset
);
10412 isl_basic_set_free(bset
);
10416 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10422 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10427 set
= isl_set_cow(set
);
10431 n_div
= set
->p
[0]->n_div
;
10432 space
= isl_set_get_space(set
);
10433 space
= isl_space_lift(space
, n_div
);
10436 isl_space_free(set
->dim
);
10439 for (i
= 0; i
< set
->n
; ++i
) {
10440 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10451 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10459 dim
= isl_basic_set_total_dim(bset
);
10460 size
+= bset
->n_eq
* (1 + dim
);
10461 size
+= bset
->n_ineq
* (1 + dim
);
10462 size
+= bset
->n_div
* (2 + dim
);
10467 int isl_set_size(__isl_keep isl_set
*set
)
10475 for (i
= 0; i
< set
->n
; ++i
)
10476 size
+= isl_basic_set_size(set
->p
[i
]);
10481 /* Check if there is any lower bound (if lower == 0) and/or upper
10482 * bound (if upper == 0) on the specified dim.
10484 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10485 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10489 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10490 return isl_bool_error
;
10492 pos
+= isl_basic_map_offset(bmap
, type
);
10494 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10495 if (isl_int_is_zero(bmap
->div
[i
][0]))
10497 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10498 return isl_bool_true
;
10501 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10502 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10503 return isl_bool_true
;
10505 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10506 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10513 return lower
&& upper
;
10516 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10517 enum isl_dim_type type
, unsigned pos
)
10519 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10522 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10523 enum isl_dim_type type
, unsigned pos
)
10525 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10528 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10529 enum isl_dim_type type
, unsigned pos
)
10531 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10534 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10535 enum isl_dim_type type
, unsigned pos
)
10540 return isl_bool_error
;
10542 for (i
= 0; i
< map
->n
; ++i
) {
10544 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10545 if (bounded
< 0 || !bounded
)
10549 return isl_bool_true
;
10552 /* Return true if the specified dim is involved in both an upper bound
10553 * and a lower bound.
10555 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10556 enum isl_dim_type type
, unsigned pos
)
10558 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10561 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10563 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10564 enum isl_dim_type type
, unsigned pos
,
10565 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10566 enum isl_dim_type type
, unsigned pos
))
10571 return isl_bool_error
;
10573 for (i
= 0; i
< map
->n
; ++i
) {
10575 bounded
= fn(map
->p
[i
], type
, pos
);
10576 if (bounded
< 0 || bounded
)
10580 return isl_bool_false
;
10583 /* Return 1 if the specified dim is involved in any lower bound.
10585 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10586 enum isl_dim_type type
, unsigned pos
)
10588 return has_any_bound(set
, type
, pos
,
10589 &isl_basic_map_dim_has_lower_bound
);
10592 /* Return 1 if the specified dim is involved in any upper bound.
10594 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10595 enum isl_dim_type type
, unsigned pos
)
10597 return has_any_bound(set
, type
, pos
,
10598 &isl_basic_map_dim_has_upper_bound
);
10601 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10603 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10604 enum isl_dim_type type
, unsigned pos
,
10605 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10606 enum isl_dim_type type
, unsigned pos
))
10611 return isl_bool_error
;
10613 for (i
= 0; i
< map
->n
; ++i
) {
10615 bounded
= fn(map
->p
[i
], type
, pos
);
10616 if (bounded
< 0 || !bounded
)
10620 return isl_bool_true
;
10623 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10625 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10626 enum isl_dim_type type
, unsigned pos
)
10628 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10631 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10633 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10634 enum isl_dim_type type
, unsigned pos
)
10636 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10639 /* For each of the "n" variables starting at "first", determine
10640 * the sign of the variable and put the results in the first "n"
10641 * elements of the array "signs".
10643 * 1 means that the variable is non-negative
10644 * -1 means that the variable is non-positive
10645 * 0 means the variable attains both positive and negative values.
10647 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10648 unsigned first
, unsigned n
, int *signs
)
10650 isl_vec
*bound
= NULL
;
10651 struct isl_tab
*tab
= NULL
;
10652 struct isl_tab_undo
*snap
;
10655 if (!bset
|| !signs
)
10656 return isl_stat_error
;
10658 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10659 tab
= isl_tab_from_basic_set(bset
, 0);
10660 if (!bound
|| !tab
)
10663 isl_seq_clr(bound
->el
, bound
->size
);
10664 isl_int_set_si(bound
->el
[0], -1);
10666 snap
= isl_tab_snap(tab
);
10667 for (i
= 0; i
< n
; ++i
) {
10670 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10671 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10673 empty
= tab
->empty
;
10674 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10675 if (isl_tab_rollback(tab
, snap
) < 0)
10683 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10684 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10686 empty
= tab
->empty
;
10687 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10688 if (isl_tab_rollback(tab
, snap
) < 0)
10691 signs
[i
] = empty
? -1 : 0;
10695 isl_vec_free(bound
);
10696 return isl_stat_ok
;
10699 isl_vec_free(bound
);
10700 return isl_stat_error
;
10703 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10704 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10706 if (!bset
|| !signs
)
10707 return isl_stat_error
;
10708 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10709 return isl_stat_error
;
10711 first
+= pos(bset
->dim
, type
) - 1;
10712 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10715 /* Is it possible for the integer division "div" to depend (possibly
10716 * indirectly) on any output dimensions?
10718 * If the div is undefined, then we conservatively assume that it
10719 * may depend on them.
10720 * Otherwise, we check if it actually depends on them or on any integer
10721 * divisions that may depend on them.
10723 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10726 unsigned n_out
, o_out
;
10727 unsigned n_div
, o_div
;
10729 if (isl_int_is_zero(bmap
->div
[div
][0]))
10730 return isl_bool_true
;
10732 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10733 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10735 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10736 return isl_bool_true
;
10738 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10739 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10741 for (i
= 0; i
< n_div
; ++i
) {
10742 isl_bool may_involve
;
10744 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10746 may_involve
= div_may_involve_output(bmap
, i
);
10747 if (may_involve
< 0 || may_involve
)
10748 return may_involve
;
10751 return isl_bool_false
;
10754 /* Return the first integer division of "bmap" in the range
10755 * [first, first + n[ that may depend on any output dimensions and
10756 * that has a non-zero coefficient in "c" (where the first coefficient
10757 * in "c" corresponds to integer division "first").
10759 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10760 isl_int
*c
, int first
, int n
)
10767 for (k
= first
; k
< first
+ n
; ++k
) {
10768 isl_bool may_involve
;
10770 if (isl_int_is_zero(c
[k
]))
10772 may_involve
= div_may_involve_output(bmap
, k
);
10773 if (may_involve
< 0)
10782 /* Look for a pair of inequality constraints in "bmap" of the form
10784 * -l + i >= 0 or i >= l
10786 * n + l - i >= 0 or i <= l + n
10788 * with n < "m" and i the output dimension at position "pos".
10789 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10790 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10791 * and earlier output dimensions, as well as integer divisions that do
10792 * not involve any of the output dimensions.
10794 * Return the index of the first inequality constraint or bmap->n_ineq
10795 * if no such pair can be found.
10797 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10798 int pos
, isl_int m
)
10803 unsigned n_div
, o_div
;
10804 unsigned n_out
, o_out
;
10810 ctx
= isl_basic_map_get_ctx(bmap
);
10811 total
= isl_basic_map_total_dim(bmap
);
10812 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10813 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10814 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10815 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10816 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10817 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10819 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10820 n_out
- (pos
+ 1)) != -1)
10822 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10825 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10826 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10829 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10830 bmap
->ineq
[j
] + 1, total
))
10834 if (j
>= bmap
->n_ineq
)
10836 isl_int_add(bmap
->ineq
[i
][0],
10837 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10838 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10839 isl_int_sub(bmap
->ineq
[i
][0],
10840 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10843 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10849 return bmap
->n_ineq
;
10852 /* Return the index of the equality of "bmap" that defines
10853 * the output dimension "pos" in terms of earlier dimensions.
10854 * The equality may also involve integer divisions, as long
10855 * as those integer divisions are defined in terms of
10856 * parameters or input dimensions.
10857 * In this case, *div is set to the number of integer divisions and
10858 * *ineq is set to the number of inequality constraints (provided
10859 * div and ineq are not NULL).
10861 * The equality may also involve a single integer division involving
10862 * the output dimensions (typically only output dimension "pos") as
10863 * long as the coefficient of output dimension "pos" is 1 or -1 and
10864 * there is a pair of constraints i >= l and i <= l + n, with i referring
10865 * to output dimension "pos", l an expression involving only earlier
10866 * dimensions and n smaller than the coefficient of the integer division
10867 * in the equality. In this case, the output dimension can be defined
10868 * in terms of a modulo expression that does not involve the integer division.
10869 * *div is then set to this single integer division and
10870 * *ineq is set to the index of constraint i >= l.
10872 * Return bmap->n_eq if there is no such equality.
10873 * Return -1 on error.
10875 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10876 int pos
, int *div
, int *ineq
)
10879 unsigned n_out
, o_out
;
10880 unsigned n_div
, o_div
;
10885 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10886 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10887 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10888 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10891 *ineq
= bmap
->n_ineq
;
10894 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10895 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10897 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10898 n_out
- (pos
+ 1)) != -1)
10900 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10904 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10905 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10907 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10908 k
+ 1, n_div
- (k
+1)) < n_div
)
10910 l
= find_modulo_constraint_pair(bmap
, pos
,
10911 bmap
->eq
[j
][o_div
+ k
]);
10914 if (l
>= bmap
->n_ineq
)
10926 /* Check if the given basic map is obviously single-valued.
10927 * In particular, for each output dimension, check that there is
10928 * an equality that defines the output dimension in terms of
10929 * earlier dimensions.
10931 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10937 return isl_bool_error
;
10939 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10941 for (i
= 0; i
< n_out
; ++i
) {
10944 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10947 return isl_bool_error
;
10948 if (eq
>= bmap
->n_eq
)
10949 return isl_bool_false
;
10952 return isl_bool_true
;
10955 /* Check if the given basic map is single-valued.
10956 * We simply compute
10960 * and check if the result is a subset of the identity mapping.
10962 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10965 isl_basic_map
*test
;
10969 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10973 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10974 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10976 space
= isl_basic_map_get_space(bmap
);
10977 space
= isl_space_map_from_set(isl_space_range(space
));
10978 id
= isl_basic_map_identity(space
);
10980 sv
= isl_basic_map_is_subset(test
, id
);
10982 isl_basic_map_free(test
);
10983 isl_basic_map_free(id
);
10988 /* Check if the given map is obviously single-valued.
10990 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10993 return isl_bool_error
;
10995 return isl_bool_true
;
10997 return isl_bool_false
;
10999 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11002 /* Check if the given map is single-valued.
11003 * We simply compute
11007 * and check if the result is a subset of the identity mapping.
11009 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11016 sv
= isl_map_plain_is_single_valued(map
);
11020 test
= isl_map_reverse(isl_map_copy(map
));
11021 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11023 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11024 id
= isl_map_identity(dim
);
11026 sv
= isl_map_is_subset(test
, id
);
11028 isl_map_free(test
);
11034 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11038 map
= isl_map_copy(map
);
11039 map
= isl_map_reverse(map
);
11040 in
= isl_map_is_single_valued(map
);
11046 /* Check if the given map is obviously injective.
11048 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11052 map
= isl_map_copy(map
);
11053 map
= isl_map_reverse(map
);
11054 in
= isl_map_plain_is_single_valued(map
);
11060 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11064 sv
= isl_map_is_single_valued(map
);
11068 return isl_map_is_injective(map
);
11071 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11073 return isl_map_is_single_valued(set_to_map(set
));
11076 /* Does "map" only map elements to themselves?
11078 * If the domain and range spaces are different, then "map"
11079 * is considered not to be an identity relation, even if it is empty.
11080 * Otherwise, construct the maximal identity relation and
11081 * check whether "map" is a subset of this relation.
11083 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11087 isl_bool equal
, is_identity
;
11089 space
= isl_map_get_space(map
);
11090 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11091 isl_space_free(space
);
11092 if (equal
< 0 || !equal
)
11095 id
= isl_map_identity(isl_map_get_space(map
));
11096 is_identity
= isl_map_is_subset(map
, id
);
11099 return is_identity
;
11102 int isl_map_is_translation(__isl_keep isl_map
*map
)
11107 delta
= isl_map_deltas(isl_map_copy(map
));
11108 ok
= isl_set_is_singleton(delta
);
11109 isl_set_free(delta
);
11114 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11116 if (isl_seq_first_non_zero(p
, pos
) != -1)
11118 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11123 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11130 return isl_bool_error
;
11132 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11133 return isl_bool_false
;
11135 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11136 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11137 for (j
= 0; j
< nvar
; ++j
) {
11138 int lower
= 0, upper
= 0;
11139 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11140 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11142 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11143 return isl_bool_false
;
11146 if (i
< bset
->n_eq
)
11148 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11149 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11151 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11152 return isl_bool_false
;
11153 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11158 if (!lower
|| !upper
)
11159 return isl_bool_false
;
11162 return isl_bool_true
;
11165 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11168 return isl_bool_error
;
11170 return isl_bool_false
;
11172 return isl_basic_set_is_box(set
->p
[0]);
11175 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11178 return isl_bool_error
;
11180 return isl_space_is_wrapping(bset
->dim
);
11183 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11186 return isl_bool_error
;
11188 return isl_space_is_wrapping(set
->dim
);
11191 /* Modify the space of "map" through a call to "change".
11192 * If "can_change" is set (not NULL), then first call it to check
11193 * if the modification is allowed, printing the error message "cannot_change"
11196 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11197 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11198 const char *cannot_change
,
11199 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11207 ok
= can_change
? can_change(map
) : isl_bool_true
;
11209 return isl_map_free(map
);
11211 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11212 return isl_map_free(map
));
11214 space
= change(isl_map_get_space(map
));
11215 map
= isl_map_reset_space(map
, space
);
11220 /* Is the domain of "map" a wrapped relation?
11222 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11225 return isl_bool_error
;
11227 return isl_space_domain_is_wrapping(map
->dim
);
11230 /* Does "map" have a wrapped relation in both domain and range?
11232 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11234 return isl_space_is_product(isl_map_peek_space(map
));
11237 /* Is the range of "map" a wrapped relation?
11239 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11242 return isl_bool_error
;
11244 return isl_space_range_is_wrapping(map
->dim
);
11247 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11249 bmap
= isl_basic_map_cow(bmap
);
11253 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11257 bmap
= isl_basic_map_finalize(bmap
);
11259 return bset_from_bmap(bmap
);
11261 isl_basic_map_free(bmap
);
11265 /* Given a map A -> B, return the set (A -> B).
11267 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11269 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11272 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11274 bset
= isl_basic_set_cow(bset
);
11278 bset
->dim
= isl_space_unwrap(bset
->dim
);
11282 bset
= isl_basic_set_finalize(bset
);
11284 return bset_to_bmap(bset
);
11286 isl_basic_set_free(bset
);
11290 /* Given a set (A -> B), return the map A -> B.
11291 * Error out if "set" is not of the form (A -> B).
11293 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11295 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11296 "not a wrapping set", &isl_space_unwrap
);
11299 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11300 enum isl_dim_type type
)
11305 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11308 bmap
= isl_basic_map_cow(bmap
);
11312 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11316 bmap
= isl_basic_map_finalize(bmap
);
11320 isl_basic_map_free(bmap
);
11324 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11325 enum isl_dim_type type
)
11332 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11335 map
= isl_map_cow(map
);
11339 for (i
= 0; i
< map
->n
; ++i
) {
11340 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11344 map
->dim
= isl_space_reset(map
->dim
, type
);
11354 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11359 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11362 bmap
= isl_basic_map_cow(bmap
);
11366 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11370 bmap
= isl_basic_map_finalize(bmap
);
11374 isl_basic_map_free(bmap
);
11378 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11380 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11383 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11384 __isl_take isl_basic_map
*bmap
)
11389 if (!bmap
->dim
->nested
[0])
11392 bmap
= isl_basic_map_cow(bmap
);
11396 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11400 bmap
= isl_basic_map_finalize(bmap
);
11404 isl_basic_map_free(bmap
);
11408 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11409 __isl_take isl_basic_map
*bmap
)
11414 if (!bmap
->dim
->nested
[1])
11417 bmap
= isl_basic_map_cow(bmap
);
11421 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11425 bmap
= isl_basic_map_finalize(bmap
);
11429 isl_basic_map_free(bmap
);
11433 /* Remove any internal structure from the spaces of domain and range of "map".
11435 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11440 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11443 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11446 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11448 return set_from_map(isl_map_flatten(set_to_map(set
)));
11451 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11453 isl_space
*space
, *flat_space
;
11456 space
= isl_set_get_space(set
);
11457 flat_space
= isl_space_flatten(isl_space_copy(space
));
11458 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11460 map
= isl_map_intersect_domain(map
, set
);
11465 /* Remove any internal structure from the space of the domain of "map".
11467 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11472 if (!map
->dim
->nested
[0])
11475 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11478 /* Remove any internal structure from the space of the range of "map".
11480 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11485 if (!map
->dim
->nested
[1])
11488 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11491 /* Reorder the dimensions of "bmap" according to the given dim_map
11492 * and set the dimension specification to "space" and
11493 * perform Gaussian elimination on the result.
11495 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11496 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11498 isl_basic_map
*res
;
11502 if (!bmap
|| !space
|| !dim_map
)
11505 flags
= bmap
->flags
;
11506 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11507 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11508 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11509 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11510 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11511 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11513 res
->flags
= flags
;
11514 res
= isl_basic_map_gauss(res
, NULL
);
11515 res
= isl_basic_map_finalize(res
);
11518 isl_dim_map_free(dim_map
);
11519 isl_basic_map_free(bmap
);
11520 isl_space_free(space
);
11524 /* Reorder the dimensions of "map" according to given reordering.
11526 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11527 __isl_take isl_reordering
*r
)
11530 struct isl_dim_map
*dim_map
;
11532 map
= isl_map_cow(map
);
11533 dim_map
= isl_dim_map_from_reordering(r
);
11534 if (!map
|| !r
|| !dim_map
)
11537 for (i
= 0; i
< map
->n
; ++i
) {
11538 struct isl_dim_map
*dim_map_i
;
11541 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11543 space
= isl_reordering_get_space(r
);
11544 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11550 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11551 map
= isl_map_unmark_normalized(map
);
11553 isl_reordering_free(r
);
11554 isl_dim_map_free(dim_map
);
11557 isl_dim_map_free(dim_map
);
11559 isl_reordering_free(r
);
11563 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11564 __isl_take isl_reordering
*r
)
11566 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11569 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11570 __isl_take isl_space
*model
)
11575 if (!map
|| !model
)
11578 ctx
= isl_space_get_ctx(model
);
11579 if (!isl_space_has_named_params(model
))
11580 isl_die(ctx
, isl_error_invalid
,
11581 "model has unnamed parameters", goto error
);
11582 if (isl_map_check_named_params(map
) < 0)
11584 aligned
= isl_map_space_has_equal_params(map
, model
);
11588 isl_reordering
*exp
;
11590 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11591 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11592 map
= isl_map_realign(map
, exp
);
11595 isl_space_free(model
);
11598 isl_space_free(model
);
11603 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11604 __isl_take isl_space
*model
)
11606 return isl_map_align_params(set
, model
);
11609 /* Align the parameters of "bmap" to those of "model", introducing
11610 * additional parameters if needed.
11612 __isl_give isl_basic_map
*isl_basic_map_align_params(
11613 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11616 isl_bool equal_params
;
11618 if (!bmap
|| !model
)
11621 ctx
= isl_space_get_ctx(model
);
11622 if (!isl_space_has_named_params(model
))
11623 isl_die(ctx
, isl_error_invalid
,
11624 "model has unnamed parameters", goto error
);
11625 if (isl_basic_map_check_named_params(bmap
) < 0)
11627 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11628 if (equal_params
< 0)
11630 if (!equal_params
) {
11631 isl_reordering
*exp
;
11632 struct isl_dim_map
*dim_map
;
11634 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11635 exp
= isl_reordering_extend_space(exp
,
11636 isl_basic_map_get_space(bmap
));
11637 dim_map
= isl_dim_map_from_reordering(exp
);
11638 bmap
= isl_basic_map_realign(bmap
,
11639 isl_reordering_get_space(exp
),
11640 isl_dim_map_extend(dim_map
, bmap
));
11641 isl_reordering_free(exp
);
11642 isl_dim_map_free(dim_map
);
11645 isl_space_free(model
);
11648 isl_space_free(model
);
11649 isl_basic_map_free(bmap
);
11653 /* Do "bset" and "space" have the same parameters?
11655 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11656 __isl_keep isl_space
*space
)
11658 isl_space
*bset_space
;
11660 bset_space
= isl_basic_set_peek_space(bset
);
11661 return isl_space_has_equal_params(bset_space
, space
);
11664 /* Do "map" and "space" have the same parameters?
11666 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11667 __isl_keep isl_space
*space
)
11669 isl_space
*map_space
;
11671 map_space
= isl_map_peek_space(map
);
11672 return isl_space_has_equal_params(map_space
, space
);
11675 /* Do "set" and "space" have the same parameters?
11677 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11678 __isl_keep isl_space
*space
)
11680 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11683 /* Align the parameters of "bset" to those of "model", introducing
11684 * additional parameters if needed.
11686 __isl_give isl_basic_set
*isl_basic_set_align_params(
11687 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11689 return isl_basic_map_align_params(bset
, model
);
11692 /* Drop all parameters not referenced by "map".
11694 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11698 if (isl_map_check_named_params(map
) < 0)
11699 return isl_map_free(map
);
11701 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11704 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11706 return isl_map_free(map
);
11708 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11714 /* Drop all parameters not referenced by "set".
11716 __isl_give isl_set
*isl_set_drop_unused_params(
11717 __isl_take isl_set
*set
)
11719 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11722 /* Drop all parameters not referenced by "bmap".
11724 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11725 __isl_take isl_basic_map
*bmap
)
11729 if (isl_basic_map_check_named_params(bmap
) < 0)
11730 return isl_basic_map_free(bmap
);
11732 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11735 involves
= isl_basic_map_involves_dims(bmap
,
11736 isl_dim_param
, i
, 1);
11738 return isl_basic_map_free(bmap
);
11740 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11746 /* Drop all parameters not referenced by "bset".
11748 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11749 __isl_take isl_basic_set
*bset
)
11751 return bset_from_bmap(isl_basic_map_drop_unused_params(
11752 bset_to_bmap(bset
)));
11755 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11756 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11757 enum isl_dim_type c2
, enum isl_dim_type c3
,
11758 enum isl_dim_type c4
, enum isl_dim_type c5
)
11760 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11761 struct isl_mat
*mat
;
11767 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11768 isl_basic_map_total_dim(bmap
) + 1);
11771 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11772 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11773 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11774 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11775 isl_int_set(mat
->row
[i
][pos
],
11776 bmap
->eq
[i
][off
+ k
]);
11784 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11785 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11786 enum isl_dim_type c2
, enum isl_dim_type c3
,
11787 enum isl_dim_type c4
, enum isl_dim_type c5
)
11789 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11790 struct isl_mat
*mat
;
11796 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11797 isl_basic_map_total_dim(bmap
) + 1);
11800 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11801 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11802 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11803 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11804 isl_int_set(mat
->row
[i
][pos
],
11805 bmap
->ineq
[i
][off
+ k
]);
11813 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11814 __isl_take isl_space
*space
,
11815 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11816 enum isl_dim_type c2
, enum isl_dim_type c3
,
11817 enum isl_dim_type c4
, enum isl_dim_type c5
)
11819 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11820 isl_basic_map
*bmap
= NULL
;
11826 if (!space
|| !eq
|| !ineq
)
11829 if (eq
->n_col
!= ineq
->n_col
)
11830 isl_die(space
->ctx
, isl_error_invalid
,
11831 "equalities and inequalities matrices should have "
11832 "same number of columns", goto error
);
11834 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11836 if (eq
->n_col
< total
)
11837 isl_die(space
->ctx
, isl_error_invalid
,
11838 "number of columns too small", goto error
);
11840 extra
= eq
->n_col
- total
;
11842 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11843 eq
->n_row
, ineq
->n_row
);
11846 for (i
= 0; i
< extra
; ++i
) {
11847 k
= isl_basic_map_alloc_div(bmap
);
11850 isl_int_set_si(bmap
->div
[k
][0], 0);
11852 for (i
= 0; i
< eq
->n_row
; ++i
) {
11853 l
= isl_basic_map_alloc_equality(bmap
);
11856 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11857 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11858 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11859 isl_int_set(bmap
->eq
[l
][off
+ k
],
11865 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11866 l
= isl_basic_map_alloc_inequality(bmap
);
11869 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11870 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11871 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11872 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11873 ineq
->row
[i
][pos
]);
11879 isl_space_free(space
);
11881 isl_mat_free(ineq
);
11883 bmap
= isl_basic_map_simplify(bmap
);
11884 return isl_basic_map_finalize(bmap
);
11886 isl_space_free(space
);
11888 isl_mat_free(ineq
);
11889 isl_basic_map_free(bmap
);
11893 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11894 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11895 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11897 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11898 c1
, c2
, c3
, c4
, isl_dim_in
);
11901 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11902 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11903 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11905 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11906 c1
, c2
, c3
, c4
, isl_dim_in
);
11909 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11910 __isl_take isl_space
*dim
,
11911 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11912 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11914 isl_basic_map
*bmap
;
11915 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11916 c1
, c2
, c3
, c4
, isl_dim_in
);
11917 return bset_from_bmap(bmap
);
11920 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11923 return isl_bool_error
;
11925 return isl_space_can_zip(bmap
->dim
);
11928 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11931 return isl_bool_error
;
11933 return isl_space_can_zip(map
->dim
);
11936 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11937 * (A -> C) -> (B -> D).
11939 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11948 if (!isl_basic_map_can_zip(bmap
))
11949 isl_die(bmap
->ctx
, isl_error_invalid
,
11950 "basic map cannot be zipped", goto error
);
11951 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11952 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11953 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11954 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11955 bmap
= isl_basic_map_cow(bmap
);
11956 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11959 bmap
->dim
= isl_space_zip(bmap
->dim
);
11962 bmap
= isl_basic_map_mark_final(bmap
);
11965 isl_basic_map_free(bmap
);
11969 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11970 * (A -> C) -> (B -> D).
11972 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11979 if (!isl_map_can_zip(map
))
11980 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11983 map
= isl_map_cow(map
);
11987 for (i
= 0; i
< map
->n
; ++i
) {
11988 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11993 map
->dim
= isl_space_zip(map
->dim
);
12003 /* Can we apply isl_basic_map_curry to "bmap"?
12004 * That is, does it have a nested relation in its domain?
12006 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12009 return isl_bool_error
;
12011 return isl_space_can_curry(bmap
->dim
);
12014 /* Can we apply isl_map_curry to "map"?
12015 * That is, does it have a nested relation in its domain?
12017 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12020 return isl_bool_error
;
12022 return isl_space_can_curry(map
->dim
);
12025 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12028 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12034 if (!isl_basic_map_can_curry(bmap
))
12035 isl_die(bmap
->ctx
, isl_error_invalid
,
12036 "basic map cannot be curried", goto error
);
12037 bmap
= isl_basic_map_cow(bmap
);
12040 bmap
->dim
= isl_space_curry(bmap
->dim
);
12043 bmap
= isl_basic_map_mark_final(bmap
);
12046 isl_basic_map_free(bmap
);
12050 /* Given a map (A -> B) -> C, return the corresponding map
12053 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12055 return isl_map_change_space(map
, &isl_map_can_curry
,
12056 "map cannot be curried", &isl_space_curry
);
12059 /* Can isl_map_range_curry be applied to "map"?
12060 * That is, does it have a nested relation in its range,
12061 * the domain of which is itself a nested relation?
12063 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12066 return isl_bool_error
;
12068 return isl_space_can_range_curry(map
->dim
);
12071 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12072 * A -> (B -> (C -> D)).
12074 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12076 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12077 "map range cannot be curried",
12078 &isl_space_range_curry
);
12081 /* Can we apply isl_basic_map_uncurry to "bmap"?
12082 * That is, does it have a nested relation in its domain?
12084 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12087 return isl_bool_error
;
12089 return isl_space_can_uncurry(bmap
->dim
);
12092 /* Can we apply isl_map_uncurry to "map"?
12093 * That is, does it have a nested relation in its domain?
12095 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12098 return isl_bool_error
;
12100 return isl_space_can_uncurry(map
->dim
);
12103 /* Given a basic map A -> (B -> C), return the corresponding basic map
12106 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12112 if (!isl_basic_map_can_uncurry(bmap
))
12113 isl_die(bmap
->ctx
, isl_error_invalid
,
12114 "basic map cannot be uncurried",
12115 return isl_basic_map_free(bmap
));
12116 bmap
= isl_basic_map_cow(bmap
);
12119 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12121 return isl_basic_map_free(bmap
);
12122 bmap
= isl_basic_map_mark_final(bmap
);
12126 /* Given a map A -> (B -> C), return the corresponding map
12129 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12131 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12132 "map cannot be uncurried", &isl_space_uncurry
);
12135 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12136 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12138 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12141 /* Construct a basic map where the given dimensions are equal to each other.
12143 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12144 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12146 isl_basic_map
*bmap
= NULL
;
12149 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12150 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12153 if (type1
== type2
&& pos1
== pos2
)
12154 return isl_basic_map_universe(space
);
12156 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12157 i
= isl_basic_map_alloc_equality(bmap
);
12160 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12161 pos1
+= isl_basic_map_offset(bmap
, type1
);
12162 pos2
+= isl_basic_map_offset(bmap
, type2
);
12163 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12164 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12165 bmap
= isl_basic_map_finalize(bmap
);
12166 isl_space_free(space
);
12169 isl_space_free(space
);
12170 isl_basic_map_free(bmap
);
12174 /* Add a constraint imposing that the given two dimensions are equal.
12176 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12177 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12181 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12183 bmap
= isl_basic_map_intersect(bmap
, eq
);
12188 /* Add a constraint imposing that the given two dimensions are equal.
12190 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12191 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12193 isl_basic_map
*bmap
;
12195 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12197 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12202 /* Add a constraint imposing that the given two dimensions have opposite values.
12204 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12205 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12207 isl_basic_map
*bmap
= NULL
;
12210 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12211 return isl_map_free(map
);
12212 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12213 return isl_map_free(map
);
12215 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12216 i
= isl_basic_map_alloc_equality(bmap
);
12219 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12220 pos1
+= isl_basic_map_offset(bmap
, type1
);
12221 pos2
+= isl_basic_map_offset(bmap
, type2
);
12222 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12223 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12224 bmap
= isl_basic_map_finalize(bmap
);
12226 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12230 isl_basic_map_free(bmap
);
12235 /* Construct a constraint imposing that the value of the first dimension is
12236 * greater than or equal to that of the second.
12238 static __isl_give isl_constraint
*constraint_order_ge(
12239 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12240 enum isl_dim_type type2
, int pos2
)
12244 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12245 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12246 space
= isl_space_free(space
);
12250 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12252 if (type1
== type2
&& pos1
== pos2
)
12255 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12256 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12261 /* Add a constraint imposing that the value of the first dimension is
12262 * greater than or equal to that of the second.
12264 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12265 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12270 if (type1
== type2
&& pos1
== pos2
)
12272 space
= isl_basic_map_get_space(bmap
);
12273 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12274 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12279 /* Add a constraint imposing that the value of the first dimension is
12280 * greater than or equal to that of the second.
12282 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12283 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12288 if (type1
== type2
&& pos1
== pos2
)
12290 space
= isl_map_get_space(map
);
12291 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12292 map
= isl_map_add_constraint(map
, c
);
12297 /* Add a constraint imposing that the value of the first dimension is
12298 * less than or equal to that of the second.
12300 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12301 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12303 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12306 /* Construct a basic map where the value of the first dimension is
12307 * greater than that of the second.
12309 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12310 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12312 isl_basic_map
*bmap
= NULL
;
12315 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12316 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12319 if (type1
== type2
&& pos1
== pos2
)
12320 return isl_basic_map_empty(space
);
12322 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12323 i
= isl_basic_map_alloc_inequality(bmap
);
12325 return isl_basic_map_free(bmap
);
12326 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12327 pos1
+= isl_basic_map_offset(bmap
, type1
);
12328 pos2
+= isl_basic_map_offset(bmap
, type2
);
12329 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12330 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12331 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12332 bmap
= isl_basic_map_finalize(bmap
);
12336 isl_space_free(space
);
12337 isl_basic_map_free(bmap
);
12341 /* Add a constraint imposing that the value of the first dimension is
12342 * greater than that of the second.
12344 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12345 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12349 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12351 bmap
= isl_basic_map_intersect(bmap
, gt
);
12356 /* Add a constraint imposing that the value of the first dimension is
12357 * greater than that of the second.
12359 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12360 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12362 isl_basic_map
*bmap
;
12364 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12366 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * smaller than that of the second.
12374 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12375 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12377 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12380 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12384 isl_local_space
*ls
;
12389 if (!isl_basic_map_divs_known(bmap
))
12390 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12391 "some divs are unknown", return NULL
);
12393 ls
= isl_basic_map_get_local_space(bmap
);
12394 div
= isl_local_space_get_div(ls
, pos
);
12395 isl_local_space_free(ls
);
12400 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12403 return isl_basic_map_get_div(bset
, pos
);
12406 /* Plug in "subs" for dimension "type", "pos" of "bset".
12408 * Let i be the dimension to replace and let "subs" be of the form
12412 * Any integer division with a non-zero coefficient for i,
12414 * floor((a i + g)/m)
12418 * floor((a f + d g)/(m d))
12420 * Constraints of the form
12428 * We currently require that "subs" is an integral expression.
12429 * Handling rational expressions may require us to add stride constraints
12430 * as we do in isl_basic_set_preimage_multi_aff.
12432 __isl_give isl_basic_set
*isl_basic_set_substitute(
12433 __isl_take isl_basic_set
*bset
,
12434 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12440 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12443 bset
= isl_basic_set_cow(bset
);
12444 if (!bset
|| !subs
)
12447 ctx
= isl_basic_set_get_ctx(bset
);
12448 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12449 isl_die(ctx
, isl_error_invalid
,
12450 "spaces don't match", goto error
);
12451 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12452 isl_die(ctx
, isl_error_unsupported
,
12453 "cannot handle divs yet", goto error
);
12454 if (!isl_int_is_one(subs
->v
->el
[0]))
12455 isl_die(ctx
, isl_error_invalid
,
12456 "can only substitute integer expressions", goto error
);
12458 pos
+= isl_basic_set_offset(bset
, type
);
12462 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12463 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12465 isl_int_set(v
, bset
->eq
[i
][pos
]);
12466 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12467 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12468 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12471 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12472 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12474 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12475 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12476 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12477 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12480 for (i
= 0; i
< bset
->n_div
; ++i
) {
12481 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12483 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12484 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12485 isl_seq_combine(bset
->div
[i
] + 1,
12486 subs
->v
->el
[0], bset
->div
[i
] + 1,
12487 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12488 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12493 bset
= isl_basic_set_simplify(bset
);
12494 return isl_basic_set_finalize(bset
);
12496 isl_basic_set_free(bset
);
12500 /* Plug in "subs" for dimension "type", "pos" of "set".
12502 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12503 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12507 if (set
&& isl_set_plain_is_empty(set
))
12510 set
= isl_set_cow(set
);
12514 for (i
= set
->n
- 1; i
>= 0; --i
) {
12515 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12516 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12527 /* Check if the range of "ma" is compatible with the domain or range
12528 * (depending on "type") of "bmap".
12530 static isl_stat
check_basic_map_compatible_range_multi_aff(
12531 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12532 __isl_keep isl_multi_aff
*ma
)
12535 isl_space
*ma_space
;
12537 ma_space
= isl_multi_aff_get_space(ma
);
12539 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12543 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12544 "parameters don't match", goto error
);
12545 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12549 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12550 "spaces don't match", goto error
);
12552 isl_space_free(ma_space
);
12553 return isl_stat_ok
;
12555 isl_space_free(ma_space
);
12556 return isl_stat_error
;
12559 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12560 * coefficients before the transformed range of dimensions,
12561 * the "n_after" coefficients after the transformed range of dimensions
12562 * and the coefficients of the other divs in "bmap".
12564 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12565 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12570 isl_local_space
*ls
;
12575 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12577 return isl_basic_map_free(bmap
);
12579 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12580 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12581 for (i
= 0; i
< n_div
; ++i
) {
12582 int o_bmap
= 0, o_ls
= 0;
12584 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12585 o_bmap
+= 1 + 1 + n_param
;
12586 o_ls
+= 1 + 1 + n_param
;
12587 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12588 o_bmap
+= n_before
;
12589 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12590 ls
->div
->row
[i
] + o_ls
, n_set
);
12593 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12595 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12596 ls
->div
->row
[i
] + o_ls
, n_div
);
12599 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12600 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12605 isl_local_space_free(ls
);
12608 isl_local_space_free(ls
);
12609 return isl_basic_map_free(bmap
);
12612 /* How many stride constraints does "ma" enforce?
12613 * That is, how many of the affine expressions have a denominator
12614 * different from one?
12616 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12621 for (i
= 0; i
< ma
->n
; ++i
)
12622 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12628 /* For each affine expression in ma of the form
12630 * x_i = (f_i y + h_i)/m_i
12632 * with m_i different from one, add a constraint to "bmap"
12635 * f_i y + h_i = m_i alpha_i
12637 * with alpha_i an additional existentially quantified variable.
12639 * The input variables of "ma" correspond to a subset of the variables
12640 * of "bmap". There are "n_before" variables in "bmap" before this
12641 * subset and "n_after" variables after this subset.
12642 * The integer divisions of the affine expressions in "ma" are assumed
12643 * to have been aligned. There are "n_div_ma" of them and
12644 * they appear first in "bmap", straight after the "n_after" variables.
12646 static __isl_give isl_basic_map
*add_ma_strides(
12647 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12648 int n_before
, int n_after
, int n_div_ma
)
12656 total
= isl_basic_map_total_dim(bmap
);
12657 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12658 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12659 for (i
= 0; i
< ma
->n
; ++i
) {
12660 int o_bmap
= 0, o_ma
= 1;
12662 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12664 div
= isl_basic_map_alloc_div(bmap
);
12665 k
= isl_basic_map_alloc_equality(bmap
);
12666 if (div
< 0 || k
< 0)
12668 isl_int_set_si(bmap
->div
[div
][0], 0);
12669 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12670 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12671 o_bmap
+= 1 + n_param
;
12672 o_ma
+= 1 + n_param
;
12673 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12674 o_bmap
+= n_before
;
12675 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12676 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12679 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12681 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12682 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12683 o_bmap
+= n_div_ma
;
12685 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12686 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12692 isl_basic_map_free(bmap
);
12696 /* Replace the domain or range space (depending on "type) of "space" by "set".
12698 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12699 enum isl_dim_type type
, __isl_take isl_space
*set
)
12701 if (type
== isl_dim_in
) {
12702 space
= isl_space_range(space
);
12703 space
= isl_space_map_from_domain_and_range(set
, space
);
12705 space
= isl_space_domain(space
);
12706 space
= isl_space_map_from_domain_and_range(space
, set
);
12712 /* Compute the preimage of the domain or range (depending on "type")
12713 * of "bmap" under the function represented by "ma".
12714 * In other words, plug in "ma" in the domain or range of "bmap".
12715 * The result is a basic map that lives in the same space as "bmap"
12716 * except that the domain or range has been replaced by
12717 * the domain space of "ma".
12719 * If bmap is represented by
12721 * A(p) + S u + B x + T v + C(divs) >= 0,
12723 * where u and x are input and output dimensions if type == isl_dim_out
12724 * while x and v are input and output dimensions if type == isl_dim_in,
12725 * and ma is represented by
12727 * x = D(p) + F(y) + G(divs')
12729 * then the result is
12731 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12733 * The divs in the input set are similarly adjusted.
12736 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12740 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12741 * B_i G(divs') + c_i(divs))/n_i)
12743 * If bmap is not a rational map and if F(y) involves any denominators
12745 * x_i = (f_i y + h_i)/m_i
12747 * then additional constraints are added to ensure that we only
12748 * map back integer points. That is we enforce
12750 * f_i y + h_i = m_i alpha_i
12752 * with alpha_i an additional existentially quantified variable.
12754 * We first copy over the divs from "ma".
12755 * Then we add the modified constraints and divs from "bmap".
12756 * Finally, we add the stride constraints, if needed.
12758 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12759 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12760 __isl_take isl_multi_aff
*ma
)
12764 isl_basic_map
*res
= NULL
;
12765 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12766 isl_int f
, c1
, c2
, g
;
12775 ma
= isl_multi_aff_align_divs(ma
);
12778 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12781 if (type
== isl_dim_in
) {
12783 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12785 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12788 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12789 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12791 space
= isl_multi_aff_get_domain_space(ma
);
12792 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12793 rational
= isl_basic_map_is_rational(bmap
);
12794 strides
= rational
? 0 : multi_aff_strides(ma
);
12795 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12796 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12798 res
= isl_basic_map_set_rational(res
);
12800 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12801 if (isl_basic_map_alloc_div(res
) < 0)
12804 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
12808 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12809 k
= isl_basic_map_alloc_equality(res
);
12812 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12813 n_after
, n_div_ma
, n_div_bmap
,
12814 f
, c1
, c2
, g
, 0) < 0)
12818 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12819 k
= isl_basic_map_alloc_inequality(res
);
12822 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12823 n_after
, n_div_ma
, n_div_bmap
,
12824 f
, c1
, c2
, g
, 0) < 0)
12828 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12829 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12830 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12833 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12834 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12835 f
, c1
, c2
, g
, 1) < 0)
12840 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12846 isl_basic_map_free(bmap
);
12847 isl_multi_aff_free(ma
);
12848 res
= isl_basic_map_simplify(res
);
12849 return isl_basic_map_finalize(res
);
12855 isl_basic_map_free(bmap
);
12856 isl_multi_aff_free(ma
);
12857 isl_basic_map_free(res
);
12861 /* Compute the preimage of "bset" under the function represented by "ma".
12862 * In other words, plug in "ma" in "bset". The result is a basic set
12863 * that lives in the domain space of "ma".
12865 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12866 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12868 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12871 /* Compute the preimage of the domain of "bmap" under the function
12872 * represented by "ma".
12873 * In other words, plug in "ma" in the domain of "bmap".
12874 * The result is a basic map that lives in the same space as "bmap"
12875 * except that the domain has been replaced by the domain space of "ma".
12877 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12878 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12880 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12883 /* Compute the preimage of the range of "bmap" under the function
12884 * represented by "ma".
12885 * In other words, plug in "ma" in the range of "bmap".
12886 * The result is a basic map that lives in the same space as "bmap"
12887 * except that the range has been replaced by the domain space of "ma".
12889 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12890 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12892 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12895 /* Check if the range of "ma" is compatible with the domain or range
12896 * (depending on "type") of "map".
12897 * Return isl_stat_error if anything is wrong.
12899 static isl_stat
check_map_compatible_range_multi_aff(
12900 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12901 __isl_keep isl_multi_aff
*ma
)
12904 isl_space
*ma_space
;
12906 ma_space
= isl_multi_aff_get_space(ma
);
12907 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12908 isl_space_free(ma_space
);
12910 return isl_stat_error
;
12912 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12913 "spaces don't match", return isl_stat_error
);
12914 return isl_stat_ok
;
12917 /* Compute the preimage of the domain or range (depending on "type")
12918 * of "map" under the function represented by "ma".
12919 * In other words, plug in "ma" in the domain or range of "map".
12920 * The result is a map that lives in the same space as "map"
12921 * except that the domain or range has been replaced by
12922 * the domain space of "ma".
12924 * The parameters are assumed to have been aligned.
12926 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12927 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12932 map
= isl_map_cow(map
);
12933 ma
= isl_multi_aff_align_divs(ma
);
12936 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12939 for (i
= 0; i
< map
->n
; ++i
) {
12940 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12941 isl_multi_aff_copy(ma
));
12946 space
= isl_multi_aff_get_domain_space(ma
);
12947 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12949 isl_space_free(map
->dim
);
12954 isl_multi_aff_free(ma
);
12956 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12957 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12960 isl_multi_aff_free(ma
);
12965 /* Compute the preimage of the domain or range (depending on "type")
12966 * of "map" under the function represented by "ma".
12967 * In other words, plug in "ma" in the domain or range of "map".
12968 * The result is a map that lives in the same space as "map"
12969 * except that the domain or range has been replaced by
12970 * the domain space of "ma".
12972 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12973 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12980 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
12984 return map_preimage_multi_aff(map
, type
, ma
);
12986 if (isl_map_check_named_params(map
) < 0)
12988 if (!isl_space_has_named_params(ma
->space
))
12989 isl_die(map
->ctx
, isl_error_invalid
,
12990 "unaligned unnamed parameters", goto error
);
12991 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12992 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12994 return map_preimage_multi_aff(map
, type
, ma
);
12996 isl_multi_aff_free(ma
);
12997 return isl_map_free(map
);
13000 /* Compute the preimage of "set" under the function represented by "ma".
13001 * In other words, plug in "ma" in "set". The result is a set
13002 * that lives in the domain space of "ma".
13004 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13005 __isl_take isl_multi_aff
*ma
)
13007 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13010 /* Compute the preimage of the domain of "map" under the function
13011 * represented by "ma".
13012 * In other words, plug in "ma" in the domain of "map".
13013 * The result is a map that lives in the same space as "map"
13014 * except that the domain has been replaced by the domain space of "ma".
13016 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13017 __isl_take isl_multi_aff
*ma
)
13019 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13022 /* Compute the preimage of the range of "map" under the function
13023 * represented by "ma".
13024 * In other words, plug in "ma" in the range of "map".
13025 * The result is a map that lives in the same space as "map"
13026 * except that the range has been replaced by the domain space of "ma".
13028 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13029 __isl_take isl_multi_aff
*ma
)
13031 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13034 /* Compute the preimage of "map" under the function represented by "pma".
13035 * In other words, plug in "pma" in the domain or range of "map".
13036 * The result is a map that lives in the same space as "map",
13037 * except that the space of type "type" has been replaced by
13038 * the domain space of "pma".
13040 * The parameters of "map" and "pma" are assumed to have been aligned.
13042 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13043 __isl_take isl_map
*map
, enum isl_dim_type type
,
13044 __isl_take isl_pw_multi_aff
*pma
)
13053 isl_pw_multi_aff_free(pma
);
13054 res
= isl_map_empty(isl_map_get_space(map
));
13059 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13060 isl_multi_aff_copy(pma
->p
[0].maff
));
13061 if (type
== isl_dim_in
)
13062 res
= isl_map_intersect_domain(res
,
13063 isl_map_copy(pma
->p
[0].set
));
13065 res
= isl_map_intersect_range(res
,
13066 isl_map_copy(pma
->p
[0].set
));
13068 for (i
= 1; i
< pma
->n
; ++i
) {
13071 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13072 isl_multi_aff_copy(pma
->p
[i
].maff
));
13073 if (type
== isl_dim_in
)
13074 res_i
= isl_map_intersect_domain(res_i
,
13075 isl_map_copy(pma
->p
[i
].set
));
13077 res_i
= isl_map_intersect_range(res_i
,
13078 isl_map_copy(pma
->p
[i
].set
));
13079 res
= isl_map_union(res
, res_i
);
13082 isl_pw_multi_aff_free(pma
);
13086 isl_pw_multi_aff_free(pma
);
13091 /* Compute the preimage of "map" under the function represented by "pma".
13092 * In other words, plug in "pma" in the domain or range of "map".
13093 * The result is a map that lives in the same space as "map",
13094 * except that the space of type "type" has been replaced by
13095 * the domain space of "pma".
13097 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13098 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13105 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13109 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13111 if (isl_map_check_named_params(map
) < 0)
13113 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13115 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13116 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13118 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13120 isl_pw_multi_aff_free(pma
);
13121 return isl_map_free(map
);
13124 /* Compute the preimage of "set" under the function represented by "pma".
13125 * In other words, plug in "pma" in "set". The result is a set
13126 * that lives in the domain space of "pma".
13128 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13129 __isl_take isl_pw_multi_aff
*pma
)
13131 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13134 /* Compute the preimage of the domain of "map" under the function
13135 * represented by "pma".
13136 * In other words, plug in "pma" in the domain of "map".
13137 * The result is a map that lives in the same space as "map",
13138 * except that domain space has been replaced by the domain space of "pma".
13140 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13141 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13143 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13146 /* Compute the preimage of the range of "map" under the function
13147 * represented by "pma".
13148 * In other words, plug in "pma" in the range of "map".
13149 * The result is a map that lives in the same space as "map",
13150 * except that range space has been replaced by the domain space of "pma".
13152 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13153 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13155 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13158 /* Compute the preimage of "map" under the function represented by "mpa".
13159 * In other words, plug in "mpa" in the domain or range of "map".
13160 * The result is a map that lives in the same space as "map",
13161 * except that the space of type "type" has been replaced by
13162 * the domain space of "mpa".
13164 * If the map does not involve any constraints that refer to the
13165 * dimensions of the substituted space, then the only possible
13166 * effect of "mpa" on the map is to map the space to a different space.
13167 * We create a separate isl_multi_aff to effectuate this change
13168 * in order to avoid spurious splitting of the map along the pieces
13170 * If "mpa" has a non-trivial explicit domain, however,
13171 * then the full substitution should be performed.
13173 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13174 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13178 isl_pw_multi_aff
*pma
;
13183 n
= isl_map_dim(map
, type
);
13184 full
= isl_map_involves_dims(map
, type
, 0, n
);
13185 if (full
>= 0 && !full
)
13186 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13193 space
= isl_multi_pw_aff_get_space(mpa
);
13194 isl_multi_pw_aff_free(mpa
);
13195 ma
= isl_multi_aff_zero(space
);
13196 return isl_map_preimage_multi_aff(map
, type
, ma
);
13199 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13200 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13203 isl_multi_pw_aff_free(mpa
);
13207 /* Compute the preimage of "map" under the function represented by "mpa".
13208 * In other words, plug in "mpa" in the domain "map".
13209 * The result is a map that lives in the same space as "map",
13210 * except that domain space has been replaced by the domain space of "mpa".
13212 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13213 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13215 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13218 /* Compute the preimage of "set" by the function represented by "mpa".
13219 * In other words, plug in "mpa" in "set".
13221 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13222 __isl_take isl_multi_pw_aff
*mpa
)
13224 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13227 /* Return a copy of the equality constraints of "bset" as a matrix.
13229 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13230 __isl_keep isl_basic_set
*bset
)
13238 ctx
= isl_basic_set_get_ctx(bset
);
13239 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13240 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13243 /* Are the "n" "coefficients" starting at "first" of the integer division
13244 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13246 * The "coefficient" at position 0 is the denominator.
13247 * The "coefficient" at position 1 is the constant term.
13249 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13250 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13251 unsigned first
, unsigned n
)
13253 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13254 return isl_bool_error
;
13255 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13256 return isl_bool_error
;
13257 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13258 bmap2
->div
[pos2
] + first
, n
);
13261 /* Are the integer division expressions at position "pos1" in "bmap1" and
13262 * "pos2" in "bmap2" equal to each other, except that the constant terms
13265 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13266 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13267 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13272 if (!bmap1
|| !bmap2
)
13273 return isl_bool_error
;
13274 total
= isl_basic_map_total_dim(bmap1
);
13275 if (total
!= isl_basic_map_total_dim(bmap2
))
13276 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13277 "incomparable div expressions", return isl_bool_error
);
13278 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13280 if (equal
< 0 || !equal
)
13282 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13284 if (equal
< 0 || equal
)
13285 return isl_bool_not(equal
);
13286 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13290 /* Replace the numerator of the constant term of the integer division
13291 * expression at position "div" in "bmap" by "value".
13292 * The caller guarantees that this does not change the meaning
13295 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13296 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13298 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13299 return isl_basic_map_free(bmap
);
13301 isl_int_set_si(bmap
->div
[div
][1], value
);
13306 /* Is the point "inner" internal to inequality constraint "ineq"
13308 * The point is considered to be internal to the inequality constraint,
13309 * if it strictly lies on the positive side of the inequality constraint,
13310 * or if it lies on the constraint and the constraint is lexico-positive.
13312 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13313 __isl_keep isl_basic_set
*bset
, int ineq
)
13319 if (!inner
|| !bset
)
13320 return isl_bool_error
;
13322 ctx
= isl_basic_set_get_ctx(bset
);
13323 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13324 &ctx
->normalize_gcd
);
13325 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13326 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13328 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13329 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13330 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13333 /* Tighten the inequality constraints of "bset" that are outward with respect
13334 * to the point "vec".
13335 * That is, tighten the constraints that are not satisfied by "vec".
13337 * "vec" is a point internal to some superset S of "bset" that is used
13338 * to make the subsets of S disjoint, by tightening one half of the constraints
13339 * that separate two subsets. In particular, the constraints of S
13340 * are all satisfied by "vec" and should not be tightened.
13341 * Of the internal constraints, those that have "vec" on the outside
13342 * are tightened. The shared facet is included in the adjacent subset
13343 * with the opposite constraint.
13344 * For constraints that saturate "vec", this criterion cannot be used
13345 * to determine which of the two sides should be tightened.
13346 * Instead, the sign of the first non-zero coefficient is used
13347 * to make this choice. Note that this second criterion is never used
13348 * on the constraints of S since "vec" is interior to "S".
13350 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13351 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13355 bset
= isl_basic_set_cow(bset
);
13358 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13361 internal
= is_internal(vec
, bset
, j
);
13363 return isl_basic_set_free(bset
);
13366 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13372 /* Replace the variables x of type "type" starting at "first" in "bmap"
13373 * by x' with x = M x' with M the matrix trans.
13374 * That is, replace the corresponding coefficients c by c M.
13376 * The transformation matrix should be a square matrix.
13378 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13379 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13380 __isl_take isl_mat
*trans
)
13384 bmap
= isl_basic_map_cow(bmap
);
13385 if (!bmap
|| !trans
)
13388 if (trans
->n_row
!= trans
->n_col
)
13389 isl_die(trans
->ctx
, isl_error_invalid
,
13390 "expecting square transformation matrix", goto error
);
13391 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13394 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13396 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13397 isl_mat_copy(trans
)) < 0)
13399 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13400 isl_mat_copy(trans
)) < 0)
13402 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13403 isl_mat_copy(trans
)) < 0)
13406 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13407 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13409 isl_mat_free(trans
);
13412 isl_mat_free(trans
);
13413 isl_basic_map_free(bmap
);
13417 /* Replace the variables x of type "type" starting at "first" in "bset"
13418 * by x' with x = M x' with M the matrix trans.
13419 * That is, replace the corresponding coefficients c by c M.
13421 * The transformation matrix should be a square matrix.
13423 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13424 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13425 __isl_take isl_mat
*trans
)
13427 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);