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 coefficients of the variables of the given type
150 * within the sequence of coefficients of "bmap".
152 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
153 enum isl_dim_type type
)
156 case isl_dim_cst
: return 0;
160 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
165 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
166 enum isl_dim_type type
)
168 return isl_basic_map_offset(bset
, type
);
171 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
173 return pos(map
->dim
, type
);
176 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
177 enum isl_dim_type type
)
179 return isl_basic_map_dim(bset
, type
);
182 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
184 return isl_basic_set_dim(bset
, isl_dim_set
);
187 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
189 return isl_basic_set_dim(bset
, isl_dim_param
);
192 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
194 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
197 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
199 return isl_set_dim(set
, isl_dim_set
);
202 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
204 return isl_set_dim(set
, isl_dim_param
);
207 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
209 return bmap
? bmap
->dim
->n_in
: 0;
212 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
214 return bmap
? bmap
->dim
->n_out
: 0;
217 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
219 return bmap
? bmap
->dim
->nparam
: 0;
222 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
224 return bmap
? bmap
->n_div
: 0;
227 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
229 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
232 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
234 return map
? map
->dim
->n_in
: 0;
237 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
239 return map
? map
->dim
->n_out
: 0;
242 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
244 return map
? map
->dim
->nparam
: 0;
247 /* Return the number of equality constraints in the description of "bmap".
248 * Return -1 on error.
250 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
257 /* Return the number of equality constraints in the description of "bset".
258 * Return -1 on error.
260 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
262 return isl_basic_map_n_equality(bset_to_bmap(bset
));
265 /* Return the number of inequality constraints in the description of "bmap".
266 * Return -1 on error.
268 int isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
275 /* Return the number of inequality constraints in the description of "bset".
276 * Return -1 on error.
278 int isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
280 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
283 /* Do "bmap1" and "bmap2" have the same parameters?
285 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
286 __isl_keep isl_basic_map
*bmap2
)
288 isl_space
*space1
, *space2
;
290 space1
= isl_basic_map_peek_space(bmap1
);
291 space2
= isl_basic_map_peek_space(bmap2
);
292 return isl_space_has_equal_params(space1
, space2
);
295 /* Do "map1" and "map2" have the same parameters?
297 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
298 __isl_keep isl_map
*map2
)
300 isl_space
*space1
, *space2
;
302 space1
= isl_map_peek_space(map1
);
303 space2
= isl_map_peek_space(map2
);
304 return isl_space_has_equal_params(space1
, space2
);
307 /* Do "map" and "set" have the same parameters?
309 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
310 __isl_keep isl_set
*set
)
312 return isl_map_has_equal_params(map
, set_to_map(set
));
315 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
316 __isl_keep isl_set
*set
)
320 return isl_bool_error
;
321 m
= isl_map_has_equal_params(map
, set_to_map(set
));
324 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
325 set
->dim
, isl_dim_set
);
328 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
329 __isl_keep isl_basic_set
*bset
)
333 return isl_bool_error
;
334 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
337 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
338 bset
->dim
, isl_dim_set
);
341 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
342 __isl_keep isl_set
*set
)
346 return isl_bool_error
;
347 m
= isl_map_has_equal_params(map
, set_to_map(set
));
350 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
351 set
->dim
, isl_dim_set
);
354 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
355 __isl_keep isl_basic_set
*bset
)
359 return isl_bool_error
;
360 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
363 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
364 bset
->dim
, isl_dim_set
);
367 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
369 return bmap
? bmap
->ctx
: NULL
;
372 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
374 return bset
? bset
->ctx
: NULL
;
377 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
379 return map
? map
->ctx
: NULL
;
382 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
384 return set
? set
->ctx
: NULL
;
387 /* Return the space of "bmap".
389 __isl_keep isl_space
*isl_basic_map_peek_space(
390 __isl_keep
const isl_basic_map
*bmap
)
392 return bmap
? bmap
->dim
: NULL
;
395 /* Return the space of "bset".
397 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
399 return isl_basic_map_peek_space(bset_to_bmap(bset
));
402 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
404 return isl_space_copy(isl_basic_map_peek_space(bmap
));
407 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
409 return isl_basic_map_get_space(bset_to_bmap(bset
));
412 /* Extract the divs in "bmap" as a matrix.
414 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
422 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
426 ctx
= isl_basic_map_get_ctx(bmap
);
427 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
428 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
432 for (i
= 0; i
< bmap
->n_div
; ++i
)
433 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
438 /* Extract the divs in "bset" as a matrix.
440 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
442 return isl_basic_map_get_divs(bset
);
445 __isl_give isl_local_space
*isl_basic_map_get_local_space(
446 __isl_keep isl_basic_map
*bmap
)
453 div
= isl_basic_map_get_divs(bmap
);
454 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
457 __isl_give isl_local_space
*isl_basic_set_get_local_space(
458 __isl_keep isl_basic_set
*bset
)
460 return isl_basic_map_get_local_space(bset
);
463 /* For each known div d = floor(f/m), add the constraints
466 * -(f-(m-1)) + m d >= 0
468 * Do not finalize the result.
470 static __isl_give isl_basic_map
*add_known_div_constraints(
471 __isl_take isl_basic_map
*bmap
)
478 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
481 bmap
= isl_basic_map_cow(bmap
);
482 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
485 for (i
= 0; i
< n_div
; ++i
) {
486 if (isl_int_is_zero(bmap
->div
[i
][0]))
488 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
494 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
495 __isl_take isl_local_space
*ls
)
504 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
505 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
506 n_div
, 0, 2 * n_div
);
508 for (i
= 0; i
< n_div
; ++i
)
509 if (isl_basic_map_alloc_div(bmap
) < 0)
512 for (i
= 0; i
< n_div
; ++i
)
513 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
514 bmap
= add_known_div_constraints(bmap
);
516 isl_local_space_free(ls
);
519 isl_local_space_free(ls
);
520 isl_basic_map_free(bmap
);
524 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
525 __isl_take isl_local_space
*ls
)
527 return isl_basic_map_from_local_space(ls
);
530 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
532 return isl_space_copy(isl_map_peek_space(map
));
535 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
539 return isl_space_copy(set
->dim
);
542 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
545 bmap
= isl_basic_map_cow(bmap
);
548 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
551 bmap
= isl_basic_map_finalize(bmap
);
554 isl_basic_map_free(bmap
);
558 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set
*bset
, const char *s
)
561 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
565 enum isl_dim_type type
)
567 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
570 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
571 enum isl_dim_type type
, const char *s
)
575 map
= isl_map_cow(map
);
579 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
583 for (i
= 0; i
< map
->n
; ++i
) {
584 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map
*bmap
,
599 enum isl_dim_type type
, __isl_take isl_id
*id
)
601 bmap
= isl_basic_map_cow(bmap
);
604 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
606 return isl_basic_map_free(bmap
);
607 bmap
= isl_basic_map_finalize(bmap
);
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
619 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
622 /* Does the input or output tuple have a name?
624 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
626 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
630 enum isl_dim_type type
)
632 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
635 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
642 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
643 enum isl_dim_type type
, __isl_take isl_id
*id
)
645 map
= isl_map_cow(map
);
649 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
651 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
657 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
658 __isl_take isl_id
*id
)
660 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
663 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
664 enum isl_dim_type type
)
666 map
= isl_map_cow(map
);
670 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
672 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
675 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
677 return isl_map_reset_tuple_id(set
, isl_dim_set
);
680 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
682 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
685 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
686 enum isl_dim_type type
)
688 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
691 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
693 return isl_map_has_tuple_id(set
, isl_dim_set
);
696 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
698 return isl_map_get_tuple_id(set
, isl_dim_set
);
701 /* Does the set tuple have a name?
703 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
706 return isl_bool_error
;
707 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
713 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
718 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
722 enum isl_dim_type type
, unsigned pos
)
724 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
728 enum isl_dim_type type
, unsigned pos
)
730 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
733 /* Does the given dimension have a name?
735 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
736 enum isl_dim_type type
, unsigned pos
)
739 return isl_bool_error
;
740 return isl_space_has_dim_name(map
->dim
, type
, pos
);
743 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
744 enum isl_dim_type type
, unsigned pos
)
746 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
749 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
750 enum isl_dim_type type
, unsigned pos
)
752 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
755 /* Does the given dimension have a name?
757 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
758 enum isl_dim_type type
, unsigned pos
)
761 return isl_bool_error
;
762 return isl_space_has_dim_name(set
->dim
, type
, pos
);
765 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map
*bmap
,
767 enum isl_dim_type type
, unsigned pos
, const char *s
)
769 bmap
= isl_basic_map_cow(bmap
);
772 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
775 return isl_basic_map_finalize(bmap
);
777 isl_basic_map_free(bmap
);
781 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
782 enum isl_dim_type type
, unsigned pos
, const char *s
)
786 map
= isl_map_cow(map
);
790 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
794 for (i
= 0; i
< map
->n
; ++i
) {
795 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
806 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set
*bset
,
808 enum isl_dim_type type
, unsigned pos
, const char *s
)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
814 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
815 enum isl_dim_type type
, unsigned pos
, const char *s
)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
821 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
822 enum isl_dim_type type
, unsigned pos
)
825 return isl_bool_error
;
826 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
829 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
830 enum isl_dim_type type
, unsigned pos
)
832 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
835 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
836 enum isl_dim_type type
, unsigned pos
)
838 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
841 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
842 enum isl_dim_type type
, unsigned pos
)
844 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
847 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
848 enum isl_dim_type type
, unsigned pos
)
850 return isl_map_has_dim_id(set
, type
, pos
);
853 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
854 enum isl_dim_type type
, unsigned pos
)
856 return isl_map_get_dim_id(set
, type
, pos
);
859 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
860 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
862 map
= isl_map_cow(map
);
866 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
868 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
874 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
875 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
877 return isl_map_set_dim_id(set
, type
, pos
, id
);
880 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
881 __isl_keep isl_id
*id
)
885 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
888 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
889 __isl_keep isl_id
*id
)
891 return isl_map_find_dim_by_id(set
, type
, id
);
894 /* Return the position of the dimension of the given type and name
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
899 enum isl_dim_type type
, const char *name
)
903 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
906 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
911 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
914 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
917 return isl_map_find_dim_by_name(set
, type
, name
);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
925 * with k a constant and e an existentially quantified variable.
927 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
936 return isl_bool_error
;
938 if (!isl_int_is_zero(bset
->eq
[i
][0]))
939 return isl_bool_false
;
941 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
942 d
= isl_basic_set_dim(bset
, isl_dim_set
);
943 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
945 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
946 return isl_bool_false
;
947 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
949 return isl_bool_false
;
950 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
952 return isl_bool_false
;
954 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
956 return isl_bool_false
;
957 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
958 n_div
- pos2
- 1) != -1)
959 return isl_bool_false
;
960 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
961 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
962 return isl_bool_false
;
964 return isl_bool_true
;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
974 space
= isl_map_get_space(map
);
975 space
= isl_space_reset_user(space
);
976 map
= isl_map_reset_space(map
, space
);
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
986 return isl_map_reset_user(set
);
989 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
992 return isl_bool_error
;
993 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1002 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1008 return isl_bool_error
;
1010 return isl_bool_false
;
1011 rational
= isl_basic_map_is_rational(map
->p
[0]);
1014 for (i
= 1; i
< map
->n
; ++i
) {
1015 isl_bool rational_i
;
1017 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1020 if (rational
!= rational_i
)
1021 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error
);
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1035 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1037 return isl_map_is_rational(set
);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1042 return isl_basic_map_is_rational(bset
);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1053 isl_bool has_rational
= isl_bool_true
;
1057 return isl_bool_error
;
1058 if (isl_basic_map_plain_is_empty(bmap
))
1059 return isl_bool_false
;
1060 if (!isl_basic_map_is_rational(bmap
))
1061 return isl_bool_false
;
1062 bmap
= isl_basic_map_copy(bmap
);
1063 bmap
= isl_basic_map_implicit_equalities(bmap
);
1065 return isl_bool_error
;
1066 total
= isl_basic_map_total_dim(bmap
);
1067 if (bmap
->n_eq
== total
) {
1069 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1070 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1073 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1074 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1076 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1081 if (i
== bmap
->n_eq
)
1082 has_rational
= isl_bool_false
;
1084 isl_basic_map_free(bmap
);
1086 return has_rational
;
1089 /* Does "map" contain any rational points?
1091 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1094 isl_bool has_rational
;
1097 return isl_bool_error
;
1098 for (i
= 0; i
< map
->n
; ++i
) {
1099 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1100 if (has_rational
< 0 || has_rational
)
1101 return has_rational
;
1103 return isl_bool_false
;
1106 /* Does "set" contain any rational points?
1108 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1110 return isl_map_has_rational(set
);
1113 /* Is this basic set a parameter domain?
1115 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1118 return isl_bool_error
;
1119 return isl_space_is_params(bset
->dim
);
1122 /* Is this set a parameter domain?
1124 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1127 return isl_bool_error
;
1128 return isl_space_is_params(set
->dim
);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1138 return isl_bool_error
;
1139 return isl_space_is_params(map
->dim
);
1142 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1143 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1144 unsigned n_eq
, unsigned n_ineq
)
1147 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1148 unsigned n_var
= isl_space_dim(space
, isl_dim_all
);
1149 size_t row_size
= 1 + n_var
+ extra
;
1155 return isl_basic_map_free(bmap
);
1157 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1158 if (isl_blk_is_error(bmap
->block
))
1161 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1162 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1166 bmap
->block2
= isl_blk_empty();
1169 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1170 if (isl_blk_is_error(bmap
->block2
))
1173 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1178 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1179 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1181 for (i
= 0; i
< extra
; ++i
)
1182 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1186 bmap
->c_size
= n_eq
+ n_ineq
;
1187 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1188 bmap
->extra
= extra
;
1192 bmap
->sample
= NULL
;
1196 isl_basic_map_free(bmap
);
1200 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1201 unsigned nparam
, unsigned dim
, unsigned extra
,
1202 unsigned n_eq
, unsigned n_ineq
)
1204 struct isl_basic_map
*bmap
;
1207 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1211 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1212 return bset_from_bmap(bmap
);
1215 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1216 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1218 struct isl_basic_map
*bmap
;
1221 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1222 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1223 return bset_from_bmap(bmap
);
1225 isl_space_free(dim
);
1229 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1230 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1232 struct isl_basic_map
*bmap
;
1236 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1241 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1243 isl_space_free(space
);
1247 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1248 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1249 unsigned n_eq
, unsigned n_ineq
)
1251 struct isl_basic_map
*bmap
;
1254 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1258 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1262 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1263 __isl_keep isl_basic_map
*src
)
1266 unsigned total
= isl_basic_map_total_dim(src
);
1271 for (i
= 0; i
< src
->n_eq
; ++i
) {
1272 int j
= isl_basic_map_alloc_equality(dst
);
1274 return isl_basic_map_free(dst
);
1275 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1278 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1279 int j
= isl_basic_map_alloc_inequality(dst
);
1281 return isl_basic_map_free(dst
);
1282 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1285 for (i
= 0; i
< src
->n_div
; ++i
) {
1286 int j
= isl_basic_map_alloc_div(dst
);
1288 return isl_basic_map_free(dst
);
1289 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1291 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1295 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1297 struct isl_basic_map
*dup
;
1301 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1302 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1303 dup
= dup_constraints(dup
, bmap
);
1306 dup
->flags
= bmap
->flags
;
1307 dup
->sample
= isl_vec_copy(bmap
->sample
);
1311 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1313 struct isl_basic_map
*dup
;
1315 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1316 return bset_from_bmap(dup
);
1319 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1324 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1328 return isl_basic_set_dup(bset
);
1331 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1340 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1345 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1349 bmap
= isl_basic_map_dup(bmap
);
1351 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1355 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1364 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1369 if (--bmap
->ref
> 0)
1372 isl_ctx_deref(bmap
->ctx
);
1374 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1376 isl_blk_free(bmap
->ctx
, bmap
->block
);
1377 isl_vec_free(bmap
->sample
);
1378 isl_space_free(bmap
->dim
);
1384 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1386 return isl_basic_map_free(bset_to_bmap(bset
));
1389 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1391 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1394 /* Check that "bset" does not involve any parameters.
1396 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1399 return isl_stat_error
;
1400 if (isl_basic_set_dim(bset
, isl_dim_param
) != 0)
1401 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1402 "basic set should not have any parameters",
1403 return isl_stat_error
);
1407 /* Check that "bset" does not involve any local variables.
1409 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1412 return isl_stat_error
;
1413 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
1414 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1415 "basic set should not have any local variables",
1416 return isl_stat_error
);
1420 /* Check that "map" has only named parameters, reporting an error
1423 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1425 return isl_space_check_named_params(isl_map_peek_space(map
));
1428 /* Check that "bmap" has only named parameters, reporting an error
1431 static isl_stat
isl_basic_map_check_named_params(__isl_keep isl_basic_map
*bmap
)
1433 return isl_space_check_named_params(isl_basic_map_peek_space(bmap
));
1436 /* Check that "bmap1" and "bmap2" have the same parameters,
1437 * reporting an error if they do not.
1439 static isl_stat
isl_basic_map_check_equal_params(
1440 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1444 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1446 return isl_stat_error
;
1448 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1449 "parameters don't match", return isl_stat_error
);
1453 __isl_give isl_map
*isl_map_align_params_map_map_and(
1454 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1455 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1456 __isl_take isl_map
*map2
))
1460 if (isl_map_has_equal_params(map1
, map2
))
1461 return fn(map1
, map2
);
1462 if (isl_map_check_named_params(map1
) < 0)
1464 if (isl_map_check_named_params(map2
) < 0)
1466 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1467 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1468 return fn(map1
, map2
);
1475 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1476 __isl_keep isl_map
*map2
,
1477 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1482 return isl_bool_error
;
1483 if (isl_map_has_equal_params(map1
, map2
))
1484 return fn(map1
, map2
);
1485 if (isl_map_check_named_params(map1
) < 0)
1486 return isl_bool_error
;
1487 if (isl_map_check_named_params(map2
) < 0)
1488 return isl_bool_error
;
1489 map1
= isl_map_copy(map1
);
1490 map2
= isl_map_copy(map2
);
1491 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1492 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1499 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1501 struct isl_ctx
*ctx
;
1505 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1506 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1508 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1509 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1510 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1511 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1512 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1514 int j
= isl_basic_map_alloc_inequality(bmap
);
1518 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1519 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1526 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1527 bmap
->extra
- bmap
->n_div
);
1528 return bmap
->n_eq
++;
1531 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1533 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1536 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1540 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1545 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1547 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1550 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1555 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1557 if (pos
!= bmap
->n_eq
- 1) {
1559 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1560 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1566 /* Turn inequality "pos" of "bmap" into an equality.
1568 * In particular, we move the inequality in front of the equalities
1569 * and move the last inequality in the position of the moved inequality.
1570 * Note that isl_tab_make_equalities_explicit depends on this particular
1571 * change in the ordering of the constraints.
1573 void isl_basic_map_inequality_to_equality(
1574 struct isl_basic_map
*bmap
, unsigned pos
)
1578 t
= bmap
->ineq
[pos
];
1579 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1580 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1585 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1586 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1587 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1588 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1591 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1593 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1596 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1598 struct isl_ctx
*ctx
;
1602 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1603 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1604 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1605 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1606 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1607 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1608 1 + isl_basic_map_total_dim(bmap
),
1609 bmap
->extra
- bmap
->n_div
);
1610 return bmap
->n_ineq
++;
1613 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1615 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1618 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1622 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1627 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1629 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1632 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1637 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1639 if (pos
!= bmap
->n_ineq
- 1) {
1640 t
= bmap
->ineq
[pos
];
1641 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1642 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1643 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1649 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1651 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1654 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1660 empty
= isl_basic_map_plain_is_empty(bmap
);
1662 return isl_basic_map_free(bmap
);
1666 bmap
= isl_basic_map_cow(bmap
);
1667 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1670 k
= isl_basic_map_alloc_equality(bmap
);
1673 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1676 isl_basic_map_free(bmap
);
1680 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1683 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1686 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1691 bmap
= isl_basic_map_cow(bmap
);
1692 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1695 k
= isl_basic_map_alloc_inequality(bmap
);
1698 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1701 isl_basic_map_free(bmap
);
1705 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1708 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1711 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1715 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1716 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1717 1 + 1 + isl_basic_map_total_dim(bmap
),
1718 bmap
->extra
- bmap
->n_div
);
1719 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1720 return bmap
->n_div
++;
1723 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1725 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1729 #define TYPE isl_basic_map
1730 #include "check_type_range_templ.c"
1732 /* Check that there are "n" dimensions of type "type" starting at "first"
1735 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1736 enum isl_dim_type type
, unsigned first
, unsigned n
)
1738 return isl_basic_map_check_range(bset_to_bmap(bset
),
1742 /* Insert an extra integer division, prescribed by "div", to "bmap"
1743 * at (integer division) position "pos".
1745 * The integer division is first added at the end and then moved
1746 * into the right position.
1748 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1749 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1753 bmap
= isl_basic_map_cow(bmap
);
1755 return isl_basic_map_free(bmap
);
1757 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1758 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1759 "unexpected size", return isl_basic_map_free(bmap
));
1760 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1761 return isl_basic_map_free(bmap
);
1763 bmap
= isl_basic_map_extend_space(bmap
,
1764 isl_basic_map_get_space(bmap
), 1, 0, 2);
1765 k
= isl_basic_map_alloc_div(bmap
);
1767 return isl_basic_map_free(bmap
);
1768 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1769 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1771 for (i
= k
; i
> pos
; --i
)
1772 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1777 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1780 return isl_stat_error
;
1781 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1786 static __isl_give isl_basic_map
*add_constraints(
1787 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1788 unsigned i_pos
, unsigned o_pos
)
1790 unsigned total
, n_param
, n_in
, o_in
, n_out
, o_out
, n_div
;
1793 struct isl_dim_map
*dim_map
;
1795 space
= isl_basic_map_peek_space(bmap2
);
1796 if (!bmap1
|| !space
)
1799 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1800 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1801 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1802 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1803 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1804 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1805 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1806 ctx
= isl_basic_map_get_ctx(bmap1
);
1807 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1808 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1809 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1810 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1811 isl_dim_map_div(dim_map
, bmap2
, total
);
1813 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1815 isl_basic_map_free(bmap1
);
1816 isl_basic_map_free(bmap2
);
1820 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1821 struct isl_basic_set
*bset2
, unsigned pos
)
1823 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1824 bset_to_bmap(bset2
), 0, pos
));
1827 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1828 __isl_take isl_basic_map
*base
, __isl_take isl_space
*space
,
1829 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1831 struct isl_basic_map
*ext
;
1841 dims_ok
= isl_space_is_equal(base
->dim
, space
) &&
1842 base
->extra
>= base
->n_div
+ extra
;
1844 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1845 room_for_ineq(base
, n_ineq
)) {
1846 isl_space_free(space
);
1850 isl_assert(base
->ctx
, base
->dim
->nparam
<= space
->nparam
, goto error
);
1851 isl_assert(base
->ctx
, base
->dim
->n_in
<= space
->n_in
, goto error
);
1852 isl_assert(base
->ctx
, base
->dim
->n_out
<= space
->n_out
, goto error
);
1853 extra
+= base
->extra
;
1855 n_ineq
+= base
->n_ineq
;
1857 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1863 ext
->sample
= isl_vec_copy(base
->sample
);
1864 flags
= base
->flags
;
1865 ext
= add_constraints(ext
, base
, 0, 0);
1868 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1874 isl_space_free(space
);
1875 isl_basic_map_free(base
);
1879 __isl_give isl_basic_set
*isl_basic_set_extend_space(
1880 __isl_take isl_basic_set
*base
,
1881 __isl_take isl_space
*dim
, unsigned extra
,
1882 unsigned n_eq
, unsigned n_ineq
)
1884 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1885 dim
, extra
, n_eq
, n_ineq
));
1888 struct isl_basic_map
*isl_basic_map_extend_constraints(
1889 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1893 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1897 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1898 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1899 unsigned n_eq
, unsigned n_ineq
)
1901 struct isl_basic_map
*bmap
;
1906 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1910 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1913 isl_basic_map_free(base
);
1917 struct isl_basic_set
*isl_basic_set_extend_constraints(
1918 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1920 isl_basic_map
*bmap
= bset_to_bmap(base
);
1921 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1922 return bset_from_bmap(bmap
);
1925 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1927 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1930 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1935 if (bmap
->ref
> 1) {
1937 bmap
= isl_basic_map_dup(bmap
);
1940 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1941 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1946 /* Clear all cached information in "map", either because it is about
1947 * to be modified or because it is being freed.
1948 * Always return the same pointer that is passed in.
1949 * This is needed for the use in isl_map_free.
1951 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1953 isl_basic_map_free(map
->cached_simple_hull
[0]);
1954 isl_basic_map_free(map
->cached_simple_hull
[1]);
1955 map
->cached_simple_hull
[0] = NULL
;
1956 map
->cached_simple_hull
[1] = NULL
;
1960 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1962 return isl_map_cow(set
);
1965 /* Return an isl_map that is equal to "map" and that has only
1966 * a single reference.
1968 * If the original input already has only one reference, then
1969 * simply return it, but clear all cached information, since
1970 * it may be rendered invalid by the operations that will be
1971 * performed on the result.
1973 * Otherwise, create a duplicate (without any cached information).
1975 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1981 return clear_caches(map
);
1983 return isl_map_dup(map
);
1986 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1987 unsigned a_len
, unsigned b_len
)
1989 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1990 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1991 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1994 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1995 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2000 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2003 if (n1
== 0 || n2
== 0)
2006 bmap
= isl_basic_map_cow(bmap
);
2010 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2011 if (isl_blk_is_error(blk
))
2014 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2016 bmap
->eq
[i
] + pos
, n1
, n2
);
2018 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2020 bmap
->ineq
[i
] + pos
, n1
, n2
);
2022 for (i
= 0; i
< bmap
->n_div
; ++i
)
2024 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2026 isl_blk_free(bmap
->ctx
, blk
);
2028 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2029 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2030 return isl_basic_map_finalize(bmap
);
2032 isl_basic_map_free(bmap
);
2036 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2037 __isl_take isl_basic_map
*bmap
)
2043 total
= isl_basic_map_total_dim(bmap
);
2044 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2045 return isl_basic_map_free(bmap
);
2046 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2048 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2050 i
= isl_basic_map_alloc_equality(bmap
);
2054 isl_int_set_si(bmap
->eq
[i
][0], 1);
2055 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2056 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2057 isl_vec_free(bmap
->sample
);
2058 bmap
->sample
= NULL
;
2059 return isl_basic_map_finalize(bmap
);
2061 isl_basic_map_free(bmap
);
2065 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2066 __isl_take isl_basic_set
*bset
)
2068 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2071 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2072 __isl_take isl_basic_map
*bmap
)
2077 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2080 bmap
= isl_basic_map_cow(bmap
);
2084 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2086 return isl_basic_map_finalize(bmap
);
2089 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2090 __isl_take isl_basic_set
*bset
)
2092 return isl_basic_map_set_rational(bset
);
2095 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2096 __isl_take isl_basic_set
*bset
)
2101 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2104 bset
= isl_basic_set_cow(bset
);
2108 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2110 return isl_basic_set_finalize(bset
);
2113 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2117 map
= isl_map_cow(map
);
2120 for (i
= 0; i
< map
->n
; ++i
) {
2121 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2131 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2133 return isl_map_set_rational(set
);
2136 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2139 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2141 isl_int
*t
= bmap
->div
[a
];
2142 bmap
->div
[a
] = bmap
->div
[b
];
2146 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2147 * div definitions accordingly.
2149 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2155 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2157 return isl_basic_map_free(bmap
);
2159 swap_div(bmap
, a
, b
);
2161 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2162 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2164 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2165 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2167 for (i
= 0; i
< bmap
->n_div
; ++i
)
2168 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2169 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2174 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2176 isl_seq_cpy(c
, c
+ n
, rem
);
2177 isl_seq_clr(c
+ rem
, n
);
2180 /* Drop n dimensions starting at first.
2182 * In principle, this frees up some extra variables as the number
2183 * of columns remains constant, but we would have to extend
2184 * the div array too as the number of rows in this array is assumed
2185 * to be equal to extra.
2187 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2188 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2190 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2193 /* Move "n" divs starting at "first" to the end of the list of divs.
2195 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2196 unsigned first
, unsigned n
)
2201 if (first
+ n
== bmap
->n_div
)
2204 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2207 for (i
= 0; i
< n
; ++i
)
2208 div
[i
] = bmap
->div
[first
+ i
];
2209 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2210 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2211 for (i
= 0; i
< n
; ++i
)
2212 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2216 isl_basic_map_free(bmap
);
2221 #define TYPE isl_map
2223 #include "check_type_range_templ.c"
2225 /* Check that there are "n" dimensions of type "type" starting at "first"
2228 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2229 enum isl_dim_type type
, unsigned first
, unsigned n
)
2231 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2234 /* Drop "n" dimensions of type "type" starting at "first".
2235 * Perform the core computation, without cowing or
2236 * simplifying and finalizing the result.
2238 * In principle, this frees up some extra variables as the number
2239 * of columns remains constant, but we would have to extend
2240 * the div array too as the number of rows in this array is assumed
2241 * to be equal to extra.
2243 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2244 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2245 unsigned first
, unsigned n
)
2251 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2252 return isl_basic_map_free(bmap
);
2254 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2255 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2256 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2257 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2259 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2260 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2262 for (i
= 0; i
< bmap
->n_div
; ++i
)
2263 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2265 if (type
== isl_dim_div
) {
2266 bmap
= move_divs_last(bmap
, first
, n
);
2269 if (isl_basic_map_free_div(bmap
, n
) < 0)
2270 return isl_basic_map_free(bmap
);
2272 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2274 return isl_basic_map_free(bmap
);
2276 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2277 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2281 /* Drop "n" dimensions of type "type" starting at "first".
2283 * In principle, this frees up some extra variables as the number
2284 * of columns remains constant, but we would have to extend
2285 * the div array too as the number of rows in this array is assumed
2286 * to be equal to extra.
2288 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2289 enum isl_dim_type type
, unsigned first
, unsigned n
)
2293 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2296 bmap
= isl_basic_map_cow(bmap
);
2300 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2302 bmap
= isl_basic_map_simplify(bmap
);
2303 return isl_basic_map_finalize(bmap
);
2306 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2307 enum isl_dim_type type
, unsigned first
, unsigned n
)
2309 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2313 /* No longer consider "map" to be normalized.
2315 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2319 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2323 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2324 enum isl_dim_type type
, unsigned first
, unsigned n
)
2328 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2329 return isl_map_free(map
);
2331 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2333 map
= isl_map_cow(map
);
2336 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2340 for (i
= 0; i
< map
->n
; ++i
) {
2341 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2345 map
= isl_map_unmark_normalized(map
);
2353 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2354 enum isl_dim_type type
, unsigned first
, unsigned n
)
2356 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2359 /* Drop the integer division at position "div", which is assumed
2360 * not to appear in any of the constraints or
2361 * in any of the other integer divisions.
2363 * Since the integer division is redundant, there is no need to cow.
2365 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2366 __isl_take isl_basic_map
*bmap
, unsigned div
)
2368 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2371 /* Eliminate the specified n dimensions starting at first from the
2372 * constraints, without removing the dimensions from the space.
2373 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2375 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2376 enum isl_dim_type type
, unsigned first
, unsigned n
)
2383 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2384 return isl_map_free(map
);
2386 map
= isl_map_cow(map
);
2390 for (i
= 0; i
< map
->n
; ++i
) {
2391 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2401 /* Eliminate the specified n dimensions starting at first from the
2402 * constraints, without removing the dimensions from the space.
2403 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2405 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2406 enum isl_dim_type type
, unsigned first
, unsigned n
)
2408 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2411 /* Eliminate the specified n dimensions starting at first from the
2412 * constraints, without removing the dimensions from the space.
2413 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2415 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2416 unsigned first
, unsigned n
)
2418 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2421 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2422 __isl_take isl_basic_map
*bmap
)
2426 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2428 return isl_basic_map_free(bmap
);
2429 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2433 return isl_basic_map_finalize(bmap
);
2436 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2437 __isl_take isl_basic_set
*bset
)
2439 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2442 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2451 map
= isl_map_cow(map
);
2455 for (i
= 0; i
< map
->n
; ++i
) {
2456 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2466 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2468 return isl_map_remove_divs(set
);
2471 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2472 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2473 unsigned first
, unsigned n
)
2475 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2476 return isl_basic_map_free(bmap
);
2477 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2479 bmap
= isl_basic_map_eliminate_vars(bmap
,
2480 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2483 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2485 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2489 /* Return true if the definition of the given div (recursively) involves
2490 * any of the given variables.
2492 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2493 unsigned first
, unsigned n
)
2496 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2498 if (isl_int_is_zero(bmap
->div
[div
][0]))
2499 return isl_bool_false
;
2500 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2501 return isl_bool_true
;
2503 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2506 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2508 involves
= div_involves_vars(bmap
, i
, first
, n
);
2509 if (involves
< 0 || involves
)
2513 return isl_bool_false
;
2516 /* Try and add a lower and/or upper bound on "div" to "bmap"
2517 * based on inequality "i".
2518 * "total" is the total number of variables (excluding the divs).
2519 * "v" is a temporary object that can be used during the calculations.
2520 * If "lb" is set, then a lower bound should be constructed.
2521 * If "ub" is set, then an upper bound should be constructed.
2523 * The calling function has already checked that the inequality does not
2524 * reference "div", but we still need to check that the inequality is
2525 * of the right form. We'll consider the case where we want to construct
2526 * a lower bound. The construction of upper bounds is similar.
2528 * Let "div" be of the form
2530 * q = floor((a + f(x))/d)
2532 * We essentially check if constraint "i" is of the form
2536 * so that we can use it to derive a lower bound on "div".
2537 * However, we allow a slightly more general form
2541 * with the condition that the coefficients of g(x) - f(x) are all
2543 * Rewriting this constraint as
2547 * adding a + f(x) to both sides and dividing by d, we obtain
2549 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2551 * Taking the floor on both sides, we obtain
2553 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2557 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2559 * In the case of an upper bound, we construct the constraint
2561 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2564 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2565 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2566 unsigned total
, isl_int v
, int lb
, int ub
)
2570 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2572 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2573 bmap
->div
[div
][1 + 1 + j
]);
2574 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2577 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2578 bmap
->div
[div
][1 + 1 + j
]);
2579 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2585 bmap
= isl_basic_map_cow(bmap
);
2586 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2588 int k
= isl_basic_map_alloc_inequality(bmap
);
2591 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2592 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2593 bmap
->div
[div
][1 + j
]);
2594 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2595 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2597 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2600 int k
= isl_basic_map_alloc_inequality(bmap
);
2603 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2604 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2605 bmap
->div
[div
][1 + j
]);
2606 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2607 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2609 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2614 isl_basic_map_free(bmap
);
2618 /* This function is called right before "div" is eliminated from "bmap"
2619 * using Fourier-Motzkin.
2620 * Look through the constraints of "bmap" for constraints on the argument
2621 * of the integer division and use them to construct constraints on the
2622 * integer division itself. These constraints can then be combined
2623 * during the Fourier-Motzkin elimination.
2624 * Note that it is only useful to introduce lower bounds on "div"
2625 * if "bmap" already contains upper bounds on "div" as the newly
2626 * introduce lower bounds can then be combined with the pre-existing
2627 * upper bounds. Similarly for upper bounds.
2628 * We therefore first check if "bmap" contains any lower and/or upper bounds
2631 * It is interesting to note that the introduction of these constraints
2632 * can indeed lead to more accurate results, even when compared to
2633 * deriving constraints on the argument of "div" from constraints on "div".
2634 * Consider, for example, the set
2636 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2638 * The second constraint can be rewritten as
2640 * 2 * [(-i-2j+3)/4] + k >= 0
2642 * from which we can derive
2644 * -i - 2j + 3 >= -2k
2650 * Combined with the first constraint, we obtain
2652 * -3 <= 3 + 2k or k >= -3
2654 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2655 * the first constraint, we obtain
2657 * [(i + 2j)/4] >= [-3/4] = -1
2659 * Combining this constraint with the second constraint, we obtain
2663 static __isl_give isl_basic_map
*insert_bounds_on_div(
2664 __isl_take isl_basic_map
*bmap
, int div
)
2667 int check_lb
, check_ub
;
2674 if (isl_int_is_zero(bmap
->div
[div
][0]))
2677 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2679 return isl_basic_map_free(bmap
);
2683 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2684 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2691 if (!check_lb
&& !check_ub
)
2696 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2697 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2700 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2701 check_lb
, check_ub
);
2709 /* Remove all divs (recursively) involving any of the given dimensions
2710 * in their definitions.
2712 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2713 __isl_take isl_basic_map
*bmap
,
2714 enum isl_dim_type type
, unsigned first
, unsigned n
)
2718 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2719 return isl_basic_map_free(bmap
);
2720 first
+= isl_basic_map_offset(bmap
, type
);
2722 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2725 involves
= div_involves_vars(bmap
, i
, first
, n
);
2727 return isl_basic_map_free(bmap
);
2730 bmap
= insert_bounds_on_div(bmap
, i
);
2731 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2740 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2741 __isl_take isl_basic_set
*bset
,
2742 enum isl_dim_type type
, unsigned first
, unsigned n
)
2744 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2747 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2748 enum isl_dim_type type
, unsigned first
, unsigned n
)
2757 map
= isl_map_cow(map
);
2761 for (i
= 0; i
< map
->n
; ++i
) {
2762 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2773 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2774 enum isl_dim_type type
, unsigned first
, unsigned n
)
2776 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2780 /* Does the description of "bmap" depend on the specified dimensions?
2781 * We also check whether the dimensions appear in any of the div definitions.
2782 * In principle there is no need for this check. If the dimensions appear
2783 * in a div definition, they also appear in the defining constraints of that
2786 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2787 enum isl_dim_type type
, unsigned first
, unsigned n
)
2791 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2792 return isl_bool_error
;
2794 first
+= isl_basic_map_offset(bmap
, type
);
2795 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2796 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2797 return isl_bool_true
;
2798 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2799 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2800 return isl_bool_true
;
2801 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2802 if (isl_int_is_zero(bmap
->div
[i
][0]))
2804 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2805 return isl_bool_true
;
2808 return isl_bool_false
;
2811 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2812 enum isl_dim_type type
, unsigned first
, unsigned n
)
2816 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2817 return isl_bool_error
;
2819 for (i
= 0; i
< map
->n
; ++i
) {
2820 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2822 if (involves
< 0 || involves
)
2826 return isl_bool_false
;
2829 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2830 enum isl_dim_type type
, unsigned first
, unsigned n
)
2832 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2835 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2836 enum isl_dim_type type
, unsigned first
, unsigned n
)
2838 return isl_map_involves_dims(set
, type
, first
, n
);
2841 /* Drop all constraints in bmap that involve any of the dimensions
2842 * first to first+n-1.
2843 * This function only performs the actual removal of constraints.
2845 * This function should not call finalize since it is used by
2846 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2848 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2849 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2856 bmap
= isl_basic_map_cow(bmap
);
2861 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2862 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2864 isl_basic_map_drop_equality(bmap
, i
);
2867 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2868 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2870 isl_basic_map_drop_inequality(bmap
, i
);
2876 /* Drop all constraints in bset that involve any of the dimensions
2877 * first to first+n-1.
2878 * This function only performs the actual removal of constraints.
2880 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2881 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2883 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2886 /* Drop all constraints in bmap that do not involve any of the dimensions
2887 * first to first + n - 1 of the given type.
2889 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2890 __isl_take isl_basic_map
*bmap
,
2891 enum isl_dim_type type
, unsigned first
, unsigned n
)
2896 isl_space
*space
= isl_basic_map_get_space(bmap
);
2897 isl_basic_map_free(bmap
);
2898 return isl_basic_map_universe(space
);
2900 bmap
= isl_basic_map_cow(bmap
);
2904 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2905 return isl_basic_map_free(bmap
);
2907 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2909 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2910 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2912 isl_basic_map_drop_equality(bmap
, i
);
2915 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2916 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2918 isl_basic_map_drop_inequality(bmap
, i
);
2921 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2925 /* Drop all constraints in bset that do not involve any of the dimensions
2926 * first to first + n - 1 of the given type.
2928 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2929 __isl_take isl_basic_set
*bset
,
2930 enum isl_dim_type type
, unsigned first
, unsigned n
)
2932 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2936 /* Drop all constraints in bmap that involve any of the dimensions
2937 * first to first + n - 1 of the given type.
2939 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2940 __isl_take isl_basic_map
*bmap
,
2941 enum isl_dim_type type
, unsigned first
, unsigned n
)
2948 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2949 return isl_basic_map_free(bmap
);
2951 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2952 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2953 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2954 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2958 /* Drop all constraints in bset that involve any of the dimensions
2959 * first to first + n - 1 of the given type.
2961 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2962 __isl_take isl_basic_set
*bset
,
2963 enum isl_dim_type type
, unsigned first
, unsigned n
)
2965 return isl_basic_map_drop_constraints_involving_dims(bset
,
2969 /* Drop constraints from "map" by applying "drop" to each basic map.
2971 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2972 enum isl_dim_type type
, unsigned first
, unsigned n
,
2973 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2974 enum isl_dim_type type
, unsigned first
, unsigned n
))
2978 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2979 return isl_map_free(map
);
2981 map
= isl_map_cow(map
);
2985 for (i
= 0; i
< map
->n
; ++i
) {
2986 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2988 return isl_map_free(map
);
2992 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2997 /* Drop all constraints in map that involve any of the dimensions
2998 * first to first + n - 1 of the given type.
3000 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3001 __isl_take isl_map
*map
,
3002 enum isl_dim_type type
, unsigned first
, unsigned n
)
3006 return drop_constraints(map
, type
, first
, n
,
3007 &isl_basic_map_drop_constraints_involving_dims
);
3010 /* Drop all constraints in "map" that do not involve any of the dimensions
3011 * first to first + n - 1 of the given type.
3013 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3014 __isl_take isl_map
*map
,
3015 enum isl_dim_type type
, unsigned first
, unsigned n
)
3018 isl_space
*space
= isl_map_get_space(map
);
3020 return isl_map_universe(space
);
3022 return drop_constraints(map
, type
, first
, n
,
3023 &isl_basic_map_drop_constraints_not_involving_dims
);
3026 /* Drop all constraints in set that involve any of the dimensions
3027 * first to first + n - 1 of the given type.
3029 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3030 __isl_take isl_set
*set
,
3031 enum isl_dim_type type
, unsigned first
, unsigned n
)
3033 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3036 /* Drop all constraints in "set" that do not involve any of the dimensions
3037 * first to first + n - 1 of the given type.
3039 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3040 __isl_take isl_set
*set
,
3041 enum isl_dim_type type
, unsigned first
, unsigned n
)
3043 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3046 /* Does local variable "div" of "bmap" have a complete explicit representation?
3047 * Having a complete explicit representation requires not only
3048 * an explicit representation, but also that all local variables
3049 * that appear in this explicit representation in turn have
3050 * a complete explicit representation.
3052 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3055 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3058 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3059 if (marked
< 0 || marked
)
3060 return isl_bool_not(marked
);
3062 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3065 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3067 known
= isl_basic_map_div_is_known(bmap
, i
);
3068 if (known
< 0 || !known
)
3072 return isl_bool_true
;
3075 /* Remove all divs that are unknown or defined in terms of unknown divs.
3077 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3078 __isl_take isl_basic_map
*bmap
)
3085 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3086 if (isl_basic_map_div_is_known(bmap
, i
))
3088 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3097 /* Remove all divs that are unknown or defined in terms of unknown divs.
3099 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3100 __isl_take isl_basic_set
*bset
)
3102 return isl_basic_map_remove_unknown_divs(bset
);
3105 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3114 map
= isl_map_cow(map
);
3118 for (i
= 0; i
< map
->n
; ++i
) {
3119 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3129 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3131 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3134 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3135 __isl_take isl_basic_set
*bset
,
3136 enum isl_dim_type type
, unsigned first
, unsigned n
)
3138 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3139 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3140 return bset_from_bmap(bmap
);
3143 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3144 enum isl_dim_type type
, unsigned first
, unsigned n
)
3151 map
= isl_map_cow(map
);
3152 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3153 return isl_map_free(map
);
3155 for (i
= 0; i
< map
->n
; ++i
) {
3156 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3157 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3161 map
= isl_map_drop(map
, type
, first
, n
);
3168 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3169 enum isl_dim_type type
, unsigned first
, unsigned n
)
3171 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3175 /* Project out n inputs starting at first using Fourier-Motzkin */
3176 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3177 unsigned first
, unsigned n
)
3179 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3182 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3183 FILE *out
, int indent
)
3188 fprintf(out
, "null basic set\n");
3192 fprintf(out
, "%*s", indent
, "");
3193 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3194 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3195 bset
->extra
, bset
->flags
);
3197 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3198 p
= isl_printer_set_dump(p
, 1);
3199 p
= isl_printer_set_indent(p
, indent
);
3200 p
= isl_printer_start_line(p
);
3201 p
= isl_printer_print_basic_set(p
, bset
);
3202 p
= isl_printer_end_line(p
);
3203 isl_printer_free(p
);
3206 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3207 FILE *out
, int indent
)
3212 fprintf(out
, "null basic map\n");
3216 fprintf(out
, "%*s", indent
, "");
3217 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3218 "flags: %x, n_name: %d\n",
3220 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3221 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3223 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3224 p
= isl_printer_set_dump(p
, 1);
3225 p
= isl_printer_set_indent(p
, indent
);
3226 p
= isl_printer_start_line(p
);
3227 p
= isl_printer_print_basic_map(p
, bmap
);
3228 p
= isl_printer_end_line(p
);
3229 isl_printer_free(p
);
3232 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3239 total
= isl_basic_map_total_dim(bmap
);
3240 if (pos
>= bmap
->n_ineq
)
3241 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3242 "invalid position", return isl_basic_map_free(bmap
));
3243 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3244 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3245 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3246 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3250 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3253 if (isl_space_check_is_set(space
) < 0)
3255 return isl_map_alloc_space(space
, n
, flags
);
3257 isl_space_free(space
);
3261 /* Make sure "map" has room for at least "n" more basic maps.
3263 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3266 struct isl_map
*grown
= NULL
;
3270 isl_assert(map
->ctx
, n
>= 0, goto error
);
3271 if (map
->n
+ n
<= map
->size
)
3273 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3276 for (i
= 0; i
< map
->n
; ++i
) {
3277 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3285 isl_map_free(grown
);
3290 /* Make sure "set" has room for at least "n" more basic sets.
3292 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3294 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3297 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3299 return isl_map_from_basic_map(bset
);
3302 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3304 struct isl_map
*map
;
3309 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3310 return isl_map_add_basic_map(map
, bmap
);
3313 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3314 __isl_take isl_basic_set
*bset
)
3316 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3317 bset_to_bmap(bset
)));
3320 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3322 return isl_map_free(set
);
3325 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3330 fprintf(out
, "null set\n");
3334 fprintf(out
, "%*s", indent
, "");
3335 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3336 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3338 for (i
= 0; i
< set
->n
; ++i
) {
3339 fprintf(out
, "%*s", indent
, "");
3340 fprintf(out
, "basic set %d:\n", i
);
3341 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3345 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3350 fprintf(out
, "null map\n");
3354 fprintf(out
, "%*s", indent
, "");
3355 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3356 "flags: %x, n_name: %d\n",
3357 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3358 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3359 for (i
= 0; i
< map
->n
; ++i
) {
3360 fprintf(out
, "%*s", indent
, "");
3361 fprintf(out
, "basic map %d:\n", i
);
3362 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3366 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3367 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3369 struct isl_basic_map
*bmap_domain
;
3371 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3374 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3375 isl_assert(bset
->ctx
,
3376 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3378 bmap
= isl_basic_map_cow(bmap
);
3381 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3382 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3383 bmap_domain
= isl_basic_map_from_domain(bset
);
3384 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3386 bmap
= isl_basic_map_simplify(bmap
);
3387 return isl_basic_map_finalize(bmap
);
3389 isl_basic_map_free(bmap
);
3390 isl_basic_set_free(bset
);
3394 /* Check that the space of "bset" is the same as that of the range of "bmap".
3396 static isl_stat
isl_basic_map_check_compatible_range(
3397 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3401 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3403 return isl_stat_error
;
3405 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3406 "incompatible spaces", return isl_stat_error
);
3411 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3412 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3414 struct isl_basic_map
*bmap_range
;
3416 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3419 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3420 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3423 if (isl_basic_set_plain_is_universe(bset
)) {
3424 isl_basic_set_free(bset
);
3428 bmap
= isl_basic_map_cow(bmap
);
3431 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3432 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3433 bmap_range
= bset_to_bmap(bset
);
3434 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3436 bmap
= isl_basic_map_simplify(bmap
);
3437 return isl_basic_map_finalize(bmap
);
3439 isl_basic_map_free(bmap
);
3440 isl_basic_set_free(bset
);
3444 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3445 __isl_keep isl_vec
*vec
)
3452 return isl_bool_error
;
3454 total
= 1 + isl_basic_map_total_dim(bmap
);
3455 if (total
!= vec
->size
)
3456 return isl_bool_false
;
3460 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3461 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3462 if (!isl_int_is_zero(s
)) {
3464 return isl_bool_false
;
3468 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3469 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3470 if (isl_int_is_neg(s
)) {
3472 return isl_bool_false
;
3478 return isl_bool_true
;
3481 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3482 __isl_keep isl_vec
*vec
)
3484 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3487 __isl_give isl_basic_map
*isl_basic_map_intersect(
3488 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3490 struct isl_vec
*sample
= NULL
;
3492 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3494 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3495 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3496 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3497 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3498 return isl_basic_map_intersect(bmap2
, bmap1
);
3500 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3501 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3502 isl_assert(bmap1
->ctx
,
3503 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3505 if (isl_basic_map_plain_is_empty(bmap1
)) {
3506 isl_basic_map_free(bmap2
);
3509 if (isl_basic_map_plain_is_empty(bmap2
)) {
3510 isl_basic_map_free(bmap1
);
3514 if (bmap1
->sample
&&
3515 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3516 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3517 sample
= isl_vec_copy(bmap1
->sample
);
3518 else if (bmap2
->sample
&&
3519 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3520 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3521 sample
= isl_vec_copy(bmap2
->sample
);
3523 bmap1
= isl_basic_map_cow(bmap1
);
3526 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3527 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3528 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3531 isl_vec_free(sample
);
3533 isl_vec_free(bmap1
->sample
);
3534 bmap1
->sample
= sample
;
3537 bmap1
= isl_basic_map_simplify(bmap1
);
3538 return isl_basic_map_finalize(bmap1
);
3541 isl_vec_free(sample
);
3542 isl_basic_map_free(bmap1
);
3543 isl_basic_map_free(bmap2
);
3547 struct isl_basic_set
*isl_basic_set_intersect(
3548 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3550 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3551 bset_to_bmap(bset2
)));
3554 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3555 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3557 return isl_basic_set_intersect(bset1
, bset2
);
3560 /* Special case of isl_map_intersect, where both map1 and map2
3561 * are convex, without any divs and such that either map1 or map2
3562 * contains a single constraint. This constraint is then simply
3563 * added to the other map.
3565 static __isl_give isl_map
*map_intersect_add_constraint(
3566 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3568 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3569 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3570 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3571 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3573 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3574 return isl_map_intersect(map2
, map1
);
3576 map1
= isl_map_cow(map1
);
3579 if (isl_map_plain_is_empty(map1
)) {
3583 if (map2
->p
[0]->n_eq
== 1)
3584 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3586 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3587 map2
->p
[0]->ineq
[0]);
3589 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3590 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3594 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3595 isl_basic_map_free(map1
->p
[0]);
3601 map1
= isl_map_unmark_normalized(map1
);
3609 /* map2 may be either a parameter domain or a map living in the same
3612 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3613 __isl_take isl_map
*map2
)
3623 if ((isl_map_plain_is_empty(map1
) ||
3624 isl_map_plain_is_universe(map2
)) &&
3625 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3629 if ((isl_map_plain_is_empty(map2
) ||
3630 isl_map_plain_is_universe(map1
)) &&
3631 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3636 if (map1
->n
== 1 && map2
->n
== 1 &&
3637 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3638 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3639 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3640 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3641 return map_intersect_add_constraint(map1
, map2
);
3643 equal
= isl_map_plain_is_equal(map1
, map2
);
3651 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3652 isl_space_dim(map2
->dim
, isl_dim_param
))
3653 isl_assert(map1
->ctx
,
3654 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3656 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3657 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3658 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3660 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3661 map1
->n
* map2
->n
, flags
);
3664 for (i
= 0; i
< map1
->n
; ++i
)
3665 for (j
= 0; j
< map2
->n
; ++j
) {
3666 struct isl_basic_map
*part
;
3667 part
= isl_basic_map_intersect(
3668 isl_basic_map_copy(map1
->p
[i
]),
3669 isl_basic_map_copy(map2
->p
[j
]));
3670 if (isl_basic_map_is_empty(part
) < 0)
3671 part
= isl_basic_map_free(part
);
3672 result
= isl_map_add_basic_map(result
, part
);
3685 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3686 __isl_take isl_map
*map2
)
3690 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3691 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3692 "spaces don't match", goto error
);
3693 return map_intersect_internal(map1
, map2
);
3700 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3701 __isl_take isl_map
*map2
)
3703 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3706 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3708 return set_from_map(isl_map_intersect(set_to_map(set1
),
3712 /* map_intersect_internal accepts intersections
3713 * with parameter domains, so we can just call that function.
3715 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3716 __isl_take isl_set
*params
)
3718 return map_intersect_internal(map
, params
);
3721 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3722 __isl_take isl_map
*map2
)
3724 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3727 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3728 __isl_take isl_set
*params
)
3730 return isl_map_intersect_params(set
, params
);
3733 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3736 unsigned pos
, n1
, n2
;
3740 bmap
= isl_basic_map_cow(bmap
);
3743 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3744 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3745 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3746 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3747 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3748 return isl_basic_map_reset_space(bmap
, space
);
3751 static __isl_give isl_basic_map
*basic_map_space_reset(
3752 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3758 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3761 space
= isl_basic_map_get_space(bmap
);
3762 space
= isl_space_reset(space
, type
);
3763 bmap
= isl_basic_map_reset_space(bmap
, space
);
3767 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3768 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3769 unsigned pos
, unsigned n
)
3771 isl_bool rational
, is_empty
;
3772 isl_space
*res_space
;
3773 struct isl_basic_map
*res
;
3774 struct isl_dim_map
*dim_map
;
3775 unsigned total
, off
;
3776 enum isl_dim_type t
;
3779 return basic_map_space_reset(bmap
, type
);
3781 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3783 return isl_basic_map_free(bmap
);
3784 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3787 return isl_basic_map_free(bmap
);
3789 isl_basic_map_free(bmap
);
3790 return isl_basic_map_empty(res_space
);
3793 total
= isl_basic_map_total_dim(bmap
) + n
;
3794 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3796 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3798 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3800 unsigned size
= isl_basic_map_dim(bmap
, t
);
3801 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3803 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3804 pos
, size
- pos
, off
+ pos
+ n
);
3806 off
+= isl_space_dim(res_space
, t
);
3808 isl_dim_map_div(dim_map
, bmap
, off
);
3810 res
= isl_basic_map_alloc_space(res_space
,
3811 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3812 rational
= isl_basic_map_is_rational(bmap
);
3814 res
= isl_basic_map_free(res
);
3816 res
= isl_basic_map_set_rational(res
);
3817 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3818 return isl_basic_map_finalize(res
);
3821 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3822 __isl_take isl_basic_set
*bset
,
3823 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3825 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3828 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3829 enum isl_dim_type type
, unsigned n
)
3833 return isl_basic_map_insert_dims(bmap
, type
,
3834 isl_basic_map_dim(bmap
, type
), n
);
3837 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3838 enum isl_dim_type type
, unsigned n
)
3842 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3843 return isl_basic_map_add_dims(bset
, type
, n
);
3845 isl_basic_set_free(bset
);
3849 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3850 enum isl_dim_type type
)
3854 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3857 space
= isl_map_get_space(map
);
3858 space
= isl_space_reset(space
, type
);
3859 map
= isl_map_reset_space(map
, space
);
3863 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3864 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3869 return map_space_reset(map
, type
);
3871 map
= isl_map_cow(map
);
3875 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3879 for (i
= 0; i
< map
->n
; ++i
) {
3880 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3891 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3892 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3894 return isl_map_insert_dims(set
, type
, pos
, n
);
3897 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3898 enum isl_dim_type type
, unsigned n
)
3902 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3905 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3906 enum isl_dim_type type
, unsigned n
)
3910 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3911 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3917 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3918 __isl_take isl_basic_map
*bmap
,
3919 enum isl_dim_type dst_type
, unsigned dst_pos
,
3920 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3922 struct isl_dim_map
*dim_map
;
3923 struct isl_basic_map
*res
;
3924 enum isl_dim_type t
;
3925 unsigned total
, off
;
3930 bmap
= isl_basic_map_reset(bmap
, src_type
);
3931 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3935 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3936 return isl_basic_map_free(bmap
);
3938 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3941 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3943 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3944 pos(bmap
->dim
, src_type
) + src_pos
+
3945 ((src_type
< dst_type
) ? n
: 0)) {
3946 bmap
= isl_basic_map_cow(bmap
);
3950 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3951 src_type
, src_pos
, n
);
3955 bmap
= isl_basic_map_finalize(bmap
);
3960 total
= isl_basic_map_total_dim(bmap
);
3961 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3964 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3965 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3966 if (t
== dst_type
) {
3967 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3970 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3973 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3974 dst_pos
, size
- dst_pos
, off
);
3975 off
+= size
- dst_pos
;
3976 } else if (t
== src_type
) {
3977 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3980 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3981 src_pos
+ n
, size
- src_pos
- n
, off
);
3982 off
+= size
- src_pos
- n
;
3984 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3988 isl_dim_map_div(dim_map
, bmap
, off
);
3990 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3991 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3992 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3996 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3997 src_type
, src_pos
, n
);
4001 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4002 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4003 bmap
= isl_basic_map_finalize(bmap
);
4007 isl_basic_map_free(bmap
);
4011 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4012 enum isl_dim_type dst_type
, unsigned dst_pos
,
4013 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4015 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4016 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4017 src_type
, src_pos
, n
);
4018 return bset_from_bmap(bmap
);
4021 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4022 enum isl_dim_type dst_type
, unsigned dst_pos
,
4023 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4027 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4028 return set_from_map(isl_map_move_dims(set_to_map(set
),
4029 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4035 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4036 enum isl_dim_type dst_type
, unsigned dst_pos
,
4037 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4042 map
= isl_map_reset(map
, src_type
);
4043 map
= isl_map_reset(map
, dst_type
);
4047 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4048 return isl_map_free(map
);
4050 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4053 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4055 map
= isl_map_cow(map
);
4059 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4063 for (i
= 0; i
< map
->n
; ++i
) {
4064 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4066 src_type
, src_pos
, n
);
4077 /* Move the specified dimensions to the last columns right before
4078 * the divs. Don't change the dimension specification of bmap.
4079 * That's the responsibility of the caller.
4081 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4082 enum isl_dim_type type
, unsigned first
, unsigned n
)
4084 struct isl_dim_map
*dim_map
;
4085 struct isl_basic_map
*res
;
4086 enum isl_dim_type t
;
4087 unsigned total
, off
;
4091 if (pos(bmap
->dim
, type
) + first
+ n
==
4092 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4095 total
= isl_basic_map_total_dim(bmap
);
4096 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4099 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4100 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4102 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4105 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4106 first
, n
, total
- bmap
->n_div
- n
);
4107 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4108 first
+ n
, size
- (first
+ n
), off
);
4109 off
+= size
- (first
+ n
);
4111 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4115 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4117 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4118 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4119 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4123 /* Insert "n" rows in the divs of "bmap".
4125 * The number of columns is not changed, which means that the last
4126 * dimensions of "bmap" are being reintepreted as the new divs.
4127 * The space of "bmap" is not adjusted, however, which means
4128 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4129 * from the space of "bmap" is the responsibility of the caller.
4131 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4139 bmap
= isl_basic_map_cow(bmap
);
4143 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4144 old
= bmap
->block2
.data
;
4145 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4146 (bmap
->extra
+ n
) * (1 + row_size
));
4147 if (!bmap
->block2
.data
)
4148 return isl_basic_map_free(bmap
);
4149 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4151 return isl_basic_map_free(bmap
);
4152 for (i
= 0; i
< n
; ++i
) {
4153 new_div
[i
] = bmap
->block2
.data
+
4154 (bmap
->extra
+ i
) * (1 + row_size
);
4155 isl_seq_clr(new_div
[i
], 1 + row_size
);
4157 for (i
= 0; i
< bmap
->extra
; ++i
)
4158 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4160 bmap
->div
= new_div
;
4167 /* Drop constraints from "bmap" that only involve the variables
4168 * of "type" in the range [first, first + n] that are not related
4169 * to any of the variables outside that interval.
4170 * These constraints cannot influence the values for the variables
4171 * outside the interval, except in case they cause "bmap" to be empty.
4172 * Only drop the constraints if "bmap" is known to be non-empty.
4174 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4175 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4176 unsigned first
, unsigned n
)
4180 unsigned dim
, n_div
;
4183 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4185 return isl_basic_map_free(bmap
);
4189 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4190 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4191 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4193 return isl_basic_map_free(bmap
);
4194 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4195 for (i
= 0; i
< first
; ++i
)
4197 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4200 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4205 /* Turn the n dimensions of type type, starting at first
4206 * into existentially quantified variables.
4208 * If a subset of the projected out variables are unrelated
4209 * to any of the variables that remain, then the constraints
4210 * involving this subset are simply dropped first.
4212 __isl_give isl_basic_map
*isl_basic_map_project_out(
4213 __isl_take isl_basic_map
*bmap
,
4214 enum isl_dim_type type
, unsigned first
, unsigned n
)
4219 return basic_map_space_reset(bmap
, type
);
4220 if (type
== isl_dim_div
)
4221 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4222 "cannot project out existentially quantified variables",
4223 return isl_basic_map_free(bmap
));
4225 empty
= isl_basic_map_plain_is_empty(bmap
);
4227 return isl_basic_map_free(bmap
);
4229 bmap
= isl_basic_map_set_to_empty(bmap
);
4231 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4235 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4236 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4238 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4239 return isl_basic_map_free(bmap
);
4241 bmap
= move_last(bmap
, type
, first
, n
);
4242 bmap
= isl_basic_map_cow(bmap
);
4243 bmap
= insert_div_rows(bmap
, n
);
4247 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4250 bmap
= isl_basic_map_simplify(bmap
);
4251 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4252 return isl_basic_map_finalize(bmap
);
4254 isl_basic_map_free(bmap
);
4258 /* Turn the n dimensions of type type, starting at first
4259 * into existentially quantified variables.
4261 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4262 enum isl_dim_type type
, unsigned first
, unsigned n
)
4264 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4268 /* Turn the n dimensions of type type, starting at first
4269 * into existentially quantified variables.
4271 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4272 enum isl_dim_type type
, unsigned first
, unsigned n
)
4277 return map_space_reset(map
, type
);
4279 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4280 return isl_map_free(map
);
4282 map
= isl_map_cow(map
);
4286 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4290 for (i
= 0; i
< map
->n
; ++i
) {
4291 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4302 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4303 * into existentially quantified variables.
4305 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4306 enum isl_dim_type type
, unsigned first
, unsigned n
)
4310 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4311 return isl_map_free(map
);
4312 dim
= isl_map_dim(map
, type
);
4313 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4314 map
= isl_map_project_out(map
, type
, 0, first
);
4318 /* Turn the n dimensions of type type, starting at first
4319 * into existentially quantified variables.
4321 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4322 enum isl_dim_type type
, unsigned first
, unsigned n
)
4324 return set_from_map(isl_map_project_out(set_to_map(set
),
4328 /* Return a map that projects the elements in "set" onto their
4329 * "n" set dimensions starting at "first".
4330 * "type" should be equal to isl_dim_set.
4332 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4333 enum isl_dim_type type
, unsigned first
, unsigned n
)
4338 if (type
!= isl_dim_set
)
4339 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4340 "only set dimensions can be projected out", goto error
);
4341 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4342 return isl_set_free(set
);
4344 map
= isl_map_from_domain(set
);
4345 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4346 for (i
= 0; i
< n
; ++i
)
4347 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4355 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4360 for (i
= 0; i
< n
; ++i
) {
4361 j
= isl_basic_map_alloc_div(bmap
);
4364 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4368 isl_basic_map_free(bmap
);
4372 struct isl_basic_map
*isl_basic_map_apply_range(
4373 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4375 isl_space
*space_result
= NULL
;
4376 struct isl_basic_map
*bmap
;
4377 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4378 struct isl_dim_map
*dim_map1
, *dim_map2
;
4380 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4382 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4383 bmap2
->dim
, isl_dim_in
))
4384 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4385 "spaces don't match", goto error
);
4387 space_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4388 isl_space_copy(bmap2
->dim
));
4390 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4391 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4392 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4393 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4395 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4396 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4397 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4398 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4399 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4400 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4401 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4402 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4403 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4404 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4405 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4407 bmap
= isl_basic_map_alloc_space(space_result
,
4408 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4409 bmap1
->n_eq
+ bmap2
->n_eq
,
4410 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4411 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4412 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4413 bmap
= add_divs(bmap
, n
);
4414 bmap
= isl_basic_map_simplify(bmap
);
4415 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4416 return isl_basic_map_finalize(bmap
);
4418 isl_basic_map_free(bmap1
);
4419 isl_basic_map_free(bmap2
);
4423 struct isl_basic_set
*isl_basic_set_apply(
4424 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4429 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4432 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4435 isl_basic_set_free(bset
);
4436 isl_basic_map_free(bmap
);
4440 struct isl_basic_map
*isl_basic_map_apply_domain(
4441 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4443 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4445 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4446 bmap2
->dim
, isl_dim_in
))
4447 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4448 "spaces don't match", goto error
);
4450 bmap1
= isl_basic_map_reverse(bmap1
);
4451 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4452 return isl_basic_map_reverse(bmap1
);
4454 isl_basic_map_free(bmap1
);
4455 isl_basic_map_free(bmap2
);
4459 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4460 * A \cap B -> f(A) + f(B)
4462 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4463 __isl_take isl_basic_map
*bmap2
)
4465 unsigned n_in
, n_out
, nparam
, total
, pos
;
4466 struct isl_basic_map
*bmap
= NULL
;
4467 struct isl_dim_map
*dim_map1
, *dim_map2
;
4470 if (!bmap1
|| !bmap2
)
4473 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4476 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4477 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4478 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4480 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4481 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4482 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4483 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4484 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4485 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4486 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4487 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4488 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4489 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4490 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4492 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4493 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4494 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4495 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4496 for (i
= 0; i
< n_out
; ++i
) {
4497 int j
= isl_basic_map_alloc_equality(bmap
);
4500 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4501 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4502 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4503 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4505 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4506 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4507 bmap
= add_divs(bmap
, 2 * n_out
);
4509 bmap
= isl_basic_map_simplify(bmap
);
4510 return isl_basic_map_finalize(bmap
);
4512 isl_basic_map_free(bmap
);
4513 isl_basic_map_free(bmap1
);
4514 isl_basic_map_free(bmap2
);
4518 /* Given two maps A -> f(A) and B -> g(B), construct a map
4519 * A \cap B -> f(A) + f(B)
4521 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4522 __isl_take isl_map
*map2
)
4524 struct isl_map
*result
;
4530 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4532 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4533 map1
->n
* map2
->n
, 0);
4536 for (i
= 0; i
< map1
->n
; ++i
)
4537 for (j
= 0; j
< map2
->n
; ++j
) {
4538 struct isl_basic_map
*part
;
4539 part
= isl_basic_map_sum(
4540 isl_basic_map_copy(map1
->p
[i
]),
4541 isl_basic_map_copy(map2
->p
[j
]));
4542 if (isl_basic_map_is_empty(part
))
4543 isl_basic_map_free(part
);
4545 result
= isl_map_add_basic_map(result
, part
);
4558 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4559 __isl_take isl_set
*set2
)
4561 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4564 /* Given a basic map A -> f(A), construct A -> -f(A).
4566 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4571 bmap
= isl_basic_map_cow(bmap
);
4575 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4576 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4577 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4578 for (j
= 0; j
< n
; ++j
)
4579 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4580 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4581 for (j
= 0; j
< n
; ++j
)
4582 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4583 for (i
= 0; i
< bmap
->n_div
; ++i
)
4584 for (j
= 0; j
< n
; ++j
)
4585 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4586 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4587 return isl_basic_map_finalize(bmap
);
4590 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4592 return isl_basic_map_neg(bset
);
4595 /* Given a map A -> f(A), construct A -> -f(A).
4597 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4601 map
= isl_map_cow(map
);
4605 for (i
= 0; i
< map
->n
; ++i
) {
4606 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4617 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4619 return set_from_map(isl_map_neg(set_to_map(set
)));
4622 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4623 * A -> floor(f(A)/d).
4625 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4628 unsigned n_in
, n_out
, nparam
, total
, pos
;
4629 struct isl_basic_map
*result
= NULL
;
4630 struct isl_dim_map
*dim_map
;
4636 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4637 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4638 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4640 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4641 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4642 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4643 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4644 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4645 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4647 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4648 bmap
->n_div
+ n_out
,
4649 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4650 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4651 result
= add_divs(result
, n_out
);
4652 for (i
= 0; i
< n_out
; ++i
) {
4654 j
= isl_basic_map_alloc_inequality(result
);
4657 isl_seq_clr(result
->ineq
[j
], 1+total
);
4658 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4659 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4660 j
= isl_basic_map_alloc_inequality(result
);
4663 isl_seq_clr(result
->ineq
[j
], 1+total
);
4664 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4665 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4666 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4669 result
= isl_basic_map_simplify(result
);
4670 return isl_basic_map_finalize(result
);
4672 isl_basic_map_free(result
);
4676 /* Given a map A -> f(A) and an integer d, construct a map
4677 * A -> floor(f(A)/d).
4679 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4683 map
= isl_map_cow(map
);
4687 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4688 for (i
= 0; i
< map
->n
; ++i
) {
4689 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4693 map
= isl_map_unmark_normalized(map
);
4701 /* Given a map A -> f(A) and an integer d, construct a map
4702 * A -> floor(f(A)/d).
4704 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4705 __isl_take isl_val
*d
)
4709 if (!isl_val_is_int(d
))
4710 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4711 "expecting integer denominator", goto error
);
4712 map
= isl_map_floordiv(map
, d
->n
);
4721 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4728 i
= isl_basic_map_alloc_equality(bmap
);
4731 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4732 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4733 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4734 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4735 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4736 return isl_basic_map_finalize(bmap
);
4738 isl_basic_map_free(bmap
);
4742 /* Add a constraint to "bmap" expressing i_pos < o_pos
4744 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4751 i
= isl_basic_map_alloc_inequality(bmap
);
4754 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4755 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4756 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4757 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4758 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4759 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4760 return isl_basic_map_finalize(bmap
);
4762 isl_basic_map_free(bmap
);
4766 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4768 static __isl_give isl_basic_map
*var_less_or_equal(
4769 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4775 i
= isl_basic_map_alloc_inequality(bmap
);
4778 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4779 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4780 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4781 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4782 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4783 return isl_basic_map_finalize(bmap
);
4785 isl_basic_map_free(bmap
);
4789 /* Add a constraint to "bmap" expressing i_pos > o_pos
4791 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4798 i
= isl_basic_map_alloc_inequality(bmap
);
4801 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4802 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4803 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4804 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4805 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4806 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4807 return isl_basic_map_finalize(bmap
);
4809 isl_basic_map_free(bmap
);
4813 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4815 static __isl_give isl_basic_map
*var_more_or_equal(
4816 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4822 i
= isl_basic_map_alloc_inequality(bmap
);
4825 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4826 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4827 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4828 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4829 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4830 return isl_basic_map_finalize(bmap
);
4832 isl_basic_map_free(bmap
);
4836 __isl_give isl_basic_map
*isl_basic_map_equal(
4837 __isl_take isl_space
*space
, unsigned n_equal
)
4840 struct isl_basic_map
*bmap
;
4841 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
4844 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4845 bmap
= var_equal(bmap
, i
);
4846 return isl_basic_map_finalize(bmap
);
4849 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4851 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
4855 struct isl_basic_map
*bmap
;
4856 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4859 for (i
= 0; i
< pos
&& bmap
; ++i
)
4860 bmap
= var_equal(bmap
, i
);
4862 bmap
= var_less(bmap
, pos
);
4863 return isl_basic_map_finalize(bmap
);
4866 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4868 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4869 __isl_take isl_space
*space
, unsigned pos
)
4872 isl_basic_map
*bmap
;
4874 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4875 for (i
= 0; i
< pos
; ++i
)
4876 bmap
= var_equal(bmap
, i
);
4877 bmap
= var_less_or_equal(bmap
, pos
);
4878 return isl_basic_map_finalize(bmap
);
4881 /* Return a relation on "space" expressing i_pos > o_pos
4883 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
4887 struct isl_basic_map
*bmap
;
4888 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4891 for (i
= 0; i
< pos
&& bmap
; ++i
)
4892 bmap
= var_equal(bmap
, i
);
4894 bmap
= var_more(bmap
, pos
);
4895 return isl_basic_map_finalize(bmap
);
4898 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4900 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4901 __isl_take isl_space
*space
, unsigned pos
)
4904 isl_basic_map
*bmap
;
4906 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
4907 for (i
= 0; i
< pos
; ++i
)
4908 bmap
= var_equal(bmap
, i
);
4909 bmap
= var_more_or_equal(bmap
, pos
);
4910 return isl_basic_map_finalize(bmap
);
4913 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
4914 unsigned n
, int equal
)
4916 struct isl_map
*map
;
4919 if (n
== 0 && equal
)
4920 return isl_map_universe(space
);
4922 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4924 for (i
= 0; i
+ 1 < n
; ++i
)
4925 map
= isl_map_add_basic_map(map
,
4926 isl_basic_map_less_at(isl_space_copy(space
), i
));
4929 map
= isl_map_add_basic_map(map
,
4930 isl_basic_map_less_or_equal_at(space
, n
- 1));
4932 map
= isl_map_add_basic_map(map
,
4933 isl_basic_map_less_at(space
, n
- 1));
4935 isl_space_free(space
);
4940 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
4944 return map_lex_lte_first(space
, space
->n_out
, equal
);
4947 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4949 return map_lex_lte_first(dim
, n
, 0);
4952 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4954 return map_lex_lte_first(dim
, n
, 1);
4957 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4959 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4962 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4964 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4967 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
4968 unsigned n
, int equal
)
4970 struct isl_map
*map
;
4973 if (n
== 0 && equal
)
4974 return isl_map_universe(space
);
4976 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
4978 for (i
= 0; i
+ 1 < n
; ++i
)
4979 map
= isl_map_add_basic_map(map
,
4980 isl_basic_map_more_at(isl_space_copy(space
), i
));
4983 map
= isl_map_add_basic_map(map
,
4984 isl_basic_map_more_or_equal_at(space
, n
- 1));
4986 map
= isl_map_add_basic_map(map
,
4987 isl_basic_map_more_at(space
, n
- 1));
4989 isl_space_free(space
);
4994 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
4998 return map_lex_gte_first(space
, space
->n_out
, equal
);
5001 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5003 return map_lex_gte_first(dim
, n
, 0);
5006 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5008 return map_lex_gte_first(dim
, n
, 1);
5011 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5013 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5016 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5018 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5021 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5022 __isl_take isl_set
*set2
)
5025 map
= isl_map_lex_le(isl_set_get_space(set1
));
5026 map
= isl_map_intersect_domain(map
, set1
);
5027 map
= isl_map_intersect_range(map
, set2
);
5031 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5032 __isl_take isl_set
*set2
)
5035 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5036 map
= isl_map_intersect_domain(map
, set1
);
5037 map
= isl_map_intersect_range(map
, set2
);
5041 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5042 __isl_take isl_set
*set2
)
5045 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5046 map
= isl_map_intersect_domain(map
, set1
);
5047 map
= isl_map_intersect_range(map
, set2
);
5051 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5052 __isl_take isl_set
*set2
)
5055 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5056 map
= isl_map_intersect_domain(map
, set1
);
5057 map
= isl_map_intersect_range(map
, set2
);
5061 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5062 __isl_take isl_map
*map2
)
5065 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5066 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5067 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5071 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5072 __isl_take isl_map
*map2
)
5075 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5076 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5077 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5081 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5082 __isl_take isl_map
*map2
)
5085 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5086 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5087 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5091 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5092 __isl_take isl_map
*map2
)
5095 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5096 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5097 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5101 /* For the div d = floor(f/m) at position "div", add the constraint
5105 static __isl_give isl_basic_map
*add_upper_div_constraint(
5106 __isl_take isl_basic_map
*bmap
, unsigned div
)
5109 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5110 unsigned n_div
, pos
;
5112 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5114 return isl_basic_map_free(bmap
);
5116 i
= isl_basic_map_alloc_inequality(bmap
);
5118 return isl_basic_map_free(bmap
);
5119 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5120 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5125 /* For the div d = floor(f/m) at position "div", add the constraint
5127 * -(f-(m-1)) + m d >= 0
5129 static __isl_give isl_basic_map
*add_lower_div_constraint(
5130 __isl_take isl_basic_map
*bmap
, unsigned div
)
5133 int v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5134 unsigned n_div
, pos
;
5136 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5138 return isl_basic_map_free(bmap
);
5140 i
= isl_basic_map_alloc_inequality(bmap
);
5142 return isl_basic_map_free(bmap
);
5143 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5144 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5145 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5146 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5151 /* For the div d = floor(f/m) at position "pos", add the constraints
5154 * -(f-(m-1)) + m d >= 0
5156 * Note that the second constraint is the negation of
5160 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5161 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5163 bmap
= add_upper_div_constraint(bmap
, pos
);
5164 bmap
= add_lower_div_constraint(bmap
, pos
);
5168 /* For each known div d = floor(f/m), add the constraints
5171 * -(f-(m-1)) + m d >= 0
5173 * Remove duplicate constraints in case of some these div constraints
5174 * already appear in "bmap".
5176 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5177 __isl_take isl_basic_map
*bmap
)
5183 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5187 bmap
= add_known_div_constraints(bmap
);
5188 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5189 bmap
= isl_basic_map_finalize(bmap
);
5193 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5195 * In particular, if this div is of the form d = floor(f/m),
5196 * then add the constraint
5200 * if sign < 0 or the constraint
5202 * -(f-(m-1)) + m d >= 0
5206 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5207 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5210 return add_upper_div_constraint(bmap
, div
);
5212 return add_lower_div_constraint(bmap
, div
);
5215 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5216 __isl_take isl_basic_map
*bmap
)
5220 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5222 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5223 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5224 return bset_from_bmap(bmap
);
5225 bmap
= isl_basic_map_cow(bmap
);
5228 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5231 bmap
->extra
-= bmap
->n_div
;
5233 bmap
= isl_basic_map_finalize(bmap
);
5234 return bset_from_bmap(bmap
);
5236 isl_basic_map_free(bmap
);
5240 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5241 __isl_take isl_basic_set
*bset
)
5243 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5246 /* Replace each element in "list" by the result of applying
5247 * isl_basic_map_underlying_set to the element.
5249 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5250 __isl_take isl_basic_map_list
*list
)
5257 n
= isl_basic_map_list_n_basic_map(list
);
5258 for (i
= 0; i
< n
; ++i
) {
5259 isl_basic_map
*bmap
;
5260 isl_basic_set
*bset
;
5262 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5263 bset
= isl_basic_set_underlying_set(bmap
);
5264 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5270 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5271 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5273 struct isl_basic_map
*bmap
;
5274 struct isl_ctx
*ctx
;
5281 if (isl_basic_set_check_no_params(bset
) < 0 ||
5282 isl_basic_set_check_no_locals(bset
) < 0)
5284 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5286 if (like
->n_div
== 0) {
5287 isl_space
*space
= isl_basic_map_get_space(like
);
5288 isl_basic_map_free(like
);
5289 return isl_basic_map_reset_space(bset
, space
);
5291 bset
= isl_basic_set_cow(bset
);
5294 total
= bset
->dim
->n_out
+ bset
->extra
;
5295 bmap
= bset_to_bmap(bset
);
5296 isl_space_free(bmap
->dim
);
5297 bmap
->dim
= isl_space_copy(like
->dim
);
5300 bmap
->n_div
= like
->n_div
;
5301 bmap
->extra
+= like
->n_div
;
5305 ltotal
= total
- bmap
->extra
+ like
->extra
;
5308 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5309 bmap
->extra
* (1 + 1 + total
));
5310 if (isl_blk_is_error(bmap
->block2
))
5312 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5316 for (i
= 0; i
< bmap
->extra
; ++i
)
5317 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5318 for (i
= 0; i
< like
->n_div
; ++i
) {
5319 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5320 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5322 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5324 isl_basic_map_free(like
);
5325 bmap
= isl_basic_map_simplify(bmap
);
5326 bmap
= isl_basic_map_finalize(bmap
);
5329 isl_basic_map_free(like
);
5330 isl_basic_set_free(bset
);
5334 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5335 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5337 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5338 bset_to_bmap(like
)));
5341 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5345 map
= isl_map_cow(map
);
5348 map
->dim
= isl_space_cow(map
->dim
);
5352 for (i
= 1; i
< map
->n
; ++i
)
5353 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5355 for (i
= 0; i
< map
->n
; ++i
) {
5356 map
->p
[i
] = bset_to_bmap(
5357 isl_basic_map_underlying_set(map
->p
[i
]));
5362 map
->dim
= isl_space_underlying(map
->dim
, 0);
5364 isl_space_free(map
->dim
);
5365 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5369 return set_from_map(map
);
5375 /* Replace the space of "bmap" by "space".
5377 * If the space of "bmap" is identical to "space" (including the identifiers
5378 * of the input and output dimensions), then simply return the original input.
5380 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5381 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5384 isl_space
*bmap_space
;
5386 bmap_space
= isl_basic_map_peek_space(bmap
);
5387 equal
= isl_space_is_equal(bmap_space
, space
);
5388 if (equal
>= 0 && equal
)
5389 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5393 isl_space_free(space
);
5396 bmap
= isl_basic_map_cow(bmap
);
5397 if (!bmap
|| !space
)
5400 isl_space_free(bmap
->dim
);
5403 bmap
= isl_basic_map_finalize(bmap
);
5407 isl_basic_map_free(bmap
);
5408 isl_space_free(space
);
5412 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5413 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5415 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5419 /* Check that the total dimensions of "map" and "space" are the same.
5421 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5422 __isl_keep isl_space
*space
)
5424 unsigned dim1
, dim2
;
5427 return isl_stat_error
;
5428 dim1
= isl_map_dim(map
, isl_dim_all
);
5429 dim2
= isl_space_dim(space
, isl_dim_all
);
5432 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5433 "total dimensions do not match", return isl_stat_error
);
5436 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5437 __isl_take isl_space
*space
)
5441 map
= isl_map_cow(map
);
5445 for (i
= 0; i
< map
->n
; ++i
) {
5446 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5447 isl_space_copy(space
));
5451 isl_space_free(map
->dim
);
5457 isl_space_free(space
);
5461 /* Replace the space of "map" by "space", without modifying
5462 * the dimension of "map".
5464 * If the space of "map" is identical to "space" (including the identifiers
5465 * of the input and output dimensions), then simply return the original input.
5467 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5468 __isl_take isl_space
*space
)
5471 isl_space
*map_space
;
5473 map_space
= isl_map_peek_space(map
);
5474 equal
= isl_space_is_equal(map_space
, space
);
5475 if (equal
>= 0 && equal
)
5476 equal
= isl_space_has_equal_ids(map_space
, space
);
5480 isl_space_free(space
);
5483 if (check_map_space_equal_total_dim(map
, space
) < 0)
5485 return isl_map_reset_space(map
, space
);
5488 isl_space_free(space
);
5492 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5493 __isl_take isl_space
*dim
)
5495 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5498 /* Compute the parameter domain of the given basic set.
5500 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5506 is_params
= isl_basic_set_is_params(bset
);
5508 return isl_basic_set_free(bset
);
5512 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5513 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5514 space
= isl_basic_set_get_space(bset
);
5515 space
= isl_space_params(space
);
5516 bset
= isl_basic_set_reset_space(bset
, space
);
5520 /* Construct a zero-dimensional basic set with the given parameter domain.
5522 __isl_give isl_basic_set
*isl_basic_set_from_params(
5523 __isl_take isl_basic_set
*bset
)
5526 space
= isl_basic_set_get_space(bset
);
5527 space
= isl_space_set_from_params(space
);
5528 bset
= isl_basic_set_reset_space(bset
, space
);
5532 /* Compute the parameter domain of the given set.
5534 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5539 if (isl_set_is_params(set
))
5542 n
= isl_set_dim(set
, isl_dim_set
);
5543 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5544 space
= isl_set_get_space(set
);
5545 space
= isl_space_params(space
);
5546 set
= isl_set_reset_space(set
, space
);
5550 /* Construct a zero-dimensional set with the given parameter domain.
5552 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5555 space
= isl_set_get_space(set
);
5556 space
= isl_space_set_from_params(space
);
5557 set
= isl_set_reset_space(set
, space
);
5561 /* Compute the parameter domain of the given map.
5563 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5568 n
= isl_map_dim(map
, isl_dim_in
);
5569 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5570 n
= isl_map_dim(map
, isl_dim_out
);
5571 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5572 space
= isl_map_get_space(map
);
5573 space
= isl_space_params(space
);
5574 map
= isl_map_reset_space(map
, space
);
5578 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5585 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5587 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5588 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5590 return isl_basic_map_reset_space(bmap
, space
);
5593 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5596 return isl_bool_error
;
5597 return isl_space_may_be_set(bmap
->dim
);
5600 /* Is this basic map actually a set?
5601 * Users should never call this function. Outside of isl,
5602 * the type should indicate whether something is a set or a map.
5604 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5607 return isl_bool_error
;
5608 return isl_space_is_set(bmap
->dim
);
5611 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5615 is_set
= isl_basic_map_is_set(bmap
);
5620 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5622 isl_basic_map_free(bmap
);
5626 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5627 __isl_take isl_basic_map
*bmap
)
5631 isl_basic_map
*domain
;
5632 int nparam
, n_in
, n_out
;
5634 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5635 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5636 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5638 space
= isl_basic_map_get_space(bmap
);
5639 space
= isl_space_from_range(isl_space_domain(space
));
5640 domain
= isl_basic_map_universe(space
);
5642 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5643 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5644 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5646 for (i
= 0; i
< n_in
; ++i
)
5647 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5650 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5651 return isl_basic_map_finalize(bmap
);
5654 __isl_give isl_basic_map
*isl_basic_map_range_map(
5655 __isl_take isl_basic_map
*bmap
)
5659 isl_basic_map
*range
;
5660 int nparam
, n_in
, n_out
;
5662 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5663 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5664 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5666 space
= isl_basic_map_get_space(bmap
);
5667 space
= isl_space_from_range(isl_space_range(space
));
5668 range
= isl_basic_map_universe(space
);
5670 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5671 bmap
= isl_basic_map_apply_range(bmap
, range
);
5672 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5674 for (i
= 0; i
< n_out
; ++i
)
5675 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5678 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5679 return isl_basic_map_finalize(bmap
);
5682 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5686 return isl_space_may_be_set(map
->dim
);
5689 /* Is this map actually a set?
5690 * Users should never call this function. Outside of isl,
5691 * the type should indicate whether something is a set or a map.
5693 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5696 return isl_bool_error
;
5697 return isl_space_is_set(map
->dim
);
5700 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5704 struct isl_set
*set
;
5706 is_set
= isl_map_is_set(map
);
5710 return set_from_map(map
);
5712 map
= isl_map_cow(map
);
5716 set
= set_from_map(map
);
5717 set
->dim
= isl_space_range(set
->dim
);
5720 for (i
= 0; i
< map
->n
; ++i
) {
5721 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5725 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5726 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5733 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5737 map
= isl_map_cow(map
);
5741 map
->dim
= isl_space_domain_map(map
->dim
);
5744 for (i
= 0; i
< map
->n
; ++i
) {
5745 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5749 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5750 map
= isl_map_unmark_normalized(map
);
5757 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5760 isl_space
*range_dim
;
5762 map
= isl_map_cow(map
);
5766 range_dim
= isl_space_range(isl_map_get_space(map
));
5767 range_dim
= isl_space_from_range(range_dim
);
5768 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5769 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5772 for (i
= 0; i
< map
->n
; ++i
) {
5773 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5777 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5778 map
= isl_map_unmark_normalized(map
);
5785 /* Given a wrapped map of the form A[B -> C],
5786 * return the map A[B -> C] -> B.
5788 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5795 if (!isl_set_has_tuple_id(set
))
5796 return isl_map_domain_map(isl_set_unwrap(set
));
5798 id
= isl_set_get_tuple_id(set
);
5799 map
= isl_map_domain_map(isl_set_unwrap(set
));
5800 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5805 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5806 __isl_take isl_basic_set
*bset
)
5808 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5811 __isl_give isl_basic_map
*isl_basic_map_from_range(
5812 __isl_take isl_basic_set
*bset
)
5815 space
= isl_basic_set_get_space(bset
);
5816 space
= isl_space_from_range(space
);
5817 bset
= isl_basic_set_reset_space(bset
, space
);
5818 return bset_to_bmap(bset
);
5821 /* Create a relation with the given set as range.
5822 * The domain of the created relation is a zero-dimensional
5823 * flat anonymous space.
5825 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5828 space
= isl_set_get_space(set
);
5829 space
= isl_space_from_range(space
);
5830 set
= isl_set_reset_space(set
, space
);
5831 return set_to_map(set
);
5834 /* Create a relation with the given set as domain.
5835 * The range of the created relation is a zero-dimensional
5836 * flat anonymous space.
5838 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5840 return isl_map_reverse(isl_map_from_range(set
));
5843 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5844 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5846 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5849 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5850 __isl_take isl_set
*range
)
5852 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5855 /* Return a newly allocated isl_map with given space and flags and
5856 * room for "n" basic maps.
5857 * Make sure that all cached information is cleared.
5859 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5862 struct isl_map
*map
;
5867 isl_die(space
->ctx
, isl_error_internal
,
5868 "negative number of basic maps", goto error
);
5869 map
= isl_calloc(space
->ctx
, struct isl_map
,
5870 sizeof(struct isl_map
) +
5871 (n
- 1) * sizeof(struct isl_basic_map
*));
5875 map
->ctx
= space
->ctx
;
5876 isl_ctx_ref(map
->ctx
);
5884 isl_space_free(space
);
5888 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
5890 struct isl_basic_map
*bmap
;
5891 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
5892 bmap
= isl_basic_map_set_to_empty(bmap
);
5896 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
5898 struct isl_basic_set
*bset
;
5899 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
5900 bset
= isl_basic_set_set_to_empty(bset
);
5904 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
5906 struct isl_basic_map
*bmap
;
5907 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
5908 bmap
= isl_basic_map_finalize(bmap
);
5912 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
5914 struct isl_basic_set
*bset
;
5915 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
5916 bset
= isl_basic_set_finalize(bset
);
5920 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
5921 __isl_take isl_space
*space
)
5924 unsigned total
= isl_space_dim(space
, isl_dim_all
);
5925 isl_basic_map
*bmap
;
5927 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
5928 for (i
= 0; i
< total
; ++i
) {
5929 int k
= isl_basic_map_alloc_inequality(bmap
);
5932 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5933 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5937 isl_basic_map_free(bmap
);
5941 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
5942 __isl_take isl_space
*space
)
5944 return isl_basic_map_nat_universe(space
);
5947 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5949 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5952 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5954 return isl_map_nat_universe(dim
);
5957 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
5959 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5962 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
5964 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
5967 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
5969 struct isl_map
*map
;
5972 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5973 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
5977 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
5979 struct isl_set
*set
;
5982 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
5983 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
5987 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5990 struct isl_map
*dup
;
5994 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5995 for (i
= 0; i
< map
->n
; ++i
)
5996 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6000 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6001 __isl_take isl_basic_map
*bmap
)
6005 if (isl_basic_map_plain_is_empty(bmap
)) {
6006 isl_basic_map_free(bmap
);
6009 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6010 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6011 map
->p
[map
->n
] = bmap
;
6013 map
= isl_map_unmark_normalized(map
);
6019 isl_basic_map_free(bmap
);
6023 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6034 isl_ctx_deref(map
->ctx
);
6035 for (i
= 0; i
< map
->n
; ++i
)
6036 isl_basic_map_free(map
->p
[i
]);
6037 isl_space_free(map
->dim
);
6043 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6044 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6048 bmap
= isl_basic_map_cow(bmap
);
6049 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6050 j
= isl_basic_map_alloc_equality(bmap
);
6053 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6054 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6055 isl_int_set_si(bmap
->eq
[j
][0], value
);
6056 bmap
= isl_basic_map_simplify(bmap
);
6057 return isl_basic_map_finalize(bmap
);
6059 isl_basic_map_free(bmap
);
6063 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6064 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6068 bmap
= isl_basic_map_cow(bmap
);
6069 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6070 j
= isl_basic_map_alloc_equality(bmap
);
6073 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6074 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6075 isl_int_set(bmap
->eq
[j
][0], value
);
6076 bmap
= isl_basic_map_simplify(bmap
);
6077 return isl_basic_map_finalize(bmap
);
6079 isl_basic_map_free(bmap
);
6083 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6084 enum isl_dim_type type
, unsigned pos
, int value
)
6086 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6087 return isl_basic_map_free(bmap
);
6088 return isl_basic_map_fix_pos_si(bmap
,
6089 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6092 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6093 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6095 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6096 return isl_basic_map_free(bmap
);
6097 return isl_basic_map_fix_pos(bmap
,
6098 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6101 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6102 * to be equal to "v".
6104 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6105 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6109 if (!isl_val_is_int(v
))
6110 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6111 "expecting integer value", goto error
);
6112 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6114 pos
+= isl_basic_map_offset(bmap
, type
);
6115 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6119 isl_basic_map_free(bmap
);
6124 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6125 * to be equal to "v".
6127 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6128 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6130 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6133 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6134 enum isl_dim_type type
, unsigned pos
, int value
)
6136 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6140 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6141 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6143 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6147 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6148 unsigned input
, int value
)
6150 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6153 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6154 unsigned dim
, int value
)
6156 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6157 isl_dim_set
, dim
, value
));
6160 /* Remove the basic map at position "i" from "map" if this basic map
6161 * is (obviously) empty.
6163 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6170 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6172 return isl_map_free(map
);
6176 isl_basic_map_free(map
->p
[i
]);
6179 map
->p
[i
] = map
->p
[map
->n
];
6180 map
= isl_map_unmark_normalized(map
);
6187 /* Perform "fn" on each basic map of "map", where we may not be holding
6188 * the only reference to "map".
6189 * In particular, "fn" should be a semantics preserving operation
6190 * that we want to apply to all copies of "map". We therefore need
6191 * to be careful not to modify "map" in a way that breaks "map"
6192 * in case anything goes wrong.
6194 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6195 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6197 struct isl_basic_map
*bmap
;
6203 for (i
= map
->n
- 1; i
>= 0; --i
) {
6204 bmap
= isl_basic_map_copy(map
->p
[i
]);
6208 isl_basic_map_free(map
->p
[i
]);
6210 map
= remove_if_empty(map
, i
);
6221 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6222 enum isl_dim_type type
, unsigned pos
, int value
)
6226 map
= isl_map_cow(map
);
6227 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6228 return isl_map_free(map
);
6229 for (i
= map
->n
- 1; i
>= 0; --i
) {
6230 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6231 map
= remove_if_empty(map
, i
);
6235 map
= isl_map_unmark_normalized(map
);
6239 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6240 enum isl_dim_type type
, unsigned pos
, int value
)
6242 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6245 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6246 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6250 map
= isl_map_cow(map
);
6251 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6252 return isl_map_free(map
);
6253 for (i
= 0; i
< map
->n
; ++i
) {
6254 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6258 map
= isl_map_unmark_normalized(map
);
6265 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6266 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6268 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6271 /* Fix the value of the variable at position "pos" of type "type" of "map"
6272 * to be equal to "v".
6274 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6275 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6279 map
= isl_map_cow(map
);
6283 if (!isl_val_is_int(v
))
6284 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6285 "expecting integer value", goto error
);
6286 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6288 for (i
= map
->n
- 1; i
>= 0; --i
) {
6289 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6291 map
= remove_if_empty(map
, i
);
6295 map
= isl_map_unmark_normalized(map
);
6304 /* Fix the value of the variable at position "pos" of type "type" of "set"
6305 * to be equal to "v".
6307 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6308 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6310 return isl_map_fix_val(set
, type
, pos
, v
);
6313 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6314 unsigned input
, int value
)
6316 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6319 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6321 return set_from_map(isl_map_fix_si(set_to_map(set
),
6322 isl_dim_set
, dim
, value
));
6325 static __isl_give isl_basic_map
*basic_map_bound_si(
6326 __isl_take isl_basic_map
*bmap
,
6327 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6331 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6332 return isl_basic_map_free(bmap
);
6333 pos
+= isl_basic_map_offset(bmap
, type
);
6334 bmap
= isl_basic_map_cow(bmap
);
6335 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6336 j
= isl_basic_map_alloc_inequality(bmap
);
6339 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6341 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6342 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6344 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6345 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6347 bmap
= isl_basic_map_simplify(bmap
);
6348 return isl_basic_map_finalize(bmap
);
6350 isl_basic_map_free(bmap
);
6354 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6355 __isl_take isl_basic_map
*bmap
,
6356 enum isl_dim_type type
, unsigned pos
, int value
)
6358 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6361 /* Constrain the values of the given dimension to be no greater than "value".
6363 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6364 __isl_take isl_basic_map
*bmap
,
6365 enum isl_dim_type type
, unsigned pos
, int value
)
6367 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6370 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6371 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6375 map
= isl_map_cow(map
);
6376 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6377 return isl_map_free(map
);
6378 for (i
= 0; i
< map
->n
; ++i
) {
6379 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6380 type
, pos
, value
, upper
);
6384 map
= isl_map_unmark_normalized(map
);
6391 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6392 enum isl_dim_type type
, unsigned pos
, int value
)
6394 return map_bound_si(map
, type
, pos
, value
, 0);
6397 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6398 enum isl_dim_type type
, unsigned pos
, int value
)
6400 return map_bound_si(map
, type
, pos
, value
, 1);
6403 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6404 enum isl_dim_type type
, unsigned pos
, int value
)
6406 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6410 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6411 enum isl_dim_type type
, unsigned pos
, int value
)
6413 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6416 /* Bound the given variable of "bmap" from below (or above is "upper"
6417 * is set) to "value".
6419 static __isl_give isl_basic_map
*basic_map_bound(
6420 __isl_take isl_basic_map
*bmap
,
6421 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6425 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6426 return isl_basic_map_free(bmap
);
6427 pos
+= isl_basic_map_offset(bmap
, type
);
6428 bmap
= isl_basic_map_cow(bmap
);
6429 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6430 j
= isl_basic_map_alloc_inequality(bmap
);
6433 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6435 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6436 isl_int_set(bmap
->ineq
[j
][0], value
);
6438 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6439 isl_int_neg(bmap
->ineq
[j
][0], value
);
6441 bmap
= isl_basic_map_simplify(bmap
);
6442 return isl_basic_map_finalize(bmap
);
6444 isl_basic_map_free(bmap
);
6448 /* Bound the given variable of "map" from below (or above is "upper"
6449 * is set) to "value".
6451 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6452 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6456 map
= isl_map_cow(map
);
6457 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6458 return isl_map_free(map
);
6459 for (i
= map
->n
- 1; i
>= 0; --i
) {
6460 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6461 map
= remove_if_empty(map
, i
);
6465 map
= isl_map_unmark_normalized(map
);
6469 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6470 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6472 return map_bound(map
, type
, pos
, value
, 0);
6475 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6476 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6478 return map_bound(map
, type
, pos
, value
, 1);
6481 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6482 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6484 return isl_map_lower_bound(set
, type
, pos
, value
);
6487 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6488 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6490 return isl_map_upper_bound(set
, type
, pos
, value
);
6493 /* Force the values of the variable at position "pos" of type "type" of "set"
6494 * to be no smaller than "value".
6496 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6497 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6501 if (!isl_val_is_int(value
))
6502 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6503 "expecting integer value", goto error
);
6504 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6505 isl_val_free(value
);
6508 isl_val_free(value
);
6513 /* Force the values of the variable at position "pos" of type "type" of "set"
6514 * to be no greater than "value".
6516 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6517 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6521 if (!isl_val_is_int(value
))
6522 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6523 "expecting integer value", goto error
);
6524 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6525 isl_val_free(value
);
6528 isl_val_free(value
);
6533 /* Bound the given variable of "bset" from below (or above is "upper"
6534 * is set) to "value".
6536 static __isl_give isl_basic_set
*isl_basic_set_bound(
6537 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6538 isl_int value
, int upper
)
6540 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6541 type
, pos
, value
, upper
));
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_val(
6548 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6549 __isl_take isl_val
*value
, int upper
)
6553 if (!isl_val_is_int(value
))
6554 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
6555 "expecting integer value", goto error
);
6556 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
6557 isl_val_free(value
);
6560 isl_val_free(value
);
6561 isl_basic_set_free(bset
);
6565 /* Bound the given variable of "bset" from below to "value".
6567 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
6568 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6569 __isl_take isl_val
*value
)
6571 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
6574 /* Bound the given variable of "bset" from above to "value".
6576 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
6577 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6578 __isl_take isl_val
*value
)
6580 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
6583 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6587 map
= isl_map_cow(map
);
6591 map
->dim
= isl_space_reverse(map
->dim
);
6594 for (i
= 0; i
< map
->n
; ++i
) {
6595 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6599 map
= isl_map_unmark_normalized(map
);
6607 #define TYPE isl_pw_multi_aff
6609 #define SUFFIX _pw_multi_aff
6611 #define EMPTY isl_pw_multi_aff_empty
6613 #define ADD isl_pw_multi_aff_union_add
6614 #include "isl_map_lexopt_templ.c"
6616 /* Given a map "map", compute the lexicographically minimal
6617 * (or maximal) image element for each domain element in dom,
6618 * in the form of an isl_pw_multi_aff.
6619 * If "empty" is not NULL, then set *empty to those elements in dom that
6620 * do not have an image element.
6621 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6622 * should be computed over the domain of "map". "empty" is also NULL
6625 * We first compute the lexicographically minimal or maximal element
6626 * in the first basic map. This results in a partial solution "res"
6627 * and a subset "todo" of dom that still need to be handled.
6628 * We then consider each of the remaining maps in "map" and successively
6629 * update both "res" and "todo".
6630 * If "empty" is NULL, then the todo sets are not needed and therefore
6631 * also not computed.
6633 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6634 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6635 __isl_give isl_set
**empty
, unsigned flags
)
6639 isl_pw_multi_aff
*res
;
6642 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6643 if (!map
|| (!full
&& !dom
))
6646 if (isl_map_plain_is_empty(map
)) {
6651 return isl_pw_multi_aff_from_map(map
);
6654 res
= basic_map_partial_lexopt_pw_multi_aff(
6655 isl_basic_map_copy(map
->p
[0]),
6656 isl_set_copy(dom
), empty
, flags
);
6660 for (i
= 1; i
< map
->n
; ++i
) {
6661 isl_pw_multi_aff
*res_i
;
6663 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6664 isl_basic_map_copy(map
->p
[i
]),
6665 isl_set_copy(dom
), empty
, flags
);
6667 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6668 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6670 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6673 todo
= isl_set_intersect(todo
, *empty
);
6692 #define TYPE isl_map
6696 #define EMPTY isl_map_empty
6698 #define ADD isl_map_union_disjoint
6699 #include "isl_map_lexopt_templ.c"
6701 /* Given a map "map", compute the lexicographically minimal
6702 * (or maximal) image element for each domain element in "dom",
6703 * in the form of an isl_map.
6704 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6705 * do not have an image element.
6706 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6707 * should be computed over the domain of "map". "empty" is also NULL
6710 * If the input consists of more than one disjunct, then first
6711 * compute the desired result in the form of an isl_pw_multi_aff and
6712 * then convert that into an isl_map.
6714 * This function used to have an explicit implementation in terms
6715 * of isl_maps, but it would continually intersect the domains of
6716 * partial results with the complement of the domain of the next
6717 * partial solution, potentially leading to an explosion in the number
6718 * of disjuncts if there are several disjuncts in the input.
6719 * An even earlier implementation of this function would look for
6720 * better results in the domain of the partial result and for extra
6721 * results in the complement of this domain, which would lead to
6722 * even more splintering.
6724 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6725 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6726 __isl_give isl_set
**empty
, unsigned flags
)
6729 struct isl_map
*res
;
6730 isl_pw_multi_aff
*pma
;
6732 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6733 if (!map
|| (!full
&& !dom
))
6736 if (isl_map_plain_is_empty(map
)) {
6745 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6751 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6753 return isl_map_from_pw_multi_aff(pma
);
6762 __isl_give isl_map
*isl_map_partial_lexmax(
6763 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6764 __isl_give isl_set
**empty
)
6766 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6769 __isl_give isl_map
*isl_map_partial_lexmin(
6770 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6771 __isl_give isl_set
**empty
)
6773 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6776 __isl_give isl_set
*isl_set_partial_lexmin(
6777 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6778 __isl_give isl_set
**empty
)
6780 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6784 __isl_give isl_set
*isl_set_partial_lexmax(
6785 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6786 __isl_give isl_set
**empty
)
6788 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6792 /* Compute the lexicographic minimum (or maximum if "flags" includes
6793 * ISL_OPT_MAX) of "bset" over its parametric domain.
6795 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6798 return isl_basic_map_lexopt(bset
, flags
);
6801 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6803 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6806 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6808 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6811 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6813 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6816 /* Compute the lexicographic minimum of "bset" over its parametric domain
6817 * for the purpose of quantifier elimination.
6818 * That is, find an explicit representation for all the existentially
6819 * quantified variables in "bset" by computing their lexicographic
6822 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6823 __isl_take isl_basic_set
*bset
)
6825 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6828 /* Given a basic map with one output dimension, compute the minimum or
6829 * maximum of that dimension as an isl_pw_aff.
6831 * Compute the optimum as a lexicographic optimum over the single
6832 * output dimension and extract the single isl_pw_aff from the result.
6834 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6837 isl_pw_multi_aff
*pma
;
6840 bmap
= isl_basic_map_copy(bmap
);
6841 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
6842 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
6843 isl_pw_multi_aff_free(pma
);
6848 /* Compute the minimum or maximum of the given output dimension
6849 * as a function of the parameters and the input dimensions,
6850 * but independently of the other output dimensions.
6852 * We first project out the other output dimension and then compute
6853 * the "lexicographic" maximum in each basic map, combining the results
6854 * using isl_pw_aff_union_max.
6856 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6863 n_out
= isl_map_dim(map
, isl_dim_out
);
6864 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6865 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6870 isl_space
*space
= isl_map_get_space(map
);
6872 return isl_pw_aff_empty(space
);
6875 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6876 for (i
= 1; i
< map
->n
; ++i
) {
6877 isl_pw_aff
*pwaff_i
;
6879 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6880 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6888 /* Compute the minimum of the given output dimension as a function of the
6889 * parameters and input dimensions, but independently of
6890 * the other output dimensions.
6892 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6894 return map_dim_opt(map
, pos
, 0);
6897 /* Compute the maximum of the given output dimension as a function of the
6898 * parameters and input dimensions, but independently of
6899 * the other output dimensions.
6901 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6903 return map_dim_opt(map
, pos
, 1);
6906 /* Compute the minimum or maximum of the given set dimension
6907 * as a function of the parameters,
6908 * but independently of the other set dimensions.
6910 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6913 return map_dim_opt(set
, pos
, max
);
6916 /* Compute the maximum of the given set dimension as a function of the
6917 * parameters, but independently of the other set dimensions.
6919 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6921 return set_dim_opt(set
, pos
, 1);
6924 /* Compute the minimum of the given set dimension as a function of the
6925 * parameters, but independently of the other set dimensions.
6927 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6929 return set_dim_opt(set
, pos
, 0);
6932 /* Apply a preimage specified by "mat" on the parameters of "bset".
6933 * bset is assumed to have only parameters and divs.
6935 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6936 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6943 bset
->dim
= isl_space_cow(bset
->dim
);
6947 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6949 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6951 bset
->dim
->nparam
= 0;
6952 bset
->dim
->n_out
= nparam
;
6953 bset
= isl_basic_set_preimage(bset
, mat
);
6955 bset
->dim
->nparam
= bset
->dim
->n_out
;
6956 bset
->dim
->n_out
= 0;
6961 isl_basic_set_free(bset
);
6965 /* Apply a preimage specified by "mat" on the parameters of "set".
6966 * set is assumed to have only parameters and divs.
6968 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6969 __isl_take isl_mat
*mat
)
6977 nparam
= isl_set_dim(set
, isl_dim_param
);
6979 if (mat
->n_row
!= 1 + nparam
)
6980 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6981 "unexpected number of rows", goto error
);
6983 space
= isl_set_get_space(set
);
6984 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6985 isl_dim_param
, 0, nparam
);
6986 set
= isl_set_reset_space(set
, space
);
6987 set
= isl_set_preimage(set
, mat
);
6988 nparam
= isl_set_dim(set
, isl_dim_out
);
6989 space
= isl_set_get_space(set
);
6990 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6991 isl_dim_out
, 0, nparam
);
6992 set
= isl_set_reset_space(set
, space
);
7000 /* Intersect the basic set "bset" with the affine space specified by the
7001 * equalities in "eq".
7003 static __isl_give isl_basic_set
*basic_set_append_equalities(
7004 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7012 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7017 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7018 for (i
= 0; i
< eq
->n_row
; ++i
) {
7019 k
= isl_basic_set_alloc_equality(bset
);
7022 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7023 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7027 bset
= isl_basic_set_gauss(bset
, NULL
);
7028 bset
= isl_basic_set_finalize(bset
);
7033 isl_basic_set_free(bset
);
7037 /* Intersect the set "set" with the affine space specified by the
7038 * equalities in "eq".
7040 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7048 for (i
= 0; i
< set
->n
; ++i
) {
7049 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7062 /* Given a basic set "bset" that only involves parameters and existentially
7063 * quantified variables, return the index of the first equality
7064 * that only involves parameters. If there is no such equality then
7065 * return bset->n_eq.
7067 * This function assumes that isl_basic_set_gauss has been called on "bset".
7069 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7072 unsigned nparam
, n_div
;
7077 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7078 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7080 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7081 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7088 /* Compute an explicit representation for the existentially quantified
7089 * variables in "bset" by computing the "minimal value" of the set
7090 * variables. Since there are no set variables, the computation of
7091 * the minimal value essentially computes an explicit representation
7092 * of the non-empty part(s) of "bset".
7094 * The input only involves parameters and existentially quantified variables.
7095 * All equalities among parameters have been removed.
7097 * Since the existentially quantified variables in the result are in general
7098 * going to be different from those in the input, we first replace
7099 * them by the minimal number of variables based on their equalities.
7100 * This should simplify the parametric integer programming.
7102 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7104 isl_morph
*morph1
, *morph2
;
7110 if (bset
->n_eq
== 0)
7111 return isl_basic_set_lexmin_compute_divs(bset
);
7113 morph1
= isl_basic_set_parameter_compression(bset
);
7114 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7115 bset
= isl_basic_set_lift(bset
);
7116 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7117 bset
= isl_morph_basic_set(morph2
, bset
);
7118 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7119 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7121 set
= isl_basic_set_lexmin_compute_divs(bset
);
7123 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7128 /* Project the given basic set onto its parameter domain, possibly introducing
7129 * new, explicit, existential variables in the constraints.
7130 * The input has parameters and (possibly implicit) existential variables.
7131 * The output has the same parameters, but only
7132 * explicit existentially quantified variables.
7134 * The actual projection is performed by pip, but pip doesn't seem
7135 * to like equalities very much, so we first remove the equalities
7136 * among the parameters by performing a variable compression on
7137 * the parameters. Afterward, an inverse transformation is performed
7138 * and the equalities among the parameters are inserted back in.
7140 * The variable compression on the parameters may uncover additional
7141 * equalities that were only implicit before. We therefore check
7142 * if there are any new parameter equalities in the result and
7143 * if so recurse. The removal of parameter equalities is required
7144 * for the parameter compression performed by base_compute_divs.
7146 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7150 struct isl_mat
*T
, *T2
;
7151 struct isl_set
*set
;
7154 bset
= isl_basic_set_cow(bset
);
7158 if (bset
->n_eq
== 0)
7159 return base_compute_divs(bset
);
7161 bset
= isl_basic_set_gauss(bset
, NULL
);
7164 if (isl_basic_set_plain_is_empty(bset
))
7165 return isl_set_from_basic_set(bset
);
7167 i
= first_parameter_equality(bset
);
7168 if (i
== bset
->n_eq
)
7169 return base_compute_divs(bset
);
7171 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7172 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7174 eq
= isl_mat_cow(eq
);
7175 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7176 if (T
&& T
->n_col
== 0) {
7180 bset
= isl_basic_set_set_to_empty(bset
);
7181 return isl_set_from_basic_set(bset
);
7183 bset
= basic_set_parameter_preimage(bset
, T
);
7185 i
= first_parameter_equality(bset
);
7188 else if (i
== bset
->n_eq
)
7189 set
= base_compute_divs(bset
);
7191 set
= parameter_compute_divs(bset
);
7192 set
= set_parameter_preimage(set
, T2
);
7193 set
= set_append_equalities(set
, eq
);
7197 /* Insert the divs from "ls" before those of "bmap".
7199 * The number of columns is not changed, which means that the last
7200 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7201 * The caller is responsible for removing the same number of dimensions
7202 * from the space of "bmap".
7204 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7205 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7211 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7215 old_n_div
= bmap
->n_div
;
7216 bmap
= insert_div_rows(bmap
, n_div
);
7220 for (i
= 0; i
< n_div
; ++i
) {
7221 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7222 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7228 /* Replace the space of "bmap" by the space and divs of "ls".
7230 * If "ls" has any divs, then we simplify the result since we may
7231 * have discovered some additional equalities that could simplify
7232 * the div expressions.
7234 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7235 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7239 bmap
= isl_basic_map_cow(bmap
);
7243 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7244 bmap
= insert_divs_from_local_space(bmap
, ls
);
7248 isl_space_free(bmap
->dim
);
7249 bmap
->dim
= isl_local_space_get_space(ls
);
7253 isl_local_space_free(ls
);
7255 bmap
= isl_basic_map_simplify(bmap
);
7256 bmap
= isl_basic_map_finalize(bmap
);
7259 isl_basic_map_free(bmap
);
7260 isl_local_space_free(ls
);
7264 /* Replace the space of "map" by the space and divs of "ls".
7266 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7267 __isl_take isl_local_space
*ls
)
7271 map
= isl_map_cow(map
);
7275 for (i
= 0; i
< map
->n
; ++i
) {
7276 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7277 isl_local_space_copy(ls
));
7281 isl_space_free(map
->dim
);
7282 map
->dim
= isl_local_space_get_space(ls
);
7286 isl_local_space_free(ls
);
7289 isl_local_space_free(ls
);
7294 /* Compute an explicit representation for the existentially
7295 * quantified variables for which do not know any explicit representation yet.
7297 * We first sort the existentially quantified variables so that the
7298 * existentially quantified variables for which we already have an explicit
7299 * representation are placed before those for which we do not.
7300 * The input dimensions, the output dimensions and the existentially
7301 * quantified variables for which we already have an explicit
7302 * representation are then turned into parameters.
7303 * compute_divs returns a map with the same parameters and
7304 * no input or output dimensions and the dimension specification
7305 * is reset to that of the input, including the existentially quantified
7306 * variables for which we already had an explicit representation.
7308 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7310 struct isl_basic_set
*bset
;
7311 struct isl_set
*set
;
7312 struct isl_map
*map
;
7314 isl_local_space
*ls
;
7321 bmap
= isl_basic_map_sort_divs(bmap
);
7322 bmap
= isl_basic_map_cow(bmap
);
7326 n_known
= isl_basic_map_first_unknown_div(bmap
);
7328 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7330 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7331 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7332 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7333 space
= isl_space_set_alloc(bmap
->ctx
,
7334 nparam
+ n_in
+ n_out
+ n_known
, 0);
7338 ls
= isl_basic_map_get_local_space(bmap
);
7339 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7340 n_known
, bmap
->n_div
- n_known
);
7342 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7343 swap_div(bmap
, i
- n_known
, i
);
7344 bmap
->n_div
-= n_known
;
7345 bmap
->extra
-= n_known
;
7347 bmap
= isl_basic_map_reset_space(bmap
, space
);
7348 bset
= bset_from_bmap(bmap
);
7350 set
= parameter_compute_divs(bset
);
7351 map
= set_to_map(set
);
7352 map
= replace_space_by_local_space(map
, ls
);
7356 isl_basic_map_free(bmap
);
7360 /* Remove the explicit representation of local variable "div",
7363 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7364 __isl_take isl_basic_map
*bmap
, int div
)
7368 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7370 return isl_basic_map_free(bmap
);
7374 bmap
= isl_basic_map_cow(bmap
);
7377 isl_int_set_si(bmap
->div
[div
][0], 0);
7381 /* Is local variable "div" of "bmap" marked as not having an explicit
7383 * Note that even if "div" is not marked in this way and therefore
7384 * has an explicit representation, this representation may still
7385 * depend (indirectly) on other local variables that do not
7386 * have an explicit representation.
7388 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7391 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7392 return isl_bool_error
;
7393 return isl_int_is_zero(bmap
->div
[div
][0]);
7396 /* Return the position of the first local variable that does not
7397 * have an explicit representation.
7398 * Return the total number of local variables if they all have
7399 * an explicit representation.
7400 * Return -1 on error.
7402 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7409 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7410 if (!isl_basic_map_div_is_known(bmap
, i
))
7416 /* Return the position of the first local variable that does not
7417 * have an explicit representation.
7418 * Return the total number of local variables if they all have
7419 * an explicit representation.
7420 * Return -1 on error.
7422 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7424 return isl_basic_map_first_unknown_div(bset
);
7427 /* Does "bmap" have an explicit representation for all local variables?
7429 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7433 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7434 first
= isl_basic_map_first_unknown_div(bmap
);
7436 return isl_bool_error
;
7440 /* Do all basic maps in "map" have an explicit representation
7441 * for all local variables?
7443 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7448 return isl_bool_error
;
7450 for (i
= 0; i
< map
->n
; ++i
) {
7451 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7456 return isl_bool_true
;
7459 /* If bmap contains any unknown divs, then compute explicit
7460 * expressions for them. However, this computation may be
7461 * quite expensive, so first try to remove divs that aren't
7464 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7467 struct isl_map
*map
;
7469 known
= isl_basic_map_divs_known(bmap
);
7473 return isl_map_from_basic_map(bmap
);
7475 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7477 known
= isl_basic_map_divs_known(bmap
);
7481 return isl_map_from_basic_map(bmap
);
7483 map
= compute_divs(bmap
);
7486 isl_basic_map_free(bmap
);
7490 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7494 struct isl_map
*res
;
7501 known
= isl_map_divs_known(map
);
7509 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7510 for (i
= 1 ; i
< map
->n
; ++i
) {
7512 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7513 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7514 res
= isl_map_union_disjoint(res
, r2
);
7516 res
= isl_map_union(res
, r2
);
7523 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7525 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7528 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7530 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7533 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7536 struct isl_set
*set
;
7541 map
= isl_map_cow(map
);
7545 set
= set_from_map(map
);
7546 set
->dim
= isl_space_domain(set
->dim
);
7549 for (i
= 0; i
< map
->n
; ++i
) {
7550 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7554 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7555 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7562 /* Return the union of "map1" and "map2", where we assume for now that
7563 * "map1" and "map2" are disjoint. Note that the basic maps inside
7564 * "map1" or "map2" may not be disjoint from each other.
7565 * Also note that this function is also called from isl_map_union,
7566 * which takes care of handling the situation where "map1" and "map2"
7567 * may not be disjoint.
7569 * If one of the inputs is empty, we can simply return the other input.
7570 * Similarly, if one of the inputs is universal, then it is equal to the union.
7572 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7573 __isl_take isl_map
*map2
)
7577 struct isl_map
*map
= NULL
;
7583 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7584 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7585 "spaces don't match", goto error
);
7596 is_universe
= isl_map_plain_is_universe(map1
);
7597 if (is_universe
< 0)
7604 is_universe
= isl_map_plain_is_universe(map2
);
7605 if (is_universe
< 0)
7612 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7613 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7614 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7616 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7617 map1
->n
+ map2
->n
, flags
);
7620 for (i
= 0; i
< map1
->n
; ++i
) {
7621 map
= isl_map_add_basic_map(map
,
7622 isl_basic_map_copy(map1
->p
[i
]));
7626 for (i
= 0; i
< map2
->n
; ++i
) {
7627 map
= isl_map_add_basic_map(map
,
7628 isl_basic_map_copy(map2
->p
[i
]));
7642 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7643 * guaranteed to be disjoint by the caller.
7645 * Note that this functions is called from within isl_map_make_disjoint,
7646 * so we have to be careful not to touch the constraints of the inputs
7649 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7650 __isl_take isl_map
*map2
)
7652 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7655 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7656 * not be disjoint. The parameters are assumed to have been aligned.
7658 * We currently simply call map_union_disjoint, the internal operation
7659 * of which does not really depend on the inputs being disjoint.
7660 * If the result contains more than one basic map, then we clear
7661 * the disjoint flag since the result may contain basic maps from
7662 * both inputs and these are not guaranteed to be disjoint.
7664 * As a special case, if "map1" and "map2" are obviously equal,
7665 * then we simply return "map1".
7667 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7668 __isl_take isl_map
*map2
)
7675 equal
= isl_map_plain_is_equal(map1
, map2
);
7683 map1
= map_union_disjoint(map1
, map2
);
7687 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7695 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7698 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7699 __isl_take isl_map
*map2
)
7701 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7704 __isl_give isl_set
*isl_set_union_disjoint(
7705 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
7707 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7711 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7713 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7716 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7719 * "map" and "set" are assumed to be compatible and non-NULL.
7721 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7722 __isl_take isl_set
*set
,
7723 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7724 __isl_take isl_basic_set
*bset
))
7727 struct isl_map
*result
;
7730 if (isl_set_plain_is_universe(set
)) {
7735 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7736 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7737 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7739 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7740 map
->n
* set
->n
, flags
);
7741 for (i
= 0; result
&& i
< map
->n
; ++i
)
7742 for (j
= 0; j
< set
->n
; ++j
) {
7743 result
= isl_map_add_basic_map(result
,
7744 fn(isl_basic_map_copy(map
->p
[i
]),
7745 isl_basic_set_copy(set
->p
[j
])));
7755 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7756 __isl_take isl_set
*set
)
7760 ok
= isl_map_compatible_range(map
, set
);
7764 isl_die(set
->ctx
, isl_error_invalid
,
7765 "incompatible spaces", goto error
);
7767 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7774 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7775 __isl_take isl_set
*set
)
7777 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7780 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7781 __isl_take isl_set
*set
)
7785 ok
= isl_map_compatible_domain(map
, set
);
7789 isl_die(set
->ctx
, isl_error_invalid
,
7790 "incompatible spaces", goto error
);
7792 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7799 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7800 __isl_take isl_set
*set
)
7802 return isl_map_align_params_map_map_and(map
, set
,
7803 &map_intersect_domain
);
7806 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7807 * in the space B -> C, return the intersection.
7808 * The parameters are assumed to have been aligned.
7810 * The map "factor" is first extended to a map living in the space
7811 * [A -> B] -> C and then a regular intersection is computed.
7813 static __isl_give isl_map
*map_intersect_domain_factor_range(
7814 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7817 isl_map
*ext_factor
;
7819 space
= isl_space_domain_factor_domain(isl_map_get_space(map
));
7820 ext_factor
= isl_map_universe(space
);
7821 ext_factor
= isl_map_domain_product(ext_factor
, factor
);
7822 return map_intersect(map
, ext_factor
);
7825 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7826 * in the space B -> C, return the intersection.
7828 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
7829 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7831 return isl_map_align_params_map_map_and(map
, factor
,
7832 &map_intersect_domain_factor_range
);
7835 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7836 * in the space A -> C, return the intersection.
7838 * The map "factor" is first extended to a map living in the space
7839 * A -> [B -> C] and then a regular intersection is computed.
7841 static __isl_give isl_map
*map_intersect_range_factor_range(
7842 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7845 isl_map
*ext_factor
;
7847 space
= isl_space_range_factor_domain(isl_map_get_space(map
));
7848 ext_factor
= isl_map_universe(space
);
7849 ext_factor
= isl_map_range_product(ext_factor
, factor
);
7850 return isl_map_intersect(map
, ext_factor
);
7853 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7854 * in the space A -> C, return the intersection.
7856 __isl_give isl_map
*isl_map_intersect_range_factor_range(
7857 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
7859 return isl_map_align_params_map_map_and(map
, factor
,
7860 &map_intersect_range_factor_range
);
7863 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7864 __isl_take isl_map
*map2
)
7868 map1
= isl_map_reverse(map1
);
7869 map1
= isl_map_apply_range(map1
, map2
);
7870 return isl_map_reverse(map1
);
7877 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7878 __isl_take isl_map
*map2
)
7880 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7883 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7884 __isl_take isl_map
*map2
)
7887 struct isl_map
*result
;
7893 space
= isl_space_join(isl_space_copy(map1
->dim
),
7894 isl_space_copy(map2
->dim
));
7896 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
7899 for (i
= 0; i
< map1
->n
; ++i
)
7900 for (j
= 0; j
< map2
->n
; ++j
) {
7901 result
= isl_map_add_basic_map(result
,
7902 isl_basic_map_apply_range(
7903 isl_basic_map_copy(map1
->p
[i
]),
7904 isl_basic_map_copy(map2
->p
[j
])));
7910 if (result
&& result
->n
<= 1)
7911 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7919 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7920 __isl_take isl_map
*map2
)
7922 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7926 * returns range - domain
7928 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
7930 isl_space
*target_space
;
7931 struct isl_basic_set
*bset
;
7938 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7939 bmap
->dim
, isl_dim_out
),
7941 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7942 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7943 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7944 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7945 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7946 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7947 for (i
= 0; i
< dim
; ++i
) {
7948 int j
= isl_basic_map_alloc_equality(bmap
);
7950 bmap
= isl_basic_map_free(bmap
);
7953 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7954 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7955 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7956 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7958 bset
= isl_basic_map_domain(bmap
);
7959 bset
= isl_basic_set_reset_space(bset
, target_space
);
7962 isl_basic_map_free(bmap
);
7966 /* Check that domain and range of "map" are the same.
7968 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
7973 space
= isl_map_peek_space(map
);
7974 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
7976 return isl_stat_error
;
7978 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7979 "domain and range don't match", return isl_stat_error
);
7984 * returns range - domain
7986 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7990 struct isl_set
*result
;
7995 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7996 map
->dim
, isl_dim_out
),
7998 dim
= isl_map_get_space(map
);
7999 dim
= isl_space_domain(dim
);
8000 result
= isl_set_alloc_space(dim
, map
->n
, 0);
8003 for (i
= 0; i
< map
->n
; ++i
)
8004 result
= isl_set_add_basic_set(result
,
8005 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8014 * returns [domain -> range] -> range - domain
8016 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8017 __isl_take isl_basic_map
*bmap
)
8021 isl_basic_map
*domain
;
8025 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8026 bmap
->dim
, isl_dim_out
))
8027 isl_die(bmap
->ctx
, isl_error_invalid
,
8028 "domain and range don't match", goto error
);
8030 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8031 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8033 space
= isl_basic_map_get_space(bmap
);
8034 space
= isl_space_from_range(isl_space_domain(space
));
8035 domain
= isl_basic_map_universe(space
);
8037 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8038 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8039 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8041 total
= isl_basic_map_total_dim(bmap
);
8043 for (i
= 0; i
< n
; ++i
) {
8044 k
= isl_basic_map_alloc_equality(bmap
);
8047 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8048 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8049 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8050 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8053 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8054 return isl_basic_map_finalize(bmap
);
8056 isl_basic_map_free(bmap
);
8061 * returns [domain -> range] -> range - domain
8063 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8066 isl_space
*domain_space
;
8068 if (isl_map_check_equal_tuples(map
) < 0)
8069 return isl_map_free(map
);
8071 map
= isl_map_cow(map
);
8075 domain_space
= isl_space_domain(isl_map_get_space(map
));
8076 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8077 map
->dim
= isl_space_join(map
->dim
, isl_space_from_range(domain_space
));
8080 for (i
= 0; i
< map
->n
; ++i
) {
8081 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8085 map
= isl_map_unmark_normalized(map
);
8092 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8094 unsigned n_in
, n_out
;
8098 n_in
= isl_space_dim(space
, isl_dim_in
);
8099 n_out
= isl_space_dim(space
, isl_dim_out
);
8101 isl_die(space
->ctx
, isl_error_invalid
,
8102 "number of input and output dimensions needs to be "
8103 "the same", goto error
);
8104 return isl_basic_map_equal(space
, n_in
);
8106 isl_space_free(space
);
8110 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8112 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8115 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8117 isl_space
*dim
= isl_set_get_space(set
);
8119 id
= isl_map_identity(isl_space_map_from_set(dim
));
8120 return isl_map_intersect_range(id
, set
);
8123 /* Construct a basic set with all set dimensions having only non-negative
8126 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8127 __isl_take isl_space
*space
)
8132 struct isl_basic_set
*bset
;
8136 nparam
= isl_space_dim(space
, isl_dim_param
);
8137 dim
= isl_space_dim(space
, isl_dim_set
);
8138 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8141 for (i
= 0; i
< dim
; ++i
) {
8142 int k
= isl_basic_set_alloc_inequality(bset
);
8145 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8146 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8150 isl_basic_set_free(bset
);
8154 /* Construct the half-space x_pos >= 0.
8156 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8160 isl_basic_set
*nonneg
;
8162 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8163 k
= isl_basic_set_alloc_inequality(nonneg
);
8166 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8167 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8169 return isl_basic_set_finalize(nonneg
);
8171 isl_basic_set_free(nonneg
);
8175 /* Construct the half-space x_pos <= -1.
8177 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8183 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8184 k
= isl_basic_set_alloc_inequality(neg
);
8187 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8188 isl_int_set_si(neg
->ineq
[k
][0], -1);
8189 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8191 return isl_basic_set_finalize(neg
);
8193 isl_basic_set_free(neg
);
8197 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8198 enum isl_dim_type type
, unsigned first
, unsigned n
)
8202 isl_basic_set
*nonneg
;
8208 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8209 return isl_set_free(set
);
8211 offset
= pos(set
->dim
, type
);
8212 for (i
= 0; i
< n
; ++i
) {
8213 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8214 offset
+ first
+ i
);
8215 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8217 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8223 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8225 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8231 return isl_stat_error
;
8232 if (isl_set_plain_is_empty(set
)) {
8237 return fn(set
, signs
, user
);
8240 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8242 half
= isl_set_intersect(half
, isl_set_copy(set
));
8243 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8247 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8249 half
= isl_set_intersect(half
, set
);
8250 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8253 return isl_stat_error
;
8256 /* Call "fn" on the intersections of "set" with each of the orthants
8257 * (except for obviously empty intersections). The orthant is identified
8258 * by the signs array, with each entry having value 1 or -1 according
8259 * to the sign of the corresponding variable.
8261 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8262 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8271 return isl_stat_error
;
8272 if (isl_set_plain_is_empty(set
))
8275 nparam
= isl_set_dim(set
, isl_dim_param
);
8276 nvar
= isl_set_dim(set
, isl_dim_set
);
8278 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8280 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8288 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8290 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8293 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8294 __isl_keep isl_basic_map
*bmap2
)
8297 struct isl_map
*map1
;
8298 struct isl_map
*map2
;
8300 if (!bmap1
|| !bmap2
)
8301 return isl_bool_error
;
8303 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8304 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8306 is_subset
= isl_map_is_subset(map1
, map2
);
8314 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8315 __isl_keep isl_basic_set
*bset2
)
8317 return isl_basic_map_is_subset(bset1
, bset2
);
8320 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8321 __isl_keep isl_basic_map
*bmap2
)
8325 if (!bmap1
|| !bmap2
)
8326 return isl_bool_error
;
8327 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8328 if (is_subset
!= isl_bool_true
)
8330 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8334 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8335 __isl_keep isl_basic_set
*bset2
)
8337 return isl_basic_map_is_equal(
8338 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8341 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8347 return isl_bool_error
;
8348 for (i
= 0; i
< map
->n
; ++i
) {
8349 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8351 return isl_bool_error
;
8353 return isl_bool_false
;
8355 return isl_bool_true
;
8358 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8360 return map
? map
->n
== 0 : isl_bool_error
;
8363 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8365 return set
? set
->n
== 0 : isl_bool_error
;
8368 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8370 return isl_map_is_empty(set_to_map(set
));
8373 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8374 __isl_keep isl_map
*map2
)
8377 return isl_bool_error
;
8379 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8382 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8383 __isl_keep isl_set
*set2
)
8386 return isl_bool_error
;
8388 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8391 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8396 return isl_bool_error
;
8397 is_subset
= isl_map_is_subset(map1
, map2
);
8398 if (is_subset
!= isl_bool_true
)
8400 is_subset
= isl_map_is_subset(map2
, map1
);
8404 /* Is "map1" equal to "map2"?
8406 * First check if they are obviously equal.
8407 * If not, then perform a more detailed analysis.
8409 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8413 equal
= isl_map_plain_is_equal(map1
, map2
);
8414 if (equal
< 0 || equal
)
8416 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8419 isl_bool
isl_basic_map_is_strict_subset(
8420 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8424 if (!bmap1
|| !bmap2
)
8425 return isl_bool_error
;
8426 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8427 if (is_subset
!= isl_bool_true
)
8429 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8430 return isl_bool_not(is_subset
);
8433 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8434 __isl_keep isl_map
*map2
)
8439 return isl_bool_error
;
8440 is_subset
= isl_map_is_subset(map1
, map2
);
8441 if (is_subset
!= isl_bool_true
)
8443 is_subset
= isl_map_is_subset(map2
, map1
);
8444 return isl_bool_not(is_subset
);
8447 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8448 __isl_keep isl_set
*set2
)
8450 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8453 /* Is "bmap" obviously equal to the universe with the same space?
8455 * That is, does it not have any constraints?
8457 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8460 return isl_bool_error
;
8461 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8464 /* Is "bset" obviously equal to the universe with the same space?
8466 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8468 return isl_basic_map_plain_is_universe(bset
);
8471 /* If "c" does not involve any existentially quantified variables,
8472 * then set *univ to false and abort
8474 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8476 isl_bool
*univ
= user
;
8479 n
= isl_constraint_dim(c
, isl_dim_div
);
8480 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8481 isl_constraint_free(c
);
8482 if (*univ
< 0 || !*univ
)
8483 return isl_stat_error
;
8487 /* Is "bmap" equal to the universe with the same space?
8489 * First check if it is obviously equal to the universe.
8490 * If not and if there are any constraints not involving
8491 * existentially quantified variables, then it is certainly
8492 * not equal to the universe.
8493 * Otherwise, check if the universe is a subset of "bmap".
8495 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8498 isl_basic_map
*test
;
8500 univ
= isl_basic_map_plain_is_universe(bmap
);
8501 if (univ
< 0 || univ
)
8503 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8504 return isl_bool_false
;
8505 univ
= isl_bool_true
;
8506 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8508 return isl_bool_error
;
8509 if (univ
< 0 || !univ
)
8511 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8512 univ
= isl_basic_map_is_subset(test
, bmap
);
8513 isl_basic_map_free(test
);
8517 /* Is "bset" equal to the universe with the same space?
8519 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8521 return isl_basic_map_is_universe(bset
);
8524 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8529 return isl_bool_error
;
8531 for (i
= 0; i
< map
->n
; ++i
) {
8532 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8537 return isl_bool_false
;
8540 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8542 return isl_map_plain_is_universe(set_to_map(set
));
8545 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8547 struct isl_basic_set
*bset
= NULL
;
8548 struct isl_vec
*sample
= NULL
;
8549 isl_bool empty
, non_empty
;
8552 return isl_bool_error
;
8554 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8555 return isl_bool_true
;
8557 if (isl_basic_map_plain_is_universe(bmap
))
8558 return isl_bool_false
;
8560 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8561 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8562 copy
= isl_basic_map_remove_redundancies(copy
);
8563 empty
= isl_basic_map_plain_is_empty(copy
);
8564 isl_basic_map_free(copy
);
8568 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8570 return isl_bool_error
;
8572 return isl_bool_false
;
8573 isl_vec_free(bmap
->sample
);
8574 bmap
->sample
= NULL
;
8575 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8577 return isl_bool_error
;
8578 sample
= isl_basic_set_sample_vec(bset
);
8580 return isl_bool_error
;
8581 empty
= sample
->size
== 0;
8582 isl_vec_free(bmap
->sample
);
8583 bmap
->sample
= sample
;
8585 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8590 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8593 return isl_bool_error
;
8594 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8597 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8600 return isl_bool_error
;
8601 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8604 /* Is "bmap" known to be non-empty?
8606 * That is, is the cached sample still valid?
8608 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8613 return isl_bool_error
;
8615 return isl_bool_false
;
8616 total
= 1 + isl_basic_map_total_dim(bmap
);
8617 if (bmap
->sample
->size
!= total
)
8618 return isl_bool_false
;
8619 return isl_basic_map_contains(bmap
, bmap
->sample
);
8622 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8624 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8627 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8628 __isl_take isl_basic_map
*bmap2
)
8630 struct isl_map
*map
;
8631 if (!bmap1
|| !bmap2
)
8634 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8636 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8639 map
= isl_map_add_basic_map(map
, bmap1
);
8640 map
= isl_map_add_basic_map(map
, bmap2
);
8643 isl_basic_map_free(bmap1
);
8644 isl_basic_map_free(bmap2
);
8648 struct isl_set
*isl_basic_set_union(
8649 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8651 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8652 bset_to_bmap(bset2
)));
8655 /* Order divs such that any div only depends on previous divs */
8656 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8657 __isl_take isl_basic_map
*bmap
)
8662 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
8664 return isl_basic_map_free(bmap
);
8666 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8668 if (isl_int_is_zero(bmap
->div
[i
][0]))
8670 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8675 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8676 "integer division depends on itself",
8677 return isl_basic_map_free(bmap
));
8678 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8686 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8688 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8691 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8698 for (i
= 0; i
< map
->n
; ++i
) {
8699 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8710 /* Sort the local variables of "bset".
8712 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8713 __isl_take isl_basic_set
*bset
)
8715 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8718 /* Apply the expansion computed by isl_merge_divs.
8719 * The expansion itself is given by "exp" while the resulting
8720 * list of divs is given by "div".
8722 * Move the integer divisions of "bmap" into the right position
8723 * according to "exp" and then introduce the additional integer
8724 * divisions, adding div constraints.
8725 * The moving should be done first to avoid moving coefficients
8726 * in the definitions of the extra integer divisions.
8728 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8729 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8734 bmap
= isl_basic_map_cow(bmap
);
8738 if (div
->n_row
< bmap
->n_div
)
8739 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8740 "not an expansion", goto error
);
8742 n_div
= bmap
->n_div
;
8743 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8744 div
->n_row
- n_div
, 0,
8745 2 * (div
->n_row
- n_div
));
8747 for (i
= n_div
; i
< div
->n_row
; ++i
)
8748 if (isl_basic_map_alloc_div(bmap
) < 0)
8751 for (j
= n_div
- 1; j
>= 0; --j
) {
8754 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8759 for (i
= 0; i
< div
->n_row
; ++i
) {
8760 if (j
< n_div
&& exp
[j
] == i
) {
8763 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8764 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8766 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
8775 isl_basic_map_free(bmap
);
8780 /* Apply the expansion computed by isl_merge_divs.
8781 * The expansion itself is given by "exp" while the resulting
8782 * list of divs is given by "div".
8784 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8785 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8787 return isl_basic_map_expand_divs(bset
, div
, exp
);
8790 /* Look for a div in dst that corresponds to the div "div" in src.
8791 * The divs before "div" in src and dst are assumed to be the same.
8793 * Return the position of the corresponding div in dst
8794 * if there is one. Otherwise, return a position beyond the integer divisions.
8795 * Return -1 on error.
8797 static int find_div(__isl_keep isl_basic_map
*dst
,
8798 __isl_keep isl_basic_map
*src
, unsigned div
)
8804 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8805 if (!dst
|| v_div
< 0)
8808 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8809 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
8810 for (i
= div
; i
< n_div
; ++i
)
8811 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
8812 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
8818 /* Align the divs of "dst" to those of "src", adding divs from "src"
8819 * if needed. That is, make sure that the first src->n_div divs
8820 * of the result are equal to those of src.
8822 * The result is not finalized as by design it will have redundant
8823 * divs if any divs from "src" were copied.
8825 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8826 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8835 return isl_basic_map_free(dst
);
8837 if (src
->n_div
== 0)
8840 known
= isl_basic_map_divs_known(src
);
8842 return isl_basic_map_free(dst
);
8844 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8845 "some src divs are unknown",
8846 return isl_basic_map_free(dst
));
8848 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
8850 return isl_basic_map_free(dst
);
8852 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
8854 return isl_basic_map_free(dst
);
8857 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
8858 for (i
= 0; i
< src
->n_div
; ++i
) {
8859 int j
= find_div(dst
, src
, i
);
8861 dst
= isl_basic_map_free(dst
);
8862 if (j
== dst_n_div
) {
8864 int extra
= src
->n_div
- i
;
8865 dst
= isl_basic_map_cow(dst
);
8868 dst
= isl_basic_map_extend_space(dst
,
8869 isl_space_copy(dst
->dim
),
8870 extra
, 0, 2 * extra
);
8873 j
= isl_basic_map_alloc_div(dst
);
8876 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
8877 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
8879 dst
= isl_basic_map_add_div_constraints(dst
, j
);
8884 dst
= isl_basic_map_swap_div(dst
, i
, j
);
8888 isl_basic_map_free(src
);
8891 isl_basic_map_free(src
);
8892 isl_basic_map_free(dst
);
8896 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8904 map
= isl_map_compute_divs(map
);
8905 map
= isl_map_cow(map
);
8909 for (i
= 1; i
< map
->n
; ++i
)
8910 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8911 for (i
= 1; i
< map
->n
; ++i
) {
8912 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8914 return isl_map_free(map
);
8917 map
= isl_map_unmark_normalized(map
);
8921 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8923 return isl_map_align_divs_internal(map
);
8926 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8928 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8931 /* Align the divs of the basic maps in "map" to those
8932 * of the basic maps in "list", as well as to the other basic maps in "map".
8933 * The elements in "list" are assumed to have known divs.
8935 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8936 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8940 map
= isl_map_compute_divs(map
);
8941 map
= isl_map_cow(map
);
8943 return isl_map_free(map
);
8947 n
= isl_basic_map_list_n_basic_map(list
);
8948 for (i
= 0; i
< n
; ++i
) {
8949 isl_basic_map
*bmap
;
8951 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8952 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8953 isl_basic_map_free(bmap
);
8956 return isl_map_free(map
);
8958 return isl_map_align_divs_internal(map
);
8961 /* Align the divs of each element of "list" to those of "bmap".
8962 * Both "bmap" and the elements of "list" are assumed to have known divs.
8964 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8965 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8970 return isl_basic_map_list_free(list
);
8972 n
= isl_basic_map_list_n_basic_map(list
);
8973 for (i
= 0; i
< n
; ++i
) {
8974 isl_basic_map
*bmap_i
;
8976 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8977 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8978 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8984 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8985 __isl_take isl_map
*map
)
8989 ok
= isl_map_compatible_domain(map
, set
);
8993 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8994 "incompatible spaces", goto error
);
8995 map
= isl_map_intersect_domain(map
, set
);
8996 set
= isl_map_range(map
);
9004 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9005 __isl_take isl_map
*map
)
9007 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
9010 /* There is no need to cow as removing empty parts doesn't change
9011 * the meaning of the set.
9013 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9020 for (i
= map
->n
- 1; i
>= 0; --i
)
9021 map
= remove_if_empty(map
, i
);
9026 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
9028 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9031 /* Create a binary relation that maps the shared initial "pos" dimensions
9032 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9034 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9035 __isl_keep isl_basic_set
*bset2
, int pos
)
9037 isl_basic_map
*bmap1
;
9038 isl_basic_map
*bmap2
;
9040 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9041 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9042 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9043 isl_dim_out
, 0, pos
);
9044 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9045 isl_dim_out
, 0, pos
);
9046 return isl_basic_map_range_product(bmap1
, bmap2
);
9049 /* Given two basic sets bset1 and bset2, compute the maximal difference
9050 * between the values of dimension pos in bset1 and those in bset2
9051 * for any common value of the parameters and dimensions preceding pos.
9053 static enum isl_lp_result
basic_set_maximal_difference_at(
9054 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9055 int pos
, isl_int
*opt
)
9057 isl_basic_map
*bmap1
;
9058 struct isl_ctx
*ctx
;
9059 struct isl_vec
*obj
;
9063 enum isl_lp_result res
;
9065 if (!bset1
|| !bset2
)
9066 return isl_lp_error
;
9068 nparam
= isl_basic_set_n_param(bset1
);
9069 dim1
= isl_basic_set_n_dim(bset1
);
9071 bmap1
= join_initial(bset1
, bset2
, pos
);
9073 return isl_lp_error
;
9075 total
= isl_basic_map_total_dim(bmap1
);
9077 obj
= isl_vec_alloc(ctx
, 1 + total
);
9080 isl_seq_clr(obj
->block
.data
, 1 + total
);
9081 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9082 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9083 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9085 isl_basic_map_free(bmap1
);
9089 isl_basic_map_free(bmap1
);
9090 return isl_lp_error
;
9093 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9094 * for any common value of the parameters and dimensions preceding pos
9095 * in both basic sets, the values of dimension pos in bset1 are
9096 * smaller or larger than those in bset2.
9099 * 1 if bset1 follows bset2
9100 * -1 if bset1 precedes bset2
9101 * 0 if bset1 and bset2 are incomparable
9102 * -2 if some error occurred.
9104 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9105 __isl_keep isl_basic_set
*bset2
, int pos
)
9108 enum isl_lp_result res
;
9113 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9115 if (res
== isl_lp_empty
)
9117 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9118 res
== isl_lp_unbounded
)
9120 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9129 /* Given two basic sets bset1 and bset2, check whether
9130 * for any common value of the parameters and dimensions preceding pos
9131 * there is a value of dimension pos in bset1 that is larger
9132 * than a value of the same dimension in bset2.
9135 * 1 if there exists such a pair
9136 * 0 if there is no such pair, but there is a pair of equal values
9138 * -2 if some error occurred.
9140 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9141 __isl_keep isl_basic_set
*bset2
, int pos
)
9144 isl_basic_map
*bmap
;
9147 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9148 bmap
= join_initial(bset1
, bset2
, pos
);
9149 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9150 isl_dim_out
, dim1
- pos
);
9151 empty
= isl_basic_map_is_empty(bmap
);
9155 isl_basic_map_free(bmap
);
9158 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9159 isl_dim_out
, dim1
- pos
);
9160 empty
= isl_basic_map_is_empty(bmap
);
9163 isl_basic_map_free(bmap
);
9168 isl_basic_map_free(bmap
);
9172 /* Given two sets set1 and set2, check whether
9173 * for any common value of the parameters and dimensions preceding pos
9174 * there is a value of dimension pos in set1 that is larger
9175 * than a value of the same dimension in set2.
9178 * 1 if there exists such a pair
9179 * 0 if there is no such pair, but there is a pair of equal values
9181 * -2 if some error occurred.
9183 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9184 __isl_keep isl_set
*set2
, int pos
)
9192 for (i
= 0; i
< set1
->n
; ++i
)
9193 for (j
= 0; j
< set2
->n
; ++j
) {
9195 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9196 if (f
== 1 || f
== -2)
9205 static isl_bool
isl_basic_map_plain_has_fixed_var(
9206 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9213 return isl_bool_error
;
9214 total
= isl_basic_map_total_dim(bmap
);
9215 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9216 for (; d
+1 > pos
; --d
)
9217 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9221 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9222 return isl_bool_false
;
9223 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9224 return isl_bool_false
;
9225 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9226 return isl_bool_false
;
9228 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9229 return isl_bool_true
;
9231 return isl_bool_false
;
9234 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9235 unsigned pos
, isl_int
*val
)
9243 return isl_bool_error
;
9245 return isl_bool_false
;
9247 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9250 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9251 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9252 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9253 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9254 fixed
= isl_bool_false
;
9257 isl_int_set(*val
, v
);
9263 static isl_bool
isl_basic_set_plain_has_fixed_var(
9264 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9266 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9270 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9271 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9273 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9274 return isl_bool_error
;
9275 return isl_basic_map_plain_has_fixed_var(bmap
,
9276 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9279 /* If "bmap" obviously lies on a hyperplane where the given dimension
9280 * has a fixed value, then return that value.
9281 * Otherwise return NaN.
9283 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9284 __isl_keep isl_basic_map
*bmap
,
9285 enum isl_dim_type type
, unsigned pos
)
9293 ctx
= isl_basic_map_get_ctx(bmap
);
9294 v
= isl_val_alloc(ctx
);
9297 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9299 return isl_val_free(v
);
9301 isl_int_set_si(v
->d
, 1);
9305 return isl_val_nan(ctx
);
9308 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9309 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9311 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9312 return isl_bool_error
;
9313 return isl_map_plain_has_fixed_var(map
,
9314 map_offset(map
, type
) - 1 + pos
, val
);
9317 /* If "map" obviously lies on a hyperplane where the given dimension
9318 * has a fixed value, then return that value.
9319 * Otherwise return NaN.
9321 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9322 enum isl_dim_type type
, unsigned pos
)
9330 ctx
= isl_map_get_ctx(map
);
9331 v
= isl_val_alloc(ctx
);
9334 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9336 return isl_val_free(v
);
9338 isl_int_set_si(v
->d
, 1);
9342 return isl_val_nan(ctx
);
9345 /* If "set" obviously lies on a hyperplane where the given dimension
9346 * has a fixed value, then return that value.
9347 * Otherwise return NaN.
9349 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9350 enum isl_dim_type type
, unsigned pos
)
9352 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9355 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9356 * then return this fixed value in *val.
9358 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9359 unsigned dim
, isl_int
*val
)
9361 return isl_basic_set_plain_has_fixed_var(bset
,
9362 isl_basic_set_n_param(bset
) + dim
, val
);
9365 /* Return -1 if the constraint "c1" should be sorted before "c2"
9366 * and 1 if it should be sorted after "c2".
9367 * Return 0 if the two constraints are the same (up to the constant term).
9369 * In particular, if a constraint involves later variables than another
9370 * then it is sorted after this other constraint.
9371 * uset_gist depends on constraints without existentially quantified
9372 * variables sorting first.
9374 * For constraints that have the same latest variable, those
9375 * with the same coefficient for this latest variable (first in absolute value
9376 * and then in actual value) are grouped together.
9377 * This is useful for detecting pairs of constraints that can
9378 * be chained in their printed representation.
9380 * Finally, within a group, constraints are sorted according to
9381 * their coefficients (excluding the constant term).
9383 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9385 isl_int
**c1
= (isl_int
**) p1
;
9386 isl_int
**c2
= (isl_int
**) p2
;
9388 unsigned size
= *(unsigned *) arg
;
9391 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9392 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9397 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9400 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9404 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9407 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9408 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9409 * and 0 if the two constraints are the same (up to the constant term).
9411 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9412 isl_int
*c1
, isl_int
*c2
)
9418 total
= isl_basic_map_total_dim(bmap
);
9419 return sort_constraint_cmp(&c1
, &c2
, &total
);
9422 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9423 __isl_take isl_basic_map
*bmap
)
9429 if (bmap
->n_ineq
== 0)
9431 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
9433 total
= isl_basic_map_total_dim(bmap
);
9434 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9435 &sort_constraint_cmp
, &total
) < 0)
9436 return isl_basic_map_free(bmap
);
9437 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
9441 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9442 __isl_take isl_basic_set
*bset
)
9444 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9445 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9448 __isl_give isl_basic_map
*isl_basic_map_normalize(
9449 __isl_take isl_basic_map
*bmap
)
9451 bmap
= isl_basic_map_remove_redundancies(bmap
);
9452 bmap
= isl_basic_map_sort_constraints(bmap
);
9455 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9456 __isl_keep isl_basic_map
*bmap2
)
9460 isl_space
*space1
, *space2
;
9462 if (!bmap1
|| !bmap2
)
9467 space1
= isl_basic_map_peek_space(bmap1
);
9468 space2
= isl_basic_map_peek_space(bmap2
);
9469 cmp
= isl_space_cmp(space1
, space2
);
9472 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9473 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9474 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9475 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9476 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9478 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9480 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9482 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9483 return bmap1
->n_eq
- bmap2
->n_eq
;
9484 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9485 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9486 if (bmap1
->n_div
!= bmap2
->n_div
)
9487 return bmap1
->n_div
- bmap2
->n_div
;
9488 total
= isl_basic_map_total_dim(bmap1
);
9489 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9490 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9494 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9495 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9499 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9500 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9507 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9508 __isl_keep isl_basic_set
*bset2
)
9510 return isl_basic_map_plain_cmp(bset1
, bset2
);
9513 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9519 if (set1
->n
!= set2
->n
)
9520 return set1
->n
- set2
->n
;
9522 for (i
= 0; i
< set1
->n
; ++i
) {
9523 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9531 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9532 __isl_keep isl_basic_map
*bmap2
)
9534 if (!bmap1
|| !bmap2
)
9535 return isl_bool_error
;
9536 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9539 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9540 __isl_keep isl_basic_set
*bset2
)
9542 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9543 bset_to_bmap(bset2
));
9546 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9548 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9549 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9551 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9554 /* Sort the basic maps of "map" and remove duplicate basic maps.
9556 * While removing basic maps, we make sure that the basic maps remain
9557 * sorted because isl_map_normalize expects the basic maps of the result
9560 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9564 map
= isl_map_remove_empty_parts(map
);
9567 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9568 for (i
= map
->n
- 1; i
>= 1; --i
) {
9569 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9571 isl_basic_map_free(map
->p
[i
-1]);
9572 for (j
= i
; j
< map
->n
; ++j
)
9573 map
->p
[j
- 1] = map
->p
[j
];
9580 /* Remove obvious duplicates among the basic maps of "map".
9582 * Unlike isl_map_normalize, this function does not remove redundant
9583 * constraints and only removes duplicates that have exactly the same
9584 * constraints in the input. It does sort the constraints and
9585 * the basic maps to ease the detection of duplicates.
9587 * If "map" has already been normalized or if the basic maps are
9588 * disjoint, then there can be no duplicates.
9590 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9593 isl_basic_map
*bmap
;
9599 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9601 for (i
= 0; i
< map
->n
; ++i
) {
9602 bmap
= isl_basic_map_copy(map
->p
[i
]);
9603 bmap
= isl_basic_map_sort_constraints(bmap
);
9605 return isl_map_free(map
);
9606 isl_basic_map_free(map
->p
[i
]);
9610 map
= sort_and_remove_duplicates(map
);
9614 /* We normalize in place, but if anything goes wrong we need
9615 * to return NULL, so we need to make sure we don't change the
9616 * meaning of any possible other copies of map.
9618 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9621 struct isl_basic_map
*bmap
;
9625 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9627 for (i
= 0; i
< map
->n
; ++i
) {
9628 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9631 isl_basic_map_free(map
->p
[i
]);
9635 map
= sort_and_remove_duplicates(map
);
9637 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9644 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9646 return set_from_map(isl_map_normalize(set_to_map(set
)));
9649 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9650 __isl_keep isl_map
*map2
)
9656 return isl_bool_error
;
9659 return isl_bool_true
;
9660 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9661 return isl_bool_false
;
9663 map1
= isl_map_copy(map1
);
9664 map2
= isl_map_copy(map2
);
9665 map1
= isl_map_normalize(map1
);
9666 map2
= isl_map_normalize(map2
);
9669 equal
= map1
->n
== map2
->n
;
9670 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9671 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9681 return isl_bool_error
;
9684 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9685 __isl_keep isl_set
*set2
)
9687 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9690 /* Return the basic maps in "map" as a list.
9692 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9693 __isl_keep isl_map
*map
)
9697 isl_basic_map_list
*list
;
9701 ctx
= isl_map_get_ctx(map
);
9702 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9704 for (i
= 0; i
< map
->n
; ++i
) {
9705 isl_basic_map
*bmap
;
9707 bmap
= isl_basic_map_copy(map
->p
[i
]);
9708 list
= isl_basic_map_list_add(list
, bmap
);
9714 /* Return the intersection of the elements in the non-empty list "list".
9715 * All elements are assumed to live in the same space.
9717 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9718 __isl_take isl_basic_map_list
*list
)
9721 isl_basic_map
*bmap
;
9725 n
= isl_basic_map_list_n_basic_map(list
);
9727 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9728 "expecting non-empty list", goto error
);
9730 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9731 for (i
= 1; i
< n
; ++i
) {
9732 isl_basic_map
*bmap_i
;
9734 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9735 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9738 isl_basic_map_list_free(list
);
9741 isl_basic_map_list_free(list
);
9745 /* Return the intersection of the elements in the non-empty list "list".
9746 * All elements are assumed to live in the same space.
9748 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9749 __isl_take isl_basic_set_list
*list
)
9751 return isl_basic_map_list_intersect(list
);
9754 /* Return the union of the elements of "list".
9755 * The list is required to have at least one element.
9757 __isl_give isl_set
*isl_basic_set_list_union(
9758 __isl_take isl_basic_set_list
*list
)
9762 isl_basic_set
*bset
;
9767 n
= isl_basic_set_list_n_basic_set(list
);
9769 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9770 "expecting non-empty list", goto error
);
9772 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9773 space
= isl_basic_set_get_space(bset
);
9774 isl_basic_set_free(bset
);
9776 set
= isl_set_alloc_space(space
, n
, 0);
9777 for (i
= 0; i
< n
; ++i
) {
9778 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9779 set
= isl_set_add_basic_set(set
, bset
);
9782 isl_basic_set_list_free(list
);
9785 isl_basic_set_list_free(list
);
9789 /* Return the union of the elements in the non-empty list "list".
9790 * All elements are assumed to live in the same space.
9792 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9799 n
= isl_set_list_n_set(list
);
9801 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9802 "expecting non-empty list", goto error
);
9804 set
= isl_set_list_get_set(list
, 0);
9805 for (i
= 1; i
< n
; ++i
) {
9808 set_i
= isl_set_list_get_set(list
, i
);
9809 set
= isl_set_union(set
, set_i
);
9812 isl_set_list_free(list
);
9815 isl_set_list_free(list
);
9819 __isl_give isl_basic_map
*isl_basic_map_product(
9820 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9822 isl_space
*space_result
= NULL
;
9823 struct isl_basic_map
*bmap
;
9824 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9825 struct isl_dim_map
*dim_map1
, *dim_map2
;
9827 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9829 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9830 isl_space_copy(bmap2
->dim
));
9832 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9833 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9834 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9835 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9836 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9838 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9839 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9840 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9841 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9842 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9843 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9844 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9845 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9846 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9847 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9848 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9850 bmap
= isl_basic_map_alloc_space(space_result
,
9851 bmap1
->n_div
+ bmap2
->n_div
,
9852 bmap1
->n_eq
+ bmap2
->n_eq
,
9853 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9854 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9855 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9856 bmap
= isl_basic_map_simplify(bmap
);
9857 return isl_basic_map_finalize(bmap
);
9859 isl_basic_map_free(bmap1
);
9860 isl_basic_map_free(bmap2
);
9864 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9865 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9867 isl_basic_map
*prod
;
9869 prod
= isl_basic_map_product(bmap1
, bmap2
);
9870 prod
= isl_basic_map_flatten(prod
);
9874 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9875 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9877 return isl_basic_map_flat_range_product(bset1
, bset2
);
9880 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9881 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9883 isl_space
*space_result
= NULL
;
9884 isl_basic_map
*bmap
;
9885 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9886 struct isl_dim_map
*dim_map1
, *dim_map2
;
9888 if (!bmap1
|| !bmap2
)
9891 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9892 isl_space_copy(bmap2
->dim
));
9894 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9895 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9896 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9897 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9899 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9900 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9901 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9902 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9903 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9904 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9905 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9906 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9907 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9908 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9909 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9911 bmap
= isl_basic_map_alloc_space(space_result
,
9912 bmap1
->n_div
+ bmap2
->n_div
,
9913 bmap1
->n_eq
+ bmap2
->n_eq
,
9914 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9915 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9916 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9917 bmap
= isl_basic_map_simplify(bmap
);
9918 return isl_basic_map_finalize(bmap
);
9920 isl_basic_map_free(bmap1
);
9921 isl_basic_map_free(bmap2
);
9925 __isl_give isl_basic_map
*isl_basic_map_range_product(
9926 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9929 isl_space
*space_result
= NULL
;
9930 isl_basic_map
*bmap
;
9931 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9932 struct isl_dim_map
*dim_map1
, *dim_map2
;
9934 rational
= isl_basic_map_is_rational(bmap1
);
9935 if (rational
>= 0 && rational
)
9936 rational
= isl_basic_map_is_rational(bmap2
);
9937 if (!bmap1
|| !bmap2
|| rational
< 0)
9940 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9943 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9944 isl_space_copy(bmap2
->dim
));
9946 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9947 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9948 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9949 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9951 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9952 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9953 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9954 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9955 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9956 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9957 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9958 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9959 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9960 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9961 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9963 bmap
= isl_basic_map_alloc_space(space_result
,
9964 bmap1
->n_div
+ bmap2
->n_div
,
9965 bmap1
->n_eq
+ bmap2
->n_eq
,
9966 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9967 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9968 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9970 bmap
= isl_basic_map_set_rational(bmap
);
9971 bmap
= isl_basic_map_simplify(bmap
);
9972 return isl_basic_map_finalize(bmap
);
9974 isl_basic_map_free(bmap1
);
9975 isl_basic_map_free(bmap2
);
9979 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9980 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9982 isl_basic_map
*prod
;
9984 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9985 prod
= isl_basic_map_flatten_range(prod
);
9989 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9990 * and collect the results.
9991 * The result live in the space obtained by calling "space_product"
9992 * on the spaces of "map1" and "map2".
9993 * If "remove_duplicates" is set then the result may contain duplicates
9994 * (even if the inputs do not) and so we try and remove the obvious
9997 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9998 __isl_take isl_map
*map2
,
9999 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10000 __isl_take isl_space
*right
),
10001 __isl_give isl_basic_map
*(*basic_map_product
)(
10002 __isl_take isl_basic_map
*left
,
10003 __isl_take isl_basic_map
*right
),
10004 int remove_duplicates
)
10006 unsigned flags
= 0;
10007 struct isl_map
*result
;
10011 m
= isl_map_has_equal_params(map1
, map2
);
10015 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10016 "parameters don't match", goto error
);
10018 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10019 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10020 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10022 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10023 isl_space_copy(map2
->dim
)),
10024 map1
->n
* map2
->n
, flags
);
10027 for (i
= 0; i
< map1
->n
; ++i
)
10028 for (j
= 0; j
< map2
->n
; ++j
) {
10029 struct isl_basic_map
*part
;
10030 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10031 isl_basic_map_copy(map2
->p
[j
]));
10032 if (isl_basic_map_is_empty(part
))
10033 isl_basic_map_free(part
);
10035 result
= isl_map_add_basic_map(result
, part
);
10039 if (remove_duplicates
)
10040 result
= isl_map_remove_obvious_duplicates(result
);
10041 isl_map_free(map1
);
10042 isl_map_free(map2
);
10045 isl_map_free(map1
);
10046 isl_map_free(map2
);
10050 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10052 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10053 __isl_take isl_map
*map2
)
10055 return map_product(map1
, map2
, &isl_space_product
,
10056 &isl_basic_map_product
, 0);
10059 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10060 __isl_take isl_map
*map2
)
10062 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10065 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10067 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10068 __isl_take isl_map
*map2
)
10072 prod
= isl_map_product(map1
, map2
);
10073 prod
= isl_map_flatten(prod
);
10077 /* Given two set A and B, construct its Cartesian product A x B.
10079 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10081 return isl_map_range_product(set1
, set2
);
10084 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10085 __isl_take isl_set
*set2
)
10087 return isl_map_flat_range_product(set1
, set2
);
10090 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10092 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10093 __isl_take isl_map
*map2
)
10095 return map_product(map1
, map2
, &isl_space_domain_product
,
10096 &isl_basic_map_domain_product
, 1);
10099 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10101 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10102 __isl_take isl_map
*map2
)
10104 return map_product(map1
, map2
, &isl_space_range_product
,
10105 &isl_basic_map_range_product
, 1);
10108 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10109 __isl_take isl_map
*map2
)
10111 return isl_map_align_params_map_map_and(map1
, map2
,
10112 &map_domain_product_aligned
);
10115 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10116 __isl_take isl_map
*map2
)
10118 return isl_map_align_params_map_map_and(map1
, map2
,
10119 &map_range_product_aligned
);
10122 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10124 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10127 int total1
, keep1
, total2
, keep2
;
10131 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10132 !isl_space_range_is_wrapping(map
->dim
))
10133 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10134 "not a product", return isl_map_free(map
));
10136 space
= isl_map_get_space(map
);
10137 total1
= isl_space_dim(space
, isl_dim_in
);
10138 total2
= isl_space_dim(space
, isl_dim_out
);
10139 space
= isl_space_factor_domain(space
);
10140 keep1
= isl_space_dim(space
, isl_dim_in
);
10141 keep2
= isl_space_dim(space
, isl_dim_out
);
10142 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10143 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10144 map
= isl_map_reset_space(map
, space
);
10149 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10151 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10154 int total1
, keep1
, total2
, keep2
;
10158 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10159 !isl_space_range_is_wrapping(map
->dim
))
10160 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10161 "not a product", return isl_map_free(map
));
10163 space
= isl_map_get_space(map
);
10164 total1
= isl_space_dim(space
, isl_dim_in
);
10165 total2
= isl_space_dim(space
, isl_dim_out
);
10166 space
= isl_space_factor_range(space
);
10167 keep1
= isl_space_dim(space
, isl_dim_in
);
10168 keep2
= isl_space_dim(space
, isl_dim_out
);
10169 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10170 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10171 map
= isl_map_reset_space(map
, space
);
10176 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10178 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10185 if (!isl_space_domain_is_wrapping(map
->dim
))
10186 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10187 "domain is not a product", return isl_map_free(map
));
10189 space
= isl_map_get_space(map
);
10190 total
= isl_space_dim(space
, isl_dim_in
);
10191 space
= isl_space_domain_factor_domain(space
);
10192 keep
= isl_space_dim(space
, isl_dim_in
);
10193 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10194 map
= isl_map_reset_space(map
, space
);
10199 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10201 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10208 if (!isl_space_domain_is_wrapping(map
->dim
))
10209 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10210 "domain is not a product", return isl_map_free(map
));
10212 space
= isl_map_get_space(map
);
10213 total
= isl_space_dim(space
, isl_dim_in
);
10214 space
= isl_space_domain_factor_range(space
);
10215 keep
= isl_space_dim(space
, isl_dim_in
);
10216 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10217 map
= isl_map_reset_space(map
, space
);
10222 /* Given a map A -> [B -> C], extract the map A -> B.
10224 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10231 if (!isl_space_range_is_wrapping(map
->dim
))
10232 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10233 "range is not a product", return isl_map_free(map
));
10235 space
= isl_map_get_space(map
);
10236 total
= isl_space_dim(space
, isl_dim_out
);
10237 space
= isl_space_range_factor_domain(space
);
10238 keep
= isl_space_dim(space
, isl_dim_out
);
10239 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10240 map
= isl_map_reset_space(map
, space
);
10245 /* Given a map A -> [B -> C], extract the map A -> C.
10247 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10254 if (!isl_space_range_is_wrapping(map
->dim
))
10255 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10256 "range is not a product", return isl_map_free(map
));
10258 space
= isl_map_get_space(map
);
10259 total
= isl_space_dim(space
, isl_dim_out
);
10260 space
= isl_space_range_factor_range(space
);
10261 keep
= isl_space_dim(space
, isl_dim_out
);
10262 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10263 map
= isl_map_reset_space(map
, space
);
10268 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10270 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10271 __isl_take isl_map
*map2
)
10275 prod
= isl_map_domain_product(map1
, map2
);
10276 prod
= isl_map_flatten_domain(prod
);
10280 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10282 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10283 __isl_take isl_map
*map2
)
10287 prod
= isl_map_range_product(map1
, map2
);
10288 prod
= isl_map_flatten_range(prod
);
10292 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10295 uint32_t hash
= isl_hash_init();
10300 bmap
= isl_basic_map_copy(bmap
);
10301 bmap
= isl_basic_map_normalize(bmap
);
10304 total
= isl_basic_map_total_dim(bmap
);
10305 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10306 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10308 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10309 isl_hash_hash(hash
, c_hash
);
10311 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10312 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10314 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10315 isl_hash_hash(hash
, c_hash
);
10317 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10318 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10320 if (isl_int_is_zero(bmap
->div
[i
][0]))
10322 isl_hash_byte(hash
, i
& 0xFF);
10323 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10324 isl_hash_hash(hash
, c_hash
);
10326 isl_basic_map_free(bmap
);
10330 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10332 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10335 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10342 map
= isl_map_copy(map
);
10343 map
= isl_map_normalize(map
);
10347 hash
= isl_hash_init();
10348 for (i
= 0; i
< map
->n
; ++i
) {
10349 uint32_t bmap_hash
;
10350 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10351 isl_hash_hash(hash
, bmap_hash
);
10359 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10361 return isl_map_get_hash(set_to_map(set
));
10364 /* Return the number of basic maps in the (current) representation of "map".
10366 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10368 return map
? map
->n
: 0;
10371 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10373 return set
? set
->n
: 0;
10376 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10377 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10382 return isl_stat_error
;
10384 for (i
= 0; i
< map
->n
; ++i
)
10385 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10386 return isl_stat_error
;
10388 return isl_stat_ok
;
10391 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10392 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10397 return isl_stat_error
;
10399 for (i
= 0; i
< set
->n
; ++i
)
10400 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10401 return isl_stat_error
;
10403 return isl_stat_ok
;
10406 /* Return a list of basic sets, the union of which is equal to "set".
10408 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10409 __isl_keep isl_set
*set
)
10412 isl_basic_set_list
*list
;
10417 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10418 for (i
= 0; i
< set
->n
; ++i
) {
10419 isl_basic_set
*bset
;
10421 bset
= isl_basic_set_copy(set
->p
[i
]);
10422 list
= isl_basic_set_list_add(list
, bset
);
10428 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10435 bset
= isl_basic_set_cow(bset
);
10439 space
= isl_basic_set_get_space(bset
);
10440 space
= isl_space_lift(space
, bset
->n_div
);
10443 isl_space_free(bset
->dim
);
10445 bset
->extra
-= bset
->n_div
;
10448 bset
= isl_basic_set_finalize(bset
);
10452 isl_basic_set_free(bset
);
10456 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10462 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10467 set
= isl_set_cow(set
);
10471 n_div
= set
->p
[0]->n_div
;
10472 space
= isl_set_get_space(set
);
10473 space
= isl_space_lift(space
, n_div
);
10476 isl_space_free(set
->dim
);
10479 for (i
= 0; i
< set
->n
; ++i
) {
10480 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10491 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10499 dim
= isl_basic_set_total_dim(bset
);
10500 size
+= bset
->n_eq
* (1 + dim
);
10501 size
+= bset
->n_ineq
* (1 + dim
);
10502 size
+= bset
->n_div
* (2 + dim
);
10507 int isl_set_size(__isl_keep isl_set
*set
)
10515 for (i
= 0; i
< set
->n
; ++i
)
10516 size
+= isl_basic_set_size(set
->p
[i
]);
10521 /* Check if there is any lower bound (if lower == 0) and/or upper
10522 * bound (if upper == 0) on the specified dim.
10524 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10525 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10529 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10530 return isl_bool_error
;
10532 pos
+= isl_basic_map_offset(bmap
, type
);
10534 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10535 if (isl_int_is_zero(bmap
->div
[i
][0]))
10537 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10538 return isl_bool_true
;
10541 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10542 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10543 return isl_bool_true
;
10545 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10546 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10553 return lower
&& upper
;
10556 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10557 enum isl_dim_type type
, unsigned pos
)
10559 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10562 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10563 enum isl_dim_type type
, unsigned pos
)
10565 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10568 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10569 enum isl_dim_type type
, unsigned pos
)
10571 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10574 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10575 enum isl_dim_type type
, unsigned pos
)
10580 return isl_bool_error
;
10582 for (i
= 0; i
< map
->n
; ++i
) {
10584 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10585 if (bounded
< 0 || !bounded
)
10589 return isl_bool_true
;
10592 /* Return true if the specified dim is involved in both an upper bound
10593 * and a lower bound.
10595 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10596 enum isl_dim_type type
, unsigned pos
)
10598 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10601 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10603 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10604 enum isl_dim_type type
, unsigned pos
,
10605 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10606 enum isl_dim_type type
, unsigned pos
))
10611 return isl_bool_error
;
10613 for (i
= 0; i
< map
->n
; ++i
) {
10615 bounded
= fn(map
->p
[i
], type
, pos
);
10616 if (bounded
< 0 || bounded
)
10620 return isl_bool_false
;
10623 /* Return 1 if the specified dim is involved in any lower bound.
10625 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10626 enum isl_dim_type type
, unsigned pos
)
10628 return has_any_bound(set
, type
, pos
,
10629 &isl_basic_map_dim_has_lower_bound
);
10632 /* Return 1 if the specified dim is involved in any upper bound.
10634 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10635 enum isl_dim_type type
, unsigned pos
)
10637 return has_any_bound(set
, type
, pos
,
10638 &isl_basic_map_dim_has_upper_bound
);
10641 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10643 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10644 enum isl_dim_type type
, unsigned pos
,
10645 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10646 enum isl_dim_type type
, unsigned pos
))
10651 return isl_bool_error
;
10653 for (i
= 0; i
< map
->n
; ++i
) {
10655 bounded
= fn(map
->p
[i
], type
, pos
);
10656 if (bounded
< 0 || !bounded
)
10660 return isl_bool_true
;
10663 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10665 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10666 enum isl_dim_type type
, unsigned pos
)
10668 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10671 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10673 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10674 enum isl_dim_type type
, unsigned pos
)
10676 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10679 /* For each of the "n" variables starting at "first", determine
10680 * the sign of the variable and put the results in the first "n"
10681 * elements of the array "signs".
10683 * 1 means that the variable is non-negative
10684 * -1 means that the variable is non-positive
10685 * 0 means the variable attains both positive and negative values.
10687 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10688 unsigned first
, unsigned n
, int *signs
)
10690 isl_vec
*bound
= NULL
;
10691 struct isl_tab
*tab
= NULL
;
10692 struct isl_tab_undo
*snap
;
10695 if (!bset
|| !signs
)
10696 return isl_stat_error
;
10698 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10699 tab
= isl_tab_from_basic_set(bset
, 0);
10700 if (!bound
|| !tab
)
10703 isl_seq_clr(bound
->el
, bound
->size
);
10704 isl_int_set_si(bound
->el
[0], -1);
10706 snap
= isl_tab_snap(tab
);
10707 for (i
= 0; i
< n
; ++i
) {
10710 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10711 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10713 empty
= tab
->empty
;
10714 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10715 if (isl_tab_rollback(tab
, snap
) < 0)
10723 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10724 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10726 empty
= tab
->empty
;
10727 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10728 if (isl_tab_rollback(tab
, snap
) < 0)
10731 signs
[i
] = empty
? -1 : 0;
10735 isl_vec_free(bound
);
10736 return isl_stat_ok
;
10739 isl_vec_free(bound
);
10740 return isl_stat_error
;
10743 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10744 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10746 if (!bset
|| !signs
)
10747 return isl_stat_error
;
10748 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
10749 return isl_stat_error
;
10751 first
+= pos(bset
->dim
, type
) - 1;
10752 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10755 /* Is it possible for the integer division "div" to depend (possibly
10756 * indirectly) on any output dimensions?
10758 * If the div is undefined, then we conservatively assume that it
10759 * may depend on them.
10760 * Otherwise, we check if it actually depends on them or on any integer
10761 * divisions that may depend on them.
10763 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10766 unsigned n_out
, o_out
;
10767 unsigned n_div
, o_div
;
10769 if (isl_int_is_zero(bmap
->div
[div
][0]))
10770 return isl_bool_true
;
10772 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10773 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10775 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10776 return isl_bool_true
;
10778 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10779 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10781 for (i
= 0; i
< n_div
; ++i
) {
10782 isl_bool may_involve
;
10784 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10786 may_involve
= div_may_involve_output(bmap
, i
);
10787 if (may_involve
< 0 || may_involve
)
10788 return may_involve
;
10791 return isl_bool_false
;
10794 /* Return the first integer division of "bmap" in the range
10795 * [first, first + n[ that may depend on any output dimensions and
10796 * that has a non-zero coefficient in "c" (where the first coefficient
10797 * in "c" corresponds to integer division "first").
10799 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10800 isl_int
*c
, int first
, int n
)
10807 for (k
= first
; k
< first
+ n
; ++k
) {
10808 isl_bool may_involve
;
10810 if (isl_int_is_zero(c
[k
]))
10812 may_involve
= div_may_involve_output(bmap
, k
);
10813 if (may_involve
< 0)
10822 /* Look for a pair of inequality constraints in "bmap" of the form
10824 * -l + i >= 0 or i >= l
10826 * n + l - i >= 0 or i <= l + n
10828 * with n < "m" and i the output dimension at position "pos".
10829 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10830 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10831 * and earlier output dimensions, as well as integer divisions that do
10832 * not involve any of the output dimensions.
10834 * Return the index of the first inequality constraint or bmap->n_ineq
10835 * if no such pair can be found.
10837 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10838 int pos
, isl_int m
)
10843 unsigned n_div
, o_div
;
10844 unsigned n_out
, o_out
;
10850 ctx
= isl_basic_map_get_ctx(bmap
);
10851 total
= isl_basic_map_total_dim(bmap
);
10852 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10853 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10854 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10855 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10856 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10857 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10859 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10860 n_out
- (pos
+ 1)) != -1)
10862 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10865 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10866 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10869 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10870 bmap
->ineq
[j
] + 1, total
))
10874 if (j
>= bmap
->n_ineq
)
10876 isl_int_add(bmap
->ineq
[i
][0],
10877 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10878 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10879 isl_int_sub(bmap
->ineq
[i
][0],
10880 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10883 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10889 return bmap
->n_ineq
;
10892 /* Return the index of the equality of "bmap" that defines
10893 * the output dimension "pos" in terms of earlier dimensions.
10894 * The equality may also involve integer divisions, as long
10895 * as those integer divisions are defined in terms of
10896 * parameters or input dimensions.
10897 * In this case, *div is set to the number of integer divisions and
10898 * *ineq is set to the number of inequality constraints (provided
10899 * div and ineq are not NULL).
10901 * The equality may also involve a single integer division involving
10902 * the output dimensions (typically only output dimension "pos") as
10903 * long as the coefficient of output dimension "pos" is 1 or -1 and
10904 * there is a pair of constraints i >= l and i <= l + n, with i referring
10905 * to output dimension "pos", l an expression involving only earlier
10906 * dimensions and n smaller than the coefficient of the integer division
10907 * in the equality. In this case, the output dimension can be defined
10908 * in terms of a modulo expression that does not involve the integer division.
10909 * *div is then set to this single integer division and
10910 * *ineq is set to the index of constraint i >= l.
10912 * Return bmap->n_eq if there is no such equality.
10913 * Return -1 on error.
10915 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10916 int pos
, int *div
, int *ineq
)
10919 unsigned n_out
, o_out
;
10920 unsigned n_div
, o_div
;
10925 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10926 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10927 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10928 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10931 *ineq
= bmap
->n_ineq
;
10934 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10935 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10937 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10938 n_out
- (pos
+ 1)) != -1)
10940 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10944 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10945 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10947 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10948 k
+ 1, n_div
- (k
+1)) < n_div
)
10950 l
= find_modulo_constraint_pair(bmap
, pos
,
10951 bmap
->eq
[j
][o_div
+ k
]);
10954 if (l
>= bmap
->n_ineq
)
10966 /* Check if the given basic map is obviously single-valued.
10967 * In particular, for each output dimension, check that there is
10968 * an equality that defines the output dimension in terms of
10969 * earlier dimensions.
10971 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10977 return isl_bool_error
;
10979 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10981 for (i
= 0; i
< n_out
; ++i
) {
10984 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10987 return isl_bool_error
;
10988 if (eq
>= bmap
->n_eq
)
10989 return isl_bool_false
;
10992 return isl_bool_true
;
10995 /* Check if the given basic map is single-valued.
10996 * We simply compute
11000 * and check if the result is a subset of the identity mapping.
11002 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11005 isl_basic_map
*test
;
11009 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11013 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11014 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11016 space
= isl_basic_map_get_space(bmap
);
11017 space
= isl_space_map_from_set(isl_space_range(space
));
11018 id
= isl_basic_map_identity(space
);
11020 sv
= isl_basic_map_is_subset(test
, id
);
11022 isl_basic_map_free(test
);
11023 isl_basic_map_free(id
);
11028 /* Check if the given map is obviously single-valued.
11030 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11033 return isl_bool_error
;
11035 return isl_bool_true
;
11037 return isl_bool_false
;
11039 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11042 /* Check if the given map is single-valued.
11043 * We simply compute
11047 * and check if the result is a subset of the identity mapping.
11049 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11056 sv
= isl_map_plain_is_single_valued(map
);
11060 test
= isl_map_reverse(isl_map_copy(map
));
11061 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11063 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11064 id
= isl_map_identity(dim
);
11066 sv
= isl_map_is_subset(test
, id
);
11068 isl_map_free(test
);
11074 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11078 map
= isl_map_copy(map
);
11079 map
= isl_map_reverse(map
);
11080 in
= isl_map_is_single_valued(map
);
11086 /* Check if the given map is obviously injective.
11088 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11092 map
= isl_map_copy(map
);
11093 map
= isl_map_reverse(map
);
11094 in
= isl_map_plain_is_single_valued(map
);
11100 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11104 sv
= isl_map_is_single_valued(map
);
11108 return isl_map_is_injective(map
);
11111 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11113 return isl_map_is_single_valued(set_to_map(set
));
11116 /* Does "map" only map elements to themselves?
11118 * If the domain and range spaces are different, then "map"
11119 * is considered not to be an identity relation, even if it is empty.
11120 * Otherwise, construct the maximal identity relation and
11121 * check whether "map" is a subset of this relation.
11123 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11127 isl_bool equal
, is_identity
;
11129 space
= isl_map_get_space(map
);
11130 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11131 isl_space_free(space
);
11132 if (equal
< 0 || !equal
)
11135 id
= isl_map_identity(isl_map_get_space(map
));
11136 is_identity
= isl_map_is_subset(map
, id
);
11139 return is_identity
;
11142 int isl_map_is_translation(__isl_keep isl_map
*map
)
11147 delta
= isl_map_deltas(isl_map_copy(map
));
11148 ok
= isl_set_is_singleton(delta
);
11149 isl_set_free(delta
);
11154 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11156 if (isl_seq_first_non_zero(p
, pos
) != -1)
11158 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11163 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11170 return isl_bool_error
;
11172 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11173 return isl_bool_false
;
11175 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11176 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11177 for (j
= 0; j
< nvar
; ++j
) {
11178 int lower
= 0, upper
= 0;
11179 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11180 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11182 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11183 return isl_bool_false
;
11186 if (i
< bset
->n_eq
)
11188 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11189 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11191 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11192 return isl_bool_false
;
11193 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11198 if (!lower
|| !upper
)
11199 return isl_bool_false
;
11202 return isl_bool_true
;
11205 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11208 return isl_bool_error
;
11210 return isl_bool_false
;
11212 return isl_basic_set_is_box(set
->p
[0]);
11215 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11218 return isl_bool_error
;
11220 return isl_space_is_wrapping(bset
->dim
);
11223 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11226 return isl_bool_error
;
11228 return isl_space_is_wrapping(set
->dim
);
11231 /* Modify the space of "map" through a call to "change".
11232 * If "can_change" is set (not NULL), then first call it to check
11233 * if the modification is allowed, printing the error message "cannot_change"
11236 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11237 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11238 const char *cannot_change
,
11239 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11247 ok
= can_change
? can_change(map
) : isl_bool_true
;
11249 return isl_map_free(map
);
11251 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11252 return isl_map_free(map
));
11254 space
= change(isl_map_get_space(map
));
11255 map
= isl_map_reset_space(map
, space
);
11260 /* Is the domain of "map" a wrapped relation?
11262 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11265 return isl_bool_error
;
11267 return isl_space_domain_is_wrapping(map
->dim
);
11270 /* Does "map" have a wrapped relation in both domain and range?
11272 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11274 return isl_space_is_product(isl_map_peek_space(map
));
11277 /* Is the range of "map" a wrapped relation?
11279 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11282 return isl_bool_error
;
11284 return isl_space_range_is_wrapping(map
->dim
);
11287 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11289 bmap
= isl_basic_map_cow(bmap
);
11293 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11297 bmap
= isl_basic_map_finalize(bmap
);
11299 return bset_from_bmap(bmap
);
11301 isl_basic_map_free(bmap
);
11305 /* Given a map A -> B, return the set (A -> B).
11307 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11309 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11312 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11314 bset
= isl_basic_set_cow(bset
);
11318 bset
->dim
= isl_space_unwrap(bset
->dim
);
11322 bset
= isl_basic_set_finalize(bset
);
11324 return bset_to_bmap(bset
);
11326 isl_basic_set_free(bset
);
11330 /* Given a set (A -> B), return the map A -> B.
11331 * Error out if "set" is not of the form (A -> B).
11333 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11335 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11336 "not a wrapping set", &isl_space_unwrap
);
11339 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11340 enum isl_dim_type type
)
11345 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11348 bmap
= isl_basic_map_cow(bmap
);
11352 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11356 bmap
= isl_basic_map_finalize(bmap
);
11360 isl_basic_map_free(bmap
);
11364 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11365 enum isl_dim_type type
)
11372 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11375 map
= isl_map_cow(map
);
11379 for (i
= 0; i
< map
->n
; ++i
) {
11380 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11384 map
->dim
= isl_space_reset(map
->dim
, type
);
11394 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11399 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11402 bmap
= isl_basic_map_cow(bmap
);
11406 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11410 bmap
= isl_basic_map_finalize(bmap
);
11414 isl_basic_map_free(bmap
);
11418 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11420 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11423 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11424 __isl_take isl_basic_map
*bmap
)
11429 if (!bmap
->dim
->nested
[0])
11432 bmap
= isl_basic_map_cow(bmap
);
11436 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11440 bmap
= isl_basic_map_finalize(bmap
);
11444 isl_basic_map_free(bmap
);
11448 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11449 __isl_take isl_basic_map
*bmap
)
11454 if (!bmap
->dim
->nested
[1])
11457 bmap
= isl_basic_map_cow(bmap
);
11461 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11465 bmap
= isl_basic_map_finalize(bmap
);
11469 isl_basic_map_free(bmap
);
11473 /* Remove any internal structure from the spaces of domain and range of "map".
11475 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11480 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11483 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11486 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11488 return set_from_map(isl_map_flatten(set_to_map(set
)));
11491 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11493 isl_space
*space
, *flat_space
;
11496 space
= isl_set_get_space(set
);
11497 flat_space
= isl_space_flatten(isl_space_copy(space
));
11498 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
11500 map
= isl_map_intersect_domain(map
, set
);
11505 /* Remove any internal structure from the space of the domain of "map".
11507 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11512 if (!map
->dim
->nested
[0])
11515 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11518 /* Remove any internal structure from the space of the range of "map".
11520 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11525 if (!map
->dim
->nested
[1])
11528 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11531 /* Reorder the dimensions of "bmap" according to the given dim_map
11532 * and set the dimension specification to "space" and
11533 * perform Gaussian elimination on the result.
11535 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11536 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
11538 isl_basic_map
*res
;
11542 if (!bmap
|| !space
|| !dim_map
)
11545 flags
= bmap
->flags
;
11546 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11547 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
11548 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11549 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11550 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11551 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11553 res
->flags
= flags
;
11554 res
= isl_basic_map_gauss(res
, NULL
);
11555 res
= isl_basic_map_finalize(res
);
11558 isl_dim_map_free(dim_map
);
11559 isl_basic_map_free(bmap
);
11560 isl_space_free(space
);
11564 /* Reorder the dimensions of "map" according to given reordering.
11566 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11567 __isl_take isl_reordering
*r
)
11570 struct isl_dim_map
*dim_map
;
11572 map
= isl_map_cow(map
);
11573 dim_map
= isl_dim_map_from_reordering(r
);
11574 if (!map
|| !r
|| !dim_map
)
11577 for (i
= 0; i
< map
->n
; ++i
) {
11578 struct isl_dim_map
*dim_map_i
;
11581 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11583 space
= isl_reordering_get_space(r
);
11584 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
11590 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
11591 map
= isl_map_unmark_normalized(map
);
11593 isl_reordering_free(r
);
11594 isl_dim_map_free(dim_map
);
11597 isl_dim_map_free(dim_map
);
11599 isl_reordering_free(r
);
11603 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11604 __isl_take isl_reordering
*r
)
11606 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11609 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11610 __isl_take isl_space
*model
)
11615 if (!map
|| !model
)
11618 ctx
= isl_space_get_ctx(model
);
11619 if (!isl_space_has_named_params(model
))
11620 isl_die(ctx
, isl_error_invalid
,
11621 "model has unnamed parameters", goto error
);
11622 if (isl_map_check_named_params(map
) < 0)
11624 aligned
= isl_map_space_has_equal_params(map
, model
);
11628 isl_reordering
*exp
;
11630 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11631 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11632 map
= isl_map_realign(map
, exp
);
11635 isl_space_free(model
);
11638 isl_space_free(model
);
11643 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11644 __isl_take isl_space
*model
)
11646 return isl_map_align_params(set
, model
);
11649 /* Align the parameters of "bmap" to those of "model", introducing
11650 * additional parameters if needed.
11652 __isl_give isl_basic_map
*isl_basic_map_align_params(
11653 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11656 isl_bool equal_params
;
11658 if (!bmap
|| !model
)
11661 ctx
= isl_space_get_ctx(model
);
11662 if (!isl_space_has_named_params(model
))
11663 isl_die(ctx
, isl_error_invalid
,
11664 "model has unnamed parameters", goto error
);
11665 if (isl_basic_map_check_named_params(bmap
) < 0)
11667 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11668 if (equal_params
< 0)
11670 if (!equal_params
) {
11671 isl_reordering
*exp
;
11672 struct isl_dim_map
*dim_map
;
11674 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11675 exp
= isl_reordering_extend_space(exp
,
11676 isl_basic_map_get_space(bmap
));
11677 dim_map
= isl_dim_map_from_reordering(exp
);
11678 bmap
= isl_basic_map_realign(bmap
,
11679 isl_reordering_get_space(exp
),
11680 isl_dim_map_extend(dim_map
, bmap
));
11681 isl_reordering_free(exp
);
11682 isl_dim_map_free(dim_map
);
11685 isl_space_free(model
);
11688 isl_space_free(model
);
11689 isl_basic_map_free(bmap
);
11693 /* Do "bset" and "space" have the same parameters?
11695 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11696 __isl_keep isl_space
*space
)
11698 isl_space
*bset_space
;
11700 bset_space
= isl_basic_set_peek_space(bset
);
11701 return isl_space_has_equal_params(bset_space
, space
);
11704 /* Do "map" and "space" have the same parameters?
11706 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11707 __isl_keep isl_space
*space
)
11709 isl_space
*map_space
;
11711 map_space
= isl_map_peek_space(map
);
11712 return isl_space_has_equal_params(map_space
, space
);
11715 /* Do "set" and "space" have the same parameters?
11717 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11718 __isl_keep isl_space
*space
)
11720 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11723 /* Align the parameters of "bset" to those of "model", introducing
11724 * additional parameters if needed.
11726 __isl_give isl_basic_set
*isl_basic_set_align_params(
11727 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11729 return isl_basic_map_align_params(bset
, model
);
11732 /* Drop all parameters not referenced by "map".
11734 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
11738 if (isl_map_check_named_params(map
) < 0)
11739 return isl_map_free(map
);
11741 for (i
= isl_map_dim(map
, isl_dim_param
) - 1; i
>= 0; i
--) {
11744 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
11746 return isl_map_free(map
);
11748 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
11754 /* Drop all parameters not referenced by "set".
11756 __isl_give isl_set
*isl_set_drop_unused_params(
11757 __isl_take isl_set
*set
)
11759 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
11762 /* Drop all parameters not referenced by "bmap".
11764 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
11765 __isl_take isl_basic_map
*bmap
)
11769 if (isl_basic_map_check_named_params(bmap
) < 0)
11770 return isl_basic_map_free(bmap
);
11772 for (i
= isl_basic_map_dim(bmap
, isl_dim_param
) - 1; i
>= 0; i
--) {
11775 involves
= isl_basic_map_involves_dims(bmap
,
11776 isl_dim_param
, i
, 1);
11778 return isl_basic_map_free(bmap
);
11780 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
11786 /* Drop all parameters not referenced by "bset".
11788 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
11789 __isl_take isl_basic_set
*bset
)
11791 return bset_from_bmap(isl_basic_map_drop_unused_params(
11792 bset_to_bmap(bset
)));
11795 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11796 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11797 enum isl_dim_type c2
, enum isl_dim_type c3
,
11798 enum isl_dim_type c4
, enum isl_dim_type c5
)
11800 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11801 struct isl_mat
*mat
;
11807 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11808 isl_basic_map_total_dim(bmap
) + 1);
11811 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11812 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11813 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11814 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11815 isl_int_set(mat
->row
[i
][pos
],
11816 bmap
->eq
[i
][off
+ k
]);
11824 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11825 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11826 enum isl_dim_type c2
, enum isl_dim_type c3
,
11827 enum isl_dim_type c4
, enum isl_dim_type c5
)
11829 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11830 struct isl_mat
*mat
;
11836 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11837 isl_basic_map_total_dim(bmap
) + 1);
11840 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11841 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11842 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11843 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11844 isl_int_set(mat
->row
[i
][pos
],
11845 bmap
->ineq
[i
][off
+ k
]);
11853 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11854 __isl_take isl_space
*space
,
11855 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11856 enum isl_dim_type c2
, enum isl_dim_type c3
,
11857 enum isl_dim_type c4
, enum isl_dim_type c5
)
11859 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11860 isl_basic_map
*bmap
= NULL
;
11866 if (!space
|| !eq
|| !ineq
)
11869 if (eq
->n_col
!= ineq
->n_col
)
11870 isl_die(space
->ctx
, isl_error_invalid
,
11871 "equalities and inequalities matrices should have "
11872 "same number of columns", goto error
);
11874 total
= 1 + isl_space_dim(space
, isl_dim_all
);
11876 if (eq
->n_col
< total
)
11877 isl_die(space
->ctx
, isl_error_invalid
,
11878 "number of columns too small", goto error
);
11880 extra
= eq
->n_col
- total
;
11882 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
11883 eq
->n_row
, ineq
->n_row
);
11886 for (i
= 0; i
< extra
; ++i
) {
11887 k
= isl_basic_map_alloc_div(bmap
);
11890 isl_int_set_si(bmap
->div
[k
][0], 0);
11892 for (i
= 0; i
< eq
->n_row
; ++i
) {
11893 l
= isl_basic_map_alloc_equality(bmap
);
11896 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11897 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11898 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11899 isl_int_set(bmap
->eq
[l
][off
+ k
],
11905 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11906 l
= isl_basic_map_alloc_inequality(bmap
);
11909 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11910 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11911 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11912 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11913 ineq
->row
[i
][pos
]);
11919 isl_space_free(space
);
11921 isl_mat_free(ineq
);
11923 bmap
= isl_basic_map_simplify(bmap
);
11924 return isl_basic_map_finalize(bmap
);
11926 isl_space_free(space
);
11928 isl_mat_free(ineq
);
11929 isl_basic_map_free(bmap
);
11933 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11934 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11935 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11937 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11938 c1
, c2
, c3
, c4
, isl_dim_in
);
11941 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11942 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11943 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11945 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11946 c1
, c2
, c3
, c4
, isl_dim_in
);
11949 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11950 __isl_take isl_space
*dim
,
11951 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11952 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11954 isl_basic_map
*bmap
;
11955 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11956 c1
, c2
, c3
, c4
, isl_dim_in
);
11957 return bset_from_bmap(bmap
);
11960 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11963 return isl_bool_error
;
11965 return isl_space_can_zip(bmap
->dim
);
11968 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11971 return isl_bool_error
;
11973 return isl_space_can_zip(map
->dim
);
11976 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11977 * (A -> C) -> (B -> D).
11979 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11988 if (!isl_basic_map_can_zip(bmap
))
11989 isl_die(bmap
->ctx
, isl_error_invalid
,
11990 "basic map cannot be zipped", goto error
);
11991 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11992 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11993 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11994 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11995 bmap
= isl_basic_map_cow(bmap
);
11996 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11999 bmap
->dim
= isl_space_zip(bmap
->dim
);
12002 bmap
= isl_basic_map_mark_final(bmap
);
12005 isl_basic_map_free(bmap
);
12009 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12010 * (A -> C) -> (B -> D).
12012 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12019 if (!isl_map_can_zip(map
))
12020 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12023 map
= isl_map_cow(map
);
12027 for (i
= 0; i
< map
->n
; ++i
) {
12028 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12033 map
->dim
= isl_space_zip(map
->dim
);
12043 /* Can we apply isl_basic_map_curry to "bmap"?
12044 * That is, does it have a nested relation in its domain?
12046 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12049 return isl_bool_error
;
12051 return isl_space_can_curry(bmap
->dim
);
12054 /* Can we apply isl_map_curry to "map"?
12055 * That is, does it have a nested relation in its domain?
12057 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12060 return isl_bool_error
;
12062 return isl_space_can_curry(map
->dim
);
12065 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12068 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12074 if (!isl_basic_map_can_curry(bmap
))
12075 isl_die(bmap
->ctx
, isl_error_invalid
,
12076 "basic map cannot be curried", goto error
);
12077 bmap
= isl_basic_map_cow(bmap
);
12080 bmap
->dim
= isl_space_curry(bmap
->dim
);
12083 bmap
= isl_basic_map_mark_final(bmap
);
12086 isl_basic_map_free(bmap
);
12090 /* Given a map (A -> B) -> C, return the corresponding map
12093 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12095 return isl_map_change_space(map
, &isl_map_can_curry
,
12096 "map cannot be curried", &isl_space_curry
);
12099 /* Can isl_map_range_curry be applied to "map"?
12100 * That is, does it have a nested relation in its range,
12101 * the domain of which is itself a nested relation?
12103 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12106 return isl_bool_error
;
12108 return isl_space_can_range_curry(map
->dim
);
12111 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12112 * A -> (B -> (C -> D)).
12114 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12116 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12117 "map range cannot be curried",
12118 &isl_space_range_curry
);
12121 /* Can we apply isl_basic_map_uncurry to "bmap"?
12122 * That is, does it have a nested relation in its domain?
12124 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12127 return isl_bool_error
;
12129 return isl_space_can_uncurry(bmap
->dim
);
12132 /* Can we apply isl_map_uncurry to "map"?
12133 * That is, does it have a nested relation in its domain?
12135 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12138 return isl_bool_error
;
12140 return isl_space_can_uncurry(map
->dim
);
12143 /* Given a basic map A -> (B -> C), return the corresponding basic map
12146 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12152 if (!isl_basic_map_can_uncurry(bmap
))
12153 isl_die(bmap
->ctx
, isl_error_invalid
,
12154 "basic map cannot be uncurried",
12155 return isl_basic_map_free(bmap
));
12156 bmap
= isl_basic_map_cow(bmap
);
12159 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12161 return isl_basic_map_free(bmap
);
12162 bmap
= isl_basic_map_mark_final(bmap
);
12166 /* Given a map A -> (B -> C), return the corresponding map
12169 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12171 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12172 "map cannot be uncurried", &isl_space_uncurry
);
12175 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12176 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12178 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12181 /* Construct a basic map where the given dimensions are equal to each other.
12183 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12184 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12186 isl_basic_map
*bmap
= NULL
;
12189 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12190 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12193 if (type1
== type2
&& pos1
== pos2
)
12194 return isl_basic_map_universe(space
);
12196 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12197 i
= isl_basic_map_alloc_equality(bmap
);
12200 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12201 pos1
+= isl_basic_map_offset(bmap
, type1
);
12202 pos2
+= isl_basic_map_offset(bmap
, type2
);
12203 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12204 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12205 bmap
= isl_basic_map_finalize(bmap
);
12206 isl_space_free(space
);
12209 isl_space_free(space
);
12210 isl_basic_map_free(bmap
);
12214 /* Add a constraint imposing that the given two dimensions are equal.
12216 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12217 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12221 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12223 bmap
= isl_basic_map_intersect(bmap
, eq
);
12228 /* Add a constraint imposing that the given two dimensions are equal.
12230 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12231 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12233 isl_basic_map
*bmap
;
12235 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12237 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12242 /* Add a constraint imposing that the given two dimensions have opposite values.
12244 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12245 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12247 isl_basic_map
*bmap
= NULL
;
12250 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
12251 return isl_map_free(map
);
12252 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
12253 return isl_map_free(map
);
12255 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12256 i
= isl_basic_map_alloc_equality(bmap
);
12259 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12260 pos1
+= isl_basic_map_offset(bmap
, type1
);
12261 pos2
+= isl_basic_map_offset(bmap
, type2
);
12262 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12263 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12264 bmap
= isl_basic_map_finalize(bmap
);
12266 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12270 isl_basic_map_free(bmap
);
12275 /* Construct a constraint imposing that the value of the first dimension is
12276 * greater than or equal to that of the second.
12278 static __isl_give isl_constraint
*constraint_order_ge(
12279 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12280 enum isl_dim_type type2
, int pos2
)
12284 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12285 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12286 space
= isl_space_free(space
);
12290 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12292 if (type1
== type2
&& pos1
== pos2
)
12295 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12296 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12301 /* Add a constraint imposing that the value of the first dimension is
12302 * greater than or equal to that of the second.
12304 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12305 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12310 if (type1
== type2
&& pos1
== pos2
)
12312 space
= isl_basic_map_get_space(bmap
);
12313 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12314 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12319 /* Add a constraint imposing that the value of the first dimension is
12320 * greater than or equal to that of the second.
12322 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12323 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12328 if (type1
== type2
&& pos1
== pos2
)
12330 space
= isl_map_get_space(map
);
12331 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12332 map
= isl_map_add_constraint(map
, c
);
12337 /* Add a constraint imposing that the value of the first dimension is
12338 * less than or equal to that of the second.
12340 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12341 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12343 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12346 /* Construct a basic map where the value of the first dimension is
12347 * greater than that of the second.
12349 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12350 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12352 isl_basic_map
*bmap
= NULL
;
12355 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12356 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12359 if (type1
== type2
&& pos1
== pos2
)
12360 return isl_basic_map_empty(space
);
12362 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12363 i
= isl_basic_map_alloc_inequality(bmap
);
12365 return isl_basic_map_free(bmap
);
12366 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12367 pos1
+= isl_basic_map_offset(bmap
, type1
);
12368 pos2
+= isl_basic_map_offset(bmap
, type2
);
12369 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12370 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12371 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12372 bmap
= isl_basic_map_finalize(bmap
);
12376 isl_space_free(space
);
12377 isl_basic_map_free(bmap
);
12381 /* Add a constraint imposing that the value of the first dimension is
12382 * greater than that of the second.
12384 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12385 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12389 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12391 bmap
= isl_basic_map_intersect(bmap
, gt
);
12396 /* Add a constraint imposing that the value of the first dimension is
12397 * greater than that of the second.
12399 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12400 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12402 isl_basic_map
*bmap
;
12404 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12406 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12411 /* Add a constraint imposing that the value of the first dimension is
12412 * smaller than that of the second.
12414 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12415 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12417 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12420 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12424 isl_local_space
*ls
;
12429 if (!isl_basic_map_divs_known(bmap
))
12430 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12431 "some divs are unknown", return NULL
);
12433 ls
= isl_basic_map_get_local_space(bmap
);
12434 div
= isl_local_space_get_div(ls
, pos
);
12435 isl_local_space_free(ls
);
12440 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12443 return isl_basic_map_get_div(bset
, pos
);
12446 /* Plug in "subs" for dimension "type", "pos" of "bset".
12448 * Let i be the dimension to replace and let "subs" be of the form
12452 * Any integer division with a non-zero coefficient for i,
12454 * floor((a i + g)/m)
12458 * floor((a f + d g)/(m d))
12460 * Constraints of the form
12468 * We currently require that "subs" is an integral expression.
12469 * Handling rational expressions may require us to add stride constraints
12470 * as we do in isl_basic_set_preimage_multi_aff.
12472 __isl_give isl_basic_set
*isl_basic_set_substitute(
12473 __isl_take isl_basic_set
*bset
,
12474 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12480 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12483 bset
= isl_basic_set_cow(bset
);
12484 if (!bset
|| !subs
)
12487 ctx
= isl_basic_set_get_ctx(bset
);
12488 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12489 isl_die(ctx
, isl_error_invalid
,
12490 "spaces don't match", goto error
);
12491 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12492 isl_die(ctx
, isl_error_unsupported
,
12493 "cannot handle divs yet", goto error
);
12494 if (!isl_int_is_one(subs
->v
->el
[0]))
12495 isl_die(ctx
, isl_error_invalid
,
12496 "can only substitute integer expressions", goto error
);
12498 pos
+= isl_basic_set_offset(bset
, type
);
12502 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12503 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12505 isl_int_set(v
, bset
->eq
[i
][pos
]);
12506 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12507 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12508 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12511 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12512 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12514 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12515 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12516 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12517 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12520 for (i
= 0; i
< bset
->n_div
; ++i
) {
12521 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12523 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12524 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12525 isl_seq_combine(bset
->div
[i
] + 1,
12526 subs
->v
->el
[0], bset
->div
[i
] + 1,
12527 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12528 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12533 bset
= isl_basic_set_simplify(bset
);
12534 return isl_basic_set_finalize(bset
);
12536 isl_basic_set_free(bset
);
12540 /* Plug in "subs" for dimension "type", "pos" of "set".
12542 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12543 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12547 if (set
&& isl_set_plain_is_empty(set
))
12550 set
= isl_set_cow(set
);
12554 for (i
= set
->n
- 1; i
>= 0; --i
) {
12555 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12556 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
12567 /* Check if the range of "ma" is compatible with the domain or range
12568 * (depending on "type") of "bmap".
12570 static isl_stat
check_basic_map_compatible_range_multi_aff(
12571 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12572 __isl_keep isl_multi_aff
*ma
)
12575 isl_space
*ma_space
;
12577 ma_space
= isl_multi_aff_get_space(ma
);
12579 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12583 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12584 "parameters don't match", goto error
);
12585 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12589 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12590 "spaces don't match", goto error
);
12592 isl_space_free(ma_space
);
12593 return isl_stat_ok
;
12595 isl_space_free(ma_space
);
12596 return isl_stat_error
;
12599 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12600 * coefficients before the transformed range of dimensions,
12601 * the "n_after" coefficients after the transformed range of dimensions
12602 * and the coefficients of the other divs in "bmap".
12604 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
12605 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12610 isl_local_space
*ls
;
12615 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
12617 return isl_basic_map_free(bmap
);
12619 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12620 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12621 for (i
= 0; i
< n_div
; ++i
) {
12622 int o_bmap
= 0, o_ls
= 0;
12624 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12625 o_bmap
+= 1 + 1 + n_param
;
12626 o_ls
+= 1 + 1 + n_param
;
12627 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12628 o_bmap
+= n_before
;
12629 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12630 ls
->div
->row
[i
] + o_ls
, n_set
);
12633 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12635 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12636 ls
->div
->row
[i
] + o_ls
, n_div
);
12639 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12640 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
12645 isl_local_space_free(ls
);
12648 isl_local_space_free(ls
);
12649 return isl_basic_map_free(bmap
);
12652 /* How many stride constraints does "ma" enforce?
12653 * That is, how many of the affine expressions have a denominator
12654 * different from one?
12656 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12661 for (i
= 0; i
< ma
->n
; ++i
)
12662 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12668 /* For each affine expression in ma of the form
12670 * x_i = (f_i y + h_i)/m_i
12672 * with m_i different from one, add a constraint to "bmap"
12675 * f_i y + h_i = m_i alpha_i
12677 * with alpha_i an additional existentially quantified variable.
12679 * The input variables of "ma" correspond to a subset of the variables
12680 * of "bmap". There are "n_before" variables in "bmap" before this
12681 * subset and "n_after" variables after this subset.
12682 * The integer divisions of the affine expressions in "ma" are assumed
12683 * to have been aligned. There are "n_div_ma" of them and
12684 * they appear first in "bmap", straight after the "n_after" variables.
12686 static __isl_give isl_basic_map
*add_ma_strides(
12687 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12688 int n_before
, int n_after
, int n_div_ma
)
12696 total
= isl_basic_map_total_dim(bmap
);
12697 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12698 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12699 for (i
= 0; i
< ma
->n
; ++i
) {
12700 int o_bmap
= 0, o_ma
= 1;
12702 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
12704 div
= isl_basic_map_alloc_div(bmap
);
12705 k
= isl_basic_map_alloc_equality(bmap
);
12706 if (div
< 0 || k
< 0)
12708 isl_int_set_si(bmap
->div
[div
][0], 0);
12709 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12710 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12711 o_bmap
+= 1 + n_param
;
12712 o_ma
+= 1 + n_param
;
12713 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12714 o_bmap
+= n_before
;
12715 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12716 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
12719 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12721 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12722 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12723 o_bmap
+= n_div_ma
;
12725 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12726 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
12732 isl_basic_map_free(bmap
);
12736 /* Replace the domain or range space (depending on "type) of "space" by "set".
12738 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12739 enum isl_dim_type type
, __isl_take isl_space
*set
)
12741 if (type
== isl_dim_in
) {
12742 space
= isl_space_range(space
);
12743 space
= isl_space_map_from_domain_and_range(set
, space
);
12745 space
= isl_space_domain(space
);
12746 space
= isl_space_map_from_domain_and_range(space
, set
);
12752 /* Compute the preimage of the domain or range (depending on "type")
12753 * of "bmap" under the function represented by "ma".
12754 * In other words, plug in "ma" in the domain or range of "bmap".
12755 * The result is a basic map that lives in the same space as "bmap"
12756 * except that the domain or range has been replaced by
12757 * the domain space of "ma".
12759 * If bmap is represented by
12761 * A(p) + S u + B x + T v + C(divs) >= 0,
12763 * where u and x are input and output dimensions if type == isl_dim_out
12764 * while x and v are input and output dimensions if type == isl_dim_in,
12765 * and ma is represented by
12767 * x = D(p) + F(y) + G(divs')
12769 * then the result is
12771 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12773 * The divs in the input set are similarly adjusted.
12776 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12780 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12781 * B_i G(divs') + c_i(divs))/n_i)
12783 * If bmap is not a rational map and if F(y) involves any denominators
12785 * x_i = (f_i y + h_i)/m_i
12787 * then additional constraints are added to ensure that we only
12788 * map back integer points. That is we enforce
12790 * f_i y + h_i = m_i alpha_i
12792 * with alpha_i an additional existentially quantified variable.
12794 * We first copy over the divs from "ma".
12795 * Then we add the modified constraints and divs from "bmap".
12796 * Finally, we add the stride constraints, if needed.
12798 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12799 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12800 __isl_take isl_multi_aff
*ma
)
12804 isl_basic_map
*res
= NULL
;
12805 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12806 isl_int f
, c1
, c2
, g
;
12815 ma
= isl_multi_aff_align_divs(ma
);
12818 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12821 if (type
== isl_dim_in
) {
12823 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12825 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12828 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12829 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
12831 space
= isl_multi_aff_get_domain_space(ma
);
12832 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12833 rational
= isl_basic_map_is_rational(bmap
);
12834 strides
= rational
? 0 : multi_aff_strides(ma
);
12835 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12836 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12838 res
= isl_basic_map_set_rational(res
);
12840 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12841 if (isl_basic_map_alloc_div(res
) < 0)
12844 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
12848 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12849 k
= isl_basic_map_alloc_equality(res
);
12852 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12853 n_after
, n_div_ma
, n_div_bmap
,
12854 f
, c1
, c2
, g
, 0) < 0)
12858 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12859 k
= isl_basic_map_alloc_inequality(res
);
12862 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12863 n_after
, n_div_ma
, n_div_bmap
,
12864 f
, c1
, c2
, g
, 0) < 0)
12868 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12869 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12870 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12873 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12874 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12875 f
, c1
, c2
, g
, 1) < 0)
12880 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12886 isl_basic_map_free(bmap
);
12887 isl_multi_aff_free(ma
);
12888 res
= isl_basic_map_simplify(res
);
12889 return isl_basic_map_finalize(res
);
12895 isl_basic_map_free(bmap
);
12896 isl_multi_aff_free(ma
);
12897 isl_basic_map_free(res
);
12901 /* Compute the preimage of "bset" under the function represented by "ma".
12902 * In other words, plug in "ma" in "bset". The result is a basic set
12903 * that lives in the domain space of "ma".
12905 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12906 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12908 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12911 /* Compute the preimage of the domain of "bmap" under the function
12912 * represented by "ma".
12913 * In other words, plug in "ma" in the domain of "bmap".
12914 * The result is a basic map that lives in the same space as "bmap"
12915 * except that the domain has been replaced by the domain space of "ma".
12917 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12918 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12920 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12923 /* Compute the preimage of the range of "bmap" under the function
12924 * represented by "ma".
12925 * In other words, plug in "ma" in the range of "bmap".
12926 * The result is a basic map that lives in the same space as "bmap"
12927 * except that the range has been replaced by the domain space of "ma".
12929 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12930 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12932 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12935 /* Check if the range of "ma" is compatible with the domain or range
12936 * (depending on "type") of "map".
12937 * Return isl_stat_error if anything is wrong.
12939 static isl_stat
check_map_compatible_range_multi_aff(
12940 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12941 __isl_keep isl_multi_aff
*ma
)
12944 isl_space
*ma_space
;
12946 ma_space
= isl_multi_aff_get_space(ma
);
12947 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12948 isl_space_free(ma_space
);
12950 return isl_stat_error
;
12952 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12953 "spaces don't match", return isl_stat_error
);
12954 return isl_stat_ok
;
12957 /* Compute the preimage of the domain or range (depending on "type")
12958 * of "map" under the function represented by "ma".
12959 * In other words, plug in "ma" in the domain or range of "map".
12960 * The result is a map that lives in the same space as "map"
12961 * except that the domain or range has been replaced by
12962 * the domain space of "ma".
12964 * The parameters are assumed to have been aligned.
12966 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12967 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12972 map
= isl_map_cow(map
);
12973 ma
= isl_multi_aff_align_divs(ma
);
12976 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12979 for (i
= 0; i
< map
->n
; ++i
) {
12980 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12981 isl_multi_aff_copy(ma
));
12986 space
= isl_multi_aff_get_domain_space(ma
);
12987 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12989 isl_space_free(map
->dim
);
12994 isl_multi_aff_free(ma
);
12996 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12997 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13000 isl_multi_aff_free(ma
);
13005 /* Compute the preimage of the domain or range (depending on "type")
13006 * of "map" under the function represented by "ma".
13007 * In other words, plug in "ma" in the domain or range of "map".
13008 * The result is a map that lives in the same space as "map"
13009 * except that the domain or range has been replaced by
13010 * the domain space of "ma".
13012 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13013 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13020 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13024 return map_preimage_multi_aff(map
, type
, ma
);
13026 if (isl_map_check_named_params(map
) < 0)
13028 if (!isl_space_has_named_params(ma
->space
))
13029 isl_die(map
->ctx
, isl_error_invalid
,
13030 "unaligned unnamed parameters", goto error
);
13031 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13032 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13034 return map_preimage_multi_aff(map
, type
, ma
);
13036 isl_multi_aff_free(ma
);
13037 return isl_map_free(map
);
13040 /* Compute the preimage of "set" under the function represented by "ma".
13041 * In other words, plug in "ma" in "set". The result is a set
13042 * that lives in the domain space of "ma".
13044 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13045 __isl_take isl_multi_aff
*ma
)
13047 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13050 /* Compute the preimage of the domain of "map" under the function
13051 * represented by "ma".
13052 * In other words, plug in "ma" in the domain of "map".
13053 * The result is a map that lives in the same space as "map"
13054 * except that the domain has been replaced by the domain space of "ma".
13056 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13057 __isl_take isl_multi_aff
*ma
)
13059 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13062 /* Compute the preimage of the range of "map" under the function
13063 * represented by "ma".
13064 * In other words, plug in "ma" in the range of "map".
13065 * The result is a map that lives in the same space as "map"
13066 * except that the range has been replaced by the domain space of "ma".
13068 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13069 __isl_take isl_multi_aff
*ma
)
13071 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13074 /* Compute the preimage of "map" under the function represented by "pma".
13075 * In other words, plug in "pma" in the domain or range of "map".
13076 * The result is a map that lives in the same space as "map",
13077 * except that the space of type "type" has been replaced by
13078 * the domain space of "pma".
13080 * The parameters of "map" and "pma" are assumed to have been aligned.
13082 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13083 __isl_take isl_map
*map
, enum isl_dim_type type
,
13084 __isl_take isl_pw_multi_aff
*pma
)
13093 isl_pw_multi_aff_free(pma
);
13094 res
= isl_map_empty(isl_map_get_space(map
));
13099 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13100 isl_multi_aff_copy(pma
->p
[0].maff
));
13101 if (type
== isl_dim_in
)
13102 res
= isl_map_intersect_domain(res
,
13103 isl_map_copy(pma
->p
[0].set
));
13105 res
= isl_map_intersect_range(res
,
13106 isl_map_copy(pma
->p
[0].set
));
13108 for (i
= 1; i
< pma
->n
; ++i
) {
13111 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13112 isl_multi_aff_copy(pma
->p
[i
].maff
));
13113 if (type
== isl_dim_in
)
13114 res_i
= isl_map_intersect_domain(res_i
,
13115 isl_map_copy(pma
->p
[i
].set
));
13117 res_i
= isl_map_intersect_range(res_i
,
13118 isl_map_copy(pma
->p
[i
].set
));
13119 res
= isl_map_union(res
, res_i
);
13122 isl_pw_multi_aff_free(pma
);
13126 isl_pw_multi_aff_free(pma
);
13131 /* Compute the preimage of "map" under the function represented by "pma".
13132 * In other words, plug in "pma" in the domain or range of "map".
13133 * The result is a map that lives in the same space as "map",
13134 * except that the space of type "type" has been replaced by
13135 * the domain space of "pma".
13137 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13138 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13145 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13149 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13151 if (isl_map_check_named_params(map
) < 0)
13153 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13155 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13156 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13158 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13160 isl_pw_multi_aff_free(pma
);
13161 return isl_map_free(map
);
13164 /* Compute the preimage of "set" under the function represented by "pma".
13165 * In other words, plug in "pma" in "set". The result is a set
13166 * that lives in the domain space of "pma".
13168 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13169 __isl_take isl_pw_multi_aff
*pma
)
13171 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13174 /* Compute the preimage of the domain of "map" under the function
13175 * represented by "pma".
13176 * In other words, plug in "pma" in the domain of "map".
13177 * The result is a map that lives in the same space as "map",
13178 * except that domain space has been replaced by the domain space of "pma".
13180 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13181 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13183 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13186 /* Compute the preimage of the range of "map" under the function
13187 * represented by "pma".
13188 * In other words, plug in "pma" in the range of "map".
13189 * The result is a map that lives in the same space as "map",
13190 * except that range space has been replaced by the domain space of "pma".
13192 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13193 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13195 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13198 /* Compute the preimage of "map" under the function represented by "mpa".
13199 * In other words, plug in "mpa" in the domain or range of "map".
13200 * The result is a map that lives in the same space as "map",
13201 * except that the space of type "type" has been replaced by
13202 * the domain space of "mpa".
13204 * If the map does not involve any constraints that refer to the
13205 * dimensions of the substituted space, then the only possible
13206 * effect of "mpa" on the map is to map the space to a different space.
13207 * We create a separate isl_multi_aff to effectuate this change
13208 * in order to avoid spurious splitting of the map along the pieces
13210 * If "mpa" has a non-trivial explicit domain, however,
13211 * then the full substitution should be performed.
13213 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13214 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13218 isl_pw_multi_aff
*pma
;
13223 n
= isl_map_dim(map
, type
);
13224 full
= isl_map_involves_dims(map
, type
, 0, n
);
13225 if (full
>= 0 && !full
)
13226 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
13233 space
= isl_multi_pw_aff_get_space(mpa
);
13234 isl_multi_pw_aff_free(mpa
);
13235 ma
= isl_multi_aff_zero(space
);
13236 return isl_map_preimage_multi_aff(map
, type
, ma
);
13239 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13240 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13243 isl_multi_pw_aff_free(mpa
);
13247 /* Compute the preimage of "map" under the function represented by "mpa".
13248 * In other words, plug in "mpa" in the domain "map".
13249 * The result is a map that lives in the same space as "map",
13250 * except that domain space has been replaced by the domain space of "mpa".
13252 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13253 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13255 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13258 /* Compute the preimage of "set" by the function represented by "mpa".
13259 * In other words, plug in "mpa" in "set".
13261 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13262 __isl_take isl_multi_pw_aff
*mpa
)
13264 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13267 /* Return a copy of the equality constraints of "bset" as a matrix.
13269 __isl_give isl_mat
*isl_basic_set_extract_equalities(
13270 __isl_keep isl_basic_set
*bset
)
13278 ctx
= isl_basic_set_get_ctx(bset
);
13279 total
= 1 + isl_basic_set_dim(bset
, isl_dim_all
);
13280 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, total
);
13283 /* Are the "n" "coefficients" starting at "first" of the integer division
13284 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13286 * The "coefficient" at position 0 is the denominator.
13287 * The "coefficient" at position 1 is the constant term.
13289 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13290 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13291 unsigned first
, unsigned n
)
13293 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13294 return isl_bool_error
;
13295 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13296 return isl_bool_error
;
13297 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13298 bmap2
->div
[pos2
] + first
, n
);
13301 /* Are the integer division expressions at position "pos1" in "bmap1" and
13302 * "pos2" in "bmap2" equal to each other, except that the constant terms
13305 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13306 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13307 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13312 if (!bmap1
|| !bmap2
)
13313 return isl_bool_error
;
13314 total
= isl_basic_map_total_dim(bmap1
);
13315 if (total
!= isl_basic_map_total_dim(bmap2
))
13316 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13317 "incomparable div expressions", return isl_bool_error
);
13318 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13320 if (equal
< 0 || !equal
)
13322 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13324 if (equal
< 0 || equal
)
13325 return isl_bool_not(equal
);
13326 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13330 /* Replace the numerator of the constant term of the integer division
13331 * expression at position "div" in "bmap" by "value".
13332 * The caller guarantees that this does not change the meaning
13335 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13336 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13338 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13339 return isl_basic_map_free(bmap
);
13341 isl_int_set_si(bmap
->div
[div
][1], value
);
13346 /* Is the point "inner" internal to inequality constraint "ineq"
13348 * The point is considered to be internal to the inequality constraint,
13349 * if it strictly lies on the positive side of the inequality constraint,
13350 * or if it lies on the constraint and the constraint is lexico-positive.
13352 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13353 __isl_keep isl_basic_set
*bset
, int ineq
)
13359 if (!inner
|| !bset
)
13360 return isl_bool_error
;
13362 ctx
= isl_basic_set_get_ctx(bset
);
13363 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13364 &ctx
->normalize_gcd
);
13365 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13366 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13368 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13369 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13370 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13373 /* Tighten the inequality constraints of "bset" that are outward with respect
13374 * to the point "vec".
13375 * That is, tighten the constraints that are not satisfied by "vec".
13377 * "vec" is a point internal to some superset S of "bset" that is used
13378 * to make the subsets of S disjoint, by tightening one half of the constraints
13379 * that separate two subsets. In particular, the constraints of S
13380 * are all satisfied by "vec" and should not be tightened.
13381 * Of the internal constraints, those that have "vec" on the outside
13382 * are tightened. The shared facet is included in the adjacent subset
13383 * with the opposite constraint.
13384 * For constraints that saturate "vec", this criterion cannot be used
13385 * to determine which of the two sides should be tightened.
13386 * Instead, the sign of the first non-zero coefficient is used
13387 * to make this choice. Note that this second criterion is never used
13388 * on the constraints of S since "vec" is interior to "S".
13390 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13391 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13395 bset
= isl_basic_set_cow(bset
);
13398 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13401 internal
= is_internal(vec
, bset
, j
);
13403 return isl_basic_set_free(bset
);
13406 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13412 /* Replace the variables x of type "type" starting at "first" in "bmap"
13413 * by x' with x = M x' with M the matrix trans.
13414 * That is, replace the corresponding coefficients c by c M.
13416 * The transformation matrix should be a square matrix.
13418 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13419 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13420 __isl_take isl_mat
*trans
)
13424 bmap
= isl_basic_map_cow(bmap
);
13425 if (!bmap
|| !trans
)
13428 if (trans
->n_row
!= trans
->n_col
)
13429 isl_die(trans
->ctx
, isl_error_invalid
,
13430 "expecting square transformation matrix", goto error
);
13431 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
13434 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13436 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13437 isl_mat_copy(trans
)) < 0)
13439 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13440 isl_mat_copy(trans
)) < 0)
13442 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13443 isl_mat_copy(trans
)) < 0)
13446 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
13447 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13449 isl_mat_free(trans
);
13452 isl_mat_free(trans
);
13453 isl_basic_map_free(bmap
);
13457 /* Replace the variables x of type "type" starting at "first" in "bset"
13458 * by x' with x = M x' with M the matrix trans.
13459 * That is, replace the corresponding coefficients c by c M.
13461 * The transformation matrix should be a square matrix.
13463 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13464 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13465 __isl_take isl_mat
*trans
)
13467 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);