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 static 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
)
1976 isl_assert(bmap
->ctx
,
1977 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1979 if (n1
== 0 || n2
== 0)
1982 bmap
= isl_basic_map_cow(bmap
);
1986 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1987 if (isl_blk_is_error(blk
))
1990 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1992 bmap
->eq
[i
] + pos
, n1
, n2
);
1994 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1996 bmap
->ineq
[i
] + pos
, n1
, n2
);
1998 for (i
= 0; i
< bmap
->n_div
; ++i
)
2000 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2002 isl_blk_free(bmap
->ctx
, blk
);
2004 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2005 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2006 return isl_basic_map_finalize(bmap
);
2008 isl_basic_map_free(bmap
);
2012 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2013 __isl_take isl_basic_map
*bmap
)
2019 total
= isl_basic_map_total_dim(bmap
);
2020 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2021 return isl_basic_map_free(bmap
);
2022 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2024 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2026 i
= isl_basic_map_alloc_equality(bmap
);
2030 isl_int_set_si(bmap
->eq
[i
][0], 1);
2031 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2032 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2033 isl_vec_free(bmap
->sample
);
2034 bmap
->sample
= NULL
;
2035 return isl_basic_map_finalize(bmap
);
2037 isl_basic_map_free(bmap
);
2041 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2042 __isl_take isl_basic_set
*bset
)
2044 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2047 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2048 __isl_take isl_basic_map
*bmap
)
2053 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2056 bmap
= isl_basic_map_cow(bmap
);
2060 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2062 return isl_basic_map_finalize(bmap
);
2065 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2066 __isl_take isl_basic_set
*bset
)
2068 return isl_basic_map_set_rational(bset
);
2071 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2072 __isl_take isl_basic_set
*bset
)
2077 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2080 bset
= isl_basic_set_cow(bset
);
2084 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2086 return isl_basic_set_finalize(bset
);
2089 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2093 map
= isl_map_cow(map
);
2096 for (i
= 0; i
< map
->n
; ++i
) {
2097 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2107 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2109 return isl_map_set_rational(set
);
2112 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2115 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2117 isl_int
*t
= bmap
->div
[a
];
2118 bmap
->div
[a
] = bmap
->div
[b
];
2122 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2123 * div definitions accordingly.
2125 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2128 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2130 swap_div(bmap
, a
, b
);
2132 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2133 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2135 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2136 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2138 for (i
= 0; i
< bmap
->n_div
; ++i
)
2139 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2140 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2143 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2145 isl_seq_cpy(c
, c
+ n
, rem
);
2146 isl_seq_clr(c
+ rem
, n
);
2149 /* Drop n dimensions starting at first.
2151 * In principle, this frees up some extra variables as the number
2152 * of columns remains constant, but we would have to extend
2153 * the div array too as the number of rows in this array is assumed
2154 * to be equal to extra.
2156 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2157 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2159 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2162 /* Move "n" divs starting at "first" to the end of the list of divs.
2164 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2165 unsigned first
, unsigned n
)
2170 if (first
+ n
== bmap
->n_div
)
2173 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2176 for (i
= 0; i
< n
; ++i
)
2177 div
[i
] = bmap
->div
[first
+ i
];
2178 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2179 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2180 for (i
= 0; i
< n
; ++i
)
2181 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2185 isl_basic_map_free(bmap
);
2189 /* Check that there are "n" dimensions of type "type" starting at "first"
2192 static isl_stat
isl_map_check_range(__isl_keep isl_map
*map
,
2193 enum isl_dim_type type
, unsigned first
, unsigned n
)
2196 return isl_stat_error
;
2197 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2198 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2199 "position or range out of bounds",
2200 return isl_stat_error
);
2204 /* Drop "n" dimensions of type "type" starting at "first".
2206 * In principle, this frees up some extra variables as the number
2207 * of columns remains constant, but we would have to extend
2208 * the div array too as the number of rows in this array is assumed
2209 * to be equal to extra.
2211 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2212 enum isl_dim_type type
, unsigned first
, unsigned n
)
2222 dim
= isl_basic_map_dim(bmap
, type
);
2223 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
2225 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2228 bmap
= isl_basic_map_cow(bmap
);
2232 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2233 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2234 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2235 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2237 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2238 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2240 for (i
= 0; i
< bmap
->n_div
; ++i
)
2241 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2243 if (type
== isl_dim_div
) {
2244 bmap
= move_divs_last(bmap
, first
, n
);
2247 if (isl_basic_map_free_div(bmap
, n
) < 0)
2248 return isl_basic_map_free(bmap
);
2250 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2254 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2256 bmap
= isl_basic_map_simplify(bmap
);
2257 return isl_basic_map_finalize(bmap
);
2259 isl_basic_map_free(bmap
);
2263 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2264 enum isl_dim_type type
, unsigned first
, unsigned n
)
2266 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2270 /* No longer consider "map" to be normalized.
2272 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2276 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2280 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2281 enum isl_dim_type type
, unsigned first
, unsigned n
)
2285 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2286 return isl_map_free(map
);
2288 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2290 map
= isl_map_cow(map
);
2293 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2297 for (i
= 0; i
< map
->n
; ++i
) {
2298 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2302 map
= isl_map_unmark_normalized(map
);
2310 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2311 enum isl_dim_type type
, unsigned first
, unsigned n
)
2313 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2317 * We don't cow, as the div is assumed to be redundant.
2319 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2320 __isl_take isl_basic_map
*bmap
, unsigned div
)
2328 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
2330 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
2332 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2333 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2335 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2336 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
2337 isl_basic_map_drop_inequality(bmap
, i
);
2341 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2344 for (i
= 0; i
< bmap
->n_div
; ++i
)
2345 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
2347 if (div
!= bmap
->n_div
- 1) {
2349 isl_int
*t
= bmap
->div
[div
];
2351 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
2352 bmap
->div
[j
] = bmap
->div
[j
+1];
2354 bmap
->div
[bmap
->n_div
- 1] = t
;
2356 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2357 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2358 if (isl_basic_map_free_div(bmap
, 1) < 0)
2359 return isl_basic_map_free(bmap
);
2363 isl_basic_map_free(bmap
);
2367 /* Eliminate the specified n dimensions starting at first from the
2368 * constraints, without removing the dimensions from the space.
2369 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2371 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2372 enum isl_dim_type type
, unsigned first
, unsigned n
)
2379 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2380 return isl_map_free(map
);
2382 map
= isl_map_cow(map
);
2386 for (i
= 0; i
< map
->n
; ++i
) {
2387 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2397 /* Eliminate the specified n dimensions starting at first from the
2398 * constraints, without removing the dimensions from the space.
2399 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2401 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2402 enum isl_dim_type type
, unsigned first
, unsigned n
)
2404 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2407 /* Eliminate the specified n dimensions starting at first from the
2408 * constraints, without removing the dimensions from the space.
2409 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2411 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2412 unsigned first
, unsigned n
)
2414 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2417 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2418 __isl_take isl_basic_map
*bmap
)
2422 bmap
= isl_basic_map_eliminate_vars(bmap
,
2423 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2427 return isl_basic_map_finalize(bmap
);
2430 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2431 __isl_take isl_basic_set
*bset
)
2433 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2436 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2445 map
= isl_map_cow(map
);
2449 for (i
= 0; i
< map
->n
; ++i
) {
2450 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2460 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2462 return isl_map_remove_divs(set
);
2465 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2466 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2467 unsigned first
, unsigned n
)
2469 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2470 return isl_basic_map_free(bmap
);
2471 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2473 bmap
= isl_basic_map_eliminate_vars(bmap
,
2474 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2477 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2479 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2483 /* Return true if the definition of the given div (recursively) involves
2484 * any of the given variables.
2486 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2487 unsigned first
, unsigned n
)
2490 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2492 if (isl_int_is_zero(bmap
->div
[div
][0]))
2493 return isl_bool_false
;
2494 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2495 return isl_bool_true
;
2497 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2500 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2502 involves
= div_involves_vars(bmap
, i
, first
, n
);
2503 if (involves
< 0 || involves
)
2507 return isl_bool_false
;
2510 /* Try and add a lower and/or upper bound on "div" to "bmap"
2511 * based on inequality "i".
2512 * "total" is the total number of variables (excluding the divs).
2513 * "v" is a temporary object that can be used during the calculations.
2514 * If "lb" is set, then a lower bound should be constructed.
2515 * If "ub" is set, then an upper bound should be constructed.
2517 * The calling function has already checked that the inequality does not
2518 * reference "div", but we still need to check that the inequality is
2519 * of the right form. We'll consider the case where we want to construct
2520 * a lower bound. The construction of upper bounds is similar.
2522 * Let "div" be of the form
2524 * q = floor((a + f(x))/d)
2526 * We essentially check if constraint "i" is of the form
2530 * so that we can use it to derive a lower bound on "div".
2531 * However, we allow a slightly more general form
2535 * with the condition that the coefficients of g(x) - f(x) are all
2537 * Rewriting this constraint as
2541 * adding a + f(x) to both sides and dividing by d, we obtain
2543 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2545 * Taking the floor on both sides, we obtain
2547 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2551 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2553 * In the case of an upper bound, we construct the constraint
2555 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2558 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2559 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2560 unsigned total
, isl_int v
, int lb
, int ub
)
2564 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2566 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2567 bmap
->div
[div
][1 + 1 + j
]);
2568 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2571 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2572 bmap
->div
[div
][1 + 1 + j
]);
2573 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2579 bmap
= isl_basic_map_cow(bmap
);
2580 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2582 int k
= isl_basic_map_alloc_inequality(bmap
);
2585 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2586 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2587 bmap
->div
[div
][1 + j
]);
2588 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2589 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2591 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2594 int k
= isl_basic_map_alloc_inequality(bmap
);
2597 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2598 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2599 bmap
->div
[div
][1 + j
]);
2600 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2601 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2603 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2608 isl_basic_map_free(bmap
);
2612 /* This function is called right before "div" is eliminated from "bmap"
2613 * using Fourier-Motzkin.
2614 * Look through the constraints of "bmap" for constraints on the argument
2615 * of the integer division and use them to construct constraints on the
2616 * integer division itself. These constraints can then be combined
2617 * during the Fourier-Motzkin elimination.
2618 * Note that it is only useful to introduce lower bounds on "div"
2619 * if "bmap" already contains upper bounds on "div" as the newly
2620 * introduce lower bounds can then be combined with the pre-existing
2621 * upper bounds. Similarly for upper bounds.
2622 * We therefore first check if "bmap" contains any lower and/or upper bounds
2625 * It is interesting to note that the introduction of these constraints
2626 * can indeed lead to more accurate results, even when compared to
2627 * deriving constraints on the argument of "div" from constraints on "div".
2628 * Consider, for example, the set
2630 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2632 * The second constraint can be rewritten as
2634 * 2 * [(-i-2j+3)/4] + k >= 0
2636 * from which we can derive
2638 * -i - 2j + 3 >= -2k
2644 * Combined with the first constraint, we obtain
2646 * -3 <= 3 + 2k or k >= -3
2648 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2649 * the first constraint, we obtain
2651 * [(i + 2j)/4] >= [-3/4] = -1
2653 * Combining this constraint with the second constraint, we obtain
2657 static __isl_give isl_basic_map
*insert_bounds_on_div(
2658 __isl_take isl_basic_map
*bmap
, int div
)
2661 int check_lb
, check_ub
;
2668 if (isl_int_is_zero(bmap
->div
[div
][0]))
2671 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2675 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2676 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2683 if (!check_lb
&& !check_ub
)
2688 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2689 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2692 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2693 check_lb
, check_ub
);
2701 /* Remove all divs (recursively) involving any of the given dimensions
2702 * in their definitions.
2704 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2705 __isl_take isl_basic_map
*bmap
,
2706 enum isl_dim_type type
, unsigned first
, unsigned n
)
2710 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2711 return isl_basic_map_free(bmap
);
2712 first
+= isl_basic_map_offset(bmap
, type
);
2714 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2717 involves
= div_involves_vars(bmap
, i
, first
, n
);
2719 return isl_basic_map_free(bmap
);
2722 bmap
= insert_bounds_on_div(bmap
, i
);
2723 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2732 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2733 __isl_take isl_basic_set
*bset
,
2734 enum isl_dim_type type
, unsigned first
, unsigned n
)
2736 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2739 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2740 enum isl_dim_type type
, unsigned first
, unsigned n
)
2749 map
= isl_map_cow(map
);
2753 for (i
= 0; i
< map
->n
; ++i
) {
2754 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2765 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2766 enum isl_dim_type type
, unsigned first
, unsigned n
)
2768 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2772 /* Does the description of "bmap" depend on the specified dimensions?
2773 * We also check whether the dimensions appear in any of the div definitions.
2774 * In principle there is no need for this check. If the dimensions appear
2775 * in a div definition, they also appear in the defining constraints of that
2778 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2779 enum isl_dim_type type
, unsigned first
, unsigned n
)
2783 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2784 return isl_bool_error
;
2786 first
+= isl_basic_map_offset(bmap
, type
);
2787 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2788 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2789 return isl_bool_true
;
2790 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2791 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2792 return isl_bool_true
;
2793 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2794 if (isl_int_is_zero(bmap
->div
[i
][0]))
2796 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2797 return isl_bool_true
;
2800 return isl_bool_false
;
2803 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2804 enum isl_dim_type type
, unsigned first
, unsigned n
)
2808 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2809 return isl_bool_error
;
2811 for (i
= 0; i
< map
->n
; ++i
) {
2812 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2814 if (involves
< 0 || involves
)
2818 return isl_bool_false
;
2821 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2822 enum isl_dim_type type
, unsigned first
, unsigned n
)
2824 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2827 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2828 enum isl_dim_type type
, unsigned first
, unsigned n
)
2830 return isl_map_involves_dims(set
, type
, first
, n
);
2833 /* Drop all constraints in bmap that involve any of the dimensions
2834 * first to first+n-1.
2835 * This function only performs the actual removal of constraints.
2837 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2838 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2845 bmap
= isl_basic_map_cow(bmap
);
2850 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2851 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2853 isl_basic_map_drop_equality(bmap
, i
);
2856 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2857 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2859 isl_basic_map_drop_inequality(bmap
, i
);
2865 /* Drop all constraints in bset that involve any of the dimensions
2866 * first to first+n-1.
2867 * This function only performs the actual removal of constraints.
2869 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2870 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2872 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2875 /* Drop all constraints in bmap that do not involve any of the dimensions
2876 * first to first + n - 1 of the given type.
2878 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2879 __isl_take isl_basic_map
*bmap
,
2880 enum isl_dim_type type
, unsigned first
, unsigned n
)
2885 isl_space
*space
= isl_basic_map_get_space(bmap
);
2886 isl_basic_map_free(bmap
);
2887 return isl_basic_map_universe(space
);
2889 bmap
= isl_basic_map_cow(bmap
);
2893 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2894 return isl_basic_map_free(bmap
);
2896 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2898 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2899 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2901 isl_basic_map_drop_equality(bmap
, i
);
2904 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2905 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2907 isl_basic_map_drop_inequality(bmap
, i
);
2910 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2914 /* Drop all constraints in bset that do not involve any of the dimensions
2915 * first to first + n - 1 of the given type.
2917 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2918 __isl_take isl_basic_set
*bset
,
2919 enum isl_dim_type type
, unsigned first
, unsigned n
)
2921 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2925 /* Drop all constraints in bmap that involve any of the dimensions
2926 * first to first + n - 1 of the given type.
2928 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2929 __isl_take isl_basic_map
*bmap
,
2930 enum isl_dim_type type
, unsigned first
, unsigned n
)
2937 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2938 return isl_basic_map_free(bmap
);
2940 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2941 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2942 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2943 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2947 /* Drop all constraints in bset that involve any of the dimensions
2948 * first to first + n - 1 of the given type.
2950 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2951 __isl_take isl_basic_set
*bset
,
2952 enum isl_dim_type type
, unsigned first
, unsigned n
)
2954 return isl_basic_map_drop_constraints_involving_dims(bset
,
2958 /* Drop constraints from "map" by applying "drop" to each basic map.
2960 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2961 enum isl_dim_type type
, unsigned first
, unsigned n
,
2962 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2963 enum isl_dim_type type
, unsigned first
, unsigned n
))
2967 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2968 return isl_map_free(map
);
2970 map
= isl_map_cow(map
);
2974 for (i
= 0; i
< map
->n
; ++i
) {
2975 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2977 return isl_map_free(map
);
2981 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2986 /* Drop all constraints in map that involve any of the dimensions
2987 * first to first + n - 1 of the given type.
2989 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2990 __isl_take isl_map
*map
,
2991 enum isl_dim_type type
, unsigned first
, unsigned n
)
2995 return drop_constraints(map
, type
, first
, n
,
2996 &isl_basic_map_drop_constraints_involving_dims
);
2999 /* Drop all constraints in "map" that do not involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3003 __isl_take isl_map
*map
,
3004 enum isl_dim_type type
, unsigned first
, unsigned n
)
3007 isl_space
*space
= isl_map_get_space(map
);
3009 return isl_map_universe(space
);
3011 return drop_constraints(map
, type
, first
, n
,
3012 &isl_basic_map_drop_constraints_not_involving_dims
);
3015 /* Drop all constraints in set that involve any of the dimensions
3016 * first to first + n - 1 of the given type.
3018 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3019 __isl_take isl_set
*set
,
3020 enum isl_dim_type type
, unsigned first
, unsigned n
)
3022 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3025 /* Drop all constraints in "set" that do not involve any of the dimensions
3026 * first to first + n - 1 of the given type.
3028 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3029 __isl_take isl_set
*set
,
3030 enum isl_dim_type type
, unsigned first
, unsigned n
)
3032 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3035 /* Does local variable "div" of "bmap" have a complete explicit representation?
3036 * Having a complete explicit representation requires not only
3037 * an explicit representation, but also that all local variables
3038 * that appear in this explicit representation in turn have
3039 * a complete explicit representation.
3041 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3044 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3047 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3048 if (marked
< 0 || marked
)
3049 return isl_bool_not(marked
);
3051 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3054 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3056 known
= isl_basic_map_div_is_known(bmap
, i
);
3057 if (known
< 0 || !known
)
3061 return isl_bool_true
;
3064 /* Remove all divs that are unknown or defined in terms of unknown divs.
3066 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3067 __isl_take isl_basic_map
*bmap
)
3074 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3075 if (isl_basic_map_div_is_known(bmap
, i
))
3077 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3086 /* Remove all divs that are unknown or defined in terms of unknown divs.
3088 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3089 __isl_take isl_basic_set
*bset
)
3091 return isl_basic_map_remove_unknown_divs(bset
);
3094 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3103 map
= isl_map_cow(map
);
3107 for (i
= 0; i
< map
->n
; ++i
) {
3108 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3118 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3120 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3123 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3124 __isl_take isl_basic_set
*bset
,
3125 enum isl_dim_type type
, unsigned first
, unsigned n
)
3127 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3128 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3129 return bset_from_bmap(bmap
);
3132 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3133 enum isl_dim_type type
, unsigned first
, unsigned n
)
3140 map
= isl_map_cow(map
);
3141 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3142 return isl_map_free(map
);
3144 for (i
= 0; i
< map
->n
; ++i
) {
3145 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3146 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3150 map
= isl_map_drop(map
, type
, first
, n
);
3157 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3158 enum isl_dim_type type
, unsigned first
, unsigned n
)
3160 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3164 /* Project out n inputs starting at first using Fourier-Motzkin */
3165 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3166 unsigned first
, unsigned n
)
3168 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3171 static void dump_term(struct isl_basic_map
*bmap
,
3172 isl_int c
, int pos
, FILE *out
)
3175 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3176 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3177 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3179 isl_int_print(out
, c
, 0);
3181 if (!isl_int_is_one(c
))
3182 isl_int_print(out
, c
, 0);
3183 if (pos
< 1 + nparam
) {
3184 name
= isl_space_get_dim_name(bmap
->dim
,
3185 isl_dim_param
, pos
- 1);
3187 fprintf(out
, "%s", name
);
3189 fprintf(out
, "p%d", pos
- 1);
3190 } else if (pos
< 1 + nparam
+ in
)
3191 fprintf(out
, "i%d", pos
- 1 - nparam
);
3192 else if (pos
< 1 + nparam
+ dim
)
3193 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3195 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3199 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3200 int sign
, FILE *out
)
3204 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3208 for (i
= 0, first
= 1; i
< len
; ++i
) {
3209 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3212 fprintf(out
, " + ");
3214 isl_int_abs(v
, c
[i
]);
3215 dump_term(bmap
, v
, i
, out
);
3222 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3223 const char *op
, FILE *out
, int indent
)
3227 fprintf(out
, "%*s", indent
, "");
3229 dump_constraint_sign(bmap
, c
, 1, out
);
3230 fprintf(out
, " %s ", op
);
3231 dump_constraint_sign(bmap
, c
, -1, out
);
3235 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3236 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3238 fprintf(out
, "%*s", indent
, "");
3239 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3244 static void dump_constraints(struct isl_basic_map
*bmap
,
3245 isl_int
**c
, unsigned n
,
3246 const char *op
, FILE *out
, int indent
)
3250 for (i
= 0; i
< n
; ++i
)
3251 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3254 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3258 unsigned total
= isl_basic_map_total_dim(bmap
);
3260 for (j
= 0; j
< 1 + total
; ++j
) {
3261 if (isl_int_is_zero(exp
[j
]))
3263 if (!first
&& isl_int_is_pos(exp
[j
]))
3265 dump_term(bmap
, exp
[j
], j
, out
);
3270 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3274 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3275 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3277 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3278 fprintf(out
, "%*s", indent
, "");
3279 fprintf(out
, "e%d = [(", i
);
3280 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3282 isl_int_print(out
, bmap
->div
[i
][0], 0);
3283 fprintf(out
, "]\n");
3287 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3288 FILE *out
, int indent
)
3291 fprintf(out
, "null basic set\n");
3295 fprintf(out
, "%*s", indent
, "");
3296 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3297 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3298 bset
->extra
, bset
->flags
);
3299 dump(bset_to_bmap(bset
), out
, indent
);
3302 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3303 FILE *out
, int indent
)
3306 fprintf(out
, "null basic map\n");
3310 fprintf(out
, "%*s", indent
, "");
3311 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3312 "flags: %x, n_name: %d\n",
3314 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3315 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3316 dump(bmap
, out
, indent
);
3319 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3324 total
= isl_basic_map_total_dim(bmap
);
3325 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3326 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3327 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3328 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3329 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3333 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3336 if (isl_space_check_is_set(space
) < 0)
3338 return isl_map_alloc_space(space
, n
, flags
);
3340 isl_space_free(space
);
3344 /* Make sure "map" has room for at least "n" more basic maps.
3346 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3349 struct isl_map
*grown
= NULL
;
3353 isl_assert(map
->ctx
, n
>= 0, goto error
);
3354 if (map
->n
+ n
<= map
->size
)
3356 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3359 for (i
= 0; i
< map
->n
; ++i
) {
3360 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3368 isl_map_free(grown
);
3373 /* Make sure "set" has room for at least "n" more basic sets.
3375 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3377 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3380 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3382 return isl_map_from_basic_map(bset
);
3385 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3387 struct isl_map
*map
;
3392 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3393 return isl_map_add_basic_map(map
, bmap
);
3396 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3397 __isl_take isl_basic_set
*bset
)
3399 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3400 bset_to_bmap(bset
)));
3403 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3405 return isl_map_free(set
);
3408 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3413 fprintf(out
, "null set\n");
3417 fprintf(out
, "%*s", indent
, "");
3418 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3419 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3421 for (i
= 0; i
< set
->n
; ++i
) {
3422 fprintf(out
, "%*s", indent
, "");
3423 fprintf(out
, "basic set %d:\n", i
);
3424 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3428 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3433 fprintf(out
, "null map\n");
3437 fprintf(out
, "%*s", indent
, "");
3438 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3439 "flags: %x, n_name: %d\n",
3440 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3441 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3442 for (i
= 0; i
< map
->n
; ++i
) {
3443 fprintf(out
, "%*s", indent
, "");
3444 fprintf(out
, "basic map %d:\n", i
);
3445 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3449 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3450 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3452 struct isl_basic_map
*bmap_domain
;
3454 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3457 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3458 isl_assert(bset
->ctx
,
3459 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3461 bmap
= isl_basic_map_cow(bmap
);
3464 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3465 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3466 bmap_domain
= isl_basic_map_from_domain(bset
);
3467 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3469 bmap
= isl_basic_map_simplify(bmap
);
3470 return isl_basic_map_finalize(bmap
);
3472 isl_basic_map_free(bmap
);
3473 isl_basic_set_free(bset
);
3477 /* Check that the space of "bset" is the same as that of the range of "bmap".
3479 static isl_stat
isl_basic_map_check_compatible_range(
3480 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3484 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3486 return isl_stat_error
;
3488 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3489 "incompatible spaces", return isl_stat_error
);
3494 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3495 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3497 struct isl_basic_map
*bmap_range
;
3499 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3502 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3503 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3506 if (isl_basic_set_plain_is_universe(bset
)) {
3507 isl_basic_set_free(bset
);
3511 bmap
= isl_basic_map_cow(bmap
);
3514 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3515 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3516 bmap_range
= bset_to_bmap(bset
);
3517 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3519 bmap
= isl_basic_map_simplify(bmap
);
3520 return isl_basic_map_finalize(bmap
);
3522 isl_basic_map_free(bmap
);
3523 isl_basic_set_free(bset
);
3527 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3528 __isl_keep isl_vec
*vec
)
3535 return isl_bool_error
;
3537 total
= 1 + isl_basic_map_total_dim(bmap
);
3538 if (total
!= vec
->size
)
3539 return isl_bool_false
;
3543 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3544 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3545 if (!isl_int_is_zero(s
)) {
3547 return isl_bool_false
;
3551 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3552 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3553 if (isl_int_is_neg(s
)) {
3555 return isl_bool_false
;
3561 return isl_bool_true
;
3564 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3565 __isl_keep isl_vec
*vec
)
3567 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3570 __isl_give isl_basic_map
*isl_basic_map_intersect(
3571 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3573 struct isl_vec
*sample
= NULL
;
3575 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3577 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3578 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3579 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3580 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3581 return isl_basic_map_intersect(bmap2
, bmap1
);
3583 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3584 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3585 isl_assert(bmap1
->ctx
,
3586 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3588 if (isl_basic_map_plain_is_empty(bmap1
)) {
3589 isl_basic_map_free(bmap2
);
3592 if (isl_basic_map_plain_is_empty(bmap2
)) {
3593 isl_basic_map_free(bmap1
);
3597 if (bmap1
->sample
&&
3598 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3599 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3600 sample
= isl_vec_copy(bmap1
->sample
);
3601 else if (bmap2
->sample
&&
3602 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3603 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3604 sample
= isl_vec_copy(bmap2
->sample
);
3606 bmap1
= isl_basic_map_cow(bmap1
);
3609 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3610 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3611 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3614 isl_vec_free(sample
);
3616 isl_vec_free(bmap1
->sample
);
3617 bmap1
->sample
= sample
;
3620 bmap1
= isl_basic_map_simplify(bmap1
);
3621 return isl_basic_map_finalize(bmap1
);
3624 isl_vec_free(sample
);
3625 isl_basic_map_free(bmap1
);
3626 isl_basic_map_free(bmap2
);
3630 struct isl_basic_set
*isl_basic_set_intersect(
3631 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3633 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3634 bset_to_bmap(bset2
)));
3637 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3638 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3640 return isl_basic_set_intersect(bset1
, bset2
);
3643 /* Special case of isl_map_intersect, where both map1 and map2
3644 * are convex, without any divs and such that either map1 or map2
3645 * contains a single constraint. This constraint is then simply
3646 * added to the other map.
3648 static __isl_give isl_map
*map_intersect_add_constraint(
3649 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3651 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3652 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3653 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3654 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3656 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3657 return isl_map_intersect(map2
, map1
);
3659 map1
= isl_map_cow(map1
);
3662 if (isl_map_plain_is_empty(map1
)) {
3666 if (map2
->p
[0]->n_eq
== 1)
3667 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3669 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3670 map2
->p
[0]->ineq
[0]);
3672 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3673 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3677 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3678 isl_basic_map_free(map1
->p
[0]);
3684 map1
= isl_map_unmark_normalized(map1
);
3692 /* map2 may be either a parameter domain or a map living in the same
3695 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3696 __isl_take isl_map
*map2
)
3706 if ((isl_map_plain_is_empty(map1
) ||
3707 isl_map_plain_is_universe(map2
)) &&
3708 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3712 if ((isl_map_plain_is_empty(map2
) ||
3713 isl_map_plain_is_universe(map1
)) &&
3714 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3719 if (map1
->n
== 1 && map2
->n
== 1 &&
3720 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3721 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3722 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3723 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3724 return map_intersect_add_constraint(map1
, map2
);
3726 equal
= isl_map_plain_is_equal(map1
, map2
);
3734 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3735 isl_space_dim(map2
->dim
, isl_dim_param
))
3736 isl_assert(map1
->ctx
,
3737 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3739 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3740 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3741 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3743 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3744 map1
->n
* map2
->n
, flags
);
3747 for (i
= 0; i
< map1
->n
; ++i
)
3748 for (j
= 0; j
< map2
->n
; ++j
) {
3749 struct isl_basic_map
*part
;
3750 part
= isl_basic_map_intersect(
3751 isl_basic_map_copy(map1
->p
[i
]),
3752 isl_basic_map_copy(map2
->p
[j
]));
3753 if (isl_basic_map_is_empty(part
) < 0)
3754 part
= isl_basic_map_free(part
);
3755 result
= isl_map_add_basic_map(result
, part
);
3768 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3769 __isl_take isl_map
*map2
)
3773 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3774 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3775 "spaces don't match", goto error
);
3776 return map_intersect_internal(map1
, map2
);
3783 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3784 __isl_take isl_map
*map2
)
3786 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3789 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3791 return set_from_map(isl_map_intersect(set_to_map(set1
),
3795 /* map_intersect_internal accepts intersections
3796 * with parameter domains, so we can just call that function.
3798 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3799 __isl_take isl_set
*params
)
3801 return map_intersect_internal(map
, params
);
3804 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3805 __isl_take isl_map
*map2
)
3807 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3810 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3811 __isl_take isl_set
*params
)
3813 return isl_map_intersect_params(set
, params
);
3816 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3819 unsigned pos
, n1
, n2
;
3823 bmap
= isl_basic_map_cow(bmap
);
3826 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3827 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3828 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3829 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3830 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3831 return isl_basic_map_reset_space(bmap
, space
);
3834 static __isl_give isl_basic_map
*basic_map_space_reset(
3835 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3841 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3844 space
= isl_basic_map_get_space(bmap
);
3845 space
= isl_space_reset(space
, type
);
3846 bmap
= isl_basic_map_reset_space(bmap
, space
);
3850 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3851 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3852 unsigned pos
, unsigned n
)
3855 isl_space
*res_space
;
3856 struct isl_basic_map
*res
;
3857 struct isl_dim_map
*dim_map
;
3858 unsigned total
, off
;
3859 enum isl_dim_type t
;
3862 return basic_map_space_reset(bmap
, type
);
3864 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3867 return isl_basic_map_free(bmap
);
3869 total
= isl_basic_map_total_dim(bmap
) + n
;
3870 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3872 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3874 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3876 unsigned size
= isl_basic_map_dim(bmap
, t
);
3877 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3879 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3880 pos
, size
- pos
, off
+ pos
+ n
);
3882 off
+= isl_space_dim(res_space
, t
);
3884 isl_dim_map_div(dim_map
, bmap
, off
);
3886 res
= isl_basic_map_alloc_space(res_space
,
3887 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3888 rational
= isl_basic_map_is_rational(bmap
);
3890 res
= isl_basic_map_free(res
);
3892 res
= isl_basic_map_set_rational(res
);
3893 if (isl_basic_map_plain_is_empty(bmap
)) {
3894 isl_basic_map_free(bmap
);
3896 return isl_basic_map_set_to_empty(res
);
3898 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3899 return isl_basic_map_finalize(res
);
3902 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3903 __isl_take isl_basic_set
*bset
,
3904 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3906 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3909 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3910 enum isl_dim_type type
, unsigned n
)
3914 return isl_basic_map_insert_dims(bmap
, type
,
3915 isl_basic_map_dim(bmap
, type
), n
);
3918 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3919 enum isl_dim_type type
, unsigned n
)
3923 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3924 return isl_basic_map_add_dims(bset
, type
, n
);
3926 isl_basic_set_free(bset
);
3930 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3931 enum isl_dim_type type
)
3935 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3938 space
= isl_map_get_space(map
);
3939 space
= isl_space_reset(space
, type
);
3940 map
= isl_map_reset_space(map
, space
);
3944 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3945 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3950 return map_space_reset(map
, type
);
3952 map
= isl_map_cow(map
);
3956 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3960 for (i
= 0; i
< map
->n
; ++i
) {
3961 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3972 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3973 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3975 return isl_map_insert_dims(set
, type
, pos
, n
);
3978 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3979 enum isl_dim_type type
, unsigned n
)
3983 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3986 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3987 enum isl_dim_type type
, unsigned n
)
3991 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3992 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3998 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3999 __isl_take isl_basic_map
*bmap
,
4000 enum isl_dim_type dst_type
, unsigned dst_pos
,
4001 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4003 struct isl_dim_map
*dim_map
;
4004 struct isl_basic_map
*res
;
4005 enum isl_dim_type t
;
4006 unsigned total
, off
;
4011 bmap
= isl_basic_map_reset(bmap
, src_type
);
4012 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4016 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4017 return isl_basic_map_free(bmap
);
4019 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4022 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4024 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4025 pos(bmap
->dim
, src_type
) + src_pos
+
4026 ((src_type
< dst_type
) ? n
: 0)) {
4027 bmap
= isl_basic_map_cow(bmap
);
4031 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4032 src_type
, src_pos
, n
);
4036 bmap
= isl_basic_map_finalize(bmap
);
4041 total
= isl_basic_map_total_dim(bmap
);
4042 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4045 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4046 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4047 if (t
== dst_type
) {
4048 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4051 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4054 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4055 dst_pos
, size
- dst_pos
, off
);
4056 off
+= size
- dst_pos
;
4057 } else if (t
== src_type
) {
4058 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4061 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4062 src_pos
+ n
, size
- src_pos
- n
, off
);
4063 off
+= size
- src_pos
- n
;
4065 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4069 isl_dim_map_div(dim_map
, bmap
, off
);
4071 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4072 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4073 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4077 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4078 src_type
, src_pos
, n
);
4082 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4083 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4084 bmap
= isl_basic_map_finalize(bmap
);
4088 isl_basic_map_free(bmap
);
4092 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4093 enum isl_dim_type dst_type
, unsigned dst_pos
,
4094 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4096 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4097 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4098 src_type
, src_pos
, n
);
4099 return bset_from_bmap(bmap
);
4102 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4103 enum isl_dim_type dst_type
, unsigned dst_pos
,
4104 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4108 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4109 return set_from_map(isl_map_move_dims(set_to_map(set
),
4110 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4116 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4117 enum isl_dim_type dst_type
, unsigned dst_pos
,
4118 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4123 map
= isl_map_reset(map
, src_type
);
4124 map
= isl_map_reset(map
, dst_type
);
4128 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4129 return isl_map_free(map
);
4131 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4134 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4136 map
= isl_map_cow(map
);
4140 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4144 for (i
= 0; i
< map
->n
; ++i
) {
4145 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4147 src_type
, src_pos
, n
);
4158 /* Move the specified dimensions to the last columns right before
4159 * the divs. Don't change the dimension specification of bmap.
4160 * That's the responsibility of the caller.
4162 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4163 enum isl_dim_type type
, unsigned first
, unsigned n
)
4165 struct isl_dim_map
*dim_map
;
4166 struct isl_basic_map
*res
;
4167 enum isl_dim_type t
;
4168 unsigned total
, off
;
4172 if (pos(bmap
->dim
, type
) + first
+ n
==
4173 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4176 total
= isl_basic_map_total_dim(bmap
);
4177 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4180 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4181 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4183 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4186 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4187 first
, n
, total
- bmap
->n_div
- n
);
4188 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4189 first
+ n
, size
- (first
+ n
), off
);
4190 off
+= size
- (first
+ n
);
4192 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4196 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4198 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4199 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4200 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4204 /* Insert "n" rows in the divs of "bmap".
4206 * The number of columns is not changed, which means that the last
4207 * dimensions of "bmap" are being reintepreted as the new divs.
4208 * The space of "bmap" is not adjusted, however, which means
4209 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4210 * from the space of "bmap" is the responsibility of the caller.
4212 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4220 bmap
= isl_basic_map_cow(bmap
);
4224 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4225 old
= bmap
->block2
.data
;
4226 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4227 (bmap
->extra
+ n
) * (1 + row_size
));
4228 if (!bmap
->block2
.data
)
4229 return isl_basic_map_free(bmap
);
4230 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4232 return isl_basic_map_free(bmap
);
4233 for (i
= 0; i
< n
; ++i
) {
4234 new_div
[i
] = bmap
->block2
.data
+
4235 (bmap
->extra
+ i
) * (1 + row_size
);
4236 isl_seq_clr(new_div
[i
], 1 + row_size
);
4238 for (i
= 0; i
< bmap
->extra
; ++i
)
4239 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4241 bmap
->div
= new_div
;
4248 /* Drop constraints from "bmap" that only involve the variables
4249 * of "type" in the range [first, first + n] that are not related
4250 * to any of the variables outside that interval.
4251 * These constraints cannot influence the values for the variables
4252 * outside the interval, except in case they cause "bmap" to be empty.
4253 * Only drop the constraints if "bmap" is known to be non-empty.
4255 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4256 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4257 unsigned first
, unsigned n
)
4261 unsigned dim
, n_div
;
4264 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4266 return isl_basic_map_free(bmap
);
4270 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4271 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4272 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4274 return isl_basic_map_free(bmap
);
4275 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4276 for (i
= 0; i
< first
; ++i
)
4278 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4281 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4286 /* Turn the n dimensions of type type, starting at first
4287 * into existentially quantified variables.
4289 * If a subset of the projected out variables are unrelated
4290 * to any of the variables that remain, then the constraints
4291 * involving this subset are simply dropped first.
4293 __isl_give isl_basic_map
*isl_basic_map_project_out(
4294 __isl_take isl_basic_map
*bmap
,
4295 enum isl_dim_type type
, unsigned first
, unsigned n
)
4300 return basic_map_space_reset(bmap
, type
);
4301 if (type
== isl_dim_div
)
4302 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4303 "cannot project out existentially quantified variables",
4304 return isl_basic_map_free(bmap
));
4306 empty
= isl_basic_map_plain_is_empty(bmap
);
4308 return isl_basic_map_free(bmap
);
4310 bmap
= isl_basic_map_set_to_empty(bmap
);
4312 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4316 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4317 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4319 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4320 return isl_basic_map_free(bmap
);
4322 bmap
= move_last(bmap
, type
, first
, n
);
4323 bmap
= isl_basic_map_cow(bmap
);
4324 bmap
= insert_div_rows(bmap
, n
);
4328 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4331 bmap
= isl_basic_map_simplify(bmap
);
4332 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4333 return isl_basic_map_finalize(bmap
);
4335 isl_basic_map_free(bmap
);
4339 /* Turn the n dimensions of type type, starting at first
4340 * into existentially quantified variables.
4342 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4343 enum isl_dim_type type
, unsigned first
, unsigned n
)
4345 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4349 /* Turn the n dimensions of type type, starting at first
4350 * into existentially quantified variables.
4352 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4353 enum isl_dim_type type
, unsigned first
, unsigned n
)
4358 return map_space_reset(map
, type
);
4360 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4361 return isl_map_free(map
);
4363 map
= isl_map_cow(map
);
4367 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4371 for (i
= 0; i
< map
->n
; ++i
) {
4372 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4383 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4384 * into existentially quantified variables.
4386 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4387 enum isl_dim_type type
, unsigned first
, unsigned n
)
4391 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4392 return isl_map_free(map
);
4393 dim
= isl_map_dim(map
, type
);
4394 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4395 map
= isl_map_project_out(map
, type
, 0, first
);
4399 /* Turn the n dimensions of type type, starting at first
4400 * into existentially quantified variables.
4402 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4403 enum isl_dim_type type
, unsigned first
, unsigned n
)
4405 return set_from_map(isl_map_project_out(set_to_map(set
),
4409 /* Return a map that projects the elements in "set" onto their
4410 * "n" set dimensions starting at "first".
4411 * "type" should be equal to isl_dim_set.
4413 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4414 enum isl_dim_type type
, unsigned first
, unsigned n
)
4422 if (type
!= isl_dim_set
)
4423 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4424 "only set dimensions can be projected out", goto error
);
4425 dim
= isl_set_dim(set
, isl_dim_set
);
4426 if (first
+ n
> dim
|| first
+ n
< first
)
4427 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4428 "index out of bounds", goto error
);
4430 map
= isl_map_from_domain(set
);
4431 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4432 for (i
= 0; i
< n
; ++i
)
4433 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4441 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4446 for (i
= 0; i
< n
; ++i
) {
4447 j
= isl_basic_map_alloc_div(bmap
);
4450 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4454 isl_basic_map_free(bmap
);
4458 struct isl_basic_map
*isl_basic_map_apply_range(
4459 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4461 isl_space
*space_result
= NULL
;
4462 struct isl_basic_map
*bmap
;
4463 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4464 struct isl_dim_map
*dim_map1
, *dim_map2
;
4466 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4468 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4469 bmap2
->dim
, isl_dim_in
))
4470 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4471 "spaces don't match", goto error
);
4473 space_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4474 isl_space_copy(bmap2
->dim
));
4476 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4477 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4478 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4479 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4481 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4482 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4483 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4484 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4485 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4486 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4487 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4488 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4489 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4490 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4491 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4493 bmap
= isl_basic_map_alloc_space(space_result
,
4494 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4495 bmap1
->n_eq
+ bmap2
->n_eq
,
4496 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4497 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4498 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4499 bmap
= add_divs(bmap
, n
);
4500 bmap
= isl_basic_map_simplify(bmap
);
4501 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4502 return isl_basic_map_finalize(bmap
);
4504 isl_basic_map_free(bmap1
);
4505 isl_basic_map_free(bmap2
);
4509 struct isl_basic_set
*isl_basic_set_apply(
4510 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4515 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4518 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4521 isl_basic_set_free(bset
);
4522 isl_basic_map_free(bmap
);
4526 struct isl_basic_map
*isl_basic_map_apply_domain(
4527 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4529 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4531 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4532 bmap2
->dim
, isl_dim_in
))
4533 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4534 "spaces don't match", goto error
);
4536 bmap1
= isl_basic_map_reverse(bmap1
);
4537 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4538 return isl_basic_map_reverse(bmap1
);
4540 isl_basic_map_free(bmap1
);
4541 isl_basic_map_free(bmap2
);
4545 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4546 * A \cap B -> f(A) + f(B)
4548 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4549 __isl_take isl_basic_map
*bmap2
)
4551 unsigned n_in
, n_out
, nparam
, total
, pos
;
4552 struct isl_basic_map
*bmap
= NULL
;
4553 struct isl_dim_map
*dim_map1
, *dim_map2
;
4556 if (!bmap1
|| !bmap2
)
4559 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4562 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4563 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4564 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4566 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4567 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4568 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4569 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4570 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4571 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4572 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4573 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4574 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4575 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4576 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4578 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4579 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4580 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4581 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4582 for (i
= 0; i
< n_out
; ++i
) {
4583 int j
= isl_basic_map_alloc_equality(bmap
);
4586 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4587 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4588 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4589 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4591 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4592 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4593 bmap
= add_divs(bmap
, 2 * n_out
);
4595 bmap
= isl_basic_map_simplify(bmap
);
4596 return isl_basic_map_finalize(bmap
);
4598 isl_basic_map_free(bmap
);
4599 isl_basic_map_free(bmap1
);
4600 isl_basic_map_free(bmap2
);
4604 /* Given two maps A -> f(A) and B -> g(B), construct a map
4605 * A \cap B -> f(A) + f(B)
4607 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4608 __isl_take isl_map
*map2
)
4610 struct isl_map
*result
;
4616 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4618 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4619 map1
->n
* map2
->n
, 0);
4622 for (i
= 0; i
< map1
->n
; ++i
)
4623 for (j
= 0; j
< map2
->n
; ++j
) {
4624 struct isl_basic_map
*part
;
4625 part
= isl_basic_map_sum(
4626 isl_basic_map_copy(map1
->p
[i
]),
4627 isl_basic_map_copy(map2
->p
[j
]));
4628 if (isl_basic_map_is_empty(part
))
4629 isl_basic_map_free(part
);
4631 result
= isl_map_add_basic_map(result
, part
);
4644 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4645 __isl_take isl_set
*set2
)
4647 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4650 /* Given a basic map A -> f(A), construct A -> -f(A).
4652 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4657 bmap
= isl_basic_map_cow(bmap
);
4661 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4662 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4663 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4664 for (j
= 0; j
< n
; ++j
)
4665 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4666 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4667 for (j
= 0; j
< n
; ++j
)
4668 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4669 for (i
= 0; i
< bmap
->n_div
; ++i
)
4670 for (j
= 0; j
< n
; ++j
)
4671 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4672 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4673 return isl_basic_map_finalize(bmap
);
4676 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4678 return isl_basic_map_neg(bset
);
4681 /* Given a map A -> f(A), construct A -> -f(A).
4683 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4687 map
= isl_map_cow(map
);
4691 for (i
= 0; i
< map
->n
; ++i
) {
4692 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4703 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4705 return set_from_map(isl_map_neg(set_to_map(set
)));
4708 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4709 * A -> floor(f(A)/d).
4711 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4714 unsigned n_in
, n_out
, nparam
, total
, pos
;
4715 struct isl_basic_map
*result
= NULL
;
4716 struct isl_dim_map
*dim_map
;
4722 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4723 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4724 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4726 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4727 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4728 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4729 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4730 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4731 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4733 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4734 bmap
->n_div
+ n_out
,
4735 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4736 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4737 result
= add_divs(result
, n_out
);
4738 for (i
= 0; i
< n_out
; ++i
) {
4740 j
= isl_basic_map_alloc_inequality(result
);
4743 isl_seq_clr(result
->ineq
[j
], 1+total
);
4744 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4745 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4746 j
= isl_basic_map_alloc_inequality(result
);
4749 isl_seq_clr(result
->ineq
[j
], 1+total
);
4750 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4751 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4752 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4755 result
= isl_basic_map_simplify(result
);
4756 return isl_basic_map_finalize(result
);
4758 isl_basic_map_free(result
);
4762 /* Given a map A -> f(A) and an integer d, construct a map
4763 * A -> floor(f(A)/d).
4765 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4769 map
= isl_map_cow(map
);
4773 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4774 for (i
= 0; i
< map
->n
; ++i
) {
4775 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4779 map
= isl_map_unmark_normalized(map
);
4787 /* Given a map A -> f(A) and an integer d, construct a map
4788 * A -> floor(f(A)/d).
4790 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4791 __isl_take isl_val
*d
)
4795 if (!isl_val_is_int(d
))
4796 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4797 "expecting integer denominator", goto error
);
4798 map
= isl_map_floordiv(map
, d
->n
);
4807 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4814 i
= isl_basic_map_alloc_equality(bmap
);
4817 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4818 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4819 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4820 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4821 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4822 return isl_basic_map_finalize(bmap
);
4824 isl_basic_map_free(bmap
);
4828 /* Add a constraint to "bmap" expressing i_pos < o_pos
4830 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4837 i
= isl_basic_map_alloc_inequality(bmap
);
4840 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4841 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4842 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4843 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4844 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4845 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4846 return isl_basic_map_finalize(bmap
);
4848 isl_basic_map_free(bmap
);
4852 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4854 static __isl_give isl_basic_map
*var_less_or_equal(
4855 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4861 i
= isl_basic_map_alloc_inequality(bmap
);
4864 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4865 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4866 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4867 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4868 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4869 return isl_basic_map_finalize(bmap
);
4871 isl_basic_map_free(bmap
);
4875 /* Add a constraint to "bmap" expressing i_pos > o_pos
4877 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4884 i
= isl_basic_map_alloc_inequality(bmap
);
4887 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4888 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4889 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4890 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4891 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4892 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4893 return isl_basic_map_finalize(bmap
);
4895 isl_basic_map_free(bmap
);
4899 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4901 static __isl_give isl_basic_map
*var_more_or_equal(
4902 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4908 i
= isl_basic_map_alloc_inequality(bmap
);
4911 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4912 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4913 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4914 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4915 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4916 return isl_basic_map_finalize(bmap
);
4918 isl_basic_map_free(bmap
);
4922 __isl_give isl_basic_map
*isl_basic_map_equal(
4923 __isl_take isl_space
*space
, unsigned n_equal
)
4926 struct isl_basic_map
*bmap
;
4927 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4930 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4931 bmap
= var_equal(bmap
, i
);
4932 return isl_basic_map_finalize(bmap
);
4935 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4937 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4941 struct isl_basic_map
*bmap
;
4942 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4945 for (i
= 0; i
< pos
&& bmap
; ++i
)
4946 bmap
= var_equal(bmap
, i
);
4948 bmap
= var_less(bmap
, pos
);
4949 return isl_basic_map_finalize(bmap
);
4952 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4954 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4955 __isl_take isl_space
*space
, unsigned pos
)
4958 isl_basic_map
*bmap
;
4960 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4961 for (i
= 0; i
< pos
; ++i
)
4962 bmap
= var_equal(bmap
, i
);
4963 bmap
= var_less_or_equal(bmap
, pos
);
4964 return isl_basic_map_finalize(bmap
);
4967 /* Return a relation on "space" expressing i_pos > o_pos
4969 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4973 struct isl_basic_map
*bmap
;
4974 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4977 for (i
= 0; i
< pos
&& bmap
; ++i
)
4978 bmap
= var_equal(bmap
, i
);
4980 bmap
= var_more(bmap
, pos
);
4981 return isl_basic_map_finalize(bmap
);
4984 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4986 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4987 __isl_take isl_space
*space
, unsigned pos
)
4990 isl_basic_map
*bmap
;
4992 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4993 for (i
= 0; i
< pos
; ++i
)
4994 bmap
= var_equal(bmap
, i
);
4995 bmap
= var_more_or_equal(bmap
, pos
);
4996 return isl_basic_map_finalize(bmap
);
4999 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5000 unsigned n
, int equal
)
5002 struct isl_map
*map
;
5005 if (n
== 0 && equal
)
5006 return isl_map_universe(space
);
5008 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5010 for (i
= 0; i
+ 1 < n
; ++i
)
5011 map
= isl_map_add_basic_map(map
,
5012 isl_basic_map_less_at(isl_space_copy(space
), i
));
5015 map
= isl_map_add_basic_map(map
,
5016 isl_basic_map_less_or_equal_at(space
, n
- 1));
5018 map
= isl_map_add_basic_map(map
,
5019 isl_basic_map_less_at(space
, n
- 1));
5021 isl_space_free(space
);
5026 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5030 return map_lex_lte_first(space
, space
->n_out
, equal
);
5033 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5035 return map_lex_lte_first(dim
, n
, 0);
5038 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5040 return map_lex_lte_first(dim
, n
, 1);
5043 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5045 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5048 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5050 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5053 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5054 unsigned n
, int equal
)
5056 struct isl_map
*map
;
5059 if (n
== 0 && equal
)
5060 return isl_map_universe(space
);
5062 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5064 for (i
= 0; i
+ 1 < n
; ++i
)
5065 map
= isl_map_add_basic_map(map
,
5066 isl_basic_map_more_at(isl_space_copy(space
), i
));
5069 map
= isl_map_add_basic_map(map
,
5070 isl_basic_map_more_or_equal_at(space
, n
- 1));
5072 map
= isl_map_add_basic_map(map
,
5073 isl_basic_map_more_at(space
, n
- 1));
5075 isl_space_free(space
);
5080 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5084 return map_lex_gte_first(space
, space
->n_out
, equal
);
5087 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5089 return map_lex_gte_first(dim
, n
, 0);
5092 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5094 return map_lex_gte_first(dim
, n
, 1);
5097 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5099 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5102 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5104 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5107 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5108 __isl_take isl_set
*set2
)
5111 map
= isl_map_lex_le(isl_set_get_space(set1
));
5112 map
= isl_map_intersect_domain(map
, set1
);
5113 map
= isl_map_intersect_range(map
, set2
);
5117 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5118 __isl_take isl_set
*set2
)
5121 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5122 map
= isl_map_intersect_domain(map
, set1
);
5123 map
= isl_map_intersect_range(map
, set2
);
5127 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5128 __isl_take isl_set
*set2
)
5131 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5132 map
= isl_map_intersect_domain(map
, set1
);
5133 map
= isl_map_intersect_range(map
, set2
);
5137 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5138 __isl_take isl_set
*set2
)
5141 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5142 map
= isl_map_intersect_domain(map
, set1
);
5143 map
= isl_map_intersect_range(map
, set2
);
5147 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5148 __isl_take isl_map
*map2
)
5151 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5152 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5153 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5157 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5158 __isl_take isl_map
*map2
)
5161 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5162 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5163 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5167 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5168 __isl_take isl_map
*map2
)
5171 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5172 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5173 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5177 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5178 __isl_take isl_map
*map2
)
5181 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5182 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5183 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5187 /* For a div d = floor(f/m), add the constraint
5191 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5192 unsigned pos
, isl_int
*div
)
5195 unsigned total
= isl_basic_map_total_dim(bmap
);
5197 i
= isl_basic_map_alloc_inequality(bmap
);
5199 return isl_stat_error
;
5200 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5201 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5206 /* For a div d = floor(f/m), add the constraint
5208 * -(f-(m-1)) + m d >= 0
5210 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5211 unsigned pos
, isl_int
*div
)
5214 unsigned total
= isl_basic_map_total_dim(bmap
);
5216 i
= isl_basic_map_alloc_inequality(bmap
);
5218 return isl_stat_error
;
5219 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5220 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5221 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5222 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5227 /* For a div d = floor(f/m), add the constraints
5230 * -(f-(m-1)) + m d >= 0
5232 * Note that the second constraint is the negation of
5236 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5237 unsigned pos
, isl_int
*div
)
5239 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5241 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5246 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5247 unsigned pos
, isl_int
*div
)
5249 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5253 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map
*bmap
,
5256 unsigned total
= isl_basic_map_total_dim(bmap
);
5257 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5259 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5263 /* For each known div d = floor(f/m), add the constraints
5266 * -(f-(m-1)) + m d >= 0
5268 * Remove duplicate constraints in case of some these div constraints
5269 * already appear in "bmap".
5271 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5272 __isl_take isl_basic_map
*bmap
)
5278 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5282 bmap
= add_known_div_constraints(bmap
);
5283 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5284 bmap
= isl_basic_map_finalize(bmap
);
5288 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5290 * In particular, if this div is of the form d = floor(f/m),
5291 * then add the constraint
5295 * if sign < 0 or the constraint
5297 * -(f-(m-1)) + m d >= 0
5301 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5302 unsigned div
, int sign
)
5310 total
= isl_basic_map_total_dim(bmap
);
5311 div_pos
= total
- bmap
->n_div
+ div
;
5314 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5316 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5319 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5320 __isl_take isl_basic_map
*bmap
)
5324 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5326 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5327 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5328 return bset_from_bmap(bmap
);
5329 bmap
= isl_basic_map_cow(bmap
);
5332 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5335 bmap
->extra
-= bmap
->n_div
;
5337 bmap
= isl_basic_map_finalize(bmap
);
5338 return bset_from_bmap(bmap
);
5340 isl_basic_map_free(bmap
);
5344 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5345 __isl_take isl_basic_set
*bset
)
5347 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5350 /* Replace each element in "list" by the result of applying
5351 * isl_basic_map_underlying_set to the element.
5353 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5354 __isl_take isl_basic_map_list
*list
)
5361 n
= isl_basic_map_list_n_basic_map(list
);
5362 for (i
= 0; i
< n
; ++i
) {
5363 isl_basic_map
*bmap
;
5364 isl_basic_set
*bset
;
5366 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5367 bset
= isl_basic_set_underlying_set(bmap
);
5368 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5374 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5375 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5377 struct isl_basic_map
*bmap
;
5378 struct isl_ctx
*ctx
;
5385 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5386 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5387 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5389 if (like
->n_div
== 0) {
5390 isl_space
*space
= isl_basic_map_get_space(like
);
5391 isl_basic_map_free(like
);
5392 return isl_basic_map_reset_space(bset
, space
);
5394 bset
= isl_basic_set_cow(bset
);
5397 total
= bset
->dim
->n_out
+ bset
->extra
;
5398 bmap
= bset_to_bmap(bset
);
5399 isl_space_free(bmap
->dim
);
5400 bmap
->dim
= isl_space_copy(like
->dim
);
5403 bmap
->n_div
= like
->n_div
;
5404 bmap
->extra
+= like
->n_div
;
5408 ltotal
= total
- bmap
->extra
+ like
->extra
;
5411 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5412 bmap
->extra
* (1 + 1 + total
));
5413 if (isl_blk_is_error(bmap
->block2
))
5415 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5419 for (i
= 0; i
< bmap
->extra
; ++i
)
5420 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5421 for (i
= 0; i
< like
->n_div
; ++i
) {
5422 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5423 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5425 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5427 isl_basic_map_free(like
);
5428 bmap
= isl_basic_map_simplify(bmap
);
5429 bmap
= isl_basic_map_finalize(bmap
);
5432 isl_basic_map_free(like
);
5433 isl_basic_set_free(bset
);
5437 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5438 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5440 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5441 bset_to_bmap(like
)));
5444 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5448 map
= isl_map_cow(map
);
5451 map
->dim
= isl_space_cow(map
->dim
);
5455 for (i
= 1; i
< map
->n
; ++i
)
5456 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5458 for (i
= 0; i
< map
->n
; ++i
) {
5459 map
->p
[i
] = bset_to_bmap(
5460 isl_basic_map_underlying_set(map
->p
[i
]));
5465 map
->dim
= isl_space_underlying(map
->dim
, 0);
5467 isl_space_free(map
->dim
);
5468 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5472 return set_from_map(map
);
5478 /* Replace the space of "bmap" by "space".
5480 * If the space of "bmap" is identical to "space" (including the identifiers
5481 * of the input and output dimensions), then simply return the original input.
5483 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5484 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5487 isl_space
*bmap_space
;
5489 bmap_space
= isl_basic_map_peek_space(bmap
);
5490 equal
= isl_space_is_equal(bmap_space
, space
);
5491 if (equal
>= 0 && equal
)
5492 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5496 isl_space_free(space
);
5499 bmap
= isl_basic_map_cow(bmap
);
5500 if (!bmap
|| !space
)
5503 isl_space_free(bmap
->dim
);
5506 bmap
= isl_basic_map_finalize(bmap
);
5510 isl_basic_map_free(bmap
);
5511 isl_space_free(space
);
5515 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5516 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5518 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5522 /* Check that the total dimensions of "map" and "space" are the same.
5524 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5525 __isl_keep isl_space
*space
)
5527 unsigned dim1
, dim2
;
5530 return isl_stat_error
;
5531 dim1
= isl_map_dim(map
, isl_dim_all
);
5532 dim2
= isl_space_dim(space
, isl_dim_all
);
5535 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5536 "total dimensions do not match", return isl_stat_error
);
5539 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5540 __isl_take isl_space
*space
)
5544 map
= isl_map_cow(map
);
5548 for (i
= 0; i
< map
->n
; ++i
) {
5549 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5550 isl_space_copy(space
));
5554 isl_space_free(map
->dim
);
5560 isl_space_free(space
);
5564 /* Replace the space of "map" by "space", without modifying
5565 * the dimension of "map".
5567 * If the space of "map" is identical to "space" (including the identifiers
5568 * of the input and output dimensions), then simply return the original input.
5570 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5571 __isl_take isl_space
*space
)
5574 isl_space
*map_space
;
5576 map_space
= isl_map_peek_space(map
);
5577 equal
= isl_space_is_equal(map_space
, space
);
5578 if (equal
>= 0 && equal
)
5579 equal
= isl_space_has_equal_ids(map_space
, space
);
5583 isl_space_free(space
);
5586 if (check_map_space_equal_total_dim(map
, space
) < 0)
5588 return isl_map_reset_space(map
, space
);
5591 isl_space_free(space
);
5595 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5596 __isl_take isl_space
*dim
)
5598 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5601 /* Compute the parameter domain of the given basic set.
5603 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5609 is_params
= isl_basic_set_is_params(bset
);
5611 return isl_basic_set_free(bset
);
5615 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5616 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5617 space
= isl_basic_set_get_space(bset
);
5618 space
= isl_space_params(space
);
5619 bset
= isl_basic_set_reset_space(bset
, space
);
5623 /* Construct a zero-dimensional basic set with the given parameter domain.
5625 __isl_give isl_basic_set
*isl_basic_set_from_params(
5626 __isl_take isl_basic_set
*bset
)
5629 space
= isl_basic_set_get_space(bset
);
5630 space
= isl_space_set_from_params(space
);
5631 bset
= isl_basic_set_reset_space(bset
, space
);
5635 /* Compute the parameter domain of the given set.
5637 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5642 if (isl_set_is_params(set
))
5645 n
= isl_set_dim(set
, isl_dim_set
);
5646 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5647 space
= isl_set_get_space(set
);
5648 space
= isl_space_params(space
);
5649 set
= isl_set_reset_space(set
, space
);
5653 /* Construct a zero-dimensional set with the given parameter domain.
5655 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5658 space
= isl_set_get_space(set
);
5659 space
= isl_space_set_from_params(space
);
5660 set
= isl_set_reset_space(set
, space
);
5664 /* Compute the parameter domain of the given map.
5666 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5671 n
= isl_map_dim(map
, isl_dim_in
);
5672 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5673 n
= isl_map_dim(map
, isl_dim_out
);
5674 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5675 space
= isl_map_get_space(map
);
5676 space
= isl_space_params(space
);
5677 map
= isl_map_reset_space(map
, space
);
5681 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5688 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5690 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5691 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5693 return isl_basic_map_reset_space(bmap
, space
);
5696 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5699 return isl_bool_error
;
5700 return isl_space_may_be_set(bmap
->dim
);
5703 /* Is this basic map actually a set?
5704 * Users should never call this function. Outside of isl,
5705 * the type should indicate whether something is a set or a map.
5707 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5710 return isl_bool_error
;
5711 return isl_space_is_set(bmap
->dim
);
5714 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5718 is_set
= isl_basic_map_is_set(bmap
);
5723 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5725 isl_basic_map_free(bmap
);
5729 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5730 __isl_take isl_basic_map
*bmap
)
5734 isl_basic_map
*domain
;
5735 int nparam
, n_in
, n_out
;
5737 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5738 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5739 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5741 space
= isl_basic_map_get_space(bmap
);
5742 space
= isl_space_from_range(isl_space_domain(space
));
5743 domain
= isl_basic_map_universe(space
);
5745 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5746 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5747 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5749 for (i
= 0; i
< n_in
; ++i
)
5750 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5753 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5754 return isl_basic_map_finalize(bmap
);
5757 __isl_give isl_basic_map
*isl_basic_map_range_map(
5758 __isl_take isl_basic_map
*bmap
)
5762 isl_basic_map
*range
;
5763 int nparam
, n_in
, n_out
;
5765 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5766 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5767 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5769 space
= isl_basic_map_get_space(bmap
);
5770 space
= isl_space_from_range(isl_space_range(space
));
5771 range
= isl_basic_map_universe(space
);
5773 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5774 bmap
= isl_basic_map_apply_range(bmap
, range
);
5775 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5777 for (i
= 0; i
< n_out
; ++i
)
5778 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5781 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5782 return isl_basic_map_finalize(bmap
);
5785 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5789 return isl_space_may_be_set(map
->dim
);
5792 /* Is this map actually a set?
5793 * Users should never call this function. Outside of isl,
5794 * the type should indicate whether something is a set or a map.
5796 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5799 return isl_bool_error
;
5800 return isl_space_is_set(map
->dim
);
5803 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5807 struct isl_set
*set
;
5809 is_set
= isl_map_is_set(map
);
5813 return set_from_map(map
);
5815 map
= isl_map_cow(map
);
5819 set
= set_from_map(map
);
5820 set
->dim
= isl_space_range(set
->dim
);
5823 for (i
= 0; i
< map
->n
; ++i
) {
5824 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5828 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5829 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5836 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5840 map
= isl_map_cow(map
);
5844 map
->dim
= isl_space_domain_map(map
->dim
);
5847 for (i
= 0; i
< map
->n
; ++i
) {
5848 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5852 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5853 map
= isl_map_unmark_normalized(map
);
5860 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5863 isl_space
*range_dim
;
5865 map
= isl_map_cow(map
);
5869 range_dim
= isl_space_range(isl_map_get_space(map
));
5870 range_dim
= isl_space_from_range(range_dim
);
5871 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5872 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5875 for (i
= 0; i
< map
->n
; ++i
) {
5876 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5880 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5881 map
= isl_map_unmark_normalized(map
);
5888 /* Given a wrapped map of the form A[B -> C],
5889 * return the map A[B -> C] -> B.
5891 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5898 if (!isl_set_has_tuple_id(set
))
5899 return isl_map_domain_map(isl_set_unwrap(set
));
5901 id
= isl_set_get_tuple_id(set
);
5902 map
= isl_map_domain_map(isl_set_unwrap(set
));
5903 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5908 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5909 __isl_take isl_basic_set
*bset
)
5911 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5914 __isl_give isl_basic_map
*isl_basic_map_from_range(
5915 __isl_take isl_basic_set
*bset
)
5918 space
= isl_basic_set_get_space(bset
);
5919 space
= isl_space_from_range(space
);
5920 bset
= isl_basic_set_reset_space(bset
, space
);
5921 return bset_to_bmap(bset
);
5924 /* Create a relation with the given set as range.
5925 * The domain of the created relation is a zero-dimensional
5926 * flat anonymous space.
5928 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5931 space
= isl_set_get_space(set
);
5932 space
= isl_space_from_range(space
);
5933 set
= isl_set_reset_space(set
, space
);
5934 return set_to_map(set
);
5937 /* Create a relation with the given set as domain.
5938 * The range of the created relation is a zero-dimensional
5939 * flat anonymous space.
5941 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5943 return isl_map_reverse(isl_map_from_range(set
));
5946 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5947 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5949 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5952 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5953 __isl_take isl_set
*range
)
5955 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5958 /* Return a newly allocated isl_map with given space and flags and
5959 * room for "n" basic maps.
5960 * Make sure that all cached information is cleared.
5962 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5965 struct isl_map
*map
;
5970 isl_die(space
->ctx
, isl_error_internal
,
5971 "negative number of basic maps", goto error
);
5972 map
= isl_calloc(space
->ctx
, struct isl_map
,
5973 sizeof(struct isl_map
) +
5974 (n
- 1) * sizeof(struct isl_basic_map
*));
5978 map
->ctx
= space
->ctx
;
5979 isl_ctx_ref(map
->ctx
);
5987 isl_space_free(space
);
5991 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5993 struct isl_basic_map
*bmap
;
5994 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5995 bmap
= isl_basic_map_set_to_empty(bmap
);
5999 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6001 struct isl_basic_set
*bset
;
6002 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6003 bset
= isl_basic_set_set_to_empty(bset
);
6007 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6009 struct isl_basic_map
*bmap
;
6010 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6011 bmap
= isl_basic_map_finalize(bmap
);
6015 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6017 struct isl_basic_set
*bset
;
6018 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6019 bset
= isl_basic_set_finalize(bset
);
6023 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6024 __isl_take isl_space
*space
)
6027 unsigned total
= isl_space_dim(space
, isl_dim_all
);
6028 isl_basic_map
*bmap
;
6030 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6031 for (i
= 0; i
< total
; ++i
) {
6032 int k
= isl_basic_map_alloc_inequality(bmap
);
6035 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6036 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6040 isl_basic_map_free(bmap
);
6044 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6045 __isl_take isl_space
*space
)
6047 return isl_basic_map_nat_universe(space
);
6050 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6052 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6055 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6057 return isl_map_nat_universe(dim
);
6060 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6062 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6065 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6067 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6070 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6072 struct isl_map
*map
;
6075 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6076 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6080 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6082 struct isl_set
*set
;
6085 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6086 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6090 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6093 struct isl_map
*dup
;
6097 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6098 for (i
= 0; i
< map
->n
; ++i
)
6099 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6103 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6104 __isl_take isl_basic_map
*bmap
)
6108 if (isl_basic_map_plain_is_empty(bmap
)) {
6109 isl_basic_map_free(bmap
);
6112 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6113 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6114 map
->p
[map
->n
] = bmap
;
6116 map
= isl_map_unmark_normalized(map
);
6122 isl_basic_map_free(bmap
);
6126 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6137 isl_ctx_deref(map
->ctx
);
6138 for (i
= 0; i
< map
->n
; ++i
)
6139 isl_basic_map_free(map
->p
[i
]);
6140 isl_space_free(map
->dim
);
6146 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6147 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6151 bmap
= isl_basic_map_cow(bmap
);
6152 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6153 j
= isl_basic_map_alloc_equality(bmap
);
6156 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6157 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6158 isl_int_set_si(bmap
->eq
[j
][0], value
);
6159 bmap
= isl_basic_map_simplify(bmap
);
6160 return isl_basic_map_finalize(bmap
);
6162 isl_basic_map_free(bmap
);
6166 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6167 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6171 bmap
= isl_basic_map_cow(bmap
);
6172 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6173 j
= isl_basic_map_alloc_equality(bmap
);
6176 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6177 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6178 isl_int_set(bmap
->eq
[j
][0], value
);
6179 bmap
= isl_basic_map_simplify(bmap
);
6180 return isl_basic_map_finalize(bmap
);
6182 isl_basic_map_free(bmap
);
6186 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6187 enum isl_dim_type type
, unsigned pos
, int value
)
6189 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6190 return isl_basic_map_free(bmap
);
6191 return isl_basic_map_fix_pos_si(bmap
,
6192 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6195 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6196 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6198 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6199 return isl_basic_map_free(bmap
);
6200 return isl_basic_map_fix_pos(bmap
,
6201 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6204 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6205 * to be equal to "v".
6207 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6208 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6212 if (!isl_val_is_int(v
))
6213 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6214 "expecting integer value", goto error
);
6215 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6217 pos
+= isl_basic_map_offset(bmap
, type
);
6218 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6222 isl_basic_map_free(bmap
);
6227 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6228 * to be equal to "v".
6230 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6231 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6233 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6236 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6237 enum isl_dim_type type
, unsigned pos
, int value
)
6239 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6243 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6244 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6246 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6250 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6251 unsigned input
, int value
)
6253 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6256 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6257 unsigned dim
, int value
)
6259 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6260 isl_dim_set
, dim
, value
));
6263 /* Remove the basic map at position "i" from "map" if this basic map
6264 * is (obviously) empty.
6266 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6273 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6275 return isl_map_free(map
);
6279 isl_basic_map_free(map
->p
[i
]);
6282 map
->p
[i
] = map
->p
[map
->n
];
6283 map
= isl_map_unmark_normalized(map
);
6290 /* Perform "fn" on each basic map of "map", where we may not be holding
6291 * the only reference to "map".
6292 * In particular, "fn" should be a semantics preserving operation
6293 * that we want to apply to all copies of "map". We therefore need
6294 * to be careful not to modify "map" in a way that breaks "map"
6295 * in case anything goes wrong.
6297 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6298 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6300 struct isl_basic_map
*bmap
;
6306 for (i
= map
->n
- 1; i
>= 0; --i
) {
6307 bmap
= isl_basic_map_copy(map
->p
[i
]);
6311 isl_basic_map_free(map
->p
[i
]);
6313 map
= remove_if_empty(map
, i
);
6324 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6325 enum isl_dim_type type
, unsigned pos
, int value
)
6329 map
= isl_map_cow(map
);
6333 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6334 for (i
= map
->n
- 1; i
>= 0; --i
) {
6335 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6336 map
= remove_if_empty(map
, i
);
6340 map
= isl_map_unmark_normalized(map
);
6347 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6348 enum isl_dim_type type
, unsigned pos
, int value
)
6350 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6353 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6354 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6358 map
= isl_map_cow(map
);
6362 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6363 for (i
= 0; i
< map
->n
; ++i
) {
6364 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6368 map
= isl_map_unmark_normalized(map
);
6375 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6376 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6378 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6381 /* Fix the value of the variable at position "pos" of type "type" of "map"
6382 * to be equal to "v".
6384 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6385 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6389 map
= isl_map_cow(map
);
6393 if (!isl_val_is_int(v
))
6394 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6395 "expecting integer value", goto error
);
6396 if (pos
>= isl_map_dim(map
, type
))
6397 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6398 "index out of bounds", goto error
);
6399 for (i
= map
->n
- 1; i
>= 0; --i
) {
6400 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6402 map
= remove_if_empty(map
, i
);
6406 map
= isl_map_unmark_normalized(map
);
6415 /* Fix the value of the variable at position "pos" of type "type" of "set"
6416 * to be equal to "v".
6418 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6419 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6421 return isl_map_fix_val(set
, type
, pos
, v
);
6424 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6425 unsigned input
, int value
)
6427 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6430 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6432 return set_from_map(isl_map_fix_si(set_to_map(set
),
6433 isl_dim_set
, dim
, value
));
6436 static __isl_give isl_basic_map
*basic_map_bound_si(
6437 __isl_take isl_basic_map
*bmap
,
6438 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6442 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6443 return isl_basic_map_free(bmap
);
6444 pos
+= isl_basic_map_offset(bmap
, type
);
6445 bmap
= isl_basic_map_cow(bmap
);
6446 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6447 j
= isl_basic_map_alloc_inequality(bmap
);
6450 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6452 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6453 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6455 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6456 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6458 bmap
= isl_basic_map_simplify(bmap
);
6459 return isl_basic_map_finalize(bmap
);
6461 isl_basic_map_free(bmap
);
6465 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6466 __isl_take isl_basic_map
*bmap
,
6467 enum isl_dim_type type
, unsigned pos
, int value
)
6469 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6472 /* Constrain the values of the given dimension to be no greater than "value".
6474 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6475 __isl_take isl_basic_map
*bmap
,
6476 enum isl_dim_type type
, unsigned pos
, int value
)
6478 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6481 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6482 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6486 map
= isl_map_cow(map
);
6490 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6491 for (i
= 0; i
< map
->n
; ++i
) {
6492 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6493 type
, pos
, value
, upper
);
6497 map
= isl_map_unmark_normalized(map
);
6504 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6505 enum isl_dim_type type
, unsigned pos
, int value
)
6507 return map_bound_si(map
, type
, pos
, value
, 0);
6510 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6511 enum isl_dim_type type
, unsigned pos
, int value
)
6513 return map_bound_si(map
, type
, pos
, value
, 1);
6516 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6517 enum isl_dim_type type
, unsigned pos
, int value
)
6519 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6523 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6524 enum isl_dim_type type
, unsigned pos
, int value
)
6526 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6529 /* Bound the given variable of "bmap" from below (or above is "upper"
6530 * is set) to "value".
6532 static __isl_give isl_basic_map
*basic_map_bound(
6533 __isl_take isl_basic_map
*bmap
,
6534 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6538 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6539 return isl_basic_map_free(bmap
);
6540 pos
+= isl_basic_map_offset(bmap
, type
);
6541 bmap
= isl_basic_map_cow(bmap
);
6542 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6543 j
= isl_basic_map_alloc_inequality(bmap
);
6546 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6548 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6549 isl_int_set(bmap
->ineq
[j
][0], value
);
6551 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6552 isl_int_neg(bmap
->ineq
[j
][0], value
);
6554 bmap
= isl_basic_map_simplify(bmap
);
6555 return isl_basic_map_finalize(bmap
);
6557 isl_basic_map_free(bmap
);
6561 /* Bound the given variable of "map" from below (or above is "upper"
6562 * is set) to "value".
6564 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6565 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6569 map
= isl_map_cow(map
);
6573 if (pos
>= isl_map_dim(map
, type
))
6574 isl_die(map
->ctx
, isl_error_invalid
,
6575 "index out of bounds", goto error
);
6576 for (i
= map
->n
- 1; i
>= 0; --i
) {
6577 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6578 map
= remove_if_empty(map
, i
);
6582 map
= isl_map_unmark_normalized(map
);
6589 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6590 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6592 return map_bound(map
, type
, pos
, value
, 0);
6595 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6596 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6598 return map_bound(map
, type
, pos
, value
, 1);
6601 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6602 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6604 return isl_map_lower_bound(set
, type
, pos
, value
);
6607 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6608 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6610 return isl_map_upper_bound(set
, type
, pos
, value
);
6613 /* Force the values of the variable at position "pos" of type "type" of "set"
6614 * to be no smaller than "value".
6616 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6617 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6621 if (!isl_val_is_int(value
))
6622 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6623 "expecting integer value", goto error
);
6624 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6625 isl_val_free(value
);
6628 isl_val_free(value
);
6633 /* Force the values of the variable at position "pos" of type "type" of "set"
6634 * to be no greater than "value".
6636 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6637 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6641 if (!isl_val_is_int(value
))
6642 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6643 "expecting integer value", goto error
);
6644 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6645 isl_val_free(value
);
6648 isl_val_free(value
);
6653 /* Bound the given variable of "bset" from below (or above is "upper"
6654 * is set) to "value".
6656 static __isl_give isl_basic_set
*isl_basic_set_bound(
6657 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6658 isl_int value
, int upper
)
6660 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6661 type
, pos
, value
, upper
));
6664 /* Bound the given variable of "bset" from below (or above is "upper"
6665 * is set) to "value".
6667 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6668 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6669 __isl_take isl_val
*value
, int upper
)
6673 if (!isl_val_is_int(value
))
6674 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6675 "expecting integer value", goto error
);
6676 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6677 isl_val_free(value
);
6680 isl_val_free(value
);
6681 isl_basic_set_free(bset
);
6685 /* Bound the given variable of "bset" from below to "value".
6687 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6688 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6689 __isl_take isl_val
*value
)
6691 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6694 /* Bound the given variable of "bset" from above to "value".
6696 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6697 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6698 __isl_take isl_val
*value
)
6700 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6703 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6707 map
= isl_map_cow(map
);
6711 map
->dim
= isl_space_reverse(map
->dim
);
6714 for (i
= 0; i
< map
->n
; ++i
) {
6715 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6719 map
= isl_map_unmark_normalized(map
);
6727 #define TYPE isl_pw_multi_aff
6729 #define SUFFIX _pw_multi_aff
6731 #define EMPTY isl_pw_multi_aff_empty
6733 #define ADD isl_pw_multi_aff_union_add
6734 #include "isl_map_lexopt_templ.c"
6736 /* Given a map "map", compute the lexicographically minimal
6737 * (or maximal) image element for each domain element in dom,
6738 * in the form of an isl_pw_multi_aff.
6739 * If "empty" is not NULL, then set *empty to those elements in dom that
6740 * do not have an image element.
6741 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6742 * should be computed over the domain of "map". "empty" is also NULL
6745 * We first compute the lexicographically minimal or maximal element
6746 * in the first basic map. This results in a partial solution "res"
6747 * and a subset "todo" of dom that still need to be handled.
6748 * We then consider each of the remaining maps in "map" and successively
6749 * update both "res" and "todo".
6750 * If "empty" is NULL, then the todo sets are not needed and therefore
6751 * also not computed.
6753 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6754 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6755 __isl_give isl_set
**empty
, unsigned flags
)
6759 isl_pw_multi_aff
*res
;
6762 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6763 if (!map
|| (!full
&& !dom
))
6766 if (isl_map_plain_is_empty(map
)) {
6771 return isl_pw_multi_aff_from_map(map
);
6774 res
= basic_map_partial_lexopt_pw_multi_aff(
6775 isl_basic_map_copy(map
->p
[0]),
6776 isl_set_copy(dom
), empty
, flags
);
6780 for (i
= 1; i
< map
->n
; ++i
) {
6781 isl_pw_multi_aff
*res_i
;
6783 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6784 isl_basic_map_copy(map
->p
[i
]),
6785 isl_set_copy(dom
), empty
, flags
);
6787 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6788 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6790 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6793 todo
= isl_set_intersect(todo
, *empty
);
6812 #define TYPE isl_map
6816 #define EMPTY isl_map_empty
6818 #define ADD isl_map_union_disjoint
6819 #include "isl_map_lexopt_templ.c"
6821 /* Given a map "map", compute the lexicographically minimal
6822 * (or maximal) image element for each domain element in "dom",
6823 * in the form of an isl_map.
6824 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6825 * do not have an image element.
6826 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6827 * should be computed over the domain of "map". "empty" is also NULL
6830 * If the input consists of more than one disjunct, then first
6831 * compute the desired result in the form of an isl_pw_multi_aff and
6832 * then convert that into an isl_map.
6834 * This function used to have an explicit implementation in terms
6835 * of isl_maps, but it would continually intersect the domains of
6836 * partial results with the complement of the domain of the next
6837 * partial solution, potentially leading to an explosion in the number
6838 * of disjuncts if there are several disjuncts in the input.
6839 * An even earlier implementation of this function would look for
6840 * better results in the domain of the partial result and for extra
6841 * results in the complement of this domain, which would lead to
6842 * even more splintering.
6844 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6845 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6846 __isl_give isl_set
**empty
, unsigned flags
)
6849 struct isl_map
*res
;
6850 isl_pw_multi_aff
*pma
;
6852 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6853 if (!map
|| (!full
&& !dom
))
6856 if (isl_map_plain_is_empty(map
)) {
6865 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6871 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6873 return isl_map_from_pw_multi_aff(pma
);
6882 __isl_give isl_map
*isl_map_partial_lexmax(
6883 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6884 __isl_give isl_set
**empty
)
6886 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6889 __isl_give isl_map
*isl_map_partial_lexmin(
6890 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6891 __isl_give isl_set
**empty
)
6893 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6896 __isl_give isl_set
*isl_set_partial_lexmin(
6897 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6898 __isl_give isl_set
**empty
)
6900 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6904 __isl_give isl_set
*isl_set_partial_lexmax(
6905 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6906 __isl_give isl_set
**empty
)
6908 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6912 /* Compute the lexicographic minimum (or maximum if "flags" includes
6913 * ISL_OPT_MAX) of "bset" over its parametric domain.
6915 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6918 return isl_basic_map_lexopt(bset
, flags
);
6921 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6923 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6926 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6928 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6931 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6933 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6936 /* Compute the lexicographic minimum of "bset" over its parametric domain
6937 * for the purpose of quantifier elimination.
6938 * That is, find an explicit representation for all the existentially
6939 * quantified variables in "bset" by computing their lexicographic
6942 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6943 __isl_take isl_basic_set
*bset
)
6945 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6948 /* Given a basic map with one output dimension, compute the minimum or
6949 * maximum of that dimension as an isl_pw_aff.
6951 * Compute the optimum as a lexicographic optimum over the single
6952 * output dimension and extract the single isl_pw_aff from the result.
6954 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6957 isl_pw_multi_aff
*pma
;
6960 bmap
= isl_basic_map_copy(bmap
);
6961 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6962 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6963 isl_pw_multi_aff_free(pma
);
6968 /* Compute the minimum or maximum of the given output dimension
6969 * as a function of the parameters and the input dimensions,
6970 * but independently of the other output dimensions.
6972 * We first project out the other output dimension and then compute
6973 * the "lexicographic" maximum in each basic map, combining the results
6974 * using isl_pw_aff_union_max.
6976 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6983 n_out
= isl_map_dim(map
, isl_dim_out
);
6984 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6985 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6990 isl_space
*space
= isl_map_get_space(map
);
6992 return isl_pw_aff_empty(space
);
6995 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6996 for (i
= 1; i
< map
->n
; ++i
) {
6997 isl_pw_aff
*pwaff_i
;
6999 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7000 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7008 /* Compute the minimum of the given output dimension as a function of the
7009 * parameters and input dimensions, but independently of
7010 * the other output dimensions.
7012 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7014 return map_dim_opt(map
, pos
, 0);
7017 /* Compute the maximum of the given output dimension as a function of the
7018 * parameters and input dimensions, but independently of
7019 * the other output dimensions.
7021 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7023 return map_dim_opt(map
, pos
, 1);
7026 /* Compute the minimum or maximum of the given set dimension
7027 * as a function of the parameters,
7028 * but independently of the other set dimensions.
7030 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7033 return map_dim_opt(set
, pos
, max
);
7036 /* Compute the maximum of the given set dimension as a function of the
7037 * parameters, but independently of the other set dimensions.
7039 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7041 return set_dim_opt(set
, pos
, 1);
7044 /* Compute the minimum of the given set dimension as a function of the
7045 * parameters, but independently of the other set dimensions.
7047 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7049 return set_dim_opt(set
, pos
, 0);
7052 /* Apply a preimage specified by "mat" on the parameters of "bset".
7053 * bset is assumed to have only parameters and divs.
7055 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7056 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7063 bset
->dim
= isl_space_cow(bset
->dim
);
7067 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7069 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7071 bset
->dim
->nparam
= 0;
7072 bset
->dim
->n_out
= nparam
;
7073 bset
= isl_basic_set_preimage(bset
, mat
);
7075 bset
->dim
->nparam
= bset
->dim
->n_out
;
7076 bset
->dim
->n_out
= 0;
7081 isl_basic_set_free(bset
);
7085 /* Apply a preimage specified by "mat" on the parameters of "set".
7086 * set is assumed to have only parameters and divs.
7088 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7089 __isl_take isl_mat
*mat
)
7097 nparam
= isl_set_dim(set
, isl_dim_param
);
7099 if (mat
->n_row
!= 1 + nparam
)
7100 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7101 "unexpected number of rows", goto error
);
7103 space
= isl_set_get_space(set
);
7104 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7105 isl_dim_param
, 0, nparam
);
7106 set
= isl_set_reset_space(set
, space
);
7107 set
= isl_set_preimage(set
, mat
);
7108 nparam
= isl_set_dim(set
, isl_dim_out
);
7109 space
= isl_set_get_space(set
);
7110 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7111 isl_dim_out
, 0, nparam
);
7112 set
= isl_set_reset_space(set
, space
);
7120 /* Intersect the basic set "bset" with the affine space specified by the
7121 * equalities in "eq".
7123 static __isl_give isl_basic_set
*basic_set_append_equalities(
7124 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7132 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7137 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7138 for (i
= 0; i
< eq
->n_row
; ++i
) {
7139 k
= isl_basic_set_alloc_equality(bset
);
7142 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7143 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7147 bset
= isl_basic_set_gauss(bset
, NULL
);
7148 bset
= isl_basic_set_finalize(bset
);
7153 isl_basic_set_free(bset
);
7157 /* Intersect the set "set" with the affine space specified by the
7158 * equalities in "eq".
7160 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7168 for (i
= 0; i
< set
->n
; ++i
) {
7169 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7182 /* Given a basic set "bset" that only involves parameters and existentially
7183 * quantified variables, return the index of the first equality
7184 * that only involves parameters. If there is no such equality then
7185 * return bset->n_eq.
7187 * This function assumes that isl_basic_set_gauss has been called on "bset".
7189 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7192 unsigned nparam
, n_div
;
7197 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7198 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7200 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7201 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7208 /* Compute an explicit representation for the existentially quantified
7209 * variables in "bset" by computing the "minimal value" of the set
7210 * variables. Since there are no set variables, the computation of
7211 * the minimal value essentially computes an explicit representation
7212 * of the non-empty part(s) of "bset".
7214 * The input only involves parameters and existentially quantified variables.
7215 * All equalities among parameters have been removed.
7217 * Since the existentially quantified variables in the result are in general
7218 * going to be different from those in the input, we first replace
7219 * them by the minimal number of variables based on their equalities.
7220 * This should simplify the parametric integer programming.
7222 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7224 isl_morph
*morph1
, *morph2
;
7230 if (bset
->n_eq
== 0)
7231 return isl_basic_set_lexmin_compute_divs(bset
);
7233 morph1
= isl_basic_set_parameter_compression(bset
);
7234 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7235 bset
= isl_basic_set_lift(bset
);
7236 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7237 bset
= isl_morph_basic_set(morph2
, bset
);
7238 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7239 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7241 set
= isl_basic_set_lexmin_compute_divs(bset
);
7243 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7248 /* Project the given basic set onto its parameter domain, possibly introducing
7249 * new, explicit, existential variables in the constraints.
7250 * The input has parameters and (possibly implicit) existential variables.
7251 * The output has the same parameters, but only
7252 * explicit existentially quantified variables.
7254 * The actual projection is performed by pip, but pip doesn't seem
7255 * to like equalities very much, so we first remove the equalities
7256 * among the parameters by performing a variable compression on
7257 * the parameters. Afterward, an inverse transformation is performed
7258 * and the equalities among the parameters are inserted back in.
7260 * The variable compression on the parameters may uncover additional
7261 * equalities that were only implicit before. We therefore check
7262 * if there are any new parameter equalities in the result and
7263 * if so recurse. The removal of parameter equalities is required
7264 * for the parameter compression performed by base_compute_divs.
7266 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7270 struct isl_mat
*T
, *T2
;
7271 struct isl_set
*set
;
7274 bset
= isl_basic_set_cow(bset
);
7278 if (bset
->n_eq
== 0)
7279 return base_compute_divs(bset
);
7281 bset
= isl_basic_set_gauss(bset
, NULL
);
7284 if (isl_basic_set_plain_is_empty(bset
))
7285 return isl_set_from_basic_set(bset
);
7287 i
= first_parameter_equality(bset
);
7288 if (i
== bset
->n_eq
)
7289 return base_compute_divs(bset
);
7291 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7292 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7294 eq
= isl_mat_cow(eq
);
7295 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7296 if (T
&& T
->n_col
== 0) {
7300 bset
= isl_basic_set_set_to_empty(bset
);
7301 return isl_set_from_basic_set(bset
);
7303 bset
= basic_set_parameter_preimage(bset
, T
);
7305 i
= first_parameter_equality(bset
);
7308 else if (i
== bset
->n_eq
)
7309 set
= base_compute_divs(bset
);
7311 set
= parameter_compute_divs(bset
);
7312 set
= set_parameter_preimage(set
, T2
);
7313 set
= set_append_equalities(set
, eq
);
7317 /* Insert the divs from "ls" before those of "bmap".
7319 * The number of columns is not changed, which means that the last
7320 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7321 * The caller is responsible for removing the same number of dimensions
7322 * from the space of "bmap".
7324 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7325 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7331 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7335 old_n_div
= bmap
->n_div
;
7336 bmap
= insert_div_rows(bmap
, n_div
);
7340 for (i
= 0; i
< n_div
; ++i
) {
7341 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7342 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7348 /* Replace the space of "bmap" by the space and divs of "ls".
7350 * If "ls" has any divs, then we simplify the result since we may
7351 * have discovered some additional equalities that could simplify
7352 * the div expressions.
7354 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7355 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7359 bmap
= isl_basic_map_cow(bmap
);
7363 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7364 bmap
= insert_divs_from_local_space(bmap
, ls
);
7368 isl_space_free(bmap
->dim
);
7369 bmap
->dim
= isl_local_space_get_space(ls
);
7373 isl_local_space_free(ls
);
7375 bmap
= isl_basic_map_simplify(bmap
);
7376 bmap
= isl_basic_map_finalize(bmap
);
7379 isl_basic_map_free(bmap
);
7380 isl_local_space_free(ls
);
7384 /* Replace the space of "map" by the space and divs of "ls".
7386 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7387 __isl_take isl_local_space
*ls
)
7391 map
= isl_map_cow(map
);
7395 for (i
= 0; i
< map
->n
; ++i
) {
7396 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7397 isl_local_space_copy(ls
));
7401 isl_space_free(map
->dim
);
7402 map
->dim
= isl_local_space_get_space(ls
);
7406 isl_local_space_free(ls
);
7409 isl_local_space_free(ls
);
7414 /* Compute an explicit representation for the existentially
7415 * quantified variables for which do not know any explicit representation yet.
7417 * We first sort the existentially quantified variables so that the
7418 * existentially quantified variables for which we already have an explicit
7419 * representation are placed before those for which we do not.
7420 * The input dimensions, the output dimensions and the existentially
7421 * quantified variables for which we already have an explicit
7422 * representation are then turned into parameters.
7423 * compute_divs returns a map with the same parameters and
7424 * no input or output dimensions and the dimension specification
7425 * is reset to that of the input, including the existentially quantified
7426 * variables for which we already had an explicit representation.
7428 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7430 struct isl_basic_set
*bset
;
7431 struct isl_set
*set
;
7432 struct isl_map
*map
;
7434 isl_local_space
*ls
;
7441 bmap
= isl_basic_map_sort_divs(bmap
);
7442 bmap
= isl_basic_map_cow(bmap
);
7446 n_known
= isl_basic_map_first_unknown_div(bmap
);
7448 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7450 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7451 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7452 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7453 space
= isl_space_set_alloc(bmap
->ctx
,
7454 nparam
+ n_in
+ n_out
+ n_known
, 0);
7458 ls
= isl_basic_map_get_local_space(bmap
);
7459 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7460 n_known
, bmap
->n_div
- n_known
);
7462 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7463 swap_div(bmap
, i
- n_known
, i
);
7464 bmap
->n_div
-= n_known
;
7465 bmap
->extra
-= n_known
;
7467 bmap
= isl_basic_map_reset_space(bmap
, space
);
7468 bset
= bset_from_bmap(bmap
);
7470 set
= parameter_compute_divs(bset
);
7471 map
= set_to_map(set
);
7472 map
= replace_space_by_local_space(map
, ls
);
7476 isl_basic_map_free(bmap
);
7480 /* Remove the explicit representation of local variable "div",
7483 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7484 __isl_take isl_basic_map
*bmap
, int div
)
7488 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7490 return isl_basic_map_free(bmap
);
7494 bmap
= isl_basic_map_cow(bmap
);
7497 isl_int_set_si(bmap
->div
[div
][0], 0);
7501 /* Is local variable "div" of "bmap" marked as not having an explicit
7503 * Note that even if "div" is not marked in this way and therefore
7504 * has an explicit representation, this representation may still
7505 * depend (indirectly) on other local variables that do not
7506 * have an explicit representation.
7508 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7511 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7512 return isl_bool_error
;
7513 return isl_int_is_zero(bmap
->div
[div
][0]);
7516 /* Return the position of the first local variable that does not
7517 * have an explicit representation.
7518 * Return the total number of local variables if they all have
7519 * an explicit representation.
7520 * Return -1 on error.
7522 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7529 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7530 if (!isl_basic_map_div_is_known(bmap
, i
))
7536 /* Return the position of the first local variable that does not
7537 * have an explicit representation.
7538 * Return the total number of local variables if they all have
7539 * an explicit representation.
7540 * Return -1 on error.
7542 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7544 return isl_basic_map_first_unknown_div(bset
);
7547 /* Does "bmap" have an explicit representation for all local variables?
7549 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7553 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7554 first
= isl_basic_map_first_unknown_div(bmap
);
7556 return isl_bool_error
;
7560 /* Do all basic maps in "map" have an explicit representation
7561 * for all local variables?
7563 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7568 return isl_bool_error
;
7570 for (i
= 0; i
< map
->n
; ++i
) {
7571 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7576 return isl_bool_true
;
7579 /* If bmap contains any unknown divs, then compute explicit
7580 * expressions for them. However, this computation may be
7581 * quite expensive, so first try to remove divs that aren't
7584 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7587 struct isl_map
*map
;
7589 known
= isl_basic_map_divs_known(bmap
);
7593 return isl_map_from_basic_map(bmap
);
7595 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7597 known
= isl_basic_map_divs_known(bmap
);
7601 return isl_map_from_basic_map(bmap
);
7603 map
= compute_divs(bmap
);
7606 isl_basic_map_free(bmap
);
7610 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7614 struct isl_map
*res
;
7621 known
= isl_map_divs_known(map
);
7629 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7630 for (i
= 1 ; i
< map
->n
; ++i
) {
7632 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7633 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7634 res
= isl_map_union_disjoint(res
, r2
);
7636 res
= isl_map_union(res
, r2
);
7643 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7645 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7648 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7650 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7653 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7656 struct isl_set
*set
;
7661 map
= isl_map_cow(map
);
7665 set
= set_from_map(map
);
7666 set
->dim
= isl_space_domain(set
->dim
);
7669 for (i
= 0; i
< map
->n
; ++i
) {
7670 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7674 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7675 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7682 /* Return the union of "map1" and "map2", where we assume for now that
7683 * "map1" and "map2" are disjoint. Note that the basic maps inside
7684 * "map1" or "map2" may not be disjoint from each other.
7685 * Also note that this function is also called from isl_map_union,
7686 * which takes care of handling the situation where "map1" and "map2"
7687 * may not be disjoint.
7689 * If one of the inputs is empty, we can simply return the other input.
7690 * Similarly, if one of the inputs is universal, then it is equal to the union.
7692 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7693 __isl_take isl_map
*map2
)
7697 struct isl_map
*map
= NULL
;
7703 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7704 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7705 "spaces don't match", goto error
);
7716 is_universe
= isl_map_plain_is_universe(map1
);
7717 if (is_universe
< 0)
7724 is_universe
= isl_map_plain_is_universe(map2
);
7725 if (is_universe
< 0)
7732 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7733 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7734 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7736 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7737 map1
->n
+ map2
->n
, flags
);
7740 for (i
= 0; i
< map1
->n
; ++i
) {
7741 map
= isl_map_add_basic_map(map
,
7742 isl_basic_map_copy(map1
->p
[i
]));
7746 for (i
= 0; i
< map2
->n
; ++i
) {
7747 map
= isl_map_add_basic_map(map
,
7748 isl_basic_map_copy(map2
->p
[i
]));
7762 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7763 * guaranteed to be disjoint by the caller.
7765 * Note that this functions is called from within isl_map_make_disjoint,
7766 * so we have to be careful not to touch the constraints of the inputs
7769 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7770 __isl_take isl_map
*map2
)
7772 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7775 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7776 * not be disjoint. The parameters are assumed to have been aligned.
7778 * We currently simply call map_union_disjoint, the internal operation
7779 * of which does not really depend on the inputs being disjoint.
7780 * If the result contains more than one basic map, then we clear
7781 * the disjoint flag since the result may contain basic maps from
7782 * both inputs and these are not guaranteed to be disjoint.
7784 * As a special case, if "map1" and "map2" are obviously equal,
7785 * then we simply return "map1".
7787 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7788 __isl_take isl_map
*map2
)
7795 equal
= isl_map_plain_is_equal(map1
, map2
);
7803 map1
= map_union_disjoint(map1
, map2
);
7807 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7815 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7818 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7819 __isl_take isl_map
*map2
)
7821 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7824 __isl_give isl_set
*isl_set_union_disjoint(
7825 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7827 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7831 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7833 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7836 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7839 * "map" and "set" are assumed to be compatible and non-NULL.
7841 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7842 __isl_take isl_set
*set
,
7843 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7844 __isl_take isl_basic_set
*bset
))
7847 struct isl_map
*result
;
7850 if (isl_set_plain_is_universe(set
)) {
7855 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7856 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7857 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7859 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7860 map
->n
* set
->n
, flags
);
7861 for (i
= 0; result
&& i
< map
->n
; ++i
)
7862 for (j
= 0; j
< set
->n
; ++j
) {
7863 result
= isl_map_add_basic_map(result
,
7864 fn(isl_basic_map_copy(map
->p
[i
]),
7865 isl_basic_set_copy(set
->p
[j
])));
7875 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7876 __isl_take isl_set
*set
)
7880 ok
= isl_map_compatible_range(map
, set
);
7884 isl_die(set
->ctx
, isl_error_invalid
,
7885 "incompatible spaces", goto error
);
7887 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7894 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7895 __isl_take isl_set
*set
)
7897 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7900 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7901 __isl_take isl_set
*set
)
7905 ok
= isl_map_compatible_domain(map
, set
);
7909 isl_die(set
->ctx
, isl_error_invalid
,
7910 "incompatible spaces", goto error
);
7912 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7919 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7920 __isl_take isl_set
*set
)
7922 return isl_map_align_params_map_map_and(map
, set
,
7923 &map_intersect_domain
);
7926 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7927 * in the space B -> C, return the intersection.
7928 * The parameters are assumed to have been aligned.
7930 * The map "factor" is first extended to a map living in the space
7931 * [A -> B] -> C and then a regular intersection is computed.
7933 static __isl_give isl_map
*map_intersect_domain_factor_range(
7934 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7937 isl_map
*ext_factor
;
7939 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7940 ext_factor
= isl_map_universe(space
);
7941 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7942 return map_intersect(map
, ext_factor
);
7945 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7946 * in the space B -> C, return the intersection.
7948 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7949 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7951 return isl_map_align_params_map_map_and(map
, factor
,
7952 &map_intersect_domain_factor_range
);
7955 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7956 * in the space A -> C, return the intersection.
7958 * The map "factor" is first extended to a map living in the space
7959 * A -> [B -> C] and then a regular intersection is computed.
7961 static __isl_give isl_map
*map_intersect_range_factor_range(
7962 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7965 isl_map
*ext_factor
;
7967 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7968 ext_factor
= isl_map_universe(space
);
7969 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7970 return isl_map_intersect(map
, ext_factor
);
7973 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7974 * in the space A -> C, return the intersection.
7976 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7977 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7979 return isl_map_align_params_map_map_and(map
, factor
,
7980 &map_intersect_range_factor_range
);
7983 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7984 __isl_take isl_map
*map2
)
7988 map1
= isl_map_reverse(map1
);
7989 map1
= isl_map_apply_range(map1
, map2
);
7990 return isl_map_reverse(map1
);
7997 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7998 __isl_take isl_map
*map2
)
8000 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
8003 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
8004 __isl_take isl_map
*map2
)
8007 struct isl_map
*result
;
8013 space
= isl_space_join(isl_space_copy(map1
->dim
),
8014 isl_space_copy(map2
->dim
));
8016 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8019 for (i
= 0; i
< map1
->n
; ++i
)
8020 for (j
= 0; j
< map2
->n
; ++j
) {
8021 result
= isl_map_add_basic_map(result
,
8022 isl_basic_map_apply_range(
8023 isl_basic_map_copy(map1
->p
[i
]),
8024 isl_basic_map_copy(map2
->p
[j
])));
8030 if (result
&& result
->n
<= 1)
8031 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8039 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8040 __isl_take isl_map
*map2
)
8042 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8046 * returns range - domain
8048 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8050 isl_space
*target_space
;
8051 struct isl_basic_set
*bset
;
8058 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8059 bmap
->dim
, isl_dim_out
),
8061 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8062 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8063 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8064 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8065 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8066 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8067 for (i
= 0; i
< dim
; ++i
) {
8068 int j
= isl_basic_map_alloc_equality(bmap
);
8070 bmap
= isl_basic_map_free(bmap
);
8073 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8074 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8075 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8076 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8078 bset
= isl_basic_map_domain(bmap
);
8079 bset
= isl_basic_set_reset_space(bset
, target_space
);
8082 isl_basic_map_free(bmap
);
8087 * returns range - domain
8089 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8093 struct isl_set
*result
;
8098 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8099 map
->dim
, isl_dim_out
),
8101 dim
= isl_map_get_space(map
);
8102 dim
= isl_space_domain(dim
);
8103 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8106 for (i
= 0; i
< map
->n
; ++i
)
8107 result
= isl_set_add_basic_set(result
,
8108 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8117 * returns [domain -> range] -> range - domain
8119 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8120 __isl_take isl_basic_map
*bmap
)
8124 isl_basic_map
*domain
;
8128 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8129 bmap
->dim
, isl_dim_out
))
8130 isl_die(bmap
->ctx
, isl_error_invalid
,
8131 "domain and range don't match", goto error
);
8133 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8134 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8136 space
= isl_basic_map_get_space(bmap
);
8137 space
= isl_space_from_range(isl_space_domain(space
));
8138 domain
= isl_basic_map_universe(space
);
8140 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8141 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8142 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8144 total
= isl_basic_map_total_dim(bmap
);
8146 for (i
= 0; i
< n
; ++i
) {
8147 k
= isl_basic_map_alloc_equality(bmap
);
8150 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8151 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8152 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8153 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8156 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8157 return isl_basic_map_finalize(bmap
);
8159 isl_basic_map_free(bmap
);
8164 * returns [domain -> range] -> range - domain
8166 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8169 isl_space
*domain_space
;
8174 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8175 map
->dim
, isl_dim_out
))
8176 isl_die(map
->ctx
, isl_error_invalid
,
8177 "domain and range don't match", goto error
);
8179 map
= isl_map_cow(map
);
8183 domain_space
= isl_space_domain(isl_map_get_space(map
));
8184 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8185 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8188 for (i
= 0; i
< map
->n
; ++i
) {
8189 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8193 map
= isl_map_unmark_normalized(map
);
8200 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8202 unsigned n_in
, n_out
;
8206 n_in
= isl_space_dim(space
, isl_dim_in
);
8207 n_out
= isl_space_dim(space
, isl_dim_out
);
8209 isl_die(space
->ctx
, isl_error_invalid
,
8210 "number of input and output dimensions needs to be "
8211 "the same", goto error
);
8212 return isl_basic_map_equal(space
, n_in
);
8214 isl_space_free(space
);
8218 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8220 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8223 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8225 isl_space
*dim
= isl_set_get_space(set
);
8227 id
= isl_map_identity(isl_space_map_from_set(dim
));
8228 return isl_map_intersect_range(id
, set
);
8231 /* Construct a basic set with all set dimensions having only non-negative
8234 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8235 __isl_take isl_space
*space
)
8240 struct isl_basic_set
*bset
;
8244 nparam
= space
->nparam
;
8246 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8249 for (i
= 0; i
< dim
; ++i
) {
8250 int k
= isl_basic_set_alloc_inequality(bset
);
8253 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8254 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8258 isl_basic_set_free(bset
);
8262 /* Construct the half-space x_pos >= 0.
8264 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8268 isl_basic_set
*nonneg
;
8270 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8271 k
= isl_basic_set_alloc_inequality(nonneg
);
8274 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8275 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8277 return isl_basic_set_finalize(nonneg
);
8279 isl_basic_set_free(nonneg
);
8283 /* Construct the half-space x_pos <= -1.
8285 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8291 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8292 k
= isl_basic_set_alloc_inequality(neg
);
8295 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8296 isl_int_set_si(neg
->ineq
[k
][0], -1);
8297 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8299 return isl_basic_set_finalize(neg
);
8301 isl_basic_set_free(neg
);
8305 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8306 enum isl_dim_type type
, unsigned first
, unsigned n
)
8310 isl_basic_set
*nonneg
;
8318 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8320 offset
= pos(set
->dim
, type
);
8321 for (i
= 0; i
< n
; ++i
) {
8322 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8323 offset
+ first
+ i
);
8324 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8326 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8335 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8337 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8343 return isl_stat_error
;
8344 if (isl_set_plain_is_empty(set
)) {
8349 return fn(set
, signs
, user
);
8352 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8354 half
= isl_set_intersect(half
, isl_set_copy(set
));
8355 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8359 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8361 half
= isl_set_intersect(half
, set
);
8362 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8365 return isl_stat_error
;
8368 /* Call "fn" on the intersections of "set" with each of the orthants
8369 * (except for obviously empty intersections). The orthant is identified
8370 * by the signs array, with each entry having value 1 or -1 according
8371 * to the sign of the corresponding variable.
8373 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8374 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8383 return isl_stat_error
;
8384 if (isl_set_plain_is_empty(set
))
8387 nparam
= isl_set_dim(set
, isl_dim_param
);
8388 nvar
= isl_set_dim(set
, isl_dim_set
);
8390 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8392 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8400 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8402 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8405 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8406 __isl_keep isl_basic_map
*bmap2
)
8409 struct isl_map
*map1
;
8410 struct isl_map
*map2
;
8412 if (!bmap1
|| !bmap2
)
8413 return isl_bool_error
;
8415 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8416 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8418 is_subset
= isl_map_is_subset(map1
, map2
);
8426 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8427 __isl_keep isl_basic_set
*bset2
)
8429 return isl_basic_map_is_subset(bset1
, bset2
);
8432 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8433 __isl_keep isl_basic_map
*bmap2
)
8437 if (!bmap1
|| !bmap2
)
8438 return isl_bool_error
;
8439 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8440 if (is_subset
!= isl_bool_true
)
8442 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8446 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8447 __isl_keep isl_basic_set
*bset2
)
8449 return isl_basic_map_is_equal(
8450 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8453 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8459 return isl_bool_error
;
8460 for (i
= 0; i
< map
->n
; ++i
) {
8461 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8463 return isl_bool_error
;
8465 return isl_bool_false
;
8467 return isl_bool_true
;
8470 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8472 return map
? map
->n
== 0 : isl_bool_error
;
8475 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8477 return set
? set
->n
== 0 : isl_bool_error
;
8480 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8482 return isl_map_is_empty(set_to_map(set
));
8485 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8486 __isl_keep isl_map
*map2
)
8489 return isl_bool_error
;
8491 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8494 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8495 __isl_keep isl_set
*set2
)
8498 return isl_bool_error
;
8500 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8503 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8508 return isl_bool_error
;
8509 is_subset
= isl_map_is_subset(map1
, map2
);
8510 if (is_subset
!= isl_bool_true
)
8512 is_subset
= isl_map_is_subset(map2
, map1
);
8516 /* Is "map1" equal to "map2"?
8518 * First check if they are obviously equal.
8519 * If not, then perform a more detailed analysis.
8521 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8525 equal
= isl_map_plain_is_equal(map1
, map2
);
8526 if (equal
< 0 || equal
)
8528 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8531 isl_bool
isl_basic_map_is_strict_subset(
8532 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8536 if (!bmap1
|| !bmap2
)
8537 return isl_bool_error
;
8538 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8539 if (is_subset
!= isl_bool_true
)
8541 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8542 return isl_bool_not(is_subset
);
8545 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8546 __isl_keep isl_map
*map2
)
8551 return isl_bool_error
;
8552 is_subset
= isl_map_is_subset(map1
, map2
);
8553 if (is_subset
!= isl_bool_true
)
8555 is_subset
= isl_map_is_subset(map2
, map1
);
8556 return isl_bool_not(is_subset
);
8559 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8560 __isl_keep isl_set
*set2
)
8562 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8565 /* Is "bmap" obviously equal to the universe with the same space?
8567 * That is, does it not have any constraints?
8569 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8572 return isl_bool_error
;
8573 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8576 /* Is "bset" obviously equal to the universe with the same space?
8578 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8580 return isl_basic_map_plain_is_universe(bset
);
8583 /* If "c" does not involve any existentially quantified variables,
8584 * then set *univ to false and abort
8586 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8588 isl_bool
*univ
= user
;
8591 n
= isl_constraint_dim(c
, isl_dim_div
);
8592 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8593 isl_constraint_free(c
);
8594 if (*univ
< 0 || !*univ
)
8595 return isl_stat_error
;
8599 /* Is "bmap" equal to the universe with the same space?
8601 * First check if it is obviously equal to the universe.
8602 * If not and if there are any constraints not involving
8603 * existentially quantified variables, then it is certainly
8604 * not equal to the universe.
8605 * Otherwise, check if the universe is a subset of "bmap".
8607 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8610 isl_basic_map
*test
;
8612 univ
= isl_basic_map_plain_is_universe(bmap
);
8613 if (univ
< 0 || univ
)
8615 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8616 return isl_bool_false
;
8617 univ
= isl_bool_true
;
8618 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8620 return isl_bool_error
;
8621 if (univ
< 0 || !univ
)
8623 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8624 univ
= isl_basic_map_is_subset(test
, bmap
);
8625 isl_basic_map_free(test
);
8629 /* Is "bset" equal to the universe with the same space?
8631 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8633 return isl_basic_map_is_universe(bset
);
8636 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8641 return isl_bool_error
;
8643 for (i
= 0; i
< map
->n
; ++i
) {
8644 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8649 return isl_bool_false
;
8652 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8654 return isl_map_plain_is_universe(set_to_map(set
));
8657 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8659 struct isl_basic_set
*bset
= NULL
;
8660 struct isl_vec
*sample
= NULL
;
8661 isl_bool empty
, non_empty
;
8664 return isl_bool_error
;
8666 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8667 return isl_bool_true
;
8669 if (isl_basic_map_plain_is_universe(bmap
))
8670 return isl_bool_false
;
8672 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8673 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8674 copy
= isl_basic_map_remove_redundancies(copy
);
8675 empty
= isl_basic_map_plain_is_empty(copy
);
8676 isl_basic_map_free(copy
);
8680 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8682 return isl_bool_error
;
8684 return isl_bool_false
;
8685 isl_vec_free(bmap
->sample
);
8686 bmap
->sample
= NULL
;
8687 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8689 return isl_bool_error
;
8690 sample
= isl_basic_set_sample_vec(bset
);
8692 return isl_bool_error
;
8693 empty
= sample
->size
== 0;
8694 isl_vec_free(bmap
->sample
);
8695 bmap
->sample
= sample
;
8697 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8702 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8705 return isl_bool_error
;
8706 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8709 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8712 return isl_bool_error
;
8713 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8716 /* Is "bmap" known to be non-empty?
8718 * That is, is the cached sample still valid?
8720 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8725 return isl_bool_error
;
8727 return isl_bool_false
;
8728 total
= 1 + isl_basic_map_total_dim(bmap
);
8729 if (bmap
->sample
->size
!= total
)
8730 return isl_bool_false
;
8731 return isl_basic_map_contains(bmap
, bmap
->sample
);
8734 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8736 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8739 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8740 __isl_take isl_basic_map
*bmap2
)
8742 struct isl_map
*map
;
8743 if (!bmap1
|| !bmap2
)
8746 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8748 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8751 map
= isl_map_add_basic_map(map
, bmap1
);
8752 map
= isl_map_add_basic_map(map
, bmap2
);
8755 isl_basic_map_free(bmap1
);
8756 isl_basic_map_free(bmap2
);
8760 struct isl_set
*isl_basic_set_union(
8761 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8763 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8764 bset_to_bmap(bset2
)));
8767 /* Order divs such that any div only depends on previous divs */
8768 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8769 __isl_take isl_basic_map
*bmap
)
8777 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8779 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8781 if (isl_int_is_zero(bmap
->div
[i
][0]))
8783 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8788 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8789 "integer division depends on itself",
8790 return isl_basic_map_free(bmap
));
8791 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8797 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8799 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8802 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8809 for (i
= 0; i
< map
->n
; ++i
) {
8810 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8821 /* Sort the local variables of "bset".
8823 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8824 __isl_take isl_basic_set
*bset
)
8826 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8829 /* Apply the expansion computed by isl_merge_divs.
8830 * The expansion itself is given by "exp" while the resulting
8831 * list of divs is given by "div".
8833 * Move the integer divisions of "bmap" into the right position
8834 * according to "exp" and then introduce the additional integer
8835 * divisions, adding div constraints.
8836 * The moving should be done first to avoid moving coefficients
8837 * in the definitions of the extra integer divisions.
8839 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8840 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8845 bmap
= isl_basic_map_cow(bmap
);
8849 if (div
->n_row
< bmap
->n_div
)
8850 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8851 "not an expansion", goto error
);
8853 n_div
= bmap
->n_div
;
8854 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8855 div
->n_row
- n_div
, 0,
8856 2 * (div
->n_row
- n_div
));
8858 for (i
= n_div
; i
< div
->n_row
; ++i
)
8859 if (isl_basic_map_alloc_div(bmap
) < 0)
8862 for (j
= n_div
- 1; j
>= 0; --j
) {
8865 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8868 for (i
= 0; i
< div
->n_row
; ++i
) {
8869 if (j
< n_div
&& exp
[j
] == i
) {
8872 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8873 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8875 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8883 isl_basic_map_free(bmap
);
8888 /* Apply the expansion computed by isl_merge_divs.
8889 * The expansion itself is given by "exp" while the resulting
8890 * list of divs is given by "div".
8892 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8893 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8895 return isl_basic_map_expand_divs(bset
, div
, exp
);
8898 /* Look for a div in dst that corresponds to the div "div" in src.
8899 * The divs before "div" in src and dst are assumed to be the same.
8901 * Returns -1 if no corresponding div was found and the position
8902 * of the corresponding div in dst otherwise.
8904 static int find_div(__isl_keep isl_basic_map
*dst
,
8905 __isl_keep isl_basic_map
*src
, unsigned div
)
8909 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8911 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8912 for (i
= div
; i
< dst
->n_div
; ++i
)
8913 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8914 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8915 dst
->n_div
- div
) == -1)
8920 /* Align the divs of "dst" to those of "src", adding divs from "src"
8921 * if needed. That is, make sure that the first src->n_div divs
8922 * of the result are equal to those of src.
8924 * The result is not finalized as by design it will have redundant
8925 * divs if any divs from "src" were copied.
8927 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8928 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8936 return isl_basic_map_free(dst
);
8938 if (src
->n_div
== 0)
8941 known
= isl_basic_map_divs_known(src
);
8943 return isl_basic_map_free(dst
);
8945 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8946 "some src divs are unknown",
8947 return isl_basic_map_free(dst
));
8949 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8951 return isl_basic_map_free(dst
);
8954 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8955 for (i
= 0; i
< src
->n_div
; ++i
) {
8956 int j
= find_div(dst
, src
, i
);
8959 int extra
= src
->n_div
- i
;
8960 dst
= isl_basic_map_cow(dst
);
8963 dst
= isl_basic_map_extend_space(dst
,
8964 isl_space_copy(dst
->dim
),
8965 extra
, 0, 2 * extra
);
8968 j
= isl_basic_map_alloc_div(dst
);
8971 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8972 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8973 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8977 isl_basic_map_swap_div(dst
, i
, j
);
8979 isl_basic_map_free(src
);
8982 isl_basic_map_free(src
);
8983 isl_basic_map_free(dst
);
8987 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8995 map
= isl_map_compute_divs(map
);
8996 map
= isl_map_cow(map
);
9000 for (i
= 1; i
< map
->n
; ++i
)
9001 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9002 for (i
= 1; i
< map
->n
; ++i
) {
9003 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9005 return isl_map_free(map
);
9008 map
= isl_map_unmark_normalized(map
);
9012 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9014 return isl_map_align_divs_internal(map
);
9017 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
9019 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9022 /* Align the divs of the basic maps in "map" to those
9023 * of the basic maps in "list", as well as to the other basic maps in "map".
9024 * The elements in "list" are assumed to have known divs.
9026 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9027 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9031 map
= isl_map_compute_divs(map
);
9032 map
= isl_map_cow(map
);
9034 return isl_map_free(map
);
9038 n
= isl_basic_map_list_n_basic_map(list
);
9039 for (i
= 0; i
< n
; ++i
) {
9040 isl_basic_map
*bmap
;
9042 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9043 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9044 isl_basic_map_free(bmap
);
9047 return isl_map_free(map
);
9049 return isl_map_align_divs_internal(map
);
9052 /* Align the divs of each element of "list" to those of "bmap".
9053 * Both "bmap" and the elements of "list" are assumed to have known divs.
9055 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9056 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9061 return isl_basic_map_list_free(list
);
9063 n
= isl_basic_map_list_n_basic_map(list
);
9064 for (i
= 0; i
< n
; ++i
) {
9065 isl_basic_map
*bmap_i
;
9067 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9068 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9069 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9075 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9076 __isl_take isl_map
*map
)
9080 ok
= isl_map_compatible_domain(map
, set
);
9084 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9085 "incompatible spaces", goto error
);
9086 map
= isl_map_intersect_domain(map
, set
);
9087 set
= isl_map_range(map
);
9095 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9096 __isl_take isl_map
*map
)
9098 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9101 /* There is no need to cow as removing empty parts doesn't change
9102 * the meaning of the set.
9104 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9111 for (i
= map
->n
- 1; i
>= 0; --i
)
9112 map
= remove_if_empty(map
, i
);
9117 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9119 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9122 /* Create a binary relation that maps the shared initial "pos" dimensions
9123 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9125 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9126 __isl_keep isl_basic_set
*bset2
, int pos
)
9128 isl_basic_map
*bmap1
;
9129 isl_basic_map
*bmap2
;
9131 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9132 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9133 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9134 isl_dim_out
, 0, pos
);
9135 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9136 isl_dim_out
, 0, pos
);
9137 return isl_basic_map_range_product(bmap1
, bmap2
);
9140 /* Given two basic sets bset1 and bset2, compute the maximal difference
9141 * between the values of dimension pos in bset1 and those in bset2
9142 * for any common value of the parameters and dimensions preceding pos.
9144 static enum isl_lp_result
basic_set_maximal_difference_at(
9145 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9146 int pos
, isl_int
*opt
)
9148 isl_basic_map
*bmap1
;
9149 struct isl_ctx
*ctx
;
9150 struct isl_vec
*obj
;
9154 enum isl_lp_result res
;
9156 if (!bset1
|| !bset2
)
9157 return isl_lp_error
;
9159 nparam
= isl_basic_set_n_param(bset1
);
9160 dim1
= isl_basic_set_n_dim(bset1
);
9162 bmap1
= join_initial(bset1
, bset2
, pos
);
9164 return isl_lp_error
;
9166 total
= isl_basic_map_total_dim(bmap1
);
9168 obj
= isl_vec_alloc(ctx
, 1 + total
);
9171 isl_seq_clr(obj
->block
.data
, 1 + total
);
9172 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9173 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9174 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9176 isl_basic_map_free(bmap1
);
9180 isl_basic_map_free(bmap1
);
9181 return isl_lp_error
;
9184 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9185 * for any common value of the parameters and dimensions preceding pos
9186 * in both basic sets, the values of dimension pos in bset1 are
9187 * smaller or larger than those in bset2.
9190 * 1 if bset1 follows bset2
9191 * -1 if bset1 precedes bset2
9192 * 0 if bset1 and bset2 are incomparable
9193 * -2 if some error occurred.
9195 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9196 __isl_keep isl_basic_set
*bset2
, int pos
)
9199 enum isl_lp_result res
;
9204 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9206 if (res
== isl_lp_empty
)
9208 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9209 res
== isl_lp_unbounded
)
9211 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9220 /* Given two basic sets bset1 and bset2, check whether
9221 * for any common value of the parameters and dimensions preceding pos
9222 * there is a value of dimension pos in bset1 that is larger
9223 * than a value of the same dimension in bset2.
9226 * 1 if there exists such a pair
9227 * 0 if there is no such pair, but there is a pair of equal values
9229 * -2 if some error occurred.
9231 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9232 __isl_keep isl_basic_set
*bset2
, int pos
)
9235 isl_basic_map
*bmap
;
9238 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9239 bmap
= join_initial(bset1
, bset2
, pos
);
9240 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9241 isl_dim_out
, dim1
- pos
);
9242 empty
= isl_basic_map_is_empty(bmap
);
9246 isl_basic_map_free(bmap
);
9249 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9250 isl_dim_out
, dim1
- pos
);
9251 empty
= isl_basic_map_is_empty(bmap
);
9254 isl_basic_map_free(bmap
);
9259 isl_basic_map_free(bmap
);
9263 /* Given two sets set1 and set2, check whether
9264 * for any common value of the parameters and dimensions preceding pos
9265 * there is a value of dimension pos in set1 that is larger
9266 * than a value of the same dimension in set2.
9269 * 1 if there exists such a pair
9270 * 0 if there is no such pair, but there is a pair of equal values
9272 * -2 if some error occurred.
9274 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9275 __isl_keep isl_set
*set2
, int pos
)
9283 for (i
= 0; i
< set1
->n
; ++i
)
9284 for (j
= 0; j
< set2
->n
; ++j
) {
9286 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9287 if (f
== 1 || f
== -2)
9296 static isl_bool
isl_basic_map_plain_has_fixed_var(
9297 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9304 return isl_bool_error
;
9305 total
= isl_basic_map_total_dim(bmap
);
9306 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9307 for (; d
+1 > pos
; --d
)
9308 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9312 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9313 return isl_bool_false
;
9314 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9315 return isl_bool_false
;
9316 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9317 return isl_bool_false
;
9319 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9320 return isl_bool_true
;
9322 return isl_bool_false
;
9325 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9326 unsigned pos
, isl_int
*val
)
9334 return isl_bool_error
;
9336 return isl_bool_false
;
9338 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9341 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9342 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9343 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9344 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9345 fixed
= isl_bool_false
;
9348 isl_int_set(*val
, v
);
9354 static isl_bool
isl_basic_set_plain_has_fixed_var(
9355 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9357 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9361 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9362 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9364 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9365 return isl_bool_error
;
9366 return isl_basic_map_plain_has_fixed_var(bmap
,
9367 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9370 /* If "bmap" obviously lies on a hyperplane where the given dimension
9371 * has a fixed value, then return that value.
9372 * Otherwise return NaN.
9374 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9375 __isl_keep isl_basic_map
*bmap
,
9376 enum isl_dim_type type
, unsigned pos
)
9384 ctx
= isl_basic_map_get_ctx(bmap
);
9385 v
= isl_val_alloc(ctx
);
9388 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9390 return isl_val_free(v
);
9392 isl_int_set_si(v
->d
, 1);
9396 return isl_val_nan(ctx
);
9399 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9400 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9402 if (pos
>= isl_map_dim(map
, type
))
9403 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9404 "position out of bounds", return isl_bool_error
);
9405 return isl_map_plain_has_fixed_var(map
,
9406 map_offset(map
, type
) - 1 + pos
, val
);
9409 /* If "map" obviously lies on a hyperplane where the given dimension
9410 * has a fixed value, then return that value.
9411 * Otherwise return NaN.
9413 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9414 enum isl_dim_type type
, unsigned pos
)
9422 ctx
= isl_map_get_ctx(map
);
9423 v
= isl_val_alloc(ctx
);
9426 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9428 return isl_val_free(v
);
9430 isl_int_set_si(v
->d
, 1);
9434 return isl_val_nan(ctx
);
9437 /* If "set" obviously lies on a hyperplane where the given dimension
9438 * has a fixed value, then return that value.
9439 * Otherwise return NaN.
9441 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9442 enum isl_dim_type type
, unsigned pos
)
9444 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9447 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9448 * then return this fixed value in *val.
9450 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9451 unsigned dim
, isl_int
*val
)
9453 return isl_basic_set_plain_has_fixed_var(bset
,
9454 isl_basic_set_n_param(bset
) + dim
, val
);
9457 /* Return -1 if the constraint "c1" should be sorted before "c2"
9458 * and 1 if it should be sorted after "c2".
9459 * Return 0 if the two constraints are the same (up to the constant term).
9461 * In particular, if a constraint involves later variables than another
9462 * then it is sorted after this other constraint.
9463 * uset_gist depends on constraints without existentially quantified
9464 * variables sorting first.
9466 * For constraints that have the same latest variable, those
9467 * with the same coefficient for this latest variable (first in absolute value
9468 * and then in actual value) are grouped together.
9469 * This is useful for detecting pairs of constraints that can
9470 * be chained in their printed representation.
9472 * Finally, within a group, constraints are sorted according to
9473 * their coefficients (excluding the constant term).
9475 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9477 isl_int
**c1
= (isl_int
**) p1
;
9478 isl_int
**c2
= (isl_int
**) p2
;
9480 unsigned size
= *(unsigned *) arg
;
9483 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9484 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9489 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9492 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9496 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9499 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9500 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9501 * and 0 if the two constraints are the same (up to the constant term).
9503 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9504 isl_int
*c1
, isl_int
*c2
)
9510 total
= isl_basic_map_total_dim(bmap
);
9511 return sort_constraint_cmp(&c1
, &c2
, &total
);
9514 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9515 __isl_take isl_basic_map
*bmap
)
9521 if (bmap
->n_ineq
== 0)
9523 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9525 total
= isl_basic_map_total_dim(bmap
);
9526 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9527 &sort_constraint_cmp
, &total
) < 0)
9528 return isl_basic_map_free(bmap
);
9529 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9533 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9534 __isl_take isl_basic_set
*bset
)
9536 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9537 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9540 __isl_give isl_basic_map
*isl_basic_map_normalize(
9541 __isl_take isl_basic_map
*bmap
)
9543 bmap
= isl_basic_map_remove_redundancies(bmap
);
9544 bmap
= isl_basic_map_sort_constraints(bmap
);
9547 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9548 __isl_keep isl_basic_map
*bmap2
)
9552 isl_space
*space1
, *space2
;
9554 if (!bmap1
|| !bmap2
)
9559 space1
= isl_basic_map_peek_space(bmap1
);
9560 space2
= isl_basic_map_peek_space(bmap2
);
9561 cmp
= isl_space_cmp(space1
, space2
);
9564 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9565 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9566 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9567 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9568 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9570 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9572 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9574 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9575 return bmap1
->n_eq
- bmap2
->n_eq
;
9576 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9577 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9578 if (bmap1
->n_div
!= bmap2
->n_div
)
9579 return bmap1
->n_div
- bmap2
->n_div
;
9580 total
= isl_basic_map_total_dim(bmap1
);
9581 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9582 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9586 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9587 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9591 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9592 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9599 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9600 __isl_keep isl_basic_set
*bset2
)
9602 return isl_basic_map_plain_cmp(bset1
, bset2
);
9605 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9611 if (set1
->n
!= set2
->n
)
9612 return set1
->n
- set2
->n
;
9614 for (i
= 0; i
< set1
->n
; ++i
) {
9615 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9623 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9624 __isl_keep isl_basic_map
*bmap2
)
9626 if (!bmap1
|| !bmap2
)
9627 return isl_bool_error
;
9628 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9631 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9632 __isl_keep isl_basic_set
*bset2
)
9634 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9635 bset_to_bmap(bset2
));
9638 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9640 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9641 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9643 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9646 /* Sort the basic maps of "map" and remove duplicate basic maps.
9648 * While removing basic maps, we make sure that the basic maps remain
9649 * sorted because isl_map_normalize expects the basic maps of the result
9652 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9656 map
= isl_map_remove_empty_parts(map
);
9659 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9660 for (i
= map
->n
- 1; i
>= 1; --i
) {
9661 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9663 isl_basic_map_free(map
->p
[i
-1]);
9664 for (j
= i
; j
< map
->n
; ++j
)
9665 map
->p
[j
- 1] = map
->p
[j
];
9672 /* Remove obvious duplicates among the basic maps of "map".
9674 * Unlike isl_map_normalize, this function does not remove redundant
9675 * constraints and only removes duplicates that have exactly the same
9676 * constraints in the input. It does sort the constraints and
9677 * the basic maps to ease the detection of duplicates.
9679 * If "map" has already been normalized or if the basic maps are
9680 * disjoint, then there can be no duplicates.
9682 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9685 isl_basic_map
*bmap
;
9691 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9693 for (i
= 0; i
< map
->n
; ++i
) {
9694 bmap
= isl_basic_map_copy(map
->p
[i
]);
9695 bmap
= isl_basic_map_sort_constraints(bmap
);
9697 return isl_map_free(map
);
9698 isl_basic_map_free(map
->p
[i
]);
9702 map
= sort_and_remove_duplicates(map
);
9706 /* We normalize in place, but if anything goes wrong we need
9707 * to return NULL, so we need to make sure we don't change the
9708 * meaning of any possible other copies of map.
9710 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9713 struct isl_basic_map
*bmap
;
9717 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9719 for (i
= 0; i
< map
->n
; ++i
) {
9720 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9723 isl_basic_map_free(map
->p
[i
]);
9727 map
= sort_and_remove_duplicates(map
);
9729 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9736 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9738 return set_from_map(isl_map_normalize(set_to_map(set
)));
9741 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9742 __isl_keep isl_map
*map2
)
9748 return isl_bool_error
;
9751 return isl_bool_true
;
9752 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9753 return isl_bool_false
;
9755 map1
= isl_map_copy(map1
);
9756 map2
= isl_map_copy(map2
);
9757 map1
= isl_map_normalize(map1
);
9758 map2
= isl_map_normalize(map2
);
9761 equal
= map1
->n
== map2
->n
;
9762 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9763 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9773 return isl_bool_error
;
9776 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9777 __isl_keep isl_set
*set2
)
9779 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9782 /* Return the basic maps in "map" as a list.
9784 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9785 __isl_keep isl_map
*map
)
9789 isl_basic_map_list
*list
;
9793 ctx
= isl_map_get_ctx(map
);
9794 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9796 for (i
= 0; i
< map
->n
; ++i
) {
9797 isl_basic_map
*bmap
;
9799 bmap
= isl_basic_map_copy(map
->p
[i
]);
9800 list
= isl_basic_map_list_add(list
, bmap
);
9806 /* Return the intersection of the elements in the non-empty list "list".
9807 * All elements are assumed to live in the same space.
9809 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9810 __isl_take isl_basic_map_list
*list
)
9813 isl_basic_map
*bmap
;
9817 n
= isl_basic_map_list_n_basic_map(list
);
9819 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9820 "expecting non-empty list", goto error
);
9822 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9823 for (i
= 1; i
< n
; ++i
) {
9824 isl_basic_map
*bmap_i
;
9826 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9827 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9830 isl_basic_map_list_free(list
);
9833 isl_basic_map_list_free(list
);
9837 /* Return the intersection of the elements in the non-empty list "list".
9838 * All elements are assumed to live in the same space.
9840 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9841 __isl_take isl_basic_set_list
*list
)
9843 return isl_basic_map_list_intersect(list
);
9846 /* Return the union of the elements of "list".
9847 * The list is required to have at least one element.
9849 __isl_give isl_set
*isl_basic_set_list_union(
9850 __isl_take isl_basic_set_list
*list
)
9854 isl_basic_set
*bset
;
9859 n
= isl_basic_set_list_n_basic_set(list
);
9861 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9862 "expecting non-empty list", goto error
);
9864 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9865 space
= isl_basic_set_get_space(bset
);
9866 isl_basic_set_free(bset
);
9868 set
= isl_set_alloc_space(space
, n
, 0);
9869 for (i
= 0; i
< n
; ++i
) {
9870 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9871 set
= isl_set_add_basic_set(set
, bset
);
9874 isl_basic_set_list_free(list
);
9877 isl_basic_set_list_free(list
);
9881 /* Return the union of the elements in the non-empty list "list".
9882 * All elements are assumed to live in the same space.
9884 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9891 n
= isl_set_list_n_set(list
);
9893 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9894 "expecting non-empty list", goto error
);
9896 set
= isl_set_list_get_set(list
, 0);
9897 for (i
= 1; i
< n
; ++i
) {
9900 set_i
= isl_set_list_get_set(list
, i
);
9901 set
= isl_set_union(set
, set_i
);
9904 isl_set_list_free(list
);
9907 isl_set_list_free(list
);
9911 __isl_give isl_basic_map
*isl_basic_map_product(
9912 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9914 isl_space
*space_result
= NULL
;
9915 struct isl_basic_map
*bmap
;
9916 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9917 struct isl_dim_map
*dim_map1
, *dim_map2
;
9919 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9921 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9922 isl_space_copy(bmap2
->dim
));
9924 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9925 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9926 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9927 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9928 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9930 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9931 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9932 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9933 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9934 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9935 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9936 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9937 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9938 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9939 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9940 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9942 bmap
= isl_basic_map_alloc_space(space_result
,
9943 bmap1
->n_div
+ bmap2
->n_div
,
9944 bmap1
->n_eq
+ bmap2
->n_eq
,
9945 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9946 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9947 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9948 bmap
= isl_basic_map_simplify(bmap
);
9949 return isl_basic_map_finalize(bmap
);
9951 isl_basic_map_free(bmap1
);
9952 isl_basic_map_free(bmap2
);
9956 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9957 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9959 isl_basic_map
*prod
;
9961 prod
= isl_basic_map_product(bmap1
, bmap2
);
9962 prod
= isl_basic_map_flatten(prod
);
9966 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9967 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9969 return isl_basic_map_flat_range_product(bset1
, bset2
);
9972 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9973 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9975 isl_space
*space_result
= NULL
;
9976 isl_basic_map
*bmap
;
9977 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9978 struct isl_dim_map
*dim_map1
, *dim_map2
;
9980 if (!bmap1
|| !bmap2
)
9983 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9984 isl_space_copy(bmap2
->dim
));
9986 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9987 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9988 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9989 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9991 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9992 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9993 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9994 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9995 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9996 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9997 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9998 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9999 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10000 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10001 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10003 bmap
= isl_basic_map_alloc_space(space_result
,
10004 bmap1
->n_div
+ bmap2
->n_div
,
10005 bmap1
->n_eq
+ bmap2
->n_eq
,
10006 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10007 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10008 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10009 bmap
= isl_basic_map_simplify(bmap
);
10010 return isl_basic_map_finalize(bmap
);
10012 isl_basic_map_free(bmap1
);
10013 isl_basic_map_free(bmap2
);
10017 __isl_give isl_basic_map
*isl_basic_map_range_product(
10018 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10021 isl_space
*space_result
= NULL
;
10022 isl_basic_map
*bmap
;
10023 unsigned in
, out1
, out2
, nparam
, total
, pos
;
10024 struct isl_dim_map
*dim_map1
, *dim_map2
;
10026 rational
= isl_basic_map_is_rational(bmap1
);
10027 if (rational
>= 0 && rational
)
10028 rational
= isl_basic_map_is_rational(bmap2
);
10029 if (!bmap1
|| !bmap2
|| rational
< 0)
10032 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10035 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10036 isl_space_copy(bmap2
->dim
));
10038 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10039 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10040 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10041 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10043 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10044 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10045 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10046 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10047 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10048 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10049 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10050 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10051 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10052 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10053 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10055 bmap
= isl_basic_map_alloc_space(space_result
,
10056 bmap1
->n_div
+ bmap2
->n_div
,
10057 bmap1
->n_eq
+ bmap2
->n_eq
,
10058 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10059 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10060 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10062 bmap
= isl_basic_map_set_rational(bmap
);
10063 bmap
= isl_basic_map_simplify(bmap
);
10064 return isl_basic_map_finalize(bmap
);
10066 isl_basic_map_free(bmap1
);
10067 isl_basic_map_free(bmap2
);
10071 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10072 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10074 isl_basic_map
*prod
;
10076 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10077 prod
= isl_basic_map_flatten_range(prod
);
10081 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10082 * and collect the results.
10083 * The result live in the space obtained by calling "space_product"
10084 * on the spaces of "map1" and "map2".
10085 * If "remove_duplicates" is set then the result may contain duplicates
10086 * (even if the inputs do not) and so we try and remove the obvious
10089 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10090 __isl_take isl_map
*map2
,
10091 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10092 __isl_take isl_space
*right
),
10093 __isl_give isl_basic_map
*(*basic_map_product
)(
10094 __isl_take isl_basic_map
*left
,
10095 __isl_take isl_basic_map
*right
),
10096 int remove_duplicates
)
10098 unsigned flags
= 0;
10099 struct isl_map
*result
;
10103 m
= isl_map_has_equal_params(map1
, map2
);
10107 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10108 "parameters don't match", goto error
);
10110 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10111 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10112 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10114 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10115 isl_space_copy(map2
->dim
)),
10116 map1
->n
* map2
->n
, flags
);
10119 for (i
= 0; i
< map1
->n
; ++i
)
10120 for (j
= 0; j
< map2
->n
; ++j
) {
10121 struct isl_basic_map
*part
;
10122 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10123 isl_basic_map_copy(map2
->p
[j
]));
10124 if (isl_basic_map_is_empty(part
))
10125 isl_basic_map_free(part
);
10127 result
= isl_map_add_basic_map(result
, part
);
10131 if (remove_duplicates
)
10132 result
= isl_map_remove_obvious_duplicates(result
);
10133 isl_map_free(map1
);
10134 isl_map_free(map2
);
10137 isl_map_free(map1
);
10138 isl_map_free(map2
);
10142 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10144 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10145 __isl_take isl_map
*map2
)
10147 return map_product(map1
, map2
, &isl_space_product
,
10148 &isl_basic_map_product
, 0);
10151 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10152 __isl_take isl_map
*map2
)
10154 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10157 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10159 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10160 __isl_take isl_map
*map2
)
10164 prod
= isl_map_product(map1
, map2
);
10165 prod
= isl_map_flatten(prod
);
10169 /* Given two set A and B, construct its Cartesian product A x B.
10171 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10173 return isl_map_range_product(set1
, set2
);
10176 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10177 __isl_take isl_set
*set2
)
10179 return isl_map_flat_range_product(set1
, set2
);
10182 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10184 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10185 __isl_take isl_map
*map2
)
10187 return map_product(map1
, map2
, &isl_space_domain_product
,
10188 &isl_basic_map_domain_product
, 1);
10191 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10193 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10194 __isl_take isl_map
*map2
)
10196 return map_product(map1
, map2
, &isl_space_range_product
,
10197 &isl_basic_map_range_product
, 1);
10200 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10201 __isl_take isl_map
*map2
)
10203 return isl_map_align_params_map_map_and(map1
, map2
,
10204 &map_domain_product_aligned
);
10207 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10208 __isl_take isl_map
*map2
)
10210 return isl_map_align_params_map_map_and(map1
, map2
,
10211 &map_range_product_aligned
);
10214 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10216 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10219 int total1
, keep1
, total2
, keep2
;
10223 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10224 !isl_space_range_is_wrapping(map
->dim
))
10225 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10226 "not a product", return isl_map_free(map
));
10228 space
= isl_map_get_space(map
);
10229 total1
= isl_space_dim(space
, isl_dim_in
);
10230 total2
= isl_space_dim(space
, isl_dim_out
);
10231 space
= isl_space_factor_domain(space
);
10232 keep1
= isl_space_dim(space
, isl_dim_in
);
10233 keep2
= isl_space_dim(space
, isl_dim_out
);
10234 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10235 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10236 map
= isl_map_reset_space(map
, space
);
10241 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10243 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10246 int total1
, keep1
, total2
, keep2
;
10250 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10251 !isl_space_range_is_wrapping(map
->dim
))
10252 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10253 "not a product", return isl_map_free(map
));
10255 space
= isl_map_get_space(map
);
10256 total1
= isl_space_dim(space
, isl_dim_in
);
10257 total2
= isl_space_dim(space
, isl_dim_out
);
10258 space
= isl_space_factor_range(space
);
10259 keep1
= isl_space_dim(space
, isl_dim_in
);
10260 keep2
= isl_space_dim(space
, isl_dim_out
);
10261 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10262 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10263 map
= isl_map_reset_space(map
, space
);
10268 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10270 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10277 if (!isl_space_domain_is_wrapping(map
->dim
))
10278 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10279 "domain is not a product", return isl_map_free(map
));
10281 space
= isl_map_get_space(map
);
10282 total
= isl_space_dim(space
, isl_dim_in
);
10283 space
= isl_space_domain_factor_domain(space
);
10284 keep
= isl_space_dim(space
, isl_dim_in
);
10285 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10286 map
= isl_map_reset_space(map
, space
);
10291 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10293 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10300 if (!isl_space_domain_is_wrapping(map
->dim
))
10301 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10302 "domain is not a product", return isl_map_free(map
));
10304 space
= isl_map_get_space(map
);
10305 total
= isl_space_dim(space
, isl_dim_in
);
10306 space
= isl_space_domain_factor_range(space
);
10307 keep
= isl_space_dim(space
, isl_dim_in
);
10308 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10309 map
= isl_map_reset_space(map
, space
);
10314 /* Given a map A -> [B -> C], extract the map A -> B.
10316 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10323 if (!isl_space_range_is_wrapping(map
->dim
))
10324 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10325 "range is not a product", return isl_map_free(map
));
10327 space
= isl_map_get_space(map
);
10328 total
= isl_space_dim(space
, isl_dim_out
);
10329 space
= isl_space_range_factor_domain(space
);
10330 keep
= isl_space_dim(space
, isl_dim_out
);
10331 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10332 map
= isl_map_reset_space(map
, space
);
10337 /* Given a map A -> [B -> C], extract the map A -> C.
10339 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10346 if (!isl_space_range_is_wrapping(map
->dim
))
10347 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10348 "range is not a product", return isl_map_free(map
));
10350 space
= isl_map_get_space(map
);
10351 total
= isl_space_dim(space
, isl_dim_out
);
10352 space
= isl_space_range_factor_range(space
);
10353 keep
= isl_space_dim(space
, isl_dim_out
);
10354 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10355 map
= isl_map_reset_space(map
, space
);
10360 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10362 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10363 __isl_take isl_map
*map2
)
10367 prod
= isl_map_domain_product(map1
, map2
);
10368 prod
= isl_map_flatten_domain(prod
);
10372 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10374 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10375 __isl_take isl_map
*map2
)
10379 prod
= isl_map_range_product(map1
, map2
);
10380 prod
= isl_map_flatten_range(prod
);
10384 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10387 uint32_t hash
= isl_hash_init();
10392 bmap
= isl_basic_map_copy(bmap
);
10393 bmap
= isl_basic_map_normalize(bmap
);
10396 total
= isl_basic_map_total_dim(bmap
);
10397 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10398 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10400 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10401 isl_hash_hash(hash
, c_hash
);
10403 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10404 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10406 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10407 isl_hash_hash(hash
, c_hash
);
10409 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10410 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10412 if (isl_int_is_zero(bmap
->div
[i
][0]))
10414 isl_hash_byte(hash
, i
& 0xFF);
10415 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10416 isl_hash_hash(hash
, c_hash
);
10418 isl_basic_map_free(bmap
);
10422 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10424 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10427 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10434 map
= isl_map_copy(map
);
10435 map
= isl_map_normalize(map
);
10439 hash
= isl_hash_init();
10440 for (i
= 0; i
< map
->n
; ++i
) {
10441 uint32_t bmap_hash
;
10442 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10443 isl_hash_hash(hash
, bmap_hash
);
10451 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10453 return isl_map_get_hash(set_to_map(set
));
10456 /* Return the number of basic maps in the (current) representation of "map".
10458 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10460 return map
? map
->n
: 0;
10463 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10465 return set
? set
->n
: 0;
10468 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10469 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10474 return isl_stat_error
;
10476 for (i
= 0; i
< map
->n
; ++i
)
10477 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10478 return isl_stat_error
;
10480 return isl_stat_ok
;
10483 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10484 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10489 return isl_stat_error
;
10491 for (i
= 0; i
< set
->n
; ++i
)
10492 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10493 return isl_stat_error
;
10495 return isl_stat_ok
;
10498 /* Return a list of basic sets, the union of which is equal to "set".
10500 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10501 __isl_keep isl_set
*set
)
10504 isl_basic_set_list
*list
;
10509 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10510 for (i
= 0; i
< set
->n
; ++i
) {
10511 isl_basic_set
*bset
;
10513 bset
= isl_basic_set_copy(set
->p
[i
]);
10514 list
= isl_basic_set_list_add(list
, bset
);
10520 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10527 bset
= isl_basic_set_cow(bset
);
10531 space
= isl_basic_set_get_space(bset
);
10532 space
= isl_space_lift(space
, bset
->n_div
);
10535 isl_space_free(bset
->dim
);
10537 bset
->extra
-= bset
->n_div
;
10540 bset
= isl_basic_set_finalize(bset
);
10544 isl_basic_set_free(bset
);
10548 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10554 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10559 set
= isl_set_cow(set
);
10563 n_div
= set
->p
[0]->n_div
;
10564 space
= isl_set_get_space(set
);
10565 space
= isl_space_lift(space
, n_div
);
10568 isl_space_free(set
->dim
);
10571 for (i
= 0; i
< set
->n
; ++i
) {
10572 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10583 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10591 dim
= isl_basic_set_total_dim(bset
);
10592 size
+= bset
->n_eq
* (1 + dim
);
10593 size
+= bset
->n_ineq
* (1 + dim
);
10594 size
+= bset
->n_div
* (2 + dim
);
10599 int isl_set_size(__isl_keep isl_set
*set
)
10607 for (i
= 0; i
< set
->n
; ++i
)
10608 size
+= isl_basic_set_size(set
->p
[i
]);
10613 /* Check if there is any lower bound (if lower == 0) and/or upper
10614 * bound (if upper == 0) on the specified dim.
10616 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10617 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10621 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10622 return isl_bool_error
;
10624 pos
+= isl_basic_map_offset(bmap
, type
);
10626 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10627 if (isl_int_is_zero(bmap
->div
[i
][0]))
10629 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10630 return isl_bool_true
;
10633 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10634 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10635 return isl_bool_true
;
10637 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10638 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10645 return lower
&& upper
;
10648 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10649 enum isl_dim_type type
, unsigned pos
)
10651 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10654 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10655 enum isl_dim_type type
, unsigned pos
)
10657 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10660 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10661 enum isl_dim_type type
, unsigned pos
)
10663 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10666 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10667 enum isl_dim_type type
, unsigned pos
)
10672 return isl_bool_error
;
10674 for (i
= 0; i
< map
->n
; ++i
) {
10676 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10677 if (bounded
< 0 || !bounded
)
10681 return isl_bool_true
;
10684 /* Return true if the specified dim is involved in both an upper bound
10685 * and a lower bound.
10687 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10688 enum isl_dim_type type
, unsigned pos
)
10690 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10693 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10695 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10696 enum isl_dim_type type
, unsigned pos
,
10697 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10698 enum isl_dim_type type
, unsigned pos
))
10703 return isl_bool_error
;
10705 for (i
= 0; i
< map
->n
; ++i
) {
10707 bounded
= fn(map
->p
[i
], type
, pos
);
10708 if (bounded
< 0 || bounded
)
10712 return isl_bool_false
;
10715 /* Return 1 if the specified dim is involved in any lower bound.
10717 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10718 enum isl_dim_type type
, unsigned pos
)
10720 return has_any_bound(set
, type
, pos
,
10721 &isl_basic_map_dim_has_lower_bound
);
10724 /* Return 1 if the specified dim is involved in any upper bound.
10726 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10727 enum isl_dim_type type
, unsigned pos
)
10729 return has_any_bound(set
, type
, pos
,
10730 &isl_basic_map_dim_has_upper_bound
);
10733 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10735 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10736 enum isl_dim_type type
, unsigned pos
,
10737 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10738 enum isl_dim_type type
, unsigned pos
))
10743 return isl_bool_error
;
10745 for (i
= 0; i
< map
->n
; ++i
) {
10747 bounded
= fn(map
->p
[i
], type
, pos
);
10748 if (bounded
< 0 || !bounded
)
10752 return isl_bool_true
;
10755 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10757 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10758 enum isl_dim_type type
, unsigned pos
)
10760 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10763 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10765 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10766 enum isl_dim_type type
, unsigned pos
)
10768 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10771 /* For each of the "n" variables starting at "first", determine
10772 * the sign of the variable and put the results in the first "n"
10773 * elements of the array "signs".
10775 * 1 means that the variable is non-negative
10776 * -1 means that the variable is non-positive
10777 * 0 means the variable attains both positive and negative values.
10779 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10780 unsigned first
, unsigned n
, int *signs
)
10782 isl_vec
*bound
= NULL
;
10783 struct isl_tab
*tab
= NULL
;
10784 struct isl_tab_undo
*snap
;
10787 if (!bset
|| !signs
)
10788 return isl_stat_error
;
10790 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10791 tab
= isl_tab_from_basic_set(bset
, 0);
10792 if (!bound
|| !tab
)
10795 isl_seq_clr(bound
->el
, bound
->size
);
10796 isl_int_set_si(bound
->el
[0], -1);
10798 snap
= isl_tab_snap(tab
);
10799 for (i
= 0; i
< n
; ++i
) {
10802 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10803 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10805 empty
= tab
->empty
;
10806 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10807 if (isl_tab_rollback(tab
, snap
) < 0)
10815 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10816 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10818 empty
= tab
->empty
;
10819 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10820 if (isl_tab_rollback(tab
, snap
) < 0)
10823 signs
[i
] = empty
? -1 : 0;
10827 isl_vec_free(bound
);
10828 return isl_stat_ok
;
10831 isl_vec_free(bound
);
10832 return isl_stat_error
;
10835 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10836 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10838 if (!bset
|| !signs
)
10839 return isl_stat_error
;
10840 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10841 return isl_stat_error
);
10843 first
+= pos(bset
->dim
, type
) - 1;
10844 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10847 /* Is it possible for the integer division "div" to depend (possibly
10848 * indirectly) on any output dimensions?
10850 * If the div is undefined, then we conservatively assume that it
10851 * may depend on them.
10852 * Otherwise, we check if it actually depends on them or on any integer
10853 * divisions that may depend on them.
10855 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10858 unsigned n_out
, o_out
;
10859 unsigned n_div
, o_div
;
10861 if (isl_int_is_zero(bmap
->div
[div
][0]))
10862 return isl_bool_true
;
10864 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10865 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10867 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10868 return isl_bool_true
;
10870 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10871 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10873 for (i
= 0; i
< n_div
; ++i
) {
10874 isl_bool may_involve
;
10876 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10878 may_involve
= div_may_involve_output(bmap
, i
);
10879 if (may_involve
< 0 || may_involve
)
10880 return may_involve
;
10883 return isl_bool_false
;
10886 /* Return the first integer division of "bmap" in the range
10887 * [first, first + n[ that may depend on any output dimensions and
10888 * that has a non-zero coefficient in "c" (where the first coefficient
10889 * in "c" corresponds to integer division "first").
10891 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10892 isl_int
*c
, int first
, int n
)
10899 for (k
= first
; k
< first
+ n
; ++k
) {
10900 isl_bool may_involve
;
10902 if (isl_int_is_zero(c
[k
]))
10904 may_involve
= div_may_involve_output(bmap
, k
);
10905 if (may_involve
< 0)
10914 /* Look for a pair of inequality constraints in "bmap" of the form
10916 * -l + i >= 0 or i >= l
10918 * n + l - i >= 0 or i <= l + n
10920 * with n < "m" and i the output dimension at position "pos".
10921 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10922 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10923 * and earlier output dimensions, as well as integer divisions that do
10924 * not involve any of the output dimensions.
10926 * Return the index of the first inequality constraint or bmap->n_ineq
10927 * if no such pair can be found.
10929 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10930 int pos
, isl_int m
)
10935 unsigned n_div
, o_div
;
10936 unsigned n_out
, o_out
;
10942 ctx
= isl_basic_map_get_ctx(bmap
);
10943 total
= isl_basic_map_total_dim(bmap
);
10944 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10945 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10946 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10947 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10948 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10949 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10951 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10952 n_out
- (pos
+ 1)) != -1)
10954 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10957 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10958 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10961 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10962 bmap
->ineq
[j
] + 1, total
))
10966 if (j
>= bmap
->n_ineq
)
10968 isl_int_add(bmap
->ineq
[i
][0],
10969 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10970 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10971 isl_int_sub(bmap
->ineq
[i
][0],
10972 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10975 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10981 return bmap
->n_ineq
;
10984 /* Return the index of the equality of "bmap" that defines
10985 * the output dimension "pos" in terms of earlier dimensions.
10986 * The equality may also involve integer divisions, as long
10987 * as those integer divisions are defined in terms of
10988 * parameters or input dimensions.
10989 * In this case, *div is set to the number of integer divisions and
10990 * *ineq is set to the number of inequality constraints (provided
10991 * div and ineq are not NULL).
10993 * The equality may also involve a single integer division involving
10994 * the output dimensions (typically only output dimension "pos") as
10995 * long as the coefficient of output dimension "pos" is 1 or -1 and
10996 * there is a pair of constraints i >= l and i <= l + n, with i referring
10997 * to output dimension "pos", l an expression involving only earlier
10998 * dimensions and n smaller than the coefficient of the integer division
10999 * in the equality. In this case, the output dimension can be defined
11000 * in terms of a modulo expression that does not involve the integer division.
11001 * *div is then set to this single integer division and
11002 * *ineq is set to the index of constraint i >= l.
11004 * Return bmap->n_eq if there is no such equality.
11005 * Return -1 on error.
11007 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11008 int pos
, int *div
, int *ineq
)
11011 unsigned n_out
, o_out
;
11012 unsigned n_div
, o_div
;
11017 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11018 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11019 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11020 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11023 *ineq
= bmap
->n_ineq
;
11026 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11027 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11029 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11030 n_out
- (pos
+ 1)) != -1)
11032 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11036 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11037 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11039 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11040 k
+ 1, n_div
- (k
+1)) < n_div
)
11042 l
= find_modulo_constraint_pair(bmap
, pos
,
11043 bmap
->eq
[j
][o_div
+ k
]);
11046 if (l
>= bmap
->n_ineq
)
11058 /* Check if the given basic map is obviously single-valued.
11059 * In particular, for each output dimension, check that there is
11060 * an equality that defines the output dimension in terms of
11061 * earlier dimensions.
11063 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11069 return isl_bool_error
;
11071 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11073 for (i
= 0; i
< n_out
; ++i
) {
11076 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11079 return isl_bool_error
;
11080 if (eq
>= bmap
->n_eq
)
11081 return isl_bool_false
;
11084 return isl_bool_true
;
11087 /* Check if the given basic map is single-valued.
11088 * We simply compute
11092 * and check if the result is a subset of the identity mapping.
11094 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11097 isl_basic_map
*test
;
11101 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11105 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11106 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11108 space
= isl_basic_map_get_space(bmap
);
11109 space
= isl_space_map_from_set(isl_space_range(space
));
11110 id
= isl_basic_map_identity(space
);
11112 sv
= isl_basic_map_is_subset(test
, id
);
11114 isl_basic_map_free(test
);
11115 isl_basic_map_free(id
);
11120 /* Check if the given map is obviously single-valued.
11122 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11125 return isl_bool_error
;
11127 return isl_bool_true
;
11129 return isl_bool_false
;
11131 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11134 /* Check if the given map is single-valued.
11135 * We simply compute
11139 * and check if the result is a subset of the identity mapping.
11141 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11148 sv
= isl_map_plain_is_single_valued(map
);
11152 test
= isl_map_reverse(isl_map_copy(map
));
11153 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11155 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11156 id
= isl_map_identity(dim
);
11158 sv
= isl_map_is_subset(test
, id
);
11160 isl_map_free(test
);
11166 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11170 map
= isl_map_copy(map
);
11171 map
= isl_map_reverse(map
);
11172 in
= isl_map_is_single_valued(map
);
11178 /* Check if the given map is obviously injective.
11180 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11184 map
= isl_map_copy(map
);
11185 map
= isl_map_reverse(map
);
11186 in
= isl_map_plain_is_single_valued(map
);
11192 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11196 sv
= isl_map_is_single_valued(map
);
11200 return isl_map_is_injective(map
);
11203 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11205 return isl_map_is_single_valued(set_to_map(set
));
11208 /* Does "map" only map elements to themselves?
11210 * If the domain and range spaces are different, then "map"
11211 * is considered not to be an identity relation, even if it is empty.
11212 * Otherwise, construct the maximal identity relation and
11213 * check whether "map" is a subset of this relation.
11215 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11219 isl_bool equal
, is_identity
;
11221 space
= isl_map_get_space(map
);
11222 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11223 isl_space_free(space
);
11224 if (equal
< 0 || !equal
)
11227 id
= isl_map_identity(isl_map_get_space(map
));
11228 is_identity
= isl_map_is_subset(map
, id
);
11231 return is_identity
;
11234 int isl_map_is_translation(__isl_keep isl_map
*map
)
11239 delta
= isl_map_deltas(isl_map_copy(map
));
11240 ok
= isl_set_is_singleton(delta
);
11241 isl_set_free(delta
);
11246 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11248 if (isl_seq_first_non_zero(p
, pos
) != -1)
11250 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11255 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11262 return isl_bool_error
;
11264 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11265 return isl_bool_false
;
11267 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11268 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11269 for (j
= 0; j
< nvar
; ++j
) {
11270 int lower
= 0, upper
= 0;
11271 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11272 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11274 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11275 return isl_bool_false
;
11278 if (i
< bset
->n_eq
)
11280 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11281 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11283 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11284 return isl_bool_false
;
11285 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11290 if (!lower
|| !upper
)
11291 return isl_bool_false
;
11294 return isl_bool_true
;
11297 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11300 return isl_bool_error
;
11302 return isl_bool_false
;
11304 return isl_basic_set_is_box(set
->p
[0]);
11307 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11310 return isl_bool_error
;
11312 return isl_space_is_wrapping(bset
->dim
);
11315 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11318 return isl_bool_error
;
11320 return isl_space_is_wrapping(set
->dim
);
11323 /* Modify the space of "map" through a call to "change".
11324 * If "can_change" is set (not NULL), then first call it to check
11325 * if the modification is allowed, printing the error message "cannot_change"
11328 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11329 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11330 const char *cannot_change
,
11331 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11339 ok
= can_change
? can_change(map
) : isl_bool_true
;
11341 return isl_map_free(map
);
11343 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11344 return isl_map_free(map
));
11346 space
= change(isl_map_get_space(map
));
11347 map
= isl_map_reset_space(map
, space
);
11352 /* Is the domain of "map" a wrapped relation?
11354 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11357 return isl_bool_error
;
11359 return isl_space_domain_is_wrapping(map
->dim
);
11362 /* Does "map" have a wrapped relation in both domain and range?
11364 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11366 return isl_space_is_product(isl_map_peek_space(map
));
11369 /* Is the range of "map" a wrapped relation?
11371 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11374 return isl_bool_error
;
11376 return isl_space_range_is_wrapping(map
->dim
);
11379 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11381 bmap
= isl_basic_map_cow(bmap
);
11385 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11389 bmap
= isl_basic_map_finalize(bmap
);
11391 return bset_from_bmap(bmap
);
11393 isl_basic_map_free(bmap
);
11397 /* Given a map A -> B, return the set (A -> B).
11399 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11401 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11404 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11406 bset
= isl_basic_set_cow(bset
);
11410 bset
->dim
= isl_space_unwrap(bset
->dim
);
11414 bset
= isl_basic_set_finalize(bset
);
11416 return bset_to_bmap(bset
);
11418 isl_basic_set_free(bset
);
11422 /* Given a set (A -> B), return the map A -> B.
11423 * Error out if "set" is not of the form (A -> B).
11425 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11427 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11428 "not a wrapping set", &isl_space_unwrap
);
11431 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11432 enum isl_dim_type type
)
11437 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11440 bmap
= isl_basic_map_cow(bmap
);
11444 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11448 bmap
= isl_basic_map_finalize(bmap
);
11452 isl_basic_map_free(bmap
);
11456 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11457 enum isl_dim_type type
)
11464 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11467 map
= isl_map_cow(map
);
11471 for (i
= 0; i
< map
->n
; ++i
) {
11472 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11476 map
->dim
= isl_space_reset(map
->dim
, type
);
11486 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11491 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11494 bmap
= isl_basic_map_cow(bmap
);
11498 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11502 bmap
= isl_basic_map_finalize(bmap
);
11506 isl_basic_map_free(bmap
);
11510 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11512 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11515 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11516 __isl_take isl_basic_map
*bmap
)
11521 if (!bmap
->dim
->nested
[0])
11524 bmap
= isl_basic_map_cow(bmap
);
11528 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11532 bmap
= isl_basic_map_finalize(bmap
);
11536 isl_basic_map_free(bmap
);
11540 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11541 __isl_take isl_basic_map
*bmap
)
11546 if (!bmap
->dim
->nested
[1])
11549 bmap
= isl_basic_map_cow(bmap
);
11553 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11557 bmap
= isl_basic_map_finalize(bmap
);
11561 isl_basic_map_free(bmap
);
11565 /* Remove any internal structure from the spaces of domain and range of "map".
11567 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11572 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11575 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11578 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11580 return set_from_map(isl_map_flatten(set_to_map(set
)));
11583 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11585 isl_space
*space
, *flat_space
;
11588 space
= isl_set_get_space(set
);
11589 flat_space
= isl_space_flatten(isl_space_copy(space
));
11590 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11592 map
= isl_map_intersect_domain(map
, set
);
11597 /* Remove any internal structure from the space of the domain of "map".
11599 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11604 if (!map
->dim
->nested
[0])
11607 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11610 /* Remove any internal structure from the space of the range of "map".
11612 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11617 if (!map
->dim
->nested
[1])
11620 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11623 /* Reorder the dimensions of "bmap" according to the given dim_map
11624 * and set the dimension specification to "space" and
11625 * perform Gaussian elimination on the result.
11627 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11628 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11630 isl_basic_map
*res
;
11634 if (!bmap
|| !space
|| !dim_map
)
11637 flags
= bmap
->flags
;
11638 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11639 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11640 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11641 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11642 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11643 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11645 res
->flags
= flags
;
11646 res
= isl_basic_map_gauss(res
, NULL
);
11647 res
= isl_basic_map_finalize(res
);
11651 isl_basic_map_free(bmap
);
11652 isl_space_free(space
);
11656 /* Reorder the dimensions of "map" according to given reordering.
11658 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11659 __isl_take isl_reordering
*r
)
11662 struct isl_dim_map
*dim_map
;
11664 map
= isl_map_cow(map
);
11665 dim_map
= isl_dim_map_from_reordering(r
);
11666 if (!map
|| !r
|| !dim_map
)
11669 for (i
= 0; i
< map
->n
; ++i
) {
11670 struct isl_dim_map
*dim_map_i
;
11673 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11675 space
= isl_reordering_get_space(r
);
11676 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11682 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11683 map
= isl_map_unmark_normalized(map
);
11685 isl_reordering_free(r
);
11691 isl_reordering_free(r
);
11695 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11696 __isl_take isl_reordering
*r
)
11698 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11701 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11702 __isl_take isl_space
*model
)
11707 if (!map
|| !model
)
11710 ctx
= isl_space_get_ctx(model
);
11711 if (!isl_space_has_named_params(model
))
11712 isl_die(ctx
, isl_error_invalid
,
11713 "model has unnamed parameters", goto error
);
11714 if (isl_map_check_named_params(map
) < 0)
11716 aligned
= isl_map_space_has_equal_params(map
, model
);
11720 isl_reordering
*exp
;
11722 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11723 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11724 map
= isl_map_realign(map
, exp
);
11727 isl_space_free(model
);
11730 isl_space_free(model
);
11735 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11736 __isl_take isl_space
*model
)
11738 return isl_map_align_params(set
, model
);
11741 /* Align the parameters of "bmap" to those of "model", introducing
11742 * additional parameters if needed.
11744 __isl_give isl_basic_map
*isl_basic_map_align_params(
11745 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11748 isl_bool equal_params
;
11750 if (!bmap
|| !model
)
11753 ctx
= isl_space_get_ctx(model
);
11754 if (!isl_space_has_named_params(model
))
11755 isl_die(ctx
, isl_error_invalid
,
11756 "model has unnamed parameters", goto error
);
11757 if (isl_basic_map_check_named_params(bmap
) < 0)
11759 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11760 if (equal_params
< 0)
11762 if (!equal_params
) {
11763 isl_reordering
*exp
;
11764 struct isl_dim_map
*dim_map
;
11766 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11767 exp
= isl_reordering_extend_space(exp
,
11768 isl_basic_map_get_space(bmap
));
11769 dim_map
= isl_dim_map_from_reordering(exp
);
11770 bmap
= isl_basic_map_realign(bmap
,
11771 isl_reordering_get_space(exp
),
11772 isl_dim_map_extend(dim_map
, bmap
));
11773 isl_reordering_free(exp
);
11777 isl_space_free(model
);
11780 isl_space_free(model
);
11781 isl_basic_map_free(bmap
);
11785 /* Do "bset" and "space" have the same parameters?
11787 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11788 __isl_keep isl_space
*space
)
11790 isl_space
*bset_space
;
11792 bset_space
= isl_basic_set_peek_space(bset
);
11793 return isl_space_has_equal_params(bset_space
, space
);
11796 /* Do "map" and "space" have the same parameters?
11798 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11799 __isl_keep isl_space
*space
)
11801 isl_space
*map_space
;
11803 map_space
= isl_map_peek_space(map
);
11804 return isl_space_has_equal_params(map_space
, space
);
11807 /* Do "set" and "space" have the same parameters?
11809 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11810 __isl_keep isl_space
*space
)
11812 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11815 /* Align the parameters of "bset" to those of "model", introducing
11816 * additional parameters if needed.
11818 __isl_give isl_basic_set
*isl_basic_set_align_params(
11819 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11821 return isl_basic_map_align_params(bset
, model
);
11824 /* Drop all parameters not referenced by "map".
11826 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11830 if (isl_map_check_named_params(map
) < 0)
11831 return isl_map_free(map
);
11833 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11836 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11838 return isl_map_free(map
);
11840 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11846 /* Drop all parameters not referenced by "set".
11848 __isl_give isl_set
*isl_set_drop_unused_params(
11849 __isl_take isl_set
*set
)
11851 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11854 /* Drop all parameters not referenced by "bmap".
11856 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11857 __isl_take isl_basic_map
*bmap
)
11861 if (isl_basic_map_check_named_params(bmap
) < 0)
11862 return isl_basic_map_free(bmap
);
11864 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11867 involves
= isl_basic_map_involves_dims(bmap
,
11868 isl_dim_param
, i
, 1);
11870 return isl_basic_map_free(bmap
);
11872 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11878 /* Drop all parameters not referenced by "bset".
11880 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11881 __isl_take isl_basic_set
*bset
)
11883 return bset_from_bmap(isl_basic_map_drop_unused_params(
11884 bset_to_bmap(bset
)));
11887 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11888 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11889 enum isl_dim_type c2
, enum isl_dim_type c3
,
11890 enum isl_dim_type c4
, enum isl_dim_type c5
)
11892 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11893 struct isl_mat
*mat
;
11899 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11900 isl_basic_map_total_dim(bmap
) + 1);
11903 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11904 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11905 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11906 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11907 isl_int_set(mat
->row
[i
][pos
],
11908 bmap
->eq
[i
][off
+ k
]);
11916 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11917 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11918 enum isl_dim_type c2
, enum isl_dim_type c3
,
11919 enum isl_dim_type c4
, enum isl_dim_type c5
)
11921 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11922 struct isl_mat
*mat
;
11928 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11929 isl_basic_map_total_dim(bmap
) + 1);
11932 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11933 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11934 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11935 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11936 isl_int_set(mat
->row
[i
][pos
],
11937 bmap
->ineq
[i
][off
+ k
]);
11945 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11946 __isl_take isl_space
*space
,
11947 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11948 enum isl_dim_type c2
, enum isl_dim_type c3
,
11949 enum isl_dim_type c4
, enum isl_dim_type c5
)
11951 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11952 isl_basic_map
*bmap
= NULL
;
11958 if (!space
|| !eq
|| !ineq
)
11961 if (eq
->n_col
!= ineq
->n_col
)
11962 isl_die(space
->ctx
, isl_error_invalid
,
11963 "equalities and inequalities matrices should have "
11964 "same number of columns", goto error
);
11966 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11968 if (eq
->n_col
< total
)
11969 isl_die(space
->ctx
, isl_error_invalid
,
11970 "number of columns too small", goto error
);
11972 extra
= eq
->n_col
- total
;
11974 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11975 eq
->n_row
, ineq
->n_row
);
11978 for (i
= 0; i
< extra
; ++i
) {
11979 k
= isl_basic_map_alloc_div(bmap
);
11982 isl_int_set_si(bmap
->div
[k
][0], 0);
11984 for (i
= 0; i
< eq
->n_row
; ++i
) {
11985 l
= isl_basic_map_alloc_equality(bmap
);
11988 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11989 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11990 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11991 isl_int_set(bmap
->eq
[l
][off
+ k
],
11997 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11998 l
= isl_basic_map_alloc_inequality(bmap
);
12001 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12002 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12003 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
12004 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12005 ineq
->row
[i
][pos
]);
12011 isl_space_free(space
);
12013 isl_mat_free(ineq
);
12015 bmap
= isl_basic_map_simplify(bmap
);
12016 return isl_basic_map_finalize(bmap
);
12018 isl_space_free(space
);
12020 isl_mat_free(ineq
);
12021 isl_basic_map_free(bmap
);
12025 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12026 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12027 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12029 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12030 c1
, c2
, c3
, c4
, isl_dim_in
);
12033 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12034 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12035 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12037 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12038 c1
, c2
, c3
, c4
, isl_dim_in
);
12041 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12042 __isl_take isl_space
*dim
,
12043 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12044 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12046 isl_basic_map
*bmap
;
12047 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12048 c1
, c2
, c3
, c4
, isl_dim_in
);
12049 return bset_from_bmap(bmap
);
12052 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12055 return isl_bool_error
;
12057 return isl_space_can_zip(bmap
->dim
);
12060 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12063 return isl_bool_error
;
12065 return isl_space_can_zip(map
->dim
);
12068 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12069 * (A -> C) -> (B -> D).
12071 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12080 if (!isl_basic_map_can_zip(bmap
))
12081 isl_die(bmap
->ctx
, isl_error_invalid
,
12082 "basic map cannot be zipped", goto error
);
12083 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12084 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12085 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12086 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12087 bmap
= isl_basic_map_cow(bmap
);
12088 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12091 bmap
->dim
= isl_space_zip(bmap
->dim
);
12094 bmap
= isl_basic_map_mark_final(bmap
);
12097 isl_basic_map_free(bmap
);
12101 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12102 * (A -> C) -> (B -> D).
12104 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12111 if (!isl_map_can_zip(map
))
12112 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12115 map
= isl_map_cow(map
);
12119 for (i
= 0; i
< map
->n
; ++i
) {
12120 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12125 map
->dim
= isl_space_zip(map
->dim
);
12135 /* Can we apply isl_basic_map_curry to "bmap"?
12136 * That is, does it have a nested relation in its domain?
12138 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12141 return isl_bool_error
;
12143 return isl_space_can_curry(bmap
->dim
);
12146 /* Can we apply isl_map_curry to "map"?
12147 * That is, does it have a nested relation in its domain?
12149 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12152 return isl_bool_error
;
12154 return isl_space_can_curry(map
->dim
);
12157 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12160 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12166 if (!isl_basic_map_can_curry(bmap
))
12167 isl_die(bmap
->ctx
, isl_error_invalid
,
12168 "basic map cannot be curried", goto error
);
12169 bmap
= isl_basic_map_cow(bmap
);
12172 bmap
->dim
= isl_space_curry(bmap
->dim
);
12175 bmap
= isl_basic_map_mark_final(bmap
);
12178 isl_basic_map_free(bmap
);
12182 /* Given a map (A -> B) -> C, return the corresponding map
12185 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12187 return isl_map_change_space(map
, &isl_map_can_curry
,
12188 "map cannot be curried", &isl_space_curry
);
12191 /* Can isl_map_range_curry be applied to "map"?
12192 * That is, does it have a nested relation in its range,
12193 * the domain of which is itself a nested relation?
12195 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12198 return isl_bool_error
;
12200 return isl_space_can_range_curry(map
->dim
);
12203 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12204 * A -> (B -> (C -> D)).
12206 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12208 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12209 "map range cannot be curried",
12210 &isl_space_range_curry
);
12213 /* Can we apply isl_basic_map_uncurry to "bmap"?
12214 * That is, does it have a nested relation in its domain?
12216 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12219 return isl_bool_error
;
12221 return isl_space_can_uncurry(bmap
->dim
);
12224 /* Can we apply isl_map_uncurry to "map"?
12225 * That is, does it have a nested relation in its domain?
12227 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12230 return isl_bool_error
;
12232 return isl_space_can_uncurry(map
->dim
);
12235 /* Given a basic map A -> (B -> C), return the corresponding basic map
12238 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12244 if (!isl_basic_map_can_uncurry(bmap
))
12245 isl_die(bmap
->ctx
, isl_error_invalid
,
12246 "basic map cannot be uncurried",
12247 return isl_basic_map_free(bmap
));
12248 bmap
= isl_basic_map_cow(bmap
);
12251 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12253 return isl_basic_map_free(bmap
);
12254 bmap
= isl_basic_map_mark_final(bmap
);
12258 /* Given a map A -> (B -> C), return the corresponding map
12261 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12263 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12264 "map cannot be uncurried", &isl_space_uncurry
);
12267 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12268 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12270 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12273 /* Construct a basic map where the given dimensions are equal to each other.
12275 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12276 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12278 isl_basic_map
*bmap
= NULL
;
12284 if (pos1
>= isl_space_dim(space
, type1
))
12285 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12286 "index out of bounds", goto error
);
12287 if (pos2
>= isl_space_dim(space
, type2
))
12288 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12289 "index out of bounds", goto error
);
12291 if (type1
== type2
&& pos1
== pos2
)
12292 return isl_basic_map_universe(space
);
12294 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12295 i
= isl_basic_map_alloc_equality(bmap
);
12298 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12299 pos1
+= isl_basic_map_offset(bmap
, type1
);
12300 pos2
+= isl_basic_map_offset(bmap
, type2
);
12301 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12302 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12303 bmap
= isl_basic_map_finalize(bmap
);
12304 isl_space_free(space
);
12307 isl_space_free(space
);
12308 isl_basic_map_free(bmap
);
12312 /* Add a constraint imposing that the given two dimensions are equal.
12314 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12315 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12319 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12321 bmap
= isl_basic_map_intersect(bmap
, eq
);
12326 /* Add a constraint imposing that the given two dimensions are equal.
12328 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12329 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12331 isl_basic_map
*bmap
;
12333 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12335 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12340 /* Add a constraint imposing that the given two dimensions have opposite values.
12342 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12343 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12345 isl_basic_map
*bmap
= NULL
;
12351 if (pos1
>= isl_map_dim(map
, type1
))
12352 isl_die(map
->ctx
, isl_error_invalid
,
12353 "index out of bounds", goto error
);
12354 if (pos2
>= isl_map_dim(map
, type2
))
12355 isl_die(map
->ctx
, isl_error_invalid
,
12356 "index out of bounds", goto error
);
12358 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12359 i
= isl_basic_map_alloc_equality(bmap
);
12362 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12363 pos1
+= isl_basic_map_offset(bmap
, type1
);
12364 pos2
+= isl_basic_map_offset(bmap
, type2
);
12365 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12366 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12367 bmap
= isl_basic_map_finalize(bmap
);
12369 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12373 isl_basic_map_free(bmap
);
12378 /* Construct a constraint imposing that the value of the first dimension is
12379 * greater than or equal to that of the second.
12381 static __isl_give isl_constraint
*constraint_order_ge(
12382 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12383 enum isl_dim_type type2
, int pos2
)
12390 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12392 if (pos1
>= isl_constraint_dim(c
, type1
))
12393 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12394 "index out of bounds", return isl_constraint_free(c
));
12395 if (pos2
>= isl_constraint_dim(c
, type2
))
12396 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12397 "index out of bounds", return isl_constraint_free(c
));
12399 if (type1
== type2
&& pos1
== pos2
)
12402 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12403 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12408 /* Add a constraint imposing that the value of the first dimension is
12409 * greater than or equal to that of the second.
12411 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12412 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12417 if (type1
== type2
&& pos1
== pos2
)
12419 space
= isl_basic_map_get_space(bmap
);
12420 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12421 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12426 /* Add a constraint imposing that the value of the first dimension is
12427 * greater than or equal to that of the second.
12429 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12430 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12435 if (type1
== type2
&& pos1
== pos2
)
12437 space
= isl_map_get_space(map
);
12438 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12439 map
= isl_map_add_constraint(map
, c
);
12444 /* Add a constraint imposing that the value of the first dimension is
12445 * less than or equal to that of the second.
12447 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12448 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12450 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12453 /* Construct a basic map where the value of the first dimension is
12454 * greater than that of the second.
12456 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12457 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12459 isl_basic_map
*bmap
= NULL
;
12465 if (pos1
>= isl_space_dim(space
, type1
))
12466 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12467 "index out of bounds", goto error
);
12468 if (pos2
>= isl_space_dim(space
, type2
))
12469 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12470 "index out of bounds", goto error
);
12472 if (type1
== type2
&& pos1
== pos2
)
12473 return isl_basic_map_empty(space
);
12475 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12476 i
= isl_basic_map_alloc_inequality(bmap
);
12478 return isl_basic_map_free(bmap
);
12479 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12480 pos1
+= isl_basic_map_offset(bmap
, type1
);
12481 pos2
+= isl_basic_map_offset(bmap
, type2
);
12482 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12483 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12484 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12485 bmap
= isl_basic_map_finalize(bmap
);
12489 isl_space_free(space
);
12490 isl_basic_map_free(bmap
);
12494 /* Add a constraint imposing that the value of the first dimension is
12495 * greater than that of the second.
12497 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12498 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12502 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12504 bmap
= isl_basic_map_intersect(bmap
, gt
);
12509 /* Add a constraint imposing that the value of the first dimension is
12510 * greater than that of the second.
12512 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12513 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12515 isl_basic_map
*bmap
;
12517 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12519 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12524 /* Add a constraint imposing that the value of the first dimension is
12525 * smaller than that of the second.
12527 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12528 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12530 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12533 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12537 isl_local_space
*ls
;
12542 if (!isl_basic_map_divs_known(bmap
))
12543 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12544 "some divs are unknown", return NULL
);
12546 ls
= isl_basic_map_get_local_space(bmap
);
12547 div
= isl_local_space_get_div(ls
, pos
);
12548 isl_local_space_free(ls
);
12553 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12556 return isl_basic_map_get_div(bset
, pos
);
12559 /* Plug in "subs" for dimension "type", "pos" of "bset".
12561 * Let i be the dimension to replace and let "subs" be of the form
12565 * Any integer division with a non-zero coefficient for i,
12567 * floor((a i + g)/m)
12571 * floor((a f + d g)/(m d))
12573 * Constraints of the form
12581 * We currently require that "subs" is an integral expression.
12582 * Handling rational expressions may require us to add stride constraints
12583 * as we do in isl_basic_set_preimage_multi_aff.
12585 __isl_give isl_basic_set
*isl_basic_set_substitute(
12586 __isl_take isl_basic_set
*bset
,
12587 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12593 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12596 bset
= isl_basic_set_cow(bset
);
12597 if (!bset
|| !subs
)
12600 ctx
= isl_basic_set_get_ctx(bset
);
12601 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12602 isl_die(ctx
, isl_error_invalid
,
12603 "spaces don't match", goto error
);
12604 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12605 isl_die(ctx
, isl_error_unsupported
,
12606 "cannot handle divs yet", goto error
);
12607 if (!isl_int_is_one(subs
->v
->el
[0]))
12608 isl_die(ctx
, isl_error_invalid
,
12609 "can only substitute integer expressions", goto error
);
12611 pos
+= isl_basic_set_offset(bset
, type
);
12615 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12616 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12618 isl_int_set(v
, bset
->eq
[i
][pos
]);
12619 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12620 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12621 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12624 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12625 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12627 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12628 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12629 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12630 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12633 for (i
= 0; i
< bset
->n_div
; ++i
) {
12634 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12636 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12637 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12638 isl_seq_combine(bset
->div
[i
] + 1,
12639 subs
->v
->el
[0], bset
->div
[i
] + 1,
12640 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12641 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12646 bset
= isl_basic_set_simplify(bset
);
12647 return isl_basic_set_finalize(bset
);
12649 isl_basic_set_free(bset
);
12653 /* Plug in "subs" for dimension "type", "pos" of "set".
12655 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12656 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12660 if (set
&& isl_set_plain_is_empty(set
))
12663 set
= isl_set_cow(set
);
12667 for (i
= set
->n
- 1; i
>= 0; --i
) {
12668 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12669 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12680 /* Check if the range of "ma" is compatible with the domain or range
12681 * (depending on "type") of "bmap".
12683 static isl_stat
check_basic_map_compatible_range_multi_aff(
12684 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12685 __isl_keep isl_multi_aff
*ma
)
12688 isl_space
*ma_space
;
12690 ma_space
= isl_multi_aff_get_space(ma
);
12692 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12696 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12697 "parameters don't match", goto error
);
12698 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12702 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12703 "spaces don't match", goto error
);
12705 isl_space_free(ma_space
);
12706 return isl_stat_ok
;
12708 isl_space_free(ma_space
);
12709 return isl_stat_error
;
12712 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12713 * coefficients before the transformed range of dimensions,
12714 * the "n_after" coefficients after the transformed range of dimensions
12715 * and the coefficients of the other divs in "bmap".
12717 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12718 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12723 isl_local_space
*ls
;
12728 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12732 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12733 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12734 for (i
= 0; i
< n_div
; ++i
) {
12735 int o_bmap
= 0, o_ls
= 0;
12737 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12738 o_bmap
+= 1 + 1 + n_param
;
12739 o_ls
+= 1 + 1 + n_param
;
12740 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12741 o_bmap
+= n_before
;
12742 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12743 ls
->div
->row
[i
] + o_ls
, n_set
);
12746 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12748 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12749 ls
->div
->row
[i
] + o_ls
, n_div
);
12752 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12753 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12757 isl_local_space_free(ls
);
12760 isl_local_space_free(ls
);
12764 /* How many stride constraints does "ma" enforce?
12765 * That is, how many of the affine expressions have a denominator
12766 * different from one?
12768 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12773 for (i
= 0; i
< ma
->n
; ++i
)
12774 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12780 /* For each affine expression in ma of the form
12782 * x_i = (f_i y + h_i)/m_i
12784 * with m_i different from one, add a constraint to "bmap"
12787 * f_i y + h_i = m_i alpha_i
12789 * with alpha_i an additional existentially quantified variable.
12791 * The input variables of "ma" correspond to a subset of the variables
12792 * of "bmap". There are "n_before" variables in "bmap" before this
12793 * subset and "n_after" variables after this subset.
12794 * The integer divisions of the affine expressions in "ma" are assumed
12795 * to have been aligned. There are "n_div_ma" of them and
12796 * they appear first in "bmap", straight after the "n_after" variables.
12798 static __isl_give isl_basic_map
*add_ma_strides(
12799 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12800 int n_before
, int n_after
, int n_div_ma
)
12808 total
= isl_basic_map_total_dim(bmap
);
12809 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12810 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12811 for (i
= 0; i
< ma
->n
; ++i
) {
12812 int o_bmap
= 0, o_ma
= 1;
12814 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12816 div
= isl_basic_map_alloc_div(bmap
);
12817 k
= isl_basic_map_alloc_equality(bmap
);
12818 if (div
< 0 || k
< 0)
12820 isl_int_set_si(bmap
->div
[div
][0], 0);
12821 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12822 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12823 o_bmap
+= 1 + n_param
;
12824 o_ma
+= 1 + n_param
;
12825 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12826 o_bmap
+= n_before
;
12827 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12828 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12831 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12833 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12834 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12835 o_bmap
+= n_div_ma
;
12837 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12838 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12844 isl_basic_map_free(bmap
);
12848 /* Replace the domain or range space (depending on "type) of "space" by "set".
12850 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12851 enum isl_dim_type type
, __isl_take isl_space
*set
)
12853 if (type
== isl_dim_in
) {
12854 space
= isl_space_range(space
);
12855 space
= isl_space_map_from_domain_and_range(set
, space
);
12857 space
= isl_space_domain(space
);
12858 space
= isl_space_map_from_domain_and_range(space
, set
);
12864 /* Compute the preimage of the domain or range (depending on "type")
12865 * of "bmap" under the function represented by "ma".
12866 * In other words, plug in "ma" in the domain or range of "bmap".
12867 * The result is a basic map that lives in the same space as "bmap"
12868 * except that the domain or range has been replaced by
12869 * the domain space of "ma".
12871 * If bmap is represented by
12873 * A(p) + S u + B x + T v + C(divs) >= 0,
12875 * where u and x are input and output dimensions if type == isl_dim_out
12876 * while x and v are input and output dimensions if type == isl_dim_in,
12877 * and ma is represented by
12879 * x = D(p) + F(y) + G(divs')
12881 * then the result is
12883 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12885 * The divs in the input set are similarly adjusted.
12888 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12892 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12893 * B_i G(divs') + c_i(divs))/n_i)
12895 * If bmap is not a rational map and if F(y) involves any denominators
12897 * x_i = (f_i y + h_i)/m_i
12899 * then additional constraints are added to ensure that we only
12900 * map back integer points. That is we enforce
12902 * f_i y + h_i = m_i alpha_i
12904 * with alpha_i an additional existentially quantified variable.
12906 * We first copy over the divs from "ma".
12907 * Then we add the modified constraints and divs from "bmap".
12908 * Finally, we add the stride constraints, if needed.
12910 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12911 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12912 __isl_take isl_multi_aff
*ma
)
12916 isl_basic_map
*res
= NULL
;
12917 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12918 isl_int f
, c1
, c2
, g
;
12927 ma
= isl_multi_aff_align_divs(ma
);
12930 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12933 if (type
== isl_dim_in
) {
12935 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12937 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12940 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12941 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12943 space
= isl_multi_aff_get_domain_space(ma
);
12944 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12945 rational
= isl_basic_map_is_rational(bmap
);
12946 strides
= rational
? 0 : multi_aff_strides(ma
);
12947 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12948 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12950 res
= isl_basic_map_set_rational(res
);
12952 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12953 if (isl_basic_map_alloc_div(res
) < 0)
12956 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12959 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12960 k
= isl_basic_map_alloc_equality(res
);
12963 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12964 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12967 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12968 k
= isl_basic_map_alloc_inequality(res
);
12971 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12972 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12975 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12976 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12977 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12980 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12981 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12986 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12992 isl_basic_map_free(bmap
);
12993 isl_multi_aff_free(ma
);
12994 res
= isl_basic_map_simplify(res
);
12995 return isl_basic_map_finalize(res
);
13001 isl_basic_map_free(bmap
);
13002 isl_multi_aff_free(ma
);
13003 isl_basic_map_free(res
);
13007 /* Compute the preimage of "bset" under the function represented by "ma".
13008 * In other words, plug in "ma" in "bset". The result is a basic set
13009 * that lives in the domain space of "ma".
13011 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13012 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13014 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13017 /* Compute the preimage of the domain of "bmap" under the function
13018 * represented by "ma".
13019 * In other words, plug in "ma" in the domain of "bmap".
13020 * The result is a basic map that lives in the same space as "bmap"
13021 * except that the domain has been replaced by the domain space of "ma".
13023 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13024 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13026 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13029 /* Compute the preimage of the range of "bmap" under the function
13030 * represented by "ma".
13031 * In other words, plug in "ma" in the range of "bmap".
13032 * The result is a basic map that lives in the same space as "bmap"
13033 * except that the range has been replaced by the domain space of "ma".
13035 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13036 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13038 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13041 /* Check if the range of "ma" is compatible with the domain or range
13042 * (depending on "type") of "map".
13043 * Return isl_stat_error if anything is wrong.
13045 static isl_stat
check_map_compatible_range_multi_aff(
13046 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13047 __isl_keep isl_multi_aff
*ma
)
13050 isl_space
*ma_space
;
13052 ma_space
= isl_multi_aff_get_space(ma
);
13053 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13054 isl_space_free(ma_space
);
13056 return isl_stat_error
;
13058 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13059 "spaces don't match", return isl_stat_error
);
13060 return isl_stat_ok
;
13063 /* Compute the preimage of the domain or range (depending on "type")
13064 * of "map" under the function represented by "ma".
13065 * In other words, plug in "ma" in the domain or range of "map".
13066 * The result is a map that lives in the same space as "map"
13067 * except that the domain or range has been replaced by
13068 * the domain space of "ma".
13070 * The parameters are assumed to have been aligned.
13072 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13073 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13078 map
= isl_map_cow(map
);
13079 ma
= isl_multi_aff_align_divs(ma
);
13082 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13085 for (i
= 0; i
< map
->n
; ++i
) {
13086 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13087 isl_multi_aff_copy(ma
));
13092 space
= isl_multi_aff_get_domain_space(ma
);
13093 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13095 isl_space_free(map
->dim
);
13100 isl_multi_aff_free(ma
);
13102 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13103 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13106 isl_multi_aff_free(ma
);
13111 /* Compute the preimage of the domain or range (depending on "type")
13112 * of "map" under the function represented by "ma".
13113 * In other words, plug in "ma" in the domain or range of "map".
13114 * The result is a map that lives in the same space as "map"
13115 * except that the domain or range has been replaced by
13116 * the domain space of "ma".
13118 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13119 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13126 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13130 return map_preimage_multi_aff(map
, type
, ma
);
13132 if (isl_map_check_named_params(map
) < 0)
13134 if (!isl_space_has_named_params(ma
->space
))
13135 isl_die(map
->ctx
, isl_error_invalid
,
13136 "unaligned unnamed parameters", goto error
);
13137 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13138 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13140 return map_preimage_multi_aff(map
, type
, ma
);
13142 isl_multi_aff_free(ma
);
13143 return isl_map_free(map
);
13146 /* Compute the preimage of "set" under the function represented by "ma".
13147 * In other words, plug in "ma" in "set". The result is a set
13148 * that lives in the domain space of "ma".
13150 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13151 __isl_take isl_multi_aff
*ma
)
13153 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13156 /* Compute the preimage of the domain of "map" under the function
13157 * represented by "ma".
13158 * In other words, plug in "ma" in the domain of "map".
13159 * The result is a map that lives in the same space as "map"
13160 * except that the domain has been replaced by the domain space of "ma".
13162 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13163 __isl_take isl_multi_aff
*ma
)
13165 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13168 /* Compute the preimage of the range of "map" under the function
13169 * represented by "ma".
13170 * In other words, plug in "ma" in the range of "map".
13171 * The result is a map that lives in the same space as "map"
13172 * except that the range has been replaced by the domain space of "ma".
13174 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13175 __isl_take isl_multi_aff
*ma
)
13177 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13180 /* Compute the preimage of "map" under the function represented by "pma".
13181 * In other words, plug in "pma" in the domain or range of "map".
13182 * The result is a map that lives in the same space as "map",
13183 * except that the space of type "type" has been replaced by
13184 * the domain space of "pma".
13186 * The parameters of "map" and "pma" are assumed to have been aligned.
13188 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13189 __isl_take isl_map
*map
, enum isl_dim_type type
,
13190 __isl_take isl_pw_multi_aff
*pma
)
13199 isl_pw_multi_aff_free(pma
);
13200 res
= isl_map_empty(isl_map_get_space(map
));
13205 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13206 isl_multi_aff_copy(pma
->p
[0].maff
));
13207 if (type
== isl_dim_in
)
13208 res
= isl_map_intersect_domain(res
,
13209 isl_map_copy(pma
->p
[0].set
));
13211 res
= isl_map_intersect_range(res
,
13212 isl_map_copy(pma
->p
[0].set
));
13214 for (i
= 1; i
< pma
->n
; ++i
) {
13217 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13218 isl_multi_aff_copy(pma
->p
[i
].maff
));
13219 if (type
== isl_dim_in
)
13220 res_i
= isl_map_intersect_domain(res_i
,
13221 isl_map_copy(pma
->p
[i
].set
));
13223 res_i
= isl_map_intersect_range(res_i
,
13224 isl_map_copy(pma
->p
[i
].set
));
13225 res
= isl_map_union(res
, res_i
);
13228 isl_pw_multi_aff_free(pma
);
13232 isl_pw_multi_aff_free(pma
);
13237 /* Compute the preimage of "map" under the function represented by "pma".
13238 * In other words, plug in "pma" in the domain or range of "map".
13239 * The result is a map that lives in the same space as "map",
13240 * except that the space of type "type" has been replaced by
13241 * the domain space of "pma".
13243 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13244 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13251 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13255 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13257 if (isl_map_check_named_params(map
) < 0)
13259 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13261 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13262 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13264 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13266 isl_pw_multi_aff_free(pma
);
13267 return isl_map_free(map
);
13270 /* Compute the preimage of "set" under the function represented by "pma".
13271 * In other words, plug in "pma" in "set". The result is a set
13272 * that lives in the domain space of "pma".
13274 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13275 __isl_take isl_pw_multi_aff
*pma
)
13277 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13280 /* Compute the preimage of the domain of "map" under the function
13281 * represented by "pma".
13282 * In other words, plug in "pma" in the domain of "map".
13283 * The result is a map that lives in the same space as "map",
13284 * except that domain space has been replaced by the domain space of "pma".
13286 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13287 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13289 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13292 /* Compute the preimage of the range of "map" under the function
13293 * represented by "pma".
13294 * In other words, plug in "pma" in the range of "map".
13295 * The result is a map that lives in the same space as "map",
13296 * except that range space has been replaced by the domain space of "pma".
13298 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13299 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13301 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13304 /* Compute the preimage of "map" under the function represented by "mpa".
13305 * In other words, plug in "mpa" in the domain or range of "map".
13306 * The result is a map that lives in the same space as "map",
13307 * except that the space of type "type" has been replaced by
13308 * the domain space of "mpa".
13310 * If the map does not involve any constraints that refer to the
13311 * dimensions of the substituted space, then the only possible
13312 * effect of "mpa" on the map is to map the space to a different space.
13313 * We create a separate isl_multi_aff to effectuate this change
13314 * in order to avoid spurious splitting of the map along the pieces
13316 * If "mpa" has a non-trivial explicit domain, however,
13317 * then the full substitution should be performed.
13319 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13320 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13324 isl_pw_multi_aff
*pma
;
13329 n
= isl_map_dim(map
, type
);
13330 full
= isl_map_involves_dims(map
, type
, 0, n
);
13331 if (full
>= 0 && !full
)
13332 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13339 space
= isl_multi_pw_aff_get_space(mpa
);
13340 isl_multi_pw_aff_free(mpa
);
13341 ma
= isl_multi_aff_zero(space
);
13342 return isl_map_preimage_multi_aff(map
, type
, ma
);
13345 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13346 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13349 isl_multi_pw_aff_free(mpa
);
13353 /* Compute the preimage of "map" under the function represented by "mpa".
13354 * In other words, plug in "mpa" in the domain "map".
13355 * The result is a map that lives in the same space as "map",
13356 * except that domain space has been replaced by the domain space of "mpa".
13358 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13359 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13361 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13364 /* Compute the preimage of "set" by the function represented by "mpa".
13365 * In other words, plug in "mpa" in "set".
13367 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13368 __isl_take isl_multi_pw_aff
*mpa
)
13370 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13373 /* Return a copy of the equality constraints of "bset" as a matrix.
13375 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13376 __isl_keep isl_basic_set
*bset
)
13384 ctx
= isl_basic_set_get_ctx(bset
);
13385 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13386 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13389 /* Are the "n" "coefficients" starting at "first" of the integer division
13390 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13392 * The "coefficient" at position 0 is the denominator.
13393 * The "coefficient" at position 1 is the constant term.
13395 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13396 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13397 unsigned first
, unsigned n
)
13399 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13400 return isl_bool_error
;
13401 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13402 return isl_bool_error
;
13403 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13404 bmap2
->div
[pos2
] + first
, n
);
13407 /* Are the integer division expressions at position "pos1" in "bmap1" and
13408 * "pos2" in "bmap2" equal to each other, except that the constant terms
13411 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13412 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13413 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13418 if (!bmap1
|| !bmap2
)
13419 return isl_bool_error
;
13420 total
= isl_basic_map_total_dim(bmap1
);
13421 if (total
!= isl_basic_map_total_dim(bmap2
))
13422 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13423 "incomparable div expressions", return isl_bool_error
);
13424 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13426 if (equal
< 0 || !equal
)
13428 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13430 if (equal
< 0 || equal
)
13431 return isl_bool_not(equal
);
13432 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13436 /* Replace the numerator of the constant term of the integer division
13437 * expression at position "div" in "bmap" by "value".
13438 * The caller guarantees that this does not change the meaning
13441 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13442 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13444 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13445 return isl_basic_map_free(bmap
);
13447 isl_int_set_si(bmap
->div
[div
][1], value
);
13452 /* Is the point "inner" internal to inequality constraint "ineq"
13454 * The point is considered to be internal to the inequality constraint,
13455 * if it strictly lies on the positive side of the inequality constraint,
13456 * or if it lies on the constraint and the constraint is lexico-positive.
13458 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13459 __isl_keep isl_basic_set
*bset
, int ineq
)
13465 if (!inner
|| !bset
)
13466 return isl_bool_error
;
13468 ctx
= isl_basic_set_get_ctx(bset
);
13469 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13470 &ctx
->normalize_gcd
);
13471 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13472 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13474 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13475 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13476 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13479 /* Tighten the inequality constraints of "bset" that are outward with respect
13480 * to the point "vec".
13481 * That is, tighten the constraints that are not satisfied by "vec".
13483 * "vec" is a point internal to some superset S of "bset" that is used
13484 * to make the subsets of S disjoint, by tightening one half of the constraints
13485 * that separate two subsets. In particular, the constraints of S
13486 * are all satisfied by "vec" and should not be tightened.
13487 * Of the internal constraints, those that have "vec" on the outside
13488 * are tightened. The shared facet is included in the adjacent subset
13489 * with the opposite constraint.
13490 * For constraints that saturate "vec", this criterion cannot be used
13491 * to determine which of the two sides should be tightened.
13492 * Instead, the sign of the first non-zero coefficient is used
13493 * to make this choice. Note that this second criterion is never used
13494 * on the constraints of S since "vec" is interior to "S".
13496 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13497 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13501 bset
= isl_basic_set_cow(bset
);
13504 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13507 internal
= is_internal(vec
, bset
, j
);
13509 return isl_basic_set_free(bset
);
13512 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13518 /* Replace the variables x of type "type" starting at "first" in "bmap"
13519 * by x' with x = M x' with M the matrix trans.
13520 * That is, replace the corresponding coefficients c by c M.
13522 * The transformation matrix should be a square matrix.
13524 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13525 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13526 __isl_take isl_mat
*trans
)
13530 bmap
= isl_basic_map_cow(bmap
);
13531 if (!bmap
|| !trans
)
13534 if (trans
->n_row
!= trans
->n_col
)
13535 isl_die(trans
->ctx
, isl_error_invalid
,
13536 "expecting square transformation matrix", goto error
);
13537 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13538 isl_die(trans
->ctx
, isl_error_invalid
,
13539 "oversized transformation matrix", goto error
);
13541 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13543 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13544 isl_mat_copy(trans
)) < 0)
13546 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13547 isl_mat_copy(trans
)) < 0)
13549 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13550 isl_mat_copy(trans
)) < 0)
13553 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13554 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13556 isl_mat_free(trans
);
13559 isl_mat_free(trans
);
13560 isl_basic_map_free(bmap
);
13564 /* Replace the variables x of type "type" starting at "first" in "bset"
13565 * by x' with x = M x' with M the matrix trans.
13566 * That is, replace the corresponding coefficients c by c M.
13568 * The transformation matrix should be a square matrix.
13570 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13571 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13572 __isl_take isl_mat
*trans
)
13574 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);