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>
47 #include <isl_printer_private.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 /* Treat "bset" as a basic map.
55 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
56 * this function performs a redundant cast.
58 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
59 __isl_keep
const isl_basic_set
*bset
)
61 return (const isl_basic_map
*) bset
;
64 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
67 case isl_dim_param
: return dim
->nparam
;
68 case isl_dim_in
: return dim
->n_in
;
69 case isl_dim_out
: return dim
->n_out
;
70 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
75 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
78 case isl_dim_param
: return 1;
79 case isl_dim_in
: return 1 + dim
->nparam
;
80 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
85 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
86 enum isl_dim_type type
)
91 case isl_dim_cst
: return 1;
94 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
95 case isl_dim_div
: return bmap
->n_div
;
96 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
101 /* Return the space of "map".
103 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
105 return map
? map
->dim
: NULL
;
108 /* Return the space of "set".
110 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
112 return isl_map_peek_space(set_to_map(set
));
115 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
117 return map
? n(map
->dim
, type
) : 0;
120 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
122 return set
? n(set
->dim
, type
) : 0;
125 /* Return the position of the variables of the given type
126 * within the sequence of variables of "bmap".
128 int isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
129 enum isl_dim_type type
)
133 space
= isl_basic_map_peek_space(bmap
);
140 case isl_dim_out
: return isl_space_offset(space
, type
);
141 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
144 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
145 "invalid dimension type", return -1);
149 /* Return the position of the variables of the given type
150 * within the sequence of variables of "bset".
152 int isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
153 enum isl_dim_type type
)
155 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
158 /* Return the position of the coefficients of the variables of the given type
159 * within the sequence of coefficients of "bmap".
161 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
162 enum isl_dim_type type
)
165 case isl_dim_cst
: return 0;
169 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
174 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
175 enum isl_dim_type type
)
177 return isl_basic_map_offset(bset
, type
);
180 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
182 return pos(map
->dim
, type
);
185 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
186 enum isl_dim_type type
)
188 return isl_basic_map_dim(bset
, type
);
191 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
193 return isl_basic_set_dim(bset
, isl_dim_set
);
196 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
198 return isl_basic_set_dim(bset
, isl_dim_param
);
201 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
203 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
206 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
208 return isl_set_dim(set
, isl_dim_set
);
211 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
213 return isl_set_dim(set
, isl_dim_param
);
216 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
218 return bmap
? bmap
->dim
->n_in
: 0;
221 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
223 return bmap
? bmap
->dim
->n_out
: 0;
226 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
228 return bmap
? bmap
->dim
->nparam
: 0;
231 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
233 return bmap
? bmap
->n_div
: 0;
236 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
238 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
241 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
243 return map
? map
->dim
->n_in
: 0;
246 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
248 return map
? map
->dim
->n_out
: 0;
251 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
253 return map
? map
->dim
->nparam
: 0;
256 /* Return the number of equality constraints in the description of "bmap".
257 * Return -1 on error.
259 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
266 /* Return the number of equality constraints in the description of "bset".
267 * Return -1 on error.
269 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
271 return isl_basic_map_n_equality(bset_to_bmap(bset
));
274 /* Return the number of inequality constraints in the description of "bmap".
275 * Return -1 on error.
277 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
284 /* Return the number of inequality constraints in the description of "bset".
285 * Return -1 on error.
287 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
289 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
292 /* Do "bmap1" and "bmap2" have the same parameters?
294 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
295 __isl_keep isl_basic_map
*bmap2
)
297 isl_space
*space1
, *space2
;
299 space1
= isl_basic_map_peek_space(bmap1
);
300 space2
= isl_basic_map_peek_space(bmap2
);
301 return isl_space_has_equal_params(space1
, space2
);
304 /* Do "map1" and "map2" have the same parameters?
306 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
307 __isl_keep isl_map
*map2
)
309 isl_space
*space1
, *space2
;
311 space1
= isl_map_peek_space(map1
);
312 space2
= isl_map_peek_space(map2
);
313 return isl_space_has_equal_params(space1
, space2
);
316 /* Do "map" and "set" have the same parameters?
318 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
319 __isl_keep isl_set
*set
)
321 return isl_map_has_equal_params(map
, set_to_map(set
));
324 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
325 __isl_keep isl_set
*set
)
329 return isl_bool_error
;
330 m
= isl_map_has_equal_params(map
, set_to_map(set
));
333 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
334 set
->dim
, isl_dim_set
);
337 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
338 __isl_keep isl_basic_set
*bset
)
342 return isl_bool_error
;
343 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
346 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
347 bset
->dim
, isl_dim_set
);
350 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
351 __isl_keep isl_set
*set
)
355 return isl_bool_error
;
356 m
= isl_map_has_equal_params(map
, set_to_map(set
));
359 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
360 set
->dim
, isl_dim_set
);
363 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
364 __isl_keep isl_basic_set
*bset
)
368 return isl_bool_error
;
369 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
372 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
373 bset
->dim
, isl_dim_set
);
376 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
378 return bmap
? bmap
->ctx
: NULL
;
381 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
383 return bset
? bset
->ctx
: NULL
;
386 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
388 return map
? map
->ctx
: NULL
;
391 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
393 return set
? set
->ctx
: NULL
;
396 /* Return the space of "bmap".
398 __isl_keep isl_space
*isl_basic_map_peek_space(
399 __isl_keep
const isl_basic_map
*bmap
)
401 return bmap
? bmap
->dim
: NULL
;
404 /* Return the space of "bset".
406 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
408 return isl_basic_map_peek_space(bset_to_bmap(bset
));
411 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
413 return isl_space_copy(isl_basic_map_peek_space(bmap
));
416 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
418 return isl_basic_map_get_space(bset_to_bmap(bset
));
421 /* Extract the divs in "bmap" as a matrix.
423 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
431 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
435 ctx
= isl_basic_map_get_ctx(bmap
);
436 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
437 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
441 for (i
= 0; i
< bmap
->n_div
; ++i
)
442 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
447 /* Extract the divs in "bset" as a matrix.
449 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
451 return isl_basic_map_get_divs(bset
);
454 __isl_give isl_local_space
*isl_basic_map_get_local_space(
455 __isl_keep isl_basic_map
*bmap
)
462 div
= isl_basic_map_get_divs(bmap
);
463 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
466 __isl_give isl_local_space
*isl_basic_set_get_local_space(
467 __isl_keep isl_basic_set
*bset
)
469 return isl_basic_map_get_local_space(bset
);
472 /* For each known div d = floor(f/m), add the constraints
475 * -(f-(m-1)) + m d >= 0
477 * Do not finalize the result.
479 static __isl_give isl_basic_map
*add_known_div_constraints(
480 __isl_take isl_basic_map
*bmap
)
487 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
490 bmap
= isl_basic_map_cow(bmap
);
491 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
494 for (i
= 0; i
< n_div
; ++i
) {
495 if (isl_int_is_zero(bmap
->div
[i
][0]))
497 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
503 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
504 __isl_take isl_local_space
*ls
)
513 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
514 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
515 n_div
, 0, 2 * n_div
);
517 for (i
= 0; i
< n_div
; ++i
)
518 if (isl_basic_map_alloc_div(bmap
) < 0)
521 for (i
= 0; i
< n_div
; ++i
)
522 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
523 bmap
= add_known_div_constraints(bmap
);
525 isl_local_space_free(ls
);
528 isl_local_space_free(ls
);
529 isl_basic_map_free(bmap
);
533 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
534 __isl_take isl_local_space
*ls
)
536 return isl_basic_map_from_local_space(ls
);
539 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
541 return isl_space_copy(isl_map_peek_space(map
));
544 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
548 return isl_space_copy(set
->dim
);
551 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
552 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
554 bmap
= isl_basic_map_cow(bmap
);
557 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
560 bmap
= isl_basic_map_finalize(bmap
);
563 isl_basic_map_free(bmap
);
567 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
568 __isl_take isl_basic_set
*bset
, const char *s
)
570 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
573 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
574 enum isl_dim_type type
)
576 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
579 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
580 enum isl_dim_type type
, const char *s
)
584 map
= isl_map_cow(map
);
588 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
592 for (i
= 0; i
< map
->n
; ++i
) {
593 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
604 /* Replace the identifier of the tuple of type "type" by "id".
606 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
607 __isl_take isl_basic_map
*bmap
,
608 enum isl_dim_type type
, __isl_take isl_id
*id
)
610 bmap
= isl_basic_map_cow(bmap
);
613 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
615 return isl_basic_map_free(bmap
);
616 bmap
= isl_basic_map_finalize(bmap
);
623 /* Replace the identifier of the tuple by "id".
625 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
626 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
628 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
631 /* Does the input or output tuple have a name?
633 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
635 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
638 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
639 enum isl_dim_type type
)
641 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
644 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
647 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
651 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
652 enum isl_dim_type type
, __isl_take isl_id
*id
)
654 map
= isl_map_cow(map
);
658 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
660 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
666 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
667 __isl_take isl_id
*id
)
669 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
672 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
673 enum isl_dim_type type
)
675 map
= isl_map_cow(map
);
679 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
681 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
684 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
686 return isl_map_reset_tuple_id(set
, isl_dim_set
);
689 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
691 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
694 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
695 enum isl_dim_type type
)
697 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
700 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
702 return isl_map_has_tuple_id(set
, isl_dim_set
);
705 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
707 return isl_map_get_tuple_id(set
, isl_dim_set
);
710 /* Does the set tuple have a name?
712 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
715 return isl_bool_error
;
716 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
720 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
722 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
725 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
727 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
730 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
731 enum isl_dim_type type
, unsigned pos
)
733 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
736 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
737 enum isl_dim_type type
, unsigned pos
)
739 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
742 /* Does the given dimension have a name?
744 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
745 enum isl_dim_type type
, unsigned pos
)
748 return isl_bool_error
;
749 return isl_space_has_dim_name(map
->dim
, type
, pos
);
752 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
753 enum isl_dim_type type
, unsigned pos
)
755 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
758 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
759 enum isl_dim_type type
, unsigned pos
)
761 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
764 /* Does the given dimension have a name?
766 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
767 enum isl_dim_type type
, unsigned pos
)
770 return isl_bool_error
;
771 return isl_space_has_dim_name(set
->dim
, type
, pos
);
774 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
775 __isl_take isl_basic_map
*bmap
,
776 enum isl_dim_type type
, unsigned pos
, const char *s
)
778 bmap
= isl_basic_map_cow(bmap
);
781 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
784 return isl_basic_map_finalize(bmap
);
786 isl_basic_map_free(bmap
);
790 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
791 enum isl_dim_type type
, unsigned pos
, const char *s
)
795 map
= isl_map_cow(map
);
799 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
803 for (i
= 0; i
< map
->n
; ++i
) {
804 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
815 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
816 __isl_take isl_basic_set
*bset
,
817 enum isl_dim_type type
, unsigned pos
, const char *s
)
819 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
823 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
824 enum isl_dim_type type
, unsigned pos
, const char *s
)
826 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
830 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
831 enum isl_dim_type type
, unsigned pos
)
834 return isl_bool_error
;
835 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
838 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
839 enum isl_dim_type type
, unsigned pos
)
841 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
844 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
845 enum isl_dim_type type
, unsigned pos
)
847 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
850 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
851 enum isl_dim_type type
, unsigned pos
)
853 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
856 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
857 enum isl_dim_type type
, unsigned pos
)
859 return isl_map_has_dim_id(set
, type
, pos
);
862 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
863 enum isl_dim_type type
, unsigned pos
)
865 return isl_map_get_dim_id(set
, type
, pos
);
868 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
869 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
871 map
= isl_map_cow(map
);
875 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
877 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
883 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
884 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
886 return isl_map_set_dim_id(set
, type
, pos
, id
);
889 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
890 __isl_keep isl_id
*id
)
894 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
897 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
898 __isl_keep isl_id
*id
)
900 return isl_map_find_dim_by_id(set
, type
, id
);
903 /* Return the position of the dimension of the given type and name
905 * Return -1 if no such dimension can be found.
907 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
908 enum isl_dim_type type
, const char *name
)
912 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
915 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
920 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
923 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
926 return isl_map_find_dim_by_name(set
, type
, name
);
929 /* Check whether equality i of bset is a pure stride constraint
930 * on a single dimension, i.e., of the form
934 * with k a constant and e an existentially quantified variable.
936 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
945 return isl_bool_error
;
947 if (!isl_int_is_zero(bset
->eq
[i
][0]))
948 return isl_bool_false
;
950 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
951 d
= isl_basic_set_dim(bset
, isl_dim_set
);
952 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
954 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
955 return isl_bool_false
;
956 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
958 return isl_bool_false
;
959 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
961 return isl_bool_false
;
963 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
965 return isl_bool_false
;
966 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
967 n_div
- pos2
- 1) != -1)
968 return isl_bool_false
;
969 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
970 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
971 return isl_bool_false
;
973 return isl_bool_true
;
976 /* Reset the user pointer on all identifiers of parameters and tuples
977 * of the space of "map".
979 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
983 space
= isl_map_get_space(map
);
984 space
= isl_space_reset_user(space
);
985 map
= isl_map_reset_space(map
, space
);
990 /* Reset the user pointer on all identifiers of parameters and tuples
991 * of the space of "set".
993 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
995 return isl_map_reset_user(set
);
998 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1001 return isl_bool_error
;
1002 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1005 /* Has "map" been marked as a rational map?
1006 * In particular, have all basic maps in "map" been marked this way?
1007 * An empty map is not considered to be rational.
1008 * Maps where only some of the basic maps are marked rational
1011 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1017 return isl_bool_error
;
1019 return isl_bool_false
;
1020 rational
= isl_basic_map_is_rational(map
->p
[0]);
1023 for (i
= 1; i
< map
->n
; ++i
) {
1024 isl_bool rational_i
;
1026 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1029 if (rational
!= rational_i
)
1030 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1031 "mixed rational and integer basic maps "
1032 "not supported", return isl_bool_error
);
1038 /* Has "set" been marked as a rational set?
1039 * In particular, have all basic set in "set" been marked this way?
1040 * An empty set is not considered to be rational.
1041 * Sets where only some of the basic sets are marked rational
1044 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1046 return isl_map_is_rational(set
);
1049 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1051 return isl_basic_map_is_rational(bset
);
1054 /* Does "bmap" contain any rational points?
1056 * If "bmap" has an equality for each dimension, equating the dimension
1057 * to an integer constant, then it has no rational points, even if it
1058 * is marked as rational.
1060 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1062 isl_bool has_rational
= isl_bool_true
;
1066 return isl_bool_error
;
1067 if (isl_basic_map_plain_is_empty(bmap
))
1068 return isl_bool_false
;
1069 if (!isl_basic_map_is_rational(bmap
))
1070 return isl_bool_false
;
1071 bmap
= isl_basic_map_copy(bmap
);
1072 bmap
= isl_basic_map_implicit_equalities(bmap
);
1074 return isl_bool_error
;
1075 total
= isl_basic_map_total_dim(bmap
);
1076 if (bmap
->n_eq
== total
) {
1078 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1079 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1082 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1083 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1085 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1090 if (i
== bmap
->n_eq
)
1091 has_rational
= isl_bool_false
;
1093 isl_basic_map_free(bmap
);
1095 return has_rational
;
1098 /* Does "map" contain any rational points?
1100 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1103 isl_bool has_rational
;
1106 return isl_bool_error
;
1107 for (i
= 0; i
< map
->n
; ++i
) {
1108 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1109 if (has_rational
< 0 || has_rational
)
1110 return has_rational
;
1112 return isl_bool_false
;
1115 /* Does "set" contain any rational points?
1117 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1119 return isl_map_has_rational(set
);
1122 /* Is this basic set a parameter domain?
1124 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1127 return isl_bool_error
;
1128 return isl_space_is_params(bset
->dim
);
1131 /* Is this set a parameter domain?
1133 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1136 return isl_bool_error
;
1137 return isl_space_is_params(set
->dim
);
1140 /* Is this map actually a parameter domain?
1141 * Users should never call this function. Outside of isl,
1142 * a map can never be a parameter domain.
1144 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1147 return isl_bool_error
;
1148 return isl_space_is_params(map
->dim
);
1151 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1152 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1153 unsigned n_eq
, unsigned n_ineq
)
1156 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1157 unsigned n_var
= isl_space_dim(space
, isl_dim_all
);
1158 size_t row_size
= 1 + n_var
+ extra
;
1164 return isl_basic_map_free(bmap
);
1166 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1167 if (isl_blk_is_error(bmap
->block
))
1170 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1171 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1175 bmap
->block2
= isl_blk_empty();
1178 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1179 if (isl_blk_is_error(bmap
->block2
))
1182 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1187 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1188 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1190 for (i
= 0; i
< extra
; ++i
)
1191 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1195 bmap
->c_size
= n_eq
+ n_ineq
;
1196 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1197 bmap
->extra
= extra
;
1201 bmap
->sample
= NULL
;
1205 isl_basic_map_free(bmap
);
1209 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1210 unsigned nparam
, unsigned dim
, unsigned extra
,
1211 unsigned n_eq
, unsigned n_ineq
)
1213 struct isl_basic_map
*bmap
;
1216 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1220 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1221 return bset_from_bmap(bmap
);
1224 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1225 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1227 struct isl_basic_map
*bmap
;
1230 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1231 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1232 return bset_from_bmap(bmap
);
1234 isl_space_free(dim
);
1238 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1239 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1241 struct isl_basic_map
*bmap
;
1245 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1250 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1252 isl_space_free(space
);
1256 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1257 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1258 unsigned n_eq
, unsigned n_ineq
)
1260 struct isl_basic_map
*bmap
;
1263 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1267 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1271 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1272 __isl_keep isl_basic_map
*src
)
1275 unsigned total
= isl_basic_map_total_dim(src
);
1280 for (i
= 0; i
< src
->n_eq
; ++i
) {
1281 int j
= isl_basic_map_alloc_equality(dst
);
1283 return isl_basic_map_free(dst
);
1284 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1287 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1288 int j
= isl_basic_map_alloc_inequality(dst
);
1290 return isl_basic_map_free(dst
);
1291 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1294 for (i
= 0; i
< src
->n_div
; ++i
) {
1295 int j
= isl_basic_map_alloc_div(dst
);
1297 return isl_basic_map_free(dst
);
1298 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1300 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1304 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1306 struct isl_basic_map
*dup
;
1310 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1311 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1312 dup
= dup_constraints(dup
, bmap
);
1315 dup
->flags
= bmap
->flags
;
1316 dup
->sample
= isl_vec_copy(bmap
->sample
);
1320 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1322 struct isl_basic_map
*dup
;
1324 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1325 return bset_from_bmap(dup
);
1328 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1333 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1337 return isl_basic_set_dup(bset
);
1340 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1349 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1354 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1358 bmap
= isl_basic_map_dup(bmap
);
1360 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1364 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1373 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1378 if (--bmap
->ref
> 0)
1381 isl_ctx_deref(bmap
->ctx
);
1383 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1385 isl_blk_free(bmap
->ctx
, bmap
->block
);
1386 isl_vec_free(bmap
->sample
);
1387 isl_space_free(bmap
->dim
);
1393 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1395 return isl_basic_map_free(bset_to_bmap(bset
));
1398 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1400 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1403 /* Check that "bset" does not involve any parameters.
1405 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1408 return isl_stat_error
;
1409 if (isl_basic_set_dim(bset
, isl_dim_param
) != 0)
1410 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1411 "basic set should not have any parameters",
1412 return isl_stat_error
);
1416 /* Check that "bset" does not involve any local variables.
1418 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1421 return isl_stat_error
;
1422 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
1423 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1424 "basic set should not have any local variables",
1425 return isl_stat_error
);
1429 /* Check that "map" has only named parameters, reporting an error
1432 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1434 return isl_space_check_named_params(isl_map_peek_space(map
));
1437 /* Check that "bmap" has only named parameters, reporting an error
1440 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1442 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1445 /* Check that "bmap1" and "bmap2" have the same parameters,
1446 * reporting an error if they do not.
1448 static isl_stat
isl_basic_map_check_equal_params(
1449 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1453 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1455 return isl_stat_error
;
1457 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1458 "parameters don't match", return isl_stat_error
);
1462 __isl_give isl_map
*isl_map_align_params_map_map_and(
1463 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1464 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1465 __isl_take isl_map
*map2
))
1469 if (isl_map_has_equal_params(map1
, map2
))
1470 return fn(map1
, map2
);
1471 if (isl_map_check_named_params(map1
) < 0)
1473 if (isl_map_check_named_params(map2
) < 0)
1475 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1476 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1477 return fn(map1
, map2
);
1484 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1485 __isl_keep isl_map
*map2
,
1486 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1491 return isl_bool_error
;
1492 if (isl_map_has_equal_params(map1
, map2
))
1493 return fn(map1
, map2
);
1494 if (isl_map_check_named_params(map1
) < 0)
1495 return isl_bool_error
;
1496 if (isl_map_check_named_params(map2
) < 0)
1497 return isl_bool_error
;
1498 map1
= isl_map_copy(map1
);
1499 map2
= isl_map_copy(map2
);
1500 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1501 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1508 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1510 struct isl_ctx
*ctx
;
1514 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1515 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1517 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1518 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1519 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1520 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1521 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1523 int j
= isl_basic_map_alloc_inequality(bmap
);
1527 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1528 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1535 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1536 bmap
->extra
- bmap
->n_div
);
1537 return bmap
->n_eq
++;
1540 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1542 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1545 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1549 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1554 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1556 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1559 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1564 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1566 if (pos
!= bmap
->n_eq
- 1) {
1568 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1569 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1575 /* Turn inequality "pos" of "bmap" into an equality.
1577 * In particular, we move the inequality in front of the equalities
1578 * and move the last inequality in the position of the moved inequality.
1579 * Note that isl_tab_make_equalities_explicit depends on this particular
1580 * change in the ordering of the constraints.
1582 void isl_basic_map_inequality_to_equality(
1583 struct isl_basic_map
*bmap
, unsigned pos
)
1587 t
= bmap
->ineq
[pos
];
1588 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1589 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1594 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1595 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1596 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1597 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1600 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1602 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1605 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1607 struct isl_ctx
*ctx
;
1611 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1612 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1613 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1614 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1615 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1616 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1617 1 + isl_basic_map_total_dim(bmap
),
1618 bmap
->extra
- bmap
->n_div
);
1619 return bmap
->n_ineq
++;
1622 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1624 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1627 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1631 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1636 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1638 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1641 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1646 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1648 if (pos
!= bmap
->n_ineq
- 1) {
1649 t
= bmap
->ineq
[pos
];
1650 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1651 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1652 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1658 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1660 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1663 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1669 empty
= isl_basic_map_plain_is_empty(bmap
);
1671 return isl_basic_map_free(bmap
);
1675 bmap
= isl_basic_map_cow(bmap
);
1676 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1679 k
= isl_basic_map_alloc_equality(bmap
);
1682 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1685 isl_basic_map_free(bmap
);
1689 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1692 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1695 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1700 bmap
= isl_basic_map_cow(bmap
);
1701 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1704 k
= isl_basic_map_alloc_inequality(bmap
);
1707 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1710 isl_basic_map_free(bmap
);
1714 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1717 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1720 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1724 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1725 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1726 1 + 1 + isl_basic_map_total_dim(bmap
),
1727 bmap
->extra
- bmap
->n_div
);
1728 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1729 return bmap
->n_div
++;
1732 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1734 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1738 #define TYPE isl_basic_map
1739 #include "check_type_range_templ.c"
1741 /* Check that there are "n" dimensions of type "type" starting at "first"
1744 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1745 enum isl_dim_type type
, unsigned first
, unsigned n
)
1747 return isl_basic_map_check_range(bset_to_bmap(bset
),
1751 /* Insert an extra integer division, prescribed by "div", to "bmap"
1752 * at (integer division) position "pos".
1754 * The integer division is first added at the end and then moved
1755 * into the right position.
1757 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1758 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1762 bmap
= isl_basic_map_cow(bmap
);
1764 return isl_basic_map_free(bmap
);
1766 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1767 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1768 "unexpected size", return isl_basic_map_free(bmap
));
1769 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1770 return isl_basic_map_free(bmap
);
1772 bmap
= isl_basic_map_extend_space(bmap
,
1773 isl_basic_map_get_space(bmap
), 1, 0, 2);
1774 k
= isl_basic_map_alloc_div(bmap
);
1776 return isl_basic_map_free(bmap
);
1777 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1778 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1780 for (i
= k
; i
> pos
; --i
)
1781 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1786 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1789 return isl_stat_error
;
1790 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1795 static __isl_give isl_basic_map
*add_constraints(
1796 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1797 unsigned i_pos
, unsigned o_pos
)
1799 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1802 struct isl_dim_map
*dim_map
;
1804 space
= isl_basic_map_peek_space(bmap2
);
1805 if (!bmap1
|| !space
)
1808 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1809 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1810 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1811 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1812 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1813 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1814 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1815 ctx
= isl_basic_map_get_ctx(bmap1
);
1816 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1817 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1818 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1819 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1820 isl_dim_map_div(dim_map
, bmap2
, total
);
1822 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1824 isl_basic_map_free(bmap1
);
1825 isl_basic_map_free(bmap2
);
1829 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1830 struct isl_basic_set
*bset2
, unsigned pos
)
1832 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1833 bset_to_bmap(bset2
), 0, pos
));
1836 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1837 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1838 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1840 struct isl_basic_map
*ext
;
1850 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1851 base
->extra
>= base
->n_div
+ extra
;
1853 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1854 room_for_ineq(base
, n_ineq
)) {
1855 isl_space_free(space
);
1859 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1860 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1861 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1862 extra
+= base
->extra
;
1864 n_ineq
+= base
->n_ineq
;
1866 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1872 ext
->sample
= isl_vec_copy(base
->sample
);
1873 flags
= base
->flags
;
1874 ext
= add_constraints(ext
, base
, 0, 0);
1877 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1883 isl_space_free(space
);
1884 isl_basic_map_free(base
);
1888 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1889 __isl_take isl_basic_set
*base
,
1890 __isl_take isl_space
*dim
, unsigned extra
,
1891 unsigned n_eq
, unsigned n_ineq
)
1893 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1894 dim
, extra
, n_eq
, n_ineq
));
1897 struct isl_basic_map
*isl_basic_map_extend_constraints(
1898 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1902 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1906 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1907 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1908 unsigned n_eq
, unsigned n_ineq
)
1910 struct isl_basic_map
*bmap
;
1915 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1919 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1922 isl_basic_map_free(base
);
1926 struct isl_basic_set
*isl_basic_set_extend_constraints(
1927 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1929 isl_basic_map
*bmap
= bset_to_bmap(base
);
1930 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1931 return bset_from_bmap(bmap
);
1934 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1936 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1939 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1944 if (bmap
->ref
> 1) {
1946 bmap
= isl_basic_map_dup(bmap
);
1949 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1950 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1955 /* Clear all cached information in "map", either because it is about
1956 * to be modified or because it is being freed.
1957 * Always return the same pointer that is passed in.
1958 * This is needed for the use in isl_map_free.
1960 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1962 isl_basic_map_free(map
->cached_simple_hull
[0]);
1963 isl_basic_map_free(map
->cached_simple_hull
[1]);
1964 map
->cached_simple_hull
[0] = NULL
;
1965 map
->cached_simple_hull
[1] = NULL
;
1969 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1971 return isl_map_cow(set
);
1974 /* Return an isl_map that is equal to "map" and that has only
1975 * a single reference.
1977 * If the original input already has only one reference, then
1978 * simply return it, but clear all cached information, since
1979 * it may be rendered invalid by the operations that will be
1980 * performed on the result.
1982 * Otherwise, create a duplicate (without any cached information).
1984 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1990 return clear_caches(map
);
1992 return isl_map_dup(map
);
1995 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1996 unsigned a_len
, unsigned b_len
)
1998 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1999 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2000 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2003 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2004 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2009 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2012 if (n1
== 0 || n2
== 0)
2015 bmap
= isl_basic_map_cow(bmap
);
2019 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2020 if (isl_blk_is_error(blk
))
2023 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2025 bmap
->eq
[i
] + pos
, n1
, n2
);
2027 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2029 bmap
->ineq
[i
] + pos
, n1
, n2
);
2031 for (i
= 0; i
< bmap
->n_div
; ++i
)
2033 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2035 isl_blk_free(bmap
->ctx
, blk
);
2037 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2038 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2039 return isl_basic_map_finalize(bmap
);
2041 isl_basic_map_free(bmap
);
2045 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2046 __isl_take isl_basic_map
*bmap
)
2052 total
= isl_basic_map_total_dim(bmap
);
2053 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2054 return isl_basic_map_free(bmap
);
2055 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2057 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2059 i
= isl_basic_map_alloc_equality(bmap
);
2063 isl_int_set_si(bmap
->eq
[i
][0], 1);
2064 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2065 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2066 isl_vec_free(bmap
->sample
);
2067 bmap
->sample
= NULL
;
2068 return isl_basic_map_finalize(bmap
);
2070 isl_basic_map_free(bmap
);
2074 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2075 __isl_take isl_basic_set
*bset
)
2077 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2080 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2081 __isl_take isl_basic_map
*bmap
)
2086 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2089 bmap
= isl_basic_map_cow(bmap
);
2093 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2095 return isl_basic_map_finalize(bmap
);
2098 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2099 __isl_take isl_basic_set
*bset
)
2101 return isl_basic_map_set_rational(bset
);
2104 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2105 __isl_take isl_basic_set
*bset
)
2110 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2113 bset
= isl_basic_set_cow(bset
);
2117 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2119 return isl_basic_set_finalize(bset
);
2122 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2126 map
= isl_map_cow(map
);
2129 for (i
= 0; i
< map
->n
; ++i
) {
2130 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2140 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2142 return isl_map_set_rational(set
);
2145 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2148 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2150 isl_int
*t
= bmap
->div
[a
];
2151 bmap
->div
[a
] = bmap
->div
[b
];
2155 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2156 * div definitions accordingly.
2158 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2164 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2166 return isl_basic_map_free(bmap
);
2168 swap_div(bmap
, a
, b
);
2170 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2171 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2173 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2174 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2176 for (i
= 0; i
< bmap
->n_div
; ++i
)
2177 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2178 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2183 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2185 isl_seq_cpy(c
, c
+ n
, rem
);
2186 isl_seq_clr(c
+ rem
, n
);
2189 /* Drop n dimensions starting at first.
2191 * In principle, this frees up some extra variables as the number
2192 * of columns remains constant, but we would have to extend
2193 * the div array too as the number of rows in this array is assumed
2194 * to be equal to extra.
2196 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2197 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2199 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2202 /* Move "n" divs starting at "first" to the end of the list of divs.
2204 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2205 unsigned first
, unsigned n
)
2210 if (first
+ n
== bmap
->n_div
)
2213 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2216 for (i
= 0; i
< n
; ++i
)
2217 div
[i
] = bmap
->div
[first
+ i
];
2218 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2219 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2220 for (i
= 0; i
< n
; ++i
)
2221 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2225 isl_basic_map_free(bmap
);
2230 #define TYPE isl_map
2232 #include "check_type_range_templ.c"
2234 /* Check that there are "n" dimensions of type "type" starting at "first"
2237 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2238 enum isl_dim_type type
, unsigned first
, unsigned n
)
2240 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2243 /* Drop "n" dimensions of type "type" starting at "first".
2244 * Perform the core computation, without cowing or
2245 * simplifying and finalizing the result.
2247 * In principle, this frees up some extra variables as the number
2248 * of columns remains constant, but we would have to extend
2249 * the div array too as the number of rows in this array is assumed
2250 * to be equal to extra.
2252 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2253 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2254 unsigned first
, unsigned n
)
2260 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2261 return isl_basic_map_free(bmap
);
2263 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2264 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2265 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2266 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2268 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2269 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2271 for (i
= 0; i
< bmap
->n_div
; ++i
)
2272 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2274 if (type
== isl_dim_div
) {
2275 bmap
= move_divs_last(bmap
, first
, n
);
2278 if (isl_basic_map_free_div(bmap
, n
) < 0)
2279 return isl_basic_map_free(bmap
);
2281 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2283 return isl_basic_map_free(bmap
);
2285 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2286 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2290 /* Drop "n" dimensions of type "type" starting at "first".
2292 * In principle, this frees up some extra variables as the number
2293 * of columns remains constant, but we would have to extend
2294 * the div array too as the number of rows in this array is assumed
2295 * to be equal to extra.
2297 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2298 enum isl_dim_type type
, unsigned first
, unsigned n
)
2302 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2305 bmap
= isl_basic_map_cow(bmap
);
2309 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2311 bmap
= isl_basic_map_simplify(bmap
);
2312 return isl_basic_map_finalize(bmap
);
2315 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2316 enum isl_dim_type type
, unsigned first
, unsigned n
)
2318 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2322 /* No longer consider "map" to be normalized.
2324 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2328 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2332 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2333 enum isl_dim_type type
, unsigned first
, unsigned n
)
2337 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2338 return isl_map_free(map
);
2340 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2342 map
= isl_map_cow(map
);
2345 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2349 for (i
= 0; i
< map
->n
; ++i
) {
2350 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2354 map
= isl_map_unmark_normalized(map
);
2362 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2363 enum isl_dim_type type
, unsigned first
, unsigned n
)
2365 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2368 /* Drop the integer division at position "div", which is assumed
2369 * not to appear in any of the constraints or
2370 * in any of the other integer divisions.
2372 * Since the integer division is redundant, there is no need to cow.
2374 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2375 __isl_take isl_basic_map
*bmap
, unsigned div
)
2377 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2380 /* Eliminate the specified n dimensions starting at first from the
2381 * constraints, without removing the dimensions from the space.
2382 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2384 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2385 enum isl_dim_type type
, unsigned first
, unsigned n
)
2392 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2393 return isl_map_free(map
);
2395 map
= isl_map_cow(map
);
2399 for (i
= 0; i
< map
->n
; ++i
) {
2400 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2410 /* Eliminate the specified n dimensions starting at first from the
2411 * constraints, without removing the dimensions from the space.
2412 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2414 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2415 enum isl_dim_type type
, unsigned first
, unsigned n
)
2417 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2420 /* Eliminate the specified n dimensions starting at first from the
2421 * constraints, without removing the dimensions from the space.
2422 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2424 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2425 unsigned first
, unsigned n
)
2427 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2430 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2431 __isl_take isl_basic_map
*bmap
)
2435 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2437 return isl_basic_map_free(bmap
);
2438 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2442 return isl_basic_map_finalize(bmap
);
2445 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2446 __isl_take isl_basic_set
*bset
)
2448 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2451 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2460 map
= isl_map_cow(map
);
2464 for (i
= 0; i
< map
->n
; ++i
) {
2465 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2475 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2477 return isl_map_remove_divs(set
);
2480 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2481 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2482 unsigned first
, unsigned n
)
2484 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2485 return isl_basic_map_free(bmap
);
2486 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2488 bmap
= isl_basic_map_eliminate_vars(bmap
,
2489 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2492 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2494 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2498 /* Return true if the definition of the given div (recursively) involves
2499 * any of the given variables.
2501 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2502 unsigned first
, unsigned n
)
2505 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2507 if (isl_int_is_zero(bmap
->div
[div
][0]))
2508 return isl_bool_false
;
2509 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2510 return isl_bool_true
;
2512 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2515 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2517 involves
= div_involves_vars(bmap
, i
, first
, n
);
2518 if (involves
< 0 || involves
)
2522 return isl_bool_false
;
2525 /* Try and add a lower and/or upper bound on "div" to "bmap"
2526 * based on inequality "i".
2527 * "total" is the total number of variables (excluding the divs).
2528 * "v" is a temporary object that can be used during the calculations.
2529 * If "lb" is set, then a lower bound should be constructed.
2530 * If "ub" is set, then an upper bound should be constructed.
2532 * The calling function has already checked that the inequality does not
2533 * reference "div", but we still need to check that the inequality is
2534 * of the right form. We'll consider the case where we want to construct
2535 * a lower bound. The construction of upper bounds is similar.
2537 * Let "div" be of the form
2539 * q = floor((a + f(x))/d)
2541 * We essentially check if constraint "i" is of the form
2545 * so that we can use it to derive a lower bound on "div".
2546 * However, we allow a slightly more general form
2550 * with the condition that the coefficients of g(x) - f(x) are all
2552 * Rewriting this constraint as
2556 * adding a + f(x) to both sides and dividing by d, we obtain
2558 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2560 * Taking the floor on both sides, we obtain
2562 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2566 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2568 * In the case of an upper bound, we construct the constraint
2570 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2573 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2574 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2575 unsigned total
, isl_int v
, int lb
, int ub
)
2579 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2581 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2582 bmap
->div
[div
][1 + 1 + j
]);
2583 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2586 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2587 bmap
->div
[div
][1 + 1 + j
]);
2588 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2594 bmap
= isl_basic_map_cow(bmap
);
2595 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2597 int k
= isl_basic_map_alloc_inequality(bmap
);
2600 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2601 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2602 bmap
->div
[div
][1 + j
]);
2603 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2604 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2606 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2609 int k
= isl_basic_map_alloc_inequality(bmap
);
2612 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2613 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2614 bmap
->div
[div
][1 + j
]);
2615 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2616 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2618 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2623 isl_basic_map_free(bmap
);
2627 /* This function is called right before "div" is eliminated from "bmap"
2628 * using Fourier-Motzkin.
2629 * Look through the constraints of "bmap" for constraints on the argument
2630 * of the integer division and use them to construct constraints on the
2631 * integer division itself. These constraints can then be combined
2632 * during the Fourier-Motzkin elimination.
2633 * Note that it is only useful to introduce lower bounds on "div"
2634 * if "bmap" already contains upper bounds on "div" as the newly
2635 * introduce lower bounds can then be combined with the pre-existing
2636 * upper bounds. Similarly for upper bounds.
2637 * We therefore first check if "bmap" contains any lower and/or upper bounds
2640 * It is interesting to note that the introduction of these constraints
2641 * can indeed lead to more accurate results, even when compared to
2642 * deriving constraints on the argument of "div" from constraints on "div".
2643 * Consider, for example, the set
2645 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2647 * The second constraint can be rewritten as
2649 * 2 * [(-i-2j+3)/4] + k >= 0
2651 * from which we can derive
2653 * -i - 2j + 3 >= -2k
2659 * Combined with the first constraint, we obtain
2661 * -3 <= 3 + 2k or k >= -3
2663 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2664 * the first constraint, we obtain
2666 * [(i + 2j)/4] >= [-3/4] = -1
2668 * Combining this constraint with the second constraint, we obtain
2672 static __isl_give isl_basic_map
*insert_bounds_on_div(
2673 __isl_take isl_basic_map
*bmap
, int div
)
2676 int check_lb
, check_ub
;
2683 if (isl_int_is_zero(bmap
->div
[div
][0]))
2686 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2688 return isl_basic_map_free(bmap
);
2692 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2693 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2700 if (!check_lb
&& !check_ub
)
2705 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2706 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2709 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2710 check_lb
, check_ub
);
2718 /* Remove all divs (recursively) involving any of the given dimensions
2719 * in their definitions.
2721 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2722 __isl_take isl_basic_map
*bmap
,
2723 enum isl_dim_type type
, unsigned first
, unsigned n
)
2727 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2728 return isl_basic_map_free(bmap
);
2729 first
+= isl_basic_map_offset(bmap
, type
);
2731 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2734 involves
= div_involves_vars(bmap
, i
, first
, n
);
2736 return isl_basic_map_free(bmap
);
2739 bmap
= insert_bounds_on_div(bmap
, i
);
2740 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2749 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2750 __isl_take isl_basic_set
*bset
,
2751 enum isl_dim_type type
, unsigned first
, unsigned n
)
2753 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2756 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2757 enum isl_dim_type type
, unsigned first
, unsigned n
)
2766 map
= isl_map_cow(map
);
2770 for (i
= 0; i
< map
->n
; ++i
) {
2771 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2782 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2783 enum isl_dim_type type
, unsigned first
, unsigned n
)
2785 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2789 /* Does the description of "bmap" depend on the specified dimensions?
2790 * We also check whether the dimensions appear in any of the div definitions.
2791 * In principle there is no need for this check. If the dimensions appear
2792 * in a div definition, they also appear in the defining constraints of that
2795 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2796 enum isl_dim_type type
, unsigned first
, unsigned n
)
2800 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2801 return isl_bool_error
;
2803 first
+= isl_basic_map_offset(bmap
, type
);
2804 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2805 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2806 return isl_bool_true
;
2807 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2808 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2809 return isl_bool_true
;
2810 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2811 if (isl_int_is_zero(bmap
->div
[i
][0]))
2813 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2814 return isl_bool_true
;
2817 return isl_bool_false
;
2820 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2821 enum isl_dim_type type
, unsigned first
, unsigned n
)
2825 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2826 return isl_bool_error
;
2828 for (i
= 0; i
< map
->n
; ++i
) {
2829 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2831 if (involves
< 0 || involves
)
2835 return isl_bool_false
;
2838 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2839 enum isl_dim_type type
, unsigned first
, unsigned n
)
2841 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2844 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2845 enum isl_dim_type type
, unsigned first
, unsigned n
)
2847 return isl_map_involves_dims(set
, type
, first
, n
);
2850 /* Drop all constraints in bmap that involve any of the dimensions
2851 * first to first+n-1.
2852 * This function only performs the actual removal of constraints.
2854 * This function should not call finalize since it is used by
2855 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2857 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2858 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2865 bmap
= isl_basic_map_cow(bmap
);
2870 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2871 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2873 isl_basic_map_drop_equality(bmap
, i
);
2876 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2877 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2879 isl_basic_map_drop_inequality(bmap
, i
);
2885 /* Drop all constraints in bset that involve any of the dimensions
2886 * first to first+n-1.
2887 * This function only performs the actual removal of constraints.
2889 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2890 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2892 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2895 /* Drop all constraints in bmap that do not involve any of the dimensions
2896 * first to first + n - 1 of the given type.
2898 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2899 __isl_take isl_basic_map
*bmap
,
2900 enum isl_dim_type type
, unsigned first
, unsigned n
)
2905 isl_space
*space
= isl_basic_map_get_space(bmap
);
2906 isl_basic_map_free(bmap
);
2907 return isl_basic_map_universe(space
);
2909 bmap
= isl_basic_map_cow(bmap
);
2913 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2914 return isl_basic_map_free(bmap
);
2916 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2918 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2919 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2921 isl_basic_map_drop_equality(bmap
, i
);
2924 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2925 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2927 isl_basic_map_drop_inequality(bmap
, i
);
2930 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2934 /* Drop all constraints in bset that do not involve any of the dimensions
2935 * first to first + n - 1 of the given type.
2937 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2938 __isl_take isl_basic_set
*bset
,
2939 enum isl_dim_type type
, unsigned first
, unsigned n
)
2941 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2945 /* Drop all constraints in bmap that involve any of the dimensions
2946 * first to first + n - 1 of the given type.
2948 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2949 __isl_take isl_basic_map
*bmap
,
2950 enum isl_dim_type type
, unsigned first
, unsigned n
)
2957 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2958 return isl_basic_map_free(bmap
);
2960 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2961 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2962 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2963 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2967 /* Drop all constraints in bset that involve any of the dimensions
2968 * first to first + n - 1 of the given type.
2970 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2971 __isl_take isl_basic_set
*bset
,
2972 enum isl_dim_type type
, unsigned first
, unsigned n
)
2974 return isl_basic_map_drop_constraints_involving_dims(bset
,
2978 /* Drop constraints from "map" by applying "drop" to each basic map.
2980 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2981 enum isl_dim_type type
, unsigned first
, unsigned n
,
2982 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2983 enum isl_dim_type type
, unsigned first
, unsigned n
))
2987 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2988 return isl_map_free(map
);
2990 map
= isl_map_cow(map
);
2994 for (i
= 0; i
< map
->n
; ++i
) {
2995 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2997 return isl_map_free(map
);
3001 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3006 /* Drop all constraints in map that involve any of the dimensions
3007 * first to first + n - 1 of the given type.
3009 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3010 __isl_take isl_map
*map
,
3011 enum isl_dim_type type
, unsigned first
, unsigned n
)
3015 return drop_constraints(map
, type
, first
, n
,
3016 &isl_basic_map_drop_constraints_involving_dims
);
3019 /* Drop all constraints in "map" that do not involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3023 __isl_take isl_map
*map
,
3024 enum isl_dim_type type
, unsigned first
, unsigned n
)
3027 isl_space
*space
= isl_map_get_space(map
);
3029 return isl_map_universe(space
);
3031 return drop_constraints(map
, type
, first
, n
,
3032 &isl_basic_map_drop_constraints_not_involving_dims
);
3035 /* Drop all constraints in set that involve any of the dimensions
3036 * first to first + n - 1 of the given type.
3038 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3039 __isl_take isl_set
*set
,
3040 enum isl_dim_type type
, unsigned first
, unsigned n
)
3042 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3045 /* Drop all constraints in "set" that do not involve any of the dimensions
3046 * first to first + n - 1 of the given type.
3048 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3049 __isl_take isl_set
*set
,
3050 enum isl_dim_type type
, unsigned first
, unsigned n
)
3052 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3055 /* Does local variable "div" of "bmap" have a complete explicit representation?
3056 * Having a complete explicit representation requires not only
3057 * an explicit representation, but also that all local variables
3058 * that appear in this explicit representation in turn have
3059 * a complete explicit representation.
3061 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3064 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3067 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3068 if (marked
< 0 || marked
)
3069 return isl_bool_not(marked
);
3071 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3074 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3076 known
= isl_basic_map_div_is_known(bmap
, i
);
3077 if (known
< 0 || !known
)
3081 return isl_bool_true
;
3084 /* Remove all divs that are unknown or defined in terms of unknown divs.
3086 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3087 __isl_take isl_basic_map
*bmap
)
3094 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3095 if (isl_basic_map_div_is_known(bmap
, i
))
3097 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3106 /* Remove all divs that are unknown or defined in terms of unknown divs.
3108 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3109 __isl_take isl_basic_set
*bset
)
3111 return isl_basic_map_remove_unknown_divs(bset
);
3114 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3123 map
= isl_map_cow(map
);
3127 for (i
= 0; i
< map
->n
; ++i
) {
3128 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3138 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3140 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3143 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3144 __isl_take isl_basic_set
*bset
,
3145 enum isl_dim_type type
, unsigned first
, unsigned n
)
3147 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3148 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3149 return bset_from_bmap(bmap
);
3152 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3153 enum isl_dim_type type
, unsigned first
, unsigned n
)
3160 map
= isl_map_cow(map
);
3161 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3162 return isl_map_free(map
);
3164 for (i
= 0; i
< map
->n
; ++i
) {
3165 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3166 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3170 map
= isl_map_drop(map
, type
, first
, n
);
3177 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3178 enum isl_dim_type type
, unsigned first
, unsigned n
)
3180 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3184 /* Project out n inputs starting at first using Fourier-Motzkin */
3185 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3186 unsigned first
, unsigned n
)
3188 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3191 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3192 FILE *out
, int indent
)
3197 fprintf(out
, "null basic set\n");
3201 fprintf(out
, "%*s", indent
, "");
3202 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3203 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3204 bset
->extra
, bset
->flags
);
3206 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3207 p
= isl_printer_set_dump(p
, 1);
3208 p
= isl_printer_set_indent(p
, indent
);
3209 p
= isl_printer_start_line(p
);
3210 p
= isl_printer_print_basic_set(p
, bset
);
3211 p
= isl_printer_end_line(p
);
3212 isl_printer_free(p
);
3215 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3216 FILE *out
, int indent
)
3221 fprintf(out
, "null basic map\n");
3225 fprintf(out
, "%*s", indent
, "");
3226 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3227 "flags: %x, n_name: %d\n",
3229 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3230 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3232 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3233 p
= isl_printer_set_dump(p
, 1);
3234 p
= isl_printer_set_indent(p
, indent
);
3235 p
= isl_printer_start_line(p
);
3236 p
= isl_printer_print_basic_map(p
, bmap
);
3237 p
= isl_printer_end_line(p
);
3238 isl_printer_free(p
);
3241 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3248 total
= isl_basic_map_total_dim(bmap
);
3249 if (pos
>= bmap
->n_ineq
)
3250 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3251 "invalid position", return isl_basic_map_free(bmap
));
3252 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3253 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3254 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3259 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3262 if (isl_space_check_is_set(space
) < 0)
3264 return isl_map_alloc_space(space
, n
, flags
);
3266 isl_space_free(space
);
3270 /* Make sure "map" has room for at least "n" more basic maps.
3272 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3275 struct isl_map
*grown
= NULL
;
3279 isl_assert(map
->ctx
, n
>= 0, goto error
);
3280 if (map
->n
+ n
<= map
->size
)
3282 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3285 for (i
= 0; i
< map
->n
; ++i
) {
3286 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3294 isl_map_free(grown
);
3299 /* Make sure "set" has room for at least "n" more basic sets.
3301 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3303 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3306 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3308 return isl_map_from_basic_map(bset
);
3311 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3313 struct isl_map
*map
;
3318 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3319 return isl_map_add_basic_map(map
, bmap
);
3322 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3323 __isl_take isl_basic_set
*bset
)
3325 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3326 bset_to_bmap(bset
)));
3329 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3331 return isl_map_free(set
);
3334 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3339 fprintf(out
, "null set\n");
3343 fprintf(out
, "%*s", indent
, "");
3344 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3345 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3347 for (i
= 0; i
< set
->n
; ++i
) {
3348 fprintf(out
, "%*s", indent
, "");
3349 fprintf(out
, "basic set %d:\n", i
);
3350 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3354 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3359 fprintf(out
, "null map\n");
3363 fprintf(out
, "%*s", indent
, "");
3364 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3365 "flags: %x, n_name: %d\n",
3366 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3367 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3368 for (i
= 0; i
< map
->n
; ++i
) {
3369 fprintf(out
, "%*s", indent
, "");
3370 fprintf(out
, "basic map %d:\n", i
);
3371 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3375 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3376 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3378 struct isl_basic_map
*bmap_domain
;
3380 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3383 if (isl_basic_set_dim(bset
, isl_dim_set
) != 0)
3384 isl_assert(bset
->ctx
,
3385 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3387 bmap
= isl_basic_map_cow(bmap
);
3390 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3391 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3392 bmap_domain
= isl_basic_map_from_domain(bset
);
3393 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3395 bmap
= isl_basic_map_simplify(bmap
);
3396 return isl_basic_map_finalize(bmap
);
3398 isl_basic_map_free(bmap
);
3399 isl_basic_set_free(bset
);
3403 /* Check that the space of "bset" is the same as that of the range of "bmap".
3405 static isl_stat
isl_basic_map_check_compatible_range(
3406 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3410 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3412 return isl_stat_error
;
3414 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3415 "incompatible spaces", return isl_stat_error
);
3420 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3421 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3423 struct isl_basic_map
*bmap_range
;
3425 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3428 if (isl_basic_set_dim(bset
, isl_dim_set
) != 0 &&
3429 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3432 if (isl_basic_set_plain_is_universe(bset
)) {
3433 isl_basic_set_free(bset
);
3437 bmap
= isl_basic_map_cow(bmap
);
3440 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3441 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3442 bmap_range
= bset_to_bmap(bset
);
3443 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3445 bmap
= isl_basic_map_simplify(bmap
);
3446 return isl_basic_map_finalize(bmap
);
3448 isl_basic_map_free(bmap
);
3449 isl_basic_set_free(bset
);
3453 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3454 __isl_keep isl_vec
*vec
)
3461 return isl_bool_error
;
3463 total
= 1 + isl_basic_map_total_dim(bmap
);
3464 if (total
!= vec
->size
)
3465 return isl_bool_false
;
3469 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3470 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3471 if (!isl_int_is_zero(s
)) {
3473 return isl_bool_false
;
3477 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3478 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3479 if (isl_int_is_neg(s
)) {
3481 return isl_bool_false
;
3487 return isl_bool_true
;
3490 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3491 __isl_keep isl_vec
*vec
)
3493 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3496 __isl_give isl_basic_map
*isl_basic_map_intersect(
3497 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3499 struct isl_vec
*sample
= NULL
;
3500 isl_space
*space1
, *space2
;
3502 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3504 space1
= isl_basic_map_peek_space(bmap1
);
3505 space2
= isl_basic_map_peek_space(bmap2
);
3506 if (isl_space_dim(space1
, isl_dim_all
) ==
3507 isl_space_dim(space1
, isl_dim_param
) &&
3508 isl_space_dim(space2
, isl_dim_all
) !=
3509 isl_space_dim(space2
, isl_dim_param
))
3510 return isl_basic_map_intersect(bmap2
, bmap1
);
3512 if (isl_space_dim(space2
, isl_dim_all
) !=
3513 isl_space_dim(space2
, isl_dim_param
))
3514 isl_assert(bmap1
->ctx
,
3515 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3517 if (isl_basic_map_plain_is_empty(bmap1
)) {
3518 isl_basic_map_free(bmap2
);
3521 if (isl_basic_map_plain_is_empty(bmap2
)) {
3522 isl_basic_map_free(bmap1
);
3526 if (bmap1
->sample
&&
3527 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3528 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3529 sample
= isl_vec_copy(bmap1
->sample
);
3530 else if (bmap2
->sample
&&
3531 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3532 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3533 sample
= isl_vec_copy(bmap2
->sample
);
3535 bmap1
= isl_basic_map_cow(bmap1
);
3538 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3539 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3540 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3543 isl_vec_free(sample
);
3545 isl_vec_free(bmap1
->sample
);
3546 bmap1
->sample
= sample
;
3549 bmap1
= isl_basic_map_simplify(bmap1
);
3550 return isl_basic_map_finalize(bmap1
);
3553 isl_vec_free(sample
);
3554 isl_basic_map_free(bmap1
);
3555 isl_basic_map_free(bmap2
);
3559 struct isl_basic_set
*isl_basic_set_intersect(
3560 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3562 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3563 bset_to_bmap(bset2
)));
3566 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3567 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3569 return isl_basic_set_intersect(bset1
, bset2
);
3572 /* Special case of isl_map_intersect, where both map1 and map2
3573 * are convex, without any divs and such that either map1 or map2
3574 * contains a single constraint. This constraint is then simply
3575 * added to the other map.
3577 static __isl_give isl_map
*map_intersect_add_constraint(
3578 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3580 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3581 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3582 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3583 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3585 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3586 return isl_map_intersect(map2
, map1
);
3588 map1
= isl_map_cow(map1
);
3591 if (isl_map_plain_is_empty(map1
)) {
3595 if (map2
->p
[0]->n_eq
== 1)
3596 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3598 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3599 map2
->p
[0]->ineq
[0]);
3601 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3602 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3606 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3607 isl_basic_map_free(map1
->p
[0]);
3613 map1
= isl_map_unmark_normalized(map1
);
3621 /* map2 may be either a parameter domain or a map living in the same
3624 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3625 __isl_take isl_map
*map2
)
3635 if ((isl_map_plain_is_empty(map1
) ||
3636 isl_map_plain_is_universe(map2
)) &&
3637 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3641 if ((isl_map_plain_is_empty(map2
) ||
3642 isl_map_plain_is_universe(map1
)) &&
3643 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3648 if (map1
->n
== 1 && map2
->n
== 1 &&
3649 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3650 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3651 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3652 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3653 return map_intersect_add_constraint(map1
, map2
);
3655 equal
= isl_map_plain_is_equal(map1
, map2
);
3663 if (isl_map_dim(map2
, isl_dim_all
) != isl_map_dim(map2
, isl_dim_param
))
3664 isl_assert(map1
->ctx
,
3665 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3667 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3668 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3669 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3671 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3672 map1
->n
* map2
->n
, flags
);
3675 for (i
= 0; i
< map1
->n
; ++i
)
3676 for (j
= 0; j
< map2
->n
; ++j
) {
3677 struct isl_basic_map
*part
;
3678 part
= isl_basic_map_intersect(
3679 isl_basic_map_copy(map1
->p
[i
]),
3680 isl_basic_map_copy(map2
->p
[j
]));
3681 if (isl_basic_map_is_empty(part
) < 0)
3682 part
= isl_basic_map_free(part
);
3683 result
= isl_map_add_basic_map(result
, part
);
3696 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3697 __isl_take isl_map
*map2
)
3701 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3702 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3703 "spaces don't match", goto error
);
3704 return map_intersect_internal(map1
, map2
);
3711 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3712 __isl_take isl_map
*map2
)
3714 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3717 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3719 return set_from_map(isl_map_intersect(set_to_map(set1
),
3723 /* map_intersect_internal accepts intersections
3724 * with parameter domains, so we can just call that function.
3726 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3727 __isl_take isl_set
*params
)
3729 return map_intersect_internal(map
, params
);
3732 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3733 __isl_take isl_map
*map2
)
3735 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3738 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3739 __isl_take isl_set
*params
)
3741 return isl_map_intersect_params(set
, params
);
3744 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3747 unsigned pos
, n1
, n2
;
3751 bmap
= isl_basic_map_cow(bmap
);
3754 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3755 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3756 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3757 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3758 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3759 return isl_basic_map_reset_space(bmap
, space
);
3762 static __isl_give isl_basic_map
*basic_map_space_reset(
3763 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3769 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3772 space
= isl_basic_map_get_space(bmap
);
3773 space
= isl_space_reset(space
, type
);
3774 bmap
= isl_basic_map_reset_space(bmap
, space
);
3778 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3779 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3780 unsigned pos
, unsigned n
)
3782 isl_bool rational
, is_empty
;
3783 isl_space
*res_space
;
3784 struct isl_basic_map
*res
;
3785 struct isl_dim_map
*dim_map
;
3786 unsigned total
, off
;
3787 enum isl_dim_type t
;
3790 return basic_map_space_reset(bmap
, type
);
3792 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3794 return isl_basic_map_free(bmap
);
3795 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3798 return isl_basic_map_free(bmap
);
3800 isl_basic_map_free(bmap
);
3801 return isl_basic_map_empty(res_space
);
3804 total
= isl_basic_map_total_dim(bmap
) + n
;
3805 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3807 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3809 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3811 unsigned size
= isl_basic_map_dim(bmap
, t
);
3812 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3814 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3815 pos
, size
- pos
, off
+ pos
+ n
);
3817 off
+= isl_space_dim(res_space
, t
);
3819 isl_dim_map_div(dim_map
, bmap
, off
);
3821 res
= isl_basic_map_alloc_space(res_space
,
3822 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3823 rational
= isl_basic_map_is_rational(bmap
);
3825 res
= isl_basic_map_free(res
);
3827 res
= isl_basic_map_set_rational(res
);
3828 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3829 return isl_basic_map_finalize(res
);
3832 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3833 __isl_take isl_basic_set
*bset
,
3834 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3836 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3839 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3840 enum isl_dim_type type
, unsigned n
)
3844 return isl_basic_map_insert_dims(bmap
, type
,
3845 isl_basic_map_dim(bmap
, type
), n
);
3848 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3849 enum isl_dim_type type
, unsigned n
)
3853 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3854 return isl_basic_map_add_dims(bset
, type
, n
);
3856 isl_basic_set_free(bset
);
3860 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3861 enum isl_dim_type type
)
3865 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3868 space
= isl_map_get_space(map
);
3869 space
= isl_space_reset(space
, type
);
3870 map
= isl_map_reset_space(map
, space
);
3874 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3875 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3880 return map_space_reset(map
, type
);
3882 map
= isl_map_cow(map
);
3886 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3890 for (i
= 0; i
< map
->n
; ++i
) {
3891 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3902 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3903 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3905 return isl_map_insert_dims(set
, type
, pos
, n
);
3908 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3909 enum isl_dim_type type
, unsigned n
)
3913 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3916 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3917 enum isl_dim_type type
, unsigned n
)
3921 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3922 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3928 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3929 __isl_take isl_basic_map
*bmap
,
3930 enum isl_dim_type dst_type
, unsigned dst_pos
,
3931 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3933 struct isl_dim_map
*dim_map
;
3934 struct isl_basic_map
*res
;
3935 enum isl_dim_type t
;
3936 unsigned total
, off
;
3941 bmap
= isl_basic_map_reset(bmap
, src_type
);
3942 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3946 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3947 return isl_basic_map_free(bmap
);
3949 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3952 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3954 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3955 pos(bmap
->dim
, src_type
) + src_pos
+
3956 ((src_type
< dst_type
) ? n
: 0)) {
3957 bmap
= isl_basic_map_cow(bmap
);
3961 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3962 src_type
, src_pos
, n
);
3966 bmap
= isl_basic_map_finalize(bmap
);
3971 total
= isl_basic_map_total_dim(bmap
);
3972 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3975 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3976 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3977 if (t
== dst_type
) {
3978 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3981 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3984 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3985 dst_pos
, size
- dst_pos
, off
);
3986 off
+= size
- dst_pos
;
3987 } else if (t
== src_type
) {
3988 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3991 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3992 src_pos
+ n
, size
- src_pos
- n
, off
);
3993 off
+= size
- src_pos
- n
;
3995 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3999 isl_dim_map_div(dim_map
, bmap
, off
);
4001 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4002 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4003 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4007 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4008 src_type
, src_pos
, n
);
4012 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4013 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4014 bmap
= isl_basic_map_finalize(bmap
);
4018 isl_basic_map_free(bmap
);
4022 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4023 enum isl_dim_type dst_type
, unsigned dst_pos
,
4024 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4026 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4027 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4028 src_type
, src_pos
, n
);
4029 return bset_from_bmap(bmap
);
4032 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4033 enum isl_dim_type dst_type
, unsigned dst_pos
,
4034 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4038 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4039 return set_from_map(isl_map_move_dims(set_to_map(set
),
4040 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4046 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4047 enum isl_dim_type dst_type
, unsigned dst_pos
,
4048 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4053 map
= isl_map_reset(map
, src_type
);
4054 map
= isl_map_reset(map
, dst_type
);
4058 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4059 return isl_map_free(map
);
4061 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4064 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4066 map
= isl_map_cow(map
);
4070 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4074 for (i
= 0; i
< map
->n
; ++i
) {
4075 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4077 src_type
, src_pos
, n
);
4088 /* Move the specified dimensions to the last columns right before
4089 * the divs. Don't change the dimension specification of bmap.
4090 * That's the responsibility of the caller.
4092 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4093 enum isl_dim_type type
, unsigned first
, unsigned n
)
4095 struct isl_dim_map
*dim_map
;
4096 struct isl_basic_map
*res
;
4097 enum isl_dim_type t
;
4098 unsigned total
, off
;
4102 if (pos(bmap
->dim
, type
) + first
+ n
==
4103 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4106 total
= isl_basic_map_total_dim(bmap
);
4107 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4110 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4111 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4113 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4116 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4117 first
, n
, total
- bmap
->n_div
- n
);
4118 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4119 first
+ n
, size
- (first
+ n
), off
);
4120 off
+= size
- (first
+ n
);
4122 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4126 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4128 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4129 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4130 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4134 /* Insert "n" rows in the divs of "bmap".
4136 * The number of columns is not changed, which means that the last
4137 * dimensions of "bmap" are being reintepreted as the new divs.
4138 * The space of "bmap" is not adjusted, however, which means
4139 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4140 * from the space of "bmap" is the responsibility of the caller.
4142 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4150 bmap
= isl_basic_map_cow(bmap
);
4154 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4155 old
= bmap
->block2
.data
;
4156 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4157 (bmap
->extra
+ n
) * (1 + row_size
));
4158 if (!bmap
->block2
.data
)
4159 return isl_basic_map_free(bmap
);
4160 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4162 return isl_basic_map_free(bmap
);
4163 for (i
= 0; i
< n
; ++i
) {
4164 new_div
[i
] = bmap
->block2
.data
+
4165 (bmap
->extra
+ i
) * (1 + row_size
);
4166 isl_seq_clr(new_div
[i
], 1 + row_size
);
4168 for (i
= 0; i
< bmap
->extra
; ++i
)
4169 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4171 bmap
->div
= new_div
;
4178 /* Drop constraints from "bmap" that only involve the variables
4179 * of "type" in the range [first, first + n] that are not related
4180 * to any of the variables outside that interval.
4181 * These constraints cannot influence the values for the variables
4182 * outside the interval, except in case they cause "bmap" to be empty.
4183 * Only drop the constraints if "bmap" is known to be non-empty.
4185 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4186 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4187 unsigned first
, unsigned n
)
4191 unsigned dim
, n_div
;
4194 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4196 return isl_basic_map_free(bmap
);
4200 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4201 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4202 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4204 return isl_basic_map_free(bmap
);
4205 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4206 for (i
= 0; i
< first
; ++i
)
4208 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4211 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4216 /* Turn the n dimensions of type type, starting at first
4217 * into existentially quantified variables.
4219 * If a subset of the projected out variables are unrelated
4220 * to any of the variables that remain, then the constraints
4221 * involving this subset are simply dropped first.
4223 __isl_give isl_basic_map
*isl_basic_map_project_out(
4224 __isl_take isl_basic_map
*bmap
,
4225 enum isl_dim_type type
, unsigned first
, unsigned n
)
4230 return basic_map_space_reset(bmap
, type
);
4231 if (type
== isl_dim_div
)
4232 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4233 "cannot project out existentially quantified variables",
4234 return isl_basic_map_free(bmap
));
4236 empty
= isl_basic_map_plain_is_empty(bmap
);
4238 return isl_basic_map_free(bmap
);
4240 bmap
= isl_basic_map_set_to_empty(bmap
);
4242 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4246 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4247 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4249 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4250 return isl_basic_map_free(bmap
);
4252 bmap
= move_last(bmap
, type
, first
, n
);
4253 bmap
= isl_basic_map_cow(bmap
);
4254 bmap
= insert_div_rows(bmap
, n
);
4258 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4261 bmap
= isl_basic_map_simplify(bmap
);
4262 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4263 return isl_basic_map_finalize(bmap
);
4265 isl_basic_map_free(bmap
);
4269 /* Turn the n dimensions of type type, starting at first
4270 * into existentially quantified variables.
4272 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4273 enum isl_dim_type type
, unsigned first
, unsigned n
)
4275 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4279 /* Turn the n dimensions of type type, starting at first
4280 * into existentially quantified variables.
4282 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4283 enum isl_dim_type type
, unsigned first
, unsigned n
)
4288 return map_space_reset(map
, type
);
4290 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4291 return isl_map_free(map
);
4293 map
= isl_map_cow(map
);
4297 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4301 for (i
= 0; i
< map
->n
; ++i
) {
4302 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4313 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4314 * into existentially quantified variables.
4316 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4317 enum isl_dim_type type
, unsigned first
, unsigned n
)
4321 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4322 return isl_map_free(map
);
4323 dim
= isl_map_dim(map
, type
);
4324 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4325 map
= isl_map_project_out(map
, type
, 0, first
);
4329 /* Turn the n dimensions of type type, starting at first
4330 * into existentially quantified variables.
4332 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4333 enum isl_dim_type type
, unsigned first
, unsigned n
)
4335 return set_from_map(isl_map_project_out(set_to_map(set
),
4339 /* Return a map that projects the elements in "set" onto their
4340 * "n" set dimensions starting at "first".
4341 * "type" should be equal to isl_dim_set.
4343 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4344 enum isl_dim_type type
, unsigned first
, unsigned n
)
4349 if (type
!= isl_dim_set
)
4350 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4351 "only set dimensions can be projected out", goto error
);
4352 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4353 return isl_set_free(set
);
4355 map
= isl_map_from_domain(set
);
4356 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4357 for (i
= 0; i
< n
; ++i
)
4358 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4366 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4371 for (i
= 0; i
< n
; ++i
) {
4372 j
= isl_basic_map_alloc_div(bmap
);
4375 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4379 isl_basic_map_free(bmap
);
4383 struct isl_basic_map
*isl_basic_map_apply_range(
4384 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4386 isl_space
*space_result
= NULL
;
4387 struct isl_basic_map
*bmap
;
4388 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4389 struct isl_dim_map
*dim_map1
, *dim_map2
;
4391 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4393 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4394 bmap2
->dim
, isl_dim_in
))
4395 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4396 "spaces don't match", goto error
);
4398 space_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4399 isl_space_copy(bmap2
->dim
));
4401 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4402 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4403 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4404 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4406 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4407 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4408 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4409 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4410 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4411 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4412 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4413 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4414 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4415 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4416 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4418 bmap
= isl_basic_map_alloc_space(space_result
,
4419 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4420 bmap1
->n_eq
+ bmap2
->n_eq
,
4421 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4422 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4423 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4424 bmap
= add_divs(bmap
, n
);
4425 bmap
= isl_basic_map_simplify(bmap
);
4426 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4427 return isl_basic_map_finalize(bmap
);
4429 isl_basic_map_free(bmap1
);
4430 isl_basic_map_free(bmap2
);
4434 struct isl_basic_set
*isl_basic_set_apply(
4435 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4440 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4443 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4446 isl_basic_set_free(bset
);
4447 isl_basic_map_free(bmap
);
4451 struct isl_basic_map
*isl_basic_map_apply_domain(
4452 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4454 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4456 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4457 bmap2
->dim
, isl_dim_in
))
4458 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4459 "spaces don't match", goto error
);
4461 bmap1
= isl_basic_map_reverse(bmap1
);
4462 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4463 return isl_basic_map_reverse(bmap1
);
4465 isl_basic_map_free(bmap1
);
4466 isl_basic_map_free(bmap2
);
4470 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4471 * A \cap B -> f(A) + f(B)
4473 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4474 __isl_take isl_basic_map
*bmap2
)
4476 unsigned n_in
, n_out
, nparam
, total
, pos
;
4477 struct isl_basic_map
*bmap
= NULL
;
4478 struct isl_dim_map
*dim_map1
, *dim_map2
;
4481 if (!bmap1
|| !bmap2
)
4484 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4487 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4488 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4489 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4491 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4492 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4493 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4494 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4495 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4496 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4497 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4498 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4499 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4500 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4501 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4503 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4504 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4505 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4506 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4507 for (i
= 0; i
< n_out
; ++i
) {
4508 int j
= isl_basic_map_alloc_equality(bmap
);
4511 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4512 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4513 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4514 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4516 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4517 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4518 bmap
= add_divs(bmap
, 2 * n_out
);
4520 bmap
= isl_basic_map_simplify(bmap
);
4521 return isl_basic_map_finalize(bmap
);
4523 isl_basic_map_free(bmap
);
4524 isl_basic_map_free(bmap1
);
4525 isl_basic_map_free(bmap2
);
4529 /* Given two maps A -> f(A) and B -> g(B), construct a map
4530 * A \cap B -> f(A) + f(B)
4532 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4533 __isl_take isl_map
*map2
)
4535 struct isl_map
*result
;
4541 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4543 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4544 map1
->n
* map2
->n
, 0);
4547 for (i
= 0; i
< map1
->n
; ++i
)
4548 for (j
= 0; j
< map2
->n
; ++j
) {
4549 struct isl_basic_map
*part
;
4550 part
= isl_basic_map_sum(
4551 isl_basic_map_copy(map1
->p
[i
]),
4552 isl_basic_map_copy(map2
->p
[j
]));
4553 if (isl_basic_map_is_empty(part
))
4554 isl_basic_map_free(part
);
4556 result
= isl_map_add_basic_map(result
, part
);
4569 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4570 __isl_take isl_set
*set2
)
4572 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4575 /* Given a basic map A -> f(A), construct A -> -f(A).
4577 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4582 bmap
= isl_basic_map_cow(bmap
);
4586 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4587 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4588 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4589 for (j
= 0; j
< n
; ++j
)
4590 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4591 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4592 for (j
= 0; j
< n
; ++j
)
4593 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4594 for (i
= 0; i
< bmap
->n_div
; ++i
)
4595 for (j
= 0; j
< n
; ++j
)
4596 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4597 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4598 return isl_basic_map_finalize(bmap
);
4601 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4603 return isl_basic_map_neg(bset
);
4606 /* Given a map A -> f(A), construct A -> -f(A).
4608 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4612 map
= isl_map_cow(map
);
4616 for (i
= 0; i
< map
->n
; ++i
) {
4617 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4628 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4630 return set_from_map(isl_map_neg(set_to_map(set
)));
4633 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4634 * A -> floor(f(A)/d).
4636 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4639 unsigned n_in
, n_out
, nparam
, total
, pos
;
4640 struct isl_basic_map
*result
= NULL
;
4641 struct isl_dim_map
*dim_map
;
4647 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4648 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4649 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4651 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4652 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4653 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4654 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4655 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4656 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4658 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4659 bmap
->n_div
+ n_out
,
4660 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4661 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4662 result
= add_divs(result
, n_out
);
4663 for (i
= 0; i
< n_out
; ++i
) {
4665 j
= isl_basic_map_alloc_inequality(result
);
4668 isl_seq_clr(result
->ineq
[j
], 1+total
);
4669 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4670 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4671 j
= isl_basic_map_alloc_inequality(result
);
4674 isl_seq_clr(result
->ineq
[j
], 1+total
);
4675 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4676 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4677 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4680 result
= isl_basic_map_simplify(result
);
4681 return isl_basic_map_finalize(result
);
4683 isl_basic_map_free(result
);
4687 /* Given a map A -> f(A) and an integer d, construct a map
4688 * A -> floor(f(A)/d).
4690 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4694 map
= isl_map_cow(map
);
4698 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4699 for (i
= 0; i
< map
->n
; ++i
) {
4700 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4704 map
= isl_map_unmark_normalized(map
);
4712 /* Given a map A -> f(A) and an integer d, construct a map
4713 * A -> floor(f(A)/d).
4715 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4716 __isl_take isl_val
*d
)
4720 if (!isl_val_is_int(d
))
4721 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4722 "expecting integer denominator", goto error
);
4723 map
= isl_map_floordiv(map
, d
->n
);
4732 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4739 i
= isl_basic_map_alloc_equality(bmap
);
4742 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4743 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4744 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4745 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4746 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4747 return isl_basic_map_finalize(bmap
);
4749 isl_basic_map_free(bmap
);
4753 /* Add a constraint to "bmap" expressing i_pos < o_pos
4755 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4762 i
= isl_basic_map_alloc_inequality(bmap
);
4765 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4766 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4767 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4768 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4769 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4770 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4771 return isl_basic_map_finalize(bmap
);
4773 isl_basic_map_free(bmap
);
4777 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4779 static __isl_give isl_basic_map
*var_less_or_equal(
4780 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4786 i
= isl_basic_map_alloc_inequality(bmap
);
4789 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4790 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4791 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4792 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4793 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4794 return isl_basic_map_finalize(bmap
);
4796 isl_basic_map_free(bmap
);
4800 /* Add a constraint to "bmap" expressing i_pos > o_pos
4802 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4809 i
= isl_basic_map_alloc_inequality(bmap
);
4812 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4813 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4814 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4815 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4816 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4817 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4818 return isl_basic_map_finalize(bmap
);
4820 isl_basic_map_free(bmap
);
4824 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4826 static __isl_give isl_basic_map
*var_more_or_equal(
4827 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4833 i
= isl_basic_map_alloc_inequality(bmap
);
4836 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4837 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4838 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4839 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4840 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4841 return isl_basic_map_finalize(bmap
);
4843 isl_basic_map_free(bmap
);
4847 __isl_give isl_basic_map
*isl_basic_map_equal(
4848 __isl_take isl_space
*space
, unsigned n_equal
)
4851 struct isl_basic_map
*bmap
;
4852 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4855 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4856 bmap
= var_equal(bmap
, i
);
4857 return isl_basic_map_finalize(bmap
);
4860 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4862 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4866 struct isl_basic_map
*bmap
;
4867 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4870 for (i
= 0; i
< pos
&& bmap
; ++i
)
4871 bmap
= var_equal(bmap
, i
);
4873 bmap
= var_less(bmap
, pos
);
4874 return isl_basic_map_finalize(bmap
);
4877 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4879 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4880 __isl_take isl_space
*space
, unsigned pos
)
4883 isl_basic_map
*bmap
;
4885 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4886 for (i
= 0; i
< pos
; ++i
)
4887 bmap
= var_equal(bmap
, i
);
4888 bmap
= var_less_or_equal(bmap
, pos
);
4889 return isl_basic_map_finalize(bmap
);
4892 /* Return a relation on "space" expressing i_pos > o_pos
4894 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4898 struct isl_basic_map
*bmap
;
4899 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4902 for (i
= 0; i
< pos
&& bmap
; ++i
)
4903 bmap
= var_equal(bmap
, i
);
4905 bmap
= var_more(bmap
, pos
);
4906 return isl_basic_map_finalize(bmap
);
4909 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4911 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4912 __isl_take isl_space
*space
, unsigned pos
)
4915 isl_basic_map
*bmap
;
4917 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4918 for (i
= 0; i
< pos
; ++i
)
4919 bmap
= var_equal(bmap
, i
);
4920 bmap
= var_more_or_equal(bmap
, pos
);
4921 return isl_basic_map_finalize(bmap
);
4924 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4925 unsigned n
, int equal
)
4927 struct isl_map
*map
;
4930 if (n
== 0 && equal
)
4931 return isl_map_universe(space
);
4933 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4935 for (i
= 0; i
+ 1 < n
; ++i
)
4936 map
= isl_map_add_basic_map(map
,
4937 isl_basic_map_less_at(isl_space_copy(space
), i
));
4940 map
= isl_map_add_basic_map(map
,
4941 isl_basic_map_less_or_equal_at(space
, n
- 1));
4943 map
= isl_map_add_basic_map(map
,
4944 isl_basic_map_less_at(space
, n
- 1));
4946 isl_space_free(space
);
4951 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
4955 return map_lex_lte_first(space
, space
->n_out
, equal
);
4958 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4960 return map_lex_lte_first(dim
, n
, 0);
4963 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4965 return map_lex_lte_first(dim
, n
, 1);
4968 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4970 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4973 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4975 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4978 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
4979 unsigned n
, int equal
)
4981 struct isl_map
*map
;
4984 if (n
== 0 && equal
)
4985 return isl_map_universe(space
);
4987 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4989 for (i
= 0; i
+ 1 < n
; ++i
)
4990 map
= isl_map_add_basic_map(map
,
4991 isl_basic_map_more_at(isl_space_copy(space
), i
));
4994 map
= isl_map_add_basic_map(map
,
4995 isl_basic_map_more_or_equal_at(space
, n
- 1));
4997 map
= isl_map_add_basic_map(map
,
4998 isl_basic_map_more_at(space
, n
- 1));
5000 isl_space_free(space
);
5005 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5009 return map_lex_gte_first(space
, space
->n_out
, equal
);
5012 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5014 return map_lex_gte_first(dim
, n
, 0);
5017 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5019 return map_lex_gte_first(dim
, n
, 1);
5022 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5024 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5027 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5029 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5032 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5033 __isl_take isl_set
*set2
)
5036 map
= isl_map_lex_le(isl_set_get_space(set1
));
5037 map
= isl_map_intersect_domain(map
, set1
);
5038 map
= isl_map_intersect_range(map
, set2
);
5042 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5043 __isl_take isl_set
*set2
)
5046 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5047 map
= isl_map_intersect_domain(map
, set1
);
5048 map
= isl_map_intersect_range(map
, set2
);
5052 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5053 __isl_take isl_set
*set2
)
5056 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5057 map
= isl_map_intersect_domain(map
, set1
);
5058 map
= isl_map_intersect_range(map
, set2
);
5062 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5063 __isl_take isl_set
*set2
)
5066 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5067 map
= isl_map_intersect_domain(map
, set1
);
5068 map
= isl_map_intersect_range(map
, set2
);
5072 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5073 __isl_take isl_map
*map2
)
5076 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5077 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5078 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5082 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5083 __isl_take isl_map
*map2
)
5086 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5087 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5088 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5092 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5093 __isl_take isl_map
*map2
)
5096 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5097 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5098 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5102 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5103 __isl_take isl_map
*map2
)
5106 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5107 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5108 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5112 /* For the div d = floor(f/m) at position "div", add the constraint
5116 static __isl_give isl_basic_map
*add_upper_div_constraint(
5117 __isl_take isl_basic_map
*bmap
, unsigned div
)
5120 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5121 unsigned n_div
, pos
;
5123 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5125 return isl_basic_map_free(bmap
);
5127 i
= isl_basic_map_alloc_inequality(bmap
);
5129 return isl_basic_map_free(bmap
);
5130 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5131 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5136 /* For the div d = floor(f/m) at position "div", add the constraint
5138 * -(f-(m-1)) + m d >= 0
5140 static __isl_give isl_basic_map
*add_lower_div_constraint(
5141 __isl_take isl_basic_map
*bmap
, unsigned div
)
5144 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5145 unsigned n_div
, pos
;
5147 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5149 return isl_basic_map_free(bmap
);
5151 i
= isl_basic_map_alloc_inequality(bmap
);
5153 return isl_basic_map_free(bmap
);
5154 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5155 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5156 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5157 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5162 /* For the div d = floor(f/m) at position "pos", add the constraints
5165 * -(f-(m-1)) + m d >= 0
5167 * Note that the second constraint is the negation of
5171 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5172 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5174 bmap
= add_upper_div_constraint(bmap
, pos
);
5175 bmap
= add_lower_div_constraint(bmap
, pos
);
5179 /* For each known div d = floor(f/m), add the constraints
5182 * -(f-(m-1)) + m d >= 0
5184 * Remove duplicate constraints in case of some these div constraints
5185 * already appear in "bmap".
5187 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5188 __isl_take isl_basic_map
*bmap
)
5194 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5198 bmap
= add_known_div_constraints(bmap
);
5199 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5200 bmap
= isl_basic_map_finalize(bmap
);
5204 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5206 * In particular, if this div is of the form d = floor(f/m),
5207 * then add the constraint
5211 * if sign < 0 or the constraint
5213 * -(f-(m-1)) + m d >= 0
5217 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5218 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5221 return add_upper_div_constraint(bmap
, div
);
5223 return add_lower_div_constraint(bmap
, div
);
5226 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5227 __isl_take isl_basic_map
*bmap
)
5231 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5233 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5234 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5235 return bset_from_bmap(bmap
);
5236 bmap
= isl_basic_map_cow(bmap
);
5239 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5242 bmap
->extra
-= bmap
->n_div
;
5244 bmap
= isl_basic_map_finalize(bmap
);
5245 return bset_from_bmap(bmap
);
5247 isl_basic_map_free(bmap
);
5251 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5252 __isl_take isl_basic_set
*bset
)
5254 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5257 /* Replace each element in "list" by the result of applying
5258 * isl_basic_map_underlying_set to the element.
5260 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5261 __isl_take isl_basic_map_list
*list
)
5268 n
= isl_basic_map_list_n_basic_map(list
);
5269 for (i
= 0; i
< n
; ++i
) {
5270 isl_basic_map
*bmap
;
5271 isl_basic_set
*bset
;
5273 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5274 bset
= isl_basic_set_underlying_set(bmap
);
5275 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5281 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5282 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5284 struct isl_basic_map
*bmap
;
5285 struct isl_ctx
*ctx
;
5292 if (isl_basic_set_check_no_params(bset
) < 0 ||
5293 isl_basic_set_check_no_locals(bset
) < 0)
5295 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5297 if (like
->n_div
== 0) {
5298 isl_space
*space
= isl_basic_map_get_space(like
);
5299 isl_basic_map_free(like
);
5300 return isl_basic_map_reset_space(bset
, space
);
5302 bset
= isl_basic_set_cow(bset
);
5305 total
= bset
->dim
->n_out
+ bset
->extra
;
5306 bmap
= bset_to_bmap(bset
);
5307 isl_space_free(bmap
->dim
);
5308 bmap
->dim
= isl_space_copy(like
->dim
);
5311 bmap
->n_div
= like
->n_div
;
5312 bmap
->extra
+= like
->n_div
;
5316 ltotal
= total
- bmap
->extra
+ like
->extra
;
5319 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5320 bmap
->extra
* (1 + 1 + total
));
5321 if (isl_blk_is_error(bmap
->block2
))
5323 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5327 for (i
= 0; i
< bmap
->extra
; ++i
)
5328 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5329 for (i
= 0; i
< like
->n_div
; ++i
) {
5330 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5331 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5333 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5335 isl_basic_map_free(like
);
5336 bmap
= isl_basic_map_simplify(bmap
);
5337 bmap
= isl_basic_map_finalize(bmap
);
5340 isl_basic_map_free(like
);
5341 isl_basic_set_free(bset
);
5345 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5346 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5348 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5349 bset_to_bmap(like
)));
5352 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5356 map
= isl_map_cow(map
);
5359 map
->dim
= isl_space_cow(map
->dim
);
5363 for (i
= 1; i
< map
->n
; ++i
)
5364 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5366 for (i
= 0; i
< map
->n
; ++i
) {
5367 map
->p
[i
] = bset_to_bmap(
5368 isl_basic_map_underlying_set(map
->p
[i
]));
5373 map
->dim
= isl_space_underlying(map
->dim
, 0);
5375 isl_space_free(map
->dim
);
5376 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5380 return set_from_map(map
);
5386 /* Replace the space of "bmap" by "space".
5388 * If the space of "bmap" is identical to "space" (including the identifiers
5389 * of the input and output dimensions), then simply return the original input.
5391 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5392 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5395 isl_space
*bmap_space
;
5397 bmap_space
= isl_basic_map_peek_space(bmap
);
5398 equal
= isl_space_is_equal(bmap_space
, space
);
5399 if (equal
>= 0 && equal
)
5400 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5404 isl_space_free(space
);
5407 bmap
= isl_basic_map_cow(bmap
);
5408 if (!bmap
|| !space
)
5411 isl_space_free(bmap
->dim
);
5414 bmap
= isl_basic_map_finalize(bmap
);
5418 isl_basic_map_free(bmap
);
5419 isl_space_free(space
);
5423 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5424 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5426 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5430 /* Check that the total dimensions of "map" and "space" are the same.
5432 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5433 __isl_keep isl_space
*space
)
5435 unsigned dim1
, dim2
;
5438 return isl_stat_error
;
5439 dim1
= isl_map_dim(map
, isl_dim_all
);
5440 dim2
= isl_space_dim(space
, isl_dim_all
);
5443 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5444 "total dimensions do not match", return isl_stat_error
);
5447 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5448 __isl_take isl_space
*space
)
5452 map
= isl_map_cow(map
);
5456 for (i
= 0; i
< map
->n
; ++i
) {
5457 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5458 isl_space_copy(space
));
5462 isl_space_free(map
->dim
);
5468 isl_space_free(space
);
5472 /* Replace the space of "map" by "space", without modifying
5473 * the dimension of "map".
5475 * If the space of "map" is identical to "space" (including the identifiers
5476 * of the input and output dimensions), then simply return the original input.
5478 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5479 __isl_take isl_space
*space
)
5482 isl_space
*map_space
;
5484 map_space
= isl_map_peek_space(map
);
5485 equal
= isl_space_is_equal(map_space
, space
);
5486 if (equal
>= 0 && equal
)
5487 equal
= isl_space_has_equal_ids(map_space
, space
);
5491 isl_space_free(space
);
5494 if (check_map_space_equal_total_dim(map
, space
) < 0)
5496 return isl_map_reset_space(map
, space
);
5499 isl_space_free(space
);
5503 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5504 __isl_take isl_space
*dim
)
5506 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5509 /* Compute the parameter domain of the given basic set.
5511 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5517 is_params
= isl_basic_set_is_params(bset
);
5519 return isl_basic_set_free(bset
);
5523 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5524 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5525 space
= isl_basic_set_get_space(bset
);
5526 space
= isl_space_params(space
);
5527 bset
= isl_basic_set_reset_space(bset
, space
);
5531 /* Construct a zero-dimensional basic set with the given parameter domain.
5533 __isl_give isl_basic_set
*isl_basic_set_from_params(
5534 __isl_take isl_basic_set
*bset
)
5537 space
= isl_basic_set_get_space(bset
);
5538 space
= isl_space_set_from_params(space
);
5539 bset
= isl_basic_set_reset_space(bset
, space
);
5543 /* Compute the parameter domain of the given set.
5545 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5550 if (isl_set_is_params(set
))
5553 n
= isl_set_dim(set
, isl_dim_set
);
5554 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5555 space
= isl_set_get_space(set
);
5556 space
= isl_space_params(space
);
5557 set
= isl_set_reset_space(set
, space
);
5561 /* Construct a zero-dimensional set with the given parameter domain.
5563 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5566 space
= isl_set_get_space(set
);
5567 space
= isl_space_set_from_params(space
);
5568 set
= isl_set_reset_space(set
, space
);
5572 /* Compute the parameter domain of the given map.
5574 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5579 n
= isl_map_dim(map
, isl_dim_in
);
5580 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5581 n
= isl_map_dim(map
, isl_dim_out
);
5582 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5583 space
= isl_map_get_space(map
);
5584 space
= isl_space_params(space
);
5585 map
= isl_map_reset_space(map
, space
);
5589 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5596 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5598 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5599 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5601 return isl_basic_map_reset_space(bmap
, space
);
5604 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5607 return isl_bool_error
;
5608 return isl_space_may_be_set(bmap
->dim
);
5611 /* Is this basic map actually a set?
5612 * Users should never call this function. Outside of isl,
5613 * the type should indicate whether something is a set or a map.
5615 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5618 return isl_bool_error
;
5619 return isl_space_is_set(bmap
->dim
);
5622 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5626 is_set
= isl_basic_map_is_set(bmap
);
5631 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5633 isl_basic_map_free(bmap
);
5637 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5638 __isl_take isl_basic_map
*bmap
)
5642 isl_basic_map
*domain
;
5643 int nparam
, n_in
, n_out
;
5645 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5646 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5647 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5649 space
= isl_basic_map_get_space(bmap
);
5650 space
= isl_space_from_range(isl_space_domain(space
));
5651 domain
= isl_basic_map_universe(space
);
5653 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5654 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5655 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5657 for (i
= 0; i
< n_in
; ++i
)
5658 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5661 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5662 return isl_basic_map_finalize(bmap
);
5665 __isl_give isl_basic_map
*isl_basic_map_range_map(
5666 __isl_take isl_basic_map
*bmap
)
5670 isl_basic_map
*range
;
5671 int nparam
, n_in
, n_out
;
5673 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5674 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5675 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5677 space
= isl_basic_map_get_space(bmap
);
5678 space
= isl_space_from_range(isl_space_range(space
));
5679 range
= isl_basic_map_universe(space
);
5681 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5682 bmap
= isl_basic_map_apply_range(bmap
, range
);
5683 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5685 for (i
= 0; i
< n_out
; ++i
)
5686 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5689 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5690 return isl_basic_map_finalize(bmap
);
5693 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5697 return isl_space_may_be_set(map
->dim
);
5700 /* Is this map actually a set?
5701 * Users should never call this function. Outside of isl,
5702 * the type should indicate whether something is a set or a map.
5704 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5707 return isl_bool_error
;
5708 return isl_space_is_set(map
->dim
);
5711 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5715 struct isl_set
*set
;
5717 is_set
= isl_map_is_set(map
);
5721 return set_from_map(map
);
5723 map
= isl_map_cow(map
);
5727 set
= set_from_map(map
);
5728 set
->dim
= isl_space_range(set
->dim
);
5731 for (i
= 0; i
< map
->n
; ++i
) {
5732 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5736 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5737 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5744 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5748 map
= isl_map_cow(map
);
5752 map
->dim
= isl_space_domain_map(map
->dim
);
5755 for (i
= 0; i
< map
->n
; ++i
) {
5756 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5760 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5761 map
= isl_map_unmark_normalized(map
);
5768 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5771 isl_space
*range_dim
;
5773 map
= isl_map_cow(map
);
5777 range_dim
= isl_space_range(isl_map_get_space(map
));
5778 range_dim
= isl_space_from_range(range_dim
);
5779 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5780 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5783 for (i
= 0; i
< map
->n
; ++i
) {
5784 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5788 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5789 map
= isl_map_unmark_normalized(map
);
5796 /* Given a wrapped map of the form A[B -> C],
5797 * return the map A[B -> C] -> B.
5799 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5806 if (!isl_set_has_tuple_id(set
))
5807 return isl_map_domain_map(isl_set_unwrap(set
));
5809 id
= isl_set_get_tuple_id(set
);
5810 map
= isl_map_domain_map(isl_set_unwrap(set
));
5811 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5816 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5817 __isl_take isl_basic_set
*bset
)
5819 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5822 __isl_give isl_basic_map
*isl_basic_map_from_range(
5823 __isl_take isl_basic_set
*bset
)
5826 space
= isl_basic_set_get_space(bset
);
5827 space
= isl_space_from_range(space
);
5828 bset
= isl_basic_set_reset_space(bset
, space
);
5829 return bset_to_bmap(bset
);
5832 /* Create a relation with the given set as range.
5833 * The domain of the created relation is a zero-dimensional
5834 * flat anonymous space.
5836 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5839 space
= isl_set_get_space(set
);
5840 space
= isl_space_from_range(space
);
5841 set
= isl_set_reset_space(set
, space
);
5842 return set_to_map(set
);
5845 /* Create a relation with the given set as domain.
5846 * The range of the created relation is a zero-dimensional
5847 * flat anonymous space.
5849 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5851 return isl_map_reverse(isl_map_from_range(set
));
5854 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5855 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5857 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5860 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5861 __isl_take isl_set
*range
)
5863 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5866 /* Return a newly allocated isl_map with given space and flags and
5867 * room for "n" basic maps.
5868 * Make sure that all cached information is cleared.
5870 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5873 struct isl_map
*map
;
5878 isl_die(space
->ctx
, isl_error_internal
,
5879 "negative number of basic maps", goto error
);
5880 map
= isl_calloc(space
->ctx
, struct isl_map
,
5881 sizeof(struct isl_map
) +
5882 (n
- 1) * sizeof(struct isl_basic_map
*));
5886 map
->ctx
= space
->ctx
;
5887 isl_ctx_ref(map
->ctx
);
5895 isl_space_free(space
);
5899 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5901 struct isl_basic_map
*bmap
;
5902 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5903 bmap
= isl_basic_map_set_to_empty(bmap
);
5907 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5909 struct isl_basic_set
*bset
;
5910 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5911 bset
= isl_basic_set_set_to_empty(bset
);
5915 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5917 struct isl_basic_map
*bmap
;
5918 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5919 bmap
= isl_basic_map_finalize(bmap
);
5923 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5925 struct isl_basic_set
*bset
;
5926 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5927 bset
= isl_basic_set_finalize(bset
);
5931 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
5932 __isl_take isl_space
*space
)
5935 unsigned total
= isl_space_dim(space
, isl_dim_all
);
5936 isl_basic_map
*bmap
;
5938 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
5939 for (i
= 0; i
< total
; ++i
) {
5940 int k
= isl_basic_map_alloc_inequality(bmap
);
5943 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5944 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5948 isl_basic_map_free(bmap
);
5952 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
5953 __isl_take isl_space
*space
)
5955 return isl_basic_map_nat_universe(space
);
5958 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5960 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5963 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5965 return isl_map_nat_universe(dim
);
5968 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
5970 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5973 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
5975 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5978 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
5980 struct isl_map
*map
;
5983 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5984 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
5988 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
5990 struct isl_set
*set
;
5993 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5994 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
5998 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6001 struct isl_map
*dup
;
6005 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6006 for (i
= 0; i
< map
->n
; ++i
)
6007 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6011 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6012 __isl_take isl_basic_map
*bmap
)
6016 if (isl_basic_map_plain_is_empty(bmap
)) {
6017 isl_basic_map_free(bmap
);
6020 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6021 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6022 map
->p
[map
->n
] = bmap
;
6024 map
= isl_map_unmark_normalized(map
);
6030 isl_basic_map_free(bmap
);
6034 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6045 isl_ctx_deref(map
->ctx
);
6046 for (i
= 0; i
< map
->n
; ++i
)
6047 isl_basic_map_free(map
->p
[i
]);
6048 isl_space_free(map
->dim
);
6054 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6055 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6059 bmap
= isl_basic_map_cow(bmap
);
6060 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6061 j
= isl_basic_map_alloc_equality(bmap
);
6064 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6065 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6066 isl_int_set_si(bmap
->eq
[j
][0], value
);
6067 bmap
= isl_basic_map_simplify(bmap
);
6068 return isl_basic_map_finalize(bmap
);
6070 isl_basic_map_free(bmap
);
6074 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6075 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6079 bmap
= isl_basic_map_cow(bmap
);
6080 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6081 j
= isl_basic_map_alloc_equality(bmap
);
6084 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6085 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6086 isl_int_set(bmap
->eq
[j
][0], value
);
6087 bmap
= isl_basic_map_simplify(bmap
);
6088 return isl_basic_map_finalize(bmap
);
6090 isl_basic_map_free(bmap
);
6094 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6095 enum isl_dim_type type
, unsigned pos
, int value
)
6097 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6098 return isl_basic_map_free(bmap
);
6099 return isl_basic_map_fix_pos_si(bmap
,
6100 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6103 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6104 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6106 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6107 return isl_basic_map_free(bmap
);
6108 return isl_basic_map_fix_pos(bmap
,
6109 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6112 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6113 * to be equal to "v".
6115 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6116 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6120 if (!isl_val_is_int(v
))
6121 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6122 "expecting integer value", goto error
);
6123 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6125 pos
+= isl_basic_map_offset(bmap
, type
);
6126 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6130 isl_basic_map_free(bmap
);
6135 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6136 * to be equal to "v".
6138 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6139 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6141 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6144 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6145 enum isl_dim_type type
, unsigned pos
, int value
)
6147 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6151 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6152 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6154 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6158 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6159 unsigned input
, int value
)
6161 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6164 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6165 unsigned dim
, int value
)
6167 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6168 isl_dim_set
, dim
, value
));
6171 /* Remove the basic map at position "i" from "map" if this basic map
6172 * is (obviously) empty.
6174 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6181 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6183 return isl_map_free(map
);
6187 isl_basic_map_free(map
->p
[i
]);
6190 map
->p
[i
] = map
->p
[map
->n
];
6191 map
= isl_map_unmark_normalized(map
);
6198 /* Perform "fn" on each basic map of "map", where we may not be holding
6199 * the only reference to "map".
6200 * In particular, "fn" should be a semantics preserving operation
6201 * that we want to apply to all copies of "map". We therefore need
6202 * to be careful not to modify "map" in a way that breaks "map"
6203 * in case anything goes wrong.
6205 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6206 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6208 struct isl_basic_map
*bmap
;
6214 for (i
= map
->n
- 1; i
>= 0; --i
) {
6215 bmap
= isl_basic_map_copy(map
->p
[i
]);
6219 isl_basic_map_free(map
->p
[i
]);
6221 map
= remove_if_empty(map
, i
);
6232 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6233 enum isl_dim_type type
, unsigned pos
, int value
)
6237 map
= isl_map_cow(map
);
6238 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6239 return isl_map_free(map
);
6240 for (i
= map
->n
- 1; i
>= 0; --i
) {
6241 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6242 map
= remove_if_empty(map
, i
);
6246 map
= isl_map_unmark_normalized(map
);
6250 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6251 enum isl_dim_type type
, unsigned pos
, int value
)
6253 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6256 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6257 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6261 map
= isl_map_cow(map
);
6262 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6263 return isl_map_free(map
);
6264 for (i
= 0; i
< map
->n
; ++i
) {
6265 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6269 map
= isl_map_unmark_normalized(map
);
6276 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6277 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6279 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6282 /* Fix the value of the variable at position "pos" of type "type" of "map"
6283 * to be equal to "v".
6285 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6286 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6290 map
= isl_map_cow(map
);
6294 if (!isl_val_is_int(v
))
6295 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6296 "expecting integer value", goto error
);
6297 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6299 for (i
= map
->n
- 1; i
>= 0; --i
) {
6300 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6302 map
= remove_if_empty(map
, i
);
6306 map
= isl_map_unmark_normalized(map
);
6315 /* Fix the value of the variable at position "pos" of type "type" of "set"
6316 * to be equal to "v".
6318 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6319 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6321 return isl_map_fix_val(set
, type
, pos
, v
);
6324 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6325 unsigned input
, int value
)
6327 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6330 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6332 return set_from_map(isl_map_fix_si(set_to_map(set
),
6333 isl_dim_set
, dim
, value
));
6336 static __isl_give isl_basic_map
*basic_map_bound_si(
6337 __isl_take isl_basic_map
*bmap
,
6338 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6342 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6343 return isl_basic_map_free(bmap
);
6344 pos
+= isl_basic_map_offset(bmap
, type
);
6345 bmap
= isl_basic_map_cow(bmap
);
6346 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6347 j
= isl_basic_map_alloc_inequality(bmap
);
6350 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6352 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6353 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6355 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6356 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6358 bmap
= isl_basic_map_simplify(bmap
);
6359 return isl_basic_map_finalize(bmap
);
6361 isl_basic_map_free(bmap
);
6365 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6366 __isl_take isl_basic_map
*bmap
,
6367 enum isl_dim_type type
, unsigned pos
, int value
)
6369 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6372 /* Constrain the values of the given dimension to be no greater than "value".
6374 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6375 __isl_take isl_basic_map
*bmap
,
6376 enum isl_dim_type type
, unsigned pos
, int value
)
6378 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6381 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6382 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6386 map
= isl_map_cow(map
);
6387 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6388 return isl_map_free(map
);
6389 for (i
= 0; i
< map
->n
; ++i
) {
6390 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6391 type
, pos
, value
, upper
);
6395 map
= isl_map_unmark_normalized(map
);
6402 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6403 enum isl_dim_type type
, unsigned pos
, int value
)
6405 return map_bound_si(map
, type
, pos
, value
, 0);
6408 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6409 enum isl_dim_type type
, unsigned pos
, int value
)
6411 return map_bound_si(map
, type
, pos
, value
, 1);
6414 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6415 enum isl_dim_type type
, unsigned pos
, int value
)
6417 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6421 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6422 enum isl_dim_type type
, unsigned pos
, int value
)
6424 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6427 /* Bound the given variable of "bmap" from below (or above is "upper"
6428 * is set) to "value".
6430 static __isl_give isl_basic_map
*basic_map_bound(
6431 __isl_take isl_basic_map
*bmap
,
6432 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6436 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6437 return isl_basic_map_free(bmap
);
6438 pos
+= isl_basic_map_offset(bmap
, type
);
6439 bmap
= isl_basic_map_cow(bmap
);
6440 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6441 j
= isl_basic_map_alloc_inequality(bmap
);
6444 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6446 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6447 isl_int_set(bmap
->ineq
[j
][0], value
);
6449 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6450 isl_int_neg(bmap
->ineq
[j
][0], value
);
6452 bmap
= isl_basic_map_simplify(bmap
);
6453 return isl_basic_map_finalize(bmap
);
6455 isl_basic_map_free(bmap
);
6459 /* Bound the given variable of "map" from below (or above is "upper"
6460 * is set) to "value".
6462 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6463 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6467 map
= isl_map_cow(map
);
6468 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6469 return isl_map_free(map
);
6470 for (i
= map
->n
- 1; i
>= 0; --i
) {
6471 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6472 map
= remove_if_empty(map
, i
);
6476 map
= isl_map_unmark_normalized(map
);
6480 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6481 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6483 return map_bound(map
, type
, pos
, value
, 0);
6486 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6487 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6489 return map_bound(map
, type
, pos
, value
, 1);
6492 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6493 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6495 return isl_map_lower_bound(set
, type
, pos
, value
);
6498 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6499 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6501 return isl_map_upper_bound(set
, type
, pos
, value
);
6504 /* Force the values of the variable at position "pos" of type "type" of "set"
6505 * to be no smaller than "value".
6507 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6508 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6512 if (!isl_val_is_int(value
))
6513 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6514 "expecting integer value", goto error
);
6515 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6516 isl_val_free(value
);
6519 isl_val_free(value
);
6524 /* Force the values of the variable at position "pos" of type "type" of "set"
6525 * to be no greater than "value".
6527 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6528 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6532 if (!isl_val_is_int(value
))
6533 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6534 "expecting integer value", goto error
);
6535 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6536 isl_val_free(value
);
6539 isl_val_free(value
);
6544 /* Bound the given variable of "bset" from below (or above is "upper"
6545 * is set) to "value".
6547 static __isl_give isl_basic_set
*isl_basic_set_bound(
6548 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6549 isl_int value
, int upper
)
6551 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6552 type
, pos
, value
, upper
));
6555 /* Bound the given variable of "bset" from below (or above is "upper"
6556 * is set) to "value".
6558 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
6559 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6560 __isl_take isl_val
*value
, int upper
)
6564 if (!isl_val_is_int(value
))
6565 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6566 "expecting integer value", goto error
);
6567 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6568 isl_val_free(value
);
6571 isl_val_free(value
);
6572 isl_basic_set_free(bset
);
6576 /* Bound the given variable of "bset" from below to "value".
6578 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6579 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6580 __isl_take isl_val
*value
)
6582 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6585 /* Bound the given variable of "bset" from above to "value".
6587 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6588 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6589 __isl_take isl_val
*value
)
6591 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6594 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6598 map
= isl_map_cow(map
);
6602 map
->dim
= isl_space_reverse(map
->dim
);
6605 for (i
= 0; i
< map
->n
; ++i
) {
6606 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6610 map
= isl_map_unmark_normalized(map
);
6618 #define TYPE isl_pw_multi_aff
6620 #define SUFFIX _pw_multi_aff
6622 #define EMPTY isl_pw_multi_aff_empty
6624 #define ADD isl_pw_multi_aff_union_add
6625 #include "isl_map_lexopt_templ.c"
6627 /* Given a map "map", compute the lexicographically minimal
6628 * (or maximal) image element for each domain element in dom,
6629 * in the form of an isl_pw_multi_aff.
6630 * If "empty" is not NULL, then set *empty to those elements in dom that
6631 * do not have an image element.
6632 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6633 * should be computed over the domain of "map". "empty" is also NULL
6636 * We first compute the lexicographically minimal or maximal element
6637 * in the first basic map. This results in a partial solution "res"
6638 * and a subset "todo" of dom that still need to be handled.
6639 * We then consider each of the remaining maps in "map" and successively
6640 * update both "res" and "todo".
6641 * If "empty" is NULL, then the todo sets are not needed and therefore
6642 * also not computed.
6644 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6645 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6646 __isl_give isl_set
**empty
, unsigned flags
)
6650 isl_pw_multi_aff
*res
;
6653 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6654 if (!map
|| (!full
&& !dom
))
6657 if (isl_map_plain_is_empty(map
)) {
6662 return isl_pw_multi_aff_from_map(map
);
6665 res
= basic_map_partial_lexopt_pw_multi_aff(
6666 isl_basic_map_copy(map
->p
[0]),
6667 isl_set_copy(dom
), empty
, flags
);
6671 for (i
= 1; i
< map
->n
; ++i
) {
6672 isl_pw_multi_aff
*res_i
;
6674 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6675 isl_basic_map_copy(map
->p
[i
]),
6676 isl_set_copy(dom
), empty
, flags
);
6678 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6679 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6681 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6684 todo
= isl_set_intersect(todo
, *empty
);
6703 #define TYPE isl_map
6707 #define EMPTY isl_map_empty
6709 #define ADD isl_map_union_disjoint
6710 #include "isl_map_lexopt_templ.c"
6712 /* Given a map "map", compute the lexicographically minimal
6713 * (or maximal) image element for each domain element in "dom",
6714 * in the form of an isl_map.
6715 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6716 * do not have an image element.
6717 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6718 * should be computed over the domain of "map". "empty" is also NULL
6721 * If the input consists of more than one disjunct, then first
6722 * compute the desired result in the form of an isl_pw_multi_aff and
6723 * then convert that into an isl_map.
6725 * This function used to have an explicit implementation in terms
6726 * of isl_maps, but it would continually intersect the domains of
6727 * partial results with the complement of the domain of the next
6728 * partial solution, potentially leading to an explosion in the number
6729 * of disjuncts if there are several disjuncts in the input.
6730 * An even earlier implementation of this function would look for
6731 * better results in the domain of the partial result and for extra
6732 * results in the complement of this domain, which would lead to
6733 * even more splintering.
6735 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6736 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6737 __isl_give isl_set
**empty
, unsigned flags
)
6740 struct isl_map
*res
;
6741 isl_pw_multi_aff
*pma
;
6743 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6744 if (!map
|| (!full
&& !dom
))
6747 if (isl_map_plain_is_empty(map
)) {
6756 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6762 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6764 return isl_map_from_pw_multi_aff(pma
);
6773 __isl_give isl_map
*isl_map_partial_lexmax(
6774 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6775 __isl_give isl_set
**empty
)
6777 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6780 __isl_give isl_map
*isl_map_partial_lexmin(
6781 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6782 __isl_give isl_set
**empty
)
6784 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6787 __isl_give isl_set
*isl_set_partial_lexmin(
6788 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6789 __isl_give isl_set
**empty
)
6791 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6795 __isl_give isl_set
*isl_set_partial_lexmax(
6796 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6797 __isl_give isl_set
**empty
)
6799 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6803 /* Compute the lexicographic minimum (or maximum if "flags" includes
6804 * ISL_OPT_MAX) of "bset" over its parametric domain.
6806 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6809 return isl_basic_map_lexopt(bset
, flags
);
6812 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6814 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6817 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6819 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6822 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6824 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6827 /* Compute the lexicographic minimum of "bset" over its parametric domain
6828 * for the purpose of quantifier elimination.
6829 * That is, find an explicit representation for all the existentially
6830 * quantified variables in "bset" by computing their lexicographic
6833 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6834 __isl_take isl_basic_set
*bset
)
6836 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6839 /* Given a basic map with one output dimension, compute the minimum or
6840 * maximum of that dimension as an isl_pw_aff.
6842 * Compute the optimum as a lexicographic optimum over the single
6843 * output dimension and extract the single isl_pw_aff from the result.
6845 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6848 isl_pw_multi_aff
*pma
;
6851 bmap
= isl_basic_map_copy(bmap
);
6852 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6853 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6854 isl_pw_multi_aff_free(pma
);
6859 /* Compute the minimum or maximum of the given output dimension
6860 * as a function of the parameters and the input dimensions,
6861 * but independently of the other output dimensions.
6863 * We first project out the other output dimension and then compute
6864 * the "lexicographic" maximum in each basic map, combining the results
6865 * using isl_pw_aff_union_max.
6867 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6874 n_out
= isl_map_dim(map
, isl_dim_out
);
6875 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6876 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6881 isl_space
*space
= isl_map_get_space(map
);
6883 return isl_pw_aff_empty(space
);
6886 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6887 for (i
= 1; i
< map
->n
; ++i
) {
6888 isl_pw_aff
*pwaff_i
;
6890 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6891 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6899 /* Compute the minimum of the given output dimension as a function of the
6900 * parameters and input dimensions, but independently of
6901 * the other output dimensions.
6903 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6905 return map_dim_opt(map
, pos
, 0);
6908 /* Compute the maximum of the given output dimension as a function of the
6909 * parameters and input dimensions, but independently of
6910 * the other output dimensions.
6912 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6914 return map_dim_opt(map
, pos
, 1);
6917 /* Compute the minimum or maximum of the given set dimension
6918 * as a function of the parameters,
6919 * but independently of the other set dimensions.
6921 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6924 return map_dim_opt(set
, pos
, max
);
6927 /* Compute the maximum of the given set dimension as a function of the
6928 * parameters, but independently of the other set dimensions.
6930 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6932 return set_dim_opt(set
, pos
, 1);
6935 /* Compute the minimum of the given set dimension as a function of the
6936 * parameters, but independently of the other set dimensions.
6938 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6940 return set_dim_opt(set
, pos
, 0);
6943 /* Apply a preimage specified by "mat" on the parameters of "bset".
6944 * bset is assumed to have only parameters and divs.
6946 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6947 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6954 bset
->dim
= isl_space_cow(bset
->dim
);
6958 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6960 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6962 bset
->dim
->nparam
= 0;
6963 bset
->dim
->n_out
= nparam
;
6964 bset
= isl_basic_set_preimage(bset
, mat
);
6966 bset
->dim
->nparam
= bset
->dim
->n_out
;
6967 bset
->dim
->n_out
= 0;
6972 isl_basic_set_free(bset
);
6976 /* Apply a preimage specified by "mat" on the parameters of "set".
6977 * set is assumed to have only parameters and divs.
6979 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6980 __isl_take isl_mat
*mat
)
6988 nparam
= isl_set_dim(set
, isl_dim_param
);
6990 if (mat
->n_row
!= 1 + nparam
)
6991 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6992 "unexpected number of rows", goto error
);
6994 space
= isl_set_get_space(set
);
6995 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6996 isl_dim_param
, 0, nparam
);
6997 set
= isl_set_reset_space(set
, space
);
6998 set
= isl_set_preimage(set
, mat
);
6999 nparam
= isl_set_dim(set
, isl_dim_out
);
7000 space
= isl_set_get_space(set
);
7001 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7002 isl_dim_out
, 0, nparam
);
7003 set
= isl_set_reset_space(set
, space
);
7011 /* Intersect the basic set "bset" with the affine space specified by the
7012 * equalities in "eq".
7014 static __isl_give isl_basic_set
*basic_set_append_equalities(
7015 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7023 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7028 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7029 for (i
= 0; i
< eq
->n_row
; ++i
) {
7030 k
= isl_basic_set_alloc_equality(bset
);
7033 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7034 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7038 bset
= isl_basic_set_gauss(bset
, NULL
);
7039 bset
= isl_basic_set_finalize(bset
);
7044 isl_basic_set_free(bset
);
7048 /* Intersect the set "set" with the affine space specified by the
7049 * equalities in "eq".
7051 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7059 for (i
= 0; i
< set
->n
; ++i
) {
7060 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7073 /* Given a basic set "bset" that only involves parameters and existentially
7074 * quantified variables, return the index of the first equality
7075 * that only involves parameters. If there is no such equality then
7076 * return bset->n_eq.
7078 * This function assumes that isl_basic_set_gauss has been called on "bset".
7080 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7083 unsigned nparam
, n_div
;
7088 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7089 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7091 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7092 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7099 /* Compute an explicit representation for the existentially quantified
7100 * variables in "bset" by computing the "minimal value" of the set
7101 * variables. Since there are no set variables, the computation of
7102 * the minimal value essentially computes an explicit representation
7103 * of the non-empty part(s) of "bset".
7105 * The input only involves parameters and existentially quantified variables.
7106 * All equalities among parameters have been removed.
7108 * Since the existentially quantified variables in the result are in general
7109 * going to be different from those in the input, we first replace
7110 * them by the minimal number of variables based on their equalities.
7111 * This should simplify the parametric integer programming.
7113 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7115 isl_morph
*morph1
, *morph2
;
7121 if (bset
->n_eq
== 0)
7122 return isl_basic_set_lexmin_compute_divs(bset
);
7124 morph1
= isl_basic_set_parameter_compression(bset
);
7125 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7126 bset
= isl_basic_set_lift(bset
);
7127 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7128 bset
= isl_morph_basic_set(morph2
, bset
);
7129 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7130 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7132 set
= isl_basic_set_lexmin_compute_divs(bset
);
7134 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7139 /* Project the given basic set onto its parameter domain, possibly introducing
7140 * new, explicit, existential variables in the constraints.
7141 * The input has parameters and (possibly implicit) existential variables.
7142 * The output has the same parameters, but only
7143 * explicit existentially quantified variables.
7145 * The actual projection is performed by pip, but pip doesn't seem
7146 * to like equalities very much, so we first remove the equalities
7147 * among the parameters by performing a variable compression on
7148 * the parameters. Afterward, an inverse transformation is performed
7149 * and the equalities among the parameters are inserted back in.
7151 * The variable compression on the parameters may uncover additional
7152 * equalities that were only implicit before. We therefore check
7153 * if there are any new parameter equalities in the result and
7154 * if so recurse. The removal of parameter equalities is required
7155 * for the parameter compression performed by base_compute_divs.
7157 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7161 struct isl_mat
*T
, *T2
;
7162 struct isl_set
*set
;
7165 bset
= isl_basic_set_cow(bset
);
7169 if (bset
->n_eq
== 0)
7170 return base_compute_divs(bset
);
7172 bset
= isl_basic_set_gauss(bset
, NULL
);
7175 if (isl_basic_set_plain_is_empty(bset
))
7176 return isl_set_from_basic_set(bset
);
7178 i
= first_parameter_equality(bset
);
7179 if (i
== bset
->n_eq
)
7180 return base_compute_divs(bset
);
7182 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7183 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7185 eq
= isl_mat_cow(eq
);
7186 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7187 if (T
&& T
->n_col
== 0) {
7191 bset
= isl_basic_set_set_to_empty(bset
);
7192 return isl_set_from_basic_set(bset
);
7194 bset
= basic_set_parameter_preimage(bset
, T
);
7196 i
= first_parameter_equality(bset
);
7199 else if (i
== bset
->n_eq
)
7200 set
= base_compute_divs(bset
);
7202 set
= parameter_compute_divs(bset
);
7203 set
= set_parameter_preimage(set
, T2
);
7204 set
= set_append_equalities(set
, eq
);
7208 /* Insert the divs from "ls" before those of "bmap".
7210 * The number of columns is not changed, which means that the last
7211 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7212 * The caller is responsible for removing the same number of dimensions
7213 * from the space of "bmap".
7215 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7216 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7222 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7226 old_n_div
= bmap
->n_div
;
7227 bmap
= insert_div_rows(bmap
, n_div
);
7231 for (i
= 0; i
< n_div
; ++i
) {
7232 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7233 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7239 /* Replace the space of "bmap" by the space and divs of "ls".
7241 * If "ls" has any divs, then we simplify the result since we may
7242 * have discovered some additional equalities that could simplify
7243 * the div expressions.
7245 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7246 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7250 bmap
= isl_basic_map_cow(bmap
);
7254 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7255 bmap
= insert_divs_from_local_space(bmap
, ls
);
7259 isl_space_free(bmap
->dim
);
7260 bmap
->dim
= isl_local_space_get_space(ls
);
7264 isl_local_space_free(ls
);
7266 bmap
= isl_basic_map_simplify(bmap
);
7267 bmap
= isl_basic_map_finalize(bmap
);
7270 isl_basic_map_free(bmap
);
7271 isl_local_space_free(ls
);
7275 /* Replace the space of "map" by the space and divs of "ls".
7277 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7278 __isl_take isl_local_space
*ls
)
7282 map
= isl_map_cow(map
);
7286 for (i
= 0; i
< map
->n
; ++i
) {
7287 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7288 isl_local_space_copy(ls
));
7292 isl_space_free(map
->dim
);
7293 map
->dim
= isl_local_space_get_space(ls
);
7297 isl_local_space_free(ls
);
7300 isl_local_space_free(ls
);
7305 /* Compute an explicit representation for the existentially
7306 * quantified variables for which do not know any explicit representation yet.
7308 * We first sort the existentially quantified variables so that the
7309 * existentially quantified variables for which we already have an explicit
7310 * representation are placed before those for which we do not.
7311 * The input dimensions, the output dimensions and the existentially
7312 * quantified variables for which we already have an explicit
7313 * representation are then turned into parameters.
7314 * compute_divs returns a map with the same parameters and
7315 * no input or output dimensions and the dimension specification
7316 * is reset to that of the input, including the existentially quantified
7317 * variables for which we already had an explicit representation.
7319 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7321 struct isl_basic_set
*bset
;
7322 struct isl_set
*set
;
7323 struct isl_map
*map
;
7325 isl_local_space
*ls
;
7332 bmap
= isl_basic_map_sort_divs(bmap
);
7333 bmap
= isl_basic_map_cow(bmap
);
7337 n_known
= isl_basic_map_first_unknown_div(bmap
);
7339 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7341 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7342 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7343 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7344 space
= isl_space_set_alloc(bmap
->ctx
,
7345 nparam
+ n_in
+ n_out
+ n_known
, 0);
7349 ls
= isl_basic_map_get_local_space(bmap
);
7350 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7351 n_known
, bmap
->n_div
- n_known
);
7353 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7354 swap_div(bmap
, i
- n_known
, i
);
7355 bmap
->n_div
-= n_known
;
7356 bmap
->extra
-= n_known
;
7358 bmap
= isl_basic_map_reset_space(bmap
, space
);
7359 bset
= bset_from_bmap(bmap
);
7361 set
= parameter_compute_divs(bset
);
7362 map
= set_to_map(set
);
7363 map
= replace_space_by_local_space(map
, ls
);
7367 isl_basic_map_free(bmap
);
7371 /* Remove the explicit representation of local variable "div",
7374 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7375 __isl_take isl_basic_map
*bmap
, int div
)
7379 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7381 return isl_basic_map_free(bmap
);
7385 bmap
= isl_basic_map_cow(bmap
);
7388 isl_int_set_si(bmap
->div
[div
][0], 0);
7392 /* Is local variable "div" of "bmap" marked as not having an explicit
7394 * Note that even if "div" is not marked in this way and therefore
7395 * has an explicit representation, this representation may still
7396 * depend (indirectly) on other local variables that do not
7397 * have an explicit representation.
7399 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7402 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7403 return isl_bool_error
;
7404 return isl_int_is_zero(bmap
->div
[div
][0]);
7407 /* Return the position of the first local variable that does not
7408 * have an explicit representation.
7409 * Return the total number of local variables if they all have
7410 * an explicit representation.
7411 * Return -1 on error.
7413 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7420 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7421 if (!isl_basic_map_div_is_known(bmap
, i
))
7427 /* Return the position of the first local variable that does not
7428 * have an explicit representation.
7429 * Return the total number of local variables if they all have
7430 * an explicit representation.
7431 * Return -1 on error.
7433 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7435 return isl_basic_map_first_unknown_div(bset
);
7438 /* Does "bmap" have an explicit representation for all local variables?
7440 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7444 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7445 first
= isl_basic_map_first_unknown_div(bmap
);
7447 return isl_bool_error
;
7451 /* Do all basic maps in "map" have an explicit representation
7452 * for all local variables?
7454 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7459 return isl_bool_error
;
7461 for (i
= 0; i
< map
->n
; ++i
) {
7462 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7467 return isl_bool_true
;
7470 /* If bmap contains any unknown divs, then compute explicit
7471 * expressions for them. However, this computation may be
7472 * quite expensive, so first try to remove divs that aren't
7475 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7478 struct isl_map
*map
;
7480 known
= isl_basic_map_divs_known(bmap
);
7484 return isl_map_from_basic_map(bmap
);
7486 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7488 known
= isl_basic_map_divs_known(bmap
);
7492 return isl_map_from_basic_map(bmap
);
7494 map
= compute_divs(bmap
);
7497 isl_basic_map_free(bmap
);
7501 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7505 struct isl_map
*res
;
7512 known
= isl_map_divs_known(map
);
7520 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7521 for (i
= 1 ; i
< map
->n
; ++i
) {
7523 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7524 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7525 res
= isl_map_union_disjoint(res
, r2
);
7527 res
= isl_map_union(res
, r2
);
7534 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7536 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7539 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7541 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7544 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7547 struct isl_set
*set
;
7552 map
= isl_map_cow(map
);
7556 set
= set_from_map(map
);
7557 set
->dim
= isl_space_domain(set
->dim
);
7560 for (i
= 0; i
< map
->n
; ++i
) {
7561 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7565 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7566 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7573 /* Return the union of "map1" and "map2", where we assume for now that
7574 * "map1" and "map2" are disjoint. Note that the basic maps inside
7575 * "map1" or "map2" may not be disjoint from each other.
7576 * Also note that this function is also called from isl_map_union,
7577 * which takes care of handling the situation where "map1" and "map2"
7578 * may not be disjoint.
7580 * If one of the inputs is empty, we can simply return the other input.
7581 * Similarly, if one of the inputs is universal, then it is equal to the union.
7583 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7584 __isl_take isl_map
*map2
)
7588 struct isl_map
*map
= NULL
;
7594 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7595 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7596 "spaces don't match", goto error
);
7607 is_universe
= isl_map_plain_is_universe(map1
);
7608 if (is_universe
< 0)
7615 is_universe
= isl_map_plain_is_universe(map2
);
7616 if (is_universe
< 0)
7623 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7624 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7625 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7627 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7628 map1
->n
+ map2
->n
, flags
);
7631 for (i
= 0; i
< map1
->n
; ++i
) {
7632 map
= isl_map_add_basic_map(map
,
7633 isl_basic_map_copy(map1
->p
[i
]));
7637 for (i
= 0; i
< map2
->n
; ++i
) {
7638 map
= isl_map_add_basic_map(map
,
7639 isl_basic_map_copy(map2
->p
[i
]));
7653 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7654 * guaranteed to be disjoint by the caller.
7656 * Note that this functions is called from within isl_map_make_disjoint,
7657 * so we have to be careful not to touch the constraints of the inputs
7660 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7661 __isl_take isl_map
*map2
)
7663 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7666 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7667 * not be disjoint. The parameters are assumed to have been aligned.
7669 * We currently simply call map_union_disjoint, the internal operation
7670 * of which does not really depend on the inputs being disjoint.
7671 * If the result contains more than one basic map, then we clear
7672 * the disjoint flag since the result may contain basic maps from
7673 * both inputs and these are not guaranteed to be disjoint.
7675 * As a special case, if "map1" and "map2" are obviously equal,
7676 * then we simply return "map1".
7678 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7679 __isl_take isl_map
*map2
)
7686 equal
= isl_map_plain_is_equal(map1
, map2
);
7694 map1
= map_union_disjoint(map1
, map2
);
7698 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7706 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7709 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7710 __isl_take isl_map
*map2
)
7712 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7715 __isl_give isl_set
*isl_set_union_disjoint(
7716 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7718 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7722 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7724 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7727 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7730 * "map" and "set" are assumed to be compatible and non-NULL.
7732 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7733 __isl_take isl_set
*set
,
7734 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7735 __isl_take isl_basic_set
*bset
))
7738 struct isl_map
*result
;
7741 if (isl_set_plain_is_universe(set
)) {
7746 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7747 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7748 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7750 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7751 map
->n
* set
->n
, flags
);
7752 for (i
= 0; result
&& i
< map
->n
; ++i
)
7753 for (j
= 0; j
< set
->n
; ++j
) {
7754 result
= isl_map_add_basic_map(result
,
7755 fn(isl_basic_map_copy(map
->p
[i
]),
7756 isl_basic_set_copy(set
->p
[j
])));
7766 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7767 __isl_take isl_set
*set
)
7771 ok
= isl_map_compatible_range(map
, set
);
7775 isl_die(set
->ctx
, isl_error_invalid
,
7776 "incompatible spaces", goto error
);
7778 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7785 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7786 __isl_take isl_set
*set
)
7788 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7791 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7792 __isl_take isl_set
*set
)
7796 ok
= isl_map_compatible_domain(map
, set
);
7800 isl_die(set
->ctx
, isl_error_invalid
,
7801 "incompatible spaces", goto error
);
7803 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7810 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7811 __isl_take isl_set
*set
)
7813 return isl_map_align_params_map_map_and(map
, set
,
7814 &map_intersect_domain
);
7817 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7818 * in the space B -> C, return the intersection.
7819 * The parameters are assumed to have been aligned.
7821 * The map "factor" is first extended to a map living in the space
7822 * [A -> B] -> C and then a regular intersection is computed.
7824 static __isl_give isl_map
*map_intersect_domain_factor_range(
7825 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7828 isl_map
*ext_factor
;
7830 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7831 ext_factor
= isl_map_universe(space
);
7832 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7833 return map_intersect(map
, ext_factor
);
7836 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7837 * in the space B -> C, return the intersection.
7839 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7840 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7842 return isl_map_align_params_map_map_and(map
, factor
,
7843 &map_intersect_domain_factor_range
);
7846 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7847 * in the space A -> C, return the intersection.
7849 * The map "factor" is first extended to a map living in the space
7850 * A -> [B -> C] and then a regular intersection is computed.
7852 static __isl_give isl_map
*map_intersect_range_factor_range(
7853 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7856 isl_map
*ext_factor
;
7858 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7859 ext_factor
= isl_map_universe(space
);
7860 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7861 return isl_map_intersect(map
, ext_factor
);
7864 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7865 * in the space A -> C, return the intersection.
7867 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7868 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7870 return isl_map_align_params_map_map_and(map
, factor
,
7871 &map_intersect_range_factor_range
);
7874 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7875 __isl_take isl_map
*map2
)
7879 map1
= isl_map_reverse(map1
);
7880 map1
= isl_map_apply_range(map1
, map2
);
7881 return isl_map_reverse(map1
);
7888 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7889 __isl_take isl_map
*map2
)
7891 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7894 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7895 __isl_take isl_map
*map2
)
7898 struct isl_map
*result
;
7904 space
= isl_space_join(isl_space_copy(map1
->dim
),
7905 isl_space_copy(map2
->dim
));
7907 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
7910 for (i
= 0; i
< map1
->n
; ++i
)
7911 for (j
= 0; j
< map2
->n
; ++j
) {
7912 result
= isl_map_add_basic_map(result
,
7913 isl_basic_map_apply_range(
7914 isl_basic_map_copy(map1
->p
[i
]),
7915 isl_basic_map_copy(map2
->p
[j
])));
7921 if (result
&& result
->n
<= 1)
7922 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7930 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7931 __isl_take isl_map
*map2
)
7933 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7937 * returns range - domain
7939 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
7941 isl_space
*target_space
;
7942 struct isl_basic_set
*bset
;
7949 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7950 bmap
->dim
, isl_dim_out
),
7952 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7953 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7954 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7955 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7956 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7957 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7958 for (i
= 0; i
< dim
; ++i
) {
7959 int j
= isl_basic_map_alloc_equality(bmap
);
7961 bmap
= isl_basic_map_free(bmap
);
7964 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7965 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7966 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7967 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7969 bset
= isl_basic_map_domain(bmap
);
7970 bset
= isl_basic_set_reset_space(bset
, target_space
);
7973 isl_basic_map_free(bmap
);
7977 /* Check that domain and range of "map" are the same.
7979 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
7984 space
= isl_map_peek_space(map
);
7985 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
7987 return isl_stat_error
;
7989 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7990 "domain and range don't match", return isl_stat_error
);
7995 * returns range - domain
7997 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8001 struct isl_set
*result
;
8006 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8007 map
->dim
, isl_dim_out
),
8009 dim
= isl_map_get_space(map
);
8010 dim
= isl_space_domain(dim
);
8011 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8014 for (i
= 0; i
< map
->n
; ++i
)
8015 result
= isl_set_add_basic_set(result
,
8016 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8025 * returns [domain -> range] -> range - domain
8027 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8028 __isl_take isl_basic_map
*bmap
)
8032 isl_basic_map
*domain
;
8036 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8037 bmap
->dim
, isl_dim_out
))
8038 isl_die(bmap
->ctx
, isl_error_invalid
,
8039 "domain and range don't match", goto error
);
8041 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8042 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8044 space
= isl_basic_map_get_space(bmap
);
8045 space
= isl_space_from_range(isl_space_domain(space
));
8046 domain
= isl_basic_map_universe(space
);
8048 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8049 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8050 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8052 total
= isl_basic_map_total_dim(bmap
);
8054 for (i
= 0; i
< n
; ++i
) {
8055 k
= isl_basic_map_alloc_equality(bmap
);
8058 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8059 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8060 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8061 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8064 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8065 return isl_basic_map_finalize(bmap
);
8067 isl_basic_map_free(bmap
);
8072 * returns [domain -> range] -> range - domain
8074 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8077 isl_space
*domain_space
;
8079 if (isl_map_check_equal_tuples(map
) < 0)
8080 return isl_map_free(map
);
8082 map
= isl_map_cow(map
);
8086 domain_space
= isl_space_domain(isl_map_get_space(map
));
8087 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8088 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8091 for (i
= 0; i
< map
->n
; ++i
) {
8092 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8096 map
= isl_map_unmark_normalized(map
);
8103 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8105 unsigned n_in
, n_out
;
8109 n_in
= isl_space_dim(space
, isl_dim_in
);
8110 n_out
= isl_space_dim(space
, isl_dim_out
);
8112 isl_die(space
->ctx
, isl_error_invalid
,
8113 "number of input and output dimensions needs to be "
8114 "the same", goto error
);
8115 return isl_basic_map_equal(space
, n_in
);
8117 isl_space_free(space
);
8121 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8123 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8126 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8128 isl_space
*dim
= isl_set_get_space(set
);
8130 id
= isl_map_identity(isl_space_map_from_set(dim
));
8131 return isl_map_intersect_range(id
, set
);
8134 /* Construct a basic set with all set dimensions having only non-negative
8137 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8138 __isl_take isl_space
*space
)
8143 struct isl_basic_set
*bset
;
8147 nparam
= isl_space_dim(space
, isl_dim_param
);
8148 dim
= isl_space_dim(space
, isl_dim_set
);
8149 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8152 for (i
= 0; i
< dim
; ++i
) {
8153 int k
= isl_basic_set_alloc_inequality(bset
);
8156 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8157 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8161 isl_basic_set_free(bset
);
8165 /* Construct the half-space x_pos >= 0.
8167 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8171 isl_basic_set
*nonneg
;
8173 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8174 k
= isl_basic_set_alloc_inequality(nonneg
);
8177 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8178 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8180 return isl_basic_set_finalize(nonneg
);
8182 isl_basic_set_free(nonneg
);
8186 /* Construct the half-space x_pos <= -1.
8188 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8194 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8195 k
= isl_basic_set_alloc_inequality(neg
);
8198 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8199 isl_int_set_si(neg
->ineq
[k
][0], -1);
8200 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8202 return isl_basic_set_finalize(neg
);
8204 isl_basic_set_free(neg
);
8208 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8209 enum isl_dim_type type
, unsigned first
, unsigned n
)
8213 isl_basic_set
*nonneg
;
8219 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8220 return isl_set_free(set
);
8222 offset
= pos(set
->dim
, type
);
8223 for (i
= 0; i
< n
; ++i
) {
8224 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8225 offset
+ first
+ i
);
8226 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8228 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8234 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8236 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8242 return isl_stat_error
;
8243 if (isl_set_plain_is_empty(set
)) {
8248 return fn(set
, signs
, user
);
8251 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8253 half
= isl_set_intersect(half
, isl_set_copy(set
));
8254 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8258 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8260 half
= isl_set_intersect(half
, set
);
8261 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8264 return isl_stat_error
;
8267 /* Call "fn" on the intersections of "set" with each of the orthants
8268 * (except for obviously empty intersections). The orthant is identified
8269 * by the signs array, with each entry having value 1 or -1 according
8270 * to the sign of the corresponding variable.
8272 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8273 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8282 return isl_stat_error
;
8283 if (isl_set_plain_is_empty(set
))
8286 nparam
= isl_set_dim(set
, isl_dim_param
);
8287 nvar
= isl_set_dim(set
, isl_dim_set
);
8289 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8291 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8299 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8301 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8304 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8305 __isl_keep isl_basic_map
*bmap2
)
8308 struct isl_map
*map1
;
8309 struct isl_map
*map2
;
8311 if (!bmap1
|| !bmap2
)
8312 return isl_bool_error
;
8314 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8315 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8317 is_subset
= isl_map_is_subset(map1
, map2
);
8325 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8326 __isl_keep isl_basic_set
*bset2
)
8328 return isl_basic_map_is_subset(bset1
, bset2
);
8331 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8332 __isl_keep isl_basic_map
*bmap2
)
8336 if (!bmap1
|| !bmap2
)
8337 return isl_bool_error
;
8338 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8339 if (is_subset
!= isl_bool_true
)
8341 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8345 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8346 __isl_keep isl_basic_set
*bset2
)
8348 return isl_basic_map_is_equal(
8349 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8352 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8358 return isl_bool_error
;
8359 for (i
= 0; i
< map
->n
; ++i
) {
8360 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8362 return isl_bool_error
;
8364 return isl_bool_false
;
8366 return isl_bool_true
;
8369 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8371 return map
? map
->n
== 0 : isl_bool_error
;
8374 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8376 return set
? set
->n
== 0 : isl_bool_error
;
8379 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8381 return isl_map_is_empty(set_to_map(set
));
8384 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8385 __isl_keep isl_map
*map2
)
8388 return isl_bool_error
;
8390 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8393 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8394 __isl_keep isl_set
*set2
)
8397 return isl_bool_error
;
8399 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8402 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8407 return isl_bool_error
;
8408 is_subset
= isl_map_is_subset(map1
, map2
);
8409 if (is_subset
!= isl_bool_true
)
8411 is_subset
= isl_map_is_subset(map2
, map1
);
8415 /* Is "map1" equal to "map2"?
8417 * First check if they are obviously equal.
8418 * If not, then perform a more detailed analysis.
8420 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8424 equal
= isl_map_plain_is_equal(map1
, map2
);
8425 if (equal
< 0 || equal
)
8427 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8430 isl_bool
isl_basic_map_is_strict_subset(
8431 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8435 if (!bmap1
|| !bmap2
)
8436 return isl_bool_error
;
8437 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8438 if (is_subset
!= isl_bool_true
)
8440 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8441 return isl_bool_not(is_subset
);
8444 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8445 __isl_keep isl_map
*map2
)
8450 return isl_bool_error
;
8451 is_subset
= isl_map_is_subset(map1
, map2
);
8452 if (is_subset
!= isl_bool_true
)
8454 is_subset
= isl_map_is_subset(map2
, map1
);
8455 return isl_bool_not(is_subset
);
8458 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8459 __isl_keep isl_set
*set2
)
8461 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8464 /* Is "bmap" obviously equal to the universe with the same space?
8466 * That is, does it not have any constraints?
8468 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8471 return isl_bool_error
;
8472 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8475 /* Is "bset" obviously equal to the universe with the same space?
8477 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8479 return isl_basic_map_plain_is_universe(bset
);
8482 /* If "c" does not involve any existentially quantified variables,
8483 * then set *univ to false and abort
8485 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8487 isl_bool
*univ
= user
;
8490 n
= isl_constraint_dim(c
, isl_dim_div
);
8491 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8492 isl_constraint_free(c
);
8493 if (*univ
< 0 || !*univ
)
8494 return isl_stat_error
;
8498 /* Is "bmap" equal to the universe with the same space?
8500 * First check if it is obviously equal to the universe.
8501 * If not and if there are any constraints not involving
8502 * existentially quantified variables, then it is certainly
8503 * not equal to the universe.
8504 * Otherwise, check if the universe is a subset of "bmap".
8506 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8509 isl_basic_map
*test
;
8511 univ
= isl_basic_map_plain_is_universe(bmap
);
8512 if (univ
< 0 || univ
)
8514 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8515 return isl_bool_false
;
8516 univ
= isl_bool_true
;
8517 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8519 return isl_bool_error
;
8520 if (univ
< 0 || !univ
)
8522 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8523 univ
= isl_basic_map_is_subset(test
, bmap
);
8524 isl_basic_map_free(test
);
8528 /* Is "bset" equal to the universe with the same space?
8530 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8532 return isl_basic_map_is_universe(bset
);
8535 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8540 return isl_bool_error
;
8542 for (i
= 0; i
< map
->n
; ++i
) {
8543 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8548 return isl_bool_false
;
8551 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8553 return isl_map_plain_is_universe(set_to_map(set
));
8556 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8558 struct isl_basic_set
*bset
= NULL
;
8559 struct isl_vec
*sample
= NULL
;
8560 isl_bool empty
, non_empty
;
8563 return isl_bool_error
;
8565 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8566 return isl_bool_true
;
8568 if (isl_basic_map_plain_is_universe(bmap
))
8569 return isl_bool_false
;
8571 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8572 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8573 copy
= isl_basic_map_remove_redundancies(copy
);
8574 empty
= isl_basic_map_plain_is_empty(copy
);
8575 isl_basic_map_free(copy
);
8579 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8581 return isl_bool_error
;
8583 return isl_bool_false
;
8584 isl_vec_free(bmap
->sample
);
8585 bmap
->sample
= NULL
;
8586 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8588 return isl_bool_error
;
8589 sample
= isl_basic_set_sample_vec(bset
);
8591 return isl_bool_error
;
8592 empty
= sample
->size
== 0;
8593 isl_vec_free(bmap
->sample
);
8594 bmap
->sample
= sample
;
8596 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8601 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8604 return isl_bool_error
;
8605 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8608 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8611 return isl_bool_error
;
8612 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8615 /* Is "bmap" known to be non-empty?
8617 * That is, is the cached sample still valid?
8619 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8624 return isl_bool_error
;
8626 return isl_bool_false
;
8627 total
= 1 + isl_basic_map_total_dim(bmap
);
8628 if (bmap
->sample
->size
!= total
)
8629 return isl_bool_false
;
8630 return isl_basic_map_contains(bmap
, bmap
->sample
);
8633 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8635 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8638 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8639 __isl_take isl_basic_map
*bmap2
)
8641 struct isl_map
*map
;
8642 if (!bmap1
|| !bmap2
)
8645 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8647 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8650 map
= isl_map_add_basic_map(map
, bmap1
);
8651 map
= isl_map_add_basic_map(map
, bmap2
);
8654 isl_basic_map_free(bmap1
);
8655 isl_basic_map_free(bmap2
);
8659 struct isl_set
*isl_basic_set_union(
8660 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8662 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8663 bset_to_bmap(bset2
)));
8666 /* Order divs such that any div only depends on previous divs */
8667 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8668 __isl_take isl_basic_map
*bmap
)
8673 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8675 return isl_basic_map_free(bmap
);
8677 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8679 if (isl_int_is_zero(bmap
->div
[i
][0]))
8681 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8686 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8687 "integer division depends on itself",
8688 return isl_basic_map_free(bmap
));
8689 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8697 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8699 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8702 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8709 for (i
= 0; i
< map
->n
; ++i
) {
8710 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8721 /* Sort the local variables of "bset".
8723 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8724 __isl_take isl_basic_set
*bset
)
8726 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8729 /* Apply the expansion computed by isl_merge_divs.
8730 * The expansion itself is given by "exp" while the resulting
8731 * list of divs is given by "div".
8733 * Move the integer divisions of "bmap" into the right position
8734 * according to "exp" and then introduce the additional integer
8735 * divisions, adding div constraints.
8736 * The moving should be done first to avoid moving coefficients
8737 * in the definitions of the extra integer divisions.
8739 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8740 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8745 bmap
= isl_basic_map_cow(bmap
);
8749 if (div
->n_row
< bmap
->n_div
)
8750 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8751 "not an expansion", goto error
);
8753 n_div
= bmap
->n_div
;
8754 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8755 div
->n_row
- n_div
, 0,
8756 2 * (div
->n_row
- n_div
));
8758 for (i
= n_div
; i
< div
->n_row
; ++i
)
8759 if (isl_basic_map_alloc_div(bmap
) < 0)
8762 for (j
= n_div
- 1; j
>= 0; --j
) {
8765 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8770 for (i
= 0; i
< div
->n_row
; ++i
) {
8771 if (j
< n_div
&& exp
[j
] == i
) {
8774 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8775 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8777 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8786 isl_basic_map_free(bmap
);
8791 /* Apply the expansion computed by isl_merge_divs.
8792 * The expansion itself is given by "exp" while the resulting
8793 * list of divs is given by "div".
8795 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8796 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8798 return isl_basic_map_expand_divs(bset
, div
, exp
);
8801 /* Look for a div in dst that corresponds to the div "div" in src.
8802 * The divs before "div" in src and dst are assumed to be the same.
8804 * Return the position of the corresponding div in dst
8805 * if there is one. Otherwise, return a position beyond the integer divisions.
8806 * Return -1 on error.
8808 static int find_div(__isl_keep isl_basic_map
*dst
,
8809 __isl_keep isl_basic_map
*src
, unsigned div
)
8815 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8816 if (!dst
|| v_div
< 0)
8819 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8820 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8821 for (i
= div
; i
< n_div
; ++i
)
8822 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8823 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8829 /* Align the divs of "dst" to those of "src", adding divs from "src"
8830 * if needed. That is, make sure that the first src->n_div divs
8831 * of the result are equal to those of src.
8833 * The result is not finalized as by design it will have redundant
8834 * divs if any divs from "src" were copied.
8836 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8837 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8846 return isl_basic_map_free(dst
);
8848 if (src
->n_div
== 0)
8851 known
= isl_basic_map_divs_known(src
);
8853 return isl_basic_map_free(dst
);
8855 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8856 "some src divs are unknown",
8857 return isl_basic_map_free(dst
));
8859 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8861 return isl_basic_map_free(dst
);
8863 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8865 return isl_basic_map_free(dst
);
8868 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8869 for (i
= 0; i
< src
->n_div
; ++i
) {
8870 int j
= find_div(dst
, src
, i
);
8872 dst
= isl_basic_map_free(dst
);
8873 if (j
== dst_n_div
) {
8875 int extra
= src
->n_div
- i
;
8876 dst
= isl_basic_map_cow(dst
);
8879 dst
= isl_basic_map_extend_space(dst
,
8880 isl_space_copy(dst
->dim
),
8881 extra
, 0, 2 * extra
);
8884 j
= isl_basic_map_alloc_div(dst
);
8887 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
8888 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
8890 dst
= isl_basic_map_add_div_constraints(dst
, j
);
8895 dst
= isl_basic_map_swap_div(dst
, i
, j
);
8899 isl_basic_map_free(src
);
8902 isl_basic_map_free(src
);
8903 isl_basic_map_free(dst
);
8907 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8915 map
= isl_map_compute_divs(map
);
8916 map
= isl_map_cow(map
);
8920 for (i
= 1; i
< map
->n
; ++i
)
8921 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8922 for (i
= 1; i
< map
->n
; ++i
) {
8923 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8925 return isl_map_free(map
);
8928 map
= isl_map_unmark_normalized(map
);
8932 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8934 return isl_map_align_divs_internal(map
);
8937 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8939 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8942 /* Align the divs of the basic maps in "map" to those
8943 * of the basic maps in "list", as well as to the other basic maps in "map".
8944 * The elements in "list" are assumed to have known divs.
8946 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8947 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8951 map
= isl_map_compute_divs(map
);
8952 map
= isl_map_cow(map
);
8954 return isl_map_free(map
);
8958 n
= isl_basic_map_list_n_basic_map(list
);
8959 for (i
= 0; i
< n
; ++i
) {
8960 isl_basic_map
*bmap
;
8962 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8963 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8964 isl_basic_map_free(bmap
);
8967 return isl_map_free(map
);
8969 return isl_map_align_divs_internal(map
);
8972 /* Align the divs of each element of "list" to those of "bmap".
8973 * Both "bmap" and the elements of "list" are assumed to have known divs.
8975 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8976 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8981 return isl_basic_map_list_free(list
);
8983 n
= isl_basic_map_list_n_basic_map(list
);
8984 for (i
= 0; i
< n
; ++i
) {
8985 isl_basic_map
*bmap_i
;
8987 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8988 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8989 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8995 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8996 __isl_take isl_map
*map
)
9000 ok
= isl_map_compatible_domain(map
, set
);
9004 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9005 "incompatible spaces", goto error
);
9006 map
= isl_map_intersect_domain(map
, set
);
9007 set
= isl_map_range(map
);
9015 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9016 __isl_take isl_map
*map
)
9018 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9021 /* There is no need to cow as removing empty parts doesn't change
9022 * the meaning of the set.
9024 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9031 for (i
= map
->n
- 1; i
>= 0; --i
)
9032 map
= remove_if_empty(map
, i
);
9037 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9039 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9042 /* Create a binary relation that maps the shared initial "pos" dimensions
9043 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9045 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9046 __isl_keep isl_basic_set
*bset2
, int pos
)
9048 isl_basic_map
*bmap1
;
9049 isl_basic_map
*bmap2
;
9051 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9052 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9053 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9054 isl_dim_out
, 0, pos
);
9055 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9056 isl_dim_out
, 0, pos
);
9057 return isl_basic_map_range_product(bmap1
, bmap2
);
9060 /* Given two basic sets bset1 and bset2, compute the maximal difference
9061 * between the values of dimension pos in bset1 and those in bset2
9062 * for any common value of the parameters and dimensions preceding pos.
9064 static enum isl_lp_result
basic_set_maximal_difference_at(
9065 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9066 int pos
, isl_int
*opt
)
9068 isl_basic_map
*bmap1
;
9069 struct isl_ctx
*ctx
;
9070 struct isl_vec
*obj
;
9074 enum isl_lp_result res
;
9076 if (!bset1
|| !bset2
)
9077 return isl_lp_error
;
9079 nparam
= isl_basic_set_n_param(bset1
);
9080 dim1
= isl_basic_set_n_dim(bset1
);
9082 bmap1
= join_initial(bset1
, bset2
, pos
);
9084 return isl_lp_error
;
9086 total
= isl_basic_map_total_dim(bmap1
);
9088 obj
= isl_vec_alloc(ctx
, 1 + total
);
9091 isl_seq_clr(obj
->block
.data
, 1 + total
);
9092 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9093 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9094 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9096 isl_basic_map_free(bmap1
);
9100 isl_basic_map_free(bmap1
);
9101 return isl_lp_error
;
9104 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9105 * for any common value of the parameters and dimensions preceding pos
9106 * in both basic sets, the values of dimension pos in bset1 are
9107 * smaller or larger than those in bset2.
9110 * 1 if bset1 follows bset2
9111 * -1 if bset1 precedes bset2
9112 * 0 if bset1 and bset2 are incomparable
9113 * -2 if some error occurred.
9115 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9116 __isl_keep isl_basic_set
*bset2
, int pos
)
9119 enum isl_lp_result res
;
9124 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9126 if (res
== isl_lp_empty
)
9128 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9129 res
== isl_lp_unbounded
)
9131 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9140 /* Given two basic sets bset1 and bset2, check whether
9141 * for any common value of the parameters and dimensions preceding pos
9142 * there is a value of dimension pos in bset1 that is larger
9143 * than a value of the same dimension in bset2.
9146 * 1 if there exists such a pair
9147 * 0 if there is no such pair, but there is a pair of equal values
9149 * -2 if some error occurred.
9151 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9152 __isl_keep isl_basic_set
*bset2
, int pos
)
9155 isl_basic_map
*bmap
;
9158 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9159 bmap
= join_initial(bset1
, bset2
, pos
);
9160 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9161 isl_dim_out
, dim1
- pos
);
9162 empty
= isl_basic_map_is_empty(bmap
);
9166 isl_basic_map_free(bmap
);
9169 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9170 isl_dim_out
, dim1
- pos
);
9171 empty
= isl_basic_map_is_empty(bmap
);
9174 isl_basic_map_free(bmap
);
9179 isl_basic_map_free(bmap
);
9183 /* Given two sets set1 and set2, check whether
9184 * for any common value of the parameters and dimensions preceding pos
9185 * there is a value of dimension pos in set1 that is larger
9186 * than a value of the same dimension in set2.
9189 * 1 if there exists such a pair
9190 * 0 if there is no such pair, but there is a pair of equal values
9192 * -2 if some error occurred.
9194 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9195 __isl_keep isl_set
*set2
, int pos
)
9203 for (i
= 0; i
< set1
->n
; ++i
)
9204 for (j
= 0; j
< set2
->n
; ++j
) {
9206 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9207 if (f
== 1 || f
== -2)
9216 static isl_bool
isl_basic_map_plain_has_fixed_var(
9217 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9224 return isl_bool_error
;
9225 total
= isl_basic_map_total_dim(bmap
);
9226 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9227 for (; d
+1 > pos
; --d
)
9228 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9232 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9233 return isl_bool_false
;
9234 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9235 return isl_bool_false
;
9236 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9237 return isl_bool_false
;
9239 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9240 return isl_bool_true
;
9242 return isl_bool_false
;
9245 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9246 unsigned pos
, isl_int
*val
)
9254 return isl_bool_error
;
9256 return isl_bool_false
;
9258 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9261 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9262 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9263 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9264 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9265 fixed
= isl_bool_false
;
9268 isl_int_set(*val
, v
);
9274 static isl_bool
isl_basic_set_plain_has_fixed_var(
9275 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9277 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9281 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9282 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9284 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9285 return isl_bool_error
;
9286 return isl_basic_map_plain_has_fixed_var(bmap
,
9287 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9290 /* If "bmap" obviously lies on a hyperplane where the given dimension
9291 * has a fixed value, then return that value.
9292 * Otherwise return NaN.
9294 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9295 __isl_keep isl_basic_map
*bmap
,
9296 enum isl_dim_type type
, unsigned pos
)
9304 ctx
= isl_basic_map_get_ctx(bmap
);
9305 v
= isl_val_alloc(ctx
);
9308 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9310 return isl_val_free(v
);
9312 isl_int_set_si(v
->d
, 1);
9316 return isl_val_nan(ctx
);
9319 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9320 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9322 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9323 return isl_bool_error
;
9324 return isl_map_plain_has_fixed_var(map
,
9325 map_offset(map
, type
) - 1 + pos
, val
);
9328 /* If "map" obviously lies on a hyperplane where the given dimension
9329 * has a fixed value, then return that value.
9330 * Otherwise return NaN.
9332 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9333 enum isl_dim_type type
, unsigned pos
)
9341 ctx
= isl_map_get_ctx(map
);
9342 v
= isl_val_alloc(ctx
);
9345 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9347 return isl_val_free(v
);
9349 isl_int_set_si(v
->d
, 1);
9353 return isl_val_nan(ctx
);
9356 /* If "set" obviously lies on a hyperplane where the given dimension
9357 * has a fixed value, then return that value.
9358 * Otherwise return NaN.
9360 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9361 enum isl_dim_type type
, unsigned pos
)
9363 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9366 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9367 * then return this fixed value in *val.
9369 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9370 unsigned dim
, isl_int
*val
)
9372 return isl_basic_set_plain_has_fixed_var(bset
,
9373 isl_basic_set_n_param(bset
) + dim
, val
);
9376 /* Return -1 if the constraint "c1" should be sorted before "c2"
9377 * and 1 if it should be sorted after "c2".
9378 * Return 0 if the two constraints are the same (up to the constant term).
9380 * In particular, if a constraint involves later variables than another
9381 * then it is sorted after this other constraint.
9382 * uset_gist depends on constraints without existentially quantified
9383 * variables sorting first.
9385 * For constraints that have the same latest variable, those
9386 * with the same coefficient for this latest variable (first in absolute value
9387 * and then in actual value) are grouped together.
9388 * This is useful for detecting pairs of constraints that can
9389 * be chained in their printed representation.
9391 * Finally, within a group, constraints are sorted according to
9392 * their coefficients (excluding the constant term).
9394 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9396 isl_int
**c1
= (isl_int
**) p1
;
9397 isl_int
**c2
= (isl_int
**) p2
;
9399 unsigned size
= *(unsigned *) arg
;
9402 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9403 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9408 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9411 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9415 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9418 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9419 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9420 * and 0 if the two constraints are the same (up to the constant term).
9422 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9423 isl_int
*c1
, isl_int
*c2
)
9429 total
= isl_basic_map_total_dim(bmap
);
9430 return sort_constraint_cmp(&c1
, &c2
, &total
);
9433 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9434 __isl_take isl_basic_map
*bmap
)
9440 if (bmap
->n_ineq
== 0)
9442 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9444 total
= isl_basic_map_total_dim(bmap
);
9445 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9446 &sort_constraint_cmp
, &total
) < 0)
9447 return isl_basic_map_free(bmap
);
9448 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9452 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9453 __isl_take isl_basic_set
*bset
)
9455 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9456 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9459 __isl_give isl_basic_map
*isl_basic_map_normalize(
9460 __isl_take isl_basic_map
*bmap
)
9462 bmap
= isl_basic_map_remove_redundancies(bmap
);
9463 bmap
= isl_basic_map_sort_constraints(bmap
);
9466 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9467 __isl_keep isl_basic_map
*bmap2
)
9471 isl_space
*space1
, *space2
;
9473 if (!bmap1
|| !bmap2
)
9478 space1
= isl_basic_map_peek_space(bmap1
);
9479 space2
= isl_basic_map_peek_space(bmap2
);
9480 cmp
= isl_space_cmp(space1
, space2
);
9483 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9484 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9485 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9486 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9487 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9489 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9491 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9493 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9494 return bmap1
->n_eq
- bmap2
->n_eq
;
9495 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9496 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9497 if (bmap1
->n_div
!= bmap2
->n_div
)
9498 return bmap1
->n_div
- bmap2
->n_div
;
9499 total
= isl_basic_map_total_dim(bmap1
);
9500 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9501 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9505 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9506 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9510 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9511 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9518 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9519 __isl_keep isl_basic_set
*bset2
)
9521 return isl_basic_map_plain_cmp(bset1
, bset2
);
9524 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9530 if (set1
->n
!= set2
->n
)
9531 return set1
->n
- set2
->n
;
9533 for (i
= 0; i
< set1
->n
; ++i
) {
9534 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9542 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9543 __isl_keep isl_basic_map
*bmap2
)
9545 if (!bmap1
|| !bmap2
)
9546 return isl_bool_error
;
9547 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9550 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9551 __isl_keep isl_basic_set
*bset2
)
9553 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9554 bset_to_bmap(bset2
));
9557 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9559 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9560 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9562 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9565 /* Sort the basic maps of "map" and remove duplicate basic maps.
9567 * While removing basic maps, we make sure that the basic maps remain
9568 * sorted because isl_map_normalize expects the basic maps of the result
9571 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9575 map
= isl_map_remove_empty_parts(map
);
9578 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9579 for (i
= map
->n
- 1; i
>= 1; --i
) {
9580 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9582 isl_basic_map_free(map
->p
[i
-1]);
9583 for (j
= i
; j
< map
->n
; ++j
)
9584 map
->p
[j
- 1] = map
->p
[j
];
9591 /* Remove obvious duplicates among the basic maps of "map".
9593 * Unlike isl_map_normalize, this function does not remove redundant
9594 * constraints and only removes duplicates that have exactly the same
9595 * constraints in the input. It does sort the constraints and
9596 * the basic maps to ease the detection of duplicates.
9598 * If "map" has already been normalized or if the basic maps are
9599 * disjoint, then there can be no duplicates.
9601 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9604 isl_basic_map
*bmap
;
9610 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9612 for (i
= 0; i
< map
->n
; ++i
) {
9613 bmap
= isl_basic_map_copy(map
->p
[i
]);
9614 bmap
= isl_basic_map_sort_constraints(bmap
);
9616 return isl_map_free(map
);
9617 isl_basic_map_free(map
->p
[i
]);
9621 map
= sort_and_remove_duplicates(map
);
9625 /* We normalize in place, but if anything goes wrong we need
9626 * to return NULL, so we need to make sure we don't change the
9627 * meaning of any possible other copies of map.
9629 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9632 struct isl_basic_map
*bmap
;
9636 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9638 for (i
= 0; i
< map
->n
; ++i
) {
9639 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9642 isl_basic_map_free(map
->p
[i
]);
9646 map
= sort_and_remove_duplicates(map
);
9648 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9655 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9657 return set_from_map(isl_map_normalize(set_to_map(set
)));
9660 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9661 __isl_keep isl_map
*map2
)
9667 return isl_bool_error
;
9670 return isl_bool_true
;
9671 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9672 return isl_bool_false
;
9674 map1
= isl_map_copy(map1
);
9675 map2
= isl_map_copy(map2
);
9676 map1
= isl_map_normalize(map1
);
9677 map2
= isl_map_normalize(map2
);
9680 equal
= map1
->n
== map2
->n
;
9681 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9682 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9692 return isl_bool_error
;
9695 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9696 __isl_keep isl_set
*set2
)
9698 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9701 /* Return the basic maps in "map" as a list.
9703 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9704 __isl_keep isl_map
*map
)
9708 isl_basic_map_list
*list
;
9712 ctx
= isl_map_get_ctx(map
);
9713 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9715 for (i
= 0; i
< map
->n
; ++i
) {
9716 isl_basic_map
*bmap
;
9718 bmap
= isl_basic_map_copy(map
->p
[i
]);
9719 list
= isl_basic_map_list_add(list
, bmap
);
9725 /* Return the intersection of the elements in the non-empty list "list".
9726 * All elements are assumed to live in the same space.
9728 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9729 __isl_take isl_basic_map_list
*list
)
9732 isl_basic_map
*bmap
;
9736 n
= isl_basic_map_list_n_basic_map(list
);
9738 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9739 "expecting non-empty list", goto error
);
9741 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9742 for (i
= 1; i
< n
; ++i
) {
9743 isl_basic_map
*bmap_i
;
9745 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9746 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9749 isl_basic_map_list_free(list
);
9752 isl_basic_map_list_free(list
);
9756 /* Return the intersection of the elements in the non-empty list "list".
9757 * All elements are assumed to live in the same space.
9759 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9760 __isl_take isl_basic_set_list
*list
)
9762 return isl_basic_map_list_intersect(list
);
9765 /* Return the union of the elements of "list".
9766 * The list is required to have at least one element.
9768 __isl_give isl_set
*isl_basic_set_list_union(
9769 __isl_take isl_basic_set_list
*list
)
9773 isl_basic_set
*bset
;
9778 n
= isl_basic_set_list_n_basic_set(list
);
9780 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9781 "expecting non-empty list", goto error
);
9783 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9784 space
= isl_basic_set_get_space(bset
);
9785 isl_basic_set_free(bset
);
9787 set
= isl_set_alloc_space(space
, n
, 0);
9788 for (i
= 0; i
< n
; ++i
) {
9789 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9790 set
= isl_set_add_basic_set(set
, bset
);
9793 isl_basic_set_list_free(list
);
9796 isl_basic_set_list_free(list
);
9800 /* Return the union of the elements in the non-empty list "list".
9801 * All elements are assumed to live in the same space.
9803 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9810 n
= isl_set_list_n_set(list
);
9812 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9813 "expecting non-empty list", goto error
);
9815 set
= isl_set_list_get_set(list
, 0);
9816 for (i
= 1; i
< n
; ++i
) {
9819 set_i
= isl_set_list_get_set(list
, i
);
9820 set
= isl_set_union(set
, set_i
);
9823 isl_set_list_free(list
);
9826 isl_set_list_free(list
);
9830 __isl_give isl_basic_map
*isl_basic_map_product(
9831 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9833 isl_space
*space_result
= NULL
;
9834 struct isl_basic_map
*bmap
;
9835 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9836 struct isl_dim_map
*dim_map1
, *dim_map2
;
9838 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9840 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9841 isl_space_copy(bmap2
->dim
));
9843 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9844 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9845 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9846 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9847 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9849 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9850 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9851 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9852 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9853 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9854 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9855 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9856 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9857 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9858 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9859 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9861 bmap
= isl_basic_map_alloc_space(space_result
,
9862 bmap1
->n_div
+ bmap2
->n_div
,
9863 bmap1
->n_eq
+ bmap2
->n_eq
,
9864 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9865 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9866 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9867 bmap
= isl_basic_map_simplify(bmap
);
9868 return isl_basic_map_finalize(bmap
);
9870 isl_basic_map_free(bmap1
);
9871 isl_basic_map_free(bmap2
);
9875 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9876 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9878 isl_basic_map
*prod
;
9880 prod
= isl_basic_map_product(bmap1
, bmap2
);
9881 prod
= isl_basic_map_flatten(prod
);
9885 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9886 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9888 return isl_basic_map_flat_range_product(bset1
, bset2
);
9891 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9892 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9894 isl_space
*space_result
= NULL
;
9895 isl_basic_map
*bmap
;
9896 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9897 struct isl_dim_map
*dim_map1
, *dim_map2
;
9899 if (!bmap1
|| !bmap2
)
9902 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9903 isl_space_copy(bmap2
->dim
));
9905 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9906 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9907 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9908 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9910 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9911 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9912 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9913 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9914 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9915 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9916 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9917 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9918 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9919 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9920 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9922 bmap
= isl_basic_map_alloc_space(space_result
,
9923 bmap1
->n_div
+ bmap2
->n_div
,
9924 bmap1
->n_eq
+ bmap2
->n_eq
,
9925 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9926 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9927 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9928 bmap
= isl_basic_map_simplify(bmap
);
9929 return isl_basic_map_finalize(bmap
);
9931 isl_basic_map_free(bmap1
);
9932 isl_basic_map_free(bmap2
);
9936 __isl_give isl_basic_map
*isl_basic_map_range_product(
9937 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9940 isl_space
*space_result
= NULL
;
9941 isl_basic_map
*bmap
;
9942 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9943 struct isl_dim_map
*dim_map1
, *dim_map2
;
9945 rational
= isl_basic_map_is_rational(bmap1
);
9946 if (rational
>= 0 && rational
)
9947 rational
= isl_basic_map_is_rational(bmap2
);
9948 if (!bmap1
|| !bmap2
|| rational
< 0)
9951 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9954 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9955 isl_space_copy(bmap2
->dim
));
9957 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9958 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9959 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9960 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9962 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9963 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9964 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9965 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9966 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9967 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9968 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9969 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9970 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9971 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9972 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9974 bmap
= isl_basic_map_alloc_space(space_result
,
9975 bmap1
->n_div
+ bmap2
->n_div
,
9976 bmap1
->n_eq
+ bmap2
->n_eq
,
9977 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9978 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9979 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9981 bmap
= isl_basic_map_set_rational(bmap
);
9982 bmap
= isl_basic_map_simplify(bmap
);
9983 return isl_basic_map_finalize(bmap
);
9985 isl_basic_map_free(bmap1
);
9986 isl_basic_map_free(bmap2
);
9990 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9991 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9993 isl_basic_map
*prod
;
9995 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9996 prod
= isl_basic_map_flatten_range(prod
);
10000 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10001 * and collect the results.
10002 * The result live in the space obtained by calling "space_product"
10003 * on the spaces of "map1" and "map2".
10004 * If "remove_duplicates" is set then the result may contain duplicates
10005 * (even if the inputs do not) and so we try and remove the obvious
10008 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10009 __isl_take isl_map
*map2
,
10010 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10011 __isl_take isl_space
*right
),
10012 __isl_give isl_basic_map
*(*basic_map_product
)(
10013 __isl_take isl_basic_map
*left
,
10014 __isl_take isl_basic_map
*right
),
10015 int remove_duplicates
)
10017 unsigned flags
= 0;
10018 struct isl_map
*result
;
10022 m
= isl_map_has_equal_params(map1
, map2
);
10026 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10027 "parameters don't match", goto error
);
10029 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10030 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10031 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10033 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10034 isl_space_copy(map2
->dim
)),
10035 map1
->n
* map2
->n
, flags
);
10038 for (i
= 0; i
< map1
->n
; ++i
)
10039 for (j
= 0; j
< map2
->n
; ++j
) {
10040 struct isl_basic_map
*part
;
10041 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10042 isl_basic_map_copy(map2
->p
[j
]));
10043 if (isl_basic_map_is_empty(part
))
10044 isl_basic_map_free(part
);
10046 result
= isl_map_add_basic_map(result
, part
);
10050 if (remove_duplicates
)
10051 result
= isl_map_remove_obvious_duplicates(result
);
10052 isl_map_free(map1
);
10053 isl_map_free(map2
);
10056 isl_map_free(map1
);
10057 isl_map_free(map2
);
10061 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10063 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10064 __isl_take isl_map
*map2
)
10066 return map_product(map1
, map2
, &isl_space_product
,
10067 &isl_basic_map_product
, 0);
10070 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10071 __isl_take isl_map
*map2
)
10073 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10076 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10078 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10079 __isl_take isl_map
*map2
)
10083 prod
= isl_map_product(map1
, map2
);
10084 prod
= isl_map_flatten(prod
);
10088 /* Given two set A and B, construct its Cartesian product A x B.
10090 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10092 return isl_map_range_product(set1
, set2
);
10095 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10096 __isl_take isl_set
*set2
)
10098 return isl_map_flat_range_product(set1
, set2
);
10101 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10103 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10104 __isl_take isl_map
*map2
)
10106 return map_product(map1
, map2
, &isl_space_domain_product
,
10107 &isl_basic_map_domain_product
, 1);
10110 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10112 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10113 __isl_take isl_map
*map2
)
10115 return map_product(map1
, map2
, &isl_space_range_product
,
10116 &isl_basic_map_range_product
, 1);
10119 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10120 __isl_take isl_map
*map2
)
10122 return isl_map_align_params_map_map_and(map1
, map2
,
10123 &map_domain_product_aligned
);
10126 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10127 __isl_take isl_map
*map2
)
10129 return isl_map_align_params_map_map_and(map1
, map2
,
10130 &map_range_product_aligned
);
10133 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10135 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10138 int total1
, keep1
, total2
, keep2
;
10142 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10143 !isl_space_range_is_wrapping(map
->dim
))
10144 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10145 "not a product", return isl_map_free(map
));
10147 space
= isl_map_get_space(map
);
10148 total1
= isl_space_dim(space
, isl_dim_in
);
10149 total2
= isl_space_dim(space
, isl_dim_out
);
10150 space
= isl_space_factor_domain(space
);
10151 keep1
= isl_space_dim(space
, isl_dim_in
);
10152 keep2
= isl_space_dim(space
, isl_dim_out
);
10153 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10154 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10155 map
= isl_map_reset_space(map
, space
);
10160 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10162 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10165 int total1
, keep1
, total2
, keep2
;
10169 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10170 !isl_space_range_is_wrapping(map
->dim
))
10171 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10172 "not a product", return isl_map_free(map
));
10174 space
= isl_map_get_space(map
);
10175 total1
= isl_space_dim(space
, isl_dim_in
);
10176 total2
= isl_space_dim(space
, isl_dim_out
);
10177 space
= isl_space_factor_range(space
);
10178 keep1
= isl_space_dim(space
, isl_dim_in
);
10179 keep2
= isl_space_dim(space
, isl_dim_out
);
10180 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10181 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10182 map
= isl_map_reset_space(map
, space
);
10187 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10189 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10196 if (!isl_space_domain_is_wrapping(map
->dim
))
10197 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10198 "domain is not a product", return isl_map_free(map
));
10200 space
= isl_map_get_space(map
);
10201 total
= isl_space_dim(space
, isl_dim_in
);
10202 space
= isl_space_domain_factor_domain(space
);
10203 keep
= isl_space_dim(space
, isl_dim_in
);
10204 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10205 map
= isl_map_reset_space(map
, space
);
10210 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10212 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10219 if (!isl_space_domain_is_wrapping(map
->dim
))
10220 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10221 "domain is not a product", return isl_map_free(map
));
10223 space
= isl_map_get_space(map
);
10224 total
= isl_space_dim(space
, isl_dim_in
);
10225 space
= isl_space_domain_factor_range(space
);
10226 keep
= isl_space_dim(space
, isl_dim_in
);
10227 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10228 map
= isl_map_reset_space(map
, space
);
10233 /* Given a map A -> [B -> C], extract the map A -> B.
10235 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10242 if (!isl_space_range_is_wrapping(map
->dim
))
10243 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10244 "range is not a product", return isl_map_free(map
));
10246 space
= isl_map_get_space(map
);
10247 total
= isl_space_dim(space
, isl_dim_out
);
10248 space
= isl_space_range_factor_domain(space
);
10249 keep
= isl_space_dim(space
, isl_dim_out
);
10250 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10251 map
= isl_map_reset_space(map
, space
);
10256 /* Given a map A -> [B -> C], extract the map A -> C.
10258 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10265 if (!isl_space_range_is_wrapping(map
->dim
))
10266 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10267 "range is not a product", return isl_map_free(map
));
10269 space
= isl_map_get_space(map
);
10270 total
= isl_space_dim(space
, isl_dim_out
);
10271 space
= isl_space_range_factor_range(space
);
10272 keep
= isl_space_dim(space
, isl_dim_out
);
10273 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10274 map
= isl_map_reset_space(map
, space
);
10279 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10281 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10282 __isl_take isl_map
*map2
)
10286 prod
= isl_map_domain_product(map1
, map2
);
10287 prod
= isl_map_flatten_domain(prod
);
10291 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10293 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10294 __isl_take isl_map
*map2
)
10298 prod
= isl_map_range_product(map1
, map2
);
10299 prod
= isl_map_flatten_range(prod
);
10303 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10306 uint32_t hash
= isl_hash_init();
10311 bmap
= isl_basic_map_copy(bmap
);
10312 bmap
= isl_basic_map_normalize(bmap
);
10315 total
= isl_basic_map_total_dim(bmap
);
10316 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10317 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10319 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10320 isl_hash_hash(hash
, c_hash
);
10322 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10323 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10325 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10326 isl_hash_hash(hash
, c_hash
);
10328 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10329 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10331 if (isl_int_is_zero(bmap
->div
[i
][0]))
10333 isl_hash_byte(hash
, i
& 0xFF);
10334 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10335 isl_hash_hash(hash
, c_hash
);
10337 isl_basic_map_free(bmap
);
10341 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10343 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10346 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10353 map
= isl_map_copy(map
);
10354 map
= isl_map_normalize(map
);
10358 hash
= isl_hash_init();
10359 for (i
= 0; i
< map
->n
; ++i
) {
10360 uint32_t bmap_hash
;
10361 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10362 isl_hash_hash(hash
, bmap_hash
);
10370 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10372 return isl_map_get_hash(set_to_map(set
));
10375 /* Return the number of basic maps in the (current) representation of "map".
10377 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10379 return map
? map
->n
: 0;
10382 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10384 return set
? set
->n
: 0;
10387 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10388 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10393 return isl_stat_error
;
10395 for (i
= 0; i
< map
->n
; ++i
)
10396 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10397 return isl_stat_error
;
10399 return isl_stat_ok
;
10402 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10403 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10408 return isl_stat_error
;
10410 for (i
= 0; i
< set
->n
; ++i
)
10411 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10412 return isl_stat_error
;
10414 return isl_stat_ok
;
10417 /* Return a list of basic sets, the union of which is equal to "set".
10419 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10420 __isl_keep isl_set
*set
)
10423 isl_basic_set_list
*list
;
10428 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10429 for (i
= 0; i
< set
->n
; ++i
) {
10430 isl_basic_set
*bset
;
10432 bset
= isl_basic_set_copy(set
->p
[i
]);
10433 list
= isl_basic_set_list_add(list
, bset
);
10439 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10446 bset
= isl_basic_set_cow(bset
);
10450 space
= isl_basic_set_get_space(bset
);
10451 space
= isl_space_lift(space
, bset
->n_div
);
10454 isl_space_free(bset
->dim
);
10456 bset
->extra
-= bset
->n_div
;
10459 bset
= isl_basic_set_finalize(bset
);
10463 isl_basic_set_free(bset
);
10467 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10473 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10478 set
= isl_set_cow(set
);
10482 n_div
= set
->p
[0]->n_div
;
10483 space
= isl_set_get_space(set
);
10484 space
= isl_space_lift(space
, n_div
);
10487 isl_space_free(set
->dim
);
10490 for (i
= 0; i
< set
->n
; ++i
) {
10491 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10502 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10510 dim
= isl_basic_set_total_dim(bset
);
10511 size
+= bset
->n_eq
* (1 + dim
);
10512 size
+= bset
->n_ineq
* (1 + dim
);
10513 size
+= bset
->n_div
* (2 + dim
);
10518 int isl_set_size(__isl_keep isl_set
*set
)
10526 for (i
= 0; i
< set
->n
; ++i
)
10527 size
+= isl_basic_set_size(set
->p
[i
]);
10532 /* Check if there is any lower bound (if lower == 0) and/or upper
10533 * bound (if upper == 0) on the specified dim.
10535 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10536 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10540 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10541 return isl_bool_error
;
10543 pos
+= isl_basic_map_offset(bmap
, type
);
10545 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10546 if (isl_int_is_zero(bmap
->div
[i
][0]))
10548 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10549 return isl_bool_true
;
10552 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10553 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10554 return isl_bool_true
;
10556 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10557 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10564 return lower
&& upper
;
10567 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10568 enum isl_dim_type type
, unsigned pos
)
10570 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10573 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10574 enum isl_dim_type type
, unsigned pos
)
10576 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10579 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10580 enum isl_dim_type type
, unsigned pos
)
10582 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10585 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10586 enum isl_dim_type type
, unsigned pos
)
10591 return isl_bool_error
;
10593 for (i
= 0; i
< map
->n
; ++i
) {
10595 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10596 if (bounded
< 0 || !bounded
)
10600 return isl_bool_true
;
10603 /* Return true if the specified dim is involved in both an upper bound
10604 * and a lower bound.
10606 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10607 enum isl_dim_type type
, unsigned pos
)
10609 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10612 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10614 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10615 enum isl_dim_type type
, unsigned pos
,
10616 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10617 enum isl_dim_type type
, unsigned pos
))
10622 return isl_bool_error
;
10624 for (i
= 0; i
< map
->n
; ++i
) {
10626 bounded
= fn(map
->p
[i
], type
, pos
);
10627 if (bounded
< 0 || bounded
)
10631 return isl_bool_false
;
10634 /* Return 1 if the specified dim is involved in any lower bound.
10636 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10637 enum isl_dim_type type
, unsigned pos
)
10639 return has_any_bound(set
, type
, pos
,
10640 &isl_basic_map_dim_has_lower_bound
);
10643 /* Return 1 if the specified dim is involved in any upper bound.
10645 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10646 enum isl_dim_type type
, unsigned pos
)
10648 return has_any_bound(set
, type
, pos
,
10649 &isl_basic_map_dim_has_upper_bound
);
10652 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10654 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10655 enum isl_dim_type type
, unsigned pos
,
10656 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10657 enum isl_dim_type type
, unsigned pos
))
10662 return isl_bool_error
;
10664 for (i
= 0; i
< map
->n
; ++i
) {
10666 bounded
= fn(map
->p
[i
], type
, pos
);
10667 if (bounded
< 0 || !bounded
)
10671 return isl_bool_true
;
10674 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10676 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10677 enum isl_dim_type type
, unsigned pos
)
10679 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10682 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10684 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10685 enum isl_dim_type type
, unsigned pos
)
10687 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10690 /* For each of the "n" variables starting at "first", determine
10691 * the sign of the variable and put the results in the first "n"
10692 * elements of the array "signs".
10694 * 1 means that the variable is non-negative
10695 * -1 means that the variable is non-positive
10696 * 0 means the variable attains both positive and negative values.
10698 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10699 unsigned first
, unsigned n
, int *signs
)
10701 isl_vec
*bound
= NULL
;
10702 struct isl_tab
*tab
= NULL
;
10703 struct isl_tab_undo
*snap
;
10706 if (!bset
|| !signs
)
10707 return isl_stat_error
;
10709 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10710 tab
= isl_tab_from_basic_set(bset
, 0);
10711 if (!bound
|| !tab
)
10714 isl_seq_clr(bound
->el
, bound
->size
);
10715 isl_int_set_si(bound
->el
[0], -1);
10717 snap
= isl_tab_snap(tab
);
10718 for (i
= 0; i
< n
; ++i
) {
10721 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10722 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10724 empty
= tab
->empty
;
10725 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10726 if (isl_tab_rollback(tab
, snap
) < 0)
10734 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10735 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10737 empty
= tab
->empty
;
10738 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10739 if (isl_tab_rollback(tab
, snap
) < 0)
10742 signs
[i
] = empty
? -1 : 0;
10746 isl_vec_free(bound
);
10747 return isl_stat_ok
;
10750 isl_vec_free(bound
);
10751 return isl_stat_error
;
10754 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10755 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10757 if (!bset
|| !signs
)
10758 return isl_stat_error
;
10759 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10760 return isl_stat_error
;
10762 first
+= pos(bset
->dim
, type
) - 1;
10763 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10766 /* Is it possible for the integer division "div" to depend (possibly
10767 * indirectly) on any output dimensions?
10769 * If the div is undefined, then we conservatively assume that it
10770 * may depend on them.
10771 * Otherwise, we check if it actually depends on them or on any integer
10772 * divisions that may depend on them.
10774 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10777 unsigned n_out
, o_out
;
10778 unsigned n_div
, o_div
;
10780 if (isl_int_is_zero(bmap
->div
[div
][0]))
10781 return isl_bool_true
;
10783 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10784 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10786 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10787 return isl_bool_true
;
10789 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10790 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10792 for (i
= 0; i
< n_div
; ++i
) {
10793 isl_bool may_involve
;
10795 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10797 may_involve
= div_may_involve_output(bmap
, i
);
10798 if (may_involve
< 0 || may_involve
)
10799 return may_involve
;
10802 return isl_bool_false
;
10805 /* Return the first integer division of "bmap" in the range
10806 * [first, first + n[ that may depend on any output dimensions and
10807 * that has a non-zero coefficient in "c" (where the first coefficient
10808 * in "c" corresponds to integer division "first").
10810 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10811 isl_int
*c
, int first
, int n
)
10818 for (k
= first
; k
< first
+ n
; ++k
) {
10819 isl_bool may_involve
;
10821 if (isl_int_is_zero(c
[k
]))
10823 may_involve
= div_may_involve_output(bmap
, k
);
10824 if (may_involve
< 0)
10833 /* Look for a pair of inequality constraints in "bmap" of the form
10835 * -l + i >= 0 or i >= l
10837 * n + l - i >= 0 or i <= l + n
10839 * with n < "m" and i the output dimension at position "pos".
10840 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10841 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10842 * and earlier output dimensions, as well as integer divisions that do
10843 * not involve any of the output dimensions.
10845 * Return the index of the first inequality constraint or bmap->n_ineq
10846 * if no such pair can be found.
10848 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10849 int pos
, isl_int m
)
10854 unsigned n_div
, o_div
;
10855 unsigned n_out
, o_out
;
10861 ctx
= isl_basic_map_get_ctx(bmap
);
10862 total
= isl_basic_map_total_dim(bmap
);
10863 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10864 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10865 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10866 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10867 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10868 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10870 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10871 n_out
- (pos
+ 1)) != -1)
10873 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10876 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10877 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10880 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10881 bmap
->ineq
[j
] + 1, total
))
10885 if (j
>= bmap
->n_ineq
)
10887 isl_int_add(bmap
->ineq
[i
][0],
10888 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10889 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10890 isl_int_sub(bmap
->ineq
[i
][0],
10891 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10894 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10900 return bmap
->n_ineq
;
10903 /* Return the index of the equality of "bmap" that defines
10904 * the output dimension "pos" in terms of earlier dimensions.
10905 * The equality may also involve integer divisions, as long
10906 * as those integer divisions are defined in terms of
10907 * parameters or input dimensions.
10908 * In this case, *div is set to the number of integer divisions and
10909 * *ineq is set to the number of inequality constraints (provided
10910 * div and ineq are not NULL).
10912 * The equality may also involve a single integer division involving
10913 * the output dimensions (typically only output dimension "pos") as
10914 * long as the coefficient of output dimension "pos" is 1 or -1 and
10915 * there is a pair of constraints i >= l and i <= l + n, with i referring
10916 * to output dimension "pos", l an expression involving only earlier
10917 * dimensions and n smaller than the coefficient of the integer division
10918 * in the equality. In this case, the output dimension can be defined
10919 * in terms of a modulo expression that does not involve the integer division.
10920 * *div is then set to this single integer division and
10921 * *ineq is set to the index of constraint i >= l.
10923 * Return bmap->n_eq if there is no such equality.
10924 * Return -1 on error.
10926 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10927 int pos
, int *div
, int *ineq
)
10930 unsigned n_out
, o_out
;
10931 unsigned n_div
, o_div
;
10936 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10937 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10938 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10939 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10942 *ineq
= bmap
->n_ineq
;
10945 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10946 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10948 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10949 n_out
- (pos
+ 1)) != -1)
10951 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10955 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10956 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10958 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10959 k
+ 1, n_div
- (k
+1)) < n_div
)
10961 l
= find_modulo_constraint_pair(bmap
, pos
,
10962 bmap
->eq
[j
][o_div
+ k
]);
10965 if (l
>= bmap
->n_ineq
)
10977 /* Check if the given basic map is obviously single-valued.
10978 * In particular, for each output dimension, check that there is
10979 * an equality that defines the output dimension in terms of
10980 * earlier dimensions.
10982 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10988 return isl_bool_error
;
10990 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10992 for (i
= 0; i
< n_out
; ++i
) {
10995 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10998 return isl_bool_error
;
10999 if (eq
>= bmap
->n_eq
)
11000 return isl_bool_false
;
11003 return isl_bool_true
;
11006 /* Check if the given basic map is single-valued.
11007 * We simply compute
11011 * and check if the result is a subset of the identity mapping.
11013 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11016 isl_basic_map
*test
;
11020 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11024 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11025 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11027 space
= isl_basic_map_get_space(bmap
);
11028 space
= isl_space_map_from_set(isl_space_range(space
));
11029 id
= isl_basic_map_identity(space
);
11031 sv
= isl_basic_map_is_subset(test
, id
);
11033 isl_basic_map_free(test
);
11034 isl_basic_map_free(id
);
11039 /* Check if the given map is obviously single-valued.
11041 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11044 return isl_bool_error
;
11046 return isl_bool_true
;
11048 return isl_bool_false
;
11050 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11053 /* Check if the given map is single-valued.
11054 * We simply compute
11058 * and check if the result is a subset of the identity mapping.
11060 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11067 sv
= isl_map_plain_is_single_valued(map
);
11071 test
= isl_map_reverse(isl_map_copy(map
));
11072 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11074 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11075 id
= isl_map_identity(dim
);
11077 sv
= isl_map_is_subset(test
, id
);
11079 isl_map_free(test
);
11085 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11089 map
= isl_map_copy(map
);
11090 map
= isl_map_reverse(map
);
11091 in
= isl_map_is_single_valued(map
);
11097 /* Check if the given map is obviously injective.
11099 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11103 map
= isl_map_copy(map
);
11104 map
= isl_map_reverse(map
);
11105 in
= isl_map_plain_is_single_valued(map
);
11111 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11115 sv
= isl_map_is_single_valued(map
);
11119 return isl_map_is_injective(map
);
11122 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11124 return isl_map_is_single_valued(set_to_map(set
));
11127 /* Does "map" only map elements to themselves?
11129 * If the domain and range spaces are different, then "map"
11130 * is considered not to be an identity relation, even if it is empty.
11131 * Otherwise, construct the maximal identity relation and
11132 * check whether "map" is a subset of this relation.
11134 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11138 isl_bool equal
, is_identity
;
11140 space
= isl_map_get_space(map
);
11141 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11142 isl_space_free(space
);
11143 if (equal
< 0 || !equal
)
11146 id
= isl_map_identity(isl_map_get_space(map
));
11147 is_identity
= isl_map_is_subset(map
, id
);
11150 return is_identity
;
11153 int isl_map_is_translation(__isl_keep isl_map
*map
)
11158 delta
= isl_map_deltas(isl_map_copy(map
));
11159 ok
= isl_set_is_singleton(delta
);
11160 isl_set_free(delta
);
11165 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11167 if (isl_seq_first_non_zero(p
, pos
) != -1)
11169 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11174 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11181 return isl_bool_error
;
11183 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11184 return isl_bool_false
;
11186 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11187 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11188 for (j
= 0; j
< nvar
; ++j
) {
11189 int lower
= 0, upper
= 0;
11190 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11191 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11193 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11194 return isl_bool_false
;
11197 if (i
< bset
->n_eq
)
11199 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11200 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11202 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11203 return isl_bool_false
;
11204 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11209 if (!lower
|| !upper
)
11210 return isl_bool_false
;
11213 return isl_bool_true
;
11216 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11219 return isl_bool_error
;
11221 return isl_bool_false
;
11223 return isl_basic_set_is_box(set
->p
[0]);
11226 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11229 return isl_bool_error
;
11231 return isl_space_is_wrapping(bset
->dim
);
11234 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11237 return isl_bool_error
;
11239 return isl_space_is_wrapping(set
->dim
);
11242 /* Modify the space of "map" through a call to "change".
11243 * If "can_change" is set (not NULL), then first call it to check
11244 * if the modification is allowed, printing the error message "cannot_change"
11247 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11248 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11249 const char *cannot_change
,
11250 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11258 ok
= can_change
? can_change(map
) : isl_bool_true
;
11260 return isl_map_free(map
);
11262 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11263 return isl_map_free(map
));
11265 space
= change(isl_map_get_space(map
));
11266 map
= isl_map_reset_space(map
, space
);
11271 /* Is the domain of "map" a wrapped relation?
11273 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11276 return isl_bool_error
;
11278 return isl_space_domain_is_wrapping(map
->dim
);
11281 /* Does "map" have a wrapped relation in both domain and range?
11283 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11285 return isl_space_is_product(isl_map_peek_space(map
));
11288 /* Is the range of "map" a wrapped relation?
11290 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11293 return isl_bool_error
;
11295 return isl_space_range_is_wrapping(map
->dim
);
11298 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11300 bmap
= isl_basic_map_cow(bmap
);
11304 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11308 bmap
= isl_basic_map_finalize(bmap
);
11310 return bset_from_bmap(bmap
);
11312 isl_basic_map_free(bmap
);
11316 /* Given a map A -> B, return the set (A -> B).
11318 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11320 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11323 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11325 bset
= isl_basic_set_cow(bset
);
11329 bset
->dim
= isl_space_unwrap(bset
->dim
);
11333 bset
= isl_basic_set_finalize(bset
);
11335 return bset_to_bmap(bset
);
11337 isl_basic_set_free(bset
);
11341 /* Given a set (A -> B), return the map A -> B.
11342 * Error out if "set" is not of the form (A -> B).
11344 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11346 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11347 "not a wrapping set", &isl_space_unwrap
);
11350 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11351 enum isl_dim_type type
)
11356 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11359 bmap
= isl_basic_map_cow(bmap
);
11363 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11367 bmap
= isl_basic_map_finalize(bmap
);
11371 isl_basic_map_free(bmap
);
11375 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11376 enum isl_dim_type type
)
11383 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11386 map
= isl_map_cow(map
);
11390 for (i
= 0; i
< map
->n
; ++i
) {
11391 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11395 map
->dim
= isl_space_reset(map
->dim
, type
);
11405 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11410 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11413 bmap
= isl_basic_map_cow(bmap
);
11417 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11421 bmap
= isl_basic_map_finalize(bmap
);
11425 isl_basic_map_free(bmap
);
11429 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11431 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11434 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11435 __isl_take isl_basic_map
*bmap
)
11440 if (!bmap
->dim
->nested
[0])
11443 bmap
= isl_basic_map_cow(bmap
);
11447 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11451 bmap
= isl_basic_map_finalize(bmap
);
11455 isl_basic_map_free(bmap
);
11459 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11460 __isl_take isl_basic_map
*bmap
)
11465 if (!bmap
->dim
->nested
[1])
11468 bmap
= isl_basic_map_cow(bmap
);
11472 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11476 bmap
= isl_basic_map_finalize(bmap
);
11480 isl_basic_map_free(bmap
);
11484 /* Remove any internal structure from the spaces of domain and range of "map".
11486 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11491 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11494 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11497 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11499 return set_from_map(isl_map_flatten(set_to_map(set
)));
11502 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11504 isl_space
*space
, *flat_space
;
11507 space
= isl_set_get_space(set
);
11508 flat_space
= isl_space_flatten(isl_space_copy(space
));
11509 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11511 map
= isl_map_intersect_domain(map
, set
);
11516 /* Remove any internal structure from the space of the domain of "map".
11518 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11523 if (!map
->dim
->nested
[0])
11526 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11529 /* Remove any internal structure from the space of the range of "map".
11531 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11536 if (!map
->dim
->nested
[1])
11539 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11542 /* Reorder the dimensions of "bmap" according to the given dim_map
11543 * and set the dimension specification to "space" and
11544 * perform Gaussian elimination on the result.
11546 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11547 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11549 isl_basic_map
*res
;
11553 if (!bmap
|| !space
|| !dim_map
)
11556 flags
= bmap
->flags
;
11557 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11558 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11559 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11560 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11561 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11562 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11564 res
->flags
= flags
;
11565 res
= isl_basic_map_gauss(res
, NULL
);
11566 res
= isl_basic_map_finalize(res
);
11569 isl_dim_map_free(dim_map
);
11570 isl_basic_map_free(bmap
);
11571 isl_space_free(space
);
11575 /* Reorder the dimensions of "map" according to given reordering.
11577 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11578 __isl_take isl_reordering
*r
)
11581 struct isl_dim_map
*dim_map
;
11583 map
= isl_map_cow(map
);
11584 dim_map
= isl_dim_map_from_reordering(r
);
11585 if (!map
|| !r
|| !dim_map
)
11588 for (i
= 0; i
< map
->n
; ++i
) {
11589 struct isl_dim_map
*dim_map_i
;
11592 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11594 space
= isl_reordering_get_space(r
);
11595 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11601 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11602 map
= isl_map_unmark_normalized(map
);
11604 isl_reordering_free(r
);
11605 isl_dim_map_free(dim_map
);
11608 isl_dim_map_free(dim_map
);
11610 isl_reordering_free(r
);
11614 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11615 __isl_take isl_reordering
*r
)
11617 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11620 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11621 __isl_take isl_space
*model
)
11626 if (!map
|| !model
)
11629 ctx
= isl_space_get_ctx(model
);
11630 if (!isl_space_has_named_params(model
))
11631 isl_die(ctx
, isl_error_invalid
,
11632 "model has unnamed parameters", goto error
);
11633 if (isl_map_check_named_params(map
) < 0)
11635 aligned
= isl_map_space_has_equal_params(map
, model
);
11639 isl_reordering
*exp
;
11641 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11642 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11643 map
= isl_map_realign(map
, exp
);
11646 isl_space_free(model
);
11649 isl_space_free(model
);
11654 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11655 __isl_take isl_space
*model
)
11657 return isl_map_align_params(set
, model
);
11660 /* Align the parameters of "bmap" to those of "model", introducing
11661 * additional parameters if needed.
11663 __isl_give isl_basic_map
*isl_basic_map_align_params(
11664 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11667 isl_bool equal_params
;
11669 if (!bmap
|| !model
)
11672 ctx
= isl_space_get_ctx(model
);
11673 if (!isl_space_has_named_params(model
))
11674 isl_die(ctx
, isl_error_invalid
,
11675 "model has unnamed parameters", goto error
);
11676 if (isl_basic_map_check_named_params(bmap
) < 0)
11678 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11679 if (equal_params
< 0)
11681 if (!equal_params
) {
11682 isl_reordering
*exp
;
11683 struct isl_dim_map
*dim_map
;
11685 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11686 exp
= isl_reordering_extend_space(exp
,
11687 isl_basic_map_get_space(bmap
));
11688 dim_map
= isl_dim_map_from_reordering(exp
);
11689 bmap
= isl_basic_map_realign(bmap
,
11690 isl_reordering_get_space(exp
),
11691 isl_dim_map_extend(dim_map
, bmap
));
11692 isl_reordering_free(exp
);
11693 isl_dim_map_free(dim_map
);
11696 isl_space_free(model
);
11699 isl_space_free(model
);
11700 isl_basic_map_free(bmap
);
11704 /* Do "bset" and "space" have the same parameters?
11706 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11707 __isl_keep isl_space
*space
)
11709 isl_space
*bset_space
;
11711 bset_space
= isl_basic_set_peek_space(bset
);
11712 return isl_space_has_equal_params(bset_space
, space
);
11715 /* Do "map" and "space" have the same parameters?
11717 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11718 __isl_keep isl_space
*space
)
11720 isl_space
*map_space
;
11722 map_space
= isl_map_peek_space(map
);
11723 return isl_space_has_equal_params(map_space
, space
);
11726 /* Do "set" and "space" have the same parameters?
11728 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11729 __isl_keep isl_space
*space
)
11731 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11734 /* Align the parameters of "bset" to those of "model", introducing
11735 * additional parameters if needed.
11737 __isl_give isl_basic_set
*isl_basic_set_align_params(
11738 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11740 return isl_basic_map_align_params(bset
, model
);
11743 /* Drop all parameters not referenced by "map".
11745 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11749 if (isl_map_check_named_params(map
) < 0)
11750 return isl_map_free(map
);
11752 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11755 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11757 return isl_map_free(map
);
11759 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11765 /* Drop all parameters not referenced by "set".
11767 __isl_give isl_set
*isl_set_drop_unused_params(
11768 __isl_take isl_set
*set
)
11770 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11773 /* Drop all parameters not referenced by "bmap".
11775 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11776 __isl_take isl_basic_map
*bmap
)
11780 if (isl_basic_map_check_named_params(bmap
) < 0)
11781 return isl_basic_map_free(bmap
);
11783 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11786 involves
= isl_basic_map_involves_dims(bmap
,
11787 isl_dim_param
, i
, 1);
11789 return isl_basic_map_free(bmap
);
11791 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11797 /* Drop all parameters not referenced by "bset".
11799 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11800 __isl_take isl_basic_set
*bset
)
11802 return bset_from_bmap(isl_basic_map_drop_unused_params(
11803 bset_to_bmap(bset
)));
11806 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11807 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11808 enum isl_dim_type c2
, enum isl_dim_type c3
,
11809 enum isl_dim_type c4
, enum isl_dim_type c5
)
11811 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11812 struct isl_mat
*mat
;
11818 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11819 isl_basic_map_total_dim(bmap
) + 1);
11822 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11823 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11824 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11825 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11826 isl_int_set(mat
->row
[i
][pos
],
11827 bmap
->eq
[i
][off
+ k
]);
11835 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11836 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11837 enum isl_dim_type c2
, enum isl_dim_type c3
,
11838 enum isl_dim_type c4
, enum isl_dim_type c5
)
11840 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11841 struct isl_mat
*mat
;
11847 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11848 isl_basic_map_total_dim(bmap
) + 1);
11851 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11852 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11853 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11854 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11855 isl_int_set(mat
->row
[i
][pos
],
11856 bmap
->ineq
[i
][off
+ k
]);
11864 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11865 __isl_take isl_space
*space
,
11866 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11867 enum isl_dim_type c2
, enum isl_dim_type c3
,
11868 enum isl_dim_type c4
, enum isl_dim_type c5
)
11870 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11871 isl_basic_map
*bmap
= NULL
;
11877 if (!space
|| !eq
|| !ineq
)
11880 if (eq
->n_col
!= ineq
->n_col
)
11881 isl_die(space
->ctx
, isl_error_invalid
,
11882 "equalities and inequalities matrices should have "
11883 "same number of columns", goto error
);
11885 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11887 if (eq
->n_col
< total
)
11888 isl_die(space
->ctx
, isl_error_invalid
,
11889 "number of columns too small", goto error
);
11891 extra
= eq
->n_col
- total
;
11893 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11894 eq
->n_row
, ineq
->n_row
);
11897 for (i
= 0; i
< extra
; ++i
) {
11898 k
= isl_basic_map_alloc_div(bmap
);
11901 isl_int_set_si(bmap
->div
[k
][0], 0);
11903 for (i
= 0; i
< eq
->n_row
; ++i
) {
11904 l
= isl_basic_map_alloc_equality(bmap
);
11907 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11908 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11909 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11910 isl_int_set(bmap
->eq
[l
][off
+ k
],
11916 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11917 l
= isl_basic_map_alloc_inequality(bmap
);
11920 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11921 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11922 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11923 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11924 ineq
->row
[i
][pos
]);
11930 isl_space_free(space
);
11932 isl_mat_free(ineq
);
11934 bmap
= isl_basic_map_simplify(bmap
);
11935 return isl_basic_map_finalize(bmap
);
11937 isl_space_free(space
);
11939 isl_mat_free(ineq
);
11940 isl_basic_map_free(bmap
);
11944 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11945 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11946 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11948 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11949 c1
, c2
, c3
, c4
, isl_dim_in
);
11952 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11953 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11954 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11956 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11957 c1
, c2
, c3
, c4
, isl_dim_in
);
11960 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11961 __isl_take isl_space
*dim
,
11962 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11963 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11965 isl_basic_map
*bmap
;
11966 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11967 c1
, c2
, c3
, c4
, isl_dim_in
);
11968 return bset_from_bmap(bmap
);
11971 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11974 return isl_bool_error
;
11976 return isl_space_can_zip(bmap
->dim
);
11979 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11982 return isl_bool_error
;
11984 return isl_space_can_zip(map
->dim
);
11987 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11988 * (A -> C) -> (B -> D).
11990 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11999 if (!isl_basic_map_can_zip(bmap
))
12000 isl_die(bmap
->ctx
, isl_error_invalid
,
12001 "basic map cannot be zipped", goto error
);
12002 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
12003 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12004 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12005 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12006 bmap
= isl_basic_map_cow(bmap
);
12007 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12010 bmap
->dim
= isl_space_zip(bmap
->dim
);
12013 bmap
= isl_basic_map_mark_final(bmap
);
12016 isl_basic_map_free(bmap
);
12020 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12021 * (A -> C) -> (B -> D).
12023 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12030 if (!isl_map_can_zip(map
))
12031 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12034 map
= isl_map_cow(map
);
12038 for (i
= 0; i
< map
->n
; ++i
) {
12039 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12044 map
->dim
= isl_space_zip(map
->dim
);
12054 /* Can we apply isl_basic_map_curry to "bmap"?
12055 * That is, does it have a nested relation in its domain?
12057 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12060 return isl_bool_error
;
12062 return isl_space_can_curry(bmap
->dim
);
12065 /* Can we apply isl_map_curry to "map"?
12066 * That is, does it have a nested relation in its domain?
12068 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12071 return isl_bool_error
;
12073 return isl_space_can_curry(map
->dim
);
12076 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12079 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12085 if (!isl_basic_map_can_curry(bmap
))
12086 isl_die(bmap
->ctx
, isl_error_invalid
,
12087 "basic map cannot be curried", goto error
);
12088 bmap
= isl_basic_map_cow(bmap
);
12091 bmap
->dim
= isl_space_curry(bmap
->dim
);
12094 bmap
= isl_basic_map_mark_final(bmap
);
12097 isl_basic_map_free(bmap
);
12101 /* Given a map (A -> B) -> C, return the corresponding map
12104 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12106 return isl_map_change_space(map
, &isl_map_can_curry
,
12107 "map cannot be curried", &isl_space_curry
);
12110 /* Can isl_map_range_curry be applied to "map"?
12111 * That is, does it have a nested relation in its range,
12112 * the domain of which is itself a nested relation?
12114 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12117 return isl_bool_error
;
12119 return isl_space_can_range_curry(map
->dim
);
12122 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12123 * A -> (B -> (C -> D)).
12125 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12127 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12128 "map range cannot be curried",
12129 &isl_space_range_curry
);
12132 /* Can we apply isl_basic_map_uncurry to "bmap"?
12133 * That is, does it have a nested relation in its domain?
12135 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12138 return isl_bool_error
;
12140 return isl_space_can_uncurry(bmap
->dim
);
12143 /* Can we apply isl_map_uncurry to "map"?
12144 * That is, does it have a nested relation in its domain?
12146 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12149 return isl_bool_error
;
12151 return isl_space_can_uncurry(map
->dim
);
12154 /* Given a basic map A -> (B -> C), return the corresponding basic map
12157 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12163 if (!isl_basic_map_can_uncurry(bmap
))
12164 isl_die(bmap
->ctx
, isl_error_invalid
,
12165 "basic map cannot be uncurried",
12166 return isl_basic_map_free(bmap
));
12167 bmap
= isl_basic_map_cow(bmap
);
12170 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12172 return isl_basic_map_free(bmap
);
12173 bmap
= isl_basic_map_mark_final(bmap
);
12177 /* Given a map A -> (B -> C), return the corresponding map
12180 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12182 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12183 "map cannot be uncurried", &isl_space_uncurry
);
12186 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12187 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12189 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12192 /* Construct a basic map where the given dimensions are equal to each other.
12194 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12195 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12197 isl_basic_map
*bmap
= NULL
;
12200 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12201 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12204 if (type1
== type2
&& pos1
== pos2
)
12205 return isl_basic_map_universe(space
);
12207 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12208 i
= isl_basic_map_alloc_equality(bmap
);
12211 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12212 pos1
+= isl_basic_map_offset(bmap
, type1
);
12213 pos2
+= isl_basic_map_offset(bmap
, type2
);
12214 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12215 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12216 bmap
= isl_basic_map_finalize(bmap
);
12217 isl_space_free(space
);
12220 isl_space_free(space
);
12221 isl_basic_map_free(bmap
);
12225 /* Add a constraint imposing that the given two dimensions are equal.
12227 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12228 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12232 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12234 bmap
= isl_basic_map_intersect(bmap
, eq
);
12239 /* Add a constraint imposing that the given two dimensions are equal.
12241 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12242 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12244 isl_basic_map
*bmap
;
12246 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12248 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12253 /* Add a constraint imposing that the given two dimensions have opposite values.
12255 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12256 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12258 isl_basic_map
*bmap
= NULL
;
12261 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12262 return isl_map_free(map
);
12263 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12264 return isl_map_free(map
);
12266 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12267 i
= isl_basic_map_alloc_equality(bmap
);
12270 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12271 pos1
+= isl_basic_map_offset(bmap
, type1
);
12272 pos2
+= isl_basic_map_offset(bmap
, type2
);
12273 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12274 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12275 bmap
= isl_basic_map_finalize(bmap
);
12277 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12281 isl_basic_map_free(bmap
);
12286 /* Construct a constraint imposing that the value of the first dimension is
12287 * greater than or equal to that of the second.
12289 static __isl_give isl_constraint
*constraint_order_ge(
12290 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12291 enum isl_dim_type type2
, int pos2
)
12295 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12296 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12297 space
= isl_space_free(space
);
12301 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12303 if (type1
== type2
&& pos1
== pos2
)
12306 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12307 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12312 /* Add a constraint imposing that the value of the first dimension is
12313 * greater than or equal to that of the second.
12315 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12316 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12321 if (type1
== type2
&& pos1
== pos2
)
12323 space
= isl_basic_map_get_space(bmap
);
12324 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12325 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12330 /* Add a constraint imposing that the value of the first dimension is
12331 * greater than or equal to that of the second.
12333 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12334 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12339 if (type1
== type2
&& pos1
== pos2
)
12341 space
= isl_map_get_space(map
);
12342 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12343 map
= isl_map_add_constraint(map
, c
);
12348 /* Add a constraint imposing that the value of the first dimension is
12349 * less than or equal to that of the second.
12351 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12352 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12354 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12357 /* Construct a basic map where the value of the first dimension is
12358 * greater than that of the second.
12360 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12361 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12363 isl_basic_map
*bmap
= NULL
;
12366 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12367 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12370 if (type1
== type2
&& pos1
== pos2
)
12371 return isl_basic_map_empty(space
);
12373 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12374 i
= isl_basic_map_alloc_inequality(bmap
);
12376 return isl_basic_map_free(bmap
);
12377 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12378 pos1
+= isl_basic_map_offset(bmap
, type1
);
12379 pos2
+= isl_basic_map_offset(bmap
, type2
);
12380 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12381 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12382 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12383 bmap
= isl_basic_map_finalize(bmap
);
12387 isl_space_free(space
);
12388 isl_basic_map_free(bmap
);
12392 /* Add a constraint imposing that the value of the first dimension is
12393 * greater than that of the second.
12395 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12396 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12400 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12402 bmap
= isl_basic_map_intersect(bmap
, gt
);
12407 /* Add a constraint imposing that the value of the first dimension is
12408 * greater than that of the second.
12410 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12411 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12413 isl_basic_map
*bmap
;
12415 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12417 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12422 /* Add a constraint imposing that the value of the first dimension is
12423 * smaller than that of the second.
12425 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12426 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12428 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12431 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12435 isl_local_space
*ls
;
12440 if (!isl_basic_map_divs_known(bmap
))
12441 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12442 "some divs are unknown", return NULL
);
12444 ls
= isl_basic_map_get_local_space(bmap
);
12445 div
= isl_local_space_get_div(ls
, pos
);
12446 isl_local_space_free(ls
);
12451 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12454 return isl_basic_map_get_div(bset
, pos
);
12457 /* Plug in "subs" for dimension "type", "pos" of "bset".
12459 * Let i be the dimension to replace and let "subs" be of the form
12463 * Any integer division with a non-zero coefficient for i,
12465 * floor((a i + g)/m)
12469 * floor((a f + d g)/(m d))
12471 * Constraints of the form
12479 * We currently require that "subs" is an integral expression.
12480 * Handling rational expressions may require us to add stride constraints
12481 * as we do in isl_basic_set_preimage_multi_aff.
12483 __isl_give isl_basic_set
*isl_basic_set_substitute(
12484 __isl_take isl_basic_set
*bset
,
12485 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12491 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12494 bset
= isl_basic_set_cow(bset
);
12495 if (!bset
|| !subs
)
12498 ctx
= isl_basic_set_get_ctx(bset
);
12499 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12500 isl_die(ctx
, isl_error_invalid
,
12501 "spaces don't match", goto error
);
12502 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12503 isl_die(ctx
, isl_error_unsupported
,
12504 "cannot handle divs yet", goto error
);
12505 if (!isl_int_is_one(subs
->v
->el
[0]))
12506 isl_die(ctx
, isl_error_invalid
,
12507 "can only substitute integer expressions", goto error
);
12509 pos
+= isl_basic_set_offset(bset
, type
);
12513 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12514 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12516 isl_int_set(v
, bset
->eq
[i
][pos
]);
12517 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12518 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12519 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12522 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12523 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12525 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12526 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12527 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12528 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12531 for (i
= 0; i
< bset
->n_div
; ++i
) {
12532 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12534 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12535 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12536 isl_seq_combine(bset
->div
[i
] + 1,
12537 subs
->v
->el
[0], bset
->div
[i
] + 1,
12538 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12539 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12544 bset
= isl_basic_set_simplify(bset
);
12545 return isl_basic_set_finalize(bset
);
12547 isl_basic_set_free(bset
);
12551 /* Plug in "subs" for dimension "type", "pos" of "set".
12553 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12554 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12558 if (set
&& isl_set_plain_is_empty(set
))
12561 set
= isl_set_cow(set
);
12565 for (i
= set
->n
- 1; i
>= 0; --i
) {
12566 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12567 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12578 /* Check if the range of "ma" is compatible with the domain or range
12579 * (depending on "type") of "bmap".
12581 static isl_stat
check_basic_map_compatible_range_multi_aff(
12582 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12583 __isl_keep isl_multi_aff
*ma
)
12586 isl_space
*ma_space
;
12588 ma_space
= isl_multi_aff_get_space(ma
);
12590 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12594 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12595 "parameters don't match", goto error
);
12596 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12600 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12601 "spaces don't match", goto error
);
12603 isl_space_free(ma_space
);
12604 return isl_stat_ok
;
12606 isl_space_free(ma_space
);
12607 return isl_stat_error
;
12610 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12611 * coefficients before the transformed range of dimensions,
12612 * the "n_after" coefficients after the transformed range of dimensions
12613 * and the coefficients of the other divs in "bmap".
12615 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12616 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12621 isl_local_space
*ls
;
12626 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12628 return isl_basic_map_free(bmap
);
12630 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12631 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12632 for (i
= 0; i
< n_div
; ++i
) {
12633 int o_bmap
= 0, o_ls
= 0;
12635 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12636 o_bmap
+= 1 + 1 + n_param
;
12637 o_ls
+= 1 + 1 + n_param
;
12638 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12639 o_bmap
+= n_before
;
12640 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12641 ls
->div
->row
[i
] + o_ls
, n_set
);
12644 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12646 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12647 ls
->div
->row
[i
] + o_ls
, n_div
);
12650 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12651 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12656 isl_local_space_free(ls
);
12659 isl_local_space_free(ls
);
12660 return isl_basic_map_free(bmap
);
12663 /* How many stride constraints does "ma" enforce?
12664 * That is, how many of the affine expressions have a denominator
12665 * different from one?
12667 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12672 for (i
= 0; i
< ma
->n
; ++i
)
12673 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12679 /* For each affine expression in ma of the form
12681 * x_i = (f_i y + h_i)/m_i
12683 * with m_i different from one, add a constraint to "bmap"
12686 * f_i y + h_i = m_i alpha_i
12688 * with alpha_i an additional existentially quantified variable.
12690 * The input variables of "ma" correspond to a subset of the variables
12691 * of "bmap". There are "n_before" variables in "bmap" before this
12692 * subset and "n_after" variables after this subset.
12693 * The integer divisions of the affine expressions in "ma" are assumed
12694 * to have been aligned. There are "n_div_ma" of them and
12695 * they appear first in "bmap", straight after the "n_after" variables.
12697 static __isl_give isl_basic_map
*add_ma_strides(
12698 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12699 int n_before
, int n_after
, int n_div_ma
)
12707 total
= isl_basic_map_total_dim(bmap
);
12708 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12709 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12710 for (i
= 0; i
< ma
->n
; ++i
) {
12711 int o_bmap
= 0, o_ma
= 1;
12713 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12715 div
= isl_basic_map_alloc_div(bmap
);
12716 k
= isl_basic_map_alloc_equality(bmap
);
12717 if (div
< 0 || k
< 0)
12719 isl_int_set_si(bmap
->div
[div
][0], 0);
12720 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12721 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12722 o_bmap
+= 1 + n_param
;
12723 o_ma
+= 1 + n_param
;
12724 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12725 o_bmap
+= n_before
;
12726 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12727 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12730 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12732 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12733 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12734 o_bmap
+= n_div_ma
;
12736 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12737 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12743 isl_basic_map_free(bmap
);
12747 /* Replace the domain or range space (depending on "type) of "space" by "set".
12749 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12750 enum isl_dim_type type
, __isl_take isl_space
*set
)
12752 if (type
== isl_dim_in
) {
12753 space
= isl_space_range(space
);
12754 space
= isl_space_map_from_domain_and_range(set
, space
);
12756 space
= isl_space_domain(space
);
12757 space
= isl_space_map_from_domain_and_range(space
, set
);
12763 /* Compute the preimage of the domain or range (depending on "type")
12764 * of "bmap" under the function represented by "ma".
12765 * In other words, plug in "ma" in the domain or range of "bmap".
12766 * The result is a basic map that lives in the same space as "bmap"
12767 * except that the domain or range has been replaced by
12768 * the domain space of "ma".
12770 * If bmap is represented by
12772 * A(p) + S u + B x + T v + C(divs) >= 0,
12774 * where u and x are input and output dimensions if type == isl_dim_out
12775 * while x and v are input and output dimensions if type == isl_dim_in,
12776 * and ma is represented by
12778 * x = D(p) + F(y) + G(divs')
12780 * then the result is
12782 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12784 * The divs in the input set are similarly adjusted.
12787 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12791 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12792 * B_i G(divs') + c_i(divs))/n_i)
12794 * If bmap is not a rational map and if F(y) involves any denominators
12796 * x_i = (f_i y + h_i)/m_i
12798 * then additional constraints are added to ensure that we only
12799 * map back integer points. That is we enforce
12801 * f_i y + h_i = m_i alpha_i
12803 * with alpha_i an additional existentially quantified variable.
12805 * We first copy over the divs from "ma".
12806 * Then we add the modified constraints and divs from "bmap".
12807 * Finally, we add the stride constraints, if needed.
12809 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12810 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12811 __isl_take isl_multi_aff
*ma
)
12815 isl_basic_map
*res
= NULL
;
12816 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12817 isl_int f
, c1
, c2
, g
;
12826 ma
= isl_multi_aff_align_divs(ma
);
12829 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12832 if (type
== isl_dim_in
) {
12834 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12836 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12839 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12840 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12842 space
= isl_multi_aff_get_domain_space(ma
);
12843 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12844 rational
= isl_basic_map_is_rational(bmap
);
12845 strides
= rational
? 0 : multi_aff_strides(ma
);
12846 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12847 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12849 res
= isl_basic_map_set_rational(res
);
12851 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12852 if (isl_basic_map_alloc_div(res
) < 0)
12855 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
12859 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12860 k
= isl_basic_map_alloc_equality(res
);
12863 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12864 n_after
, n_div_ma
, n_div_bmap
,
12865 f
, c1
, c2
, g
, 0) < 0)
12869 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12870 k
= isl_basic_map_alloc_inequality(res
);
12873 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12874 n_after
, n_div_ma
, n_div_bmap
,
12875 f
, c1
, c2
, g
, 0) < 0)
12879 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12880 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12881 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12884 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12885 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12886 f
, c1
, c2
, g
, 1) < 0)
12891 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12897 isl_basic_map_free(bmap
);
12898 isl_multi_aff_free(ma
);
12899 res
= isl_basic_map_simplify(res
);
12900 return isl_basic_map_finalize(res
);
12906 isl_basic_map_free(bmap
);
12907 isl_multi_aff_free(ma
);
12908 isl_basic_map_free(res
);
12912 /* Compute the preimage of "bset" under the function represented by "ma".
12913 * In other words, plug in "ma" in "bset". The result is a basic set
12914 * that lives in the domain space of "ma".
12916 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12917 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12919 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12922 /* Compute the preimage of the domain of "bmap" under the function
12923 * represented by "ma".
12924 * In other words, plug in "ma" in the domain of "bmap".
12925 * The result is a basic map that lives in the same space as "bmap"
12926 * except that the domain has been replaced by the domain space of "ma".
12928 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12929 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12931 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12934 /* Compute the preimage of the range of "bmap" under the function
12935 * represented by "ma".
12936 * In other words, plug in "ma" in the range of "bmap".
12937 * The result is a basic map that lives in the same space as "bmap"
12938 * except that the range has been replaced by the domain space of "ma".
12940 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12941 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12943 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12946 /* Check if the range of "ma" is compatible with the domain or range
12947 * (depending on "type") of "map".
12948 * Return isl_stat_error if anything is wrong.
12950 static isl_stat
check_map_compatible_range_multi_aff(
12951 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12952 __isl_keep isl_multi_aff
*ma
)
12955 isl_space
*ma_space
;
12957 ma_space
= isl_multi_aff_get_space(ma
);
12958 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12959 isl_space_free(ma_space
);
12961 return isl_stat_error
;
12963 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12964 "spaces don't match", return isl_stat_error
);
12965 return isl_stat_ok
;
12968 /* Compute the preimage of the domain or range (depending on "type")
12969 * of "map" under the function represented by "ma".
12970 * In other words, plug in "ma" in the domain or range of "map".
12971 * The result is a map that lives in the same space as "map"
12972 * except that the domain or range has been replaced by
12973 * the domain space of "ma".
12975 * The parameters are assumed to have been aligned.
12977 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12978 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12983 map
= isl_map_cow(map
);
12984 ma
= isl_multi_aff_align_divs(ma
);
12987 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12990 for (i
= 0; i
< map
->n
; ++i
) {
12991 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12992 isl_multi_aff_copy(ma
));
12997 space
= isl_multi_aff_get_domain_space(ma
);
12998 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13000 isl_space_free(map
->dim
);
13005 isl_multi_aff_free(ma
);
13007 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13008 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13011 isl_multi_aff_free(ma
);
13016 /* Compute the preimage of the domain or range (depending on "type")
13017 * of "map" under the function represented by "ma".
13018 * In other words, plug in "ma" in the domain or range of "map".
13019 * The result is a map that lives in the same space as "map"
13020 * except that the domain or range has been replaced by
13021 * the domain space of "ma".
13023 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13024 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13031 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13035 return map_preimage_multi_aff(map
, type
, ma
);
13037 if (isl_map_check_named_params(map
) < 0)
13039 if (!isl_space_has_named_params(ma
->space
))
13040 isl_die(map
->ctx
, isl_error_invalid
,
13041 "unaligned unnamed parameters", goto error
);
13042 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13043 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13045 return map_preimage_multi_aff(map
, type
, ma
);
13047 isl_multi_aff_free(ma
);
13048 return isl_map_free(map
);
13051 /* Compute the preimage of "set" under the function represented by "ma".
13052 * In other words, plug in "ma" in "set". The result is a set
13053 * that lives in the domain space of "ma".
13055 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13056 __isl_take isl_multi_aff
*ma
)
13058 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13061 /* Compute the preimage of the domain of "map" under the function
13062 * represented by "ma".
13063 * In other words, plug in "ma" in the domain of "map".
13064 * The result is a map that lives in the same space as "map"
13065 * except that the domain has been replaced by the domain space of "ma".
13067 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13068 __isl_take isl_multi_aff
*ma
)
13070 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13073 /* Compute the preimage of the range of "map" under the function
13074 * represented by "ma".
13075 * In other words, plug in "ma" in the range of "map".
13076 * The result is a map that lives in the same space as "map"
13077 * except that the range has been replaced by the domain space of "ma".
13079 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13080 __isl_take isl_multi_aff
*ma
)
13082 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13085 /* Compute the preimage of "map" under the function represented by "pma".
13086 * In other words, plug in "pma" in the domain or range of "map".
13087 * The result is a map that lives in the same space as "map",
13088 * except that the space of type "type" has been replaced by
13089 * the domain space of "pma".
13091 * The parameters of "map" and "pma" are assumed to have been aligned.
13093 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13094 __isl_take isl_map
*map
, enum isl_dim_type type
,
13095 __isl_take isl_pw_multi_aff
*pma
)
13104 isl_pw_multi_aff_free(pma
);
13105 res
= isl_map_empty(isl_map_get_space(map
));
13110 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13111 isl_multi_aff_copy(pma
->p
[0].maff
));
13112 if (type
== isl_dim_in
)
13113 res
= isl_map_intersect_domain(res
,
13114 isl_map_copy(pma
->p
[0].set
));
13116 res
= isl_map_intersect_range(res
,
13117 isl_map_copy(pma
->p
[0].set
));
13119 for (i
= 1; i
< pma
->n
; ++i
) {
13122 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13123 isl_multi_aff_copy(pma
->p
[i
].maff
));
13124 if (type
== isl_dim_in
)
13125 res_i
= isl_map_intersect_domain(res_i
,
13126 isl_map_copy(pma
->p
[i
].set
));
13128 res_i
= isl_map_intersect_range(res_i
,
13129 isl_map_copy(pma
->p
[i
].set
));
13130 res
= isl_map_union(res
, res_i
);
13133 isl_pw_multi_aff_free(pma
);
13137 isl_pw_multi_aff_free(pma
);
13142 /* Compute the preimage of "map" under the function represented by "pma".
13143 * In other words, plug in "pma" in the domain or range of "map".
13144 * The result is a map that lives in the same space as "map",
13145 * except that the space of type "type" has been replaced by
13146 * the domain space of "pma".
13148 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13149 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13156 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13160 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13162 if (isl_map_check_named_params(map
) < 0)
13164 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13166 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13167 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13169 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13171 isl_pw_multi_aff_free(pma
);
13172 return isl_map_free(map
);
13175 /* Compute the preimage of "set" under the function represented by "pma".
13176 * In other words, plug in "pma" in "set". The result is a set
13177 * that lives in the domain space of "pma".
13179 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13180 __isl_take isl_pw_multi_aff
*pma
)
13182 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13185 /* Compute the preimage of the domain of "map" under the function
13186 * represented by "pma".
13187 * In other words, plug in "pma" in the domain of "map".
13188 * The result is a map that lives in the same space as "map",
13189 * except that domain space has been replaced by the domain space of "pma".
13191 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13192 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13194 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13197 /* Compute the preimage of the range of "map" under the function
13198 * represented by "pma".
13199 * In other words, plug in "pma" in the range of "map".
13200 * The result is a map that lives in the same space as "map",
13201 * except that range space has been replaced by the domain space of "pma".
13203 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13204 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13206 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13209 /* Compute the preimage of "map" under the function represented by "mpa".
13210 * In other words, plug in "mpa" in the domain or range of "map".
13211 * The result is a map that lives in the same space as "map",
13212 * except that the space of type "type" has been replaced by
13213 * the domain space of "mpa".
13215 * If the map does not involve any constraints that refer to the
13216 * dimensions of the substituted space, then the only possible
13217 * effect of "mpa" on the map is to map the space to a different space.
13218 * We create a separate isl_multi_aff to effectuate this change
13219 * in order to avoid spurious splitting of the map along the pieces
13221 * If "mpa" has a non-trivial explicit domain, however,
13222 * then the full substitution should be performed.
13224 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13225 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13229 isl_pw_multi_aff
*pma
;
13234 n
= isl_map_dim(map
, type
);
13235 full
= isl_map_involves_dims(map
, type
, 0, n
);
13236 if (full
>= 0 && !full
)
13237 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13244 space
= isl_multi_pw_aff_get_space(mpa
);
13245 isl_multi_pw_aff_free(mpa
);
13246 ma
= isl_multi_aff_zero(space
);
13247 return isl_map_preimage_multi_aff(map
, type
, ma
);
13250 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13251 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13254 isl_multi_pw_aff_free(mpa
);
13258 /* Compute the preimage of "map" under the function represented by "mpa".
13259 * In other words, plug in "mpa" in the domain "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 "mpa".
13263 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13264 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13266 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13269 /* Compute the preimage of "set" by the function represented by "mpa".
13270 * In other words, plug in "mpa" in "set".
13272 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13273 __isl_take isl_multi_pw_aff
*mpa
)
13275 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13278 /* Return a copy of the equality constraints of "bset" as a matrix.
13280 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13281 __isl_keep isl_basic_set
*bset
)
13289 ctx
= isl_basic_set_get_ctx(bset
);
13290 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13291 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13294 /* Are the "n" "coefficients" starting at "first" of the integer division
13295 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13297 * The "coefficient" at position 0 is the denominator.
13298 * The "coefficient" at position 1 is the constant term.
13300 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13301 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13302 unsigned first
, unsigned n
)
13304 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13305 return isl_bool_error
;
13306 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13307 return isl_bool_error
;
13308 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13309 bmap2
->div
[pos2
] + first
, n
);
13312 /* Are the integer division expressions at position "pos1" in "bmap1" and
13313 * "pos2" in "bmap2" equal to each other, except that the constant terms
13316 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13317 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13318 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13323 if (!bmap1
|| !bmap2
)
13324 return isl_bool_error
;
13325 total
= isl_basic_map_total_dim(bmap1
);
13326 if (total
!= isl_basic_map_total_dim(bmap2
))
13327 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13328 "incomparable div expressions", return isl_bool_error
);
13329 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13331 if (equal
< 0 || !equal
)
13333 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13335 if (equal
< 0 || equal
)
13336 return isl_bool_not(equal
);
13337 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13341 /* Replace the numerator of the constant term of the integer division
13342 * expression at position "div" in "bmap" by "value".
13343 * The caller guarantees that this does not change the meaning
13346 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13347 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13349 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13350 return isl_basic_map_free(bmap
);
13352 isl_int_set_si(bmap
->div
[div
][1], value
);
13357 /* Is the point "inner" internal to inequality constraint "ineq"
13359 * The point is considered to be internal to the inequality constraint,
13360 * if it strictly lies on the positive side of the inequality constraint,
13361 * or if it lies on the constraint and the constraint is lexico-positive.
13363 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13364 __isl_keep isl_basic_set
*bset
, int ineq
)
13370 if (!inner
|| !bset
)
13371 return isl_bool_error
;
13373 ctx
= isl_basic_set_get_ctx(bset
);
13374 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13375 &ctx
->normalize_gcd
);
13376 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13377 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13379 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13380 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13381 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13384 /* Tighten the inequality constraints of "bset" that are outward with respect
13385 * to the point "vec".
13386 * That is, tighten the constraints that are not satisfied by "vec".
13388 * "vec" is a point internal to some superset S of "bset" that is used
13389 * to make the subsets of S disjoint, by tightening one half of the constraints
13390 * that separate two subsets. In particular, the constraints of S
13391 * are all satisfied by "vec" and should not be tightened.
13392 * Of the internal constraints, those that have "vec" on the outside
13393 * are tightened. The shared facet is included in the adjacent subset
13394 * with the opposite constraint.
13395 * For constraints that saturate "vec", this criterion cannot be used
13396 * to determine which of the two sides should be tightened.
13397 * Instead, the sign of the first non-zero coefficient is used
13398 * to make this choice. Note that this second criterion is never used
13399 * on the constraints of S since "vec" is interior to "S".
13401 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13402 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13406 bset
= isl_basic_set_cow(bset
);
13409 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13412 internal
= is_internal(vec
, bset
, j
);
13414 return isl_basic_set_free(bset
);
13417 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13423 /* Replace the variables x of type "type" starting at "first" in "bmap"
13424 * by x' with x = M x' with M the matrix trans.
13425 * That is, replace the corresponding coefficients c by c M.
13427 * The transformation matrix should be a square matrix.
13429 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13430 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13431 __isl_take isl_mat
*trans
)
13435 bmap
= isl_basic_map_cow(bmap
);
13436 if (!bmap
|| !trans
)
13439 if (trans
->n_row
!= trans
->n_col
)
13440 isl_die(trans
->ctx
, isl_error_invalid
,
13441 "expecting square transformation matrix", goto error
);
13442 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13445 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13447 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13448 isl_mat_copy(trans
)) < 0)
13450 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13451 isl_mat_copy(trans
)) < 0)
13453 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13454 isl_mat_copy(trans
)) < 0)
13457 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13458 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13460 isl_mat_free(trans
);
13463 isl_mat_free(trans
);
13464 isl_basic_map_free(bmap
);
13468 /* Replace the variables x of type "type" starting at "first" in "bset"
13469 * by x' with x = M x' with M the matrix trans.
13470 * That is, replace the corresponding coefficients c by c M.
13472 * The transformation matrix should be a square matrix.
13474 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13475 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13476 __isl_take isl_mat
*trans
)
13478 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);