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>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
58 __isl_keep
const isl_basic_set
*bset
)
60 return (const isl_basic_map
*) bset
;
63 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
66 case isl_dim_param
: return dim
->nparam
;
67 case isl_dim_in
: return dim
->n_in
;
68 case isl_dim_out
: return dim
->n_out
;
69 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
74 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
77 case isl_dim_param
: return 1;
78 case isl_dim_in
: return 1 + dim
->nparam
;
79 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
85 enum isl_dim_type type
)
90 case isl_dim_cst
: return 1;
93 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
94 case isl_dim_div
: return bmap
->n_div
;
95 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
100 /* Return the space of "map".
102 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
104 return map
? map
->dim
: NULL
;
107 /* Return the space of "set".
109 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
111 return isl_map_peek_space(set_to_map(set
));
114 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
116 return map
? n(map
->dim
, type
) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
121 return set
? n(set
->dim
, type
) : 0;
124 /* Return the position of the variables of the given type
125 * within the sequence of variables of "bmap".
127 int isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
128 enum isl_dim_type type
)
132 space
= isl_basic_map_peek_space(bmap
);
139 case isl_dim_out
: return isl_space_offset(space
, type
);
140 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
143 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
144 "invalid dimension type", return -1);
148 /* Return the position of the coefficients of the variables of the given type
149 * within the sequence of coefficients of "bmap".
151 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
152 enum isl_dim_type type
)
155 case isl_dim_cst
: return 0;
159 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
164 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
165 enum isl_dim_type type
)
167 return isl_basic_map_offset(bset
, type
);
170 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
172 return pos(map
->dim
, type
);
175 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
176 enum isl_dim_type type
)
178 return isl_basic_map_dim(bset
, type
);
181 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
183 return isl_basic_set_dim(bset
, isl_dim_set
);
186 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
188 return isl_basic_set_dim(bset
, isl_dim_param
);
191 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
193 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
196 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
198 return isl_set_dim(set
, isl_dim_set
);
201 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
203 return isl_set_dim(set
, isl_dim_param
);
206 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
208 return bmap
? bmap
->dim
->n_in
: 0;
211 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
213 return bmap
? bmap
->dim
->n_out
: 0;
216 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
218 return bmap
? bmap
->dim
->nparam
: 0;
221 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
223 return bmap
? bmap
->n_div
: 0;
226 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
228 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
231 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
233 return map
? map
->dim
->n_in
: 0;
236 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
238 return map
? map
->dim
->n_out
: 0;
241 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
243 return map
? map
->dim
->nparam
: 0;
246 /* Return the number of equality constraints in the description of "bmap".
247 * Return -1 on error.
249 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
256 /* Return the number of equality constraints in the description of "bset".
257 * Return -1 on error.
259 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
261 return isl_basic_map_n_equality(bset_to_bmap(bset
));
264 /* Return the number of inequality constraints in the description of "bmap".
265 * Return -1 on error.
267 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
274 /* Return the number of inequality constraints in the description of "bset".
275 * Return -1 on error.
277 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
279 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
282 /* Do "bmap1" and "bmap2" have the same parameters?
284 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
285 __isl_keep isl_basic_map
*bmap2
)
287 isl_space
*space1
, *space2
;
289 space1
= isl_basic_map_peek_space(bmap1
);
290 space2
= isl_basic_map_peek_space(bmap2
);
291 return isl_space_has_equal_params(space1
, space2
);
294 /* Do "map1" and "map2" have the same parameters?
296 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
297 __isl_keep isl_map
*map2
)
299 isl_space
*space1
, *space2
;
301 space1
= isl_map_peek_space(map1
);
302 space2
= isl_map_peek_space(map2
);
303 return isl_space_has_equal_params(space1
, space2
);
306 /* Do "map" and "set" have the same parameters?
308 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
309 __isl_keep isl_set
*set
)
311 return isl_map_has_equal_params(map
, set_to_map(set
));
314 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
315 __isl_keep isl_set
*set
)
319 return isl_bool_error
;
320 m
= isl_map_has_equal_params(map
, set_to_map(set
));
323 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
324 set
->dim
, isl_dim_set
);
327 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
328 __isl_keep isl_basic_set
*bset
)
332 return isl_bool_error
;
333 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
336 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
337 bset
->dim
, isl_dim_set
);
340 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
341 __isl_keep isl_set
*set
)
345 return isl_bool_error
;
346 m
= isl_map_has_equal_params(map
, set_to_map(set
));
349 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
350 set
->dim
, isl_dim_set
);
353 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
354 __isl_keep isl_basic_set
*bset
)
358 return isl_bool_error
;
359 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
362 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
363 bset
->dim
, isl_dim_set
);
366 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
368 return bmap
? bmap
->ctx
: NULL
;
371 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
373 return bset
? bset
->ctx
: NULL
;
376 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
378 return map
? map
->ctx
: NULL
;
381 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
383 return set
? set
->ctx
: NULL
;
386 /* Return the space of "bmap".
388 __isl_keep isl_space
*isl_basic_map_peek_space(
389 __isl_keep
const isl_basic_map
*bmap
)
391 return bmap
? bmap
->dim
: NULL
;
394 /* Return the space of "bset".
396 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
398 return isl_basic_map_peek_space(bset_to_bmap(bset
));
401 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
403 return isl_space_copy(isl_basic_map_peek_space(bmap
));
406 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
408 return isl_basic_map_get_space(bset_to_bmap(bset
));
411 /* Extract the divs in "bmap" as a matrix.
413 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
424 ctx
= isl_basic_map_get_ctx(bmap
);
425 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
426 cols
= 1 + 1 + total
+ bmap
->n_div
;
427 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
431 for (i
= 0; i
< bmap
->n_div
; ++i
)
432 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
437 /* Extract the divs in "bset" as a matrix.
439 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
441 return isl_basic_map_get_divs(bset
);
444 __isl_give isl_local_space
*isl_basic_map_get_local_space(
445 __isl_keep isl_basic_map
*bmap
)
452 div
= isl_basic_map_get_divs(bmap
);
453 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
456 __isl_give isl_local_space
*isl_basic_set_get_local_space(
457 __isl_keep isl_basic_set
*bset
)
459 return isl_basic_map_get_local_space(bset
);
462 /* For each known div d = floor(f/m), add the constraints
465 * -(f-(m-1)) + m d >= 0
467 * Do not finalize the result.
469 static __isl_give isl_basic_map
*add_known_div_constraints(
470 __isl_take isl_basic_map
*bmap
)
477 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
480 bmap
= isl_basic_map_cow(bmap
);
481 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
484 for (i
= 0; i
< n_div
; ++i
) {
485 if (isl_int_is_zero(bmap
->div
[i
][0]))
487 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
488 return isl_basic_map_free(bmap
);
494 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
495 __isl_take isl_local_space
*ls
)
504 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
505 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
506 n_div
, 0, 2 * n_div
);
508 for (i
= 0; i
< n_div
; ++i
)
509 if (isl_basic_map_alloc_div(bmap
) < 0)
512 for (i
= 0; i
< n_div
; ++i
)
513 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
514 bmap
= add_known_div_constraints(bmap
);
516 isl_local_space_free(ls
);
519 isl_local_space_free(ls
);
520 isl_basic_map_free(bmap
);
524 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
525 __isl_take isl_local_space
*ls
)
527 return isl_basic_map_from_local_space(ls
);
530 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
532 return isl_space_copy(isl_map_peek_space(map
));
535 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
539 return isl_space_copy(set
->dim
);
542 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
545 bmap
= isl_basic_map_cow(bmap
);
548 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
551 bmap
= isl_basic_map_finalize(bmap
);
554 isl_basic_map_free(bmap
);
558 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set
*bset
, const char *s
)
561 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
565 enum isl_dim_type type
)
567 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
570 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
571 enum isl_dim_type type
, const char *s
)
575 map
= isl_map_cow(map
);
579 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
583 for (i
= 0; i
< map
->n
; ++i
) {
584 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map
*bmap
,
599 enum isl_dim_type type
, __isl_take isl_id
*id
)
601 bmap
= isl_basic_map_cow(bmap
);
604 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
606 return isl_basic_map_free(bmap
);
607 bmap
= isl_basic_map_finalize(bmap
);
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
619 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
622 /* Does the input or output tuple have a name?
624 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
626 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
630 enum isl_dim_type type
)
632 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
635 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
642 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
643 enum isl_dim_type type
, __isl_take isl_id
*id
)
645 map
= isl_map_cow(map
);
649 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
651 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
657 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
658 __isl_take isl_id
*id
)
660 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
663 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
664 enum isl_dim_type type
)
666 map
= isl_map_cow(map
);
670 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
672 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
675 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
677 return isl_map_reset_tuple_id(set
, isl_dim_set
);
680 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
682 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
685 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
686 enum isl_dim_type type
)
688 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
691 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
693 return isl_map_has_tuple_id(set
, isl_dim_set
);
696 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
698 return isl_map_get_tuple_id(set
, isl_dim_set
);
701 /* Does the set tuple have a name?
703 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
706 return isl_bool_error
;
707 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
713 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
718 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
722 enum isl_dim_type type
, unsigned pos
)
724 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
728 enum isl_dim_type type
, unsigned pos
)
730 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
733 /* Does the given dimension have a name?
735 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
736 enum isl_dim_type type
, unsigned pos
)
739 return isl_bool_error
;
740 return isl_space_has_dim_name(map
->dim
, type
, pos
);
743 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
744 enum isl_dim_type type
, unsigned pos
)
746 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
749 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
750 enum isl_dim_type type
, unsigned pos
)
752 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
755 /* Does the given dimension have a name?
757 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
758 enum isl_dim_type type
, unsigned pos
)
761 return isl_bool_error
;
762 return isl_space_has_dim_name(set
->dim
, type
, pos
);
765 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map
*bmap
,
767 enum isl_dim_type type
, unsigned pos
, const char *s
)
769 bmap
= isl_basic_map_cow(bmap
);
772 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
775 return isl_basic_map_finalize(bmap
);
777 isl_basic_map_free(bmap
);
781 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
782 enum isl_dim_type type
, unsigned pos
, const char *s
)
786 map
= isl_map_cow(map
);
790 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
794 for (i
= 0; i
< map
->n
; ++i
) {
795 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
806 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set
*bset
,
808 enum isl_dim_type type
, unsigned pos
, const char *s
)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
814 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
815 enum isl_dim_type type
, unsigned pos
, const char *s
)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
821 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
822 enum isl_dim_type type
, unsigned pos
)
825 return isl_bool_error
;
826 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
829 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
830 enum isl_dim_type type
, unsigned pos
)
832 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
835 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
836 enum isl_dim_type type
, unsigned pos
)
838 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
841 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
842 enum isl_dim_type type
, unsigned pos
)
844 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
847 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
848 enum isl_dim_type type
, unsigned pos
)
850 return isl_map_has_dim_id(set
, type
, pos
);
853 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
854 enum isl_dim_type type
, unsigned pos
)
856 return isl_map_get_dim_id(set
, type
, pos
);
859 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
860 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
862 map
= isl_map_cow(map
);
866 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
868 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
874 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
875 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
877 return isl_map_set_dim_id(set
, type
, pos
, id
);
880 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
881 __isl_keep isl_id
*id
)
885 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
888 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
889 __isl_keep isl_id
*id
)
891 return isl_map_find_dim_by_id(set
, type
, id
);
894 /* Return the position of the dimension of the given type and name
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
899 enum isl_dim_type type
, const char *name
)
903 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
906 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
911 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
914 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
917 return isl_map_find_dim_by_name(set
, type
, name
);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
925 * with k a constant and e an existentially quantified variable.
927 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
936 return isl_bool_error
;
938 if (!isl_int_is_zero(bset
->eq
[i
][0]))
939 return isl_bool_false
;
941 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
942 d
= isl_basic_set_dim(bset
, isl_dim_set
);
943 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
945 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
946 return isl_bool_false
;
947 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
949 return isl_bool_false
;
950 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
952 return isl_bool_false
;
954 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
956 return isl_bool_false
;
957 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
958 n_div
- pos2
- 1) != -1)
959 return isl_bool_false
;
960 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
961 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
962 return isl_bool_false
;
964 return isl_bool_true
;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
974 space
= isl_map_get_space(map
);
975 space
= isl_space_reset_user(space
);
976 map
= isl_map_reset_space(map
, space
);
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
986 return isl_map_reset_user(set
);
989 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
992 return isl_bool_error
;
993 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1002 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1008 return isl_bool_error
;
1010 return isl_bool_false
;
1011 rational
= isl_basic_map_is_rational(map
->p
[0]);
1014 for (i
= 1; i
< map
->n
; ++i
) {
1015 isl_bool rational_i
;
1017 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1020 if (rational
!= rational_i
)
1021 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error
);
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1035 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1037 return isl_map_is_rational(set
);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1042 return isl_basic_map_is_rational(bset
);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1053 isl_bool has_rational
= isl_bool_true
;
1057 return isl_bool_error
;
1058 if (isl_basic_map_plain_is_empty(bmap
))
1059 return isl_bool_false
;
1060 if (!isl_basic_map_is_rational(bmap
))
1061 return isl_bool_false
;
1062 bmap
= isl_basic_map_copy(bmap
);
1063 bmap
= isl_basic_map_implicit_equalities(bmap
);
1065 return isl_bool_error
;
1066 total
= isl_basic_map_total_dim(bmap
);
1067 if (bmap
->n_eq
== total
) {
1069 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1070 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1073 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1074 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1076 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1081 if (i
== bmap
->n_eq
)
1082 has_rational
= isl_bool_false
;
1084 isl_basic_map_free(bmap
);
1086 return has_rational
;
1089 /* Does "map" contain any rational points?
1091 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1094 isl_bool has_rational
;
1097 return isl_bool_error
;
1098 for (i
= 0; i
< map
->n
; ++i
) {
1099 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1100 if (has_rational
< 0 || has_rational
)
1101 return has_rational
;
1103 return isl_bool_false
;
1106 /* Does "set" contain any rational points?
1108 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1110 return isl_map_has_rational(set
);
1113 /* Is this basic set a parameter domain?
1115 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1118 return isl_bool_error
;
1119 return isl_space_is_params(bset
->dim
);
1122 /* Is this set a parameter domain?
1124 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1127 return isl_bool_error
;
1128 return isl_space_is_params(set
->dim
);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1138 return isl_bool_error
;
1139 return isl_space_is_params(map
->dim
);
1142 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1143 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1144 unsigned n_eq
, unsigned n_ineq
)
1147 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1152 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1153 if (isl_blk_is_error(bmap
->block
))
1156 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1157 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1161 bmap
->block2
= isl_blk_empty();
1164 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1165 if (isl_blk_is_error(bmap
->block2
))
1168 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1173 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1174 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1176 for (i
= 0; i
< extra
; ++i
)
1177 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1181 bmap
->c_size
= n_eq
+ n_ineq
;
1182 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1183 bmap
->extra
= extra
;
1187 bmap
->sample
= NULL
;
1191 isl_basic_map_free(bmap
);
1195 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1196 unsigned nparam
, unsigned dim
, unsigned extra
,
1197 unsigned n_eq
, unsigned n_ineq
)
1199 struct isl_basic_map
*bmap
;
1202 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1206 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1207 return bset_from_bmap(bmap
);
1210 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1211 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1213 struct isl_basic_map
*bmap
;
1216 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1217 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1218 return bset_from_bmap(bmap
);
1220 isl_space_free(dim
);
1224 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1225 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1227 struct isl_basic_map
*bmap
;
1231 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1236 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1238 isl_space_free(space
);
1242 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1243 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1244 unsigned n_eq
, unsigned n_ineq
)
1246 struct isl_basic_map
*bmap
;
1249 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1253 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1257 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1258 __isl_keep isl_basic_map
*src
)
1261 unsigned total
= isl_basic_map_total_dim(src
);
1266 for (i
= 0; i
< src
->n_eq
; ++i
) {
1267 int j
= isl_basic_map_alloc_equality(dst
);
1269 return isl_basic_map_free(dst
);
1270 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1273 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1274 int j
= isl_basic_map_alloc_inequality(dst
);
1276 return isl_basic_map_free(dst
);
1277 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1280 for (i
= 0; i
< src
->n_div
; ++i
) {
1281 int j
= isl_basic_map_alloc_div(dst
);
1283 return isl_basic_map_free(dst
);
1284 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1286 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1290 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1292 struct isl_basic_map
*dup
;
1296 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1297 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1298 dup
= dup_constraints(dup
, bmap
);
1301 dup
->flags
= bmap
->flags
;
1302 dup
->sample
= isl_vec_copy(bmap
->sample
);
1306 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1308 struct isl_basic_map
*dup
;
1310 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1311 return bset_from_bmap(dup
);
1314 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1319 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1323 return isl_basic_set_dup(bset
);
1326 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1335 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1340 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1344 bmap
= isl_basic_map_dup(bmap
);
1346 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1350 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1359 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1364 if (--bmap
->ref
> 0)
1367 isl_ctx_deref(bmap
->ctx
);
1369 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1371 isl_blk_free(bmap
->ctx
, bmap
->block
);
1372 isl_vec_free(bmap
->sample
);
1373 isl_space_free(bmap
->dim
);
1379 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1381 return isl_basic_map_free(bset_to_bmap(bset
));
1384 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1386 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1389 /* Check that "bset" does not involve any parameters.
1391 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1394 return isl_stat_error
;
1395 if (isl_basic_set_dim(bset
, isl_dim_param
) != 0)
1396 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1397 "basic set should not have any parameters",
1398 return isl_stat_error
);
1402 /* Check that "bset" does not involve any local variables.
1404 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1407 return isl_stat_error
;
1408 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
1409 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1410 "basic set should not have any local variables",
1411 return isl_stat_error
);
1415 /* Check that "map" has only named parameters, reporting an error
1418 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1420 return isl_space_check_named_params(isl_map_peek_space(map
));
1423 /* Check that "bmap" has only named parameters, reporting an error
1426 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1428 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1431 /* Check that "bmap1" and "bmap2" have the same parameters,
1432 * reporting an error if they do not.
1434 static isl_stat
isl_basic_map_check_equal_params(
1435 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1439 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1441 return isl_stat_error
;
1443 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1444 "parameters don't match", return isl_stat_error
);
1448 __isl_give isl_map
*isl_map_align_params_map_map_and(
1449 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1450 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1451 __isl_take isl_map
*map2
))
1455 if (isl_map_has_equal_params(map1
, map2
))
1456 return fn(map1
, map2
);
1457 if (isl_map_check_named_params(map1
) < 0)
1459 if (isl_map_check_named_params(map2
) < 0)
1461 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1462 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1463 return fn(map1
, map2
);
1470 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1471 __isl_keep isl_map
*map2
,
1472 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1477 return isl_bool_error
;
1478 if (isl_map_has_equal_params(map1
, map2
))
1479 return fn(map1
, map2
);
1480 if (isl_map_check_named_params(map1
) < 0)
1481 return isl_bool_error
;
1482 if (isl_map_check_named_params(map2
) < 0)
1483 return isl_bool_error
;
1484 map1
= isl_map_copy(map1
);
1485 map2
= isl_map_copy(map2
);
1486 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1487 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1494 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1496 struct isl_ctx
*ctx
;
1500 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1501 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1504 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1505 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1506 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1507 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1509 int j
= isl_basic_map_alloc_inequality(bmap
);
1513 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1514 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1521 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1522 bmap
->extra
- bmap
->n_div
);
1523 return bmap
->n_eq
++;
1526 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1528 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1531 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1535 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1540 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1542 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1545 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1550 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1552 if (pos
!= bmap
->n_eq
- 1) {
1554 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1555 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1561 /* Turn inequality "pos" of "bmap" into an equality.
1563 * In particular, we move the inequality in front of the equalities
1564 * and move the last inequality in the position of the moved inequality.
1565 * Note that isl_tab_make_equalities_explicit depends on this particular
1566 * change in the ordering of the constraints.
1568 void isl_basic_map_inequality_to_equality(
1569 struct isl_basic_map
*bmap
, unsigned pos
)
1573 t
= bmap
->ineq
[pos
];
1574 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1575 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1580 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1581 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1582 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1583 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1586 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1588 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1591 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1593 struct isl_ctx
*ctx
;
1597 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1598 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1599 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1600 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1601 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1602 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1603 1 + isl_basic_map_total_dim(bmap
),
1604 bmap
->extra
- bmap
->n_div
);
1605 return bmap
->n_ineq
++;
1608 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1610 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1613 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1617 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1622 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1624 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1627 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1632 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1634 if (pos
!= bmap
->n_ineq
- 1) {
1635 t
= bmap
->ineq
[pos
];
1636 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1637 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1638 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1644 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1646 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1649 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1655 empty
= isl_basic_map_plain_is_empty(bmap
);
1657 return isl_basic_map_free(bmap
);
1661 bmap
= isl_basic_map_cow(bmap
);
1662 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1665 k
= isl_basic_map_alloc_equality(bmap
);
1668 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1671 isl_basic_map_free(bmap
);
1675 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1678 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1681 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1686 bmap
= isl_basic_map_cow(bmap
);
1687 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1690 k
= isl_basic_map_alloc_inequality(bmap
);
1693 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1696 isl_basic_map_free(bmap
);
1700 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1703 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1706 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1710 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1711 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1712 1 + 1 + isl_basic_map_total_dim(bmap
),
1713 bmap
->extra
- bmap
->n_div
);
1714 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1715 return bmap
->n_div
++;
1718 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1720 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1724 #define TYPE isl_basic_map
1725 #include "check_type_range_templ.c"
1727 /* Check that there are "n" dimensions of type "type" starting at "first"
1730 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1731 enum isl_dim_type type
, unsigned first
, unsigned n
)
1733 return isl_basic_map_check_range(bset_to_bmap(bset
),
1737 /* Insert an extra integer division, prescribed by "div", to "bmap"
1738 * at (integer division) position "pos".
1740 * The integer division is first added at the end and then moved
1741 * into the right position.
1743 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1744 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1748 bmap
= isl_basic_map_cow(bmap
);
1750 return isl_basic_map_free(bmap
);
1752 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1753 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1754 "unexpected size", return isl_basic_map_free(bmap
));
1755 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1756 return isl_basic_map_free(bmap
);
1758 bmap
= isl_basic_map_extend_space(bmap
,
1759 isl_basic_map_get_space(bmap
), 1, 0, 2);
1760 k
= isl_basic_map_alloc_div(bmap
);
1762 return isl_basic_map_free(bmap
);
1763 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1764 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1766 for (i
= k
; i
> pos
; --i
)
1767 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1772 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1775 return isl_stat_error
;
1776 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1781 static __isl_give isl_basic_map
*add_constraints(
1782 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1783 unsigned i_pos
, unsigned o_pos
)
1785 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1788 struct isl_dim_map
*dim_map
;
1790 space
= isl_basic_map_peek_space(bmap2
);
1791 if (!bmap1
|| !space
)
1794 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1795 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1796 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1797 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1798 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1799 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1800 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1801 ctx
= isl_basic_map_get_ctx(bmap1
);
1802 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1803 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1804 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1805 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1806 isl_dim_map_div(dim_map
, bmap2
, total
);
1808 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1810 isl_basic_map_free(bmap1
);
1811 isl_basic_map_free(bmap2
);
1815 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1816 struct isl_basic_set
*bset2
, unsigned pos
)
1818 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1819 bset_to_bmap(bset2
), 0, pos
));
1822 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1823 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1824 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1826 struct isl_basic_map
*ext
;
1836 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1837 base
->extra
>= base
->n_div
+ extra
;
1839 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1840 room_for_ineq(base
, n_ineq
)) {
1841 isl_space_free(space
);
1845 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1846 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1847 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1848 extra
+= base
->extra
;
1850 n_ineq
+= base
->n_ineq
;
1852 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1858 ext
->sample
= isl_vec_copy(base
->sample
);
1859 flags
= base
->flags
;
1860 ext
= add_constraints(ext
, base
, 0, 0);
1863 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1869 isl_space_free(space
);
1870 isl_basic_map_free(base
);
1874 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1875 __isl_take isl_basic_set
*base
,
1876 __isl_take isl_space
*dim
, unsigned extra
,
1877 unsigned n_eq
, unsigned n_ineq
)
1879 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1880 dim
, extra
, n_eq
, n_ineq
));
1883 struct isl_basic_map
*isl_basic_map_extend_constraints(
1884 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1888 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1892 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1893 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1894 unsigned n_eq
, unsigned n_ineq
)
1896 struct isl_basic_map
*bmap
;
1901 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1905 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1908 isl_basic_map_free(base
);
1912 struct isl_basic_set
*isl_basic_set_extend_constraints(
1913 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1915 isl_basic_map
*bmap
= bset_to_bmap(base
);
1916 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1917 return bset_from_bmap(bmap
);
1920 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1922 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1925 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1930 if (bmap
->ref
> 1) {
1932 bmap
= isl_basic_map_dup(bmap
);
1935 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1936 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1941 /* Clear all cached information in "map", either because it is about
1942 * to be modified or because it is being freed.
1943 * Always return the same pointer that is passed in.
1944 * This is needed for the use in isl_map_free.
1946 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1948 isl_basic_map_free(map
->cached_simple_hull
[0]);
1949 isl_basic_map_free(map
->cached_simple_hull
[1]);
1950 map
->cached_simple_hull
[0] = NULL
;
1951 map
->cached_simple_hull
[1] = NULL
;
1955 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1957 return isl_map_cow(set
);
1960 /* Return an isl_map that is equal to "map" and that has only
1961 * a single reference.
1963 * If the original input already has only one reference, then
1964 * simply return it, but clear all cached information, since
1965 * it may be rendered invalid by the operations that will be
1966 * performed on the result.
1968 * Otherwise, create a duplicate (without any cached information).
1970 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1976 return clear_caches(map
);
1978 return isl_map_dup(map
);
1981 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1982 unsigned a_len
, unsigned b_len
)
1984 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1985 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1986 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1989 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1990 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1995 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
1998 if (n1
== 0 || n2
== 0)
2001 bmap
= isl_basic_map_cow(bmap
);
2005 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2006 if (isl_blk_is_error(blk
))
2009 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2011 bmap
->eq
[i
] + pos
, n1
, n2
);
2013 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2015 bmap
->ineq
[i
] + pos
, n1
, n2
);
2017 for (i
= 0; i
< bmap
->n_div
; ++i
)
2019 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2021 isl_blk_free(bmap
->ctx
, blk
);
2023 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2024 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2025 return isl_basic_map_finalize(bmap
);
2027 isl_basic_map_free(bmap
);
2031 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map
*bmap
)
2038 total
= isl_basic_map_total_dim(bmap
);
2039 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2040 return isl_basic_map_free(bmap
);
2041 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2043 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2045 i
= isl_basic_map_alloc_equality(bmap
);
2049 isl_int_set_si(bmap
->eq
[i
][0], 1);
2050 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2051 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2052 isl_vec_free(bmap
->sample
);
2053 bmap
->sample
= NULL
;
2054 return isl_basic_map_finalize(bmap
);
2056 isl_basic_map_free(bmap
);
2060 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set
*bset
)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2066 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2067 __isl_take isl_basic_map
*bmap
)
2072 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2075 bmap
= isl_basic_map_cow(bmap
);
2079 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2081 return isl_basic_map_finalize(bmap
);
2084 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2085 __isl_take isl_basic_set
*bset
)
2087 return isl_basic_map_set_rational(bset
);
2090 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2091 __isl_take isl_basic_set
*bset
)
2096 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2099 bset
= isl_basic_set_cow(bset
);
2103 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2105 return isl_basic_set_finalize(bset
);
2108 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2112 map
= isl_map_cow(map
);
2115 for (i
= 0; i
< map
->n
; ++i
) {
2116 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2126 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2128 return isl_map_set_rational(set
);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2134 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2136 isl_int
*t
= bmap
->div
[a
];
2137 bmap
->div
[a
] = bmap
->div
[b
];
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2153 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2154 swap_div(bmap
, a
, b
);
2156 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2157 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2159 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2160 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2162 for (i
= 0; i
< bmap
->n_div
; ++i
)
2163 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2164 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2169 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2171 isl_seq_cpy(c
, c
+ n
, rem
);
2172 isl_seq_clr(c
+ rem
, n
);
2175 /* Drop n dimensions starting at first.
2177 * In principle, this frees up some extra variables as the number
2178 * of columns remains constant, but we would have to extend
2179 * the div array too as the number of rows in this array is assumed
2180 * to be equal to extra.
2182 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2183 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2185 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2188 /* Move "n" divs starting at "first" to the end of the list of divs.
2190 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2191 unsigned first
, unsigned n
)
2196 if (first
+ n
== bmap
->n_div
)
2199 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2202 for (i
= 0; i
< n
; ++i
)
2203 div
[i
] = bmap
->div
[first
+ i
];
2204 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2205 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2206 for (i
= 0; i
< n
; ++i
)
2207 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2211 isl_basic_map_free(bmap
);
2216 #define TYPE isl_map
2218 #include "check_type_range_templ.c"
2220 /* Check that there are "n" dimensions of type "type" starting at "first"
2223 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2224 enum isl_dim_type type
, unsigned first
, unsigned n
)
2226 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2229 /* Drop "n" dimensions of type "type" starting at "first".
2230 * Perform the core computation, without cowing or
2231 * simplifying and finalizing the result.
2233 * In principle, this frees up some extra variables as the number
2234 * of columns remains constant, but we would have to extend
2235 * the div array too as the number of rows in this array is assumed
2236 * to be equal to extra.
2238 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2239 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2240 unsigned first
, unsigned n
)
2246 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2247 return isl_basic_map_free(bmap
);
2249 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2250 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2251 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2252 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2254 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2255 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2257 for (i
= 0; i
< bmap
->n_div
; ++i
)
2258 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2260 if (type
== isl_dim_div
) {
2261 bmap
= move_divs_last(bmap
, first
, n
);
2264 if (isl_basic_map_free_div(bmap
, n
) < 0)
2265 return isl_basic_map_free(bmap
);
2267 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2269 return isl_basic_map_free(bmap
);
2271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2276 /* Drop "n" dimensions of type "type" starting at "first".
2278 * In principle, this frees up some extra variables as the number
2279 * of columns remains constant, but we would have to extend
2280 * the div array too as the number of rows in this array is assumed
2281 * to be equal to extra.
2283 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2284 enum isl_dim_type type
, unsigned first
, unsigned n
)
2288 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2291 bmap
= isl_basic_map_cow(bmap
);
2295 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2297 bmap
= isl_basic_map_simplify(bmap
);
2298 return isl_basic_map_finalize(bmap
);
2301 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2302 enum isl_dim_type type
, unsigned first
, unsigned n
)
2304 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2308 /* No longer consider "map" to be normalized.
2310 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2314 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2318 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2319 enum isl_dim_type type
, unsigned first
, unsigned n
)
2323 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2324 return isl_map_free(map
);
2326 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2328 map
= isl_map_cow(map
);
2331 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2335 for (i
= 0; i
< map
->n
; ++i
) {
2336 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2340 map
= isl_map_unmark_normalized(map
);
2348 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2349 enum isl_dim_type type
, unsigned first
, unsigned n
)
2351 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2354 /* Drop the integer division at position "div", which is assumed
2355 * not to appear in any of the constraints or
2356 * in any of the other integer divisions.
2358 * Since the integer division is redundant, there is no need to cow.
2360 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2361 __isl_take isl_basic_map
*bmap
, unsigned div
)
2363 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2366 /* Eliminate the specified n dimensions starting at first from the
2367 * constraints, without removing the dimensions from the space.
2368 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2370 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2371 enum isl_dim_type type
, unsigned first
, unsigned n
)
2378 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2379 return isl_map_free(map
);
2381 map
= isl_map_cow(map
);
2385 for (i
= 0; i
< map
->n
; ++i
) {
2386 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2396 /* Eliminate the specified n dimensions starting at first from the
2397 * constraints, without removing the dimensions from the space.
2398 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2400 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2401 enum isl_dim_type type
, unsigned first
, unsigned n
)
2403 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2406 /* Eliminate the specified n dimensions starting at first from the
2407 * constraints, without removing the dimensions from the space.
2408 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2410 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2411 unsigned first
, unsigned n
)
2413 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2416 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2417 __isl_take isl_basic_map
*bmap
)
2421 bmap
= isl_basic_map_eliminate_vars(bmap
,
2422 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2426 return isl_basic_map_finalize(bmap
);
2429 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2430 __isl_take isl_basic_set
*bset
)
2432 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2435 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2444 map
= isl_map_cow(map
);
2448 for (i
= 0; i
< map
->n
; ++i
) {
2449 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2459 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2461 return isl_map_remove_divs(set
);
2464 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2465 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2466 unsigned first
, unsigned n
)
2468 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2469 return isl_basic_map_free(bmap
);
2470 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2472 bmap
= isl_basic_map_eliminate_vars(bmap
,
2473 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2476 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2478 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2482 /* Return true if the definition of the given div (recursively) involves
2483 * any of the given variables.
2485 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2486 unsigned first
, unsigned n
)
2489 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2491 if (isl_int_is_zero(bmap
->div
[div
][0]))
2492 return isl_bool_false
;
2493 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2494 return isl_bool_true
;
2496 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2499 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2501 involves
= div_involves_vars(bmap
, i
, first
, n
);
2502 if (involves
< 0 || involves
)
2506 return isl_bool_false
;
2509 /* Try and add a lower and/or upper bound on "div" to "bmap"
2510 * based on inequality "i".
2511 * "total" is the total number of variables (excluding the divs).
2512 * "v" is a temporary object that can be used during the calculations.
2513 * If "lb" is set, then a lower bound should be constructed.
2514 * If "ub" is set, then an upper bound should be constructed.
2516 * The calling function has already checked that the inequality does not
2517 * reference "div", but we still need to check that the inequality is
2518 * of the right form. We'll consider the case where we want to construct
2519 * a lower bound. The construction of upper bounds is similar.
2521 * Let "div" be of the form
2523 * q = floor((a + f(x))/d)
2525 * We essentially check if constraint "i" is of the form
2529 * so that we can use it to derive a lower bound on "div".
2530 * However, we allow a slightly more general form
2534 * with the condition that the coefficients of g(x) - f(x) are all
2536 * Rewriting this constraint as
2540 * adding a + f(x) to both sides and dividing by d, we obtain
2542 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2544 * Taking the floor on both sides, we obtain
2546 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2550 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2552 * In the case of an upper bound, we construct the constraint
2554 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2557 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2558 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2559 unsigned total
, isl_int v
, int lb
, int ub
)
2563 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2565 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2566 bmap
->div
[div
][1 + 1 + j
]);
2567 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2570 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2571 bmap
->div
[div
][1 + 1 + j
]);
2572 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2578 bmap
= isl_basic_map_cow(bmap
);
2579 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2581 int k
= isl_basic_map_alloc_inequality(bmap
);
2584 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2585 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2586 bmap
->div
[div
][1 + j
]);
2587 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2588 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2590 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2593 int k
= isl_basic_map_alloc_inequality(bmap
);
2596 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2597 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2598 bmap
->div
[div
][1 + j
]);
2599 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2600 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2602 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2607 isl_basic_map_free(bmap
);
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2656 static __isl_give isl_basic_map
*insert_bounds_on_div(
2657 __isl_take isl_basic_map
*bmap
, int div
)
2660 int check_lb
, check_ub
;
2667 if (isl_int_is_zero(bmap
->div
[div
][0]))
2670 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2674 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2675 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2682 if (!check_lb
&& !check_ub
)
2687 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2688 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2691 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2692 check_lb
, check_ub
);
2700 /* Remove all divs (recursively) involving any of the given dimensions
2701 * in their definitions.
2703 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2704 __isl_take isl_basic_map
*bmap
,
2705 enum isl_dim_type type
, unsigned first
, unsigned n
)
2709 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2710 return isl_basic_map_free(bmap
);
2711 first
+= isl_basic_map_offset(bmap
, type
);
2713 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2716 involves
= div_involves_vars(bmap
, i
, first
, n
);
2718 return isl_basic_map_free(bmap
);
2721 bmap
= insert_bounds_on_div(bmap
, i
);
2722 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2731 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2732 __isl_take isl_basic_set
*bset
,
2733 enum isl_dim_type type
, unsigned first
, unsigned n
)
2735 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2738 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2739 enum isl_dim_type type
, unsigned first
, unsigned n
)
2748 map
= isl_map_cow(map
);
2752 for (i
= 0; i
< map
->n
; ++i
) {
2753 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2764 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2765 enum isl_dim_type type
, unsigned first
, unsigned n
)
2767 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2771 /* Does the description of "bmap" depend on the specified dimensions?
2772 * We also check whether the dimensions appear in any of the div definitions.
2773 * In principle there is no need for this check. If the dimensions appear
2774 * in a div definition, they also appear in the defining constraints of that
2777 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2778 enum isl_dim_type type
, unsigned first
, unsigned n
)
2782 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2783 return isl_bool_error
;
2785 first
+= isl_basic_map_offset(bmap
, type
);
2786 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2787 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2788 return isl_bool_true
;
2789 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2790 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2791 return isl_bool_true
;
2792 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2793 if (isl_int_is_zero(bmap
->div
[i
][0]))
2795 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2796 return isl_bool_true
;
2799 return isl_bool_false
;
2802 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2803 enum isl_dim_type type
, unsigned first
, unsigned n
)
2807 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2808 return isl_bool_error
;
2810 for (i
= 0; i
< map
->n
; ++i
) {
2811 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2813 if (involves
< 0 || involves
)
2817 return isl_bool_false
;
2820 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2821 enum isl_dim_type type
, unsigned first
, unsigned n
)
2823 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2826 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2827 enum isl_dim_type type
, unsigned first
, unsigned n
)
2829 return isl_map_involves_dims(set
, type
, first
, n
);
2832 /* Drop all constraints in bmap that involve any of the dimensions
2833 * first to first+n-1.
2834 * This function only performs the actual removal of constraints.
2836 * This function should not call finalize since it is used by
2837 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2839 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2840 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2847 bmap
= isl_basic_map_cow(bmap
);
2852 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2853 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2855 isl_basic_map_drop_equality(bmap
, i
);
2858 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2859 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2861 isl_basic_map_drop_inequality(bmap
, i
);
2867 /* Drop all constraints in bset that involve any of the dimensions
2868 * first to first+n-1.
2869 * This function only performs the actual removal of constraints.
2871 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2872 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2874 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2877 /* Drop all constraints in bmap that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_map
*bmap
,
2882 enum isl_dim_type type
, unsigned first
, unsigned n
)
2887 isl_space
*space
= isl_basic_map_get_space(bmap
);
2888 isl_basic_map_free(bmap
);
2889 return isl_basic_map_universe(space
);
2891 bmap
= isl_basic_map_cow(bmap
);
2895 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2896 return isl_basic_map_free(bmap
);
2898 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2900 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2901 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2903 isl_basic_map_drop_equality(bmap
, i
);
2906 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2907 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2909 isl_basic_map_drop_inequality(bmap
, i
);
2912 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2916 /* Drop all constraints in bset that do not involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2919 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2920 __isl_take isl_basic_set
*bset
,
2921 enum isl_dim_type type
, unsigned first
, unsigned n
)
2923 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2927 /* Drop all constraints in bmap that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2931 __isl_take isl_basic_map
*bmap
,
2932 enum isl_dim_type type
, unsigned first
, unsigned n
)
2939 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2940 return isl_basic_map_free(bmap
);
2942 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2943 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2944 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2945 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2949 /* Drop all constraints in bset that involve any of the dimensions
2950 * first to first + n - 1 of the given type.
2952 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2953 __isl_take isl_basic_set
*bset
,
2954 enum isl_dim_type type
, unsigned first
, unsigned n
)
2956 return isl_basic_map_drop_constraints_involving_dims(bset
,
2960 /* Drop constraints from "map" by applying "drop" to each basic map.
2962 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2963 enum isl_dim_type type
, unsigned first
, unsigned n
,
2964 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2965 enum isl_dim_type type
, unsigned first
, unsigned n
))
2969 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2970 return isl_map_free(map
);
2972 map
= isl_map_cow(map
);
2976 for (i
= 0; i
< map
->n
; ++i
) {
2977 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2979 return isl_map_free(map
);
2983 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2988 /* Drop all constraints in map that involve any of the dimensions
2989 * first to first + n - 1 of the given type.
2991 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2992 __isl_take isl_map
*map
,
2993 enum isl_dim_type type
, unsigned first
, unsigned n
)
2997 return drop_constraints(map
, type
, first
, n
,
2998 &isl_basic_map_drop_constraints_involving_dims
);
3001 /* Drop all constraints in "map" that do not involve any of the dimensions
3002 * first to first + n - 1 of the given type.
3004 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3005 __isl_take isl_map
*map
,
3006 enum isl_dim_type type
, unsigned first
, unsigned n
)
3009 isl_space
*space
= isl_map_get_space(map
);
3011 return isl_map_universe(space
);
3013 return drop_constraints(map
, type
, first
, n
,
3014 &isl_basic_map_drop_constraints_not_involving_dims
);
3017 /* Drop all constraints in set that involve any of the dimensions
3018 * first to first + n - 1 of the given type.
3020 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3021 __isl_take isl_set
*set
,
3022 enum isl_dim_type type
, unsigned first
, unsigned n
)
3024 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3027 /* Drop all constraints in "set" that do not involve any of the dimensions
3028 * first to first + n - 1 of the given type.
3030 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3031 __isl_take isl_set
*set
,
3032 enum isl_dim_type type
, unsigned first
, unsigned n
)
3034 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3037 /* Does local variable "div" of "bmap" have a complete explicit representation?
3038 * Having a complete explicit representation requires not only
3039 * an explicit representation, but also that all local variables
3040 * that appear in this explicit representation in turn have
3041 * a complete explicit representation.
3043 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3046 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3049 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3050 if (marked
< 0 || marked
)
3051 return isl_bool_not(marked
);
3053 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3056 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3058 known
= isl_basic_map_div_is_known(bmap
, i
);
3059 if (known
< 0 || !known
)
3063 return isl_bool_true
;
3066 /* Remove all divs that are unknown or defined in terms of unknown divs.
3068 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3069 __isl_take isl_basic_map
*bmap
)
3076 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3077 if (isl_basic_map_div_is_known(bmap
, i
))
3079 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3088 /* Remove all divs that are unknown or defined in terms of unknown divs.
3090 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3091 __isl_take isl_basic_set
*bset
)
3093 return isl_basic_map_remove_unknown_divs(bset
);
3096 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3105 map
= isl_map_cow(map
);
3109 for (i
= 0; i
< map
->n
; ++i
) {
3110 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3120 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3122 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3125 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3126 __isl_take isl_basic_set
*bset
,
3127 enum isl_dim_type type
, unsigned first
, unsigned n
)
3129 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3130 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3131 return bset_from_bmap(bmap
);
3134 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3135 enum isl_dim_type type
, unsigned first
, unsigned n
)
3142 map
= isl_map_cow(map
);
3143 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3144 return isl_map_free(map
);
3146 for (i
= 0; i
< map
->n
; ++i
) {
3147 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3148 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3152 map
= isl_map_drop(map
, type
, first
, n
);
3159 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3160 enum isl_dim_type type
, unsigned first
, unsigned n
)
3162 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3166 /* Project out n inputs starting at first using Fourier-Motzkin */
3167 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3168 unsigned first
, unsigned n
)
3170 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3173 static void dump_term(struct isl_basic_map
*bmap
,
3174 isl_int c
, int pos
, FILE *out
)
3177 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3178 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3179 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3181 isl_int_print(out
, c
, 0);
3183 if (!isl_int_is_one(c
))
3184 isl_int_print(out
, c
, 0);
3185 if (pos
< 1 + nparam
) {
3186 name
= isl_space_get_dim_name(bmap
->dim
,
3187 isl_dim_param
, pos
- 1);
3189 fprintf(out
, "%s", name
);
3191 fprintf(out
, "p%d", pos
- 1);
3192 } else if (pos
< 1 + nparam
+ in
)
3193 fprintf(out
, "i%d", pos
- 1 - nparam
);
3194 else if (pos
< 1 + nparam
+ dim
)
3195 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3197 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3201 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3202 int sign
, FILE *out
)
3206 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3210 for (i
= 0, first
= 1; i
< len
; ++i
) {
3211 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3214 fprintf(out
, " + ");
3216 isl_int_abs(v
, c
[i
]);
3217 dump_term(bmap
, v
, i
, out
);
3224 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3225 const char *op
, FILE *out
, int indent
)
3229 fprintf(out
, "%*s", indent
, "");
3231 dump_constraint_sign(bmap
, c
, 1, out
);
3232 fprintf(out
, " %s ", op
);
3233 dump_constraint_sign(bmap
, c
, -1, out
);
3237 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3238 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3240 fprintf(out
, "%*s", indent
, "");
3241 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3246 static void dump_constraints(struct isl_basic_map
*bmap
,
3247 isl_int
**c
, unsigned n
,
3248 const char *op
, FILE *out
, int indent
)
3252 for (i
= 0; i
< n
; ++i
)
3253 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3256 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3260 unsigned total
= isl_basic_map_total_dim(bmap
);
3262 for (j
= 0; j
< 1 + total
; ++j
) {
3263 if (isl_int_is_zero(exp
[j
]))
3265 if (!first
&& isl_int_is_pos(exp
[j
]))
3267 dump_term(bmap
, exp
[j
], j
, out
);
3272 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3276 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3277 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3279 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3280 fprintf(out
, "%*s", indent
, "");
3281 fprintf(out
, "e%d = [(", i
);
3282 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3284 isl_int_print(out
, bmap
->div
[i
][0], 0);
3285 fprintf(out
, "]\n");
3289 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3290 FILE *out
, int indent
)
3293 fprintf(out
, "null basic set\n");
3297 fprintf(out
, "%*s", indent
, "");
3298 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3299 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3300 bset
->extra
, bset
->flags
);
3301 dump(bset_to_bmap(bset
), out
, indent
);
3304 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3305 FILE *out
, int indent
)
3308 fprintf(out
, "null basic map\n");
3312 fprintf(out
, "%*s", indent
, "");
3313 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3314 "flags: %x, n_name: %d\n",
3316 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3317 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3318 dump(bmap
, out
, indent
);
3321 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3326 total
= isl_basic_map_total_dim(bmap
);
3327 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3328 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3329 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3330 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3331 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3335 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3338 if (isl_space_check_is_set(space
) < 0)
3340 return isl_map_alloc_space(space
, n
, flags
);
3342 isl_space_free(space
);
3346 /* Make sure "map" has room for at least "n" more basic maps.
3348 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3351 struct isl_map
*grown
= NULL
;
3355 isl_assert(map
->ctx
, n
>= 0, goto error
);
3356 if (map
->n
+ n
<= map
->size
)
3358 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3361 for (i
= 0; i
< map
->n
; ++i
) {
3362 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3370 isl_map_free(grown
);
3375 /* Make sure "set" has room for at least "n" more basic sets.
3377 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3379 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3382 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3384 return isl_map_from_basic_map(bset
);
3387 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3389 struct isl_map
*map
;
3394 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3395 return isl_map_add_basic_map(map
, bmap
);
3398 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3399 __isl_take isl_basic_set
*bset
)
3401 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3402 bset_to_bmap(bset
)));
3405 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3407 return isl_map_free(set
);
3410 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3415 fprintf(out
, "null set\n");
3419 fprintf(out
, "%*s", indent
, "");
3420 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3421 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3423 for (i
= 0; i
< set
->n
; ++i
) {
3424 fprintf(out
, "%*s", indent
, "");
3425 fprintf(out
, "basic set %d:\n", i
);
3426 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3430 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3435 fprintf(out
, "null map\n");
3439 fprintf(out
, "%*s", indent
, "");
3440 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3441 "flags: %x, n_name: %d\n",
3442 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3443 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3444 for (i
= 0; i
< map
->n
; ++i
) {
3445 fprintf(out
, "%*s", indent
, "");
3446 fprintf(out
, "basic map %d:\n", i
);
3447 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3451 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3452 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3454 struct isl_basic_map
*bmap_domain
;
3456 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3459 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3460 isl_assert(bset
->ctx
,
3461 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3463 bmap
= isl_basic_map_cow(bmap
);
3466 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3467 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3468 bmap_domain
= isl_basic_map_from_domain(bset
);
3469 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3471 bmap
= isl_basic_map_simplify(bmap
);
3472 return isl_basic_map_finalize(bmap
);
3474 isl_basic_map_free(bmap
);
3475 isl_basic_set_free(bset
);
3479 /* Check that the space of "bset" is the same as that of the range of "bmap".
3481 static isl_stat
isl_basic_map_check_compatible_range(
3482 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3486 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3488 return isl_stat_error
;
3490 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3491 "incompatible spaces", return isl_stat_error
);
3496 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3497 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3499 struct isl_basic_map
*bmap_range
;
3501 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3504 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3505 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3508 if (isl_basic_set_plain_is_universe(bset
)) {
3509 isl_basic_set_free(bset
);
3513 bmap
= isl_basic_map_cow(bmap
);
3516 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3517 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3518 bmap_range
= bset_to_bmap(bset
);
3519 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3521 bmap
= isl_basic_map_simplify(bmap
);
3522 return isl_basic_map_finalize(bmap
);
3524 isl_basic_map_free(bmap
);
3525 isl_basic_set_free(bset
);
3529 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3530 __isl_keep isl_vec
*vec
)
3537 return isl_bool_error
;
3539 total
= 1 + isl_basic_map_total_dim(bmap
);
3540 if (total
!= vec
->size
)
3541 return isl_bool_false
;
3545 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3546 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3547 if (!isl_int_is_zero(s
)) {
3549 return isl_bool_false
;
3553 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3554 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3555 if (isl_int_is_neg(s
)) {
3557 return isl_bool_false
;
3563 return isl_bool_true
;
3566 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3567 __isl_keep isl_vec
*vec
)
3569 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3572 __isl_give isl_basic_map
*isl_basic_map_intersect(
3573 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3575 struct isl_vec
*sample
= NULL
;
3577 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3579 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3580 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3581 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3582 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3583 return isl_basic_map_intersect(bmap2
, bmap1
);
3585 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3586 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3587 isl_assert(bmap1
->ctx
,
3588 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3590 if (isl_basic_map_plain_is_empty(bmap1
)) {
3591 isl_basic_map_free(bmap2
);
3594 if (isl_basic_map_plain_is_empty(bmap2
)) {
3595 isl_basic_map_free(bmap1
);
3599 if (bmap1
->sample
&&
3600 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3601 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3602 sample
= isl_vec_copy(bmap1
->sample
);
3603 else if (bmap2
->sample
&&
3604 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3605 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3606 sample
= isl_vec_copy(bmap2
->sample
);
3608 bmap1
= isl_basic_map_cow(bmap1
);
3611 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3612 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3613 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3616 isl_vec_free(sample
);
3618 isl_vec_free(bmap1
->sample
);
3619 bmap1
->sample
= sample
;
3622 bmap1
= isl_basic_map_simplify(bmap1
);
3623 return isl_basic_map_finalize(bmap1
);
3626 isl_vec_free(sample
);
3627 isl_basic_map_free(bmap1
);
3628 isl_basic_map_free(bmap2
);
3632 struct isl_basic_set
*isl_basic_set_intersect(
3633 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3635 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3636 bset_to_bmap(bset2
)));
3639 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3640 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3642 return isl_basic_set_intersect(bset1
, bset2
);
3645 /* Special case of isl_map_intersect, where both map1 and map2
3646 * are convex, without any divs and such that either map1 or map2
3647 * contains a single constraint. This constraint is then simply
3648 * added to the other map.
3650 static __isl_give isl_map
*map_intersect_add_constraint(
3651 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3653 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3654 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3655 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3656 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3658 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3659 return isl_map_intersect(map2
, map1
);
3661 map1
= isl_map_cow(map1
);
3664 if (isl_map_plain_is_empty(map1
)) {
3668 if (map2
->p
[0]->n_eq
== 1)
3669 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3671 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3672 map2
->p
[0]->ineq
[0]);
3674 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3675 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3679 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3680 isl_basic_map_free(map1
->p
[0]);
3686 map1
= isl_map_unmark_normalized(map1
);
3694 /* map2 may be either a parameter domain or a map living in the same
3697 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3698 __isl_take isl_map
*map2
)
3708 if ((isl_map_plain_is_empty(map1
) ||
3709 isl_map_plain_is_universe(map2
)) &&
3710 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3714 if ((isl_map_plain_is_empty(map2
) ||
3715 isl_map_plain_is_universe(map1
)) &&
3716 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3721 if (map1
->n
== 1 && map2
->n
== 1 &&
3722 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3723 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3724 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3725 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3726 return map_intersect_add_constraint(map1
, map2
);
3728 equal
= isl_map_plain_is_equal(map1
, map2
);
3736 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3737 isl_space_dim(map2
->dim
, isl_dim_param
))
3738 isl_assert(map1
->ctx
,
3739 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3741 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3742 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3743 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3745 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3746 map1
->n
* map2
->n
, flags
);
3749 for (i
= 0; i
< map1
->n
; ++i
)
3750 for (j
= 0; j
< map2
->n
; ++j
) {
3751 struct isl_basic_map
*part
;
3752 part
= isl_basic_map_intersect(
3753 isl_basic_map_copy(map1
->p
[i
]),
3754 isl_basic_map_copy(map2
->p
[j
]));
3755 if (isl_basic_map_is_empty(part
) < 0)
3756 part
= isl_basic_map_free(part
);
3757 result
= isl_map_add_basic_map(result
, part
);
3770 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3771 __isl_take isl_map
*map2
)
3775 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3776 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3777 "spaces don't match", goto error
);
3778 return map_intersect_internal(map1
, map2
);
3785 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3786 __isl_take isl_map
*map2
)
3788 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3791 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3793 return set_from_map(isl_map_intersect(set_to_map(set1
),
3797 /* map_intersect_internal accepts intersections
3798 * with parameter domains, so we can just call that function.
3800 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3801 __isl_take isl_set
*params
)
3803 return map_intersect_internal(map
, params
);
3806 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3807 __isl_take isl_map
*map2
)
3809 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3812 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3813 __isl_take isl_set
*params
)
3815 return isl_map_intersect_params(set
, params
);
3818 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3821 unsigned pos
, n1
, n2
;
3825 bmap
= isl_basic_map_cow(bmap
);
3828 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3829 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3830 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3831 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3832 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3833 return isl_basic_map_reset_space(bmap
, space
);
3836 static __isl_give isl_basic_map
*basic_map_space_reset(
3837 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3843 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3846 space
= isl_basic_map_get_space(bmap
);
3847 space
= isl_space_reset(space
, type
);
3848 bmap
= isl_basic_map_reset_space(bmap
, space
);
3852 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3853 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3854 unsigned pos
, unsigned n
)
3856 isl_bool rational
, is_empty
;
3857 isl_space
*res_space
;
3858 struct isl_basic_map
*res
;
3859 struct isl_dim_map
*dim_map
;
3860 unsigned total
, off
;
3861 enum isl_dim_type t
;
3864 return basic_map_space_reset(bmap
, type
);
3866 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3868 return isl_basic_map_free(bmap
);
3869 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3872 return isl_basic_map_free(bmap
);
3874 isl_basic_map_free(bmap
);
3875 return isl_basic_map_empty(res_space
);
3878 total
= isl_basic_map_total_dim(bmap
) + n
;
3879 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3881 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3883 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3885 unsigned size
= isl_basic_map_dim(bmap
, t
);
3886 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3888 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3889 pos
, size
- pos
, off
+ pos
+ n
);
3891 off
+= isl_space_dim(res_space
, t
);
3893 isl_dim_map_div(dim_map
, bmap
, off
);
3895 res
= isl_basic_map_alloc_space(res_space
,
3896 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3897 rational
= isl_basic_map_is_rational(bmap
);
3899 res
= isl_basic_map_free(res
);
3901 res
= isl_basic_map_set_rational(res
);
3902 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3903 return isl_basic_map_finalize(res
);
3906 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3907 __isl_take isl_basic_set
*bset
,
3908 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3910 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3913 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3914 enum isl_dim_type type
, unsigned n
)
3918 return isl_basic_map_insert_dims(bmap
, type
,
3919 isl_basic_map_dim(bmap
, type
), n
);
3922 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3923 enum isl_dim_type type
, unsigned n
)
3927 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3928 return isl_basic_map_add_dims(bset
, type
, n
);
3930 isl_basic_set_free(bset
);
3934 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3935 enum isl_dim_type type
)
3939 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3942 space
= isl_map_get_space(map
);
3943 space
= isl_space_reset(space
, type
);
3944 map
= isl_map_reset_space(map
, space
);
3948 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3949 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3954 return map_space_reset(map
, type
);
3956 map
= isl_map_cow(map
);
3960 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3964 for (i
= 0; i
< map
->n
; ++i
) {
3965 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3976 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3977 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3979 return isl_map_insert_dims(set
, type
, pos
, n
);
3982 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3983 enum isl_dim_type type
, unsigned n
)
3987 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3990 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3991 enum isl_dim_type type
, unsigned n
)
3995 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3996 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4002 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4003 __isl_take isl_basic_map
*bmap
,
4004 enum isl_dim_type dst_type
, unsigned dst_pos
,
4005 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4007 struct isl_dim_map
*dim_map
;
4008 struct isl_basic_map
*res
;
4009 enum isl_dim_type t
;
4010 unsigned total
, off
;
4015 bmap
= isl_basic_map_reset(bmap
, src_type
);
4016 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4020 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4021 return isl_basic_map_free(bmap
);
4023 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4026 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4028 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4029 pos(bmap
->dim
, src_type
) + src_pos
+
4030 ((src_type
< dst_type
) ? n
: 0)) {
4031 bmap
= isl_basic_map_cow(bmap
);
4035 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4036 src_type
, src_pos
, n
);
4040 bmap
= isl_basic_map_finalize(bmap
);
4045 total
= isl_basic_map_total_dim(bmap
);
4046 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4049 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4050 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4051 if (t
== dst_type
) {
4052 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4055 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4058 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4059 dst_pos
, size
- dst_pos
, off
);
4060 off
+= size
- dst_pos
;
4061 } else if (t
== src_type
) {
4062 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4065 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4066 src_pos
+ n
, size
- src_pos
- n
, off
);
4067 off
+= size
- src_pos
- n
;
4069 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4073 isl_dim_map_div(dim_map
, bmap
, off
);
4075 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4076 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4077 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4081 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4082 src_type
, src_pos
, n
);
4086 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4087 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4088 bmap
= isl_basic_map_finalize(bmap
);
4092 isl_basic_map_free(bmap
);
4096 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4097 enum isl_dim_type dst_type
, unsigned dst_pos
,
4098 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4100 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4101 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4102 src_type
, src_pos
, n
);
4103 return bset_from_bmap(bmap
);
4106 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4107 enum isl_dim_type dst_type
, unsigned dst_pos
,
4108 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4112 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4113 return set_from_map(isl_map_move_dims(set_to_map(set
),
4114 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4120 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4121 enum isl_dim_type dst_type
, unsigned dst_pos
,
4122 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4127 map
= isl_map_reset(map
, src_type
);
4128 map
= isl_map_reset(map
, dst_type
);
4132 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4133 return isl_map_free(map
);
4135 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4138 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4140 map
= isl_map_cow(map
);
4144 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4148 for (i
= 0; i
< map
->n
; ++i
) {
4149 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4151 src_type
, src_pos
, n
);
4162 /* Move the specified dimensions to the last columns right before
4163 * the divs. Don't change the dimension specification of bmap.
4164 * That's the responsibility of the caller.
4166 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4167 enum isl_dim_type type
, unsigned first
, unsigned n
)
4169 struct isl_dim_map
*dim_map
;
4170 struct isl_basic_map
*res
;
4171 enum isl_dim_type t
;
4172 unsigned total
, off
;
4176 if (pos(bmap
->dim
, type
) + first
+ n
==
4177 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4180 total
= isl_basic_map_total_dim(bmap
);
4181 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4184 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4185 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4187 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4190 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4191 first
, n
, total
- bmap
->n_div
- n
);
4192 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4193 first
+ n
, size
- (first
+ n
), off
);
4194 off
+= size
- (first
+ n
);
4196 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4200 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4202 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4203 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4204 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4208 /* Insert "n" rows in the divs of "bmap".
4210 * The number of columns is not changed, which means that the last
4211 * dimensions of "bmap" are being reintepreted as the new divs.
4212 * The space of "bmap" is not adjusted, however, which means
4213 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4214 * from the space of "bmap" is the responsibility of the caller.
4216 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4224 bmap
= isl_basic_map_cow(bmap
);
4228 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4229 old
= bmap
->block2
.data
;
4230 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4231 (bmap
->extra
+ n
) * (1 + row_size
));
4232 if (!bmap
->block2
.data
)
4233 return isl_basic_map_free(bmap
);
4234 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4236 return isl_basic_map_free(bmap
);
4237 for (i
= 0; i
< n
; ++i
) {
4238 new_div
[i
] = bmap
->block2
.data
+
4239 (bmap
->extra
+ i
) * (1 + row_size
);
4240 isl_seq_clr(new_div
[i
], 1 + row_size
);
4242 for (i
= 0; i
< bmap
->extra
; ++i
)
4243 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4245 bmap
->div
= new_div
;
4252 /* Drop constraints from "bmap" that only involve the variables
4253 * of "type" in the range [first, first + n] that are not related
4254 * to any of the variables outside that interval.
4255 * These constraints cannot influence the values for the variables
4256 * outside the interval, except in case they cause "bmap" to be empty.
4257 * Only drop the constraints if "bmap" is known to be non-empty.
4259 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4260 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4261 unsigned first
, unsigned n
)
4265 unsigned dim
, n_div
;
4268 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4270 return isl_basic_map_free(bmap
);
4274 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4275 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4276 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4278 return isl_basic_map_free(bmap
);
4279 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4280 for (i
= 0; i
< first
; ++i
)
4282 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4285 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4290 /* Turn the n dimensions of type type, starting at first
4291 * into existentially quantified variables.
4293 * If a subset of the projected out variables are unrelated
4294 * to any of the variables that remain, then the constraints
4295 * involving this subset are simply dropped first.
4297 __isl_give isl_basic_map
*isl_basic_map_project_out(
4298 __isl_take isl_basic_map
*bmap
,
4299 enum isl_dim_type type
, unsigned first
, unsigned n
)
4304 return basic_map_space_reset(bmap
, type
);
4305 if (type
== isl_dim_div
)
4306 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4307 "cannot project out existentially quantified variables",
4308 return isl_basic_map_free(bmap
));
4310 empty
= isl_basic_map_plain_is_empty(bmap
);
4312 return isl_basic_map_free(bmap
);
4314 bmap
= isl_basic_map_set_to_empty(bmap
);
4316 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4320 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4321 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4323 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4324 return isl_basic_map_free(bmap
);
4326 bmap
= move_last(bmap
, type
, first
, n
);
4327 bmap
= isl_basic_map_cow(bmap
);
4328 bmap
= insert_div_rows(bmap
, n
);
4332 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4335 bmap
= isl_basic_map_simplify(bmap
);
4336 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4337 return isl_basic_map_finalize(bmap
);
4339 isl_basic_map_free(bmap
);
4343 /* Turn the n dimensions of type type, starting at first
4344 * into existentially quantified variables.
4346 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4347 enum isl_dim_type type
, unsigned first
, unsigned n
)
4349 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4353 /* Turn the n dimensions of type type, starting at first
4354 * into existentially quantified variables.
4356 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4357 enum isl_dim_type type
, unsigned first
, unsigned n
)
4362 return map_space_reset(map
, type
);
4364 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4365 return isl_map_free(map
);
4367 map
= isl_map_cow(map
);
4371 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4375 for (i
= 0; i
< map
->n
; ++i
) {
4376 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4387 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4388 * into existentially quantified variables.
4390 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4391 enum isl_dim_type type
, unsigned first
, unsigned n
)
4395 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4396 return isl_map_free(map
);
4397 dim
= isl_map_dim(map
, type
);
4398 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4399 map
= isl_map_project_out(map
, type
, 0, first
);
4403 /* Turn the n dimensions of type type, starting at first
4404 * into existentially quantified variables.
4406 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4407 enum isl_dim_type type
, unsigned first
, unsigned n
)
4409 return set_from_map(isl_map_project_out(set_to_map(set
),
4413 /* Return a map that projects the elements in "set" onto their
4414 * "n" set dimensions starting at "first".
4415 * "type" should be equal to isl_dim_set.
4417 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4418 enum isl_dim_type type
, unsigned first
, unsigned n
)
4423 if (type
!= isl_dim_set
)
4424 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4425 "only set dimensions can be projected out", goto error
);
4426 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4427 return isl_set_free(set
);
4429 map
= isl_map_from_domain(set
);
4430 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4431 for (i
= 0; i
< n
; ++i
)
4432 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4440 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4445 for (i
= 0; i
< n
; ++i
) {
4446 j
= isl_basic_map_alloc_div(bmap
);
4449 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4453 isl_basic_map_free(bmap
);
4457 struct isl_basic_map
*isl_basic_map_apply_range(
4458 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4460 isl_space
*space_result
= NULL
;
4461 struct isl_basic_map
*bmap
;
4462 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4463 struct isl_dim_map
*dim_map1
, *dim_map2
;
4465 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4467 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4468 bmap2
->dim
, isl_dim_in
))
4469 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4470 "spaces don't match", goto error
);
4472 space_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4473 isl_space_copy(bmap2
->dim
));
4475 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4476 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4477 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4478 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4480 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4481 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4482 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4483 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4484 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4485 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4486 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4487 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4488 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4489 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4490 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4492 bmap
= isl_basic_map_alloc_space(space_result
,
4493 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4494 bmap1
->n_eq
+ bmap2
->n_eq
,
4495 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4496 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4497 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4498 bmap
= add_divs(bmap
, n
);
4499 bmap
= isl_basic_map_simplify(bmap
);
4500 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4501 return isl_basic_map_finalize(bmap
);
4503 isl_basic_map_free(bmap1
);
4504 isl_basic_map_free(bmap2
);
4508 struct isl_basic_set
*isl_basic_set_apply(
4509 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4514 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4517 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4520 isl_basic_set_free(bset
);
4521 isl_basic_map_free(bmap
);
4525 struct isl_basic_map
*isl_basic_map_apply_domain(
4526 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4528 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4530 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4531 bmap2
->dim
, isl_dim_in
))
4532 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4533 "spaces don't match", goto error
);
4535 bmap1
= isl_basic_map_reverse(bmap1
);
4536 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4537 return isl_basic_map_reverse(bmap1
);
4539 isl_basic_map_free(bmap1
);
4540 isl_basic_map_free(bmap2
);
4544 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4545 * A \cap B -> f(A) + f(B)
4547 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4548 __isl_take isl_basic_map
*bmap2
)
4550 unsigned n_in
, n_out
, nparam
, total
, pos
;
4551 struct isl_basic_map
*bmap
= NULL
;
4552 struct isl_dim_map
*dim_map1
, *dim_map2
;
4555 if (!bmap1
|| !bmap2
)
4558 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4561 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4562 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4563 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4565 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4566 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4567 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4568 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4569 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4570 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4571 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4572 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4573 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4574 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4575 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4577 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4578 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4579 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4580 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4581 for (i
= 0; i
< n_out
; ++i
) {
4582 int j
= isl_basic_map_alloc_equality(bmap
);
4585 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4586 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4587 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4588 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4590 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4591 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4592 bmap
= add_divs(bmap
, 2 * n_out
);
4594 bmap
= isl_basic_map_simplify(bmap
);
4595 return isl_basic_map_finalize(bmap
);
4597 isl_basic_map_free(bmap
);
4598 isl_basic_map_free(bmap1
);
4599 isl_basic_map_free(bmap2
);
4603 /* Given two maps A -> f(A) and B -> g(B), construct a map
4604 * A \cap B -> f(A) + f(B)
4606 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4607 __isl_take isl_map
*map2
)
4609 struct isl_map
*result
;
4615 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4617 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4618 map1
->n
* map2
->n
, 0);
4621 for (i
= 0; i
< map1
->n
; ++i
)
4622 for (j
= 0; j
< map2
->n
; ++j
) {
4623 struct isl_basic_map
*part
;
4624 part
= isl_basic_map_sum(
4625 isl_basic_map_copy(map1
->p
[i
]),
4626 isl_basic_map_copy(map2
->p
[j
]));
4627 if (isl_basic_map_is_empty(part
))
4628 isl_basic_map_free(part
);
4630 result
= isl_map_add_basic_map(result
, part
);
4643 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4644 __isl_take isl_set
*set2
)
4646 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4649 /* Given a basic map A -> f(A), construct A -> -f(A).
4651 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4656 bmap
= isl_basic_map_cow(bmap
);
4660 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4661 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4662 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4663 for (j
= 0; j
< n
; ++j
)
4664 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4665 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4666 for (j
= 0; j
< n
; ++j
)
4667 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4668 for (i
= 0; i
< bmap
->n_div
; ++i
)
4669 for (j
= 0; j
< n
; ++j
)
4670 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4671 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4672 return isl_basic_map_finalize(bmap
);
4675 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4677 return isl_basic_map_neg(bset
);
4680 /* Given a map A -> f(A), construct A -> -f(A).
4682 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4686 map
= isl_map_cow(map
);
4690 for (i
= 0; i
< map
->n
; ++i
) {
4691 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4702 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4704 return set_from_map(isl_map_neg(set_to_map(set
)));
4707 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4708 * A -> floor(f(A)/d).
4710 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4713 unsigned n_in
, n_out
, nparam
, total
, pos
;
4714 struct isl_basic_map
*result
= NULL
;
4715 struct isl_dim_map
*dim_map
;
4721 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4722 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4723 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4725 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4726 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4727 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4728 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4729 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4730 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4732 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4733 bmap
->n_div
+ n_out
,
4734 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4735 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4736 result
= add_divs(result
, n_out
);
4737 for (i
= 0; i
< n_out
; ++i
) {
4739 j
= isl_basic_map_alloc_inequality(result
);
4742 isl_seq_clr(result
->ineq
[j
], 1+total
);
4743 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4744 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4745 j
= isl_basic_map_alloc_inequality(result
);
4748 isl_seq_clr(result
->ineq
[j
], 1+total
);
4749 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4750 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4751 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4754 result
= isl_basic_map_simplify(result
);
4755 return isl_basic_map_finalize(result
);
4757 isl_basic_map_free(result
);
4761 /* Given a map A -> f(A) and an integer d, construct a map
4762 * A -> floor(f(A)/d).
4764 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4768 map
= isl_map_cow(map
);
4772 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4773 for (i
= 0; i
< map
->n
; ++i
) {
4774 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4778 map
= isl_map_unmark_normalized(map
);
4786 /* Given a map A -> f(A) and an integer d, construct a map
4787 * A -> floor(f(A)/d).
4789 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4790 __isl_take isl_val
*d
)
4794 if (!isl_val_is_int(d
))
4795 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4796 "expecting integer denominator", goto error
);
4797 map
= isl_map_floordiv(map
, d
->n
);
4806 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4813 i
= isl_basic_map_alloc_equality(bmap
);
4816 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4817 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4818 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4819 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4820 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4821 return isl_basic_map_finalize(bmap
);
4823 isl_basic_map_free(bmap
);
4827 /* Add a constraint to "bmap" expressing i_pos < o_pos
4829 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4836 i
= isl_basic_map_alloc_inequality(bmap
);
4839 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4840 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4841 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4842 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4843 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4844 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4845 return isl_basic_map_finalize(bmap
);
4847 isl_basic_map_free(bmap
);
4851 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4853 static __isl_give isl_basic_map
*var_less_or_equal(
4854 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4860 i
= isl_basic_map_alloc_inequality(bmap
);
4863 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4864 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4865 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4866 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4867 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4868 return isl_basic_map_finalize(bmap
);
4870 isl_basic_map_free(bmap
);
4874 /* Add a constraint to "bmap" expressing i_pos > o_pos
4876 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4883 i
= isl_basic_map_alloc_inequality(bmap
);
4886 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4887 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4888 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4889 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4890 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4891 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4892 return isl_basic_map_finalize(bmap
);
4894 isl_basic_map_free(bmap
);
4898 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4900 static __isl_give isl_basic_map
*var_more_or_equal(
4901 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4907 i
= isl_basic_map_alloc_inequality(bmap
);
4910 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4911 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4912 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4913 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4914 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4915 return isl_basic_map_finalize(bmap
);
4917 isl_basic_map_free(bmap
);
4921 __isl_give isl_basic_map
*isl_basic_map_equal(
4922 __isl_take isl_space
*space
, unsigned n_equal
)
4925 struct isl_basic_map
*bmap
;
4926 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4929 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4930 bmap
= var_equal(bmap
, i
);
4931 return isl_basic_map_finalize(bmap
);
4934 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4936 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4940 struct isl_basic_map
*bmap
;
4941 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4944 for (i
= 0; i
< pos
&& bmap
; ++i
)
4945 bmap
= var_equal(bmap
, i
);
4947 bmap
= var_less(bmap
, pos
);
4948 return isl_basic_map_finalize(bmap
);
4951 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4953 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4954 __isl_take isl_space
*space
, unsigned pos
)
4957 isl_basic_map
*bmap
;
4959 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4960 for (i
= 0; i
< pos
; ++i
)
4961 bmap
= var_equal(bmap
, i
);
4962 bmap
= var_less_or_equal(bmap
, pos
);
4963 return isl_basic_map_finalize(bmap
);
4966 /* Return a relation on "space" expressing i_pos > o_pos
4968 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4972 struct isl_basic_map
*bmap
;
4973 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4976 for (i
= 0; i
< pos
&& bmap
; ++i
)
4977 bmap
= var_equal(bmap
, i
);
4979 bmap
= var_more(bmap
, pos
);
4980 return isl_basic_map_finalize(bmap
);
4983 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4985 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4986 __isl_take isl_space
*space
, unsigned pos
)
4989 isl_basic_map
*bmap
;
4991 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4992 for (i
= 0; i
< pos
; ++i
)
4993 bmap
= var_equal(bmap
, i
);
4994 bmap
= var_more_or_equal(bmap
, pos
);
4995 return isl_basic_map_finalize(bmap
);
4998 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4999 unsigned n
, int equal
)
5001 struct isl_map
*map
;
5004 if (n
== 0 && equal
)
5005 return isl_map_universe(space
);
5007 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5009 for (i
= 0; i
+ 1 < n
; ++i
)
5010 map
= isl_map_add_basic_map(map
,
5011 isl_basic_map_less_at(isl_space_copy(space
), i
));
5014 map
= isl_map_add_basic_map(map
,
5015 isl_basic_map_less_or_equal_at(space
, n
- 1));
5017 map
= isl_map_add_basic_map(map
,
5018 isl_basic_map_less_at(space
, n
- 1));
5020 isl_space_free(space
);
5025 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5029 return map_lex_lte_first(space
, space
->n_out
, equal
);
5032 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5034 return map_lex_lte_first(dim
, n
, 0);
5037 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5039 return map_lex_lte_first(dim
, n
, 1);
5042 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5044 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5047 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5049 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5052 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5053 unsigned n
, int equal
)
5055 struct isl_map
*map
;
5058 if (n
== 0 && equal
)
5059 return isl_map_universe(space
);
5061 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5063 for (i
= 0; i
+ 1 < n
; ++i
)
5064 map
= isl_map_add_basic_map(map
,
5065 isl_basic_map_more_at(isl_space_copy(space
), i
));
5068 map
= isl_map_add_basic_map(map
,
5069 isl_basic_map_more_or_equal_at(space
, n
- 1));
5071 map
= isl_map_add_basic_map(map
,
5072 isl_basic_map_more_at(space
, n
- 1));
5074 isl_space_free(space
);
5079 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5083 return map_lex_gte_first(space
, space
->n_out
, equal
);
5086 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5088 return map_lex_gte_first(dim
, n
, 0);
5091 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5093 return map_lex_gte_first(dim
, n
, 1);
5096 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5098 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5101 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5103 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5106 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5107 __isl_take isl_set
*set2
)
5110 map
= isl_map_lex_le(isl_set_get_space(set1
));
5111 map
= isl_map_intersect_domain(map
, set1
);
5112 map
= isl_map_intersect_range(map
, set2
);
5116 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5117 __isl_take isl_set
*set2
)
5120 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5121 map
= isl_map_intersect_domain(map
, set1
);
5122 map
= isl_map_intersect_range(map
, set2
);
5126 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5127 __isl_take isl_set
*set2
)
5130 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5131 map
= isl_map_intersect_domain(map
, set1
);
5132 map
= isl_map_intersect_range(map
, set2
);
5136 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5137 __isl_take isl_set
*set2
)
5140 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5141 map
= isl_map_intersect_domain(map
, set1
);
5142 map
= isl_map_intersect_range(map
, set2
);
5146 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5147 __isl_take isl_map
*map2
)
5150 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5151 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5152 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5156 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5157 __isl_take isl_map
*map2
)
5160 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5161 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5162 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5166 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5167 __isl_take isl_map
*map2
)
5170 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5171 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5172 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5176 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5177 __isl_take isl_map
*map2
)
5180 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5181 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5182 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5186 /* For a div d = floor(f/m), add the constraint
5190 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5191 unsigned pos
, isl_int
*div
)
5194 unsigned total
= isl_basic_map_total_dim(bmap
);
5196 i
= isl_basic_map_alloc_inequality(bmap
);
5198 return isl_stat_error
;
5199 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5200 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5205 /* For a div d = floor(f/m), add the constraint
5207 * -(f-(m-1)) + m d >= 0
5209 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5210 unsigned pos
, isl_int
*div
)
5213 unsigned total
= isl_basic_map_total_dim(bmap
);
5215 i
= isl_basic_map_alloc_inequality(bmap
);
5217 return isl_stat_error
;
5218 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5219 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5220 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5221 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5226 /* For a div d = floor(f/m), add the constraints
5229 * -(f-(m-1)) + m d >= 0
5231 * Note that the second constraint is the negation of
5235 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5236 unsigned pos
, isl_int
*div
)
5238 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5240 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5245 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5246 unsigned pos
, isl_int
*div
)
5248 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5252 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map
*bmap
,
5255 unsigned total
= isl_basic_map_total_dim(bmap
);
5256 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5258 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5262 /* For each known div d = floor(f/m), add the constraints
5265 * -(f-(m-1)) + m d >= 0
5267 * Remove duplicate constraints in case of some these div constraints
5268 * already appear in "bmap".
5270 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5271 __isl_take isl_basic_map
*bmap
)
5277 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5281 bmap
= add_known_div_constraints(bmap
);
5282 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5283 bmap
= isl_basic_map_finalize(bmap
);
5287 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5289 * In particular, if this div is of the form d = floor(f/m),
5290 * then add the constraint
5294 * if sign < 0 or the constraint
5296 * -(f-(m-1)) + m d >= 0
5300 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5301 unsigned div
, int sign
)
5309 total
= isl_basic_map_total_dim(bmap
);
5310 div_pos
= total
- bmap
->n_div
+ div
;
5313 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5315 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5318 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5319 __isl_take isl_basic_map
*bmap
)
5323 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5325 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5326 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5327 return bset_from_bmap(bmap
);
5328 bmap
= isl_basic_map_cow(bmap
);
5331 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5334 bmap
->extra
-= bmap
->n_div
;
5336 bmap
= isl_basic_map_finalize(bmap
);
5337 return bset_from_bmap(bmap
);
5339 isl_basic_map_free(bmap
);
5343 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5344 __isl_take isl_basic_set
*bset
)
5346 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5349 /* Replace each element in "list" by the result of applying
5350 * isl_basic_map_underlying_set to the element.
5352 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5353 __isl_take isl_basic_map_list
*list
)
5360 n
= isl_basic_map_list_n_basic_map(list
);
5361 for (i
= 0; i
< n
; ++i
) {
5362 isl_basic_map
*bmap
;
5363 isl_basic_set
*bset
;
5365 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5366 bset
= isl_basic_set_underlying_set(bmap
);
5367 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5373 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5374 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5376 struct isl_basic_map
*bmap
;
5377 struct isl_ctx
*ctx
;
5384 if (isl_basic_set_check_no_params(bset
) < 0 ||
5385 isl_basic_set_check_no_locals(bset
) < 0)
5387 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5389 if (like
->n_div
== 0) {
5390 isl_space
*space
= isl_basic_map_get_space(like
);
5391 isl_basic_map_free(like
);
5392 return isl_basic_map_reset_space(bset
, space
);
5394 bset
= isl_basic_set_cow(bset
);
5397 total
= bset
->dim
->n_out
+ bset
->extra
;
5398 bmap
= bset_to_bmap(bset
);
5399 isl_space_free(bmap
->dim
);
5400 bmap
->dim
= isl_space_copy(like
->dim
);
5403 bmap
->n_div
= like
->n_div
;
5404 bmap
->extra
+= like
->n_div
;
5408 ltotal
= total
- bmap
->extra
+ like
->extra
;
5411 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5412 bmap
->extra
* (1 + 1 + total
));
5413 if (isl_blk_is_error(bmap
->block2
))
5415 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5419 for (i
= 0; i
< bmap
->extra
; ++i
)
5420 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5421 for (i
= 0; i
< like
->n_div
; ++i
) {
5422 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5423 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5425 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5427 isl_basic_map_free(like
);
5428 bmap
= isl_basic_map_simplify(bmap
);
5429 bmap
= isl_basic_map_finalize(bmap
);
5432 isl_basic_map_free(like
);
5433 isl_basic_set_free(bset
);
5437 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5438 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5440 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5441 bset_to_bmap(like
)));
5444 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5448 map
= isl_map_cow(map
);
5451 map
->dim
= isl_space_cow(map
->dim
);
5455 for (i
= 1; i
< map
->n
; ++i
)
5456 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5458 for (i
= 0; i
< map
->n
; ++i
) {
5459 map
->p
[i
] = bset_to_bmap(
5460 isl_basic_map_underlying_set(map
->p
[i
]));
5465 map
->dim
= isl_space_underlying(map
->dim
, 0);
5467 isl_space_free(map
->dim
);
5468 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5472 return set_from_map(map
);
5478 /* Replace the space of "bmap" by "space".
5480 * If the space of "bmap" is identical to "space" (including the identifiers
5481 * of the input and output dimensions), then simply return the original input.
5483 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5484 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5487 isl_space
*bmap_space
;
5489 bmap_space
= isl_basic_map_peek_space(bmap
);
5490 equal
= isl_space_is_equal(bmap_space
, space
);
5491 if (equal
>= 0 && equal
)
5492 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5496 isl_space_free(space
);
5499 bmap
= isl_basic_map_cow(bmap
);
5500 if (!bmap
|| !space
)
5503 isl_space_free(bmap
->dim
);
5506 bmap
= isl_basic_map_finalize(bmap
);
5510 isl_basic_map_free(bmap
);
5511 isl_space_free(space
);
5515 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5516 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5518 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5522 /* Check that the total dimensions of "map" and "space" are the same.
5524 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5525 __isl_keep isl_space
*space
)
5527 unsigned dim1
, dim2
;
5530 return isl_stat_error
;
5531 dim1
= isl_map_dim(map
, isl_dim_all
);
5532 dim2
= isl_space_dim(space
, isl_dim_all
);
5535 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5536 "total dimensions do not match", return isl_stat_error
);
5539 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5540 __isl_take isl_space
*space
)
5544 map
= isl_map_cow(map
);
5548 for (i
= 0; i
< map
->n
; ++i
) {
5549 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5550 isl_space_copy(space
));
5554 isl_space_free(map
->dim
);
5560 isl_space_free(space
);
5564 /* Replace the space of "map" by "space", without modifying
5565 * the dimension of "map".
5567 * If the space of "map" is identical to "space" (including the identifiers
5568 * of the input and output dimensions), then simply return the original input.
5570 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5571 __isl_take isl_space
*space
)
5574 isl_space
*map_space
;
5576 map_space
= isl_map_peek_space(map
);
5577 equal
= isl_space_is_equal(map_space
, space
);
5578 if (equal
>= 0 && equal
)
5579 equal
= isl_space_has_equal_ids(map_space
, space
);
5583 isl_space_free(space
);
5586 if (check_map_space_equal_total_dim(map
, space
) < 0)
5588 return isl_map_reset_space(map
, space
);
5591 isl_space_free(space
);
5595 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5596 __isl_take isl_space
*dim
)
5598 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5601 /* Compute the parameter domain of the given basic set.
5603 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5609 is_params
= isl_basic_set_is_params(bset
);
5611 return isl_basic_set_free(bset
);
5615 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5616 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5617 space
= isl_basic_set_get_space(bset
);
5618 space
= isl_space_params(space
);
5619 bset
= isl_basic_set_reset_space(bset
, space
);
5623 /* Construct a zero-dimensional basic set with the given parameter domain.
5625 __isl_give isl_basic_set
*isl_basic_set_from_params(
5626 __isl_take isl_basic_set
*bset
)
5629 space
= isl_basic_set_get_space(bset
);
5630 space
= isl_space_set_from_params(space
);
5631 bset
= isl_basic_set_reset_space(bset
, space
);
5635 /* Compute the parameter domain of the given set.
5637 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5642 if (isl_set_is_params(set
))
5645 n
= isl_set_dim(set
, isl_dim_set
);
5646 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5647 space
= isl_set_get_space(set
);
5648 space
= isl_space_params(space
);
5649 set
= isl_set_reset_space(set
, space
);
5653 /* Construct a zero-dimensional set with the given parameter domain.
5655 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5658 space
= isl_set_get_space(set
);
5659 space
= isl_space_set_from_params(space
);
5660 set
= isl_set_reset_space(set
, space
);
5664 /* Compute the parameter domain of the given map.
5666 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5671 n
= isl_map_dim(map
, isl_dim_in
);
5672 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5673 n
= isl_map_dim(map
, isl_dim_out
);
5674 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5675 space
= isl_map_get_space(map
);
5676 space
= isl_space_params(space
);
5677 map
= isl_map_reset_space(map
, space
);
5681 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5688 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5690 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5691 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5693 return isl_basic_map_reset_space(bmap
, space
);
5696 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5699 return isl_bool_error
;
5700 return isl_space_may_be_set(bmap
->dim
);
5703 /* Is this basic map actually a set?
5704 * Users should never call this function. Outside of isl,
5705 * the type should indicate whether something is a set or a map.
5707 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5710 return isl_bool_error
;
5711 return isl_space_is_set(bmap
->dim
);
5714 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5718 is_set
= isl_basic_map_is_set(bmap
);
5723 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5725 isl_basic_map_free(bmap
);
5729 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5730 __isl_take isl_basic_map
*bmap
)
5734 isl_basic_map
*domain
;
5735 int nparam
, n_in
, n_out
;
5737 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5738 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5739 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5741 space
= isl_basic_map_get_space(bmap
);
5742 space
= isl_space_from_range(isl_space_domain(space
));
5743 domain
= isl_basic_map_universe(space
);
5745 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5746 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5747 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5749 for (i
= 0; i
< n_in
; ++i
)
5750 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5753 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5754 return isl_basic_map_finalize(bmap
);
5757 __isl_give isl_basic_map
*isl_basic_map_range_map(
5758 __isl_take isl_basic_map
*bmap
)
5762 isl_basic_map
*range
;
5763 int nparam
, n_in
, n_out
;
5765 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5766 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5767 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5769 space
= isl_basic_map_get_space(bmap
);
5770 space
= isl_space_from_range(isl_space_range(space
));
5771 range
= isl_basic_map_universe(space
);
5773 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5774 bmap
= isl_basic_map_apply_range(bmap
, range
);
5775 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5777 for (i
= 0; i
< n_out
; ++i
)
5778 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5781 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5782 return isl_basic_map_finalize(bmap
);
5785 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5789 return isl_space_may_be_set(map
->dim
);
5792 /* Is this map actually a set?
5793 * Users should never call this function. Outside of isl,
5794 * the type should indicate whether something is a set or a map.
5796 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5799 return isl_bool_error
;
5800 return isl_space_is_set(map
->dim
);
5803 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5807 struct isl_set
*set
;
5809 is_set
= isl_map_is_set(map
);
5813 return set_from_map(map
);
5815 map
= isl_map_cow(map
);
5819 set
= set_from_map(map
);
5820 set
->dim
= isl_space_range(set
->dim
);
5823 for (i
= 0; i
< map
->n
; ++i
) {
5824 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5828 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5829 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5836 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5840 map
= isl_map_cow(map
);
5844 map
->dim
= isl_space_domain_map(map
->dim
);
5847 for (i
= 0; i
< map
->n
; ++i
) {
5848 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5852 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5853 map
= isl_map_unmark_normalized(map
);
5860 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5863 isl_space
*range_dim
;
5865 map
= isl_map_cow(map
);
5869 range_dim
= isl_space_range(isl_map_get_space(map
));
5870 range_dim
= isl_space_from_range(range_dim
);
5871 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5872 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5875 for (i
= 0; i
< map
->n
; ++i
) {
5876 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5880 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5881 map
= isl_map_unmark_normalized(map
);
5888 /* Given a wrapped map of the form A[B -> C],
5889 * return the map A[B -> C] -> B.
5891 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5898 if (!isl_set_has_tuple_id(set
))
5899 return isl_map_domain_map(isl_set_unwrap(set
));
5901 id
= isl_set_get_tuple_id(set
);
5902 map
= isl_map_domain_map(isl_set_unwrap(set
));
5903 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5908 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5909 __isl_take isl_basic_set
*bset
)
5911 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5914 __isl_give isl_basic_map
*isl_basic_map_from_range(
5915 __isl_take isl_basic_set
*bset
)
5918 space
= isl_basic_set_get_space(bset
);
5919 space
= isl_space_from_range(space
);
5920 bset
= isl_basic_set_reset_space(bset
, space
);
5921 return bset_to_bmap(bset
);
5924 /* Create a relation with the given set as range.
5925 * The domain of the created relation is a zero-dimensional
5926 * flat anonymous space.
5928 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5931 space
= isl_set_get_space(set
);
5932 space
= isl_space_from_range(space
);
5933 set
= isl_set_reset_space(set
, space
);
5934 return set_to_map(set
);
5937 /* Create a relation with the given set as domain.
5938 * The range of the created relation is a zero-dimensional
5939 * flat anonymous space.
5941 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5943 return isl_map_reverse(isl_map_from_range(set
));
5946 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5947 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5949 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5952 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5953 __isl_take isl_set
*range
)
5955 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5958 /* Return a newly allocated isl_map with given space and flags and
5959 * room for "n" basic maps.
5960 * Make sure that all cached information is cleared.
5962 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5965 struct isl_map
*map
;
5970 isl_die(space
->ctx
, isl_error_internal
,
5971 "negative number of basic maps", goto error
);
5972 map
= isl_calloc(space
->ctx
, struct isl_map
,
5973 sizeof(struct isl_map
) +
5974 (n
- 1) * sizeof(struct isl_basic_map
*));
5978 map
->ctx
= space
->ctx
;
5979 isl_ctx_ref(map
->ctx
);
5987 isl_space_free(space
);
5991 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5993 struct isl_basic_map
*bmap
;
5994 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5995 bmap
= isl_basic_map_set_to_empty(bmap
);
5999 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6001 struct isl_basic_set
*bset
;
6002 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6003 bset
= isl_basic_set_set_to_empty(bset
);
6007 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6009 struct isl_basic_map
*bmap
;
6010 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6011 bmap
= isl_basic_map_finalize(bmap
);
6015 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6017 struct isl_basic_set
*bset
;
6018 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6019 bset
= isl_basic_set_finalize(bset
);
6023 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6024 __isl_take isl_space
*space
)
6027 unsigned total
= isl_space_dim(space
, isl_dim_all
);
6028 isl_basic_map
*bmap
;
6030 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6031 for (i
= 0; i
< total
; ++i
) {
6032 int k
= isl_basic_map_alloc_inequality(bmap
);
6035 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6036 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6040 isl_basic_map_free(bmap
);
6044 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6045 __isl_take isl_space
*space
)
6047 return isl_basic_map_nat_universe(space
);
6050 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6052 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6055 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6057 return isl_map_nat_universe(dim
);
6060 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6062 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6065 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6067 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6070 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6072 struct isl_map
*map
;
6075 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6076 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6080 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6082 struct isl_set
*set
;
6085 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6086 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6090 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6093 struct isl_map
*dup
;
6097 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6098 for (i
= 0; i
< map
->n
; ++i
)
6099 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6103 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6104 __isl_take isl_basic_map
*bmap
)
6108 if (isl_basic_map_plain_is_empty(bmap
)) {
6109 isl_basic_map_free(bmap
);
6112 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6113 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6114 map
->p
[map
->n
] = bmap
;
6116 map
= isl_map_unmark_normalized(map
);
6122 isl_basic_map_free(bmap
);
6126 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6137 isl_ctx_deref(map
->ctx
);
6138 for (i
= 0; i
< map
->n
; ++i
)
6139 isl_basic_map_free(map
->p
[i
]);
6140 isl_space_free(map
->dim
);
6146 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6147 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6151 bmap
= isl_basic_map_cow(bmap
);
6152 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6153 j
= isl_basic_map_alloc_equality(bmap
);
6156 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6157 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6158 isl_int_set_si(bmap
->eq
[j
][0], value
);
6159 bmap
= isl_basic_map_simplify(bmap
);
6160 return isl_basic_map_finalize(bmap
);
6162 isl_basic_map_free(bmap
);
6166 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6167 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6171 bmap
= isl_basic_map_cow(bmap
);
6172 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6173 j
= isl_basic_map_alloc_equality(bmap
);
6176 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6177 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6178 isl_int_set(bmap
->eq
[j
][0], value
);
6179 bmap
= isl_basic_map_simplify(bmap
);
6180 return isl_basic_map_finalize(bmap
);
6182 isl_basic_map_free(bmap
);
6186 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6187 enum isl_dim_type type
, unsigned pos
, int value
)
6189 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6190 return isl_basic_map_free(bmap
);
6191 return isl_basic_map_fix_pos_si(bmap
,
6192 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6195 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6196 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6198 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6199 return isl_basic_map_free(bmap
);
6200 return isl_basic_map_fix_pos(bmap
,
6201 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6204 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6205 * to be equal to "v".
6207 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6208 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6212 if (!isl_val_is_int(v
))
6213 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6214 "expecting integer value", goto error
);
6215 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6217 pos
+= isl_basic_map_offset(bmap
, type
);
6218 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6222 isl_basic_map_free(bmap
);
6227 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6228 * to be equal to "v".
6230 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6231 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6233 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6236 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6237 enum isl_dim_type type
, unsigned pos
, int value
)
6239 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6243 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6244 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6246 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6250 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6251 unsigned input
, int value
)
6253 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6256 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6257 unsigned dim
, int value
)
6259 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6260 isl_dim_set
, dim
, value
));
6263 /* Remove the basic map at position "i" from "map" if this basic map
6264 * is (obviously) empty.
6266 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6273 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6275 return isl_map_free(map
);
6279 isl_basic_map_free(map
->p
[i
]);
6282 map
->p
[i
] = map
->p
[map
->n
];
6283 map
= isl_map_unmark_normalized(map
);
6290 /* Perform "fn" on each basic map of "map", where we may not be holding
6291 * the only reference to "map".
6292 * In particular, "fn" should be a semantics preserving operation
6293 * that we want to apply to all copies of "map". We therefore need
6294 * to be careful not to modify "map" in a way that breaks "map"
6295 * in case anything goes wrong.
6297 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6298 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6300 struct isl_basic_map
*bmap
;
6306 for (i
= map
->n
- 1; i
>= 0; --i
) {
6307 bmap
= isl_basic_map_copy(map
->p
[i
]);
6311 isl_basic_map_free(map
->p
[i
]);
6313 map
= remove_if_empty(map
, i
);
6324 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6325 enum isl_dim_type type
, unsigned pos
, int value
)
6329 map
= isl_map_cow(map
);
6330 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6331 return isl_map_free(map
);
6332 for (i
= map
->n
- 1; i
>= 0; --i
) {
6333 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6334 map
= remove_if_empty(map
, i
);
6338 map
= isl_map_unmark_normalized(map
);
6342 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6343 enum isl_dim_type type
, unsigned pos
, int value
)
6345 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6348 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6349 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6353 map
= isl_map_cow(map
);
6354 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6355 return isl_map_free(map
);
6356 for (i
= 0; i
< map
->n
; ++i
) {
6357 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6361 map
= isl_map_unmark_normalized(map
);
6368 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6369 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6371 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6374 /* Fix the value of the variable at position "pos" of type "type" of "map"
6375 * to be equal to "v".
6377 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6378 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6382 map
= isl_map_cow(map
);
6386 if (!isl_val_is_int(v
))
6387 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6388 "expecting integer value", goto error
);
6389 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6391 for (i
= map
->n
- 1; i
>= 0; --i
) {
6392 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6394 map
= remove_if_empty(map
, i
);
6398 map
= isl_map_unmark_normalized(map
);
6407 /* Fix the value of the variable at position "pos" of type "type" of "set"
6408 * to be equal to "v".
6410 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6411 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6413 return isl_map_fix_val(set
, type
, pos
, v
);
6416 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6417 unsigned input
, int value
)
6419 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6422 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6424 return set_from_map(isl_map_fix_si(set_to_map(set
),
6425 isl_dim_set
, dim
, value
));
6428 static __isl_give isl_basic_map
*basic_map_bound_si(
6429 __isl_take isl_basic_map
*bmap
,
6430 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6434 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6435 return isl_basic_map_free(bmap
);
6436 pos
+= isl_basic_map_offset(bmap
, type
);
6437 bmap
= isl_basic_map_cow(bmap
);
6438 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6439 j
= isl_basic_map_alloc_inequality(bmap
);
6442 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6444 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6445 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6447 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6448 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6450 bmap
= isl_basic_map_simplify(bmap
);
6451 return isl_basic_map_finalize(bmap
);
6453 isl_basic_map_free(bmap
);
6457 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6458 __isl_take isl_basic_map
*bmap
,
6459 enum isl_dim_type type
, unsigned pos
, int value
)
6461 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6464 /* Constrain the values of the given dimension to be no greater than "value".
6466 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6467 __isl_take isl_basic_map
*bmap
,
6468 enum isl_dim_type type
, unsigned pos
, int value
)
6470 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6473 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6474 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6478 map
= isl_map_cow(map
);
6479 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6480 return isl_map_free(map
);
6481 for (i
= 0; i
< map
->n
; ++i
) {
6482 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6483 type
, pos
, value
, upper
);
6487 map
= isl_map_unmark_normalized(map
);
6494 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6495 enum isl_dim_type type
, unsigned pos
, int value
)
6497 return map_bound_si(map
, type
, pos
, value
, 0);
6500 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6501 enum isl_dim_type type
, unsigned pos
, int value
)
6503 return map_bound_si(map
, type
, pos
, value
, 1);
6506 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6507 enum isl_dim_type type
, unsigned pos
, int value
)
6509 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6513 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6514 enum isl_dim_type type
, unsigned pos
, int value
)
6516 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6519 /* Bound the given variable of "bmap" from below (or above is "upper"
6520 * is set) to "value".
6522 static __isl_give isl_basic_map
*basic_map_bound(
6523 __isl_take isl_basic_map
*bmap
,
6524 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6528 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6529 return isl_basic_map_free(bmap
);
6530 pos
+= isl_basic_map_offset(bmap
, type
);
6531 bmap
= isl_basic_map_cow(bmap
);
6532 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6533 j
= isl_basic_map_alloc_inequality(bmap
);
6536 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6538 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6539 isl_int_set(bmap
->ineq
[j
][0], value
);
6541 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6542 isl_int_neg(bmap
->ineq
[j
][0], value
);
6544 bmap
= isl_basic_map_simplify(bmap
);
6545 return isl_basic_map_finalize(bmap
);
6547 isl_basic_map_free(bmap
);
6551 /* Bound the given variable of "map" from below (or above is "upper"
6552 * is set) to "value".
6554 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6555 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6559 map
= isl_map_cow(map
);
6560 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6561 return isl_map_free(map
);
6562 for (i
= map
->n
- 1; i
>= 0; --i
) {
6563 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6564 map
= remove_if_empty(map
, i
);
6568 map
= isl_map_unmark_normalized(map
);
6572 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6573 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6575 return map_bound(map
, type
, pos
, value
, 0);
6578 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6579 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6581 return map_bound(map
, type
, pos
, value
, 1);
6584 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6585 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6587 return isl_map_lower_bound(set
, type
, pos
, value
);
6590 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6591 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6593 return isl_map_upper_bound(set
, type
, pos
, value
);
6596 /* Force the values of the variable at position "pos" of type "type" of "set"
6597 * to be no smaller than "value".
6599 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6600 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6604 if (!isl_val_is_int(value
))
6605 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6606 "expecting integer value", goto error
);
6607 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6608 isl_val_free(value
);
6611 isl_val_free(value
);
6616 /* Force the values of the variable at position "pos" of type "type" of "set"
6617 * to be no greater than "value".
6619 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6620 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6624 if (!isl_val_is_int(value
))
6625 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6626 "expecting integer value", goto error
);
6627 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6628 isl_val_free(value
);
6631 isl_val_free(value
);
6636 /* Bound the given variable of "bset" from below (or above is "upper"
6637 * is set) to "value".
6639 static __isl_give isl_basic_set
*isl_basic_set_bound(
6640 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6641 isl_int value
, int upper
)
6643 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6644 type
, pos
, value
, upper
));
6647 /* Bound the given variable of "bset" from below (or above is "upper"
6648 * is set) to "value".
6650 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6651 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6652 __isl_take isl_val
*value
, int upper
)
6656 if (!isl_val_is_int(value
))
6657 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6658 "expecting integer value", goto error
);
6659 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6660 isl_val_free(value
);
6663 isl_val_free(value
);
6664 isl_basic_set_free(bset
);
6668 /* Bound the given variable of "bset" from below to "value".
6670 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6671 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6672 __isl_take isl_val
*value
)
6674 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6677 /* Bound the given variable of "bset" from above to "value".
6679 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6680 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6681 __isl_take isl_val
*value
)
6683 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6686 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6690 map
= isl_map_cow(map
);
6694 map
->dim
= isl_space_reverse(map
->dim
);
6697 for (i
= 0; i
< map
->n
; ++i
) {
6698 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6702 map
= isl_map_unmark_normalized(map
);
6710 #define TYPE isl_pw_multi_aff
6712 #define SUFFIX _pw_multi_aff
6714 #define EMPTY isl_pw_multi_aff_empty
6716 #define ADD isl_pw_multi_aff_union_add
6717 #include "isl_map_lexopt_templ.c"
6719 /* Given a map "map", compute the lexicographically minimal
6720 * (or maximal) image element for each domain element in dom,
6721 * in the form of an isl_pw_multi_aff.
6722 * If "empty" is not NULL, then set *empty to those elements in dom that
6723 * do not have an image element.
6724 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6725 * should be computed over the domain of "map". "empty" is also NULL
6728 * We first compute the lexicographically minimal or maximal element
6729 * in the first basic map. This results in a partial solution "res"
6730 * and a subset "todo" of dom that still need to be handled.
6731 * We then consider each of the remaining maps in "map" and successively
6732 * update both "res" and "todo".
6733 * If "empty" is NULL, then the todo sets are not needed and therefore
6734 * also not computed.
6736 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6737 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6738 __isl_give isl_set
**empty
, unsigned flags
)
6742 isl_pw_multi_aff
*res
;
6745 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6746 if (!map
|| (!full
&& !dom
))
6749 if (isl_map_plain_is_empty(map
)) {
6754 return isl_pw_multi_aff_from_map(map
);
6757 res
= basic_map_partial_lexopt_pw_multi_aff(
6758 isl_basic_map_copy(map
->p
[0]),
6759 isl_set_copy(dom
), empty
, flags
);
6763 for (i
= 1; i
< map
->n
; ++i
) {
6764 isl_pw_multi_aff
*res_i
;
6766 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6767 isl_basic_map_copy(map
->p
[i
]),
6768 isl_set_copy(dom
), empty
, flags
);
6770 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6771 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6773 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6776 todo
= isl_set_intersect(todo
, *empty
);
6795 #define TYPE isl_map
6799 #define EMPTY isl_map_empty
6801 #define ADD isl_map_union_disjoint
6802 #include "isl_map_lexopt_templ.c"
6804 /* Given a map "map", compute the lexicographically minimal
6805 * (or maximal) image element for each domain element in "dom",
6806 * in the form of an isl_map.
6807 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6808 * do not have an image element.
6809 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6810 * should be computed over the domain of "map". "empty" is also NULL
6813 * If the input consists of more than one disjunct, then first
6814 * compute the desired result in the form of an isl_pw_multi_aff and
6815 * then convert that into an isl_map.
6817 * This function used to have an explicit implementation in terms
6818 * of isl_maps, but it would continually intersect the domains of
6819 * partial results with the complement of the domain of the next
6820 * partial solution, potentially leading to an explosion in the number
6821 * of disjuncts if there are several disjuncts in the input.
6822 * An even earlier implementation of this function would look for
6823 * better results in the domain of the partial result and for extra
6824 * results in the complement of this domain, which would lead to
6825 * even more splintering.
6827 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6828 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6829 __isl_give isl_set
**empty
, unsigned flags
)
6832 struct isl_map
*res
;
6833 isl_pw_multi_aff
*pma
;
6835 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6836 if (!map
|| (!full
&& !dom
))
6839 if (isl_map_plain_is_empty(map
)) {
6848 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6854 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6856 return isl_map_from_pw_multi_aff(pma
);
6865 __isl_give isl_map
*isl_map_partial_lexmax(
6866 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6867 __isl_give isl_set
**empty
)
6869 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6872 __isl_give isl_map
*isl_map_partial_lexmin(
6873 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6874 __isl_give isl_set
**empty
)
6876 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6879 __isl_give isl_set
*isl_set_partial_lexmin(
6880 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6881 __isl_give isl_set
**empty
)
6883 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6887 __isl_give isl_set
*isl_set_partial_lexmax(
6888 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6889 __isl_give isl_set
**empty
)
6891 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6895 /* Compute the lexicographic minimum (or maximum if "flags" includes
6896 * ISL_OPT_MAX) of "bset" over its parametric domain.
6898 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6901 return isl_basic_map_lexopt(bset
, flags
);
6904 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6906 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6909 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6911 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6914 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6916 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6919 /* Compute the lexicographic minimum of "bset" over its parametric domain
6920 * for the purpose of quantifier elimination.
6921 * That is, find an explicit representation for all the existentially
6922 * quantified variables in "bset" by computing their lexicographic
6925 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6926 __isl_take isl_basic_set
*bset
)
6928 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6931 /* Given a basic map with one output dimension, compute the minimum or
6932 * maximum of that dimension as an isl_pw_aff.
6934 * Compute the optimum as a lexicographic optimum over the single
6935 * output dimension and extract the single isl_pw_aff from the result.
6937 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6940 isl_pw_multi_aff
*pma
;
6943 bmap
= isl_basic_map_copy(bmap
);
6944 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6945 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6946 isl_pw_multi_aff_free(pma
);
6951 /* Compute the minimum or maximum of the given output dimension
6952 * as a function of the parameters and the input dimensions,
6953 * but independently of the other output dimensions.
6955 * We first project out the other output dimension and then compute
6956 * the "lexicographic" maximum in each basic map, combining the results
6957 * using isl_pw_aff_union_max.
6959 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6966 n_out
= isl_map_dim(map
, isl_dim_out
);
6967 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6968 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6973 isl_space
*space
= isl_map_get_space(map
);
6975 return isl_pw_aff_empty(space
);
6978 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6979 for (i
= 1; i
< map
->n
; ++i
) {
6980 isl_pw_aff
*pwaff_i
;
6982 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6983 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6991 /* Compute the minimum of the given output dimension as a function of the
6992 * parameters and input dimensions, but independently of
6993 * the other output dimensions.
6995 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6997 return map_dim_opt(map
, pos
, 0);
7000 /* Compute the maximum of the given output dimension as a function of the
7001 * parameters and input dimensions, but independently of
7002 * the other output dimensions.
7004 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7006 return map_dim_opt(map
, pos
, 1);
7009 /* Compute the minimum or maximum of the given set dimension
7010 * as a function of the parameters,
7011 * but independently of the other set dimensions.
7013 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7016 return map_dim_opt(set
, pos
, max
);
7019 /* Compute the maximum of the given set dimension as a function of the
7020 * parameters, but independently of the other set dimensions.
7022 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7024 return set_dim_opt(set
, pos
, 1);
7027 /* Compute the minimum of the given set dimension as a function of the
7028 * parameters, but independently of the other set dimensions.
7030 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7032 return set_dim_opt(set
, pos
, 0);
7035 /* Apply a preimage specified by "mat" on the parameters of "bset".
7036 * bset is assumed to have only parameters and divs.
7038 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7039 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7046 bset
->dim
= isl_space_cow(bset
->dim
);
7050 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7052 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7054 bset
->dim
->nparam
= 0;
7055 bset
->dim
->n_out
= nparam
;
7056 bset
= isl_basic_set_preimage(bset
, mat
);
7058 bset
->dim
->nparam
= bset
->dim
->n_out
;
7059 bset
->dim
->n_out
= 0;
7064 isl_basic_set_free(bset
);
7068 /* Apply a preimage specified by "mat" on the parameters of "set".
7069 * set is assumed to have only parameters and divs.
7071 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7072 __isl_take isl_mat
*mat
)
7080 nparam
= isl_set_dim(set
, isl_dim_param
);
7082 if (mat
->n_row
!= 1 + nparam
)
7083 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7084 "unexpected number of rows", goto error
);
7086 space
= isl_set_get_space(set
);
7087 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7088 isl_dim_param
, 0, nparam
);
7089 set
= isl_set_reset_space(set
, space
);
7090 set
= isl_set_preimage(set
, mat
);
7091 nparam
= isl_set_dim(set
, isl_dim_out
);
7092 space
= isl_set_get_space(set
);
7093 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7094 isl_dim_out
, 0, nparam
);
7095 set
= isl_set_reset_space(set
, space
);
7103 /* Intersect the basic set "bset" with the affine space specified by the
7104 * equalities in "eq".
7106 static __isl_give isl_basic_set
*basic_set_append_equalities(
7107 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7115 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7120 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7121 for (i
= 0; i
< eq
->n_row
; ++i
) {
7122 k
= isl_basic_set_alloc_equality(bset
);
7125 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7126 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7130 bset
= isl_basic_set_gauss(bset
, NULL
);
7131 bset
= isl_basic_set_finalize(bset
);
7136 isl_basic_set_free(bset
);
7140 /* Intersect the set "set" with the affine space specified by the
7141 * equalities in "eq".
7143 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7151 for (i
= 0; i
< set
->n
; ++i
) {
7152 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7165 /* Given a basic set "bset" that only involves parameters and existentially
7166 * quantified variables, return the index of the first equality
7167 * that only involves parameters. If there is no such equality then
7168 * return bset->n_eq.
7170 * This function assumes that isl_basic_set_gauss has been called on "bset".
7172 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7175 unsigned nparam
, n_div
;
7180 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7181 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7183 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7184 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7191 /* Compute an explicit representation for the existentially quantified
7192 * variables in "bset" by computing the "minimal value" of the set
7193 * variables. Since there are no set variables, the computation of
7194 * the minimal value essentially computes an explicit representation
7195 * of the non-empty part(s) of "bset".
7197 * The input only involves parameters and existentially quantified variables.
7198 * All equalities among parameters have been removed.
7200 * Since the existentially quantified variables in the result are in general
7201 * going to be different from those in the input, we first replace
7202 * them by the minimal number of variables based on their equalities.
7203 * This should simplify the parametric integer programming.
7205 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7207 isl_morph
*morph1
, *morph2
;
7213 if (bset
->n_eq
== 0)
7214 return isl_basic_set_lexmin_compute_divs(bset
);
7216 morph1
= isl_basic_set_parameter_compression(bset
);
7217 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7218 bset
= isl_basic_set_lift(bset
);
7219 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7220 bset
= isl_morph_basic_set(morph2
, bset
);
7221 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7222 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7224 set
= isl_basic_set_lexmin_compute_divs(bset
);
7226 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7231 /* Project the given basic set onto its parameter domain, possibly introducing
7232 * new, explicit, existential variables in the constraints.
7233 * The input has parameters and (possibly implicit) existential variables.
7234 * The output has the same parameters, but only
7235 * explicit existentially quantified variables.
7237 * The actual projection is performed by pip, but pip doesn't seem
7238 * to like equalities very much, so we first remove the equalities
7239 * among the parameters by performing a variable compression on
7240 * the parameters. Afterward, an inverse transformation is performed
7241 * and the equalities among the parameters are inserted back in.
7243 * The variable compression on the parameters may uncover additional
7244 * equalities that were only implicit before. We therefore check
7245 * if there are any new parameter equalities in the result and
7246 * if so recurse. The removal of parameter equalities is required
7247 * for the parameter compression performed by base_compute_divs.
7249 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7253 struct isl_mat
*T
, *T2
;
7254 struct isl_set
*set
;
7257 bset
= isl_basic_set_cow(bset
);
7261 if (bset
->n_eq
== 0)
7262 return base_compute_divs(bset
);
7264 bset
= isl_basic_set_gauss(bset
, NULL
);
7267 if (isl_basic_set_plain_is_empty(bset
))
7268 return isl_set_from_basic_set(bset
);
7270 i
= first_parameter_equality(bset
);
7271 if (i
== bset
->n_eq
)
7272 return base_compute_divs(bset
);
7274 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7275 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7277 eq
= isl_mat_cow(eq
);
7278 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7279 if (T
&& T
->n_col
== 0) {
7283 bset
= isl_basic_set_set_to_empty(bset
);
7284 return isl_set_from_basic_set(bset
);
7286 bset
= basic_set_parameter_preimage(bset
, T
);
7288 i
= first_parameter_equality(bset
);
7291 else if (i
== bset
->n_eq
)
7292 set
= base_compute_divs(bset
);
7294 set
= parameter_compute_divs(bset
);
7295 set
= set_parameter_preimage(set
, T2
);
7296 set
= set_append_equalities(set
, eq
);
7300 /* Insert the divs from "ls" before those of "bmap".
7302 * The number of columns is not changed, which means that the last
7303 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7304 * The caller is responsible for removing the same number of dimensions
7305 * from the space of "bmap".
7307 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7308 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7314 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7318 old_n_div
= bmap
->n_div
;
7319 bmap
= insert_div_rows(bmap
, n_div
);
7323 for (i
= 0; i
< n_div
; ++i
) {
7324 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7325 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7331 /* Replace the space of "bmap" by the space and divs of "ls".
7333 * If "ls" has any divs, then we simplify the result since we may
7334 * have discovered some additional equalities that could simplify
7335 * the div expressions.
7337 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7338 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7342 bmap
= isl_basic_map_cow(bmap
);
7346 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7347 bmap
= insert_divs_from_local_space(bmap
, ls
);
7351 isl_space_free(bmap
->dim
);
7352 bmap
->dim
= isl_local_space_get_space(ls
);
7356 isl_local_space_free(ls
);
7358 bmap
= isl_basic_map_simplify(bmap
);
7359 bmap
= isl_basic_map_finalize(bmap
);
7362 isl_basic_map_free(bmap
);
7363 isl_local_space_free(ls
);
7367 /* Replace the space of "map" by the space and divs of "ls".
7369 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7370 __isl_take isl_local_space
*ls
)
7374 map
= isl_map_cow(map
);
7378 for (i
= 0; i
< map
->n
; ++i
) {
7379 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7380 isl_local_space_copy(ls
));
7384 isl_space_free(map
->dim
);
7385 map
->dim
= isl_local_space_get_space(ls
);
7389 isl_local_space_free(ls
);
7392 isl_local_space_free(ls
);
7397 /* Compute an explicit representation for the existentially
7398 * quantified variables for which do not know any explicit representation yet.
7400 * We first sort the existentially quantified variables so that the
7401 * existentially quantified variables for which we already have an explicit
7402 * representation are placed before those for which we do not.
7403 * The input dimensions, the output dimensions and the existentially
7404 * quantified variables for which we already have an explicit
7405 * representation are then turned into parameters.
7406 * compute_divs returns a map with the same parameters and
7407 * no input or output dimensions and the dimension specification
7408 * is reset to that of the input, including the existentially quantified
7409 * variables for which we already had an explicit representation.
7411 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7413 struct isl_basic_set
*bset
;
7414 struct isl_set
*set
;
7415 struct isl_map
*map
;
7417 isl_local_space
*ls
;
7424 bmap
= isl_basic_map_sort_divs(bmap
);
7425 bmap
= isl_basic_map_cow(bmap
);
7429 n_known
= isl_basic_map_first_unknown_div(bmap
);
7431 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7433 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7434 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7435 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7436 space
= isl_space_set_alloc(bmap
->ctx
,
7437 nparam
+ n_in
+ n_out
+ n_known
, 0);
7441 ls
= isl_basic_map_get_local_space(bmap
);
7442 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7443 n_known
, bmap
->n_div
- n_known
);
7445 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7446 swap_div(bmap
, i
- n_known
, i
);
7447 bmap
->n_div
-= n_known
;
7448 bmap
->extra
-= n_known
;
7450 bmap
= isl_basic_map_reset_space(bmap
, space
);
7451 bset
= bset_from_bmap(bmap
);
7453 set
= parameter_compute_divs(bset
);
7454 map
= set_to_map(set
);
7455 map
= replace_space_by_local_space(map
, ls
);
7459 isl_basic_map_free(bmap
);
7463 /* Remove the explicit representation of local variable "div",
7466 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7467 __isl_take isl_basic_map
*bmap
, int div
)
7471 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7473 return isl_basic_map_free(bmap
);
7477 bmap
= isl_basic_map_cow(bmap
);
7480 isl_int_set_si(bmap
->div
[div
][0], 0);
7484 /* Is local variable "div" of "bmap" marked as not having an explicit
7486 * Note that even if "div" is not marked in this way and therefore
7487 * has an explicit representation, this representation may still
7488 * depend (indirectly) on other local variables that do not
7489 * have an explicit representation.
7491 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7494 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7495 return isl_bool_error
;
7496 return isl_int_is_zero(bmap
->div
[div
][0]);
7499 /* Return the position of the first local variable that does not
7500 * have an explicit representation.
7501 * Return the total number of local variables if they all have
7502 * an explicit representation.
7503 * Return -1 on error.
7505 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7512 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7513 if (!isl_basic_map_div_is_known(bmap
, i
))
7519 /* Return the position of the first local variable that does not
7520 * have an explicit representation.
7521 * Return the total number of local variables if they all have
7522 * an explicit representation.
7523 * Return -1 on error.
7525 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7527 return isl_basic_map_first_unknown_div(bset
);
7530 /* Does "bmap" have an explicit representation for all local variables?
7532 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7536 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7537 first
= isl_basic_map_first_unknown_div(bmap
);
7539 return isl_bool_error
;
7543 /* Do all basic maps in "map" have an explicit representation
7544 * for all local variables?
7546 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7551 return isl_bool_error
;
7553 for (i
= 0; i
< map
->n
; ++i
) {
7554 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7559 return isl_bool_true
;
7562 /* If bmap contains any unknown divs, then compute explicit
7563 * expressions for them. However, this computation may be
7564 * quite expensive, so first try to remove divs that aren't
7567 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7570 struct isl_map
*map
;
7572 known
= isl_basic_map_divs_known(bmap
);
7576 return isl_map_from_basic_map(bmap
);
7578 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7580 known
= isl_basic_map_divs_known(bmap
);
7584 return isl_map_from_basic_map(bmap
);
7586 map
= compute_divs(bmap
);
7589 isl_basic_map_free(bmap
);
7593 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7597 struct isl_map
*res
;
7604 known
= isl_map_divs_known(map
);
7612 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7613 for (i
= 1 ; i
< map
->n
; ++i
) {
7615 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7616 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7617 res
= isl_map_union_disjoint(res
, r2
);
7619 res
= isl_map_union(res
, r2
);
7626 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7628 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7631 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7633 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7636 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7639 struct isl_set
*set
;
7644 map
= isl_map_cow(map
);
7648 set
= set_from_map(map
);
7649 set
->dim
= isl_space_domain(set
->dim
);
7652 for (i
= 0; i
< map
->n
; ++i
) {
7653 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7657 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7658 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7665 /* Return the union of "map1" and "map2", where we assume for now that
7666 * "map1" and "map2" are disjoint. Note that the basic maps inside
7667 * "map1" or "map2" may not be disjoint from each other.
7668 * Also note that this function is also called from isl_map_union,
7669 * which takes care of handling the situation where "map1" and "map2"
7670 * may not be disjoint.
7672 * If one of the inputs is empty, we can simply return the other input.
7673 * Similarly, if one of the inputs is universal, then it is equal to the union.
7675 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7676 __isl_take isl_map
*map2
)
7680 struct isl_map
*map
= NULL
;
7686 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7687 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7688 "spaces don't match", goto error
);
7699 is_universe
= isl_map_plain_is_universe(map1
);
7700 if (is_universe
< 0)
7707 is_universe
= isl_map_plain_is_universe(map2
);
7708 if (is_universe
< 0)
7715 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7716 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7717 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7719 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7720 map1
->n
+ map2
->n
, flags
);
7723 for (i
= 0; i
< map1
->n
; ++i
) {
7724 map
= isl_map_add_basic_map(map
,
7725 isl_basic_map_copy(map1
->p
[i
]));
7729 for (i
= 0; i
< map2
->n
; ++i
) {
7730 map
= isl_map_add_basic_map(map
,
7731 isl_basic_map_copy(map2
->p
[i
]));
7745 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7746 * guaranteed to be disjoint by the caller.
7748 * Note that this functions is called from within isl_map_make_disjoint,
7749 * so we have to be careful not to touch the constraints of the inputs
7752 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7753 __isl_take isl_map
*map2
)
7755 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7758 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7759 * not be disjoint. The parameters are assumed to have been aligned.
7761 * We currently simply call map_union_disjoint, the internal operation
7762 * of which does not really depend on the inputs being disjoint.
7763 * If the result contains more than one basic map, then we clear
7764 * the disjoint flag since the result may contain basic maps from
7765 * both inputs and these are not guaranteed to be disjoint.
7767 * As a special case, if "map1" and "map2" are obviously equal,
7768 * then we simply return "map1".
7770 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7771 __isl_take isl_map
*map2
)
7778 equal
= isl_map_plain_is_equal(map1
, map2
);
7786 map1
= map_union_disjoint(map1
, map2
);
7790 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7798 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7801 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7802 __isl_take isl_map
*map2
)
7804 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7807 __isl_give isl_set
*isl_set_union_disjoint(
7808 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7810 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7814 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7816 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7819 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7822 * "map" and "set" are assumed to be compatible and non-NULL.
7824 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7825 __isl_take isl_set
*set
,
7826 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7827 __isl_take isl_basic_set
*bset
))
7830 struct isl_map
*result
;
7833 if (isl_set_plain_is_universe(set
)) {
7838 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7839 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7840 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7842 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7843 map
->n
* set
->n
, flags
);
7844 for (i
= 0; result
&& i
< map
->n
; ++i
)
7845 for (j
= 0; j
< set
->n
; ++j
) {
7846 result
= isl_map_add_basic_map(result
,
7847 fn(isl_basic_map_copy(map
->p
[i
]),
7848 isl_basic_set_copy(set
->p
[j
])));
7858 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7859 __isl_take isl_set
*set
)
7863 ok
= isl_map_compatible_range(map
, set
);
7867 isl_die(set
->ctx
, isl_error_invalid
,
7868 "incompatible spaces", goto error
);
7870 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7877 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7878 __isl_take isl_set
*set
)
7880 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7883 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7884 __isl_take isl_set
*set
)
7888 ok
= isl_map_compatible_domain(map
, set
);
7892 isl_die(set
->ctx
, isl_error_invalid
,
7893 "incompatible spaces", goto error
);
7895 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7902 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7903 __isl_take isl_set
*set
)
7905 return isl_map_align_params_map_map_and(map
, set
,
7906 &map_intersect_domain
);
7909 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7910 * in the space B -> C, return the intersection.
7911 * The parameters are assumed to have been aligned.
7913 * The map "factor" is first extended to a map living in the space
7914 * [A -> B] -> C and then a regular intersection is computed.
7916 static __isl_give isl_map
*map_intersect_domain_factor_range(
7917 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7920 isl_map
*ext_factor
;
7922 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7923 ext_factor
= isl_map_universe(space
);
7924 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7925 return map_intersect(map
, ext_factor
);
7928 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7929 * in the space B -> C, return the intersection.
7931 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7932 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7934 return isl_map_align_params_map_map_and(map
, factor
,
7935 &map_intersect_domain_factor_range
);
7938 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7939 * in the space A -> C, return the intersection.
7941 * The map "factor" is first extended to a map living in the space
7942 * A -> [B -> C] and then a regular intersection is computed.
7944 static __isl_give isl_map
*map_intersect_range_factor_range(
7945 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7948 isl_map
*ext_factor
;
7950 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7951 ext_factor
= isl_map_universe(space
);
7952 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7953 return isl_map_intersect(map
, ext_factor
);
7956 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7957 * in the space A -> C, return the intersection.
7959 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7960 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7962 return isl_map_align_params_map_map_and(map
, factor
,
7963 &map_intersect_range_factor_range
);
7966 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7967 __isl_take isl_map
*map2
)
7971 map1
= isl_map_reverse(map1
);
7972 map1
= isl_map_apply_range(map1
, map2
);
7973 return isl_map_reverse(map1
);
7980 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7981 __isl_take isl_map
*map2
)
7983 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7986 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7987 __isl_take isl_map
*map2
)
7990 struct isl_map
*result
;
7996 space
= isl_space_join(isl_space_copy(map1
->dim
),
7997 isl_space_copy(map2
->dim
));
7999 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8002 for (i
= 0; i
< map1
->n
; ++i
)
8003 for (j
= 0; j
< map2
->n
; ++j
) {
8004 result
= isl_map_add_basic_map(result
,
8005 isl_basic_map_apply_range(
8006 isl_basic_map_copy(map1
->p
[i
]),
8007 isl_basic_map_copy(map2
->p
[j
])));
8013 if (result
&& result
->n
<= 1)
8014 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8022 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8023 __isl_take isl_map
*map2
)
8025 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8029 * returns range - domain
8031 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8033 isl_space
*target_space
;
8034 struct isl_basic_set
*bset
;
8041 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8042 bmap
->dim
, isl_dim_out
),
8044 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8045 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8046 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8047 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8048 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8049 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8050 for (i
= 0; i
< dim
; ++i
) {
8051 int j
= isl_basic_map_alloc_equality(bmap
);
8053 bmap
= isl_basic_map_free(bmap
);
8056 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8057 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8058 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8059 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8061 bset
= isl_basic_map_domain(bmap
);
8062 bset
= isl_basic_set_reset_space(bset
, target_space
);
8065 isl_basic_map_free(bmap
);
8069 /* Check that domain and range of "map" are the same.
8071 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
8076 space
= isl_map_peek_space(map
);
8077 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8079 return isl_stat_error
;
8081 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8082 "domain and range don't match", return isl_stat_error
);
8087 * returns range - domain
8089 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8093 struct isl_set
*result
;
8098 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8099 map
->dim
, isl_dim_out
),
8101 dim
= isl_map_get_space(map
);
8102 dim
= isl_space_domain(dim
);
8103 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8106 for (i
= 0; i
< map
->n
; ++i
)
8107 result
= isl_set_add_basic_set(result
,
8108 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8117 * returns [domain -> range] -> range - domain
8119 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8120 __isl_take isl_basic_map
*bmap
)
8124 isl_basic_map
*domain
;
8128 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8129 bmap
->dim
, isl_dim_out
))
8130 isl_die(bmap
->ctx
, isl_error_invalid
,
8131 "domain and range don't match", goto error
);
8133 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8134 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8136 space
= isl_basic_map_get_space(bmap
);
8137 space
= isl_space_from_range(isl_space_domain(space
));
8138 domain
= isl_basic_map_universe(space
);
8140 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8141 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8142 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8144 total
= isl_basic_map_total_dim(bmap
);
8146 for (i
= 0; i
< n
; ++i
) {
8147 k
= isl_basic_map_alloc_equality(bmap
);
8150 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8151 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8152 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8153 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8156 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8157 return isl_basic_map_finalize(bmap
);
8159 isl_basic_map_free(bmap
);
8164 * returns [domain -> range] -> range - domain
8166 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8169 isl_space
*domain_space
;
8171 if (isl_map_check_equal_tuples(map
) < 0)
8172 return isl_map_free(map
);
8174 map
= isl_map_cow(map
);
8178 domain_space
= isl_space_domain(isl_map_get_space(map
));
8179 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8180 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8183 for (i
= 0; i
< map
->n
; ++i
) {
8184 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8188 map
= isl_map_unmark_normalized(map
);
8195 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8197 unsigned n_in
, n_out
;
8201 n_in
= isl_space_dim(space
, isl_dim_in
);
8202 n_out
= isl_space_dim(space
, isl_dim_out
);
8204 isl_die(space
->ctx
, isl_error_invalid
,
8205 "number of input and output dimensions needs to be "
8206 "the same", goto error
);
8207 return isl_basic_map_equal(space
, n_in
);
8209 isl_space_free(space
);
8213 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8215 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8218 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8220 isl_space
*dim
= isl_set_get_space(set
);
8222 id
= isl_map_identity(isl_space_map_from_set(dim
));
8223 return isl_map_intersect_range(id
, set
);
8226 /* Construct a basic set with all set dimensions having only non-negative
8229 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8230 __isl_take isl_space
*space
)
8235 struct isl_basic_set
*bset
;
8239 nparam
= space
->nparam
;
8241 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8244 for (i
= 0; i
< dim
; ++i
) {
8245 int k
= isl_basic_set_alloc_inequality(bset
);
8248 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8249 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8253 isl_basic_set_free(bset
);
8257 /* Construct the half-space x_pos >= 0.
8259 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8263 isl_basic_set
*nonneg
;
8265 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8266 k
= isl_basic_set_alloc_inequality(nonneg
);
8269 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8270 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8272 return isl_basic_set_finalize(nonneg
);
8274 isl_basic_set_free(nonneg
);
8278 /* Construct the half-space x_pos <= -1.
8280 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8286 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8287 k
= isl_basic_set_alloc_inequality(neg
);
8290 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8291 isl_int_set_si(neg
->ineq
[k
][0], -1);
8292 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8294 return isl_basic_set_finalize(neg
);
8296 isl_basic_set_free(neg
);
8300 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8301 enum isl_dim_type type
, unsigned first
, unsigned n
)
8305 isl_basic_set
*nonneg
;
8311 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8312 return isl_set_free(set
);
8314 offset
= pos(set
->dim
, type
);
8315 for (i
= 0; i
< n
; ++i
) {
8316 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8317 offset
+ first
+ i
);
8318 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8320 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8326 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8328 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8334 return isl_stat_error
;
8335 if (isl_set_plain_is_empty(set
)) {
8340 return fn(set
, signs
, user
);
8343 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8345 half
= isl_set_intersect(half
, isl_set_copy(set
));
8346 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8350 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8352 half
= isl_set_intersect(half
, set
);
8353 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8356 return isl_stat_error
;
8359 /* Call "fn" on the intersections of "set" with each of the orthants
8360 * (except for obviously empty intersections). The orthant is identified
8361 * by the signs array, with each entry having value 1 or -1 according
8362 * to the sign of the corresponding variable.
8364 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8365 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8374 return isl_stat_error
;
8375 if (isl_set_plain_is_empty(set
))
8378 nparam
= isl_set_dim(set
, isl_dim_param
);
8379 nvar
= isl_set_dim(set
, isl_dim_set
);
8381 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8383 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8391 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8393 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8396 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8397 __isl_keep isl_basic_map
*bmap2
)
8400 struct isl_map
*map1
;
8401 struct isl_map
*map2
;
8403 if (!bmap1
|| !bmap2
)
8404 return isl_bool_error
;
8406 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8407 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8409 is_subset
= isl_map_is_subset(map1
, map2
);
8417 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8418 __isl_keep isl_basic_set
*bset2
)
8420 return isl_basic_map_is_subset(bset1
, bset2
);
8423 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8424 __isl_keep isl_basic_map
*bmap2
)
8428 if (!bmap1
|| !bmap2
)
8429 return isl_bool_error
;
8430 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8431 if (is_subset
!= isl_bool_true
)
8433 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8437 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8438 __isl_keep isl_basic_set
*bset2
)
8440 return isl_basic_map_is_equal(
8441 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8444 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8450 return isl_bool_error
;
8451 for (i
= 0; i
< map
->n
; ++i
) {
8452 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8454 return isl_bool_error
;
8456 return isl_bool_false
;
8458 return isl_bool_true
;
8461 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8463 return map
? map
->n
== 0 : isl_bool_error
;
8466 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8468 return set
? set
->n
== 0 : isl_bool_error
;
8471 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8473 return isl_map_is_empty(set_to_map(set
));
8476 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8477 __isl_keep isl_map
*map2
)
8480 return isl_bool_error
;
8482 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8485 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8486 __isl_keep isl_set
*set2
)
8489 return isl_bool_error
;
8491 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8494 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8499 return isl_bool_error
;
8500 is_subset
= isl_map_is_subset(map1
, map2
);
8501 if (is_subset
!= isl_bool_true
)
8503 is_subset
= isl_map_is_subset(map2
, map1
);
8507 /* Is "map1" equal to "map2"?
8509 * First check if they are obviously equal.
8510 * If not, then perform a more detailed analysis.
8512 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8516 equal
= isl_map_plain_is_equal(map1
, map2
);
8517 if (equal
< 0 || equal
)
8519 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8522 isl_bool
isl_basic_map_is_strict_subset(
8523 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8527 if (!bmap1
|| !bmap2
)
8528 return isl_bool_error
;
8529 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8530 if (is_subset
!= isl_bool_true
)
8532 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8533 return isl_bool_not(is_subset
);
8536 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8537 __isl_keep isl_map
*map2
)
8542 return isl_bool_error
;
8543 is_subset
= isl_map_is_subset(map1
, map2
);
8544 if (is_subset
!= isl_bool_true
)
8546 is_subset
= isl_map_is_subset(map2
, map1
);
8547 return isl_bool_not(is_subset
);
8550 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8551 __isl_keep isl_set
*set2
)
8553 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8556 /* Is "bmap" obviously equal to the universe with the same space?
8558 * That is, does it not have any constraints?
8560 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8563 return isl_bool_error
;
8564 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8567 /* Is "bset" obviously equal to the universe with the same space?
8569 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8571 return isl_basic_map_plain_is_universe(bset
);
8574 /* If "c" does not involve any existentially quantified variables,
8575 * then set *univ to false and abort
8577 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8579 isl_bool
*univ
= user
;
8582 n
= isl_constraint_dim(c
, isl_dim_div
);
8583 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8584 isl_constraint_free(c
);
8585 if (*univ
< 0 || !*univ
)
8586 return isl_stat_error
;
8590 /* Is "bmap" equal to the universe with the same space?
8592 * First check if it is obviously equal to the universe.
8593 * If not and if there are any constraints not involving
8594 * existentially quantified variables, then it is certainly
8595 * not equal to the universe.
8596 * Otherwise, check if the universe is a subset of "bmap".
8598 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8601 isl_basic_map
*test
;
8603 univ
= isl_basic_map_plain_is_universe(bmap
);
8604 if (univ
< 0 || univ
)
8606 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8607 return isl_bool_false
;
8608 univ
= isl_bool_true
;
8609 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8611 return isl_bool_error
;
8612 if (univ
< 0 || !univ
)
8614 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8615 univ
= isl_basic_map_is_subset(test
, bmap
);
8616 isl_basic_map_free(test
);
8620 /* Is "bset" equal to the universe with the same space?
8622 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8624 return isl_basic_map_is_universe(bset
);
8627 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8632 return isl_bool_error
;
8634 for (i
= 0; i
< map
->n
; ++i
) {
8635 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8640 return isl_bool_false
;
8643 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8645 return isl_map_plain_is_universe(set_to_map(set
));
8648 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8650 struct isl_basic_set
*bset
= NULL
;
8651 struct isl_vec
*sample
= NULL
;
8652 isl_bool empty
, non_empty
;
8655 return isl_bool_error
;
8657 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8658 return isl_bool_true
;
8660 if (isl_basic_map_plain_is_universe(bmap
))
8661 return isl_bool_false
;
8663 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8664 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8665 copy
= isl_basic_map_remove_redundancies(copy
);
8666 empty
= isl_basic_map_plain_is_empty(copy
);
8667 isl_basic_map_free(copy
);
8671 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8673 return isl_bool_error
;
8675 return isl_bool_false
;
8676 isl_vec_free(bmap
->sample
);
8677 bmap
->sample
= NULL
;
8678 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8680 return isl_bool_error
;
8681 sample
= isl_basic_set_sample_vec(bset
);
8683 return isl_bool_error
;
8684 empty
= sample
->size
== 0;
8685 isl_vec_free(bmap
->sample
);
8686 bmap
->sample
= sample
;
8688 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8693 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8696 return isl_bool_error
;
8697 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8700 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8703 return isl_bool_error
;
8704 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8707 /* Is "bmap" known to be non-empty?
8709 * That is, is the cached sample still valid?
8711 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8716 return isl_bool_error
;
8718 return isl_bool_false
;
8719 total
= 1 + isl_basic_map_total_dim(bmap
);
8720 if (bmap
->sample
->size
!= total
)
8721 return isl_bool_false
;
8722 return isl_basic_map_contains(bmap
, bmap
->sample
);
8725 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8727 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8730 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8731 __isl_take isl_basic_map
*bmap2
)
8733 struct isl_map
*map
;
8734 if (!bmap1
|| !bmap2
)
8737 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8739 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8742 map
= isl_map_add_basic_map(map
, bmap1
);
8743 map
= isl_map_add_basic_map(map
, bmap2
);
8746 isl_basic_map_free(bmap1
);
8747 isl_basic_map_free(bmap2
);
8751 struct isl_set
*isl_basic_set_union(
8752 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8754 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8755 bset_to_bmap(bset2
)));
8758 /* Order divs such that any div only depends on previous divs */
8759 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8760 __isl_take isl_basic_map
*bmap
)
8768 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8770 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8772 if (isl_int_is_zero(bmap
->div
[i
][0]))
8774 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8779 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8780 "integer division depends on itself",
8781 return isl_basic_map_free(bmap
));
8782 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8790 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8792 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8795 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8802 for (i
= 0; i
< map
->n
; ++i
) {
8803 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8814 /* Sort the local variables of "bset".
8816 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8817 __isl_take isl_basic_set
*bset
)
8819 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8822 /* Apply the expansion computed by isl_merge_divs.
8823 * The expansion itself is given by "exp" while the resulting
8824 * list of divs is given by "div".
8826 * Move the integer divisions of "bmap" into the right position
8827 * according to "exp" and then introduce the additional integer
8828 * divisions, adding div constraints.
8829 * The moving should be done first to avoid moving coefficients
8830 * in the definitions of the extra integer divisions.
8832 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8833 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8838 bmap
= isl_basic_map_cow(bmap
);
8842 if (div
->n_row
< bmap
->n_div
)
8843 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8844 "not an expansion", goto error
);
8846 n_div
= bmap
->n_div
;
8847 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8848 div
->n_row
- n_div
, 0,
8849 2 * (div
->n_row
- n_div
));
8851 for (i
= n_div
; i
< div
->n_row
; ++i
)
8852 if (isl_basic_map_alloc_div(bmap
) < 0)
8855 for (j
= n_div
- 1; j
>= 0; --j
) {
8858 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8863 for (i
= 0; i
< div
->n_row
; ++i
) {
8864 if (j
< n_div
&& exp
[j
] == i
) {
8867 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8868 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8870 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8878 isl_basic_map_free(bmap
);
8883 /* Apply the expansion computed by isl_merge_divs.
8884 * The expansion itself is given by "exp" while the resulting
8885 * list of divs is given by "div".
8887 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8888 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8890 return isl_basic_map_expand_divs(bset
, div
, exp
);
8893 /* Look for a div in dst that corresponds to the div "div" in src.
8894 * The divs before "div" in src and dst are assumed to be the same.
8896 * Returns -1 if no corresponding div was found and the position
8897 * of the corresponding div in dst otherwise.
8899 static int find_div(__isl_keep isl_basic_map
*dst
,
8900 __isl_keep isl_basic_map
*src
, unsigned div
)
8904 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8906 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8907 for (i
= div
; i
< dst
->n_div
; ++i
)
8908 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8909 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8910 dst
->n_div
- div
) == -1)
8915 /* Align the divs of "dst" to those of "src", adding divs from "src"
8916 * if needed. That is, make sure that the first src->n_div divs
8917 * of the result are equal to those of src.
8919 * The result is not finalized as by design it will have redundant
8920 * divs if any divs from "src" were copied.
8922 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8923 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8931 return isl_basic_map_free(dst
);
8933 if (src
->n_div
== 0)
8936 known
= isl_basic_map_divs_known(src
);
8938 return isl_basic_map_free(dst
);
8940 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8941 "some src divs are unknown",
8942 return isl_basic_map_free(dst
));
8944 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8946 return isl_basic_map_free(dst
);
8949 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8950 for (i
= 0; i
< src
->n_div
; ++i
) {
8951 int j
= find_div(dst
, src
, i
);
8954 int extra
= src
->n_div
- i
;
8955 dst
= isl_basic_map_cow(dst
);
8958 dst
= isl_basic_map_extend_space(dst
,
8959 isl_space_copy(dst
->dim
),
8960 extra
, 0, 2 * extra
);
8963 j
= isl_basic_map_alloc_div(dst
);
8966 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8967 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8968 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8972 dst
= isl_basic_map_swap_div(dst
, i
, j
);
8976 isl_basic_map_free(src
);
8979 isl_basic_map_free(src
);
8980 isl_basic_map_free(dst
);
8984 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8992 map
= isl_map_compute_divs(map
);
8993 map
= isl_map_cow(map
);
8997 for (i
= 1; i
< map
->n
; ++i
)
8998 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8999 for (i
= 1; i
< map
->n
; ++i
) {
9000 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9002 return isl_map_free(map
);
9005 map
= isl_map_unmark_normalized(map
);
9009 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9011 return isl_map_align_divs_internal(map
);
9014 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
9016 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9019 /* Align the divs of the basic maps in "map" to those
9020 * of the basic maps in "list", as well as to the other basic maps in "map".
9021 * The elements in "list" are assumed to have known divs.
9023 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9024 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9028 map
= isl_map_compute_divs(map
);
9029 map
= isl_map_cow(map
);
9031 return isl_map_free(map
);
9035 n
= isl_basic_map_list_n_basic_map(list
);
9036 for (i
= 0; i
< n
; ++i
) {
9037 isl_basic_map
*bmap
;
9039 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9040 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9041 isl_basic_map_free(bmap
);
9044 return isl_map_free(map
);
9046 return isl_map_align_divs_internal(map
);
9049 /* Align the divs of each element of "list" to those of "bmap".
9050 * Both "bmap" and the elements of "list" are assumed to have known divs.
9052 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9053 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9058 return isl_basic_map_list_free(list
);
9060 n
= isl_basic_map_list_n_basic_map(list
);
9061 for (i
= 0; i
< n
; ++i
) {
9062 isl_basic_map
*bmap_i
;
9064 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9065 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9066 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9072 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9073 __isl_take isl_map
*map
)
9077 ok
= isl_map_compatible_domain(map
, set
);
9081 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9082 "incompatible spaces", goto error
);
9083 map
= isl_map_intersect_domain(map
, set
);
9084 set
= isl_map_range(map
);
9092 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9093 __isl_take isl_map
*map
)
9095 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9098 /* There is no need to cow as removing empty parts doesn't change
9099 * the meaning of the set.
9101 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9108 for (i
= map
->n
- 1; i
>= 0; --i
)
9109 map
= remove_if_empty(map
, i
);
9114 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9116 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9119 /* Create a binary relation that maps the shared initial "pos" dimensions
9120 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9122 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9123 __isl_keep isl_basic_set
*bset2
, int pos
)
9125 isl_basic_map
*bmap1
;
9126 isl_basic_map
*bmap2
;
9128 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9129 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9130 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9131 isl_dim_out
, 0, pos
);
9132 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9133 isl_dim_out
, 0, pos
);
9134 return isl_basic_map_range_product(bmap1
, bmap2
);
9137 /* Given two basic sets bset1 and bset2, compute the maximal difference
9138 * between the values of dimension pos in bset1 and those in bset2
9139 * for any common value of the parameters and dimensions preceding pos.
9141 static enum isl_lp_result
basic_set_maximal_difference_at(
9142 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9143 int pos
, isl_int
*opt
)
9145 isl_basic_map
*bmap1
;
9146 struct isl_ctx
*ctx
;
9147 struct isl_vec
*obj
;
9151 enum isl_lp_result res
;
9153 if (!bset1
|| !bset2
)
9154 return isl_lp_error
;
9156 nparam
= isl_basic_set_n_param(bset1
);
9157 dim1
= isl_basic_set_n_dim(bset1
);
9159 bmap1
= join_initial(bset1
, bset2
, pos
);
9161 return isl_lp_error
;
9163 total
= isl_basic_map_total_dim(bmap1
);
9165 obj
= isl_vec_alloc(ctx
, 1 + total
);
9168 isl_seq_clr(obj
->block
.data
, 1 + total
);
9169 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9170 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9171 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9173 isl_basic_map_free(bmap1
);
9177 isl_basic_map_free(bmap1
);
9178 return isl_lp_error
;
9181 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9182 * for any common value of the parameters and dimensions preceding pos
9183 * in both basic sets, the values of dimension pos in bset1 are
9184 * smaller or larger than those in bset2.
9187 * 1 if bset1 follows bset2
9188 * -1 if bset1 precedes bset2
9189 * 0 if bset1 and bset2 are incomparable
9190 * -2 if some error occurred.
9192 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9193 __isl_keep isl_basic_set
*bset2
, int pos
)
9196 enum isl_lp_result res
;
9201 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9203 if (res
== isl_lp_empty
)
9205 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9206 res
== isl_lp_unbounded
)
9208 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9217 /* Given two basic sets bset1 and bset2, check whether
9218 * for any common value of the parameters and dimensions preceding pos
9219 * there is a value of dimension pos in bset1 that is larger
9220 * than a value of the same dimension in bset2.
9223 * 1 if there exists such a pair
9224 * 0 if there is no such pair, but there is a pair of equal values
9226 * -2 if some error occurred.
9228 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9229 __isl_keep isl_basic_set
*bset2
, int pos
)
9232 isl_basic_map
*bmap
;
9235 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9236 bmap
= join_initial(bset1
, bset2
, pos
);
9237 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9238 isl_dim_out
, dim1
- pos
);
9239 empty
= isl_basic_map_is_empty(bmap
);
9243 isl_basic_map_free(bmap
);
9246 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9247 isl_dim_out
, dim1
- pos
);
9248 empty
= isl_basic_map_is_empty(bmap
);
9251 isl_basic_map_free(bmap
);
9256 isl_basic_map_free(bmap
);
9260 /* Given two sets set1 and set2, check whether
9261 * for any common value of the parameters and dimensions preceding pos
9262 * there is a value of dimension pos in set1 that is larger
9263 * than a value of the same dimension in set2.
9266 * 1 if there exists such a pair
9267 * 0 if there is no such pair, but there is a pair of equal values
9269 * -2 if some error occurred.
9271 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9272 __isl_keep isl_set
*set2
, int pos
)
9280 for (i
= 0; i
< set1
->n
; ++i
)
9281 for (j
= 0; j
< set2
->n
; ++j
) {
9283 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9284 if (f
== 1 || f
== -2)
9293 static isl_bool
isl_basic_map_plain_has_fixed_var(
9294 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9301 return isl_bool_error
;
9302 total
= isl_basic_map_total_dim(bmap
);
9303 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9304 for (; d
+1 > pos
; --d
)
9305 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9309 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9310 return isl_bool_false
;
9311 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9312 return isl_bool_false
;
9313 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9314 return isl_bool_false
;
9316 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9317 return isl_bool_true
;
9319 return isl_bool_false
;
9322 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9323 unsigned pos
, isl_int
*val
)
9331 return isl_bool_error
;
9333 return isl_bool_false
;
9335 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9338 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9339 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9340 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9341 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9342 fixed
= isl_bool_false
;
9345 isl_int_set(*val
, v
);
9351 static isl_bool
isl_basic_set_plain_has_fixed_var(
9352 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9354 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9358 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9359 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9361 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9362 return isl_bool_error
;
9363 return isl_basic_map_plain_has_fixed_var(bmap
,
9364 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9367 /* If "bmap" obviously lies on a hyperplane where the given dimension
9368 * has a fixed value, then return that value.
9369 * Otherwise return NaN.
9371 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9372 __isl_keep isl_basic_map
*bmap
,
9373 enum isl_dim_type type
, unsigned pos
)
9381 ctx
= isl_basic_map_get_ctx(bmap
);
9382 v
= isl_val_alloc(ctx
);
9385 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9387 return isl_val_free(v
);
9389 isl_int_set_si(v
->d
, 1);
9393 return isl_val_nan(ctx
);
9396 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9397 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9399 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9400 return isl_bool_error
;
9401 return isl_map_plain_has_fixed_var(map
,
9402 map_offset(map
, type
) - 1 + pos
, val
);
9405 /* If "map" obviously lies on a hyperplane where the given dimension
9406 * has a fixed value, then return that value.
9407 * Otherwise return NaN.
9409 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9410 enum isl_dim_type type
, unsigned pos
)
9418 ctx
= isl_map_get_ctx(map
);
9419 v
= isl_val_alloc(ctx
);
9422 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9424 return isl_val_free(v
);
9426 isl_int_set_si(v
->d
, 1);
9430 return isl_val_nan(ctx
);
9433 /* If "set" obviously lies on a hyperplane where the given dimension
9434 * has a fixed value, then return that value.
9435 * Otherwise return NaN.
9437 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9438 enum isl_dim_type type
, unsigned pos
)
9440 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9443 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9444 * then return this fixed value in *val.
9446 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9447 unsigned dim
, isl_int
*val
)
9449 return isl_basic_set_plain_has_fixed_var(bset
,
9450 isl_basic_set_n_param(bset
) + dim
, val
);
9453 /* Return -1 if the constraint "c1" should be sorted before "c2"
9454 * and 1 if it should be sorted after "c2".
9455 * Return 0 if the two constraints are the same (up to the constant term).
9457 * In particular, if a constraint involves later variables than another
9458 * then it is sorted after this other constraint.
9459 * uset_gist depends on constraints without existentially quantified
9460 * variables sorting first.
9462 * For constraints that have the same latest variable, those
9463 * with the same coefficient for this latest variable (first in absolute value
9464 * and then in actual value) are grouped together.
9465 * This is useful for detecting pairs of constraints that can
9466 * be chained in their printed representation.
9468 * Finally, within a group, constraints are sorted according to
9469 * their coefficients (excluding the constant term).
9471 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9473 isl_int
**c1
= (isl_int
**) p1
;
9474 isl_int
**c2
= (isl_int
**) p2
;
9476 unsigned size
= *(unsigned *) arg
;
9479 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9480 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9485 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9488 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9492 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9495 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9496 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9497 * and 0 if the two constraints are the same (up to the constant term).
9499 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9500 isl_int
*c1
, isl_int
*c2
)
9506 total
= isl_basic_map_total_dim(bmap
);
9507 return sort_constraint_cmp(&c1
, &c2
, &total
);
9510 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9511 __isl_take isl_basic_map
*bmap
)
9517 if (bmap
->n_ineq
== 0)
9519 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9521 total
= isl_basic_map_total_dim(bmap
);
9522 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9523 &sort_constraint_cmp
, &total
) < 0)
9524 return isl_basic_map_free(bmap
);
9525 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9529 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9530 __isl_take isl_basic_set
*bset
)
9532 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9533 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9536 __isl_give isl_basic_map
*isl_basic_map_normalize(
9537 __isl_take isl_basic_map
*bmap
)
9539 bmap
= isl_basic_map_remove_redundancies(bmap
);
9540 bmap
= isl_basic_map_sort_constraints(bmap
);
9543 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9544 __isl_keep isl_basic_map
*bmap2
)
9548 isl_space
*space1
, *space2
;
9550 if (!bmap1
|| !bmap2
)
9555 space1
= isl_basic_map_peek_space(bmap1
);
9556 space2
= isl_basic_map_peek_space(bmap2
);
9557 cmp
= isl_space_cmp(space1
, space2
);
9560 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9561 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9562 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9563 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9564 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9566 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9568 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9570 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9571 return bmap1
->n_eq
- bmap2
->n_eq
;
9572 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9573 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9574 if (bmap1
->n_div
!= bmap2
->n_div
)
9575 return bmap1
->n_div
- bmap2
->n_div
;
9576 total
= isl_basic_map_total_dim(bmap1
);
9577 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9578 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9582 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9583 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9587 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9588 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9595 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9596 __isl_keep isl_basic_set
*bset2
)
9598 return isl_basic_map_plain_cmp(bset1
, bset2
);
9601 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9607 if (set1
->n
!= set2
->n
)
9608 return set1
->n
- set2
->n
;
9610 for (i
= 0; i
< set1
->n
; ++i
) {
9611 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9619 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9620 __isl_keep isl_basic_map
*bmap2
)
9622 if (!bmap1
|| !bmap2
)
9623 return isl_bool_error
;
9624 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9627 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9628 __isl_keep isl_basic_set
*bset2
)
9630 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9631 bset_to_bmap(bset2
));
9634 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9636 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9637 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9639 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9642 /* Sort the basic maps of "map" and remove duplicate basic maps.
9644 * While removing basic maps, we make sure that the basic maps remain
9645 * sorted because isl_map_normalize expects the basic maps of the result
9648 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9652 map
= isl_map_remove_empty_parts(map
);
9655 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9656 for (i
= map
->n
- 1; i
>= 1; --i
) {
9657 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9659 isl_basic_map_free(map
->p
[i
-1]);
9660 for (j
= i
; j
< map
->n
; ++j
)
9661 map
->p
[j
- 1] = map
->p
[j
];
9668 /* Remove obvious duplicates among the basic maps of "map".
9670 * Unlike isl_map_normalize, this function does not remove redundant
9671 * constraints and only removes duplicates that have exactly the same
9672 * constraints in the input. It does sort the constraints and
9673 * the basic maps to ease the detection of duplicates.
9675 * If "map" has already been normalized or if the basic maps are
9676 * disjoint, then there can be no duplicates.
9678 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9681 isl_basic_map
*bmap
;
9687 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9689 for (i
= 0; i
< map
->n
; ++i
) {
9690 bmap
= isl_basic_map_copy(map
->p
[i
]);
9691 bmap
= isl_basic_map_sort_constraints(bmap
);
9693 return isl_map_free(map
);
9694 isl_basic_map_free(map
->p
[i
]);
9698 map
= sort_and_remove_duplicates(map
);
9702 /* We normalize in place, but if anything goes wrong we need
9703 * to return NULL, so we need to make sure we don't change the
9704 * meaning of any possible other copies of map.
9706 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9709 struct isl_basic_map
*bmap
;
9713 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9715 for (i
= 0; i
< map
->n
; ++i
) {
9716 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9719 isl_basic_map_free(map
->p
[i
]);
9723 map
= sort_and_remove_duplicates(map
);
9725 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9732 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9734 return set_from_map(isl_map_normalize(set_to_map(set
)));
9737 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9738 __isl_keep isl_map
*map2
)
9744 return isl_bool_error
;
9747 return isl_bool_true
;
9748 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9749 return isl_bool_false
;
9751 map1
= isl_map_copy(map1
);
9752 map2
= isl_map_copy(map2
);
9753 map1
= isl_map_normalize(map1
);
9754 map2
= isl_map_normalize(map2
);
9757 equal
= map1
->n
== map2
->n
;
9758 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9759 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9769 return isl_bool_error
;
9772 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9773 __isl_keep isl_set
*set2
)
9775 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9778 /* Return the basic maps in "map" as a list.
9780 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9781 __isl_keep isl_map
*map
)
9785 isl_basic_map_list
*list
;
9789 ctx
= isl_map_get_ctx(map
);
9790 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9792 for (i
= 0; i
< map
->n
; ++i
) {
9793 isl_basic_map
*bmap
;
9795 bmap
= isl_basic_map_copy(map
->p
[i
]);
9796 list
= isl_basic_map_list_add(list
, bmap
);
9802 /* Return the intersection of the elements in the non-empty list "list".
9803 * All elements are assumed to live in the same space.
9805 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9806 __isl_take isl_basic_map_list
*list
)
9809 isl_basic_map
*bmap
;
9813 n
= isl_basic_map_list_n_basic_map(list
);
9815 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9816 "expecting non-empty list", goto error
);
9818 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9819 for (i
= 1; i
< n
; ++i
) {
9820 isl_basic_map
*bmap_i
;
9822 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9823 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9826 isl_basic_map_list_free(list
);
9829 isl_basic_map_list_free(list
);
9833 /* Return the intersection of the elements in the non-empty list "list".
9834 * All elements are assumed to live in the same space.
9836 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9837 __isl_take isl_basic_set_list
*list
)
9839 return isl_basic_map_list_intersect(list
);
9842 /* Return the union of the elements of "list".
9843 * The list is required to have at least one element.
9845 __isl_give isl_set
*isl_basic_set_list_union(
9846 __isl_take isl_basic_set_list
*list
)
9850 isl_basic_set
*bset
;
9855 n
= isl_basic_set_list_n_basic_set(list
);
9857 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9858 "expecting non-empty list", goto error
);
9860 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9861 space
= isl_basic_set_get_space(bset
);
9862 isl_basic_set_free(bset
);
9864 set
= isl_set_alloc_space(space
, n
, 0);
9865 for (i
= 0; i
< n
; ++i
) {
9866 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9867 set
= isl_set_add_basic_set(set
, bset
);
9870 isl_basic_set_list_free(list
);
9873 isl_basic_set_list_free(list
);
9877 /* Return the union of the elements in the non-empty list "list".
9878 * All elements are assumed to live in the same space.
9880 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9887 n
= isl_set_list_n_set(list
);
9889 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9890 "expecting non-empty list", goto error
);
9892 set
= isl_set_list_get_set(list
, 0);
9893 for (i
= 1; i
< n
; ++i
) {
9896 set_i
= isl_set_list_get_set(list
, i
);
9897 set
= isl_set_union(set
, set_i
);
9900 isl_set_list_free(list
);
9903 isl_set_list_free(list
);
9907 __isl_give isl_basic_map
*isl_basic_map_product(
9908 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9910 isl_space
*space_result
= NULL
;
9911 struct isl_basic_map
*bmap
;
9912 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9913 struct isl_dim_map
*dim_map1
, *dim_map2
;
9915 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9917 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9918 isl_space_copy(bmap2
->dim
));
9920 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9921 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9922 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9923 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9924 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9926 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9927 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9928 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9929 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9930 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9931 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9932 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9933 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9934 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9935 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9936 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9938 bmap
= isl_basic_map_alloc_space(space_result
,
9939 bmap1
->n_div
+ bmap2
->n_div
,
9940 bmap1
->n_eq
+ bmap2
->n_eq
,
9941 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9942 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9943 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9944 bmap
= isl_basic_map_simplify(bmap
);
9945 return isl_basic_map_finalize(bmap
);
9947 isl_basic_map_free(bmap1
);
9948 isl_basic_map_free(bmap2
);
9952 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9953 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9955 isl_basic_map
*prod
;
9957 prod
= isl_basic_map_product(bmap1
, bmap2
);
9958 prod
= isl_basic_map_flatten(prod
);
9962 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9963 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9965 return isl_basic_map_flat_range_product(bset1
, bset2
);
9968 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9969 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9971 isl_space
*space_result
= NULL
;
9972 isl_basic_map
*bmap
;
9973 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9974 struct isl_dim_map
*dim_map1
, *dim_map2
;
9976 if (!bmap1
|| !bmap2
)
9979 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9980 isl_space_copy(bmap2
->dim
));
9982 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9983 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9984 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9985 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9987 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9988 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9989 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9990 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9991 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9992 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9993 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9994 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9995 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9996 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9997 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9999 bmap
= isl_basic_map_alloc_space(space_result
,
10000 bmap1
->n_div
+ bmap2
->n_div
,
10001 bmap1
->n_eq
+ bmap2
->n_eq
,
10002 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10003 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10004 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10005 bmap
= isl_basic_map_simplify(bmap
);
10006 return isl_basic_map_finalize(bmap
);
10008 isl_basic_map_free(bmap1
);
10009 isl_basic_map_free(bmap2
);
10013 __isl_give isl_basic_map
*isl_basic_map_range_product(
10014 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10017 isl_space
*space_result
= NULL
;
10018 isl_basic_map
*bmap
;
10019 unsigned in
, out1
, out2
, nparam
, total
, pos
;
10020 struct isl_dim_map
*dim_map1
, *dim_map2
;
10022 rational
= isl_basic_map_is_rational(bmap1
);
10023 if (rational
>= 0 && rational
)
10024 rational
= isl_basic_map_is_rational(bmap2
);
10025 if (!bmap1
|| !bmap2
|| rational
< 0)
10028 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10031 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10032 isl_space_copy(bmap2
->dim
));
10034 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10035 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10036 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10037 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10039 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10040 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10041 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10042 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10043 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10044 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10045 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10046 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10047 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10048 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10049 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10051 bmap
= isl_basic_map_alloc_space(space_result
,
10052 bmap1
->n_div
+ bmap2
->n_div
,
10053 bmap1
->n_eq
+ bmap2
->n_eq
,
10054 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10055 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10056 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10058 bmap
= isl_basic_map_set_rational(bmap
);
10059 bmap
= isl_basic_map_simplify(bmap
);
10060 return isl_basic_map_finalize(bmap
);
10062 isl_basic_map_free(bmap1
);
10063 isl_basic_map_free(bmap2
);
10067 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10068 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10070 isl_basic_map
*prod
;
10072 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10073 prod
= isl_basic_map_flatten_range(prod
);
10077 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10078 * and collect the results.
10079 * The result live in the space obtained by calling "space_product"
10080 * on the spaces of "map1" and "map2".
10081 * If "remove_duplicates" is set then the result may contain duplicates
10082 * (even if the inputs do not) and so we try and remove the obvious
10085 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10086 __isl_take isl_map
*map2
,
10087 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10088 __isl_take isl_space
*right
),
10089 __isl_give isl_basic_map
*(*basic_map_product
)(
10090 __isl_take isl_basic_map
*left
,
10091 __isl_take isl_basic_map
*right
),
10092 int remove_duplicates
)
10094 unsigned flags
= 0;
10095 struct isl_map
*result
;
10099 m
= isl_map_has_equal_params(map1
, map2
);
10103 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10104 "parameters don't match", goto error
);
10106 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10107 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10108 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10110 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10111 isl_space_copy(map2
->dim
)),
10112 map1
->n
* map2
->n
, flags
);
10115 for (i
= 0; i
< map1
->n
; ++i
)
10116 for (j
= 0; j
< map2
->n
; ++j
) {
10117 struct isl_basic_map
*part
;
10118 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10119 isl_basic_map_copy(map2
->p
[j
]));
10120 if (isl_basic_map_is_empty(part
))
10121 isl_basic_map_free(part
);
10123 result
= isl_map_add_basic_map(result
, part
);
10127 if (remove_duplicates
)
10128 result
= isl_map_remove_obvious_duplicates(result
);
10129 isl_map_free(map1
);
10130 isl_map_free(map2
);
10133 isl_map_free(map1
);
10134 isl_map_free(map2
);
10138 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10140 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10141 __isl_take isl_map
*map2
)
10143 return map_product(map1
, map2
, &isl_space_product
,
10144 &isl_basic_map_product
, 0);
10147 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10148 __isl_take isl_map
*map2
)
10150 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10153 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10155 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10156 __isl_take isl_map
*map2
)
10160 prod
= isl_map_product(map1
, map2
);
10161 prod
= isl_map_flatten(prod
);
10165 /* Given two set A and B, construct its Cartesian product A x B.
10167 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10169 return isl_map_range_product(set1
, set2
);
10172 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10173 __isl_take isl_set
*set2
)
10175 return isl_map_flat_range_product(set1
, set2
);
10178 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10180 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10181 __isl_take isl_map
*map2
)
10183 return map_product(map1
, map2
, &isl_space_domain_product
,
10184 &isl_basic_map_domain_product
, 1);
10187 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10189 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10190 __isl_take isl_map
*map2
)
10192 return map_product(map1
, map2
, &isl_space_range_product
,
10193 &isl_basic_map_range_product
, 1);
10196 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10197 __isl_take isl_map
*map2
)
10199 return isl_map_align_params_map_map_and(map1
, map2
,
10200 &map_domain_product_aligned
);
10203 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10204 __isl_take isl_map
*map2
)
10206 return isl_map_align_params_map_map_and(map1
, map2
,
10207 &map_range_product_aligned
);
10210 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10212 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10215 int total1
, keep1
, total2
, keep2
;
10219 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10220 !isl_space_range_is_wrapping(map
->dim
))
10221 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10222 "not a product", return isl_map_free(map
));
10224 space
= isl_map_get_space(map
);
10225 total1
= isl_space_dim(space
, isl_dim_in
);
10226 total2
= isl_space_dim(space
, isl_dim_out
);
10227 space
= isl_space_factor_domain(space
);
10228 keep1
= isl_space_dim(space
, isl_dim_in
);
10229 keep2
= isl_space_dim(space
, isl_dim_out
);
10230 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10231 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10232 map
= isl_map_reset_space(map
, space
);
10237 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10239 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10242 int total1
, keep1
, total2
, keep2
;
10246 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10247 !isl_space_range_is_wrapping(map
->dim
))
10248 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10249 "not a product", return isl_map_free(map
));
10251 space
= isl_map_get_space(map
);
10252 total1
= isl_space_dim(space
, isl_dim_in
);
10253 total2
= isl_space_dim(space
, isl_dim_out
);
10254 space
= isl_space_factor_range(space
);
10255 keep1
= isl_space_dim(space
, isl_dim_in
);
10256 keep2
= isl_space_dim(space
, isl_dim_out
);
10257 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10258 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10259 map
= isl_map_reset_space(map
, space
);
10264 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10266 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10273 if (!isl_space_domain_is_wrapping(map
->dim
))
10274 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10275 "domain is not a product", return isl_map_free(map
));
10277 space
= isl_map_get_space(map
);
10278 total
= isl_space_dim(space
, isl_dim_in
);
10279 space
= isl_space_domain_factor_domain(space
);
10280 keep
= isl_space_dim(space
, isl_dim_in
);
10281 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10282 map
= isl_map_reset_space(map
, space
);
10287 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10289 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10296 if (!isl_space_domain_is_wrapping(map
->dim
))
10297 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10298 "domain is not a product", return isl_map_free(map
));
10300 space
= isl_map_get_space(map
);
10301 total
= isl_space_dim(space
, isl_dim_in
);
10302 space
= isl_space_domain_factor_range(space
);
10303 keep
= isl_space_dim(space
, isl_dim_in
);
10304 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10305 map
= isl_map_reset_space(map
, space
);
10310 /* Given a map A -> [B -> C], extract the map A -> B.
10312 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10319 if (!isl_space_range_is_wrapping(map
->dim
))
10320 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10321 "range is not a product", return isl_map_free(map
));
10323 space
= isl_map_get_space(map
);
10324 total
= isl_space_dim(space
, isl_dim_out
);
10325 space
= isl_space_range_factor_domain(space
);
10326 keep
= isl_space_dim(space
, isl_dim_out
);
10327 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10328 map
= isl_map_reset_space(map
, space
);
10333 /* Given a map A -> [B -> C], extract the map A -> C.
10335 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10342 if (!isl_space_range_is_wrapping(map
->dim
))
10343 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10344 "range is not a product", return isl_map_free(map
));
10346 space
= isl_map_get_space(map
);
10347 total
= isl_space_dim(space
, isl_dim_out
);
10348 space
= isl_space_range_factor_range(space
);
10349 keep
= isl_space_dim(space
, isl_dim_out
);
10350 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10351 map
= isl_map_reset_space(map
, space
);
10356 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10358 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10359 __isl_take isl_map
*map2
)
10363 prod
= isl_map_domain_product(map1
, map2
);
10364 prod
= isl_map_flatten_domain(prod
);
10368 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10370 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10371 __isl_take isl_map
*map2
)
10375 prod
= isl_map_range_product(map1
, map2
);
10376 prod
= isl_map_flatten_range(prod
);
10380 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10383 uint32_t hash
= isl_hash_init();
10388 bmap
= isl_basic_map_copy(bmap
);
10389 bmap
= isl_basic_map_normalize(bmap
);
10392 total
= isl_basic_map_total_dim(bmap
);
10393 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10394 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10396 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10397 isl_hash_hash(hash
, c_hash
);
10399 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10400 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10402 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10403 isl_hash_hash(hash
, c_hash
);
10405 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10406 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10408 if (isl_int_is_zero(bmap
->div
[i
][0]))
10410 isl_hash_byte(hash
, i
& 0xFF);
10411 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10412 isl_hash_hash(hash
, c_hash
);
10414 isl_basic_map_free(bmap
);
10418 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10420 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10423 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10430 map
= isl_map_copy(map
);
10431 map
= isl_map_normalize(map
);
10435 hash
= isl_hash_init();
10436 for (i
= 0; i
< map
->n
; ++i
) {
10437 uint32_t bmap_hash
;
10438 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10439 isl_hash_hash(hash
, bmap_hash
);
10447 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10449 return isl_map_get_hash(set_to_map(set
));
10452 /* Return the number of basic maps in the (current) representation of "map".
10454 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10456 return map
? map
->n
: 0;
10459 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10461 return set
? set
->n
: 0;
10464 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10465 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10470 return isl_stat_error
;
10472 for (i
= 0; i
< map
->n
; ++i
)
10473 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10474 return isl_stat_error
;
10476 return isl_stat_ok
;
10479 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10480 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10485 return isl_stat_error
;
10487 for (i
= 0; i
< set
->n
; ++i
)
10488 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10489 return isl_stat_error
;
10491 return isl_stat_ok
;
10494 /* Return a list of basic sets, the union of which is equal to "set".
10496 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10497 __isl_keep isl_set
*set
)
10500 isl_basic_set_list
*list
;
10505 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10506 for (i
= 0; i
< set
->n
; ++i
) {
10507 isl_basic_set
*bset
;
10509 bset
= isl_basic_set_copy(set
->p
[i
]);
10510 list
= isl_basic_set_list_add(list
, bset
);
10516 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10523 bset
= isl_basic_set_cow(bset
);
10527 space
= isl_basic_set_get_space(bset
);
10528 space
= isl_space_lift(space
, bset
->n_div
);
10531 isl_space_free(bset
->dim
);
10533 bset
->extra
-= bset
->n_div
;
10536 bset
= isl_basic_set_finalize(bset
);
10540 isl_basic_set_free(bset
);
10544 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10550 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10555 set
= isl_set_cow(set
);
10559 n_div
= set
->p
[0]->n_div
;
10560 space
= isl_set_get_space(set
);
10561 space
= isl_space_lift(space
, n_div
);
10564 isl_space_free(set
->dim
);
10567 for (i
= 0; i
< set
->n
; ++i
) {
10568 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10579 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10587 dim
= isl_basic_set_total_dim(bset
);
10588 size
+= bset
->n_eq
* (1 + dim
);
10589 size
+= bset
->n_ineq
* (1 + dim
);
10590 size
+= bset
->n_div
* (2 + dim
);
10595 int isl_set_size(__isl_keep isl_set
*set
)
10603 for (i
= 0; i
< set
->n
; ++i
)
10604 size
+= isl_basic_set_size(set
->p
[i
]);
10609 /* Check if there is any lower bound (if lower == 0) and/or upper
10610 * bound (if upper == 0) on the specified dim.
10612 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10613 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10617 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10618 return isl_bool_error
;
10620 pos
+= isl_basic_map_offset(bmap
, type
);
10622 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10623 if (isl_int_is_zero(bmap
->div
[i
][0]))
10625 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10626 return isl_bool_true
;
10629 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10630 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10631 return isl_bool_true
;
10633 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10634 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10641 return lower
&& upper
;
10644 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10645 enum isl_dim_type type
, unsigned pos
)
10647 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10650 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10651 enum isl_dim_type type
, unsigned pos
)
10653 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10656 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10657 enum isl_dim_type type
, unsigned pos
)
10659 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10662 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10663 enum isl_dim_type type
, unsigned pos
)
10668 return isl_bool_error
;
10670 for (i
= 0; i
< map
->n
; ++i
) {
10672 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10673 if (bounded
< 0 || !bounded
)
10677 return isl_bool_true
;
10680 /* Return true if the specified dim is involved in both an upper bound
10681 * and a lower bound.
10683 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10684 enum isl_dim_type type
, unsigned pos
)
10686 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10689 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10691 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10692 enum isl_dim_type type
, unsigned pos
,
10693 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10694 enum isl_dim_type type
, unsigned pos
))
10699 return isl_bool_error
;
10701 for (i
= 0; i
< map
->n
; ++i
) {
10703 bounded
= fn(map
->p
[i
], type
, pos
);
10704 if (bounded
< 0 || bounded
)
10708 return isl_bool_false
;
10711 /* Return 1 if the specified dim is involved in any lower bound.
10713 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10714 enum isl_dim_type type
, unsigned pos
)
10716 return has_any_bound(set
, type
, pos
,
10717 &isl_basic_map_dim_has_lower_bound
);
10720 /* Return 1 if the specified dim is involved in any upper bound.
10722 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10723 enum isl_dim_type type
, unsigned pos
)
10725 return has_any_bound(set
, type
, pos
,
10726 &isl_basic_map_dim_has_upper_bound
);
10729 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10731 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10732 enum isl_dim_type type
, unsigned pos
,
10733 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10734 enum isl_dim_type type
, unsigned pos
))
10739 return isl_bool_error
;
10741 for (i
= 0; i
< map
->n
; ++i
) {
10743 bounded
= fn(map
->p
[i
], type
, pos
);
10744 if (bounded
< 0 || !bounded
)
10748 return isl_bool_true
;
10751 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10753 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10754 enum isl_dim_type type
, unsigned pos
)
10756 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10759 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10761 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10762 enum isl_dim_type type
, unsigned pos
)
10764 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10767 /* For each of the "n" variables starting at "first", determine
10768 * the sign of the variable and put the results in the first "n"
10769 * elements of the array "signs".
10771 * 1 means that the variable is non-negative
10772 * -1 means that the variable is non-positive
10773 * 0 means the variable attains both positive and negative values.
10775 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10776 unsigned first
, unsigned n
, int *signs
)
10778 isl_vec
*bound
= NULL
;
10779 struct isl_tab
*tab
= NULL
;
10780 struct isl_tab_undo
*snap
;
10783 if (!bset
|| !signs
)
10784 return isl_stat_error
;
10786 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10787 tab
= isl_tab_from_basic_set(bset
, 0);
10788 if (!bound
|| !tab
)
10791 isl_seq_clr(bound
->el
, bound
->size
);
10792 isl_int_set_si(bound
->el
[0], -1);
10794 snap
= isl_tab_snap(tab
);
10795 for (i
= 0; i
< n
; ++i
) {
10798 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10799 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10801 empty
= tab
->empty
;
10802 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10803 if (isl_tab_rollback(tab
, snap
) < 0)
10811 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10812 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10814 empty
= tab
->empty
;
10815 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10816 if (isl_tab_rollback(tab
, snap
) < 0)
10819 signs
[i
] = empty
? -1 : 0;
10823 isl_vec_free(bound
);
10824 return isl_stat_ok
;
10827 isl_vec_free(bound
);
10828 return isl_stat_error
;
10831 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10832 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10834 if (!bset
|| !signs
)
10835 return isl_stat_error
;
10836 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10837 return isl_stat_error
;
10839 first
+= pos(bset
->dim
, type
) - 1;
10840 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10843 /* Is it possible for the integer division "div" to depend (possibly
10844 * indirectly) on any output dimensions?
10846 * If the div is undefined, then we conservatively assume that it
10847 * may depend on them.
10848 * Otherwise, we check if it actually depends on them or on any integer
10849 * divisions that may depend on them.
10851 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10854 unsigned n_out
, o_out
;
10855 unsigned n_div
, o_div
;
10857 if (isl_int_is_zero(bmap
->div
[div
][0]))
10858 return isl_bool_true
;
10860 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10861 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10863 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10864 return isl_bool_true
;
10866 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10867 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10869 for (i
= 0; i
< n_div
; ++i
) {
10870 isl_bool may_involve
;
10872 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10874 may_involve
= div_may_involve_output(bmap
, i
);
10875 if (may_involve
< 0 || may_involve
)
10876 return may_involve
;
10879 return isl_bool_false
;
10882 /* Return the first integer division of "bmap" in the range
10883 * [first, first + n[ that may depend on any output dimensions and
10884 * that has a non-zero coefficient in "c" (where the first coefficient
10885 * in "c" corresponds to integer division "first").
10887 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10888 isl_int
*c
, int first
, int n
)
10895 for (k
= first
; k
< first
+ n
; ++k
) {
10896 isl_bool may_involve
;
10898 if (isl_int_is_zero(c
[k
]))
10900 may_involve
= div_may_involve_output(bmap
, k
);
10901 if (may_involve
< 0)
10910 /* Look for a pair of inequality constraints in "bmap" of the form
10912 * -l + i >= 0 or i >= l
10914 * n + l - i >= 0 or i <= l + n
10916 * with n < "m" and i the output dimension at position "pos".
10917 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10918 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10919 * and earlier output dimensions, as well as integer divisions that do
10920 * not involve any of the output dimensions.
10922 * Return the index of the first inequality constraint or bmap->n_ineq
10923 * if no such pair can be found.
10925 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10926 int pos
, isl_int m
)
10931 unsigned n_div
, o_div
;
10932 unsigned n_out
, o_out
;
10938 ctx
= isl_basic_map_get_ctx(bmap
);
10939 total
= isl_basic_map_total_dim(bmap
);
10940 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10941 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10942 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10943 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10944 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10945 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10947 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10948 n_out
- (pos
+ 1)) != -1)
10950 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10953 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10954 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10957 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10958 bmap
->ineq
[j
] + 1, total
))
10962 if (j
>= bmap
->n_ineq
)
10964 isl_int_add(bmap
->ineq
[i
][0],
10965 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10966 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10967 isl_int_sub(bmap
->ineq
[i
][0],
10968 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10971 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10977 return bmap
->n_ineq
;
10980 /* Return the index of the equality of "bmap" that defines
10981 * the output dimension "pos" in terms of earlier dimensions.
10982 * The equality may also involve integer divisions, as long
10983 * as those integer divisions are defined in terms of
10984 * parameters or input dimensions.
10985 * In this case, *div is set to the number of integer divisions and
10986 * *ineq is set to the number of inequality constraints (provided
10987 * div and ineq are not NULL).
10989 * The equality may also involve a single integer division involving
10990 * the output dimensions (typically only output dimension "pos") as
10991 * long as the coefficient of output dimension "pos" is 1 or -1 and
10992 * there is a pair of constraints i >= l and i <= l + n, with i referring
10993 * to output dimension "pos", l an expression involving only earlier
10994 * dimensions and n smaller than the coefficient of the integer division
10995 * in the equality. In this case, the output dimension can be defined
10996 * in terms of a modulo expression that does not involve the integer division.
10997 * *div is then set to this single integer division and
10998 * *ineq is set to the index of constraint i >= l.
11000 * Return bmap->n_eq if there is no such equality.
11001 * Return -1 on error.
11003 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11004 int pos
, int *div
, int *ineq
)
11007 unsigned n_out
, o_out
;
11008 unsigned n_div
, o_div
;
11013 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11014 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11015 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11016 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11019 *ineq
= bmap
->n_ineq
;
11022 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11023 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11025 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11026 n_out
- (pos
+ 1)) != -1)
11028 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11032 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11033 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11035 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11036 k
+ 1, n_div
- (k
+1)) < n_div
)
11038 l
= find_modulo_constraint_pair(bmap
, pos
,
11039 bmap
->eq
[j
][o_div
+ k
]);
11042 if (l
>= bmap
->n_ineq
)
11054 /* Check if the given basic map is obviously single-valued.
11055 * In particular, for each output dimension, check that there is
11056 * an equality that defines the output dimension in terms of
11057 * earlier dimensions.
11059 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11065 return isl_bool_error
;
11067 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11069 for (i
= 0; i
< n_out
; ++i
) {
11072 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11075 return isl_bool_error
;
11076 if (eq
>= bmap
->n_eq
)
11077 return isl_bool_false
;
11080 return isl_bool_true
;
11083 /* Check if the given basic map is single-valued.
11084 * We simply compute
11088 * and check if the result is a subset of the identity mapping.
11090 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11093 isl_basic_map
*test
;
11097 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11101 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11102 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11104 space
= isl_basic_map_get_space(bmap
);
11105 space
= isl_space_map_from_set(isl_space_range(space
));
11106 id
= isl_basic_map_identity(space
);
11108 sv
= isl_basic_map_is_subset(test
, id
);
11110 isl_basic_map_free(test
);
11111 isl_basic_map_free(id
);
11116 /* Check if the given map is obviously single-valued.
11118 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11121 return isl_bool_error
;
11123 return isl_bool_true
;
11125 return isl_bool_false
;
11127 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11130 /* Check if the given map is single-valued.
11131 * We simply compute
11135 * and check if the result is a subset of the identity mapping.
11137 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11144 sv
= isl_map_plain_is_single_valued(map
);
11148 test
= isl_map_reverse(isl_map_copy(map
));
11149 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11151 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11152 id
= isl_map_identity(dim
);
11154 sv
= isl_map_is_subset(test
, id
);
11156 isl_map_free(test
);
11162 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11166 map
= isl_map_copy(map
);
11167 map
= isl_map_reverse(map
);
11168 in
= isl_map_is_single_valued(map
);
11174 /* Check if the given map is obviously injective.
11176 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11180 map
= isl_map_copy(map
);
11181 map
= isl_map_reverse(map
);
11182 in
= isl_map_plain_is_single_valued(map
);
11188 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11192 sv
= isl_map_is_single_valued(map
);
11196 return isl_map_is_injective(map
);
11199 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11201 return isl_map_is_single_valued(set_to_map(set
));
11204 /* Does "map" only map elements to themselves?
11206 * If the domain and range spaces are different, then "map"
11207 * is considered not to be an identity relation, even if it is empty.
11208 * Otherwise, construct the maximal identity relation and
11209 * check whether "map" is a subset of this relation.
11211 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11215 isl_bool equal
, is_identity
;
11217 space
= isl_map_get_space(map
);
11218 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11219 isl_space_free(space
);
11220 if (equal
< 0 || !equal
)
11223 id
= isl_map_identity(isl_map_get_space(map
));
11224 is_identity
= isl_map_is_subset(map
, id
);
11227 return is_identity
;
11230 int isl_map_is_translation(__isl_keep isl_map
*map
)
11235 delta
= isl_map_deltas(isl_map_copy(map
));
11236 ok
= isl_set_is_singleton(delta
);
11237 isl_set_free(delta
);
11242 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11244 if (isl_seq_first_non_zero(p
, pos
) != -1)
11246 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11251 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11258 return isl_bool_error
;
11260 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11261 return isl_bool_false
;
11263 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11264 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11265 for (j
= 0; j
< nvar
; ++j
) {
11266 int lower
= 0, upper
= 0;
11267 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11268 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11270 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11271 return isl_bool_false
;
11274 if (i
< bset
->n_eq
)
11276 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11277 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11279 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11280 return isl_bool_false
;
11281 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11286 if (!lower
|| !upper
)
11287 return isl_bool_false
;
11290 return isl_bool_true
;
11293 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11296 return isl_bool_error
;
11298 return isl_bool_false
;
11300 return isl_basic_set_is_box(set
->p
[0]);
11303 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11306 return isl_bool_error
;
11308 return isl_space_is_wrapping(bset
->dim
);
11311 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11314 return isl_bool_error
;
11316 return isl_space_is_wrapping(set
->dim
);
11319 /* Modify the space of "map" through a call to "change".
11320 * If "can_change" is set (not NULL), then first call it to check
11321 * if the modification is allowed, printing the error message "cannot_change"
11324 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11325 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11326 const char *cannot_change
,
11327 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11335 ok
= can_change
? can_change(map
) : isl_bool_true
;
11337 return isl_map_free(map
);
11339 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11340 return isl_map_free(map
));
11342 space
= change(isl_map_get_space(map
));
11343 map
= isl_map_reset_space(map
, space
);
11348 /* Is the domain of "map" a wrapped relation?
11350 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11353 return isl_bool_error
;
11355 return isl_space_domain_is_wrapping(map
->dim
);
11358 /* Does "map" have a wrapped relation in both domain and range?
11360 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11362 return isl_space_is_product(isl_map_peek_space(map
));
11365 /* Is the range of "map" a wrapped relation?
11367 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11370 return isl_bool_error
;
11372 return isl_space_range_is_wrapping(map
->dim
);
11375 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11377 bmap
= isl_basic_map_cow(bmap
);
11381 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11385 bmap
= isl_basic_map_finalize(bmap
);
11387 return bset_from_bmap(bmap
);
11389 isl_basic_map_free(bmap
);
11393 /* Given a map A -> B, return the set (A -> B).
11395 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11397 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11400 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11402 bset
= isl_basic_set_cow(bset
);
11406 bset
->dim
= isl_space_unwrap(bset
->dim
);
11410 bset
= isl_basic_set_finalize(bset
);
11412 return bset_to_bmap(bset
);
11414 isl_basic_set_free(bset
);
11418 /* Given a set (A -> B), return the map A -> B.
11419 * Error out if "set" is not of the form (A -> B).
11421 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11423 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11424 "not a wrapping set", &isl_space_unwrap
);
11427 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11428 enum isl_dim_type type
)
11433 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11436 bmap
= isl_basic_map_cow(bmap
);
11440 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11444 bmap
= isl_basic_map_finalize(bmap
);
11448 isl_basic_map_free(bmap
);
11452 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11453 enum isl_dim_type type
)
11460 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11463 map
= isl_map_cow(map
);
11467 for (i
= 0; i
< map
->n
; ++i
) {
11468 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11472 map
->dim
= isl_space_reset(map
->dim
, type
);
11482 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11487 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11490 bmap
= isl_basic_map_cow(bmap
);
11494 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11498 bmap
= isl_basic_map_finalize(bmap
);
11502 isl_basic_map_free(bmap
);
11506 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11508 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11511 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11512 __isl_take isl_basic_map
*bmap
)
11517 if (!bmap
->dim
->nested
[0])
11520 bmap
= isl_basic_map_cow(bmap
);
11524 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11528 bmap
= isl_basic_map_finalize(bmap
);
11532 isl_basic_map_free(bmap
);
11536 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11537 __isl_take isl_basic_map
*bmap
)
11542 if (!bmap
->dim
->nested
[1])
11545 bmap
= isl_basic_map_cow(bmap
);
11549 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11553 bmap
= isl_basic_map_finalize(bmap
);
11557 isl_basic_map_free(bmap
);
11561 /* Remove any internal structure from the spaces of domain and range of "map".
11563 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11568 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11571 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11574 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11576 return set_from_map(isl_map_flatten(set_to_map(set
)));
11579 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11581 isl_space
*space
, *flat_space
;
11584 space
= isl_set_get_space(set
);
11585 flat_space
= isl_space_flatten(isl_space_copy(space
));
11586 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11588 map
= isl_map_intersect_domain(map
, set
);
11593 /* Remove any internal structure from the space of the domain of "map".
11595 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11600 if (!map
->dim
->nested
[0])
11603 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11606 /* Remove any internal structure from the space of the range of "map".
11608 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11613 if (!map
->dim
->nested
[1])
11616 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11619 /* Reorder the dimensions of "bmap" according to the given dim_map
11620 * and set the dimension specification to "space" and
11621 * perform Gaussian elimination on the result.
11623 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11624 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11626 isl_basic_map
*res
;
11630 if (!bmap
|| !space
|| !dim_map
)
11633 flags
= bmap
->flags
;
11634 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11635 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11636 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11637 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11638 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11639 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11641 res
->flags
= flags
;
11642 res
= isl_basic_map_gauss(res
, NULL
);
11643 res
= isl_basic_map_finalize(res
);
11646 isl_dim_map_free(dim_map
);
11647 isl_basic_map_free(bmap
);
11648 isl_space_free(space
);
11652 /* Reorder the dimensions of "map" according to given reordering.
11654 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11655 __isl_take isl_reordering
*r
)
11658 struct isl_dim_map
*dim_map
;
11660 map
= isl_map_cow(map
);
11661 dim_map
= isl_dim_map_from_reordering(r
);
11662 if (!map
|| !r
|| !dim_map
)
11665 for (i
= 0; i
< map
->n
; ++i
) {
11666 struct isl_dim_map
*dim_map_i
;
11669 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11671 space
= isl_reordering_get_space(r
);
11672 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11678 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11679 map
= isl_map_unmark_normalized(map
);
11681 isl_reordering_free(r
);
11682 isl_dim_map_free(dim_map
);
11685 isl_dim_map_free(dim_map
);
11687 isl_reordering_free(r
);
11691 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11692 __isl_take isl_reordering
*r
)
11694 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11697 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11698 __isl_take isl_space
*model
)
11703 if (!map
|| !model
)
11706 ctx
= isl_space_get_ctx(model
);
11707 if (!isl_space_has_named_params(model
))
11708 isl_die(ctx
, isl_error_invalid
,
11709 "model has unnamed parameters", goto error
);
11710 if (isl_map_check_named_params(map
) < 0)
11712 aligned
= isl_map_space_has_equal_params(map
, model
);
11716 isl_reordering
*exp
;
11718 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11719 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11720 map
= isl_map_realign(map
, exp
);
11723 isl_space_free(model
);
11726 isl_space_free(model
);
11731 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11732 __isl_take isl_space
*model
)
11734 return isl_map_align_params(set
, model
);
11737 /* Align the parameters of "bmap" to those of "model", introducing
11738 * additional parameters if needed.
11740 __isl_give isl_basic_map
*isl_basic_map_align_params(
11741 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11744 isl_bool equal_params
;
11746 if (!bmap
|| !model
)
11749 ctx
= isl_space_get_ctx(model
);
11750 if (!isl_space_has_named_params(model
))
11751 isl_die(ctx
, isl_error_invalid
,
11752 "model has unnamed parameters", goto error
);
11753 if (isl_basic_map_check_named_params(bmap
) < 0)
11755 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11756 if (equal_params
< 0)
11758 if (!equal_params
) {
11759 isl_reordering
*exp
;
11760 struct isl_dim_map
*dim_map
;
11762 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11763 exp
= isl_reordering_extend_space(exp
,
11764 isl_basic_map_get_space(bmap
));
11765 dim_map
= isl_dim_map_from_reordering(exp
);
11766 bmap
= isl_basic_map_realign(bmap
,
11767 isl_reordering_get_space(exp
),
11768 isl_dim_map_extend(dim_map
, bmap
));
11769 isl_reordering_free(exp
);
11770 isl_dim_map_free(dim_map
);
11773 isl_space_free(model
);
11776 isl_space_free(model
);
11777 isl_basic_map_free(bmap
);
11781 /* Do "bset" and "space" have the same parameters?
11783 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11784 __isl_keep isl_space
*space
)
11786 isl_space
*bset_space
;
11788 bset_space
= isl_basic_set_peek_space(bset
);
11789 return isl_space_has_equal_params(bset_space
, space
);
11792 /* Do "map" and "space" have the same parameters?
11794 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11795 __isl_keep isl_space
*space
)
11797 isl_space
*map_space
;
11799 map_space
= isl_map_peek_space(map
);
11800 return isl_space_has_equal_params(map_space
, space
);
11803 /* Do "set" and "space" have the same parameters?
11805 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11806 __isl_keep isl_space
*space
)
11808 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11811 /* Align the parameters of "bset" to those of "model", introducing
11812 * additional parameters if needed.
11814 __isl_give isl_basic_set
*isl_basic_set_align_params(
11815 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11817 return isl_basic_map_align_params(bset
, model
);
11820 /* Drop all parameters not referenced by "map".
11822 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11826 if (isl_map_check_named_params(map
) < 0)
11827 return isl_map_free(map
);
11829 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11832 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11834 return isl_map_free(map
);
11836 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11842 /* Drop all parameters not referenced by "set".
11844 __isl_give isl_set
*isl_set_drop_unused_params(
11845 __isl_take isl_set
*set
)
11847 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11850 /* Drop all parameters not referenced by "bmap".
11852 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11853 __isl_take isl_basic_map
*bmap
)
11857 if (isl_basic_map_check_named_params(bmap
) < 0)
11858 return isl_basic_map_free(bmap
);
11860 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11863 involves
= isl_basic_map_involves_dims(bmap
,
11864 isl_dim_param
, i
, 1);
11866 return isl_basic_map_free(bmap
);
11868 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11874 /* Drop all parameters not referenced by "bset".
11876 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11877 __isl_take isl_basic_set
*bset
)
11879 return bset_from_bmap(isl_basic_map_drop_unused_params(
11880 bset_to_bmap(bset
)));
11883 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11884 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11885 enum isl_dim_type c2
, enum isl_dim_type c3
,
11886 enum isl_dim_type c4
, enum isl_dim_type c5
)
11888 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11889 struct isl_mat
*mat
;
11895 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11896 isl_basic_map_total_dim(bmap
) + 1);
11899 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11900 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11901 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11902 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11903 isl_int_set(mat
->row
[i
][pos
],
11904 bmap
->eq
[i
][off
+ k
]);
11912 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11913 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11914 enum isl_dim_type c2
, enum isl_dim_type c3
,
11915 enum isl_dim_type c4
, enum isl_dim_type c5
)
11917 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11918 struct isl_mat
*mat
;
11924 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11925 isl_basic_map_total_dim(bmap
) + 1);
11928 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11929 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11930 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11931 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11932 isl_int_set(mat
->row
[i
][pos
],
11933 bmap
->ineq
[i
][off
+ k
]);
11941 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11942 __isl_take isl_space
*space
,
11943 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11944 enum isl_dim_type c2
, enum isl_dim_type c3
,
11945 enum isl_dim_type c4
, enum isl_dim_type c5
)
11947 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11948 isl_basic_map
*bmap
= NULL
;
11954 if (!space
|| !eq
|| !ineq
)
11957 if (eq
->n_col
!= ineq
->n_col
)
11958 isl_die(space
->ctx
, isl_error_invalid
,
11959 "equalities and inequalities matrices should have "
11960 "same number of columns", goto error
);
11962 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11964 if (eq
->n_col
< total
)
11965 isl_die(space
->ctx
, isl_error_invalid
,
11966 "number of columns too small", goto error
);
11968 extra
= eq
->n_col
- total
;
11970 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11971 eq
->n_row
, ineq
->n_row
);
11974 for (i
= 0; i
< extra
; ++i
) {
11975 k
= isl_basic_map_alloc_div(bmap
);
11978 isl_int_set_si(bmap
->div
[k
][0], 0);
11980 for (i
= 0; i
< eq
->n_row
; ++i
) {
11981 l
= isl_basic_map_alloc_equality(bmap
);
11984 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11985 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11986 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11987 isl_int_set(bmap
->eq
[l
][off
+ k
],
11993 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11994 l
= isl_basic_map_alloc_inequality(bmap
);
11997 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11998 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11999 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
12000 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12001 ineq
->row
[i
][pos
]);
12007 isl_space_free(space
);
12009 isl_mat_free(ineq
);
12011 bmap
= isl_basic_map_simplify(bmap
);
12012 return isl_basic_map_finalize(bmap
);
12014 isl_space_free(space
);
12016 isl_mat_free(ineq
);
12017 isl_basic_map_free(bmap
);
12021 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12022 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12023 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12025 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12026 c1
, c2
, c3
, c4
, isl_dim_in
);
12029 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12030 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12031 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12033 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12034 c1
, c2
, c3
, c4
, isl_dim_in
);
12037 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12038 __isl_take isl_space
*dim
,
12039 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12040 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12042 isl_basic_map
*bmap
;
12043 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12044 c1
, c2
, c3
, c4
, isl_dim_in
);
12045 return bset_from_bmap(bmap
);
12048 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12051 return isl_bool_error
;
12053 return isl_space_can_zip(bmap
->dim
);
12056 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12059 return isl_bool_error
;
12061 return isl_space_can_zip(map
->dim
);
12064 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12065 * (A -> C) -> (B -> D).
12067 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12076 if (!isl_basic_map_can_zip(bmap
))
12077 isl_die(bmap
->ctx
, isl_error_invalid
,
12078 "basic map cannot be zipped", goto error
);
12079 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12080 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12081 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12082 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12083 bmap
= isl_basic_map_cow(bmap
);
12084 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12087 bmap
->dim
= isl_space_zip(bmap
->dim
);
12090 bmap
= isl_basic_map_mark_final(bmap
);
12093 isl_basic_map_free(bmap
);
12097 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12098 * (A -> C) -> (B -> D).
12100 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12107 if (!isl_map_can_zip(map
))
12108 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12111 map
= isl_map_cow(map
);
12115 for (i
= 0; i
< map
->n
; ++i
) {
12116 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12121 map
->dim
= isl_space_zip(map
->dim
);
12131 /* Can we apply isl_basic_map_curry to "bmap"?
12132 * That is, does it have a nested relation in its domain?
12134 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12137 return isl_bool_error
;
12139 return isl_space_can_curry(bmap
->dim
);
12142 /* Can we apply isl_map_curry to "map"?
12143 * That is, does it have a nested relation in its domain?
12145 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12148 return isl_bool_error
;
12150 return isl_space_can_curry(map
->dim
);
12153 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12156 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12162 if (!isl_basic_map_can_curry(bmap
))
12163 isl_die(bmap
->ctx
, isl_error_invalid
,
12164 "basic map cannot be curried", goto error
);
12165 bmap
= isl_basic_map_cow(bmap
);
12168 bmap
->dim
= isl_space_curry(bmap
->dim
);
12171 bmap
= isl_basic_map_mark_final(bmap
);
12174 isl_basic_map_free(bmap
);
12178 /* Given a map (A -> B) -> C, return the corresponding map
12181 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12183 return isl_map_change_space(map
, &isl_map_can_curry
,
12184 "map cannot be curried", &isl_space_curry
);
12187 /* Can isl_map_range_curry be applied to "map"?
12188 * That is, does it have a nested relation in its range,
12189 * the domain of which is itself a nested relation?
12191 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12194 return isl_bool_error
;
12196 return isl_space_can_range_curry(map
->dim
);
12199 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12200 * A -> (B -> (C -> D)).
12202 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12204 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12205 "map range cannot be curried",
12206 &isl_space_range_curry
);
12209 /* Can we apply isl_basic_map_uncurry to "bmap"?
12210 * That is, does it have a nested relation in its domain?
12212 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12215 return isl_bool_error
;
12217 return isl_space_can_uncurry(bmap
->dim
);
12220 /* Can we apply isl_map_uncurry to "map"?
12221 * That is, does it have a nested relation in its domain?
12223 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12226 return isl_bool_error
;
12228 return isl_space_can_uncurry(map
->dim
);
12231 /* Given a basic map A -> (B -> C), return the corresponding basic map
12234 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12240 if (!isl_basic_map_can_uncurry(bmap
))
12241 isl_die(bmap
->ctx
, isl_error_invalid
,
12242 "basic map cannot be uncurried",
12243 return isl_basic_map_free(bmap
));
12244 bmap
= isl_basic_map_cow(bmap
);
12247 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12249 return isl_basic_map_free(bmap
);
12250 bmap
= isl_basic_map_mark_final(bmap
);
12254 /* Given a map A -> (B -> C), return the corresponding map
12257 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12259 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12260 "map cannot be uncurried", &isl_space_uncurry
);
12263 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12264 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12266 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12269 /* Construct a basic map where the given dimensions are equal to each other.
12271 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12272 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12274 isl_basic_map
*bmap
= NULL
;
12277 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12278 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12281 if (type1
== type2
&& pos1
== pos2
)
12282 return isl_basic_map_universe(space
);
12284 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12285 i
= isl_basic_map_alloc_equality(bmap
);
12288 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12289 pos1
+= isl_basic_map_offset(bmap
, type1
);
12290 pos2
+= isl_basic_map_offset(bmap
, type2
);
12291 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12292 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12293 bmap
= isl_basic_map_finalize(bmap
);
12294 isl_space_free(space
);
12297 isl_space_free(space
);
12298 isl_basic_map_free(bmap
);
12302 /* Add a constraint imposing that the given two dimensions are equal.
12304 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12305 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12309 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12311 bmap
= isl_basic_map_intersect(bmap
, eq
);
12316 /* Add a constraint imposing that the given two dimensions are equal.
12318 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12319 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12321 isl_basic_map
*bmap
;
12323 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12325 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12330 /* Add a constraint imposing that the given two dimensions have opposite values.
12332 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12333 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12335 isl_basic_map
*bmap
= NULL
;
12338 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12339 return isl_map_free(map
);
12340 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12341 return isl_map_free(map
);
12343 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12344 i
= isl_basic_map_alloc_equality(bmap
);
12347 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12348 pos1
+= isl_basic_map_offset(bmap
, type1
);
12349 pos2
+= isl_basic_map_offset(bmap
, type2
);
12350 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12351 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12352 bmap
= isl_basic_map_finalize(bmap
);
12354 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12358 isl_basic_map_free(bmap
);
12363 /* Construct a constraint imposing that the value of the first dimension is
12364 * greater than or equal to that of the second.
12366 static __isl_give isl_constraint
*constraint_order_ge(
12367 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12368 enum isl_dim_type type2
, int pos2
)
12372 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12373 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12374 space
= isl_space_free(space
);
12378 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12380 if (type1
== type2
&& pos1
== pos2
)
12383 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12384 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12389 /* Add a constraint imposing that the value of the first dimension is
12390 * greater than or equal to that of the second.
12392 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12393 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12398 if (type1
== type2
&& pos1
== pos2
)
12400 space
= isl_basic_map_get_space(bmap
);
12401 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12402 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12407 /* Add a constraint imposing that the value of the first dimension is
12408 * greater than or equal to that of the second.
12410 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12411 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12416 if (type1
== type2
&& pos1
== pos2
)
12418 space
= isl_map_get_space(map
);
12419 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12420 map
= isl_map_add_constraint(map
, c
);
12425 /* Add a constraint imposing that the value of the first dimension is
12426 * less than or equal to that of the second.
12428 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12429 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12431 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12434 /* Construct a basic map where the value of the first dimension is
12435 * greater than that of the second.
12437 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12438 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12440 isl_basic_map
*bmap
= NULL
;
12443 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12444 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12447 if (type1
== type2
&& pos1
== pos2
)
12448 return isl_basic_map_empty(space
);
12450 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12451 i
= isl_basic_map_alloc_inequality(bmap
);
12453 return isl_basic_map_free(bmap
);
12454 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12455 pos1
+= isl_basic_map_offset(bmap
, type1
);
12456 pos2
+= isl_basic_map_offset(bmap
, type2
);
12457 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12458 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12459 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12460 bmap
= isl_basic_map_finalize(bmap
);
12464 isl_space_free(space
);
12465 isl_basic_map_free(bmap
);
12469 /* Add a constraint imposing that the value of the first dimension is
12470 * greater than that of the second.
12472 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12473 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12477 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12479 bmap
= isl_basic_map_intersect(bmap
, gt
);
12484 /* Add a constraint imposing that the value of the first dimension is
12485 * greater than that of the second.
12487 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12488 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12490 isl_basic_map
*bmap
;
12492 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12494 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12499 /* Add a constraint imposing that the value of the first dimension is
12500 * smaller than that of the second.
12502 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12503 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12505 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12508 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12512 isl_local_space
*ls
;
12517 if (!isl_basic_map_divs_known(bmap
))
12518 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12519 "some divs are unknown", return NULL
);
12521 ls
= isl_basic_map_get_local_space(bmap
);
12522 div
= isl_local_space_get_div(ls
, pos
);
12523 isl_local_space_free(ls
);
12528 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12531 return isl_basic_map_get_div(bset
, pos
);
12534 /* Plug in "subs" for dimension "type", "pos" of "bset".
12536 * Let i be the dimension to replace and let "subs" be of the form
12540 * Any integer division with a non-zero coefficient for i,
12542 * floor((a i + g)/m)
12546 * floor((a f + d g)/(m d))
12548 * Constraints of the form
12556 * We currently require that "subs" is an integral expression.
12557 * Handling rational expressions may require us to add stride constraints
12558 * as we do in isl_basic_set_preimage_multi_aff.
12560 __isl_give isl_basic_set
*isl_basic_set_substitute(
12561 __isl_take isl_basic_set
*bset
,
12562 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12568 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12571 bset
= isl_basic_set_cow(bset
);
12572 if (!bset
|| !subs
)
12575 ctx
= isl_basic_set_get_ctx(bset
);
12576 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12577 isl_die(ctx
, isl_error_invalid
,
12578 "spaces don't match", goto error
);
12579 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12580 isl_die(ctx
, isl_error_unsupported
,
12581 "cannot handle divs yet", goto error
);
12582 if (!isl_int_is_one(subs
->v
->el
[0]))
12583 isl_die(ctx
, isl_error_invalid
,
12584 "can only substitute integer expressions", goto error
);
12586 pos
+= isl_basic_set_offset(bset
, type
);
12590 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12591 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12593 isl_int_set(v
, bset
->eq
[i
][pos
]);
12594 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12595 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12596 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12599 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12600 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12602 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12603 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12604 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12605 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12608 for (i
= 0; i
< bset
->n_div
; ++i
) {
12609 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12611 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12612 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12613 isl_seq_combine(bset
->div
[i
] + 1,
12614 subs
->v
->el
[0], bset
->div
[i
] + 1,
12615 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12616 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12621 bset
= isl_basic_set_simplify(bset
);
12622 return isl_basic_set_finalize(bset
);
12624 isl_basic_set_free(bset
);
12628 /* Plug in "subs" for dimension "type", "pos" of "set".
12630 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12631 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12635 if (set
&& isl_set_plain_is_empty(set
))
12638 set
= isl_set_cow(set
);
12642 for (i
= set
->n
- 1; i
>= 0; --i
) {
12643 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12644 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12655 /* Check if the range of "ma" is compatible with the domain or range
12656 * (depending on "type") of "bmap".
12658 static isl_stat
check_basic_map_compatible_range_multi_aff(
12659 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12660 __isl_keep isl_multi_aff
*ma
)
12663 isl_space
*ma_space
;
12665 ma_space
= isl_multi_aff_get_space(ma
);
12667 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12671 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12672 "parameters don't match", goto error
);
12673 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12677 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12678 "spaces don't match", goto error
);
12680 isl_space_free(ma_space
);
12681 return isl_stat_ok
;
12683 isl_space_free(ma_space
);
12684 return isl_stat_error
;
12687 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12688 * coefficients before the transformed range of dimensions,
12689 * the "n_after" coefficients after the transformed range of dimensions
12690 * and the coefficients of the other divs in "bmap".
12692 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12693 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12698 isl_local_space
*ls
;
12703 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12707 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12708 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12709 for (i
= 0; i
< n_div
; ++i
) {
12710 int o_bmap
= 0, o_ls
= 0;
12712 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12713 o_bmap
+= 1 + 1 + n_param
;
12714 o_ls
+= 1 + 1 + n_param
;
12715 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12716 o_bmap
+= n_before
;
12717 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12718 ls
->div
->row
[i
] + o_ls
, n_set
);
12721 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12723 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12724 ls
->div
->row
[i
] + o_ls
, n_div
);
12727 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12728 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12732 isl_local_space_free(ls
);
12735 isl_local_space_free(ls
);
12739 /* How many stride constraints does "ma" enforce?
12740 * That is, how many of the affine expressions have a denominator
12741 * different from one?
12743 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12748 for (i
= 0; i
< ma
->n
; ++i
)
12749 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12755 /* For each affine expression in ma of the form
12757 * x_i = (f_i y + h_i)/m_i
12759 * with m_i different from one, add a constraint to "bmap"
12762 * f_i y + h_i = m_i alpha_i
12764 * with alpha_i an additional existentially quantified variable.
12766 * The input variables of "ma" correspond to a subset of the variables
12767 * of "bmap". There are "n_before" variables in "bmap" before this
12768 * subset and "n_after" variables after this subset.
12769 * The integer divisions of the affine expressions in "ma" are assumed
12770 * to have been aligned. There are "n_div_ma" of them and
12771 * they appear first in "bmap", straight after the "n_after" variables.
12773 static __isl_give isl_basic_map
*add_ma_strides(
12774 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12775 int n_before
, int n_after
, int n_div_ma
)
12783 total
= isl_basic_map_total_dim(bmap
);
12784 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12785 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12786 for (i
= 0; i
< ma
->n
; ++i
) {
12787 int o_bmap
= 0, o_ma
= 1;
12789 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12791 div
= isl_basic_map_alloc_div(bmap
);
12792 k
= isl_basic_map_alloc_equality(bmap
);
12793 if (div
< 0 || k
< 0)
12795 isl_int_set_si(bmap
->div
[div
][0], 0);
12796 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12797 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12798 o_bmap
+= 1 + n_param
;
12799 o_ma
+= 1 + n_param
;
12800 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12801 o_bmap
+= n_before
;
12802 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12803 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12806 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12808 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12809 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12810 o_bmap
+= n_div_ma
;
12812 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12813 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12819 isl_basic_map_free(bmap
);
12823 /* Replace the domain or range space (depending on "type) of "space" by "set".
12825 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12826 enum isl_dim_type type
, __isl_take isl_space
*set
)
12828 if (type
== isl_dim_in
) {
12829 space
= isl_space_range(space
);
12830 space
= isl_space_map_from_domain_and_range(set
, space
);
12832 space
= isl_space_domain(space
);
12833 space
= isl_space_map_from_domain_and_range(space
, set
);
12839 /* Compute the preimage of the domain or range (depending on "type")
12840 * of "bmap" under the function represented by "ma".
12841 * In other words, plug in "ma" in the domain or range of "bmap".
12842 * The result is a basic map that lives in the same space as "bmap"
12843 * except that the domain or range has been replaced by
12844 * the domain space of "ma".
12846 * If bmap is represented by
12848 * A(p) + S u + B x + T v + C(divs) >= 0,
12850 * where u and x are input and output dimensions if type == isl_dim_out
12851 * while x and v are input and output dimensions if type == isl_dim_in,
12852 * and ma is represented by
12854 * x = D(p) + F(y) + G(divs')
12856 * then the result is
12858 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12860 * The divs in the input set are similarly adjusted.
12863 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12867 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12868 * B_i G(divs') + c_i(divs))/n_i)
12870 * If bmap is not a rational map and if F(y) involves any denominators
12872 * x_i = (f_i y + h_i)/m_i
12874 * then additional constraints are added to ensure that we only
12875 * map back integer points. That is we enforce
12877 * f_i y + h_i = m_i alpha_i
12879 * with alpha_i an additional existentially quantified variable.
12881 * We first copy over the divs from "ma".
12882 * Then we add the modified constraints and divs from "bmap".
12883 * Finally, we add the stride constraints, if needed.
12885 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12886 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12887 __isl_take isl_multi_aff
*ma
)
12891 isl_basic_map
*res
= NULL
;
12892 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12893 isl_int f
, c1
, c2
, g
;
12902 ma
= isl_multi_aff_align_divs(ma
);
12905 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12908 if (type
== isl_dim_in
) {
12910 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12912 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12915 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12916 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12918 space
= isl_multi_aff_get_domain_space(ma
);
12919 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12920 rational
= isl_basic_map_is_rational(bmap
);
12921 strides
= rational
? 0 : multi_aff_strides(ma
);
12922 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12923 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12925 res
= isl_basic_map_set_rational(res
);
12927 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12928 if (isl_basic_map_alloc_div(res
) < 0)
12931 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12934 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12935 k
= isl_basic_map_alloc_equality(res
);
12938 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12939 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12942 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12943 k
= isl_basic_map_alloc_inequality(res
);
12946 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12947 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12950 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12951 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12952 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12955 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12956 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12961 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12967 isl_basic_map_free(bmap
);
12968 isl_multi_aff_free(ma
);
12969 res
= isl_basic_map_simplify(res
);
12970 return isl_basic_map_finalize(res
);
12976 isl_basic_map_free(bmap
);
12977 isl_multi_aff_free(ma
);
12978 isl_basic_map_free(res
);
12982 /* Compute the preimage of "bset" under the function represented by "ma".
12983 * In other words, plug in "ma" in "bset". The result is a basic set
12984 * that lives in the domain space of "ma".
12986 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12987 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12989 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12992 /* Compute the preimage of the domain of "bmap" under the function
12993 * represented by "ma".
12994 * In other words, plug in "ma" in the domain of "bmap".
12995 * The result is a basic map that lives in the same space as "bmap"
12996 * except that the domain has been replaced by the domain space of "ma".
12998 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12999 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13001 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13004 /* Compute the preimage of the range of "bmap" under the function
13005 * represented by "ma".
13006 * In other words, plug in "ma" in the range of "bmap".
13007 * The result is a basic map that lives in the same space as "bmap"
13008 * except that the range has been replaced by the domain space of "ma".
13010 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13011 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13013 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13016 /* Check if the range of "ma" is compatible with the domain or range
13017 * (depending on "type") of "map".
13018 * Return isl_stat_error if anything is wrong.
13020 static isl_stat
check_map_compatible_range_multi_aff(
13021 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13022 __isl_keep isl_multi_aff
*ma
)
13025 isl_space
*ma_space
;
13027 ma_space
= isl_multi_aff_get_space(ma
);
13028 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13029 isl_space_free(ma_space
);
13031 return isl_stat_error
;
13033 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13034 "spaces don't match", return isl_stat_error
);
13035 return isl_stat_ok
;
13038 /* Compute the preimage of the domain or range (depending on "type")
13039 * of "map" under the function represented by "ma".
13040 * In other words, plug in "ma" in the domain or range of "map".
13041 * The result is a map that lives in the same space as "map"
13042 * except that the domain or range has been replaced by
13043 * the domain space of "ma".
13045 * The parameters are assumed to have been aligned.
13047 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13048 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13053 map
= isl_map_cow(map
);
13054 ma
= isl_multi_aff_align_divs(ma
);
13057 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13060 for (i
= 0; i
< map
->n
; ++i
) {
13061 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13062 isl_multi_aff_copy(ma
));
13067 space
= isl_multi_aff_get_domain_space(ma
);
13068 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13070 isl_space_free(map
->dim
);
13075 isl_multi_aff_free(ma
);
13077 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13078 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13081 isl_multi_aff_free(ma
);
13086 /* Compute the preimage of the domain or range (depending on "type")
13087 * of "map" under the function represented by "ma".
13088 * In other words, plug in "ma" in the domain or range of "map".
13089 * The result is a map that lives in the same space as "map"
13090 * except that the domain or range has been replaced by
13091 * the domain space of "ma".
13093 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13094 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13101 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13105 return map_preimage_multi_aff(map
, type
, ma
);
13107 if (isl_map_check_named_params(map
) < 0)
13109 if (!isl_space_has_named_params(ma
->space
))
13110 isl_die(map
->ctx
, isl_error_invalid
,
13111 "unaligned unnamed parameters", goto error
);
13112 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13113 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13115 return map_preimage_multi_aff(map
, type
, ma
);
13117 isl_multi_aff_free(ma
);
13118 return isl_map_free(map
);
13121 /* Compute the preimage of "set" under the function represented by "ma".
13122 * In other words, plug in "ma" in "set". The result is a set
13123 * that lives in the domain space of "ma".
13125 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13126 __isl_take isl_multi_aff
*ma
)
13128 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13131 /* Compute the preimage of the domain of "map" under the function
13132 * represented by "ma".
13133 * In other words, plug in "ma" in the domain of "map".
13134 * The result is a map that lives in the same space as "map"
13135 * except that the domain has been replaced by the domain space of "ma".
13137 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13138 __isl_take isl_multi_aff
*ma
)
13140 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13143 /* Compute the preimage of the range of "map" under the function
13144 * represented by "ma".
13145 * In other words, plug in "ma" in the range of "map".
13146 * The result is a map that lives in the same space as "map"
13147 * except that the range has been replaced by the domain space of "ma".
13149 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13150 __isl_take isl_multi_aff
*ma
)
13152 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13155 /* Compute the preimage of "map" under the function represented by "pma".
13156 * In other words, plug in "pma" in the domain or range of "map".
13157 * The result is a map that lives in the same space as "map",
13158 * except that the space of type "type" has been replaced by
13159 * the domain space of "pma".
13161 * The parameters of "map" and "pma" are assumed to have been aligned.
13163 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13164 __isl_take isl_map
*map
, enum isl_dim_type type
,
13165 __isl_take isl_pw_multi_aff
*pma
)
13174 isl_pw_multi_aff_free(pma
);
13175 res
= isl_map_empty(isl_map_get_space(map
));
13180 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13181 isl_multi_aff_copy(pma
->p
[0].maff
));
13182 if (type
== isl_dim_in
)
13183 res
= isl_map_intersect_domain(res
,
13184 isl_map_copy(pma
->p
[0].set
));
13186 res
= isl_map_intersect_range(res
,
13187 isl_map_copy(pma
->p
[0].set
));
13189 for (i
= 1; i
< pma
->n
; ++i
) {
13192 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13193 isl_multi_aff_copy(pma
->p
[i
].maff
));
13194 if (type
== isl_dim_in
)
13195 res_i
= isl_map_intersect_domain(res_i
,
13196 isl_map_copy(pma
->p
[i
].set
));
13198 res_i
= isl_map_intersect_range(res_i
,
13199 isl_map_copy(pma
->p
[i
].set
));
13200 res
= isl_map_union(res
, res_i
);
13203 isl_pw_multi_aff_free(pma
);
13207 isl_pw_multi_aff_free(pma
);
13212 /* Compute the preimage of "map" under the function represented by "pma".
13213 * In other words, plug in "pma" in the domain or range of "map".
13214 * The result is a map that lives in the same space as "map",
13215 * except that the space of type "type" has been replaced by
13216 * the domain space of "pma".
13218 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13219 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13226 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13230 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13232 if (isl_map_check_named_params(map
) < 0)
13234 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13236 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13237 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13239 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13241 isl_pw_multi_aff_free(pma
);
13242 return isl_map_free(map
);
13245 /* Compute the preimage of "set" under the function represented by "pma".
13246 * In other words, plug in "pma" in "set". The result is a set
13247 * that lives in the domain space of "pma".
13249 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13250 __isl_take isl_pw_multi_aff
*pma
)
13252 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13255 /* Compute the preimage of the domain of "map" under the function
13256 * represented by "pma".
13257 * In other words, plug in "pma" in the domain of "map".
13258 * The result is a map that lives in the same space as "map",
13259 * except that domain space has been replaced by the domain space of "pma".
13261 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13262 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13264 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13267 /* Compute the preimage of the range of "map" under the function
13268 * represented by "pma".
13269 * In other words, plug in "pma" in the range of "map".
13270 * The result is a map that lives in the same space as "map",
13271 * except that range space has been replaced by the domain space of "pma".
13273 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13274 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13276 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13279 /* Compute the preimage of "map" under the function represented by "mpa".
13280 * In other words, plug in "mpa" in the domain or range of "map".
13281 * The result is a map that lives in the same space as "map",
13282 * except that the space of type "type" has been replaced by
13283 * the domain space of "mpa".
13285 * If the map does not involve any constraints that refer to the
13286 * dimensions of the substituted space, then the only possible
13287 * effect of "mpa" on the map is to map the space to a different space.
13288 * We create a separate isl_multi_aff to effectuate this change
13289 * in order to avoid spurious splitting of the map along the pieces
13291 * If "mpa" has a non-trivial explicit domain, however,
13292 * then the full substitution should be performed.
13294 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13295 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13299 isl_pw_multi_aff
*pma
;
13304 n
= isl_map_dim(map
, type
);
13305 full
= isl_map_involves_dims(map
, type
, 0, n
);
13306 if (full
>= 0 && !full
)
13307 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13314 space
= isl_multi_pw_aff_get_space(mpa
);
13315 isl_multi_pw_aff_free(mpa
);
13316 ma
= isl_multi_aff_zero(space
);
13317 return isl_map_preimage_multi_aff(map
, type
, ma
);
13320 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13321 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13324 isl_multi_pw_aff_free(mpa
);
13328 /* Compute the preimage of "map" under the function represented by "mpa".
13329 * In other words, plug in "mpa" in the domain "map".
13330 * The result is a map that lives in the same space as "map",
13331 * except that domain space has been replaced by the domain space of "mpa".
13333 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13334 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13336 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13339 /* Compute the preimage of "set" by the function represented by "mpa".
13340 * In other words, plug in "mpa" in "set".
13342 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13343 __isl_take isl_multi_pw_aff
*mpa
)
13345 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13348 /* Return a copy of the equality constraints of "bset" as a matrix.
13350 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13351 __isl_keep isl_basic_set
*bset
)
13359 ctx
= isl_basic_set_get_ctx(bset
);
13360 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13361 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13364 /* Are the "n" "coefficients" starting at "first" of the integer division
13365 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13367 * The "coefficient" at position 0 is the denominator.
13368 * The "coefficient" at position 1 is the constant term.
13370 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13371 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13372 unsigned first
, unsigned n
)
13374 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13375 return isl_bool_error
;
13376 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13377 return isl_bool_error
;
13378 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13379 bmap2
->div
[pos2
] + first
, n
);
13382 /* Are the integer division expressions at position "pos1" in "bmap1" and
13383 * "pos2" in "bmap2" equal to each other, except that the constant terms
13386 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13387 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13388 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13393 if (!bmap1
|| !bmap2
)
13394 return isl_bool_error
;
13395 total
= isl_basic_map_total_dim(bmap1
);
13396 if (total
!= isl_basic_map_total_dim(bmap2
))
13397 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13398 "incomparable div expressions", return isl_bool_error
);
13399 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13401 if (equal
< 0 || !equal
)
13403 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13405 if (equal
< 0 || equal
)
13406 return isl_bool_not(equal
);
13407 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13411 /* Replace the numerator of the constant term of the integer division
13412 * expression at position "div" in "bmap" by "value".
13413 * The caller guarantees that this does not change the meaning
13416 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13417 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13419 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13420 return isl_basic_map_free(bmap
);
13422 isl_int_set_si(bmap
->div
[div
][1], value
);
13427 /* Is the point "inner" internal to inequality constraint "ineq"
13429 * The point is considered to be internal to the inequality constraint,
13430 * if it strictly lies on the positive side of the inequality constraint,
13431 * or if it lies on the constraint and the constraint is lexico-positive.
13433 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13434 __isl_keep isl_basic_set
*bset
, int ineq
)
13440 if (!inner
|| !bset
)
13441 return isl_bool_error
;
13443 ctx
= isl_basic_set_get_ctx(bset
);
13444 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13445 &ctx
->normalize_gcd
);
13446 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13447 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13449 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13450 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13451 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13454 /* Tighten the inequality constraints of "bset" that are outward with respect
13455 * to the point "vec".
13456 * That is, tighten the constraints that are not satisfied by "vec".
13458 * "vec" is a point internal to some superset S of "bset" that is used
13459 * to make the subsets of S disjoint, by tightening one half of the constraints
13460 * that separate two subsets. In particular, the constraints of S
13461 * are all satisfied by "vec" and should not be tightened.
13462 * Of the internal constraints, those that have "vec" on the outside
13463 * are tightened. The shared facet is included in the adjacent subset
13464 * with the opposite constraint.
13465 * For constraints that saturate "vec", this criterion cannot be used
13466 * to determine which of the two sides should be tightened.
13467 * Instead, the sign of the first non-zero coefficient is used
13468 * to make this choice. Note that this second criterion is never used
13469 * on the constraints of S since "vec" is interior to "S".
13471 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13472 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13476 bset
= isl_basic_set_cow(bset
);
13479 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13482 internal
= is_internal(vec
, bset
, j
);
13484 return isl_basic_set_free(bset
);
13487 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13493 /* Replace the variables x of type "type" starting at "first" in "bmap"
13494 * by x' with x = M x' with M the matrix trans.
13495 * That is, replace the corresponding coefficients c by c M.
13497 * The transformation matrix should be a square matrix.
13499 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13500 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13501 __isl_take isl_mat
*trans
)
13505 bmap
= isl_basic_map_cow(bmap
);
13506 if (!bmap
|| !trans
)
13509 if (trans
->n_row
!= trans
->n_col
)
13510 isl_die(trans
->ctx
, isl_error_invalid
,
13511 "expecting square transformation matrix", goto error
);
13512 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13515 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13517 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13518 isl_mat_copy(trans
)) < 0)
13520 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13521 isl_mat_copy(trans
)) < 0)
13523 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13524 isl_mat_copy(trans
)) < 0)
13527 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13528 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13530 isl_mat_free(trans
);
13533 isl_mat_free(trans
);
13534 isl_basic_map_free(bmap
);
13538 /* Replace the variables x of type "type" starting at "first" in "bset"
13539 * by x' with x = M x' with M the matrix trans.
13540 * That is, replace the corresponding coefficients c by c M.
13542 * The transformation matrix should be a square matrix.
13544 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13545 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13546 __isl_take isl_mat
*trans
)
13548 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);