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 "map" has only named parameters, reporting an error
1392 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1394 return isl_space_check_named_params(isl_map_peek_space(map
));
1397 /* Check that "bmap" has only named parameters, reporting an error
1400 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1402 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1405 /* Check that "bmap1" and "bmap2" have the same parameters,
1406 * reporting an error if they do not.
1408 static isl_stat
isl_basic_map_check_equal_params(
1409 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1413 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1415 return isl_stat_error
;
1417 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1418 "parameters don't match", return isl_stat_error
);
1422 __isl_give isl_map
*isl_map_align_params_map_map_and(
1423 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1424 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1425 __isl_take isl_map
*map2
))
1429 if (isl_map_has_equal_params(map1
, map2
))
1430 return fn(map1
, map2
);
1431 if (isl_map_check_named_params(map1
) < 0)
1433 if (isl_map_check_named_params(map2
) < 0)
1435 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1436 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1437 return fn(map1
, map2
);
1444 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1445 __isl_keep isl_map
*map2
,
1446 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1451 return isl_bool_error
;
1452 if (isl_map_has_equal_params(map1
, map2
))
1453 return fn(map1
, map2
);
1454 if (isl_map_check_named_params(map1
) < 0)
1455 return isl_bool_error
;
1456 if (isl_map_check_named_params(map2
) < 0)
1457 return isl_bool_error
;
1458 map1
= isl_map_copy(map1
);
1459 map2
= isl_map_copy(map2
);
1460 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1461 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1468 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1470 struct isl_ctx
*ctx
;
1474 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1475 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1477 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1478 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1479 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1480 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1481 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1483 int j
= isl_basic_map_alloc_inequality(bmap
);
1487 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1488 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1495 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1496 bmap
->extra
- bmap
->n_div
);
1497 return bmap
->n_eq
++;
1500 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1502 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1505 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1509 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1514 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1516 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1519 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1524 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1526 if (pos
!= bmap
->n_eq
- 1) {
1528 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1529 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1535 /* Turn inequality "pos" of "bmap" into an equality.
1537 * In particular, we move the inequality in front of the equalities
1538 * and move the last inequality in the position of the moved inequality.
1539 * Note that isl_tab_make_equalities_explicit depends on this particular
1540 * change in the ordering of the constraints.
1542 void isl_basic_map_inequality_to_equality(
1543 struct isl_basic_map
*bmap
, unsigned pos
)
1547 t
= bmap
->ineq
[pos
];
1548 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1549 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1554 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1555 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1556 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1557 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1560 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1562 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1565 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1567 struct isl_ctx
*ctx
;
1571 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1572 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1573 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1574 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1575 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1576 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1577 1 + isl_basic_map_total_dim(bmap
),
1578 bmap
->extra
- bmap
->n_div
);
1579 return bmap
->n_ineq
++;
1582 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1584 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1587 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1591 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1596 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1598 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1601 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1606 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1608 if (pos
!= bmap
->n_ineq
- 1) {
1609 t
= bmap
->ineq
[pos
];
1610 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1611 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1612 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1618 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1620 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1623 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1629 empty
= isl_basic_map_plain_is_empty(bmap
);
1631 return isl_basic_map_free(bmap
);
1635 bmap
= isl_basic_map_cow(bmap
);
1636 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1639 k
= isl_basic_map_alloc_equality(bmap
);
1642 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1645 isl_basic_map_free(bmap
);
1649 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1652 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1655 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1660 bmap
= isl_basic_map_cow(bmap
);
1661 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1664 k
= isl_basic_map_alloc_inequality(bmap
);
1667 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1670 isl_basic_map_free(bmap
);
1674 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1677 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1680 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1684 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1685 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1686 1 + 1 + isl_basic_map_total_dim(bmap
),
1687 bmap
->extra
- bmap
->n_div
);
1688 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1689 return bmap
->n_div
++;
1692 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1694 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1697 /* Check that there are "n" dimensions of type "type" starting at "first"
1700 isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1701 enum isl_dim_type type
, unsigned first
, unsigned n
)
1706 return isl_stat_error
;
1707 dim
= isl_basic_map_dim(bmap
, type
);
1708 if (first
+ n
> dim
|| first
+ n
< first
)
1709 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1710 "position or range out of bounds",
1711 return isl_stat_error
);
1715 /* Insert an extra integer division, prescribed by "div", to "bmap"
1716 * at (integer division) position "pos".
1718 * The integer division is first added at the end and then moved
1719 * into the right position.
1721 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1722 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1726 bmap
= isl_basic_map_cow(bmap
);
1728 return isl_basic_map_free(bmap
);
1730 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1731 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1732 "unexpected size", return isl_basic_map_free(bmap
));
1733 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1734 return isl_basic_map_free(bmap
);
1736 bmap
= isl_basic_map_extend_space(bmap
,
1737 isl_basic_map_get_space(bmap
), 1, 0, 2);
1738 k
= isl_basic_map_alloc_div(bmap
);
1740 return isl_basic_map_free(bmap
);
1741 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1742 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1744 for (i
= k
; i
> pos
; --i
)
1745 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1750 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1753 return isl_stat_error
;
1754 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1759 static __isl_give isl_basic_map
*add_constraints(
1760 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1761 unsigned i_pos
, unsigned o_pos
)
1763 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1766 struct isl_dim_map
*dim_map
;
1768 space
= isl_basic_map_peek_space(bmap2
);
1769 if (!bmap1
|| !space
)
1772 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1773 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1774 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1775 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1776 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1777 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1778 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1779 ctx
= isl_basic_map_get_ctx(bmap1
);
1780 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1781 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1782 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1783 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1784 isl_dim_map_div(dim_map
, bmap2
, total
);
1786 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1788 isl_basic_map_free(bmap1
);
1789 isl_basic_map_free(bmap2
);
1793 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1794 struct isl_basic_set
*bset2
, unsigned pos
)
1796 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1797 bset_to_bmap(bset2
), 0, pos
));
1800 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1801 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1802 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1804 struct isl_basic_map
*ext
;
1814 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1815 base
->extra
>= base
->n_div
+ extra
;
1817 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1818 room_for_ineq(base
, n_ineq
)) {
1819 isl_space_free(space
);
1823 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1824 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1825 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1826 extra
+= base
->extra
;
1828 n_ineq
+= base
->n_ineq
;
1830 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1836 ext
->sample
= isl_vec_copy(base
->sample
);
1837 flags
= base
->flags
;
1838 ext
= add_constraints(ext
, base
, 0, 0);
1841 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1847 isl_space_free(space
);
1848 isl_basic_map_free(base
);
1852 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1853 __isl_take isl_basic_set
*base
,
1854 __isl_take isl_space
*dim
, unsigned extra
,
1855 unsigned n_eq
, unsigned n_ineq
)
1857 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1858 dim
, extra
, n_eq
, n_ineq
));
1861 struct isl_basic_map
*isl_basic_map_extend_constraints(
1862 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1866 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1870 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1871 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1872 unsigned n_eq
, unsigned n_ineq
)
1874 struct isl_basic_map
*bmap
;
1879 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1883 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1886 isl_basic_map_free(base
);
1890 struct isl_basic_set
*isl_basic_set_extend_constraints(
1891 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1893 isl_basic_map
*bmap
= bset_to_bmap(base
);
1894 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1895 return bset_from_bmap(bmap
);
1898 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1900 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1903 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1908 if (bmap
->ref
> 1) {
1910 bmap
= isl_basic_map_dup(bmap
);
1913 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1914 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1919 /* Clear all cached information in "map", either because it is about
1920 * to be modified or because it is being freed.
1921 * Always return the same pointer that is passed in.
1922 * This is needed for the use in isl_map_free.
1924 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1926 isl_basic_map_free(map
->cached_simple_hull
[0]);
1927 isl_basic_map_free(map
->cached_simple_hull
[1]);
1928 map
->cached_simple_hull
[0] = NULL
;
1929 map
->cached_simple_hull
[1] = NULL
;
1933 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1935 return isl_map_cow(set
);
1938 /* Return an isl_map that is equal to "map" and that has only
1939 * a single reference.
1941 * If the original input already has only one reference, then
1942 * simply return it, but clear all cached information, since
1943 * it may be rendered invalid by the operations that will be
1944 * performed on the result.
1946 * Otherwise, create a duplicate (without any cached information).
1948 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1954 return clear_caches(map
);
1956 return isl_map_dup(map
);
1959 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1960 unsigned a_len
, unsigned b_len
)
1962 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1963 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1964 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1967 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1968 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1973 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
1976 if (n1
== 0 || n2
== 0)
1979 bmap
= isl_basic_map_cow(bmap
);
1983 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1984 if (isl_blk_is_error(blk
))
1987 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1989 bmap
->eq
[i
] + pos
, n1
, n2
);
1991 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1993 bmap
->ineq
[i
] + pos
, n1
, n2
);
1995 for (i
= 0; i
< bmap
->n_div
; ++i
)
1997 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1999 isl_blk_free(bmap
->ctx
, blk
);
2001 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2002 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2003 return isl_basic_map_finalize(bmap
);
2005 isl_basic_map_free(bmap
);
2009 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2010 __isl_take isl_basic_map
*bmap
)
2016 total
= isl_basic_map_total_dim(bmap
);
2017 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2018 return isl_basic_map_free(bmap
);
2019 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2021 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2023 i
= isl_basic_map_alloc_equality(bmap
);
2027 isl_int_set_si(bmap
->eq
[i
][0], 1);
2028 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2029 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2030 isl_vec_free(bmap
->sample
);
2031 bmap
->sample
= NULL
;
2032 return isl_basic_map_finalize(bmap
);
2034 isl_basic_map_free(bmap
);
2038 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2039 __isl_take isl_basic_set
*bset
)
2041 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2044 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2045 __isl_take isl_basic_map
*bmap
)
2050 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2053 bmap
= isl_basic_map_cow(bmap
);
2057 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2059 return isl_basic_map_finalize(bmap
);
2062 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2063 __isl_take isl_basic_set
*bset
)
2065 return isl_basic_map_set_rational(bset
);
2068 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2069 __isl_take isl_basic_set
*bset
)
2074 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2077 bset
= isl_basic_set_cow(bset
);
2081 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2083 return isl_basic_set_finalize(bset
);
2086 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2090 map
= isl_map_cow(map
);
2093 for (i
= 0; i
< map
->n
; ++i
) {
2094 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2104 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2106 return isl_map_set_rational(set
);
2109 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2112 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2114 isl_int
*t
= bmap
->div
[a
];
2115 bmap
->div
[a
] = bmap
->div
[b
];
2119 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2120 * div definitions accordingly.
2122 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2125 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2127 swap_div(bmap
, a
, b
);
2129 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2130 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2132 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2133 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2135 for (i
= 0; i
< bmap
->n_div
; ++i
)
2136 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2137 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2140 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2142 isl_seq_cpy(c
, c
+ n
, rem
);
2143 isl_seq_clr(c
+ rem
, n
);
2146 /* Drop n dimensions starting at first.
2148 * In principle, this frees up some extra variables as the number
2149 * of columns remains constant, but we would have to extend
2150 * the div array too as the number of rows in this array is assumed
2151 * to be equal to extra.
2153 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2154 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2156 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2159 /* Move "n" divs starting at "first" to the end of the list of divs.
2161 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2162 unsigned first
, unsigned n
)
2167 if (first
+ n
== bmap
->n_div
)
2170 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2173 for (i
= 0; i
< n
; ++i
)
2174 div
[i
] = bmap
->div
[first
+ i
];
2175 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2176 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2177 for (i
= 0; i
< n
; ++i
)
2178 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2182 isl_basic_map_free(bmap
);
2186 /* Check that there are "n" dimensions of type "type" starting at "first"
2189 static isl_stat
isl_map_check_range(__isl_keep isl_map
*map
,
2190 enum isl_dim_type type
, unsigned first
, unsigned n
)
2193 return isl_stat_error
;
2194 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2195 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2196 "position or range out of bounds",
2197 return isl_stat_error
);
2201 /* Drop "n" dimensions of type "type" starting at "first".
2202 * Perform the core computation, without cowing or
2203 * simplifying and finalizing the result.
2205 * In principle, this frees up some extra variables as the number
2206 * of columns remains constant, but we would have to extend
2207 * the div array too as the number of rows in this array is assumed
2208 * to be equal to extra.
2210 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2211 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2212 unsigned first
, unsigned n
)
2218 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2219 return isl_basic_map_free(bmap
);
2221 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2222 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2223 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2224 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2226 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2227 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2229 for (i
= 0; i
< bmap
->n_div
; ++i
)
2230 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2232 if (type
== isl_dim_div
) {
2233 bmap
= move_divs_last(bmap
, first
, n
);
2236 if (isl_basic_map_free_div(bmap
, n
) < 0)
2237 return isl_basic_map_free(bmap
);
2239 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2241 return isl_basic_map_free(bmap
);
2243 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2244 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2248 /* Drop "n" dimensions of type "type" starting at "first".
2250 * In principle, this frees up some extra variables as the number
2251 * of columns remains constant, but we would have to extend
2252 * the div array too as the number of rows in this array is assumed
2253 * to be equal to extra.
2255 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2256 enum isl_dim_type type
, unsigned first
, unsigned n
)
2260 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2263 bmap
= isl_basic_map_cow(bmap
);
2267 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2269 bmap
= isl_basic_map_simplify(bmap
);
2270 return isl_basic_map_finalize(bmap
);
2273 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2274 enum isl_dim_type type
, unsigned first
, unsigned n
)
2276 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2280 /* No longer consider "map" to be normalized.
2282 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2286 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2290 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2291 enum isl_dim_type type
, unsigned first
, unsigned n
)
2295 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2296 return isl_map_free(map
);
2298 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2300 map
= isl_map_cow(map
);
2303 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2307 for (i
= 0; i
< map
->n
; ++i
) {
2308 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2312 map
= isl_map_unmark_normalized(map
);
2320 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2321 enum isl_dim_type type
, unsigned first
, unsigned n
)
2323 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2326 /* Drop the integer division at position "div", which is assumed
2327 * not to appear in any of the constraints or
2328 * in any of the other integer divisions.
2330 * Since the integer division is redundant, there is no need to cow.
2332 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2333 __isl_take isl_basic_map
*bmap
, unsigned div
)
2335 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2338 /* Eliminate the specified n dimensions starting at first from the
2339 * constraints, without removing the dimensions from the space.
2340 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2342 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2343 enum isl_dim_type type
, unsigned first
, unsigned n
)
2350 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2351 return isl_map_free(map
);
2353 map
= isl_map_cow(map
);
2357 for (i
= 0; i
< map
->n
; ++i
) {
2358 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2368 /* Eliminate the specified n dimensions starting at first from the
2369 * constraints, without removing the dimensions from the space.
2370 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2372 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2373 enum isl_dim_type type
, unsigned first
, unsigned n
)
2375 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2378 /* Eliminate the specified n dimensions starting at first from the
2379 * constraints, without removing the dimensions from the space.
2380 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2382 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2383 unsigned first
, unsigned n
)
2385 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2388 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2389 __isl_take isl_basic_map
*bmap
)
2393 bmap
= isl_basic_map_eliminate_vars(bmap
,
2394 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2398 return isl_basic_map_finalize(bmap
);
2401 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2402 __isl_take isl_basic_set
*bset
)
2404 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2407 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2416 map
= isl_map_cow(map
);
2420 for (i
= 0; i
< map
->n
; ++i
) {
2421 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2431 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2433 return isl_map_remove_divs(set
);
2436 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2437 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2438 unsigned first
, unsigned n
)
2440 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2441 return isl_basic_map_free(bmap
);
2442 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2444 bmap
= isl_basic_map_eliminate_vars(bmap
,
2445 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2448 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2450 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2454 /* Return true if the definition of the given div (recursively) involves
2455 * any of the given variables.
2457 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2458 unsigned first
, unsigned n
)
2461 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2463 if (isl_int_is_zero(bmap
->div
[div
][0]))
2464 return isl_bool_false
;
2465 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2466 return isl_bool_true
;
2468 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2471 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2473 involves
= div_involves_vars(bmap
, i
, first
, n
);
2474 if (involves
< 0 || involves
)
2478 return isl_bool_false
;
2481 /* Try and add a lower and/or upper bound on "div" to "bmap"
2482 * based on inequality "i".
2483 * "total" is the total number of variables (excluding the divs).
2484 * "v" is a temporary object that can be used during the calculations.
2485 * If "lb" is set, then a lower bound should be constructed.
2486 * If "ub" is set, then an upper bound should be constructed.
2488 * The calling function has already checked that the inequality does not
2489 * reference "div", but we still need to check that the inequality is
2490 * of the right form. We'll consider the case where we want to construct
2491 * a lower bound. The construction of upper bounds is similar.
2493 * Let "div" be of the form
2495 * q = floor((a + f(x))/d)
2497 * We essentially check if constraint "i" is of the form
2501 * so that we can use it to derive a lower bound on "div".
2502 * However, we allow a slightly more general form
2506 * with the condition that the coefficients of g(x) - f(x) are all
2508 * Rewriting this constraint as
2512 * adding a + f(x) to both sides and dividing by d, we obtain
2514 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2516 * Taking the floor on both sides, we obtain
2518 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2522 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2524 * In the case of an upper bound, we construct the constraint
2526 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2529 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2530 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2531 unsigned total
, isl_int v
, int lb
, int ub
)
2535 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2537 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2538 bmap
->div
[div
][1 + 1 + j
]);
2539 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2542 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2543 bmap
->div
[div
][1 + 1 + j
]);
2544 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2550 bmap
= isl_basic_map_cow(bmap
);
2551 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2553 int k
= isl_basic_map_alloc_inequality(bmap
);
2556 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2557 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2558 bmap
->div
[div
][1 + j
]);
2559 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2560 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2562 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2565 int k
= isl_basic_map_alloc_inequality(bmap
);
2568 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2569 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2570 bmap
->div
[div
][1 + j
]);
2571 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2572 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2574 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2579 isl_basic_map_free(bmap
);
2583 /* This function is called right before "div" is eliminated from "bmap"
2584 * using Fourier-Motzkin.
2585 * Look through the constraints of "bmap" for constraints on the argument
2586 * of the integer division and use them to construct constraints on the
2587 * integer division itself. These constraints can then be combined
2588 * during the Fourier-Motzkin elimination.
2589 * Note that it is only useful to introduce lower bounds on "div"
2590 * if "bmap" already contains upper bounds on "div" as the newly
2591 * introduce lower bounds can then be combined with the pre-existing
2592 * upper bounds. Similarly for upper bounds.
2593 * We therefore first check if "bmap" contains any lower and/or upper bounds
2596 * It is interesting to note that the introduction of these constraints
2597 * can indeed lead to more accurate results, even when compared to
2598 * deriving constraints on the argument of "div" from constraints on "div".
2599 * Consider, for example, the set
2601 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2603 * The second constraint can be rewritten as
2605 * 2 * [(-i-2j+3)/4] + k >= 0
2607 * from which we can derive
2609 * -i - 2j + 3 >= -2k
2615 * Combined with the first constraint, we obtain
2617 * -3 <= 3 + 2k or k >= -3
2619 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2620 * the first constraint, we obtain
2622 * [(i + 2j)/4] >= [-3/4] = -1
2624 * Combining this constraint with the second constraint, we obtain
2628 static __isl_give isl_basic_map
*insert_bounds_on_div(
2629 __isl_take isl_basic_map
*bmap
, int div
)
2632 int check_lb
, check_ub
;
2639 if (isl_int_is_zero(bmap
->div
[div
][0]))
2642 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2646 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2647 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2654 if (!check_lb
&& !check_ub
)
2659 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2660 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2663 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2664 check_lb
, check_ub
);
2672 /* Remove all divs (recursively) involving any of the given dimensions
2673 * in their definitions.
2675 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2676 __isl_take isl_basic_map
*bmap
,
2677 enum isl_dim_type type
, unsigned first
, unsigned n
)
2681 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2682 return isl_basic_map_free(bmap
);
2683 first
+= isl_basic_map_offset(bmap
, type
);
2685 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2688 involves
= div_involves_vars(bmap
, i
, first
, n
);
2690 return isl_basic_map_free(bmap
);
2693 bmap
= insert_bounds_on_div(bmap
, i
);
2694 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2703 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2704 __isl_take isl_basic_set
*bset
,
2705 enum isl_dim_type type
, unsigned first
, unsigned n
)
2707 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2710 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2711 enum isl_dim_type type
, unsigned first
, unsigned n
)
2720 map
= isl_map_cow(map
);
2724 for (i
= 0; i
< map
->n
; ++i
) {
2725 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2736 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2737 enum isl_dim_type type
, unsigned first
, unsigned n
)
2739 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2743 /* Does the description of "bmap" depend on the specified dimensions?
2744 * We also check whether the dimensions appear in any of the div definitions.
2745 * In principle there is no need for this check. If the dimensions appear
2746 * in a div definition, they also appear in the defining constraints of that
2749 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2750 enum isl_dim_type type
, unsigned first
, unsigned n
)
2754 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2755 return isl_bool_error
;
2757 first
+= isl_basic_map_offset(bmap
, type
);
2758 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2759 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2760 return isl_bool_true
;
2761 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2762 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2763 return isl_bool_true
;
2764 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2765 if (isl_int_is_zero(bmap
->div
[i
][0]))
2767 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2768 return isl_bool_true
;
2771 return isl_bool_false
;
2774 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2775 enum isl_dim_type type
, unsigned first
, unsigned n
)
2779 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2780 return isl_bool_error
;
2782 for (i
= 0; i
< map
->n
; ++i
) {
2783 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2785 if (involves
< 0 || involves
)
2789 return isl_bool_false
;
2792 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2793 enum isl_dim_type type
, unsigned first
, unsigned n
)
2795 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2798 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2799 enum isl_dim_type type
, unsigned first
, unsigned n
)
2801 return isl_map_involves_dims(set
, type
, first
, n
);
2804 /* Drop all constraints in bmap that involve any of the dimensions
2805 * first to first+n-1.
2806 * This function only performs the actual removal of constraints.
2808 * This function should not call finalize since it is used by
2809 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2811 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2812 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2819 bmap
= isl_basic_map_cow(bmap
);
2824 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2825 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2827 isl_basic_map_drop_equality(bmap
, i
);
2830 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2831 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2833 isl_basic_map_drop_inequality(bmap
, i
);
2839 /* Drop all constraints in bset that involve any of the dimensions
2840 * first to first+n-1.
2841 * This function only performs the actual removal of constraints.
2843 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2844 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2846 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2849 /* Drop all constraints in bmap that do not involve any of the dimensions
2850 * first to first + n - 1 of the given type.
2852 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2853 __isl_take isl_basic_map
*bmap
,
2854 enum isl_dim_type type
, unsigned first
, unsigned n
)
2859 isl_space
*space
= isl_basic_map_get_space(bmap
);
2860 isl_basic_map_free(bmap
);
2861 return isl_basic_map_universe(space
);
2863 bmap
= isl_basic_map_cow(bmap
);
2867 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2868 return isl_basic_map_free(bmap
);
2870 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2872 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2873 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2875 isl_basic_map_drop_equality(bmap
, i
);
2878 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2879 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2881 isl_basic_map_drop_inequality(bmap
, i
);
2884 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2888 /* Drop all constraints in bset that do not involve any of the dimensions
2889 * first to first + n - 1 of the given type.
2891 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2892 __isl_take isl_basic_set
*bset
,
2893 enum isl_dim_type type
, unsigned first
, unsigned n
)
2895 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2899 /* Drop all constraints in bmap that involve any of the dimensions
2900 * first to first + n - 1 of the given type.
2902 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2903 __isl_take isl_basic_map
*bmap
,
2904 enum isl_dim_type type
, unsigned first
, unsigned n
)
2911 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2912 return isl_basic_map_free(bmap
);
2914 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2915 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2916 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2917 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2921 /* Drop all constraints in bset that involve any of the dimensions
2922 * first to first + n - 1 of the given type.
2924 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2925 __isl_take isl_basic_set
*bset
,
2926 enum isl_dim_type type
, unsigned first
, unsigned n
)
2928 return isl_basic_map_drop_constraints_involving_dims(bset
,
2932 /* Drop constraints from "map" by applying "drop" to each basic map.
2934 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2935 enum isl_dim_type type
, unsigned first
, unsigned n
,
2936 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2937 enum isl_dim_type type
, unsigned first
, unsigned n
))
2941 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2942 return isl_map_free(map
);
2944 map
= isl_map_cow(map
);
2948 for (i
= 0; i
< map
->n
; ++i
) {
2949 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2951 return isl_map_free(map
);
2955 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2960 /* Drop all constraints in map that involve any of the dimensions
2961 * first to first + n - 1 of the given type.
2963 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2964 __isl_take isl_map
*map
,
2965 enum isl_dim_type type
, unsigned first
, unsigned n
)
2969 return drop_constraints(map
, type
, first
, n
,
2970 &isl_basic_map_drop_constraints_involving_dims
);
2973 /* Drop all constraints in "map" that do not involve any of the dimensions
2974 * first to first + n - 1 of the given type.
2976 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2977 __isl_take isl_map
*map
,
2978 enum isl_dim_type type
, unsigned first
, unsigned n
)
2981 isl_space
*space
= isl_map_get_space(map
);
2983 return isl_map_universe(space
);
2985 return drop_constraints(map
, type
, first
, n
,
2986 &isl_basic_map_drop_constraints_not_involving_dims
);
2989 /* Drop all constraints in set that involve any of the dimensions
2990 * first to first + n - 1 of the given type.
2992 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2993 __isl_take isl_set
*set
,
2994 enum isl_dim_type type
, unsigned first
, unsigned n
)
2996 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2999 /* Drop all constraints in "set" that do not involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3003 __isl_take isl_set
*set
,
3004 enum isl_dim_type type
, unsigned first
, unsigned n
)
3006 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3009 /* Does local variable "div" of "bmap" have a complete explicit representation?
3010 * Having a complete explicit representation requires not only
3011 * an explicit representation, but also that all local variables
3012 * that appear in this explicit representation in turn have
3013 * a complete explicit representation.
3015 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3018 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3021 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3022 if (marked
< 0 || marked
)
3023 return isl_bool_not(marked
);
3025 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3028 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3030 known
= isl_basic_map_div_is_known(bmap
, i
);
3031 if (known
< 0 || !known
)
3035 return isl_bool_true
;
3038 /* Remove all divs that are unknown or defined in terms of unknown divs.
3040 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3041 __isl_take isl_basic_map
*bmap
)
3048 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3049 if (isl_basic_map_div_is_known(bmap
, i
))
3051 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3060 /* Remove all divs that are unknown or defined in terms of unknown divs.
3062 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3063 __isl_take isl_basic_set
*bset
)
3065 return isl_basic_map_remove_unknown_divs(bset
);
3068 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3077 map
= isl_map_cow(map
);
3081 for (i
= 0; i
< map
->n
; ++i
) {
3082 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3092 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3094 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3097 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3098 __isl_take isl_basic_set
*bset
,
3099 enum isl_dim_type type
, unsigned first
, unsigned n
)
3101 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3102 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3103 return bset_from_bmap(bmap
);
3106 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3107 enum isl_dim_type type
, unsigned first
, unsigned n
)
3114 map
= isl_map_cow(map
);
3115 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3116 return isl_map_free(map
);
3118 for (i
= 0; i
< map
->n
; ++i
) {
3119 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3120 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3124 map
= isl_map_drop(map
, type
, first
, n
);
3131 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3132 enum isl_dim_type type
, unsigned first
, unsigned n
)
3134 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3138 /* Project out n inputs starting at first using Fourier-Motzkin */
3139 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3140 unsigned first
, unsigned n
)
3142 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3145 static void dump_term(struct isl_basic_map
*bmap
,
3146 isl_int c
, int pos
, FILE *out
)
3149 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3150 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3151 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3153 isl_int_print(out
, c
, 0);
3155 if (!isl_int_is_one(c
))
3156 isl_int_print(out
, c
, 0);
3157 if (pos
< 1 + nparam
) {
3158 name
= isl_space_get_dim_name(bmap
->dim
,
3159 isl_dim_param
, pos
- 1);
3161 fprintf(out
, "%s", name
);
3163 fprintf(out
, "p%d", pos
- 1);
3164 } else if (pos
< 1 + nparam
+ in
)
3165 fprintf(out
, "i%d", pos
- 1 - nparam
);
3166 else if (pos
< 1 + nparam
+ dim
)
3167 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3169 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3173 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3174 int sign
, FILE *out
)
3178 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3182 for (i
= 0, first
= 1; i
< len
; ++i
) {
3183 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3186 fprintf(out
, " + ");
3188 isl_int_abs(v
, c
[i
]);
3189 dump_term(bmap
, v
, i
, out
);
3196 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3197 const char *op
, FILE *out
, int indent
)
3201 fprintf(out
, "%*s", indent
, "");
3203 dump_constraint_sign(bmap
, c
, 1, out
);
3204 fprintf(out
, " %s ", op
);
3205 dump_constraint_sign(bmap
, c
, -1, out
);
3209 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3210 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3212 fprintf(out
, "%*s", indent
, "");
3213 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3218 static void dump_constraints(struct isl_basic_map
*bmap
,
3219 isl_int
**c
, unsigned n
,
3220 const char *op
, FILE *out
, int indent
)
3224 for (i
= 0; i
< n
; ++i
)
3225 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3228 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3232 unsigned total
= isl_basic_map_total_dim(bmap
);
3234 for (j
= 0; j
< 1 + total
; ++j
) {
3235 if (isl_int_is_zero(exp
[j
]))
3237 if (!first
&& isl_int_is_pos(exp
[j
]))
3239 dump_term(bmap
, exp
[j
], j
, out
);
3244 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3248 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3249 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3251 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3252 fprintf(out
, "%*s", indent
, "");
3253 fprintf(out
, "e%d = [(", i
);
3254 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3256 isl_int_print(out
, bmap
->div
[i
][0], 0);
3257 fprintf(out
, "]\n");
3261 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3262 FILE *out
, int indent
)
3265 fprintf(out
, "null basic set\n");
3269 fprintf(out
, "%*s", indent
, "");
3270 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3271 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3272 bset
->extra
, bset
->flags
);
3273 dump(bset_to_bmap(bset
), out
, indent
);
3276 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3277 FILE *out
, int indent
)
3280 fprintf(out
, "null basic map\n");
3284 fprintf(out
, "%*s", indent
, "");
3285 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3286 "flags: %x, n_name: %d\n",
3288 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3289 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3290 dump(bmap
, out
, indent
);
3293 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3298 total
= isl_basic_map_total_dim(bmap
);
3299 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3300 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3301 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3302 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3303 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3307 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3310 if (isl_space_check_is_set(space
) < 0)
3312 return isl_map_alloc_space(space
, n
, flags
);
3314 isl_space_free(space
);
3318 /* Make sure "map" has room for at least "n" more basic maps.
3320 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3323 struct isl_map
*grown
= NULL
;
3327 isl_assert(map
->ctx
, n
>= 0, goto error
);
3328 if (map
->n
+ n
<= map
->size
)
3330 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3333 for (i
= 0; i
< map
->n
; ++i
) {
3334 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3342 isl_map_free(grown
);
3347 /* Make sure "set" has room for at least "n" more basic sets.
3349 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3351 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3354 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3356 return isl_map_from_basic_map(bset
);
3359 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3361 struct isl_map
*map
;
3366 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3367 return isl_map_add_basic_map(map
, bmap
);
3370 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3371 __isl_take isl_basic_set
*bset
)
3373 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3374 bset_to_bmap(bset
)));
3377 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3379 return isl_map_free(set
);
3382 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3387 fprintf(out
, "null set\n");
3391 fprintf(out
, "%*s", indent
, "");
3392 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3393 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3395 for (i
= 0; i
< set
->n
; ++i
) {
3396 fprintf(out
, "%*s", indent
, "");
3397 fprintf(out
, "basic set %d:\n", i
);
3398 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3402 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3407 fprintf(out
, "null map\n");
3411 fprintf(out
, "%*s", indent
, "");
3412 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3413 "flags: %x, n_name: %d\n",
3414 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3415 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3416 for (i
= 0; i
< map
->n
; ++i
) {
3417 fprintf(out
, "%*s", indent
, "");
3418 fprintf(out
, "basic map %d:\n", i
);
3419 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3423 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3424 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3426 struct isl_basic_map
*bmap_domain
;
3428 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3431 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3432 isl_assert(bset
->ctx
,
3433 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3435 bmap
= isl_basic_map_cow(bmap
);
3438 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3439 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3440 bmap_domain
= isl_basic_map_from_domain(bset
);
3441 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3443 bmap
= isl_basic_map_simplify(bmap
);
3444 return isl_basic_map_finalize(bmap
);
3446 isl_basic_map_free(bmap
);
3447 isl_basic_set_free(bset
);
3451 /* Check that the space of "bset" is the same as that of the range of "bmap".
3453 static isl_stat
isl_basic_map_check_compatible_range(
3454 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3458 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3460 return isl_stat_error
;
3462 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3463 "incompatible spaces", return isl_stat_error
);
3468 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3469 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3471 struct isl_basic_map
*bmap_range
;
3473 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3476 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3477 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3480 if (isl_basic_set_plain_is_universe(bset
)) {
3481 isl_basic_set_free(bset
);
3485 bmap
= isl_basic_map_cow(bmap
);
3488 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3489 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3490 bmap_range
= bset_to_bmap(bset
);
3491 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3493 bmap
= isl_basic_map_simplify(bmap
);
3494 return isl_basic_map_finalize(bmap
);
3496 isl_basic_map_free(bmap
);
3497 isl_basic_set_free(bset
);
3501 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3502 __isl_keep isl_vec
*vec
)
3509 return isl_bool_error
;
3511 total
= 1 + isl_basic_map_total_dim(bmap
);
3512 if (total
!= vec
->size
)
3513 return isl_bool_false
;
3517 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3518 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3519 if (!isl_int_is_zero(s
)) {
3521 return isl_bool_false
;
3525 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3526 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3527 if (isl_int_is_neg(s
)) {
3529 return isl_bool_false
;
3535 return isl_bool_true
;
3538 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3539 __isl_keep isl_vec
*vec
)
3541 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3544 __isl_give isl_basic_map
*isl_basic_map_intersect(
3545 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3547 struct isl_vec
*sample
= NULL
;
3549 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3551 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3552 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3553 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3554 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3555 return isl_basic_map_intersect(bmap2
, bmap1
);
3557 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3558 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3559 isl_assert(bmap1
->ctx
,
3560 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3562 if (isl_basic_map_plain_is_empty(bmap1
)) {
3563 isl_basic_map_free(bmap2
);
3566 if (isl_basic_map_plain_is_empty(bmap2
)) {
3567 isl_basic_map_free(bmap1
);
3571 if (bmap1
->sample
&&
3572 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3573 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3574 sample
= isl_vec_copy(bmap1
->sample
);
3575 else if (bmap2
->sample
&&
3576 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3577 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3578 sample
= isl_vec_copy(bmap2
->sample
);
3580 bmap1
= isl_basic_map_cow(bmap1
);
3583 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3584 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3585 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3588 isl_vec_free(sample
);
3590 isl_vec_free(bmap1
->sample
);
3591 bmap1
->sample
= sample
;
3594 bmap1
= isl_basic_map_simplify(bmap1
);
3595 return isl_basic_map_finalize(bmap1
);
3598 isl_vec_free(sample
);
3599 isl_basic_map_free(bmap1
);
3600 isl_basic_map_free(bmap2
);
3604 struct isl_basic_set
*isl_basic_set_intersect(
3605 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3607 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3608 bset_to_bmap(bset2
)));
3611 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3612 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3614 return isl_basic_set_intersect(bset1
, bset2
);
3617 /* Special case of isl_map_intersect, where both map1 and map2
3618 * are convex, without any divs and such that either map1 or map2
3619 * contains a single constraint. This constraint is then simply
3620 * added to the other map.
3622 static __isl_give isl_map
*map_intersect_add_constraint(
3623 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3625 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3626 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3627 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3628 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3630 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3631 return isl_map_intersect(map2
, map1
);
3633 map1
= isl_map_cow(map1
);
3636 if (isl_map_plain_is_empty(map1
)) {
3640 if (map2
->p
[0]->n_eq
== 1)
3641 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3643 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3644 map2
->p
[0]->ineq
[0]);
3646 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3647 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3651 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3652 isl_basic_map_free(map1
->p
[0]);
3658 map1
= isl_map_unmark_normalized(map1
);
3666 /* map2 may be either a parameter domain or a map living in the same
3669 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3670 __isl_take isl_map
*map2
)
3680 if ((isl_map_plain_is_empty(map1
) ||
3681 isl_map_plain_is_universe(map2
)) &&
3682 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3686 if ((isl_map_plain_is_empty(map2
) ||
3687 isl_map_plain_is_universe(map1
)) &&
3688 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3693 if (map1
->n
== 1 && map2
->n
== 1 &&
3694 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3695 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3696 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3697 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3698 return map_intersect_add_constraint(map1
, map2
);
3700 equal
= isl_map_plain_is_equal(map1
, map2
);
3708 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3709 isl_space_dim(map2
->dim
, isl_dim_param
))
3710 isl_assert(map1
->ctx
,
3711 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3713 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3714 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3715 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3717 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3718 map1
->n
* map2
->n
, flags
);
3721 for (i
= 0; i
< map1
->n
; ++i
)
3722 for (j
= 0; j
< map2
->n
; ++j
) {
3723 struct isl_basic_map
*part
;
3724 part
= isl_basic_map_intersect(
3725 isl_basic_map_copy(map1
->p
[i
]),
3726 isl_basic_map_copy(map2
->p
[j
]));
3727 if (isl_basic_map_is_empty(part
) < 0)
3728 part
= isl_basic_map_free(part
);
3729 result
= isl_map_add_basic_map(result
, part
);
3742 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3743 __isl_take isl_map
*map2
)
3747 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3748 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3749 "spaces don't match", goto error
);
3750 return map_intersect_internal(map1
, map2
);
3757 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3758 __isl_take isl_map
*map2
)
3760 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3763 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3765 return set_from_map(isl_map_intersect(set_to_map(set1
),
3769 /* map_intersect_internal accepts intersections
3770 * with parameter domains, so we can just call that function.
3772 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3773 __isl_take isl_set
*params
)
3775 return map_intersect_internal(map
, params
);
3778 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3779 __isl_take isl_map
*map2
)
3781 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3784 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3785 __isl_take isl_set
*params
)
3787 return isl_map_intersect_params(set
, params
);
3790 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3793 unsigned pos
, n1
, n2
;
3797 bmap
= isl_basic_map_cow(bmap
);
3800 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3801 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3802 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3803 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3804 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3805 return isl_basic_map_reset_space(bmap
, space
);
3808 static __isl_give isl_basic_map
*basic_map_space_reset(
3809 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3815 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3818 space
= isl_basic_map_get_space(bmap
);
3819 space
= isl_space_reset(space
, type
);
3820 bmap
= isl_basic_map_reset_space(bmap
, space
);
3824 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3825 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3826 unsigned pos
, unsigned n
)
3829 isl_space
*res_space
;
3830 struct isl_basic_map
*res
;
3831 struct isl_dim_map
*dim_map
;
3832 unsigned total
, off
;
3833 enum isl_dim_type t
;
3836 return basic_map_space_reset(bmap
, type
);
3838 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3841 return isl_basic_map_free(bmap
);
3843 total
= isl_basic_map_total_dim(bmap
) + n
;
3844 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3846 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3848 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3850 unsigned size
= isl_basic_map_dim(bmap
, t
);
3851 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3853 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3854 pos
, size
- pos
, off
+ pos
+ n
);
3856 off
+= isl_space_dim(res_space
, t
);
3858 isl_dim_map_div(dim_map
, bmap
, off
);
3860 res
= isl_basic_map_alloc_space(res_space
,
3861 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3862 rational
= isl_basic_map_is_rational(bmap
);
3864 res
= isl_basic_map_free(res
);
3866 res
= isl_basic_map_set_rational(res
);
3867 if (isl_basic_map_plain_is_empty(bmap
)) {
3868 isl_basic_map_free(bmap
);
3870 return isl_basic_map_set_to_empty(res
);
3872 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3873 return isl_basic_map_finalize(res
);
3876 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3877 __isl_take isl_basic_set
*bset
,
3878 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3880 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3883 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3884 enum isl_dim_type type
, unsigned n
)
3888 return isl_basic_map_insert_dims(bmap
, type
,
3889 isl_basic_map_dim(bmap
, type
), n
);
3892 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3893 enum isl_dim_type type
, unsigned n
)
3897 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3898 return isl_basic_map_add_dims(bset
, type
, n
);
3900 isl_basic_set_free(bset
);
3904 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3905 enum isl_dim_type type
)
3909 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3912 space
= isl_map_get_space(map
);
3913 space
= isl_space_reset(space
, type
);
3914 map
= isl_map_reset_space(map
, space
);
3918 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3919 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3924 return map_space_reset(map
, type
);
3926 map
= isl_map_cow(map
);
3930 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3934 for (i
= 0; i
< map
->n
; ++i
) {
3935 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3946 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3947 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3949 return isl_map_insert_dims(set
, type
, pos
, n
);
3952 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3953 enum isl_dim_type type
, unsigned n
)
3957 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3960 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3961 enum isl_dim_type type
, unsigned n
)
3965 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3966 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3972 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3973 __isl_take isl_basic_map
*bmap
,
3974 enum isl_dim_type dst_type
, unsigned dst_pos
,
3975 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3977 struct isl_dim_map
*dim_map
;
3978 struct isl_basic_map
*res
;
3979 enum isl_dim_type t
;
3980 unsigned total
, off
;
3985 bmap
= isl_basic_map_reset(bmap
, src_type
);
3986 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3990 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3991 return isl_basic_map_free(bmap
);
3993 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3996 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3998 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3999 pos(bmap
->dim
, src_type
) + src_pos
+
4000 ((src_type
< dst_type
) ? n
: 0)) {
4001 bmap
= isl_basic_map_cow(bmap
);
4005 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4006 src_type
, src_pos
, n
);
4010 bmap
= isl_basic_map_finalize(bmap
);
4015 total
= isl_basic_map_total_dim(bmap
);
4016 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4019 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4020 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4021 if (t
== dst_type
) {
4022 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4025 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4028 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4029 dst_pos
, size
- dst_pos
, off
);
4030 off
+= size
- dst_pos
;
4031 } else if (t
== src_type
) {
4032 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4035 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4036 src_pos
+ n
, size
- src_pos
- n
, off
);
4037 off
+= size
- src_pos
- n
;
4039 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4043 isl_dim_map_div(dim_map
, bmap
, off
);
4045 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4046 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4047 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4051 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4052 src_type
, src_pos
, n
);
4056 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4057 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4058 bmap
= isl_basic_map_finalize(bmap
);
4062 isl_basic_map_free(bmap
);
4066 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4067 enum isl_dim_type dst_type
, unsigned dst_pos
,
4068 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4070 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4071 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4072 src_type
, src_pos
, n
);
4073 return bset_from_bmap(bmap
);
4076 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4077 enum isl_dim_type dst_type
, unsigned dst_pos
,
4078 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4082 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4083 return set_from_map(isl_map_move_dims(set_to_map(set
),
4084 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4090 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4091 enum isl_dim_type dst_type
, unsigned dst_pos
,
4092 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4097 map
= isl_map_reset(map
, src_type
);
4098 map
= isl_map_reset(map
, dst_type
);
4102 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4103 return isl_map_free(map
);
4105 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4108 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4110 map
= isl_map_cow(map
);
4114 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4118 for (i
= 0; i
< map
->n
; ++i
) {
4119 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4121 src_type
, src_pos
, n
);
4132 /* Move the specified dimensions to the last columns right before
4133 * the divs. Don't change the dimension specification of bmap.
4134 * That's the responsibility of the caller.
4136 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4137 enum isl_dim_type type
, unsigned first
, unsigned n
)
4139 struct isl_dim_map
*dim_map
;
4140 struct isl_basic_map
*res
;
4141 enum isl_dim_type t
;
4142 unsigned total
, off
;
4146 if (pos(bmap
->dim
, type
) + first
+ n
==
4147 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4150 total
= isl_basic_map_total_dim(bmap
);
4151 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4154 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4155 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4157 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4160 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4161 first
, n
, total
- bmap
->n_div
- n
);
4162 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4163 first
+ n
, size
- (first
+ n
), off
);
4164 off
+= size
- (first
+ n
);
4166 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4170 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4172 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4173 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4174 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4178 /* Insert "n" rows in the divs of "bmap".
4180 * The number of columns is not changed, which means that the last
4181 * dimensions of "bmap" are being reintepreted as the new divs.
4182 * The space of "bmap" is not adjusted, however, which means
4183 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4184 * from the space of "bmap" is the responsibility of the caller.
4186 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4194 bmap
= isl_basic_map_cow(bmap
);
4198 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4199 old
= bmap
->block2
.data
;
4200 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4201 (bmap
->extra
+ n
) * (1 + row_size
));
4202 if (!bmap
->block2
.data
)
4203 return isl_basic_map_free(bmap
);
4204 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4206 return isl_basic_map_free(bmap
);
4207 for (i
= 0; i
< n
; ++i
) {
4208 new_div
[i
] = bmap
->block2
.data
+
4209 (bmap
->extra
+ i
) * (1 + row_size
);
4210 isl_seq_clr(new_div
[i
], 1 + row_size
);
4212 for (i
= 0; i
< bmap
->extra
; ++i
)
4213 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4215 bmap
->div
= new_div
;
4222 /* Drop constraints from "bmap" that only involve the variables
4223 * of "type" in the range [first, first + n] that are not related
4224 * to any of the variables outside that interval.
4225 * These constraints cannot influence the values for the variables
4226 * outside the interval, except in case they cause "bmap" to be empty.
4227 * Only drop the constraints if "bmap" is known to be non-empty.
4229 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4230 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4231 unsigned first
, unsigned n
)
4235 unsigned dim
, n_div
;
4238 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4240 return isl_basic_map_free(bmap
);
4244 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4245 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4246 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4248 return isl_basic_map_free(bmap
);
4249 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4250 for (i
= 0; i
< first
; ++i
)
4252 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4255 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4260 /* Turn the n dimensions of type type, starting at first
4261 * into existentially quantified variables.
4263 * If a subset of the projected out variables are unrelated
4264 * to any of the variables that remain, then the constraints
4265 * involving this subset are simply dropped first.
4267 __isl_give isl_basic_map
*isl_basic_map_project_out(
4268 __isl_take isl_basic_map
*bmap
,
4269 enum isl_dim_type type
, unsigned first
, unsigned n
)
4274 return basic_map_space_reset(bmap
, type
);
4275 if (type
== isl_dim_div
)
4276 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4277 "cannot project out existentially quantified variables",
4278 return isl_basic_map_free(bmap
));
4280 empty
= isl_basic_map_plain_is_empty(bmap
);
4282 return isl_basic_map_free(bmap
);
4284 bmap
= isl_basic_map_set_to_empty(bmap
);
4286 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4290 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4291 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4293 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4294 return isl_basic_map_free(bmap
);
4296 bmap
= move_last(bmap
, type
, first
, n
);
4297 bmap
= isl_basic_map_cow(bmap
);
4298 bmap
= insert_div_rows(bmap
, n
);
4302 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4305 bmap
= isl_basic_map_simplify(bmap
);
4306 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4307 return isl_basic_map_finalize(bmap
);
4309 isl_basic_map_free(bmap
);
4313 /* Turn the n dimensions of type type, starting at first
4314 * into existentially quantified variables.
4316 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4317 enum isl_dim_type type
, unsigned first
, unsigned n
)
4319 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4323 /* Turn the n dimensions of type type, starting at first
4324 * into existentially quantified variables.
4326 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4327 enum isl_dim_type type
, unsigned first
, unsigned n
)
4332 return map_space_reset(map
, type
);
4334 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4335 return isl_map_free(map
);
4337 map
= isl_map_cow(map
);
4341 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4345 for (i
= 0; i
< map
->n
; ++i
) {
4346 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4357 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4358 * into existentially quantified variables.
4360 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4361 enum isl_dim_type type
, unsigned first
, unsigned n
)
4365 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4366 return isl_map_free(map
);
4367 dim
= isl_map_dim(map
, type
);
4368 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4369 map
= isl_map_project_out(map
, type
, 0, first
);
4373 /* Turn the n dimensions of type type, starting at first
4374 * into existentially quantified variables.
4376 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4377 enum isl_dim_type type
, unsigned first
, unsigned n
)
4379 return set_from_map(isl_map_project_out(set_to_map(set
),
4383 /* Return a map that projects the elements in "set" onto their
4384 * "n" set dimensions starting at "first".
4385 * "type" should be equal to isl_dim_set.
4387 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4388 enum isl_dim_type type
, unsigned first
, unsigned n
)
4396 if (type
!= isl_dim_set
)
4397 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4398 "only set dimensions can be projected out", goto error
);
4399 dim
= isl_set_dim(set
, isl_dim_set
);
4400 if (first
+ n
> dim
|| first
+ n
< first
)
4401 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4402 "index out of bounds", goto error
);
4404 map
= isl_map_from_domain(set
);
4405 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4406 for (i
= 0; i
< n
; ++i
)
4407 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4415 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4420 for (i
= 0; i
< n
; ++i
) {
4421 j
= isl_basic_map_alloc_div(bmap
);
4424 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4428 isl_basic_map_free(bmap
);
4432 struct isl_basic_map
*isl_basic_map_apply_range(
4433 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4435 isl_space
*space_result
= NULL
;
4436 struct isl_basic_map
*bmap
;
4437 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4438 struct isl_dim_map
*dim_map1
, *dim_map2
;
4440 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4442 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4443 bmap2
->dim
, isl_dim_in
))
4444 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4445 "spaces don't match", goto error
);
4447 space_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4448 isl_space_copy(bmap2
->dim
));
4450 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4451 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4452 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4453 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4455 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4456 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4457 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4458 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4459 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4460 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4461 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4462 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4463 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4464 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4465 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4467 bmap
= isl_basic_map_alloc_space(space_result
,
4468 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4469 bmap1
->n_eq
+ bmap2
->n_eq
,
4470 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4471 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4472 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4473 bmap
= add_divs(bmap
, n
);
4474 bmap
= isl_basic_map_simplify(bmap
);
4475 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4476 return isl_basic_map_finalize(bmap
);
4478 isl_basic_map_free(bmap1
);
4479 isl_basic_map_free(bmap2
);
4483 struct isl_basic_set
*isl_basic_set_apply(
4484 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4489 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4492 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4495 isl_basic_set_free(bset
);
4496 isl_basic_map_free(bmap
);
4500 struct isl_basic_map
*isl_basic_map_apply_domain(
4501 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4503 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4505 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4506 bmap2
->dim
, isl_dim_in
))
4507 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4508 "spaces don't match", goto error
);
4510 bmap1
= isl_basic_map_reverse(bmap1
);
4511 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4512 return isl_basic_map_reverse(bmap1
);
4514 isl_basic_map_free(bmap1
);
4515 isl_basic_map_free(bmap2
);
4519 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4520 * A \cap B -> f(A) + f(B)
4522 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4523 __isl_take isl_basic_map
*bmap2
)
4525 unsigned n_in
, n_out
, nparam
, total
, pos
;
4526 struct isl_basic_map
*bmap
= NULL
;
4527 struct isl_dim_map
*dim_map1
, *dim_map2
;
4530 if (!bmap1
|| !bmap2
)
4533 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4536 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4537 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4538 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4540 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4541 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4542 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4543 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4544 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4545 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4546 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4547 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4548 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4549 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4550 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4552 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4553 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4554 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4555 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4556 for (i
= 0; i
< n_out
; ++i
) {
4557 int j
= isl_basic_map_alloc_equality(bmap
);
4560 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4561 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4562 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4563 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4565 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4566 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4567 bmap
= add_divs(bmap
, 2 * n_out
);
4569 bmap
= isl_basic_map_simplify(bmap
);
4570 return isl_basic_map_finalize(bmap
);
4572 isl_basic_map_free(bmap
);
4573 isl_basic_map_free(bmap1
);
4574 isl_basic_map_free(bmap2
);
4578 /* Given two maps A -> f(A) and B -> g(B), construct a map
4579 * A \cap B -> f(A) + f(B)
4581 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4582 __isl_take isl_map
*map2
)
4584 struct isl_map
*result
;
4590 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4592 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4593 map1
->n
* map2
->n
, 0);
4596 for (i
= 0; i
< map1
->n
; ++i
)
4597 for (j
= 0; j
< map2
->n
; ++j
) {
4598 struct isl_basic_map
*part
;
4599 part
= isl_basic_map_sum(
4600 isl_basic_map_copy(map1
->p
[i
]),
4601 isl_basic_map_copy(map2
->p
[j
]));
4602 if (isl_basic_map_is_empty(part
))
4603 isl_basic_map_free(part
);
4605 result
= isl_map_add_basic_map(result
, part
);
4618 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4619 __isl_take isl_set
*set2
)
4621 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4624 /* Given a basic map A -> f(A), construct A -> -f(A).
4626 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4631 bmap
= isl_basic_map_cow(bmap
);
4635 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4636 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4637 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4638 for (j
= 0; j
< n
; ++j
)
4639 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4640 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4641 for (j
= 0; j
< n
; ++j
)
4642 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4643 for (i
= 0; i
< bmap
->n_div
; ++i
)
4644 for (j
= 0; j
< n
; ++j
)
4645 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4646 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4647 return isl_basic_map_finalize(bmap
);
4650 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4652 return isl_basic_map_neg(bset
);
4655 /* Given a map A -> f(A), construct A -> -f(A).
4657 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4661 map
= isl_map_cow(map
);
4665 for (i
= 0; i
< map
->n
; ++i
) {
4666 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4677 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4679 return set_from_map(isl_map_neg(set_to_map(set
)));
4682 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4683 * A -> floor(f(A)/d).
4685 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4688 unsigned n_in
, n_out
, nparam
, total
, pos
;
4689 struct isl_basic_map
*result
= NULL
;
4690 struct isl_dim_map
*dim_map
;
4696 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4697 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4698 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4700 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4701 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4702 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4703 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4704 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4705 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4707 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4708 bmap
->n_div
+ n_out
,
4709 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4710 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4711 result
= add_divs(result
, n_out
);
4712 for (i
= 0; i
< n_out
; ++i
) {
4714 j
= isl_basic_map_alloc_inequality(result
);
4717 isl_seq_clr(result
->ineq
[j
], 1+total
);
4718 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4719 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4720 j
= isl_basic_map_alloc_inequality(result
);
4723 isl_seq_clr(result
->ineq
[j
], 1+total
);
4724 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4725 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4726 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4729 result
= isl_basic_map_simplify(result
);
4730 return isl_basic_map_finalize(result
);
4732 isl_basic_map_free(result
);
4736 /* Given a map A -> f(A) and an integer d, construct a map
4737 * A -> floor(f(A)/d).
4739 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4743 map
= isl_map_cow(map
);
4747 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4748 for (i
= 0; i
< map
->n
; ++i
) {
4749 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4753 map
= isl_map_unmark_normalized(map
);
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_val(__isl_take isl_map
*map
,
4765 __isl_take isl_val
*d
)
4769 if (!isl_val_is_int(d
))
4770 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4771 "expecting integer denominator", goto error
);
4772 map
= isl_map_floordiv(map
, d
->n
);
4781 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4788 i
= isl_basic_map_alloc_equality(bmap
);
4791 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4792 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4793 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4794 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4795 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4796 return isl_basic_map_finalize(bmap
);
4798 isl_basic_map_free(bmap
);
4802 /* Add a constraint to "bmap" expressing i_pos < o_pos
4804 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4811 i
= isl_basic_map_alloc_inequality(bmap
);
4814 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4815 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4816 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4817 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4818 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4819 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4820 return isl_basic_map_finalize(bmap
);
4822 isl_basic_map_free(bmap
);
4826 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4828 static __isl_give isl_basic_map
*var_less_or_equal(
4829 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4835 i
= isl_basic_map_alloc_inequality(bmap
);
4838 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4839 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4840 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4841 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4842 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4843 return isl_basic_map_finalize(bmap
);
4845 isl_basic_map_free(bmap
);
4849 /* Add a constraint to "bmap" expressing i_pos > o_pos
4851 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4858 i
= isl_basic_map_alloc_inequality(bmap
);
4861 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4862 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4863 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4864 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4865 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4866 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4867 return isl_basic_map_finalize(bmap
);
4869 isl_basic_map_free(bmap
);
4873 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4875 static __isl_give isl_basic_map
*var_more_or_equal(
4876 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4882 i
= isl_basic_map_alloc_inequality(bmap
);
4885 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4886 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4887 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4888 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4889 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4890 return isl_basic_map_finalize(bmap
);
4892 isl_basic_map_free(bmap
);
4896 __isl_give isl_basic_map
*isl_basic_map_equal(
4897 __isl_take isl_space
*space
, unsigned n_equal
)
4900 struct isl_basic_map
*bmap
;
4901 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4904 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4905 bmap
= var_equal(bmap
, i
);
4906 return isl_basic_map_finalize(bmap
);
4909 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4911 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4915 struct isl_basic_map
*bmap
;
4916 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4919 for (i
= 0; i
< pos
&& bmap
; ++i
)
4920 bmap
= var_equal(bmap
, i
);
4922 bmap
= var_less(bmap
, pos
);
4923 return isl_basic_map_finalize(bmap
);
4926 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4928 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4929 __isl_take isl_space
*space
, unsigned pos
)
4932 isl_basic_map
*bmap
;
4934 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4935 for (i
= 0; i
< pos
; ++i
)
4936 bmap
= var_equal(bmap
, i
);
4937 bmap
= var_less_or_equal(bmap
, pos
);
4938 return isl_basic_map_finalize(bmap
);
4941 /* Return a relation on "space" expressing i_pos > o_pos
4943 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4947 struct isl_basic_map
*bmap
;
4948 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4951 for (i
= 0; i
< pos
&& bmap
; ++i
)
4952 bmap
= var_equal(bmap
, i
);
4954 bmap
= var_more(bmap
, pos
);
4955 return isl_basic_map_finalize(bmap
);
4958 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4960 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4961 __isl_take isl_space
*space
, unsigned pos
)
4964 isl_basic_map
*bmap
;
4966 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4967 for (i
= 0; i
< pos
; ++i
)
4968 bmap
= var_equal(bmap
, i
);
4969 bmap
= var_more_or_equal(bmap
, pos
);
4970 return isl_basic_map_finalize(bmap
);
4973 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4974 unsigned n
, int equal
)
4976 struct isl_map
*map
;
4979 if (n
== 0 && equal
)
4980 return isl_map_universe(space
);
4982 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4984 for (i
= 0; i
+ 1 < n
; ++i
)
4985 map
= isl_map_add_basic_map(map
,
4986 isl_basic_map_less_at(isl_space_copy(space
), i
));
4989 map
= isl_map_add_basic_map(map
,
4990 isl_basic_map_less_or_equal_at(space
, n
- 1));
4992 map
= isl_map_add_basic_map(map
,
4993 isl_basic_map_less_at(space
, n
- 1));
4995 isl_space_free(space
);
5000 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5004 return map_lex_lte_first(space
, space
->n_out
, equal
);
5007 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5009 return map_lex_lte_first(dim
, n
, 0);
5012 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5014 return map_lex_lte_first(dim
, n
, 1);
5017 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5019 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5022 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5024 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5027 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5028 unsigned n
, int equal
)
5030 struct isl_map
*map
;
5033 if (n
== 0 && equal
)
5034 return isl_map_universe(space
);
5036 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5038 for (i
= 0; i
+ 1 < n
; ++i
)
5039 map
= isl_map_add_basic_map(map
,
5040 isl_basic_map_more_at(isl_space_copy(space
), i
));
5043 map
= isl_map_add_basic_map(map
,
5044 isl_basic_map_more_or_equal_at(space
, n
- 1));
5046 map
= isl_map_add_basic_map(map
,
5047 isl_basic_map_more_at(space
, n
- 1));
5049 isl_space_free(space
);
5054 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5058 return map_lex_gte_first(space
, space
->n_out
, equal
);
5061 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5063 return map_lex_gte_first(dim
, n
, 0);
5066 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5068 return map_lex_gte_first(dim
, n
, 1);
5071 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5073 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5076 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5078 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5081 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5082 __isl_take isl_set
*set2
)
5085 map
= isl_map_lex_le(isl_set_get_space(set1
));
5086 map
= isl_map_intersect_domain(map
, set1
);
5087 map
= isl_map_intersect_range(map
, set2
);
5091 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5092 __isl_take isl_set
*set2
)
5095 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5096 map
= isl_map_intersect_domain(map
, set1
);
5097 map
= isl_map_intersect_range(map
, set2
);
5101 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5102 __isl_take isl_set
*set2
)
5105 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5106 map
= isl_map_intersect_domain(map
, set1
);
5107 map
= isl_map_intersect_range(map
, set2
);
5111 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5112 __isl_take isl_set
*set2
)
5115 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5116 map
= isl_map_intersect_domain(map
, set1
);
5117 map
= isl_map_intersect_range(map
, set2
);
5121 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5122 __isl_take isl_map
*map2
)
5125 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5126 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5127 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5131 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5132 __isl_take isl_map
*map2
)
5135 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5136 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5137 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5141 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5142 __isl_take isl_map
*map2
)
5145 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5146 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5147 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5151 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5152 __isl_take isl_map
*map2
)
5155 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5156 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5157 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5161 /* For a div d = floor(f/m), add the constraint
5165 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5166 unsigned pos
, isl_int
*div
)
5169 unsigned total
= isl_basic_map_total_dim(bmap
);
5171 i
= isl_basic_map_alloc_inequality(bmap
);
5173 return isl_stat_error
;
5174 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5175 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5180 /* For a div d = floor(f/m), add the constraint
5182 * -(f-(m-1)) + m d >= 0
5184 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5185 unsigned pos
, isl_int
*div
)
5188 unsigned total
= isl_basic_map_total_dim(bmap
);
5190 i
= isl_basic_map_alloc_inequality(bmap
);
5192 return isl_stat_error
;
5193 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5194 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5195 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5196 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5201 /* For a div d = floor(f/m), add the constraints
5204 * -(f-(m-1)) + m d >= 0
5206 * Note that the second constraint is the negation of
5210 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5211 unsigned pos
, isl_int
*div
)
5213 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5215 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5220 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5221 unsigned pos
, isl_int
*div
)
5223 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5227 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map
*bmap
,
5230 unsigned total
= isl_basic_map_total_dim(bmap
);
5231 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5233 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5237 /* For each known div d = floor(f/m), add the constraints
5240 * -(f-(m-1)) + m d >= 0
5242 * Remove duplicate constraints in case of some these div constraints
5243 * already appear in "bmap".
5245 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5246 __isl_take isl_basic_map
*bmap
)
5252 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5256 bmap
= add_known_div_constraints(bmap
);
5257 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5258 bmap
= isl_basic_map_finalize(bmap
);
5262 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5264 * In particular, if this div is of the form d = floor(f/m),
5265 * then add the constraint
5269 * if sign < 0 or the constraint
5271 * -(f-(m-1)) + m d >= 0
5275 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5276 unsigned div
, int sign
)
5284 total
= isl_basic_map_total_dim(bmap
);
5285 div_pos
= total
- bmap
->n_div
+ div
;
5288 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5290 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5293 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5294 __isl_take isl_basic_map
*bmap
)
5298 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5300 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5301 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5302 return bset_from_bmap(bmap
);
5303 bmap
= isl_basic_map_cow(bmap
);
5306 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5309 bmap
->extra
-= bmap
->n_div
;
5311 bmap
= isl_basic_map_finalize(bmap
);
5312 return bset_from_bmap(bmap
);
5314 isl_basic_map_free(bmap
);
5318 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5319 __isl_take isl_basic_set
*bset
)
5321 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5324 /* Replace each element in "list" by the result of applying
5325 * isl_basic_map_underlying_set to the element.
5327 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5328 __isl_take isl_basic_map_list
*list
)
5335 n
= isl_basic_map_list_n_basic_map(list
);
5336 for (i
= 0; i
< n
; ++i
) {
5337 isl_basic_map
*bmap
;
5338 isl_basic_set
*bset
;
5340 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5341 bset
= isl_basic_set_underlying_set(bmap
);
5342 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5348 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5349 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5351 struct isl_basic_map
*bmap
;
5352 struct isl_ctx
*ctx
;
5359 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5360 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5361 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5363 if (like
->n_div
== 0) {
5364 isl_space
*space
= isl_basic_map_get_space(like
);
5365 isl_basic_map_free(like
);
5366 return isl_basic_map_reset_space(bset
, space
);
5368 bset
= isl_basic_set_cow(bset
);
5371 total
= bset
->dim
->n_out
+ bset
->extra
;
5372 bmap
= bset_to_bmap(bset
);
5373 isl_space_free(bmap
->dim
);
5374 bmap
->dim
= isl_space_copy(like
->dim
);
5377 bmap
->n_div
= like
->n_div
;
5378 bmap
->extra
+= like
->n_div
;
5382 ltotal
= total
- bmap
->extra
+ like
->extra
;
5385 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5386 bmap
->extra
* (1 + 1 + total
));
5387 if (isl_blk_is_error(bmap
->block2
))
5389 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5393 for (i
= 0; i
< bmap
->extra
; ++i
)
5394 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5395 for (i
= 0; i
< like
->n_div
; ++i
) {
5396 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5397 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5399 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5401 isl_basic_map_free(like
);
5402 bmap
= isl_basic_map_simplify(bmap
);
5403 bmap
= isl_basic_map_finalize(bmap
);
5406 isl_basic_map_free(like
);
5407 isl_basic_set_free(bset
);
5411 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5412 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5414 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5415 bset_to_bmap(like
)));
5418 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5422 map
= isl_map_cow(map
);
5425 map
->dim
= isl_space_cow(map
->dim
);
5429 for (i
= 1; i
< map
->n
; ++i
)
5430 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5432 for (i
= 0; i
< map
->n
; ++i
) {
5433 map
->p
[i
] = bset_to_bmap(
5434 isl_basic_map_underlying_set(map
->p
[i
]));
5439 map
->dim
= isl_space_underlying(map
->dim
, 0);
5441 isl_space_free(map
->dim
);
5442 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5446 return set_from_map(map
);
5452 /* Replace the space of "bmap" by "space".
5454 * If the space of "bmap" is identical to "space" (including the identifiers
5455 * of the input and output dimensions), then simply return the original input.
5457 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5458 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5461 isl_space
*bmap_space
;
5463 bmap_space
= isl_basic_map_peek_space(bmap
);
5464 equal
= isl_space_is_equal(bmap_space
, space
);
5465 if (equal
>= 0 && equal
)
5466 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5470 isl_space_free(space
);
5473 bmap
= isl_basic_map_cow(bmap
);
5474 if (!bmap
|| !space
)
5477 isl_space_free(bmap
->dim
);
5480 bmap
= isl_basic_map_finalize(bmap
);
5484 isl_basic_map_free(bmap
);
5485 isl_space_free(space
);
5489 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5490 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5492 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5496 /* Check that the total dimensions of "map" and "space" are the same.
5498 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5499 __isl_keep isl_space
*space
)
5501 unsigned dim1
, dim2
;
5504 return isl_stat_error
;
5505 dim1
= isl_map_dim(map
, isl_dim_all
);
5506 dim2
= isl_space_dim(space
, isl_dim_all
);
5509 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5510 "total dimensions do not match", return isl_stat_error
);
5513 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5514 __isl_take isl_space
*space
)
5518 map
= isl_map_cow(map
);
5522 for (i
= 0; i
< map
->n
; ++i
) {
5523 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5524 isl_space_copy(space
));
5528 isl_space_free(map
->dim
);
5534 isl_space_free(space
);
5538 /* Replace the space of "map" by "space", without modifying
5539 * the dimension of "map".
5541 * If the space of "map" is identical to "space" (including the identifiers
5542 * of the input and output dimensions), then simply return the original input.
5544 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5545 __isl_take isl_space
*space
)
5548 isl_space
*map_space
;
5550 map_space
= isl_map_peek_space(map
);
5551 equal
= isl_space_is_equal(map_space
, space
);
5552 if (equal
>= 0 && equal
)
5553 equal
= isl_space_has_equal_ids(map_space
, space
);
5557 isl_space_free(space
);
5560 if (check_map_space_equal_total_dim(map
, space
) < 0)
5562 return isl_map_reset_space(map
, space
);
5565 isl_space_free(space
);
5569 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5570 __isl_take isl_space
*dim
)
5572 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5575 /* Compute the parameter domain of the given basic set.
5577 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5583 is_params
= isl_basic_set_is_params(bset
);
5585 return isl_basic_set_free(bset
);
5589 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5590 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5591 space
= isl_basic_set_get_space(bset
);
5592 space
= isl_space_params(space
);
5593 bset
= isl_basic_set_reset_space(bset
, space
);
5597 /* Construct a zero-dimensional basic set with the given parameter domain.
5599 __isl_give isl_basic_set
*isl_basic_set_from_params(
5600 __isl_take isl_basic_set
*bset
)
5603 space
= isl_basic_set_get_space(bset
);
5604 space
= isl_space_set_from_params(space
);
5605 bset
= isl_basic_set_reset_space(bset
, space
);
5609 /* Compute the parameter domain of the given set.
5611 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5616 if (isl_set_is_params(set
))
5619 n
= isl_set_dim(set
, isl_dim_set
);
5620 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5621 space
= isl_set_get_space(set
);
5622 space
= isl_space_params(space
);
5623 set
= isl_set_reset_space(set
, space
);
5627 /* Construct a zero-dimensional set with the given parameter domain.
5629 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5632 space
= isl_set_get_space(set
);
5633 space
= isl_space_set_from_params(space
);
5634 set
= isl_set_reset_space(set
, space
);
5638 /* Compute the parameter domain of the given map.
5640 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5645 n
= isl_map_dim(map
, isl_dim_in
);
5646 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5647 n
= isl_map_dim(map
, isl_dim_out
);
5648 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5649 space
= isl_map_get_space(map
);
5650 space
= isl_space_params(space
);
5651 map
= isl_map_reset_space(map
, space
);
5655 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5662 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5664 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5665 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5667 return isl_basic_map_reset_space(bmap
, space
);
5670 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5673 return isl_bool_error
;
5674 return isl_space_may_be_set(bmap
->dim
);
5677 /* Is this basic map actually a set?
5678 * Users should never call this function. Outside of isl,
5679 * the type should indicate whether something is a set or a map.
5681 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5684 return isl_bool_error
;
5685 return isl_space_is_set(bmap
->dim
);
5688 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5692 is_set
= isl_basic_map_is_set(bmap
);
5697 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5699 isl_basic_map_free(bmap
);
5703 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5704 __isl_take isl_basic_map
*bmap
)
5708 isl_basic_map
*domain
;
5709 int nparam
, n_in
, n_out
;
5711 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5712 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5713 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5715 space
= isl_basic_map_get_space(bmap
);
5716 space
= isl_space_from_range(isl_space_domain(space
));
5717 domain
= isl_basic_map_universe(space
);
5719 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5720 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5721 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5723 for (i
= 0; i
< n_in
; ++i
)
5724 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5727 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5728 return isl_basic_map_finalize(bmap
);
5731 __isl_give isl_basic_map
*isl_basic_map_range_map(
5732 __isl_take isl_basic_map
*bmap
)
5736 isl_basic_map
*range
;
5737 int nparam
, n_in
, n_out
;
5739 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5740 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5741 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5743 space
= isl_basic_map_get_space(bmap
);
5744 space
= isl_space_from_range(isl_space_range(space
));
5745 range
= isl_basic_map_universe(space
);
5747 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5748 bmap
= isl_basic_map_apply_range(bmap
, range
);
5749 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5751 for (i
= 0; i
< n_out
; ++i
)
5752 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5755 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5756 return isl_basic_map_finalize(bmap
);
5759 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5763 return isl_space_may_be_set(map
->dim
);
5766 /* Is this map actually a set?
5767 * Users should never call this function. Outside of isl,
5768 * the type should indicate whether something is a set or a map.
5770 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5773 return isl_bool_error
;
5774 return isl_space_is_set(map
->dim
);
5777 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5781 struct isl_set
*set
;
5783 is_set
= isl_map_is_set(map
);
5787 return set_from_map(map
);
5789 map
= isl_map_cow(map
);
5793 set
= set_from_map(map
);
5794 set
->dim
= isl_space_range(set
->dim
);
5797 for (i
= 0; i
< map
->n
; ++i
) {
5798 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5802 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5803 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5810 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5814 map
= isl_map_cow(map
);
5818 map
->dim
= isl_space_domain_map(map
->dim
);
5821 for (i
= 0; i
< map
->n
; ++i
) {
5822 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5826 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5827 map
= isl_map_unmark_normalized(map
);
5834 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5837 isl_space
*range_dim
;
5839 map
= isl_map_cow(map
);
5843 range_dim
= isl_space_range(isl_map_get_space(map
));
5844 range_dim
= isl_space_from_range(range_dim
);
5845 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5846 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5849 for (i
= 0; i
< map
->n
; ++i
) {
5850 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5854 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5855 map
= isl_map_unmark_normalized(map
);
5862 /* Given a wrapped map of the form A[B -> C],
5863 * return the map A[B -> C] -> B.
5865 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5872 if (!isl_set_has_tuple_id(set
))
5873 return isl_map_domain_map(isl_set_unwrap(set
));
5875 id
= isl_set_get_tuple_id(set
);
5876 map
= isl_map_domain_map(isl_set_unwrap(set
));
5877 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5882 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5883 __isl_take isl_basic_set
*bset
)
5885 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5888 __isl_give isl_basic_map
*isl_basic_map_from_range(
5889 __isl_take isl_basic_set
*bset
)
5892 space
= isl_basic_set_get_space(bset
);
5893 space
= isl_space_from_range(space
);
5894 bset
= isl_basic_set_reset_space(bset
, space
);
5895 return bset_to_bmap(bset
);
5898 /* Create a relation with the given set as range.
5899 * The domain of the created relation is a zero-dimensional
5900 * flat anonymous space.
5902 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5905 space
= isl_set_get_space(set
);
5906 space
= isl_space_from_range(space
);
5907 set
= isl_set_reset_space(set
, space
);
5908 return set_to_map(set
);
5911 /* Create a relation with the given set as domain.
5912 * The range of the created relation is a zero-dimensional
5913 * flat anonymous space.
5915 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5917 return isl_map_reverse(isl_map_from_range(set
));
5920 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5921 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5923 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5926 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5927 __isl_take isl_set
*range
)
5929 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5932 /* Return a newly allocated isl_map with given space and flags and
5933 * room for "n" basic maps.
5934 * Make sure that all cached information is cleared.
5936 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5939 struct isl_map
*map
;
5944 isl_die(space
->ctx
, isl_error_internal
,
5945 "negative number of basic maps", goto error
);
5946 map
= isl_calloc(space
->ctx
, struct isl_map
,
5947 sizeof(struct isl_map
) +
5948 (n
- 1) * sizeof(struct isl_basic_map
*));
5952 map
->ctx
= space
->ctx
;
5953 isl_ctx_ref(map
->ctx
);
5961 isl_space_free(space
);
5965 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5967 struct isl_basic_map
*bmap
;
5968 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5969 bmap
= isl_basic_map_set_to_empty(bmap
);
5973 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5975 struct isl_basic_set
*bset
;
5976 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5977 bset
= isl_basic_set_set_to_empty(bset
);
5981 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5983 struct isl_basic_map
*bmap
;
5984 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5985 bmap
= isl_basic_map_finalize(bmap
);
5989 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5991 struct isl_basic_set
*bset
;
5992 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5993 bset
= isl_basic_set_finalize(bset
);
5997 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
5998 __isl_take isl_space
*space
)
6001 unsigned total
= isl_space_dim(space
, isl_dim_all
);
6002 isl_basic_map
*bmap
;
6004 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6005 for (i
= 0; i
< total
; ++i
) {
6006 int k
= isl_basic_map_alloc_inequality(bmap
);
6009 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6010 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6014 isl_basic_map_free(bmap
);
6018 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6019 __isl_take isl_space
*space
)
6021 return isl_basic_map_nat_universe(space
);
6024 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6026 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6029 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6031 return isl_map_nat_universe(dim
);
6034 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6036 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6039 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6041 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6044 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6046 struct isl_map
*map
;
6049 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6050 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6054 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6056 struct isl_set
*set
;
6059 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6060 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6064 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6067 struct isl_map
*dup
;
6071 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6072 for (i
= 0; i
< map
->n
; ++i
)
6073 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6077 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6078 __isl_take isl_basic_map
*bmap
)
6082 if (isl_basic_map_plain_is_empty(bmap
)) {
6083 isl_basic_map_free(bmap
);
6086 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6087 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6088 map
->p
[map
->n
] = bmap
;
6090 map
= isl_map_unmark_normalized(map
);
6096 isl_basic_map_free(bmap
);
6100 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6111 isl_ctx_deref(map
->ctx
);
6112 for (i
= 0; i
< map
->n
; ++i
)
6113 isl_basic_map_free(map
->p
[i
]);
6114 isl_space_free(map
->dim
);
6120 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6121 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6125 bmap
= isl_basic_map_cow(bmap
);
6126 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6127 j
= isl_basic_map_alloc_equality(bmap
);
6130 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6131 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6132 isl_int_set_si(bmap
->eq
[j
][0], value
);
6133 bmap
= isl_basic_map_simplify(bmap
);
6134 return isl_basic_map_finalize(bmap
);
6136 isl_basic_map_free(bmap
);
6140 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6141 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6145 bmap
= isl_basic_map_cow(bmap
);
6146 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6147 j
= isl_basic_map_alloc_equality(bmap
);
6150 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6151 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6152 isl_int_set(bmap
->eq
[j
][0], value
);
6153 bmap
= isl_basic_map_simplify(bmap
);
6154 return isl_basic_map_finalize(bmap
);
6156 isl_basic_map_free(bmap
);
6160 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6161 enum isl_dim_type type
, unsigned pos
, int value
)
6163 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6164 return isl_basic_map_free(bmap
);
6165 return isl_basic_map_fix_pos_si(bmap
,
6166 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6169 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6170 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6172 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6173 return isl_basic_map_free(bmap
);
6174 return isl_basic_map_fix_pos(bmap
,
6175 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6178 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6179 * to be equal to "v".
6181 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6182 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6186 if (!isl_val_is_int(v
))
6187 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6188 "expecting integer value", goto error
);
6189 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6191 pos
+= isl_basic_map_offset(bmap
, type
);
6192 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6196 isl_basic_map_free(bmap
);
6201 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6202 * to be equal to "v".
6204 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6205 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6207 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6210 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6211 enum isl_dim_type type
, unsigned pos
, int value
)
6213 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6217 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6218 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6220 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6224 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6225 unsigned input
, int value
)
6227 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6230 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6231 unsigned dim
, int value
)
6233 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6234 isl_dim_set
, dim
, value
));
6237 /* Remove the basic map at position "i" from "map" if this basic map
6238 * is (obviously) empty.
6240 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6247 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6249 return isl_map_free(map
);
6253 isl_basic_map_free(map
->p
[i
]);
6256 map
->p
[i
] = map
->p
[map
->n
];
6257 map
= isl_map_unmark_normalized(map
);
6264 /* Perform "fn" on each basic map of "map", where we may not be holding
6265 * the only reference to "map".
6266 * In particular, "fn" should be a semantics preserving operation
6267 * that we want to apply to all copies of "map". We therefore need
6268 * to be careful not to modify "map" in a way that breaks "map"
6269 * in case anything goes wrong.
6271 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6272 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6274 struct isl_basic_map
*bmap
;
6280 for (i
= map
->n
- 1; i
>= 0; --i
) {
6281 bmap
= isl_basic_map_copy(map
->p
[i
]);
6285 isl_basic_map_free(map
->p
[i
]);
6287 map
= remove_if_empty(map
, i
);
6298 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6299 enum isl_dim_type type
, unsigned pos
, int value
)
6303 map
= isl_map_cow(map
);
6307 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6308 for (i
= map
->n
- 1; i
>= 0; --i
) {
6309 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6310 map
= remove_if_empty(map
, i
);
6314 map
= isl_map_unmark_normalized(map
);
6321 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6322 enum isl_dim_type type
, unsigned pos
, int value
)
6324 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6327 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6328 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6332 map
= isl_map_cow(map
);
6336 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6337 for (i
= 0; i
< map
->n
; ++i
) {
6338 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6342 map
= isl_map_unmark_normalized(map
);
6349 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6350 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6352 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6355 /* Fix the value of the variable at position "pos" of type "type" of "map"
6356 * to be equal to "v".
6358 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6359 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6363 map
= isl_map_cow(map
);
6367 if (!isl_val_is_int(v
))
6368 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6369 "expecting integer value", goto error
);
6370 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6372 for (i
= map
->n
- 1; i
>= 0; --i
) {
6373 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6375 map
= remove_if_empty(map
, i
);
6379 map
= isl_map_unmark_normalized(map
);
6388 /* Fix the value of the variable at position "pos" of type "type" of "set"
6389 * to be equal to "v".
6391 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6392 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6394 return isl_map_fix_val(set
, type
, pos
, v
);
6397 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6398 unsigned input
, int value
)
6400 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6403 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6405 return set_from_map(isl_map_fix_si(set_to_map(set
),
6406 isl_dim_set
, dim
, value
));
6409 static __isl_give isl_basic_map
*basic_map_bound_si(
6410 __isl_take isl_basic_map
*bmap
,
6411 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6415 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6416 return isl_basic_map_free(bmap
);
6417 pos
+= isl_basic_map_offset(bmap
, type
);
6418 bmap
= isl_basic_map_cow(bmap
);
6419 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6420 j
= isl_basic_map_alloc_inequality(bmap
);
6423 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6425 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6426 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6428 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6429 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6431 bmap
= isl_basic_map_simplify(bmap
);
6432 return isl_basic_map_finalize(bmap
);
6434 isl_basic_map_free(bmap
);
6438 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6439 __isl_take isl_basic_map
*bmap
,
6440 enum isl_dim_type type
, unsigned pos
, int value
)
6442 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6445 /* Constrain the values of the given dimension to be no greater than "value".
6447 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6448 __isl_take isl_basic_map
*bmap
,
6449 enum isl_dim_type type
, unsigned pos
, int value
)
6451 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6454 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6455 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6459 map
= isl_map_cow(map
);
6460 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6461 return isl_map_free(map
);
6462 for (i
= 0; i
< map
->n
; ++i
) {
6463 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6464 type
, pos
, value
, upper
);
6468 map
= isl_map_unmark_normalized(map
);
6475 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6476 enum isl_dim_type type
, unsigned pos
, int value
)
6478 return map_bound_si(map
, type
, pos
, value
, 0);
6481 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6482 enum isl_dim_type type
, unsigned pos
, int value
)
6484 return map_bound_si(map
, type
, pos
, value
, 1);
6487 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6488 enum isl_dim_type type
, unsigned pos
, int value
)
6490 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6494 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6495 enum isl_dim_type type
, unsigned pos
, int value
)
6497 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6500 /* Bound the given variable of "bmap" from below (or above is "upper"
6501 * is set) to "value".
6503 static __isl_give isl_basic_map
*basic_map_bound(
6504 __isl_take isl_basic_map
*bmap
,
6505 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6509 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6510 return isl_basic_map_free(bmap
);
6511 pos
+= isl_basic_map_offset(bmap
, type
);
6512 bmap
= isl_basic_map_cow(bmap
);
6513 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6514 j
= isl_basic_map_alloc_inequality(bmap
);
6517 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6519 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6520 isl_int_set(bmap
->ineq
[j
][0], value
);
6522 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6523 isl_int_neg(bmap
->ineq
[j
][0], value
);
6525 bmap
= isl_basic_map_simplify(bmap
);
6526 return isl_basic_map_finalize(bmap
);
6528 isl_basic_map_free(bmap
);
6532 /* Bound the given variable of "map" from below (or above is "upper"
6533 * is set) to "value".
6535 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6536 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6540 map
= isl_map_cow(map
);
6541 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6542 return isl_map_free(map
);
6543 for (i
= map
->n
- 1; i
>= 0; --i
) {
6544 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6545 map
= remove_if_empty(map
, i
);
6549 map
= isl_map_unmark_normalized(map
);
6553 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6554 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6556 return map_bound(map
, type
, pos
, value
, 0);
6559 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6560 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6562 return map_bound(map
, type
, pos
, value
, 1);
6565 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6566 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6568 return isl_map_lower_bound(set
, type
, pos
, value
);
6571 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6572 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6574 return isl_map_upper_bound(set
, type
, pos
, value
);
6577 /* Force the values of the variable at position "pos" of type "type" of "set"
6578 * to be no smaller than "value".
6580 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6581 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6585 if (!isl_val_is_int(value
))
6586 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6587 "expecting integer value", goto error
);
6588 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6589 isl_val_free(value
);
6592 isl_val_free(value
);
6597 /* Force the values of the variable at position "pos" of type "type" of "set"
6598 * to be no greater than "value".
6600 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6601 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6605 if (!isl_val_is_int(value
))
6606 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6607 "expecting integer value", goto error
);
6608 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6609 isl_val_free(value
);
6612 isl_val_free(value
);
6617 /* Bound the given variable of "bset" from below (or above is "upper"
6618 * is set) to "value".
6620 static __isl_give isl_basic_set
*isl_basic_set_bound(
6621 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6622 isl_int value
, int upper
)
6624 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6625 type
, pos
, value
, upper
));
6628 /* Bound the given variable of "bset" from below (or above is "upper"
6629 * is set) to "value".
6631 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6632 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6633 __isl_take isl_val
*value
, int upper
)
6637 if (!isl_val_is_int(value
))
6638 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6639 "expecting integer value", goto error
);
6640 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6641 isl_val_free(value
);
6644 isl_val_free(value
);
6645 isl_basic_set_free(bset
);
6649 /* Bound the given variable of "bset" from below to "value".
6651 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6652 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6653 __isl_take isl_val
*value
)
6655 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6658 /* Bound the given variable of "bset" from above to "value".
6660 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6661 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6662 __isl_take isl_val
*value
)
6664 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6667 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6671 map
= isl_map_cow(map
);
6675 map
->dim
= isl_space_reverse(map
->dim
);
6678 for (i
= 0; i
< map
->n
; ++i
) {
6679 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6683 map
= isl_map_unmark_normalized(map
);
6691 #define TYPE isl_pw_multi_aff
6693 #define SUFFIX _pw_multi_aff
6695 #define EMPTY isl_pw_multi_aff_empty
6697 #define ADD isl_pw_multi_aff_union_add
6698 #include "isl_map_lexopt_templ.c"
6700 /* Given a map "map", compute the lexicographically minimal
6701 * (or maximal) image element for each domain element in dom,
6702 * in the form of an isl_pw_multi_aff.
6703 * If "empty" is not NULL, then set *empty to those elements in dom that
6704 * do not have an image element.
6705 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6706 * should be computed over the domain of "map". "empty" is also NULL
6709 * We first compute the lexicographically minimal or maximal element
6710 * in the first basic map. This results in a partial solution "res"
6711 * and a subset "todo" of dom that still need to be handled.
6712 * We then consider each of the remaining maps in "map" and successively
6713 * update both "res" and "todo".
6714 * If "empty" is NULL, then the todo sets are not needed and therefore
6715 * also not computed.
6717 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6718 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6719 __isl_give isl_set
**empty
, unsigned flags
)
6723 isl_pw_multi_aff
*res
;
6726 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6727 if (!map
|| (!full
&& !dom
))
6730 if (isl_map_plain_is_empty(map
)) {
6735 return isl_pw_multi_aff_from_map(map
);
6738 res
= basic_map_partial_lexopt_pw_multi_aff(
6739 isl_basic_map_copy(map
->p
[0]),
6740 isl_set_copy(dom
), empty
, flags
);
6744 for (i
= 1; i
< map
->n
; ++i
) {
6745 isl_pw_multi_aff
*res_i
;
6747 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6748 isl_basic_map_copy(map
->p
[i
]),
6749 isl_set_copy(dom
), empty
, flags
);
6751 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6752 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6754 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6757 todo
= isl_set_intersect(todo
, *empty
);
6776 #define TYPE isl_map
6780 #define EMPTY isl_map_empty
6782 #define ADD isl_map_union_disjoint
6783 #include "isl_map_lexopt_templ.c"
6785 /* Given a map "map", compute the lexicographically minimal
6786 * (or maximal) image element for each domain element in "dom",
6787 * in the form of an isl_map.
6788 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6789 * do not have an image element.
6790 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6791 * should be computed over the domain of "map". "empty" is also NULL
6794 * If the input consists of more than one disjunct, then first
6795 * compute the desired result in the form of an isl_pw_multi_aff and
6796 * then convert that into an isl_map.
6798 * This function used to have an explicit implementation in terms
6799 * of isl_maps, but it would continually intersect the domains of
6800 * partial results with the complement of the domain of the next
6801 * partial solution, potentially leading to an explosion in the number
6802 * of disjuncts if there are several disjuncts in the input.
6803 * An even earlier implementation of this function would look for
6804 * better results in the domain of the partial result and for extra
6805 * results in the complement of this domain, which would lead to
6806 * even more splintering.
6808 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6809 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6810 __isl_give isl_set
**empty
, unsigned flags
)
6813 struct isl_map
*res
;
6814 isl_pw_multi_aff
*pma
;
6816 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6817 if (!map
|| (!full
&& !dom
))
6820 if (isl_map_plain_is_empty(map
)) {
6829 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6835 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6837 return isl_map_from_pw_multi_aff(pma
);
6846 __isl_give isl_map
*isl_map_partial_lexmax(
6847 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6848 __isl_give isl_set
**empty
)
6850 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6853 __isl_give isl_map
*isl_map_partial_lexmin(
6854 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6855 __isl_give isl_set
**empty
)
6857 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6860 __isl_give isl_set
*isl_set_partial_lexmin(
6861 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6862 __isl_give isl_set
**empty
)
6864 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6868 __isl_give isl_set
*isl_set_partial_lexmax(
6869 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6870 __isl_give isl_set
**empty
)
6872 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6876 /* Compute the lexicographic minimum (or maximum if "flags" includes
6877 * ISL_OPT_MAX) of "bset" over its parametric domain.
6879 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6882 return isl_basic_map_lexopt(bset
, flags
);
6885 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6887 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6890 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6892 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6895 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6897 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6900 /* Compute the lexicographic minimum of "bset" over its parametric domain
6901 * for the purpose of quantifier elimination.
6902 * That is, find an explicit representation for all the existentially
6903 * quantified variables in "bset" by computing their lexicographic
6906 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6907 __isl_take isl_basic_set
*bset
)
6909 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6912 /* Given a basic map with one output dimension, compute the minimum or
6913 * maximum of that dimension as an isl_pw_aff.
6915 * Compute the optimum as a lexicographic optimum over the single
6916 * output dimension and extract the single isl_pw_aff from the result.
6918 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6921 isl_pw_multi_aff
*pma
;
6924 bmap
= isl_basic_map_copy(bmap
);
6925 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6926 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6927 isl_pw_multi_aff_free(pma
);
6932 /* Compute the minimum or maximum of the given output dimension
6933 * as a function of the parameters and the input dimensions,
6934 * but independently of the other output dimensions.
6936 * We first project out the other output dimension and then compute
6937 * the "lexicographic" maximum in each basic map, combining the results
6938 * using isl_pw_aff_union_max.
6940 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6947 n_out
= isl_map_dim(map
, isl_dim_out
);
6948 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6949 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6954 isl_space
*space
= isl_map_get_space(map
);
6956 return isl_pw_aff_empty(space
);
6959 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6960 for (i
= 1; i
< map
->n
; ++i
) {
6961 isl_pw_aff
*pwaff_i
;
6963 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6964 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6972 /* Compute the minimum of the given output dimension as a function of the
6973 * parameters and input dimensions, but independently of
6974 * the other output dimensions.
6976 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6978 return map_dim_opt(map
, pos
, 0);
6981 /* Compute the maximum of the given output dimension as a function of the
6982 * parameters and input dimensions, but independently of
6983 * the other output dimensions.
6985 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6987 return map_dim_opt(map
, pos
, 1);
6990 /* Compute the minimum or maximum of the given set dimension
6991 * as a function of the parameters,
6992 * but independently of the other set dimensions.
6994 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6997 return map_dim_opt(set
, pos
, max
);
7000 /* Compute the maximum of the given set dimension as a function of the
7001 * parameters, but independently of the other set dimensions.
7003 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7005 return set_dim_opt(set
, pos
, 1);
7008 /* Compute the minimum of the given set dimension as a function of the
7009 * parameters, but independently of the other set dimensions.
7011 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7013 return set_dim_opt(set
, pos
, 0);
7016 /* Apply a preimage specified by "mat" on the parameters of "bset".
7017 * bset is assumed to have only parameters and divs.
7019 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7020 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7027 bset
->dim
= isl_space_cow(bset
->dim
);
7031 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7033 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7035 bset
->dim
->nparam
= 0;
7036 bset
->dim
->n_out
= nparam
;
7037 bset
= isl_basic_set_preimage(bset
, mat
);
7039 bset
->dim
->nparam
= bset
->dim
->n_out
;
7040 bset
->dim
->n_out
= 0;
7045 isl_basic_set_free(bset
);
7049 /* Apply a preimage specified by "mat" on the parameters of "set".
7050 * set is assumed to have only parameters and divs.
7052 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7053 __isl_take isl_mat
*mat
)
7061 nparam
= isl_set_dim(set
, isl_dim_param
);
7063 if (mat
->n_row
!= 1 + nparam
)
7064 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7065 "unexpected number of rows", goto error
);
7067 space
= isl_set_get_space(set
);
7068 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7069 isl_dim_param
, 0, nparam
);
7070 set
= isl_set_reset_space(set
, space
);
7071 set
= isl_set_preimage(set
, mat
);
7072 nparam
= isl_set_dim(set
, isl_dim_out
);
7073 space
= isl_set_get_space(set
);
7074 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7075 isl_dim_out
, 0, nparam
);
7076 set
= isl_set_reset_space(set
, space
);
7084 /* Intersect the basic set "bset" with the affine space specified by the
7085 * equalities in "eq".
7087 static __isl_give isl_basic_set
*basic_set_append_equalities(
7088 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7096 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7101 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7102 for (i
= 0; i
< eq
->n_row
; ++i
) {
7103 k
= isl_basic_set_alloc_equality(bset
);
7106 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7107 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7111 bset
= isl_basic_set_gauss(bset
, NULL
);
7112 bset
= isl_basic_set_finalize(bset
);
7117 isl_basic_set_free(bset
);
7121 /* Intersect the set "set" with the affine space specified by the
7122 * equalities in "eq".
7124 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7132 for (i
= 0; i
< set
->n
; ++i
) {
7133 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7146 /* Given a basic set "bset" that only involves parameters and existentially
7147 * quantified variables, return the index of the first equality
7148 * that only involves parameters. If there is no such equality then
7149 * return bset->n_eq.
7151 * This function assumes that isl_basic_set_gauss has been called on "bset".
7153 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7156 unsigned nparam
, n_div
;
7161 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7162 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7164 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7165 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7172 /* Compute an explicit representation for the existentially quantified
7173 * variables in "bset" by computing the "minimal value" of the set
7174 * variables. Since there are no set variables, the computation of
7175 * the minimal value essentially computes an explicit representation
7176 * of the non-empty part(s) of "bset".
7178 * The input only involves parameters and existentially quantified variables.
7179 * All equalities among parameters have been removed.
7181 * Since the existentially quantified variables in the result are in general
7182 * going to be different from those in the input, we first replace
7183 * them by the minimal number of variables based on their equalities.
7184 * This should simplify the parametric integer programming.
7186 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7188 isl_morph
*morph1
, *morph2
;
7194 if (bset
->n_eq
== 0)
7195 return isl_basic_set_lexmin_compute_divs(bset
);
7197 morph1
= isl_basic_set_parameter_compression(bset
);
7198 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7199 bset
= isl_basic_set_lift(bset
);
7200 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7201 bset
= isl_morph_basic_set(morph2
, bset
);
7202 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7203 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7205 set
= isl_basic_set_lexmin_compute_divs(bset
);
7207 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7212 /* Project the given basic set onto its parameter domain, possibly introducing
7213 * new, explicit, existential variables in the constraints.
7214 * The input has parameters and (possibly implicit) existential variables.
7215 * The output has the same parameters, but only
7216 * explicit existentially quantified variables.
7218 * The actual projection is performed by pip, but pip doesn't seem
7219 * to like equalities very much, so we first remove the equalities
7220 * among the parameters by performing a variable compression on
7221 * the parameters. Afterward, an inverse transformation is performed
7222 * and the equalities among the parameters are inserted back in.
7224 * The variable compression on the parameters may uncover additional
7225 * equalities that were only implicit before. We therefore check
7226 * if there are any new parameter equalities in the result and
7227 * if so recurse. The removal of parameter equalities is required
7228 * for the parameter compression performed by base_compute_divs.
7230 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7234 struct isl_mat
*T
, *T2
;
7235 struct isl_set
*set
;
7238 bset
= isl_basic_set_cow(bset
);
7242 if (bset
->n_eq
== 0)
7243 return base_compute_divs(bset
);
7245 bset
= isl_basic_set_gauss(bset
, NULL
);
7248 if (isl_basic_set_plain_is_empty(bset
))
7249 return isl_set_from_basic_set(bset
);
7251 i
= first_parameter_equality(bset
);
7252 if (i
== bset
->n_eq
)
7253 return base_compute_divs(bset
);
7255 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7256 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7258 eq
= isl_mat_cow(eq
);
7259 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7260 if (T
&& T
->n_col
== 0) {
7264 bset
= isl_basic_set_set_to_empty(bset
);
7265 return isl_set_from_basic_set(bset
);
7267 bset
= basic_set_parameter_preimage(bset
, T
);
7269 i
= first_parameter_equality(bset
);
7272 else if (i
== bset
->n_eq
)
7273 set
= base_compute_divs(bset
);
7275 set
= parameter_compute_divs(bset
);
7276 set
= set_parameter_preimage(set
, T2
);
7277 set
= set_append_equalities(set
, eq
);
7281 /* Insert the divs from "ls" before those of "bmap".
7283 * The number of columns is not changed, which means that the last
7284 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7285 * The caller is responsible for removing the same number of dimensions
7286 * from the space of "bmap".
7288 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7289 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7295 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7299 old_n_div
= bmap
->n_div
;
7300 bmap
= insert_div_rows(bmap
, n_div
);
7304 for (i
= 0; i
< n_div
; ++i
) {
7305 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7306 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7312 /* Replace the space of "bmap" by the space and divs of "ls".
7314 * If "ls" has any divs, then we simplify the result since we may
7315 * have discovered some additional equalities that could simplify
7316 * the div expressions.
7318 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7319 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7323 bmap
= isl_basic_map_cow(bmap
);
7327 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7328 bmap
= insert_divs_from_local_space(bmap
, ls
);
7332 isl_space_free(bmap
->dim
);
7333 bmap
->dim
= isl_local_space_get_space(ls
);
7337 isl_local_space_free(ls
);
7339 bmap
= isl_basic_map_simplify(bmap
);
7340 bmap
= isl_basic_map_finalize(bmap
);
7343 isl_basic_map_free(bmap
);
7344 isl_local_space_free(ls
);
7348 /* Replace the space of "map" by the space and divs of "ls".
7350 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7351 __isl_take isl_local_space
*ls
)
7355 map
= isl_map_cow(map
);
7359 for (i
= 0; i
< map
->n
; ++i
) {
7360 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7361 isl_local_space_copy(ls
));
7365 isl_space_free(map
->dim
);
7366 map
->dim
= isl_local_space_get_space(ls
);
7370 isl_local_space_free(ls
);
7373 isl_local_space_free(ls
);
7378 /* Compute an explicit representation for the existentially
7379 * quantified variables for which do not know any explicit representation yet.
7381 * We first sort the existentially quantified variables so that the
7382 * existentially quantified variables for which we already have an explicit
7383 * representation are placed before those for which we do not.
7384 * The input dimensions, the output dimensions and the existentially
7385 * quantified variables for which we already have an explicit
7386 * representation are then turned into parameters.
7387 * compute_divs returns a map with the same parameters and
7388 * no input or output dimensions and the dimension specification
7389 * is reset to that of the input, including the existentially quantified
7390 * variables for which we already had an explicit representation.
7392 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7394 struct isl_basic_set
*bset
;
7395 struct isl_set
*set
;
7396 struct isl_map
*map
;
7398 isl_local_space
*ls
;
7405 bmap
= isl_basic_map_sort_divs(bmap
);
7406 bmap
= isl_basic_map_cow(bmap
);
7410 n_known
= isl_basic_map_first_unknown_div(bmap
);
7412 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7414 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7415 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7416 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7417 space
= isl_space_set_alloc(bmap
->ctx
,
7418 nparam
+ n_in
+ n_out
+ n_known
, 0);
7422 ls
= isl_basic_map_get_local_space(bmap
);
7423 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7424 n_known
, bmap
->n_div
- n_known
);
7426 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7427 swap_div(bmap
, i
- n_known
, i
);
7428 bmap
->n_div
-= n_known
;
7429 bmap
->extra
-= n_known
;
7431 bmap
= isl_basic_map_reset_space(bmap
, space
);
7432 bset
= bset_from_bmap(bmap
);
7434 set
= parameter_compute_divs(bset
);
7435 map
= set_to_map(set
);
7436 map
= replace_space_by_local_space(map
, ls
);
7440 isl_basic_map_free(bmap
);
7444 /* Remove the explicit representation of local variable "div",
7447 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7448 __isl_take isl_basic_map
*bmap
, int div
)
7452 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7454 return isl_basic_map_free(bmap
);
7458 bmap
= isl_basic_map_cow(bmap
);
7461 isl_int_set_si(bmap
->div
[div
][0], 0);
7465 /* Is local variable "div" of "bmap" marked as not having an explicit
7467 * Note that even if "div" is not marked in this way and therefore
7468 * has an explicit representation, this representation may still
7469 * depend (indirectly) on other local variables that do not
7470 * have an explicit representation.
7472 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7475 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7476 return isl_bool_error
;
7477 return isl_int_is_zero(bmap
->div
[div
][0]);
7480 /* Return the position of the first local variable that does not
7481 * have an explicit representation.
7482 * Return the total number of local variables if they all have
7483 * an explicit representation.
7484 * Return -1 on error.
7486 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7493 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7494 if (!isl_basic_map_div_is_known(bmap
, i
))
7500 /* Return the position of the first local variable that does not
7501 * have an explicit representation.
7502 * Return the total number of local variables if they all have
7503 * an explicit representation.
7504 * Return -1 on error.
7506 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7508 return isl_basic_map_first_unknown_div(bset
);
7511 /* Does "bmap" have an explicit representation for all local variables?
7513 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7517 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7518 first
= isl_basic_map_first_unknown_div(bmap
);
7520 return isl_bool_error
;
7524 /* Do all basic maps in "map" have an explicit representation
7525 * for all local variables?
7527 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7532 return isl_bool_error
;
7534 for (i
= 0; i
< map
->n
; ++i
) {
7535 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7540 return isl_bool_true
;
7543 /* If bmap contains any unknown divs, then compute explicit
7544 * expressions for them. However, this computation may be
7545 * quite expensive, so first try to remove divs that aren't
7548 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7551 struct isl_map
*map
;
7553 known
= isl_basic_map_divs_known(bmap
);
7557 return isl_map_from_basic_map(bmap
);
7559 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7561 known
= isl_basic_map_divs_known(bmap
);
7565 return isl_map_from_basic_map(bmap
);
7567 map
= compute_divs(bmap
);
7570 isl_basic_map_free(bmap
);
7574 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7578 struct isl_map
*res
;
7585 known
= isl_map_divs_known(map
);
7593 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7594 for (i
= 1 ; i
< map
->n
; ++i
) {
7596 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7597 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7598 res
= isl_map_union_disjoint(res
, r2
);
7600 res
= isl_map_union(res
, r2
);
7607 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7609 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7612 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7614 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7617 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7620 struct isl_set
*set
;
7625 map
= isl_map_cow(map
);
7629 set
= set_from_map(map
);
7630 set
->dim
= isl_space_domain(set
->dim
);
7633 for (i
= 0; i
< map
->n
; ++i
) {
7634 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7638 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7639 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7646 /* Return the union of "map1" and "map2", where we assume for now that
7647 * "map1" and "map2" are disjoint. Note that the basic maps inside
7648 * "map1" or "map2" may not be disjoint from each other.
7649 * Also note that this function is also called from isl_map_union,
7650 * which takes care of handling the situation where "map1" and "map2"
7651 * may not be disjoint.
7653 * If one of the inputs is empty, we can simply return the other input.
7654 * Similarly, if one of the inputs is universal, then it is equal to the union.
7656 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7657 __isl_take isl_map
*map2
)
7661 struct isl_map
*map
= NULL
;
7667 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7668 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7669 "spaces don't match", goto error
);
7680 is_universe
= isl_map_plain_is_universe(map1
);
7681 if (is_universe
< 0)
7688 is_universe
= isl_map_plain_is_universe(map2
);
7689 if (is_universe
< 0)
7696 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7697 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7698 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7700 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7701 map1
->n
+ map2
->n
, flags
);
7704 for (i
= 0; i
< map1
->n
; ++i
) {
7705 map
= isl_map_add_basic_map(map
,
7706 isl_basic_map_copy(map1
->p
[i
]));
7710 for (i
= 0; i
< map2
->n
; ++i
) {
7711 map
= isl_map_add_basic_map(map
,
7712 isl_basic_map_copy(map2
->p
[i
]));
7726 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7727 * guaranteed to be disjoint by the caller.
7729 * Note that this functions is called from within isl_map_make_disjoint,
7730 * so we have to be careful not to touch the constraints of the inputs
7733 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7734 __isl_take isl_map
*map2
)
7736 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7739 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7740 * not be disjoint. The parameters are assumed to have been aligned.
7742 * We currently simply call map_union_disjoint, the internal operation
7743 * of which does not really depend on the inputs being disjoint.
7744 * If the result contains more than one basic map, then we clear
7745 * the disjoint flag since the result may contain basic maps from
7746 * both inputs and these are not guaranteed to be disjoint.
7748 * As a special case, if "map1" and "map2" are obviously equal,
7749 * then we simply return "map1".
7751 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7752 __isl_take isl_map
*map2
)
7759 equal
= isl_map_plain_is_equal(map1
, map2
);
7767 map1
= map_union_disjoint(map1
, map2
);
7771 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7779 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7782 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7783 __isl_take isl_map
*map2
)
7785 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7788 __isl_give isl_set
*isl_set_union_disjoint(
7789 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7791 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7795 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7797 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7800 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7803 * "map" and "set" are assumed to be compatible and non-NULL.
7805 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7806 __isl_take isl_set
*set
,
7807 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7808 __isl_take isl_basic_set
*bset
))
7811 struct isl_map
*result
;
7814 if (isl_set_plain_is_universe(set
)) {
7819 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7820 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7821 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7823 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7824 map
->n
* set
->n
, flags
);
7825 for (i
= 0; result
&& i
< map
->n
; ++i
)
7826 for (j
= 0; j
< set
->n
; ++j
) {
7827 result
= isl_map_add_basic_map(result
,
7828 fn(isl_basic_map_copy(map
->p
[i
]),
7829 isl_basic_set_copy(set
->p
[j
])));
7839 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7840 __isl_take isl_set
*set
)
7844 ok
= isl_map_compatible_range(map
, set
);
7848 isl_die(set
->ctx
, isl_error_invalid
,
7849 "incompatible spaces", goto error
);
7851 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7858 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7859 __isl_take isl_set
*set
)
7861 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7864 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7865 __isl_take isl_set
*set
)
7869 ok
= isl_map_compatible_domain(map
, set
);
7873 isl_die(set
->ctx
, isl_error_invalid
,
7874 "incompatible spaces", goto error
);
7876 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7883 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7884 __isl_take isl_set
*set
)
7886 return isl_map_align_params_map_map_and(map
, set
,
7887 &map_intersect_domain
);
7890 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7891 * in the space B -> C, return the intersection.
7892 * The parameters are assumed to have been aligned.
7894 * The map "factor" is first extended to a map living in the space
7895 * [A -> B] -> C and then a regular intersection is computed.
7897 static __isl_give isl_map
*map_intersect_domain_factor_range(
7898 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7901 isl_map
*ext_factor
;
7903 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7904 ext_factor
= isl_map_universe(space
);
7905 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7906 return map_intersect(map
, ext_factor
);
7909 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7910 * in the space B -> C, return the intersection.
7912 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7913 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7915 return isl_map_align_params_map_map_and(map
, factor
,
7916 &map_intersect_domain_factor_range
);
7919 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7920 * in the space A -> C, return the intersection.
7922 * The map "factor" is first extended to a map living in the space
7923 * A -> [B -> C] and then a regular intersection is computed.
7925 static __isl_give isl_map
*map_intersect_range_factor_range(
7926 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7929 isl_map
*ext_factor
;
7931 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7932 ext_factor
= isl_map_universe(space
);
7933 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7934 return isl_map_intersect(map
, ext_factor
);
7937 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7938 * in the space A -> C, return the intersection.
7940 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7941 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7943 return isl_map_align_params_map_map_and(map
, factor
,
7944 &map_intersect_range_factor_range
);
7947 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7948 __isl_take isl_map
*map2
)
7952 map1
= isl_map_reverse(map1
);
7953 map1
= isl_map_apply_range(map1
, map2
);
7954 return isl_map_reverse(map1
);
7961 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7962 __isl_take isl_map
*map2
)
7964 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7967 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7968 __isl_take isl_map
*map2
)
7971 struct isl_map
*result
;
7977 space
= isl_space_join(isl_space_copy(map1
->dim
),
7978 isl_space_copy(map2
->dim
));
7980 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
7983 for (i
= 0; i
< map1
->n
; ++i
)
7984 for (j
= 0; j
< map2
->n
; ++j
) {
7985 result
= isl_map_add_basic_map(result
,
7986 isl_basic_map_apply_range(
7987 isl_basic_map_copy(map1
->p
[i
]),
7988 isl_basic_map_copy(map2
->p
[j
])));
7994 if (result
&& result
->n
<= 1)
7995 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8003 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8004 __isl_take isl_map
*map2
)
8006 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8010 * returns range - domain
8012 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8014 isl_space
*target_space
;
8015 struct isl_basic_set
*bset
;
8022 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8023 bmap
->dim
, isl_dim_out
),
8025 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8026 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8027 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8028 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8029 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8030 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8031 for (i
= 0; i
< dim
; ++i
) {
8032 int j
= isl_basic_map_alloc_equality(bmap
);
8034 bmap
= isl_basic_map_free(bmap
);
8037 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8038 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8039 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8040 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8042 bset
= isl_basic_map_domain(bmap
);
8043 bset
= isl_basic_set_reset_space(bset
, target_space
);
8046 isl_basic_map_free(bmap
);
8051 * returns range - domain
8053 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8057 struct isl_set
*result
;
8062 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8063 map
->dim
, isl_dim_out
),
8065 dim
= isl_map_get_space(map
);
8066 dim
= isl_space_domain(dim
);
8067 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8070 for (i
= 0; i
< map
->n
; ++i
)
8071 result
= isl_set_add_basic_set(result
,
8072 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8081 * returns [domain -> range] -> range - domain
8083 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8084 __isl_take isl_basic_map
*bmap
)
8088 isl_basic_map
*domain
;
8092 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8093 bmap
->dim
, isl_dim_out
))
8094 isl_die(bmap
->ctx
, isl_error_invalid
,
8095 "domain and range don't match", goto error
);
8097 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8098 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8100 space
= isl_basic_map_get_space(bmap
);
8101 space
= isl_space_from_range(isl_space_domain(space
));
8102 domain
= isl_basic_map_universe(space
);
8104 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8105 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8106 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8108 total
= isl_basic_map_total_dim(bmap
);
8110 for (i
= 0; i
< n
; ++i
) {
8111 k
= isl_basic_map_alloc_equality(bmap
);
8114 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8115 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8116 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8117 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8120 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8121 return isl_basic_map_finalize(bmap
);
8123 isl_basic_map_free(bmap
);
8128 * returns [domain -> range] -> range - domain
8130 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8133 isl_space
*domain_space
;
8138 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8139 map
->dim
, isl_dim_out
))
8140 isl_die(map
->ctx
, isl_error_invalid
,
8141 "domain and range don't match", goto error
);
8143 map
= isl_map_cow(map
);
8147 domain_space
= isl_space_domain(isl_map_get_space(map
));
8148 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8149 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8152 for (i
= 0; i
< map
->n
; ++i
) {
8153 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8157 map
= isl_map_unmark_normalized(map
);
8164 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8166 unsigned n_in
, n_out
;
8170 n_in
= isl_space_dim(space
, isl_dim_in
);
8171 n_out
= isl_space_dim(space
, isl_dim_out
);
8173 isl_die(space
->ctx
, isl_error_invalid
,
8174 "number of input and output dimensions needs to be "
8175 "the same", goto error
);
8176 return isl_basic_map_equal(space
, n_in
);
8178 isl_space_free(space
);
8182 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8184 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8187 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8189 isl_space
*dim
= isl_set_get_space(set
);
8191 id
= isl_map_identity(isl_space_map_from_set(dim
));
8192 return isl_map_intersect_range(id
, set
);
8195 /* Construct a basic set with all set dimensions having only non-negative
8198 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8199 __isl_take isl_space
*space
)
8204 struct isl_basic_set
*bset
;
8208 nparam
= space
->nparam
;
8210 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8213 for (i
= 0; i
< dim
; ++i
) {
8214 int k
= isl_basic_set_alloc_inequality(bset
);
8217 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8218 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8222 isl_basic_set_free(bset
);
8226 /* Construct the half-space x_pos >= 0.
8228 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8232 isl_basic_set
*nonneg
;
8234 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8235 k
= isl_basic_set_alloc_inequality(nonneg
);
8238 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8239 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8241 return isl_basic_set_finalize(nonneg
);
8243 isl_basic_set_free(nonneg
);
8247 /* Construct the half-space x_pos <= -1.
8249 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8255 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8256 k
= isl_basic_set_alloc_inequality(neg
);
8259 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8260 isl_int_set_si(neg
->ineq
[k
][0], -1);
8261 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8263 return isl_basic_set_finalize(neg
);
8265 isl_basic_set_free(neg
);
8269 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8270 enum isl_dim_type type
, unsigned first
, unsigned n
)
8274 isl_basic_set
*nonneg
;
8282 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8284 offset
= pos(set
->dim
, type
);
8285 for (i
= 0; i
< n
; ++i
) {
8286 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8287 offset
+ first
+ i
);
8288 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8290 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8299 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8301 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8307 return isl_stat_error
;
8308 if (isl_set_plain_is_empty(set
)) {
8313 return fn(set
, signs
, user
);
8316 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8318 half
= isl_set_intersect(half
, isl_set_copy(set
));
8319 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8323 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8325 half
= isl_set_intersect(half
, set
);
8326 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8329 return isl_stat_error
;
8332 /* Call "fn" on the intersections of "set" with each of the orthants
8333 * (except for obviously empty intersections). The orthant is identified
8334 * by the signs array, with each entry having value 1 or -1 according
8335 * to the sign of the corresponding variable.
8337 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8338 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8347 return isl_stat_error
;
8348 if (isl_set_plain_is_empty(set
))
8351 nparam
= isl_set_dim(set
, isl_dim_param
);
8352 nvar
= isl_set_dim(set
, isl_dim_set
);
8354 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8356 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8364 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8366 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8369 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8370 __isl_keep isl_basic_map
*bmap2
)
8373 struct isl_map
*map1
;
8374 struct isl_map
*map2
;
8376 if (!bmap1
|| !bmap2
)
8377 return isl_bool_error
;
8379 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8380 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8382 is_subset
= isl_map_is_subset(map1
, map2
);
8390 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8391 __isl_keep isl_basic_set
*bset2
)
8393 return isl_basic_map_is_subset(bset1
, bset2
);
8396 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8397 __isl_keep isl_basic_map
*bmap2
)
8401 if (!bmap1
|| !bmap2
)
8402 return isl_bool_error
;
8403 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8404 if (is_subset
!= isl_bool_true
)
8406 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8410 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8411 __isl_keep isl_basic_set
*bset2
)
8413 return isl_basic_map_is_equal(
8414 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8417 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8423 return isl_bool_error
;
8424 for (i
= 0; i
< map
->n
; ++i
) {
8425 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8427 return isl_bool_error
;
8429 return isl_bool_false
;
8431 return isl_bool_true
;
8434 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8436 return map
? map
->n
== 0 : isl_bool_error
;
8439 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8441 return set
? set
->n
== 0 : isl_bool_error
;
8444 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8446 return isl_map_is_empty(set_to_map(set
));
8449 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8450 __isl_keep isl_map
*map2
)
8453 return isl_bool_error
;
8455 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8458 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8459 __isl_keep isl_set
*set2
)
8462 return isl_bool_error
;
8464 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8467 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8472 return isl_bool_error
;
8473 is_subset
= isl_map_is_subset(map1
, map2
);
8474 if (is_subset
!= isl_bool_true
)
8476 is_subset
= isl_map_is_subset(map2
, map1
);
8480 /* Is "map1" equal to "map2"?
8482 * First check if they are obviously equal.
8483 * If not, then perform a more detailed analysis.
8485 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8489 equal
= isl_map_plain_is_equal(map1
, map2
);
8490 if (equal
< 0 || equal
)
8492 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8495 isl_bool
isl_basic_map_is_strict_subset(
8496 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8500 if (!bmap1
|| !bmap2
)
8501 return isl_bool_error
;
8502 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8503 if (is_subset
!= isl_bool_true
)
8505 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8506 return isl_bool_not(is_subset
);
8509 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8510 __isl_keep isl_map
*map2
)
8515 return isl_bool_error
;
8516 is_subset
= isl_map_is_subset(map1
, map2
);
8517 if (is_subset
!= isl_bool_true
)
8519 is_subset
= isl_map_is_subset(map2
, map1
);
8520 return isl_bool_not(is_subset
);
8523 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8524 __isl_keep isl_set
*set2
)
8526 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8529 /* Is "bmap" obviously equal to the universe with the same space?
8531 * That is, does it not have any constraints?
8533 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8536 return isl_bool_error
;
8537 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8540 /* Is "bset" obviously equal to the universe with the same space?
8542 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8544 return isl_basic_map_plain_is_universe(bset
);
8547 /* If "c" does not involve any existentially quantified variables,
8548 * then set *univ to false and abort
8550 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8552 isl_bool
*univ
= user
;
8555 n
= isl_constraint_dim(c
, isl_dim_div
);
8556 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8557 isl_constraint_free(c
);
8558 if (*univ
< 0 || !*univ
)
8559 return isl_stat_error
;
8563 /* Is "bmap" equal to the universe with the same space?
8565 * First check if it is obviously equal to the universe.
8566 * If not and if there are any constraints not involving
8567 * existentially quantified variables, then it is certainly
8568 * not equal to the universe.
8569 * Otherwise, check if the universe is a subset of "bmap".
8571 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8574 isl_basic_map
*test
;
8576 univ
= isl_basic_map_plain_is_universe(bmap
);
8577 if (univ
< 0 || univ
)
8579 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8580 return isl_bool_false
;
8581 univ
= isl_bool_true
;
8582 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8584 return isl_bool_error
;
8585 if (univ
< 0 || !univ
)
8587 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8588 univ
= isl_basic_map_is_subset(test
, bmap
);
8589 isl_basic_map_free(test
);
8593 /* Is "bset" equal to the universe with the same space?
8595 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8597 return isl_basic_map_is_universe(bset
);
8600 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8605 return isl_bool_error
;
8607 for (i
= 0; i
< map
->n
; ++i
) {
8608 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8613 return isl_bool_false
;
8616 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8618 return isl_map_plain_is_universe(set_to_map(set
));
8621 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8623 struct isl_basic_set
*bset
= NULL
;
8624 struct isl_vec
*sample
= NULL
;
8625 isl_bool empty
, non_empty
;
8628 return isl_bool_error
;
8630 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8631 return isl_bool_true
;
8633 if (isl_basic_map_plain_is_universe(bmap
))
8634 return isl_bool_false
;
8636 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8637 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8638 copy
= isl_basic_map_remove_redundancies(copy
);
8639 empty
= isl_basic_map_plain_is_empty(copy
);
8640 isl_basic_map_free(copy
);
8644 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8646 return isl_bool_error
;
8648 return isl_bool_false
;
8649 isl_vec_free(bmap
->sample
);
8650 bmap
->sample
= NULL
;
8651 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8653 return isl_bool_error
;
8654 sample
= isl_basic_set_sample_vec(bset
);
8656 return isl_bool_error
;
8657 empty
= sample
->size
== 0;
8658 isl_vec_free(bmap
->sample
);
8659 bmap
->sample
= sample
;
8661 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8666 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8669 return isl_bool_error
;
8670 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8673 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8676 return isl_bool_error
;
8677 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8680 /* Is "bmap" known to be non-empty?
8682 * That is, is the cached sample still valid?
8684 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8689 return isl_bool_error
;
8691 return isl_bool_false
;
8692 total
= 1 + isl_basic_map_total_dim(bmap
);
8693 if (bmap
->sample
->size
!= total
)
8694 return isl_bool_false
;
8695 return isl_basic_map_contains(bmap
, bmap
->sample
);
8698 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8700 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8703 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8704 __isl_take isl_basic_map
*bmap2
)
8706 struct isl_map
*map
;
8707 if (!bmap1
|| !bmap2
)
8710 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8712 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8715 map
= isl_map_add_basic_map(map
, bmap1
);
8716 map
= isl_map_add_basic_map(map
, bmap2
);
8719 isl_basic_map_free(bmap1
);
8720 isl_basic_map_free(bmap2
);
8724 struct isl_set
*isl_basic_set_union(
8725 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8727 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8728 bset_to_bmap(bset2
)));
8731 /* Order divs such that any div only depends on previous divs */
8732 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8733 __isl_take isl_basic_map
*bmap
)
8741 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8743 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8745 if (isl_int_is_zero(bmap
->div
[i
][0]))
8747 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8752 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8753 "integer division depends on itself",
8754 return isl_basic_map_free(bmap
));
8755 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8761 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8763 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8766 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8773 for (i
= 0; i
< map
->n
; ++i
) {
8774 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8785 /* Sort the local variables of "bset".
8787 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8788 __isl_take isl_basic_set
*bset
)
8790 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8793 /* Apply the expansion computed by isl_merge_divs.
8794 * The expansion itself is given by "exp" while the resulting
8795 * list of divs is given by "div".
8797 * Move the integer divisions of "bmap" into the right position
8798 * according to "exp" and then introduce the additional integer
8799 * divisions, adding div constraints.
8800 * The moving should be done first to avoid moving coefficients
8801 * in the definitions of the extra integer divisions.
8803 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8804 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8809 bmap
= isl_basic_map_cow(bmap
);
8813 if (div
->n_row
< bmap
->n_div
)
8814 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8815 "not an expansion", goto error
);
8817 n_div
= bmap
->n_div
;
8818 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8819 div
->n_row
- n_div
, 0,
8820 2 * (div
->n_row
- n_div
));
8822 for (i
= n_div
; i
< div
->n_row
; ++i
)
8823 if (isl_basic_map_alloc_div(bmap
) < 0)
8826 for (j
= n_div
- 1; j
>= 0; --j
) {
8829 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8832 for (i
= 0; i
< div
->n_row
; ++i
) {
8833 if (j
< n_div
&& exp
[j
] == i
) {
8836 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8837 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8839 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8847 isl_basic_map_free(bmap
);
8852 /* Apply the expansion computed by isl_merge_divs.
8853 * The expansion itself is given by "exp" while the resulting
8854 * list of divs is given by "div".
8856 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8857 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8859 return isl_basic_map_expand_divs(bset
, div
, exp
);
8862 /* Look for a div in dst that corresponds to the div "div" in src.
8863 * The divs before "div" in src and dst are assumed to be the same.
8865 * Returns -1 if no corresponding div was found and the position
8866 * of the corresponding div in dst otherwise.
8868 static int find_div(__isl_keep isl_basic_map
*dst
,
8869 __isl_keep isl_basic_map
*src
, unsigned div
)
8873 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8875 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8876 for (i
= div
; i
< dst
->n_div
; ++i
)
8877 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8878 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8879 dst
->n_div
- div
) == -1)
8884 /* Align the divs of "dst" to those of "src", adding divs from "src"
8885 * if needed. That is, make sure that the first src->n_div divs
8886 * of the result are equal to those of src.
8888 * The result is not finalized as by design it will have redundant
8889 * divs if any divs from "src" were copied.
8891 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8892 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8900 return isl_basic_map_free(dst
);
8902 if (src
->n_div
== 0)
8905 known
= isl_basic_map_divs_known(src
);
8907 return isl_basic_map_free(dst
);
8909 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8910 "some src divs are unknown",
8911 return isl_basic_map_free(dst
));
8913 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8915 return isl_basic_map_free(dst
);
8918 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8919 for (i
= 0; i
< src
->n_div
; ++i
) {
8920 int j
= find_div(dst
, src
, i
);
8923 int extra
= src
->n_div
- i
;
8924 dst
= isl_basic_map_cow(dst
);
8927 dst
= isl_basic_map_extend_space(dst
,
8928 isl_space_copy(dst
->dim
),
8929 extra
, 0, 2 * extra
);
8932 j
= isl_basic_map_alloc_div(dst
);
8935 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8936 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8937 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8941 isl_basic_map_swap_div(dst
, i
, j
);
8943 isl_basic_map_free(src
);
8946 isl_basic_map_free(src
);
8947 isl_basic_map_free(dst
);
8951 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8959 map
= isl_map_compute_divs(map
);
8960 map
= isl_map_cow(map
);
8964 for (i
= 1; i
< map
->n
; ++i
)
8965 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8966 for (i
= 1; i
< map
->n
; ++i
) {
8967 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8969 return isl_map_free(map
);
8972 map
= isl_map_unmark_normalized(map
);
8976 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8978 return isl_map_align_divs_internal(map
);
8981 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8983 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8986 /* Align the divs of the basic maps in "map" to those
8987 * of the basic maps in "list", as well as to the other basic maps in "map".
8988 * The elements in "list" are assumed to have known divs.
8990 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8991 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8995 map
= isl_map_compute_divs(map
);
8996 map
= isl_map_cow(map
);
8998 return isl_map_free(map
);
9002 n
= isl_basic_map_list_n_basic_map(list
);
9003 for (i
= 0; i
< n
; ++i
) {
9004 isl_basic_map
*bmap
;
9006 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9007 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9008 isl_basic_map_free(bmap
);
9011 return isl_map_free(map
);
9013 return isl_map_align_divs_internal(map
);
9016 /* Align the divs of each element of "list" to those of "bmap".
9017 * Both "bmap" and the elements of "list" are assumed to have known divs.
9019 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9020 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9025 return isl_basic_map_list_free(list
);
9027 n
= isl_basic_map_list_n_basic_map(list
);
9028 for (i
= 0; i
< n
; ++i
) {
9029 isl_basic_map
*bmap_i
;
9031 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9032 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9033 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9039 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9040 __isl_take isl_map
*map
)
9044 ok
= isl_map_compatible_domain(map
, set
);
9048 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9049 "incompatible spaces", goto error
);
9050 map
= isl_map_intersect_domain(map
, set
);
9051 set
= isl_map_range(map
);
9059 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9060 __isl_take isl_map
*map
)
9062 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9065 /* There is no need to cow as removing empty parts doesn't change
9066 * the meaning of the set.
9068 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9075 for (i
= map
->n
- 1; i
>= 0; --i
)
9076 map
= remove_if_empty(map
, i
);
9081 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9083 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9086 /* Create a binary relation that maps the shared initial "pos" dimensions
9087 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9089 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9090 __isl_keep isl_basic_set
*bset2
, int pos
)
9092 isl_basic_map
*bmap1
;
9093 isl_basic_map
*bmap2
;
9095 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9096 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9097 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9098 isl_dim_out
, 0, pos
);
9099 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9100 isl_dim_out
, 0, pos
);
9101 return isl_basic_map_range_product(bmap1
, bmap2
);
9104 /* Given two basic sets bset1 and bset2, compute the maximal difference
9105 * between the values of dimension pos in bset1 and those in bset2
9106 * for any common value of the parameters and dimensions preceding pos.
9108 static enum isl_lp_result
basic_set_maximal_difference_at(
9109 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9110 int pos
, isl_int
*opt
)
9112 isl_basic_map
*bmap1
;
9113 struct isl_ctx
*ctx
;
9114 struct isl_vec
*obj
;
9118 enum isl_lp_result res
;
9120 if (!bset1
|| !bset2
)
9121 return isl_lp_error
;
9123 nparam
= isl_basic_set_n_param(bset1
);
9124 dim1
= isl_basic_set_n_dim(bset1
);
9126 bmap1
= join_initial(bset1
, bset2
, pos
);
9128 return isl_lp_error
;
9130 total
= isl_basic_map_total_dim(bmap1
);
9132 obj
= isl_vec_alloc(ctx
, 1 + total
);
9135 isl_seq_clr(obj
->block
.data
, 1 + total
);
9136 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9137 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9138 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9140 isl_basic_map_free(bmap1
);
9144 isl_basic_map_free(bmap1
);
9145 return isl_lp_error
;
9148 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9149 * for any common value of the parameters and dimensions preceding pos
9150 * in both basic sets, the values of dimension pos in bset1 are
9151 * smaller or larger than those in bset2.
9154 * 1 if bset1 follows bset2
9155 * -1 if bset1 precedes bset2
9156 * 0 if bset1 and bset2 are incomparable
9157 * -2 if some error occurred.
9159 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9160 __isl_keep isl_basic_set
*bset2
, int pos
)
9163 enum isl_lp_result res
;
9168 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9170 if (res
== isl_lp_empty
)
9172 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9173 res
== isl_lp_unbounded
)
9175 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9184 /* Given two basic sets bset1 and bset2, check whether
9185 * for any common value of the parameters and dimensions preceding pos
9186 * there is a value of dimension pos in bset1 that is larger
9187 * than a value of the same dimension in bset2.
9190 * 1 if there exists such a pair
9191 * 0 if there is no such pair, but there is a pair of equal values
9193 * -2 if some error occurred.
9195 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9196 __isl_keep isl_basic_set
*bset2
, int pos
)
9199 isl_basic_map
*bmap
;
9202 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9203 bmap
= join_initial(bset1
, bset2
, pos
);
9204 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9205 isl_dim_out
, dim1
- pos
);
9206 empty
= isl_basic_map_is_empty(bmap
);
9210 isl_basic_map_free(bmap
);
9213 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9214 isl_dim_out
, dim1
- pos
);
9215 empty
= isl_basic_map_is_empty(bmap
);
9218 isl_basic_map_free(bmap
);
9223 isl_basic_map_free(bmap
);
9227 /* Given two sets set1 and set2, check whether
9228 * for any common value of the parameters and dimensions preceding pos
9229 * there is a value of dimension pos in set1 that is larger
9230 * than a value of the same dimension in set2.
9233 * 1 if there exists such a pair
9234 * 0 if there is no such pair, but there is a pair of equal values
9236 * -2 if some error occurred.
9238 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9239 __isl_keep isl_set
*set2
, int pos
)
9247 for (i
= 0; i
< set1
->n
; ++i
)
9248 for (j
= 0; j
< set2
->n
; ++j
) {
9250 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9251 if (f
== 1 || f
== -2)
9260 static isl_bool
isl_basic_map_plain_has_fixed_var(
9261 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9268 return isl_bool_error
;
9269 total
= isl_basic_map_total_dim(bmap
);
9270 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9271 for (; d
+1 > pos
; --d
)
9272 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9276 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9277 return isl_bool_false
;
9278 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9279 return isl_bool_false
;
9280 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9281 return isl_bool_false
;
9283 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9284 return isl_bool_true
;
9286 return isl_bool_false
;
9289 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9290 unsigned pos
, isl_int
*val
)
9298 return isl_bool_error
;
9300 return isl_bool_false
;
9302 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9305 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9306 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9307 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9308 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9309 fixed
= isl_bool_false
;
9312 isl_int_set(*val
, v
);
9318 static isl_bool
isl_basic_set_plain_has_fixed_var(
9319 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9321 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9325 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9326 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9328 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9329 return isl_bool_error
;
9330 return isl_basic_map_plain_has_fixed_var(bmap
,
9331 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9334 /* If "bmap" obviously lies on a hyperplane where the given dimension
9335 * has a fixed value, then return that value.
9336 * Otherwise return NaN.
9338 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9339 __isl_keep isl_basic_map
*bmap
,
9340 enum isl_dim_type type
, unsigned pos
)
9348 ctx
= isl_basic_map_get_ctx(bmap
);
9349 v
= isl_val_alloc(ctx
);
9352 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9354 return isl_val_free(v
);
9356 isl_int_set_si(v
->d
, 1);
9360 return isl_val_nan(ctx
);
9363 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9364 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9366 if (pos
>= isl_map_dim(map
, type
))
9367 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9368 "position out of bounds", return isl_bool_error
);
9369 return isl_map_plain_has_fixed_var(map
,
9370 map_offset(map
, type
) - 1 + pos
, val
);
9373 /* If "map" obviously lies on a hyperplane where the given dimension
9374 * has a fixed value, then return that value.
9375 * Otherwise return NaN.
9377 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9378 enum isl_dim_type type
, unsigned pos
)
9386 ctx
= isl_map_get_ctx(map
);
9387 v
= isl_val_alloc(ctx
);
9390 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9392 return isl_val_free(v
);
9394 isl_int_set_si(v
->d
, 1);
9398 return isl_val_nan(ctx
);
9401 /* If "set" obviously lies on a hyperplane where the given dimension
9402 * has a fixed value, then return that value.
9403 * Otherwise return NaN.
9405 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9406 enum isl_dim_type type
, unsigned pos
)
9408 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9411 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9412 * then return this fixed value in *val.
9414 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9415 unsigned dim
, isl_int
*val
)
9417 return isl_basic_set_plain_has_fixed_var(bset
,
9418 isl_basic_set_n_param(bset
) + dim
, val
);
9421 /* Return -1 if the constraint "c1" should be sorted before "c2"
9422 * and 1 if it should be sorted after "c2".
9423 * Return 0 if the two constraints are the same (up to the constant term).
9425 * In particular, if a constraint involves later variables than another
9426 * then it is sorted after this other constraint.
9427 * uset_gist depends on constraints without existentially quantified
9428 * variables sorting first.
9430 * For constraints that have the same latest variable, those
9431 * with the same coefficient for this latest variable (first in absolute value
9432 * and then in actual value) are grouped together.
9433 * This is useful for detecting pairs of constraints that can
9434 * be chained in their printed representation.
9436 * Finally, within a group, constraints are sorted according to
9437 * their coefficients (excluding the constant term).
9439 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9441 isl_int
**c1
= (isl_int
**) p1
;
9442 isl_int
**c2
= (isl_int
**) p2
;
9444 unsigned size
= *(unsigned *) arg
;
9447 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9448 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9453 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9456 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9460 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9463 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9464 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9465 * and 0 if the two constraints are the same (up to the constant term).
9467 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9468 isl_int
*c1
, isl_int
*c2
)
9474 total
= isl_basic_map_total_dim(bmap
);
9475 return sort_constraint_cmp(&c1
, &c2
, &total
);
9478 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9479 __isl_take isl_basic_map
*bmap
)
9485 if (bmap
->n_ineq
== 0)
9487 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9489 total
= isl_basic_map_total_dim(bmap
);
9490 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9491 &sort_constraint_cmp
, &total
) < 0)
9492 return isl_basic_map_free(bmap
);
9493 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9497 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9498 __isl_take isl_basic_set
*bset
)
9500 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9501 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9504 __isl_give isl_basic_map
*isl_basic_map_normalize(
9505 __isl_take isl_basic_map
*bmap
)
9507 bmap
= isl_basic_map_remove_redundancies(bmap
);
9508 bmap
= isl_basic_map_sort_constraints(bmap
);
9511 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9512 __isl_keep isl_basic_map
*bmap2
)
9516 isl_space
*space1
, *space2
;
9518 if (!bmap1
|| !bmap2
)
9523 space1
= isl_basic_map_peek_space(bmap1
);
9524 space2
= isl_basic_map_peek_space(bmap2
);
9525 cmp
= isl_space_cmp(space1
, space2
);
9528 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9529 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9530 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9531 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9532 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9534 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9536 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9538 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9539 return bmap1
->n_eq
- bmap2
->n_eq
;
9540 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9541 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9542 if (bmap1
->n_div
!= bmap2
->n_div
)
9543 return bmap1
->n_div
- bmap2
->n_div
;
9544 total
= isl_basic_map_total_dim(bmap1
);
9545 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9546 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9550 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9551 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9555 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9556 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9563 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9564 __isl_keep isl_basic_set
*bset2
)
9566 return isl_basic_map_plain_cmp(bset1
, bset2
);
9569 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9575 if (set1
->n
!= set2
->n
)
9576 return set1
->n
- set2
->n
;
9578 for (i
= 0; i
< set1
->n
; ++i
) {
9579 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9587 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9588 __isl_keep isl_basic_map
*bmap2
)
9590 if (!bmap1
|| !bmap2
)
9591 return isl_bool_error
;
9592 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9595 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9596 __isl_keep isl_basic_set
*bset2
)
9598 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9599 bset_to_bmap(bset2
));
9602 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9604 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9605 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9607 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9610 /* Sort the basic maps of "map" and remove duplicate basic maps.
9612 * While removing basic maps, we make sure that the basic maps remain
9613 * sorted because isl_map_normalize expects the basic maps of the result
9616 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9620 map
= isl_map_remove_empty_parts(map
);
9623 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9624 for (i
= map
->n
- 1; i
>= 1; --i
) {
9625 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9627 isl_basic_map_free(map
->p
[i
-1]);
9628 for (j
= i
; j
< map
->n
; ++j
)
9629 map
->p
[j
- 1] = map
->p
[j
];
9636 /* Remove obvious duplicates among the basic maps of "map".
9638 * Unlike isl_map_normalize, this function does not remove redundant
9639 * constraints and only removes duplicates that have exactly the same
9640 * constraints in the input. It does sort the constraints and
9641 * the basic maps to ease the detection of duplicates.
9643 * If "map" has already been normalized or if the basic maps are
9644 * disjoint, then there can be no duplicates.
9646 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9649 isl_basic_map
*bmap
;
9655 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9657 for (i
= 0; i
< map
->n
; ++i
) {
9658 bmap
= isl_basic_map_copy(map
->p
[i
]);
9659 bmap
= isl_basic_map_sort_constraints(bmap
);
9661 return isl_map_free(map
);
9662 isl_basic_map_free(map
->p
[i
]);
9666 map
= sort_and_remove_duplicates(map
);
9670 /* We normalize in place, but if anything goes wrong we need
9671 * to return NULL, so we need to make sure we don't change the
9672 * meaning of any possible other copies of map.
9674 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9677 struct isl_basic_map
*bmap
;
9681 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9683 for (i
= 0; i
< map
->n
; ++i
) {
9684 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9687 isl_basic_map_free(map
->p
[i
]);
9691 map
= sort_and_remove_duplicates(map
);
9693 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9700 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9702 return set_from_map(isl_map_normalize(set_to_map(set
)));
9705 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9706 __isl_keep isl_map
*map2
)
9712 return isl_bool_error
;
9715 return isl_bool_true
;
9716 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9717 return isl_bool_false
;
9719 map1
= isl_map_copy(map1
);
9720 map2
= isl_map_copy(map2
);
9721 map1
= isl_map_normalize(map1
);
9722 map2
= isl_map_normalize(map2
);
9725 equal
= map1
->n
== map2
->n
;
9726 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9727 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9737 return isl_bool_error
;
9740 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9741 __isl_keep isl_set
*set2
)
9743 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9746 /* Return the basic maps in "map" as a list.
9748 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9749 __isl_keep isl_map
*map
)
9753 isl_basic_map_list
*list
;
9757 ctx
= isl_map_get_ctx(map
);
9758 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9760 for (i
= 0; i
< map
->n
; ++i
) {
9761 isl_basic_map
*bmap
;
9763 bmap
= isl_basic_map_copy(map
->p
[i
]);
9764 list
= isl_basic_map_list_add(list
, bmap
);
9770 /* Return the intersection of the elements in the non-empty list "list".
9771 * All elements are assumed to live in the same space.
9773 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9774 __isl_take isl_basic_map_list
*list
)
9777 isl_basic_map
*bmap
;
9781 n
= isl_basic_map_list_n_basic_map(list
);
9783 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9784 "expecting non-empty list", goto error
);
9786 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9787 for (i
= 1; i
< n
; ++i
) {
9788 isl_basic_map
*bmap_i
;
9790 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9791 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9794 isl_basic_map_list_free(list
);
9797 isl_basic_map_list_free(list
);
9801 /* Return the intersection of the elements in the non-empty list "list".
9802 * All elements are assumed to live in the same space.
9804 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9805 __isl_take isl_basic_set_list
*list
)
9807 return isl_basic_map_list_intersect(list
);
9810 /* Return the union of the elements of "list".
9811 * The list is required to have at least one element.
9813 __isl_give isl_set
*isl_basic_set_list_union(
9814 __isl_take isl_basic_set_list
*list
)
9818 isl_basic_set
*bset
;
9823 n
= isl_basic_set_list_n_basic_set(list
);
9825 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9826 "expecting non-empty list", goto error
);
9828 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9829 space
= isl_basic_set_get_space(bset
);
9830 isl_basic_set_free(bset
);
9832 set
= isl_set_alloc_space(space
, n
, 0);
9833 for (i
= 0; i
< n
; ++i
) {
9834 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9835 set
= isl_set_add_basic_set(set
, bset
);
9838 isl_basic_set_list_free(list
);
9841 isl_basic_set_list_free(list
);
9845 /* Return the union of the elements in the non-empty list "list".
9846 * All elements are assumed to live in the same space.
9848 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9855 n
= isl_set_list_n_set(list
);
9857 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9858 "expecting non-empty list", goto error
);
9860 set
= isl_set_list_get_set(list
, 0);
9861 for (i
= 1; i
< n
; ++i
) {
9864 set_i
= isl_set_list_get_set(list
, i
);
9865 set
= isl_set_union(set
, set_i
);
9868 isl_set_list_free(list
);
9871 isl_set_list_free(list
);
9875 __isl_give isl_basic_map
*isl_basic_map_product(
9876 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9878 isl_space
*space_result
= NULL
;
9879 struct isl_basic_map
*bmap
;
9880 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9881 struct isl_dim_map
*dim_map1
, *dim_map2
;
9883 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9885 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9886 isl_space_copy(bmap2
->dim
));
9888 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9889 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9890 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9891 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9892 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9894 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9895 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9896 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9897 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9898 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9899 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9900 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9901 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9902 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9903 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9904 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9906 bmap
= isl_basic_map_alloc_space(space_result
,
9907 bmap1
->n_div
+ bmap2
->n_div
,
9908 bmap1
->n_eq
+ bmap2
->n_eq
,
9909 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9910 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9911 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9912 bmap
= isl_basic_map_simplify(bmap
);
9913 return isl_basic_map_finalize(bmap
);
9915 isl_basic_map_free(bmap1
);
9916 isl_basic_map_free(bmap2
);
9920 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9921 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9923 isl_basic_map
*prod
;
9925 prod
= isl_basic_map_product(bmap1
, bmap2
);
9926 prod
= isl_basic_map_flatten(prod
);
9930 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9931 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9933 return isl_basic_map_flat_range_product(bset1
, bset2
);
9936 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9937 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9939 isl_space
*space_result
= NULL
;
9940 isl_basic_map
*bmap
;
9941 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9942 struct isl_dim_map
*dim_map1
, *dim_map2
;
9944 if (!bmap1
|| !bmap2
)
9947 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9948 isl_space_copy(bmap2
->dim
));
9950 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9951 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9952 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9953 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9955 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9956 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9957 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9958 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9959 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9960 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9961 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9962 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9963 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9964 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9965 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9967 bmap
= isl_basic_map_alloc_space(space_result
,
9968 bmap1
->n_div
+ bmap2
->n_div
,
9969 bmap1
->n_eq
+ bmap2
->n_eq
,
9970 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9971 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9972 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9973 bmap
= isl_basic_map_simplify(bmap
);
9974 return isl_basic_map_finalize(bmap
);
9976 isl_basic_map_free(bmap1
);
9977 isl_basic_map_free(bmap2
);
9981 __isl_give isl_basic_map
*isl_basic_map_range_product(
9982 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9985 isl_space
*space_result
= NULL
;
9986 isl_basic_map
*bmap
;
9987 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9988 struct isl_dim_map
*dim_map1
, *dim_map2
;
9990 rational
= isl_basic_map_is_rational(bmap1
);
9991 if (rational
>= 0 && rational
)
9992 rational
= isl_basic_map_is_rational(bmap2
);
9993 if (!bmap1
|| !bmap2
|| rational
< 0)
9996 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9999 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10000 isl_space_copy(bmap2
->dim
));
10002 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10003 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10004 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10005 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10007 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10008 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10009 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10010 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10011 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10012 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10013 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10014 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10015 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10016 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10017 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10019 bmap
= isl_basic_map_alloc_space(space_result
,
10020 bmap1
->n_div
+ bmap2
->n_div
,
10021 bmap1
->n_eq
+ bmap2
->n_eq
,
10022 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10023 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10024 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10026 bmap
= isl_basic_map_set_rational(bmap
);
10027 bmap
= isl_basic_map_simplify(bmap
);
10028 return isl_basic_map_finalize(bmap
);
10030 isl_basic_map_free(bmap1
);
10031 isl_basic_map_free(bmap2
);
10035 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10036 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10038 isl_basic_map
*prod
;
10040 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10041 prod
= isl_basic_map_flatten_range(prod
);
10045 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10046 * and collect the results.
10047 * The result live in the space obtained by calling "space_product"
10048 * on the spaces of "map1" and "map2".
10049 * If "remove_duplicates" is set then the result may contain duplicates
10050 * (even if the inputs do not) and so we try and remove the obvious
10053 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10054 __isl_take isl_map
*map2
,
10055 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10056 __isl_take isl_space
*right
),
10057 __isl_give isl_basic_map
*(*basic_map_product
)(
10058 __isl_take isl_basic_map
*left
,
10059 __isl_take isl_basic_map
*right
),
10060 int remove_duplicates
)
10062 unsigned flags
= 0;
10063 struct isl_map
*result
;
10067 m
= isl_map_has_equal_params(map1
, map2
);
10071 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10072 "parameters don't match", goto error
);
10074 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10075 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10076 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10078 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10079 isl_space_copy(map2
->dim
)),
10080 map1
->n
* map2
->n
, flags
);
10083 for (i
= 0; i
< map1
->n
; ++i
)
10084 for (j
= 0; j
< map2
->n
; ++j
) {
10085 struct isl_basic_map
*part
;
10086 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10087 isl_basic_map_copy(map2
->p
[j
]));
10088 if (isl_basic_map_is_empty(part
))
10089 isl_basic_map_free(part
);
10091 result
= isl_map_add_basic_map(result
, part
);
10095 if (remove_duplicates
)
10096 result
= isl_map_remove_obvious_duplicates(result
);
10097 isl_map_free(map1
);
10098 isl_map_free(map2
);
10101 isl_map_free(map1
);
10102 isl_map_free(map2
);
10106 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10108 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10109 __isl_take isl_map
*map2
)
10111 return map_product(map1
, map2
, &isl_space_product
,
10112 &isl_basic_map_product
, 0);
10115 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10116 __isl_take isl_map
*map2
)
10118 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10121 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10123 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10124 __isl_take isl_map
*map2
)
10128 prod
= isl_map_product(map1
, map2
);
10129 prod
= isl_map_flatten(prod
);
10133 /* Given two set A and B, construct its Cartesian product A x B.
10135 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10137 return isl_map_range_product(set1
, set2
);
10140 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10141 __isl_take isl_set
*set2
)
10143 return isl_map_flat_range_product(set1
, set2
);
10146 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10148 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10149 __isl_take isl_map
*map2
)
10151 return map_product(map1
, map2
, &isl_space_domain_product
,
10152 &isl_basic_map_domain_product
, 1);
10155 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10157 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10158 __isl_take isl_map
*map2
)
10160 return map_product(map1
, map2
, &isl_space_range_product
,
10161 &isl_basic_map_range_product
, 1);
10164 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10165 __isl_take isl_map
*map2
)
10167 return isl_map_align_params_map_map_and(map1
, map2
,
10168 &map_domain_product_aligned
);
10171 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10172 __isl_take isl_map
*map2
)
10174 return isl_map_align_params_map_map_and(map1
, map2
,
10175 &map_range_product_aligned
);
10178 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10180 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10183 int total1
, keep1
, total2
, keep2
;
10187 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10188 !isl_space_range_is_wrapping(map
->dim
))
10189 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10190 "not a product", return isl_map_free(map
));
10192 space
= isl_map_get_space(map
);
10193 total1
= isl_space_dim(space
, isl_dim_in
);
10194 total2
= isl_space_dim(space
, isl_dim_out
);
10195 space
= isl_space_factor_domain(space
);
10196 keep1
= isl_space_dim(space
, isl_dim_in
);
10197 keep2
= isl_space_dim(space
, isl_dim_out
);
10198 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10199 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10200 map
= isl_map_reset_space(map
, space
);
10205 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10207 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10210 int total1
, keep1
, total2
, keep2
;
10214 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10215 !isl_space_range_is_wrapping(map
->dim
))
10216 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10217 "not a product", return isl_map_free(map
));
10219 space
= isl_map_get_space(map
);
10220 total1
= isl_space_dim(space
, isl_dim_in
);
10221 total2
= isl_space_dim(space
, isl_dim_out
);
10222 space
= isl_space_factor_range(space
);
10223 keep1
= isl_space_dim(space
, isl_dim_in
);
10224 keep2
= isl_space_dim(space
, isl_dim_out
);
10225 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10226 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10227 map
= isl_map_reset_space(map
, space
);
10232 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10234 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10241 if (!isl_space_domain_is_wrapping(map
->dim
))
10242 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10243 "domain is not a product", return isl_map_free(map
));
10245 space
= isl_map_get_space(map
);
10246 total
= isl_space_dim(space
, isl_dim_in
);
10247 space
= isl_space_domain_factor_domain(space
);
10248 keep
= isl_space_dim(space
, isl_dim_in
);
10249 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10250 map
= isl_map_reset_space(map
, space
);
10255 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10257 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10264 if (!isl_space_domain_is_wrapping(map
->dim
))
10265 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10266 "domain is not a product", return isl_map_free(map
));
10268 space
= isl_map_get_space(map
);
10269 total
= isl_space_dim(space
, isl_dim_in
);
10270 space
= isl_space_domain_factor_range(space
);
10271 keep
= isl_space_dim(space
, isl_dim_in
);
10272 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10273 map
= isl_map_reset_space(map
, space
);
10278 /* Given a map A -> [B -> C], extract the map A -> B.
10280 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10287 if (!isl_space_range_is_wrapping(map
->dim
))
10288 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10289 "range is not a product", return isl_map_free(map
));
10291 space
= isl_map_get_space(map
);
10292 total
= isl_space_dim(space
, isl_dim_out
);
10293 space
= isl_space_range_factor_domain(space
);
10294 keep
= isl_space_dim(space
, isl_dim_out
);
10295 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10296 map
= isl_map_reset_space(map
, space
);
10301 /* Given a map A -> [B -> C], extract the map A -> C.
10303 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10310 if (!isl_space_range_is_wrapping(map
->dim
))
10311 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10312 "range is not a product", return isl_map_free(map
));
10314 space
= isl_map_get_space(map
);
10315 total
= isl_space_dim(space
, isl_dim_out
);
10316 space
= isl_space_range_factor_range(space
);
10317 keep
= isl_space_dim(space
, isl_dim_out
);
10318 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10319 map
= isl_map_reset_space(map
, space
);
10324 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10326 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10327 __isl_take isl_map
*map2
)
10331 prod
= isl_map_domain_product(map1
, map2
);
10332 prod
= isl_map_flatten_domain(prod
);
10336 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10338 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10339 __isl_take isl_map
*map2
)
10343 prod
= isl_map_range_product(map1
, map2
);
10344 prod
= isl_map_flatten_range(prod
);
10348 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10351 uint32_t hash
= isl_hash_init();
10356 bmap
= isl_basic_map_copy(bmap
);
10357 bmap
= isl_basic_map_normalize(bmap
);
10360 total
= isl_basic_map_total_dim(bmap
);
10361 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10362 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10364 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10365 isl_hash_hash(hash
, c_hash
);
10367 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10368 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10370 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10371 isl_hash_hash(hash
, c_hash
);
10373 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10374 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10376 if (isl_int_is_zero(bmap
->div
[i
][0]))
10378 isl_hash_byte(hash
, i
& 0xFF);
10379 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10380 isl_hash_hash(hash
, c_hash
);
10382 isl_basic_map_free(bmap
);
10386 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10388 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10391 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10398 map
= isl_map_copy(map
);
10399 map
= isl_map_normalize(map
);
10403 hash
= isl_hash_init();
10404 for (i
= 0; i
< map
->n
; ++i
) {
10405 uint32_t bmap_hash
;
10406 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10407 isl_hash_hash(hash
, bmap_hash
);
10415 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10417 return isl_map_get_hash(set_to_map(set
));
10420 /* Return the number of basic maps in the (current) representation of "map".
10422 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10424 return map
? map
->n
: 0;
10427 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10429 return set
? set
->n
: 0;
10432 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10433 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10438 return isl_stat_error
;
10440 for (i
= 0; i
< map
->n
; ++i
)
10441 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10442 return isl_stat_error
;
10444 return isl_stat_ok
;
10447 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10448 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10453 return isl_stat_error
;
10455 for (i
= 0; i
< set
->n
; ++i
)
10456 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10457 return isl_stat_error
;
10459 return isl_stat_ok
;
10462 /* Return a list of basic sets, the union of which is equal to "set".
10464 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10465 __isl_keep isl_set
*set
)
10468 isl_basic_set_list
*list
;
10473 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10474 for (i
= 0; i
< set
->n
; ++i
) {
10475 isl_basic_set
*bset
;
10477 bset
= isl_basic_set_copy(set
->p
[i
]);
10478 list
= isl_basic_set_list_add(list
, bset
);
10484 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10491 bset
= isl_basic_set_cow(bset
);
10495 space
= isl_basic_set_get_space(bset
);
10496 space
= isl_space_lift(space
, bset
->n_div
);
10499 isl_space_free(bset
->dim
);
10501 bset
->extra
-= bset
->n_div
;
10504 bset
= isl_basic_set_finalize(bset
);
10508 isl_basic_set_free(bset
);
10512 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10518 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10523 set
= isl_set_cow(set
);
10527 n_div
= set
->p
[0]->n_div
;
10528 space
= isl_set_get_space(set
);
10529 space
= isl_space_lift(space
, n_div
);
10532 isl_space_free(set
->dim
);
10535 for (i
= 0; i
< set
->n
; ++i
) {
10536 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10547 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10555 dim
= isl_basic_set_total_dim(bset
);
10556 size
+= bset
->n_eq
* (1 + dim
);
10557 size
+= bset
->n_ineq
* (1 + dim
);
10558 size
+= bset
->n_div
* (2 + dim
);
10563 int isl_set_size(__isl_keep isl_set
*set
)
10571 for (i
= 0; i
< set
->n
; ++i
)
10572 size
+= isl_basic_set_size(set
->p
[i
]);
10577 /* Check if there is any lower bound (if lower == 0) and/or upper
10578 * bound (if upper == 0) on the specified dim.
10580 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10581 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10585 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10586 return isl_bool_error
;
10588 pos
+= isl_basic_map_offset(bmap
, type
);
10590 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10591 if (isl_int_is_zero(bmap
->div
[i
][0]))
10593 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10594 return isl_bool_true
;
10597 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10598 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10599 return isl_bool_true
;
10601 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10602 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10609 return lower
&& upper
;
10612 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10613 enum isl_dim_type type
, unsigned pos
)
10615 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10618 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10619 enum isl_dim_type type
, unsigned pos
)
10621 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10624 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10625 enum isl_dim_type type
, unsigned pos
)
10627 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10630 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10631 enum isl_dim_type type
, unsigned pos
)
10636 return isl_bool_error
;
10638 for (i
= 0; i
< map
->n
; ++i
) {
10640 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10641 if (bounded
< 0 || !bounded
)
10645 return isl_bool_true
;
10648 /* Return true if the specified dim is involved in both an upper bound
10649 * and a lower bound.
10651 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10652 enum isl_dim_type type
, unsigned pos
)
10654 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10657 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10659 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10660 enum isl_dim_type type
, unsigned pos
,
10661 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10662 enum isl_dim_type type
, unsigned pos
))
10667 return isl_bool_error
;
10669 for (i
= 0; i
< map
->n
; ++i
) {
10671 bounded
= fn(map
->p
[i
], type
, pos
);
10672 if (bounded
< 0 || bounded
)
10676 return isl_bool_false
;
10679 /* Return 1 if the specified dim is involved in any lower bound.
10681 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10682 enum isl_dim_type type
, unsigned pos
)
10684 return has_any_bound(set
, type
, pos
,
10685 &isl_basic_map_dim_has_lower_bound
);
10688 /* Return 1 if the specified dim is involved in any upper bound.
10690 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10691 enum isl_dim_type type
, unsigned pos
)
10693 return has_any_bound(set
, type
, pos
,
10694 &isl_basic_map_dim_has_upper_bound
);
10697 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10699 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10700 enum isl_dim_type type
, unsigned pos
,
10701 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10702 enum isl_dim_type type
, unsigned pos
))
10707 return isl_bool_error
;
10709 for (i
= 0; i
< map
->n
; ++i
) {
10711 bounded
= fn(map
->p
[i
], type
, pos
);
10712 if (bounded
< 0 || !bounded
)
10716 return isl_bool_true
;
10719 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10721 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10722 enum isl_dim_type type
, unsigned pos
)
10724 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10727 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10729 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10730 enum isl_dim_type type
, unsigned pos
)
10732 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10735 /* For each of the "n" variables starting at "first", determine
10736 * the sign of the variable and put the results in the first "n"
10737 * elements of the array "signs".
10739 * 1 means that the variable is non-negative
10740 * -1 means that the variable is non-positive
10741 * 0 means the variable attains both positive and negative values.
10743 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10744 unsigned first
, unsigned n
, int *signs
)
10746 isl_vec
*bound
= NULL
;
10747 struct isl_tab
*tab
= NULL
;
10748 struct isl_tab_undo
*snap
;
10751 if (!bset
|| !signs
)
10752 return isl_stat_error
;
10754 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10755 tab
= isl_tab_from_basic_set(bset
, 0);
10756 if (!bound
|| !tab
)
10759 isl_seq_clr(bound
->el
, bound
->size
);
10760 isl_int_set_si(bound
->el
[0], -1);
10762 snap
= isl_tab_snap(tab
);
10763 for (i
= 0; i
< n
; ++i
) {
10766 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10767 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10769 empty
= tab
->empty
;
10770 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10771 if (isl_tab_rollback(tab
, snap
) < 0)
10779 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10780 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10782 empty
= tab
->empty
;
10783 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10784 if (isl_tab_rollback(tab
, snap
) < 0)
10787 signs
[i
] = empty
? -1 : 0;
10791 isl_vec_free(bound
);
10792 return isl_stat_ok
;
10795 isl_vec_free(bound
);
10796 return isl_stat_error
;
10799 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10800 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10802 if (!bset
|| !signs
)
10803 return isl_stat_error
;
10804 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10805 return isl_stat_error
);
10807 first
+= pos(bset
->dim
, type
) - 1;
10808 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10811 /* Is it possible for the integer division "div" to depend (possibly
10812 * indirectly) on any output dimensions?
10814 * If the div is undefined, then we conservatively assume that it
10815 * may depend on them.
10816 * Otherwise, we check if it actually depends on them or on any integer
10817 * divisions that may depend on them.
10819 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10822 unsigned n_out
, o_out
;
10823 unsigned n_div
, o_div
;
10825 if (isl_int_is_zero(bmap
->div
[div
][0]))
10826 return isl_bool_true
;
10828 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10829 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10831 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10832 return isl_bool_true
;
10834 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10835 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10837 for (i
= 0; i
< n_div
; ++i
) {
10838 isl_bool may_involve
;
10840 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10842 may_involve
= div_may_involve_output(bmap
, i
);
10843 if (may_involve
< 0 || may_involve
)
10844 return may_involve
;
10847 return isl_bool_false
;
10850 /* Return the first integer division of "bmap" in the range
10851 * [first, first + n[ that may depend on any output dimensions and
10852 * that has a non-zero coefficient in "c" (where the first coefficient
10853 * in "c" corresponds to integer division "first").
10855 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10856 isl_int
*c
, int first
, int n
)
10863 for (k
= first
; k
< first
+ n
; ++k
) {
10864 isl_bool may_involve
;
10866 if (isl_int_is_zero(c
[k
]))
10868 may_involve
= div_may_involve_output(bmap
, k
);
10869 if (may_involve
< 0)
10878 /* Look for a pair of inequality constraints in "bmap" of the form
10880 * -l + i >= 0 or i >= l
10882 * n + l - i >= 0 or i <= l + n
10884 * with n < "m" and i the output dimension at position "pos".
10885 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10886 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10887 * and earlier output dimensions, as well as integer divisions that do
10888 * not involve any of the output dimensions.
10890 * Return the index of the first inequality constraint or bmap->n_ineq
10891 * if no such pair can be found.
10893 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10894 int pos
, isl_int m
)
10899 unsigned n_div
, o_div
;
10900 unsigned n_out
, o_out
;
10906 ctx
= isl_basic_map_get_ctx(bmap
);
10907 total
= isl_basic_map_total_dim(bmap
);
10908 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10909 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10910 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10911 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10912 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10913 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10915 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10916 n_out
- (pos
+ 1)) != -1)
10918 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10921 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10922 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10925 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10926 bmap
->ineq
[j
] + 1, total
))
10930 if (j
>= bmap
->n_ineq
)
10932 isl_int_add(bmap
->ineq
[i
][0],
10933 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10934 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10935 isl_int_sub(bmap
->ineq
[i
][0],
10936 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10939 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10945 return bmap
->n_ineq
;
10948 /* Return the index of the equality of "bmap" that defines
10949 * the output dimension "pos" in terms of earlier dimensions.
10950 * The equality may also involve integer divisions, as long
10951 * as those integer divisions are defined in terms of
10952 * parameters or input dimensions.
10953 * In this case, *div is set to the number of integer divisions and
10954 * *ineq is set to the number of inequality constraints (provided
10955 * div and ineq are not NULL).
10957 * The equality may also involve a single integer division involving
10958 * the output dimensions (typically only output dimension "pos") as
10959 * long as the coefficient of output dimension "pos" is 1 or -1 and
10960 * there is a pair of constraints i >= l and i <= l + n, with i referring
10961 * to output dimension "pos", l an expression involving only earlier
10962 * dimensions and n smaller than the coefficient of the integer division
10963 * in the equality. In this case, the output dimension can be defined
10964 * in terms of a modulo expression that does not involve the integer division.
10965 * *div is then set to this single integer division and
10966 * *ineq is set to the index of constraint i >= l.
10968 * Return bmap->n_eq if there is no such equality.
10969 * Return -1 on error.
10971 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10972 int pos
, int *div
, int *ineq
)
10975 unsigned n_out
, o_out
;
10976 unsigned n_div
, o_div
;
10981 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10982 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10983 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10984 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10987 *ineq
= bmap
->n_ineq
;
10990 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10991 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10993 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10994 n_out
- (pos
+ 1)) != -1)
10996 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11000 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11001 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11003 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11004 k
+ 1, n_div
- (k
+1)) < n_div
)
11006 l
= find_modulo_constraint_pair(bmap
, pos
,
11007 bmap
->eq
[j
][o_div
+ k
]);
11010 if (l
>= bmap
->n_ineq
)
11022 /* Check if the given basic map is obviously single-valued.
11023 * In particular, for each output dimension, check that there is
11024 * an equality that defines the output dimension in terms of
11025 * earlier dimensions.
11027 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11033 return isl_bool_error
;
11035 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11037 for (i
= 0; i
< n_out
; ++i
) {
11040 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11043 return isl_bool_error
;
11044 if (eq
>= bmap
->n_eq
)
11045 return isl_bool_false
;
11048 return isl_bool_true
;
11051 /* Check if the given basic map is single-valued.
11052 * We simply compute
11056 * and check if the result is a subset of the identity mapping.
11058 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11061 isl_basic_map
*test
;
11065 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11069 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11070 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11072 space
= isl_basic_map_get_space(bmap
);
11073 space
= isl_space_map_from_set(isl_space_range(space
));
11074 id
= isl_basic_map_identity(space
);
11076 sv
= isl_basic_map_is_subset(test
, id
);
11078 isl_basic_map_free(test
);
11079 isl_basic_map_free(id
);
11084 /* Check if the given map is obviously single-valued.
11086 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11089 return isl_bool_error
;
11091 return isl_bool_true
;
11093 return isl_bool_false
;
11095 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11098 /* Check if the given map is single-valued.
11099 * We simply compute
11103 * and check if the result is a subset of the identity mapping.
11105 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11112 sv
= isl_map_plain_is_single_valued(map
);
11116 test
= isl_map_reverse(isl_map_copy(map
));
11117 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11119 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11120 id
= isl_map_identity(dim
);
11122 sv
= isl_map_is_subset(test
, id
);
11124 isl_map_free(test
);
11130 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11134 map
= isl_map_copy(map
);
11135 map
= isl_map_reverse(map
);
11136 in
= isl_map_is_single_valued(map
);
11142 /* Check if the given map is obviously injective.
11144 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11148 map
= isl_map_copy(map
);
11149 map
= isl_map_reverse(map
);
11150 in
= isl_map_plain_is_single_valued(map
);
11156 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11160 sv
= isl_map_is_single_valued(map
);
11164 return isl_map_is_injective(map
);
11167 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11169 return isl_map_is_single_valued(set_to_map(set
));
11172 /* Does "map" only map elements to themselves?
11174 * If the domain and range spaces are different, then "map"
11175 * is considered not to be an identity relation, even if it is empty.
11176 * Otherwise, construct the maximal identity relation and
11177 * check whether "map" is a subset of this relation.
11179 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11183 isl_bool equal
, is_identity
;
11185 space
= isl_map_get_space(map
);
11186 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11187 isl_space_free(space
);
11188 if (equal
< 0 || !equal
)
11191 id
= isl_map_identity(isl_map_get_space(map
));
11192 is_identity
= isl_map_is_subset(map
, id
);
11195 return is_identity
;
11198 int isl_map_is_translation(__isl_keep isl_map
*map
)
11203 delta
= isl_map_deltas(isl_map_copy(map
));
11204 ok
= isl_set_is_singleton(delta
);
11205 isl_set_free(delta
);
11210 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11212 if (isl_seq_first_non_zero(p
, pos
) != -1)
11214 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11219 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11226 return isl_bool_error
;
11228 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11229 return isl_bool_false
;
11231 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11232 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11233 for (j
= 0; j
< nvar
; ++j
) {
11234 int lower
= 0, upper
= 0;
11235 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11236 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11238 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11239 return isl_bool_false
;
11242 if (i
< bset
->n_eq
)
11244 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11245 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11247 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11248 return isl_bool_false
;
11249 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11254 if (!lower
|| !upper
)
11255 return isl_bool_false
;
11258 return isl_bool_true
;
11261 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11264 return isl_bool_error
;
11266 return isl_bool_false
;
11268 return isl_basic_set_is_box(set
->p
[0]);
11271 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11274 return isl_bool_error
;
11276 return isl_space_is_wrapping(bset
->dim
);
11279 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11282 return isl_bool_error
;
11284 return isl_space_is_wrapping(set
->dim
);
11287 /* Modify the space of "map" through a call to "change".
11288 * If "can_change" is set (not NULL), then first call it to check
11289 * if the modification is allowed, printing the error message "cannot_change"
11292 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11293 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11294 const char *cannot_change
,
11295 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11303 ok
= can_change
? can_change(map
) : isl_bool_true
;
11305 return isl_map_free(map
);
11307 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11308 return isl_map_free(map
));
11310 space
= change(isl_map_get_space(map
));
11311 map
= isl_map_reset_space(map
, space
);
11316 /* Is the domain of "map" a wrapped relation?
11318 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11321 return isl_bool_error
;
11323 return isl_space_domain_is_wrapping(map
->dim
);
11326 /* Does "map" have a wrapped relation in both domain and range?
11328 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11330 return isl_space_is_product(isl_map_peek_space(map
));
11333 /* Is the range of "map" a wrapped relation?
11335 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11338 return isl_bool_error
;
11340 return isl_space_range_is_wrapping(map
->dim
);
11343 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11345 bmap
= isl_basic_map_cow(bmap
);
11349 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11353 bmap
= isl_basic_map_finalize(bmap
);
11355 return bset_from_bmap(bmap
);
11357 isl_basic_map_free(bmap
);
11361 /* Given a map A -> B, return the set (A -> B).
11363 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11365 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11368 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11370 bset
= isl_basic_set_cow(bset
);
11374 bset
->dim
= isl_space_unwrap(bset
->dim
);
11378 bset
= isl_basic_set_finalize(bset
);
11380 return bset_to_bmap(bset
);
11382 isl_basic_set_free(bset
);
11386 /* Given a set (A -> B), return the map A -> B.
11387 * Error out if "set" is not of the form (A -> B).
11389 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11391 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11392 "not a wrapping set", &isl_space_unwrap
);
11395 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11396 enum isl_dim_type type
)
11401 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11404 bmap
= isl_basic_map_cow(bmap
);
11408 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11412 bmap
= isl_basic_map_finalize(bmap
);
11416 isl_basic_map_free(bmap
);
11420 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11421 enum isl_dim_type type
)
11428 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11431 map
= isl_map_cow(map
);
11435 for (i
= 0; i
< map
->n
; ++i
) {
11436 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11440 map
->dim
= isl_space_reset(map
->dim
, type
);
11450 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11455 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11458 bmap
= isl_basic_map_cow(bmap
);
11462 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11466 bmap
= isl_basic_map_finalize(bmap
);
11470 isl_basic_map_free(bmap
);
11474 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11476 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11479 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11480 __isl_take isl_basic_map
*bmap
)
11485 if (!bmap
->dim
->nested
[0])
11488 bmap
= isl_basic_map_cow(bmap
);
11492 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11496 bmap
= isl_basic_map_finalize(bmap
);
11500 isl_basic_map_free(bmap
);
11504 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11505 __isl_take isl_basic_map
*bmap
)
11510 if (!bmap
->dim
->nested
[1])
11513 bmap
= isl_basic_map_cow(bmap
);
11517 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11521 bmap
= isl_basic_map_finalize(bmap
);
11525 isl_basic_map_free(bmap
);
11529 /* Remove any internal structure from the spaces of domain and range of "map".
11531 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11536 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11539 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11542 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11544 return set_from_map(isl_map_flatten(set_to_map(set
)));
11547 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11549 isl_space
*space
, *flat_space
;
11552 space
= isl_set_get_space(set
);
11553 flat_space
= isl_space_flatten(isl_space_copy(space
));
11554 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11556 map
= isl_map_intersect_domain(map
, set
);
11561 /* Remove any internal structure from the space of the domain of "map".
11563 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11568 if (!map
->dim
->nested
[0])
11571 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11574 /* Remove any internal structure from the space of the range of "map".
11576 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11581 if (!map
->dim
->nested
[1])
11584 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11587 /* Reorder the dimensions of "bmap" according to the given dim_map
11588 * and set the dimension specification to "space" and
11589 * perform Gaussian elimination on the result.
11591 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11592 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11594 isl_basic_map
*res
;
11598 if (!bmap
|| !space
|| !dim_map
)
11601 flags
= bmap
->flags
;
11602 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11603 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11604 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11605 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11606 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11607 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11609 res
->flags
= flags
;
11610 res
= isl_basic_map_gauss(res
, NULL
);
11611 res
= isl_basic_map_finalize(res
);
11615 isl_basic_map_free(bmap
);
11616 isl_space_free(space
);
11620 /* Reorder the dimensions of "map" according to given reordering.
11622 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11623 __isl_take isl_reordering
*r
)
11626 struct isl_dim_map
*dim_map
;
11628 map
= isl_map_cow(map
);
11629 dim_map
= isl_dim_map_from_reordering(r
);
11630 if (!map
|| !r
|| !dim_map
)
11633 for (i
= 0; i
< map
->n
; ++i
) {
11634 struct isl_dim_map
*dim_map_i
;
11637 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11639 space
= isl_reordering_get_space(r
);
11640 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11646 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11647 map
= isl_map_unmark_normalized(map
);
11649 isl_reordering_free(r
);
11655 isl_reordering_free(r
);
11659 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11660 __isl_take isl_reordering
*r
)
11662 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11665 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11666 __isl_take isl_space
*model
)
11671 if (!map
|| !model
)
11674 ctx
= isl_space_get_ctx(model
);
11675 if (!isl_space_has_named_params(model
))
11676 isl_die(ctx
, isl_error_invalid
,
11677 "model has unnamed parameters", goto error
);
11678 if (isl_map_check_named_params(map
) < 0)
11680 aligned
= isl_map_space_has_equal_params(map
, model
);
11684 isl_reordering
*exp
;
11686 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11687 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11688 map
= isl_map_realign(map
, exp
);
11691 isl_space_free(model
);
11694 isl_space_free(model
);
11699 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11700 __isl_take isl_space
*model
)
11702 return isl_map_align_params(set
, model
);
11705 /* Align the parameters of "bmap" to those of "model", introducing
11706 * additional parameters if needed.
11708 __isl_give isl_basic_map
*isl_basic_map_align_params(
11709 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11712 isl_bool equal_params
;
11714 if (!bmap
|| !model
)
11717 ctx
= isl_space_get_ctx(model
);
11718 if (!isl_space_has_named_params(model
))
11719 isl_die(ctx
, isl_error_invalid
,
11720 "model has unnamed parameters", goto error
);
11721 if (isl_basic_map_check_named_params(bmap
) < 0)
11723 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11724 if (equal_params
< 0)
11726 if (!equal_params
) {
11727 isl_reordering
*exp
;
11728 struct isl_dim_map
*dim_map
;
11730 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11731 exp
= isl_reordering_extend_space(exp
,
11732 isl_basic_map_get_space(bmap
));
11733 dim_map
= isl_dim_map_from_reordering(exp
);
11734 bmap
= isl_basic_map_realign(bmap
,
11735 isl_reordering_get_space(exp
),
11736 isl_dim_map_extend(dim_map
, bmap
));
11737 isl_reordering_free(exp
);
11741 isl_space_free(model
);
11744 isl_space_free(model
);
11745 isl_basic_map_free(bmap
);
11749 /* Do "bset" and "space" have the same parameters?
11751 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11752 __isl_keep isl_space
*space
)
11754 isl_space
*bset_space
;
11756 bset_space
= isl_basic_set_peek_space(bset
);
11757 return isl_space_has_equal_params(bset_space
, space
);
11760 /* Do "map" and "space" have the same parameters?
11762 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11763 __isl_keep isl_space
*space
)
11765 isl_space
*map_space
;
11767 map_space
= isl_map_peek_space(map
);
11768 return isl_space_has_equal_params(map_space
, space
);
11771 /* Do "set" and "space" have the same parameters?
11773 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11774 __isl_keep isl_space
*space
)
11776 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11779 /* Align the parameters of "bset" to those of "model", introducing
11780 * additional parameters if needed.
11782 __isl_give isl_basic_set
*isl_basic_set_align_params(
11783 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11785 return isl_basic_map_align_params(bset
, model
);
11788 /* Drop all parameters not referenced by "map".
11790 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11794 if (isl_map_check_named_params(map
) < 0)
11795 return isl_map_free(map
);
11797 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11800 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11802 return isl_map_free(map
);
11804 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11810 /* Drop all parameters not referenced by "set".
11812 __isl_give isl_set
*isl_set_drop_unused_params(
11813 __isl_take isl_set
*set
)
11815 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11818 /* Drop all parameters not referenced by "bmap".
11820 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11821 __isl_take isl_basic_map
*bmap
)
11825 if (isl_basic_map_check_named_params(bmap
) < 0)
11826 return isl_basic_map_free(bmap
);
11828 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11831 involves
= isl_basic_map_involves_dims(bmap
,
11832 isl_dim_param
, i
, 1);
11834 return isl_basic_map_free(bmap
);
11836 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11842 /* Drop all parameters not referenced by "bset".
11844 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11845 __isl_take isl_basic_set
*bset
)
11847 return bset_from_bmap(isl_basic_map_drop_unused_params(
11848 bset_to_bmap(bset
)));
11851 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11852 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11853 enum isl_dim_type c2
, enum isl_dim_type c3
,
11854 enum isl_dim_type c4
, enum isl_dim_type c5
)
11856 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11857 struct isl_mat
*mat
;
11863 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11864 isl_basic_map_total_dim(bmap
) + 1);
11867 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11868 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11869 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11870 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11871 isl_int_set(mat
->row
[i
][pos
],
11872 bmap
->eq
[i
][off
+ k
]);
11880 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11881 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11882 enum isl_dim_type c2
, enum isl_dim_type c3
,
11883 enum isl_dim_type c4
, enum isl_dim_type c5
)
11885 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11886 struct isl_mat
*mat
;
11892 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11893 isl_basic_map_total_dim(bmap
) + 1);
11896 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11897 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11898 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11899 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11900 isl_int_set(mat
->row
[i
][pos
],
11901 bmap
->ineq
[i
][off
+ k
]);
11909 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11910 __isl_take isl_space
*space
,
11911 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11912 enum isl_dim_type c2
, enum isl_dim_type c3
,
11913 enum isl_dim_type c4
, enum isl_dim_type c5
)
11915 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11916 isl_basic_map
*bmap
= NULL
;
11922 if (!space
|| !eq
|| !ineq
)
11925 if (eq
->n_col
!= ineq
->n_col
)
11926 isl_die(space
->ctx
, isl_error_invalid
,
11927 "equalities and inequalities matrices should have "
11928 "same number of columns", goto error
);
11930 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11932 if (eq
->n_col
< total
)
11933 isl_die(space
->ctx
, isl_error_invalid
,
11934 "number of columns too small", goto error
);
11936 extra
= eq
->n_col
- total
;
11938 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11939 eq
->n_row
, ineq
->n_row
);
11942 for (i
= 0; i
< extra
; ++i
) {
11943 k
= isl_basic_map_alloc_div(bmap
);
11946 isl_int_set_si(bmap
->div
[k
][0], 0);
11948 for (i
= 0; i
< eq
->n_row
; ++i
) {
11949 l
= isl_basic_map_alloc_equality(bmap
);
11952 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11953 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11954 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11955 isl_int_set(bmap
->eq
[l
][off
+ k
],
11961 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11962 l
= isl_basic_map_alloc_inequality(bmap
);
11965 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11966 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11967 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11968 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11969 ineq
->row
[i
][pos
]);
11975 isl_space_free(space
);
11977 isl_mat_free(ineq
);
11979 bmap
= isl_basic_map_simplify(bmap
);
11980 return isl_basic_map_finalize(bmap
);
11982 isl_space_free(space
);
11984 isl_mat_free(ineq
);
11985 isl_basic_map_free(bmap
);
11989 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11990 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11991 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11993 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11994 c1
, c2
, c3
, c4
, isl_dim_in
);
11997 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11998 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11999 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12001 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12002 c1
, c2
, c3
, c4
, isl_dim_in
);
12005 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12006 __isl_take isl_space
*dim
,
12007 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12008 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12010 isl_basic_map
*bmap
;
12011 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12012 c1
, c2
, c3
, c4
, isl_dim_in
);
12013 return bset_from_bmap(bmap
);
12016 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12019 return isl_bool_error
;
12021 return isl_space_can_zip(bmap
->dim
);
12024 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12027 return isl_bool_error
;
12029 return isl_space_can_zip(map
->dim
);
12032 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12033 * (A -> C) -> (B -> D).
12035 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12044 if (!isl_basic_map_can_zip(bmap
))
12045 isl_die(bmap
->ctx
, isl_error_invalid
,
12046 "basic map cannot be zipped", goto error
);
12047 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12048 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12049 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12050 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12051 bmap
= isl_basic_map_cow(bmap
);
12052 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12055 bmap
->dim
= isl_space_zip(bmap
->dim
);
12058 bmap
= isl_basic_map_mark_final(bmap
);
12061 isl_basic_map_free(bmap
);
12065 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12066 * (A -> C) -> (B -> D).
12068 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12075 if (!isl_map_can_zip(map
))
12076 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12079 map
= isl_map_cow(map
);
12083 for (i
= 0; i
< map
->n
; ++i
) {
12084 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12089 map
->dim
= isl_space_zip(map
->dim
);
12099 /* Can we apply isl_basic_map_curry to "bmap"?
12100 * That is, does it have a nested relation in its domain?
12102 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12105 return isl_bool_error
;
12107 return isl_space_can_curry(bmap
->dim
);
12110 /* Can we apply isl_map_curry to "map"?
12111 * That is, does it have a nested relation in its domain?
12113 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12116 return isl_bool_error
;
12118 return isl_space_can_curry(map
->dim
);
12121 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12124 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12130 if (!isl_basic_map_can_curry(bmap
))
12131 isl_die(bmap
->ctx
, isl_error_invalid
,
12132 "basic map cannot be curried", goto error
);
12133 bmap
= isl_basic_map_cow(bmap
);
12136 bmap
->dim
= isl_space_curry(bmap
->dim
);
12139 bmap
= isl_basic_map_mark_final(bmap
);
12142 isl_basic_map_free(bmap
);
12146 /* Given a map (A -> B) -> C, return the corresponding map
12149 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12151 return isl_map_change_space(map
, &isl_map_can_curry
,
12152 "map cannot be curried", &isl_space_curry
);
12155 /* Can isl_map_range_curry be applied to "map"?
12156 * That is, does it have a nested relation in its range,
12157 * the domain of which is itself a nested relation?
12159 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12162 return isl_bool_error
;
12164 return isl_space_can_range_curry(map
->dim
);
12167 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12168 * A -> (B -> (C -> D)).
12170 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12172 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12173 "map range cannot be curried",
12174 &isl_space_range_curry
);
12177 /* Can we apply isl_basic_map_uncurry to "bmap"?
12178 * That is, does it have a nested relation in its domain?
12180 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12183 return isl_bool_error
;
12185 return isl_space_can_uncurry(bmap
->dim
);
12188 /* Can we apply isl_map_uncurry to "map"?
12189 * That is, does it have a nested relation in its domain?
12191 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12194 return isl_bool_error
;
12196 return isl_space_can_uncurry(map
->dim
);
12199 /* Given a basic map A -> (B -> C), return the corresponding basic map
12202 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12208 if (!isl_basic_map_can_uncurry(bmap
))
12209 isl_die(bmap
->ctx
, isl_error_invalid
,
12210 "basic map cannot be uncurried",
12211 return isl_basic_map_free(bmap
));
12212 bmap
= isl_basic_map_cow(bmap
);
12215 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12217 return isl_basic_map_free(bmap
);
12218 bmap
= isl_basic_map_mark_final(bmap
);
12222 /* Given a map A -> (B -> C), return the corresponding map
12225 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12227 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12228 "map cannot be uncurried", &isl_space_uncurry
);
12231 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12232 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12234 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12237 /* Construct a basic map where the given dimensions are equal to each other.
12239 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12240 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12242 isl_basic_map
*bmap
= NULL
;
12248 if (pos1
>= isl_space_dim(space
, type1
))
12249 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12250 "index out of bounds", goto error
);
12251 if (pos2
>= isl_space_dim(space
, type2
))
12252 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12253 "index out of bounds", goto error
);
12255 if (type1
== type2
&& pos1
== pos2
)
12256 return isl_basic_map_universe(space
);
12258 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12259 i
= isl_basic_map_alloc_equality(bmap
);
12262 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12263 pos1
+= isl_basic_map_offset(bmap
, type1
);
12264 pos2
+= isl_basic_map_offset(bmap
, type2
);
12265 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12266 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12267 bmap
= isl_basic_map_finalize(bmap
);
12268 isl_space_free(space
);
12271 isl_space_free(space
);
12272 isl_basic_map_free(bmap
);
12276 /* Add a constraint imposing that the given two dimensions are equal.
12278 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12279 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12283 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12285 bmap
= isl_basic_map_intersect(bmap
, eq
);
12290 /* Add a constraint imposing that the given two dimensions are equal.
12292 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12293 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12295 isl_basic_map
*bmap
;
12297 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12299 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12304 /* Add a constraint imposing that the given two dimensions have opposite values.
12306 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12307 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12309 isl_basic_map
*bmap
= NULL
;
12312 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12313 return isl_map_free(map
);
12314 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12315 return isl_map_free(map
);
12317 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12318 i
= isl_basic_map_alloc_equality(bmap
);
12321 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12322 pos1
+= isl_basic_map_offset(bmap
, type1
);
12323 pos2
+= isl_basic_map_offset(bmap
, type2
);
12324 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12325 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12326 bmap
= isl_basic_map_finalize(bmap
);
12328 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12332 isl_basic_map_free(bmap
);
12337 /* Construct a constraint imposing that the value of the first dimension is
12338 * greater than or equal to that of the second.
12340 static __isl_give isl_constraint
*constraint_order_ge(
12341 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12342 enum isl_dim_type type2
, int pos2
)
12349 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12351 if (pos1
>= isl_constraint_dim(c
, type1
))
12352 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12353 "index out of bounds", return isl_constraint_free(c
));
12354 if (pos2
>= isl_constraint_dim(c
, type2
))
12355 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12356 "index out of bounds", return isl_constraint_free(c
));
12358 if (type1
== type2
&& pos1
== pos2
)
12361 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12362 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12367 /* Add a constraint imposing that the value of the first dimension is
12368 * greater than or equal to that of the second.
12370 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12371 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12376 if (type1
== type2
&& pos1
== pos2
)
12378 space
= isl_basic_map_get_space(bmap
);
12379 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12380 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12385 /* Add a constraint imposing that the value of the first dimension is
12386 * greater than or equal to that of the second.
12388 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12389 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12394 if (type1
== type2
&& pos1
== pos2
)
12396 space
= isl_map_get_space(map
);
12397 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12398 map
= isl_map_add_constraint(map
, c
);
12403 /* Add a constraint imposing that the value of the first dimension is
12404 * less than or equal to that of the second.
12406 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12407 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12409 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12412 /* Construct a basic map where the value of the first dimension is
12413 * greater than that of the second.
12415 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12416 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12418 isl_basic_map
*bmap
= NULL
;
12424 if (pos1
>= isl_space_dim(space
, type1
))
12425 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12426 "index out of bounds", goto error
);
12427 if (pos2
>= isl_space_dim(space
, type2
))
12428 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12429 "index out of bounds", goto error
);
12431 if (type1
== type2
&& pos1
== pos2
)
12432 return isl_basic_map_empty(space
);
12434 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12435 i
= isl_basic_map_alloc_inequality(bmap
);
12437 return isl_basic_map_free(bmap
);
12438 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12439 pos1
+= isl_basic_map_offset(bmap
, type1
);
12440 pos2
+= isl_basic_map_offset(bmap
, type2
);
12441 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12442 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12443 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12444 bmap
= isl_basic_map_finalize(bmap
);
12448 isl_space_free(space
);
12449 isl_basic_map_free(bmap
);
12453 /* Add a constraint imposing that the value of the first dimension is
12454 * greater than that of the second.
12456 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12457 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12461 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12463 bmap
= isl_basic_map_intersect(bmap
, gt
);
12468 /* Add a constraint imposing that the value of the first dimension is
12469 * greater than that of the second.
12471 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12472 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12474 isl_basic_map
*bmap
;
12476 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12478 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12483 /* Add a constraint imposing that the value of the first dimension is
12484 * smaller than that of the second.
12486 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12487 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12489 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12492 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12496 isl_local_space
*ls
;
12501 if (!isl_basic_map_divs_known(bmap
))
12502 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12503 "some divs are unknown", return NULL
);
12505 ls
= isl_basic_map_get_local_space(bmap
);
12506 div
= isl_local_space_get_div(ls
, pos
);
12507 isl_local_space_free(ls
);
12512 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12515 return isl_basic_map_get_div(bset
, pos
);
12518 /* Plug in "subs" for dimension "type", "pos" of "bset".
12520 * Let i be the dimension to replace and let "subs" be of the form
12524 * Any integer division with a non-zero coefficient for i,
12526 * floor((a i + g)/m)
12530 * floor((a f + d g)/(m d))
12532 * Constraints of the form
12540 * We currently require that "subs" is an integral expression.
12541 * Handling rational expressions may require us to add stride constraints
12542 * as we do in isl_basic_set_preimage_multi_aff.
12544 __isl_give isl_basic_set
*isl_basic_set_substitute(
12545 __isl_take isl_basic_set
*bset
,
12546 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12552 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12555 bset
= isl_basic_set_cow(bset
);
12556 if (!bset
|| !subs
)
12559 ctx
= isl_basic_set_get_ctx(bset
);
12560 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12561 isl_die(ctx
, isl_error_invalid
,
12562 "spaces don't match", goto error
);
12563 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12564 isl_die(ctx
, isl_error_unsupported
,
12565 "cannot handle divs yet", goto error
);
12566 if (!isl_int_is_one(subs
->v
->el
[0]))
12567 isl_die(ctx
, isl_error_invalid
,
12568 "can only substitute integer expressions", goto error
);
12570 pos
+= isl_basic_set_offset(bset
, type
);
12574 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12575 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12577 isl_int_set(v
, bset
->eq
[i
][pos
]);
12578 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12579 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12580 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12583 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12584 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12586 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12587 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12588 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12589 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12592 for (i
= 0; i
< bset
->n_div
; ++i
) {
12593 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12595 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12596 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12597 isl_seq_combine(bset
->div
[i
] + 1,
12598 subs
->v
->el
[0], bset
->div
[i
] + 1,
12599 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12600 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12605 bset
= isl_basic_set_simplify(bset
);
12606 return isl_basic_set_finalize(bset
);
12608 isl_basic_set_free(bset
);
12612 /* Plug in "subs" for dimension "type", "pos" of "set".
12614 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12615 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12619 if (set
&& isl_set_plain_is_empty(set
))
12622 set
= isl_set_cow(set
);
12626 for (i
= set
->n
- 1; i
>= 0; --i
) {
12627 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12628 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12639 /* Check if the range of "ma" is compatible with the domain or range
12640 * (depending on "type") of "bmap".
12642 static isl_stat
check_basic_map_compatible_range_multi_aff(
12643 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12644 __isl_keep isl_multi_aff
*ma
)
12647 isl_space
*ma_space
;
12649 ma_space
= isl_multi_aff_get_space(ma
);
12651 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12655 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12656 "parameters don't match", goto error
);
12657 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12661 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12662 "spaces don't match", goto error
);
12664 isl_space_free(ma_space
);
12665 return isl_stat_ok
;
12667 isl_space_free(ma_space
);
12668 return isl_stat_error
;
12671 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12672 * coefficients before the transformed range of dimensions,
12673 * the "n_after" coefficients after the transformed range of dimensions
12674 * and the coefficients of the other divs in "bmap".
12676 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12677 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12682 isl_local_space
*ls
;
12687 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12691 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12692 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12693 for (i
= 0; i
< n_div
; ++i
) {
12694 int o_bmap
= 0, o_ls
= 0;
12696 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12697 o_bmap
+= 1 + 1 + n_param
;
12698 o_ls
+= 1 + 1 + n_param
;
12699 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12700 o_bmap
+= n_before
;
12701 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12702 ls
->div
->row
[i
] + o_ls
, n_set
);
12705 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12707 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12708 ls
->div
->row
[i
] + o_ls
, n_div
);
12711 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12712 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12716 isl_local_space_free(ls
);
12719 isl_local_space_free(ls
);
12723 /* How many stride constraints does "ma" enforce?
12724 * That is, how many of the affine expressions have a denominator
12725 * different from one?
12727 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12732 for (i
= 0; i
< ma
->n
; ++i
)
12733 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12739 /* For each affine expression in ma of the form
12741 * x_i = (f_i y + h_i)/m_i
12743 * with m_i different from one, add a constraint to "bmap"
12746 * f_i y + h_i = m_i alpha_i
12748 * with alpha_i an additional existentially quantified variable.
12750 * The input variables of "ma" correspond to a subset of the variables
12751 * of "bmap". There are "n_before" variables in "bmap" before this
12752 * subset and "n_after" variables after this subset.
12753 * The integer divisions of the affine expressions in "ma" are assumed
12754 * to have been aligned. There are "n_div_ma" of them and
12755 * they appear first in "bmap", straight after the "n_after" variables.
12757 static __isl_give isl_basic_map
*add_ma_strides(
12758 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12759 int n_before
, int n_after
, int n_div_ma
)
12767 total
= isl_basic_map_total_dim(bmap
);
12768 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12769 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12770 for (i
= 0; i
< ma
->n
; ++i
) {
12771 int o_bmap
= 0, o_ma
= 1;
12773 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12775 div
= isl_basic_map_alloc_div(bmap
);
12776 k
= isl_basic_map_alloc_equality(bmap
);
12777 if (div
< 0 || k
< 0)
12779 isl_int_set_si(bmap
->div
[div
][0], 0);
12780 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12781 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12782 o_bmap
+= 1 + n_param
;
12783 o_ma
+= 1 + n_param
;
12784 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12785 o_bmap
+= n_before
;
12786 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12787 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12790 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12792 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12793 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12794 o_bmap
+= n_div_ma
;
12796 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12797 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12803 isl_basic_map_free(bmap
);
12807 /* Replace the domain or range space (depending on "type) of "space" by "set".
12809 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12810 enum isl_dim_type type
, __isl_take isl_space
*set
)
12812 if (type
== isl_dim_in
) {
12813 space
= isl_space_range(space
);
12814 space
= isl_space_map_from_domain_and_range(set
, space
);
12816 space
= isl_space_domain(space
);
12817 space
= isl_space_map_from_domain_and_range(space
, set
);
12823 /* Compute the preimage of the domain or range (depending on "type")
12824 * of "bmap" under the function represented by "ma".
12825 * In other words, plug in "ma" in the domain or range of "bmap".
12826 * The result is a basic map that lives in the same space as "bmap"
12827 * except that the domain or range has been replaced by
12828 * the domain space of "ma".
12830 * If bmap is represented by
12832 * A(p) + S u + B x + T v + C(divs) >= 0,
12834 * where u and x are input and output dimensions if type == isl_dim_out
12835 * while x and v are input and output dimensions if type == isl_dim_in,
12836 * and ma is represented by
12838 * x = D(p) + F(y) + G(divs')
12840 * then the result is
12842 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12844 * The divs in the input set are similarly adjusted.
12847 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12851 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12852 * B_i G(divs') + c_i(divs))/n_i)
12854 * If bmap is not a rational map and if F(y) involves any denominators
12856 * x_i = (f_i y + h_i)/m_i
12858 * then additional constraints are added to ensure that we only
12859 * map back integer points. That is we enforce
12861 * f_i y + h_i = m_i alpha_i
12863 * with alpha_i an additional existentially quantified variable.
12865 * We first copy over the divs from "ma".
12866 * Then we add the modified constraints and divs from "bmap".
12867 * Finally, we add the stride constraints, if needed.
12869 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12870 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12871 __isl_take isl_multi_aff
*ma
)
12875 isl_basic_map
*res
= NULL
;
12876 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12877 isl_int f
, c1
, c2
, g
;
12886 ma
= isl_multi_aff_align_divs(ma
);
12889 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12892 if (type
== isl_dim_in
) {
12894 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12896 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12899 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12900 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12902 space
= isl_multi_aff_get_domain_space(ma
);
12903 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12904 rational
= isl_basic_map_is_rational(bmap
);
12905 strides
= rational
? 0 : multi_aff_strides(ma
);
12906 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12907 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12909 res
= isl_basic_map_set_rational(res
);
12911 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12912 if (isl_basic_map_alloc_div(res
) < 0)
12915 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12918 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12919 k
= isl_basic_map_alloc_equality(res
);
12922 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12923 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12926 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12927 k
= isl_basic_map_alloc_inequality(res
);
12930 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12931 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12934 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12935 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12936 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12939 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12940 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12945 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12951 isl_basic_map_free(bmap
);
12952 isl_multi_aff_free(ma
);
12953 res
= isl_basic_map_simplify(res
);
12954 return isl_basic_map_finalize(res
);
12960 isl_basic_map_free(bmap
);
12961 isl_multi_aff_free(ma
);
12962 isl_basic_map_free(res
);
12966 /* Compute the preimage of "bset" under the function represented by "ma".
12967 * In other words, plug in "ma" in "bset". The result is a basic set
12968 * that lives in the domain space of "ma".
12970 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12971 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12973 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12976 /* Compute the preimage of the domain of "bmap" under the function
12977 * represented by "ma".
12978 * In other words, plug in "ma" in the domain of "bmap".
12979 * The result is a basic map that lives in the same space as "bmap"
12980 * except that the domain has been replaced by the domain space of "ma".
12982 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12983 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12985 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12988 /* Compute the preimage of the range of "bmap" under the function
12989 * represented by "ma".
12990 * In other words, plug in "ma" in the range of "bmap".
12991 * The result is a basic map that lives in the same space as "bmap"
12992 * except that the range has been replaced by the domain space of "ma".
12994 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12995 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12997 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13000 /* Check if the range of "ma" is compatible with the domain or range
13001 * (depending on "type") of "map".
13002 * Return isl_stat_error if anything is wrong.
13004 static isl_stat
check_map_compatible_range_multi_aff(
13005 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13006 __isl_keep isl_multi_aff
*ma
)
13009 isl_space
*ma_space
;
13011 ma_space
= isl_multi_aff_get_space(ma
);
13012 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13013 isl_space_free(ma_space
);
13015 return isl_stat_error
;
13017 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13018 "spaces don't match", return isl_stat_error
);
13019 return isl_stat_ok
;
13022 /* Compute the preimage of the domain or range (depending on "type")
13023 * of "map" under the function represented by "ma".
13024 * In other words, plug in "ma" in the domain or range of "map".
13025 * The result is a map that lives in the same space as "map"
13026 * except that the domain or range has been replaced by
13027 * the domain space of "ma".
13029 * The parameters are assumed to have been aligned.
13031 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13032 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13037 map
= isl_map_cow(map
);
13038 ma
= isl_multi_aff_align_divs(ma
);
13041 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13044 for (i
= 0; i
< map
->n
; ++i
) {
13045 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13046 isl_multi_aff_copy(ma
));
13051 space
= isl_multi_aff_get_domain_space(ma
);
13052 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13054 isl_space_free(map
->dim
);
13059 isl_multi_aff_free(ma
);
13061 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13062 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13065 isl_multi_aff_free(ma
);
13070 /* Compute the preimage of the domain or range (depending on "type")
13071 * of "map" under the function represented by "ma".
13072 * In other words, plug in "ma" in the domain or range of "map".
13073 * The result is a map that lives in the same space as "map"
13074 * except that the domain or range has been replaced by
13075 * the domain space of "ma".
13077 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13078 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13085 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13089 return map_preimage_multi_aff(map
, type
, ma
);
13091 if (isl_map_check_named_params(map
) < 0)
13093 if (!isl_space_has_named_params(ma
->space
))
13094 isl_die(map
->ctx
, isl_error_invalid
,
13095 "unaligned unnamed parameters", goto error
);
13096 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13097 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13099 return map_preimage_multi_aff(map
, type
, ma
);
13101 isl_multi_aff_free(ma
);
13102 return isl_map_free(map
);
13105 /* Compute the preimage of "set" under the function represented by "ma".
13106 * In other words, plug in "ma" in "set". The result is a set
13107 * that lives in the domain space of "ma".
13109 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13110 __isl_take isl_multi_aff
*ma
)
13112 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13115 /* Compute the preimage of the domain of "map" under the function
13116 * represented by "ma".
13117 * In other words, plug in "ma" in the domain of "map".
13118 * The result is a map that lives in the same space as "map"
13119 * except that the domain has been replaced by the domain space of "ma".
13121 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13122 __isl_take isl_multi_aff
*ma
)
13124 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13127 /* Compute the preimage of the range of "map" under the function
13128 * represented by "ma".
13129 * In other words, plug in "ma" in the range of "map".
13130 * The result is a map that lives in the same space as "map"
13131 * except that the range has been replaced by the domain space of "ma".
13133 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13134 __isl_take isl_multi_aff
*ma
)
13136 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13139 /* Compute the preimage of "map" under the function represented by "pma".
13140 * In other words, plug in "pma" in the domain or range of "map".
13141 * The result is a map that lives in the same space as "map",
13142 * except that the space of type "type" has been replaced by
13143 * the domain space of "pma".
13145 * The parameters of "map" and "pma" are assumed to have been aligned.
13147 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13148 __isl_take isl_map
*map
, enum isl_dim_type type
,
13149 __isl_take isl_pw_multi_aff
*pma
)
13158 isl_pw_multi_aff_free(pma
);
13159 res
= isl_map_empty(isl_map_get_space(map
));
13164 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13165 isl_multi_aff_copy(pma
->p
[0].maff
));
13166 if (type
== isl_dim_in
)
13167 res
= isl_map_intersect_domain(res
,
13168 isl_map_copy(pma
->p
[0].set
));
13170 res
= isl_map_intersect_range(res
,
13171 isl_map_copy(pma
->p
[0].set
));
13173 for (i
= 1; i
< pma
->n
; ++i
) {
13176 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13177 isl_multi_aff_copy(pma
->p
[i
].maff
));
13178 if (type
== isl_dim_in
)
13179 res_i
= isl_map_intersect_domain(res_i
,
13180 isl_map_copy(pma
->p
[i
].set
));
13182 res_i
= isl_map_intersect_range(res_i
,
13183 isl_map_copy(pma
->p
[i
].set
));
13184 res
= isl_map_union(res
, res_i
);
13187 isl_pw_multi_aff_free(pma
);
13191 isl_pw_multi_aff_free(pma
);
13196 /* Compute the preimage of "map" under the function represented by "pma".
13197 * In other words, plug in "pma" in the domain or range of "map".
13198 * The result is a map that lives in the same space as "map",
13199 * except that the space of type "type" has been replaced by
13200 * the domain space of "pma".
13202 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13203 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13210 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13214 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13216 if (isl_map_check_named_params(map
) < 0)
13218 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13220 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13221 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13223 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13225 isl_pw_multi_aff_free(pma
);
13226 return isl_map_free(map
);
13229 /* Compute the preimage of "set" under the function represented by "pma".
13230 * In other words, plug in "pma" in "set". The result is a set
13231 * that lives in the domain space of "pma".
13233 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13234 __isl_take isl_pw_multi_aff
*pma
)
13236 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13239 /* Compute the preimage of the domain of "map" under the function
13240 * represented by "pma".
13241 * In other words, plug in "pma" in the domain of "map".
13242 * The result is a map that lives in the same space as "map",
13243 * except that domain space has been replaced by the domain space of "pma".
13245 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13246 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13248 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13251 /* Compute the preimage of the range of "map" under the function
13252 * represented by "pma".
13253 * In other words, plug in "pma" in the range of "map".
13254 * The result is a map that lives in the same space as "map",
13255 * except that range space has been replaced by the domain space of "pma".
13257 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13258 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13260 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13263 /* Compute the preimage of "map" under the function represented by "mpa".
13264 * In other words, plug in "mpa" in the domain or range of "map".
13265 * The result is a map that lives in the same space as "map",
13266 * except that the space of type "type" has been replaced by
13267 * the domain space of "mpa".
13269 * If the map does not involve any constraints that refer to the
13270 * dimensions of the substituted space, then the only possible
13271 * effect of "mpa" on the map is to map the space to a different space.
13272 * We create a separate isl_multi_aff to effectuate this change
13273 * in order to avoid spurious splitting of the map along the pieces
13275 * If "mpa" has a non-trivial explicit domain, however,
13276 * then the full substitution should be performed.
13278 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13279 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13283 isl_pw_multi_aff
*pma
;
13288 n
= isl_map_dim(map
, type
);
13289 full
= isl_map_involves_dims(map
, type
, 0, n
);
13290 if (full
>= 0 && !full
)
13291 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13298 space
= isl_multi_pw_aff_get_space(mpa
);
13299 isl_multi_pw_aff_free(mpa
);
13300 ma
= isl_multi_aff_zero(space
);
13301 return isl_map_preimage_multi_aff(map
, type
, ma
);
13304 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13305 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13308 isl_multi_pw_aff_free(mpa
);
13312 /* Compute the preimage of "map" under the function represented by "mpa".
13313 * In other words, plug in "mpa" in the domain "map".
13314 * The result is a map that lives in the same space as "map",
13315 * except that domain space has been replaced by the domain space of "mpa".
13317 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13318 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13320 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13323 /* Compute the preimage of "set" by the function represented by "mpa".
13324 * In other words, plug in "mpa" in "set".
13326 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13327 __isl_take isl_multi_pw_aff
*mpa
)
13329 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13332 /* Return a copy of the equality constraints of "bset" as a matrix.
13334 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13335 __isl_keep isl_basic_set
*bset
)
13343 ctx
= isl_basic_set_get_ctx(bset
);
13344 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13345 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13348 /* Are the "n" "coefficients" starting at "first" of the integer division
13349 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13351 * The "coefficient" at position 0 is the denominator.
13352 * The "coefficient" at position 1 is the constant term.
13354 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13355 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13356 unsigned first
, unsigned n
)
13358 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13359 return isl_bool_error
;
13360 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13361 return isl_bool_error
;
13362 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13363 bmap2
->div
[pos2
] + first
, n
);
13366 /* Are the integer division expressions at position "pos1" in "bmap1" and
13367 * "pos2" in "bmap2" equal to each other, except that the constant terms
13370 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13371 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13372 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13377 if (!bmap1
|| !bmap2
)
13378 return isl_bool_error
;
13379 total
= isl_basic_map_total_dim(bmap1
);
13380 if (total
!= isl_basic_map_total_dim(bmap2
))
13381 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13382 "incomparable div expressions", return isl_bool_error
);
13383 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13385 if (equal
< 0 || !equal
)
13387 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13389 if (equal
< 0 || equal
)
13390 return isl_bool_not(equal
);
13391 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13395 /* Replace the numerator of the constant term of the integer division
13396 * expression at position "div" in "bmap" by "value".
13397 * The caller guarantees that this does not change the meaning
13400 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13401 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13403 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13404 return isl_basic_map_free(bmap
);
13406 isl_int_set_si(bmap
->div
[div
][1], value
);
13411 /* Is the point "inner" internal to inequality constraint "ineq"
13413 * The point is considered to be internal to the inequality constraint,
13414 * if it strictly lies on the positive side of the inequality constraint,
13415 * or if it lies on the constraint and the constraint is lexico-positive.
13417 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13418 __isl_keep isl_basic_set
*bset
, int ineq
)
13424 if (!inner
|| !bset
)
13425 return isl_bool_error
;
13427 ctx
= isl_basic_set_get_ctx(bset
);
13428 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13429 &ctx
->normalize_gcd
);
13430 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13431 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13433 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13434 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13435 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13438 /* Tighten the inequality constraints of "bset" that are outward with respect
13439 * to the point "vec".
13440 * That is, tighten the constraints that are not satisfied by "vec".
13442 * "vec" is a point internal to some superset S of "bset" that is used
13443 * to make the subsets of S disjoint, by tightening one half of the constraints
13444 * that separate two subsets. In particular, the constraints of S
13445 * are all satisfied by "vec" and should not be tightened.
13446 * Of the internal constraints, those that have "vec" on the outside
13447 * are tightened. The shared facet is included in the adjacent subset
13448 * with the opposite constraint.
13449 * For constraints that saturate "vec", this criterion cannot be used
13450 * to determine which of the two sides should be tightened.
13451 * Instead, the sign of the first non-zero coefficient is used
13452 * to make this choice. Note that this second criterion is never used
13453 * on the constraints of S since "vec" is interior to "S".
13455 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13456 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13460 bset
= isl_basic_set_cow(bset
);
13463 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13466 internal
= is_internal(vec
, bset
, j
);
13468 return isl_basic_set_free(bset
);
13471 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13477 /* Replace the variables x of type "type" starting at "first" in "bmap"
13478 * by x' with x = M x' with M the matrix trans.
13479 * That is, replace the corresponding coefficients c by c M.
13481 * The transformation matrix should be a square matrix.
13483 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13484 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13485 __isl_take isl_mat
*trans
)
13489 bmap
= isl_basic_map_cow(bmap
);
13490 if (!bmap
|| !trans
)
13493 if (trans
->n_row
!= trans
->n_col
)
13494 isl_die(trans
->ctx
, isl_error_invalid
,
13495 "expecting square transformation matrix", goto error
);
13496 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13499 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13501 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13502 isl_mat_copy(trans
)) < 0)
13504 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13505 isl_mat_copy(trans
)) < 0)
13507 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13508 isl_mat_copy(trans
)) < 0)
13511 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13512 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13514 isl_mat_free(trans
);
13517 isl_mat_free(trans
);
13518 isl_basic_map_free(bmap
);
13522 /* Replace the variables x of type "type" starting at "first" in "bset"
13523 * by x' with x = M x' with M the matrix trans.
13524 * That is, replace the corresponding coefficients c by c M.
13526 * The transformation matrix should be a square matrix.
13528 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13529 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13530 __isl_take isl_mat
*trans
)
13532 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);