2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
58 __isl_keep
const isl_basic_set
*bset
)
60 return (const isl_basic_map
*) bset
;
63 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
66 case isl_dim_param
: return dim
->nparam
;
67 case isl_dim_in
: return dim
->n_in
;
68 case isl_dim_out
: return dim
->n_out
;
69 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
74 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
77 case isl_dim_param
: return 1;
78 case isl_dim_in
: return 1 + dim
->nparam
;
79 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
85 enum isl_dim_type type
)
90 case isl_dim_cst
: return 1;
93 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
94 case isl_dim_div
: return bmap
->n_div
;
95 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
100 /* Return the space of "map".
102 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
104 return map
? map
->dim
: NULL
;
107 /* Return the space of "set".
109 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
111 return isl_map_peek_space(set_to_map(set
));
114 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
116 return map
? n(map
->dim
, type
) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
121 return set
? n(set
->dim
, type
) : 0;
124 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
125 enum isl_dim_type type
)
134 case isl_dim_cst
: return 0;
135 case isl_dim_param
: return 1;
136 case isl_dim_in
: return 1 + space
->nparam
;
137 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
138 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
144 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
145 enum isl_dim_type type
)
147 return isl_basic_map_offset(bset
, type
);
150 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
152 return pos(map
->dim
, type
);
155 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
156 enum isl_dim_type type
)
158 return isl_basic_map_dim(bset
, type
);
161 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
163 return isl_basic_set_dim(bset
, isl_dim_set
);
166 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
168 return isl_basic_set_dim(bset
, isl_dim_param
);
171 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
173 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
176 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
178 return isl_set_dim(set
, isl_dim_set
);
181 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
183 return isl_set_dim(set
, isl_dim_param
);
186 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
188 return bmap
? bmap
->dim
->n_in
: 0;
191 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
193 return bmap
? bmap
->dim
->n_out
: 0;
196 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
198 return bmap
? bmap
->dim
->nparam
: 0;
201 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
203 return bmap
? bmap
->n_div
: 0;
206 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
208 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
211 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
213 return map
? map
->dim
->n_in
: 0;
216 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
218 return map
? map
->dim
->n_out
: 0;
221 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
223 return map
? map
->dim
->nparam
: 0;
226 /* Return the number of equality constraints in the description of "bmap".
227 * Return -1 on error.
229 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
236 /* Return the number of equality constraints in the description of "bset".
237 * Return -1 on error.
239 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
241 return isl_basic_map_n_equality(bset_to_bmap(bset
));
244 /* Return the number of inequality constraints in the description of "bmap".
245 * Return -1 on error.
247 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
254 /* Return the number of inequality constraints in the description of "bset".
255 * Return -1 on error.
257 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
259 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
262 /* Do "bmap1" and "bmap2" have the same parameters?
264 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
265 __isl_keep isl_basic_map
*bmap2
)
267 isl_space
*space1
, *space2
;
269 space1
= isl_basic_map_peek_space(bmap1
);
270 space2
= isl_basic_map_peek_space(bmap2
);
271 return isl_space_has_equal_params(space1
, space2
);
274 /* Do "map1" and "map2" have the same parameters?
276 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
277 __isl_keep isl_map
*map2
)
279 isl_space
*space1
, *space2
;
281 space1
= isl_map_peek_space(map1
);
282 space2
= isl_map_peek_space(map2
);
283 return isl_space_has_equal_params(space1
, space2
);
286 /* Do "map" and "set" have the same parameters?
288 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
289 __isl_keep isl_set
*set
)
291 return isl_map_has_equal_params(map
, set_to_map(set
));
294 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
295 __isl_keep isl_set
*set
)
299 return isl_bool_error
;
300 m
= isl_map_has_equal_params(map
, set_to_map(set
));
303 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
304 set
->dim
, isl_dim_set
);
307 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
308 __isl_keep isl_basic_set
*bset
)
312 return isl_bool_error
;
313 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
316 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
317 bset
->dim
, isl_dim_set
);
320 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
321 __isl_keep isl_set
*set
)
325 return isl_bool_error
;
326 m
= isl_map_has_equal_params(map
, set_to_map(set
));
329 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
330 set
->dim
, isl_dim_set
);
333 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
334 __isl_keep isl_basic_set
*bset
)
338 return isl_bool_error
;
339 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
342 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
343 bset
->dim
, isl_dim_set
);
346 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
348 return bmap
? bmap
->ctx
: NULL
;
351 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
353 return bset
? bset
->ctx
: NULL
;
356 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
358 return map
? map
->ctx
: NULL
;
361 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
363 return set
? set
->ctx
: NULL
;
366 /* Return the space of "bmap".
368 __isl_keep isl_space
*isl_basic_map_peek_space(
369 __isl_keep
const isl_basic_map
*bmap
)
371 return bmap
? bmap
->dim
: NULL
;
374 /* Return the space of "bset".
376 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
378 return isl_basic_map_peek_space(bset_to_bmap(bset
));
381 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
383 return isl_space_copy(isl_basic_map_peek_space(bmap
));
386 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
388 return isl_basic_map_get_space(bset_to_bmap(bset
));
391 /* Extract the divs in "bmap" as a matrix.
393 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
404 ctx
= isl_basic_map_get_ctx(bmap
);
405 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
406 cols
= 1 + 1 + total
+ bmap
->n_div
;
407 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
411 for (i
= 0; i
< bmap
->n_div
; ++i
)
412 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
417 /* Extract the divs in "bset" as a matrix.
419 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
421 return isl_basic_map_get_divs(bset
);
424 __isl_give isl_local_space
*isl_basic_map_get_local_space(
425 __isl_keep isl_basic_map
*bmap
)
432 div
= isl_basic_map_get_divs(bmap
);
433 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
436 __isl_give isl_local_space
*isl_basic_set_get_local_space(
437 __isl_keep isl_basic_set
*bset
)
439 return isl_basic_map_get_local_space(bset
);
442 /* For each known div d = floor(f/m), add the constraints
445 * -(f-(m-1)) + m d >= 0
447 * Do not finalize the result.
449 static __isl_give isl_basic_map
*add_known_div_constraints(
450 __isl_take isl_basic_map
*bmap
)
457 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
460 bmap
= isl_basic_map_cow(bmap
);
461 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
464 for (i
= 0; i
< n_div
; ++i
) {
465 if (isl_int_is_zero(bmap
->div
[i
][0]))
467 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
468 return isl_basic_map_free(bmap
);
474 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
475 __isl_take isl_local_space
*ls
)
484 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
485 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
486 n_div
, 0, 2 * n_div
);
488 for (i
= 0; i
< n_div
; ++i
)
489 if (isl_basic_map_alloc_div(bmap
) < 0)
492 for (i
= 0; i
< n_div
; ++i
)
493 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
494 bmap
= add_known_div_constraints(bmap
);
496 isl_local_space_free(ls
);
499 isl_local_space_free(ls
);
500 isl_basic_map_free(bmap
);
504 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
505 __isl_take isl_local_space
*ls
)
507 return isl_basic_map_from_local_space(ls
);
510 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
512 return isl_space_copy(isl_map_peek_space(map
));
515 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
519 return isl_space_copy(set
->dim
);
522 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
523 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
525 bmap
= isl_basic_map_cow(bmap
);
528 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
531 bmap
= isl_basic_map_finalize(bmap
);
534 isl_basic_map_free(bmap
);
538 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
539 __isl_take isl_basic_set
*bset
, const char *s
)
541 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
544 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
545 enum isl_dim_type type
)
547 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
550 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
551 enum isl_dim_type type
, const char *s
)
555 map
= isl_map_cow(map
);
559 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
563 for (i
= 0; i
< map
->n
; ++i
) {
564 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
575 /* Replace the identifier of the tuple of type "type" by "id".
577 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
578 __isl_take isl_basic_map
*bmap
,
579 enum isl_dim_type type
, __isl_take isl_id
*id
)
581 bmap
= isl_basic_map_cow(bmap
);
584 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
586 return isl_basic_map_free(bmap
);
587 bmap
= isl_basic_map_finalize(bmap
);
594 /* Replace the identifier of the tuple by "id".
596 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
597 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
599 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
602 /* Does the input or output tuple have a name?
604 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
606 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
609 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
610 enum isl_dim_type type
)
612 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
615 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
618 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
622 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
623 enum isl_dim_type type
, __isl_take isl_id
*id
)
625 map
= isl_map_cow(map
);
629 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
631 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
637 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
638 __isl_take isl_id
*id
)
640 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
643 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
644 enum isl_dim_type type
)
646 map
= isl_map_cow(map
);
650 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
652 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
655 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
657 return isl_map_reset_tuple_id(set
, isl_dim_set
);
660 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
662 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
665 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
666 enum isl_dim_type type
)
668 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
671 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
673 return isl_map_has_tuple_id(set
, isl_dim_set
);
676 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
678 return isl_map_get_tuple_id(set
, isl_dim_set
);
681 /* Does the set tuple have a name?
683 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
686 return isl_bool_error
;
687 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
691 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
693 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
696 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
698 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
701 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
702 enum isl_dim_type type
, unsigned pos
)
704 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
707 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
708 enum isl_dim_type type
, unsigned pos
)
710 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
713 /* Does the given dimension have a name?
715 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
716 enum isl_dim_type type
, unsigned pos
)
719 return isl_bool_error
;
720 return isl_space_has_dim_name(map
->dim
, type
, pos
);
723 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
724 enum isl_dim_type type
, unsigned pos
)
726 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
729 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
730 enum isl_dim_type type
, unsigned pos
)
732 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
735 /* Does the given dimension have a name?
737 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
738 enum isl_dim_type type
, unsigned pos
)
741 return isl_bool_error
;
742 return isl_space_has_dim_name(set
->dim
, type
, pos
);
745 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
746 __isl_take isl_basic_map
*bmap
,
747 enum isl_dim_type type
, unsigned pos
, const char *s
)
749 bmap
= isl_basic_map_cow(bmap
);
752 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
755 return isl_basic_map_finalize(bmap
);
757 isl_basic_map_free(bmap
);
761 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
762 enum isl_dim_type type
, unsigned pos
, const char *s
)
766 map
= isl_map_cow(map
);
770 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
774 for (i
= 0; i
< map
->n
; ++i
) {
775 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
786 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
787 __isl_take isl_basic_set
*bset
,
788 enum isl_dim_type type
, unsigned pos
, const char *s
)
790 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
794 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
795 enum isl_dim_type type
, unsigned pos
, const char *s
)
797 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
801 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
802 enum isl_dim_type type
, unsigned pos
)
805 return isl_bool_error
;
806 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
809 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
810 enum isl_dim_type type
, unsigned pos
)
812 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
815 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
816 enum isl_dim_type type
, unsigned pos
)
818 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
821 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
822 enum isl_dim_type type
, unsigned pos
)
824 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
827 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
828 enum isl_dim_type type
, unsigned pos
)
830 return isl_map_has_dim_id(set
, type
, pos
);
833 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
834 enum isl_dim_type type
, unsigned pos
)
836 return isl_map_get_dim_id(set
, type
, pos
);
839 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
840 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
842 map
= isl_map_cow(map
);
846 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
848 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
854 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
855 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
857 return isl_map_set_dim_id(set
, type
, pos
, id
);
860 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
861 __isl_keep isl_id
*id
)
865 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
868 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
869 __isl_keep isl_id
*id
)
871 return isl_map_find_dim_by_id(set
, type
, id
);
874 /* Return the position of the dimension of the given type and name
876 * Return -1 if no such dimension can be found.
878 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
879 enum isl_dim_type type
, const char *name
)
883 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
886 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
891 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
894 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
897 return isl_map_find_dim_by_name(set
, type
, name
);
900 /* Check whether equality i of bset is a pure stride constraint
901 * on a single dimension, i.e., of the form
905 * with k a constant and e an existentially quantified variable.
907 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
916 return isl_bool_error
;
918 if (!isl_int_is_zero(bset
->eq
[i
][0]))
919 return isl_bool_false
;
921 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
922 d
= isl_basic_set_dim(bset
, isl_dim_set
);
923 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
925 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
926 return isl_bool_false
;
927 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
929 return isl_bool_false
;
930 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
932 return isl_bool_false
;
934 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
936 return isl_bool_false
;
937 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
938 n_div
- pos2
- 1) != -1)
939 return isl_bool_false
;
940 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
941 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
942 return isl_bool_false
;
944 return isl_bool_true
;
947 /* Reset the user pointer on all identifiers of parameters and tuples
948 * of the space of "map".
950 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
954 space
= isl_map_get_space(map
);
955 space
= isl_space_reset_user(space
);
956 map
= isl_map_reset_space(map
, space
);
961 /* Reset the user pointer on all identifiers of parameters and tuples
962 * of the space of "set".
964 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
966 return isl_map_reset_user(set
);
969 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
972 return isl_bool_error
;
973 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
976 /* Has "map" been marked as a rational map?
977 * In particular, have all basic maps in "map" been marked this way?
978 * An empty map is not considered to be rational.
979 * Maps where only some of the basic maps are marked rational
982 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
988 return isl_bool_error
;
990 return isl_bool_false
;
991 rational
= isl_basic_map_is_rational(map
->p
[0]);
994 for (i
= 1; i
< map
->n
; ++i
) {
997 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1000 if (rational
!= rational_i
)
1001 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1002 "mixed rational and integer basic maps "
1003 "not supported", return isl_bool_error
);
1009 /* Has "set" been marked as a rational set?
1010 * In particular, have all basic set in "set" been marked this way?
1011 * An empty set is not considered to be rational.
1012 * Sets where only some of the basic sets are marked rational
1015 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1017 return isl_map_is_rational(set
);
1020 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1022 return isl_basic_map_is_rational(bset
);
1025 /* Does "bmap" contain any rational points?
1027 * If "bmap" has an equality for each dimension, equating the dimension
1028 * to an integer constant, then it has no rational points, even if it
1029 * is marked as rational.
1031 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1033 isl_bool has_rational
= isl_bool_true
;
1037 return isl_bool_error
;
1038 if (isl_basic_map_plain_is_empty(bmap
))
1039 return isl_bool_false
;
1040 if (!isl_basic_map_is_rational(bmap
))
1041 return isl_bool_false
;
1042 bmap
= isl_basic_map_copy(bmap
);
1043 bmap
= isl_basic_map_implicit_equalities(bmap
);
1045 return isl_bool_error
;
1046 total
= isl_basic_map_total_dim(bmap
);
1047 if (bmap
->n_eq
== total
) {
1049 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1050 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1053 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1054 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1056 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1061 if (i
== bmap
->n_eq
)
1062 has_rational
= isl_bool_false
;
1064 isl_basic_map_free(bmap
);
1066 return has_rational
;
1069 /* Does "map" contain any rational points?
1071 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1074 isl_bool has_rational
;
1077 return isl_bool_error
;
1078 for (i
= 0; i
< map
->n
; ++i
) {
1079 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1080 if (has_rational
< 0 || has_rational
)
1081 return has_rational
;
1083 return isl_bool_false
;
1086 /* Does "set" contain any rational points?
1088 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1090 return isl_map_has_rational(set
);
1093 /* Is this basic set a parameter domain?
1095 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1098 return isl_bool_error
;
1099 return isl_space_is_params(bset
->dim
);
1102 /* Is this set a parameter domain?
1104 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1107 return isl_bool_error
;
1108 return isl_space_is_params(set
->dim
);
1111 /* Is this map actually a parameter domain?
1112 * Users should never call this function. Outside of isl,
1113 * a map can never be a parameter domain.
1115 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1118 return isl_bool_error
;
1119 return isl_space_is_params(map
->dim
);
1122 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1123 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1124 unsigned n_eq
, unsigned n_ineq
)
1127 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1132 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1133 if (isl_blk_is_error(bmap
->block
))
1136 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1137 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1141 bmap
->block2
= isl_blk_empty();
1144 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1145 if (isl_blk_is_error(bmap
->block2
))
1148 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1153 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1154 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1156 for (i
= 0; i
< extra
; ++i
)
1157 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1161 bmap
->c_size
= n_eq
+ n_ineq
;
1162 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1163 bmap
->extra
= extra
;
1167 bmap
->sample
= NULL
;
1171 isl_basic_map_free(bmap
);
1175 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1176 unsigned nparam
, unsigned dim
, unsigned extra
,
1177 unsigned n_eq
, unsigned n_ineq
)
1179 struct isl_basic_map
*bmap
;
1182 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1186 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1187 return bset_from_bmap(bmap
);
1190 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1191 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1193 struct isl_basic_map
*bmap
;
1196 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1197 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1198 return bset_from_bmap(bmap
);
1200 isl_space_free(dim
);
1204 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1205 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1207 struct isl_basic_map
*bmap
;
1211 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1216 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1218 isl_space_free(space
);
1222 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1223 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1224 unsigned n_eq
, unsigned n_ineq
)
1226 struct isl_basic_map
*bmap
;
1229 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1233 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1237 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1238 __isl_keep isl_basic_map
*src
)
1241 unsigned total
= isl_basic_map_total_dim(src
);
1246 for (i
= 0; i
< src
->n_eq
; ++i
) {
1247 int j
= isl_basic_map_alloc_equality(dst
);
1249 return isl_basic_map_free(dst
);
1250 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1253 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1254 int j
= isl_basic_map_alloc_inequality(dst
);
1256 return isl_basic_map_free(dst
);
1257 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1260 for (i
= 0; i
< src
->n_div
; ++i
) {
1261 int j
= isl_basic_map_alloc_div(dst
);
1263 return isl_basic_map_free(dst
);
1264 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1266 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1270 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1272 struct isl_basic_map
*dup
;
1276 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1277 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1278 dup
= dup_constraints(dup
, bmap
);
1281 dup
->flags
= bmap
->flags
;
1282 dup
->sample
= isl_vec_copy(bmap
->sample
);
1286 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1288 struct isl_basic_map
*dup
;
1290 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1291 return bset_from_bmap(dup
);
1294 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1299 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1303 return isl_basic_set_dup(bset
);
1306 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1315 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1320 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1324 bmap
= isl_basic_map_dup(bmap
);
1326 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1330 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1339 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1344 if (--bmap
->ref
> 0)
1347 isl_ctx_deref(bmap
->ctx
);
1349 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1351 isl_blk_free(bmap
->ctx
, bmap
->block
);
1352 isl_vec_free(bmap
->sample
);
1353 isl_space_free(bmap
->dim
);
1359 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1361 return isl_basic_map_free(bset_to_bmap(bset
));
1364 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1366 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1369 /* Check that "map" has only named parameters, reporting an error
1372 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1374 return isl_space_check_named_params(isl_map_peek_space(map
));
1377 /* Check that "bmap" has only named parameters, reporting an error
1380 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1382 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1385 /* Check that "bmap1" and "bmap2" have the same parameters,
1386 * reporting an error if they do not.
1388 static isl_stat
isl_basic_map_check_equal_params(
1389 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1393 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1395 return isl_stat_error
;
1397 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1398 "parameters don't match", return isl_stat_error
);
1402 __isl_give isl_map
*isl_map_align_params_map_map_and(
1403 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1404 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1405 __isl_take isl_map
*map2
))
1409 if (isl_map_has_equal_params(map1
, map2
))
1410 return fn(map1
, map2
);
1411 if (isl_map_check_named_params(map1
) < 0)
1413 if (isl_map_check_named_params(map2
) < 0)
1415 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1416 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1417 return fn(map1
, map2
);
1424 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1425 __isl_keep isl_map
*map2
,
1426 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1431 return isl_bool_error
;
1432 if (isl_map_has_equal_params(map1
, map2
))
1433 return fn(map1
, map2
);
1434 if (isl_map_check_named_params(map1
) < 0)
1435 return isl_bool_error
;
1436 if (isl_map_check_named_params(map2
) < 0)
1437 return isl_bool_error
;
1438 map1
= isl_map_copy(map1
);
1439 map2
= isl_map_copy(map2
);
1440 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1441 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1448 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1450 struct isl_ctx
*ctx
;
1454 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1455 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1457 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1458 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1459 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1460 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1461 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1463 int j
= isl_basic_map_alloc_inequality(bmap
);
1467 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1468 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1475 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1476 bmap
->extra
- bmap
->n_div
);
1477 return bmap
->n_eq
++;
1480 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1482 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1485 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1489 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1494 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1496 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1499 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1504 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1506 if (pos
!= bmap
->n_eq
- 1) {
1508 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1509 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1515 /* Turn inequality "pos" of "bmap" into an equality.
1517 * In particular, we move the inequality in front of the equalities
1518 * and move the last inequality in the position of the moved inequality.
1519 * Note that isl_tab_make_equalities_explicit depends on this particular
1520 * change in the ordering of the constraints.
1522 void isl_basic_map_inequality_to_equality(
1523 struct isl_basic_map
*bmap
, unsigned pos
)
1527 t
= bmap
->ineq
[pos
];
1528 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1529 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1534 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1535 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1536 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1537 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1540 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1542 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1545 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1547 struct isl_ctx
*ctx
;
1551 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1552 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1553 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1554 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1555 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1556 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1557 1 + isl_basic_map_total_dim(bmap
),
1558 bmap
->extra
- bmap
->n_div
);
1559 return bmap
->n_ineq
++;
1562 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1564 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1567 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1571 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1576 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1578 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1581 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1586 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1588 if (pos
!= bmap
->n_ineq
- 1) {
1589 t
= bmap
->ineq
[pos
];
1590 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1591 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1598 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1600 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1603 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1609 empty
= isl_basic_map_plain_is_empty(bmap
);
1611 return isl_basic_map_free(bmap
);
1615 bmap
= isl_basic_map_cow(bmap
);
1616 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1619 k
= isl_basic_map_alloc_equality(bmap
);
1622 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1625 isl_basic_map_free(bmap
);
1629 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1632 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1635 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1640 bmap
= isl_basic_map_cow(bmap
);
1641 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1644 k
= isl_basic_map_alloc_inequality(bmap
);
1647 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1650 isl_basic_map_free(bmap
);
1654 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1657 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1660 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1664 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1665 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1666 1 + 1 + isl_basic_map_total_dim(bmap
),
1667 bmap
->extra
- bmap
->n_div
);
1668 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1669 return bmap
->n_div
++;
1672 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1674 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1677 /* Check that there are "n" dimensions of type "type" starting at "first"
1680 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1681 enum isl_dim_type type
, unsigned first
, unsigned n
)
1686 return isl_stat_error
;
1687 dim
= isl_basic_map_dim(bmap
, type
);
1688 if (first
+ n
> dim
|| first
+ n
< first
)
1689 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1690 "position or range out of bounds",
1691 return isl_stat_error
);
1695 /* Insert an extra integer division, prescribed by "div", to "bmap"
1696 * at (integer division) position "pos".
1698 * The integer division is first added at the end and then moved
1699 * into the right position.
1701 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1702 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1706 bmap
= isl_basic_map_cow(bmap
);
1708 return isl_basic_map_free(bmap
);
1710 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1711 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1712 "unexpected size", return isl_basic_map_free(bmap
));
1713 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1714 return isl_basic_map_free(bmap
);
1716 bmap
= isl_basic_map_extend_space(bmap
,
1717 isl_basic_map_get_space(bmap
), 1, 0, 2);
1718 k
= isl_basic_map_alloc_div(bmap
);
1720 return isl_basic_map_free(bmap
);
1721 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1722 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1724 for (i
= k
; i
> pos
; --i
)
1725 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1730 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1733 return isl_stat_error
;
1734 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1739 static __isl_give isl_basic_map
*add_constraints(
1740 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1741 unsigned i_pos
, unsigned o_pos
)
1743 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1746 struct isl_dim_map
*dim_map
;
1748 space
= isl_basic_map_peek_space(bmap2
);
1749 if (!bmap1
|| !space
)
1752 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1753 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1754 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1755 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1756 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1757 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1758 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1759 ctx
= isl_basic_map_get_ctx(bmap1
);
1760 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1761 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1762 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1763 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1764 isl_dim_map_div(dim_map
, bmap2
, total
);
1766 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1768 isl_basic_map_free(bmap1
);
1769 isl_basic_map_free(bmap2
);
1773 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1774 struct isl_basic_set
*bset2
, unsigned pos
)
1776 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1777 bset_to_bmap(bset2
), 0, pos
));
1780 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1781 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1782 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1784 struct isl_basic_map
*ext
;
1794 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1795 base
->extra
>= base
->n_div
+ extra
;
1797 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1798 room_for_ineq(base
, n_ineq
)) {
1799 isl_space_free(space
);
1803 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1804 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1805 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1806 extra
+= base
->extra
;
1808 n_ineq
+= base
->n_ineq
;
1810 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1816 ext
->sample
= isl_vec_copy(base
->sample
);
1817 flags
= base
->flags
;
1818 ext
= add_constraints(ext
, base
, 0, 0);
1821 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1827 isl_space_free(space
);
1828 isl_basic_map_free(base
);
1832 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1833 __isl_take isl_basic_set
*base
,
1834 __isl_take isl_space
*dim
, unsigned extra
,
1835 unsigned n_eq
, unsigned n_ineq
)
1837 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1838 dim
, extra
, n_eq
, n_ineq
));
1841 struct isl_basic_map
*isl_basic_map_extend_constraints(
1842 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1846 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1850 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1851 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1852 unsigned n_eq
, unsigned n_ineq
)
1854 struct isl_basic_map
*bmap
;
1859 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1863 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1866 isl_basic_map_free(base
);
1870 struct isl_basic_set
*isl_basic_set_extend_constraints(
1871 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1873 isl_basic_map
*bmap
= bset_to_bmap(base
);
1874 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1875 return bset_from_bmap(bmap
);
1878 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1880 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1883 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1888 if (bmap
->ref
> 1) {
1890 bmap
= isl_basic_map_dup(bmap
);
1893 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1894 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1899 /* Clear all cached information in "map", either because it is about
1900 * to be modified or because it is being freed.
1901 * Always return the same pointer that is passed in.
1902 * This is needed for the use in isl_map_free.
1904 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1906 isl_basic_map_free(map
->cached_simple_hull
[0]);
1907 isl_basic_map_free(map
->cached_simple_hull
[1]);
1908 map
->cached_simple_hull
[0] = NULL
;
1909 map
->cached_simple_hull
[1] = NULL
;
1913 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1915 return isl_map_cow(set
);
1918 /* Return an isl_map that is equal to "map" and that has only
1919 * a single reference.
1921 * If the original input already has only one reference, then
1922 * simply return it, but clear all cached information, since
1923 * it may be rendered invalid by the operations that will be
1924 * performed on the result.
1926 * Otherwise, create a duplicate (without any cached information).
1928 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1934 return clear_caches(map
);
1936 return isl_map_dup(map
);
1939 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1940 unsigned a_len
, unsigned b_len
)
1942 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1943 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1944 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1947 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1948 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1956 isl_assert(bmap
->ctx
,
1957 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1959 if (n1
== 0 || n2
== 0)
1962 bmap
= isl_basic_map_cow(bmap
);
1966 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1967 if (isl_blk_is_error(blk
))
1970 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1972 bmap
->eq
[i
] + pos
, n1
, n2
);
1974 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1976 bmap
->ineq
[i
] + pos
, n1
, n2
);
1978 for (i
= 0; i
< bmap
->n_div
; ++i
)
1980 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1982 isl_blk_free(bmap
->ctx
, blk
);
1984 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
1985 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1986 return isl_basic_map_finalize(bmap
);
1988 isl_basic_map_free(bmap
);
1992 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
1993 __isl_take isl_basic_map
*bmap
)
1999 total
= isl_basic_map_total_dim(bmap
);
2000 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2001 return isl_basic_map_free(bmap
);
2002 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2004 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2006 i
= isl_basic_map_alloc_equality(bmap
);
2010 isl_int_set_si(bmap
->eq
[i
][0], 1);
2011 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2012 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2013 isl_vec_free(bmap
->sample
);
2014 bmap
->sample
= NULL
;
2015 return isl_basic_map_finalize(bmap
);
2017 isl_basic_map_free(bmap
);
2021 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2022 __isl_take isl_basic_set
*bset
)
2024 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2027 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2028 __isl_take isl_basic_map
*bmap
)
2033 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2036 bmap
= isl_basic_map_cow(bmap
);
2040 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2042 return isl_basic_map_finalize(bmap
);
2045 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2046 __isl_take isl_basic_set
*bset
)
2048 return isl_basic_map_set_rational(bset
);
2051 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2052 __isl_take isl_basic_set
*bset
)
2057 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2060 bset
= isl_basic_set_cow(bset
);
2064 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2066 return isl_basic_set_finalize(bset
);
2069 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2073 map
= isl_map_cow(map
);
2076 for (i
= 0; i
< map
->n
; ++i
) {
2077 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2087 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2089 return isl_map_set_rational(set
);
2092 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2095 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2097 isl_int
*t
= bmap
->div
[a
];
2098 bmap
->div
[a
] = bmap
->div
[b
];
2102 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2103 * div definitions accordingly.
2105 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2108 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2110 swap_div(bmap
, a
, b
);
2112 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2113 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2115 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2116 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2118 for (i
= 0; i
< bmap
->n_div
; ++i
)
2119 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2120 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2123 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2125 isl_seq_cpy(c
, c
+ n
, rem
);
2126 isl_seq_clr(c
+ rem
, n
);
2129 /* Drop n dimensions starting at first.
2131 * In principle, this frees up some extra variables as the number
2132 * of columns remains constant, but we would have to extend
2133 * the div array too as the number of rows in this array is assumed
2134 * to be equal to extra.
2136 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2137 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2139 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2142 /* Move "n" divs starting at "first" to the end of the list of divs.
2144 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2145 unsigned first
, unsigned n
)
2150 if (first
+ n
== bmap
->n_div
)
2153 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2156 for (i
= 0; i
< n
; ++i
)
2157 div
[i
] = bmap
->div
[first
+ i
];
2158 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2159 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2160 for (i
= 0; i
< n
; ++i
)
2161 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2165 isl_basic_map_free(bmap
);
2169 /* Check that there are "n" dimensions of type "type" starting at "first"
2172 static isl_stat
isl_map_check_range(__isl_keep isl_map
*map
,
2173 enum isl_dim_type type
, unsigned first
, unsigned n
)
2176 return isl_stat_error
;
2177 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2178 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2179 "position or range out of bounds",
2180 return isl_stat_error
);
2184 /* Drop "n" dimensions of type "type" starting at "first".
2186 * In principle, this frees up some extra variables as the number
2187 * of columns remains constant, but we would have to extend
2188 * the div array too as the number of rows in this array is assumed
2189 * to be equal to extra.
2191 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2192 enum isl_dim_type type
, unsigned first
, unsigned n
)
2202 dim
= isl_basic_map_dim(bmap
, type
);
2203 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
2205 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2208 bmap
= isl_basic_map_cow(bmap
);
2212 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2213 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2214 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2215 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2217 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2218 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2220 for (i
= 0; i
< bmap
->n_div
; ++i
)
2221 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2223 if (type
== isl_dim_div
) {
2224 bmap
= move_divs_last(bmap
, first
, n
);
2227 if (isl_basic_map_free_div(bmap
, n
) < 0)
2228 return isl_basic_map_free(bmap
);
2230 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2234 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2235 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2236 bmap
= isl_basic_map_simplify(bmap
);
2237 return isl_basic_map_finalize(bmap
);
2239 isl_basic_map_free(bmap
);
2243 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2244 enum isl_dim_type type
, unsigned first
, unsigned n
)
2246 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2250 /* No longer consider "map" to be normalized.
2252 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2256 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2260 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2261 enum isl_dim_type type
, unsigned first
, unsigned n
)
2265 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2266 return isl_map_free(map
);
2268 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2270 map
= isl_map_cow(map
);
2273 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2277 for (i
= 0; i
< map
->n
; ++i
) {
2278 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2282 map
= isl_map_unmark_normalized(map
);
2290 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2291 enum isl_dim_type type
, unsigned first
, unsigned n
)
2293 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2297 * We don't cow, as the div is assumed to be redundant.
2299 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2300 __isl_take isl_basic_map
*bmap
, unsigned div
)
2308 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
2310 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
2312 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2313 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2315 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2316 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
2317 isl_basic_map_drop_inequality(bmap
, i
);
2321 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2324 for (i
= 0; i
< bmap
->n_div
; ++i
)
2325 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
2327 if (div
!= bmap
->n_div
- 1) {
2329 isl_int
*t
= bmap
->div
[div
];
2331 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
2332 bmap
->div
[j
] = bmap
->div
[j
+1];
2334 bmap
->div
[bmap
->n_div
- 1] = t
;
2336 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2337 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2338 if (isl_basic_map_free_div(bmap
, 1) < 0)
2339 return isl_basic_map_free(bmap
);
2343 isl_basic_map_free(bmap
);
2347 /* Eliminate the specified n dimensions starting at first from the
2348 * constraints, without removing the dimensions from the space.
2349 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2351 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2352 enum isl_dim_type type
, unsigned first
, unsigned n
)
2359 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2360 return isl_map_free(map
);
2362 map
= isl_map_cow(map
);
2366 for (i
= 0; i
< map
->n
; ++i
) {
2367 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2377 /* Eliminate the specified n dimensions starting at first from the
2378 * constraints, without removing the dimensions from the space.
2379 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2381 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2382 enum isl_dim_type type
, unsigned first
, unsigned n
)
2384 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2387 /* Eliminate the specified n dimensions starting at first from the
2388 * constraints, without removing the dimensions from the space.
2389 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2391 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2392 unsigned first
, unsigned n
)
2394 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2397 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2398 __isl_take isl_basic_map
*bmap
)
2402 bmap
= isl_basic_map_eliminate_vars(bmap
,
2403 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2407 return isl_basic_map_finalize(bmap
);
2410 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2411 __isl_take isl_basic_set
*bset
)
2413 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2416 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2425 map
= isl_map_cow(map
);
2429 for (i
= 0; i
< map
->n
; ++i
) {
2430 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2440 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2442 return isl_map_remove_divs(set
);
2445 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2446 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2447 unsigned first
, unsigned n
)
2449 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2450 return isl_basic_map_free(bmap
);
2451 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2453 bmap
= isl_basic_map_eliminate_vars(bmap
,
2454 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2457 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2459 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2463 /* Return true if the definition of the given div (recursively) involves
2464 * any of the given variables.
2466 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2467 unsigned first
, unsigned n
)
2470 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2472 if (isl_int_is_zero(bmap
->div
[div
][0]))
2473 return isl_bool_false
;
2474 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2475 return isl_bool_true
;
2477 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2480 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2482 involves
= div_involves_vars(bmap
, i
, first
, n
);
2483 if (involves
< 0 || involves
)
2487 return isl_bool_false
;
2490 /* Try and add a lower and/or upper bound on "div" to "bmap"
2491 * based on inequality "i".
2492 * "total" is the total number of variables (excluding the divs).
2493 * "v" is a temporary object that can be used during the calculations.
2494 * If "lb" is set, then a lower bound should be constructed.
2495 * If "ub" is set, then an upper bound should be constructed.
2497 * The calling function has already checked that the inequality does not
2498 * reference "div", but we still need to check that the inequality is
2499 * of the right form. We'll consider the case where we want to construct
2500 * a lower bound. The construction of upper bounds is similar.
2502 * Let "div" be of the form
2504 * q = floor((a + f(x))/d)
2506 * We essentially check if constraint "i" is of the form
2510 * so that we can use it to derive a lower bound on "div".
2511 * However, we allow a slightly more general form
2515 * with the condition that the coefficients of g(x) - f(x) are all
2517 * Rewriting this constraint as
2521 * adding a + f(x) to both sides and dividing by d, we obtain
2523 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2525 * Taking the floor on both sides, we obtain
2527 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2531 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2533 * In the case of an upper bound, we construct the constraint
2535 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2538 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2539 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2540 unsigned total
, isl_int v
, int lb
, int ub
)
2544 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2546 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2547 bmap
->div
[div
][1 + 1 + j
]);
2548 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2551 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2552 bmap
->div
[div
][1 + 1 + j
]);
2553 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2559 bmap
= isl_basic_map_cow(bmap
);
2560 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2562 int k
= isl_basic_map_alloc_inequality(bmap
);
2565 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2566 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2567 bmap
->div
[div
][1 + j
]);
2568 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2569 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2571 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2574 int k
= isl_basic_map_alloc_inequality(bmap
);
2577 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2578 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2579 bmap
->div
[div
][1 + j
]);
2580 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2581 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2583 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2588 isl_basic_map_free(bmap
);
2592 /* This function is called right before "div" is eliminated from "bmap"
2593 * using Fourier-Motzkin.
2594 * Look through the constraints of "bmap" for constraints on the argument
2595 * of the integer division and use them to construct constraints on the
2596 * integer division itself. These constraints can then be combined
2597 * during the Fourier-Motzkin elimination.
2598 * Note that it is only useful to introduce lower bounds on "div"
2599 * if "bmap" already contains upper bounds on "div" as the newly
2600 * introduce lower bounds can then be combined with the pre-existing
2601 * upper bounds. Similarly for upper bounds.
2602 * We therefore first check if "bmap" contains any lower and/or upper bounds
2605 * It is interesting to note that the introduction of these constraints
2606 * can indeed lead to more accurate results, even when compared to
2607 * deriving constraints on the argument of "div" from constraints on "div".
2608 * Consider, for example, the set
2610 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2612 * The second constraint can be rewritten as
2614 * 2 * [(-i-2j+3)/4] + k >= 0
2616 * from which we can derive
2618 * -i - 2j + 3 >= -2k
2624 * Combined with the first constraint, we obtain
2626 * -3 <= 3 + 2k or k >= -3
2628 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2629 * the first constraint, we obtain
2631 * [(i + 2j)/4] >= [-3/4] = -1
2633 * Combining this constraint with the second constraint, we obtain
2637 static __isl_give isl_basic_map
*insert_bounds_on_div(
2638 __isl_take isl_basic_map
*bmap
, int div
)
2641 int check_lb
, check_ub
;
2648 if (isl_int_is_zero(bmap
->div
[div
][0]))
2651 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2655 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2656 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2663 if (!check_lb
&& !check_ub
)
2668 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2669 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2672 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2673 check_lb
, check_ub
);
2681 /* Remove all divs (recursively) involving any of the given dimensions
2682 * in their definitions.
2684 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2685 __isl_take isl_basic_map
*bmap
,
2686 enum isl_dim_type type
, unsigned first
, unsigned n
)
2690 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2691 return isl_basic_map_free(bmap
);
2692 first
+= isl_basic_map_offset(bmap
, type
);
2694 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2697 involves
= div_involves_vars(bmap
, i
, first
, n
);
2699 return isl_basic_map_free(bmap
);
2702 bmap
= insert_bounds_on_div(bmap
, i
);
2703 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2712 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2713 __isl_take isl_basic_set
*bset
,
2714 enum isl_dim_type type
, unsigned first
, unsigned n
)
2716 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2719 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2720 enum isl_dim_type type
, unsigned first
, unsigned n
)
2729 map
= isl_map_cow(map
);
2733 for (i
= 0; i
< map
->n
; ++i
) {
2734 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2745 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2746 enum isl_dim_type type
, unsigned first
, unsigned n
)
2748 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2752 /* Does the description of "bmap" depend on the specified dimensions?
2753 * We also check whether the dimensions appear in any of the div definitions.
2754 * In principle there is no need for this check. If the dimensions appear
2755 * in a div definition, they also appear in the defining constraints of that
2758 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2759 enum isl_dim_type type
, unsigned first
, unsigned n
)
2763 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2764 return isl_bool_error
;
2766 first
+= isl_basic_map_offset(bmap
, type
);
2767 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2768 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2769 return isl_bool_true
;
2770 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2771 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2772 return isl_bool_true
;
2773 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2774 if (isl_int_is_zero(bmap
->div
[i
][0]))
2776 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2777 return isl_bool_true
;
2780 return isl_bool_false
;
2783 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2784 enum isl_dim_type type
, unsigned first
, unsigned n
)
2788 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2789 return isl_bool_error
;
2791 for (i
= 0; i
< map
->n
; ++i
) {
2792 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2794 if (involves
< 0 || involves
)
2798 return isl_bool_false
;
2801 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2802 enum isl_dim_type type
, unsigned first
, unsigned n
)
2804 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2807 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2808 enum isl_dim_type type
, unsigned first
, unsigned n
)
2810 return isl_map_involves_dims(set
, type
, first
, n
);
2813 /* Drop all constraints in bmap that involve any of the dimensions
2814 * first to first+n-1.
2816 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2817 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2824 bmap
= isl_basic_map_cow(bmap
);
2829 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2830 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2832 isl_basic_map_drop_equality(bmap
, i
);
2835 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2836 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2838 isl_basic_map_drop_inequality(bmap
, i
);
2841 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2845 /* Drop all constraints in bset that involve any of the dimensions
2846 * first to first+n-1.
2848 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2849 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2851 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2854 /* Drop all constraints in bmap that do not involve any of the dimensions
2855 * first to first + n - 1 of the given type.
2857 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2858 __isl_take isl_basic_map
*bmap
,
2859 enum isl_dim_type type
, unsigned first
, unsigned n
)
2864 isl_space
*space
= isl_basic_map_get_space(bmap
);
2865 isl_basic_map_free(bmap
);
2866 return isl_basic_map_universe(space
);
2868 bmap
= isl_basic_map_cow(bmap
);
2872 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2873 return isl_basic_map_free(bmap
);
2875 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2877 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2878 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2880 isl_basic_map_drop_equality(bmap
, i
);
2883 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2884 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2886 isl_basic_map_drop_inequality(bmap
, i
);
2889 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2893 /* Drop all constraints in bset that do not involve any of the dimensions
2894 * first to first + n - 1 of the given type.
2896 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2897 __isl_take isl_basic_set
*bset
,
2898 enum isl_dim_type type
, unsigned first
, unsigned n
)
2900 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2904 /* Drop all constraints in bmap that involve any of the dimensions
2905 * first to first + n - 1 of the given type.
2907 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2908 __isl_take isl_basic_map
*bmap
,
2909 enum isl_dim_type type
, unsigned first
, unsigned n
)
2916 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2917 return isl_basic_map_free(bmap
);
2919 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2920 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2921 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2924 /* Drop all constraints in bset that involve any of the dimensions
2925 * first to first + n - 1 of the given type.
2927 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2928 __isl_take isl_basic_set
*bset
,
2929 enum isl_dim_type type
, unsigned first
, unsigned n
)
2931 return isl_basic_map_drop_constraints_involving_dims(bset
,
2935 /* Drop constraints from "map" by applying "drop" to each basic map.
2937 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2938 enum isl_dim_type type
, unsigned first
, unsigned n
,
2939 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2940 enum isl_dim_type type
, unsigned first
, unsigned n
))
2944 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2945 return isl_map_free(map
);
2947 map
= isl_map_cow(map
);
2951 for (i
= 0; i
< map
->n
; ++i
) {
2952 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2954 return isl_map_free(map
);
2958 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2963 /* Drop all constraints in map that involve any of the dimensions
2964 * first to first + n - 1 of the given type.
2966 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2967 __isl_take isl_map
*map
,
2968 enum isl_dim_type type
, unsigned first
, unsigned n
)
2972 return drop_constraints(map
, type
, first
, n
,
2973 &isl_basic_map_drop_constraints_involving_dims
);
2976 /* Drop all constraints in "map" that do not involve any of the dimensions
2977 * first to first + n - 1 of the given type.
2979 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2980 __isl_take isl_map
*map
,
2981 enum isl_dim_type type
, unsigned first
, unsigned n
)
2984 isl_space
*space
= isl_map_get_space(map
);
2986 return isl_map_universe(space
);
2988 return drop_constraints(map
, type
, first
, n
,
2989 &isl_basic_map_drop_constraints_not_involving_dims
);
2992 /* Drop all constraints in set that involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2995 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2996 __isl_take isl_set
*set
,
2997 enum isl_dim_type type
, unsigned first
, unsigned n
)
2999 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3002 /* Drop all constraints in "set" that do not involve any of the dimensions
3003 * first to first + n - 1 of the given type.
3005 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3006 __isl_take isl_set
*set
,
3007 enum isl_dim_type type
, unsigned first
, unsigned n
)
3009 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3012 /* Does local variable "div" of "bmap" have a complete explicit representation?
3013 * Having a complete explicit representation requires not only
3014 * an explicit representation, but also that all local variables
3015 * that appear in this explicit representation in turn have
3016 * a complete explicit representation.
3018 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3021 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3024 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3025 if (marked
< 0 || marked
)
3026 return isl_bool_not(marked
);
3028 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3031 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3033 known
= isl_basic_map_div_is_known(bmap
, i
);
3034 if (known
< 0 || !known
)
3038 return isl_bool_true
;
3041 /* Remove all divs that are unknown or defined in terms of unknown divs.
3043 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3044 __isl_take isl_basic_map
*bmap
)
3051 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3052 if (isl_basic_map_div_is_known(bmap
, i
))
3054 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3063 /* Remove all divs that are unknown or defined in terms of unknown divs.
3065 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3066 __isl_take isl_basic_set
*bset
)
3068 return isl_basic_map_remove_unknown_divs(bset
);
3071 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3080 map
= isl_map_cow(map
);
3084 for (i
= 0; i
< map
->n
; ++i
) {
3085 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3095 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3097 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3100 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3101 __isl_take isl_basic_set
*bset
,
3102 enum isl_dim_type type
, unsigned first
, unsigned n
)
3104 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3105 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3106 return bset_from_bmap(bmap
);
3109 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3110 enum isl_dim_type type
, unsigned first
, unsigned n
)
3117 map
= isl_map_cow(map
);
3118 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3119 return isl_map_free(map
);
3121 for (i
= 0; i
< map
->n
; ++i
) {
3122 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3123 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3127 map
= isl_map_drop(map
, type
, first
, n
);
3134 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3135 enum isl_dim_type type
, unsigned first
, unsigned n
)
3137 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3141 /* Project out n inputs starting at first using Fourier-Motzkin */
3142 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3143 unsigned first
, unsigned n
)
3145 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3148 static void dump_term(struct isl_basic_map
*bmap
,
3149 isl_int c
, int pos
, FILE *out
)
3152 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3153 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3154 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3156 isl_int_print(out
, c
, 0);
3158 if (!isl_int_is_one(c
))
3159 isl_int_print(out
, c
, 0);
3160 if (pos
< 1 + nparam
) {
3161 name
= isl_space_get_dim_name(bmap
->dim
,
3162 isl_dim_param
, pos
- 1);
3164 fprintf(out
, "%s", name
);
3166 fprintf(out
, "p%d", pos
- 1);
3167 } else if (pos
< 1 + nparam
+ in
)
3168 fprintf(out
, "i%d", pos
- 1 - nparam
);
3169 else if (pos
< 1 + nparam
+ dim
)
3170 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3172 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3176 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3177 int sign
, FILE *out
)
3181 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3185 for (i
= 0, first
= 1; i
< len
; ++i
) {
3186 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3189 fprintf(out
, " + ");
3191 isl_int_abs(v
, c
[i
]);
3192 dump_term(bmap
, v
, i
, out
);
3199 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3200 const char *op
, FILE *out
, int indent
)
3204 fprintf(out
, "%*s", indent
, "");
3206 dump_constraint_sign(bmap
, c
, 1, out
);
3207 fprintf(out
, " %s ", op
);
3208 dump_constraint_sign(bmap
, c
, -1, out
);
3212 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3213 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3215 fprintf(out
, "%*s", indent
, "");
3216 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3221 static void dump_constraints(struct isl_basic_map
*bmap
,
3222 isl_int
**c
, unsigned n
,
3223 const char *op
, FILE *out
, int indent
)
3227 for (i
= 0; i
< n
; ++i
)
3228 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3231 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3235 unsigned total
= isl_basic_map_total_dim(bmap
);
3237 for (j
= 0; j
< 1 + total
; ++j
) {
3238 if (isl_int_is_zero(exp
[j
]))
3240 if (!first
&& isl_int_is_pos(exp
[j
]))
3242 dump_term(bmap
, exp
[j
], j
, out
);
3247 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3251 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3252 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3254 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3255 fprintf(out
, "%*s", indent
, "");
3256 fprintf(out
, "e%d = [(", i
);
3257 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3259 isl_int_print(out
, bmap
->div
[i
][0], 0);
3260 fprintf(out
, "]\n");
3264 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3265 FILE *out
, int indent
)
3268 fprintf(out
, "null basic set\n");
3272 fprintf(out
, "%*s", indent
, "");
3273 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3274 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3275 bset
->extra
, bset
->flags
);
3276 dump(bset_to_bmap(bset
), out
, indent
);
3279 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3280 FILE *out
, int indent
)
3283 fprintf(out
, "null basic map\n");
3287 fprintf(out
, "%*s", indent
, "");
3288 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3289 "flags: %x, n_name: %d\n",
3291 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3292 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3293 dump(bmap
, out
, indent
);
3296 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3301 total
= isl_basic_map_total_dim(bmap
);
3302 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3303 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3304 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3305 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3306 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3310 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3313 if (isl_space_check_is_set(space
) < 0)
3315 return isl_map_alloc_space(space
, n
, flags
);
3317 isl_space_free(space
);
3321 /* Make sure "map" has room for at least "n" more basic maps.
3323 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3326 struct isl_map
*grown
= NULL
;
3330 isl_assert(map
->ctx
, n
>= 0, goto error
);
3331 if (map
->n
+ n
<= map
->size
)
3333 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3336 for (i
= 0; i
< map
->n
; ++i
) {
3337 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3345 isl_map_free(grown
);
3350 /* Make sure "set" has room for at least "n" more basic sets.
3352 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3354 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3357 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3359 return isl_map_from_basic_map(bset
);
3362 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3364 struct isl_map
*map
;
3369 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3370 return isl_map_add_basic_map(map
, bmap
);
3373 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3374 __isl_take isl_basic_set
*bset
)
3376 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3377 bset_to_bmap(bset
)));
3380 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3382 return isl_map_free(set
);
3385 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3390 fprintf(out
, "null set\n");
3394 fprintf(out
, "%*s", indent
, "");
3395 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3396 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3398 for (i
= 0; i
< set
->n
; ++i
) {
3399 fprintf(out
, "%*s", indent
, "");
3400 fprintf(out
, "basic set %d:\n", i
);
3401 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3405 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3410 fprintf(out
, "null map\n");
3414 fprintf(out
, "%*s", indent
, "");
3415 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3416 "flags: %x, n_name: %d\n",
3417 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3418 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3419 for (i
= 0; i
< map
->n
; ++i
) {
3420 fprintf(out
, "%*s", indent
, "");
3421 fprintf(out
, "basic map %d:\n", i
);
3422 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3426 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3427 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3429 struct isl_basic_map
*bmap_domain
;
3431 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3434 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3435 isl_assert(bset
->ctx
,
3436 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3438 bmap
= isl_basic_map_cow(bmap
);
3441 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3442 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3443 bmap_domain
= isl_basic_map_from_domain(bset
);
3444 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3446 bmap
= isl_basic_map_simplify(bmap
);
3447 return isl_basic_map_finalize(bmap
);
3449 isl_basic_map_free(bmap
);
3450 isl_basic_set_free(bset
);
3454 /* Check that the space of "bset" is the same as that of the range of "bmap".
3456 static isl_stat
isl_basic_map_check_compatible_range(
3457 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3461 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3463 return isl_stat_error
;
3465 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3466 "incompatible spaces", return isl_stat_error
);
3471 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3472 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3474 struct isl_basic_map
*bmap_range
;
3476 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3479 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3480 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3483 if (isl_basic_set_plain_is_universe(bset
)) {
3484 isl_basic_set_free(bset
);
3488 bmap
= isl_basic_map_cow(bmap
);
3491 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3492 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3493 bmap_range
= bset_to_bmap(bset
);
3494 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3496 bmap
= isl_basic_map_simplify(bmap
);
3497 return isl_basic_map_finalize(bmap
);
3499 isl_basic_map_free(bmap
);
3500 isl_basic_set_free(bset
);
3504 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3505 __isl_keep isl_vec
*vec
)
3512 return isl_bool_error
;
3514 total
= 1 + isl_basic_map_total_dim(bmap
);
3515 if (total
!= vec
->size
)
3516 return isl_bool_false
;
3520 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3521 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3522 if (!isl_int_is_zero(s
)) {
3524 return isl_bool_false
;
3528 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3529 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3530 if (isl_int_is_neg(s
)) {
3532 return isl_bool_false
;
3538 return isl_bool_true
;
3541 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3542 __isl_keep isl_vec
*vec
)
3544 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3547 __isl_give isl_basic_map
*isl_basic_map_intersect(
3548 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3550 struct isl_vec
*sample
= NULL
;
3552 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3554 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3555 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3556 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3557 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3558 return isl_basic_map_intersect(bmap2
, bmap1
);
3560 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3561 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3562 isl_assert(bmap1
->ctx
,
3563 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3565 if (isl_basic_map_plain_is_empty(bmap1
)) {
3566 isl_basic_map_free(bmap2
);
3569 if (isl_basic_map_plain_is_empty(bmap2
)) {
3570 isl_basic_map_free(bmap1
);
3574 if (bmap1
->sample
&&
3575 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3576 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3577 sample
= isl_vec_copy(bmap1
->sample
);
3578 else if (bmap2
->sample
&&
3579 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3580 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3581 sample
= isl_vec_copy(bmap2
->sample
);
3583 bmap1
= isl_basic_map_cow(bmap1
);
3586 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3587 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3588 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3591 isl_vec_free(sample
);
3593 isl_vec_free(bmap1
->sample
);
3594 bmap1
->sample
= sample
;
3597 bmap1
= isl_basic_map_simplify(bmap1
);
3598 return isl_basic_map_finalize(bmap1
);
3601 isl_vec_free(sample
);
3602 isl_basic_map_free(bmap1
);
3603 isl_basic_map_free(bmap2
);
3607 struct isl_basic_set
*isl_basic_set_intersect(
3608 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3610 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3611 bset_to_bmap(bset2
)));
3614 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3615 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3617 return isl_basic_set_intersect(bset1
, bset2
);
3620 /* Special case of isl_map_intersect, where both map1 and map2
3621 * are convex, without any divs and such that either map1 or map2
3622 * contains a single constraint. This constraint is then simply
3623 * added to the other map.
3625 static __isl_give isl_map
*map_intersect_add_constraint(
3626 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3628 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3629 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3630 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3631 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3633 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3634 return isl_map_intersect(map2
, map1
);
3636 map1
= isl_map_cow(map1
);
3639 if (isl_map_plain_is_empty(map1
)) {
3643 if (map2
->p
[0]->n_eq
== 1)
3644 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3646 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3647 map2
->p
[0]->ineq
[0]);
3649 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3650 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3654 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3655 isl_basic_map_free(map1
->p
[0]);
3661 map1
= isl_map_unmark_normalized(map1
);
3669 /* map2 may be either a parameter domain or a map living in the same
3672 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3673 __isl_take isl_map
*map2
)
3683 if ((isl_map_plain_is_empty(map1
) ||
3684 isl_map_plain_is_universe(map2
)) &&
3685 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3689 if ((isl_map_plain_is_empty(map2
) ||
3690 isl_map_plain_is_universe(map1
)) &&
3691 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3696 if (map1
->n
== 1 && map2
->n
== 1 &&
3697 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3698 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3699 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3700 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3701 return map_intersect_add_constraint(map1
, map2
);
3703 equal
= isl_map_plain_is_equal(map1
, map2
);
3711 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3712 isl_space_dim(map2
->dim
, isl_dim_param
))
3713 isl_assert(map1
->ctx
,
3714 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3716 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3717 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3718 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3720 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3721 map1
->n
* map2
->n
, flags
);
3724 for (i
= 0; i
< map1
->n
; ++i
)
3725 for (j
= 0; j
< map2
->n
; ++j
) {
3726 struct isl_basic_map
*part
;
3727 part
= isl_basic_map_intersect(
3728 isl_basic_map_copy(map1
->p
[i
]),
3729 isl_basic_map_copy(map2
->p
[j
]));
3730 if (isl_basic_map_is_empty(part
) < 0)
3731 part
= isl_basic_map_free(part
);
3732 result
= isl_map_add_basic_map(result
, part
);
3745 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3746 __isl_take isl_map
*map2
)
3750 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3751 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3752 "spaces don't match", goto error
);
3753 return map_intersect_internal(map1
, map2
);
3760 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3761 __isl_take isl_map
*map2
)
3763 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3766 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3768 return set_from_map(isl_map_intersect(set_to_map(set1
),
3772 /* map_intersect_internal accepts intersections
3773 * with parameter domains, so we can just call that function.
3775 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3776 __isl_take isl_set
*params
)
3778 return map_intersect_internal(map
, params
);
3781 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3782 __isl_take isl_map
*map2
)
3784 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3787 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3788 __isl_take isl_set
*params
)
3790 return isl_map_intersect_params(set
, params
);
3793 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3796 unsigned pos
, n1
, n2
;
3800 bmap
= isl_basic_map_cow(bmap
);
3803 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3804 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3805 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3806 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3807 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3808 return isl_basic_map_reset_space(bmap
, space
);
3811 static __isl_give isl_basic_map
*basic_map_space_reset(
3812 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3818 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3821 space
= isl_basic_map_get_space(bmap
);
3822 space
= isl_space_reset(space
, type
);
3823 bmap
= isl_basic_map_reset_space(bmap
, space
);
3827 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3828 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3829 unsigned pos
, unsigned n
)
3832 isl_space
*res_space
;
3833 struct isl_basic_map
*res
;
3834 struct isl_dim_map
*dim_map
;
3835 unsigned total
, off
;
3836 enum isl_dim_type t
;
3839 return basic_map_space_reset(bmap
, type
);
3841 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3844 return isl_basic_map_free(bmap
);
3846 total
= isl_basic_map_total_dim(bmap
) + n
;
3847 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3849 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3851 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3853 unsigned size
= isl_basic_map_dim(bmap
, t
);
3854 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3856 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3857 pos
, size
- pos
, off
+ pos
+ n
);
3859 off
+= isl_space_dim(res_space
, t
);
3861 isl_dim_map_div(dim_map
, bmap
, off
);
3863 res
= isl_basic_map_alloc_space(res_space
,
3864 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3865 rational
= isl_basic_map_is_rational(bmap
);
3867 res
= isl_basic_map_free(res
);
3869 res
= isl_basic_map_set_rational(res
);
3870 if (isl_basic_map_plain_is_empty(bmap
)) {
3871 isl_basic_map_free(bmap
);
3873 return isl_basic_map_set_to_empty(res
);
3875 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3876 return isl_basic_map_finalize(res
);
3879 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3880 __isl_take isl_basic_set
*bset
,
3881 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3883 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3886 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3887 enum isl_dim_type type
, unsigned n
)
3891 return isl_basic_map_insert_dims(bmap
, type
,
3892 isl_basic_map_dim(bmap
, type
), n
);
3895 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3896 enum isl_dim_type type
, unsigned n
)
3900 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3901 return isl_basic_map_add_dims(bset
, type
, n
);
3903 isl_basic_set_free(bset
);
3907 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3908 enum isl_dim_type type
)
3912 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3915 space
= isl_map_get_space(map
);
3916 space
= isl_space_reset(space
, type
);
3917 map
= isl_map_reset_space(map
, space
);
3921 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3922 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3927 return map_space_reset(map
, type
);
3929 map
= isl_map_cow(map
);
3933 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3937 for (i
= 0; i
< map
->n
; ++i
) {
3938 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3949 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3950 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3952 return isl_map_insert_dims(set
, type
, pos
, n
);
3955 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3956 enum isl_dim_type type
, unsigned n
)
3960 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3963 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3964 enum isl_dim_type type
, unsigned n
)
3968 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3969 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3975 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3976 __isl_take isl_basic_map
*bmap
,
3977 enum isl_dim_type dst_type
, unsigned dst_pos
,
3978 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3980 struct isl_dim_map
*dim_map
;
3981 struct isl_basic_map
*res
;
3982 enum isl_dim_type t
;
3983 unsigned total
, off
;
3988 bmap
= isl_basic_map_reset(bmap
, src_type
);
3989 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3993 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3994 return isl_basic_map_free(bmap
);
3996 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3999 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4001 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4002 pos(bmap
->dim
, src_type
) + src_pos
+
4003 ((src_type
< dst_type
) ? n
: 0)) {
4004 bmap
= isl_basic_map_cow(bmap
);
4008 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4009 src_type
, src_pos
, n
);
4013 bmap
= isl_basic_map_finalize(bmap
);
4018 total
= isl_basic_map_total_dim(bmap
);
4019 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4022 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4023 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4024 if (t
== dst_type
) {
4025 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4028 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4031 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4032 dst_pos
, size
- dst_pos
, off
);
4033 off
+= size
- dst_pos
;
4034 } else if (t
== src_type
) {
4035 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4038 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4039 src_pos
+ n
, size
- src_pos
- n
, off
);
4040 off
+= size
- src_pos
- n
;
4042 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4046 isl_dim_map_div(dim_map
, bmap
, off
);
4048 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4049 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4050 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4054 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4055 src_type
, src_pos
, n
);
4059 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4060 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4061 bmap
= isl_basic_map_finalize(bmap
);
4065 isl_basic_map_free(bmap
);
4069 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4070 enum isl_dim_type dst_type
, unsigned dst_pos
,
4071 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4073 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4074 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4075 src_type
, src_pos
, n
);
4076 return bset_from_bmap(bmap
);
4079 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4080 enum isl_dim_type dst_type
, unsigned dst_pos
,
4081 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4085 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4086 return set_from_map(isl_map_move_dims(set_to_map(set
),
4087 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4093 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4094 enum isl_dim_type dst_type
, unsigned dst_pos
,
4095 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4100 map
= isl_map_reset(map
, src_type
);
4101 map
= isl_map_reset(map
, dst_type
);
4105 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4106 return isl_map_free(map
);
4108 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4111 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4113 map
= isl_map_cow(map
);
4117 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4121 for (i
= 0; i
< map
->n
; ++i
) {
4122 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4124 src_type
, src_pos
, n
);
4135 /* Move the specified dimensions to the last columns right before
4136 * the divs. Don't change the dimension specification of bmap.
4137 * That's the responsibility of the caller.
4139 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4140 enum isl_dim_type type
, unsigned first
, unsigned n
)
4142 struct isl_dim_map
*dim_map
;
4143 struct isl_basic_map
*res
;
4144 enum isl_dim_type t
;
4145 unsigned total
, off
;
4149 if (pos(bmap
->dim
, type
) + first
+ n
==
4150 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4153 total
= isl_basic_map_total_dim(bmap
);
4154 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4157 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4158 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4160 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4163 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4164 first
, n
, total
- bmap
->n_div
- n
);
4165 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4166 first
+ n
, size
- (first
+ n
), off
);
4167 off
+= size
- (first
+ n
);
4169 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4173 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4175 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4176 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4177 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4181 /* Insert "n" rows in the divs of "bmap".
4183 * The number of columns is not changed, which means that the last
4184 * dimensions of "bmap" are being reintepreted as the new divs.
4185 * The space of "bmap" is not adjusted, however, which means
4186 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4187 * from the space of "bmap" is the responsibility of the caller.
4189 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4197 bmap
= isl_basic_map_cow(bmap
);
4201 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4202 old
= bmap
->block2
.data
;
4203 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4204 (bmap
->extra
+ n
) * (1 + row_size
));
4205 if (!bmap
->block2
.data
)
4206 return isl_basic_map_free(bmap
);
4207 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4209 return isl_basic_map_free(bmap
);
4210 for (i
= 0; i
< n
; ++i
) {
4211 new_div
[i
] = bmap
->block2
.data
+
4212 (bmap
->extra
+ i
) * (1 + row_size
);
4213 isl_seq_clr(new_div
[i
], 1 + row_size
);
4215 for (i
= 0; i
< bmap
->extra
; ++i
)
4216 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4218 bmap
->div
= new_div
;
4225 /* Drop constraints from "bmap" that only involve the variables
4226 * of "type" in the range [first, first + n] that are not related
4227 * to any of the variables outside that interval.
4228 * These constraints cannot influence the values for the variables
4229 * outside the interval, except in case they cause "bmap" to be empty.
4230 * Only drop the constraints if "bmap" is known to be non-empty.
4232 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4233 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4234 unsigned first
, unsigned n
)
4238 unsigned dim
, n_div
;
4241 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4243 return isl_basic_map_free(bmap
);
4247 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4248 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4249 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4251 return isl_basic_map_free(bmap
);
4252 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4253 for (i
= 0; i
< first
; ++i
)
4255 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4258 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4263 /* Turn the n dimensions of type type, starting at first
4264 * into existentially quantified variables.
4266 * If a subset of the projected out variables are unrelated
4267 * to any of the variables that remain, then the constraints
4268 * involving this subset are simply dropped first.
4270 __isl_give isl_basic_map
*isl_basic_map_project_out(
4271 __isl_take isl_basic_map
*bmap
,
4272 enum isl_dim_type type
, unsigned first
, unsigned n
)
4277 return basic_map_space_reset(bmap
, type
);
4278 if (type
== isl_dim_div
)
4279 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4280 "cannot project out existentially quantified variables",
4281 return isl_basic_map_free(bmap
));
4283 empty
= isl_basic_map_plain_is_empty(bmap
);
4285 return isl_basic_map_free(bmap
);
4287 bmap
= isl_basic_map_set_to_empty(bmap
);
4289 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4293 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4294 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4296 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4297 return isl_basic_map_free(bmap
);
4299 bmap
= move_last(bmap
, type
, first
, n
);
4300 bmap
= isl_basic_map_cow(bmap
);
4301 bmap
= insert_div_rows(bmap
, n
);
4305 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4308 bmap
= isl_basic_map_simplify(bmap
);
4309 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4310 return isl_basic_map_finalize(bmap
);
4312 isl_basic_map_free(bmap
);
4316 /* Turn the n dimensions of type type, starting at first
4317 * into existentially quantified variables.
4319 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4320 enum isl_dim_type type
, unsigned first
, unsigned n
)
4322 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4326 /* Turn the n dimensions of type type, starting at first
4327 * into existentially quantified variables.
4329 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4330 enum isl_dim_type type
, unsigned first
, unsigned n
)
4335 return map_space_reset(map
, type
);
4337 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4338 return isl_map_free(map
);
4340 map
= isl_map_cow(map
);
4344 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4348 for (i
= 0; i
< map
->n
; ++i
) {
4349 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4360 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4361 * into existentially quantified variables.
4363 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4364 enum isl_dim_type type
, unsigned first
, unsigned n
)
4368 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4369 return isl_map_free(map
);
4370 dim
= isl_map_dim(map
, type
);
4371 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4372 map
= isl_map_project_out(map
, type
, 0, first
);
4376 /* Turn the n dimensions of type type, starting at first
4377 * into existentially quantified variables.
4379 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4380 enum isl_dim_type type
, unsigned first
, unsigned n
)
4382 return set_from_map(isl_map_project_out(set_to_map(set
),
4386 /* Return a map that projects the elements in "set" onto their
4387 * "n" set dimensions starting at "first".
4388 * "type" should be equal to isl_dim_set.
4390 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4391 enum isl_dim_type type
, unsigned first
, unsigned n
)
4399 if (type
!= isl_dim_set
)
4400 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4401 "only set dimensions can be projected out", goto error
);
4402 dim
= isl_set_dim(set
, isl_dim_set
);
4403 if (first
+ n
> dim
|| first
+ n
< first
)
4404 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4405 "index out of bounds", goto error
);
4407 map
= isl_map_from_domain(set
);
4408 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4409 for (i
= 0; i
< n
; ++i
)
4410 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4418 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4423 for (i
= 0; i
< n
; ++i
) {
4424 j
= isl_basic_map_alloc_div(bmap
);
4427 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4431 isl_basic_map_free(bmap
);
4435 struct isl_basic_map
*isl_basic_map_apply_range(
4436 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4438 isl_space
*space_result
= NULL
;
4439 struct isl_basic_map
*bmap
;
4440 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4441 struct isl_dim_map
*dim_map1
, *dim_map2
;
4443 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4445 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4446 bmap2
->dim
, isl_dim_in
))
4447 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4448 "spaces don't match", goto error
);
4450 space_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4451 isl_space_copy(bmap2
->dim
));
4453 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4454 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4455 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4456 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4458 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4459 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4460 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4461 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4462 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4463 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4464 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4465 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4466 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4467 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4468 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4470 bmap
= isl_basic_map_alloc_space(space_result
,
4471 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4472 bmap1
->n_eq
+ bmap2
->n_eq
,
4473 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4474 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4475 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4476 bmap
= add_divs(bmap
, n
);
4477 bmap
= isl_basic_map_simplify(bmap
);
4478 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4479 return isl_basic_map_finalize(bmap
);
4481 isl_basic_map_free(bmap1
);
4482 isl_basic_map_free(bmap2
);
4486 struct isl_basic_set
*isl_basic_set_apply(
4487 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4492 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4495 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4498 isl_basic_set_free(bset
);
4499 isl_basic_map_free(bmap
);
4503 struct isl_basic_map
*isl_basic_map_apply_domain(
4504 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4506 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4508 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4509 bmap2
->dim
, isl_dim_in
))
4510 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4511 "spaces don't match", goto error
);
4513 bmap1
= isl_basic_map_reverse(bmap1
);
4514 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4515 return isl_basic_map_reverse(bmap1
);
4517 isl_basic_map_free(bmap1
);
4518 isl_basic_map_free(bmap2
);
4522 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4523 * A \cap B -> f(A) + f(B)
4525 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4526 __isl_take isl_basic_map
*bmap2
)
4528 unsigned n_in
, n_out
, nparam
, total
, pos
;
4529 struct isl_basic_map
*bmap
= NULL
;
4530 struct isl_dim_map
*dim_map1
, *dim_map2
;
4533 if (!bmap1
|| !bmap2
)
4536 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4539 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4540 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4541 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4543 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4544 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4545 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4546 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4547 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4548 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4549 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4550 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4551 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4552 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4553 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4555 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4556 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4557 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4558 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4559 for (i
= 0; i
< n_out
; ++i
) {
4560 int j
= isl_basic_map_alloc_equality(bmap
);
4563 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4564 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4565 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4566 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4568 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4569 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4570 bmap
= add_divs(bmap
, 2 * n_out
);
4572 bmap
= isl_basic_map_simplify(bmap
);
4573 return isl_basic_map_finalize(bmap
);
4575 isl_basic_map_free(bmap
);
4576 isl_basic_map_free(bmap1
);
4577 isl_basic_map_free(bmap2
);
4581 /* Given two maps A -> f(A) and B -> g(B), construct a map
4582 * A \cap B -> f(A) + f(B)
4584 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4585 __isl_take isl_map
*map2
)
4587 struct isl_map
*result
;
4593 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4595 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4596 map1
->n
* map2
->n
, 0);
4599 for (i
= 0; i
< map1
->n
; ++i
)
4600 for (j
= 0; j
< map2
->n
; ++j
) {
4601 struct isl_basic_map
*part
;
4602 part
= isl_basic_map_sum(
4603 isl_basic_map_copy(map1
->p
[i
]),
4604 isl_basic_map_copy(map2
->p
[j
]));
4605 if (isl_basic_map_is_empty(part
))
4606 isl_basic_map_free(part
);
4608 result
= isl_map_add_basic_map(result
, part
);
4621 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4622 __isl_take isl_set
*set2
)
4624 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4627 /* Given a basic map A -> f(A), construct A -> -f(A).
4629 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4634 bmap
= isl_basic_map_cow(bmap
);
4638 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4639 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4640 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4641 for (j
= 0; j
< n
; ++j
)
4642 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4643 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4644 for (j
= 0; j
< n
; ++j
)
4645 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4646 for (i
= 0; i
< bmap
->n_div
; ++i
)
4647 for (j
= 0; j
< n
; ++j
)
4648 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4649 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4650 return isl_basic_map_finalize(bmap
);
4653 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4655 return isl_basic_map_neg(bset
);
4658 /* Given a map A -> f(A), construct A -> -f(A).
4660 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4664 map
= isl_map_cow(map
);
4668 for (i
= 0; i
< map
->n
; ++i
) {
4669 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4680 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4682 return set_from_map(isl_map_neg(set_to_map(set
)));
4685 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4686 * A -> floor(f(A)/d).
4688 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4691 unsigned n_in
, n_out
, nparam
, total
, pos
;
4692 struct isl_basic_map
*result
= NULL
;
4693 struct isl_dim_map
*dim_map
;
4699 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4700 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4701 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4703 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4704 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4705 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4706 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4707 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4708 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4710 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4711 bmap
->n_div
+ n_out
,
4712 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4713 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4714 result
= add_divs(result
, n_out
);
4715 for (i
= 0; i
< n_out
; ++i
) {
4717 j
= isl_basic_map_alloc_inequality(result
);
4720 isl_seq_clr(result
->ineq
[j
], 1+total
);
4721 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4722 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4723 j
= isl_basic_map_alloc_inequality(result
);
4726 isl_seq_clr(result
->ineq
[j
], 1+total
);
4727 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4728 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4729 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4732 result
= isl_basic_map_simplify(result
);
4733 return isl_basic_map_finalize(result
);
4735 isl_basic_map_free(result
);
4739 /* Given a map A -> f(A) and an integer d, construct a map
4740 * A -> floor(f(A)/d).
4742 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4746 map
= isl_map_cow(map
);
4750 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4751 for (i
= 0; i
< map
->n
; ++i
) {
4752 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4756 map
= isl_map_unmark_normalized(map
);
4764 /* Given a map A -> f(A) and an integer d, construct a map
4765 * A -> floor(f(A)/d).
4767 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4768 __isl_take isl_val
*d
)
4772 if (!isl_val_is_int(d
))
4773 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4774 "expecting integer denominator", goto error
);
4775 map
= isl_map_floordiv(map
, d
->n
);
4784 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4791 i
= isl_basic_map_alloc_equality(bmap
);
4794 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4795 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4796 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4797 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4798 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4799 return isl_basic_map_finalize(bmap
);
4801 isl_basic_map_free(bmap
);
4805 /* Add a constraint to "bmap" expressing i_pos < o_pos
4807 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4814 i
= isl_basic_map_alloc_inequality(bmap
);
4817 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4818 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4819 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4820 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4821 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4822 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4823 return isl_basic_map_finalize(bmap
);
4825 isl_basic_map_free(bmap
);
4829 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4831 static __isl_give isl_basic_map
*var_less_or_equal(
4832 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4838 i
= isl_basic_map_alloc_inequality(bmap
);
4841 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4842 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4843 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4844 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4845 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4846 return isl_basic_map_finalize(bmap
);
4848 isl_basic_map_free(bmap
);
4852 /* Add a constraint to "bmap" expressing i_pos > o_pos
4854 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4861 i
= isl_basic_map_alloc_inequality(bmap
);
4864 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4865 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4866 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4867 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4868 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4869 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4870 return isl_basic_map_finalize(bmap
);
4872 isl_basic_map_free(bmap
);
4876 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4878 static __isl_give isl_basic_map
*var_more_or_equal(
4879 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4885 i
= isl_basic_map_alloc_inequality(bmap
);
4888 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4889 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4890 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4891 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4892 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4893 return isl_basic_map_finalize(bmap
);
4895 isl_basic_map_free(bmap
);
4899 __isl_give isl_basic_map
*isl_basic_map_equal(
4900 __isl_take isl_space
*space
, unsigned n_equal
)
4903 struct isl_basic_map
*bmap
;
4904 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4907 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4908 bmap
= var_equal(bmap
, i
);
4909 return isl_basic_map_finalize(bmap
);
4912 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4914 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4918 struct isl_basic_map
*bmap
;
4919 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4922 for (i
= 0; i
< pos
&& bmap
; ++i
)
4923 bmap
= var_equal(bmap
, i
);
4925 bmap
= var_less(bmap
, pos
);
4926 return isl_basic_map_finalize(bmap
);
4929 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4931 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4932 __isl_take isl_space
*space
, unsigned pos
)
4935 isl_basic_map
*bmap
;
4937 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4938 for (i
= 0; i
< pos
; ++i
)
4939 bmap
= var_equal(bmap
, i
);
4940 bmap
= var_less_or_equal(bmap
, pos
);
4941 return isl_basic_map_finalize(bmap
);
4944 /* Return a relation on "space" expressing i_pos > o_pos
4946 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4950 struct isl_basic_map
*bmap
;
4951 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4954 for (i
= 0; i
< pos
&& bmap
; ++i
)
4955 bmap
= var_equal(bmap
, i
);
4957 bmap
= var_more(bmap
, pos
);
4958 return isl_basic_map_finalize(bmap
);
4961 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4963 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4964 __isl_take isl_space
*space
, unsigned pos
)
4967 isl_basic_map
*bmap
;
4969 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4970 for (i
= 0; i
< pos
; ++i
)
4971 bmap
= var_equal(bmap
, i
);
4972 bmap
= var_more_or_equal(bmap
, pos
);
4973 return isl_basic_map_finalize(bmap
);
4976 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4977 unsigned n
, int equal
)
4979 struct isl_map
*map
;
4982 if (n
== 0 && equal
)
4983 return isl_map_universe(space
);
4985 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4987 for (i
= 0; i
+ 1 < n
; ++i
)
4988 map
= isl_map_add_basic_map(map
,
4989 isl_basic_map_less_at(isl_space_copy(space
), i
));
4992 map
= isl_map_add_basic_map(map
,
4993 isl_basic_map_less_or_equal_at(space
, n
- 1));
4995 map
= isl_map_add_basic_map(map
,
4996 isl_basic_map_less_at(space
, n
- 1));
4998 isl_space_free(space
);
5003 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5007 return map_lex_lte_first(space
, space
->n_out
, equal
);
5010 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5012 return map_lex_lte_first(dim
, n
, 0);
5015 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5017 return map_lex_lte_first(dim
, n
, 1);
5020 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5022 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5025 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5027 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5030 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5031 unsigned n
, int equal
)
5033 struct isl_map
*map
;
5036 if (n
== 0 && equal
)
5037 return isl_map_universe(space
);
5039 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5041 for (i
= 0; i
+ 1 < n
; ++i
)
5042 map
= isl_map_add_basic_map(map
,
5043 isl_basic_map_more_at(isl_space_copy(space
), i
));
5046 map
= isl_map_add_basic_map(map
,
5047 isl_basic_map_more_or_equal_at(space
, n
- 1));
5049 map
= isl_map_add_basic_map(map
,
5050 isl_basic_map_more_at(space
, n
- 1));
5052 isl_space_free(space
);
5057 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5061 return map_lex_gte_first(space
, space
->n_out
, equal
);
5064 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5066 return map_lex_gte_first(dim
, n
, 0);
5069 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5071 return map_lex_gte_first(dim
, n
, 1);
5074 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5076 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5079 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5081 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5084 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5085 __isl_take isl_set
*set2
)
5088 map
= isl_map_lex_le(isl_set_get_space(set1
));
5089 map
= isl_map_intersect_domain(map
, set1
);
5090 map
= isl_map_intersect_range(map
, set2
);
5094 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5095 __isl_take isl_set
*set2
)
5098 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5099 map
= isl_map_intersect_domain(map
, set1
);
5100 map
= isl_map_intersect_range(map
, set2
);
5104 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5105 __isl_take isl_set
*set2
)
5108 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5109 map
= isl_map_intersect_domain(map
, set1
);
5110 map
= isl_map_intersect_range(map
, set2
);
5114 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5115 __isl_take isl_set
*set2
)
5118 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5119 map
= isl_map_intersect_domain(map
, set1
);
5120 map
= isl_map_intersect_range(map
, set2
);
5124 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5125 __isl_take isl_map
*map2
)
5128 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5129 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5130 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5134 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5135 __isl_take isl_map
*map2
)
5138 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5139 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5140 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5144 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5145 __isl_take isl_map
*map2
)
5148 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5149 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5150 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5154 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5155 __isl_take isl_map
*map2
)
5158 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5159 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5160 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5164 /* For a div d = floor(f/m), add the constraint
5168 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5169 unsigned pos
, isl_int
*div
)
5172 unsigned total
= isl_basic_map_total_dim(bmap
);
5174 i
= isl_basic_map_alloc_inequality(bmap
);
5176 return isl_stat_error
;
5177 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5178 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5183 /* For a div d = floor(f/m), add the constraint
5185 * -(f-(m-1)) + m d >= 0
5187 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5188 unsigned pos
, isl_int
*div
)
5191 unsigned total
= isl_basic_map_total_dim(bmap
);
5193 i
= isl_basic_map_alloc_inequality(bmap
);
5195 return isl_stat_error
;
5196 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5197 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5198 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5199 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5204 /* For a div d = floor(f/m), add the constraints
5207 * -(f-(m-1)) + m d >= 0
5209 * Note that the second constraint is the negation of
5213 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5214 unsigned pos
, isl_int
*div
)
5216 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5218 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5223 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5224 unsigned pos
, isl_int
*div
)
5226 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5230 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map
*bmap
,
5233 unsigned total
= isl_basic_map_total_dim(bmap
);
5234 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5236 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5240 /* For each known div d = floor(f/m), add the constraints
5243 * -(f-(m-1)) + m d >= 0
5245 * Remove duplicate constraints in case of some these div constraints
5246 * already appear in "bmap".
5248 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5249 __isl_take isl_basic_map
*bmap
)
5255 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5259 bmap
= add_known_div_constraints(bmap
);
5260 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5261 bmap
= isl_basic_map_finalize(bmap
);
5265 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5267 * In particular, if this div is of the form d = floor(f/m),
5268 * then add the constraint
5272 * if sign < 0 or the constraint
5274 * -(f-(m-1)) + m d >= 0
5278 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5279 unsigned div
, int sign
)
5287 total
= isl_basic_map_total_dim(bmap
);
5288 div_pos
= total
- bmap
->n_div
+ div
;
5291 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5293 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5296 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5297 __isl_take isl_basic_map
*bmap
)
5301 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5303 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5304 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5305 return bset_from_bmap(bmap
);
5306 bmap
= isl_basic_map_cow(bmap
);
5309 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5312 bmap
->extra
-= bmap
->n_div
;
5314 bmap
= isl_basic_map_finalize(bmap
);
5315 return bset_from_bmap(bmap
);
5317 isl_basic_map_free(bmap
);
5321 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5322 __isl_take isl_basic_set
*bset
)
5324 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5327 /* Replace each element in "list" by the result of applying
5328 * isl_basic_map_underlying_set to the element.
5330 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5331 __isl_take isl_basic_map_list
*list
)
5338 n
= isl_basic_map_list_n_basic_map(list
);
5339 for (i
= 0; i
< n
; ++i
) {
5340 isl_basic_map
*bmap
;
5341 isl_basic_set
*bset
;
5343 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5344 bset
= isl_basic_set_underlying_set(bmap
);
5345 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5351 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5352 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5354 struct isl_basic_map
*bmap
;
5355 struct isl_ctx
*ctx
;
5362 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5363 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5364 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5366 if (like
->n_div
== 0) {
5367 isl_space
*space
= isl_basic_map_get_space(like
);
5368 isl_basic_map_free(like
);
5369 return isl_basic_map_reset_space(bset
, space
);
5371 bset
= isl_basic_set_cow(bset
);
5374 total
= bset
->dim
->n_out
+ bset
->extra
;
5375 bmap
= bset_to_bmap(bset
);
5376 isl_space_free(bmap
->dim
);
5377 bmap
->dim
= isl_space_copy(like
->dim
);
5380 bmap
->n_div
= like
->n_div
;
5381 bmap
->extra
+= like
->n_div
;
5385 ltotal
= total
- bmap
->extra
+ like
->extra
;
5388 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5389 bmap
->extra
* (1 + 1 + total
));
5390 if (isl_blk_is_error(bmap
->block2
))
5392 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5396 for (i
= 0; i
< bmap
->extra
; ++i
)
5397 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5398 for (i
= 0; i
< like
->n_div
; ++i
) {
5399 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5400 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5402 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5404 isl_basic_map_free(like
);
5405 bmap
= isl_basic_map_simplify(bmap
);
5406 bmap
= isl_basic_map_finalize(bmap
);
5409 isl_basic_map_free(like
);
5410 isl_basic_set_free(bset
);
5414 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5415 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5417 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5418 bset_to_bmap(like
)));
5421 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5425 map
= isl_map_cow(map
);
5428 map
->dim
= isl_space_cow(map
->dim
);
5432 for (i
= 1; i
< map
->n
; ++i
)
5433 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5435 for (i
= 0; i
< map
->n
; ++i
) {
5436 map
->p
[i
] = bset_to_bmap(
5437 isl_basic_map_underlying_set(map
->p
[i
]));
5442 map
->dim
= isl_space_underlying(map
->dim
, 0);
5444 isl_space_free(map
->dim
);
5445 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5449 return set_from_map(map
);
5455 /* Replace the space of "bmap" by "space".
5457 * If the space of "bmap" is identical to "space" (including the identifiers
5458 * of the input and output dimensions), then simply return the original input.
5460 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5461 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5464 isl_space
*bmap_space
;
5466 bmap_space
= isl_basic_map_peek_space(bmap
);
5467 equal
= isl_space_is_equal(bmap_space
, space
);
5468 if (equal
>= 0 && equal
)
5469 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5473 isl_space_free(space
);
5476 bmap
= isl_basic_map_cow(bmap
);
5477 if (!bmap
|| !space
)
5480 isl_space_free(bmap
->dim
);
5483 bmap
= isl_basic_map_finalize(bmap
);
5487 isl_basic_map_free(bmap
);
5488 isl_space_free(space
);
5492 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5493 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5495 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5499 /* Check that the total dimensions of "map" and "space" are the same.
5501 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5502 __isl_keep isl_space
*space
)
5504 unsigned dim1
, dim2
;
5507 return isl_stat_error
;
5508 dim1
= isl_map_dim(map
, isl_dim_all
);
5509 dim2
= isl_space_dim(space
, isl_dim_all
);
5512 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5513 "total dimensions do not match", return isl_stat_error
);
5516 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5517 __isl_take isl_space
*space
)
5521 map
= isl_map_cow(map
);
5525 for (i
= 0; i
< map
->n
; ++i
) {
5526 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5527 isl_space_copy(space
));
5531 isl_space_free(map
->dim
);
5537 isl_space_free(space
);
5541 /* Replace the space of "map" by "space", without modifying
5542 * the dimension of "map".
5544 * If the space of "map" is identical to "space" (including the identifiers
5545 * of the input and output dimensions), then simply return the original input.
5547 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5548 __isl_take isl_space
*space
)
5551 isl_space
*map_space
;
5553 map_space
= isl_map_peek_space(map
);
5554 equal
= isl_space_is_equal(map_space
, space
);
5555 if (equal
>= 0 && equal
)
5556 equal
= isl_space_has_equal_ids(map_space
, space
);
5560 isl_space_free(space
);
5563 if (check_map_space_equal_total_dim(map
, space
) < 0)
5565 return isl_map_reset_space(map
, space
);
5568 isl_space_free(space
);
5572 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5573 __isl_take isl_space
*dim
)
5575 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5578 /* Compute the parameter domain of the given basic set.
5580 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5586 is_params
= isl_basic_set_is_params(bset
);
5588 return isl_basic_set_free(bset
);
5592 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5593 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5594 space
= isl_basic_set_get_space(bset
);
5595 space
= isl_space_params(space
);
5596 bset
= isl_basic_set_reset_space(bset
, space
);
5600 /* Construct a zero-dimensional basic set with the given parameter domain.
5602 __isl_give isl_basic_set
*isl_basic_set_from_params(
5603 __isl_take isl_basic_set
*bset
)
5606 space
= isl_basic_set_get_space(bset
);
5607 space
= isl_space_set_from_params(space
);
5608 bset
= isl_basic_set_reset_space(bset
, space
);
5612 /* Compute the parameter domain of the given set.
5614 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5619 if (isl_set_is_params(set
))
5622 n
= isl_set_dim(set
, isl_dim_set
);
5623 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5624 space
= isl_set_get_space(set
);
5625 space
= isl_space_params(space
);
5626 set
= isl_set_reset_space(set
, space
);
5630 /* Construct a zero-dimensional set with the given parameter domain.
5632 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5635 space
= isl_set_get_space(set
);
5636 space
= isl_space_set_from_params(space
);
5637 set
= isl_set_reset_space(set
, space
);
5641 /* Compute the parameter domain of the given map.
5643 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5648 n
= isl_map_dim(map
, isl_dim_in
);
5649 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5650 n
= isl_map_dim(map
, isl_dim_out
);
5651 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5652 space
= isl_map_get_space(map
);
5653 space
= isl_space_params(space
);
5654 map
= isl_map_reset_space(map
, space
);
5658 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5665 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5667 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5668 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5670 return isl_basic_map_reset_space(bmap
, space
);
5673 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5676 return isl_bool_error
;
5677 return isl_space_may_be_set(bmap
->dim
);
5680 /* Is this basic map actually a set?
5681 * Users should never call this function. Outside of isl,
5682 * the type should indicate whether something is a set or a map.
5684 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5687 return isl_bool_error
;
5688 return isl_space_is_set(bmap
->dim
);
5691 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5695 is_set
= isl_basic_map_is_set(bmap
);
5700 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5702 isl_basic_map_free(bmap
);
5706 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5707 __isl_take isl_basic_map
*bmap
)
5711 isl_basic_map
*domain
;
5712 int nparam
, n_in
, n_out
;
5714 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5715 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5716 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5718 space
= isl_basic_map_get_space(bmap
);
5719 space
= isl_space_from_range(isl_space_domain(space
));
5720 domain
= isl_basic_map_universe(space
);
5722 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5723 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5724 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5726 for (i
= 0; i
< n_in
; ++i
)
5727 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5730 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5731 return isl_basic_map_finalize(bmap
);
5734 __isl_give isl_basic_map
*isl_basic_map_range_map(
5735 __isl_take isl_basic_map
*bmap
)
5739 isl_basic_map
*range
;
5740 int nparam
, n_in
, n_out
;
5742 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5743 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5744 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5746 space
= isl_basic_map_get_space(bmap
);
5747 space
= isl_space_from_range(isl_space_range(space
));
5748 range
= isl_basic_map_universe(space
);
5750 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5751 bmap
= isl_basic_map_apply_range(bmap
, range
);
5752 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5754 for (i
= 0; i
< n_out
; ++i
)
5755 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5758 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5759 return isl_basic_map_finalize(bmap
);
5762 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5766 return isl_space_may_be_set(map
->dim
);
5769 /* Is this map actually a set?
5770 * Users should never call this function. Outside of isl,
5771 * the type should indicate whether something is a set or a map.
5773 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5776 return isl_bool_error
;
5777 return isl_space_is_set(map
->dim
);
5780 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5784 struct isl_set
*set
;
5786 is_set
= isl_map_is_set(map
);
5790 return set_from_map(map
);
5792 map
= isl_map_cow(map
);
5796 set
= set_from_map(map
);
5797 set
->dim
= isl_space_range(set
->dim
);
5800 for (i
= 0; i
< map
->n
; ++i
) {
5801 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5805 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5806 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5813 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5817 map
= isl_map_cow(map
);
5821 map
->dim
= isl_space_domain_map(map
->dim
);
5824 for (i
= 0; i
< map
->n
; ++i
) {
5825 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5829 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5830 map
= isl_map_unmark_normalized(map
);
5837 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5840 isl_space
*range_dim
;
5842 map
= isl_map_cow(map
);
5846 range_dim
= isl_space_range(isl_map_get_space(map
));
5847 range_dim
= isl_space_from_range(range_dim
);
5848 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5849 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5852 for (i
= 0; i
< map
->n
; ++i
) {
5853 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5857 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5858 map
= isl_map_unmark_normalized(map
);
5865 /* Given a wrapped map of the form A[B -> C],
5866 * return the map A[B -> C] -> B.
5868 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5875 if (!isl_set_has_tuple_id(set
))
5876 return isl_map_domain_map(isl_set_unwrap(set
));
5878 id
= isl_set_get_tuple_id(set
);
5879 map
= isl_map_domain_map(isl_set_unwrap(set
));
5880 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5885 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5886 __isl_take isl_basic_set
*bset
)
5888 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5891 __isl_give isl_basic_map
*isl_basic_map_from_range(
5892 __isl_take isl_basic_set
*bset
)
5895 space
= isl_basic_set_get_space(bset
);
5896 space
= isl_space_from_range(space
);
5897 bset
= isl_basic_set_reset_space(bset
, space
);
5898 return bset_to_bmap(bset
);
5901 /* Create a relation with the given set as range.
5902 * The domain of the created relation is a zero-dimensional
5903 * flat anonymous space.
5905 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5908 space
= isl_set_get_space(set
);
5909 space
= isl_space_from_range(space
);
5910 set
= isl_set_reset_space(set
, space
);
5911 return set_to_map(set
);
5914 /* Create a relation with the given set as domain.
5915 * The range of the created relation is a zero-dimensional
5916 * flat anonymous space.
5918 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5920 return isl_map_reverse(isl_map_from_range(set
));
5923 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5924 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5926 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5929 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5930 __isl_take isl_set
*range
)
5932 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5935 /* Return a newly allocated isl_map with given space and flags and
5936 * room for "n" basic maps.
5937 * Make sure that all cached information is cleared.
5939 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5942 struct isl_map
*map
;
5947 isl_die(space
->ctx
, isl_error_internal
,
5948 "negative number of basic maps", goto error
);
5949 map
= isl_calloc(space
->ctx
, struct isl_map
,
5950 sizeof(struct isl_map
) +
5951 (n
- 1) * sizeof(struct isl_basic_map
*));
5955 map
->ctx
= space
->ctx
;
5956 isl_ctx_ref(map
->ctx
);
5964 isl_space_free(space
);
5968 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5970 struct isl_basic_map
*bmap
;
5971 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5972 bmap
= isl_basic_map_set_to_empty(bmap
);
5976 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5978 struct isl_basic_set
*bset
;
5979 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5980 bset
= isl_basic_set_set_to_empty(bset
);
5984 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5986 struct isl_basic_map
*bmap
;
5987 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5988 bmap
= isl_basic_map_finalize(bmap
);
5992 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5994 struct isl_basic_set
*bset
;
5995 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5996 bset
= isl_basic_set_finalize(bset
);
6000 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6001 __isl_take isl_space
*space
)
6004 unsigned total
= isl_space_dim(space
, isl_dim_all
);
6005 isl_basic_map
*bmap
;
6007 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6008 for (i
= 0; i
< total
; ++i
) {
6009 int k
= isl_basic_map_alloc_inequality(bmap
);
6012 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6013 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6017 isl_basic_map_free(bmap
);
6021 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6022 __isl_take isl_space
*space
)
6024 return isl_basic_map_nat_universe(space
);
6027 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
6029 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
6032 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
6034 return isl_map_nat_universe(dim
);
6037 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6039 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6042 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6044 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6047 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6049 struct isl_map
*map
;
6052 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6053 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6057 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6059 struct isl_set
*set
;
6062 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6063 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6067 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6070 struct isl_map
*dup
;
6074 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6075 for (i
= 0; i
< map
->n
; ++i
)
6076 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6080 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6081 __isl_take isl_basic_map
*bmap
)
6085 if (isl_basic_map_plain_is_empty(bmap
)) {
6086 isl_basic_map_free(bmap
);
6089 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6090 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6091 map
->p
[map
->n
] = bmap
;
6093 map
= isl_map_unmark_normalized(map
);
6099 isl_basic_map_free(bmap
);
6103 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6114 isl_ctx_deref(map
->ctx
);
6115 for (i
= 0; i
< map
->n
; ++i
)
6116 isl_basic_map_free(map
->p
[i
]);
6117 isl_space_free(map
->dim
);
6123 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6124 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6128 bmap
= isl_basic_map_cow(bmap
);
6129 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6130 j
= isl_basic_map_alloc_equality(bmap
);
6133 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6134 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6135 isl_int_set_si(bmap
->eq
[j
][0], value
);
6136 bmap
= isl_basic_map_simplify(bmap
);
6137 return isl_basic_map_finalize(bmap
);
6139 isl_basic_map_free(bmap
);
6143 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6144 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6148 bmap
= isl_basic_map_cow(bmap
);
6149 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6150 j
= isl_basic_map_alloc_equality(bmap
);
6153 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6154 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6155 isl_int_set(bmap
->eq
[j
][0], value
);
6156 bmap
= isl_basic_map_simplify(bmap
);
6157 return isl_basic_map_finalize(bmap
);
6159 isl_basic_map_free(bmap
);
6163 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6164 enum isl_dim_type type
, unsigned pos
, int value
)
6166 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6167 return isl_basic_map_free(bmap
);
6168 return isl_basic_map_fix_pos_si(bmap
,
6169 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6172 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6173 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6175 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6176 return isl_basic_map_free(bmap
);
6177 return isl_basic_map_fix_pos(bmap
,
6178 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6181 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6182 * to be equal to "v".
6184 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6185 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6189 if (!isl_val_is_int(v
))
6190 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6191 "expecting integer value", goto error
);
6192 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6194 pos
+= isl_basic_map_offset(bmap
, type
);
6195 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6199 isl_basic_map_free(bmap
);
6204 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6205 * to be equal to "v".
6207 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6208 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6210 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6213 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6214 enum isl_dim_type type
, unsigned pos
, int value
)
6216 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6220 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6221 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6223 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6227 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6228 unsigned input
, int value
)
6230 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6233 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6234 unsigned dim
, int value
)
6236 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6237 isl_dim_set
, dim
, value
));
6240 /* Remove the basic map at position "i" from "map" if this basic map
6241 * is (obviously) empty.
6243 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6250 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6252 return isl_map_free(map
);
6256 isl_basic_map_free(map
->p
[i
]);
6259 map
->p
[i
] = map
->p
[map
->n
];
6260 map
= isl_map_unmark_normalized(map
);
6267 /* Perform "fn" on each basic map of "map", where we may not be holding
6268 * the only reference to "map".
6269 * In particular, "fn" should be a semantics preserving operation
6270 * that we want to apply to all copies of "map". We therefore need
6271 * to be careful not to modify "map" in a way that breaks "map"
6272 * in case anything goes wrong.
6274 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6275 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6277 struct isl_basic_map
*bmap
;
6283 for (i
= map
->n
- 1; i
>= 0; --i
) {
6284 bmap
= isl_basic_map_copy(map
->p
[i
]);
6288 isl_basic_map_free(map
->p
[i
]);
6290 map
= remove_if_empty(map
, i
);
6301 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6302 enum isl_dim_type type
, unsigned pos
, int value
)
6306 map
= isl_map_cow(map
);
6310 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6311 for (i
= map
->n
- 1; i
>= 0; --i
) {
6312 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6313 map
= remove_if_empty(map
, i
);
6317 map
= isl_map_unmark_normalized(map
);
6324 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6325 enum isl_dim_type type
, unsigned pos
, int value
)
6327 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6330 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6331 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6335 map
= isl_map_cow(map
);
6339 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6340 for (i
= 0; i
< map
->n
; ++i
) {
6341 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6345 map
= isl_map_unmark_normalized(map
);
6352 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6353 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6355 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6358 /* Fix the value of the variable at position "pos" of type "type" of "map"
6359 * to be equal to "v".
6361 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6362 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6366 map
= isl_map_cow(map
);
6370 if (!isl_val_is_int(v
))
6371 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6372 "expecting integer value", goto error
);
6373 if (pos
>= isl_map_dim(map
, type
))
6374 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6375 "index out of bounds", goto error
);
6376 for (i
= map
->n
- 1; i
>= 0; --i
) {
6377 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6379 map
= remove_if_empty(map
, i
);
6383 map
= isl_map_unmark_normalized(map
);
6392 /* Fix the value of the variable at position "pos" of type "type" of "set"
6393 * to be equal to "v".
6395 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6396 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6398 return isl_map_fix_val(set
, type
, pos
, v
);
6401 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6402 unsigned input
, int value
)
6404 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6407 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6409 return set_from_map(isl_map_fix_si(set_to_map(set
),
6410 isl_dim_set
, dim
, value
));
6413 static __isl_give isl_basic_map
*basic_map_bound_si(
6414 __isl_take isl_basic_map
*bmap
,
6415 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6419 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6420 return isl_basic_map_free(bmap
);
6421 pos
+= isl_basic_map_offset(bmap
, type
);
6422 bmap
= isl_basic_map_cow(bmap
);
6423 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6424 j
= isl_basic_map_alloc_inequality(bmap
);
6427 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6429 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6430 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6432 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6433 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6435 bmap
= isl_basic_map_simplify(bmap
);
6436 return isl_basic_map_finalize(bmap
);
6438 isl_basic_map_free(bmap
);
6442 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6443 __isl_take isl_basic_map
*bmap
,
6444 enum isl_dim_type type
, unsigned pos
, int value
)
6446 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6449 /* Constrain the values of the given dimension to be no greater than "value".
6451 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6452 __isl_take isl_basic_map
*bmap
,
6453 enum isl_dim_type type
, unsigned pos
, int value
)
6455 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6458 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6459 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6463 map
= isl_map_cow(map
);
6467 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6468 for (i
= 0; i
< map
->n
; ++i
) {
6469 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6470 type
, pos
, value
, upper
);
6474 map
= isl_map_unmark_normalized(map
);
6481 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6482 enum isl_dim_type type
, unsigned pos
, int value
)
6484 return map_bound_si(map
, type
, pos
, value
, 0);
6487 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6488 enum isl_dim_type type
, unsigned pos
, int value
)
6490 return map_bound_si(map
, type
, pos
, value
, 1);
6493 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6494 enum isl_dim_type type
, unsigned pos
, int value
)
6496 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6500 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6501 enum isl_dim_type type
, unsigned pos
, int value
)
6503 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6506 /* Bound the given variable of "bmap" from below (or above is "upper"
6507 * is set) to "value".
6509 static __isl_give isl_basic_map
*basic_map_bound(
6510 __isl_take isl_basic_map
*bmap
,
6511 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6515 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6516 return isl_basic_map_free(bmap
);
6517 pos
+= isl_basic_map_offset(bmap
, type
);
6518 bmap
= isl_basic_map_cow(bmap
);
6519 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6520 j
= isl_basic_map_alloc_inequality(bmap
);
6523 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6525 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6526 isl_int_set(bmap
->ineq
[j
][0], value
);
6528 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6529 isl_int_neg(bmap
->ineq
[j
][0], value
);
6531 bmap
= isl_basic_map_simplify(bmap
);
6532 return isl_basic_map_finalize(bmap
);
6534 isl_basic_map_free(bmap
);
6538 /* Bound the given variable of "map" from below (or above is "upper"
6539 * is set) to "value".
6541 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6542 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6546 map
= isl_map_cow(map
);
6550 if (pos
>= isl_map_dim(map
, type
))
6551 isl_die(map
->ctx
, isl_error_invalid
,
6552 "index out of bounds", goto error
);
6553 for (i
= map
->n
- 1; i
>= 0; --i
) {
6554 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6555 map
= remove_if_empty(map
, i
);
6559 map
= isl_map_unmark_normalized(map
);
6566 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6567 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6569 return map_bound(map
, type
, pos
, value
, 0);
6572 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6573 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6575 return map_bound(map
, type
, pos
, value
, 1);
6578 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6579 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6581 return isl_map_lower_bound(set
, type
, pos
, value
);
6584 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6585 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6587 return isl_map_upper_bound(set
, type
, pos
, value
);
6590 /* Force the values of the variable at position "pos" of type "type" of "set"
6591 * to be no smaller than "value".
6593 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6594 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6598 if (!isl_val_is_int(value
))
6599 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6600 "expecting integer value", goto error
);
6601 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6602 isl_val_free(value
);
6605 isl_val_free(value
);
6610 /* Force the values of the variable at position "pos" of type "type" of "set"
6611 * to be no greater than "value".
6613 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6614 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6618 if (!isl_val_is_int(value
))
6619 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6620 "expecting integer value", goto error
);
6621 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6622 isl_val_free(value
);
6625 isl_val_free(value
);
6630 /* Bound the given variable of "bset" from below (or above is "upper"
6631 * is set) to "value".
6633 static __isl_give isl_basic_set
*isl_basic_set_bound(
6634 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6635 isl_int value
, int upper
)
6637 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6638 type
, pos
, value
, upper
));
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_val(
6645 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6646 __isl_take isl_val
*value
, int upper
)
6650 if (!isl_val_is_int(value
))
6651 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6652 "expecting integer value", goto error
);
6653 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6654 isl_val_free(value
);
6657 isl_val_free(value
);
6658 isl_basic_set_free(bset
);
6662 /* Bound the given variable of "bset" from below to "value".
6664 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6665 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6666 __isl_take isl_val
*value
)
6668 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6671 /* Bound the given variable of "bset" from above to "value".
6673 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6674 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6675 __isl_take isl_val
*value
)
6677 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6680 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6684 map
= isl_map_cow(map
);
6688 map
->dim
= isl_space_reverse(map
->dim
);
6691 for (i
= 0; i
< map
->n
; ++i
) {
6692 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6696 map
= isl_map_unmark_normalized(map
);
6704 #define TYPE isl_pw_multi_aff
6706 #define SUFFIX _pw_multi_aff
6708 #define EMPTY isl_pw_multi_aff_empty
6710 #define ADD isl_pw_multi_aff_union_add
6711 #include "isl_map_lexopt_templ.c"
6713 /* Given a map "map", compute the lexicographically minimal
6714 * (or maximal) image element for each domain element in dom,
6715 * in the form of an isl_pw_multi_aff.
6716 * If "empty" is not NULL, then set *empty to those elements in dom that
6717 * do not have an image element.
6718 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6719 * should be computed over the domain of "map". "empty" is also NULL
6722 * We first compute the lexicographically minimal or maximal element
6723 * in the first basic map. This results in a partial solution "res"
6724 * and a subset "todo" of dom that still need to be handled.
6725 * We then consider each of the remaining maps in "map" and successively
6726 * update both "res" and "todo".
6727 * If "empty" is NULL, then the todo sets are not needed and therefore
6728 * also not computed.
6730 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6731 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6732 __isl_give isl_set
**empty
, unsigned flags
)
6736 isl_pw_multi_aff
*res
;
6739 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6740 if (!map
|| (!full
&& !dom
))
6743 if (isl_map_plain_is_empty(map
)) {
6748 return isl_pw_multi_aff_from_map(map
);
6751 res
= basic_map_partial_lexopt_pw_multi_aff(
6752 isl_basic_map_copy(map
->p
[0]),
6753 isl_set_copy(dom
), empty
, flags
);
6757 for (i
= 1; i
< map
->n
; ++i
) {
6758 isl_pw_multi_aff
*res_i
;
6760 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6761 isl_basic_map_copy(map
->p
[i
]),
6762 isl_set_copy(dom
), empty
, flags
);
6764 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6765 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6767 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6770 todo
= isl_set_intersect(todo
, *empty
);
6789 #define TYPE isl_map
6793 #define EMPTY isl_map_empty
6795 #define ADD isl_map_union_disjoint
6796 #include "isl_map_lexopt_templ.c"
6798 /* Given a map "map", compute the lexicographically minimal
6799 * (or maximal) image element for each domain element in "dom",
6800 * in the form of an isl_map.
6801 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6802 * do not have an image element.
6803 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6804 * should be computed over the domain of "map". "empty" is also NULL
6807 * If the input consists of more than one disjunct, then first
6808 * compute the desired result in the form of an isl_pw_multi_aff and
6809 * then convert that into an isl_map.
6811 * This function used to have an explicit implementation in terms
6812 * of isl_maps, but it would continually intersect the domains of
6813 * partial results with the complement of the domain of the next
6814 * partial solution, potentially leading to an explosion in the number
6815 * of disjuncts if there are several disjuncts in the input.
6816 * An even earlier implementation of this function would look for
6817 * better results in the domain of the partial result and for extra
6818 * results in the complement of this domain, which would lead to
6819 * even more splintering.
6821 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6822 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6823 __isl_give isl_set
**empty
, unsigned flags
)
6826 struct isl_map
*res
;
6827 isl_pw_multi_aff
*pma
;
6829 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6830 if (!map
|| (!full
&& !dom
))
6833 if (isl_map_plain_is_empty(map
)) {
6842 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6848 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6850 return isl_map_from_pw_multi_aff(pma
);
6859 __isl_give isl_map
*isl_map_partial_lexmax(
6860 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6861 __isl_give isl_set
**empty
)
6863 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6866 __isl_give isl_map
*isl_map_partial_lexmin(
6867 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6868 __isl_give isl_set
**empty
)
6870 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6873 __isl_give isl_set
*isl_set_partial_lexmin(
6874 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6875 __isl_give isl_set
**empty
)
6877 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6881 __isl_give isl_set
*isl_set_partial_lexmax(
6882 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6883 __isl_give isl_set
**empty
)
6885 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6889 /* Compute the lexicographic minimum (or maximum if "flags" includes
6890 * ISL_OPT_MAX) of "bset" over its parametric domain.
6892 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6895 return isl_basic_map_lexopt(bset
, flags
);
6898 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6900 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6903 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6905 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6908 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6910 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6913 /* Compute the lexicographic minimum of "bset" over its parametric domain
6914 * for the purpose of quantifier elimination.
6915 * That is, find an explicit representation for all the existentially
6916 * quantified variables in "bset" by computing their lexicographic
6919 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6920 __isl_take isl_basic_set
*bset
)
6922 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6925 /* Given a basic map with one output dimension, compute the minimum or
6926 * maximum of that dimension as an isl_pw_aff.
6928 * Compute the optimum as a lexicographic optimum over the single
6929 * output dimension and extract the single isl_pw_aff from the result.
6931 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6934 isl_pw_multi_aff
*pma
;
6937 bmap
= isl_basic_map_copy(bmap
);
6938 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6939 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6940 isl_pw_multi_aff_free(pma
);
6945 /* Compute the minimum or maximum of the given output dimension
6946 * as a function of the parameters and the input dimensions,
6947 * but independently of the other output dimensions.
6949 * We first project out the other output dimension and then compute
6950 * the "lexicographic" maximum in each basic map, combining the results
6951 * using isl_pw_aff_union_max.
6953 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6960 n_out
= isl_map_dim(map
, isl_dim_out
);
6961 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6962 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6967 isl_space
*space
= isl_map_get_space(map
);
6969 return isl_pw_aff_empty(space
);
6972 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6973 for (i
= 1; i
< map
->n
; ++i
) {
6974 isl_pw_aff
*pwaff_i
;
6976 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6977 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6985 /* Compute the minimum of the given output dimension as a function of the
6986 * parameters and input dimensions, but independently of
6987 * the other output dimensions.
6989 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6991 return map_dim_opt(map
, pos
, 0);
6994 /* Compute the maximum of the given output dimension as a function of the
6995 * parameters and input dimensions, but independently of
6996 * the other output dimensions.
6998 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7000 return map_dim_opt(map
, pos
, 1);
7003 /* Compute the minimum or maximum of the given set dimension
7004 * as a function of the parameters,
7005 * but independently of the other set dimensions.
7007 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7010 return map_dim_opt(set
, pos
, max
);
7013 /* Compute the maximum of the given set dimension as a function of the
7014 * parameters, but independently of the other set dimensions.
7016 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7018 return set_dim_opt(set
, pos
, 1);
7021 /* Compute the minimum of the given set dimension as a function of the
7022 * parameters, but independently of the other set dimensions.
7024 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7026 return set_dim_opt(set
, pos
, 0);
7029 /* Apply a preimage specified by "mat" on the parameters of "bset".
7030 * bset is assumed to have only parameters and divs.
7032 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7033 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7040 bset
->dim
= isl_space_cow(bset
->dim
);
7044 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7046 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7048 bset
->dim
->nparam
= 0;
7049 bset
->dim
->n_out
= nparam
;
7050 bset
= isl_basic_set_preimage(bset
, mat
);
7052 bset
->dim
->nparam
= bset
->dim
->n_out
;
7053 bset
->dim
->n_out
= 0;
7058 isl_basic_set_free(bset
);
7062 /* Apply a preimage specified by "mat" on the parameters of "set".
7063 * set is assumed to have only parameters and divs.
7065 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7066 __isl_take isl_mat
*mat
)
7074 nparam
= isl_set_dim(set
, isl_dim_param
);
7076 if (mat
->n_row
!= 1 + nparam
)
7077 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7078 "unexpected number of rows", goto error
);
7080 space
= isl_set_get_space(set
);
7081 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7082 isl_dim_param
, 0, nparam
);
7083 set
= isl_set_reset_space(set
, space
);
7084 set
= isl_set_preimage(set
, mat
);
7085 nparam
= isl_set_dim(set
, isl_dim_out
);
7086 space
= isl_set_get_space(set
);
7087 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7088 isl_dim_out
, 0, nparam
);
7089 set
= isl_set_reset_space(set
, space
);
7097 /* Intersect the basic set "bset" with the affine space specified by the
7098 * equalities in "eq".
7100 static __isl_give isl_basic_set
*basic_set_append_equalities(
7101 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7109 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7114 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7115 for (i
= 0; i
< eq
->n_row
; ++i
) {
7116 k
= isl_basic_set_alloc_equality(bset
);
7119 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7120 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7124 bset
= isl_basic_set_gauss(bset
, NULL
);
7125 bset
= isl_basic_set_finalize(bset
);
7130 isl_basic_set_free(bset
);
7134 /* Intersect the set "set" with the affine space specified by the
7135 * equalities in "eq".
7137 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7145 for (i
= 0; i
< set
->n
; ++i
) {
7146 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7159 /* Given a basic set "bset" that only involves parameters and existentially
7160 * quantified variables, return the index of the first equality
7161 * that only involves parameters. If there is no such equality then
7162 * return bset->n_eq.
7164 * This function assumes that isl_basic_set_gauss has been called on "bset".
7166 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7169 unsigned nparam
, n_div
;
7174 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7175 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7177 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7178 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7185 /* Compute an explicit representation for the existentially quantified
7186 * variables in "bset" by computing the "minimal value" of the set
7187 * variables. Since there are no set variables, the computation of
7188 * the minimal value essentially computes an explicit representation
7189 * of the non-empty part(s) of "bset".
7191 * The input only involves parameters and existentially quantified variables.
7192 * All equalities among parameters have been removed.
7194 * Since the existentially quantified variables in the result are in general
7195 * going to be different from those in the input, we first replace
7196 * them by the minimal number of variables based on their equalities.
7197 * This should simplify the parametric integer programming.
7199 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7201 isl_morph
*morph1
, *morph2
;
7207 if (bset
->n_eq
== 0)
7208 return isl_basic_set_lexmin_compute_divs(bset
);
7210 morph1
= isl_basic_set_parameter_compression(bset
);
7211 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7212 bset
= isl_basic_set_lift(bset
);
7213 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7214 bset
= isl_morph_basic_set(morph2
, bset
);
7215 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7216 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7218 set
= isl_basic_set_lexmin_compute_divs(bset
);
7220 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7225 /* Project the given basic set onto its parameter domain, possibly introducing
7226 * new, explicit, existential variables in the constraints.
7227 * The input has parameters and (possibly implicit) existential variables.
7228 * The output has the same parameters, but only
7229 * explicit existentially quantified variables.
7231 * The actual projection is performed by pip, but pip doesn't seem
7232 * to like equalities very much, so we first remove the equalities
7233 * among the parameters by performing a variable compression on
7234 * the parameters. Afterward, an inverse transformation is performed
7235 * and the equalities among the parameters are inserted back in.
7237 * The variable compression on the parameters may uncover additional
7238 * equalities that were only implicit before. We therefore check
7239 * if there are any new parameter equalities in the result and
7240 * if so recurse. The removal of parameter equalities is required
7241 * for the parameter compression performed by base_compute_divs.
7243 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7247 struct isl_mat
*T
, *T2
;
7248 struct isl_set
*set
;
7251 bset
= isl_basic_set_cow(bset
);
7255 if (bset
->n_eq
== 0)
7256 return base_compute_divs(bset
);
7258 bset
= isl_basic_set_gauss(bset
, NULL
);
7261 if (isl_basic_set_plain_is_empty(bset
))
7262 return isl_set_from_basic_set(bset
);
7264 i
= first_parameter_equality(bset
);
7265 if (i
== bset
->n_eq
)
7266 return base_compute_divs(bset
);
7268 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7269 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7271 eq
= isl_mat_cow(eq
);
7272 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7273 if (T
&& T
->n_col
== 0) {
7277 bset
= isl_basic_set_set_to_empty(bset
);
7278 return isl_set_from_basic_set(bset
);
7280 bset
= basic_set_parameter_preimage(bset
, T
);
7282 i
= first_parameter_equality(bset
);
7285 else if (i
== bset
->n_eq
)
7286 set
= base_compute_divs(bset
);
7288 set
= parameter_compute_divs(bset
);
7289 set
= set_parameter_preimage(set
, T2
);
7290 set
= set_append_equalities(set
, eq
);
7294 /* Insert the divs from "ls" before those of "bmap".
7296 * The number of columns is not changed, which means that the last
7297 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7298 * The caller is responsible for removing the same number of dimensions
7299 * from the space of "bmap".
7301 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7302 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7308 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7312 old_n_div
= bmap
->n_div
;
7313 bmap
= insert_div_rows(bmap
, n_div
);
7317 for (i
= 0; i
< n_div
; ++i
) {
7318 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7319 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7325 /* Replace the space of "bmap" by the space and divs of "ls".
7327 * If "ls" has any divs, then we simplify the result since we may
7328 * have discovered some additional equalities that could simplify
7329 * the div expressions.
7331 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7332 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7336 bmap
= isl_basic_map_cow(bmap
);
7340 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7341 bmap
= insert_divs_from_local_space(bmap
, ls
);
7345 isl_space_free(bmap
->dim
);
7346 bmap
->dim
= isl_local_space_get_space(ls
);
7350 isl_local_space_free(ls
);
7352 bmap
= isl_basic_map_simplify(bmap
);
7353 bmap
= isl_basic_map_finalize(bmap
);
7356 isl_basic_map_free(bmap
);
7357 isl_local_space_free(ls
);
7361 /* Replace the space of "map" by the space and divs of "ls".
7363 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7364 __isl_take isl_local_space
*ls
)
7368 map
= isl_map_cow(map
);
7372 for (i
= 0; i
< map
->n
; ++i
) {
7373 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7374 isl_local_space_copy(ls
));
7378 isl_space_free(map
->dim
);
7379 map
->dim
= isl_local_space_get_space(ls
);
7383 isl_local_space_free(ls
);
7386 isl_local_space_free(ls
);
7391 /* Compute an explicit representation for the existentially
7392 * quantified variables for which do not know any explicit representation yet.
7394 * We first sort the existentially quantified variables so that the
7395 * existentially quantified variables for which we already have an explicit
7396 * representation are placed before those for which we do not.
7397 * The input dimensions, the output dimensions and the existentially
7398 * quantified variables for which we already have an explicit
7399 * representation are then turned into parameters.
7400 * compute_divs returns a map with the same parameters and
7401 * no input or output dimensions and the dimension specification
7402 * is reset to that of the input, including the existentially quantified
7403 * variables for which we already had an explicit representation.
7405 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7407 struct isl_basic_set
*bset
;
7408 struct isl_set
*set
;
7409 struct isl_map
*map
;
7411 isl_local_space
*ls
;
7418 bmap
= isl_basic_map_sort_divs(bmap
);
7419 bmap
= isl_basic_map_cow(bmap
);
7423 n_known
= isl_basic_map_first_unknown_div(bmap
);
7425 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7427 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7428 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7429 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7430 space
= isl_space_set_alloc(bmap
->ctx
,
7431 nparam
+ n_in
+ n_out
+ n_known
, 0);
7435 ls
= isl_basic_map_get_local_space(bmap
);
7436 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7437 n_known
, bmap
->n_div
- n_known
);
7439 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7440 swap_div(bmap
, i
- n_known
, i
);
7441 bmap
->n_div
-= n_known
;
7442 bmap
->extra
-= n_known
;
7444 bmap
= isl_basic_map_reset_space(bmap
, space
);
7445 bset
= bset_from_bmap(bmap
);
7447 set
= parameter_compute_divs(bset
);
7448 map
= set_to_map(set
);
7449 map
= replace_space_by_local_space(map
, ls
);
7453 isl_basic_map_free(bmap
);
7457 /* Remove the explicit representation of local variable "div",
7460 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7461 __isl_take isl_basic_map
*bmap
, int div
)
7465 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7467 return isl_basic_map_free(bmap
);
7471 bmap
= isl_basic_map_cow(bmap
);
7474 isl_int_set_si(bmap
->div
[div
][0], 0);
7478 /* Is local variable "div" of "bmap" marked as not having an explicit
7480 * Note that even if "div" is not marked in this way and therefore
7481 * has an explicit representation, this representation may still
7482 * depend (indirectly) on other local variables that do not
7483 * have an explicit representation.
7485 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7488 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7489 return isl_bool_error
;
7490 return isl_int_is_zero(bmap
->div
[div
][0]);
7493 /* Return the position of the first local variable that does not
7494 * have an explicit representation.
7495 * Return the total number of local variables if they all have
7496 * an explicit representation.
7497 * Return -1 on error.
7499 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7506 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7507 if (!isl_basic_map_div_is_known(bmap
, i
))
7513 /* Return the position of the first local variable that does not
7514 * have an explicit representation.
7515 * Return the total number of local variables if they all have
7516 * an explicit representation.
7517 * Return -1 on error.
7519 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7521 return isl_basic_map_first_unknown_div(bset
);
7524 /* Does "bmap" have an explicit representation for all local variables?
7526 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7530 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7531 first
= isl_basic_map_first_unknown_div(bmap
);
7533 return isl_bool_error
;
7537 /* Do all basic maps in "map" have an explicit representation
7538 * for all local variables?
7540 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7545 return isl_bool_error
;
7547 for (i
= 0; i
< map
->n
; ++i
) {
7548 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7553 return isl_bool_true
;
7556 /* If bmap contains any unknown divs, then compute explicit
7557 * expressions for them. However, this computation may be
7558 * quite expensive, so first try to remove divs that aren't
7561 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7564 struct isl_map
*map
;
7566 known
= isl_basic_map_divs_known(bmap
);
7570 return isl_map_from_basic_map(bmap
);
7572 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7574 known
= isl_basic_map_divs_known(bmap
);
7578 return isl_map_from_basic_map(bmap
);
7580 map
= compute_divs(bmap
);
7583 isl_basic_map_free(bmap
);
7587 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7591 struct isl_map
*res
;
7598 known
= isl_map_divs_known(map
);
7606 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7607 for (i
= 1 ; i
< map
->n
; ++i
) {
7609 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7610 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7611 res
= isl_map_union_disjoint(res
, r2
);
7613 res
= isl_map_union(res
, r2
);
7620 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7622 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7625 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7627 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7630 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7633 struct isl_set
*set
;
7638 map
= isl_map_cow(map
);
7642 set
= set_from_map(map
);
7643 set
->dim
= isl_space_domain(set
->dim
);
7646 for (i
= 0; i
< map
->n
; ++i
) {
7647 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7651 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7652 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7659 /* Return the union of "map1" and "map2", where we assume for now that
7660 * "map1" and "map2" are disjoint. Note that the basic maps inside
7661 * "map1" or "map2" may not be disjoint from each other.
7662 * Also note that this function is also called from isl_map_union,
7663 * which takes care of handling the situation where "map1" and "map2"
7664 * may not be disjoint.
7666 * If one of the inputs is empty, we can simply return the other input.
7667 * Similarly, if one of the inputs is universal, then it is equal to the union.
7669 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7670 __isl_take isl_map
*map2
)
7674 struct isl_map
*map
= NULL
;
7680 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7681 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7682 "spaces don't match", goto error
);
7693 is_universe
= isl_map_plain_is_universe(map1
);
7694 if (is_universe
< 0)
7701 is_universe
= isl_map_plain_is_universe(map2
);
7702 if (is_universe
< 0)
7709 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7710 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7711 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7713 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7714 map1
->n
+ map2
->n
, flags
);
7717 for (i
= 0; i
< map1
->n
; ++i
) {
7718 map
= isl_map_add_basic_map(map
,
7719 isl_basic_map_copy(map1
->p
[i
]));
7723 for (i
= 0; i
< map2
->n
; ++i
) {
7724 map
= isl_map_add_basic_map(map
,
7725 isl_basic_map_copy(map2
->p
[i
]));
7739 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7740 * guaranteed to be disjoint by the caller.
7742 * Note that this functions is called from within isl_map_make_disjoint,
7743 * so we have to be careful not to touch the constraints of the inputs
7746 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7747 __isl_take isl_map
*map2
)
7749 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7752 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7753 * not be disjoint. The parameters are assumed to have been aligned.
7755 * We currently simply call map_union_disjoint, the internal operation
7756 * of which does not really depend on the inputs being disjoint.
7757 * If the result contains more than one basic map, then we clear
7758 * the disjoint flag since the result may contain basic maps from
7759 * both inputs and these are not guaranteed to be disjoint.
7761 * As a special case, if "map1" and "map2" are obviously equal,
7762 * then we simply return "map1".
7764 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7765 __isl_take isl_map
*map2
)
7772 equal
= isl_map_plain_is_equal(map1
, map2
);
7780 map1
= map_union_disjoint(map1
, map2
);
7784 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7792 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7795 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7796 __isl_take isl_map
*map2
)
7798 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7801 __isl_give isl_set
*isl_set_union_disjoint(
7802 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7804 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7808 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7810 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7813 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7816 * "map" and "set" are assumed to be compatible and non-NULL.
7818 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7819 __isl_take isl_set
*set
,
7820 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7821 __isl_take isl_basic_set
*bset
))
7824 struct isl_map
*result
;
7827 if (isl_set_plain_is_universe(set
)) {
7832 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7833 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7834 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7836 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7837 map
->n
* set
->n
, flags
);
7838 for (i
= 0; result
&& i
< map
->n
; ++i
)
7839 for (j
= 0; j
< set
->n
; ++j
) {
7840 result
= isl_map_add_basic_map(result
,
7841 fn(isl_basic_map_copy(map
->p
[i
]),
7842 isl_basic_set_copy(set
->p
[j
])));
7852 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7853 __isl_take isl_set
*set
)
7857 ok
= isl_map_compatible_range(map
, set
);
7861 isl_die(set
->ctx
, isl_error_invalid
,
7862 "incompatible spaces", goto error
);
7864 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7871 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7872 __isl_take isl_set
*set
)
7874 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7877 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7878 __isl_take isl_set
*set
)
7882 ok
= isl_map_compatible_domain(map
, set
);
7886 isl_die(set
->ctx
, isl_error_invalid
,
7887 "incompatible spaces", goto error
);
7889 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7896 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7897 __isl_take isl_set
*set
)
7899 return isl_map_align_params_map_map_and(map
, set
,
7900 &map_intersect_domain
);
7903 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7904 * in the space B -> C, return the intersection.
7905 * The parameters are assumed to have been aligned.
7907 * The map "factor" is first extended to a map living in the space
7908 * [A -> B] -> C and then a regular intersection is computed.
7910 static __isl_give isl_map
*map_intersect_domain_factor_range(
7911 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7914 isl_map
*ext_factor
;
7916 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7917 ext_factor
= isl_map_universe(space
);
7918 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7919 return map_intersect(map
, ext_factor
);
7922 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7923 * in the space B -> C, return the intersection.
7925 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7926 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7928 return isl_map_align_params_map_map_and(map
, factor
,
7929 &map_intersect_domain_factor_range
);
7932 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7933 * in the space A -> C, return the intersection.
7935 * The map "factor" is first extended to a map living in the space
7936 * A -> [B -> C] and then a regular intersection is computed.
7938 static __isl_give isl_map
*map_intersect_range_factor_range(
7939 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7942 isl_map
*ext_factor
;
7944 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7945 ext_factor
= isl_map_universe(space
);
7946 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7947 return isl_map_intersect(map
, ext_factor
);
7950 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7951 * in the space A -> C, return the intersection.
7953 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7954 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7956 return isl_map_align_params_map_map_and(map
, factor
,
7957 &map_intersect_range_factor_range
);
7960 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7961 __isl_take isl_map
*map2
)
7965 map1
= isl_map_reverse(map1
);
7966 map1
= isl_map_apply_range(map1
, map2
);
7967 return isl_map_reverse(map1
);
7974 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7975 __isl_take isl_map
*map2
)
7977 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7980 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7981 __isl_take isl_map
*map2
)
7984 struct isl_map
*result
;
7990 space
= isl_space_join(isl_space_copy(map1
->dim
),
7991 isl_space_copy(map2
->dim
));
7993 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
7996 for (i
= 0; i
< map1
->n
; ++i
)
7997 for (j
= 0; j
< map2
->n
; ++j
) {
7998 result
= isl_map_add_basic_map(result
,
7999 isl_basic_map_apply_range(
8000 isl_basic_map_copy(map1
->p
[i
]),
8001 isl_basic_map_copy(map2
->p
[j
])));
8007 if (result
&& result
->n
<= 1)
8008 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8016 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8017 __isl_take isl_map
*map2
)
8019 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
8023 * returns range - domain
8025 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8027 isl_space
*target_space
;
8028 struct isl_basic_set
*bset
;
8035 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8036 bmap
->dim
, isl_dim_out
),
8038 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8039 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8040 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8041 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8042 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8043 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8044 for (i
= 0; i
< dim
; ++i
) {
8045 int j
= isl_basic_map_alloc_equality(bmap
);
8047 bmap
= isl_basic_map_free(bmap
);
8050 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8051 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8052 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8053 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8055 bset
= isl_basic_map_domain(bmap
);
8056 bset
= isl_basic_set_reset_space(bset
, target_space
);
8059 isl_basic_map_free(bmap
);
8064 * returns range - domain
8066 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8070 struct isl_set
*result
;
8075 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8076 map
->dim
, isl_dim_out
),
8078 dim
= isl_map_get_space(map
);
8079 dim
= isl_space_domain(dim
);
8080 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8083 for (i
= 0; i
< map
->n
; ++i
)
8084 result
= isl_set_add_basic_set(result
,
8085 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8094 * returns [domain -> range] -> range - domain
8096 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8097 __isl_take isl_basic_map
*bmap
)
8101 isl_basic_map
*domain
;
8105 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8106 bmap
->dim
, isl_dim_out
))
8107 isl_die(bmap
->ctx
, isl_error_invalid
,
8108 "domain and range don't match", goto error
);
8110 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8111 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8113 space
= isl_basic_map_get_space(bmap
);
8114 space
= isl_space_from_range(isl_space_domain(space
));
8115 domain
= isl_basic_map_universe(space
);
8117 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8118 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8119 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8121 total
= isl_basic_map_total_dim(bmap
);
8123 for (i
= 0; i
< n
; ++i
) {
8124 k
= isl_basic_map_alloc_equality(bmap
);
8127 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8128 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8129 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8130 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8133 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8134 return isl_basic_map_finalize(bmap
);
8136 isl_basic_map_free(bmap
);
8141 * returns [domain -> range] -> range - domain
8143 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8146 isl_space
*domain_space
;
8151 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8152 map
->dim
, isl_dim_out
))
8153 isl_die(map
->ctx
, isl_error_invalid
,
8154 "domain and range don't match", goto error
);
8156 map
= isl_map_cow(map
);
8160 domain_space
= isl_space_domain(isl_map_get_space(map
));
8161 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8162 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8165 for (i
= 0; i
< map
->n
; ++i
) {
8166 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8170 map
= isl_map_unmark_normalized(map
);
8177 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8179 unsigned n_in
, n_out
;
8183 n_in
= isl_space_dim(space
, isl_dim_in
);
8184 n_out
= isl_space_dim(space
, isl_dim_out
);
8186 isl_die(space
->ctx
, isl_error_invalid
,
8187 "number of input and output dimensions needs to be "
8188 "the same", goto error
);
8189 return isl_basic_map_equal(space
, n_in
);
8191 isl_space_free(space
);
8195 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8197 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8200 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8202 isl_space
*dim
= isl_set_get_space(set
);
8204 id
= isl_map_identity(isl_space_map_from_set(dim
));
8205 return isl_map_intersect_range(id
, set
);
8208 /* Construct a basic set with all set dimensions having only non-negative
8211 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8212 __isl_take isl_space
*space
)
8217 struct isl_basic_set
*bset
;
8221 nparam
= space
->nparam
;
8223 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8226 for (i
= 0; i
< dim
; ++i
) {
8227 int k
= isl_basic_set_alloc_inequality(bset
);
8230 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8231 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8235 isl_basic_set_free(bset
);
8239 /* Construct the half-space x_pos >= 0.
8241 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8245 isl_basic_set
*nonneg
;
8247 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8248 k
= isl_basic_set_alloc_inequality(nonneg
);
8251 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8252 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8254 return isl_basic_set_finalize(nonneg
);
8256 isl_basic_set_free(nonneg
);
8260 /* Construct the half-space x_pos <= -1.
8262 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8268 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8269 k
= isl_basic_set_alloc_inequality(neg
);
8272 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8273 isl_int_set_si(neg
->ineq
[k
][0], -1);
8274 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8276 return isl_basic_set_finalize(neg
);
8278 isl_basic_set_free(neg
);
8282 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8283 enum isl_dim_type type
, unsigned first
, unsigned n
)
8287 isl_basic_set
*nonneg
;
8295 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8297 offset
= pos(set
->dim
, type
);
8298 for (i
= 0; i
< n
; ++i
) {
8299 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8300 offset
+ first
+ i
);
8301 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8303 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8312 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8314 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8320 return isl_stat_error
;
8321 if (isl_set_plain_is_empty(set
)) {
8326 return fn(set
, signs
, user
);
8329 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8331 half
= isl_set_intersect(half
, isl_set_copy(set
));
8332 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8336 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8338 half
= isl_set_intersect(half
, set
);
8339 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8342 return isl_stat_error
;
8345 /* Call "fn" on the intersections of "set" with each of the orthants
8346 * (except for obviously empty intersections). The orthant is identified
8347 * by the signs array, with each entry having value 1 or -1 according
8348 * to the sign of the corresponding variable.
8350 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8351 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8360 return isl_stat_error
;
8361 if (isl_set_plain_is_empty(set
))
8364 nparam
= isl_set_dim(set
, isl_dim_param
);
8365 nvar
= isl_set_dim(set
, isl_dim_set
);
8367 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8369 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8377 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8379 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8382 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8383 __isl_keep isl_basic_map
*bmap2
)
8386 struct isl_map
*map1
;
8387 struct isl_map
*map2
;
8389 if (!bmap1
|| !bmap2
)
8390 return isl_bool_error
;
8392 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8393 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8395 is_subset
= isl_map_is_subset(map1
, map2
);
8403 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8404 __isl_keep isl_basic_set
*bset2
)
8406 return isl_basic_map_is_subset(bset1
, bset2
);
8409 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8410 __isl_keep isl_basic_map
*bmap2
)
8414 if (!bmap1
|| !bmap2
)
8415 return isl_bool_error
;
8416 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8417 if (is_subset
!= isl_bool_true
)
8419 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8423 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8424 __isl_keep isl_basic_set
*bset2
)
8426 return isl_basic_map_is_equal(
8427 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8430 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8436 return isl_bool_error
;
8437 for (i
= 0; i
< map
->n
; ++i
) {
8438 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8440 return isl_bool_error
;
8442 return isl_bool_false
;
8444 return isl_bool_true
;
8447 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8449 return map
? map
->n
== 0 : isl_bool_error
;
8452 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8454 return set
? set
->n
== 0 : isl_bool_error
;
8457 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8459 return isl_map_is_empty(set_to_map(set
));
8462 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8463 __isl_keep isl_map
*map2
)
8466 return isl_bool_error
;
8468 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8471 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8472 __isl_keep isl_set
*set2
)
8475 return isl_bool_error
;
8477 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8480 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8485 return isl_bool_error
;
8486 is_subset
= isl_map_is_subset(map1
, map2
);
8487 if (is_subset
!= isl_bool_true
)
8489 is_subset
= isl_map_is_subset(map2
, map1
);
8493 /* Is "map1" equal to "map2"?
8495 * First check if they are obviously equal.
8496 * If not, then perform a more detailed analysis.
8498 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8502 equal
= isl_map_plain_is_equal(map1
, map2
);
8503 if (equal
< 0 || equal
)
8505 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8508 isl_bool
isl_basic_map_is_strict_subset(
8509 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8513 if (!bmap1
|| !bmap2
)
8514 return isl_bool_error
;
8515 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8516 if (is_subset
!= isl_bool_true
)
8518 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8519 return isl_bool_not(is_subset
);
8522 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8523 __isl_keep isl_map
*map2
)
8528 return isl_bool_error
;
8529 is_subset
= isl_map_is_subset(map1
, map2
);
8530 if (is_subset
!= isl_bool_true
)
8532 is_subset
= isl_map_is_subset(map2
, map1
);
8533 return isl_bool_not(is_subset
);
8536 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8537 __isl_keep isl_set
*set2
)
8539 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8542 /* Is "bmap" obviously equal to the universe with the same space?
8544 * That is, does it not have any constraints?
8546 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8549 return isl_bool_error
;
8550 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8553 /* Is "bset" obviously equal to the universe with the same space?
8555 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8557 return isl_basic_map_plain_is_universe(bset
);
8560 /* If "c" does not involve any existentially quantified variables,
8561 * then set *univ to false and abort
8563 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8565 isl_bool
*univ
= user
;
8568 n
= isl_constraint_dim(c
, isl_dim_div
);
8569 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8570 isl_constraint_free(c
);
8571 if (*univ
< 0 || !*univ
)
8572 return isl_stat_error
;
8576 /* Is "bmap" equal to the universe with the same space?
8578 * First check if it is obviously equal to the universe.
8579 * If not and if there are any constraints not involving
8580 * existentially quantified variables, then it is certainly
8581 * not equal to the universe.
8582 * Otherwise, check if the universe is a subset of "bmap".
8584 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8587 isl_basic_map
*test
;
8589 univ
= isl_basic_map_plain_is_universe(bmap
);
8590 if (univ
< 0 || univ
)
8592 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8593 return isl_bool_false
;
8594 univ
= isl_bool_true
;
8595 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8597 return isl_bool_error
;
8598 if (univ
< 0 || !univ
)
8600 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8601 univ
= isl_basic_map_is_subset(test
, bmap
);
8602 isl_basic_map_free(test
);
8606 /* Is "bset" equal to the universe with the same space?
8608 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8610 return isl_basic_map_is_universe(bset
);
8613 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8618 return isl_bool_error
;
8620 for (i
= 0; i
< map
->n
; ++i
) {
8621 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8626 return isl_bool_false
;
8629 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8631 return isl_map_plain_is_universe(set_to_map(set
));
8634 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8636 struct isl_basic_set
*bset
= NULL
;
8637 struct isl_vec
*sample
= NULL
;
8638 isl_bool empty
, non_empty
;
8641 return isl_bool_error
;
8643 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8644 return isl_bool_true
;
8646 if (isl_basic_map_plain_is_universe(bmap
))
8647 return isl_bool_false
;
8649 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8650 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8651 copy
= isl_basic_map_remove_redundancies(copy
);
8652 empty
= isl_basic_map_plain_is_empty(copy
);
8653 isl_basic_map_free(copy
);
8657 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8659 return isl_bool_error
;
8661 return isl_bool_false
;
8662 isl_vec_free(bmap
->sample
);
8663 bmap
->sample
= NULL
;
8664 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8666 return isl_bool_error
;
8667 sample
= isl_basic_set_sample_vec(bset
);
8669 return isl_bool_error
;
8670 empty
= sample
->size
== 0;
8671 isl_vec_free(bmap
->sample
);
8672 bmap
->sample
= sample
;
8674 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8679 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8682 return isl_bool_error
;
8683 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8686 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8689 return isl_bool_error
;
8690 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8693 /* Is "bmap" known to be non-empty?
8695 * That is, is the cached sample still valid?
8697 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8702 return isl_bool_error
;
8704 return isl_bool_false
;
8705 total
= 1 + isl_basic_map_total_dim(bmap
);
8706 if (bmap
->sample
->size
!= total
)
8707 return isl_bool_false
;
8708 return isl_basic_map_contains(bmap
, bmap
->sample
);
8711 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8713 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8716 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8717 __isl_take isl_basic_map
*bmap2
)
8719 struct isl_map
*map
;
8720 if (!bmap1
|| !bmap2
)
8723 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8725 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8728 map
= isl_map_add_basic_map(map
, bmap1
);
8729 map
= isl_map_add_basic_map(map
, bmap2
);
8732 isl_basic_map_free(bmap1
);
8733 isl_basic_map_free(bmap2
);
8737 struct isl_set
*isl_basic_set_union(
8738 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8740 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8741 bset_to_bmap(bset2
)));
8744 /* Order divs such that any div only depends on previous divs */
8745 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8746 __isl_take isl_basic_map
*bmap
)
8754 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8756 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8758 if (isl_int_is_zero(bmap
->div
[i
][0]))
8760 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8765 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8766 "integer division depends on itself",
8767 return isl_basic_map_free(bmap
));
8768 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8774 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8776 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8779 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8786 for (i
= 0; i
< map
->n
; ++i
) {
8787 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8798 /* Sort the local variables of "bset".
8800 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8801 __isl_take isl_basic_set
*bset
)
8803 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8806 /* Apply the expansion computed by isl_merge_divs.
8807 * The expansion itself is given by "exp" while the resulting
8808 * list of divs is given by "div".
8810 * Move the integer divisions of "bmap" into the right position
8811 * according to "exp" and then introduce the additional integer
8812 * divisions, adding div constraints.
8813 * The moving should be done first to avoid moving coefficients
8814 * in the definitions of the extra integer divisions.
8816 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8817 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8822 bmap
= isl_basic_map_cow(bmap
);
8826 if (div
->n_row
< bmap
->n_div
)
8827 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8828 "not an expansion", goto error
);
8830 n_div
= bmap
->n_div
;
8831 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8832 div
->n_row
- n_div
, 0,
8833 2 * (div
->n_row
- n_div
));
8835 for (i
= n_div
; i
< div
->n_row
; ++i
)
8836 if (isl_basic_map_alloc_div(bmap
) < 0)
8839 for (j
= n_div
- 1; j
>= 0; --j
) {
8842 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8845 for (i
= 0; i
< div
->n_row
; ++i
) {
8846 if (j
< n_div
&& exp
[j
] == i
) {
8849 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8850 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8852 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8860 isl_basic_map_free(bmap
);
8865 /* Apply the expansion computed by isl_merge_divs.
8866 * The expansion itself is given by "exp" while the resulting
8867 * list of divs is given by "div".
8869 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8870 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8872 return isl_basic_map_expand_divs(bset
, div
, exp
);
8875 /* Look for a div in dst that corresponds to the div "div" in src.
8876 * The divs before "div" in src and dst are assumed to be the same.
8878 * Returns -1 if no corresponding div was found and the position
8879 * of the corresponding div in dst otherwise.
8881 static int find_div(__isl_keep isl_basic_map
*dst
,
8882 __isl_keep isl_basic_map
*src
, unsigned div
)
8886 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8888 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8889 for (i
= div
; i
< dst
->n_div
; ++i
)
8890 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8891 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8892 dst
->n_div
- div
) == -1)
8897 /* Align the divs of "dst" to those of "src", adding divs from "src"
8898 * if needed. That is, make sure that the first src->n_div divs
8899 * of the result are equal to those of src.
8901 * The result is not finalized as by design it will have redundant
8902 * divs if any divs from "src" were copied.
8904 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8905 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8913 return isl_basic_map_free(dst
);
8915 if (src
->n_div
== 0)
8918 known
= isl_basic_map_divs_known(src
);
8920 return isl_basic_map_free(dst
);
8922 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8923 "some src divs are unknown",
8924 return isl_basic_map_free(dst
));
8926 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8928 return isl_basic_map_free(dst
);
8931 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8932 for (i
= 0; i
< src
->n_div
; ++i
) {
8933 int j
= find_div(dst
, src
, i
);
8936 int extra
= src
->n_div
- i
;
8937 dst
= isl_basic_map_cow(dst
);
8940 dst
= isl_basic_map_extend_space(dst
,
8941 isl_space_copy(dst
->dim
),
8942 extra
, 0, 2 * extra
);
8945 j
= isl_basic_map_alloc_div(dst
);
8948 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8949 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8950 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8954 isl_basic_map_swap_div(dst
, i
, j
);
8956 isl_basic_map_free(src
);
8959 isl_basic_map_free(src
);
8960 isl_basic_map_free(dst
);
8964 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8972 map
= isl_map_compute_divs(map
);
8973 map
= isl_map_cow(map
);
8977 for (i
= 1; i
< map
->n
; ++i
)
8978 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8979 for (i
= 1; i
< map
->n
; ++i
) {
8980 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8982 return isl_map_free(map
);
8985 map
= isl_map_unmark_normalized(map
);
8989 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8991 return isl_map_align_divs_internal(map
);
8994 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8996 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8999 /* Align the divs of the basic maps in "map" to those
9000 * of the basic maps in "list", as well as to the other basic maps in "map".
9001 * The elements in "list" are assumed to have known divs.
9003 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9004 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9008 map
= isl_map_compute_divs(map
);
9009 map
= isl_map_cow(map
);
9011 return isl_map_free(map
);
9015 n
= isl_basic_map_list_n_basic_map(list
);
9016 for (i
= 0; i
< n
; ++i
) {
9017 isl_basic_map
*bmap
;
9019 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9020 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9021 isl_basic_map_free(bmap
);
9024 return isl_map_free(map
);
9026 return isl_map_align_divs_internal(map
);
9029 /* Align the divs of each element of "list" to those of "bmap".
9030 * Both "bmap" and the elements of "list" are assumed to have known divs.
9032 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9033 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9038 return isl_basic_map_list_free(list
);
9040 n
= isl_basic_map_list_n_basic_map(list
);
9041 for (i
= 0; i
< n
; ++i
) {
9042 isl_basic_map
*bmap_i
;
9044 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9045 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9046 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9052 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
9053 __isl_take isl_map
*map
)
9057 ok
= isl_map_compatible_domain(map
, set
);
9061 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9062 "incompatible spaces", goto error
);
9063 map
= isl_map_intersect_domain(map
, set
);
9064 set
= isl_map_range(map
);
9072 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9073 __isl_take isl_map
*map
)
9075 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9078 /* There is no need to cow as removing empty parts doesn't change
9079 * the meaning of the set.
9081 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9088 for (i
= map
->n
- 1; i
>= 0; --i
)
9089 map
= remove_if_empty(map
, i
);
9094 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9096 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9099 /* Create a binary relation that maps the shared initial "pos" dimensions
9100 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9102 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9103 __isl_keep isl_basic_set
*bset2
, int pos
)
9105 isl_basic_map
*bmap1
;
9106 isl_basic_map
*bmap2
;
9108 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9109 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9110 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9111 isl_dim_out
, 0, pos
);
9112 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9113 isl_dim_out
, 0, pos
);
9114 return isl_basic_map_range_product(bmap1
, bmap2
);
9117 /* Given two basic sets bset1 and bset2, compute the maximal difference
9118 * between the values of dimension pos in bset1 and those in bset2
9119 * for any common value of the parameters and dimensions preceding pos.
9121 static enum isl_lp_result
basic_set_maximal_difference_at(
9122 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9123 int pos
, isl_int
*opt
)
9125 isl_basic_map
*bmap1
;
9126 struct isl_ctx
*ctx
;
9127 struct isl_vec
*obj
;
9131 enum isl_lp_result res
;
9133 if (!bset1
|| !bset2
)
9134 return isl_lp_error
;
9136 nparam
= isl_basic_set_n_param(bset1
);
9137 dim1
= isl_basic_set_n_dim(bset1
);
9139 bmap1
= join_initial(bset1
, bset2
, pos
);
9141 return isl_lp_error
;
9143 total
= isl_basic_map_total_dim(bmap1
);
9145 obj
= isl_vec_alloc(ctx
, 1 + total
);
9148 isl_seq_clr(obj
->block
.data
, 1 + total
);
9149 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9150 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9151 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9153 isl_basic_map_free(bmap1
);
9157 isl_basic_map_free(bmap1
);
9158 return isl_lp_error
;
9161 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9162 * for any common value of the parameters and dimensions preceding pos
9163 * in both basic sets, the values of dimension pos in bset1 are
9164 * smaller or larger than those in bset2.
9167 * 1 if bset1 follows bset2
9168 * -1 if bset1 precedes bset2
9169 * 0 if bset1 and bset2 are incomparable
9170 * -2 if some error occurred.
9172 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9173 __isl_keep isl_basic_set
*bset2
, int pos
)
9176 enum isl_lp_result res
;
9181 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9183 if (res
== isl_lp_empty
)
9185 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9186 res
== isl_lp_unbounded
)
9188 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9197 /* Given two basic sets bset1 and bset2, check whether
9198 * for any common value of the parameters and dimensions preceding pos
9199 * there is a value of dimension pos in bset1 that is larger
9200 * than a value of the same dimension in bset2.
9203 * 1 if there exists such a pair
9204 * 0 if there is no such pair, but there is a pair of equal values
9206 * -2 if some error occurred.
9208 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9209 __isl_keep isl_basic_set
*bset2
, int pos
)
9212 isl_basic_map
*bmap
;
9215 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9216 bmap
= join_initial(bset1
, bset2
, pos
);
9217 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9218 isl_dim_out
, dim1
- pos
);
9219 empty
= isl_basic_map_is_empty(bmap
);
9223 isl_basic_map_free(bmap
);
9226 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9227 isl_dim_out
, dim1
- pos
);
9228 empty
= isl_basic_map_is_empty(bmap
);
9231 isl_basic_map_free(bmap
);
9236 isl_basic_map_free(bmap
);
9240 /* Given two sets set1 and set2, check whether
9241 * for any common value of the parameters and dimensions preceding pos
9242 * there is a value of dimension pos in set1 that is larger
9243 * than a value of the same dimension in set2.
9246 * 1 if there exists such a pair
9247 * 0 if there is no such pair, but there is a pair of equal values
9249 * -2 if some error occurred.
9251 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9252 __isl_keep isl_set
*set2
, int pos
)
9260 for (i
= 0; i
< set1
->n
; ++i
)
9261 for (j
= 0; j
< set2
->n
; ++j
) {
9263 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9264 if (f
== 1 || f
== -2)
9273 static isl_bool
isl_basic_map_plain_has_fixed_var(
9274 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9281 return isl_bool_error
;
9282 total
= isl_basic_map_total_dim(bmap
);
9283 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9284 for (; d
+1 > pos
; --d
)
9285 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9289 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9290 return isl_bool_false
;
9291 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9292 return isl_bool_false
;
9293 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9294 return isl_bool_false
;
9296 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9297 return isl_bool_true
;
9299 return isl_bool_false
;
9302 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9303 unsigned pos
, isl_int
*val
)
9311 return isl_bool_error
;
9313 return isl_bool_false
;
9315 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9318 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9319 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9320 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9321 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9322 fixed
= isl_bool_false
;
9325 isl_int_set(*val
, v
);
9331 static isl_bool
isl_basic_set_plain_has_fixed_var(
9332 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9334 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9338 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9339 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9341 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9342 return isl_bool_error
;
9343 return isl_basic_map_plain_has_fixed_var(bmap
,
9344 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9347 /* If "bmap" obviously lies on a hyperplane where the given dimension
9348 * has a fixed value, then return that value.
9349 * Otherwise return NaN.
9351 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9352 __isl_keep isl_basic_map
*bmap
,
9353 enum isl_dim_type type
, unsigned pos
)
9361 ctx
= isl_basic_map_get_ctx(bmap
);
9362 v
= isl_val_alloc(ctx
);
9365 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9367 return isl_val_free(v
);
9369 isl_int_set_si(v
->d
, 1);
9373 return isl_val_nan(ctx
);
9376 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9377 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9379 if (pos
>= isl_map_dim(map
, type
))
9380 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9381 "position out of bounds", return isl_bool_error
);
9382 return isl_map_plain_has_fixed_var(map
,
9383 map_offset(map
, type
) - 1 + pos
, val
);
9386 /* If "map" obviously lies on a hyperplane where the given dimension
9387 * has a fixed value, then return that value.
9388 * Otherwise return NaN.
9390 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9391 enum isl_dim_type type
, unsigned pos
)
9399 ctx
= isl_map_get_ctx(map
);
9400 v
= isl_val_alloc(ctx
);
9403 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9405 return isl_val_free(v
);
9407 isl_int_set_si(v
->d
, 1);
9411 return isl_val_nan(ctx
);
9414 /* If "set" obviously lies on a hyperplane where the given dimension
9415 * has a fixed value, then return that value.
9416 * Otherwise return NaN.
9418 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9419 enum isl_dim_type type
, unsigned pos
)
9421 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9424 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9425 * then return this fixed value in *val.
9427 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9428 unsigned dim
, isl_int
*val
)
9430 return isl_basic_set_plain_has_fixed_var(bset
,
9431 isl_basic_set_n_param(bset
) + dim
, val
);
9434 /* Return -1 if the constraint "c1" should be sorted before "c2"
9435 * and 1 if it should be sorted after "c2".
9436 * Return 0 if the two constraints are the same (up to the constant term).
9438 * In particular, if a constraint involves later variables than another
9439 * then it is sorted after this other constraint.
9440 * uset_gist depends on constraints without existentially quantified
9441 * variables sorting first.
9443 * For constraints that have the same latest variable, those
9444 * with the same coefficient for this latest variable (first in absolute value
9445 * and then in actual value) are grouped together.
9446 * This is useful for detecting pairs of constraints that can
9447 * be chained in their printed representation.
9449 * Finally, within a group, constraints are sorted according to
9450 * their coefficients (excluding the constant term).
9452 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9454 isl_int
**c1
= (isl_int
**) p1
;
9455 isl_int
**c2
= (isl_int
**) p2
;
9457 unsigned size
= *(unsigned *) arg
;
9460 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9461 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9466 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9469 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9473 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9476 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9477 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9478 * and 0 if the two constraints are the same (up to the constant term).
9480 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9481 isl_int
*c1
, isl_int
*c2
)
9487 total
= isl_basic_map_total_dim(bmap
);
9488 return sort_constraint_cmp(&c1
, &c2
, &total
);
9491 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9492 __isl_take isl_basic_map
*bmap
)
9498 if (bmap
->n_ineq
== 0)
9500 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9502 total
= isl_basic_map_total_dim(bmap
);
9503 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9504 &sort_constraint_cmp
, &total
) < 0)
9505 return isl_basic_map_free(bmap
);
9506 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9510 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9511 __isl_take isl_basic_set
*bset
)
9513 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9514 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9517 __isl_give isl_basic_map
*isl_basic_map_normalize(
9518 __isl_take isl_basic_map
*bmap
)
9520 bmap
= isl_basic_map_remove_redundancies(bmap
);
9521 bmap
= isl_basic_map_sort_constraints(bmap
);
9524 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9525 __isl_keep isl_basic_map
*bmap2
)
9529 isl_space
*space1
, *space2
;
9531 if (!bmap1
|| !bmap2
)
9536 space1
= isl_basic_map_peek_space(bmap1
);
9537 space2
= isl_basic_map_peek_space(bmap2
);
9538 cmp
= isl_space_cmp(space1
, space2
);
9541 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9542 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9543 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9544 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9545 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9547 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9549 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9551 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9552 return bmap1
->n_eq
- bmap2
->n_eq
;
9553 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9554 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9555 if (bmap1
->n_div
!= bmap2
->n_div
)
9556 return bmap1
->n_div
- bmap2
->n_div
;
9557 total
= isl_basic_map_total_dim(bmap1
);
9558 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9559 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9563 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9564 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9568 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9569 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9576 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9577 __isl_keep isl_basic_set
*bset2
)
9579 return isl_basic_map_plain_cmp(bset1
, bset2
);
9582 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9588 if (set1
->n
!= set2
->n
)
9589 return set1
->n
- set2
->n
;
9591 for (i
= 0; i
< set1
->n
; ++i
) {
9592 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9600 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9601 __isl_keep isl_basic_map
*bmap2
)
9603 if (!bmap1
|| !bmap2
)
9604 return isl_bool_error
;
9605 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9608 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9609 __isl_keep isl_basic_set
*bset2
)
9611 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9612 bset_to_bmap(bset2
));
9615 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9617 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9618 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9620 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9623 /* Sort the basic maps of "map" and remove duplicate basic maps.
9625 * While removing basic maps, we make sure that the basic maps remain
9626 * sorted because isl_map_normalize expects the basic maps of the result
9629 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9633 map
= isl_map_remove_empty_parts(map
);
9636 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9637 for (i
= map
->n
- 1; i
>= 1; --i
) {
9638 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9640 isl_basic_map_free(map
->p
[i
-1]);
9641 for (j
= i
; j
< map
->n
; ++j
)
9642 map
->p
[j
- 1] = map
->p
[j
];
9649 /* Remove obvious duplicates among the basic maps of "map".
9651 * Unlike isl_map_normalize, this function does not remove redundant
9652 * constraints and only removes duplicates that have exactly the same
9653 * constraints in the input. It does sort the constraints and
9654 * the basic maps to ease the detection of duplicates.
9656 * If "map" has already been normalized or if the basic maps are
9657 * disjoint, then there can be no duplicates.
9659 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9662 isl_basic_map
*bmap
;
9668 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9670 for (i
= 0; i
< map
->n
; ++i
) {
9671 bmap
= isl_basic_map_copy(map
->p
[i
]);
9672 bmap
= isl_basic_map_sort_constraints(bmap
);
9674 return isl_map_free(map
);
9675 isl_basic_map_free(map
->p
[i
]);
9679 map
= sort_and_remove_duplicates(map
);
9683 /* We normalize in place, but if anything goes wrong we need
9684 * to return NULL, so we need to make sure we don't change the
9685 * meaning of any possible other copies of map.
9687 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9690 struct isl_basic_map
*bmap
;
9694 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9696 for (i
= 0; i
< map
->n
; ++i
) {
9697 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9700 isl_basic_map_free(map
->p
[i
]);
9704 map
= sort_and_remove_duplicates(map
);
9706 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9713 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9715 return set_from_map(isl_map_normalize(set_to_map(set
)));
9718 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9719 __isl_keep isl_map
*map2
)
9725 return isl_bool_error
;
9728 return isl_bool_true
;
9729 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9730 return isl_bool_false
;
9732 map1
= isl_map_copy(map1
);
9733 map2
= isl_map_copy(map2
);
9734 map1
= isl_map_normalize(map1
);
9735 map2
= isl_map_normalize(map2
);
9738 equal
= map1
->n
== map2
->n
;
9739 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9740 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9750 return isl_bool_error
;
9753 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9754 __isl_keep isl_set
*set2
)
9756 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9759 /* Return the basic maps in "map" as a list.
9761 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9762 __isl_keep isl_map
*map
)
9766 isl_basic_map_list
*list
;
9770 ctx
= isl_map_get_ctx(map
);
9771 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9773 for (i
= 0; i
< map
->n
; ++i
) {
9774 isl_basic_map
*bmap
;
9776 bmap
= isl_basic_map_copy(map
->p
[i
]);
9777 list
= isl_basic_map_list_add(list
, bmap
);
9783 /* Return the intersection of the elements in the non-empty list "list".
9784 * All elements are assumed to live in the same space.
9786 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9787 __isl_take isl_basic_map_list
*list
)
9790 isl_basic_map
*bmap
;
9794 n
= isl_basic_map_list_n_basic_map(list
);
9796 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9797 "expecting non-empty list", goto error
);
9799 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9800 for (i
= 1; i
< n
; ++i
) {
9801 isl_basic_map
*bmap_i
;
9803 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9804 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9807 isl_basic_map_list_free(list
);
9810 isl_basic_map_list_free(list
);
9814 /* Return the intersection of the elements in the non-empty list "list".
9815 * All elements are assumed to live in the same space.
9817 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9818 __isl_take isl_basic_set_list
*list
)
9820 return isl_basic_map_list_intersect(list
);
9823 /* Return the union of the elements of "list".
9824 * The list is required to have at least one element.
9826 __isl_give isl_set
*isl_basic_set_list_union(
9827 __isl_take isl_basic_set_list
*list
)
9831 isl_basic_set
*bset
;
9836 n
= isl_basic_set_list_n_basic_set(list
);
9838 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9839 "expecting non-empty list", goto error
);
9841 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9842 space
= isl_basic_set_get_space(bset
);
9843 isl_basic_set_free(bset
);
9845 set
= isl_set_alloc_space(space
, n
, 0);
9846 for (i
= 0; i
< n
; ++i
) {
9847 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9848 set
= isl_set_add_basic_set(set
, bset
);
9851 isl_basic_set_list_free(list
);
9854 isl_basic_set_list_free(list
);
9858 /* Return the union of the elements in the non-empty list "list".
9859 * All elements are assumed to live in the same space.
9861 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9868 n
= isl_set_list_n_set(list
);
9870 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9871 "expecting non-empty list", goto error
);
9873 set
= isl_set_list_get_set(list
, 0);
9874 for (i
= 1; i
< n
; ++i
) {
9877 set_i
= isl_set_list_get_set(list
, i
);
9878 set
= isl_set_union(set
, set_i
);
9881 isl_set_list_free(list
);
9884 isl_set_list_free(list
);
9888 __isl_give isl_basic_map
*isl_basic_map_product(
9889 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9891 isl_space
*space_result
= NULL
;
9892 struct isl_basic_map
*bmap
;
9893 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9894 struct isl_dim_map
*dim_map1
, *dim_map2
;
9896 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9898 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9899 isl_space_copy(bmap2
->dim
));
9901 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9902 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9903 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9904 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9905 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9907 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9908 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9909 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9910 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9911 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9912 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9913 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9914 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9915 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9916 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9917 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9919 bmap
= isl_basic_map_alloc_space(space_result
,
9920 bmap1
->n_div
+ bmap2
->n_div
,
9921 bmap1
->n_eq
+ bmap2
->n_eq
,
9922 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9923 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9924 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9925 bmap
= isl_basic_map_simplify(bmap
);
9926 return isl_basic_map_finalize(bmap
);
9928 isl_basic_map_free(bmap1
);
9929 isl_basic_map_free(bmap2
);
9933 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9934 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9936 isl_basic_map
*prod
;
9938 prod
= isl_basic_map_product(bmap1
, bmap2
);
9939 prod
= isl_basic_map_flatten(prod
);
9943 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9944 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9946 return isl_basic_map_flat_range_product(bset1
, bset2
);
9949 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9950 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9952 isl_space
*space_result
= NULL
;
9953 isl_basic_map
*bmap
;
9954 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9955 struct isl_dim_map
*dim_map1
, *dim_map2
;
9957 if (!bmap1
|| !bmap2
)
9960 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9961 isl_space_copy(bmap2
->dim
));
9963 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9964 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9965 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9966 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9968 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9969 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9970 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9971 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9972 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9973 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9974 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9975 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9976 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9977 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9978 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9980 bmap
= isl_basic_map_alloc_space(space_result
,
9981 bmap1
->n_div
+ bmap2
->n_div
,
9982 bmap1
->n_eq
+ bmap2
->n_eq
,
9983 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9984 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9985 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9986 bmap
= isl_basic_map_simplify(bmap
);
9987 return isl_basic_map_finalize(bmap
);
9989 isl_basic_map_free(bmap1
);
9990 isl_basic_map_free(bmap2
);
9994 __isl_give isl_basic_map
*isl_basic_map_range_product(
9995 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9998 isl_space
*space_result
= NULL
;
9999 isl_basic_map
*bmap
;
10000 unsigned in
, out1
, out2
, nparam
, total
, pos
;
10001 struct isl_dim_map
*dim_map1
, *dim_map2
;
10003 rational
= isl_basic_map_is_rational(bmap1
);
10004 if (rational
>= 0 && rational
)
10005 rational
= isl_basic_map_is_rational(bmap2
);
10006 if (!bmap1
|| !bmap2
|| rational
< 0)
10009 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10012 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10013 isl_space_copy(bmap2
->dim
));
10015 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10016 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10017 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10018 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10020 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10021 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10022 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10023 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10024 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10025 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10026 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10027 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10028 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10029 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10030 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10032 bmap
= isl_basic_map_alloc_space(space_result
,
10033 bmap1
->n_div
+ bmap2
->n_div
,
10034 bmap1
->n_eq
+ bmap2
->n_eq
,
10035 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10036 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10037 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10039 bmap
= isl_basic_map_set_rational(bmap
);
10040 bmap
= isl_basic_map_simplify(bmap
);
10041 return isl_basic_map_finalize(bmap
);
10043 isl_basic_map_free(bmap1
);
10044 isl_basic_map_free(bmap2
);
10048 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10049 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10051 isl_basic_map
*prod
;
10053 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10054 prod
= isl_basic_map_flatten_range(prod
);
10058 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10059 * and collect the results.
10060 * The result live in the space obtained by calling "space_product"
10061 * on the spaces of "map1" and "map2".
10062 * If "remove_duplicates" is set then the result may contain duplicates
10063 * (even if the inputs do not) and so we try and remove the obvious
10066 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10067 __isl_take isl_map
*map2
,
10068 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10069 __isl_take isl_space
*right
),
10070 __isl_give isl_basic_map
*(*basic_map_product
)(
10071 __isl_take isl_basic_map
*left
,
10072 __isl_take isl_basic_map
*right
),
10073 int remove_duplicates
)
10075 unsigned flags
= 0;
10076 struct isl_map
*result
;
10080 m
= isl_map_has_equal_params(map1
, map2
);
10084 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10085 "parameters don't match", goto error
);
10087 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10088 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10089 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10091 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10092 isl_space_copy(map2
->dim
)),
10093 map1
->n
* map2
->n
, flags
);
10096 for (i
= 0; i
< map1
->n
; ++i
)
10097 for (j
= 0; j
< map2
->n
; ++j
) {
10098 struct isl_basic_map
*part
;
10099 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10100 isl_basic_map_copy(map2
->p
[j
]));
10101 if (isl_basic_map_is_empty(part
))
10102 isl_basic_map_free(part
);
10104 result
= isl_map_add_basic_map(result
, part
);
10108 if (remove_duplicates
)
10109 result
= isl_map_remove_obvious_duplicates(result
);
10110 isl_map_free(map1
);
10111 isl_map_free(map2
);
10114 isl_map_free(map1
);
10115 isl_map_free(map2
);
10119 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10121 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10122 __isl_take isl_map
*map2
)
10124 return map_product(map1
, map2
, &isl_space_product
,
10125 &isl_basic_map_product
, 0);
10128 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10129 __isl_take isl_map
*map2
)
10131 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10134 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10136 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10137 __isl_take isl_map
*map2
)
10141 prod
= isl_map_product(map1
, map2
);
10142 prod
= isl_map_flatten(prod
);
10146 /* Given two set A and B, construct its Cartesian product A x B.
10148 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10150 return isl_map_range_product(set1
, set2
);
10153 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10154 __isl_take isl_set
*set2
)
10156 return isl_map_flat_range_product(set1
, set2
);
10159 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10161 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10162 __isl_take isl_map
*map2
)
10164 return map_product(map1
, map2
, &isl_space_domain_product
,
10165 &isl_basic_map_domain_product
, 1);
10168 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10170 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10171 __isl_take isl_map
*map2
)
10173 return map_product(map1
, map2
, &isl_space_range_product
,
10174 &isl_basic_map_range_product
, 1);
10177 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10178 __isl_take isl_map
*map2
)
10180 return isl_map_align_params_map_map_and(map1
, map2
,
10181 &map_domain_product_aligned
);
10184 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10185 __isl_take isl_map
*map2
)
10187 return isl_map_align_params_map_map_and(map1
, map2
,
10188 &map_range_product_aligned
);
10191 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10193 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10196 int total1
, keep1
, total2
, keep2
;
10200 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10201 !isl_space_range_is_wrapping(map
->dim
))
10202 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10203 "not a product", return isl_map_free(map
));
10205 space
= isl_map_get_space(map
);
10206 total1
= isl_space_dim(space
, isl_dim_in
);
10207 total2
= isl_space_dim(space
, isl_dim_out
);
10208 space
= isl_space_factor_domain(space
);
10209 keep1
= isl_space_dim(space
, isl_dim_in
);
10210 keep2
= isl_space_dim(space
, isl_dim_out
);
10211 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10212 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10213 map
= isl_map_reset_space(map
, space
);
10218 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10220 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10223 int total1
, keep1
, total2
, keep2
;
10227 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10228 !isl_space_range_is_wrapping(map
->dim
))
10229 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10230 "not a product", return isl_map_free(map
));
10232 space
= isl_map_get_space(map
);
10233 total1
= isl_space_dim(space
, isl_dim_in
);
10234 total2
= isl_space_dim(space
, isl_dim_out
);
10235 space
= isl_space_factor_range(space
);
10236 keep1
= isl_space_dim(space
, isl_dim_in
);
10237 keep2
= isl_space_dim(space
, isl_dim_out
);
10238 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10239 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10240 map
= isl_map_reset_space(map
, space
);
10245 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10247 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10254 if (!isl_space_domain_is_wrapping(map
->dim
))
10255 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10256 "domain is not a product", return isl_map_free(map
));
10258 space
= isl_map_get_space(map
);
10259 total
= isl_space_dim(space
, isl_dim_in
);
10260 space
= isl_space_domain_factor_domain(space
);
10261 keep
= isl_space_dim(space
, isl_dim_in
);
10262 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10263 map
= isl_map_reset_space(map
, space
);
10268 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10270 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10277 if (!isl_space_domain_is_wrapping(map
->dim
))
10278 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10279 "domain is not a product", return isl_map_free(map
));
10281 space
= isl_map_get_space(map
);
10282 total
= isl_space_dim(space
, isl_dim_in
);
10283 space
= isl_space_domain_factor_range(space
);
10284 keep
= isl_space_dim(space
, isl_dim_in
);
10285 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10286 map
= isl_map_reset_space(map
, space
);
10291 /* Given a map A -> [B -> C], extract the map A -> B.
10293 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10300 if (!isl_space_range_is_wrapping(map
->dim
))
10301 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10302 "range is not a product", return isl_map_free(map
));
10304 space
= isl_map_get_space(map
);
10305 total
= isl_space_dim(space
, isl_dim_out
);
10306 space
= isl_space_range_factor_domain(space
);
10307 keep
= isl_space_dim(space
, isl_dim_out
);
10308 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10309 map
= isl_map_reset_space(map
, space
);
10314 /* Given a map A -> [B -> C], extract the map A -> C.
10316 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10323 if (!isl_space_range_is_wrapping(map
->dim
))
10324 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10325 "range is not a product", return isl_map_free(map
));
10327 space
= isl_map_get_space(map
);
10328 total
= isl_space_dim(space
, isl_dim_out
);
10329 space
= isl_space_range_factor_range(space
);
10330 keep
= isl_space_dim(space
, isl_dim_out
);
10331 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10332 map
= isl_map_reset_space(map
, space
);
10337 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10339 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10340 __isl_take isl_map
*map2
)
10344 prod
= isl_map_domain_product(map1
, map2
);
10345 prod
= isl_map_flatten_domain(prod
);
10349 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10351 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10352 __isl_take isl_map
*map2
)
10356 prod
= isl_map_range_product(map1
, map2
);
10357 prod
= isl_map_flatten_range(prod
);
10361 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10364 uint32_t hash
= isl_hash_init();
10369 bmap
= isl_basic_map_copy(bmap
);
10370 bmap
= isl_basic_map_normalize(bmap
);
10373 total
= isl_basic_map_total_dim(bmap
);
10374 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10375 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10377 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10378 isl_hash_hash(hash
, c_hash
);
10380 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10381 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10383 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10384 isl_hash_hash(hash
, c_hash
);
10386 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10387 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10389 if (isl_int_is_zero(bmap
->div
[i
][0]))
10391 isl_hash_byte(hash
, i
& 0xFF);
10392 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10393 isl_hash_hash(hash
, c_hash
);
10395 isl_basic_map_free(bmap
);
10399 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10401 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10404 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10411 map
= isl_map_copy(map
);
10412 map
= isl_map_normalize(map
);
10416 hash
= isl_hash_init();
10417 for (i
= 0; i
< map
->n
; ++i
) {
10418 uint32_t bmap_hash
;
10419 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10420 isl_hash_hash(hash
, bmap_hash
);
10428 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10430 return isl_map_get_hash(set_to_map(set
));
10433 /* Return the number of basic maps in the (current) representation of "map".
10435 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10437 return map
? map
->n
: 0;
10440 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10442 return set
? set
->n
: 0;
10445 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10446 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10451 return isl_stat_error
;
10453 for (i
= 0; i
< map
->n
; ++i
)
10454 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10455 return isl_stat_error
;
10457 return isl_stat_ok
;
10460 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10461 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10466 return isl_stat_error
;
10468 for (i
= 0; i
< set
->n
; ++i
)
10469 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10470 return isl_stat_error
;
10472 return isl_stat_ok
;
10475 /* Return a list of basic sets, the union of which is equal to "set".
10477 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10478 __isl_keep isl_set
*set
)
10481 isl_basic_set_list
*list
;
10486 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10487 for (i
= 0; i
< set
->n
; ++i
) {
10488 isl_basic_set
*bset
;
10490 bset
= isl_basic_set_copy(set
->p
[i
]);
10491 list
= isl_basic_set_list_add(list
, bset
);
10497 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10504 bset
= isl_basic_set_cow(bset
);
10508 space
= isl_basic_set_get_space(bset
);
10509 space
= isl_space_lift(space
, bset
->n_div
);
10512 isl_space_free(bset
->dim
);
10514 bset
->extra
-= bset
->n_div
;
10517 bset
= isl_basic_set_finalize(bset
);
10521 isl_basic_set_free(bset
);
10525 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10531 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10536 set
= isl_set_cow(set
);
10540 n_div
= set
->p
[0]->n_div
;
10541 space
= isl_set_get_space(set
);
10542 space
= isl_space_lift(space
, n_div
);
10545 isl_space_free(set
->dim
);
10548 for (i
= 0; i
< set
->n
; ++i
) {
10549 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10560 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10568 dim
= isl_basic_set_total_dim(bset
);
10569 size
+= bset
->n_eq
* (1 + dim
);
10570 size
+= bset
->n_ineq
* (1 + dim
);
10571 size
+= bset
->n_div
* (2 + dim
);
10576 int isl_set_size(__isl_keep isl_set
*set
)
10584 for (i
= 0; i
< set
->n
; ++i
)
10585 size
+= isl_basic_set_size(set
->p
[i
]);
10590 /* Check if there is any lower bound (if lower == 0) and/or upper
10591 * bound (if upper == 0) on the specified dim.
10593 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10594 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10598 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10599 return isl_bool_error
;
10601 pos
+= isl_basic_map_offset(bmap
, type
);
10603 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10604 if (isl_int_is_zero(bmap
->div
[i
][0]))
10606 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10607 return isl_bool_true
;
10610 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10611 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10612 return isl_bool_true
;
10614 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10615 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10622 return lower
&& upper
;
10625 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10626 enum isl_dim_type type
, unsigned pos
)
10628 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10631 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10632 enum isl_dim_type type
, unsigned pos
)
10634 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10637 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10638 enum isl_dim_type type
, unsigned pos
)
10640 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10643 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10644 enum isl_dim_type type
, unsigned pos
)
10649 return isl_bool_error
;
10651 for (i
= 0; i
< map
->n
; ++i
) {
10653 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10654 if (bounded
< 0 || !bounded
)
10658 return isl_bool_true
;
10661 /* Return true if the specified dim is involved in both an upper bound
10662 * and a lower bound.
10664 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10665 enum isl_dim_type type
, unsigned pos
)
10667 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10670 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10672 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10673 enum isl_dim_type type
, unsigned pos
,
10674 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10675 enum isl_dim_type type
, unsigned pos
))
10680 return isl_bool_error
;
10682 for (i
= 0; i
< map
->n
; ++i
) {
10684 bounded
= fn(map
->p
[i
], type
, pos
);
10685 if (bounded
< 0 || bounded
)
10689 return isl_bool_false
;
10692 /* Return 1 if the specified dim is involved in any lower bound.
10694 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10695 enum isl_dim_type type
, unsigned pos
)
10697 return has_any_bound(set
, type
, pos
,
10698 &isl_basic_map_dim_has_lower_bound
);
10701 /* Return 1 if the specified dim is involved in any upper bound.
10703 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10704 enum isl_dim_type type
, unsigned pos
)
10706 return has_any_bound(set
, type
, pos
,
10707 &isl_basic_map_dim_has_upper_bound
);
10710 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10712 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10713 enum isl_dim_type type
, unsigned pos
,
10714 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10715 enum isl_dim_type type
, unsigned pos
))
10720 return isl_bool_error
;
10722 for (i
= 0; i
< map
->n
; ++i
) {
10724 bounded
= fn(map
->p
[i
], type
, pos
);
10725 if (bounded
< 0 || !bounded
)
10729 return isl_bool_true
;
10732 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10734 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10735 enum isl_dim_type type
, unsigned pos
)
10737 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10740 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10742 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10743 enum isl_dim_type type
, unsigned pos
)
10745 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10748 /* For each of the "n" variables starting at "first", determine
10749 * the sign of the variable and put the results in the first "n"
10750 * elements of the array "signs".
10752 * 1 means that the variable is non-negative
10753 * -1 means that the variable is non-positive
10754 * 0 means the variable attains both positive and negative values.
10756 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10757 unsigned first
, unsigned n
, int *signs
)
10759 isl_vec
*bound
= NULL
;
10760 struct isl_tab
*tab
= NULL
;
10761 struct isl_tab_undo
*snap
;
10764 if (!bset
|| !signs
)
10765 return isl_stat_error
;
10767 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10768 tab
= isl_tab_from_basic_set(bset
, 0);
10769 if (!bound
|| !tab
)
10772 isl_seq_clr(bound
->el
, bound
->size
);
10773 isl_int_set_si(bound
->el
[0], -1);
10775 snap
= isl_tab_snap(tab
);
10776 for (i
= 0; i
< n
; ++i
) {
10779 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10780 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10782 empty
= tab
->empty
;
10783 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10784 if (isl_tab_rollback(tab
, snap
) < 0)
10792 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10793 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10795 empty
= tab
->empty
;
10796 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10797 if (isl_tab_rollback(tab
, snap
) < 0)
10800 signs
[i
] = empty
? -1 : 0;
10804 isl_vec_free(bound
);
10805 return isl_stat_ok
;
10808 isl_vec_free(bound
);
10809 return isl_stat_error
;
10812 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10813 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10815 if (!bset
|| !signs
)
10816 return isl_stat_error
;
10817 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10818 return isl_stat_error
);
10820 first
+= pos(bset
->dim
, type
) - 1;
10821 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10824 /* Is it possible for the integer division "div" to depend (possibly
10825 * indirectly) on any output dimensions?
10827 * If the div is undefined, then we conservatively assume that it
10828 * may depend on them.
10829 * Otherwise, we check if it actually depends on them or on any integer
10830 * divisions that may depend on them.
10832 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10835 unsigned n_out
, o_out
;
10836 unsigned n_div
, o_div
;
10838 if (isl_int_is_zero(bmap
->div
[div
][0]))
10839 return isl_bool_true
;
10841 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10842 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10844 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10845 return isl_bool_true
;
10847 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10848 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10850 for (i
= 0; i
< n_div
; ++i
) {
10851 isl_bool may_involve
;
10853 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10855 may_involve
= div_may_involve_output(bmap
, i
);
10856 if (may_involve
< 0 || may_involve
)
10857 return may_involve
;
10860 return isl_bool_false
;
10863 /* Return the first integer division of "bmap" in the range
10864 * [first, first + n[ that may depend on any output dimensions and
10865 * that has a non-zero coefficient in "c" (where the first coefficient
10866 * in "c" corresponds to integer division "first").
10868 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10869 isl_int
*c
, int first
, int n
)
10876 for (k
= first
; k
< first
+ n
; ++k
) {
10877 isl_bool may_involve
;
10879 if (isl_int_is_zero(c
[k
]))
10881 may_involve
= div_may_involve_output(bmap
, k
);
10882 if (may_involve
< 0)
10891 /* Look for a pair of inequality constraints in "bmap" of the form
10893 * -l + i >= 0 or i >= l
10895 * n + l - i >= 0 or i <= l + n
10897 * with n < "m" and i the output dimension at position "pos".
10898 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10899 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10900 * and earlier output dimensions, as well as integer divisions that do
10901 * not involve any of the output dimensions.
10903 * Return the index of the first inequality constraint or bmap->n_ineq
10904 * if no such pair can be found.
10906 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10907 int pos
, isl_int m
)
10912 unsigned n_div
, o_div
;
10913 unsigned n_out
, o_out
;
10919 ctx
= isl_basic_map_get_ctx(bmap
);
10920 total
= isl_basic_map_total_dim(bmap
);
10921 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10922 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10923 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10924 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10925 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10926 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10928 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10929 n_out
- (pos
+ 1)) != -1)
10931 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10934 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10935 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10938 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10939 bmap
->ineq
[j
] + 1, total
))
10943 if (j
>= bmap
->n_ineq
)
10945 isl_int_add(bmap
->ineq
[i
][0],
10946 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10947 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10948 isl_int_sub(bmap
->ineq
[i
][0],
10949 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10952 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10958 return bmap
->n_ineq
;
10961 /* Return the index of the equality of "bmap" that defines
10962 * the output dimension "pos" in terms of earlier dimensions.
10963 * The equality may also involve integer divisions, as long
10964 * as those integer divisions are defined in terms of
10965 * parameters or input dimensions.
10966 * In this case, *div is set to the number of integer divisions and
10967 * *ineq is set to the number of inequality constraints (provided
10968 * div and ineq are not NULL).
10970 * The equality may also involve a single integer division involving
10971 * the output dimensions (typically only output dimension "pos") as
10972 * long as the coefficient of output dimension "pos" is 1 or -1 and
10973 * there is a pair of constraints i >= l and i <= l + n, with i referring
10974 * to output dimension "pos", l an expression involving only earlier
10975 * dimensions and n smaller than the coefficient of the integer division
10976 * in the equality. In this case, the output dimension can be defined
10977 * in terms of a modulo expression that does not involve the integer division.
10978 * *div is then set to this single integer division and
10979 * *ineq is set to the index of constraint i >= l.
10981 * Return bmap->n_eq if there is no such equality.
10982 * Return -1 on error.
10984 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10985 int pos
, int *div
, int *ineq
)
10988 unsigned n_out
, o_out
;
10989 unsigned n_div
, o_div
;
10994 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10995 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10996 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10997 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11000 *ineq
= bmap
->n_ineq
;
11003 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11004 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11006 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11007 n_out
- (pos
+ 1)) != -1)
11009 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11013 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11014 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11016 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11017 k
+ 1, n_div
- (k
+1)) < n_div
)
11019 l
= find_modulo_constraint_pair(bmap
, pos
,
11020 bmap
->eq
[j
][o_div
+ k
]);
11023 if (l
>= bmap
->n_ineq
)
11035 /* Check if the given basic map is obviously single-valued.
11036 * In particular, for each output dimension, check that there is
11037 * an equality that defines the output dimension in terms of
11038 * earlier dimensions.
11040 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11046 return isl_bool_error
;
11048 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11050 for (i
= 0; i
< n_out
; ++i
) {
11053 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11056 return isl_bool_error
;
11057 if (eq
>= bmap
->n_eq
)
11058 return isl_bool_false
;
11061 return isl_bool_true
;
11064 /* Check if the given basic map is single-valued.
11065 * We simply compute
11069 * and check if the result is a subset of the identity mapping.
11071 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11074 isl_basic_map
*test
;
11078 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11082 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11083 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11085 space
= isl_basic_map_get_space(bmap
);
11086 space
= isl_space_map_from_set(isl_space_range(space
));
11087 id
= isl_basic_map_identity(space
);
11089 sv
= isl_basic_map_is_subset(test
, id
);
11091 isl_basic_map_free(test
);
11092 isl_basic_map_free(id
);
11097 /* Check if the given map is obviously single-valued.
11099 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11102 return isl_bool_error
;
11104 return isl_bool_true
;
11106 return isl_bool_false
;
11108 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11111 /* Check if the given map is single-valued.
11112 * We simply compute
11116 * and check if the result is a subset of the identity mapping.
11118 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11125 sv
= isl_map_plain_is_single_valued(map
);
11129 test
= isl_map_reverse(isl_map_copy(map
));
11130 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11132 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11133 id
= isl_map_identity(dim
);
11135 sv
= isl_map_is_subset(test
, id
);
11137 isl_map_free(test
);
11143 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11147 map
= isl_map_copy(map
);
11148 map
= isl_map_reverse(map
);
11149 in
= isl_map_is_single_valued(map
);
11155 /* Check if the given map is obviously injective.
11157 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11161 map
= isl_map_copy(map
);
11162 map
= isl_map_reverse(map
);
11163 in
= isl_map_plain_is_single_valued(map
);
11169 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11173 sv
= isl_map_is_single_valued(map
);
11177 return isl_map_is_injective(map
);
11180 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11182 return isl_map_is_single_valued(set_to_map(set
));
11185 /* Does "map" only map elements to themselves?
11187 * If the domain and range spaces are different, then "map"
11188 * is considered not to be an identity relation, even if it is empty.
11189 * Otherwise, construct the maximal identity relation and
11190 * check whether "map" is a subset of this relation.
11192 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11196 isl_bool equal
, is_identity
;
11198 space
= isl_map_get_space(map
);
11199 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11200 isl_space_free(space
);
11201 if (equal
< 0 || !equal
)
11204 id
= isl_map_identity(isl_map_get_space(map
));
11205 is_identity
= isl_map_is_subset(map
, id
);
11208 return is_identity
;
11211 int isl_map_is_translation(__isl_keep isl_map
*map
)
11216 delta
= isl_map_deltas(isl_map_copy(map
));
11217 ok
= isl_set_is_singleton(delta
);
11218 isl_set_free(delta
);
11223 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11225 if (isl_seq_first_non_zero(p
, pos
) != -1)
11227 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11232 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11239 return isl_bool_error
;
11241 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11242 return isl_bool_false
;
11244 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11245 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11246 for (j
= 0; j
< nvar
; ++j
) {
11247 int lower
= 0, upper
= 0;
11248 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11249 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11251 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11252 return isl_bool_false
;
11255 if (i
< bset
->n_eq
)
11257 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11258 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11260 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11261 return isl_bool_false
;
11262 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11267 if (!lower
|| !upper
)
11268 return isl_bool_false
;
11271 return isl_bool_true
;
11274 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11277 return isl_bool_error
;
11279 return isl_bool_false
;
11281 return isl_basic_set_is_box(set
->p
[0]);
11284 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11287 return isl_bool_error
;
11289 return isl_space_is_wrapping(bset
->dim
);
11292 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11295 return isl_bool_error
;
11297 return isl_space_is_wrapping(set
->dim
);
11300 /* Modify the space of "map" through a call to "change".
11301 * If "can_change" is set (not NULL), then first call it to check
11302 * if the modification is allowed, printing the error message "cannot_change"
11305 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11306 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11307 const char *cannot_change
,
11308 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11316 ok
= can_change
? can_change(map
) : isl_bool_true
;
11318 return isl_map_free(map
);
11320 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11321 return isl_map_free(map
));
11323 space
= change(isl_map_get_space(map
));
11324 map
= isl_map_reset_space(map
, space
);
11329 /* Is the domain of "map" a wrapped relation?
11331 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11334 return isl_bool_error
;
11336 return isl_space_domain_is_wrapping(map
->dim
);
11339 /* Does "map" have a wrapped relation in both domain and range?
11341 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11343 return isl_space_is_product(isl_map_peek_space(map
));
11346 /* Is the range of "map" a wrapped relation?
11348 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11351 return isl_bool_error
;
11353 return isl_space_range_is_wrapping(map
->dim
);
11356 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11358 bmap
= isl_basic_map_cow(bmap
);
11362 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11366 bmap
= isl_basic_map_finalize(bmap
);
11368 return bset_from_bmap(bmap
);
11370 isl_basic_map_free(bmap
);
11374 /* Given a map A -> B, return the set (A -> B).
11376 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11378 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11381 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11383 bset
= isl_basic_set_cow(bset
);
11387 bset
->dim
= isl_space_unwrap(bset
->dim
);
11391 bset
= isl_basic_set_finalize(bset
);
11393 return bset_to_bmap(bset
);
11395 isl_basic_set_free(bset
);
11399 /* Given a set (A -> B), return the map A -> B.
11400 * Error out if "set" is not of the form (A -> B).
11402 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11404 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11405 "not a wrapping set", &isl_space_unwrap
);
11408 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11409 enum isl_dim_type type
)
11414 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11417 bmap
= isl_basic_map_cow(bmap
);
11421 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11425 bmap
= isl_basic_map_finalize(bmap
);
11429 isl_basic_map_free(bmap
);
11433 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11434 enum isl_dim_type type
)
11441 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11444 map
= isl_map_cow(map
);
11448 for (i
= 0; i
< map
->n
; ++i
) {
11449 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11453 map
->dim
= isl_space_reset(map
->dim
, type
);
11463 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11468 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11471 bmap
= isl_basic_map_cow(bmap
);
11475 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11479 bmap
= isl_basic_map_finalize(bmap
);
11483 isl_basic_map_free(bmap
);
11487 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11489 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11492 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11493 __isl_take isl_basic_map
*bmap
)
11498 if (!bmap
->dim
->nested
[0])
11501 bmap
= isl_basic_map_cow(bmap
);
11505 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11509 bmap
= isl_basic_map_finalize(bmap
);
11513 isl_basic_map_free(bmap
);
11517 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11518 __isl_take isl_basic_map
*bmap
)
11523 if (!bmap
->dim
->nested
[1])
11526 bmap
= isl_basic_map_cow(bmap
);
11530 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11534 bmap
= isl_basic_map_finalize(bmap
);
11538 isl_basic_map_free(bmap
);
11542 /* Remove any internal structure from the spaces of domain and range of "map".
11544 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11549 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11552 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11555 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11557 return set_from_map(isl_map_flatten(set_to_map(set
)));
11560 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11562 isl_space
*space
, *flat_space
;
11565 space
= isl_set_get_space(set
);
11566 flat_space
= isl_space_flatten(isl_space_copy(space
));
11567 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11569 map
= isl_map_intersect_domain(map
, set
);
11574 /* Remove any internal structure from the space of the domain of "map".
11576 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11581 if (!map
->dim
->nested
[0])
11584 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11587 /* Remove any internal structure from the space of the range of "map".
11589 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11594 if (!map
->dim
->nested
[1])
11597 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11600 /* Reorder the dimensions of "bmap" according to the given dim_map
11601 * and set the dimension specification to "space" and
11602 * perform Gaussian elimination on the result.
11604 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11605 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11607 isl_basic_map
*res
;
11611 if (!bmap
|| !space
|| !dim_map
)
11614 flags
= bmap
->flags
;
11615 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11616 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11617 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11618 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11619 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11620 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11622 res
->flags
= flags
;
11623 res
= isl_basic_map_gauss(res
, NULL
);
11624 res
= isl_basic_map_finalize(res
);
11628 isl_basic_map_free(bmap
);
11629 isl_space_free(space
);
11633 /* Reorder the dimensions of "map" according to given reordering.
11635 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11636 __isl_take isl_reordering
*r
)
11639 struct isl_dim_map
*dim_map
;
11641 map
= isl_map_cow(map
);
11642 dim_map
= isl_dim_map_from_reordering(r
);
11643 if (!map
|| !r
|| !dim_map
)
11646 for (i
= 0; i
< map
->n
; ++i
) {
11647 struct isl_dim_map
*dim_map_i
;
11650 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11652 space
= isl_reordering_get_space(r
);
11653 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11659 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11660 map
= isl_map_unmark_normalized(map
);
11662 isl_reordering_free(r
);
11668 isl_reordering_free(r
);
11672 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11673 __isl_take isl_reordering
*r
)
11675 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11678 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11679 __isl_take isl_space
*model
)
11684 if (!map
|| !model
)
11687 ctx
= isl_space_get_ctx(model
);
11688 if (!isl_space_has_named_params(model
))
11689 isl_die(ctx
, isl_error_invalid
,
11690 "model has unnamed parameters", goto error
);
11691 if (isl_map_check_named_params(map
) < 0)
11693 aligned
= isl_map_space_has_equal_params(map
, model
);
11697 isl_reordering
*exp
;
11699 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11700 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11701 map
= isl_map_realign(map
, exp
);
11704 isl_space_free(model
);
11707 isl_space_free(model
);
11712 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11713 __isl_take isl_space
*model
)
11715 return isl_map_align_params(set
, model
);
11718 /* Align the parameters of "bmap" to those of "model", introducing
11719 * additional parameters if needed.
11721 __isl_give isl_basic_map
*isl_basic_map_align_params(
11722 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11725 isl_bool equal_params
;
11727 if (!bmap
|| !model
)
11730 ctx
= isl_space_get_ctx(model
);
11731 if (!isl_space_has_named_params(model
))
11732 isl_die(ctx
, isl_error_invalid
,
11733 "model has unnamed parameters", goto error
);
11734 if (isl_basic_map_check_named_params(bmap
) < 0)
11736 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11737 if (equal_params
< 0)
11739 if (!equal_params
) {
11740 isl_reordering
*exp
;
11741 struct isl_dim_map
*dim_map
;
11743 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11744 exp
= isl_reordering_extend_space(exp
,
11745 isl_basic_map_get_space(bmap
));
11746 dim_map
= isl_dim_map_from_reordering(exp
);
11747 bmap
= isl_basic_map_realign(bmap
,
11748 isl_reordering_get_space(exp
),
11749 isl_dim_map_extend(dim_map
, bmap
));
11750 isl_reordering_free(exp
);
11754 isl_space_free(model
);
11757 isl_space_free(model
);
11758 isl_basic_map_free(bmap
);
11762 /* Do "bset" and "space" have the same parameters?
11764 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11765 __isl_keep isl_space
*space
)
11767 isl_space
*bset_space
;
11769 bset_space
= isl_basic_set_peek_space(bset
);
11770 return isl_space_has_equal_params(bset_space
, space
);
11773 /* Do "map" and "space" have the same parameters?
11775 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11776 __isl_keep isl_space
*space
)
11778 isl_space
*map_space
;
11780 map_space
= isl_map_peek_space(map
);
11781 return isl_space_has_equal_params(map_space
, space
);
11784 /* Do "set" and "space" have the same parameters?
11786 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11787 __isl_keep isl_space
*space
)
11789 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11792 /* Align the parameters of "bset" to those of "model", introducing
11793 * additional parameters if needed.
11795 __isl_give isl_basic_set
*isl_basic_set_align_params(
11796 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11798 return isl_basic_map_align_params(bset
, model
);
11801 /* Drop all parameters not referenced by "map".
11803 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11807 if (isl_map_check_named_params(map
) < 0)
11808 return isl_map_free(map
);
11810 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11813 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11815 return isl_map_free(map
);
11817 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11823 /* Drop all parameters not referenced by "set".
11825 __isl_give isl_set
*isl_set_drop_unused_params(
11826 __isl_take isl_set
*set
)
11828 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11831 /* Drop all parameters not referenced by "bmap".
11833 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11834 __isl_take isl_basic_map
*bmap
)
11838 if (isl_basic_map_check_named_params(bmap
) < 0)
11839 return isl_basic_map_free(bmap
);
11841 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11844 involves
= isl_basic_map_involves_dims(bmap
,
11845 isl_dim_param
, i
, 1);
11847 return isl_basic_map_free(bmap
);
11849 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11855 /* Drop all parameters not referenced by "bset".
11857 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11858 __isl_take isl_basic_set
*bset
)
11860 return bset_from_bmap(isl_basic_map_drop_unused_params(
11861 bset_to_bmap(bset
)));
11864 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11865 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11866 enum isl_dim_type c2
, enum isl_dim_type c3
,
11867 enum isl_dim_type c4
, enum isl_dim_type c5
)
11869 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11870 struct isl_mat
*mat
;
11876 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11877 isl_basic_map_total_dim(bmap
) + 1);
11880 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11881 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11882 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11883 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11884 isl_int_set(mat
->row
[i
][pos
],
11885 bmap
->eq
[i
][off
+ k
]);
11893 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11894 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11895 enum isl_dim_type c2
, enum isl_dim_type c3
,
11896 enum isl_dim_type c4
, enum isl_dim_type c5
)
11898 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11899 struct isl_mat
*mat
;
11905 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11906 isl_basic_map_total_dim(bmap
) + 1);
11909 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11910 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11911 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11912 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11913 isl_int_set(mat
->row
[i
][pos
],
11914 bmap
->ineq
[i
][off
+ k
]);
11922 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11923 __isl_take isl_space
*space
,
11924 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11925 enum isl_dim_type c2
, enum isl_dim_type c3
,
11926 enum isl_dim_type c4
, enum isl_dim_type c5
)
11928 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11929 isl_basic_map
*bmap
= NULL
;
11935 if (!space
|| !eq
|| !ineq
)
11938 if (eq
->n_col
!= ineq
->n_col
)
11939 isl_die(space
->ctx
, isl_error_invalid
,
11940 "equalities and inequalities matrices should have "
11941 "same number of columns", goto error
);
11943 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11945 if (eq
->n_col
< total
)
11946 isl_die(space
->ctx
, isl_error_invalid
,
11947 "number of columns too small", goto error
);
11949 extra
= eq
->n_col
- total
;
11951 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11952 eq
->n_row
, ineq
->n_row
);
11955 for (i
= 0; i
< extra
; ++i
) {
11956 k
= isl_basic_map_alloc_div(bmap
);
11959 isl_int_set_si(bmap
->div
[k
][0], 0);
11961 for (i
= 0; i
< eq
->n_row
; ++i
) {
11962 l
= isl_basic_map_alloc_equality(bmap
);
11965 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11966 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11967 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11968 isl_int_set(bmap
->eq
[l
][off
+ k
],
11974 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11975 l
= isl_basic_map_alloc_inequality(bmap
);
11978 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11979 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11980 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11981 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11982 ineq
->row
[i
][pos
]);
11988 isl_space_free(space
);
11990 isl_mat_free(ineq
);
11992 bmap
= isl_basic_map_simplify(bmap
);
11993 return isl_basic_map_finalize(bmap
);
11995 isl_space_free(space
);
11997 isl_mat_free(ineq
);
11998 isl_basic_map_free(bmap
);
12002 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12003 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12004 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12006 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12007 c1
, c2
, c3
, c4
, isl_dim_in
);
12010 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12011 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12012 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12014 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12015 c1
, c2
, c3
, c4
, isl_dim_in
);
12018 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12019 __isl_take isl_space
*dim
,
12020 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12021 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12023 isl_basic_map
*bmap
;
12024 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
12025 c1
, c2
, c3
, c4
, isl_dim_in
);
12026 return bset_from_bmap(bmap
);
12029 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12032 return isl_bool_error
;
12034 return isl_space_can_zip(bmap
->dim
);
12037 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12040 return isl_bool_error
;
12042 return isl_space_can_zip(map
->dim
);
12045 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12046 * (A -> C) -> (B -> D).
12048 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12057 if (!isl_basic_map_can_zip(bmap
))
12058 isl_die(bmap
->ctx
, isl_error_invalid
,
12059 "basic map cannot be zipped", goto error
);
12060 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12061 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12062 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12063 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12064 bmap
= isl_basic_map_cow(bmap
);
12065 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12068 bmap
->dim
= isl_space_zip(bmap
->dim
);
12071 bmap
= isl_basic_map_mark_final(bmap
);
12074 isl_basic_map_free(bmap
);
12078 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12079 * (A -> C) -> (B -> D).
12081 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12088 if (!isl_map_can_zip(map
))
12089 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12092 map
= isl_map_cow(map
);
12096 for (i
= 0; i
< map
->n
; ++i
) {
12097 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12102 map
->dim
= isl_space_zip(map
->dim
);
12112 /* Can we apply isl_basic_map_curry to "bmap"?
12113 * That is, does it have a nested relation in its domain?
12115 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12118 return isl_bool_error
;
12120 return isl_space_can_curry(bmap
->dim
);
12123 /* Can we apply isl_map_curry to "map"?
12124 * That is, does it have a nested relation in its domain?
12126 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12129 return isl_bool_error
;
12131 return isl_space_can_curry(map
->dim
);
12134 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12137 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12143 if (!isl_basic_map_can_curry(bmap
))
12144 isl_die(bmap
->ctx
, isl_error_invalid
,
12145 "basic map cannot be curried", goto error
);
12146 bmap
= isl_basic_map_cow(bmap
);
12149 bmap
->dim
= isl_space_curry(bmap
->dim
);
12152 bmap
= isl_basic_map_mark_final(bmap
);
12155 isl_basic_map_free(bmap
);
12159 /* Given a map (A -> B) -> C, return the corresponding map
12162 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12164 return isl_map_change_space(map
, &isl_map_can_curry
,
12165 "map cannot be curried", &isl_space_curry
);
12168 /* Can isl_map_range_curry be applied to "map"?
12169 * That is, does it have a nested relation in its range,
12170 * the domain of which is itself a nested relation?
12172 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12175 return isl_bool_error
;
12177 return isl_space_can_range_curry(map
->dim
);
12180 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12181 * A -> (B -> (C -> D)).
12183 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12185 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12186 "map range cannot be curried",
12187 &isl_space_range_curry
);
12190 /* Can we apply isl_basic_map_uncurry to "bmap"?
12191 * That is, does it have a nested relation in its domain?
12193 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12196 return isl_bool_error
;
12198 return isl_space_can_uncurry(bmap
->dim
);
12201 /* Can we apply isl_map_uncurry to "map"?
12202 * That is, does it have a nested relation in its domain?
12204 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12207 return isl_bool_error
;
12209 return isl_space_can_uncurry(map
->dim
);
12212 /* Given a basic map A -> (B -> C), return the corresponding basic map
12215 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12221 if (!isl_basic_map_can_uncurry(bmap
))
12222 isl_die(bmap
->ctx
, isl_error_invalid
,
12223 "basic map cannot be uncurried",
12224 return isl_basic_map_free(bmap
));
12225 bmap
= isl_basic_map_cow(bmap
);
12228 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12230 return isl_basic_map_free(bmap
);
12231 bmap
= isl_basic_map_mark_final(bmap
);
12235 /* Given a map A -> (B -> C), return the corresponding map
12238 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12240 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12241 "map cannot be uncurried", &isl_space_uncurry
);
12244 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12245 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12247 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12250 /* Construct a basic map where the given dimensions are equal to each other.
12252 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12253 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12255 isl_basic_map
*bmap
= NULL
;
12261 if (pos1
>= isl_space_dim(space
, type1
))
12262 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12263 "index out of bounds", goto error
);
12264 if (pos2
>= isl_space_dim(space
, type2
))
12265 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12266 "index out of bounds", goto error
);
12268 if (type1
== type2
&& pos1
== pos2
)
12269 return isl_basic_map_universe(space
);
12271 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12272 i
= isl_basic_map_alloc_equality(bmap
);
12275 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12276 pos1
+= isl_basic_map_offset(bmap
, type1
);
12277 pos2
+= isl_basic_map_offset(bmap
, type2
);
12278 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12279 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12280 bmap
= isl_basic_map_finalize(bmap
);
12281 isl_space_free(space
);
12284 isl_space_free(space
);
12285 isl_basic_map_free(bmap
);
12289 /* Add a constraint imposing that the given two dimensions are equal.
12291 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12292 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12296 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12298 bmap
= isl_basic_map_intersect(bmap
, eq
);
12303 /* Add a constraint imposing that the given two dimensions are equal.
12305 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12306 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12308 isl_basic_map
*bmap
;
12310 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12312 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12317 /* Add a constraint imposing that the given two dimensions have opposite values.
12319 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12320 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12322 isl_basic_map
*bmap
= NULL
;
12328 if (pos1
>= isl_map_dim(map
, type1
))
12329 isl_die(map
->ctx
, isl_error_invalid
,
12330 "index out of bounds", goto error
);
12331 if (pos2
>= isl_map_dim(map
, type2
))
12332 isl_die(map
->ctx
, isl_error_invalid
,
12333 "index out of bounds", goto error
);
12335 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12336 i
= isl_basic_map_alloc_equality(bmap
);
12339 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12340 pos1
+= isl_basic_map_offset(bmap
, type1
);
12341 pos2
+= isl_basic_map_offset(bmap
, type2
);
12342 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12343 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12344 bmap
= isl_basic_map_finalize(bmap
);
12346 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12350 isl_basic_map_free(bmap
);
12355 /* Construct a constraint imposing that the value of the first dimension is
12356 * greater than or equal to that of the second.
12358 static __isl_give isl_constraint
*constraint_order_ge(
12359 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12360 enum isl_dim_type type2
, int pos2
)
12367 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12369 if (pos1
>= isl_constraint_dim(c
, type1
))
12370 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12371 "index out of bounds", return isl_constraint_free(c
));
12372 if (pos2
>= isl_constraint_dim(c
, type2
))
12373 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12374 "index out of bounds", return isl_constraint_free(c
));
12376 if (type1
== type2
&& pos1
== pos2
)
12379 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12380 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12385 /* Add a constraint imposing that the value of the first dimension is
12386 * greater than or equal to that of the second.
12388 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12389 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12394 if (type1
== type2
&& pos1
== pos2
)
12396 space
= isl_basic_map_get_space(bmap
);
12397 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12398 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12403 /* Add a constraint imposing that the value of the first dimension is
12404 * greater than or equal to that of the second.
12406 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12407 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12412 if (type1
== type2
&& pos1
== pos2
)
12414 space
= isl_map_get_space(map
);
12415 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12416 map
= isl_map_add_constraint(map
, c
);
12421 /* Add a constraint imposing that the value of the first dimension is
12422 * less than or equal to that of the second.
12424 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12425 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12427 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12430 /* Construct a basic map where the value of the first dimension is
12431 * greater than that of the second.
12433 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12434 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12436 isl_basic_map
*bmap
= NULL
;
12442 if (pos1
>= isl_space_dim(space
, type1
))
12443 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12444 "index out of bounds", goto error
);
12445 if (pos2
>= isl_space_dim(space
, type2
))
12446 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12447 "index out of bounds", goto error
);
12449 if (type1
== type2
&& pos1
== pos2
)
12450 return isl_basic_map_empty(space
);
12452 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12453 i
= isl_basic_map_alloc_inequality(bmap
);
12455 return isl_basic_map_free(bmap
);
12456 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12457 pos1
+= isl_basic_map_offset(bmap
, type1
);
12458 pos2
+= isl_basic_map_offset(bmap
, type2
);
12459 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12460 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12461 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12462 bmap
= isl_basic_map_finalize(bmap
);
12466 isl_space_free(space
);
12467 isl_basic_map_free(bmap
);
12471 /* Add a constraint imposing that the value of the first dimension is
12472 * greater than that of the second.
12474 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12475 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12479 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12481 bmap
= isl_basic_map_intersect(bmap
, gt
);
12486 /* Add a constraint imposing that the value of the first dimension is
12487 * greater than that of the second.
12489 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12490 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12492 isl_basic_map
*bmap
;
12494 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12496 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12501 /* Add a constraint imposing that the value of the first dimension is
12502 * smaller than that of the second.
12504 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12505 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12507 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12510 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12514 isl_local_space
*ls
;
12519 if (!isl_basic_map_divs_known(bmap
))
12520 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12521 "some divs are unknown", return NULL
);
12523 ls
= isl_basic_map_get_local_space(bmap
);
12524 div
= isl_local_space_get_div(ls
, pos
);
12525 isl_local_space_free(ls
);
12530 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12533 return isl_basic_map_get_div(bset
, pos
);
12536 /* Plug in "subs" for dimension "type", "pos" of "bset".
12538 * Let i be the dimension to replace and let "subs" be of the form
12542 * Any integer division with a non-zero coefficient for i,
12544 * floor((a i + g)/m)
12548 * floor((a f + d g)/(m d))
12550 * Constraints of the form
12558 * We currently require that "subs" is an integral expression.
12559 * Handling rational expressions may require us to add stride constraints
12560 * as we do in isl_basic_set_preimage_multi_aff.
12562 __isl_give isl_basic_set
*isl_basic_set_substitute(
12563 __isl_take isl_basic_set
*bset
,
12564 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12570 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12573 bset
= isl_basic_set_cow(bset
);
12574 if (!bset
|| !subs
)
12577 ctx
= isl_basic_set_get_ctx(bset
);
12578 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12579 isl_die(ctx
, isl_error_invalid
,
12580 "spaces don't match", goto error
);
12581 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12582 isl_die(ctx
, isl_error_unsupported
,
12583 "cannot handle divs yet", goto error
);
12584 if (!isl_int_is_one(subs
->v
->el
[0]))
12585 isl_die(ctx
, isl_error_invalid
,
12586 "can only substitute integer expressions", goto error
);
12588 pos
+= isl_basic_set_offset(bset
, type
);
12592 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12593 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12595 isl_int_set(v
, bset
->eq
[i
][pos
]);
12596 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12597 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12598 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12601 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12602 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12604 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12605 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12606 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12607 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12610 for (i
= 0; i
< bset
->n_div
; ++i
) {
12611 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12613 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12614 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12615 isl_seq_combine(bset
->div
[i
] + 1,
12616 subs
->v
->el
[0], bset
->div
[i
] + 1,
12617 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12618 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12623 bset
= isl_basic_set_simplify(bset
);
12624 return isl_basic_set_finalize(bset
);
12626 isl_basic_set_free(bset
);
12630 /* Plug in "subs" for dimension "type", "pos" of "set".
12632 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12633 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12637 if (set
&& isl_set_plain_is_empty(set
))
12640 set
= isl_set_cow(set
);
12644 for (i
= set
->n
- 1; i
>= 0; --i
) {
12645 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12646 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12657 /* Check if the range of "ma" is compatible with the domain or range
12658 * (depending on "type") of "bmap".
12660 static isl_stat
check_basic_map_compatible_range_multi_aff(
12661 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12662 __isl_keep isl_multi_aff
*ma
)
12665 isl_space
*ma_space
;
12667 ma_space
= isl_multi_aff_get_space(ma
);
12669 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12673 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12674 "parameters don't match", goto error
);
12675 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12679 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12680 "spaces don't match", goto error
);
12682 isl_space_free(ma_space
);
12683 return isl_stat_ok
;
12685 isl_space_free(ma_space
);
12686 return isl_stat_error
;
12689 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12690 * coefficients before the transformed range of dimensions,
12691 * the "n_after" coefficients after the transformed range of dimensions
12692 * and the coefficients of the other divs in "bmap".
12694 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12695 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12700 isl_local_space
*ls
;
12705 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12709 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12710 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12711 for (i
= 0; i
< n_div
; ++i
) {
12712 int o_bmap
= 0, o_ls
= 0;
12714 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12715 o_bmap
+= 1 + 1 + n_param
;
12716 o_ls
+= 1 + 1 + n_param
;
12717 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12718 o_bmap
+= n_before
;
12719 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12720 ls
->div
->row
[i
] + o_ls
, n_set
);
12723 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12725 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12726 ls
->div
->row
[i
] + o_ls
, n_div
);
12729 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12730 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12734 isl_local_space_free(ls
);
12737 isl_local_space_free(ls
);
12741 /* How many stride constraints does "ma" enforce?
12742 * That is, how many of the affine expressions have a denominator
12743 * different from one?
12745 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12750 for (i
= 0; i
< ma
->n
; ++i
)
12751 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12757 /* For each affine expression in ma of the form
12759 * x_i = (f_i y + h_i)/m_i
12761 * with m_i different from one, add a constraint to "bmap"
12764 * f_i y + h_i = m_i alpha_i
12766 * with alpha_i an additional existentially quantified variable.
12768 * The input variables of "ma" correspond to a subset of the variables
12769 * of "bmap". There are "n_before" variables in "bmap" before this
12770 * subset and "n_after" variables after this subset.
12771 * The integer divisions of the affine expressions in "ma" are assumed
12772 * to have been aligned. There are "n_div_ma" of them and
12773 * they appear first in "bmap", straight after the "n_after" variables.
12775 static __isl_give isl_basic_map
*add_ma_strides(
12776 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12777 int n_before
, int n_after
, int n_div_ma
)
12785 total
= isl_basic_map_total_dim(bmap
);
12786 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12787 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12788 for (i
= 0; i
< ma
->n
; ++i
) {
12789 int o_bmap
= 0, o_ma
= 1;
12791 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12793 div
= isl_basic_map_alloc_div(bmap
);
12794 k
= isl_basic_map_alloc_equality(bmap
);
12795 if (div
< 0 || k
< 0)
12797 isl_int_set_si(bmap
->div
[div
][0], 0);
12798 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12799 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12800 o_bmap
+= 1 + n_param
;
12801 o_ma
+= 1 + n_param
;
12802 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12803 o_bmap
+= n_before
;
12804 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12805 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12808 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12810 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12811 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12812 o_bmap
+= n_div_ma
;
12814 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12815 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12821 isl_basic_map_free(bmap
);
12825 /* Replace the domain or range space (depending on "type) of "space" by "set".
12827 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12828 enum isl_dim_type type
, __isl_take isl_space
*set
)
12830 if (type
== isl_dim_in
) {
12831 space
= isl_space_range(space
);
12832 space
= isl_space_map_from_domain_and_range(set
, space
);
12834 space
= isl_space_domain(space
);
12835 space
= isl_space_map_from_domain_and_range(space
, set
);
12841 /* Compute the preimage of the domain or range (depending on "type")
12842 * of "bmap" under the function represented by "ma".
12843 * In other words, plug in "ma" in the domain or range of "bmap".
12844 * The result is a basic map that lives in the same space as "bmap"
12845 * except that the domain or range has been replaced by
12846 * the domain space of "ma".
12848 * If bmap is represented by
12850 * A(p) + S u + B x + T v + C(divs) >= 0,
12852 * where u and x are input and output dimensions if type == isl_dim_out
12853 * while x and v are input and output dimensions if type == isl_dim_in,
12854 * and ma is represented by
12856 * x = D(p) + F(y) + G(divs')
12858 * then the result is
12860 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12862 * The divs in the input set are similarly adjusted.
12865 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12869 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12870 * B_i G(divs') + c_i(divs))/n_i)
12872 * If bmap is not a rational map and if F(y) involves any denominators
12874 * x_i = (f_i y + h_i)/m_i
12876 * then additional constraints are added to ensure that we only
12877 * map back integer points. That is we enforce
12879 * f_i y + h_i = m_i alpha_i
12881 * with alpha_i an additional existentially quantified variable.
12883 * We first copy over the divs from "ma".
12884 * Then we add the modified constraints and divs from "bmap".
12885 * Finally, we add the stride constraints, if needed.
12887 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12888 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12889 __isl_take isl_multi_aff
*ma
)
12893 isl_basic_map
*res
= NULL
;
12894 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12895 isl_int f
, c1
, c2
, g
;
12904 ma
= isl_multi_aff_align_divs(ma
);
12907 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12910 if (type
== isl_dim_in
) {
12912 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12914 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12917 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12918 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12920 space
= isl_multi_aff_get_domain_space(ma
);
12921 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12922 rational
= isl_basic_map_is_rational(bmap
);
12923 strides
= rational
? 0 : multi_aff_strides(ma
);
12924 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12925 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12927 res
= isl_basic_map_set_rational(res
);
12929 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12930 if (isl_basic_map_alloc_div(res
) < 0)
12933 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12936 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12937 k
= isl_basic_map_alloc_equality(res
);
12940 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12941 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12944 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12945 k
= isl_basic_map_alloc_inequality(res
);
12948 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12949 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12952 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12953 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12954 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12957 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12958 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12963 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12969 isl_basic_map_free(bmap
);
12970 isl_multi_aff_free(ma
);
12971 res
= isl_basic_map_simplify(res
);
12972 return isl_basic_map_finalize(res
);
12978 isl_basic_map_free(bmap
);
12979 isl_multi_aff_free(ma
);
12980 isl_basic_map_free(res
);
12984 /* Compute the preimage of "bset" under the function represented by "ma".
12985 * In other words, plug in "ma" in "bset". The result is a basic set
12986 * that lives in the domain space of "ma".
12988 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12989 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12991 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12994 /* Compute the preimage of the domain of "bmap" under the function
12995 * represented by "ma".
12996 * In other words, plug in "ma" in the domain of "bmap".
12997 * The result is a basic map that lives in the same space as "bmap"
12998 * except that the domain has been replaced by the domain space of "ma".
13000 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13001 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13003 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13006 /* Compute the preimage of the range of "bmap" under the function
13007 * represented by "ma".
13008 * In other words, plug in "ma" in the range of "bmap".
13009 * The result is a basic map that lives in the same space as "bmap"
13010 * except that the range has been replaced by the domain space of "ma".
13012 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13013 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13015 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13018 /* Check if the range of "ma" is compatible with the domain or range
13019 * (depending on "type") of "map".
13020 * Return isl_stat_error if anything is wrong.
13022 static isl_stat
check_map_compatible_range_multi_aff(
13023 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13024 __isl_keep isl_multi_aff
*ma
)
13027 isl_space
*ma_space
;
13029 ma_space
= isl_multi_aff_get_space(ma
);
13030 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13031 isl_space_free(ma_space
);
13033 return isl_stat_error
;
13035 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13036 "spaces don't match", return isl_stat_error
);
13037 return isl_stat_ok
;
13040 /* Compute the preimage of the domain or range (depending on "type")
13041 * of "map" under the function represented by "ma".
13042 * In other words, plug in "ma" in the domain or range of "map".
13043 * The result is a map that lives in the same space as "map"
13044 * except that the domain or range has been replaced by
13045 * the domain space of "ma".
13047 * The parameters are assumed to have been aligned.
13049 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13050 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13055 map
= isl_map_cow(map
);
13056 ma
= isl_multi_aff_align_divs(ma
);
13059 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13062 for (i
= 0; i
< map
->n
; ++i
) {
13063 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13064 isl_multi_aff_copy(ma
));
13069 space
= isl_multi_aff_get_domain_space(ma
);
13070 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13072 isl_space_free(map
->dim
);
13077 isl_multi_aff_free(ma
);
13079 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13080 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13083 isl_multi_aff_free(ma
);
13088 /* Compute the preimage of the domain or range (depending on "type")
13089 * of "map" under the function represented by "ma".
13090 * In other words, plug in "ma" in the domain or range of "map".
13091 * The result is a map that lives in the same space as "map"
13092 * except that the domain or range has been replaced by
13093 * the domain space of "ma".
13095 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13096 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13103 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13107 return map_preimage_multi_aff(map
, type
, ma
);
13109 if (isl_map_check_named_params(map
) < 0)
13111 if (!isl_space_has_named_params(ma
->space
))
13112 isl_die(map
->ctx
, isl_error_invalid
,
13113 "unaligned unnamed parameters", goto error
);
13114 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13115 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13117 return map_preimage_multi_aff(map
, type
, ma
);
13119 isl_multi_aff_free(ma
);
13120 return isl_map_free(map
);
13123 /* Compute the preimage of "set" under the function represented by "ma".
13124 * In other words, plug in "ma" in "set". The result is a set
13125 * that lives in the domain space of "ma".
13127 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13128 __isl_take isl_multi_aff
*ma
)
13130 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13133 /* Compute the preimage of the domain of "map" under the function
13134 * represented by "ma".
13135 * In other words, plug in "ma" in the domain of "map".
13136 * The result is a map that lives in the same space as "map"
13137 * except that the domain has been replaced by the domain space of "ma".
13139 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13140 __isl_take isl_multi_aff
*ma
)
13142 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13145 /* Compute the preimage of the range of "map" under the function
13146 * represented by "ma".
13147 * In other words, plug in "ma" in the range of "map".
13148 * The result is a map that lives in the same space as "map"
13149 * except that the range has been replaced by the domain space of "ma".
13151 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13152 __isl_take isl_multi_aff
*ma
)
13154 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13157 /* Compute the preimage of "map" under the function represented by "pma".
13158 * In other words, plug in "pma" in the domain or range of "map".
13159 * The result is a map that lives in the same space as "map",
13160 * except that the space of type "type" has been replaced by
13161 * the domain space of "pma".
13163 * The parameters of "map" and "pma" are assumed to have been aligned.
13165 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13166 __isl_take isl_map
*map
, enum isl_dim_type type
,
13167 __isl_take isl_pw_multi_aff
*pma
)
13176 isl_pw_multi_aff_free(pma
);
13177 res
= isl_map_empty(isl_map_get_space(map
));
13182 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13183 isl_multi_aff_copy(pma
->p
[0].maff
));
13184 if (type
== isl_dim_in
)
13185 res
= isl_map_intersect_domain(res
,
13186 isl_map_copy(pma
->p
[0].set
));
13188 res
= isl_map_intersect_range(res
,
13189 isl_map_copy(pma
->p
[0].set
));
13191 for (i
= 1; i
< pma
->n
; ++i
) {
13194 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13195 isl_multi_aff_copy(pma
->p
[i
].maff
));
13196 if (type
== isl_dim_in
)
13197 res_i
= isl_map_intersect_domain(res_i
,
13198 isl_map_copy(pma
->p
[i
].set
));
13200 res_i
= isl_map_intersect_range(res_i
,
13201 isl_map_copy(pma
->p
[i
].set
));
13202 res
= isl_map_union(res
, res_i
);
13205 isl_pw_multi_aff_free(pma
);
13209 isl_pw_multi_aff_free(pma
);
13214 /* Compute the preimage of "map" under the function represented by "pma".
13215 * In other words, plug in "pma" in the domain or range of "map".
13216 * The result is a map that lives in the same space as "map",
13217 * except that the space of type "type" has been replaced by
13218 * the domain space of "pma".
13220 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13221 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13228 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13232 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13234 if (isl_map_check_named_params(map
) < 0)
13236 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13238 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13239 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13241 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13243 isl_pw_multi_aff_free(pma
);
13244 return isl_map_free(map
);
13247 /* Compute the preimage of "set" under the function represented by "pma".
13248 * In other words, plug in "pma" in "set". The result is a set
13249 * that lives in the domain space of "pma".
13251 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13252 __isl_take isl_pw_multi_aff
*pma
)
13254 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13257 /* Compute the preimage of the domain of "map" under the function
13258 * represented by "pma".
13259 * In other words, plug in "pma" in the domain of "map".
13260 * The result is a map that lives in the same space as "map",
13261 * except that domain space has been replaced by the domain space of "pma".
13263 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13264 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13266 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13269 /* Compute the preimage of the range of "map" under the function
13270 * represented by "pma".
13271 * In other words, plug in "pma" in the range of "map".
13272 * The result is a map that lives in the same space as "map",
13273 * except that range space has been replaced by the domain space of "pma".
13275 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13276 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13278 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13281 /* Compute the preimage of "map" under the function represented by "mpa".
13282 * In other words, plug in "mpa" in the domain or range of "map".
13283 * The result is a map that lives in the same space as "map",
13284 * except that the space of type "type" has been replaced by
13285 * the domain space of "mpa".
13287 * If the map does not involve any constraints that refer to the
13288 * dimensions of the substituted space, then the only possible
13289 * effect of "mpa" on the map is to map the space to a different space.
13290 * We create a separate isl_multi_aff to effectuate this change
13291 * in order to avoid spurious splitting of the map along the pieces
13293 * If "mpa" has a non-trivial explicit domain, however,
13294 * then the full substitution should be performed.
13296 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13297 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13301 isl_pw_multi_aff
*pma
;
13306 n
= isl_map_dim(map
, type
);
13307 full
= isl_map_involves_dims(map
, type
, 0, n
);
13308 if (full
>= 0 && !full
)
13309 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13316 space
= isl_multi_pw_aff_get_space(mpa
);
13317 isl_multi_pw_aff_free(mpa
);
13318 ma
= isl_multi_aff_zero(space
);
13319 return isl_map_preimage_multi_aff(map
, type
, ma
);
13322 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13323 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13326 isl_multi_pw_aff_free(mpa
);
13330 /* Compute the preimage of "map" under the function represented by "mpa".
13331 * In other words, plug in "mpa" in the domain "map".
13332 * The result is a map that lives in the same space as "map",
13333 * except that domain space has been replaced by the domain space of "mpa".
13335 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13336 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13338 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13341 /* Compute the preimage of "set" by the function represented by "mpa".
13342 * In other words, plug in "mpa" in "set".
13344 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13345 __isl_take isl_multi_pw_aff
*mpa
)
13347 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13350 /* Return a copy of the equality constraints of "bset" as a matrix.
13352 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13353 __isl_keep isl_basic_set
*bset
)
13361 ctx
= isl_basic_set_get_ctx(bset
);
13362 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13363 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13366 /* Are the "n" "coefficients" starting at "first" of the integer division
13367 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13369 * The "coefficient" at position 0 is the denominator.
13370 * The "coefficient" at position 1 is the constant term.
13372 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13373 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13374 unsigned first
, unsigned n
)
13376 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13377 return isl_bool_error
;
13378 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13379 return isl_bool_error
;
13380 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13381 bmap2
->div
[pos2
] + first
, n
);
13384 /* Are the integer division expressions at position "pos1" in "bmap1" and
13385 * "pos2" in "bmap2" equal to each other, except that the constant terms
13388 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13389 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13390 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13395 if (!bmap1
|| !bmap2
)
13396 return isl_bool_error
;
13397 total
= isl_basic_map_total_dim(bmap1
);
13398 if (total
!= isl_basic_map_total_dim(bmap2
))
13399 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13400 "incomparable div expressions", return isl_bool_error
);
13401 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13403 if (equal
< 0 || !equal
)
13405 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13407 if (equal
< 0 || equal
)
13408 return isl_bool_not(equal
);
13409 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13413 /* Replace the numerator of the constant term of the integer division
13414 * expression at position "div" in "bmap" by "value".
13415 * The caller guarantees that this does not change the meaning
13418 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13419 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13421 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13422 return isl_basic_map_free(bmap
);
13424 isl_int_set_si(bmap
->div
[div
][1], value
);
13429 /* Is the point "inner" internal to inequality constraint "ineq"
13431 * The point is considered to be internal to the inequality constraint,
13432 * if it strictly lies on the positive side of the inequality constraint,
13433 * or if it lies on the constraint and the constraint is lexico-positive.
13435 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13436 __isl_keep isl_basic_set
*bset
, int ineq
)
13442 if (!inner
|| !bset
)
13443 return isl_bool_error
;
13445 ctx
= isl_basic_set_get_ctx(bset
);
13446 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13447 &ctx
->normalize_gcd
);
13448 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13449 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13451 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13452 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13453 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13456 /* Tighten the inequality constraints of "bset" that are outward with respect
13457 * to the point "vec".
13458 * That is, tighten the constraints that are not satisfied by "vec".
13460 * "vec" is a point internal to some superset S of "bset" that is used
13461 * to make the subsets of S disjoint, by tightening one half of the constraints
13462 * that separate two subsets. In particular, the constraints of S
13463 * are all satisfied by "vec" and should not be tightened.
13464 * Of the internal constraints, those that have "vec" on the outside
13465 * are tightened. The shared facet is included in the adjacent subset
13466 * with the opposite constraint.
13467 * For constraints that saturate "vec", this criterion cannot be used
13468 * to determine which of the two sides should be tightened.
13469 * Instead, the sign of the first non-zero coefficient is used
13470 * to make this choice. Note that this second criterion is never used
13471 * on the constraints of S since "vec" is interior to "S".
13473 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13474 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13478 bset
= isl_basic_set_cow(bset
);
13481 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13484 internal
= is_internal(vec
, bset
, j
);
13486 return isl_basic_set_free(bset
);
13489 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13495 /* Replace the variables x of type "type" starting at "first" in "bmap"
13496 * by x' with x = M x' with M the matrix trans.
13497 * That is, replace the corresponding coefficients c by c M.
13499 * The transformation matrix should be a square matrix.
13501 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13502 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13503 __isl_take isl_mat
*trans
)
13507 bmap
= isl_basic_map_cow(bmap
);
13508 if (!bmap
|| !trans
)
13511 if (trans
->n_row
!= trans
->n_col
)
13512 isl_die(trans
->ctx
, isl_error_invalid
,
13513 "expecting square transformation matrix", goto error
);
13514 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13515 isl_die(trans
->ctx
, isl_error_invalid
,
13516 "oversized transformation matrix", goto error
);
13518 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13520 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13521 isl_mat_copy(trans
)) < 0)
13523 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13524 isl_mat_copy(trans
)) < 0)
13526 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13527 isl_mat_copy(trans
)) < 0)
13530 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13531 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13533 isl_mat_free(trans
);
13536 isl_mat_free(trans
);
13537 isl_basic_map_free(bmap
);
13541 /* Replace the variables x of type "type" starting at "first" in "bset"
13542 * by x' with x = M x' with M the matrix trans.
13543 * That is, replace the corresponding coefficients c by c M.
13545 * The transformation matrix should be a square matrix.
13547 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13548 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13549 __isl_take isl_mat
*trans
)
13551 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);