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 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
56 case isl_dim_param
: return dim
->nparam
;
57 case isl_dim_in
: return dim
->n_in
;
58 case isl_dim_out
: return dim
->n_out
;
59 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
64 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
67 case isl_dim_param
: return 1;
68 case isl_dim_in
: return 1 + dim
->nparam
;
69 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
74 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
75 enum isl_dim_type type
)
80 case isl_dim_cst
: return 1;
83 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
84 case isl_dim_div
: return bmap
->n_div
;
85 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
90 /* Return the space of "map".
92 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
94 return map
? map
->dim
: NULL
;
97 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
99 return map
? n(map
->dim
, type
) : 0;
102 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
104 return set
? n(set
->dim
, type
) : 0;
107 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
108 enum isl_dim_type type
)
117 case isl_dim_cst
: return 0;
118 case isl_dim_param
: return 1;
119 case isl_dim_in
: return 1 + space
->nparam
;
120 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
121 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
127 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
128 enum isl_dim_type type
)
130 return isl_basic_map_offset(bset
, type
);
133 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
135 return pos(map
->dim
, type
);
138 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
139 enum isl_dim_type type
)
141 return isl_basic_map_dim(bset
, type
);
144 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
146 return isl_basic_set_dim(bset
, isl_dim_set
);
149 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
151 return isl_basic_set_dim(bset
, isl_dim_param
);
154 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
158 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
161 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
163 return isl_set_dim(set
, isl_dim_set
);
166 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
168 return isl_set_dim(set
, isl_dim_param
);
171 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
173 return bmap
? bmap
->dim
->n_in
: 0;
176 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
178 return bmap
? bmap
->dim
->n_out
: 0;
181 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
183 return bmap
? bmap
->dim
->nparam
: 0;
186 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
188 return bmap
? bmap
->n_div
: 0;
191 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
193 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
196 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
198 return map
? map
->dim
->n_in
: 0;
201 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
203 return map
? map
->dim
->n_out
: 0;
206 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
208 return map
? map
->dim
->nparam
: 0;
211 /* Return the number of equality constraints in the description of "bmap".
212 * Return -1 on error.
214 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
221 /* Return the number of equality constraints in the description of "bset".
222 * Return -1 on error.
224 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
226 return isl_basic_map_n_equality(bset_to_bmap(bset
));
229 /* Return the number of inequality constraints in the description of "bmap".
230 * Return -1 on error.
232 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
239 /* Return the number of inequality constraints in the description of "bset".
240 * Return -1 on error.
242 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
244 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
247 /* Do "bmap1" and "bmap2" have the same parameters?
249 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
250 __isl_keep isl_basic_map
*bmap2
)
252 isl_space
*space1
, *space2
;
254 space1
= isl_basic_map_peek_space(bmap1
);
255 space2
= isl_basic_map_peek_space(bmap2
);
256 return isl_space_has_equal_params(space1
, space2
);
259 /* Do "map1" and "map2" have the same parameters?
261 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
262 __isl_keep isl_map
*map2
)
264 isl_space
*space1
, *space2
;
266 space1
= isl_map_peek_space(map1
);
267 space2
= isl_map_peek_space(map2
);
268 return isl_space_has_equal_params(space1
, space2
);
271 /* Do "map" and "set" have the same parameters?
273 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
274 __isl_keep isl_set
*set
)
276 return isl_map_has_equal_params(map
, set_to_map(set
));
279 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
280 __isl_keep isl_set
*set
)
284 return isl_bool_error
;
285 m
= isl_map_has_equal_params(map
, set_to_map(set
));
288 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
289 set
->dim
, isl_dim_set
);
292 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
293 __isl_keep isl_basic_set
*bset
)
297 return isl_bool_error
;
298 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
301 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
302 bset
->dim
, isl_dim_set
);
305 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
306 __isl_keep isl_set
*set
)
310 return isl_bool_error
;
311 m
= isl_map_has_equal_params(map
, set_to_map(set
));
314 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
315 set
->dim
, isl_dim_set
);
318 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
319 __isl_keep isl_basic_set
*bset
)
323 return isl_bool_error
;
324 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
327 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
328 bset
->dim
, isl_dim_set
);
331 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
333 return bmap
? bmap
->ctx
: NULL
;
336 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
338 return bset
? bset
->ctx
: NULL
;
341 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
343 return map
? map
->ctx
: NULL
;
346 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
348 return set
? set
->ctx
: NULL
;
351 /* Return the space of "bmap".
353 __isl_keep isl_space
*isl_basic_map_peek_space(
354 __isl_keep
const isl_basic_map
*bmap
)
356 return bmap
? bmap
->dim
: NULL
;
359 /* Return the space of "bset".
361 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
363 return isl_basic_map_peek_space(bset_to_bmap(bset
));
366 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
368 return isl_space_copy(isl_basic_map_peek_space(bmap
));
371 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
373 return isl_basic_map_get_space(bset_to_bmap(bset
));
376 /* Extract the divs in "bmap" as a matrix.
378 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
389 ctx
= isl_basic_map_get_ctx(bmap
);
390 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
391 cols
= 1 + 1 + total
+ bmap
->n_div
;
392 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
396 for (i
= 0; i
< bmap
->n_div
; ++i
)
397 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
402 /* Extract the divs in "bset" as a matrix.
404 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
406 return isl_basic_map_get_divs(bset
);
409 __isl_give isl_local_space
*isl_basic_map_get_local_space(
410 __isl_keep isl_basic_map
*bmap
)
417 div
= isl_basic_map_get_divs(bmap
);
418 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
421 __isl_give isl_local_space
*isl_basic_set_get_local_space(
422 __isl_keep isl_basic_set
*bset
)
424 return isl_basic_map_get_local_space(bset
);
427 /* For each known div d = floor(f/m), add the constraints
430 * -(f-(m-1)) + m d >= 0
432 * Do not finalize the result.
434 static __isl_give isl_basic_map
*add_known_div_constraints(
435 __isl_take isl_basic_map
*bmap
)
442 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
445 bmap
= isl_basic_map_cow(bmap
);
446 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
449 for (i
= 0; i
< n_div
; ++i
) {
450 if (isl_int_is_zero(bmap
->div
[i
][0]))
452 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
453 return isl_basic_map_free(bmap
);
459 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
460 __isl_take isl_local_space
*ls
)
469 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
470 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
471 n_div
, 0, 2 * n_div
);
473 for (i
= 0; i
< n_div
; ++i
)
474 if (isl_basic_map_alloc_div(bmap
) < 0)
477 for (i
= 0; i
< n_div
; ++i
)
478 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
479 bmap
= add_known_div_constraints(bmap
);
481 isl_local_space_free(ls
);
484 isl_local_space_free(ls
);
485 isl_basic_map_free(bmap
);
489 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
490 __isl_take isl_local_space
*ls
)
492 return isl_basic_map_from_local_space(ls
);
495 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
497 return isl_space_copy(isl_map_peek_space(map
));
500 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
504 return isl_space_copy(set
->dim
);
507 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
508 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
510 bmap
= isl_basic_map_cow(bmap
);
513 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
516 bmap
= isl_basic_map_finalize(bmap
);
519 isl_basic_map_free(bmap
);
523 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
524 __isl_take isl_basic_set
*bset
, const char *s
)
526 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
529 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
530 enum isl_dim_type type
)
532 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
535 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
536 enum isl_dim_type type
, const char *s
)
540 map
= isl_map_cow(map
);
544 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
548 for (i
= 0; i
< map
->n
; ++i
) {
549 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
560 /* Replace the identifier of the tuple of type "type" by "id".
562 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
563 __isl_take isl_basic_map
*bmap
,
564 enum isl_dim_type type
, __isl_take isl_id
*id
)
566 bmap
= isl_basic_map_cow(bmap
);
569 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
571 return isl_basic_map_free(bmap
);
572 bmap
= isl_basic_map_finalize(bmap
);
579 /* Replace the identifier of the tuple by "id".
581 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
582 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
584 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
587 /* Does the input or output tuple have a name?
589 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
591 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
594 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
595 enum isl_dim_type type
)
597 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
600 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
603 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
607 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
608 enum isl_dim_type type
, __isl_take isl_id
*id
)
610 map
= isl_map_cow(map
);
614 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
616 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
622 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
623 __isl_take isl_id
*id
)
625 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
628 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
629 enum isl_dim_type type
)
631 map
= isl_map_cow(map
);
635 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
637 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
640 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
642 return isl_map_reset_tuple_id(set
, isl_dim_set
);
645 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
647 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
650 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
651 enum isl_dim_type type
)
653 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
656 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
658 return isl_map_has_tuple_id(set
, isl_dim_set
);
661 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
663 return isl_map_get_tuple_id(set
, isl_dim_set
);
666 /* Does the set tuple have a name?
668 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
671 return isl_bool_error
;
672 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
676 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
678 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
681 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
683 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
686 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
687 enum isl_dim_type type
, unsigned pos
)
689 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
692 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
693 enum isl_dim_type type
, unsigned pos
)
695 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
698 /* Does the given dimension have a name?
700 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
701 enum isl_dim_type type
, unsigned pos
)
704 return isl_bool_error
;
705 return isl_space_has_dim_name(map
->dim
, type
, pos
);
708 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
709 enum isl_dim_type type
, unsigned pos
)
711 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
714 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
715 enum isl_dim_type type
, unsigned pos
)
717 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
720 /* Does the given dimension have a name?
722 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
723 enum isl_dim_type type
, unsigned pos
)
726 return isl_bool_error
;
727 return isl_space_has_dim_name(set
->dim
, type
, pos
);
730 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
731 __isl_take isl_basic_map
*bmap
,
732 enum isl_dim_type type
, unsigned pos
, const char *s
)
734 bmap
= isl_basic_map_cow(bmap
);
737 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
740 return isl_basic_map_finalize(bmap
);
742 isl_basic_map_free(bmap
);
746 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
747 enum isl_dim_type type
, unsigned pos
, const char *s
)
751 map
= isl_map_cow(map
);
755 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
759 for (i
= 0; i
< map
->n
; ++i
) {
760 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
771 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
772 __isl_take isl_basic_set
*bset
,
773 enum isl_dim_type type
, unsigned pos
, const char *s
)
775 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
779 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
780 enum isl_dim_type type
, unsigned pos
, const char *s
)
782 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
786 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
787 enum isl_dim_type type
, unsigned pos
)
790 return isl_bool_error
;
791 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
794 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
795 enum isl_dim_type type
, unsigned pos
)
797 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
800 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
801 enum isl_dim_type type
, unsigned pos
)
803 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
806 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
807 enum isl_dim_type type
, unsigned pos
)
809 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
812 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
813 enum isl_dim_type type
, unsigned pos
)
815 return isl_map_has_dim_id(set
, type
, pos
);
818 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
819 enum isl_dim_type type
, unsigned pos
)
821 return isl_map_get_dim_id(set
, type
, pos
);
824 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
825 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
827 map
= isl_map_cow(map
);
831 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
833 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
839 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
840 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
842 return isl_map_set_dim_id(set
, type
, pos
, id
);
845 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
846 __isl_keep isl_id
*id
)
850 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
853 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
854 __isl_keep isl_id
*id
)
856 return isl_map_find_dim_by_id(set
, type
, id
);
859 /* Return the position of the dimension of the given type and name
861 * Return -1 if no such dimension can be found.
863 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
864 enum isl_dim_type type
, const char *name
)
868 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
871 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
876 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
879 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
882 return isl_map_find_dim_by_name(set
, type
, name
);
885 /* Check whether equality i of bset is a pure stride constraint
886 * on a single dimension, i.e., of the form
890 * with k a constant and e an existentially quantified variable.
892 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
901 return isl_bool_error
;
903 if (!isl_int_is_zero(bset
->eq
[i
][0]))
904 return isl_bool_false
;
906 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
907 d
= isl_basic_set_dim(bset
, isl_dim_set
);
908 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
910 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
911 return isl_bool_false
;
912 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
914 return isl_bool_false
;
915 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
917 return isl_bool_false
;
919 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
921 return isl_bool_false
;
922 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
923 n_div
- pos2
- 1) != -1)
924 return isl_bool_false
;
925 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
926 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
927 return isl_bool_false
;
929 return isl_bool_true
;
932 /* Reset the user pointer on all identifiers of parameters and tuples
933 * of the space of "map".
935 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
939 space
= isl_map_get_space(map
);
940 space
= isl_space_reset_user(space
);
941 map
= isl_map_reset_space(map
, space
);
946 /* Reset the user pointer on all identifiers of parameters and tuples
947 * of the space of "set".
949 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
951 return isl_map_reset_user(set
);
954 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
957 return isl_bool_error
;
958 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
961 /* Has "map" been marked as a rational map?
962 * In particular, have all basic maps in "map" been marked this way?
963 * An empty map is not considered to be rational.
964 * Maps where only some of the basic maps are marked rational
967 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
973 return isl_bool_error
;
975 return isl_bool_false
;
976 rational
= isl_basic_map_is_rational(map
->p
[0]);
979 for (i
= 1; i
< map
->n
; ++i
) {
982 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
985 if (rational
!= rational_i
)
986 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
987 "mixed rational and integer basic maps "
988 "not supported", return isl_bool_error
);
994 /* Has "set" been marked as a rational set?
995 * In particular, have all basic set in "set" been marked this way?
996 * An empty set is not considered to be rational.
997 * Sets where only some of the basic sets are marked rational
1000 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1002 return isl_map_is_rational(set
);
1005 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1007 return isl_basic_map_is_rational(bset
);
1010 /* Does "bmap" contain any rational points?
1012 * If "bmap" has an equality for each dimension, equating the dimension
1013 * to an integer constant, then it has no rational points, even if it
1014 * is marked as rational.
1016 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1018 isl_bool has_rational
= isl_bool_true
;
1022 return isl_bool_error
;
1023 if (isl_basic_map_plain_is_empty(bmap
))
1024 return isl_bool_false
;
1025 if (!isl_basic_map_is_rational(bmap
))
1026 return isl_bool_false
;
1027 bmap
= isl_basic_map_copy(bmap
);
1028 bmap
= isl_basic_map_implicit_equalities(bmap
);
1030 return isl_bool_error
;
1031 total
= isl_basic_map_total_dim(bmap
);
1032 if (bmap
->n_eq
== total
) {
1034 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1035 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1038 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1039 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1041 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1046 if (i
== bmap
->n_eq
)
1047 has_rational
= isl_bool_false
;
1049 isl_basic_map_free(bmap
);
1051 return has_rational
;
1054 /* Does "map" contain any rational points?
1056 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1059 isl_bool has_rational
;
1062 return isl_bool_error
;
1063 for (i
= 0; i
< map
->n
; ++i
) {
1064 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1065 if (has_rational
< 0 || has_rational
)
1066 return has_rational
;
1068 return isl_bool_false
;
1071 /* Does "set" contain any rational points?
1073 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1075 return isl_map_has_rational(set
);
1078 /* Is this basic set a parameter domain?
1080 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1083 return isl_bool_error
;
1084 return isl_space_is_params(bset
->dim
);
1087 /* Is this set a parameter domain?
1089 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1092 return isl_bool_error
;
1093 return isl_space_is_params(set
->dim
);
1096 /* Is this map actually a parameter domain?
1097 * Users should never call this function. Outside of isl,
1098 * a map can never be a parameter domain.
1100 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1103 return isl_bool_error
;
1104 return isl_space_is_params(map
->dim
);
1107 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
1108 struct isl_basic_map
*bmap
, unsigned extra
,
1109 unsigned n_eq
, unsigned n_ineq
)
1112 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1117 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1118 if (isl_blk_is_error(bmap
->block
))
1121 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1122 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1126 bmap
->block2
= isl_blk_empty();
1129 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1130 if (isl_blk_is_error(bmap
->block2
))
1133 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1138 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1139 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1141 for (i
= 0; i
< extra
; ++i
)
1142 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1146 bmap
->c_size
= n_eq
+ n_ineq
;
1147 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1148 bmap
->extra
= extra
;
1152 bmap
->sample
= NULL
;
1156 isl_basic_map_free(bmap
);
1160 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1161 unsigned nparam
, unsigned dim
, unsigned extra
,
1162 unsigned n_eq
, unsigned n_ineq
)
1164 struct isl_basic_map
*bmap
;
1167 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1171 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1172 return bset_from_bmap(bmap
);
1175 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1176 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1178 struct isl_basic_map
*bmap
;
1181 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1182 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1183 return bset_from_bmap(bmap
);
1185 isl_space_free(dim
);
1189 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1190 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1192 struct isl_basic_map
*bmap
;
1196 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1201 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1203 isl_space_free(dim
);
1207 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1208 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1209 unsigned n_eq
, unsigned n_ineq
)
1211 struct isl_basic_map
*bmap
;
1214 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1218 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1222 static void dup_constraints(
1223 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1226 unsigned total
= isl_basic_map_total_dim(src
);
1228 for (i
= 0; i
< src
->n_eq
; ++i
) {
1229 int j
= isl_basic_map_alloc_equality(dst
);
1230 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1233 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1234 int j
= isl_basic_map_alloc_inequality(dst
);
1235 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1238 for (i
= 0; i
< src
->n_div
; ++i
) {
1239 int j
= isl_basic_map_alloc_div(dst
);
1240 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1242 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1245 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1247 struct isl_basic_map
*dup
;
1251 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1252 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1255 dup_constraints(dup
, bmap
);
1256 dup
->flags
= bmap
->flags
;
1257 dup
->sample
= isl_vec_copy(bmap
->sample
);
1261 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1263 struct isl_basic_map
*dup
;
1265 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1266 return bset_from_bmap(dup
);
1269 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1274 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1278 return isl_basic_set_dup(bset
);
1281 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1290 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1295 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1299 bmap
= isl_basic_map_dup(bmap
);
1301 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1305 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1314 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1319 if (--bmap
->ref
> 0)
1322 isl_ctx_deref(bmap
->ctx
);
1324 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1326 isl_blk_free(bmap
->ctx
, bmap
->block
);
1327 isl_vec_free(bmap
->sample
);
1328 isl_space_free(bmap
->dim
);
1334 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1336 return isl_basic_map_free(bset_to_bmap(bset
));
1339 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1341 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1344 /* Check that "map" has only named parameters, reporting an error
1347 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1349 return isl_space_check_named_params(isl_map_peek_space(map
));
1352 /* Check that "bmap" has only named parameters, reporting an error
1355 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1357 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1360 /* Check that "bmap1" and "bmap2" have the same parameters,
1361 * reporting an error if they do not.
1363 static isl_stat
isl_basic_map_check_equal_params(
1364 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1368 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1370 return isl_stat_error
;
1372 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1373 "parameters don't match", return isl_stat_error
);
1377 __isl_give isl_map
*isl_map_align_params_map_map_and(
1378 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1379 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1380 __isl_take isl_map
*map2
))
1384 if (isl_map_has_equal_params(map1
, map2
))
1385 return fn(map1
, map2
);
1386 if (isl_map_check_named_params(map1
) < 0)
1388 if (isl_map_check_named_params(map2
) < 0)
1390 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1391 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1392 return fn(map1
, map2
);
1399 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1400 __isl_keep isl_map
*map2
,
1401 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1406 return isl_bool_error
;
1407 if (isl_map_has_equal_params(map1
, map2
))
1408 return fn(map1
, map2
);
1409 if (isl_map_check_named_params(map1
) < 0)
1410 return isl_bool_error
;
1411 if (isl_map_check_named_params(map2
) < 0)
1412 return isl_bool_error
;
1413 map1
= isl_map_copy(map1
);
1414 map2
= isl_map_copy(map2
);
1415 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1416 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1423 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1425 struct isl_ctx
*ctx
;
1429 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1430 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1432 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1433 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1434 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1435 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1436 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1437 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1439 int j
= isl_basic_map_alloc_inequality(bmap
);
1443 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1444 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1451 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1452 bmap
->extra
- bmap
->n_div
);
1453 return bmap
->n_eq
++;
1456 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1458 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1461 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1465 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1470 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1472 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1475 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1480 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1482 if (pos
!= bmap
->n_eq
- 1) {
1484 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1485 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1491 /* Turn inequality "pos" of "bmap" into an equality.
1493 * In particular, we move the inequality in front of the equalities
1494 * and move the last inequality in the position of the moved inequality.
1495 * Note that isl_tab_make_equalities_explicit depends on this particular
1496 * change in the ordering of the constraints.
1498 void isl_basic_map_inequality_to_equality(
1499 struct isl_basic_map
*bmap
, unsigned pos
)
1503 t
= bmap
->ineq
[pos
];
1504 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1505 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1510 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1511 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1512 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1513 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1516 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1518 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1521 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1523 struct isl_ctx
*ctx
;
1527 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1528 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1529 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1530 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1531 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1532 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1533 1 + isl_basic_map_total_dim(bmap
),
1534 bmap
->extra
- bmap
->n_div
);
1535 return bmap
->n_ineq
++;
1538 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1540 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1543 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1547 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1552 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1554 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1557 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1562 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1564 if (pos
!= bmap
->n_ineq
- 1) {
1565 t
= bmap
->ineq
[pos
];
1566 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1567 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1568 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1574 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1576 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1579 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1584 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1587 k
= isl_basic_map_alloc_equality(bmap
);
1590 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1593 isl_basic_map_free(bmap
);
1597 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1600 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1603 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1608 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1611 k
= isl_basic_map_alloc_inequality(bmap
);
1614 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1617 isl_basic_map_free(bmap
);
1621 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1624 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1627 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1631 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1632 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1633 1 + 1 + isl_basic_map_total_dim(bmap
),
1634 bmap
->extra
- bmap
->n_div
);
1635 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1636 return bmap
->n_div
++;
1639 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1641 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1644 /* Check that there are "n" dimensions of type "type" starting at "first"
1647 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1648 enum isl_dim_type type
, unsigned first
, unsigned n
)
1653 return isl_stat_error
;
1654 dim
= isl_basic_map_dim(bmap
, type
);
1655 if (first
+ n
> dim
|| first
+ n
< first
)
1656 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1657 "position or range out of bounds",
1658 return isl_stat_error
);
1662 /* Insert an extra integer division, prescribed by "div", to "bmap"
1663 * at (integer division) position "pos".
1665 * The integer division is first added at the end and then moved
1666 * into the right position.
1668 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1669 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1673 bmap
= isl_basic_map_cow(bmap
);
1675 return isl_basic_map_free(bmap
);
1677 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1678 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1679 "unexpected size", return isl_basic_map_free(bmap
));
1680 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1681 return isl_basic_map_free(bmap
);
1683 bmap
= isl_basic_map_extend_space(bmap
,
1684 isl_basic_map_get_space(bmap
), 1, 0, 2);
1685 k
= isl_basic_map_alloc_div(bmap
);
1687 return isl_basic_map_free(bmap
);
1688 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1689 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1691 for (i
= k
; i
> pos
; --i
)
1692 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1697 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1700 return isl_stat_error
;
1701 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1706 /* Copy constraint from src to dst, putting the vars of src at offset
1707 * dim_off in dst and the divs of src at offset div_off in dst.
1708 * If both sets are actually map, then dim_off applies to the input
1711 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1712 struct isl_basic_map
*src_map
, isl_int
*src
,
1713 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1715 unsigned src_nparam
= isl_basic_map_dim(src_map
, isl_dim_param
);
1716 unsigned dst_nparam
= isl_basic_map_dim(dst_map
, isl_dim_param
);
1717 unsigned src_in
= isl_basic_map_dim(src_map
, isl_dim_in
);
1718 unsigned dst_in
= isl_basic_map_dim(dst_map
, isl_dim_in
);
1719 unsigned src_out
= isl_basic_map_dim(src_map
, isl_dim_out
);
1720 unsigned dst_out
= isl_basic_map_dim(dst_map
, isl_dim_out
);
1721 isl_int_set(dst
[0], src
[0]);
1722 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1723 if (dst_nparam
> src_nparam
)
1724 isl_seq_clr(dst
+1+src_nparam
,
1725 dst_nparam
- src_nparam
);
1726 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1727 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1729 isl_min(dst_in
-in_off
, src_in
));
1730 if (dst_in
-in_off
> src_in
)
1731 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1732 dst_in
- in_off
- src_in
);
1733 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1734 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1735 src
+1+src_nparam
+src_in
,
1736 isl_min(dst_out
-out_off
, src_out
));
1737 if (dst_out
-out_off
> src_out
)
1738 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1739 dst_out
- out_off
- src_out
);
1740 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1741 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1742 src
+1+src_nparam
+src_in
+src_out
,
1743 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1744 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1745 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1746 div_off
+src_map
->n_div
,
1747 dst_map
->n_div
- div_off
- src_map
->n_div
);
1750 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1751 struct isl_basic_map
*src_map
, isl_int
*src
,
1752 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1754 isl_int_set(dst
[0], src
[0]);
1755 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1758 static __isl_give isl_basic_map
*add_constraints(
1759 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1760 unsigned i_pos
, unsigned o_pos
)
1765 if (!bmap1
|| !bmap2
)
1768 div_off
= bmap1
->n_div
;
1770 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1771 int i1
= isl_basic_map_alloc_equality(bmap1
);
1774 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1775 i_pos
, o_pos
, div_off
);
1778 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1779 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1782 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1783 i_pos
, o_pos
, div_off
);
1786 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1787 int i1
= isl_basic_map_alloc_div(bmap1
);
1790 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1791 i_pos
, o_pos
, div_off
);
1794 isl_basic_map_free(bmap2
);
1799 isl_basic_map_free(bmap1
);
1800 isl_basic_map_free(bmap2
);
1804 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1805 struct isl_basic_set
*bset2
, unsigned pos
)
1807 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1808 bset_to_bmap(bset2
), 0, pos
));
1811 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1812 __isl_take isl_basic_map
*base
, __isl_take isl_space
*dim
,
1813 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1815 struct isl_basic_map
*ext
;
1825 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1826 base
->extra
>= base
->n_div
+ extra
;
1828 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1829 room_for_ineq(base
, n_ineq
)) {
1830 isl_space_free(dim
);
1834 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1835 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1836 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1837 extra
+= base
->extra
;
1839 n_ineq
+= base
->n_ineq
;
1841 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1847 ext
->sample
= isl_vec_copy(base
->sample
);
1848 flags
= base
->flags
;
1849 ext
= add_constraints(ext
, base
, 0, 0);
1852 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1858 isl_space_free(dim
);
1859 isl_basic_map_free(base
);
1863 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1864 __isl_take isl_basic_set
*base
,
1865 __isl_take isl_space
*dim
, unsigned extra
,
1866 unsigned n_eq
, unsigned n_ineq
)
1868 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1869 dim
, extra
, n_eq
, n_ineq
));
1872 struct isl_basic_map
*isl_basic_map_extend_constraints(
1873 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1877 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1881 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1882 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1883 unsigned n_eq
, unsigned n_ineq
)
1885 struct isl_basic_map
*bmap
;
1890 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1894 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1897 isl_basic_map_free(base
);
1901 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1902 unsigned nparam
, unsigned dim
, unsigned extra
,
1903 unsigned n_eq
, unsigned n_ineq
)
1905 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1906 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1909 struct isl_basic_set
*isl_basic_set_extend_constraints(
1910 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1912 isl_basic_map
*bmap
= bset_to_bmap(base
);
1913 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1914 return bset_from_bmap(bmap
);
1917 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1919 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1922 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1927 if (bmap
->ref
> 1) {
1929 bmap
= isl_basic_map_dup(bmap
);
1932 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1933 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1938 /* Clear all cached information in "map", either because it is about
1939 * to be modified or because it is being freed.
1940 * Always return the same pointer that is passed in.
1941 * This is needed for the use in isl_map_free.
1943 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1945 isl_basic_map_free(map
->cached_simple_hull
[0]);
1946 isl_basic_map_free(map
->cached_simple_hull
[1]);
1947 map
->cached_simple_hull
[0] = NULL
;
1948 map
->cached_simple_hull
[1] = NULL
;
1952 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1954 return isl_map_cow(set
);
1957 /* Return an isl_map that is equal to "map" and that has only
1958 * a single reference.
1960 * If the original input already has only one reference, then
1961 * simply return it, but clear all cached information, since
1962 * it may be rendered invalid by the operations that will be
1963 * performed on the result.
1965 * Otherwise, create a duplicate (without any cached information).
1967 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1973 return clear_caches(map
);
1975 return isl_map_dup(map
);
1978 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1979 unsigned a_len
, unsigned b_len
)
1981 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1982 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1983 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1986 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1987 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1995 isl_assert(bmap
->ctx
,
1996 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1998 if (n1
== 0 || n2
== 0)
2001 bmap
= isl_basic_map_cow(bmap
);
2005 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2006 if (isl_blk_is_error(blk
))
2009 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2011 bmap
->eq
[i
] + pos
, n1
, n2
);
2013 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2015 bmap
->ineq
[i
] + pos
, n1
, n2
);
2017 for (i
= 0; i
< bmap
->n_div
; ++i
)
2019 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2021 isl_blk_free(bmap
->ctx
, blk
);
2023 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
2024 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2025 return isl_basic_map_finalize(bmap
);
2027 isl_basic_map_free(bmap
);
2031 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map
*bmap
)
2038 total
= isl_basic_map_total_dim(bmap
);
2039 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2040 return isl_basic_map_free(bmap
);
2041 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2043 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2045 i
= isl_basic_map_alloc_equality(bmap
);
2049 isl_int_set_si(bmap
->eq
[i
][0], 1);
2050 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2051 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2052 isl_vec_free(bmap
->sample
);
2053 bmap
->sample
= NULL
;
2054 return isl_basic_map_finalize(bmap
);
2056 isl_basic_map_free(bmap
);
2060 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set
*bset
)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2066 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2067 __isl_take isl_basic_map
*bmap
)
2072 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2075 bmap
= isl_basic_map_cow(bmap
);
2079 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2081 return isl_basic_map_finalize(bmap
);
2084 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2085 __isl_take isl_basic_set
*bset
)
2087 return isl_basic_map_set_rational(bset
);
2090 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2091 __isl_take isl_basic_set
*bset
)
2096 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2099 bset
= isl_basic_set_cow(bset
);
2103 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2105 return isl_basic_set_finalize(bset
);
2108 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2112 map
= isl_map_cow(map
);
2115 for (i
= 0; i
< map
->n
; ++i
) {
2116 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2126 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2128 return isl_map_set_rational(set
);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2134 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2136 isl_int
*t
= bmap
->div
[a
];
2137 bmap
->div
[a
] = bmap
->div
[b
];
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2147 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2149 swap_div(bmap
, a
, b
);
2151 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2152 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2154 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2155 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2157 for (i
= 0; i
< bmap
->n_div
; ++i
)
2158 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2159 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2162 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2163 * div definitions accordingly.
2165 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
2167 isl_basic_map_swap_div(bset
, a
, b
);
2170 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2172 isl_seq_cpy(c
, c
+ n
, rem
);
2173 isl_seq_clr(c
+ rem
, n
);
2176 /* Drop n dimensions starting at first.
2178 * In principle, this frees up some extra variables as the number
2179 * of columns remains constant, but we would have to extend
2180 * the div array too as the number of rows in this array is assumed
2181 * to be equal to extra.
2183 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2184 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2186 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2189 /* Move "n" divs starting at "first" to the end of the list of divs.
2191 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2192 unsigned first
, unsigned n
)
2197 if (first
+ n
== bmap
->n_div
)
2200 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2203 for (i
= 0; i
< n
; ++i
)
2204 div
[i
] = bmap
->div
[first
+ i
];
2205 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2206 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2207 for (i
= 0; i
< n
; ++i
)
2208 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2212 isl_basic_map_free(bmap
);
2216 /* Check that there are "n" dimensions of type "type" starting at "first"
2219 static isl_stat
isl_map_check_range(__isl_keep isl_map
*map
,
2220 enum isl_dim_type type
, unsigned first
, unsigned n
)
2223 return isl_stat_error
;
2224 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2225 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2226 "position or range out of bounds",
2227 return isl_stat_error
);
2231 /* Drop "n" dimensions of type "type" starting at "first".
2233 * In principle, this frees up some extra variables as the number
2234 * of columns remains constant, but we would have to extend
2235 * the div array too as the number of rows in this array is assumed
2236 * to be equal to extra.
2238 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2239 enum isl_dim_type type
, unsigned first
, unsigned n
)
2249 dim
= isl_basic_map_dim(bmap
, type
);
2250 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
2252 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2255 bmap
= isl_basic_map_cow(bmap
);
2259 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2260 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2261 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2262 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2264 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2265 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2267 for (i
= 0; i
< bmap
->n_div
; ++i
)
2268 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2270 if (type
== isl_dim_div
) {
2271 bmap
= move_divs_last(bmap
, first
, n
);
2274 if (isl_basic_map_free_div(bmap
, n
) < 0)
2275 return isl_basic_map_free(bmap
);
2277 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2281 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2282 bmap
= isl_basic_map_simplify(bmap
);
2283 return isl_basic_map_finalize(bmap
);
2285 isl_basic_map_free(bmap
);
2289 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2290 enum isl_dim_type type
, unsigned first
, unsigned n
)
2292 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2296 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2297 enum isl_dim_type type
, unsigned first
, unsigned n
)
2301 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2302 return isl_map_free(map
);
2304 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2306 map
= isl_map_cow(map
);
2309 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2313 for (i
= 0; i
< map
->n
; ++i
) {
2314 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2318 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2326 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2327 enum isl_dim_type type
, unsigned first
, unsigned n
)
2329 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2333 * We don't cow, as the div is assumed to be redundant.
2335 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2336 __isl_take isl_basic_map
*bmap
, unsigned div
)
2344 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
2346 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
2348 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2349 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2351 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2352 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
2353 isl_basic_map_drop_inequality(bmap
, i
);
2357 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2360 for (i
= 0; i
< bmap
->n_div
; ++i
)
2361 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
2363 if (div
!= bmap
->n_div
- 1) {
2365 isl_int
*t
= bmap
->div
[div
];
2367 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
2368 bmap
->div
[j
] = bmap
->div
[j
+1];
2370 bmap
->div
[bmap
->n_div
- 1] = t
;
2372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2373 if (isl_basic_map_free_div(bmap
, 1) < 0)
2374 return isl_basic_map_free(bmap
);
2378 isl_basic_map_free(bmap
);
2382 /* Eliminate the specified n dimensions starting at first from the
2383 * constraints, without removing the dimensions from the space.
2384 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2386 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2387 enum isl_dim_type type
, unsigned first
, unsigned n
)
2394 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2395 return isl_map_free(map
);
2397 map
= isl_map_cow(map
);
2401 for (i
= 0; i
< map
->n
; ++i
) {
2402 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2412 /* Eliminate the specified n dimensions starting at first from the
2413 * constraints, without removing the dimensions from the space.
2414 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2416 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2417 enum isl_dim_type type
, unsigned first
, unsigned n
)
2419 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2422 /* Eliminate the specified n dimensions starting at first from the
2423 * constraints, without removing the dimensions from the space.
2424 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2426 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2427 unsigned first
, unsigned n
)
2429 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2432 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2433 __isl_take isl_basic_map
*bmap
)
2437 bmap
= isl_basic_map_eliminate_vars(bmap
,
2438 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2442 return isl_basic_map_finalize(bmap
);
2445 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2446 __isl_take isl_basic_set
*bset
)
2448 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2451 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2460 map
= isl_map_cow(map
);
2464 for (i
= 0; i
< map
->n
; ++i
) {
2465 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2475 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2477 return isl_map_remove_divs(set
);
2480 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2481 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2482 unsigned first
, unsigned n
)
2484 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2485 return isl_basic_map_free(bmap
);
2486 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2488 bmap
= isl_basic_map_eliminate_vars(bmap
,
2489 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2492 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2494 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2498 /* Return true if the definition of the given div (recursively) involves
2499 * any of the given variables.
2501 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2502 unsigned first
, unsigned n
)
2505 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2507 if (isl_int_is_zero(bmap
->div
[div
][0]))
2508 return isl_bool_false
;
2509 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2510 return isl_bool_true
;
2512 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2515 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2517 involves
= div_involves_vars(bmap
, i
, first
, n
);
2518 if (involves
< 0 || involves
)
2522 return isl_bool_false
;
2525 /* Try and add a lower and/or upper bound on "div" to "bmap"
2526 * based on inequality "i".
2527 * "total" is the total number of variables (excluding the divs).
2528 * "v" is a temporary object that can be used during the calculations.
2529 * If "lb" is set, then a lower bound should be constructed.
2530 * If "ub" is set, then an upper bound should be constructed.
2532 * The calling function has already checked that the inequality does not
2533 * reference "div", but we still need to check that the inequality is
2534 * of the right form. We'll consider the case where we want to construct
2535 * a lower bound. The construction of upper bounds is similar.
2537 * Let "div" be of the form
2539 * q = floor((a + f(x))/d)
2541 * We essentially check if constraint "i" is of the form
2545 * so that we can use it to derive a lower bound on "div".
2546 * However, we allow a slightly more general form
2550 * with the condition that the coefficients of g(x) - f(x) are all
2552 * Rewriting this constraint as
2556 * adding a + f(x) to both sides and dividing by d, we obtain
2558 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2560 * Taking the floor on both sides, we obtain
2562 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2566 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2568 * In the case of an upper bound, we construct the constraint
2570 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2573 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2574 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2575 unsigned total
, isl_int v
, int lb
, int ub
)
2579 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2581 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2582 bmap
->div
[div
][1 + 1 + j
]);
2583 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2586 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2587 bmap
->div
[div
][1 + 1 + j
]);
2588 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2594 bmap
= isl_basic_map_cow(bmap
);
2595 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2597 int k
= isl_basic_map_alloc_inequality(bmap
);
2600 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2601 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2602 bmap
->div
[div
][1 + j
]);
2603 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2604 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2606 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2609 int k
= isl_basic_map_alloc_inequality(bmap
);
2612 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2613 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2614 bmap
->div
[div
][1 + j
]);
2615 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2616 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2618 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2621 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2624 isl_basic_map_free(bmap
);
2628 /* This function is called right before "div" is eliminated from "bmap"
2629 * using Fourier-Motzkin.
2630 * Look through the constraints of "bmap" for constraints on the argument
2631 * of the integer division and use them to construct constraints on the
2632 * integer division itself. These constraints can then be combined
2633 * during the Fourier-Motzkin elimination.
2634 * Note that it is only useful to introduce lower bounds on "div"
2635 * if "bmap" already contains upper bounds on "div" as the newly
2636 * introduce lower bounds can then be combined with the pre-existing
2637 * upper bounds. Similarly for upper bounds.
2638 * We therefore first check if "bmap" contains any lower and/or upper bounds
2641 * It is interesting to note that the introduction of these constraints
2642 * can indeed lead to more accurate results, even when compared to
2643 * deriving constraints on the argument of "div" from constraints on "div".
2644 * Consider, for example, the set
2646 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2648 * The second constraint can be rewritten as
2650 * 2 * [(-i-2j+3)/4] + k >= 0
2652 * from which we can derive
2654 * -i - 2j + 3 >= -2k
2660 * Combined with the first constraint, we obtain
2662 * -3 <= 3 + 2k or k >= -3
2664 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2665 * the first constraint, we obtain
2667 * [(i + 2j)/4] >= [-3/4] = -1
2669 * Combining this constraint with the second constraint, we obtain
2673 static __isl_give isl_basic_map
*insert_bounds_on_div(
2674 __isl_take isl_basic_map
*bmap
, int div
)
2677 int check_lb
, check_ub
;
2684 if (isl_int_is_zero(bmap
->div
[div
][0]))
2687 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2691 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2692 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2699 if (!check_lb
&& !check_ub
)
2704 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2705 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2708 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2709 check_lb
, check_ub
);
2717 /* Remove all divs (recursively) involving any of the given dimensions
2718 * in their definitions.
2720 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2721 __isl_take isl_basic_map
*bmap
,
2722 enum isl_dim_type type
, unsigned first
, unsigned n
)
2726 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2727 return isl_basic_map_free(bmap
);
2728 first
+= isl_basic_map_offset(bmap
, type
);
2730 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2733 involves
= div_involves_vars(bmap
, i
, first
, n
);
2735 return isl_basic_map_free(bmap
);
2738 bmap
= insert_bounds_on_div(bmap
, i
);
2739 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2748 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2749 __isl_take isl_basic_set
*bset
,
2750 enum isl_dim_type type
, unsigned first
, unsigned n
)
2752 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2755 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2756 enum isl_dim_type type
, unsigned first
, unsigned n
)
2765 map
= isl_map_cow(map
);
2769 for (i
= 0; i
< map
->n
; ++i
) {
2770 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2781 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2782 enum isl_dim_type type
, unsigned first
, unsigned n
)
2784 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2788 /* Does the description of "bmap" depend on the specified dimensions?
2789 * We also check whether the dimensions appear in any of the div definitions.
2790 * In principle there is no need for this check. If the dimensions appear
2791 * in a div definition, they also appear in the defining constraints of that
2794 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2795 enum isl_dim_type type
, unsigned first
, unsigned n
)
2799 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2800 return isl_bool_error
;
2802 first
+= isl_basic_map_offset(bmap
, type
);
2803 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2804 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2805 return isl_bool_true
;
2806 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2807 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2808 return isl_bool_true
;
2809 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2810 if (isl_int_is_zero(bmap
->div
[i
][0]))
2812 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2813 return isl_bool_true
;
2816 return isl_bool_false
;
2819 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2820 enum isl_dim_type type
, unsigned first
, unsigned n
)
2824 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2825 return isl_bool_error
;
2827 for (i
= 0; i
< map
->n
; ++i
) {
2828 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2830 if (involves
< 0 || involves
)
2834 return isl_bool_false
;
2837 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2838 enum isl_dim_type type
, unsigned first
, unsigned n
)
2840 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2843 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2844 enum isl_dim_type type
, unsigned first
, unsigned n
)
2846 return isl_map_involves_dims(set
, type
, first
, n
);
2849 /* Drop all constraints in bmap that involve any of the dimensions
2850 * first to first+n-1.
2852 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2853 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2860 bmap
= isl_basic_map_cow(bmap
);
2865 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2866 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2868 isl_basic_map_drop_equality(bmap
, i
);
2871 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2872 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2874 isl_basic_map_drop_inequality(bmap
, i
);
2877 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2881 /* Drop all constraints in bset that involve any of the dimensions
2882 * first to first+n-1.
2884 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2885 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2887 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2890 /* Drop all constraints in bmap that do not involve any of the dimensions
2891 * first to first + n - 1 of the given type.
2893 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2894 __isl_take isl_basic_map
*bmap
,
2895 enum isl_dim_type type
, unsigned first
, unsigned n
)
2900 isl_space
*space
= isl_basic_map_get_space(bmap
);
2901 isl_basic_map_free(bmap
);
2902 return isl_basic_map_universe(space
);
2904 bmap
= isl_basic_map_cow(bmap
);
2908 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2909 return isl_basic_map_free(bmap
);
2911 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2913 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2914 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2916 isl_basic_map_drop_equality(bmap
, i
);
2919 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2920 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2922 isl_basic_map_drop_inequality(bmap
, i
);
2925 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2929 /* Drop all constraints in bset that do not involve any of the dimensions
2930 * first to first + n - 1 of the given type.
2932 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2933 __isl_take isl_basic_set
*bset
,
2934 enum isl_dim_type type
, unsigned first
, unsigned n
)
2936 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2940 /* Drop all constraints in bmap that involve any of the dimensions
2941 * first to first + n - 1 of the given type.
2943 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2944 __isl_take isl_basic_map
*bmap
,
2945 enum isl_dim_type type
, unsigned first
, unsigned n
)
2952 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2953 return isl_basic_map_free(bmap
);
2955 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2956 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2957 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2960 /* Drop all constraints in bset that involve any of the dimensions
2961 * first to first + n - 1 of the given type.
2963 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2964 __isl_take isl_basic_set
*bset
,
2965 enum isl_dim_type type
, unsigned first
, unsigned n
)
2967 return isl_basic_map_drop_constraints_involving_dims(bset
,
2971 /* Drop constraints from "map" by applying "drop" to each basic map.
2973 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2974 enum isl_dim_type type
, unsigned first
, unsigned n
,
2975 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2976 enum isl_dim_type type
, unsigned first
, unsigned n
))
2980 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2981 return isl_map_free(map
);
2983 map
= isl_map_cow(map
);
2987 for (i
= 0; i
< map
->n
; ++i
) {
2988 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2990 return isl_map_free(map
);
2994 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2999 /* Drop all constraints in map that involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3003 __isl_take isl_map
*map
,
3004 enum isl_dim_type type
, unsigned first
, unsigned n
)
3008 return drop_constraints(map
, type
, first
, n
,
3009 &isl_basic_map_drop_constraints_involving_dims
);
3012 /* Drop all constraints in "map" that do not involve any of the dimensions
3013 * first to first + n - 1 of the given type.
3015 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3016 __isl_take isl_map
*map
,
3017 enum isl_dim_type type
, unsigned first
, unsigned n
)
3020 isl_space
*space
= isl_map_get_space(map
);
3022 return isl_map_universe(space
);
3024 return drop_constraints(map
, type
, first
, n
,
3025 &isl_basic_map_drop_constraints_not_involving_dims
);
3028 /* Drop all constraints in set that involve any of the dimensions
3029 * first to first + n - 1 of the given type.
3031 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3032 __isl_take isl_set
*set
,
3033 enum isl_dim_type type
, unsigned first
, unsigned n
)
3035 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3038 /* Drop all constraints in "set" that do not involve any of the dimensions
3039 * first to first + n - 1 of the given type.
3041 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3042 __isl_take isl_set
*set
,
3043 enum isl_dim_type type
, unsigned first
, unsigned n
)
3045 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3048 /* Does local variable "div" of "bmap" have a complete explicit representation?
3049 * Having a complete explicit representation requires not only
3050 * an explicit representation, but also that all local variables
3051 * that appear in this explicit representation in turn have
3052 * a complete explicit representation.
3054 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3057 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3060 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3061 if (marked
< 0 || marked
)
3062 return isl_bool_not(marked
);
3064 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3067 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3069 known
= isl_basic_map_div_is_known(bmap
, i
);
3070 if (known
< 0 || !known
)
3074 return isl_bool_true
;
3077 /* Remove all divs that are unknown or defined in terms of unknown divs.
3079 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3080 __isl_take isl_basic_map
*bmap
)
3087 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3088 if (isl_basic_map_div_is_known(bmap
, i
))
3090 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3099 /* Remove all divs that are unknown or defined in terms of unknown divs.
3101 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3102 __isl_take isl_basic_set
*bset
)
3104 return isl_basic_map_remove_unknown_divs(bset
);
3107 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3116 map
= isl_map_cow(map
);
3120 for (i
= 0; i
< map
->n
; ++i
) {
3121 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3131 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3133 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3136 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3137 __isl_take isl_basic_set
*bset
,
3138 enum isl_dim_type type
, unsigned first
, unsigned n
)
3140 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3141 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3142 return bset_from_bmap(bmap
);
3145 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3146 enum isl_dim_type type
, unsigned first
, unsigned n
)
3153 map
= isl_map_cow(map
);
3154 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3155 return isl_map_free(map
);
3157 for (i
= 0; i
< map
->n
; ++i
) {
3158 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3159 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3163 map
= isl_map_drop(map
, type
, first
, n
);
3170 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3171 enum isl_dim_type type
, unsigned first
, unsigned n
)
3173 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3177 /* Project out n inputs starting at first using Fourier-Motzkin */
3178 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3179 unsigned first
, unsigned n
)
3181 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3184 static void dump_term(struct isl_basic_map
*bmap
,
3185 isl_int c
, int pos
, FILE *out
)
3188 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3189 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3190 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3192 isl_int_print(out
, c
, 0);
3194 if (!isl_int_is_one(c
))
3195 isl_int_print(out
, c
, 0);
3196 if (pos
< 1 + nparam
) {
3197 name
= isl_space_get_dim_name(bmap
->dim
,
3198 isl_dim_param
, pos
- 1);
3200 fprintf(out
, "%s", name
);
3202 fprintf(out
, "p%d", pos
- 1);
3203 } else if (pos
< 1 + nparam
+ in
)
3204 fprintf(out
, "i%d", pos
- 1 - nparam
);
3205 else if (pos
< 1 + nparam
+ dim
)
3206 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3208 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3212 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3213 int sign
, FILE *out
)
3217 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3221 for (i
= 0, first
= 1; i
< len
; ++i
) {
3222 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3225 fprintf(out
, " + ");
3227 isl_int_abs(v
, c
[i
]);
3228 dump_term(bmap
, v
, i
, out
);
3235 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3236 const char *op
, FILE *out
, int indent
)
3240 fprintf(out
, "%*s", indent
, "");
3242 dump_constraint_sign(bmap
, c
, 1, out
);
3243 fprintf(out
, " %s ", op
);
3244 dump_constraint_sign(bmap
, c
, -1, out
);
3248 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3249 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3251 fprintf(out
, "%*s", indent
, "");
3252 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3257 static void dump_constraints(struct isl_basic_map
*bmap
,
3258 isl_int
**c
, unsigned n
,
3259 const char *op
, FILE *out
, int indent
)
3263 for (i
= 0; i
< n
; ++i
)
3264 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3267 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3271 unsigned total
= isl_basic_map_total_dim(bmap
);
3273 for (j
= 0; j
< 1 + total
; ++j
) {
3274 if (isl_int_is_zero(exp
[j
]))
3276 if (!first
&& isl_int_is_pos(exp
[j
]))
3278 dump_term(bmap
, exp
[j
], j
, out
);
3283 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3287 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3288 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3290 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3291 fprintf(out
, "%*s", indent
, "");
3292 fprintf(out
, "e%d = [(", i
);
3293 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3295 isl_int_print(out
, bmap
->div
[i
][0], 0);
3296 fprintf(out
, "]\n");
3300 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3301 FILE *out
, int indent
)
3304 fprintf(out
, "null basic set\n");
3308 fprintf(out
, "%*s", indent
, "");
3309 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3310 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3311 bset
->extra
, bset
->flags
);
3312 dump(bset_to_bmap(bset
), out
, indent
);
3315 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3316 FILE *out
, int indent
)
3319 fprintf(out
, "null basic map\n");
3323 fprintf(out
, "%*s", indent
, "");
3324 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3325 "flags: %x, n_name: %d\n",
3327 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3328 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3329 dump(bmap
, out
, indent
);
3332 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3337 total
= isl_basic_map_total_dim(bmap
);
3338 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3339 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3340 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3341 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3345 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3350 if (isl_space_dim(space
, isl_dim_in
) != 0)
3351 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3352 "set cannot have input dimensions", goto error
);
3353 return isl_map_alloc_space(space
, n
, flags
);
3355 isl_space_free(space
);
3359 /* Make sure "map" has room for at least "n" more basic maps.
3361 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3364 struct isl_map
*grown
= NULL
;
3368 isl_assert(map
->ctx
, n
>= 0, goto error
);
3369 if (map
->n
+ n
<= map
->size
)
3371 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3374 for (i
= 0; i
< map
->n
; ++i
) {
3375 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3383 isl_map_free(grown
);
3388 /* Make sure "set" has room for at least "n" more basic sets.
3390 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3392 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3395 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3397 return isl_map_from_basic_map(bset
);
3400 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3402 struct isl_map
*map
;
3407 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3408 return isl_map_add_basic_map(map
, bmap
);
3411 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3412 __isl_take isl_basic_set
*bset
)
3414 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3415 bset_to_bmap(bset
)));
3418 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3420 return isl_map_free(set
);
3423 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3428 fprintf(out
, "null set\n");
3432 fprintf(out
, "%*s", indent
, "");
3433 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3434 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3436 for (i
= 0; i
< set
->n
; ++i
) {
3437 fprintf(out
, "%*s", indent
, "");
3438 fprintf(out
, "basic set %d:\n", i
);
3439 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3443 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3448 fprintf(out
, "null map\n");
3452 fprintf(out
, "%*s", indent
, "");
3453 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3454 "flags: %x, n_name: %d\n",
3455 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3456 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3457 for (i
= 0; i
< map
->n
; ++i
) {
3458 fprintf(out
, "%*s", indent
, "");
3459 fprintf(out
, "basic map %d:\n", i
);
3460 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3464 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3465 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3467 struct isl_basic_map
*bmap_domain
;
3469 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3472 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3473 isl_assert(bset
->ctx
,
3474 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3476 bmap
= isl_basic_map_cow(bmap
);
3479 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3480 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3481 bmap_domain
= isl_basic_map_from_domain(bset
);
3482 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3484 bmap
= isl_basic_map_simplify(bmap
);
3485 return isl_basic_map_finalize(bmap
);
3487 isl_basic_map_free(bmap
);
3488 isl_basic_set_free(bset
);
3492 /* Check that the space of "bset" is the same as that of the range of "bmap".
3494 static isl_stat
isl_basic_map_check_compatible_range(
3495 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3499 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3501 return isl_stat_error
;
3503 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3504 "incompatible spaces", return isl_stat_error
);
3509 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3510 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3512 struct isl_basic_map
*bmap_range
;
3514 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3517 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3518 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3521 if (isl_basic_set_plain_is_universe(bset
)) {
3522 isl_basic_set_free(bset
);
3526 bmap
= isl_basic_map_cow(bmap
);
3529 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3530 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3531 bmap_range
= bset_to_bmap(bset
);
3532 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3534 bmap
= isl_basic_map_simplify(bmap
);
3535 return isl_basic_map_finalize(bmap
);
3537 isl_basic_map_free(bmap
);
3538 isl_basic_set_free(bset
);
3542 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3543 __isl_keep isl_vec
*vec
)
3550 return isl_bool_error
;
3552 total
= 1 + isl_basic_map_total_dim(bmap
);
3553 if (total
!= vec
->size
)
3554 return isl_bool_false
;
3558 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3559 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3560 if (!isl_int_is_zero(s
)) {
3562 return isl_bool_false
;
3566 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3567 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3568 if (isl_int_is_neg(s
)) {
3570 return isl_bool_false
;
3576 return isl_bool_true
;
3579 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3580 __isl_keep isl_vec
*vec
)
3582 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3585 __isl_give isl_basic_map
*isl_basic_map_intersect(
3586 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3588 struct isl_vec
*sample
= NULL
;
3590 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3592 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3593 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3594 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3595 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3596 return isl_basic_map_intersect(bmap2
, bmap1
);
3598 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3599 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3600 isl_assert(bmap1
->ctx
,
3601 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3603 if (isl_basic_map_plain_is_empty(bmap1
)) {
3604 isl_basic_map_free(bmap2
);
3607 if (isl_basic_map_plain_is_empty(bmap2
)) {
3608 isl_basic_map_free(bmap1
);
3612 if (bmap1
->sample
&&
3613 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3614 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3615 sample
= isl_vec_copy(bmap1
->sample
);
3616 else if (bmap2
->sample
&&
3617 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3618 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3619 sample
= isl_vec_copy(bmap2
->sample
);
3621 bmap1
= isl_basic_map_cow(bmap1
);
3624 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3625 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3626 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3629 isl_vec_free(sample
);
3631 isl_vec_free(bmap1
->sample
);
3632 bmap1
->sample
= sample
;
3635 bmap1
= isl_basic_map_simplify(bmap1
);
3636 return isl_basic_map_finalize(bmap1
);
3639 isl_vec_free(sample
);
3640 isl_basic_map_free(bmap1
);
3641 isl_basic_map_free(bmap2
);
3645 struct isl_basic_set
*isl_basic_set_intersect(
3646 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3648 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3649 bset_to_bmap(bset2
)));
3652 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3653 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3655 return isl_basic_set_intersect(bset1
, bset2
);
3658 /* Special case of isl_map_intersect, where both map1 and map2
3659 * are convex, without any divs and such that either map1 or map2
3660 * contains a single constraint. This constraint is then simply
3661 * added to the other map.
3663 static __isl_give isl_map
*map_intersect_add_constraint(
3664 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3666 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3667 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3668 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3669 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3671 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3672 return isl_map_intersect(map2
, map1
);
3674 map1
= isl_map_cow(map1
);
3677 if (isl_map_plain_is_empty(map1
)) {
3681 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3682 if (map2
->p
[0]->n_eq
== 1)
3683 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3685 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3686 map2
->p
[0]->ineq
[0]);
3688 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3689 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3693 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3694 isl_basic_map_free(map1
->p
[0]);
3707 /* map2 may be either a parameter domain or a map living in the same
3710 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3711 __isl_take isl_map
*map2
)
3720 if ((isl_map_plain_is_empty(map1
) ||
3721 isl_map_plain_is_universe(map2
)) &&
3722 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3726 if ((isl_map_plain_is_empty(map2
) ||
3727 isl_map_plain_is_universe(map1
)) &&
3728 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3733 if (map1
->n
== 1 && map2
->n
== 1 &&
3734 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3735 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3736 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3737 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3738 return map_intersect_add_constraint(map1
, map2
);
3740 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3741 isl_space_dim(map2
->dim
, isl_dim_param
))
3742 isl_assert(map1
->ctx
,
3743 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3745 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3746 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3747 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3749 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3750 map1
->n
* map2
->n
, flags
);
3753 for (i
= 0; i
< map1
->n
; ++i
)
3754 for (j
= 0; j
< map2
->n
; ++j
) {
3755 struct isl_basic_map
*part
;
3756 part
= isl_basic_map_intersect(
3757 isl_basic_map_copy(map1
->p
[i
]),
3758 isl_basic_map_copy(map2
->p
[j
]));
3759 if (isl_basic_map_is_empty(part
) < 0)
3760 part
= isl_basic_map_free(part
);
3761 result
= isl_map_add_basic_map(result
, part
);
3774 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3775 __isl_take isl_map
*map2
)
3779 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3780 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3781 "spaces don't match", goto error
);
3782 return map_intersect_internal(map1
, map2
);
3789 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3790 __isl_take isl_map
*map2
)
3792 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3795 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3797 return set_from_map(isl_map_intersect(set_to_map(set1
),
3801 /* map_intersect_internal accepts intersections
3802 * with parameter domains, so we can just call that function.
3804 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3805 __isl_take isl_set
*params
)
3807 return map_intersect_internal(map
, params
);
3810 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3811 __isl_take isl_map
*map2
)
3813 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3816 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3817 __isl_take isl_set
*params
)
3819 return isl_map_intersect_params(set
, params
);
3822 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3825 unsigned pos
, n1
, n2
;
3829 bmap
= isl_basic_map_cow(bmap
);
3832 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3833 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3834 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3835 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3836 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3837 return isl_basic_map_reset_space(bmap
, space
);
3840 static __isl_give isl_basic_map
*basic_map_space_reset(
3841 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3847 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3850 space
= isl_basic_map_get_space(bmap
);
3851 space
= isl_space_reset(space
, type
);
3852 bmap
= isl_basic_map_reset_space(bmap
, space
);
3856 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3857 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3858 unsigned pos
, unsigned n
)
3862 struct isl_basic_map
*res
;
3863 struct isl_dim_map
*dim_map
;
3864 unsigned total
, off
;
3865 enum isl_dim_type t
;
3868 return basic_map_space_reset(bmap
, type
);
3873 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3875 total
= isl_basic_map_total_dim(bmap
) + n
;
3876 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3878 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3880 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3882 unsigned size
= isl_basic_map_dim(bmap
, t
);
3883 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3885 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3886 pos
, size
- pos
, off
+ pos
+ n
);
3888 off
+= isl_space_dim(res_dim
, t
);
3890 isl_dim_map_div(dim_map
, bmap
, off
);
3892 res
= isl_basic_map_alloc_space(res_dim
,
3893 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3894 rational
= isl_basic_map_is_rational(bmap
);
3896 res
= isl_basic_map_free(res
);
3898 res
= isl_basic_map_set_rational(res
);
3899 if (isl_basic_map_plain_is_empty(bmap
)) {
3900 isl_basic_map_free(bmap
);
3902 return isl_basic_map_set_to_empty(res
);
3904 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3905 return isl_basic_map_finalize(res
);
3908 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3909 __isl_take isl_basic_set
*bset
,
3910 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3912 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3915 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3916 enum isl_dim_type type
, unsigned n
)
3920 return isl_basic_map_insert_dims(bmap
, type
,
3921 isl_basic_map_dim(bmap
, type
), n
);
3924 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3925 enum isl_dim_type type
, unsigned n
)
3929 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3930 return isl_basic_map_add_dims(bset
, type
, n
);
3932 isl_basic_set_free(bset
);
3936 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3937 enum isl_dim_type type
)
3941 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3944 space
= isl_map_get_space(map
);
3945 space
= isl_space_reset(space
, type
);
3946 map
= isl_map_reset_space(map
, space
);
3950 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3951 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3956 return map_space_reset(map
, type
);
3958 map
= isl_map_cow(map
);
3962 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3966 for (i
= 0; i
< map
->n
; ++i
) {
3967 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3978 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3979 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3981 return isl_map_insert_dims(set
, type
, pos
, n
);
3984 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3985 enum isl_dim_type type
, unsigned n
)
3989 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3992 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3993 enum isl_dim_type type
, unsigned n
)
3997 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3998 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4004 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4005 __isl_take isl_basic_map
*bmap
,
4006 enum isl_dim_type dst_type
, unsigned dst_pos
,
4007 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4009 struct isl_dim_map
*dim_map
;
4010 struct isl_basic_map
*res
;
4011 enum isl_dim_type t
;
4012 unsigned total
, off
;
4017 bmap
= isl_basic_map_reset(bmap
, src_type
);
4018 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4022 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4023 return isl_basic_map_free(bmap
);
4025 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4028 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4030 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4031 pos(bmap
->dim
, src_type
) + src_pos
+
4032 ((src_type
< dst_type
) ? n
: 0)) {
4033 bmap
= isl_basic_map_cow(bmap
);
4037 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4038 src_type
, src_pos
, n
);
4042 bmap
= isl_basic_map_finalize(bmap
);
4047 total
= isl_basic_map_total_dim(bmap
);
4048 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4051 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4052 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4053 if (t
== dst_type
) {
4054 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4057 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4060 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4061 dst_pos
, size
- dst_pos
, off
);
4062 off
+= size
- dst_pos
;
4063 } else if (t
== src_type
) {
4064 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4067 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4068 src_pos
+ n
, size
- src_pos
- n
, off
);
4069 off
+= size
- src_pos
- n
;
4071 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4075 isl_dim_map_div(dim_map
, bmap
, off
);
4077 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4078 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4079 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4083 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4084 src_type
, src_pos
, n
);
4088 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4089 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4090 bmap
= isl_basic_map_finalize(bmap
);
4094 isl_basic_map_free(bmap
);
4098 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4099 enum isl_dim_type dst_type
, unsigned dst_pos
,
4100 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4102 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4103 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4104 src_type
, src_pos
, n
);
4105 return bset_from_bmap(bmap
);
4108 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4109 enum isl_dim_type dst_type
, unsigned dst_pos
,
4110 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4114 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4115 return set_from_map(isl_map_move_dims(set_to_map(set
),
4116 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4122 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4123 enum isl_dim_type dst_type
, unsigned dst_pos
,
4124 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4129 map
= isl_map_reset(map
, src_type
);
4130 map
= isl_map_reset(map
, dst_type
);
4134 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4135 return isl_map_free(map
);
4137 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4140 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4142 map
= isl_map_cow(map
);
4146 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4150 for (i
= 0; i
< map
->n
; ++i
) {
4151 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4153 src_type
, src_pos
, n
);
4164 /* Move the specified dimensions to the last columns right before
4165 * the divs. Don't change the dimension specification of bmap.
4166 * That's the responsibility of the caller.
4168 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4169 enum isl_dim_type type
, unsigned first
, unsigned n
)
4171 struct isl_dim_map
*dim_map
;
4172 struct isl_basic_map
*res
;
4173 enum isl_dim_type t
;
4174 unsigned total
, off
;
4178 if (pos(bmap
->dim
, type
) + first
+ n
==
4179 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4182 total
= isl_basic_map_total_dim(bmap
);
4183 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4186 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4187 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4189 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4192 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4193 first
, n
, total
- bmap
->n_div
- n
);
4194 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4195 first
+ n
, size
- (first
+ n
), off
);
4196 off
+= size
- (first
+ n
);
4198 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4202 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4204 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4205 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4206 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4210 /* Insert "n" rows in the divs of "bmap".
4212 * The number of columns is not changed, which means that the last
4213 * dimensions of "bmap" are being reintepreted as the new divs.
4214 * The space of "bmap" is not adjusted, however, which means
4215 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4216 * from the space of "bmap" is the responsibility of the caller.
4218 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4226 bmap
= isl_basic_map_cow(bmap
);
4230 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4231 old
= bmap
->block2
.data
;
4232 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4233 (bmap
->extra
+ n
) * (1 + row_size
));
4234 if (!bmap
->block2
.data
)
4235 return isl_basic_map_free(bmap
);
4236 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4238 return isl_basic_map_free(bmap
);
4239 for (i
= 0; i
< n
; ++i
) {
4240 new_div
[i
] = bmap
->block2
.data
+
4241 (bmap
->extra
+ i
) * (1 + row_size
);
4242 isl_seq_clr(new_div
[i
], 1 + row_size
);
4244 for (i
= 0; i
< bmap
->extra
; ++i
)
4245 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4247 bmap
->div
= new_div
;
4254 /* Drop constraints from "bmap" that only involve the variables
4255 * of "type" in the range [first, first + n] that are not related
4256 * to any of the variables outside that interval.
4257 * These constraints cannot influence the values for the variables
4258 * outside the interval, except in case they cause "bmap" to be empty.
4259 * Only drop the constraints if "bmap" is known to be non-empty.
4261 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4262 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4263 unsigned first
, unsigned n
)
4267 unsigned dim
, n_div
;
4270 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4272 return isl_basic_map_free(bmap
);
4276 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4277 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4278 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4280 return isl_basic_map_free(bmap
);
4281 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4282 for (i
= 0; i
< first
; ++i
)
4284 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4287 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4292 /* Turn the n dimensions of type type, starting at first
4293 * into existentially quantified variables.
4295 * If a subset of the projected out variables are unrelated
4296 * to any of the variables that remain, then the constraints
4297 * involving this subset are simply dropped first.
4299 __isl_give isl_basic_map
*isl_basic_map_project_out(
4300 __isl_take isl_basic_map
*bmap
,
4301 enum isl_dim_type type
, unsigned first
, unsigned n
)
4306 return basic_map_space_reset(bmap
, type
);
4307 if (type
== isl_dim_div
)
4308 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4309 "cannot project out existentially quantified variables",
4310 return isl_basic_map_free(bmap
));
4312 empty
= isl_basic_map_plain_is_empty(bmap
);
4314 return isl_basic_map_free(bmap
);
4316 bmap
= isl_basic_map_set_to_empty(bmap
);
4318 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4322 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4323 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4325 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4326 return isl_basic_map_free(bmap
);
4328 bmap
= move_last(bmap
, type
, first
, n
);
4329 bmap
= isl_basic_map_cow(bmap
);
4330 bmap
= insert_div_rows(bmap
, n
);
4334 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4337 bmap
= isl_basic_map_simplify(bmap
);
4338 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4339 return isl_basic_map_finalize(bmap
);
4341 isl_basic_map_free(bmap
);
4345 /* Turn the n dimensions of type type, starting at first
4346 * into existentially quantified variables.
4348 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4349 enum isl_dim_type type
, unsigned first
, unsigned n
)
4351 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4355 /* Turn the n dimensions of type type, starting at first
4356 * into existentially quantified variables.
4358 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4359 enum isl_dim_type type
, unsigned first
, unsigned n
)
4364 return map_space_reset(map
, type
);
4366 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4367 return isl_map_free(map
);
4369 map
= isl_map_cow(map
);
4373 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4377 for (i
= 0; i
< map
->n
; ++i
) {
4378 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4389 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4390 * into existentially quantified variables.
4392 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4393 enum isl_dim_type type
, unsigned first
, unsigned n
)
4397 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4398 return isl_map_free(map
);
4399 dim
= isl_map_dim(map
, type
);
4400 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4401 map
= isl_map_project_out(map
, type
, 0, first
);
4405 /* Turn the n dimensions of type type, starting at first
4406 * into existentially quantified variables.
4408 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4409 enum isl_dim_type type
, unsigned first
, unsigned n
)
4411 return set_from_map(isl_map_project_out(set_to_map(set
),
4415 /* Return a map that projects the elements in "set" onto their
4416 * "n" set dimensions starting at "first".
4417 * "type" should be equal to isl_dim_set.
4419 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4420 enum isl_dim_type type
, unsigned first
, unsigned n
)
4428 if (type
!= isl_dim_set
)
4429 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4430 "only set dimensions can be projected out", goto error
);
4431 dim
= isl_set_dim(set
, isl_dim_set
);
4432 if (first
+ n
> dim
|| first
+ n
< first
)
4433 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4434 "index out of bounds", goto error
);
4436 map
= isl_map_from_domain(set
);
4437 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4438 for (i
= 0; i
< n
; ++i
)
4439 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4447 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4451 for (i
= 0; i
< n
; ++i
) {
4452 j
= isl_basic_map_alloc_div(bmap
);
4455 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4459 isl_basic_map_free(bmap
);
4463 struct isl_basic_map
*isl_basic_map_apply_range(
4464 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4466 isl_space
*dim_result
= NULL
;
4467 struct isl_basic_map
*bmap
;
4468 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4469 struct isl_dim_map
*dim_map1
, *dim_map2
;
4471 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4473 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4474 bmap2
->dim
, isl_dim_in
))
4475 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4476 "spaces don't match", goto error
);
4478 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4479 isl_space_copy(bmap2
->dim
));
4481 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4482 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4483 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4484 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4486 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4487 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4488 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4489 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4490 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4491 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4492 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4493 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4494 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4495 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4496 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4498 bmap
= isl_basic_map_alloc_space(dim_result
,
4499 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4500 bmap1
->n_eq
+ bmap2
->n_eq
,
4501 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4502 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4503 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4504 bmap
= add_divs(bmap
, n
);
4505 bmap
= isl_basic_map_simplify(bmap
);
4506 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4507 return isl_basic_map_finalize(bmap
);
4509 isl_basic_map_free(bmap1
);
4510 isl_basic_map_free(bmap2
);
4514 struct isl_basic_set
*isl_basic_set_apply(
4515 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4520 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4523 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4526 isl_basic_set_free(bset
);
4527 isl_basic_map_free(bmap
);
4531 struct isl_basic_map
*isl_basic_map_apply_domain(
4532 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4534 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4536 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4537 bmap2
->dim
, isl_dim_in
))
4538 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4539 "spaces don't match", goto error
);
4541 bmap1
= isl_basic_map_reverse(bmap1
);
4542 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4543 return isl_basic_map_reverse(bmap1
);
4545 isl_basic_map_free(bmap1
);
4546 isl_basic_map_free(bmap2
);
4550 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4551 * A \cap B -> f(A) + f(B)
4553 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4554 __isl_take isl_basic_map
*bmap2
)
4556 unsigned n_in
, n_out
, nparam
, total
, pos
;
4557 struct isl_basic_map
*bmap
= NULL
;
4558 struct isl_dim_map
*dim_map1
, *dim_map2
;
4561 if (!bmap1
|| !bmap2
)
4564 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4567 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4568 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4569 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4571 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4572 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4573 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4574 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4575 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4576 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4577 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4578 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4579 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4580 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4581 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4583 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4584 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4585 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4586 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4587 for (i
= 0; i
< n_out
; ++i
) {
4588 int j
= isl_basic_map_alloc_equality(bmap
);
4591 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4592 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4593 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4594 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4596 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4597 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4598 bmap
= add_divs(bmap
, 2 * n_out
);
4600 bmap
= isl_basic_map_simplify(bmap
);
4601 return isl_basic_map_finalize(bmap
);
4603 isl_basic_map_free(bmap
);
4604 isl_basic_map_free(bmap1
);
4605 isl_basic_map_free(bmap2
);
4609 /* Given two maps A -> f(A) and B -> g(B), construct a map
4610 * A \cap B -> f(A) + f(B)
4612 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4613 __isl_take isl_map
*map2
)
4615 struct isl_map
*result
;
4621 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4623 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4624 map1
->n
* map2
->n
, 0);
4627 for (i
= 0; i
< map1
->n
; ++i
)
4628 for (j
= 0; j
< map2
->n
; ++j
) {
4629 struct isl_basic_map
*part
;
4630 part
= isl_basic_map_sum(
4631 isl_basic_map_copy(map1
->p
[i
]),
4632 isl_basic_map_copy(map2
->p
[j
]));
4633 if (isl_basic_map_is_empty(part
))
4634 isl_basic_map_free(part
);
4636 result
= isl_map_add_basic_map(result
, part
);
4649 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4650 __isl_take isl_set
*set2
)
4652 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4655 /* Given a basic map A -> f(A), construct A -> -f(A).
4657 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4662 bmap
= isl_basic_map_cow(bmap
);
4666 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4667 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4668 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4669 for (j
= 0; j
< n
; ++j
)
4670 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4671 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4672 for (j
= 0; j
< n
; ++j
)
4673 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4674 for (i
= 0; i
< bmap
->n_div
; ++i
)
4675 for (j
= 0; j
< n
; ++j
)
4676 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4677 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4678 return isl_basic_map_finalize(bmap
);
4681 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4683 return isl_basic_map_neg(bset
);
4686 /* Given a map A -> f(A), construct A -> -f(A).
4688 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4692 map
= isl_map_cow(map
);
4696 for (i
= 0; i
< map
->n
; ++i
) {
4697 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4708 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4710 return set_from_map(isl_map_neg(set_to_map(set
)));
4713 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4714 * A -> floor(f(A)/d).
4716 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4719 unsigned n_in
, n_out
, nparam
, total
, pos
;
4720 struct isl_basic_map
*result
= NULL
;
4721 struct isl_dim_map
*dim_map
;
4727 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4728 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4729 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4731 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4732 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4733 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4734 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4735 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4736 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4738 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4739 bmap
->n_div
+ n_out
,
4740 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4741 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4742 result
= add_divs(result
, n_out
);
4743 for (i
= 0; i
< n_out
; ++i
) {
4745 j
= isl_basic_map_alloc_inequality(result
);
4748 isl_seq_clr(result
->ineq
[j
], 1+total
);
4749 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4750 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4751 j
= isl_basic_map_alloc_inequality(result
);
4754 isl_seq_clr(result
->ineq
[j
], 1+total
);
4755 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4756 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4757 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4760 result
= isl_basic_map_simplify(result
);
4761 return isl_basic_map_finalize(result
);
4763 isl_basic_map_free(result
);
4767 /* Given a map A -> f(A) and an integer d, construct a map
4768 * A -> floor(f(A)/d).
4770 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4774 map
= isl_map_cow(map
);
4778 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4779 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4780 for (i
= 0; i
< map
->n
; ++i
) {
4781 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4792 /* Given a map A -> f(A) and an integer d, construct a map
4793 * A -> floor(f(A)/d).
4795 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4796 __isl_take isl_val
*d
)
4800 if (!isl_val_is_int(d
))
4801 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4802 "expecting integer denominator", goto error
);
4803 map
= isl_map_floordiv(map
, d
->n
);
4812 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4819 i
= isl_basic_map_alloc_equality(bmap
);
4822 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4823 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4824 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4825 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4826 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4827 return isl_basic_map_finalize(bmap
);
4829 isl_basic_map_free(bmap
);
4833 /* Add a constraint to "bmap" expressing i_pos < o_pos
4835 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4842 i
= isl_basic_map_alloc_inequality(bmap
);
4845 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4846 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4847 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4848 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4849 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4850 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4851 return isl_basic_map_finalize(bmap
);
4853 isl_basic_map_free(bmap
);
4857 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4859 static __isl_give isl_basic_map
*var_less_or_equal(
4860 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4866 i
= isl_basic_map_alloc_inequality(bmap
);
4869 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4870 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4871 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4872 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4873 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4874 return isl_basic_map_finalize(bmap
);
4876 isl_basic_map_free(bmap
);
4880 /* Add a constraint to "bmap" expressing i_pos > o_pos
4882 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4889 i
= isl_basic_map_alloc_inequality(bmap
);
4892 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4893 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4894 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4895 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4896 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4897 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4898 return isl_basic_map_finalize(bmap
);
4900 isl_basic_map_free(bmap
);
4904 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4906 static __isl_give isl_basic_map
*var_more_or_equal(
4907 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4913 i
= isl_basic_map_alloc_inequality(bmap
);
4916 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4917 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4918 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4919 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4920 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4921 return isl_basic_map_finalize(bmap
);
4923 isl_basic_map_free(bmap
);
4927 __isl_give isl_basic_map
*isl_basic_map_equal(
4928 __isl_take isl_space
*dim
, unsigned n_equal
)
4931 struct isl_basic_map
*bmap
;
4932 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4935 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4936 bmap
= var_equal(bmap
, i
);
4937 return isl_basic_map_finalize(bmap
);
4940 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4942 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4946 struct isl_basic_map
*bmap
;
4947 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4950 for (i
= 0; i
< pos
&& bmap
; ++i
)
4951 bmap
= var_equal(bmap
, i
);
4953 bmap
= var_less(bmap
, pos
);
4954 return isl_basic_map_finalize(bmap
);
4957 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4959 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4960 __isl_take isl_space
*dim
, unsigned pos
)
4963 isl_basic_map
*bmap
;
4965 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4966 for (i
= 0; i
< pos
; ++i
)
4967 bmap
= var_equal(bmap
, i
);
4968 bmap
= var_less_or_equal(bmap
, pos
);
4969 return isl_basic_map_finalize(bmap
);
4972 /* Return a relation on "dim" expressing i_pos > o_pos
4974 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4978 struct isl_basic_map
*bmap
;
4979 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4982 for (i
= 0; i
< pos
&& bmap
; ++i
)
4983 bmap
= var_equal(bmap
, i
);
4985 bmap
= var_more(bmap
, pos
);
4986 return isl_basic_map_finalize(bmap
);
4989 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4991 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4992 __isl_take isl_space
*dim
, unsigned pos
)
4995 isl_basic_map
*bmap
;
4997 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4998 for (i
= 0; i
< pos
; ++i
)
4999 bmap
= var_equal(bmap
, i
);
5000 bmap
= var_more_or_equal(bmap
, pos
);
5001 return isl_basic_map_finalize(bmap
);
5004 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
5005 unsigned n
, int equal
)
5007 struct isl_map
*map
;
5010 if (n
== 0 && equal
)
5011 return isl_map_universe(dims
);
5013 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
5015 for (i
= 0; i
+ 1 < n
; ++i
)
5016 map
= isl_map_add_basic_map(map
,
5017 isl_basic_map_less_at(isl_space_copy(dims
), i
));
5020 map
= isl_map_add_basic_map(map
,
5021 isl_basic_map_less_or_equal_at(dims
, n
- 1));
5023 map
= isl_map_add_basic_map(map
,
5024 isl_basic_map_less_at(dims
, n
- 1));
5026 isl_space_free(dims
);
5031 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
5035 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
5038 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5040 return map_lex_lte_first(dim
, n
, 0);
5043 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5045 return map_lex_lte_first(dim
, n
, 1);
5048 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5050 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5053 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5055 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5058 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
5059 unsigned n
, int equal
)
5061 struct isl_map
*map
;
5064 if (n
== 0 && equal
)
5065 return isl_map_universe(dims
);
5067 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
5069 for (i
= 0; i
+ 1 < n
; ++i
)
5070 map
= isl_map_add_basic_map(map
,
5071 isl_basic_map_more_at(isl_space_copy(dims
), i
));
5074 map
= isl_map_add_basic_map(map
,
5075 isl_basic_map_more_or_equal_at(dims
, n
- 1));
5077 map
= isl_map_add_basic_map(map
,
5078 isl_basic_map_more_at(dims
, n
- 1));
5080 isl_space_free(dims
);
5085 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
5089 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
5092 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5094 return map_lex_gte_first(dim
, n
, 0);
5097 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5099 return map_lex_gte_first(dim
, n
, 1);
5102 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5104 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5107 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5109 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5112 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5113 __isl_take isl_set
*set2
)
5116 map
= isl_map_lex_le(isl_set_get_space(set1
));
5117 map
= isl_map_intersect_domain(map
, set1
);
5118 map
= isl_map_intersect_range(map
, set2
);
5122 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5123 __isl_take isl_set
*set2
)
5126 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5127 map
= isl_map_intersect_domain(map
, set1
);
5128 map
= isl_map_intersect_range(map
, set2
);
5132 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5133 __isl_take isl_set
*set2
)
5136 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5137 map
= isl_map_intersect_domain(map
, set1
);
5138 map
= isl_map_intersect_range(map
, set2
);
5142 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5143 __isl_take isl_set
*set2
)
5146 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5147 map
= isl_map_intersect_domain(map
, set1
);
5148 map
= isl_map_intersect_range(map
, set2
);
5152 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5153 __isl_take isl_map
*map2
)
5156 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5157 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5158 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5162 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5163 __isl_take isl_map
*map2
)
5166 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5167 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5168 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5172 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5173 __isl_take isl_map
*map2
)
5176 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5177 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5178 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5182 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5183 __isl_take isl_map
*map2
)
5186 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5187 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5188 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5192 /* For a div d = floor(f/m), add the constraint
5196 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5197 unsigned pos
, isl_int
*div
)
5200 unsigned total
= isl_basic_map_total_dim(bmap
);
5202 i
= isl_basic_map_alloc_inequality(bmap
);
5204 return isl_stat_error
;
5205 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5206 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5211 /* For a div d = floor(f/m), add the constraint
5213 * -(f-(m-1)) + m d >= 0
5215 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5216 unsigned pos
, isl_int
*div
)
5219 unsigned total
= isl_basic_map_total_dim(bmap
);
5221 i
= isl_basic_map_alloc_inequality(bmap
);
5223 return isl_stat_error
;
5224 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5225 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5226 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5227 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5232 /* For a div d = floor(f/m), add the constraints
5235 * -(f-(m-1)) + m d >= 0
5237 * Note that the second constraint is the negation of
5241 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5242 unsigned pos
, isl_int
*div
)
5244 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5246 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5251 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5252 unsigned pos
, isl_int
*div
)
5254 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5258 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
5260 unsigned total
= isl_basic_map_total_dim(bmap
);
5261 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5263 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5267 /* For each known div d = floor(f/m), add the constraints
5270 * -(f-(m-1)) + m d >= 0
5272 * Remove duplicate constraints in case of some these div constraints
5273 * already appear in "bmap".
5275 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5276 __isl_take isl_basic_map
*bmap
)
5282 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5286 bmap
= add_known_div_constraints(bmap
);
5287 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5288 bmap
= isl_basic_map_finalize(bmap
);
5292 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5294 * In particular, if this div is of the form d = floor(f/m),
5295 * then add the constraint
5299 * if sign < 0 or the constraint
5301 * -(f-(m-1)) + m d >= 0
5305 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5306 unsigned div
, int sign
)
5314 total
= isl_basic_map_total_dim(bmap
);
5315 div_pos
= total
- bmap
->n_div
+ div
;
5318 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5320 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5323 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5324 __isl_take isl_basic_map
*bmap
)
5328 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5330 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5331 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5332 return bset_from_bmap(bmap
);
5333 bmap
= isl_basic_map_cow(bmap
);
5336 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5339 bmap
->extra
-= bmap
->n_div
;
5341 bmap
= isl_basic_map_finalize(bmap
);
5342 return bset_from_bmap(bmap
);
5344 isl_basic_map_free(bmap
);
5348 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5349 __isl_take isl_basic_set
*bset
)
5351 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5354 /* Replace each element in "list" by the result of applying
5355 * isl_basic_map_underlying_set to the element.
5357 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5358 __isl_take isl_basic_map_list
*list
)
5365 n
= isl_basic_map_list_n_basic_map(list
);
5366 for (i
= 0; i
< n
; ++i
) {
5367 isl_basic_map
*bmap
;
5368 isl_basic_set
*bset
;
5370 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5371 bset
= isl_basic_set_underlying_set(bmap
);
5372 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5378 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5379 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5381 struct isl_basic_map
*bmap
;
5382 struct isl_ctx
*ctx
;
5389 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5390 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5391 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5393 if (like
->n_div
== 0) {
5394 isl_space
*space
= isl_basic_map_get_space(like
);
5395 isl_basic_map_free(like
);
5396 return isl_basic_map_reset_space(bset
, space
);
5398 bset
= isl_basic_set_cow(bset
);
5401 total
= bset
->dim
->n_out
+ bset
->extra
;
5402 bmap
= bset_to_bmap(bset
);
5403 isl_space_free(bmap
->dim
);
5404 bmap
->dim
= isl_space_copy(like
->dim
);
5407 bmap
->n_div
= like
->n_div
;
5408 bmap
->extra
+= like
->n_div
;
5412 ltotal
= total
- bmap
->extra
+ like
->extra
;
5415 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5416 bmap
->extra
* (1 + 1 + total
));
5417 if (isl_blk_is_error(bmap
->block2
))
5419 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5423 for (i
= 0; i
< bmap
->extra
; ++i
)
5424 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5425 for (i
= 0; i
< like
->n_div
; ++i
) {
5426 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5427 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5429 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5431 isl_basic_map_free(like
);
5432 bmap
= isl_basic_map_simplify(bmap
);
5433 bmap
= isl_basic_map_finalize(bmap
);
5436 isl_basic_map_free(like
);
5437 isl_basic_set_free(bset
);
5441 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5442 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5444 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5445 bset_to_bmap(like
)));
5448 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5452 map
= isl_map_cow(map
);
5455 map
->dim
= isl_space_cow(map
->dim
);
5459 for (i
= 1; i
< map
->n
; ++i
)
5460 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5462 for (i
= 0; i
< map
->n
; ++i
) {
5463 map
->p
[i
] = bset_to_bmap(
5464 isl_basic_map_underlying_set(map
->p
[i
]));
5469 map
->dim
= isl_space_underlying(map
->dim
, 0);
5471 isl_space_free(map
->dim
);
5472 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5476 return set_from_map(map
);
5482 /* Replace the space of "bmap" by "space".
5484 * If the space of "bmap" is identical to "space" (including the identifiers
5485 * of the input and output dimensions), then simply return the original input.
5487 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5488 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5491 isl_space
*bmap_space
;
5493 bmap_space
= isl_basic_map_peek_space(bmap
);
5494 equal
= isl_space_is_equal(bmap_space
, space
);
5495 if (equal
>= 0 && equal
)
5496 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5500 isl_space_free(space
);
5503 bmap
= isl_basic_map_cow(bmap
);
5504 if (!bmap
|| !space
)
5507 isl_space_free(bmap
->dim
);
5510 bmap
= isl_basic_map_finalize(bmap
);
5514 isl_basic_map_free(bmap
);
5515 isl_space_free(space
);
5519 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5520 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5522 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5526 /* Check that the total dimensions of "map" and "space" are the same.
5528 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5529 __isl_keep isl_space
*space
)
5531 unsigned dim1
, dim2
;
5534 return isl_stat_error
;
5535 dim1
= isl_map_dim(map
, isl_dim_all
);
5536 dim2
= isl_space_dim(space
, isl_dim_all
);
5539 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5540 "total dimensions do not match", return isl_stat_error
);
5543 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5544 __isl_take isl_space
*dim
)
5548 map
= isl_map_cow(map
);
5552 for (i
= 0; i
< map
->n
; ++i
) {
5553 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5554 isl_space_copy(dim
));
5558 isl_space_free(map
->dim
);
5564 isl_space_free(dim
);
5568 /* Replace the space of "map" by "space", without modifying
5569 * the dimension of "map".
5571 * If the space of "map" is identical to "space" (including the identifiers
5572 * of the input and output dimensions), then simply return the original input.
5574 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5575 __isl_take isl_space
*space
)
5578 isl_space
*map_space
;
5580 map_space
= isl_map_peek_space(map
);
5581 equal
= isl_space_is_equal(map_space
, space
);
5582 if (equal
>= 0 && equal
)
5583 equal
= isl_space_has_equal_ids(map_space
, space
);
5587 isl_space_free(space
);
5590 if (check_map_space_equal_total_dim(map
, space
) < 0)
5592 return isl_map_reset_space(map
, space
);
5595 isl_space_free(space
);
5599 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5600 __isl_take isl_space
*dim
)
5602 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5605 /* Compute the parameter domain of the given basic set.
5607 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5613 is_params
= isl_basic_set_is_params(bset
);
5615 return isl_basic_set_free(bset
);
5619 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5620 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5621 space
= isl_basic_set_get_space(bset
);
5622 space
= isl_space_params(space
);
5623 bset
= isl_basic_set_reset_space(bset
, space
);
5627 /* Construct a zero-dimensional basic set with the given parameter domain.
5629 __isl_give isl_basic_set
*isl_basic_set_from_params(
5630 __isl_take isl_basic_set
*bset
)
5633 space
= isl_basic_set_get_space(bset
);
5634 space
= isl_space_set_from_params(space
);
5635 bset
= isl_basic_set_reset_space(bset
, space
);
5639 /* Compute the parameter domain of the given set.
5641 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5646 if (isl_set_is_params(set
))
5649 n
= isl_set_dim(set
, isl_dim_set
);
5650 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5651 space
= isl_set_get_space(set
);
5652 space
= isl_space_params(space
);
5653 set
= isl_set_reset_space(set
, space
);
5657 /* Construct a zero-dimensional set with the given parameter domain.
5659 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5662 space
= isl_set_get_space(set
);
5663 space
= isl_space_set_from_params(space
);
5664 set
= isl_set_reset_space(set
, space
);
5668 /* Compute the parameter domain of the given map.
5670 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5675 n
= isl_map_dim(map
, isl_dim_in
);
5676 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5677 n
= isl_map_dim(map
, isl_dim_out
);
5678 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5679 space
= isl_map_get_space(map
);
5680 space
= isl_space_params(space
);
5681 map
= isl_map_reset_space(map
, space
);
5685 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5692 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5694 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5695 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5697 return isl_basic_map_reset_space(bmap
, space
);
5700 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5703 return isl_bool_error
;
5704 return isl_space_may_be_set(bmap
->dim
);
5707 /* Is this basic map actually a set?
5708 * Users should never call this function. Outside of isl,
5709 * the type should indicate whether something is a set or a map.
5711 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5714 return isl_bool_error
;
5715 return isl_space_is_set(bmap
->dim
);
5718 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5722 is_set
= isl_basic_map_is_set(bmap
);
5727 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5729 isl_basic_map_free(bmap
);
5733 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5734 __isl_take isl_basic_map
*bmap
)
5738 isl_basic_map
*domain
;
5739 int nparam
, n_in
, n_out
;
5741 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5742 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5743 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5745 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5746 domain
= isl_basic_map_universe(dim
);
5748 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5749 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5750 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5752 for (i
= 0; i
< n_in
; ++i
)
5753 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5756 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5757 return isl_basic_map_finalize(bmap
);
5760 __isl_give isl_basic_map
*isl_basic_map_range_map(
5761 __isl_take isl_basic_map
*bmap
)
5765 isl_basic_map
*range
;
5766 int nparam
, n_in
, n_out
;
5768 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5769 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5770 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5772 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5773 range
= isl_basic_map_universe(dim
);
5775 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5776 bmap
= isl_basic_map_apply_range(bmap
, range
);
5777 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5779 for (i
= 0; i
< n_out
; ++i
)
5780 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5783 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5784 return isl_basic_map_finalize(bmap
);
5787 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5791 return isl_space_may_be_set(map
->dim
);
5794 /* Is this map actually a set?
5795 * Users should never call this function. Outside of isl,
5796 * the type should indicate whether something is a set or a map.
5798 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5801 return isl_bool_error
;
5802 return isl_space_is_set(map
->dim
);
5805 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5809 struct isl_set
*set
;
5811 is_set
= isl_map_is_set(map
);
5815 return set_from_map(map
);
5817 map
= isl_map_cow(map
);
5821 set
= set_from_map(map
);
5822 set
->dim
= isl_space_range(set
->dim
);
5825 for (i
= 0; i
< map
->n
; ++i
) {
5826 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5830 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5831 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5838 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5842 map
= isl_map_cow(map
);
5846 map
->dim
= isl_space_domain_map(map
->dim
);
5849 for (i
= 0; i
< map
->n
; ++i
) {
5850 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5854 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5855 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5862 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5865 isl_space
*range_dim
;
5867 map
= isl_map_cow(map
);
5871 range_dim
= isl_space_range(isl_map_get_space(map
));
5872 range_dim
= isl_space_from_range(range_dim
);
5873 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5874 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5877 for (i
= 0; i
< map
->n
; ++i
) {
5878 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5882 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5883 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5890 /* Given a wrapped map of the form A[B -> C],
5891 * return the map A[B -> C] -> B.
5893 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5900 if (!isl_set_has_tuple_id(set
))
5901 return isl_map_domain_map(isl_set_unwrap(set
));
5903 id
= isl_set_get_tuple_id(set
);
5904 map
= isl_map_domain_map(isl_set_unwrap(set
));
5905 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5910 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5911 __isl_take isl_basic_set
*bset
)
5913 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5916 __isl_give isl_basic_map
*isl_basic_map_from_range(
5917 __isl_take isl_basic_set
*bset
)
5920 space
= isl_basic_set_get_space(bset
);
5921 space
= isl_space_from_range(space
);
5922 bset
= isl_basic_set_reset_space(bset
, space
);
5923 return bset_to_bmap(bset
);
5926 /* Create a relation with the given set as range.
5927 * The domain of the created relation is a zero-dimensional
5928 * flat anonymous space.
5930 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5933 space
= isl_set_get_space(set
);
5934 space
= isl_space_from_range(space
);
5935 set
= isl_set_reset_space(set
, space
);
5936 return set_to_map(set
);
5939 /* Create a relation with the given set as domain.
5940 * The range of the created relation is a zero-dimensional
5941 * flat anonymous space.
5943 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5945 return isl_map_reverse(isl_map_from_range(set
));
5948 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5949 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5951 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5954 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5955 __isl_take isl_set
*range
)
5957 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5960 /* Return a newly allocated isl_map with given space and flags and
5961 * room for "n" basic maps.
5962 * Make sure that all cached information is cleared.
5964 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5967 struct isl_map
*map
;
5972 isl_die(space
->ctx
, isl_error_internal
,
5973 "negative number of basic maps", goto error
);
5974 map
= isl_calloc(space
->ctx
, struct isl_map
,
5975 sizeof(struct isl_map
) +
5976 (n
- 1) * sizeof(struct isl_basic_map
*));
5980 map
->ctx
= space
->ctx
;
5981 isl_ctx_ref(map
->ctx
);
5989 isl_space_free(space
);
5993 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5995 struct isl_basic_map
*bmap
;
5996 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5997 bmap
= isl_basic_map_set_to_empty(bmap
);
6001 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6003 struct isl_basic_set
*bset
;
6004 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6005 bset
= isl_basic_set_set_to_empty(bset
);
6009 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6011 struct isl_basic_map
*bmap
;
6012 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6013 bmap
= isl_basic_map_finalize(bmap
);
6017 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6019 struct isl_basic_set
*bset
;
6020 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6021 bset
= isl_basic_set_finalize(bset
);
6025 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
6028 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
6029 isl_basic_map
*bmap
;
6031 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
6032 for (i
= 0; i
< total
; ++i
) {
6033 int k
= isl_basic_map_alloc_inequality(bmap
);
6036 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6037 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6041 isl_basic_map_free(bmap
);
6045 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
6047 return isl_basic_map_nat_universe(dim
);
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 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
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 struct isl_basic_map
*isl_basic_map_fix_pos_si(
6147 struct 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 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
6265 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6272 isl_basic_map_free(map
->p
[i
]);
6273 if (i
!= map
->n
- 1) {
6274 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6275 map
->p
[i
] = map
->p
[map
->n
- 1];
6282 /* Perform "fn" on each basic map of "map", where we may not be holding
6283 * the only reference to "map".
6284 * In particular, "fn" should be a semantics preserving operation
6285 * that we want to apply to all copies of "map". We therefore need
6286 * to be careful not to modify "map" in a way that breaks "map"
6287 * in case anything goes wrong.
6289 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6290 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6292 struct isl_basic_map
*bmap
;
6298 for (i
= map
->n
- 1; i
>= 0; --i
) {
6299 bmap
= isl_basic_map_copy(map
->p
[i
]);
6303 isl_basic_map_free(map
->p
[i
]);
6305 if (remove_if_empty(map
, i
) < 0)
6315 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6316 enum isl_dim_type type
, unsigned pos
, int value
)
6320 map
= isl_map_cow(map
);
6324 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6325 for (i
= map
->n
- 1; i
>= 0; --i
) {
6326 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6327 if (remove_if_empty(map
, i
) < 0)
6330 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6337 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6338 enum isl_dim_type type
, unsigned pos
, int value
)
6340 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6343 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6344 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6348 map
= isl_map_cow(map
);
6352 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6353 for (i
= 0; i
< map
->n
; ++i
) {
6354 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6358 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6365 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6366 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6368 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6371 /* Fix the value of the variable at position "pos" of type "type" of "map"
6372 * to be equal to "v".
6374 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6375 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6379 map
= isl_map_cow(map
);
6383 if (!isl_val_is_int(v
))
6384 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6385 "expecting integer value", goto error
);
6386 if (pos
>= isl_map_dim(map
, type
))
6387 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6388 "index out of bounds", goto error
);
6389 for (i
= map
->n
- 1; i
>= 0; --i
) {
6390 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6392 if (remove_if_empty(map
, i
) < 0)
6395 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6404 /* Fix the value of the variable at position "pos" of type "type" of "set"
6405 * to be equal to "v".
6407 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6408 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6410 return isl_map_fix_val(set
, type
, pos
, v
);
6413 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6414 unsigned input
, int value
)
6416 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6419 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6421 return set_from_map(isl_map_fix_si(set_to_map(set
),
6422 isl_dim_set
, dim
, value
));
6425 static __isl_give isl_basic_map
*basic_map_bound_si(
6426 __isl_take isl_basic_map
*bmap
,
6427 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6431 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6432 return isl_basic_map_free(bmap
);
6433 pos
+= isl_basic_map_offset(bmap
, type
);
6434 bmap
= isl_basic_map_cow(bmap
);
6435 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6436 j
= isl_basic_map_alloc_inequality(bmap
);
6439 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6441 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6442 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6444 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6445 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6447 bmap
= isl_basic_map_simplify(bmap
);
6448 return isl_basic_map_finalize(bmap
);
6450 isl_basic_map_free(bmap
);
6454 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6455 __isl_take isl_basic_map
*bmap
,
6456 enum isl_dim_type type
, unsigned pos
, int value
)
6458 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6461 /* Constrain the values of the given dimension to be no greater than "value".
6463 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6464 __isl_take isl_basic_map
*bmap
,
6465 enum isl_dim_type type
, unsigned pos
, int value
)
6467 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6470 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6471 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6475 map
= isl_map_cow(map
);
6479 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6480 for (i
= 0; i
< map
->n
; ++i
) {
6481 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6482 type
, pos
, value
, upper
);
6486 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6493 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6494 enum isl_dim_type type
, unsigned pos
, int value
)
6496 return map_bound_si(map
, type
, pos
, value
, 0);
6499 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6500 enum isl_dim_type type
, unsigned pos
, int value
)
6502 return map_bound_si(map
, type
, pos
, value
, 1);
6505 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6506 enum isl_dim_type type
, unsigned pos
, int value
)
6508 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6512 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6513 enum isl_dim_type type
, unsigned pos
, int value
)
6515 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6518 /* Bound the given variable of "bmap" from below (or above is "upper"
6519 * is set) to "value".
6521 static __isl_give isl_basic_map
*basic_map_bound(
6522 __isl_take isl_basic_map
*bmap
,
6523 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6527 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6528 return isl_basic_map_free(bmap
);
6529 pos
+= isl_basic_map_offset(bmap
, type
);
6530 bmap
= isl_basic_map_cow(bmap
);
6531 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6532 j
= isl_basic_map_alloc_inequality(bmap
);
6535 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6537 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6538 isl_int_set(bmap
->ineq
[j
][0], value
);
6540 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6541 isl_int_neg(bmap
->ineq
[j
][0], value
);
6543 bmap
= isl_basic_map_simplify(bmap
);
6544 return isl_basic_map_finalize(bmap
);
6546 isl_basic_map_free(bmap
);
6550 /* Bound the given variable of "map" from below (or above is "upper"
6551 * is set) to "value".
6553 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6554 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6558 map
= isl_map_cow(map
);
6562 if (pos
>= isl_map_dim(map
, type
))
6563 isl_die(map
->ctx
, isl_error_invalid
,
6564 "index out of bounds", goto error
);
6565 for (i
= map
->n
- 1; i
>= 0; --i
) {
6566 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6567 if (remove_if_empty(map
, i
) < 0)
6570 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6577 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6578 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6580 return map_bound(map
, type
, pos
, value
, 0);
6583 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6584 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6586 return map_bound(map
, type
, pos
, value
, 1);
6589 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6590 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6592 return isl_map_lower_bound(set
, type
, pos
, value
);
6595 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6596 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6598 return isl_map_upper_bound(set
, type
, pos
, value
);
6601 /* Force the values of the variable at position "pos" of type "type" of "set"
6602 * to be no smaller than "value".
6604 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6605 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6609 if (!isl_val_is_int(value
))
6610 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6611 "expecting integer value", goto error
);
6612 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6613 isl_val_free(value
);
6616 isl_val_free(value
);
6621 /* Force the values of the variable at position "pos" of type "type" of "set"
6622 * to be no greater than "value".
6624 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6625 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6629 if (!isl_val_is_int(value
))
6630 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6631 "expecting integer value", goto error
);
6632 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6633 isl_val_free(value
);
6636 isl_val_free(value
);
6641 /* Bound the given variable of "bset" from below (or above is "upper"
6642 * is set) to "value".
6644 static __isl_give isl_basic_set
*isl_basic_set_bound(
6645 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6646 isl_int value
, int upper
)
6648 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6649 type
, pos
, value
, upper
));
6652 /* Bound the given variable of "bset" from below (or above is "upper"
6653 * is set) to "value".
6655 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6656 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6657 __isl_take isl_val
*value
, int upper
)
6661 if (!isl_val_is_int(value
))
6662 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6663 "expecting integer value", goto error
);
6664 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6665 isl_val_free(value
);
6668 isl_val_free(value
);
6669 isl_basic_set_free(bset
);
6673 /* Bound the given variable of "bset" from below to "value".
6675 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6676 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6677 __isl_take isl_val
*value
)
6679 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6682 /* Bound the given variable of "bset" from above to "value".
6684 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6685 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6686 __isl_take isl_val
*value
)
6688 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6691 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6695 map
= isl_map_cow(map
);
6699 map
->dim
= isl_space_reverse(map
->dim
);
6702 for (i
= 0; i
< map
->n
; ++i
) {
6703 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6707 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6715 #define TYPE isl_pw_multi_aff
6717 #define SUFFIX _pw_multi_aff
6719 #define EMPTY isl_pw_multi_aff_empty
6721 #define ADD isl_pw_multi_aff_union_add
6722 #include "isl_map_lexopt_templ.c"
6724 /* Given a map "map", compute the lexicographically minimal
6725 * (or maximal) image element for each domain element in dom,
6726 * in the form of an isl_pw_multi_aff.
6727 * If "empty" is not NULL, then set *empty to those elements in dom that
6728 * do not have an image element.
6729 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6730 * should be computed over the domain of "map". "empty" is also NULL
6733 * We first compute the lexicographically minimal or maximal element
6734 * in the first basic map. This results in a partial solution "res"
6735 * and a subset "todo" of dom that still need to be handled.
6736 * We then consider each of the remaining maps in "map" and successively
6737 * update both "res" and "todo".
6738 * If "empty" is NULL, then the todo sets are not needed and therefore
6739 * also not computed.
6741 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6742 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6743 __isl_give isl_set
**empty
, unsigned flags
)
6747 isl_pw_multi_aff
*res
;
6750 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6751 if (!map
|| (!full
&& !dom
))
6754 if (isl_map_plain_is_empty(map
)) {
6759 return isl_pw_multi_aff_from_map(map
);
6762 res
= basic_map_partial_lexopt_pw_multi_aff(
6763 isl_basic_map_copy(map
->p
[0]),
6764 isl_set_copy(dom
), empty
, flags
);
6768 for (i
= 1; i
< map
->n
; ++i
) {
6769 isl_pw_multi_aff
*res_i
;
6771 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6772 isl_basic_map_copy(map
->p
[i
]),
6773 isl_set_copy(dom
), empty
, flags
);
6775 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6776 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6778 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6781 todo
= isl_set_intersect(todo
, *empty
);
6800 #define TYPE isl_map
6804 #define EMPTY isl_map_empty
6806 #define ADD isl_map_union_disjoint
6807 #include "isl_map_lexopt_templ.c"
6809 /* Given a map "map", compute the lexicographically minimal
6810 * (or maximal) image element for each domain element in "dom",
6811 * in the form of an isl_map.
6812 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6813 * do not have an image element.
6814 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6815 * should be computed over the domain of "map". "empty" is also NULL
6818 * If the input consists of more than one disjunct, then first
6819 * compute the desired result in the form of an isl_pw_multi_aff and
6820 * then convert that into an isl_map.
6822 * This function used to have an explicit implementation in terms
6823 * of isl_maps, but it would continually intersect the domains of
6824 * partial results with the complement of the domain of the next
6825 * partial solution, potentially leading to an explosion in the number
6826 * of disjuncts if there are several disjuncts in the input.
6827 * An even earlier implementation of this function would look for
6828 * better results in the domain of the partial result and for extra
6829 * results in the complement of this domain, which would lead to
6830 * even more splintering.
6832 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6833 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6834 __isl_give isl_set
**empty
, unsigned flags
)
6837 struct isl_map
*res
;
6838 isl_pw_multi_aff
*pma
;
6840 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6841 if (!map
|| (!full
&& !dom
))
6844 if (isl_map_plain_is_empty(map
)) {
6853 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6859 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6861 return isl_map_from_pw_multi_aff(pma
);
6870 __isl_give isl_map
*isl_map_partial_lexmax(
6871 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6872 __isl_give isl_set
**empty
)
6874 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6877 __isl_give isl_map
*isl_map_partial_lexmin(
6878 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6879 __isl_give isl_set
**empty
)
6881 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6884 __isl_give isl_set
*isl_set_partial_lexmin(
6885 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6886 __isl_give isl_set
**empty
)
6888 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6892 __isl_give isl_set
*isl_set_partial_lexmax(
6893 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6894 __isl_give isl_set
**empty
)
6896 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6900 /* Compute the lexicographic minimum (or maximum if "flags" includes
6901 * ISL_OPT_MAX) of "bset" over its parametric domain.
6903 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6906 return isl_basic_map_lexopt(bset
, flags
);
6909 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6911 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6914 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6916 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6919 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6921 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6924 /* Compute the lexicographic minimum of "bset" over its parametric domain
6925 * for the purpose of quantifier elimination.
6926 * That is, find an explicit representation for all the existentially
6927 * quantified variables in "bset" by computing their lexicographic
6930 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6931 __isl_take isl_basic_set
*bset
)
6933 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6936 /* Given a basic map with one output dimension, compute the minimum or
6937 * maximum of that dimension as an isl_pw_aff.
6939 * Compute the optimum as a lexicographic optimum over the single
6940 * output dimension and extract the single isl_pw_aff from the result.
6942 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6945 isl_pw_multi_aff
*pma
;
6948 bmap
= isl_basic_map_copy(bmap
);
6949 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6950 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6951 isl_pw_multi_aff_free(pma
);
6956 /* Compute the minimum or maximum of the given output dimension
6957 * as a function of the parameters and the input dimensions,
6958 * but independently of the other output dimensions.
6960 * We first project out the other output dimension and then compute
6961 * the "lexicographic" maximum in each basic map, combining the results
6962 * using isl_pw_aff_union_max.
6964 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6971 n_out
= isl_map_dim(map
, isl_dim_out
);
6972 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6973 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6978 isl_space
*dim
= isl_map_get_space(map
);
6980 return isl_pw_aff_empty(dim
);
6983 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6984 for (i
= 1; i
< map
->n
; ++i
) {
6985 isl_pw_aff
*pwaff_i
;
6987 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6988 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6996 /* Compute the minimum of the given output dimension as a function of the
6997 * parameters and input dimensions, but independently of
6998 * the other output dimensions.
7000 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7002 return map_dim_opt(map
, pos
, 0);
7005 /* Compute the maximum of the given output dimension as a function of the
7006 * parameters and input dimensions, but independently of
7007 * the other output dimensions.
7009 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7011 return map_dim_opt(map
, pos
, 1);
7014 /* Compute the minimum or maximum of the given set dimension
7015 * as a function of the parameters,
7016 * but independently of the other set dimensions.
7018 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7021 return map_dim_opt(set
, pos
, max
);
7024 /* Compute the maximum of the given set dimension as a function of the
7025 * parameters, but independently of the other set dimensions.
7027 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7029 return set_dim_opt(set
, pos
, 1);
7032 /* Compute the minimum of the given set dimension as a function of the
7033 * parameters, but independently of the other set dimensions.
7035 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7037 return set_dim_opt(set
, pos
, 0);
7040 /* Apply a preimage specified by "mat" on the parameters of "bset".
7041 * bset is assumed to have only parameters and divs.
7043 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7044 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7051 bset
->dim
= isl_space_cow(bset
->dim
);
7055 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7057 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7059 bset
->dim
->nparam
= 0;
7060 bset
->dim
->n_out
= nparam
;
7061 bset
= isl_basic_set_preimage(bset
, mat
);
7063 bset
->dim
->nparam
= bset
->dim
->n_out
;
7064 bset
->dim
->n_out
= 0;
7069 isl_basic_set_free(bset
);
7073 /* Apply a preimage specified by "mat" on the parameters of "set".
7074 * set is assumed to have only parameters and divs.
7076 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7077 __isl_take isl_mat
*mat
)
7085 nparam
= isl_set_dim(set
, isl_dim_param
);
7087 if (mat
->n_row
!= 1 + nparam
)
7088 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7089 "unexpected number of rows", goto error
);
7091 space
= isl_set_get_space(set
);
7092 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7093 isl_dim_param
, 0, nparam
);
7094 set
= isl_set_reset_space(set
, space
);
7095 set
= isl_set_preimage(set
, mat
);
7096 nparam
= isl_set_dim(set
, isl_dim_out
);
7097 space
= isl_set_get_space(set
);
7098 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7099 isl_dim_out
, 0, nparam
);
7100 set
= isl_set_reset_space(set
, space
);
7108 /* Intersect the basic set "bset" with the affine space specified by the
7109 * equalities in "eq".
7111 static __isl_give isl_basic_set
*basic_set_append_equalities(
7112 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7120 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7125 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7126 for (i
= 0; i
< eq
->n_row
; ++i
) {
7127 k
= isl_basic_set_alloc_equality(bset
);
7130 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7131 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7135 bset
= isl_basic_set_gauss(bset
, NULL
);
7136 bset
= isl_basic_set_finalize(bset
);
7141 isl_basic_set_free(bset
);
7145 /* Intersect the set "set" with the affine space specified by the
7146 * equalities in "eq".
7148 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7156 for (i
= 0; i
< set
->n
; ++i
) {
7157 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7170 /* Given a basic set "bset" that only involves parameters and existentially
7171 * quantified variables, return the index of the first equality
7172 * that only involves parameters. If there is no such equality then
7173 * return bset->n_eq.
7175 * This function assumes that isl_basic_set_gauss has been called on "bset".
7177 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7180 unsigned nparam
, n_div
;
7185 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7186 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7188 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7189 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7196 /* Compute an explicit representation for the existentially quantified
7197 * variables in "bset" by computing the "minimal value" of the set
7198 * variables. Since there are no set variables, the computation of
7199 * the minimal value essentially computes an explicit representation
7200 * of the non-empty part(s) of "bset".
7202 * The input only involves parameters and existentially quantified variables.
7203 * All equalities among parameters have been removed.
7205 * Since the existentially quantified variables in the result are in general
7206 * going to be different from those in the input, we first replace
7207 * them by the minimal number of variables based on their equalities.
7208 * This should simplify the parametric integer programming.
7210 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7212 isl_morph
*morph1
, *morph2
;
7218 if (bset
->n_eq
== 0)
7219 return isl_basic_set_lexmin_compute_divs(bset
);
7221 morph1
= isl_basic_set_parameter_compression(bset
);
7222 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7223 bset
= isl_basic_set_lift(bset
);
7224 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7225 bset
= isl_morph_basic_set(morph2
, bset
);
7226 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7227 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7229 set
= isl_basic_set_lexmin_compute_divs(bset
);
7231 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7236 /* Project the given basic set onto its parameter domain, possibly introducing
7237 * new, explicit, existential variables in the constraints.
7238 * The input has parameters and (possibly implicit) existential variables.
7239 * The output has the same parameters, but only
7240 * explicit existentially quantified variables.
7242 * The actual projection is performed by pip, but pip doesn't seem
7243 * to like equalities very much, so we first remove the equalities
7244 * among the parameters by performing a variable compression on
7245 * the parameters. Afterward, an inverse transformation is performed
7246 * and the equalities among the parameters are inserted back in.
7248 * The variable compression on the parameters may uncover additional
7249 * equalities that were only implicit before. We therefore check
7250 * if there are any new parameter equalities in the result and
7251 * if so recurse. The removal of parameter equalities is required
7252 * for the parameter compression performed by base_compute_divs.
7254 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7258 struct isl_mat
*T
, *T2
;
7259 struct isl_set
*set
;
7262 bset
= isl_basic_set_cow(bset
);
7266 if (bset
->n_eq
== 0)
7267 return base_compute_divs(bset
);
7269 bset
= isl_basic_set_gauss(bset
, NULL
);
7272 if (isl_basic_set_plain_is_empty(bset
))
7273 return isl_set_from_basic_set(bset
);
7275 i
= first_parameter_equality(bset
);
7276 if (i
== bset
->n_eq
)
7277 return base_compute_divs(bset
);
7279 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7280 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7282 eq
= isl_mat_cow(eq
);
7283 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7284 if (T
&& T
->n_col
== 0) {
7288 bset
= isl_basic_set_set_to_empty(bset
);
7289 return isl_set_from_basic_set(bset
);
7291 bset
= basic_set_parameter_preimage(bset
, T
);
7293 i
= first_parameter_equality(bset
);
7296 else if (i
== bset
->n_eq
)
7297 set
= base_compute_divs(bset
);
7299 set
= parameter_compute_divs(bset
);
7300 set
= set_parameter_preimage(set
, T2
);
7301 set
= set_append_equalities(set
, eq
);
7305 /* Insert the divs from "ls" before those of "bmap".
7307 * The number of columns is not changed, which means that the last
7308 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7309 * The caller is responsible for removing the same number of dimensions
7310 * from the space of "bmap".
7312 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7313 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7319 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7323 old_n_div
= bmap
->n_div
;
7324 bmap
= insert_div_rows(bmap
, n_div
);
7328 for (i
= 0; i
< n_div
; ++i
) {
7329 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7330 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7336 /* Replace the space of "bmap" by the space and divs of "ls".
7338 * If "ls" has any divs, then we simplify the result since we may
7339 * have discovered some additional equalities that could simplify
7340 * the div expressions.
7342 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7343 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7347 bmap
= isl_basic_map_cow(bmap
);
7351 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7352 bmap
= insert_divs_from_local_space(bmap
, ls
);
7356 isl_space_free(bmap
->dim
);
7357 bmap
->dim
= isl_local_space_get_space(ls
);
7361 isl_local_space_free(ls
);
7363 bmap
= isl_basic_map_simplify(bmap
);
7364 bmap
= isl_basic_map_finalize(bmap
);
7367 isl_basic_map_free(bmap
);
7368 isl_local_space_free(ls
);
7372 /* Replace the space of "map" by the space and divs of "ls".
7374 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7375 __isl_take isl_local_space
*ls
)
7379 map
= isl_map_cow(map
);
7383 for (i
= 0; i
< map
->n
; ++i
) {
7384 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7385 isl_local_space_copy(ls
));
7389 isl_space_free(map
->dim
);
7390 map
->dim
= isl_local_space_get_space(ls
);
7394 isl_local_space_free(ls
);
7397 isl_local_space_free(ls
);
7402 /* Compute an explicit representation for the existentially
7403 * quantified variables for which do not know any explicit representation yet.
7405 * We first sort the existentially quantified variables so that the
7406 * existentially quantified variables for which we already have an explicit
7407 * representation are placed before those for which we do not.
7408 * The input dimensions, the output dimensions and the existentially
7409 * quantified variables for which we already have an explicit
7410 * representation are then turned into parameters.
7411 * compute_divs returns a map with the same parameters and
7412 * no input or output dimensions and the dimension specification
7413 * is reset to that of the input, including the existentially quantified
7414 * variables for which we already had an explicit representation.
7416 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7418 struct isl_basic_set
*bset
;
7419 struct isl_set
*set
;
7420 struct isl_map
*map
;
7422 isl_local_space
*ls
;
7429 bmap
= isl_basic_map_sort_divs(bmap
);
7430 bmap
= isl_basic_map_cow(bmap
);
7434 n_known
= isl_basic_map_first_unknown_div(bmap
);
7436 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7438 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7439 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7440 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7441 dim
= isl_space_set_alloc(bmap
->ctx
,
7442 nparam
+ n_in
+ n_out
+ n_known
, 0);
7446 ls
= isl_basic_map_get_local_space(bmap
);
7447 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7448 n_known
, bmap
->n_div
- n_known
);
7450 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7451 swap_div(bmap
, i
- n_known
, i
);
7452 bmap
->n_div
-= n_known
;
7453 bmap
->extra
-= n_known
;
7455 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7456 bset
= bset_from_bmap(bmap
);
7458 set
= parameter_compute_divs(bset
);
7459 map
= set_to_map(set
);
7460 map
= replace_space_by_local_space(map
, ls
);
7464 isl_basic_map_free(bmap
);
7468 /* Remove the explicit representation of local variable "div",
7471 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7472 __isl_take isl_basic_map
*bmap
, int div
)
7476 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7478 return isl_basic_map_free(bmap
);
7482 bmap
= isl_basic_map_cow(bmap
);
7485 isl_int_set_si(bmap
->div
[div
][0], 0);
7489 /* Is local variable "div" of "bmap" marked as not having an explicit
7491 * Note that even if "div" is not marked in this way and therefore
7492 * has an explicit representation, this representation may still
7493 * depend (indirectly) on other local variables that do not
7494 * have an explicit representation.
7496 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7499 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7500 return isl_bool_error
;
7501 return isl_int_is_zero(bmap
->div
[div
][0]);
7504 /* Return the position of the first local variable that does not
7505 * have an explicit representation.
7506 * Return the total number of local variables if they all have
7507 * an explicit representation.
7508 * Return -1 on error.
7510 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7517 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7518 if (!isl_basic_map_div_is_known(bmap
, i
))
7524 /* Return the position of the first local variable that does not
7525 * have an explicit representation.
7526 * Return the total number of local variables if they all have
7527 * an explicit representation.
7528 * Return -1 on error.
7530 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7532 return isl_basic_map_first_unknown_div(bset
);
7535 /* Does "bmap" have an explicit representation for all local variables?
7537 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7541 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7542 first
= isl_basic_map_first_unknown_div(bmap
);
7544 return isl_bool_error
;
7548 /* Do all basic maps in "map" have an explicit representation
7549 * for all local variables?
7551 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7556 return isl_bool_error
;
7558 for (i
= 0; i
< map
->n
; ++i
) {
7559 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7564 return isl_bool_true
;
7567 /* If bmap contains any unknown divs, then compute explicit
7568 * expressions for them. However, this computation may be
7569 * quite expensive, so first try to remove divs that aren't
7572 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7575 struct isl_map
*map
;
7577 known
= isl_basic_map_divs_known(bmap
);
7581 return isl_map_from_basic_map(bmap
);
7583 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7585 known
= isl_basic_map_divs_known(bmap
);
7589 return isl_map_from_basic_map(bmap
);
7591 map
= compute_divs(bmap
);
7594 isl_basic_map_free(bmap
);
7598 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7602 struct isl_map
*res
;
7609 known
= isl_map_divs_known(map
);
7617 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7618 for (i
= 1 ; i
< map
->n
; ++i
) {
7620 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7621 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7622 res
= isl_map_union_disjoint(res
, r2
);
7624 res
= isl_map_union(res
, r2
);
7631 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7633 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7636 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7638 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7641 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7644 struct isl_set
*set
;
7649 map
= isl_map_cow(map
);
7653 set
= set_from_map(map
);
7654 set
->dim
= isl_space_domain(set
->dim
);
7657 for (i
= 0; i
< map
->n
; ++i
) {
7658 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7662 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7663 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7670 /* Return the union of "map1" and "map2", where we assume for now that
7671 * "map1" and "map2" are disjoint. Note that the basic maps inside
7672 * "map1" or "map2" may not be disjoint from each other.
7673 * Also note that this function is also called from isl_map_union,
7674 * which takes care of handling the situation where "map1" and "map2"
7675 * may not be disjoint.
7677 * If one of the inputs is empty, we can simply return the other input.
7678 * Similarly, if one of the inputs is universal, then it is equal to the union.
7680 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7681 __isl_take isl_map
*map2
)
7685 struct isl_map
*map
= NULL
;
7691 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7692 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7693 "spaces don't match", goto error
);
7704 is_universe
= isl_map_plain_is_universe(map1
);
7705 if (is_universe
< 0)
7712 is_universe
= isl_map_plain_is_universe(map2
);
7713 if (is_universe
< 0)
7720 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7721 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7722 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7724 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7725 map1
->n
+ map2
->n
, flags
);
7728 for (i
= 0; i
< map1
->n
; ++i
) {
7729 map
= isl_map_add_basic_map(map
,
7730 isl_basic_map_copy(map1
->p
[i
]));
7734 for (i
= 0; i
< map2
->n
; ++i
) {
7735 map
= isl_map_add_basic_map(map
,
7736 isl_basic_map_copy(map2
->p
[i
]));
7750 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7751 * guaranteed to be disjoint by the caller.
7753 * Note that this functions is called from within isl_map_make_disjoint,
7754 * so we have to be careful not to touch the constraints of the inputs
7757 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7758 __isl_take isl_map
*map2
)
7760 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7763 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7764 * not be disjoint. The parameters are assumed to have been aligned.
7766 * We currently simply call map_union_disjoint, the internal operation
7767 * of which does not really depend on the inputs being disjoint.
7768 * If the result contains more than one basic map, then we clear
7769 * the disjoint flag since the result may contain basic maps from
7770 * both inputs and these are not guaranteed to be disjoint.
7772 * As a special case, if "map1" and "map2" are obviously equal,
7773 * then we simply return "map1".
7775 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7776 __isl_take isl_map
*map2
)
7783 equal
= isl_map_plain_is_equal(map1
, map2
);
7791 map1
= map_union_disjoint(map1
, map2
);
7795 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7803 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7806 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7807 __isl_take isl_map
*map2
)
7809 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7812 __isl_give isl_set
*isl_set_union_disjoint(
7813 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7815 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7819 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7821 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7824 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7827 * "map" and "set" are assumed to be compatible and non-NULL.
7829 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7830 __isl_take isl_set
*set
,
7831 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7832 __isl_take isl_basic_set
*bset
))
7835 struct isl_map
*result
;
7838 if (isl_set_plain_is_universe(set
)) {
7843 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7844 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7845 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7847 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7848 map
->n
* set
->n
, flags
);
7849 for (i
= 0; result
&& i
< map
->n
; ++i
)
7850 for (j
= 0; j
< set
->n
; ++j
) {
7851 result
= isl_map_add_basic_map(result
,
7852 fn(isl_basic_map_copy(map
->p
[i
]),
7853 isl_basic_set_copy(set
->p
[j
])));
7863 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7864 __isl_take isl_set
*set
)
7868 ok
= isl_map_compatible_range(map
, set
);
7872 isl_die(set
->ctx
, isl_error_invalid
,
7873 "incompatible spaces", goto error
);
7875 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7882 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7883 __isl_take isl_set
*set
)
7885 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7888 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7889 __isl_take isl_set
*set
)
7893 ok
= isl_map_compatible_domain(map
, set
);
7897 isl_die(set
->ctx
, isl_error_invalid
,
7898 "incompatible spaces", goto error
);
7900 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7907 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7908 __isl_take isl_set
*set
)
7910 return isl_map_align_params_map_map_and(map
, set
,
7911 &map_intersect_domain
);
7914 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7915 * in the space B -> C, return the intersection.
7916 * The parameters are assumed to have been aligned.
7918 * The map "factor" is first extended to a map living in the space
7919 * [A -> B] -> C and then a regular intersection is computed.
7921 static __isl_give isl_map
*map_intersect_domain_factor_range(
7922 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7925 isl_map
*ext_factor
;
7927 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7928 ext_factor
= isl_map_universe(space
);
7929 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7930 return map_intersect(map
, ext_factor
);
7933 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7934 * in the space B -> C, return the intersection.
7936 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7937 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7939 return isl_map_align_params_map_map_and(map
, factor
,
7940 &map_intersect_domain_factor_range
);
7943 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7944 * in the space A -> C, return the intersection.
7946 * The map "factor" is first extended to a map living in the space
7947 * A -> [B -> C] and then a regular intersection is computed.
7949 static __isl_give isl_map
*map_intersect_range_factor_range(
7950 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7953 isl_map
*ext_factor
;
7955 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7956 ext_factor
= isl_map_universe(space
);
7957 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7958 return isl_map_intersect(map
, ext_factor
);
7961 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7962 * in the space A -> C, return the intersection.
7964 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7965 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7967 return isl_map_align_params_map_map_and(map
, factor
,
7968 &map_intersect_range_factor_range
);
7971 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7972 __isl_take isl_map
*map2
)
7976 map1
= isl_map_reverse(map1
);
7977 map1
= isl_map_apply_range(map1
, map2
);
7978 return isl_map_reverse(map1
);
7985 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7986 __isl_take isl_map
*map2
)
7988 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7991 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7992 __isl_take isl_map
*map2
)
7994 isl_space
*dim_result
;
7995 struct isl_map
*result
;
8001 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
8002 isl_space_copy(map2
->dim
));
8004 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
8007 for (i
= 0; i
< map1
->n
; ++i
)
8008 for (j
= 0; j
< map2
->n
; ++j
) {
8009 result
= isl_map_add_basic_map(result
,
8010 isl_basic_map_apply_range(
8011 isl_basic_map_copy(map1
->p
[i
]),
8012 isl_basic_map_copy(map2
->p
[j
])));
8018 if (result
&& result
->n
<= 1)
8019 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8027 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8028 __isl_take isl_map
*map2
)
8030 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8034 * returns range - domain
8036 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8038 isl_space
*target_space
;
8039 struct isl_basic_set
*bset
;
8046 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8047 bmap
->dim
, isl_dim_out
),
8049 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8050 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8051 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8052 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8053 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8054 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8055 for (i
= 0; i
< dim
; ++i
) {
8056 int j
= isl_basic_map_alloc_equality(bmap
);
8058 bmap
= isl_basic_map_free(bmap
);
8061 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8062 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8063 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8064 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8066 bset
= isl_basic_map_domain(bmap
);
8067 bset
= isl_basic_set_reset_space(bset
, target_space
);
8070 isl_basic_map_free(bmap
);
8075 * returns range - domain
8077 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8081 struct isl_set
*result
;
8086 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8087 map
->dim
, isl_dim_out
),
8089 dim
= isl_map_get_space(map
);
8090 dim
= isl_space_domain(dim
);
8091 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8094 for (i
= 0; i
< map
->n
; ++i
)
8095 result
= isl_set_add_basic_set(result
,
8096 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8105 * returns [domain -> range] -> range - domain
8107 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8108 __isl_take isl_basic_map
*bmap
)
8112 isl_basic_map
*domain
;
8116 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8117 bmap
->dim
, isl_dim_out
))
8118 isl_die(bmap
->ctx
, isl_error_invalid
,
8119 "domain and range don't match", goto error
);
8121 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8122 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8124 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
8125 domain
= isl_basic_map_universe(dim
);
8127 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8128 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8129 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8131 total
= isl_basic_map_total_dim(bmap
);
8133 for (i
= 0; i
< n
; ++i
) {
8134 k
= isl_basic_map_alloc_equality(bmap
);
8137 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8138 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8139 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8140 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8143 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8144 return isl_basic_map_finalize(bmap
);
8146 isl_basic_map_free(bmap
);
8151 * returns [domain -> range] -> range - domain
8153 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8156 isl_space
*domain_dim
;
8161 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8162 map
->dim
, isl_dim_out
))
8163 isl_die(map
->ctx
, isl_error_invalid
,
8164 "domain and range don't match", goto error
);
8166 map
= isl_map_cow(map
);
8170 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
8171 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8172 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
8175 for (i
= 0; i
< map
->n
; ++i
) {
8176 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8180 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8187 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
8189 struct isl_basic_map
*bmap
;
8197 nparam
= dims
->nparam
;
8199 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
8203 for (i
= 0; i
< dim
; ++i
) {
8204 int j
= isl_basic_map_alloc_equality(bmap
);
8207 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8208 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8209 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
8211 return isl_basic_map_finalize(bmap
);
8213 isl_basic_map_free(bmap
);
8217 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
8221 if (dim
->n_in
!= dim
->n_out
)
8222 isl_die(dim
->ctx
, isl_error_invalid
,
8223 "number of input and output dimensions needs to be "
8224 "the same", goto error
);
8225 return basic_map_identity(dim
);
8227 isl_space_free(dim
);
8231 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8233 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8236 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8238 isl_space
*dim
= isl_set_get_space(set
);
8240 id
= isl_map_identity(isl_space_map_from_set(dim
));
8241 return isl_map_intersect_range(id
, set
);
8244 /* Construct a basic set with all set dimensions having only non-negative
8247 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8248 __isl_take isl_space
*space
)
8253 struct isl_basic_set
*bset
;
8257 nparam
= space
->nparam
;
8259 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8262 for (i
= 0; i
< dim
; ++i
) {
8263 int k
= isl_basic_set_alloc_inequality(bset
);
8266 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8267 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8271 isl_basic_set_free(bset
);
8275 /* Construct the half-space x_pos >= 0.
8277 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
8281 isl_basic_set
*nonneg
;
8283 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8284 k
= isl_basic_set_alloc_inequality(nonneg
);
8287 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8288 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8290 return isl_basic_set_finalize(nonneg
);
8292 isl_basic_set_free(nonneg
);
8296 /* Construct the half-space x_pos <= -1.
8298 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
8303 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8304 k
= isl_basic_set_alloc_inequality(neg
);
8307 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8308 isl_int_set_si(neg
->ineq
[k
][0], -1);
8309 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8311 return isl_basic_set_finalize(neg
);
8313 isl_basic_set_free(neg
);
8317 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8318 enum isl_dim_type type
, unsigned first
, unsigned n
)
8322 isl_basic_set
*nonneg
;
8330 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8332 offset
= pos(set
->dim
, type
);
8333 for (i
= 0; i
< n
; ++i
) {
8334 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8335 offset
+ first
+ i
);
8336 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8338 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8347 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8349 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8355 return isl_stat_error
;
8356 if (isl_set_plain_is_empty(set
)) {
8361 return fn(set
, signs
, user
);
8364 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8366 half
= isl_set_intersect(half
, isl_set_copy(set
));
8367 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8371 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8373 half
= isl_set_intersect(half
, set
);
8374 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8377 return isl_stat_error
;
8380 /* Call "fn" on the intersections of "set" with each of the orthants
8381 * (except for obviously empty intersections). The orthant is identified
8382 * by the signs array, with each entry having value 1 or -1 according
8383 * to the sign of the corresponding variable.
8385 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8386 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8395 return isl_stat_error
;
8396 if (isl_set_plain_is_empty(set
))
8399 nparam
= isl_set_dim(set
, isl_dim_param
);
8400 nvar
= isl_set_dim(set
, isl_dim_set
);
8402 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8404 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8412 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8414 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8417 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8418 __isl_keep isl_basic_map
*bmap2
)
8421 struct isl_map
*map1
;
8422 struct isl_map
*map2
;
8424 if (!bmap1
|| !bmap2
)
8425 return isl_bool_error
;
8427 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8428 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8430 is_subset
= isl_map_is_subset(map1
, map2
);
8438 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8439 __isl_keep isl_basic_set
*bset2
)
8441 return isl_basic_map_is_subset(bset1
, bset2
);
8444 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8445 __isl_keep isl_basic_map
*bmap2
)
8449 if (!bmap1
|| !bmap2
)
8450 return isl_bool_error
;
8451 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8452 if (is_subset
!= isl_bool_true
)
8454 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8458 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8459 __isl_keep isl_basic_set
*bset2
)
8461 return isl_basic_map_is_equal(
8462 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8465 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8471 return isl_bool_error
;
8472 for (i
= 0; i
< map
->n
; ++i
) {
8473 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8475 return isl_bool_error
;
8477 return isl_bool_false
;
8479 return isl_bool_true
;
8482 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8484 return map
? map
->n
== 0 : isl_bool_error
;
8487 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8489 return set
? set
->n
== 0 : isl_bool_error
;
8492 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8494 return isl_map_is_empty(set_to_map(set
));
8497 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8498 __isl_keep isl_map
*map2
)
8501 return isl_bool_error
;
8503 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8506 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8507 __isl_keep isl_set
*set2
)
8510 return isl_bool_error
;
8512 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8515 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8520 return isl_bool_error
;
8521 is_subset
= isl_map_is_subset(map1
, map2
);
8522 if (is_subset
!= isl_bool_true
)
8524 is_subset
= isl_map_is_subset(map2
, map1
);
8528 /* Is "map1" equal to "map2"?
8530 * First check if they are obviously equal.
8531 * If not, then perform a more detailed analysis.
8533 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8537 equal
= isl_map_plain_is_equal(map1
, map2
);
8538 if (equal
< 0 || equal
)
8540 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8543 isl_bool
isl_basic_map_is_strict_subset(
8544 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8548 if (!bmap1
|| !bmap2
)
8549 return isl_bool_error
;
8550 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8551 if (is_subset
!= isl_bool_true
)
8553 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8554 if (is_subset
== isl_bool_error
)
8559 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8560 __isl_keep isl_map
*map2
)
8565 return isl_bool_error
;
8566 is_subset
= isl_map_is_subset(map1
, map2
);
8567 if (is_subset
!= isl_bool_true
)
8569 is_subset
= isl_map_is_subset(map2
, map1
);
8570 if (is_subset
== isl_bool_error
)
8575 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8576 __isl_keep isl_set
*set2
)
8578 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8581 /* Is "bmap" obviously equal to the universe with the same space?
8583 * That is, does it not have any constraints?
8585 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8588 return isl_bool_error
;
8589 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8592 /* Is "bset" obviously equal to the universe with the same space?
8594 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8596 return isl_basic_map_plain_is_universe(bset
);
8599 /* If "c" does not involve any existentially quantified variables,
8600 * then set *univ to false and abort
8602 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8604 isl_bool
*univ
= user
;
8607 n
= isl_constraint_dim(c
, isl_dim_div
);
8608 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8609 isl_constraint_free(c
);
8610 if (*univ
< 0 || !*univ
)
8611 return isl_stat_error
;
8615 /* Is "bmap" equal to the universe with the same space?
8617 * First check if it is obviously equal to the universe.
8618 * If not and if there are any constraints not involving
8619 * existentially quantified variables, then it is certainly
8620 * not equal to the universe.
8621 * Otherwise, check if the universe is a subset of "bmap".
8623 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8626 isl_basic_map
*test
;
8628 univ
= isl_basic_map_plain_is_universe(bmap
);
8629 if (univ
< 0 || univ
)
8631 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8632 return isl_bool_false
;
8633 univ
= isl_bool_true
;
8634 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8636 return isl_bool_error
;
8637 if (univ
< 0 || !univ
)
8639 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8640 univ
= isl_basic_map_is_subset(test
, bmap
);
8641 isl_basic_map_free(test
);
8645 /* Is "bset" equal to the universe with the same space?
8647 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8649 return isl_basic_map_is_universe(bset
);
8652 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8657 return isl_bool_error
;
8659 for (i
= 0; i
< map
->n
; ++i
) {
8660 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8665 return isl_bool_false
;
8668 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8670 return isl_map_plain_is_universe(set_to_map(set
));
8673 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8675 struct isl_basic_set
*bset
= NULL
;
8676 struct isl_vec
*sample
= NULL
;
8677 isl_bool empty
, non_empty
;
8680 return isl_bool_error
;
8682 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8683 return isl_bool_true
;
8685 if (isl_basic_map_plain_is_universe(bmap
))
8686 return isl_bool_false
;
8688 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8689 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8690 copy
= isl_basic_map_remove_redundancies(copy
);
8691 empty
= isl_basic_map_plain_is_empty(copy
);
8692 isl_basic_map_free(copy
);
8696 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8698 return isl_bool_error
;
8700 return isl_bool_false
;
8701 isl_vec_free(bmap
->sample
);
8702 bmap
->sample
= NULL
;
8703 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8705 return isl_bool_error
;
8706 sample
= isl_basic_set_sample_vec(bset
);
8708 return isl_bool_error
;
8709 empty
= sample
->size
== 0;
8710 isl_vec_free(bmap
->sample
);
8711 bmap
->sample
= sample
;
8713 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8718 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8721 return isl_bool_error
;
8722 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8725 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8728 return isl_bool_error
;
8729 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8732 /* Is "bmap" known to be non-empty?
8734 * That is, is the cached sample still valid?
8736 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8741 return isl_bool_error
;
8743 return isl_bool_false
;
8744 total
= 1 + isl_basic_map_total_dim(bmap
);
8745 if (bmap
->sample
->size
!= total
)
8746 return isl_bool_false
;
8747 return isl_basic_map_contains(bmap
, bmap
->sample
);
8750 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8752 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8755 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8756 __isl_take isl_basic_map
*bmap2
)
8758 struct isl_map
*map
;
8759 if (!bmap1
|| !bmap2
)
8762 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8764 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8767 map
= isl_map_add_basic_map(map
, bmap1
);
8768 map
= isl_map_add_basic_map(map
, bmap2
);
8771 isl_basic_map_free(bmap1
);
8772 isl_basic_map_free(bmap2
);
8776 struct isl_set
*isl_basic_set_union(
8777 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8779 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8780 bset_to_bmap(bset2
)));
8783 /* Order divs such that any div only depends on previous divs */
8784 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8785 __isl_take isl_basic_map
*bmap
)
8793 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8795 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8797 if (isl_int_is_zero(bmap
->div
[i
][0]))
8799 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8804 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8805 "integer division depends on itself",
8806 return isl_basic_map_free(bmap
));
8807 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8813 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8815 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8818 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8825 for (i
= 0; i
< map
->n
; ++i
) {
8826 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8837 /* Sort the local variables of "bset".
8839 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8840 __isl_take isl_basic_set
*bset
)
8842 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8845 /* Apply the expansion computed by isl_merge_divs.
8846 * The expansion itself is given by "exp" while the resulting
8847 * list of divs is given by "div".
8849 * Move the integer divisions of "bmap" into the right position
8850 * according to "exp" and then introduce the additional integer
8851 * divisions, adding div constraints.
8852 * The moving should be done first to avoid moving coefficients
8853 * in the definitions of the extra integer divisions.
8855 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8856 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8861 bmap
= isl_basic_map_cow(bmap
);
8865 if (div
->n_row
< bmap
->n_div
)
8866 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8867 "not an expansion", goto error
);
8869 n_div
= bmap
->n_div
;
8870 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8871 div
->n_row
- n_div
, 0,
8872 2 * (div
->n_row
- n_div
));
8874 for (i
= n_div
; i
< div
->n_row
; ++i
)
8875 if (isl_basic_map_alloc_div(bmap
) < 0)
8878 for (j
= n_div
- 1; j
>= 0; --j
) {
8881 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8884 for (i
= 0; i
< div
->n_row
; ++i
) {
8885 if (j
< n_div
&& exp
[j
] == i
) {
8888 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8889 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8891 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8899 isl_basic_map_free(bmap
);
8904 /* Apply the expansion computed by isl_merge_divs.
8905 * The expansion itself is given by "exp" while the resulting
8906 * list of divs is given by "div".
8908 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8909 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8911 return isl_basic_map_expand_divs(bset
, div
, exp
);
8914 /* Look for a div in dst that corresponds to the div "div" in src.
8915 * The divs before "div" in src and dst are assumed to be the same.
8917 * Returns -1 if no corresponding div was found and the position
8918 * of the corresponding div in dst otherwise.
8920 static int find_div(__isl_keep isl_basic_map
*dst
,
8921 __isl_keep isl_basic_map
*src
, unsigned div
)
8925 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8927 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8928 for (i
= div
; i
< dst
->n_div
; ++i
)
8929 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8930 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8931 dst
->n_div
- div
) == -1)
8936 /* Align the divs of "dst" to those of "src", adding divs from "src"
8937 * if needed. That is, make sure that the first src->n_div divs
8938 * of the result are equal to those of src.
8940 * The result is not finalized as by design it will have redundant
8941 * divs if any divs from "src" were copied.
8943 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8944 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8947 int known
, extended
;
8951 return isl_basic_map_free(dst
);
8953 if (src
->n_div
== 0)
8956 known
= isl_basic_map_divs_known(src
);
8958 return isl_basic_map_free(dst
);
8960 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8961 "some src divs are unknown",
8962 return isl_basic_map_free(dst
));
8964 src
= isl_basic_map_order_divs(src
);
8967 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8968 for (i
= 0; i
< src
->n_div
; ++i
) {
8969 int j
= find_div(dst
, src
, i
);
8972 int extra
= src
->n_div
- i
;
8973 dst
= isl_basic_map_cow(dst
);
8976 dst
= isl_basic_map_extend_space(dst
,
8977 isl_space_copy(dst
->dim
),
8978 extra
, 0, 2 * extra
);
8981 j
= isl_basic_map_alloc_div(dst
);
8983 return isl_basic_map_free(dst
);
8984 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8985 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8986 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8987 return isl_basic_map_free(dst
);
8990 isl_basic_map_swap_div(dst
, i
, j
);
8995 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9003 map
= isl_map_compute_divs(map
);
9004 map
= isl_map_cow(map
);
9008 for (i
= 1; i
< map
->n
; ++i
)
9009 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9010 for (i
= 1; i
< map
->n
; ++i
) {
9011 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9013 return isl_map_free(map
);
9016 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
9020 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9022 return isl_map_align_divs_internal(map
);
9025 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
9027 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9030 /* Align the divs of the basic maps in "map" to those
9031 * of the basic maps in "list", as well as to the other basic maps in "map".
9032 * The elements in "list" are assumed to have known divs.
9034 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9035 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9039 map
= isl_map_compute_divs(map
);
9040 map
= isl_map_cow(map
);
9042 return isl_map_free(map
);
9046 n
= isl_basic_map_list_n_basic_map(list
);
9047 for (i
= 0; i
< n
; ++i
) {
9048 isl_basic_map
*bmap
;
9050 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9051 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9052 isl_basic_map_free(bmap
);
9055 return isl_map_free(map
);
9057 return isl_map_align_divs_internal(map
);
9060 /* Align the divs of each element of "list" to those of "bmap".
9061 * Both "bmap" and the elements of "list" are assumed to have known divs.
9063 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9064 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9069 return isl_basic_map_list_free(list
);
9071 n
= isl_basic_map_list_n_basic_map(list
);
9072 for (i
= 0; i
< n
; ++i
) {
9073 isl_basic_map
*bmap_i
;
9075 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9076 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9077 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9083 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9084 __isl_take isl_map
*map
)
9088 ok
= isl_map_compatible_domain(map
, set
);
9092 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9093 "incompatible spaces", goto error
);
9094 map
= isl_map_intersect_domain(map
, set
);
9095 set
= isl_map_range(map
);
9103 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9104 __isl_take isl_map
*map
)
9106 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9109 /* There is no need to cow as removing empty parts doesn't change
9110 * the meaning of the set.
9112 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9119 for (i
= map
->n
- 1; i
>= 0; --i
)
9120 remove_if_empty(map
, i
);
9125 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9127 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9130 /* Create a binary relation that maps the shared initial "pos" dimensions
9131 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9133 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9134 __isl_keep isl_basic_set
*bset2
, int pos
)
9136 isl_basic_map
*bmap1
;
9137 isl_basic_map
*bmap2
;
9139 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9140 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9141 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9142 isl_dim_out
, 0, pos
);
9143 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9144 isl_dim_out
, 0, pos
);
9145 return isl_basic_map_range_product(bmap1
, bmap2
);
9148 /* Given two basic sets bset1 and bset2, compute the maximal difference
9149 * between the values of dimension pos in bset1 and those in bset2
9150 * for any common value of the parameters and dimensions preceding pos.
9152 static enum isl_lp_result
basic_set_maximal_difference_at(
9153 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9154 int pos
, isl_int
*opt
)
9156 isl_basic_map
*bmap1
;
9157 struct isl_ctx
*ctx
;
9158 struct isl_vec
*obj
;
9162 enum isl_lp_result res
;
9164 if (!bset1
|| !bset2
)
9165 return isl_lp_error
;
9167 nparam
= isl_basic_set_n_param(bset1
);
9168 dim1
= isl_basic_set_n_dim(bset1
);
9170 bmap1
= join_initial(bset1
, bset2
, pos
);
9172 return isl_lp_error
;
9174 total
= isl_basic_map_total_dim(bmap1
);
9176 obj
= isl_vec_alloc(ctx
, 1 + total
);
9179 isl_seq_clr(obj
->block
.data
, 1 + total
);
9180 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9181 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9182 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9184 isl_basic_map_free(bmap1
);
9188 isl_basic_map_free(bmap1
);
9189 return isl_lp_error
;
9192 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9193 * for any common value of the parameters and dimensions preceding pos
9194 * in both basic sets, the values of dimension pos in bset1 are
9195 * smaller or larger than those in bset2.
9198 * 1 if bset1 follows bset2
9199 * -1 if bset1 precedes bset2
9200 * 0 if bset1 and bset2 are incomparable
9201 * -2 if some error occurred.
9203 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
9204 struct isl_basic_set
*bset2
, int pos
)
9207 enum isl_lp_result res
;
9212 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9214 if (res
== isl_lp_empty
)
9216 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9217 res
== isl_lp_unbounded
)
9219 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9228 /* Given two basic sets bset1 and bset2, check whether
9229 * for any common value of the parameters and dimensions preceding pos
9230 * there is a value of dimension pos in bset1 that is larger
9231 * than a value of the same dimension in bset2.
9234 * 1 if there exists such a pair
9235 * 0 if there is no such pair, but there is a pair of equal values
9237 * -2 if some error occurred.
9239 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9240 __isl_keep isl_basic_set
*bset2
, int pos
)
9243 isl_basic_map
*bmap
;
9246 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9247 bmap
= join_initial(bset1
, bset2
, pos
);
9248 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9249 isl_dim_out
, dim1
- pos
);
9250 empty
= isl_basic_map_is_empty(bmap
);
9254 isl_basic_map_free(bmap
);
9257 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9258 isl_dim_out
, dim1
- pos
);
9259 empty
= isl_basic_map_is_empty(bmap
);
9262 isl_basic_map_free(bmap
);
9267 isl_basic_map_free(bmap
);
9271 /* Given two sets set1 and set2, check whether
9272 * for any common value of the parameters and dimensions preceding pos
9273 * there is a value of dimension pos in set1 that is larger
9274 * than a value of the same dimension in set2.
9277 * 1 if there exists such a pair
9278 * 0 if there is no such pair, but there is a pair of equal values
9280 * -2 if some error occurred.
9282 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9283 __isl_keep isl_set
*set2
, int pos
)
9291 for (i
= 0; i
< set1
->n
; ++i
)
9292 for (j
= 0; j
< set2
->n
; ++j
) {
9294 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9295 if (f
== 1 || f
== -2)
9304 static isl_bool
isl_basic_map_plain_has_fixed_var(
9305 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9312 return isl_bool_error
;
9313 total
= isl_basic_map_total_dim(bmap
);
9314 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9315 for (; d
+1 > pos
; --d
)
9316 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9320 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9321 return isl_bool_false
;
9322 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9323 return isl_bool_false
;
9324 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9325 return isl_bool_false
;
9327 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9328 return isl_bool_true
;
9330 return isl_bool_false
;
9333 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9334 unsigned pos
, isl_int
*val
)
9342 return isl_bool_error
;
9344 return isl_bool_false
;
9346 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9349 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9350 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9351 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9352 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9353 fixed
= isl_bool_false
;
9356 isl_int_set(*val
, v
);
9362 static isl_bool
isl_basic_set_plain_has_fixed_var(
9363 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9365 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9369 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9370 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9372 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9373 return isl_bool_error
;
9374 return isl_basic_map_plain_has_fixed_var(bmap
,
9375 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9378 /* If "bmap" obviously lies on a hyperplane where the given dimension
9379 * has a fixed value, then return that value.
9380 * Otherwise return NaN.
9382 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9383 __isl_keep isl_basic_map
*bmap
,
9384 enum isl_dim_type type
, unsigned pos
)
9392 ctx
= isl_basic_map_get_ctx(bmap
);
9393 v
= isl_val_alloc(ctx
);
9396 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9398 return isl_val_free(v
);
9400 isl_int_set_si(v
->d
, 1);
9404 return isl_val_nan(ctx
);
9407 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9408 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9410 if (pos
>= isl_map_dim(map
, type
))
9411 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9412 "position out of bounds", return isl_bool_error
);
9413 return isl_map_plain_has_fixed_var(map
,
9414 map_offset(map
, type
) - 1 + pos
, val
);
9417 /* If "map" obviously lies on a hyperplane where the given dimension
9418 * has a fixed value, then return that value.
9419 * Otherwise return NaN.
9421 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9422 enum isl_dim_type type
, unsigned pos
)
9430 ctx
= isl_map_get_ctx(map
);
9431 v
= isl_val_alloc(ctx
);
9434 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9436 return isl_val_free(v
);
9438 isl_int_set_si(v
->d
, 1);
9442 return isl_val_nan(ctx
);
9445 /* If "set" obviously lies on a hyperplane where the given dimension
9446 * has a fixed value, then return that value.
9447 * Otherwise return NaN.
9449 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9450 enum isl_dim_type type
, unsigned pos
)
9452 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9455 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9456 * then return this fixed value in *val.
9458 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9459 unsigned dim
, isl_int
*val
)
9461 return isl_basic_set_plain_has_fixed_var(bset
,
9462 isl_basic_set_n_param(bset
) + dim
, val
);
9465 /* Return -1 if the constraint "c1" should be sorted before "c2"
9466 * and 1 if it should be sorted after "c2".
9467 * Return 0 if the two constraints are the same (up to the constant term).
9469 * In particular, if a constraint involves later variables than another
9470 * then it is sorted after this other constraint.
9471 * uset_gist depends on constraints without existentially quantified
9472 * variables sorting first.
9474 * For constraints that have the same latest variable, those
9475 * with the same coefficient for this latest variable (first in absolute value
9476 * and then in actual value) are grouped together.
9477 * This is useful for detecting pairs of constraints that can
9478 * be chained in their printed representation.
9480 * Finally, within a group, constraints are sorted according to
9481 * their coefficients (excluding the constant term).
9483 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9485 isl_int
**c1
= (isl_int
**) p1
;
9486 isl_int
**c2
= (isl_int
**) p2
;
9488 unsigned size
= *(unsigned *) arg
;
9491 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9492 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9497 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9500 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9504 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9507 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9508 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9509 * and 0 if the two constraints are the same (up to the constant term).
9511 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9512 isl_int
*c1
, isl_int
*c2
)
9518 total
= isl_basic_map_total_dim(bmap
);
9519 return sort_constraint_cmp(&c1
, &c2
, &total
);
9522 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9523 __isl_take isl_basic_map
*bmap
)
9529 if (bmap
->n_ineq
== 0)
9531 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9533 total
= isl_basic_map_total_dim(bmap
);
9534 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9535 &sort_constraint_cmp
, &total
) < 0)
9536 return isl_basic_map_free(bmap
);
9540 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9541 __isl_take isl_basic_set
*bset
)
9543 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9544 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9547 __isl_give isl_basic_map
*isl_basic_map_normalize(
9548 __isl_take isl_basic_map
*bmap
)
9552 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9554 bmap
= isl_basic_map_remove_redundancies(bmap
);
9555 bmap
= isl_basic_map_sort_constraints(bmap
);
9557 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9560 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9561 __isl_keep isl_basic_map
*bmap2
)
9565 isl_space
*space1
, *space2
;
9567 if (!bmap1
|| !bmap2
)
9572 space1
= isl_basic_map_peek_space(bmap1
);
9573 space2
= isl_basic_map_peek_space(bmap2
);
9574 cmp
= isl_space_cmp(space1
, space2
);
9577 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9578 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9579 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9580 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9581 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9583 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9585 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9587 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9588 return bmap1
->n_eq
- bmap2
->n_eq
;
9589 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9590 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9591 if (bmap1
->n_div
!= bmap2
->n_div
)
9592 return bmap1
->n_div
- bmap2
->n_div
;
9593 total
= isl_basic_map_total_dim(bmap1
);
9594 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9595 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9599 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9600 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9604 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9605 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9612 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9613 __isl_keep isl_basic_set
*bset2
)
9615 return isl_basic_map_plain_cmp(bset1
, bset2
);
9618 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9624 if (set1
->n
!= set2
->n
)
9625 return set1
->n
- set2
->n
;
9627 for (i
= 0; i
< set1
->n
; ++i
) {
9628 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9636 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9637 __isl_keep isl_basic_map
*bmap2
)
9639 if (!bmap1
|| !bmap2
)
9640 return isl_bool_error
;
9641 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9644 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9645 __isl_keep isl_basic_set
*bset2
)
9647 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9648 bset_to_bmap(bset2
));
9651 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9653 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9654 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9656 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9659 /* Sort the basic maps of "map" and remove duplicate basic maps.
9661 * While removing basic maps, we make sure that the basic maps remain
9662 * sorted because isl_map_normalize expects the basic maps of the result
9665 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9669 map
= isl_map_remove_empty_parts(map
);
9672 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9673 for (i
= map
->n
- 1; i
>= 1; --i
) {
9674 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9676 isl_basic_map_free(map
->p
[i
-1]);
9677 for (j
= i
; j
< map
->n
; ++j
)
9678 map
->p
[j
- 1] = map
->p
[j
];
9685 /* Remove obvious duplicates among the basic maps of "map".
9687 * Unlike isl_map_normalize, this function does not remove redundant
9688 * constraints and only removes duplicates that have exactly the same
9689 * constraints in the input. It does sort the constraints and
9690 * the basic maps to ease the detection of duplicates.
9692 * If "map" has already been normalized or if the basic maps are
9693 * disjoint, then there can be no duplicates.
9695 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9698 isl_basic_map
*bmap
;
9704 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9706 for (i
= 0; i
< map
->n
; ++i
) {
9707 bmap
= isl_basic_map_copy(map
->p
[i
]);
9708 bmap
= isl_basic_map_sort_constraints(bmap
);
9710 return isl_map_free(map
);
9711 isl_basic_map_free(map
->p
[i
]);
9715 map
= sort_and_remove_duplicates(map
);
9719 /* We normalize in place, but if anything goes wrong we need
9720 * to return NULL, so we need to make sure we don't change the
9721 * meaning of any possible other copies of map.
9723 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9726 struct isl_basic_map
*bmap
;
9730 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9732 for (i
= 0; i
< map
->n
; ++i
) {
9733 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9736 isl_basic_map_free(map
->p
[i
]);
9740 map
= sort_and_remove_duplicates(map
);
9742 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9749 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9751 return set_from_map(isl_map_normalize(set_to_map(set
)));
9754 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9755 __isl_keep isl_map
*map2
)
9761 return isl_bool_error
;
9764 return isl_bool_true
;
9765 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9766 return isl_bool_false
;
9768 map1
= isl_map_copy(map1
);
9769 map2
= isl_map_copy(map2
);
9770 map1
= isl_map_normalize(map1
);
9771 map2
= isl_map_normalize(map2
);
9774 equal
= map1
->n
== map2
->n
;
9775 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9776 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9786 return isl_bool_error
;
9789 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9790 __isl_keep isl_set
*set2
)
9792 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9795 /* Return the basic maps in "map" as a list.
9797 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9798 __isl_keep isl_map
*map
)
9802 isl_basic_map_list
*list
;
9806 ctx
= isl_map_get_ctx(map
);
9807 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9809 for (i
= 0; i
< map
->n
; ++i
) {
9810 isl_basic_map
*bmap
;
9812 bmap
= isl_basic_map_copy(map
->p
[i
]);
9813 list
= isl_basic_map_list_add(list
, bmap
);
9819 /* Return the intersection of the elements in the non-empty list "list".
9820 * All elements are assumed to live in the same space.
9822 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9823 __isl_take isl_basic_map_list
*list
)
9826 isl_basic_map
*bmap
;
9830 n
= isl_basic_map_list_n_basic_map(list
);
9832 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9833 "expecting non-empty list", goto error
);
9835 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9836 for (i
= 1; i
< n
; ++i
) {
9837 isl_basic_map
*bmap_i
;
9839 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9840 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9843 isl_basic_map_list_free(list
);
9846 isl_basic_map_list_free(list
);
9850 /* Return the intersection of the elements in the non-empty list "list".
9851 * All elements are assumed to live in the same space.
9853 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9854 __isl_take isl_basic_set_list
*list
)
9856 return isl_basic_map_list_intersect(list
);
9859 /* Return the union of the elements of "list".
9860 * The list is required to have at least one element.
9862 __isl_give isl_set
*isl_basic_set_list_union(
9863 __isl_take isl_basic_set_list
*list
)
9867 isl_basic_set
*bset
;
9872 n
= isl_basic_set_list_n_basic_set(list
);
9874 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9875 "expecting non-empty list", goto error
);
9877 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9878 space
= isl_basic_set_get_space(bset
);
9879 isl_basic_set_free(bset
);
9881 set
= isl_set_alloc_space(space
, n
, 0);
9882 for (i
= 0; i
< n
; ++i
) {
9883 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9884 set
= isl_set_add_basic_set(set
, bset
);
9887 isl_basic_set_list_free(list
);
9890 isl_basic_set_list_free(list
);
9894 /* Return the union of the elements in the non-empty list "list".
9895 * All elements are assumed to live in the same space.
9897 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9904 n
= isl_set_list_n_set(list
);
9906 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9907 "expecting non-empty list", goto error
);
9909 set
= isl_set_list_get_set(list
, 0);
9910 for (i
= 1; i
< n
; ++i
) {
9913 set_i
= isl_set_list_get_set(list
, i
);
9914 set
= isl_set_union(set
, set_i
);
9917 isl_set_list_free(list
);
9920 isl_set_list_free(list
);
9924 __isl_give isl_basic_map
*isl_basic_map_product(
9925 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9927 isl_space
*dim_result
= NULL
;
9928 struct isl_basic_map
*bmap
;
9929 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9930 struct isl_dim_map
*dim_map1
, *dim_map2
;
9932 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9934 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9935 isl_space_copy(bmap2
->dim
));
9937 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9938 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9939 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9940 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9941 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9943 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9944 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9945 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9946 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9947 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9948 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9949 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9950 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9951 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9952 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9953 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9955 bmap
= isl_basic_map_alloc_space(dim_result
,
9956 bmap1
->n_div
+ bmap2
->n_div
,
9957 bmap1
->n_eq
+ bmap2
->n_eq
,
9958 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9959 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9960 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9961 bmap
= isl_basic_map_simplify(bmap
);
9962 return isl_basic_map_finalize(bmap
);
9964 isl_basic_map_free(bmap1
);
9965 isl_basic_map_free(bmap2
);
9969 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9970 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9972 isl_basic_map
*prod
;
9974 prod
= isl_basic_map_product(bmap1
, bmap2
);
9975 prod
= isl_basic_map_flatten(prod
);
9979 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9980 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9982 return isl_basic_map_flat_range_product(bset1
, bset2
);
9985 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9986 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9988 isl_space
*space_result
= NULL
;
9989 isl_basic_map
*bmap
;
9990 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9991 struct isl_dim_map
*dim_map1
, *dim_map2
;
9993 if (!bmap1
|| !bmap2
)
9996 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9997 isl_space_copy(bmap2
->dim
));
9999 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10000 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10001 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10002 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10004 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10005 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10006 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10007 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10008 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10009 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10010 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10011 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10012 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10013 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10014 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10016 bmap
= isl_basic_map_alloc_space(space_result
,
10017 bmap1
->n_div
+ bmap2
->n_div
,
10018 bmap1
->n_eq
+ bmap2
->n_eq
,
10019 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10020 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10021 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10022 bmap
= isl_basic_map_simplify(bmap
);
10023 return isl_basic_map_finalize(bmap
);
10025 isl_basic_map_free(bmap1
);
10026 isl_basic_map_free(bmap2
);
10030 __isl_give isl_basic_map
*isl_basic_map_range_product(
10031 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10034 isl_space
*dim_result
= NULL
;
10035 isl_basic_map
*bmap
;
10036 unsigned in
, out1
, out2
, nparam
, total
, pos
;
10037 struct isl_dim_map
*dim_map1
, *dim_map2
;
10039 rational
= isl_basic_map_is_rational(bmap1
);
10040 if (rational
>= 0 && rational
)
10041 rational
= isl_basic_map_is_rational(bmap2
);
10042 if (!bmap1
|| !bmap2
|| rational
< 0)
10045 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10048 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10049 isl_space_copy(bmap2
->dim
));
10051 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10052 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10053 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10054 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10056 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10057 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10058 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10059 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10060 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10061 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10062 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10063 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10064 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10065 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10066 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10068 bmap
= isl_basic_map_alloc_space(dim_result
,
10069 bmap1
->n_div
+ bmap2
->n_div
,
10070 bmap1
->n_eq
+ bmap2
->n_eq
,
10071 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10072 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10073 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10075 bmap
= isl_basic_map_set_rational(bmap
);
10076 bmap
= isl_basic_map_simplify(bmap
);
10077 return isl_basic_map_finalize(bmap
);
10079 isl_basic_map_free(bmap1
);
10080 isl_basic_map_free(bmap2
);
10084 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10085 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10087 isl_basic_map
*prod
;
10089 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10090 prod
= isl_basic_map_flatten_range(prod
);
10094 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10095 * and collect the results.
10096 * The result live in the space obtained by calling "space_product"
10097 * on the spaces of "map1" and "map2".
10098 * If "remove_duplicates" is set then the result may contain duplicates
10099 * (even if the inputs do not) and so we try and remove the obvious
10102 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10103 __isl_take isl_map
*map2
,
10104 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10105 __isl_take isl_space
*right
),
10106 __isl_give isl_basic_map
*(*basic_map_product
)(
10107 __isl_take isl_basic_map
*left
,
10108 __isl_take isl_basic_map
*right
),
10109 int remove_duplicates
)
10111 unsigned flags
= 0;
10112 struct isl_map
*result
;
10116 m
= isl_map_has_equal_params(map1
, map2
);
10120 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10121 "parameters don't match", goto error
);
10123 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10124 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10125 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10127 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10128 isl_space_copy(map2
->dim
)),
10129 map1
->n
* map2
->n
, flags
);
10132 for (i
= 0; i
< map1
->n
; ++i
)
10133 for (j
= 0; j
< map2
->n
; ++j
) {
10134 struct isl_basic_map
*part
;
10135 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10136 isl_basic_map_copy(map2
->p
[j
]));
10137 if (isl_basic_map_is_empty(part
))
10138 isl_basic_map_free(part
);
10140 result
= isl_map_add_basic_map(result
, part
);
10144 if (remove_duplicates
)
10145 result
= isl_map_remove_obvious_duplicates(result
);
10146 isl_map_free(map1
);
10147 isl_map_free(map2
);
10150 isl_map_free(map1
);
10151 isl_map_free(map2
);
10155 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10157 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10158 __isl_take isl_map
*map2
)
10160 return map_product(map1
, map2
, &isl_space_product
,
10161 &isl_basic_map_product
, 0);
10164 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10165 __isl_take isl_map
*map2
)
10167 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10170 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10172 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10173 __isl_take isl_map
*map2
)
10177 prod
= isl_map_product(map1
, map2
);
10178 prod
= isl_map_flatten(prod
);
10182 /* Given two set A and B, construct its Cartesian product A x B.
10184 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10186 return isl_map_range_product(set1
, set2
);
10189 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10190 __isl_take isl_set
*set2
)
10192 return isl_map_flat_range_product(set1
, set2
);
10195 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10197 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10198 __isl_take isl_map
*map2
)
10200 return map_product(map1
, map2
, &isl_space_domain_product
,
10201 &isl_basic_map_domain_product
, 1);
10204 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10206 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10207 __isl_take isl_map
*map2
)
10209 return map_product(map1
, map2
, &isl_space_range_product
,
10210 &isl_basic_map_range_product
, 1);
10213 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10214 __isl_take isl_map
*map2
)
10216 return isl_map_align_params_map_map_and(map1
, map2
,
10217 &map_domain_product_aligned
);
10220 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10221 __isl_take isl_map
*map2
)
10223 return isl_map_align_params_map_map_and(map1
, map2
,
10224 &map_range_product_aligned
);
10227 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10229 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10232 int total1
, keep1
, total2
, keep2
;
10236 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10237 !isl_space_range_is_wrapping(map
->dim
))
10238 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10239 "not a product", return isl_map_free(map
));
10241 space
= isl_map_get_space(map
);
10242 total1
= isl_space_dim(space
, isl_dim_in
);
10243 total2
= isl_space_dim(space
, isl_dim_out
);
10244 space
= isl_space_factor_domain(space
);
10245 keep1
= isl_space_dim(space
, isl_dim_in
);
10246 keep2
= isl_space_dim(space
, isl_dim_out
);
10247 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10248 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10249 map
= isl_map_reset_space(map
, space
);
10254 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10256 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10259 int total1
, keep1
, total2
, keep2
;
10263 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10264 !isl_space_range_is_wrapping(map
->dim
))
10265 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10266 "not a product", return isl_map_free(map
));
10268 space
= isl_map_get_space(map
);
10269 total1
= isl_space_dim(space
, isl_dim_in
);
10270 total2
= isl_space_dim(space
, isl_dim_out
);
10271 space
= isl_space_factor_range(space
);
10272 keep1
= isl_space_dim(space
, isl_dim_in
);
10273 keep2
= isl_space_dim(space
, isl_dim_out
);
10274 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10275 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10276 map
= isl_map_reset_space(map
, space
);
10281 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10283 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10290 if (!isl_space_domain_is_wrapping(map
->dim
))
10291 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10292 "domain is not a product", return isl_map_free(map
));
10294 space
= isl_map_get_space(map
);
10295 total
= isl_space_dim(space
, isl_dim_in
);
10296 space
= isl_space_domain_factor_domain(space
);
10297 keep
= isl_space_dim(space
, isl_dim_in
);
10298 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10299 map
= isl_map_reset_space(map
, space
);
10304 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10306 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10313 if (!isl_space_domain_is_wrapping(map
->dim
))
10314 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10315 "domain is not a product", return isl_map_free(map
));
10317 space
= isl_map_get_space(map
);
10318 total
= isl_space_dim(space
, isl_dim_in
);
10319 space
= isl_space_domain_factor_range(space
);
10320 keep
= isl_space_dim(space
, isl_dim_in
);
10321 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10322 map
= isl_map_reset_space(map
, space
);
10327 /* Given a map A -> [B -> C], extract the map A -> B.
10329 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10336 if (!isl_space_range_is_wrapping(map
->dim
))
10337 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10338 "range is not a product", return isl_map_free(map
));
10340 space
= isl_map_get_space(map
);
10341 total
= isl_space_dim(space
, isl_dim_out
);
10342 space
= isl_space_range_factor_domain(space
);
10343 keep
= isl_space_dim(space
, isl_dim_out
);
10344 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10345 map
= isl_map_reset_space(map
, space
);
10350 /* Given a map A -> [B -> C], extract the map A -> C.
10352 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10359 if (!isl_space_range_is_wrapping(map
->dim
))
10360 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10361 "range is not a product", return isl_map_free(map
));
10363 space
= isl_map_get_space(map
);
10364 total
= isl_space_dim(space
, isl_dim_out
);
10365 space
= isl_space_range_factor_range(space
);
10366 keep
= isl_space_dim(space
, isl_dim_out
);
10367 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10368 map
= isl_map_reset_space(map
, space
);
10373 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10375 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10376 __isl_take isl_map
*map2
)
10380 prod
= isl_map_domain_product(map1
, map2
);
10381 prod
= isl_map_flatten_domain(prod
);
10385 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10387 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10388 __isl_take isl_map
*map2
)
10392 prod
= isl_map_range_product(map1
, map2
);
10393 prod
= isl_map_flatten_range(prod
);
10397 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10400 uint32_t hash
= isl_hash_init();
10405 bmap
= isl_basic_map_copy(bmap
);
10406 bmap
= isl_basic_map_normalize(bmap
);
10409 total
= isl_basic_map_total_dim(bmap
);
10410 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10411 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10413 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10414 isl_hash_hash(hash
, c_hash
);
10416 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10417 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10419 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10420 isl_hash_hash(hash
, c_hash
);
10422 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10423 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10425 if (isl_int_is_zero(bmap
->div
[i
][0]))
10427 isl_hash_byte(hash
, i
& 0xFF);
10428 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10429 isl_hash_hash(hash
, c_hash
);
10431 isl_basic_map_free(bmap
);
10435 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10437 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10440 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10447 map
= isl_map_copy(map
);
10448 map
= isl_map_normalize(map
);
10452 hash
= isl_hash_init();
10453 for (i
= 0; i
< map
->n
; ++i
) {
10454 uint32_t bmap_hash
;
10455 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10456 isl_hash_hash(hash
, bmap_hash
);
10464 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10466 return isl_map_get_hash(set_to_map(set
));
10469 /* Return the number of basic maps in the (current) representation of "map".
10471 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10473 return map
? map
->n
: 0;
10476 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10478 return set
? set
->n
: 0;
10481 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10482 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10487 return isl_stat_error
;
10489 for (i
= 0; i
< map
->n
; ++i
)
10490 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10491 return isl_stat_error
;
10493 return isl_stat_ok
;
10496 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10497 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10502 return isl_stat_error
;
10504 for (i
= 0; i
< set
->n
; ++i
)
10505 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10506 return isl_stat_error
;
10508 return isl_stat_ok
;
10511 /* Return a list of basic sets, the union of which is equal to "set".
10513 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10514 __isl_keep isl_set
*set
)
10517 isl_basic_set_list
*list
;
10522 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10523 for (i
= 0; i
< set
->n
; ++i
) {
10524 isl_basic_set
*bset
;
10526 bset
= isl_basic_set_copy(set
->p
[i
]);
10527 list
= isl_basic_set_list_add(list
, bset
);
10533 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10540 bset
= isl_basic_set_cow(bset
);
10544 dim
= isl_basic_set_get_space(bset
);
10545 dim
= isl_space_lift(dim
, bset
->n_div
);
10548 isl_space_free(bset
->dim
);
10550 bset
->extra
-= bset
->n_div
;
10553 bset
= isl_basic_set_finalize(bset
);
10557 isl_basic_set_free(bset
);
10561 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10567 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10572 set
= isl_set_cow(set
);
10576 n_div
= set
->p
[0]->n_div
;
10577 dim
= isl_set_get_space(set
);
10578 dim
= isl_space_lift(dim
, n_div
);
10581 isl_space_free(set
->dim
);
10584 for (i
= 0; i
< set
->n
; ++i
) {
10585 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10596 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10604 dim
= isl_basic_set_total_dim(bset
);
10605 size
+= bset
->n_eq
* (1 + dim
);
10606 size
+= bset
->n_ineq
* (1 + dim
);
10607 size
+= bset
->n_div
* (2 + dim
);
10612 int isl_set_size(__isl_keep isl_set
*set
)
10620 for (i
= 0; i
< set
->n
; ++i
)
10621 size
+= isl_basic_set_size(set
->p
[i
]);
10626 /* Check if there is any lower bound (if lower == 0) and/or upper
10627 * bound (if upper == 0) on the specified dim.
10629 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10630 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10634 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10635 return isl_bool_error
;
10637 pos
+= isl_basic_map_offset(bmap
, type
);
10639 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10640 if (isl_int_is_zero(bmap
->div
[i
][0]))
10642 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10643 return isl_bool_true
;
10646 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10647 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10648 return isl_bool_true
;
10650 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10651 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10658 return lower
&& upper
;
10661 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10662 enum isl_dim_type type
, unsigned pos
)
10664 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10667 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10668 enum isl_dim_type type
, unsigned pos
)
10670 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10673 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10674 enum isl_dim_type type
, unsigned pos
)
10676 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10679 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10680 enum isl_dim_type type
, unsigned pos
)
10685 return isl_bool_error
;
10687 for (i
= 0; i
< map
->n
; ++i
) {
10689 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10690 if (bounded
< 0 || !bounded
)
10694 return isl_bool_true
;
10697 /* Return true if the specified dim is involved in both an upper bound
10698 * and a lower bound.
10700 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10701 enum isl_dim_type type
, unsigned pos
)
10703 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10706 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10708 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10709 enum isl_dim_type type
, unsigned pos
,
10710 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10711 enum isl_dim_type type
, unsigned pos
))
10716 return isl_bool_error
;
10718 for (i
= 0; i
< map
->n
; ++i
) {
10720 bounded
= fn(map
->p
[i
], type
, pos
);
10721 if (bounded
< 0 || bounded
)
10725 return isl_bool_false
;
10728 /* Return 1 if the specified dim is involved in any lower bound.
10730 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10731 enum isl_dim_type type
, unsigned pos
)
10733 return has_any_bound(set
, type
, pos
,
10734 &isl_basic_map_dim_has_lower_bound
);
10737 /* Return 1 if the specified dim is involved in any upper bound.
10739 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10740 enum isl_dim_type type
, unsigned pos
)
10742 return has_any_bound(set
, type
, pos
,
10743 &isl_basic_map_dim_has_upper_bound
);
10746 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10748 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10749 enum isl_dim_type type
, unsigned pos
,
10750 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10751 enum isl_dim_type type
, unsigned pos
))
10756 return isl_bool_error
;
10758 for (i
= 0; i
< map
->n
; ++i
) {
10760 bounded
= fn(map
->p
[i
], type
, pos
);
10761 if (bounded
< 0 || !bounded
)
10765 return isl_bool_true
;
10768 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10770 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10771 enum isl_dim_type type
, unsigned pos
)
10773 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10776 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10778 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10779 enum isl_dim_type type
, unsigned pos
)
10781 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10784 /* For each of the "n" variables starting at "first", determine
10785 * the sign of the variable and put the results in the first "n"
10786 * elements of the array "signs".
10788 * 1 means that the variable is non-negative
10789 * -1 means that the variable is non-positive
10790 * 0 means the variable attains both positive and negative values.
10792 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10793 unsigned first
, unsigned n
, int *signs
)
10795 isl_vec
*bound
= NULL
;
10796 struct isl_tab
*tab
= NULL
;
10797 struct isl_tab_undo
*snap
;
10800 if (!bset
|| !signs
)
10801 return isl_stat_error
;
10803 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10804 tab
= isl_tab_from_basic_set(bset
, 0);
10805 if (!bound
|| !tab
)
10808 isl_seq_clr(bound
->el
, bound
->size
);
10809 isl_int_set_si(bound
->el
[0], -1);
10811 snap
= isl_tab_snap(tab
);
10812 for (i
= 0; i
< n
; ++i
) {
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)
10828 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10829 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10831 empty
= tab
->empty
;
10832 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10833 if (isl_tab_rollback(tab
, snap
) < 0)
10836 signs
[i
] = empty
? -1 : 0;
10840 isl_vec_free(bound
);
10841 return isl_stat_ok
;
10844 isl_vec_free(bound
);
10845 return isl_stat_error
;
10848 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10849 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10851 if (!bset
|| !signs
)
10852 return isl_stat_error
;
10853 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10854 return isl_stat_error
);
10856 first
+= pos(bset
->dim
, type
) - 1;
10857 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10860 /* Is it possible for the integer division "div" to depend (possibly
10861 * indirectly) on any output dimensions?
10863 * If the div is undefined, then we conservatively assume that it
10864 * may depend on them.
10865 * Otherwise, we check if it actually depends on them or on any integer
10866 * divisions that may depend on them.
10868 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10871 unsigned n_out
, o_out
;
10872 unsigned n_div
, o_div
;
10874 if (isl_int_is_zero(bmap
->div
[div
][0]))
10875 return isl_bool_true
;
10877 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10878 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10880 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10881 return isl_bool_true
;
10883 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10884 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10886 for (i
= 0; i
< n_div
; ++i
) {
10887 isl_bool may_involve
;
10889 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10891 may_involve
= div_may_involve_output(bmap
, i
);
10892 if (may_involve
< 0 || may_involve
)
10893 return may_involve
;
10896 return isl_bool_false
;
10899 /* Return the first integer division of "bmap" in the range
10900 * [first, first + n[ that may depend on any output dimensions and
10901 * that has a non-zero coefficient in "c" (where the first coefficient
10902 * in "c" corresponds to integer division "first").
10904 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10905 isl_int
*c
, int first
, int n
)
10912 for (k
= first
; k
< first
+ n
; ++k
) {
10913 isl_bool may_involve
;
10915 if (isl_int_is_zero(c
[k
]))
10917 may_involve
= div_may_involve_output(bmap
, k
);
10918 if (may_involve
< 0)
10927 /* Look for a pair of inequality constraints in "bmap" of the form
10929 * -l + i >= 0 or i >= l
10931 * n + l - i >= 0 or i <= l + n
10933 * with n < "m" and i the output dimension at position "pos".
10934 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10935 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10936 * and earlier output dimensions, as well as integer divisions that do
10937 * not involve any of the output dimensions.
10939 * Return the index of the first inequality constraint or bmap->n_ineq
10940 * if no such pair can be found.
10942 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10943 int pos
, isl_int m
)
10948 unsigned n_div
, o_div
;
10949 unsigned n_out
, o_out
;
10955 ctx
= isl_basic_map_get_ctx(bmap
);
10956 total
= isl_basic_map_total_dim(bmap
);
10957 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10958 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10959 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10960 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10961 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10962 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10964 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10965 n_out
- (pos
+ 1)) != -1)
10967 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10970 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10971 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10974 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10975 bmap
->ineq
[j
] + 1, total
))
10979 if (j
>= bmap
->n_ineq
)
10981 isl_int_add(bmap
->ineq
[i
][0],
10982 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10983 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10984 isl_int_sub(bmap
->ineq
[i
][0],
10985 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10988 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10994 return bmap
->n_ineq
;
10997 /* Return the index of the equality of "bmap" that defines
10998 * the output dimension "pos" in terms of earlier dimensions.
10999 * The equality may also involve integer divisions, as long
11000 * as those integer divisions are defined in terms of
11001 * parameters or input dimensions.
11002 * In this case, *div is set to the number of integer divisions and
11003 * *ineq is set to the number of inequality constraints (provided
11004 * div and ineq are not NULL).
11006 * The equality may also involve a single integer division involving
11007 * the output dimensions (typically only output dimension "pos") as
11008 * long as the coefficient of output dimension "pos" is 1 or -1 and
11009 * there is a pair of constraints i >= l and i <= l + n, with i referring
11010 * to output dimension "pos", l an expression involving only earlier
11011 * dimensions and n smaller than the coefficient of the integer division
11012 * in the equality. In this case, the output dimension can be defined
11013 * in terms of a modulo expression that does not involve the integer division.
11014 * *div is then set to this single integer division and
11015 * *ineq is set to the index of constraint i >= l.
11017 * Return bmap->n_eq if there is no such equality.
11018 * Return -1 on error.
11020 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11021 int pos
, int *div
, int *ineq
)
11024 unsigned n_out
, o_out
;
11025 unsigned n_div
, o_div
;
11030 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11031 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11032 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11033 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11036 *ineq
= bmap
->n_ineq
;
11039 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11040 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11042 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11043 n_out
- (pos
+ 1)) != -1)
11045 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11049 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11050 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11052 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11053 k
+ 1, n_div
- (k
+1)) < n_div
)
11055 l
= find_modulo_constraint_pair(bmap
, pos
,
11056 bmap
->eq
[j
][o_div
+ k
]);
11059 if (l
>= bmap
->n_ineq
)
11071 /* Check if the given basic map is obviously single-valued.
11072 * In particular, for each output dimension, check that there is
11073 * an equality that defines the output dimension in terms of
11074 * earlier dimensions.
11076 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11082 return isl_bool_error
;
11084 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11086 for (i
= 0; i
< n_out
; ++i
) {
11089 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11092 return isl_bool_error
;
11093 if (eq
>= bmap
->n_eq
)
11094 return isl_bool_false
;
11097 return isl_bool_true
;
11100 /* Check if the given basic map is single-valued.
11101 * We simply compute
11105 * and check if the result is a subset of the identity mapping.
11107 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11110 isl_basic_map
*test
;
11114 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11118 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11119 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11121 space
= isl_basic_map_get_space(bmap
);
11122 space
= isl_space_map_from_set(isl_space_range(space
));
11123 id
= isl_basic_map_identity(space
);
11125 sv
= isl_basic_map_is_subset(test
, id
);
11127 isl_basic_map_free(test
);
11128 isl_basic_map_free(id
);
11133 /* Check if the given map is obviously single-valued.
11135 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11138 return isl_bool_error
;
11140 return isl_bool_true
;
11142 return isl_bool_false
;
11144 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11147 /* Check if the given map is single-valued.
11148 * We simply compute
11152 * and check if the result is a subset of the identity mapping.
11154 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11161 sv
= isl_map_plain_is_single_valued(map
);
11165 test
= isl_map_reverse(isl_map_copy(map
));
11166 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11168 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11169 id
= isl_map_identity(dim
);
11171 sv
= isl_map_is_subset(test
, id
);
11173 isl_map_free(test
);
11179 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11183 map
= isl_map_copy(map
);
11184 map
= isl_map_reverse(map
);
11185 in
= isl_map_is_single_valued(map
);
11191 /* Check if the given map is obviously injective.
11193 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11197 map
= isl_map_copy(map
);
11198 map
= isl_map_reverse(map
);
11199 in
= isl_map_plain_is_single_valued(map
);
11205 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11209 sv
= isl_map_is_single_valued(map
);
11213 return isl_map_is_injective(map
);
11216 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11218 return isl_map_is_single_valued(set_to_map(set
));
11221 /* Does "map" only map elements to themselves?
11223 * If the domain and range spaces are different, then "map"
11224 * is considered not to be an identity relation, even if it is empty.
11225 * Otherwise, construct the maximal identity relation and
11226 * check whether "map" is a subset of this relation.
11228 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11232 isl_bool equal
, is_identity
;
11234 space
= isl_map_get_space(map
);
11235 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11236 isl_space_free(space
);
11237 if (equal
< 0 || !equal
)
11240 id
= isl_map_identity(isl_map_get_space(map
));
11241 is_identity
= isl_map_is_subset(map
, id
);
11244 return is_identity
;
11247 int isl_map_is_translation(__isl_keep isl_map
*map
)
11252 delta
= isl_map_deltas(isl_map_copy(map
));
11253 ok
= isl_set_is_singleton(delta
);
11254 isl_set_free(delta
);
11259 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11261 if (isl_seq_first_non_zero(p
, pos
) != -1)
11263 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11268 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11275 return isl_bool_error
;
11277 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11278 return isl_bool_false
;
11280 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11281 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11282 for (j
= 0; j
< nvar
; ++j
) {
11283 int lower
= 0, upper
= 0;
11284 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11285 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11287 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11288 return isl_bool_false
;
11291 if (i
< bset
->n_eq
)
11293 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11294 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11296 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11297 return isl_bool_false
;
11298 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11303 if (!lower
|| !upper
)
11304 return isl_bool_false
;
11307 return isl_bool_true
;
11310 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11313 return isl_bool_error
;
11315 return isl_bool_false
;
11317 return isl_basic_set_is_box(set
->p
[0]);
11320 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11323 return isl_bool_error
;
11325 return isl_space_is_wrapping(bset
->dim
);
11328 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11331 return isl_bool_error
;
11333 return isl_space_is_wrapping(set
->dim
);
11336 /* Modify the space of "map" through a call to "change".
11337 * If "can_change" is set (not NULL), then first call it to check
11338 * if the modification is allowed, printing the error message "cannot_change"
11341 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11342 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11343 const char *cannot_change
,
11344 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11352 ok
= can_change
? can_change(map
) : isl_bool_true
;
11354 return isl_map_free(map
);
11356 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11357 return isl_map_free(map
));
11359 space
= change(isl_map_get_space(map
));
11360 map
= isl_map_reset_space(map
, space
);
11365 /* Is the domain of "map" a wrapped relation?
11367 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11370 return isl_bool_error
;
11372 return isl_space_domain_is_wrapping(map
->dim
);
11375 /* Does "map" have a wrapped relation in both domain and range?
11377 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11379 return isl_space_is_product(isl_map_peek_space(map
));
11382 /* Is the range of "map" a wrapped relation?
11384 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11387 return isl_bool_error
;
11389 return isl_space_range_is_wrapping(map
->dim
);
11392 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11394 bmap
= isl_basic_map_cow(bmap
);
11398 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11402 bmap
= isl_basic_map_finalize(bmap
);
11404 return bset_from_bmap(bmap
);
11406 isl_basic_map_free(bmap
);
11410 /* Given a map A -> B, return the set (A -> B).
11412 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11414 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11417 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11419 bset
= isl_basic_set_cow(bset
);
11423 bset
->dim
= isl_space_unwrap(bset
->dim
);
11427 bset
= isl_basic_set_finalize(bset
);
11429 return bset_to_bmap(bset
);
11431 isl_basic_set_free(bset
);
11435 /* Given a set (A -> B), return the map A -> B.
11436 * Error out if "set" is not of the form (A -> B).
11438 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11440 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11441 "not a wrapping set", &isl_space_unwrap
);
11444 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11445 enum isl_dim_type type
)
11450 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11453 bmap
= isl_basic_map_cow(bmap
);
11457 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11461 bmap
= isl_basic_map_finalize(bmap
);
11465 isl_basic_map_free(bmap
);
11469 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11470 enum isl_dim_type type
)
11477 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11480 map
= isl_map_cow(map
);
11484 for (i
= 0; i
< map
->n
; ++i
) {
11485 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11489 map
->dim
= isl_space_reset(map
->dim
, type
);
11499 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11504 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11507 bmap
= isl_basic_map_cow(bmap
);
11511 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11515 bmap
= isl_basic_map_finalize(bmap
);
11519 isl_basic_map_free(bmap
);
11523 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11525 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11528 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11529 __isl_take isl_basic_map
*bmap
)
11534 if (!bmap
->dim
->nested
[0])
11537 bmap
= isl_basic_map_cow(bmap
);
11541 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11545 bmap
= isl_basic_map_finalize(bmap
);
11549 isl_basic_map_free(bmap
);
11553 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11554 __isl_take isl_basic_map
*bmap
)
11559 if (!bmap
->dim
->nested
[1])
11562 bmap
= isl_basic_map_cow(bmap
);
11566 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11570 bmap
= isl_basic_map_finalize(bmap
);
11574 isl_basic_map_free(bmap
);
11578 /* Remove any internal structure from the spaces of domain and range of "map".
11580 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11585 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11588 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11591 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11593 return set_from_map(isl_map_flatten(set_to_map(set
)));
11596 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11598 isl_space
*dim
, *flat_dim
;
11601 dim
= isl_set_get_space(set
);
11602 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11603 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11604 map
= isl_map_intersect_domain(map
, set
);
11609 /* Remove any internal structure from the space of the domain of "map".
11611 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11616 if (!map
->dim
->nested
[0])
11619 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11622 /* Remove any internal structure from the space of the range of "map".
11624 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11629 if (!map
->dim
->nested
[1])
11632 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11635 /* Reorder the dimensions of "bmap" according to the given dim_map
11636 * and set the dimension specification to "space" and
11637 * perform Gaussian elimination on the result.
11639 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11640 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11642 isl_basic_map
*res
;
11646 if (!bmap
|| !space
|| !dim_map
)
11649 flags
= bmap
->flags
;
11650 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11651 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11652 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11653 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11654 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11655 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11657 res
->flags
= flags
;
11658 res
= isl_basic_map_gauss(res
, NULL
);
11659 res
= isl_basic_map_finalize(res
);
11663 isl_basic_map_free(bmap
);
11664 isl_space_free(space
);
11668 /* Reorder the dimensions of "map" according to given reordering.
11670 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11671 __isl_take isl_reordering
*r
)
11674 struct isl_dim_map
*dim_map
;
11676 map
= isl_map_cow(map
);
11677 dim_map
= isl_dim_map_from_reordering(r
);
11678 if (!map
|| !r
|| !dim_map
)
11681 for (i
= 0; i
< map
->n
; ++i
) {
11682 struct isl_dim_map
*dim_map_i
;
11685 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11687 space
= isl_reordering_get_space(r
);
11688 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11694 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11696 isl_reordering_free(r
);
11702 isl_reordering_free(r
);
11706 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11707 __isl_take isl_reordering
*r
)
11709 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11712 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11713 __isl_take isl_space
*model
)
11718 if (!map
|| !model
)
11721 ctx
= isl_space_get_ctx(model
);
11722 if (!isl_space_has_named_params(model
))
11723 isl_die(ctx
, isl_error_invalid
,
11724 "model has unnamed parameters", goto error
);
11725 if (isl_map_check_named_params(map
) < 0)
11727 aligned
= isl_map_space_has_equal_params(map
, model
);
11731 isl_reordering
*exp
;
11733 model
= isl_space_drop_dims(model
, isl_dim_in
,
11734 0, isl_space_dim(model
, isl_dim_in
));
11735 model
= isl_space_drop_dims(model
, isl_dim_out
,
11736 0, isl_space_dim(model
, isl_dim_out
));
11737 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11738 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11739 map
= isl_map_realign(map
, exp
);
11742 isl_space_free(model
);
11745 isl_space_free(model
);
11750 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11751 __isl_take isl_space
*model
)
11753 return isl_map_align_params(set
, model
);
11756 /* Align the parameters of "bmap" to those of "model", introducing
11757 * additional parameters if needed.
11759 __isl_give isl_basic_map
*isl_basic_map_align_params(
11760 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11763 isl_bool equal_params
;
11765 if (!bmap
|| !model
)
11768 ctx
= isl_space_get_ctx(model
);
11769 if (!isl_space_has_named_params(model
))
11770 isl_die(ctx
, isl_error_invalid
,
11771 "model has unnamed parameters", goto error
);
11772 if (isl_basic_map_check_named_params(bmap
) < 0)
11774 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11775 if (equal_params
< 0)
11777 if (!equal_params
) {
11778 isl_reordering
*exp
;
11779 struct isl_dim_map
*dim_map
;
11781 model
= isl_space_drop_dims(model
, isl_dim_in
,
11782 0, isl_space_dim(model
, isl_dim_in
));
11783 model
= isl_space_drop_dims(model
, isl_dim_out
,
11784 0, isl_space_dim(model
, isl_dim_out
));
11785 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11786 exp
= isl_reordering_extend_space(exp
,
11787 isl_basic_map_get_space(bmap
));
11788 dim_map
= isl_dim_map_from_reordering(exp
);
11789 bmap
= isl_basic_map_realign(bmap
,
11790 isl_reordering_get_space(exp
),
11791 isl_dim_map_extend(dim_map
, bmap
));
11792 isl_reordering_free(exp
);
11796 isl_space_free(model
);
11799 isl_space_free(model
);
11800 isl_basic_map_free(bmap
);
11804 /* Do "bset" and "space" have the same parameters?
11806 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11807 __isl_keep isl_space
*space
)
11809 isl_space
*bset_space
;
11811 bset_space
= isl_basic_set_peek_space(bset
);
11812 return isl_space_has_equal_params(bset_space
, space
);
11815 /* Do "map" and "space" have the same parameters?
11817 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11818 __isl_keep isl_space
*space
)
11820 isl_space
*map_space
;
11822 map_space
= isl_map_peek_space(map
);
11823 return isl_space_has_equal_params(map_space
, space
);
11826 /* Do "set" and "space" have the same parameters?
11828 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11829 __isl_keep isl_space
*space
)
11831 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11834 /* Align the parameters of "bset" to those of "model", introducing
11835 * additional parameters if needed.
11837 __isl_give isl_basic_set
*isl_basic_set_align_params(
11838 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11840 return isl_basic_map_align_params(bset
, model
);
11843 /* Drop all parameters not referenced by "map".
11845 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11849 if (isl_map_check_named_params(map
) < 0)
11850 return isl_map_free(map
);
11852 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11855 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11857 return isl_map_free(map
);
11859 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11865 /* Drop all parameters not referenced by "set".
11867 __isl_give isl_set
*isl_set_drop_unused_params(
11868 __isl_take isl_set
*set
)
11870 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11873 /* Drop all parameters not referenced by "bmap".
11875 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11876 __isl_take isl_basic_map
*bmap
)
11880 if (isl_basic_map_check_named_params(bmap
) < 0)
11881 return isl_basic_map_free(bmap
);
11883 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11886 involves
= isl_basic_map_involves_dims(bmap
,
11887 isl_dim_param
, i
, 1);
11889 return isl_basic_map_free(bmap
);
11891 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11897 /* Drop all parameters not referenced by "bset".
11899 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11900 __isl_take isl_basic_set
*bset
)
11902 return bset_from_bmap(isl_basic_map_drop_unused_params(
11903 bset_to_bmap(bset
)));
11906 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11907 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11908 enum isl_dim_type c2
, enum isl_dim_type c3
,
11909 enum isl_dim_type c4
, enum isl_dim_type c5
)
11911 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11912 struct isl_mat
*mat
;
11918 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11919 isl_basic_map_total_dim(bmap
) + 1);
11922 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11923 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11924 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11925 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11926 isl_int_set(mat
->row
[i
][pos
],
11927 bmap
->eq
[i
][off
+ k
]);
11935 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11936 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11937 enum isl_dim_type c2
, enum isl_dim_type c3
,
11938 enum isl_dim_type c4
, enum isl_dim_type c5
)
11940 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11941 struct isl_mat
*mat
;
11947 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11948 isl_basic_map_total_dim(bmap
) + 1);
11951 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11952 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11953 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11954 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11955 isl_int_set(mat
->row
[i
][pos
],
11956 bmap
->ineq
[i
][off
+ k
]);
11964 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11965 __isl_take isl_space
*dim
,
11966 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11967 enum isl_dim_type c2
, enum isl_dim_type c3
,
11968 enum isl_dim_type c4
, enum isl_dim_type c5
)
11970 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11971 isl_basic_map
*bmap
;
11977 if (!dim
|| !eq
|| !ineq
)
11980 if (eq
->n_col
!= ineq
->n_col
)
11981 isl_die(dim
->ctx
, isl_error_invalid
,
11982 "equalities and inequalities matrices should have "
11983 "same number of columns", goto error
);
11985 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11987 if (eq
->n_col
< total
)
11988 isl_die(dim
->ctx
, isl_error_invalid
,
11989 "number of columns too small", goto error
);
11991 extra
= eq
->n_col
- total
;
11993 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11994 eq
->n_row
, ineq
->n_row
);
11997 for (i
= 0; i
< extra
; ++i
) {
11998 k
= isl_basic_map_alloc_div(bmap
);
12001 isl_int_set_si(bmap
->div
[k
][0], 0);
12003 for (i
= 0; i
< eq
->n_row
; ++i
) {
12004 l
= isl_basic_map_alloc_equality(bmap
);
12007 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12008 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12009 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
12010 isl_int_set(bmap
->eq
[l
][off
+ k
],
12016 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12017 l
= isl_basic_map_alloc_inequality(bmap
);
12020 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12021 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12022 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
12023 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12024 ineq
->row
[i
][pos
]);
12030 isl_space_free(dim
);
12032 isl_mat_free(ineq
);
12034 bmap
= isl_basic_map_simplify(bmap
);
12035 return isl_basic_map_finalize(bmap
);
12037 isl_space_free(dim
);
12039 isl_mat_free(ineq
);
12043 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12044 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12045 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12047 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12048 c1
, c2
, c3
, c4
, isl_dim_in
);
12051 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12052 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12053 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12055 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12056 c1
, c2
, c3
, c4
, isl_dim_in
);
12059 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12060 __isl_take isl_space
*dim
,
12061 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12062 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12064 isl_basic_map
*bmap
;
12065 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12066 c1
, c2
, c3
, c4
, isl_dim_in
);
12067 return bset_from_bmap(bmap
);
12070 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12073 return isl_bool_error
;
12075 return isl_space_can_zip(bmap
->dim
);
12078 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12081 return isl_bool_error
;
12083 return isl_space_can_zip(map
->dim
);
12086 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12087 * (A -> C) -> (B -> D).
12089 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12098 if (!isl_basic_map_can_zip(bmap
))
12099 isl_die(bmap
->ctx
, isl_error_invalid
,
12100 "basic map cannot be zipped", goto error
);
12101 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12102 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12103 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12104 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12105 bmap
= isl_basic_map_cow(bmap
);
12106 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12109 bmap
->dim
= isl_space_zip(bmap
->dim
);
12112 bmap
= isl_basic_map_mark_final(bmap
);
12115 isl_basic_map_free(bmap
);
12119 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12120 * (A -> C) -> (B -> D).
12122 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12129 if (!isl_map_can_zip(map
))
12130 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12133 map
= isl_map_cow(map
);
12137 for (i
= 0; i
< map
->n
; ++i
) {
12138 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12143 map
->dim
= isl_space_zip(map
->dim
);
12153 /* Can we apply isl_basic_map_curry to "bmap"?
12154 * That is, does it have a nested relation in its domain?
12156 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12159 return isl_bool_error
;
12161 return isl_space_can_curry(bmap
->dim
);
12164 /* Can we apply isl_map_curry to "map"?
12165 * That is, does it have a nested relation in its domain?
12167 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12170 return isl_bool_error
;
12172 return isl_space_can_curry(map
->dim
);
12175 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12178 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12184 if (!isl_basic_map_can_curry(bmap
))
12185 isl_die(bmap
->ctx
, isl_error_invalid
,
12186 "basic map cannot be curried", goto error
);
12187 bmap
= isl_basic_map_cow(bmap
);
12190 bmap
->dim
= isl_space_curry(bmap
->dim
);
12193 bmap
= isl_basic_map_mark_final(bmap
);
12196 isl_basic_map_free(bmap
);
12200 /* Given a map (A -> B) -> C, return the corresponding map
12203 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12205 return isl_map_change_space(map
, &isl_map_can_curry
,
12206 "map cannot be curried", &isl_space_curry
);
12209 /* Can isl_map_range_curry be applied to "map"?
12210 * That is, does it have a nested relation in its range,
12211 * the domain of which is itself a nested relation?
12213 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12216 return isl_bool_error
;
12218 return isl_space_can_range_curry(map
->dim
);
12221 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12222 * A -> (B -> (C -> D)).
12224 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12226 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12227 "map range cannot be curried",
12228 &isl_space_range_curry
);
12231 /* Can we apply isl_basic_map_uncurry to "bmap"?
12232 * That is, does it have a nested relation in its domain?
12234 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12237 return isl_bool_error
;
12239 return isl_space_can_uncurry(bmap
->dim
);
12242 /* Can we apply isl_map_uncurry to "map"?
12243 * That is, does it have a nested relation in its domain?
12245 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12248 return isl_bool_error
;
12250 return isl_space_can_uncurry(map
->dim
);
12253 /* Given a basic map A -> (B -> C), return the corresponding basic map
12256 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12262 if (!isl_basic_map_can_uncurry(bmap
))
12263 isl_die(bmap
->ctx
, isl_error_invalid
,
12264 "basic map cannot be uncurried",
12265 return isl_basic_map_free(bmap
));
12266 bmap
= isl_basic_map_cow(bmap
);
12269 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12271 return isl_basic_map_free(bmap
);
12272 bmap
= isl_basic_map_mark_final(bmap
);
12276 /* Given a map A -> (B -> C), return the corresponding map
12279 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12281 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12282 "map cannot be uncurried", &isl_space_uncurry
);
12285 /* Construct a basic map mapping the domain of the affine expression
12286 * to a one-dimensional range prescribed by the affine expression.
12287 * If "rational" is set, then construct a rational basic map.
12289 * A NaN affine expression cannot be converted to a basic map.
12291 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12292 __isl_take isl_aff
*aff
, int rational
)
12297 isl_local_space
*ls
;
12298 isl_basic_map
*bmap
= NULL
;
12302 is_nan
= isl_aff_is_nan(aff
);
12306 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12307 "cannot convert NaN", goto error
);
12309 ls
= isl_aff_get_local_space(aff
);
12310 bmap
= isl_basic_map_from_local_space(ls
);
12311 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12312 k
= isl_basic_map_alloc_equality(bmap
);
12316 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12317 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12318 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12319 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12320 aff
->v
->size
- (pos
+ 1));
12324 bmap
= isl_basic_map_set_rational(bmap
);
12325 bmap
= isl_basic_map_gauss(bmap
, NULL
);
12326 bmap
= isl_basic_map_finalize(bmap
);
12330 isl_basic_map_free(bmap
);
12334 /* Construct a basic map mapping the domain of the affine expression
12335 * to a one-dimensional range prescribed by the affine expression.
12337 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12339 return isl_basic_map_from_aff2(aff
, 0);
12342 /* Construct a map mapping the domain of the affine expression
12343 * to a one-dimensional range prescribed by the affine expression.
12345 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12347 isl_basic_map
*bmap
;
12349 bmap
= isl_basic_map_from_aff(aff
);
12350 return isl_map_from_basic_map(bmap
);
12353 /* Construct a basic map mapping the domain the multi-affine expression
12354 * to its range, with each dimension in the range equated to the
12355 * corresponding affine expression.
12356 * If "rational" is set, then construct a rational basic map.
12358 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12359 __isl_take isl_multi_aff
*maff
, int rational
)
12363 isl_basic_map
*bmap
;
12368 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12369 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12370 "invalid space", goto error
);
12372 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12373 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12375 bmap
= isl_basic_map_set_rational(bmap
);
12377 for (i
= 0; i
< maff
->n
; ++i
) {
12379 isl_basic_map
*bmap_i
;
12381 aff
= isl_aff_copy(maff
->u
.p
[i
]);
12382 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12384 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12387 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12389 isl_multi_aff_free(maff
);
12392 isl_multi_aff_free(maff
);
12396 /* Construct a basic map mapping the domain the multi-affine expression
12397 * to its range, with each dimension in the range equated to the
12398 * corresponding affine expression.
12400 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12401 __isl_take isl_multi_aff
*ma
)
12403 return isl_basic_map_from_multi_aff2(ma
, 0);
12406 /* Construct a map mapping the domain the multi-affine expression
12407 * to its range, with each dimension in the range equated to the
12408 * corresponding affine expression.
12410 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12412 isl_basic_map
*bmap
;
12414 bmap
= isl_basic_map_from_multi_aff(maff
);
12415 return isl_map_from_basic_map(bmap
);
12418 /* Construct a basic map mapping a domain in the given space to
12419 * to an n-dimensional range, with n the number of elements in the list,
12420 * where each coordinate in the range is prescribed by the
12421 * corresponding affine expression.
12422 * The domains of all affine expressions in the list are assumed to match
12425 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12426 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12430 isl_basic_map
*bmap
;
12435 dim
= isl_space_from_domain(domain_dim
);
12436 bmap
= isl_basic_map_universe(dim
);
12438 for (i
= 0; i
< list
->n
; ++i
) {
12440 isl_basic_map
*bmap_i
;
12442 aff
= isl_aff_copy(list
->p
[i
]);
12443 bmap_i
= isl_basic_map_from_aff(aff
);
12445 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12448 isl_aff_list_free(list
);
12452 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12453 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12455 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12458 /* Construct a basic map where the given dimensions are equal to each other.
12460 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12461 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12463 isl_basic_map
*bmap
= NULL
;
12469 if (pos1
>= isl_space_dim(space
, type1
))
12470 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12471 "index out of bounds", goto error
);
12472 if (pos2
>= isl_space_dim(space
, type2
))
12473 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12474 "index out of bounds", goto error
);
12476 if (type1
== type2
&& pos1
== pos2
)
12477 return isl_basic_map_universe(space
);
12479 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12480 i
= isl_basic_map_alloc_equality(bmap
);
12483 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12484 pos1
+= isl_basic_map_offset(bmap
, type1
);
12485 pos2
+= isl_basic_map_offset(bmap
, type2
);
12486 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12487 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12488 bmap
= isl_basic_map_finalize(bmap
);
12489 isl_space_free(space
);
12492 isl_space_free(space
);
12493 isl_basic_map_free(bmap
);
12497 /* Add a constraint imposing that the given two dimensions are equal.
12499 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12500 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12504 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12506 bmap
= isl_basic_map_intersect(bmap
, eq
);
12511 /* Add a constraint imposing that the given two dimensions are equal.
12513 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12514 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12516 isl_basic_map
*bmap
;
12518 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12520 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12525 /* Add a constraint imposing that the given two dimensions have opposite values.
12527 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12528 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12530 isl_basic_map
*bmap
= NULL
;
12536 if (pos1
>= isl_map_dim(map
, type1
))
12537 isl_die(map
->ctx
, isl_error_invalid
,
12538 "index out of bounds", goto error
);
12539 if (pos2
>= isl_map_dim(map
, type2
))
12540 isl_die(map
->ctx
, isl_error_invalid
,
12541 "index out of bounds", goto error
);
12543 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12544 i
= isl_basic_map_alloc_equality(bmap
);
12547 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12548 pos1
+= isl_basic_map_offset(bmap
, type1
);
12549 pos2
+= isl_basic_map_offset(bmap
, type2
);
12550 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12551 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12552 bmap
= isl_basic_map_finalize(bmap
);
12554 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12558 isl_basic_map_free(bmap
);
12563 /* Construct a constraint imposing that the value of the first dimension is
12564 * greater than or equal to that of the second.
12566 static __isl_give isl_constraint
*constraint_order_ge(
12567 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12568 enum isl_dim_type type2
, int pos2
)
12575 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12577 if (pos1
>= isl_constraint_dim(c
, type1
))
12578 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12579 "index out of bounds", return isl_constraint_free(c
));
12580 if (pos2
>= isl_constraint_dim(c
, type2
))
12581 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12582 "index out of bounds", return isl_constraint_free(c
));
12584 if (type1
== type2
&& pos1
== pos2
)
12587 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12588 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12593 /* Add a constraint imposing that the value of the first dimension is
12594 * greater than or equal to that of the second.
12596 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12597 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12602 if (type1
== type2
&& pos1
== pos2
)
12604 space
= isl_basic_map_get_space(bmap
);
12605 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12606 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12611 /* Add a constraint imposing that the value of the first dimension is
12612 * greater than or equal to that of the second.
12614 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12615 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12620 if (type1
== type2
&& pos1
== pos2
)
12622 space
= isl_map_get_space(map
);
12623 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12624 map
= isl_map_add_constraint(map
, c
);
12629 /* Add a constraint imposing that the value of the first dimension is
12630 * less than or equal to that of the second.
12632 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12633 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12635 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12638 /* Construct a basic map where the value of the first dimension is
12639 * greater than that of the second.
12641 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12642 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12644 isl_basic_map
*bmap
= NULL
;
12650 if (pos1
>= isl_space_dim(space
, type1
))
12651 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12652 "index out of bounds", goto error
);
12653 if (pos2
>= isl_space_dim(space
, type2
))
12654 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12655 "index out of bounds", goto error
);
12657 if (type1
== type2
&& pos1
== pos2
)
12658 return isl_basic_map_empty(space
);
12660 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12661 i
= isl_basic_map_alloc_inequality(bmap
);
12663 return isl_basic_map_free(bmap
);
12664 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12665 pos1
+= isl_basic_map_offset(bmap
, type1
);
12666 pos2
+= isl_basic_map_offset(bmap
, type2
);
12667 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12668 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12669 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12670 bmap
= isl_basic_map_finalize(bmap
);
12674 isl_space_free(space
);
12675 isl_basic_map_free(bmap
);
12679 /* Add a constraint imposing that the value of the first dimension is
12680 * greater than that of the second.
12682 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12683 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12687 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12689 bmap
= isl_basic_map_intersect(bmap
, gt
);
12694 /* Add a constraint imposing that the value of the first dimension is
12695 * greater than that of the second.
12697 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12698 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12700 isl_basic_map
*bmap
;
12702 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12704 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12709 /* Add a constraint imposing that the value of the first dimension is
12710 * smaller than that of the second.
12712 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12713 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12715 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12718 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12722 isl_local_space
*ls
;
12727 if (!isl_basic_map_divs_known(bmap
))
12728 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12729 "some divs are unknown", return NULL
);
12731 ls
= isl_basic_map_get_local_space(bmap
);
12732 div
= isl_local_space_get_div(ls
, pos
);
12733 isl_local_space_free(ls
);
12738 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12741 return isl_basic_map_get_div(bset
, pos
);
12744 /* Plug in "subs" for dimension "type", "pos" of "bset".
12746 * Let i be the dimension to replace and let "subs" be of the form
12750 * Any integer division with a non-zero coefficient for i,
12752 * floor((a i + g)/m)
12756 * floor((a f + d g)/(m d))
12758 * Constraints of the form
12766 * We currently require that "subs" is an integral expression.
12767 * Handling rational expressions may require us to add stride constraints
12768 * as we do in isl_basic_set_preimage_multi_aff.
12770 __isl_give isl_basic_set
*isl_basic_set_substitute(
12771 __isl_take isl_basic_set
*bset
,
12772 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12778 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12781 bset
= isl_basic_set_cow(bset
);
12782 if (!bset
|| !subs
)
12785 ctx
= isl_basic_set_get_ctx(bset
);
12786 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12787 isl_die(ctx
, isl_error_invalid
,
12788 "spaces don't match", goto error
);
12789 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12790 isl_die(ctx
, isl_error_unsupported
,
12791 "cannot handle divs yet", goto error
);
12792 if (!isl_int_is_one(subs
->v
->el
[0]))
12793 isl_die(ctx
, isl_error_invalid
,
12794 "can only substitute integer expressions", goto error
);
12796 pos
+= isl_basic_set_offset(bset
, type
);
12800 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12801 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12803 isl_int_set(v
, bset
->eq
[i
][pos
]);
12804 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12805 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12806 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12809 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12810 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12812 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12813 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12814 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12815 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12818 for (i
= 0; i
< bset
->n_div
; ++i
) {
12819 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12821 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12822 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12823 isl_seq_combine(bset
->div
[i
] + 1,
12824 subs
->v
->el
[0], bset
->div
[i
] + 1,
12825 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12826 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12831 bset
= isl_basic_set_simplify(bset
);
12832 return isl_basic_set_finalize(bset
);
12834 isl_basic_set_free(bset
);
12838 /* Plug in "subs" for dimension "type", "pos" of "set".
12840 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12841 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12845 if (set
&& isl_set_plain_is_empty(set
))
12848 set
= isl_set_cow(set
);
12852 for (i
= set
->n
- 1; i
>= 0; --i
) {
12853 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12854 if (remove_if_empty(set
, i
) < 0)
12864 /* Check if the range of "ma" is compatible with the domain or range
12865 * (depending on "type") of "bmap".
12867 static isl_stat
check_basic_map_compatible_range_multi_aff(
12868 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12869 __isl_keep isl_multi_aff
*ma
)
12872 isl_space
*ma_space
;
12874 ma_space
= isl_multi_aff_get_space(ma
);
12876 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12880 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12881 "parameters don't match", goto error
);
12882 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12886 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12887 "spaces don't match", goto error
);
12889 isl_space_free(ma_space
);
12890 return isl_stat_ok
;
12892 isl_space_free(ma_space
);
12893 return isl_stat_error
;
12896 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12897 * coefficients before the transformed range of dimensions,
12898 * the "n_after" coefficients after the transformed range of dimensions
12899 * and the coefficients of the other divs in "bmap".
12901 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12902 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12907 isl_local_space
*ls
;
12912 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12916 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12917 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12918 for (i
= 0; i
< n_div
; ++i
) {
12919 int o_bmap
= 0, o_ls
= 0;
12921 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12922 o_bmap
+= 1 + 1 + n_param
;
12923 o_ls
+= 1 + 1 + n_param
;
12924 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12925 o_bmap
+= n_before
;
12926 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12927 ls
->div
->row
[i
] + o_ls
, n_set
);
12930 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12932 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12933 ls
->div
->row
[i
] + o_ls
, n_div
);
12936 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12937 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12941 isl_local_space_free(ls
);
12944 isl_local_space_free(ls
);
12948 /* How many stride constraints does "ma" enforce?
12949 * That is, how many of the affine expressions have a denominator
12950 * different from one?
12952 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12957 for (i
= 0; i
< ma
->n
; ++i
)
12958 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12964 /* For each affine expression in ma of the form
12966 * x_i = (f_i y + h_i)/m_i
12968 * with m_i different from one, add a constraint to "bmap"
12971 * f_i y + h_i = m_i alpha_i
12973 * with alpha_i an additional existentially quantified variable.
12975 * The input variables of "ma" correspond to a subset of the variables
12976 * of "bmap". There are "n_before" variables in "bmap" before this
12977 * subset and "n_after" variables after this subset.
12978 * The integer divisions of the affine expressions in "ma" are assumed
12979 * to have been aligned. There are "n_div_ma" of them and
12980 * they appear first in "bmap", straight after the "n_after" variables.
12982 static __isl_give isl_basic_map
*add_ma_strides(
12983 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12984 int n_before
, int n_after
, int n_div_ma
)
12992 total
= isl_basic_map_total_dim(bmap
);
12993 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12994 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12995 for (i
= 0; i
< ma
->n
; ++i
) {
12996 int o_bmap
= 0, o_ma
= 1;
12998 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13000 div
= isl_basic_map_alloc_div(bmap
);
13001 k
= isl_basic_map_alloc_equality(bmap
);
13002 if (div
< 0 || k
< 0)
13004 isl_int_set_si(bmap
->div
[div
][0], 0);
13005 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13006 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13007 o_bmap
+= 1 + n_param
;
13008 o_ma
+= 1 + n_param
;
13009 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13010 o_bmap
+= n_before
;
13011 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13012 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13015 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13017 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13018 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13019 o_bmap
+= n_div_ma
;
13021 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13022 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13028 isl_basic_map_free(bmap
);
13032 /* Replace the domain or range space (depending on "type) of "space" by "set".
13034 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13035 enum isl_dim_type type
, __isl_take isl_space
*set
)
13037 if (type
== isl_dim_in
) {
13038 space
= isl_space_range(space
);
13039 space
= isl_space_map_from_domain_and_range(set
, space
);
13041 space
= isl_space_domain(space
);
13042 space
= isl_space_map_from_domain_and_range(space
, set
);
13048 /* Compute the preimage of the domain or range (depending on "type")
13049 * of "bmap" under the function represented by "ma".
13050 * In other words, plug in "ma" in the domain or range of "bmap".
13051 * The result is a basic map that lives in the same space as "bmap"
13052 * except that the domain or range has been replaced by
13053 * the domain space of "ma".
13055 * If bmap is represented by
13057 * A(p) + S u + B x + T v + C(divs) >= 0,
13059 * where u and x are input and output dimensions if type == isl_dim_out
13060 * while x and v are input and output dimensions if type == isl_dim_in,
13061 * and ma is represented by
13063 * x = D(p) + F(y) + G(divs')
13065 * then the result is
13067 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13069 * The divs in the input set are similarly adjusted.
13072 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13076 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13077 * B_i G(divs') + c_i(divs))/n_i)
13079 * If bmap is not a rational map and if F(y) involves any denominators
13081 * x_i = (f_i y + h_i)/m_i
13083 * then additional constraints are added to ensure that we only
13084 * map back integer points. That is we enforce
13086 * f_i y + h_i = m_i alpha_i
13088 * with alpha_i an additional existentially quantified variable.
13090 * We first copy over the divs from "ma".
13091 * Then we add the modified constraints and divs from "bmap".
13092 * Finally, we add the stride constraints, if needed.
13094 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13095 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13096 __isl_take isl_multi_aff
*ma
)
13100 isl_basic_map
*res
= NULL
;
13101 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
13102 isl_int f
, c1
, c2
, g
;
13111 ma
= isl_multi_aff_align_divs(ma
);
13114 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13117 if (type
== isl_dim_in
) {
13119 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13121 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13124 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13125 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13127 space
= isl_multi_aff_get_domain_space(ma
);
13128 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13129 rational
= isl_basic_map_is_rational(bmap
);
13130 strides
= rational
? 0 : multi_aff_strides(ma
);
13131 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13132 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13134 res
= isl_basic_map_set_rational(res
);
13136 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13137 if (isl_basic_map_alloc_div(res
) < 0)
13140 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
13143 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13144 k
= isl_basic_map_alloc_equality(res
);
13147 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13148 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13151 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13152 k
= isl_basic_map_alloc_inequality(res
);
13155 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13156 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13159 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13160 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13161 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13164 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13165 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13170 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13176 isl_basic_map_free(bmap
);
13177 isl_multi_aff_free(ma
);
13178 res
= isl_basic_map_simplify(res
);
13179 return isl_basic_map_finalize(res
);
13185 isl_basic_map_free(bmap
);
13186 isl_multi_aff_free(ma
);
13187 isl_basic_map_free(res
);
13191 /* Compute the preimage of "bset" under the function represented by "ma".
13192 * In other words, plug in "ma" in "bset". The result is a basic set
13193 * that lives in the domain space of "ma".
13195 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13196 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13198 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13201 /* Compute the preimage of the domain of "bmap" under the function
13202 * represented by "ma".
13203 * In other words, plug in "ma" in the domain of "bmap".
13204 * The result is a basic map that lives in the same space as "bmap"
13205 * except that the domain has been replaced by the domain space of "ma".
13207 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13208 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13210 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13213 /* Compute the preimage of the range of "bmap" under the function
13214 * represented by "ma".
13215 * In other words, plug in "ma" in the range of "bmap".
13216 * The result is a basic map that lives in the same space as "bmap"
13217 * except that the range has been replaced by the domain space of "ma".
13219 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13220 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13222 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13225 /* Check if the range of "ma" is compatible with the domain or range
13226 * (depending on "type") of "map".
13227 * Return isl_stat_error if anything is wrong.
13229 static isl_stat
check_map_compatible_range_multi_aff(
13230 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13231 __isl_keep isl_multi_aff
*ma
)
13234 isl_space
*ma_space
;
13236 ma_space
= isl_multi_aff_get_space(ma
);
13237 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13238 isl_space_free(ma_space
);
13240 return isl_stat_error
;
13242 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13243 "spaces don't match", return isl_stat_error
);
13244 return isl_stat_ok
;
13247 /* Compute the preimage of the domain or range (depending on "type")
13248 * of "map" under the function represented by "ma".
13249 * In other words, plug in "ma" in the domain or range of "map".
13250 * The result is a map that lives in the same space as "map"
13251 * except that the domain or range has been replaced by
13252 * the domain space of "ma".
13254 * The parameters are assumed to have been aligned.
13256 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13257 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13262 map
= isl_map_cow(map
);
13263 ma
= isl_multi_aff_align_divs(ma
);
13266 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13269 for (i
= 0; i
< map
->n
; ++i
) {
13270 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13271 isl_multi_aff_copy(ma
));
13276 space
= isl_multi_aff_get_domain_space(ma
);
13277 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13279 isl_space_free(map
->dim
);
13284 isl_multi_aff_free(ma
);
13286 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13287 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13290 isl_multi_aff_free(ma
);
13295 /* Compute the preimage of the domain or range (depending on "type")
13296 * of "map" under the function represented by "ma".
13297 * In other words, plug in "ma" in the domain or range of "map".
13298 * The result is a map that lives in the same space as "map"
13299 * except that the domain or range has been replaced by
13300 * the domain space of "ma".
13302 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13303 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13310 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13314 return map_preimage_multi_aff(map
, type
, ma
);
13316 if (isl_map_check_named_params(map
) < 0)
13318 if (!isl_space_has_named_params(ma
->space
))
13319 isl_die(map
->ctx
, isl_error_invalid
,
13320 "unaligned unnamed parameters", goto error
);
13321 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13322 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13324 return map_preimage_multi_aff(map
, type
, ma
);
13326 isl_multi_aff_free(ma
);
13327 return isl_map_free(map
);
13330 /* Compute the preimage of "set" under the function represented by "ma".
13331 * In other words, plug in "ma" in "set". The result is a set
13332 * that lives in the domain space of "ma".
13334 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13335 __isl_take isl_multi_aff
*ma
)
13337 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13340 /* Compute the preimage of the domain of "map" under the function
13341 * represented by "ma".
13342 * In other words, plug in "ma" in the domain of "map".
13343 * The result is a map that lives in the same space as "map"
13344 * except that the domain has been replaced by the domain space of "ma".
13346 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13347 __isl_take isl_multi_aff
*ma
)
13349 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13352 /* Compute the preimage of the range of "map" under the function
13353 * represented by "ma".
13354 * In other words, plug in "ma" in the range of "map".
13355 * The result is a map that lives in the same space as "map"
13356 * except that the range has been replaced by the domain space of "ma".
13358 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13359 __isl_take isl_multi_aff
*ma
)
13361 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13364 /* Compute the preimage of "map" under the function represented by "pma".
13365 * In other words, plug in "pma" in the domain or range of "map".
13366 * The result is a map that lives in the same space as "map",
13367 * except that the space of type "type" has been replaced by
13368 * the domain space of "pma".
13370 * The parameters of "map" and "pma" are assumed to have been aligned.
13372 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13373 __isl_take isl_map
*map
, enum isl_dim_type type
,
13374 __isl_take isl_pw_multi_aff
*pma
)
13383 isl_pw_multi_aff_free(pma
);
13384 res
= isl_map_empty(isl_map_get_space(map
));
13389 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13390 isl_multi_aff_copy(pma
->p
[0].maff
));
13391 if (type
== isl_dim_in
)
13392 res
= isl_map_intersect_domain(res
,
13393 isl_map_copy(pma
->p
[0].set
));
13395 res
= isl_map_intersect_range(res
,
13396 isl_map_copy(pma
->p
[0].set
));
13398 for (i
= 1; i
< pma
->n
; ++i
) {
13401 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13402 isl_multi_aff_copy(pma
->p
[i
].maff
));
13403 if (type
== isl_dim_in
)
13404 res_i
= isl_map_intersect_domain(res_i
,
13405 isl_map_copy(pma
->p
[i
].set
));
13407 res_i
= isl_map_intersect_range(res_i
,
13408 isl_map_copy(pma
->p
[i
].set
));
13409 res
= isl_map_union(res
, res_i
);
13412 isl_pw_multi_aff_free(pma
);
13416 isl_pw_multi_aff_free(pma
);
13421 /* Compute the preimage of "map" under the function represented by "pma".
13422 * In other words, plug in "pma" in the domain or range of "map".
13423 * The result is a map that lives in the same space as "map",
13424 * except that the space of type "type" has been replaced by
13425 * the domain space of "pma".
13427 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13428 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13435 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13439 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13441 if (isl_map_check_named_params(map
) < 0)
13443 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13445 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13446 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13448 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13450 isl_pw_multi_aff_free(pma
);
13451 return isl_map_free(map
);
13454 /* Compute the preimage of "set" under the function represented by "pma".
13455 * In other words, plug in "pma" in "set". The result is a set
13456 * that lives in the domain space of "pma".
13458 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13459 __isl_take isl_pw_multi_aff
*pma
)
13461 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13464 /* Compute the preimage of the domain of "map" under the function
13465 * represented by "pma".
13466 * In other words, plug in "pma" in the domain of "map".
13467 * The result is a map that lives in the same space as "map",
13468 * except that domain space has been replaced by the domain space of "pma".
13470 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13471 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13473 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13476 /* Compute the preimage of the range of "map" under the function
13477 * represented by "pma".
13478 * In other words, plug in "pma" in the range of "map".
13479 * The result is a map that lives in the same space as "map",
13480 * except that range space has been replaced by the domain space of "pma".
13482 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13483 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13485 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13488 /* Compute the preimage of "map" under the function represented by "mpa".
13489 * In other words, plug in "mpa" in the domain or range of "map".
13490 * The result is a map that lives in the same space as "map",
13491 * except that the space of type "type" has been replaced by
13492 * the domain space of "mpa".
13494 * If the map does not involve any constraints that refer to the
13495 * dimensions of the substituted space, then the only possible
13496 * effect of "mpa" on the map is to map the space to a different space.
13497 * We create a separate isl_multi_aff to effectuate this change
13498 * in order to avoid spurious splitting of the map along the pieces
13500 * If "mpa" has a non-trivial explicit domain, however,
13501 * then the full substitution should be performed.
13503 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13504 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13508 isl_pw_multi_aff
*pma
;
13513 n
= isl_map_dim(map
, type
);
13514 full
= isl_map_involves_dims(map
, type
, 0, n
);
13515 if (full
>= 0 && !full
)
13516 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13523 space
= isl_multi_pw_aff_get_space(mpa
);
13524 isl_multi_pw_aff_free(mpa
);
13525 ma
= isl_multi_aff_zero(space
);
13526 return isl_map_preimage_multi_aff(map
, type
, ma
);
13529 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13530 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13533 isl_multi_pw_aff_free(mpa
);
13537 /* Compute the preimage of "map" under the function represented by "mpa".
13538 * In other words, plug in "mpa" in the domain "map".
13539 * The result is a map that lives in the same space as "map",
13540 * except that domain space has been replaced by the domain space of "mpa".
13542 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13543 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13545 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13548 /* Compute the preimage of "set" by the function represented by "mpa".
13549 * In other words, plug in "mpa" in "set".
13551 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13552 __isl_take isl_multi_pw_aff
*mpa
)
13554 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13557 /* Return a copy of the equality constraints of "bset" as a matrix.
13559 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13560 __isl_keep isl_basic_set
*bset
)
13568 ctx
= isl_basic_set_get_ctx(bset
);
13569 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13570 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13573 /* Are the "n" "coefficients" starting at "first" of the integer division
13574 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13576 * The "coefficient" at position 0 is the denominator.
13577 * The "coefficient" at position 1 is the constant term.
13579 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13580 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13581 unsigned first
, unsigned n
)
13583 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13584 return isl_bool_error
;
13585 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13586 return isl_bool_error
;
13587 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13588 bmap2
->div
[pos2
] + first
, n
);
13591 /* Are the integer division expressions at position "pos1" in "bmap1" and
13592 * "pos2" in "bmap2" equal to each other, except that the constant terms
13595 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13596 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13597 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13602 if (!bmap1
|| !bmap2
)
13603 return isl_bool_error
;
13604 total
= isl_basic_map_total_dim(bmap1
);
13605 if (total
!= isl_basic_map_total_dim(bmap2
))
13606 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13607 "incomparable div expressions", return isl_bool_error
);
13608 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13610 if (equal
< 0 || !equal
)
13612 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13614 if (equal
< 0 || equal
)
13615 return isl_bool_not(equal
);
13616 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13620 /* Replace the numerator of the constant term of the integer division
13621 * expression at position "div" in "bmap" by "value".
13622 * The caller guarantees that this does not change the meaning
13625 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13626 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13628 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13629 return isl_basic_map_free(bmap
);
13631 isl_int_set_si(bmap
->div
[div
][1], value
);
13636 /* Is the point "inner" internal to inequality constraint "ineq"
13638 * The point is considered to be internal to the inequality constraint,
13639 * if it strictly lies on the positive side of the inequality constraint,
13640 * or if it lies on the constraint and the constraint is lexico-positive.
13642 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13643 __isl_keep isl_basic_set
*bset
, int ineq
)
13649 if (!inner
|| !bset
)
13650 return isl_bool_error
;
13652 ctx
= isl_basic_set_get_ctx(bset
);
13653 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13654 &ctx
->normalize_gcd
);
13655 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13656 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13658 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13659 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13660 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13663 /* Tighten the inequality constraints of "bset" that are outward with respect
13664 * to the point "vec".
13665 * That is, tighten the constraints that are not satisfied by "vec".
13667 * "vec" is a point internal to some superset S of "bset" that is used
13668 * to make the subsets of S disjoint, by tightening one half of the constraints
13669 * that separate two subsets. In particular, the constraints of S
13670 * are all satisfied by "vec" and should not be tightened.
13671 * Of the internal constraints, those that have "vec" on the outside
13672 * are tightened. The shared facet is included in the adjacent subset
13673 * with the opposite constraint.
13674 * For constraints that saturate "vec", this criterion cannot be used
13675 * to determine which of the two sides should be tightened.
13676 * Instead, the sign of the first non-zero coefficient is used
13677 * to make this choice. Note that this second criterion is never used
13678 * on the constraints of S since "vec" is interior to "S".
13680 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13681 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13685 bset
= isl_basic_set_cow(bset
);
13688 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13691 internal
= is_internal(vec
, bset
, j
);
13693 return isl_basic_set_free(bset
);
13696 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13702 /* Replace the variables x of type "type" starting at "first" in "bmap"
13703 * by x' with x = M x' with M the matrix trans.
13704 * That is, replace the corresponding coefficients c by c M.
13706 * The transformation matrix should be a square matrix.
13708 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13709 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13710 __isl_take isl_mat
*trans
)
13714 bmap
= isl_basic_map_cow(bmap
);
13715 if (!bmap
|| !trans
)
13718 if (trans
->n_row
!= trans
->n_col
)
13719 isl_die(trans
->ctx
, isl_error_invalid
,
13720 "expecting square transformation matrix", goto error
);
13721 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13722 isl_die(trans
->ctx
, isl_error_invalid
,
13723 "oversized transformation matrix", goto error
);
13725 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13727 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13728 isl_mat_copy(trans
)) < 0)
13730 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13731 isl_mat_copy(trans
)) < 0)
13733 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13734 isl_mat_copy(trans
)) < 0)
13737 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
13738 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13740 isl_mat_free(trans
);
13743 isl_mat_free(trans
);
13744 isl_basic_map_free(bmap
);
13748 /* Replace the variables x of type "type" starting at "first" in "bset"
13749 * by x' with x = M x' with M the matrix trans.
13750 * That is, replace the corresponding coefficients c by c M.
13752 * The transformation matrix should be a square matrix.
13754 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13755 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13756 __isl_take isl_mat
*trans
)
13758 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);