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>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.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 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 /* Return the space of "map".
93 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
95 return map
? map
->dim
: NULL
;
98 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
100 return map
? n(map
->dim
, type
) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
105 return set
? n(set
->dim
, type
) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
109 enum isl_dim_type type
)
118 case isl_dim_cst
: return 0;
119 case isl_dim_param
: return 1;
120 case isl_dim_in
: return 1 + space
->nparam
;
121 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
122 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
128 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
129 enum isl_dim_type type
)
131 return isl_basic_map_offset(bset
, type
);
134 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
136 return pos(map
->dim
, type
);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
140 enum isl_dim_type type
)
142 return isl_basic_map_dim(bset
, type
);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
147 return isl_basic_set_dim(bset
, isl_dim_set
);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
152 return isl_basic_set_dim(bset
, isl_dim_param
);
155 unsigned isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
159 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
162 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
164 return isl_set_dim(set
, isl_dim_set
);
167 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
169 return isl_set_dim(set
, isl_dim_param
);
172 unsigned isl_basic_map_n_in(__isl_keep
const isl_basic_map
*bmap
)
174 return bmap
? bmap
->dim
->n_in
: 0;
177 unsigned isl_basic_map_n_out(__isl_keep
const isl_basic_map
*bmap
)
179 return bmap
? bmap
->dim
->n_out
: 0;
182 unsigned isl_basic_map_n_param(__isl_keep
const isl_basic_map
*bmap
)
184 return bmap
? bmap
->dim
->nparam
: 0;
187 unsigned isl_basic_map_n_div(__isl_keep
const isl_basic_map
*bmap
)
189 return bmap
? bmap
->n_div
: 0;
192 unsigned isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
194 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
197 unsigned isl_map_n_in(__isl_keep
const isl_map
*map
)
199 return map
? map
->dim
->n_in
: 0;
202 unsigned isl_map_n_out(__isl_keep
const isl_map
*map
)
204 return map
? map
->dim
->n_out
: 0;
207 unsigned isl_map_n_param(__isl_keep
const isl_map
*map
)
209 return map
? map
->dim
->nparam
: 0;
212 /* Return the number of equality constraints in the description of "bmap".
213 * Return -1 on error.
215 int isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
222 /* Return the number of equality constraints in the description of "bset".
223 * Return -1 on error.
225 int isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
227 return isl_basic_map_n_equality(bset_to_bmap(bset
));
230 /* Do "bmap1" and "bmap2" have the same parameters?
232 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
233 __isl_keep isl_basic_map
*bmap2
)
235 isl_space
*space1
, *space2
;
237 space1
= isl_basic_map_peek_space(bmap1
);
238 space2
= isl_basic_map_peek_space(bmap2
);
239 return isl_space_has_equal_params(space1
, space2
);
242 /* Do "map1" and "map2" have the same parameters?
244 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
245 __isl_keep isl_map
*map2
)
247 isl_space
*space1
, *space2
;
249 space1
= isl_map_peek_space(map1
);
250 space2
= isl_map_peek_space(map2
);
251 return isl_space_has_equal_params(space1
, space2
);
254 /* Do "map" and "set" have the same parameters?
256 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
257 __isl_keep isl_set
*set
)
259 return isl_map_has_equal_params(map
, set_to_map(set
));
262 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
263 __isl_keep isl_set
*set
)
267 return isl_bool_error
;
268 m
= isl_map_has_equal_params(map
, set_to_map(set
));
271 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
272 set
->dim
, isl_dim_set
);
275 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
276 __isl_keep isl_basic_set
*bset
)
280 return isl_bool_error
;
281 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
284 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
285 bset
->dim
, isl_dim_set
);
288 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
289 __isl_keep isl_set
*set
)
293 return isl_bool_error
;
294 m
= isl_map_has_equal_params(map
, set_to_map(set
));
297 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
298 set
->dim
, isl_dim_set
);
301 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
302 __isl_keep isl_basic_set
*bset
)
306 return isl_bool_error
;
307 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
310 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
311 bset
->dim
, isl_dim_set
);
314 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
316 return bmap
? bmap
->ctx
: NULL
;
319 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
321 return bset
? bset
->ctx
: NULL
;
324 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
326 return map
? map
->ctx
: NULL
;
329 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
331 return set
? set
->ctx
: NULL
;
334 /* Return the space of "bmap".
336 __isl_keep isl_space
*isl_basic_map_peek_space(
337 __isl_keep
const isl_basic_map
*bmap
)
339 return bmap
? bmap
->dim
: NULL
;
342 /* Return the space of "bset".
344 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
346 return isl_basic_map_peek_space(bset_to_bmap(bset
));
349 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
351 return isl_space_copy(isl_basic_map_peek_space(bmap
));
354 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
356 return isl_basic_map_get_space(bset_to_bmap(bset
));
359 /* Extract the divs in "bmap" as a matrix.
361 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
372 ctx
= isl_basic_map_get_ctx(bmap
);
373 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
374 cols
= 1 + 1 + total
+ bmap
->n_div
;
375 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
379 for (i
= 0; i
< bmap
->n_div
; ++i
)
380 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
385 /* Extract the divs in "bset" as a matrix.
387 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
389 return isl_basic_map_get_divs(bset
);
392 __isl_give isl_local_space
*isl_basic_map_get_local_space(
393 __isl_keep isl_basic_map
*bmap
)
400 div
= isl_basic_map_get_divs(bmap
);
401 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
404 __isl_give isl_local_space
*isl_basic_set_get_local_space(
405 __isl_keep isl_basic_set
*bset
)
407 return isl_basic_map_get_local_space(bset
);
410 /* For each known div d = floor(f/m), add the constraints
413 * -(f-(m-1)) + m d >= 0
415 * Do not finalize the result.
417 static __isl_give isl_basic_map
*add_known_div_constraints(
418 __isl_take isl_basic_map
*bmap
)
425 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
428 bmap
= isl_basic_map_cow(bmap
);
429 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
432 for (i
= 0; i
< n_div
; ++i
) {
433 if (isl_int_is_zero(bmap
->div
[i
][0]))
435 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
436 return isl_basic_map_free(bmap
);
442 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
443 __isl_take isl_local_space
*ls
)
452 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
453 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
454 n_div
, 0, 2 * n_div
);
456 for (i
= 0; i
< n_div
; ++i
)
457 if (isl_basic_map_alloc_div(bmap
) < 0)
460 for (i
= 0; i
< n_div
; ++i
)
461 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
462 bmap
= add_known_div_constraints(bmap
);
464 isl_local_space_free(ls
);
467 isl_local_space_free(ls
);
468 isl_basic_map_free(bmap
);
472 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
473 __isl_take isl_local_space
*ls
)
475 return isl_basic_map_from_local_space(ls
);
478 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
480 return isl_space_copy(isl_map_peek_space(map
));
483 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
487 return isl_space_copy(set
->dim
);
490 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
491 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
493 bmap
= isl_basic_map_cow(bmap
);
496 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
499 bmap
= isl_basic_map_finalize(bmap
);
502 isl_basic_map_free(bmap
);
506 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
507 __isl_take isl_basic_set
*bset
, const char *s
)
509 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
512 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
513 enum isl_dim_type type
)
515 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
518 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
519 enum isl_dim_type type
, const char *s
)
523 map
= isl_map_cow(map
);
527 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
531 for (i
= 0; i
< map
->n
; ++i
) {
532 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
543 /* Replace the identifier of the tuple of type "type" by "id".
545 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
546 __isl_take isl_basic_map
*bmap
,
547 enum isl_dim_type type
, __isl_take isl_id
*id
)
549 bmap
= isl_basic_map_cow(bmap
);
552 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
554 return isl_basic_map_free(bmap
);
555 bmap
= isl_basic_map_finalize(bmap
);
562 /* Replace the identifier of the tuple by "id".
564 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
565 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
567 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
570 /* Does the input or output tuple have a name?
572 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
574 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
577 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
578 enum isl_dim_type type
)
580 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
583 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
586 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
590 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
591 enum isl_dim_type type
, __isl_take isl_id
*id
)
593 map
= isl_map_cow(map
);
597 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
599 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
605 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
606 __isl_take isl_id
*id
)
608 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
611 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
612 enum isl_dim_type type
)
614 map
= isl_map_cow(map
);
618 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
620 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
623 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
625 return isl_map_reset_tuple_id(set
, isl_dim_set
);
628 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
630 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
633 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
634 enum isl_dim_type type
)
636 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
639 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
641 return isl_map_has_tuple_id(set
, isl_dim_set
);
644 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
646 return isl_map_get_tuple_id(set
, isl_dim_set
);
649 /* Does the set tuple have a name?
651 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
654 return isl_bool_error
;
655 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
659 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
661 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
664 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
666 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
669 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
670 enum isl_dim_type type
, unsigned pos
)
672 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
675 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
676 enum isl_dim_type type
, unsigned pos
)
678 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
681 /* Does the given dimension have a name?
683 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
684 enum isl_dim_type type
, unsigned pos
)
687 return isl_bool_error
;
688 return isl_space_has_dim_name(map
->dim
, type
, pos
);
691 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
692 enum isl_dim_type type
, unsigned pos
)
694 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
697 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
698 enum isl_dim_type type
, unsigned pos
)
700 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
703 /* Does the given dimension have a name?
705 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
706 enum isl_dim_type type
, unsigned pos
)
709 return isl_bool_error
;
710 return isl_space_has_dim_name(set
->dim
, type
, pos
);
713 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
714 __isl_take isl_basic_map
*bmap
,
715 enum isl_dim_type type
, unsigned pos
, const char *s
)
717 bmap
= isl_basic_map_cow(bmap
);
720 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
723 return isl_basic_map_finalize(bmap
);
725 isl_basic_map_free(bmap
);
729 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
730 enum isl_dim_type type
, unsigned pos
, const char *s
)
734 map
= isl_map_cow(map
);
738 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
742 for (i
= 0; i
< map
->n
; ++i
) {
743 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
754 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
755 __isl_take isl_basic_set
*bset
,
756 enum isl_dim_type type
, unsigned pos
, const char *s
)
758 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
762 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
763 enum isl_dim_type type
, unsigned pos
, const char *s
)
765 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
769 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
770 enum isl_dim_type type
, unsigned pos
)
773 return isl_bool_error
;
774 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
777 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
778 enum isl_dim_type type
, unsigned pos
)
780 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
783 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
784 enum isl_dim_type type
, unsigned pos
)
786 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
789 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
790 enum isl_dim_type type
, unsigned pos
)
792 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
795 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
796 enum isl_dim_type type
, unsigned pos
)
798 return isl_map_has_dim_id(set
, type
, pos
);
801 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
802 enum isl_dim_type type
, unsigned pos
)
804 return isl_map_get_dim_id(set
, type
, pos
);
807 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
808 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
810 map
= isl_map_cow(map
);
814 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
816 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
822 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
823 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
825 return isl_map_set_dim_id(set
, type
, pos
, id
);
828 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
829 __isl_keep isl_id
*id
)
833 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
836 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
837 __isl_keep isl_id
*id
)
839 return isl_map_find_dim_by_id(set
, type
, id
);
842 /* Return the position of the dimension of the given type and name
844 * Return -1 if no such dimension can be found.
846 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
847 enum isl_dim_type type
, const char *name
)
851 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
854 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
859 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
862 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
865 return isl_map_find_dim_by_name(set
, type
, name
);
868 /* Check whether equality i of bset is a pure stride constraint
869 * on a single dimension, i.e., of the form
873 * with k a constant and e an existentially quantified variable.
875 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
884 return isl_bool_error
;
886 if (!isl_int_is_zero(bset
->eq
[i
][0]))
887 return isl_bool_false
;
889 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
890 d
= isl_basic_set_dim(bset
, isl_dim_set
);
891 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
893 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
894 return isl_bool_false
;
895 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
897 return isl_bool_false
;
898 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
900 return isl_bool_false
;
902 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
904 return isl_bool_false
;
905 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
906 n_div
- pos2
- 1) != -1)
907 return isl_bool_false
;
908 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
909 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
910 return isl_bool_false
;
912 return isl_bool_true
;
915 /* Reset the user pointer on all identifiers of parameters and tuples
916 * of the space of "map".
918 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
922 space
= isl_map_get_space(map
);
923 space
= isl_space_reset_user(space
);
924 map
= isl_map_reset_space(map
, space
);
929 /* Reset the user pointer on all identifiers of parameters and tuples
930 * of the space of "set".
932 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
934 return isl_map_reset_user(set
);
937 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
940 return isl_bool_error
;
941 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
944 /* Has "map" been marked as a rational map?
945 * In particular, have all basic maps in "map" been marked this way?
946 * An empty map is not considered to be rational.
947 * Maps where only some of the basic maps are marked rational
950 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
956 return isl_bool_error
;
958 return isl_bool_false
;
959 rational
= isl_basic_map_is_rational(map
->p
[0]);
962 for (i
= 1; i
< map
->n
; ++i
) {
965 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
968 if (rational
!= rational_i
)
969 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
970 "mixed rational and integer basic maps "
971 "not supported", return isl_bool_error
);
977 /* Has "set" been marked as a rational set?
978 * In particular, have all basic set in "set" been marked this way?
979 * An empty set is not considered to be rational.
980 * Sets where only some of the basic sets are marked rational
983 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
985 return isl_map_is_rational(set
);
988 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
990 return isl_basic_map_is_rational(bset
);
993 /* Does "bmap" contain any rational points?
995 * If "bmap" has an equality for each dimension, equating the dimension
996 * to an integer constant, then it has no rational points, even if it
997 * is marked as rational.
999 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1001 isl_bool has_rational
= isl_bool_true
;
1005 return isl_bool_error
;
1006 if (isl_basic_map_plain_is_empty(bmap
))
1007 return isl_bool_false
;
1008 if (!isl_basic_map_is_rational(bmap
))
1009 return isl_bool_false
;
1010 bmap
= isl_basic_map_copy(bmap
);
1011 bmap
= isl_basic_map_implicit_equalities(bmap
);
1013 return isl_bool_error
;
1014 total
= isl_basic_map_total_dim(bmap
);
1015 if (bmap
->n_eq
== total
) {
1017 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1018 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1021 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1022 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1024 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1029 if (i
== bmap
->n_eq
)
1030 has_rational
= isl_bool_false
;
1032 isl_basic_map_free(bmap
);
1034 return has_rational
;
1037 /* Does "map" contain any rational points?
1039 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1042 isl_bool has_rational
;
1045 return isl_bool_error
;
1046 for (i
= 0; i
< map
->n
; ++i
) {
1047 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1048 if (has_rational
< 0 || has_rational
)
1049 return has_rational
;
1051 return isl_bool_false
;
1054 /* Does "set" contain any rational points?
1056 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1058 return isl_map_has_rational(set
);
1061 /* Is this basic set a parameter domain?
1063 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1066 return isl_bool_error
;
1067 return isl_space_is_params(bset
->dim
);
1070 /* Is this set a parameter domain?
1072 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1075 return isl_bool_error
;
1076 return isl_space_is_params(set
->dim
);
1079 /* Is this map actually a parameter domain?
1080 * Users should never call this function. Outside of isl,
1081 * a map can never be a parameter domain.
1083 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1086 return isl_bool_error
;
1087 return isl_space_is_params(map
->dim
);
1090 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
1091 struct isl_basic_map
*bmap
, unsigned extra
,
1092 unsigned n_eq
, unsigned n_ineq
)
1095 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
1100 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1101 if (isl_blk_is_error(bmap
->block
))
1104 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1105 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1109 bmap
->block2
= isl_blk_empty();
1112 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1113 if (isl_blk_is_error(bmap
->block2
))
1116 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1121 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1122 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1124 for (i
= 0; i
< extra
; ++i
)
1125 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1129 bmap
->c_size
= n_eq
+ n_ineq
;
1130 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1131 bmap
->extra
= extra
;
1135 bmap
->sample
= NULL
;
1139 isl_basic_map_free(bmap
);
1143 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1144 unsigned nparam
, unsigned dim
, unsigned extra
,
1145 unsigned n_eq
, unsigned n_ineq
)
1147 struct isl_basic_map
*bmap
;
1150 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1154 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1155 return bset_from_bmap(bmap
);
1158 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1159 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1161 struct isl_basic_map
*bmap
;
1164 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1165 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1166 return bset_from_bmap(bmap
);
1168 isl_space_free(dim
);
1172 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1173 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1175 struct isl_basic_map
*bmap
;
1179 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1184 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1186 isl_space_free(dim
);
1190 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1191 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1192 unsigned n_eq
, unsigned n_ineq
)
1194 struct isl_basic_map
*bmap
;
1197 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1201 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1205 static void dup_constraints(
1206 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1209 unsigned total
= isl_basic_map_total_dim(src
);
1211 for (i
= 0; i
< src
->n_eq
; ++i
) {
1212 int j
= isl_basic_map_alloc_equality(dst
);
1213 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1216 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1217 int j
= isl_basic_map_alloc_inequality(dst
);
1218 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1221 for (i
= 0; i
< src
->n_div
; ++i
) {
1222 int j
= isl_basic_map_alloc_div(dst
);
1223 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1225 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1228 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1230 struct isl_basic_map
*dup
;
1234 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1235 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1238 dup_constraints(dup
, bmap
);
1239 dup
->flags
= bmap
->flags
;
1240 dup
->sample
= isl_vec_copy(bmap
->sample
);
1244 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1246 struct isl_basic_map
*dup
;
1248 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1249 return bset_from_bmap(dup
);
1252 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1257 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1261 return isl_basic_set_dup(bset
);
1264 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1273 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1278 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1282 bmap
= isl_basic_map_dup(bmap
);
1284 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1288 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1297 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1302 if (--bmap
->ref
> 0)
1305 isl_ctx_deref(bmap
->ctx
);
1307 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1309 isl_blk_free(bmap
->ctx
, bmap
->block
);
1310 isl_vec_free(bmap
->sample
);
1311 isl_space_free(bmap
->dim
);
1317 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1319 return isl_basic_map_free(bset_to_bmap(bset
));
1322 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1324 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1327 /* Check that "map" has only named parameters, reporting an error
1330 isl_stat
isl_map_check_named_params(__isl_keep isl_map
*map
)
1332 return isl_space_check_named_params(isl_map_peek_space(map
));
1335 /* Check that "bmap1" and "bmap2" have the same parameters,
1336 * reporting an error if they do not.
1338 static isl_stat
isl_basic_map_check_equal_params(
1339 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1343 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1345 return isl_stat_error
;
1347 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1348 "parameters don't match", return isl_stat_error
);
1352 __isl_give isl_map
*isl_map_align_params_map_map_and(
1353 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1354 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1355 __isl_take isl_map
*map2
))
1359 if (isl_map_has_equal_params(map1
, map2
))
1360 return fn(map1
, map2
);
1361 if (isl_map_check_named_params(map1
) < 0)
1363 if (isl_map_check_named_params(map2
) < 0)
1365 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1366 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1367 return fn(map1
, map2
);
1374 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1375 __isl_keep isl_map
*map2
,
1376 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1381 return isl_bool_error
;
1382 if (isl_map_has_equal_params(map1
, map2
))
1383 return fn(map1
, map2
);
1384 if (isl_map_check_named_params(map1
) < 0)
1385 return isl_bool_error
;
1386 if (isl_map_check_named_params(map2
) < 0)
1387 return isl_bool_error
;
1388 map1
= isl_map_copy(map1
);
1389 map2
= isl_map_copy(map2
);
1390 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1391 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1398 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1400 struct isl_ctx
*ctx
;
1404 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1405 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1407 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1408 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1409 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1410 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1411 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1412 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1414 int j
= isl_basic_map_alloc_inequality(bmap
);
1418 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1419 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1426 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1427 bmap
->extra
- bmap
->n_div
);
1428 return bmap
->n_eq
++;
1431 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1433 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1436 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1440 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1445 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1447 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1450 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1455 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1457 if (pos
!= bmap
->n_eq
- 1) {
1459 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1460 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1466 /* Turn inequality "pos" of "bmap" into an equality.
1468 * In particular, we move the inequality in front of the equalities
1469 * and move the last inequality in the position of the moved inequality.
1470 * Note that isl_tab_make_equalities_explicit depends on this particular
1471 * change in the ordering of the constraints.
1473 void isl_basic_map_inequality_to_equality(
1474 struct isl_basic_map
*bmap
, unsigned pos
)
1478 t
= bmap
->ineq
[pos
];
1479 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1480 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1485 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1486 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1487 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1488 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1491 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1493 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1496 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1498 struct isl_ctx
*ctx
;
1502 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1504 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1505 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1506 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1507 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1508 1 + isl_basic_map_total_dim(bmap
),
1509 bmap
->extra
- bmap
->n_div
);
1510 return bmap
->n_ineq
++;
1513 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1515 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1518 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1522 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1527 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1529 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1532 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1537 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1539 if (pos
!= bmap
->n_ineq
- 1) {
1540 t
= bmap
->ineq
[pos
];
1541 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1542 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1543 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1549 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1551 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1554 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1559 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1562 k
= isl_basic_map_alloc_equality(bmap
);
1565 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1568 isl_basic_map_free(bmap
);
1572 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1575 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1578 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1583 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1586 k
= isl_basic_map_alloc_inequality(bmap
);
1589 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1592 isl_basic_map_free(bmap
);
1596 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1599 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1602 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1606 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1607 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1608 1 + 1 + isl_basic_map_total_dim(bmap
),
1609 bmap
->extra
- bmap
->n_div
);
1610 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1611 return bmap
->n_div
++;
1614 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1616 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1619 /* Check that there are "n" dimensions of type "type" starting at "first"
1622 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1623 enum isl_dim_type type
, unsigned first
, unsigned n
)
1628 return isl_stat_error
;
1629 dim
= isl_basic_map_dim(bmap
, type
);
1630 if (first
+ n
> dim
|| first
+ n
< first
)
1631 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1632 "position or range out of bounds",
1633 return isl_stat_error
);
1637 /* Insert an extra integer division, prescribed by "div", to "bmap"
1638 * at (integer division) position "pos".
1640 * The integer division is first added at the end and then moved
1641 * into the right position.
1643 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1644 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1648 bmap
= isl_basic_map_cow(bmap
);
1650 return isl_basic_map_free(bmap
);
1652 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1653 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1654 "unexpected size", return isl_basic_map_free(bmap
));
1655 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1656 return isl_basic_map_free(bmap
);
1658 bmap
= isl_basic_map_extend_space(bmap
,
1659 isl_basic_map_get_space(bmap
), 1, 0, 2);
1660 k
= isl_basic_map_alloc_div(bmap
);
1662 return isl_basic_map_free(bmap
);
1663 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1664 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1666 for (i
= k
; i
> pos
; --i
)
1667 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1672 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1675 return isl_stat_error
;
1676 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1681 /* Copy constraint from src to dst, putting the vars of src at offset
1682 * dim_off in dst and the divs of src at offset div_off in dst.
1683 * If both sets are actually map, then dim_off applies to the input
1686 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1687 struct isl_basic_map
*src_map
, isl_int
*src
,
1688 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1690 unsigned src_nparam
= isl_basic_map_dim(src_map
, isl_dim_param
);
1691 unsigned dst_nparam
= isl_basic_map_dim(dst_map
, isl_dim_param
);
1692 unsigned src_in
= isl_basic_map_dim(src_map
, isl_dim_in
);
1693 unsigned dst_in
= isl_basic_map_dim(dst_map
, isl_dim_in
);
1694 unsigned src_out
= isl_basic_map_dim(src_map
, isl_dim_out
);
1695 unsigned dst_out
= isl_basic_map_dim(dst_map
, isl_dim_out
);
1696 isl_int_set(dst
[0], src
[0]);
1697 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1698 if (dst_nparam
> src_nparam
)
1699 isl_seq_clr(dst
+1+src_nparam
,
1700 dst_nparam
- src_nparam
);
1701 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1702 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1704 isl_min(dst_in
-in_off
, src_in
));
1705 if (dst_in
-in_off
> src_in
)
1706 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1707 dst_in
- in_off
- src_in
);
1708 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1709 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1710 src
+1+src_nparam
+src_in
,
1711 isl_min(dst_out
-out_off
, src_out
));
1712 if (dst_out
-out_off
> src_out
)
1713 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1714 dst_out
- out_off
- src_out
);
1715 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1716 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1717 src
+1+src_nparam
+src_in
+src_out
,
1718 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1719 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1720 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1721 div_off
+src_map
->n_div
,
1722 dst_map
->n_div
- div_off
- src_map
->n_div
);
1725 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1726 struct isl_basic_map
*src_map
, isl_int
*src
,
1727 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1729 isl_int_set(dst
[0], src
[0]);
1730 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1733 static __isl_give isl_basic_map
*add_constraints(
1734 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1735 unsigned i_pos
, unsigned o_pos
)
1740 if (!bmap1
|| !bmap2
)
1743 div_off
= bmap1
->n_div
;
1745 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1746 int i1
= isl_basic_map_alloc_equality(bmap1
);
1749 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1750 i_pos
, o_pos
, div_off
);
1753 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1754 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1757 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1758 i_pos
, o_pos
, div_off
);
1761 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1762 int i1
= isl_basic_map_alloc_div(bmap1
);
1765 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1766 i_pos
, o_pos
, div_off
);
1769 isl_basic_map_free(bmap2
);
1774 isl_basic_map_free(bmap1
);
1775 isl_basic_map_free(bmap2
);
1779 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1780 struct isl_basic_set
*bset2
, unsigned pos
)
1782 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1783 bset_to_bmap(bset2
), 0, pos
));
1786 __isl_give isl_basic_map
*isl_basic_map_extend_space(
1787 __isl_take isl_basic_map
*base
, __isl_take isl_space
*dim
,
1788 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1790 struct isl_basic_map
*ext
;
1800 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1801 base
->extra
>= base
->n_div
+ extra
;
1803 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1804 room_for_ineq(base
, n_ineq
)) {
1805 isl_space_free(dim
);
1809 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1810 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1811 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1812 extra
+= base
->extra
;
1814 n_ineq
+= base
->n_ineq
;
1816 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1822 ext
->sample
= isl_vec_copy(base
->sample
);
1823 flags
= base
->flags
;
1824 ext
= add_constraints(ext
, base
, 0, 0);
1827 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1833 isl_space_free(dim
);
1834 isl_basic_map_free(base
);
1838 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1839 __isl_take isl_space
*dim
, unsigned extra
,
1840 unsigned n_eq
, unsigned n_ineq
)
1842 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1843 dim
, extra
, n_eq
, n_ineq
));
1846 struct isl_basic_map
*isl_basic_map_extend_constraints(
1847 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1851 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1855 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1856 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1857 unsigned n_eq
, unsigned n_ineq
)
1859 struct isl_basic_map
*bmap
;
1864 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1868 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1871 isl_basic_map_free(base
);
1875 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1876 unsigned nparam
, unsigned dim
, unsigned extra
,
1877 unsigned n_eq
, unsigned n_ineq
)
1879 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1880 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1883 struct isl_basic_set
*isl_basic_set_extend_constraints(
1884 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1886 isl_basic_map
*bmap
= bset_to_bmap(base
);
1887 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1888 return bset_from_bmap(bmap
);
1891 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1893 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1896 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1901 if (bmap
->ref
> 1) {
1903 bmap
= isl_basic_map_dup(bmap
);
1906 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1907 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1912 /* Clear all cached information in "map", either because it is about
1913 * to be modified or because it is being freed.
1914 * Always return the same pointer that is passed in.
1915 * This is needed for the use in isl_map_free.
1917 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1919 isl_basic_map_free(map
->cached_simple_hull
[0]);
1920 isl_basic_map_free(map
->cached_simple_hull
[1]);
1921 map
->cached_simple_hull
[0] = NULL
;
1922 map
->cached_simple_hull
[1] = NULL
;
1926 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
1928 return isl_map_cow(set
);
1931 /* Return an isl_map that is equal to "map" and that has only
1932 * a single reference.
1934 * If the original input already has only one reference, then
1935 * simply return it, but clear all cached information, since
1936 * it may be rendered invalid by the operations that will be
1937 * performed on the result.
1939 * Otherwise, create a duplicate (without any cached information).
1941 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
1947 return clear_caches(map
);
1949 return isl_map_dup(map
);
1952 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1953 unsigned a_len
, unsigned b_len
)
1955 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1956 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1957 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1960 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1961 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1969 isl_assert(bmap
->ctx
,
1970 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1972 if (n1
== 0 || n2
== 0)
1975 bmap
= isl_basic_map_cow(bmap
);
1979 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1980 if (isl_blk_is_error(blk
))
1983 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1985 bmap
->eq
[i
] + pos
, n1
, n2
);
1987 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1989 bmap
->ineq
[i
] + pos
, n1
, n2
);
1991 for (i
= 0; i
< bmap
->n_div
; ++i
)
1993 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1995 isl_blk_free(bmap
->ctx
, blk
);
1997 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1998 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1999 return isl_basic_map_finalize(bmap
);
2001 isl_basic_map_free(bmap
);
2005 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2006 __isl_take isl_basic_map
*bmap
)
2012 total
= isl_basic_map_total_dim(bmap
);
2013 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2014 return isl_basic_map_free(bmap
);
2015 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2017 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
2019 i
= isl_basic_map_alloc_equality(bmap
);
2023 isl_int_set_si(bmap
->eq
[i
][0], 1);
2024 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2025 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2026 isl_vec_free(bmap
->sample
);
2027 bmap
->sample
= NULL
;
2028 return isl_basic_map_finalize(bmap
);
2030 isl_basic_map_free(bmap
);
2034 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
2036 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2039 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2040 __isl_take isl_basic_map
*bmap
)
2045 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2048 bmap
= isl_basic_map_cow(bmap
);
2052 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2054 return isl_basic_map_finalize(bmap
);
2057 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2058 __isl_take isl_basic_set
*bset
)
2060 return isl_basic_map_set_rational(bset
);
2063 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2064 __isl_take isl_basic_set
*bset
)
2069 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2072 bset
= isl_basic_set_cow(bset
);
2076 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2078 return isl_basic_set_finalize(bset
);
2081 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2085 map
= isl_map_cow(map
);
2088 for (i
= 0; i
< map
->n
; ++i
) {
2089 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2099 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2101 return isl_map_set_rational(set
);
2104 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2107 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2109 isl_int
*t
= bmap
->div
[a
];
2110 bmap
->div
[a
] = bmap
->div
[b
];
2114 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2115 * div definitions accordingly.
2117 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
2120 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2122 swap_div(bmap
, a
, b
);
2124 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2125 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2127 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2128 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2130 for (i
= 0; i
< bmap
->n_div
; ++i
)
2131 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2132 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2135 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2136 * div definitions accordingly.
2138 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
2140 isl_basic_map_swap_div(bset
, a
, b
);
2143 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2145 isl_seq_cpy(c
, c
+ n
, rem
);
2146 isl_seq_clr(c
+ rem
, n
);
2149 /* Drop n dimensions starting at first.
2151 * In principle, this frees up some extra variables as the number
2152 * of columns remains constant, but we would have to extend
2153 * the div array too as the number of rows in this array is assumed
2154 * to be equal to extra.
2156 struct isl_basic_set
*isl_basic_set_drop_dims(
2157 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
2159 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2162 /* Move "n" divs starting at "first" to the end of the list of divs.
2164 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2165 unsigned first
, unsigned n
)
2170 if (first
+ n
== bmap
->n_div
)
2173 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2176 for (i
= 0; i
< n
; ++i
)
2177 div
[i
] = bmap
->div
[first
+ i
];
2178 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2179 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2180 for (i
= 0; i
< n
; ++i
)
2181 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2185 isl_basic_map_free(bmap
);
2189 /* Drop "n" dimensions of type "type" starting at "first".
2191 * In principle, this frees up some extra variables as the number
2192 * of columns remains constant, but we would have to extend
2193 * the div array too as the number of rows in this array is assumed
2194 * to be equal to extra.
2196 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2197 enum isl_dim_type type
, unsigned first
, unsigned n
)
2207 dim
= isl_basic_map_dim(bmap
, type
);
2208 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
2210 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2213 bmap
= isl_basic_map_cow(bmap
);
2217 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2218 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
2219 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2220 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2222 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2223 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2225 for (i
= 0; i
< bmap
->n_div
; ++i
)
2226 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2228 if (type
== isl_dim_div
) {
2229 bmap
= move_divs_last(bmap
, first
, n
);
2232 if (isl_basic_map_free_div(bmap
, n
) < 0)
2233 return isl_basic_map_free(bmap
);
2235 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2239 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2240 bmap
= isl_basic_map_simplify(bmap
);
2241 return isl_basic_map_finalize(bmap
);
2243 isl_basic_map_free(bmap
);
2247 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2248 enum isl_dim_type type
, unsigned first
, unsigned n
)
2250 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2254 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2255 enum isl_dim_type type
, unsigned first
, unsigned n
)
2262 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2264 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2266 map
= isl_map_cow(map
);
2269 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
2273 for (i
= 0; i
< map
->n
; ++i
) {
2274 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2278 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2286 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2287 enum isl_dim_type type
, unsigned first
, unsigned n
)
2289 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2293 * We don't cow, as the div is assumed to be redundant.
2295 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2296 __isl_take isl_basic_map
*bmap
, unsigned div
)
2304 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
2306 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
2308 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2309 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2311 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2312 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
2313 isl_basic_map_drop_inequality(bmap
, i
);
2317 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
2320 for (i
= 0; i
< bmap
->n_div
; ++i
)
2321 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
2323 if (div
!= bmap
->n_div
- 1) {
2325 isl_int
*t
= bmap
->div
[div
];
2327 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
2328 bmap
->div
[j
] = bmap
->div
[j
+1];
2330 bmap
->div
[bmap
->n_div
- 1] = t
;
2332 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2333 if (isl_basic_map_free_div(bmap
, 1) < 0)
2334 return isl_basic_map_free(bmap
);
2338 isl_basic_map_free(bmap
);
2342 /* Eliminate the specified n dimensions starting at first from the
2343 * constraints, without removing the dimensions from the space.
2344 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2346 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2347 enum isl_dim_type type
, unsigned first
, unsigned n
)
2356 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
2357 isl_die(map
->ctx
, isl_error_invalid
,
2358 "index out of bounds", goto error
);
2360 map
= isl_map_cow(map
);
2364 for (i
= 0; i
< map
->n
; ++i
) {
2365 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2375 /* Eliminate the specified n dimensions starting at first from the
2376 * constraints, without removing the dimensions from the space.
2377 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2379 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2380 enum isl_dim_type type
, unsigned first
, unsigned n
)
2382 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2385 /* Eliminate the specified n dimensions starting at first from the
2386 * constraints, without removing the dimensions from the space.
2387 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2389 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2390 unsigned first
, unsigned n
)
2392 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2395 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2396 __isl_take isl_basic_map
*bmap
)
2400 bmap
= isl_basic_map_eliminate_vars(bmap
,
2401 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2405 return isl_basic_map_finalize(bmap
);
2408 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2409 __isl_take isl_basic_set
*bset
)
2411 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2414 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2423 map
= isl_map_cow(map
);
2427 for (i
= 0; i
< map
->n
; ++i
) {
2428 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2438 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2440 return isl_map_remove_divs(set
);
2443 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2444 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2445 unsigned first
, unsigned n
)
2447 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2448 return isl_basic_map_free(bmap
);
2449 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2451 bmap
= isl_basic_map_eliminate_vars(bmap
,
2452 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2455 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2457 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2461 /* Return true if the definition of the given div (recursively) involves
2462 * any of the given variables.
2464 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2465 unsigned first
, unsigned n
)
2468 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2470 if (isl_int_is_zero(bmap
->div
[div
][0]))
2471 return isl_bool_false
;
2472 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2473 return isl_bool_true
;
2475 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2478 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2480 involves
= div_involves_vars(bmap
, i
, first
, n
);
2481 if (involves
< 0 || involves
)
2485 return isl_bool_false
;
2488 /* Try and add a lower and/or upper bound on "div" to "bmap"
2489 * based on inequality "i".
2490 * "total" is the total number of variables (excluding the divs).
2491 * "v" is a temporary object that can be used during the calculations.
2492 * If "lb" is set, then a lower bound should be constructed.
2493 * If "ub" is set, then an upper bound should be constructed.
2495 * The calling function has already checked that the inequality does not
2496 * reference "div", but we still need to check that the inequality is
2497 * of the right form. We'll consider the case where we want to construct
2498 * a lower bound. The construction of upper bounds is similar.
2500 * Let "div" be of the form
2502 * q = floor((a + f(x))/d)
2504 * We essentially check if constraint "i" is of the form
2508 * so that we can use it to derive a lower bound on "div".
2509 * However, we allow a slightly more general form
2513 * with the condition that the coefficients of g(x) - f(x) are all
2515 * Rewriting this constraint as
2519 * adding a + f(x) to both sides and dividing by d, we obtain
2521 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2523 * Taking the floor on both sides, we obtain
2525 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2529 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2531 * In the case of an upper bound, we construct the constraint
2533 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2536 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2537 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2538 unsigned total
, isl_int v
, int lb
, int ub
)
2542 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2544 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2545 bmap
->div
[div
][1 + 1 + j
]);
2546 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2549 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2550 bmap
->div
[div
][1 + 1 + j
]);
2551 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2557 bmap
= isl_basic_map_cow(bmap
);
2558 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2560 int k
= isl_basic_map_alloc_inequality(bmap
);
2563 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2564 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2565 bmap
->div
[div
][1 + j
]);
2566 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2567 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2569 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2572 int k
= isl_basic_map_alloc_inequality(bmap
);
2575 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2576 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2577 bmap
->div
[div
][1 + j
]);
2578 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2579 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2581 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2584 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2587 isl_basic_map_free(bmap
);
2591 /* This function is called right before "div" is eliminated from "bmap"
2592 * using Fourier-Motzkin.
2593 * Look through the constraints of "bmap" for constraints on the argument
2594 * of the integer division and use them to construct constraints on the
2595 * integer division itself. These constraints can then be combined
2596 * during the Fourier-Motzkin elimination.
2597 * Note that it is only useful to introduce lower bounds on "div"
2598 * if "bmap" already contains upper bounds on "div" as the newly
2599 * introduce lower bounds can then be combined with the pre-existing
2600 * upper bounds. Similarly for upper bounds.
2601 * We therefore first check if "bmap" contains any lower and/or upper bounds
2604 * It is interesting to note that the introduction of these constraints
2605 * can indeed lead to more accurate results, even when compared to
2606 * deriving constraints on the argument of "div" from constraints on "div".
2607 * Consider, for example, the set
2609 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2611 * The second constraint can be rewritten as
2613 * 2 * [(-i-2j+3)/4] + k >= 0
2615 * from which we can derive
2617 * -i - 2j + 3 >= -2k
2623 * Combined with the first constraint, we obtain
2625 * -3 <= 3 + 2k or k >= -3
2627 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2628 * the first constraint, we obtain
2630 * [(i + 2j)/4] >= [-3/4] = -1
2632 * Combining this constraint with the second constraint, we obtain
2636 static __isl_give isl_basic_map
*insert_bounds_on_div(
2637 __isl_take isl_basic_map
*bmap
, int div
)
2640 int check_lb
, check_ub
;
2647 if (isl_int_is_zero(bmap
->div
[div
][0]))
2650 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2654 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2655 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2662 if (!check_lb
&& !check_ub
)
2667 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2668 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2671 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2672 check_lb
, check_ub
);
2680 /* Remove all divs (recursively) involving any of the given dimensions
2681 * in their definitions.
2683 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2684 __isl_take isl_basic_map
*bmap
,
2685 enum isl_dim_type type
, unsigned first
, unsigned n
)
2689 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2690 return isl_basic_map_free(bmap
);
2691 first
+= isl_basic_map_offset(bmap
, type
);
2693 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2696 involves
= div_involves_vars(bmap
, i
, first
, n
);
2698 return isl_basic_map_free(bmap
);
2701 bmap
= insert_bounds_on_div(bmap
, i
);
2702 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2711 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2712 __isl_take isl_basic_set
*bset
,
2713 enum isl_dim_type type
, unsigned first
, unsigned n
)
2715 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2718 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2719 enum isl_dim_type type
, unsigned first
, unsigned n
)
2728 map
= isl_map_cow(map
);
2732 for (i
= 0; i
< map
->n
; ++i
) {
2733 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2744 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2745 enum isl_dim_type type
, unsigned first
, unsigned n
)
2747 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2751 /* Does the description of "bmap" depend on the specified dimensions?
2752 * We also check whether the dimensions appear in any of the div definitions.
2753 * In principle there is no need for this check. If the dimensions appear
2754 * in a div definition, they also appear in the defining constraints of that
2757 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2758 enum isl_dim_type type
, unsigned first
, unsigned n
)
2762 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2763 return isl_bool_error
;
2765 first
+= isl_basic_map_offset(bmap
, type
);
2766 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2767 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2768 return isl_bool_true
;
2769 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2770 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2771 return isl_bool_true
;
2772 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2773 if (isl_int_is_zero(bmap
->div
[i
][0]))
2775 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2776 return isl_bool_true
;
2779 return isl_bool_false
;
2782 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2783 enum isl_dim_type type
, unsigned first
, unsigned n
)
2788 return isl_bool_error
;
2790 if (first
+ n
> isl_map_dim(map
, type
))
2791 isl_die(map
->ctx
, isl_error_invalid
,
2792 "index out of bounds", return isl_bool_error
);
2794 for (i
= 0; i
< map
->n
; ++i
) {
2795 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2797 if (involves
< 0 || involves
)
2801 return isl_bool_false
;
2804 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2805 enum isl_dim_type type
, unsigned first
, unsigned n
)
2807 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2810 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2811 enum isl_dim_type type
, unsigned first
, unsigned n
)
2813 return isl_map_involves_dims(set
, type
, first
, n
);
2816 /* Drop all constraints in bmap that involve any of the dimensions
2817 * first to first+n-1.
2819 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2820 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2827 bmap
= isl_basic_map_cow(bmap
);
2832 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2833 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2835 isl_basic_map_drop_equality(bmap
, i
);
2838 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2839 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2841 isl_basic_map_drop_inequality(bmap
, i
);
2844 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2848 /* Drop all constraints in bset that involve any of the dimensions
2849 * first to first+n-1.
2851 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2852 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2854 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2857 /* Drop all constraints in bmap that do not involve any of the dimensions
2858 * first to first + n - 1 of the given type.
2860 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2861 __isl_take isl_basic_map
*bmap
,
2862 enum isl_dim_type type
, unsigned first
, unsigned n
)
2867 isl_space
*space
= isl_basic_map_get_space(bmap
);
2868 isl_basic_map_free(bmap
);
2869 return isl_basic_map_universe(space
);
2871 bmap
= isl_basic_map_cow(bmap
);
2875 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2876 return isl_basic_map_free(bmap
);
2878 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2880 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2881 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2883 isl_basic_map_drop_equality(bmap
, i
);
2886 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2887 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2889 isl_basic_map_drop_inequality(bmap
, i
);
2892 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2896 /* Drop all constraints in bset that do not involve any of the dimensions
2897 * first to first + n - 1 of the given type.
2899 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2900 __isl_take isl_basic_set
*bset
,
2901 enum isl_dim_type type
, unsigned first
, unsigned n
)
2903 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2907 /* Drop all constraints in bmap that involve any of the dimensions
2908 * first to first + n - 1 of the given type.
2910 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2911 __isl_take isl_basic_map
*bmap
,
2912 enum isl_dim_type type
, unsigned first
, unsigned n
)
2919 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2920 return isl_basic_map_free(bmap
);
2922 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2923 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2924 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2927 /* Drop all constraints in bset that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2931 __isl_take isl_basic_set
*bset
,
2932 enum isl_dim_type type
, unsigned first
, unsigned n
)
2934 return isl_basic_map_drop_constraints_involving_dims(bset
,
2938 /* Drop constraints from "map" by applying "drop" to each basic map.
2940 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2941 enum isl_dim_type type
, unsigned first
, unsigned n
,
2942 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2943 enum isl_dim_type type
, unsigned first
, unsigned n
))
2951 dim
= isl_map_dim(map
, type
);
2952 if (first
+ n
> dim
|| first
+ n
< first
)
2953 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2954 "index out of bounds", return isl_map_free(map
));
2956 map
= isl_map_cow(map
);
2960 for (i
= 0; i
< map
->n
; ++i
) {
2961 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2963 return isl_map_free(map
);
2967 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2972 /* Drop all constraints in map that involve any of the dimensions
2973 * first to first + n - 1 of the given type.
2975 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2976 __isl_take isl_map
*map
,
2977 enum isl_dim_type type
, unsigned first
, unsigned n
)
2981 return drop_constraints(map
, type
, first
, n
,
2982 &isl_basic_map_drop_constraints_involving_dims
);
2985 /* Drop all constraints in "map" that do not involve any of the dimensions
2986 * first to first + n - 1 of the given type.
2988 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2989 __isl_take isl_map
*map
,
2990 enum isl_dim_type type
, unsigned first
, unsigned n
)
2993 isl_space
*space
= isl_map_get_space(map
);
2995 return isl_map_universe(space
);
2997 return drop_constraints(map
, type
, first
, n
,
2998 &isl_basic_map_drop_constraints_not_involving_dims
);
3001 /* Drop all constraints in set that involve any of the dimensions
3002 * first to first + n - 1 of the given type.
3004 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3005 __isl_take isl_set
*set
,
3006 enum isl_dim_type type
, unsigned first
, unsigned n
)
3008 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3011 /* Drop all constraints in "set" that do not involve any of the dimensions
3012 * first to first + n - 1 of the given type.
3014 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3015 __isl_take isl_set
*set
,
3016 enum isl_dim_type type
, unsigned first
, unsigned n
)
3018 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3021 /* Does local variable "div" of "bmap" have a complete explicit representation?
3022 * Having a complete explicit representation requires not only
3023 * an explicit representation, but also that all local variables
3024 * that appear in this explicit representation in turn have
3025 * a complete explicit representation.
3027 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3030 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3033 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3034 if (marked
< 0 || marked
)
3035 return isl_bool_not(marked
);
3037 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3040 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3042 known
= isl_basic_map_div_is_known(bmap
, i
);
3043 if (known
< 0 || !known
)
3047 return isl_bool_true
;
3050 /* Remove all divs that are unknown or defined in terms of unknown divs.
3052 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3053 __isl_take isl_basic_map
*bmap
)
3060 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3061 if (isl_basic_map_div_is_known(bmap
, i
))
3063 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3072 /* Remove all divs that are unknown or defined in terms of unknown divs.
3074 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3075 __isl_take isl_basic_set
*bset
)
3077 return isl_basic_map_remove_unknown_divs(bset
);
3080 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3089 map
= isl_map_cow(map
);
3093 for (i
= 0; i
< map
->n
; ++i
) {
3094 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3104 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3106 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3109 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3110 __isl_take isl_basic_set
*bset
,
3111 enum isl_dim_type type
, unsigned first
, unsigned n
)
3113 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3114 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3115 return bset_from_bmap(bmap
);
3118 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3119 enum isl_dim_type type
, unsigned first
, unsigned n
)
3126 map
= isl_map_cow(map
);
3129 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3131 for (i
= 0; i
< map
->n
; ++i
) {
3132 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3133 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3137 map
= isl_map_drop(map
, type
, first
, n
);
3144 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3145 enum isl_dim_type type
, unsigned first
, unsigned n
)
3147 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3151 /* Project out n inputs starting at first using Fourier-Motzkin */
3152 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
3153 unsigned first
, unsigned n
)
3155 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3158 static void dump_term(struct isl_basic_map
*bmap
,
3159 isl_int c
, int pos
, FILE *out
)
3162 unsigned in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3163 unsigned dim
= in
+ isl_basic_map_dim(bmap
, isl_dim_out
);
3164 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3166 isl_int_print(out
, c
, 0);
3168 if (!isl_int_is_one(c
))
3169 isl_int_print(out
, c
, 0);
3170 if (pos
< 1 + nparam
) {
3171 name
= isl_space_get_dim_name(bmap
->dim
,
3172 isl_dim_param
, pos
- 1);
3174 fprintf(out
, "%s", name
);
3176 fprintf(out
, "p%d", pos
- 1);
3177 } else if (pos
< 1 + nparam
+ in
)
3178 fprintf(out
, "i%d", pos
- 1 - nparam
);
3179 else if (pos
< 1 + nparam
+ dim
)
3180 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
3182 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
3186 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
3187 int sign
, FILE *out
)
3191 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
3195 for (i
= 0, first
= 1; i
< len
; ++i
) {
3196 if (isl_int_sgn(c
[i
]) * sign
<= 0)
3199 fprintf(out
, " + ");
3201 isl_int_abs(v
, c
[i
]);
3202 dump_term(bmap
, v
, i
, out
);
3209 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
3210 const char *op
, FILE *out
, int indent
)
3214 fprintf(out
, "%*s", indent
, "");
3216 dump_constraint_sign(bmap
, c
, 1, out
);
3217 fprintf(out
, " %s ", op
);
3218 dump_constraint_sign(bmap
, c
, -1, out
);
3222 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
3223 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
3225 fprintf(out
, "%*s", indent
, "");
3226 fprintf(out
, "ERROR: unused div coefficient not zero\n");
3231 static void dump_constraints(struct isl_basic_map
*bmap
,
3232 isl_int
**c
, unsigned n
,
3233 const char *op
, FILE *out
, int indent
)
3237 for (i
= 0; i
< n
; ++i
)
3238 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
3241 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
3245 unsigned total
= isl_basic_map_total_dim(bmap
);
3247 for (j
= 0; j
< 1 + total
; ++j
) {
3248 if (isl_int_is_zero(exp
[j
]))
3250 if (!first
&& isl_int_is_pos(exp
[j
]))
3252 dump_term(bmap
, exp
[j
], j
, out
);
3257 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
3261 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
3262 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
3264 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3265 fprintf(out
, "%*s", indent
, "");
3266 fprintf(out
, "e%d = [(", i
);
3267 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
3269 isl_int_print(out
, bmap
->div
[i
][0], 0);
3270 fprintf(out
, "]\n");
3274 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
3275 FILE *out
, int indent
)
3278 fprintf(out
, "null basic set\n");
3282 fprintf(out
, "%*s", indent
, "");
3283 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3284 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3285 bset
->extra
, bset
->flags
);
3286 dump(bset_to_bmap(bset
), out
, indent
);
3289 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
3290 FILE *out
, int indent
)
3293 fprintf(out
, "null basic map\n");
3297 fprintf(out
, "%*s", indent
, "");
3298 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3299 "flags: %x, n_name: %d\n",
3301 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3302 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3303 dump(bmap
, out
, indent
);
3306 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
3311 total
= isl_basic_map_total_dim(bmap
);
3312 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
3313 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3314 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3315 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3319 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3324 if (isl_space_dim(space
, isl_dim_in
) != 0)
3325 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3326 "set cannot have input dimensions", goto error
);
3327 return isl_map_alloc_space(space
, n
, flags
);
3329 isl_space_free(space
);
3333 /* Make sure "map" has room for at least "n" more basic maps.
3335 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3338 struct isl_map
*grown
= NULL
;
3342 isl_assert(map
->ctx
, n
>= 0, goto error
);
3343 if (map
->n
+ n
<= map
->size
)
3345 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3348 for (i
= 0; i
< map
->n
; ++i
) {
3349 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3357 isl_map_free(grown
);
3362 /* Make sure "set" has room for at least "n" more basic sets.
3364 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
3366 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3369 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3371 return isl_map_from_basic_map(bset
);
3374 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3376 struct isl_map
*map
;
3381 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3382 return isl_map_add_basic_map(map
, bmap
);
3385 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3386 __isl_take isl_basic_set
*bset
)
3388 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3389 bset_to_bmap(bset
)));
3392 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3394 return isl_map_free(set
);
3397 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3402 fprintf(out
, "null set\n");
3406 fprintf(out
, "%*s", indent
, "");
3407 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3408 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3410 for (i
= 0; i
< set
->n
; ++i
) {
3411 fprintf(out
, "%*s", indent
, "");
3412 fprintf(out
, "basic set %d:\n", i
);
3413 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3417 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3422 fprintf(out
, "null map\n");
3426 fprintf(out
, "%*s", indent
, "");
3427 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3428 "flags: %x, n_name: %d\n",
3429 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3430 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3431 for (i
= 0; i
< map
->n
; ++i
) {
3432 fprintf(out
, "%*s", indent
, "");
3433 fprintf(out
, "basic map %d:\n", i
);
3434 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3438 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3439 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3441 struct isl_basic_map
*bmap_domain
;
3443 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3446 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3447 isl_assert(bset
->ctx
,
3448 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3450 bmap
= isl_basic_map_cow(bmap
);
3453 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3454 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3455 bmap_domain
= isl_basic_map_from_domain(bset
);
3456 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3458 bmap
= isl_basic_map_simplify(bmap
);
3459 return isl_basic_map_finalize(bmap
);
3461 isl_basic_map_free(bmap
);
3462 isl_basic_set_free(bset
);
3466 /* Check that the space of "bset" is the same as that of the range of "bmap".
3468 static isl_stat
isl_basic_map_check_compatible_range(
3469 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3473 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3475 return isl_stat_error
;
3477 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3478 "incompatible spaces", return isl_stat_error
);
3483 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3484 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3486 struct isl_basic_map
*bmap_range
;
3488 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3491 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3492 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3495 if (isl_basic_set_plain_is_universe(bset
)) {
3496 isl_basic_set_free(bset
);
3500 bmap
= isl_basic_map_cow(bmap
);
3503 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3504 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3505 bmap_range
= bset_to_bmap(bset
);
3506 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3508 bmap
= isl_basic_map_simplify(bmap
);
3509 return isl_basic_map_finalize(bmap
);
3511 isl_basic_map_free(bmap
);
3512 isl_basic_set_free(bset
);
3516 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3517 __isl_keep isl_vec
*vec
)
3524 return isl_bool_error
;
3526 total
= 1 + isl_basic_map_total_dim(bmap
);
3527 if (total
!= vec
->size
)
3528 return isl_bool_false
;
3532 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3533 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3534 if (!isl_int_is_zero(s
)) {
3536 return isl_bool_false
;
3540 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3541 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3542 if (isl_int_is_neg(s
)) {
3544 return isl_bool_false
;
3550 return isl_bool_true
;
3553 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3554 __isl_keep isl_vec
*vec
)
3556 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3559 __isl_give isl_basic_map
*isl_basic_map_intersect(
3560 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3562 struct isl_vec
*sample
= NULL
;
3564 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3566 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3567 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3568 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3569 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3570 return isl_basic_map_intersect(bmap2
, bmap1
);
3572 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3573 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3574 isl_assert(bmap1
->ctx
,
3575 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3577 if (isl_basic_map_plain_is_empty(bmap1
)) {
3578 isl_basic_map_free(bmap2
);
3581 if (isl_basic_map_plain_is_empty(bmap2
)) {
3582 isl_basic_map_free(bmap1
);
3586 if (bmap1
->sample
&&
3587 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3588 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3589 sample
= isl_vec_copy(bmap1
->sample
);
3590 else if (bmap2
->sample
&&
3591 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3592 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3593 sample
= isl_vec_copy(bmap2
->sample
);
3595 bmap1
= isl_basic_map_cow(bmap1
);
3598 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3599 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3600 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3603 isl_vec_free(sample
);
3605 isl_vec_free(bmap1
->sample
);
3606 bmap1
->sample
= sample
;
3609 bmap1
= isl_basic_map_simplify(bmap1
);
3610 return isl_basic_map_finalize(bmap1
);
3613 isl_vec_free(sample
);
3614 isl_basic_map_free(bmap1
);
3615 isl_basic_map_free(bmap2
);
3619 struct isl_basic_set
*isl_basic_set_intersect(
3620 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3622 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3623 bset_to_bmap(bset2
)));
3626 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3627 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3629 return isl_basic_set_intersect(bset1
, bset2
);
3632 /* Special case of isl_map_intersect, where both map1 and map2
3633 * are convex, without any divs and such that either map1 or map2
3634 * contains a single constraint. This constraint is then simply
3635 * added to the other map.
3637 static __isl_give isl_map
*map_intersect_add_constraint(
3638 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3640 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3641 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3642 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3643 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3645 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3646 return isl_map_intersect(map2
, map1
);
3648 map1
= isl_map_cow(map1
);
3651 if (isl_map_plain_is_empty(map1
)) {
3655 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3656 if (map2
->p
[0]->n_eq
== 1)
3657 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3659 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3660 map2
->p
[0]->ineq
[0]);
3662 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3663 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3667 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3668 isl_basic_map_free(map1
->p
[0]);
3681 /* map2 may be either a parameter domain or a map living in the same
3684 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3685 __isl_take isl_map
*map2
)
3694 if ((isl_map_plain_is_empty(map1
) ||
3695 isl_map_plain_is_universe(map2
)) &&
3696 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3700 if ((isl_map_plain_is_empty(map2
) ||
3701 isl_map_plain_is_universe(map1
)) &&
3702 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3707 if (map1
->n
== 1 && map2
->n
== 1 &&
3708 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3709 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3710 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3711 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3712 return map_intersect_add_constraint(map1
, map2
);
3714 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3715 isl_space_dim(map2
->dim
, isl_dim_param
))
3716 isl_assert(map1
->ctx
,
3717 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3719 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3720 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3721 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3723 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3724 map1
->n
* map2
->n
, flags
);
3727 for (i
= 0; i
< map1
->n
; ++i
)
3728 for (j
= 0; j
< map2
->n
; ++j
) {
3729 struct isl_basic_map
*part
;
3730 part
= isl_basic_map_intersect(
3731 isl_basic_map_copy(map1
->p
[i
]),
3732 isl_basic_map_copy(map2
->p
[j
]));
3733 if (isl_basic_map_is_empty(part
) < 0)
3734 part
= isl_basic_map_free(part
);
3735 result
= isl_map_add_basic_map(result
, part
);
3748 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3749 __isl_take isl_map
*map2
)
3753 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3754 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3755 "spaces don't match", goto error
);
3756 return map_intersect_internal(map1
, map2
);
3763 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3764 __isl_take isl_map
*map2
)
3766 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3769 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3771 return set_from_map(isl_map_intersect(set_to_map(set1
),
3775 /* map_intersect_internal accepts intersections
3776 * with parameter domains, so we can just call that function.
3778 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3779 __isl_take isl_set
*params
)
3781 return map_intersect_internal(map
, params
);
3784 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3785 __isl_take isl_map
*map2
)
3787 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3790 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3791 __isl_take isl_set
*params
)
3793 return isl_map_intersect_params(set
, params
);
3796 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3799 unsigned pos
, n1
, n2
;
3803 bmap
= isl_basic_map_cow(bmap
);
3806 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3807 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3808 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3809 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3810 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3811 return isl_basic_map_reset_space(bmap
, space
);
3814 static __isl_give isl_basic_map
*basic_map_space_reset(
3815 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3821 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3824 space
= isl_basic_map_get_space(bmap
);
3825 space
= isl_space_reset(space
, type
);
3826 bmap
= isl_basic_map_reset_space(bmap
, space
);
3830 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3831 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3832 unsigned pos
, unsigned n
)
3836 struct isl_basic_map
*res
;
3837 struct isl_dim_map
*dim_map
;
3838 unsigned total
, off
;
3839 enum isl_dim_type t
;
3842 return basic_map_space_reset(bmap
, type
);
3847 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3849 total
= isl_basic_map_total_dim(bmap
) + n
;
3850 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3852 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3854 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3856 unsigned size
= isl_basic_map_dim(bmap
, t
);
3857 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3859 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3860 pos
, size
- pos
, off
+ pos
+ n
);
3862 off
+= isl_space_dim(res_dim
, t
);
3864 isl_dim_map_div(dim_map
, bmap
, off
);
3866 res
= isl_basic_map_alloc_space(res_dim
,
3867 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3868 rational
= isl_basic_map_is_rational(bmap
);
3870 res
= isl_basic_map_free(res
);
3872 res
= isl_basic_map_set_rational(res
);
3873 if (isl_basic_map_plain_is_empty(bmap
)) {
3874 isl_basic_map_free(bmap
);
3876 return isl_basic_map_set_to_empty(res
);
3878 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3879 return isl_basic_map_finalize(res
);
3882 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3883 __isl_take isl_basic_set
*bset
,
3884 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3886 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3889 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3890 enum isl_dim_type type
, unsigned n
)
3894 return isl_basic_map_insert_dims(bmap
, type
,
3895 isl_basic_map_dim(bmap
, type
), n
);
3898 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3899 enum isl_dim_type type
, unsigned n
)
3903 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3904 return isl_basic_map_add_dims(bset
, type
, n
);
3906 isl_basic_set_free(bset
);
3910 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3911 enum isl_dim_type type
)
3915 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3918 space
= isl_map_get_space(map
);
3919 space
= isl_space_reset(space
, type
);
3920 map
= isl_map_reset_space(map
, space
);
3924 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3925 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3930 return map_space_reset(map
, type
);
3932 map
= isl_map_cow(map
);
3936 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3940 for (i
= 0; i
< map
->n
; ++i
) {
3941 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3952 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3953 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3955 return isl_map_insert_dims(set
, type
, pos
, n
);
3958 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3959 enum isl_dim_type type
, unsigned n
)
3963 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3966 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3967 enum isl_dim_type type
, unsigned n
)
3971 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3972 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3978 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3979 __isl_take isl_basic_map
*bmap
,
3980 enum isl_dim_type dst_type
, unsigned dst_pos
,
3981 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3983 struct isl_dim_map
*dim_map
;
3984 struct isl_basic_map
*res
;
3985 enum isl_dim_type t
;
3986 unsigned total
, off
;
3991 bmap
= isl_basic_map_reset(bmap
, src_type
);
3992 bmap
= isl_basic_map_reset(bmap
, dst_type
);
3996 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3997 return isl_basic_map_free(bmap
);
3999 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4002 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4004 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4005 pos(bmap
->dim
, src_type
) + src_pos
+
4006 ((src_type
< dst_type
) ? n
: 0)) {
4007 bmap
= isl_basic_map_cow(bmap
);
4011 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4012 src_type
, src_pos
, n
);
4016 bmap
= isl_basic_map_finalize(bmap
);
4021 total
= isl_basic_map_total_dim(bmap
);
4022 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4025 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4026 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4027 if (t
== dst_type
) {
4028 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4031 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
4034 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4035 dst_pos
, size
- dst_pos
, off
);
4036 off
+= size
- dst_pos
;
4037 } else if (t
== src_type
) {
4038 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4041 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4042 src_pos
+ n
, size
- src_pos
- n
, off
);
4043 off
+= size
- src_pos
- n
;
4045 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4049 isl_dim_map_div(dim_map
, bmap
, off
);
4051 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4052 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4053 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4057 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
4058 src_type
, src_pos
, n
);
4062 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4063 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4064 bmap
= isl_basic_map_finalize(bmap
);
4068 isl_basic_map_free(bmap
);
4072 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4073 enum isl_dim_type dst_type
, unsigned dst_pos
,
4074 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4076 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4077 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4078 src_type
, src_pos
, n
);
4079 return bset_from_bmap(bmap
);
4082 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4083 enum isl_dim_type dst_type
, unsigned dst_pos
,
4084 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4088 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4089 return set_from_map(isl_map_move_dims(set_to_map(set
),
4090 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4096 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4097 enum isl_dim_type dst_type
, unsigned dst_pos
,
4098 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4105 map
= isl_map_reset(map
, src_type
);
4106 map
= isl_map_reset(map
, dst_type
);
4110 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
4113 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4116 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4118 map
= isl_map_cow(map
);
4122 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
4126 for (i
= 0; i
< map
->n
; ++i
) {
4127 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4129 src_type
, src_pos
, n
);
4140 /* Move the specified dimensions to the last columns right before
4141 * the divs. Don't change the dimension specification of bmap.
4142 * That's the responsibility of the caller.
4144 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4145 enum isl_dim_type type
, unsigned first
, unsigned n
)
4147 struct isl_dim_map
*dim_map
;
4148 struct isl_basic_map
*res
;
4149 enum isl_dim_type t
;
4150 unsigned total
, off
;
4154 if (pos(bmap
->dim
, type
) + first
+ n
==
4155 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
4158 total
= isl_basic_map_total_dim(bmap
);
4159 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4162 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4163 unsigned size
= isl_space_dim(bmap
->dim
, t
);
4165 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4168 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4169 first
, n
, total
- bmap
->n_div
- n
);
4170 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4171 first
+ n
, size
- (first
+ n
), off
);
4172 off
+= size
- (first
+ n
);
4174 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4178 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4180 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4181 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4182 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4186 /* Insert "n" rows in the divs of "bmap".
4188 * The number of columns is not changed, which means that the last
4189 * dimensions of "bmap" are being reintepreted as the new divs.
4190 * The space of "bmap" is not adjusted, however, which means
4191 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4192 * from the space of "bmap" is the responsibility of the caller.
4194 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4202 bmap
= isl_basic_map_cow(bmap
);
4206 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
4207 old
= bmap
->block2
.data
;
4208 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4209 (bmap
->extra
+ n
) * (1 + row_size
));
4210 if (!bmap
->block2
.data
)
4211 return isl_basic_map_free(bmap
);
4212 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4214 return isl_basic_map_free(bmap
);
4215 for (i
= 0; i
< n
; ++i
) {
4216 new_div
[i
] = bmap
->block2
.data
+
4217 (bmap
->extra
+ i
) * (1 + row_size
);
4218 isl_seq_clr(new_div
[i
], 1 + row_size
);
4220 for (i
= 0; i
< bmap
->extra
; ++i
)
4221 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4223 bmap
->div
= new_div
;
4230 /* Drop constraints from "bmap" that only involve the variables
4231 * of "type" in the range [first, first + n] that are not related
4232 * to any of the variables outside that interval.
4233 * These constraints cannot influence the values for the variables
4234 * outside the interval, except in case they cause "bmap" to be empty.
4235 * Only drop the constraints if "bmap" is known to be non-empty.
4237 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4238 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4239 unsigned first
, unsigned n
)
4243 unsigned dim
, n_div
;
4246 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4248 return isl_basic_map_free(bmap
);
4252 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4253 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4254 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4256 return isl_basic_map_free(bmap
);
4257 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4258 for (i
= 0; i
< first
; ++i
)
4260 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4263 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4268 /* Turn the n dimensions of type type, starting at first
4269 * into existentially quantified variables.
4271 * If a subset of the projected out variables are unrelated
4272 * to any of the variables that remain, then the constraints
4273 * involving this subset are simply dropped first.
4275 __isl_give isl_basic_map
*isl_basic_map_project_out(
4276 __isl_take isl_basic_map
*bmap
,
4277 enum isl_dim_type type
, unsigned first
, unsigned n
)
4282 return basic_map_space_reset(bmap
, type
);
4283 if (type
== isl_dim_div
)
4284 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4285 "cannot project out existentially quantified variables",
4286 return isl_basic_map_free(bmap
));
4288 empty
= isl_basic_map_plain_is_empty(bmap
);
4290 return isl_basic_map_free(bmap
);
4292 bmap
= isl_basic_map_set_to_empty(bmap
);
4294 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4298 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4299 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4301 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4302 return isl_basic_map_free(bmap
);
4304 bmap
= move_last(bmap
, type
, first
, n
);
4305 bmap
= isl_basic_map_cow(bmap
);
4306 bmap
= insert_div_rows(bmap
, n
);
4310 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
4313 bmap
= isl_basic_map_simplify(bmap
);
4314 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4315 return isl_basic_map_finalize(bmap
);
4317 isl_basic_map_free(bmap
);
4321 /* Turn the n dimensions of type type, starting at first
4322 * into existentially quantified variables.
4324 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
4325 enum isl_dim_type type
, unsigned first
, unsigned n
)
4327 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4331 /* Turn the n dimensions of type type, starting at first
4332 * into existentially quantified variables.
4334 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4335 enum isl_dim_type type
, unsigned first
, unsigned n
)
4343 return map_space_reset(map
, type
);
4345 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
4347 map
= isl_map_cow(map
);
4351 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
4355 for (i
= 0; i
< map
->n
; ++i
) {
4356 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4367 /* Turn the n dimensions of type type, starting at first
4368 * into existentially quantified variables.
4370 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4371 enum isl_dim_type type
, unsigned first
, unsigned n
)
4373 return set_from_map(isl_map_project_out(set_to_map(set
),
4377 /* Return a map that projects the elements in "set" onto their
4378 * "n" set dimensions starting at "first".
4379 * "type" should be equal to isl_dim_set.
4381 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4382 enum isl_dim_type type
, unsigned first
, unsigned n
)
4390 if (type
!= isl_dim_set
)
4391 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4392 "only set dimensions can be projected out", goto error
);
4393 dim
= isl_set_dim(set
, isl_dim_set
);
4394 if (first
+ n
> dim
|| first
+ n
< first
)
4395 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4396 "index out of bounds", goto error
);
4398 map
= isl_map_from_domain(set
);
4399 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4400 for (i
= 0; i
< n
; ++i
)
4401 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4409 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4413 for (i
= 0; i
< n
; ++i
) {
4414 j
= isl_basic_map_alloc_div(bmap
);
4417 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4421 isl_basic_map_free(bmap
);
4425 struct isl_basic_map
*isl_basic_map_apply_range(
4426 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4428 isl_space
*dim_result
= NULL
;
4429 struct isl_basic_map
*bmap
;
4430 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4431 struct isl_dim_map
*dim_map1
, *dim_map2
;
4433 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4435 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4436 bmap2
->dim
, isl_dim_in
))
4437 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4438 "spaces don't match", goto error
);
4440 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4441 isl_space_copy(bmap2
->dim
));
4443 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4444 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4445 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4446 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4448 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4449 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4450 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4451 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4452 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4453 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4454 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4455 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4456 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4457 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4458 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4460 bmap
= isl_basic_map_alloc_space(dim_result
,
4461 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4462 bmap1
->n_eq
+ bmap2
->n_eq
,
4463 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4464 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4465 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4466 bmap
= add_divs(bmap
, n
);
4467 bmap
= isl_basic_map_simplify(bmap
);
4468 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4469 return isl_basic_map_finalize(bmap
);
4471 isl_basic_map_free(bmap1
);
4472 isl_basic_map_free(bmap2
);
4476 struct isl_basic_set
*isl_basic_set_apply(
4477 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4482 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4485 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4488 isl_basic_set_free(bset
);
4489 isl_basic_map_free(bmap
);
4493 struct isl_basic_map
*isl_basic_map_apply_domain(
4494 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4496 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4498 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4499 bmap2
->dim
, isl_dim_in
))
4500 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4501 "spaces don't match", goto error
);
4503 bmap1
= isl_basic_map_reverse(bmap1
);
4504 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4505 return isl_basic_map_reverse(bmap1
);
4507 isl_basic_map_free(bmap1
);
4508 isl_basic_map_free(bmap2
);
4512 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4513 * A \cap B -> f(A) + f(B)
4515 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4516 __isl_take isl_basic_map
*bmap2
)
4518 unsigned n_in
, n_out
, nparam
, total
, pos
;
4519 struct isl_basic_map
*bmap
= NULL
;
4520 struct isl_dim_map
*dim_map1
, *dim_map2
;
4523 if (!bmap1
|| !bmap2
)
4526 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4529 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4530 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4531 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4533 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4534 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4535 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4536 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4537 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4538 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4539 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4540 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4541 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4542 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4543 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4545 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4546 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4547 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4548 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4549 for (i
= 0; i
< n_out
; ++i
) {
4550 int j
= isl_basic_map_alloc_equality(bmap
);
4553 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4554 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4555 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4556 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4558 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4559 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4560 bmap
= add_divs(bmap
, 2 * n_out
);
4562 bmap
= isl_basic_map_simplify(bmap
);
4563 return isl_basic_map_finalize(bmap
);
4565 isl_basic_map_free(bmap
);
4566 isl_basic_map_free(bmap1
);
4567 isl_basic_map_free(bmap2
);
4571 /* Given two maps A -> f(A) and B -> g(B), construct a map
4572 * A \cap B -> f(A) + f(B)
4574 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4575 __isl_take isl_map
*map2
)
4577 struct isl_map
*result
;
4583 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4585 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4586 map1
->n
* map2
->n
, 0);
4589 for (i
= 0; i
< map1
->n
; ++i
)
4590 for (j
= 0; j
< map2
->n
; ++j
) {
4591 struct isl_basic_map
*part
;
4592 part
= isl_basic_map_sum(
4593 isl_basic_map_copy(map1
->p
[i
]),
4594 isl_basic_map_copy(map2
->p
[j
]));
4595 if (isl_basic_map_is_empty(part
))
4596 isl_basic_map_free(part
);
4598 result
= isl_map_add_basic_map(result
, part
);
4611 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4612 __isl_take isl_set
*set2
)
4614 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4617 /* Given a basic map A -> f(A), construct A -> -f(A).
4619 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4624 bmap
= isl_basic_map_cow(bmap
);
4628 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4629 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4630 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4631 for (j
= 0; j
< n
; ++j
)
4632 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4633 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4634 for (j
= 0; j
< n
; ++j
)
4635 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4636 for (i
= 0; i
< bmap
->n_div
; ++i
)
4637 for (j
= 0; j
< n
; ++j
)
4638 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4639 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4640 return isl_basic_map_finalize(bmap
);
4643 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4645 return isl_basic_map_neg(bset
);
4648 /* Given a map A -> f(A), construct A -> -f(A).
4650 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4654 map
= isl_map_cow(map
);
4658 for (i
= 0; i
< map
->n
; ++i
) {
4659 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4670 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4672 return set_from_map(isl_map_neg(set_to_map(set
)));
4675 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4676 * A -> floor(f(A)/d).
4678 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4681 unsigned n_in
, n_out
, nparam
, total
, pos
;
4682 struct isl_basic_map
*result
= NULL
;
4683 struct isl_dim_map
*dim_map
;
4689 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4690 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4691 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4693 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4694 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4695 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4696 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4697 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4698 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4700 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4701 bmap
->n_div
+ n_out
,
4702 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4703 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4704 result
= add_divs(result
, n_out
);
4705 for (i
= 0; i
< n_out
; ++i
) {
4707 j
= isl_basic_map_alloc_inequality(result
);
4710 isl_seq_clr(result
->ineq
[j
], 1+total
);
4711 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4712 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4713 j
= isl_basic_map_alloc_inequality(result
);
4716 isl_seq_clr(result
->ineq
[j
], 1+total
);
4717 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4718 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4719 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4722 result
= isl_basic_map_simplify(result
);
4723 return isl_basic_map_finalize(result
);
4725 isl_basic_map_free(result
);
4729 /* Given a map A -> f(A) and an integer d, construct a map
4730 * A -> floor(f(A)/d).
4732 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4736 map
= isl_map_cow(map
);
4740 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4741 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4742 for (i
= 0; i
< map
->n
; ++i
) {
4743 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4754 /* Given a map A -> f(A) and an integer d, construct a map
4755 * A -> floor(f(A)/d).
4757 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4758 __isl_take isl_val
*d
)
4762 if (!isl_val_is_int(d
))
4763 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4764 "expecting integer denominator", goto error
);
4765 map
= isl_map_floordiv(map
, d
->n
);
4774 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4781 i
= isl_basic_map_alloc_equality(bmap
);
4784 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4785 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4786 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4787 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4788 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4789 return isl_basic_map_finalize(bmap
);
4791 isl_basic_map_free(bmap
);
4795 /* Add a constraint to "bmap" expressing i_pos < o_pos
4797 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4804 i
= isl_basic_map_alloc_inequality(bmap
);
4807 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4808 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4809 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4810 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4811 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4812 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4813 return isl_basic_map_finalize(bmap
);
4815 isl_basic_map_free(bmap
);
4819 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4821 static __isl_give isl_basic_map
*var_less_or_equal(
4822 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4828 i
= isl_basic_map_alloc_inequality(bmap
);
4831 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4832 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4833 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4834 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4835 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4836 return isl_basic_map_finalize(bmap
);
4838 isl_basic_map_free(bmap
);
4842 /* Add a constraint to "bmap" expressing i_pos > o_pos
4844 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
4851 i
= isl_basic_map_alloc_inequality(bmap
);
4854 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4855 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4856 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4857 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4858 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4859 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4860 return isl_basic_map_finalize(bmap
);
4862 isl_basic_map_free(bmap
);
4866 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4868 static __isl_give isl_basic_map
*var_more_or_equal(
4869 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4875 i
= isl_basic_map_alloc_inequality(bmap
);
4878 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4879 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4880 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4881 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4882 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4883 return isl_basic_map_finalize(bmap
);
4885 isl_basic_map_free(bmap
);
4889 __isl_give isl_basic_map
*isl_basic_map_equal(
4890 __isl_take isl_space
*dim
, unsigned n_equal
)
4893 struct isl_basic_map
*bmap
;
4894 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4897 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4898 bmap
= var_equal(bmap
, i
);
4899 return isl_basic_map_finalize(bmap
);
4902 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4904 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4908 struct isl_basic_map
*bmap
;
4909 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4912 for (i
= 0; i
< pos
&& bmap
; ++i
)
4913 bmap
= var_equal(bmap
, i
);
4915 bmap
= var_less(bmap
, pos
);
4916 return isl_basic_map_finalize(bmap
);
4919 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4921 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4922 __isl_take isl_space
*dim
, unsigned pos
)
4925 isl_basic_map
*bmap
;
4927 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4928 for (i
= 0; i
< pos
; ++i
)
4929 bmap
= var_equal(bmap
, i
);
4930 bmap
= var_less_or_equal(bmap
, pos
);
4931 return isl_basic_map_finalize(bmap
);
4934 /* Return a relation on "dim" expressing i_pos > o_pos
4936 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4940 struct isl_basic_map
*bmap
;
4941 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4944 for (i
= 0; i
< pos
&& bmap
; ++i
)
4945 bmap
= var_equal(bmap
, i
);
4947 bmap
= var_more(bmap
, pos
);
4948 return isl_basic_map_finalize(bmap
);
4951 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4953 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4954 __isl_take isl_space
*dim
, unsigned pos
)
4957 isl_basic_map
*bmap
;
4959 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4960 for (i
= 0; i
< pos
; ++i
)
4961 bmap
= var_equal(bmap
, i
);
4962 bmap
= var_more_or_equal(bmap
, pos
);
4963 return isl_basic_map_finalize(bmap
);
4966 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4967 unsigned n
, int equal
)
4969 struct isl_map
*map
;
4972 if (n
== 0 && equal
)
4973 return isl_map_universe(dims
);
4975 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4977 for (i
= 0; i
+ 1 < n
; ++i
)
4978 map
= isl_map_add_basic_map(map
,
4979 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4982 map
= isl_map_add_basic_map(map
,
4983 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4985 map
= isl_map_add_basic_map(map
,
4986 isl_basic_map_less_at(dims
, n
- 1));
4988 isl_space_free(dims
);
4993 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4997 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
5000 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
5002 return map_lex_lte_first(dim
, n
, 0);
5005 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
5007 return map_lex_lte_first(dim
, n
, 1);
5010 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
5012 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
5015 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
5017 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
5020 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
5021 unsigned n
, int equal
)
5023 struct isl_map
*map
;
5026 if (n
== 0 && equal
)
5027 return isl_map_universe(dims
);
5029 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
5031 for (i
= 0; i
+ 1 < n
; ++i
)
5032 map
= isl_map_add_basic_map(map
,
5033 isl_basic_map_more_at(isl_space_copy(dims
), i
));
5036 map
= isl_map_add_basic_map(map
,
5037 isl_basic_map_more_or_equal_at(dims
, n
- 1));
5039 map
= isl_map_add_basic_map(map
,
5040 isl_basic_map_more_at(dims
, n
- 1));
5042 isl_space_free(dims
);
5047 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
5051 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
5054 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
5056 return map_lex_gte_first(dim
, n
, 0);
5059 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
5061 return map_lex_gte_first(dim
, n
, 1);
5064 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
5066 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
5069 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
5071 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
5074 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5075 __isl_take isl_set
*set2
)
5078 map
= isl_map_lex_le(isl_set_get_space(set1
));
5079 map
= isl_map_intersect_domain(map
, set1
);
5080 map
= isl_map_intersect_range(map
, set2
);
5084 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5085 __isl_take isl_set
*set2
)
5088 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5089 map
= isl_map_intersect_domain(map
, set1
);
5090 map
= isl_map_intersect_range(map
, set2
);
5094 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5095 __isl_take isl_set
*set2
)
5098 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5099 map
= isl_map_intersect_domain(map
, set1
);
5100 map
= isl_map_intersect_range(map
, set2
);
5104 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5105 __isl_take isl_set
*set2
)
5108 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5109 map
= isl_map_intersect_domain(map
, set1
);
5110 map
= isl_map_intersect_range(map
, set2
);
5114 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5115 __isl_take isl_map
*map2
)
5118 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5119 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5120 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5124 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5125 __isl_take isl_map
*map2
)
5128 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5129 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5130 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5134 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5135 __isl_take isl_map
*map2
)
5138 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5139 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5140 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5144 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5145 __isl_take isl_map
*map2
)
5148 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5149 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5150 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5154 /* For a div d = floor(f/m), add the constraint
5158 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
5159 unsigned pos
, isl_int
*div
)
5162 unsigned total
= isl_basic_map_total_dim(bmap
);
5164 i
= isl_basic_map_alloc_inequality(bmap
);
5166 return isl_stat_error
;
5167 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5168 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
5173 /* For a div d = floor(f/m), add the constraint
5175 * -(f-(m-1)) + m d >= 0
5177 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
5178 unsigned pos
, isl_int
*div
)
5181 unsigned total
= isl_basic_map_total_dim(bmap
);
5183 i
= isl_basic_map_alloc_inequality(bmap
);
5185 return isl_stat_error
;
5186 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
5187 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
5188 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5189 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5194 /* For a div d = floor(f/m), add the constraints
5197 * -(f-(m-1)) + m d >= 0
5199 * Note that the second constraint is the negation of
5203 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
5204 unsigned pos
, isl_int
*div
)
5206 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
5208 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
5213 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
5214 unsigned pos
, isl_int
*div
)
5216 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
5220 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
5222 unsigned total
= isl_basic_map_total_dim(bmap
);
5223 unsigned div_pos
= total
- bmap
->n_div
+ div
;
5225 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
5229 /* For each known div d = floor(f/m), add the constraints
5232 * -(f-(m-1)) + m d >= 0
5234 * Remove duplicate constraints in case of some these div constraints
5235 * already appear in "bmap".
5237 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5238 __isl_take isl_basic_map
*bmap
)
5244 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5248 bmap
= add_known_div_constraints(bmap
);
5249 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5250 bmap
= isl_basic_map_finalize(bmap
);
5254 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5256 * In particular, if this div is of the form d = floor(f/m),
5257 * then add the constraint
5261 * if sign < 0 or the constraint
5263 * -(f-(m-1)) + m d >= 0
5267 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
5268 unsigned div
, int sign
)
5276 total
= isl_basic_map_total_dim(bmap
);
5277 div_pos
= total
- bmap
->n_div
+ div
;
5280 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5282 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
5285 struct isl_basic_set
*isl_basic_map_underlying_set(
5286 struct isl_basic_map
*bmap
)
5290 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5292 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5293 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5294 return bset_from_bmap(bmap
);
5295 bmap
= isl_basic_map_cow(bmap
);
5298 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
5301 bmap
->extra
-= bmap
->n_div
;
5303 bmap
= isl_basic_map_finalize(bmap
);
5304 return bset_from_bmap(bmap
);
5306 isl_basic_map_free(bmap
);
5310 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5311 __isl_take isl_basic_set
*bset
)
5313 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5316 /* Replace each element in "list" by the result of applying
5317 * isl_basic_map_underlying_set to the element.
5319 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5320 __isl_take isl_basic_map_list
*list
)
5327 n
= isl_basic_map_list_n_basic_map(list
);
5328 for (i
= 0; i
< n
; ++i
) {
5329 isl_basic_map
*bmap
;
5330 isl_basic_set
*bset
;
5332 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5333 bset
= isl_basic_set_underlying_set(bmap
);
5334 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5340 struct isl_basic_map
*isl_basic_map_overlying_set(
5341 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
5343 struct isl_basic_map
*bmap
;
5344 struct isl_ctx
*ctx
;
5351 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5352 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5353 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5355 if (like
->n_div
== 0) {
5356 isl_space
*space
= isl_basic_map_get_space(like
);
5357 isl_basic_map_free(like
);
5358 return isl_basic_map_reset_space(bset
, space
);
5360 bset
= isl_basic_set_cow(bset
);
5363 total
= bset
->dim
->n_out
+ bset
->extra
;
5364 bmap
= bset_to_bmap(bset
);
5365 isl_space_free(bmap
->dim
);
5366 bmap
->dim
= isl_space_copy(like
->dim
);
5369 bmap
->n_div
= like
->n_div
;
5370 bmap
->extra
+= like
->n_div
;
5374 ltotal
= total
- bmap
->extra
+ like
->extra
;
5377 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5378 bmap
->extra
* (1 + 1 + total
));
5379 if (isl_blk_is_error(bmap
->block2
))
5381 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5385 for (i
= 0; i
< bmap
->extra
; ++i
)
5386 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5387 for (i
= 0; i
< like
->n_div
; ++i
) {
5388 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5389 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5391 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5393 isl_basic_map_free(like
);
5394 bmap
= isl_basic_map_simplify(bmap
);
5395 bmap
= isl_basic_map_finalize(bmap
);
5398 isl_basic_map_free(like
);
5399 isl_basic_set_free(bset
);
5403 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5404 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5406 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5407 bset_to_bmap(like
)));
5410 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5414 map
= isl_map_cow(map
);
5417 map
->dim
= isl_space_cow(map
->dim
);
5421 for (i
= 1; i
< map
->n
; ++i
)
5422 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5424 for (i
= 0; i
< map
->n
; ++i
) {
5425 map
->p
[i
] = bset_to_bmap(
5426 isl_basic_map_underlying_set(map
->p
[i
]));
5431 map
->dim
= isl_space_underlying(map
->dim
, 0);
5433 isl_space_free(map
->dim
);
5434 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5438 return set_from_map(map
);
5444 /* Replace the space of "bmap" by "space".
5446 * If the space of "bmap" is identical to "space" (including the identifiers
5447 * of the input and output dimensions), then simply return the original input.
5449 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5450 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5453 isl_space
*bmap_space
;
5455 bmap_space
= isl_basic_map_peek_space(bmap
);
5456 equal
= isl_space_is_equal(bmap_space
, space
);
5457 if (equal
>= 0 && equal
)
5458 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5462 isl_space_free(space
);
5465 bmap
= isl_basic_map_cow(bmap
);
5466 if (!bmap
|| !space
)
5469 isl_space_free(bmap
->dim
);
5472 bmap
= isl_basic_map_finalize(bmap
);
5476 isl_basic_map_free(bmap
);
5477 isl_space_free(space
);
5481 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5482 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5484 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5488 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5489 __isl_take isl_space
*dim
)
5493 map
= isl_map_cow(map
);
5497 for (i
= 0; i
< map
->n
; ++i
) {
5498 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5499 isl_space_copy(dim
));
5503 isl_space_free(map
->dim
);
5509 isl_space_free(dim
);
5513 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5514 __isl_take isl_space
*dim
)
5516 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5519 /* Compute the parameter domain of the given basic set.
5521 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5527 is_params
= isl_basic_set_is_params(bset
);
5529 return isl_basic_set_free(bset
);
5533 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5534 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5535 space
= isl_basic_set_get_space(bset
);
5536 space
= isl_space_params(space
);
5537 bset
= isl_basic_set_reset_space(bset
, space
);
5541 /* Construct a zero-dimensional basic set with the given parameter domain.
5543 __isl_give isl_basic_set
*isl_basic_set_from_params(
5544 __isl_take isl_basic_set
*bset
)
5547 space
= isl_basic_set_get_space(bset
);
5548 space
= isl_space_set_from_params(space
);
5549 bset
= isl_basic_set_reset_space(bset
, space
);
5553 /* Compute the parameter domain of the given set.
5555 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5560 if (isl_set_is_params(set
))
5563 n
= isl_set_dim(set
, isl_dim_set
);
5564 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5565 space
= isl_set_get_space(set
);
5566 space
= isl_space_params(space
);
5567 set
= isl_set_reset_space(set
, space
);
5571 /* Construct a zero-dimensional set with the given parameter domain.
5573 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5576 space
= isl_set_get_space(set
);
5577 space
= isl_space_set_from_params(space
);
5578 set
= isl_set_reset_space(set
, space
);
5582 /* Compute the parameter domain of the given map.
5584 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5589 n
= isl_map_dim(map
, isl_dim_in
);
5590 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5591 n
= isl_map_dim(map
, isl_dim_out
);
5592 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5593 space
= isl_map_get_space(map
);
5594 space
= isl_space_params(space
);
5595 map
= isl_map_reset_space(map
, space
);
5599 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5606 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5608 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5609 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5611 return isl_basic_map_reset_space(bmap
, space
);
5614 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5617 return isl_bool_error
;
5618 return isl_space_may_be_set(bmap
->dim
);
5621 /* Is this basic map actually a set?
5622 * Users should never call this function. Outside of isl,
5623 * the type should indicate whether something is a set or a map.
5625 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5628 return isl_bool_error
;
5629 return isl_space_is_set(bmap
->dim
);
5632 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5636 is_set
= isl_basic_map_is_set(bmap
);
5641 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5643 isl_basic_map_free(bmap
);
5647 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5648 __isl_take isl_basic_map
*bmap
)
5652 isl_basic_map
*domain
;
5653 int nparam
, n_in
, n_out
;
5655 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5656 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5657 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5659 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5660 domain
= isl_basic_map_universe(dim
);
5662 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5663 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5664 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5666 for (i
= 0; i
< n_in
; ++i
)
5667 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5670 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5671 return isl_basic_map_finalize(bmap
);
5674 __isl_give isl_basic_map
*isl_basic_map_range_map(
5675 __isl_take isl_basic_map
*bmap
)
5679 isl_basic_map
*range
;
5680 int nparam
, n_in
, n_out
;
5682 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5683 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5684 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5686 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5687 range
= isl_basic_map_universe(dim
);
5689 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5690 bmap
= isl_basic_map_apply_range(bmap
, range
);
5691 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5693 for (i
= 0; i
< n_out
; ++i
)
5694 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5697 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5698 return isl_basic_map_finalize(bmap
);
5701 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5705 return isl_space_may_be_set(map
->dim
);
5708 /* Is this map actually a set?
5709 * Users should never call this function. Outside of isl,
5710 * the type should indicate whether something is a set or a map.
5712 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5715 return isl_bool_error
;
5716 return isl_space_is_set(map
->dim
);
5719 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5723 struct isl_set
*set
;
5725 is_set
= isl_map_is_set(map
);
5729 return set_from_map(map
);
5731 map
= isl_map_cow(map
);
5735 set
= set_from_map(map
);
5736 set
->dim
= isl_space_range(set
->dim
);
5739 for (i
= 0; i
< map
->n
; ++i
) {
5740 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5744 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5745 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5752 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5756 map
= isl_map_cow(map
);
5760 map
->dim
= isl_space_domain_map(map
->dim
);
5763 for (i
= 0; i
< map
->n
; ++i
) {
5764 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5768 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5769 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5776 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5779 isl_space
*range_dim
;
5781 map
= isl_map_cow(map
);
5785 range_dim
= isl_space_range(isl_map_get_space(map
));
5786 range_dim
= isl_space_from_range(range_dim
);
5787 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5788 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5791 for (i
= 0; i
< map
->n
; ++i
) {
5792 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5796 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5797 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5804 /* Given a wrapped map of the form A[B -> C],
5805 * return the map A[B -> C] -> B.
5807 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5814 if (!isl_set_has_tuple_id(set
))
5815 return isl_map_domain_map(isl_set_unwrap(set
));
5817 id
= isl_set_get_tuple_id(set
);
5818 map
= isl_map_domain_map(isl_set_unwrap(set
));
5819 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5824 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5825 __isl_take isl_basic_set
*bset
)
5827 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5830 __isl_give isl_basic_map
*isl_basic_map_from_range(
5831 __isl_take isl_basic_set
*bset
)
5834 space
= isl_basic_set_get_space(bset
);
5835 space
= isl_space_from_range(space
);
5836 bset
= isl_basic_set_reset_space(bset
, space
);
5837 return bset_to_bmap(bset
);
5840 /* Create a relation with the given set as range.
5841 * The domain of the created relation is a zero-dimensional
5842 * flat anonymous space.
5844 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5847 space
= isl_set_get_space(set
);
5848 space
= isl_space_from_range(space
);
5849 set
= isl_set_reset_space(set
, space
);
5850 return set_to_map(set
);
5853 /* Create a relation with the given set as domain.
5854 * The range of the created relation is a zero-dimensional
5855 * flat anonymous space.
5857 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5859 return isl_map_reverse(isl_map_from_range(set
));
5862 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5863 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5865 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5868 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5869 __isl_take isl_set
*range
)
5871 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5874 /* Return a newly allocated isl_map with given space and flags and
5875 * room for "n" basic maps.
5876 * Make sure that all cached information is cleared.
5878 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5881 struct isl_map
*map
;
5886 isl_die(space
->ctx
, isl_error_internal
,
5887 "negative number of basic maps", goto error
);
5888 map
= isl_calloc(space
->ctx
, struct isl_map
,
5889 sizeof(struct isl_map
) +
5890 (n
- 1) * sizeof(struct isl_basic_map
*));
5894 map
->ctx
= space
->ctx
;
5895 isl_ctx_ref(map
->ctx
);
5903 isl_space_free(space
);
5907 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5909 struct isl_basic_map
*bmap
;
5910 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5911 bmap
= isl_basic_map_set_to_empty(bmap
);
5915 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5917 struct isl_basic_set
*bset
;
5918 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5919 bset
= isl_basic_set_set_to_empty(bset
);
5923 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5925 struct isl_basic_map
*bmap
;
5926 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5927 bmap
= isl_basic_map_finalize(bmap
);
5931 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5933 struct isl_basic_set
*bset
;
5934 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5935 bset
= isl_basic_set_finalize(bset
);
5939 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5942 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5943 isl_basic_map
*bmap
;
5945 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5946 for (i
= 0; i
< total
; ++i
) {
5947 int k
= isl_basic_map_alloc_inequality(bmap
);
5950 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5951 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5955 isl_basic_map_free(bmap
);
5959 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5961 return isl_basic_map_nat_universe(dim
);
5964 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5966 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5969 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5971 return isl_map_nat_universe(dim
);
5974 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5976 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5979 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5981 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5984 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5986 struct isl_map
*map
;
5989 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5990 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5994 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5996 struct isl_set
*set
;
5999 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
6000 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
6004 struct isl_map
*isl_map_dup(struct isl_map
*map
)
6007 struct isl_map
*dup
;
6011 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6012 for (i
= 0; i
< map
->n
; ++i
)
6013 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6017 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6018 __isl_take isl_basic_map
*bmap
)
6022 if (isl_basic_map_plain_is_empty(bmap
)) {
6023 isl_basic_map_free(bmap
);
6026 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
6027 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6028 map
->p
[map
->n
] = bmap
;
6030 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6036 isl_basic_map_free(bmap
);
6040 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6051 isl_ctx_deref(map
->ctx
);
6052 for (i
= 0; i
< map
->n
; ++i
)
6053 isl_basic_map_free(map
->p
[i
]);
6054 isl_space_free(map
->dim
);
6060 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
6061 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
6065 bmap
= isl_basic_map_cow(bmap
);
6066 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6067 j
= isl_basic_map_alloc_equality(bmap
);
6070 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6071 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6072 isl_int_set_si(bmap
->eq
[j
][0], value
);
6073 bmap
= isl_basic_map_simplify(bmap
);
6074 return isl_basic_map_finalize(bmap
);
6076 isl_basic_map_free(bmap
);
6080 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6081 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6085 bmap
= isl_basic_map_cow(bmap
);
6086 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6087 j
= isl_basic_map_alloc_equality(bmap
);
6090 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
6091 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6092 isl_int_set(bmap
->eq
[j
][0], value
);
6093 bmap
= isl_basic_map_simplify(bmap
);
6094 return isl_basic_map_finalize(bmap
);
6096 isl_basic_map_free(bmap
);
6100 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6101 enum isl_dim_type type
, unsigned pos
, int value
)
6103 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6104 return isl_basic_map_free(bmap
);
6105 return isl_basic_map_fix_pos_si(bmap
,
6106 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6109 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6110 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6112 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6113 return isl_basic_map_free(bmap
);
6114 return isl_basic_map_fix_pos(bmap
,
6115 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6118 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6119 * to be equal to "v".
6121 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6122 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6126 if (!isl_val_is_int(v
))
6127 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6128 "expecting integer value", goto error
);
6129 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6131 pos
+= isl_basic_map_offset(bmap
, type
);
6132 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6136 isl_basic_map_free(bmap
);
6141 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6142 * to be equal to "v".
6144 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6145 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6147 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6150 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
6151 enum isl_dim_type type
, unsigned pos
, int value
)
6153 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6157 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6158 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6160 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6164 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
6165 unsigned input
, int value
)
6167 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
6170 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
6171 unsigned dim
, int value
)
6173 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6174 isl_dim_set
, dim
, value
));
6177 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
6179 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6186 isl_basic_map_free(map
->p
[i
]);
6187 if (i
!= map
->n
- 1) {
6188 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6189 map
->p
[i
] = map
->p
[map
->n
- 1];
6196 /* Perform "fn" on each basic map of "map", where we may not be holding
6197 * the only reference to "map".
6198 * In particular, "fn" should be a semantics preserving operation
6199 * that we want to apply to all copies of "map". We therefore need
6200 * to be careful not to modify "map" in a way that breaks "map"
6201 * in case anything goes wrong.
6203 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6204 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6206 struct isl_basic_map
*bmap
;
6212 for (i
= map
->n
- 1; i
>= 0; --i
) {
6213 bmap
= isl_basic_map_copy(map
->p
[i
]);
6217 isl_basic_map_free(map
->p
[i
]);
6219 if (remove_if_empty(map
, i
) < 0)
6229 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6230 enum isl_dim_type type
, unsigned pos
, int value
)
6234 map
= isl_map_cow(map
);
6238 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6239 for (i
= map
->n
- 1; i
>= 0; --i
) {
6240 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6241 if (remove_if_empty(map
, i
) < 0)
6244 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6251 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6252 enum isl_dim_type type
, unsigned pos
, int value
)
6254 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6257 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6258 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6262 map
= isl_map_cow(map
);
6266 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6267 for (i
= 0; i
< map
->n
; ++i
) {
6268 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6272 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6279 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6280 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6282 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6285 /* Fix the value of the variable at position "pos" of type "type" of "map"
6286 * to be equal to "v".
6288 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6289 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6293 map
= isl_map_cow(map
);
6297 if (!isl_val_is_int(v
))
6298 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6299 "expecting integer value", goto error
);
6300 if (pos
>= isl_map_dim(map
, type
))
6301 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6302 "index out of bounds", goto error
);
6303 for (i
= map
->n
- 1; i
>= 0; --i
) {
6304 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6306 if (remove_if_empty(map
, i
) < 0)
6309 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6318 /* Fix the value of the variable at position "pos" of type "type" of "set"
6319 * to be equal to "v".
6321 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6322 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6324 return isl_map_fix_val(set
, type
, pos
, v
);
6327 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6328 unsigned input
, int value
)
6330 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6333 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6335 return set_from_map(isl_map_fix_si(set_to_map(set
),
6336 isl_dim_set
, dim
, value
));
6339 static __isl_give isl_basic_map
*basic_map_bound_si(
6340 __isl_take isl_basic_map
*bmap
,
6341 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6345 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6346 return isl_basic_map_free(bmap
);
6347 pos
+= isl_basic_map_offset(bmap
, type
);
6348 bmap
= isl_basic_map_cow(bmap
);
6349 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6350 j
= isl_basic_map_alloc_inequality(bmap
);
6353 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6355 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6356 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6358 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6359 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6361 bmap
= isl_basic_map_simplify(bmap
);
6362 return isl_basic_map_finalize(bmap
);
6364 isl_basic_map_free(bmap
);
6368 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6369 __isl_take isl_basic_map
*bmap
,
6370 enum isl_dim_type type
, unsigned pos
, int value
)
6372 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6375 /* Constrain the values of the given dimension to be no greater than "value".
6377 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6378 __isl_take isl_basic_map
*bmap
,
6379 enum isl_dim_type type
, unsigned pos
, int value
)
6381 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6384 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6385 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6389 map
= isl_map_cow(map
);
6393 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6394 for (i
= 0; i
< map
->n
; ++i
) {
6395 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6396 type
, pos
, value
, upper
);
6400 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6407 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6408 enum isl_dim_type type
, unsigned pos
, int value
)
6410 return map_bound_si(map
, type
, pos
, value
, 0);
6413 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6414 enum isl_dim_type type
, unsigned pos
, int value
)
6416 return map_bound_si(map
, type
, pos
, value
, 1);
6419 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6420 enum isl_dim_type type
, unsigned pos
, int value
)
6422 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6426 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6427 enum isl_dim_type type
, unsigned pos
, int value
)
6429 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6432 /* Bound the given variable of "bmap" from below (or above is "upper"
6433 * is set) to "value".
6435 static __isl_give isl_basic_map
*basic_map_bound(
6436 __isl_take isl_basic_map
*bmap
,
6437 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6441 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6442 return isl_basic_map_free(bmap
);
6443 pos
+= isl_basic_map_offset(bmap
, type
);
6444 bmap
= isl_basic_map_cow(bmap
);
6445 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6446 j
= isl_basic_map_alloc_inequality(bmap
);
6449 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6451 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6452 isl_int_set(bmap
->ineq
[j
][0], value
);
6454 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6455 isl_int_neg(bmap
->ineq
[j
][0], value
);
6457 bmap
= isl_basic_map_simplify(bmap
);
6458 return isl_basic_map_finalize(bmap
);
6460 isl_basic_map_free(bmap
);
6464 /* Bound the given variable of "map" from below (or above is "upper"
6465 * is set) to "value".
6467 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6468 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6472 map
= isl_map_cow(map
);
6476 if (pos
>= isl_map_dim(map
, type
))
6477 isl_die(map
->ctx
, isl_error_invalid
,
6478 "index out of bounds", goto error
);
6479 for (i
= map
->n
- 1; i
>= 0; --i
) {
6480 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6481 if (remove_if_empty(map
, i
) < 0)
6484 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6491 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6492 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6494 return map_bound(map
, type
, pos
, value
, 0);
6497 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6498 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6500 return map_bound(map
, type
, pos
, value
, 1);
6503 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6504 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6506 return isl_map_lower_bound(set
, type
, pos
, value
);
6509 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6510 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6512 return isl_map_upper_bound(set
, type
, pos
, value
);
6515 /* Force the values of the variable at position "pos" of type "type" of "set"
6516 * to be no smaller than "value".
6518 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6519 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6523 if (!isl_val_is_int(value
))
6524 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6525 "expecting integer value", goto error
);
6526 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6527 isl_val_free(value
);
6530 isl_val_free(value
);
6535 /* Force the values of the variable at position "pos" of type "type" of "set"
6536 * to be no greater than "value".
6538 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6539 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6543 if (!isl_val_is_int(value
))
6544 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6545 "expecting integer value", goto error
);
6546 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6547 isl_val_free(value
);
6550 isl_val_free(value
);
6555 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
6559 map
= isl_map_cow(map
);
6563 map
->dim
= isl_space_reverse(map
->dim
);
6566 for (i
= 0; i
< map
->n
; ++i
) {
6567 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6571 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6579 #define TYPE isl_pw_multi_aff
6581 #define SUFFIX _pw_multi_aff
6583 #define EMPTY isl_pw_multi_aff_empty
6585 #define ADD isl_pw_multi_aff_union_add
6586 #include "isl_map_lexopt_templ.c"
6588 /* Given a map "map", compute the lexicographically minimal
6589 * (or maximal) image element for each domain element in dom,
6590 * in the form of an isl_pw_multi_aff.
6591 * If "empty" is not NULL, then set *empty to those elements in dom that
6592 * do not have an image element.
6593 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6594 * should be computed over the domain of "map". "empty" is also NULL
6597 * We first compute the lexicographically minimal or maximal element
6598 * in the first basic map. This results in a partial solution "res"
6599 * and a subset "todo" of dom that still need to be handled.
6600 * We then consider each of the remaining maps in "map" and successively
6601 * update both "res" and "todo".
6602 * If "empty" is NULL, then the todo sets are not needed and therefore
6603 * also not computed.
6605 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6606 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6607 __isl_give isl_set
**empty
, unsigned flags
)
6611 isl_pw_multi_aff
*res
;
6614 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6615 if (!map
|| (!full
&& !dom
))
6618 if (isl_map_plain_is_empty(map
)) {
6623 return isl_pw_multi_aff_from_map(map
);
6626 res
= basic_map_partial_lexopt_pw_multi_aff(
6627 isl_basic_map_copy(map
->p
[0]),
6628 isl_set_copy(dom
), empty
, flags
);
6632 for (i
= 1; i
< map
->n
; ++i
) {
6633 isl_pw_multi_aff
*res_i
;
6635 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6636 isl_basic_map_copy(map
->p
[i
]),
6637 isl_set_copy(dom
), empty
, flags
);
6639 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6640 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6642 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6645 todo
= isl_set_intersect(todo
, *empty
);
6664 #define TYPE isl_map
6668 #define EMPTY isl_map_empty
6670 #define ADD isl_map_union_disjoint
6671 #include "isl_map_lexopt_templ.c"
6673 /* Given a map "map", compute the lexicographically minimal
6674 * (or maximal) image element for each domain element in "dom",
6675 * in the form of an isl_map.
6676 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6677 * do not have an image element.
6678 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6679 * should be computed over the domain of "map". "empty" is also NULL
6682 * If the input consists of more than one disjunct, then first
6683 * compute the desired result in the form of an isl_pw_multi_aff and
6684 * then convert that into an isl_map.
6686 * This function used to have an explicit implementation in terms
6687 * of isl_maps, but it would continually intersect the domains of
6688 * partial results with the complement of the domain of the next
6689 * partial solution, potentially leading to an explosion in the number
6690 * of disjuncts if there are several disjuncts in the input.
6691 * An even earlier implementation of this function would look for
6692 * better results in the domain of the partial result and for extra
6693 * results in the complement of this domain, which would lead to
6694 * even more splintering.
6696 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6697 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6698 __isl_give isl_set
**empty
, unsigned flags
)
6701 struct isl_map
*res
;
6702 isl_pw_multi_aff
*pma
;
6704 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6705 if (!map
|| (!full
&& !dom
))
6708 if (isl_map_plain_is_empty(map
)) {
6717 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6723 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6725 return isl_map_from_pw_multi_aff(pma
);
6734 __isl_give isl_map
*isl_map_partial_lexmax(
6735 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6736 __isl_give isl_set
**empty
)
6738 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6741 __isl_give isl_map
*isl_map_partial_lexmin(
6742 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6743 __isl_give isl_set
**empty
)
6745 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6748 __isl_give isl_set
*isl_set_partial_lexmin(
6749 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6750 __isl_give isl_set
**empty
)
6752 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6756 __isl_give isl_set
*isl_set_partial_lexmax(
6757 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6758 __isl_give isl_set
**empty
)
6760 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6764 /* Compute the lexicographic minimum (or maximum if "flags" includes
6765 * ISL_OPT_MAX) of "bset" over its parametric domain.
6767 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6770 return isl_basic_map_lexopt(bset
, flags
);
6773 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6775 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6778 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6780 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6783 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6785 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6788 /* Compute the lexicographic minimum of "bset" over its parametric domain
6789 * for the purpose of quantifier elimination.
6790 * That is, find an explicit representation for all the existentially
6791 * quantified variables in "bset" by computing their lexicographic
6794 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6795 __isl_take isl_basic_set
*bset
)
6797 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6800 /* Extract the first and only affine expression from list
6801 * and then add it to *pwaff with the given dom.
6802 * This domain is known to be disjoint from other domains
6803 * because of the way isl_basic_map_foreach_lexmax works.
6805 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6806 __isl_take isl_aff_list
*list
, void *user
)
6808 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6810 isl_pw_aff
**pwaff
= user
;
6811 isl_pw_aff
*pwaff_i
;
6815 if (isl_aff_list_n_aff(list
) != 1)
6816 isl_die(ctx
, isl_error_internal
,
6817 "expecting single element list", goto error
);
6819 aff
= isl_aff_list_get_aff(list
, 0);
6820 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6822 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6824 isl_aff_list_free(list
);
6828 isl_basic_set_free(dom
);
6829 isl_aff_list_free(list
);
6830 return isl_stat_error
;
6833 /* Given a basic map with one output dimension, compute the minimum or
6834 * maximum of that dimension as an isl_pw_aff.
6836 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6837 * call update_dim_opt on each leaf of the result.
6839 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6842 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6846 dim
= isl_space_from_domain(isl_space_domain(dim
));
6847 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6848 pwaff
= isl_pw_aff_empty(dim
);
6850 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6852 return isl_pw_aff_free(pwaff
);
6857 /* Compute the minimum or maximum of the given output dimension
6858 * as a function of the parameters and the input dimensions,
6859 * but independently of the other output dimensions.
6861 * We first project out the other output dimension and then compute
6862 * the "lexicographic" maximum in each basic map, combining the results
6863 * using isl_pw_aff_union_max.
6865 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6872 n_out
= isl_map_dim(map
, isl_dim_out
);
6873 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6874 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6879 isl_space
*dim
= isl_map_get_space(map
);
6881 return isl_pw_aff_empty(dim
);
6884 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6885 for (i
= 1; i
< map
->n
; ++i
) {
6886 isl_pw_aff
*pwaff_i
;
6888 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6889 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6897 /* Compute the minimum 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_min(__isl_take isl_map
*map
, int pos
)
6903 return map_dim_opt(map
, pos
, 0);
6906 /* Compute the maximum of the given output dimension as a function of the
6907 * parameters and input dimensions, but independently of
6908 * the other output dimensions.
6910 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6912 return map_dim_opt(map
, pos
, 1);
6915 /* Compute the minimum or maximum of the given set dimension
6916 * as a function of the parameters,
6917 * but independently of the other set dimensions.
6919 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6922 return map_dim_opt(set
, pos
, max
);
6925 /* Compute the maximum of the given set dimension as a function of the
6926 * parameters, but independently of the other set dimensions.
6928 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6930 return set_dim_opt(set
, pos
, 1);
6933 /* Compute the minimum of the given set dimension as a function of the
6934 * parameters, but independently of the other set dimensions.
6936 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6938 return set_dim_opt(set
, pos
, 0);
6941 /* Apply a preimage specified by "mat" on the parameters of "bset".
6942 * bset is assumed to have only parameters and divs.
6944 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
6945 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
6952 bset
->dim
= isl_space_cow(bset
->dim
);
6956 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6958 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6960 bset
->dim
->nparam
= 0;
6961 bset
->dim
->n_out
= nparam
;
6962 bset
= isl_basic_set_preimage(bset
, mat
);
6964 bset
->dim
->nparam
= bset
->dim
->n_out
;
6965 bset
->dim
->n_out
= 0;
6970 isl_basic_set_free(bset
);
6974 /* Apply a preimage specified by "mat" on the parameters of "set".
6975 * set is assumed to have only parameters and divs.
6977 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6978 __isl_take isl_mat
*mat
)
6986 nparam
= isl_set_dim(set
, isl_dim_param
);
6988 if (mat
->n_row
!= 1 + nparam
)
6989 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6990 "unexpected number of rows", goto error
);
6992 space
= isl_set_get_space(set
);
6993 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6994 isl_dim_param
, 0, nparam
);
6995 set
= isl_set_reset_space(set
, space
);
6996 set
= isl_set_preimage(set
, mat
);
6997 nparam
= isl_set_dim(set
, isl_dim_out
);
6998 space
= isl_set_get_space(set
);
6999 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7000 isl_dim_out
, 0, nparam
);
7001 set
= isl_set_reset_space(set
, space
);
7009 /* Intersect the basic set "bset" with the affine space specified by the
7010 * equalities in "eq".
7012 static __isl_give isl_basic_set
*basic_set_append_equalities(
7013 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7021 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
7026 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
7027 for (i
= 0; i
< eq
->n_row
; ++i
) {
7028 k
= isl_basic_set_alloc_equality(bset
);
7031 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7032 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7036 bset
= isl_basic_set_gauss(bset
, NULL
);
7037 bset
= isl_basic_set_finalize(bset
);
7042 isl_basic_set_free(bset
);
7046 /* Intersect the set "set" with the affine space specified by the
7047 * equalities in "eq".
7049 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7057 for (i
= 0; i
< set
->n
; ++i
) {
7058 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7071 /* Given a basic set "bset" that only involves parameters and existentially
7072 * quantified variables, return the index of the first equality
7073 * that only involves parameters. If there is no such equality then
7074 * return bset->n_eq.
7076 * This function assumes that isl_basic_set_gauss has been called on "bset".
7078 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7081 unsigned nparam
, n_div
;
7086 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7087 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7089 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7090 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7097 /* Compute an explicit representation for the existentially quantified
7098 * variables in "bset" by computing the "minimal value" of the set
7099 * variables. Since there are no set variables, the computation of
7100 * the minimal value essentially computes an explicit representation
7101 * of the non-empty part(s) of "bset".
7103 * The input only involves parameters and existentially quantified variables.
7104 * All equalities among parameters have been removed.
7106 * Since the existentially quantified variables in the result are in general
7107 * going to be different from those in the input, we first replace
7108 * them by the minimal number of variables based on their equalities.
7109 * This should simplify the parametric integer programming.
7111 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7113 isl_morph
*morph1
, *morph2
;
7119 if (bset
->n_eq
== 0)
7120 return isl_basic_set_lexmin_compute_divs(bset
);
7122 morph1
= isl_basic_set_parameter_compression(bset
);
7123 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7124 bset
= isl_basic_set_lift(bset
);
7125 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7126 bset
= isl_morph_basic_set(morph2
, bset
);
7127 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7128 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7130 set
= isl_basic_set_lexmin_compute_divs(bset
);
7132 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7137 /* Project the given basic set onto its parameter domain, possibly introducing
7138 * new, explicit, existential variables in the constraints.
7139 * The input has parameters and (possibly implicit) existential variables.
7140 * The output has the same parameters, but only
7141 * explicit existentially quantified variables.
7143 * The actual projection is performed by pip, but pip doesn't seem
7144 * to like equalities very much, so we first remove the equalities
7145 * among the parameters by performing a variable compression on
7146 * the parameters. Afterward, an inverse transformation is performed
7147 * and the equalities among the parameters are inserted back in.
7149 * The variable compression on the parameters may uncover additional
7150 * equalities that were only implicit before. We therefore check
7151 * if there are any new parameter equalities in the result and
7152 * if so recurse. The removal of parameter equalities is required
7153 * for the parameter compression performed by base_compute_divs.
7155 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7159 struct isl_mat
*T
, *T2
;
7160 struct isl_set
*set
;
7163 bset
= isl_basic_set_cow(bset
);
7167 if (bset
->n_eq
== 0)
7168 return base_compute_divs(bset
);
7170 bset
= isl_basic_set_gauss(bset
, NULL
);
7173 if (isl_basic_set_plain_is_empty(bset
))
7174 return isl_set_from_basic_set(bset
);
7176 i
= first_parameter_equality(bset
);
7177 if (i
== bset
->n_eq
)
7178 return base_compute_divs(bset
);
7180 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7181 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7183 eq
= isl_mat_cow(eq
);
7184 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7185 if (T
&& T
->n_col
== 0) {
7189 bset
= isl_basic_set_set_to_empty(bset
);
7190 return isl_set_from_basic_set(bset
);
7192 bset
= basic_set_parameter_preimage(bset
, T
);
7194 i
= first_parameter_equality(bset
);
7197 else if (i
== bset
->n_eq
)
7198 set
= base_compute_divs(bset
);
7200 set
= parameter_compute_divs(bset
);
7201 set
= set_parameter_preimage(set
, T2
);
7202 set
= set_append_equalities(set
, eq
);
7206 /* Insert the divs from "ls" before those of "bmap".
7208 * The number of columns is not changed, which means that the last
7209 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7210 * The caller is responsible for removing the same number of dimensions
7211 * from the space of "bmap".
7213 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7214 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7220 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7224 old_n_div
= bmap
->n_div
;
7225 bmap
= insert_div_rows(bmap
, n_div
);
7229 for (i
= 0; i
< n_div
; ++i
) {
7230 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7231 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7237 /* Replace the space of "bmap" by the space and divs of "ls".
7239 * If "ls" has any divs, then we simplify the result since we may
7240 * have discovered some additional equalities that could simplify
7241 * the div expressions.
7243 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7244 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7248 bmap
= isl_basic_map_cow(bmap
);
7252 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7253 bmap
= insert_divs_from_local_space(bmap
, ls
);
7257 isl_space_free(bmap
->dim
);
7258 bmap
->dim
= isl_local_space_get_space(ls
);
7262 isl_local_space_free(ls
);
7264 bmap
= isl_basic_map_simplify(bmap
);
7265 bmap
= isl_basic_map_finalize(bmap
);
7268 isl_basic_map_free(bmap
);
7269 isl_local_space_free(ls
);
7273 /* Replace the space of "map" by the space and divs of "ls".
7275 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7276 __isl_take isl_local_space
*ls
)
7280 map
= isl_map_cow(map
);
7284 for (i
= 0; i
< map
->n
; ++i
) {
7285 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7286 isl_local_space_copy(ls
));
7290 isl_space_free(map
->dim
);
7291 map
->dim
= isl_local_space_get_space(ls
);
7295 isl_local_space_free(ls
);
7298 isl_local_space_free(ls
);
7303 /* Compute an explicit representation for the existentially
7304 * quantified variables for which do not know any explicit representation yet.
7306 * We first sort the existentially quantified variables so that the
7307 * existentially quantified variables for which we already have an explicit
7308 * representation are placed before those for which we do not.
7309 * The input dimensions, the output dimensions and the existentially
7310 * quantified variables for which we already have an explicit
7311 * representation are then turned into parameters.
7312 * compute_divs returns a map with the same parameters and
7313 * no input or output dimensions and the dimension specification
7314 * is reset to that of the input, including the existentially quantified
7315 * variables for which we already had an explicit representation.
7317 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7319 struct isl_basic_set
*bset
;
7320 struct isl_set
*set
;
7321 struct isl_map
*map
;
7323 isl_local_space
*ls
;
7330 bmap
= isl_basic_map_sort_divs(bmap
);
7331 bmap
= isl_basic_map_cow(bmap
);
7335 n_known
= isl_basic_map_first_unknown_div(bmap
);
7337 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7339 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7340 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7341 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7342 dim
= isl_space_set_alloc(bmap
->ctx
,
7343 nparam
+ n_in
+ n_out
+ n_known
, 0);
7347 ls
= isl_basic_map_get_local_space(bmap
);
7348 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7349 n_known
, bmap
->n_div
- n_known
);
7351 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7352 swap_div(bmap
, i
- n_known
, i
);
7353 bmap
->n_div
-= n_known
;
7354 bmap
->extra
-= n_known
;
7356 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7357 bset
= bset_from_bmap(bmap
);
7359 set
= parameter_compute_divs(bset
);
7360 map
= set_to_map(set
);
7361 map
= replace_space_by_local_space(map
, ls
);
7365 isl_basic_map_free(bmap
);
7369 /* Remove the explicit representation of local variable "div",
7372 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7373 __isl_take isl_basic_map
*bmap
, int div
)
7377 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7379 return isl_basic_map_free(bmap
);
7383 bmap
= isl_basic_map_cow(bmap
);
7386 isl_int_set_si(bmap
->div
[div
][0], 0);
7390 /* Is local variable "div" of "bmap" marked as not having an explicit
7392 * Note that even if "div" is not marked in this way and therefore
7393 * has an explicit representation, this representation may still
7394 * depend (indirectly) on other local variables that do not
7395 * have an explicit representation.
7397 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7400 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7401 return isl_bool_error
;
7402 return isl_int_is_zero(bmap
->div
[div
][0]);
7405 /* Return the position of the first local variable that does not
7406 * have an explicit representation.
7407 * Return the total number of local variables if they all have
7408 * an explicit representation.
7409 * Return -1 on error.
7411 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7418 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7419 if (!isl_basic_map_div_is_known(bmap
, i
))
7425 /* Return the position of the first local variable that does not
7426 * have an explicit representation.
7427 * Return the total number of local variables if they all have
7428 * an explicit representation.
7429 * Return -1 on error.
7431 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7433 return isl_basic_map_first_unknown_div(bset
);
7436 /* Does "bmap" have an explicit representation for all local variables?
7438 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7442 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7443 first
= isl_basic_map_first_unknown_div(bmap
);
7445 return isl_bool_error
;
7449 /* Do all basic maps in "map" have an explicit representation
7450 * for all local variables?
7452 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7457 return isl_bool_error
;
7459 for (i
= 0; i
< map
->n
; ++i
) {
7460 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7465 return isl_bool_true
;
7468 /* If bmap contains any unknown divs, then compute explicit
7469 * expressions for them. However, this computation may be
7470 * quite expensive, so first try to remove divs that aren't
7473 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7476 struct isl_map
*map
;
7478 known
= isl_basic_map_divs_known(bmap
);
7482 return isl_map_from_basic_map(bmap
);
7484 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7486 known
= isl_basic_map_divs_known(bmap
);
7490 return isl_map_from_basic_map(bmap
);
7492 map
= compute_divs(bmap
);
7495 isl_basic_map_free(bmap
);
7499 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7503 struct isl_map
*res
;
7510 known
= isl_map_divs_known(map
);
7518 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7519 for (i
= 1 ; i
< map
->n
; ++i
) {
7521 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7522 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7523 res
= isl_map_union_disjoint(res
, r2
);
7525 res
= isl_map_union(res
, r2
);
7532 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7534 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7537 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7539 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7542 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7545 struct isl_set
*set
;
7550 map
= isl_map_cow(map
);
7554 set
= set_from_map(map
);
7555 set
->dim
= isl_space_domain(set
->dim
);
7558 for (i
= 0; i
< map
->n
; ++i
) {
7559 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7563 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7564 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7571 /* Return the union of "map1" and "map2", where we assume for now that
7572 * "map1" and "map2" are disjoint. Note that the basic maps inside
7573 * "map1" or "map2" may not be disjoint from each other.
7574 * Also note that this function is also called from isl_map_union,
7575 * which takes care of handling the situation where "map1" and "map2"
7576 * may not be disjoint.
7578 * If one of the inputs is empty, we can simply return the other input.
7579 * Similarly, if one of the inputs is universal, then it is equal to the union.
7581 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7582 __isl_take isl_map
*map2
)
7586 struct isl_map
*map
= NULL
;
7592 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7593 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7594 "spaces don't match", goto error
);
7605 is_universe
= isl_map_plain_is_universe(map1
);
7606 if (is_universe
< 0)
7613 is_universe
= isl_map_plain_is_universe(map2
);
7614 if (is_universe
< 0)
7621 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7622 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7623 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7625 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7626 map1
->n
+ map2
->n
, flags
);
7629 for (i
= 0; i
< map1
->n
; ++i
) {
7630 map
= isl_map_add_basic_map(map
,
7631 isl_basic_map_copy(map1
->p
[i
]));
7635 for (i
= 0; i
< map2
->n
; ++i
) {
7636 map
= isl_map_add_basic_map(map
,
7637 isl_basic_map_copy(map2
->p
[i
]));
7651 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7652 * guaranteed to be disjoint by the caller.
7654 * Note that this functions is called from within isl_map_make_disjoint,
7655 * so we have to be careful not to touch the constraints of the inputs
7658 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7659 __isl_take isl_map
*map2
)
7661 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7664 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7665 * not be disjoint. The parameters are assumed to have been aligned.
7667 * We currently simply call map_union_disjoint, the internal operation
7668 * of which does not really depend on the inputs being disjoint.
7669 * If the result contains more than one basic map, then we clear
7670 * the disjoint flag since the result may contain basic maps from
7671 * both inputs and these are not guaranteed to be disjoint.
7673 * As a special case, if "map1" and "map2" are obviously equal,
7674 * then we simply return "map1".
7676 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7677 __isl_take isl_map
*map2
)
7684 equal
= isl_map_plain_is_equal(map1
, map2
);
7692 map1
= map_union_disjoint(map1
, map2
);
7696 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7704 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7707 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7708 __isl_take isl_map
*map2
)
7710 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7713 struct isl_set
*isl_set_union_disjoint(
7714 struct isl_set
*set1
, struct isl_set
*set2
)
7716 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7720 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7722 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7725 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7728 * "map" and "set" are assumed to be compatible and non-NULL.
7730 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7731 __isl_take isl_set
*set
,
7732 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7733 __isl_take isl_basic_set
*bset
))
7736 struct isl_map
*result
;
7739 if (isl_set_plain_is_universe(set
)) {
7744 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7745 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7746 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7748 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7749 map
->n
* set
->n
, flags
);
7750 for (i
= 0; result
&& i
< map
->n
; ++i
)
7751 for (j
= 0; j
< set
->n
; ++j
) {
7752 result
= isl_map_add_basic_map(result
,
7753 fn(isl_basic_map_copy(map
->p
[i
]),
7754 isl_basic_set_copy(set
->p
[j
])));
7764 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7765 __isl_take isl_set
*set
)
7769 ok
= isl_map_compatible_range(map
, set
);
7773 isl_die(set
->ctx
, isl_error_invalid
,
7774 "incompatible spaces", goto error
);
7776 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7783 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7784 __isl_take isl_set
*set
)
7786 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7789 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7790 __isl_take isl_set
*set
)
7794 ok
= isl_map_compatible_domain(map
, set
);
7798 isl_die(set
->ctx
, isl_error_invalid
,
7799 "incompatible spaces", goto error
);
7801 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7808 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7809 __isl_take isl_set
*set
)
7811 return isl_map_align_params_map_map_and(map
, set
,
7812 &map_intersect_domain
);
7815 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7816 __isl_take isl_map
*map2
)
7820 map1
= isl_map_reverse(map1
);
7821 map1
= isl_map_apply_range(map1
, map2
);
7822 return isl_map_reverse(map1
);
7829 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7830 __isl_take isl_map
*map2
)
7832 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7835 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7836 __isl_take isl_map
*map2
)
7838 isl_space
*dim_result
;
7839 struct isl_map
*result
;
7845 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7846 isl_space_copy(map2
->dim
));
7848 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7851 for (i
= 0; i
< map1
->n
; ++i
)
7852 for (j
= 0; j
< map2
->n
; ++j
) {
7853 result
= isl_map_add_basic_map(result
,
7854 isl_basic_map_apply_range(
7855 isl_basic_map_copy(map1
->p
[i
]),
7856 isl_basic_map_copy(map2
->p
[j
])));
7862 if (result
&& result
->n
<= 1)
7863 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7871 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7872 __isl_take isl_map
*map2
)
7874 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7878 * returns range - domain
7880 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
7882 isl_space
*target_space
;
7883 struct isl_basic_set
*bset
;
7890 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7891 bmap
->dim
, isl_dim_out
),
7893 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7894 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
7895 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7896 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7897 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7898 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7899 for (i
= 0; i
< dim
; ++i
) {
7900 int j
= isl_basic_map_alloc_equality(bmap
);
7902 bmap
= isl_basic_map_free(bmap
);
7905 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7906 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7907 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7908 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7910 bset
= isl_basic_map_domain(bmap
);
7911 bset
= isl_basic_set_reset_space(bset
, target_space
);
7914 isl_basic_map_free(bmap
);
7919 * returns range - domain
7921 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7925 struct isl_set
*result
;
7930 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7931 map
->dim
, isl_dim_out
),
7933 dim
= isl_map_get_space(map
);
7934 dim
= isl_space_domain(dim
);
7935 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7938 for (i
= 0; i
< map
->n
; ++i
)
7939 result
= isl_set_add_basic_set(result
,
7940 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7949 * returns [domain -> range] -> range - domain
7951 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7952 __isl_take isl_basic_map
*bmap
)
7956 isl_basic_map
*domain
;
7960 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7961 bmap
->dim
, isl_dim_out
))
7962 isl_die(bmap
->ctx
, isl_error_invalid
,
7963 "domain and range don't match", goto error
);
7965 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7966 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7968 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7969 domain
= isl_basic_map_universe(dim
);
7971 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7972 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7973 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7975 total
= isl_basic_map_total_dim(bmap
);
7977 for (i
= 0; i
< n
; ++i
) {
7978 k
= isl_basic_map_alloc_equality(bmap
);
7981 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7982 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7983 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7984 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7987 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7988 return isl_basic_map_finalize(bmap
);
7990 isl_basic_map_free(bmap
);
7995 * returns [domain -> range] -> range - domain
7997 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8000 isl_space
*domain_dim
;
8005 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8006 map
->dim
, isl_dim_out
))
8007 isl_die(map
->ctx
, isl_error_invalid
,
8008 "domain and range don't match", goto error
);
8010 map
= isl_map_cow(map
);
8014 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
8015 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
8016 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
8019 for (i
= 0; i
< map
->n
; ++i
) {
8020 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
8024 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8031 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
8033 struct isl_basic_map
*bmap
;
8041 nparam
= dims
->nparam
;
8043 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
8047 for (i
= 0; i
< dim
; ++i
) {
8048 int j
= isl_basic_map_alloc_equality(bmap
);
8051 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
8052 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8053 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
8055 return isl_basic_map_finalize(bmap
);
8057 isl_basic_map_free(bmap
);
8061 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
8065 if (dim
->n_in
!= dim
->n_out
)
8066 isl_die(dim
->ctx
, isl_error_invalid
,
8067 "number of input and output dimensions needs to be "
8068 "the same", goto error
);
8069 return basic_map_identity(dim
);
8071 isl_space_free(dim
);
8075 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
8077 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
8080 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8082 isl_space
*dim
= isl_set_get_space(set
);
8084 id
= isl_map_identity(isl_space_map_from_set(dim
));
8085 return isl_map_intersect_range(id
, set
);
8088 /* Construct a basic set with all set dimensions having only non-negative
8091 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8092 __isl_take isl_space
*space
)
8097 struct isl_basic_set
*bset
;
8101 nparam
= space
->nparam
;
8103 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8106 for (i
= 0; i
< dim
; ++i
) {
8107 int k
= isl_basic_set_alloc_inequality(bset
);
8110 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
8111 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8115 isl_basic_set_free(bset
);
8119 /* Construct the half-space x_pos >= 0.
8121 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
8125 isl_basic_set
*nonneg
;
8127 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8128 k
= isl_basic_set_alloc_inequality(nonneg
);
8131 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
8132 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8134 return isl_basic_set_finalize(nonneg
);
8136 isl_basic_set_free(nonneg
);
8140 /* Construct the half-space x_pos <= -1.
8142 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
8147 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
8148 k
= isl_basic_set_alloc_inequality(neg
);
8151 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
8152 isl_int_set_si(neg
->ineq
[k
][0], -1);
8153 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8155 return isl_basic_set_finalize(neg
);
8157 isl_basic_set_free(neg
);
8161 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8162 enum isl_dim_type type
, unsigned first
, unsigned n
)
8166 isl_basic_set
*nonneg
;
8174 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
8176 offset
= pos(set
->dim
, type
);
8177 for (i
= 0; i
< n
; ++i
) {
8178 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8179 offset
+ first
+ i
);
8180 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8182 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8191 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8193 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8199 return isl_stat_error
;
8200 if (isl_set_plain_is_empty(set
)) {
8205 return fn(set
, signs
, user
);
8208 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8210 half
= isl_set_intersect(half
, isl_set_copy(set
));
8211 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8215 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8217 half
= isl_set_intersect(half
, set
);
8218 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8221 return isl_stat_error
;
8224 /* Call "fn" on the intersections of "set" with each of the orthants
8225 * (except for obviously empty intersections). The orthant is identified
8226 * by the signs array, with each entry having value 1 or -1 according
8227 * to the sign of the corresponding variable.
8229 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8230 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8239 return isl_stat_error
;
8240 if (isl_set_plain_is_empty(set
))
8243 nparam
= isl_set_dim(set
, isl_dim_param
);
8244 nvar
= isl_set_dim(set
, isl_dim_set
);
8246 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8248 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8256 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8258 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8261 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8262 __isl_keep isl_basic_map
*bmap2
)
8265 struct isl_map
*map1
;
8266 struct isl_map
*map2
;
8268 if (!bmap1
|| !bmap2
)
8269 return isl_bool_error
;
8271 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8272 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8274 is_subset
= isl_map_is_subset(map1
, map2
);
8282 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8283 __isl_keep isl_basic_set
*bset2
)
8285 return isl_basic_map_is_subset(bset1
, bset2
);
8288 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8289 __isl_keep isl_basic_map
*bmap2
)
8293 if (!bmap1
|| !bmap2
)
8294 return isl_bool_error
;
8295 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8296 if (is_subset
!= isl_bool_true
)
8298 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8302 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8303 __isl_keep isl_basic_set
*bset2
)
8305 return isl_basic_map_is_equal(
8306 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8309 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8315 return isl_bool_error
;
8316 for (i
= 0; i
< map
->n
; ++i
) {
8317 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8319 return isl_bool_error
;
8321 return isl_bool_false
;
8323 return isl_bool_true
;
8326 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8328 return map
? map
->n
== 0 : isl_bool_error
;
8331 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8333 return set
? set
->n
== 0 : isl_bool_error
;
8336 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8338 return isl_map_is_empty(set_to_map(set
));
8341 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8342 __isl_keep isl_map
*map2
)
8345 return isl_bool_error
;
8347 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8350 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8351 __isl_keep isl_set
*set2
)
8354 return isl_bool_error
;
8356 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8359 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8364 return isl_bool_error
;
8365 is_subset
= isl_map_is_subset(map1
, map2
);
8366 if (is_subset
!= isl_bool_true
)
8368 is_subset
= isl_map_is_subset(map2
, map1
);
8372 /* Is "map1" equal to "map2"?
8374 * First check if they are obviously equal.
8375 * If not, then perform a more detailed analysis.
8377 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8381 equal
= isl_map_plain_is_equal(map1
, map2
);
8382 if (equal
< 0 || equal
)
8384 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8387 isl_bool
isl_basic_map_is_strict_subset(
8388 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8392 if (!bmap1
|| !bmap2
)
8393 return isl_bool_error
;
8394 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8395 if (is_subset
!= isl_bool_true
)
8397 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8398 if (is_subset
== isl_bool_error
)
8403 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8404 __isl_keep isl_map
*map2
)
8409 return isl_bool_error
;
8410 is_subset
= isl_map_is_subset(map1
, map2
);
8411 if (is_subset
!= isl_bool_true
)
8413 is_subset
= isl_map_is_subset(map2
, map1
);
8414 if (is_subset
== isl_bool_error
)
8419 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8420 __isl_keep isl_set
*set2
)
8422 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8425 /* Is "bmap" obviously equal to the universe with the same space?
8427 * That is, does it not have any constraints?
8429 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8432 return isl_bool_error
;
8433 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8436 /* Is "bset" obviously equal to the universe with the same space?
8438 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8440 return isl_basic_map_plain_is_universe(bset
);
8443 /* If "c" does not involve any existentially quantified variables,
8444 * then set *univ to false and abort
8446 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8448 isl_bool
*univ
= user
;
8451 n
= isl_constraint_dim(c
, isl_dim_div
);
8452 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8453 isl_constraint_free(c
);
8454 if (*univ
< 0 || !*univ
)
8455 return isl_stat_error
;
8459 /* Is "bmap" equal to the universe with the same space?
8461 * First check if it is obviously equal to the universe.
8462 * If not and if there are any constraints not involving
8463 * existentially quantified variables, then it is certainly
8464 * not equal to the universe.
8465 * Otherwise, check if the universe is a subset of "bmap".
8467 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8470 isl_basic_map
*test
;
8472 univ
= isl_basic_map_plain_is_universe(bmap
);
8473 if (univ
< 0 || univ
)
8475 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8476 return isl_bool_false
;
8477 univ
= isl_bool_true
;
8478 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8480 return isl_bool_error
;
8481 if (univ
< 0 || !univ
)
8483 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8484 univ
= isl_basic_map_is_subset(test
, bmap
);
8485 isl_basic_map_free(test
);
8489 /* Is "bset" equal to the universe with the same space?
8491 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8493 return isl_basic_map_is_universe(bset
);
8496 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8501 return isl_bool_error
;
8503 for (i
= 0; i
< map
->n
; ++i
) {
8504 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8509 return isl_bool_false
;
8512 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8514 return isl_map_plain_is_universe(set_to_map(set
));
8517 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8519 struct isl_basic_set
*bset
= NULL
;
8520 struct isl_vec
*sample
= NULL
;
8521 isl_bool empty
, non_empty
;
8524 return isl_bool_error
;
8526 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8527 return isl_bool_true
;
8529 if (isl_basic_map_plain_is_universe(bmap
))
8530 return isl_bool_false
;
8532 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8533 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8534 copy
= isl_basic_map_remove_redundancies(copy
);
8535 empty
= isl_basic_map_plain_is_empty(copy
);
8536 isl_basic_map_free(copy
);
8540 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8542 return isl_bool_error
;
8544 return isl_bool_false
;
8545 isl_vec_free(bmap
->sample
);
8546 bmap
->sample
= NULL
;
8547 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8549 return isl_bool_error
;
8550 sample
= isl_basic_set_sample_vec(bset
);
8552 return isl_bool_error
;
8553 empty
= sample
->size
== 0;
8554 isl_vec_free(bmap
->sample
);
8555 bmap
->sample
= sample
;
8557 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8562 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8565 return isl_bool_error
;
8566 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8569 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8572 return isl_bool_error
;
8573 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8576 /* Is "bmap" known to be non-empty?
8578 * That is, is the cached sample still valid?
8580 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8585 return isl_bool_error
;
8587 return isl_bool_false
;
8588 total
= 1 + isl_basic_map_total_dim(bmap
);
8589 if (bmap
->sample
->size
!= total
)
8590 return isl_bool_false
;
8591 return isl_basic_map_contains(bmap
, bmap
->sample
);
8594 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8596 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8599 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
8600 __isl_take isl_basic_map
*bmap2
)
8602 struct isl_map
*map
;
8603 if (!bmap1
|| !bmap2
)
8606 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8608 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8611 map
= isl_map_add_basic_map(map
, bmap1
);
8612 map
= isl_map_add_basic_map(map
, bmap2
);
8615 isl_basic_map_free(bmap1
);
8616 isl_basic_map_free(bmap2
);
8620 struct isl_set
*isl_basic_set_union(
8621 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8623 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8624 bset_to_bmap(bset2
)));
8627 /* Order divs such that any div only depends on previous divs */
8628 __isl_give isl_basic_map
*isl_basic_map_order_divs(
8629 __isl_take isl_basic_map
*bmap
)
8637 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8639 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8641 if (isl_int_is_zero(bmap
->div
[i
][0]))
8643 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8648 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8649 "integer division depends on itself",
8650 return isl_basic_map_free(bmap
));
8651 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8657 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8659 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8662 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8669 for (i
= 0; i
< map
->n
; ++i
) {
8670 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8681 /* Sort the local variables of "bset".
8683 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8684 __isl_take isl_basic_set
*bset
)
8686 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8689 /* Apply the expansion computed by isl_merge_divs.
8690 * The expansion itself is given by "exp" while the resulting
8691 * list of divs is given by "div".
8693 * Move the integer divisions of "bmap" into the right position
8694 * according to "exp" and then introduce the additional integer
8695 * divisions, adding div constraints.
8696 * The moving should be done first to avoid moving coefficients
8697 * in the definitions of the extra integer divisions.
8699 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8700 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8705 bmap
= isl_basic_map_cow(bmap
);
8709 if (div
->n_row
< bmap
->n_div
)
8710 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8711 "not an expansion", goto error
);
8713 n_div
= bmap
->n_div
;
8714 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8715 div
->n_row
- n_div
, 0,
8716 2 * (div
->n_row
- n_div
));
8718 for (i
= n_div
; i
< div
->n_row
; ++i
)
8719 if (isl_basic_map_alloc_div(bmap
) < 0)
8722 for (j
= n_div
- 1; j
>= 0; --j
) {
8725 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8728 for (i
= 0; i
< div
->n_row
; ++i
) {
8729 if (j
< n_div
&& exp
[j
] == i
) {
8732 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8733 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8735 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8743 isl_basic_map_free(bmap
);
8748 /* Apply the expansion computed by isl_merge_divs.
8749 * The expansion itself is given by "exp" while the resulting
8750 * list of divs is given by "div".
8752 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8753 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8755 return isl_basic_map_expand_divs(bset
, div
, exp
);
8758 /* Look for a div in dst that corresponds to the div "div" in src.
8759 * The divs before "div" in src and dst are assumed to be the same.
8761 * Returns -1 if no corresponding div was found and the position
8762 * of the corresponding div in dst otherwise.
8764 static int find_div(__isl_keep isl_basic_map
*dst
,
8765 __isl_keep isl_basic_map
*src
, unsigned div
)
8769 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8771 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8772 for (i
= div
; i
< dst
->n_div
; ++i
)
8773 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8774 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8775 dst
->n_div
- div
) == -1)
8780 /* Align the divs of "dst" to those of "src", adding divs from "src"
8781 * if needed. That is, make sure that the first src->n_div divs
8782 * of the result are equal to those of src.
8784 * The result is not finalized as by design it will have redundant
8785 * divs if any divs from "src" were copied.
8787 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8788 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8791 int known
, extended
;
8795 return isl_basic_map_free(dst
);
8797 if (src
->n_div
== 0)
8800 known
= isl_basic_map_divs_known(src
);
8802 return isl_basic_map_free(dst
);
8804 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8805 "some src divs are unknown",
8806 return isl_basic_map_free(dst
));
8808 src
= isl_basic_map_order_divs(src
);
8811 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8812 for (i
= 0; i
< src
->n_div
; ++i
) {
8813 int j
= find_div(dst
, src
, i
);
8816 int extra
= src
->n_div
- i
;
8817 dst
= isl_basic_map_cow(dst
);
8820 dst
= isl_basic_map_extend_space(dst
,
8821 isl_space_copy(dst
->dim
),
8822 extra
, 0, 2 * extra
);
8825 j
= isl_basic_map_alloc_div(dst
);
8827 return isl_basic_map_free(dst
);
8828 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8829 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8830 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8831 return isl_basic_map_free(dst
);
8834 isl_basic_map_swap_div(dst
, i
, j
);
8839 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
8847 map
= isl_map_compute_divs(map
);
8848 map
= isl_map_cow(map
);
8852 for (i
= 1; i
< map
->n
; ++i
)
8853 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8854 for (i
= 1; i
< map
->n
; ++i
) {
8855 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8857 return isl_map_free(map
);
8860 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8864 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
8866 return isl_map_align_divs_internal(map
);
8869 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8871 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
8874 /* Align the divs of the basic maps in "map" to those
8875 * of the basic maps in "list", as well as to the other basic maps in "map".
8876 * The elements in "list" are assumed to have known divs.
8878 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8879 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8883 map
= isl_map_compute_divs(map
);
8884 map
= isl_map_cow(map
);
8886 return isl_map_free(map
);
8890 n
= isl_basic_map_list_n_basic_map(list
);
8891 for (i
= 0; i
< n
; ++i
) {
8892 isl_basic_map
*bmap
;
8894 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8895 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8896 isl_basic_map_free(bmap
);
8899 return isl_map_free(map
);
8901 return isl_map_align_divs_internal(map
);
8904 /* Align the divs of each element of "list" to those of "bmap".
8905 * Both "bmap" and the elements of "list" are assumed to have known divs.
8907 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8908 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8913 return isl_basic_map_list_free(list
);
8915 n
= isl_basic_map_list_n_basic_map(list
);
8916 for (i
= 0; i
< n
; ++i
) {
8917 isl_basic_map
*bmap_i
;
8919 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8920 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8921 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8927 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8928 __isl_take isl_map
*map
)
8932 ok
= isl_map_compatible_domain(map
, set
);
8936 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8937 "incompatible spaces", goto error
);
8938 map
= isl_map_intersect_domain(map
, set
);
8939 set
= isl_map_range(map
);
8947 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8948 __isl_take isl_map
*map
)
8950 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8953 /* There is no need to cow as removing empty parts doesn't change
8954 * the meaning of the set.
8956 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
8963 for (i
= map
->n
- 1; i
>= 0; --i
)
8964 remove_if_empty(map
, i
);
8969 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8971 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8974 /* Given two basic sets bset1 and bset2, compute the maximal difference
8975 * between the values of dimension pos in bset1 and those in bset2
8976 * for any common value of the parameters and dimensions preceding pos.
8978 static enum isl_lp_result
basic_set_maximal_difference_at(
8979 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8980 int pos
, isl_int
*opt
)
8982 isl_basic_map
*bmap1
;
8983 isl_basic_map
*bmap2
;
8984 struct isl_ctx
*ctx
;
8985 struct isl_vec
*obj
;
8989 enum isl_lp_result res
;
8991 if (!bset1
|| !bset2
)
8992 return isl_lp_error
;
8994 nparam
= isl_basic_set_n_param(bset1
);
8995 dim1
= isl_basic_set_n_dim(bset1
);
8997 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8998 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8999 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9000 isl_dim_out
, 0, pos
);
9001 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9002 isl_dim_out
, 0, pos
);
9003 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
9005 return isl_lp_error
;
9007 total
= isl_basic_map_total_dim(bmap1
);
9009 obj
= isl_vec_alloc(ctx
, 1 + total
);
9012 isl_seq_clr(obj
->block
.data
, 1 + total
);
9013 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9014 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9015 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9017 isl_basic_map_free(bmap1
);
9021 isl_basic_map_free(bmap1
);
9022 return isl_lp_error
;
9025 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9026 * for any common value of the parameters and dimensions preceding pos
9027 * in both basic sets, the values of dimension pos in bset1 are
9028 * smaller or larger than those in bset2.
9031 * 1 if bset1 follows bset2
9032 * -1 if bset1 precedes bset2
9033 * 0 if bset1 and bset2 are incomparable
9034 * -2 if some error occurred.
9036 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
9037 struct isl_basic_set
*bset2
, int pos
)
9040 enum isl_lp_result res
;
9045 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9047 if (res
== isl_lp_empty
)
9049 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9050 res
== isl_lp_unbounded
)
9052 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9061 /* Given two basic sets bset1 and bset2, check whether
9062 * for any common value of the parameters and dimensions preceding pos
9063 * there is a value of dimension pos in bset1 that is larger
9064 * than a value of the same dimension in bset2.
9067 * 1 if there exists such a pair
9068 * 0 if there is no such pair, but there is a pair of equal values
9070 * -2 if some error occurred.
9072 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9073 __isl_keep isl_basic_set
*bset2
, int pos
)
9076 enum isl_lp_result res
;
9081 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9083 if (res
== isl_lp_empty
)
9085 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9086 res
== isl_lp_unbounded
)
9088 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9090 else if (res
== isl_lp_ok
)
9099 /* Given two sets set1 and set2, check whether
9100 * for any common value of the parameters and dimensions preceding pos
9101 * there is a value of dimension pos in set1 that is larger
9102 * than a value of the same dimension in set2.
9105 * 1 if there exists such a pair
9106 * 0 if there is no such pair, but there is a pair of equal values
9108 * -2 if some error occurred.
9110 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9111 __isl_keep isl_set
*set2
, int pos
)
9119 for (i
= 0; i
< set1
->n
; ++i
)
9120 for (j
= 0; j
< set2
->n
; ++j
) {
9122 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9123 if (f
== 1 || f
== -2)
9132 static isl_bool
isl_basic_map_plain_has_fixed_var(
9133 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9140 return isl_bool_error
;
9141 total
= isl_basic_map_total_dim(bmap
);
9142 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9143 for (; d
+1 > pos
; --d
)
9144 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9148 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9149 return isl_bool_false
;
9150 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9151 return isl_bool_false
;
9152 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9153 return isl_bool_false
;
9155 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9156 return isl_bool_true
;
9158 return isl_bool_false
;
9161 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9162 unsigned pos
, isl_int
*val
)
9170 return isl_bool_error
;
9172 return isl_bool_false
;
9174 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9177 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9178 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9179 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9180 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9181 fixed
= isl_bool_false
;
9184 isl_int_set(*val
, v
);
9190 static isl_bool
isl_basic_set_plain_has_fixed_var(
9191 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9193 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9197 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9198 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9200 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9201 return isl_bool_error
;
9202 return isl_basic_map_plain_has_fixed_var(bmap
,
9203 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9206 /* If "bmap" obviously lies on a hyperplane where the given dimension
9207 * has a fixed value, then return that value.
9208 * Otherwise return NaN.
9210 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9211 __isl_keep isl_basic_map
*bmap
,
9212 enum isl_dim_type type
, unsigned pos
)
9220 ctx
= isl_basic_map_get_ctx(bmap
);
9221 v
= isl_val_alloc(ctx
);
9224 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9226 return isl_val_free(v
);
9228 isl_int_set_si(v
->d
, 1);
9232 return isl_val_nan(ctx
);
9235 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9236 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9238 if (pos
>= isl_map_dim(map
, type
))
9239 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9240 "position out of bounds", return isl_bool_error
);
9241 return isl_map_plain_has_fixed_var(map
,
9242 map_offset(map
, type
) - 1 + pos
, val
);
9245 /* If "map" obviously lies on a hyperplane where the given dimension
9246 * has a fixed value, then return that value.
9247 * Otherwise return NaN.
9249 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9250 enum isl_dim_type type
, unsigned pos
)
9258 ctx
= isl_map_get_ctx(map
);
9259 v
= isl_val_alloc(ctx
);
9262 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9264 return isl_val_free(v
);
9266 isl_int_set_si(v
->d
, 1);
9270 return isl_val_nan(ctx
);
9273 /* If "set" obviously lies on a hyperplane where the given dimension
9274 * has a fixed value, then return that value.
9275 * Otherwise return NaN.
9277 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9278 enum isl_dim_type type
, unsigned pos
)
9280 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9283 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9284 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9286 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9289 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9290 * then return this fixed value in *val.
9292 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9293 unsigned dim
, isl_int
*val
)
9295 return isl_basic_set_plain_has_fixed_var(bset
,
9296 isl_basic_set_n_param(bset
) + dim
, val
);
9299 /* Return -1 if the constraint "c1" should be sorted before "c2"
9300 * and 1 if it should be sorted after "c2".
9301 * Return 0 if the two constraints are the same (up to the constant term).
9303 * In particular, if a constraint involves later variables than another
9304 * then it is sorted after this other constraint.
9305 * uset_gist depends on constraints without existentially quantified
9306 * variables sorting first.
9308 * For constraints that have the same latest variable, those
9309 * with the same coefficient for this latest variable (first in absolute value
9310 * and then in actual value) are grouped together.
9311 * This is useful for detecting pairs of constraints that can
9312 * be chained in their printed representation.
9314 * Finally, within a group, constraints are sorted according to
9315 * their coefficients (excluding the constant term).
9317 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9319 isl_int
**c1
= (isl_int
**) p1
;
9320 isl_int
**c2
= (isl_int
**) p2
;
9322 unsigned size
= *(unsigned *) arg
;
9325 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9326 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9331 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9334 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9338 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9341 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9342 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9343 * and 0 if the two constraints are the same (up to the constant term).
9345 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9346 isl_int
*c1
, isl_int
*c2
)
9352 total
= isl_basic_map_total_dim(bmap
);
9353 return sort_constraint_cmp(&c1
, &c2
, &total
);
9356 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9357 __isl_take isl_basic_map
*bmap
)
9363 if (bmap
->n_ineq
== 0)
9365 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9367 total
= isl_basic_map_total_dim(bmap
);
9368 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9369 &sort_constraint_cmp
, &total
) < 0)
9370 return isl_basic_map_free(bmap
);
9374 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9375 __isl_take isl_basic_set
*bset
)
9377 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9378 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9381 __isl_give isl_basic_map
*isl_basic_map_normalize(
9382 __isl_take isl_basic_map
*bmap
)
9386 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9388 bmap
= isl_basic_map_remove_redundancies(bmap
);
9389 bmap
= isl_basic_map_sort_constraints(bmap
);
9391 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9394 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9395 __isl_keep isl_basic_map
*bmap2
)
9399 isl_space
*space1
, *space2
;
9401 if (!bmap1
|| !bmap2
)
9406 space1
= isl_basic_map_peek_space(bmap1
);
9407 space2
= isl_basic_map_peek_space(bmap2
);
9408 cmp
= isl_space_cmp(space1
, space2
);
9411 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9412 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9413 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9414 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9415 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9417 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9419 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9421 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9422 return bmap1
->n_eq
- bmap2
->n_eq
;
9423 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9424 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9425 if (bmap1
->n_div
!= bmap2
->n_div
)
9426 return bmap1
->n_div
- bmap2
->n_div
;
9427 total
= isl_basic_map_total_dim(bmap1
);
9428 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9429 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9433 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9434 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9438 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9439 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9446 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9447 __isl_keep isl_basic_set
*bset2
)
9449 return isl_basic_map_plain_cmp(bset1
, bset2
);
9452 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9458 if (set1
->n
!= set2
->n
)
9459 return set1
->n
- set2
->n
;
9461 for (i
= 0; i
< set1
->n
; ++i
) {
9462 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9470 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9471 __isl_keep isl_basic_map
*bmap2
)
9473 if (!bmap1
|| !bmap2
)
9474 return isl_bool_error
;
9475 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9478 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9479 __isl_keep isl_basic_set
*bset2
)
9481 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9482 bset_to_bmap(bset2
));
9485 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9487 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9488 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9490 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9493 /* Sort the basic maps of "map" and remove duplicate basic maps.
9495 * While removing basic maps, we make sure that the basic maps remain
9496 * sorted because isl_map_normalize expects the basic maps of the result
9499 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9503 map
= isl_map_remove_empty_parts(map
);
9506 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9507 for (i
= map
->n
- 1; i
>= 1; --i
) {
9508 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9510 isl_basic_map_free(map
->p
[i
-1]);
9511 for (j
= i
; j
< map
->n
; ++j
)
9512 map
->p
[j
- 1] = map
->p
[j
];
9519 /* Remove obvious duplicates among the basic maps of "map".
9521 * Unlike isl_map_normalize, this function does not remove redundant
9522 * constraints and only removes duplicates that have exactly the same
9523 * constraints in the input. It does sort the constraints and
9524 * the basic maps to ease the detection of duplicates.
9526 * If "map" has already been normalized or if the basic maps are
9527 * disjoint, then there can be no duplicates.
9529 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9532 isl_basic_map
*bmap
;
9538 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9540 for (i
= 0; i
< map
->n
; ++i
) {
9541 bmap
= isl_basic_map_copy(map
->p
[i
]);
9542 bmap
= isl_basic_map_sort_constraints(bmap
);
9544 return isl_map_free(map
);
9545 isl_basic_map_free(map
->p
[i
]);
9549 map
= sort_and_remove_duplicates(map
);
9553 /* We normalize in place, but if anything goes wrong we need
9554 * to return NULL, so we need to make sure we don't change the
9555 * meaning of any possible other copies of map.
9557 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9560 struct isl_basic_map
*bmap
;
9564 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9566 for (i
= 0; i
< map
->n
; ++i
) {
9567 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9570 isl_basic_map_free(map
->p
[i
]);
9574 map
= sort_and_remove_duplicates(map
);
9576 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9583 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9585 return set_from_map(isl_map_normalize(set_to_map(set
)));
9588 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9589 __isl_keep isl_map
*map2
)
9595 return isl_bool_error
;
9598 return isl_bool_true
;
9599 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9600 return isl_bool_false
;
9602 map1
= isl_map_copy(map1
);
9603 map2
= isl_map_copy(map2
);
9604 map1
= isl_map_normalize(map1
);
9605 map2
= isl_map_normalize(map2
);
9608 equal
= map1
->n
== map2
->n
;
9609 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9610 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9620 return isl_bool_error
;
9623 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9624 __isl_keep isl_set
*set2
)
9626 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9629 /* Return the basic maps in "map" as a list.
9631 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9632 __isl_keep isl_map
*map
)
9636 isl_basic_map_list
*list
;
9640 ctx
= isl_map_get_ctx(map
);
9641 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9643 for (i
= 0; i
< map
->n
; ++i
) {
9644 isl_basic_map
*bmap
;
9646 bmap
= isl_basic_map_copy(map
->p
[i
]);
9647 list
= isl_basic_map_list_add(list
, bmap
);
9653 /* Return the intersection of the elements in the non-empty list "list".
9654 * All elements are assumed to live in the same space.
9656 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9657 __isl_take isl_basic_map_list
*list
)
9660 isl_basic_map
*bmap
;
9664 n
= isl_basic_map_list_n_basic_map(list
);
9666 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9667 "expecting non-empty list", goto error
);
9669 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9670 for (i
= 1; i
< n
; ++i
) {
9671 isl_basic_map
*bmap_i
;
9673 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9674 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9677 isl_basic_map_list_free(list
);
9680 isl_basic_map_list_free(list
);
9684 /* Return the intersection of the elements in the non-empty list "list".
9685 * All elements are assumed to live in the same space.
9687 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9688 __isl_take isl_basic_set_list
*list
)
9690 return isl_basic_map_list_intersect(list
);
9693 /* Return the union of the elements of "list".
9694 * The list is required to have at least one element.
9696 __isl_give isl_set
*isl_basic_set_list_union(
9697 __isl_take isl_basic_set_list
*list
)
9701 isl_basic_set
*bset
;
9706 n
= isl_basic_set_list_n_basic_set(list
);
9708 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9709 "expecting non-empty list", goto error
);
9711 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9712 space
= isl_basic_set_get_space(bset
);
9713 isl_basic_set_free(bset
);
9715 set
= isl_set_alloc_space(space
, n
, 0);
9716 for (i
= 0; i
< n
; ++i
) {
9717 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9718 set
= isl_set_add_basic_set(set
, bset
);
9721 isl_basic_set_list_free(list
);
9724 isl_basic_set_list_free(list
);
9728 /* Return the union of the elements in the non-empty list "list".
9729 * All elements are assumed to live in the same space.
9731 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9738 n
= isl_set_list_n_set(list
);
9740 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9741 "expecting non-empty list", goto error
);
9743 set
= isl_set_list_get_set(list
, 0);
9744 for (i
= 1; i
< n
; ++i
) {
9747 set_i
= isl_set_list_get_set(list
, i
);
9748 set
= isl_set_union(set
, set_i
);
9751 isl_set_list_free(list
);
9754 isl_set_list_free(list
);
9758 __isl_give isl_basic_map
*isl_basic_map_product(
9759 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9761 isl_space
*dim_result
= NULL
;
9762 struct isl_basic_map
*bmap
;
9763 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9764 struct isl_dim_map
*dim_map1
, *dim_map2
;
9766 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9768 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9769 isl_space_copy(bmap2
->dim
));
9771 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9772 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9773 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9774 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9775 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9777 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9778 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9779 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9780 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9781 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9782 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9783 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9784 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9785 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9786 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9787 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9789 bmap
= isl_basic_map_alloc_space(dim_result
,
9790 bmap1
->n_div
+ bmap2
->n_div
,
9791 bmap1
->n_eq
+ bmap2
->n_eq
,
9792 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9793 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9794 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9795 bmap
= isl_basic_map_simplify(bmap
);
9796 return isl_basic_map_finalize(bmap
);
9798 isl_basic_map_free(bmap1
);
9799 isl_basic_map_free(bmap2
);
9803 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9804 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9806 isl_basic_map
*prod
;
9808 prod
= isl_basic_map_product(bmap1
, bmap2
);
9809 prod
= isl_basic_map_flatten(prod
);
9813 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9814 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9816 return isl_basic_map_flat_range_product(bset1
, bset2
);
9819 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9820 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9822 isl_space
*space_result
= NULL
;
9823 isl_basic_map
*bmap
;
9824 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9825 struct isl_dim_map
*dim_map1
, *dim_map2
;
9827 if (!bmap1
|| !bmap2
)
9830 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9831 isl_space_copy(bmap2
->dim
));
9833 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9834 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9835 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9836 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9838 total
= nparam
+ in1
+ in2
+ out
+ 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
);
9847 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
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_range_product(
9865 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9868 isl_space
*dim_result
= NULL
;
9869 isl_basic_map
*bmap
;
9870 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9871 struct isl_dim_map
*dim_map1
, *dim_map2
;
9873 rational
= isl_basic_map_is_rational(bmap1
);
9874 if (rational
>= 0 && rational
)
9875 rational
= isl_basic_map_is_rational(bmap2
);
9876 if (!bmap1
|| !bmap2
|| rational
< 0)
9879 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
9882 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9883 isl_space_copy(bmap2
->dim
));
9885 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9886 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9887 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
9888 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9890 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9891 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9892 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9895 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9896 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9897 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9898 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9899 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9900 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9902 bmap
= isl_basic_map_alloc_space(dim_result
,
9903 bmap1
->n_div
+ bmap2
->n_div
,
9904 bmap1
->n_eq
+ bmap2
->n_eq
,
9905 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9906 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9907 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9909 bmap
= isl_basic_map_set_rational(bmap
);
9910 bmap
= isl_basic_map_simplify(bmap
);
9911 return isl_basic_map_finalize(bmap
);
9913 isl_basic_map_free(bmap1
);
9914 isl_basic_map_free(bmap2
);
9918 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9919 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9921 isl_basic_map
*prod
;
9923 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9924 prod
= isl_basic_map_flatten_range(prod
);
9928 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9929 * and collect the results.
9930 * The result live in the space obtained by calling "space_product"
9931 * on the spaces of "map1" and "map2".
9932 * If "remove_duplicates" is set then the result may contain duplicates
9933 * (even if the inputs do not) and so we try and remove the obvious
9936 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9937 __isl_take isl_map
*map2
,
9938 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9939 __isl_take isl_space
*right
),
9940 __isl_give isl_basic_map
*(*basic_map_product
)(
9941 __isl_take isl_basic_map
*left
,
9942 __isl_take isl_basic_map
*right
),
9943 int remove_duplicates
)
9946 struct isl_map
*result
;
9950 m
= isl_map_has_equal_params(map1
, map2
);
9954 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
9955 "parameters don't match", goto error
);
9957 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9958 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9959 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9961 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9962 isl_space_copy(map2
->dim
)),
9963 map1
->n
* map2
->n
, flags
);
9966 for (i
= 0; i
< map1
->n
; ++i
)
9967 for (j
= 0; j
< map2
->n
; ++j
) {
9968 struct isl_basic_map
*part
;
9969 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9970 isl_basic_map_copy(map2
->p
[j
]));
9971 if (isl_basic_map_is_empty(part
))
9972 isl_basic_map_free(part
);
9974 result
= isl_map_add_basic_map(result
, part
);
9978 if (remove_duplicates
)
9979 result
= isl_map_remove_obvious_duplicates(result
);
9989 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9991 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9992 __isl_take isl_map
*map2
)
9994 return map_product(map1
, map2
, &isl_space_product
,
9995 &isl_basic_map_product
, 0);
9998 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9999 __isl_take isl_map
*map2
)
10001 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10004 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10006 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10007 __isl_take isl_map
*map2
)
10011 prod
= isl_map_product(map1
, map2
);
10012 prod
= isl_map_flatten(prod
);
10016 /* Given two set A and B, construct its Cartesian product A x B.
10018 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10020 return isl_map_range_product(set1
, set2
);
10023 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10024 __isl_take isl_set
*set2
)
10026 return isl_map_flat_range_product(set1
, set2
);
10029 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10031 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10032 __isl_take isl_map
*map2
)
10034 return map_product(map1
, map2
, &isl_space_domain_product
,
10035 &isl_basic_map_domain_product
, 1);
10038 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10040 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10041 __isl_take isl_map
*map2
)
10043 return map_product(map1
, map2
, &isl_space_range_product
,
10044 &isl_basic_map_range_product
, 1);
10047 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10048 __isl_take isl_map
*map2
)
10050 return isl_map_align_params_map_map_and(map1
, map2
,
10051 &map_domain_product_aligned
);
10054 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10055 __isl_take isl_map
*map2
)
10057 return isl_map_align_params_map_map_and(map1
, map2
,
10058 &map_range_product_aligned
);
10061 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10063 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10066 int total1
, keep1
, total2
, keep2
;
10070 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10071 !isl_space_range_is_wrapping(map
->dim
))
10072 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10073 "not a product", return isl_map_free(map
));
10075 space
= isl_map_get_space(map
);
10076 total1
= isl_space_dim(space
, isl_dim_in
);
10077 total2
= isl_space_dim(space
, isl_dim_out
);
10078 space
= isl_space_factor_domain(space
);
10079 keep1
= isl_space_dim(space
, isl_dim_in
);
10080 keep2
= isl_space_dim(space
, isl_dim_out
);
10081 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10082 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10083 map
= isl_map_reset_space(map
, space
);
10088 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10090 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10093 int total1
, keep1
, total2
, keep2
;
10097 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10098 !isl_space_range_is_wrapping(map
->dim
))
10099 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10100 "not a product", return isl_map_free(map
));
10102 space
= isl_map_get_space(map
);
10103 total1
= isl_space_dim(space
, isl_dim_in
);
10104 total2
= isl_space_dim(space
, isl_dim_out
);
10105 space
= isl_space_factor_range(space
);
10106 keep1
= isl_space_dim(space
, isl_dim_in
);
10107 keep2
= isl_space_dim(space
, isl_dim_out
);
10108 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10109 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10110 map
= isl_map_reset_space(map
, space
);
10115 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10117 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10124 if (!isl_space_domain_is_wrapping(map
->dim
))
10125 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10126 "domain is not a product", return isl_map_free(map
));
10128 space
= isl_map_get_space(map
);
10129 total
= isl_space_dim(space
, isl_dim_in
);
10130 space
= isl_space_domain_factor_domain(space
);
10131 keep
= isl_space_dim(space
, isl_dim_in
);
10132 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10133 map
= isl_map_reset_space(map
, space
);
10138 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10140 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10147 if (!isl_space_domain_is_wrapping(map
->dim
))
10148 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10149 "domain is not a product", return isl_map_free(map
));
10151 space
= isl_map_get_space(map
);
10152 total
= isl_space_dim(space
, isl_dim_in
);
10153 space
= isl_space_domain_factor_range(space
);
10154 keep
= isl_space_dim(space
, isl_dim_in
);
10155 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10156 map
= isl_map_reset_space(map
, space
);
10161 /* Given a map A -> [B -> C], extract the map A -> B.
10163 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10170 if (!isl_space_range_is_wrapping(map
->dim
))
10171 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10172 "range is not a product", return isl_map_free(map
));
10174 space
= isl_map_get_space(map
);
10175 total
= isl_space_dim(space
, isl_dim_out
);
10176 space
= isl_space_range_factor_domain(space
);
10177 keep
= isl_space_dim(space
, isl_dim_out
);
10178 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10179 map
= isl_map_reset_space(map
, space
);
10184 /* Given a map A -> [B -> C], extract the map A -> C.
10186 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10193 if (!isl_space_range_is_wrapping(map
->dim
))
10194 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10195 "range is not a product", return isl_map_free(map
));
10197 space
= isl_map_get_space(map
);
10198 total
= isl_space_dim(space
, isl_dim_out
);
10199 space
= isl_space_range_factor_range(space
);
10200 keep
= isl_space_dim(space
, isl_dim_out
);
10201 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10202 map
= isl_map_reset_space(map
, space
);
10207 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10209 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10210 __isl_take isl_map
*map2
)
10214 prod
= isl_map_domain_product(map1
, map2
);
10215 prod
= isl_map_flatten_domain(prod
);
10219 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10221 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10222 __isl_take isl_map
*map2
)
10226 prod
= isl_map_range_product(map1
, map2
);
10227 prod
= isl_map_flatten_range(prod
);
10231 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10234 uint32_t hash
= isl_hash_init();
10239 bmap
= isl_basic_map_copy(bmap
);
10240 bmap
= isl_basic_map_normalize(bmap
);
10243 total
= isl_basic_map_total_dim(bmap
);
10244 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10245 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10247 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10248 isl_hash_hash(hash
, c_hash
);
10250 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10251 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10253 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10254 isl_hash_hash(hash
, c_hash
);
10256 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10257 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10259 if (isl_int_is_zero(bmap
->div
[i
][0]))
10261 isl_hash_byte(hash
, i
& 0xFF);
10262 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10263 isl_hash_hash(hash
, c_hash
);
10265 isl_basic_map_free(bmap
);
10269 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10271 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10274 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10281 map
= isl_map_copy(map
);
10282 map
= isl_map_normalize(map
);
10286 hash
= isl_hash_init();
10287 for (i
= 0; i
< map
->n
; ++i
) {
10288 uint32_t bmap_hash
;
10289 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10290 isl_hash_hash(hash
, bmap_hash
);
10298 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10300 return isl_map_get_hash(set_to_map(set
));
10303 /* Return the number of basic maps in the (current) representation of "map".
10305 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10307 return map
? map
->n
: 0;
10310 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10312 return set
? set
->n
: 0;
10315 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10316 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10321 return isl_stat_error
;
10323 for (i
= 0; i
< map
->n
; ++i
)
10324 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10325 return isl_stat_error
;
10327 return isl_stat_ok
;
10330 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10331 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10336 return isl_stat_error
;
10338 for (i
= 0; i
< set
->n
; ++i
)
10339 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10340 return isl_stat_error
;
10342 return isl_stat_ok
;
10345 /* Return a list of basic sets, the union of which is equal to "set".
10347 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10348 __isl_keep isl_set
*set
)
10351 isl_basic_set_list
*list
;
10356 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10357 for (i
= 0; i
< set
->n
; ++i
) {
10358 isl_basic_set
*bset
;
10360 bset
= isl_basic_set_copy(set
->p
[i
]);
10361 list
= isl_basic_set_list_add(list
, bset
);
10367 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10374 bset
= isl_basic_set_cow(bset
);
10378 dim
= isl_basic_set_get_space(bset
);
10379 dim
= isl_space_lift(dim
, bset
->n_div
);
10382 isl_space_free(bset
->dim
);
10384 bset
->extra
-= bset
->n_div
;
10387 bset
= isl_basic_set_finalize(bset
);
10391 isl_basic_set_free(bset
);
10395 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10401 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
10406 set
= isl_set_cow(set
);
10410 n_div
= set
->p
[0]->n_div
;
10411 dim
= isl_set_get_space(set
);
10412 dim
= isl_space_lift(dim
, n_div
);
10415 isl_space_free(set
->dim
);
10418 for (i
= 0; i
< set
->n
; ++i
) {
10419 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10430 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10438 dim
= isl_basic_set_total_dim(bset
);
10439 size
+= bset
->n_eq
* (1 + dim
);
10440 size
+= bset
->n_ineq
* (1 + dim
);
10441 size
+= bset
->n_div
* (2 + dim
);
10446 int isl_set_size(__isl_keep isl_set
*set
)
10454 for (i
= 0; i
< set
->n
; ++i
)
10455 size
+= isl_basic_set_size(set
->p
[i
]);
10460 /* Check if there is any lower bound (if lower == 0) and/or upper
10461 * bound (if upper == 0) on the specified dim.
10463 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10464 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10468 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10469 return isl_bool_error
;
10471 pos
+= isl_basic_map_offset(bmap
, type
);
10473 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10474 if (isl_int_is_zero(bmap
->div
[i
][0]))
10476 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10477 return isl_bool_true
;
10480 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10481 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10482 return isl_bool_true
;
10484 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10485 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10492 return lower
&& upper
;
10495 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10496 enum isl_dim_type type
, unsigned pos
)
10498 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10501 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10502 enum isl_dim_type type
, unsigned pos
)
10504 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10507 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10508 enum isl_dim_type type
, unsigned pos
)
10510 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10513 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10514 enum isl_dim_type type
, unsigned pos
)
10519 return isl_bool_error
;
10521 for (i
= 0; i
< map
->n
; ++i
) {
10523 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10524 if (bounded
< 0 || !bounded
)
10528 return isl_bool_true
;
10531 /* Return true if the specified dim is involved in both an upper bound
10532 * and a lower bound.
10534 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10535 enum isl_dim_type type
, unsigned pos
)
10537 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10540 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10542 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10543 enum isl_dim_type type
, unsigned pos
,
10544 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10545 enum isl_dim_type type
, unsigned pos
))
10550 return isl_bool_error
;
10552 for (i
= 0; i
< map
->n
; ++i
) {
10554 bounded
= fn(map
->p
[i
], type
, pos
);
10555 if (bounded
< 0 || bounded
)
10559 return isl_bool_false
;
10562 /* Return 1 if the specified dim is involved in any lower bound.
10564 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10565 enum isl_dim_type type
, unsigned pos
)
10567 return has_any_bound(set
, type
, pos
,
10568 &isl_basic_map_dim_has_lower_bound
);
10571 /* Return 1 if the specified dim is involved in any upper bound.
10573 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10574 enum isl_dim_type type
, unsigned pos
)
10576 return has_any_bound(set
, type
, pos
,
10577 &isl_basic_map_dim_has_upper_bound
);
10580 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10582 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10583 enum isl_dim_type type
, unsigned pos
,
10584 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10585 enum isl_dim_type type
, unsigned pos
))
10590 return isl_bool_error
;
10592 for (i
= 0; i
< map
->n
; ++i
) {
10594 bounded
= fn(map
->p
[i
], type
, pos
);
10595 if (bounded
< 0 || !bounded
)
10599 return isl_bool_true
;
10602 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10604 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10605 enum isl_dim_type type
, unsigned pos
)
10607 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10610 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10612 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10613 enum isl_dim_type type
, unsigned pos
)
10615 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10618 /* For each of the "n" variables starting at "first", determine
10619 * the sign of the variable and put the results in the first "n"
10620 * elements of the array "signs".
10622 * 1 means that the variable is non-negative
10623 * -1 means that the variable is non-positive
10624 * 0 means the variable attains both positive and negative values.
10626 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10627 unsigned first
, unsigned n
, int *signs
)
10629 isl_vec
*bound
= NULL
;
10630 struct isl_tab
*tab
= NULL
;
10631 struct isl_tab_undo
*snap
;
10634 if (!bset
|| !signs
)
10635 return isl_stat_error
;
10637 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10638 tab
= isl_tab_from_basic_set(bset
, 0);
10639 if (!bound
|| !tab
)
10642 isl_seq_clr(bound
->el
, bound
->size
);
10643 isl_int_set_si(bound
->el
[0], -1);
10645 snap
= isl_tab_snap(tab
);
10646 for (i
= 0; i
< n
; ++i
) {
10649 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10650 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10652 empty
= tab
->empty
;
10653 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10654 if (isl_tab_rollback(tab
, snap
) < 0)
10662 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10663 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10665 empty
= tab
->empty
;
10666 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10667 if (isl_tab_rollback(tab
, snap
) < 0)
10670 signs
[i
] = empty
? -1 : 0;
10674 isl_vec_free(bound
);
10675 return isl_stat_ok
;
10678 isl_vec_free(bound
);
10679 return isl_stat_error
;
10682 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10683 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10685 if (!bset
|| !signs
)
10686 return isl_stat_error
;
10687 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10688 return isl_stat_error
);
10690 first
+= pos(bset
->dim
, type
) - 1;
10691 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10694 /* Is it possible for the integer division "div" to depend (possibly
10695 * indirectly) on any output dimensions?
10697 * If the div is undefined, then we conservatively assume that it
10698 * may depend on them.
10699 * Otherwise, we check if it actually depends on them or on any integer
10700 * divisions that may depend on them.
10702 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10705 unsigned n_out
, o_out
;
10706 unsigned n_div
, o_div
;
10708 if (isl_int_is_zero(bmap
->div
[div
][0]))
10709 return isl_bool_true
;
10711 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10712 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10714 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10715 return isl_bool_true
;
10717 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10718 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10720 for (i
= 0; i
< n_div
; ++i
) {
10721 isl_bool may_involve
;
10723 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10725 may_involve
= div_may_involve_output(bmap
, i
);
10726 if (may_involve
< 0 || may_involve
)
10727 return may_involve
;
10730 return isl_bool_false
;
10733 /* Return the first integer division of "bmap" in the range
10734 * [first, first + n[ that may depend on any output dimensions and
10735 * that has a non-zero coefficient in "c" (where the first coefficient
10736 * in "c" corresponds to integer division "first").
10738 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10739 isl_int
*c
, int first
, int n
)
10746 for (k
= first
; k
< first
+ n
; ++k
) {
10747 isl_bool may_involve
;
10749 if (isl_int_is_zero(c
[k
]))
10751 may_involve
= div_may_involve_output(bmap
, k
);
10752 if (may_involve
< 0)
10761 /* Look for a pair of inequality constraints in "bmap" of the form
10763 * -l + i >= 0 or i >= l
10765 * n + l - i >= 0 or i <= l + n
10767 * with n < "m" and i the output dimension at position "pos".
10768 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10769 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10770 * and earlier output dimensions, as well as integer divisions that do
10771 * not involve any of the output dimensions.
10773 * Return the index of the first inequality constraint or bmap->n_ineq
10774 * if no such pair can be found.
10776 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10777 int pos
, isl_int m
)
10782 unsigned n_div
, o_div
;
10783 unsigned n_out
, o_out
;
10789 ctx
= isl_basic_map_get_ctx(bmap
);
10790 total
= isl_basic_map_total_dim(bmap
);
10791 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10792 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10793 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10794 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10795 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10796 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10798 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10799 n_out
- (pos
+ 1)) != -1)
10801 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10804 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10805 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10808 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10809 bmap
->ineq
[j
] + 1, total
))
10813 if (j
>= bmap
->n_ineq
)
10815 isl_int_add(bmap
->ineq
[i
][0],
10816 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10817 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10818 isl_int_sub(bmap
->ineq
[i
][0],
10819 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10822 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10828 return bmap
->n_ineq
;
10831 /* Return the index of the equality of "bmap" that defines
10832 * the output dimension "pos" in terms of earlier dimensions.
10833 * The equality may also involve integer divisions, as long
10834 * as those integer divisions are defined in terms of
10835 * parameters or input dimensions.
10836 * In this case, *div is set to the number of integer divisions and
10837 * *ineq is set to the number of inequality constraints (provided
10838 * div and ineq are not NULL).
10840 * The equality may also involve a single integer division involving
10841 * the output dimensions (typically only output dimension "pos") as
10842 * long as the coefficient of output dimension "pos" is 1 or -1 and
10843 * there is a pair of constraints i >= l and i <= l + n, with i referring
10844 * to output dimension "pos", l an expression involving only earlier
10845 * dimensions and n smaller than the coefficient of the integer division
10846 * in the equality. In this case, the output dimension can be defined
10847 * in terms of a modulo expression that does not involve the integer division.
10848 * *div is then set to this single integer division and
10849 * *ineq is set to the index of constraint i >= l.
10851 * Return bmap->n_eq if there is no such equality.
10852 * Return -1 on error.
10854 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10855 int pos
, int *div
, int *ineq
)
10858 unsigned n_out
, o_out
;
10859 unsigned n_div
, o_div
;
10864 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10865 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10866 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10867 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10870 *ineq
= bmap
->n_ineq
;
10873 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10874 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10876 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10877 n_out
- (pos
+ 1)) != -1)
10879 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10883 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10884 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10886 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10887 k
+ 1, n_div
- (k
+1)) < n_div
)
10889 l
= find_modulo_constraint_pair(bmap
, pos
,
10890 bmap
->eq
[j
][o_div
+ k
]);
10893 if (l
>= bmap
->n_ineq
)
10905 /* Check if the given basic map is obviously single-valued.
10906 * In particular, for each output dimension, check that there is
10907 * an equality that defines the output dimension in terms of
10908 * earlier dimensions.
10910 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10916 return isl_bool_error
;
10918 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10920 for (i
= 0; i
< n_out
; ++i
) {
10923 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10926 return isl_bool_error
;
10927 if (eq
>= bmap
->n_eq
)
10928 return isl_bool_false
;
10931 return isl_bool_true
;
10934 /* Check if the given basic map is single-valued.
10935 * We simply compute
10939 * and check if the result is a subset of the identity mapping.
10941 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10944 isl_basic_map
*test
;
10948 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10952 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10953 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10955 space
= isl_basic_map_get_space(bmap
);
10956 space
= isl_space_map_from_set(isl_space_range(space
));
10957 id
= isl_basic_map_identity(space
);
10959 sv
= isl_basic_map_is_subset(test
, id
);
10961 isl_basic_map_free(test
);
10962 isl_basic_map_free(id
);
10967 /* Check if the given map is obviously single-valued.
10969 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10972 return isl_bool_error
;
10974 return isl_bool_true
;
10976 return isl_bool_false
;
10978 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10981 /* Check if the given map is single-valued.
10982 * We simply compute
10986 * and check if the result is a subset of the identity mapping.
10988 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10995 sv
= isl_map_plain_is_single_valued(map
);
10999 test
= isl_map_reverse(isl_map_copy(map
));
11000 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11002 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11003 id
= isl_map_identity(dim
);
11005 sv
= isl_map_is_subset(test
, id
);
11007 isl_map_free(test
);
11013 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11017 map
= isl_map_copy(map
);
11018 map
= isl_map_reverse(map
);
11019 in
= isl_map_is_single_valued(map
);
11025 /* Check if the given map is obviously injective.
11027 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11031 map
= isl_map_copy(map
);
11032 map
= isl_map_reverse(map
);
11033 in
= isl_map_plain_is_single_valued(map
);
11039 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11043 sv
= isl_map_is_single_valued(map
);
11047 return isl_map_is_injective(map
);
11050 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11052 return isl_map_is_single_valued(set_to_map(set
));
11055 /* Does "map" only map elements to themselves?
11057 * If the domain and range spaces are different, then "map"
11058 * is considered not to be an identity relation, even if it is empty.
11059 * Otherwise, construct the maximal identity relation and
11060 * check whether "map" is a subset of this relation.
11062 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11066 isl_bool equal
, is_identity
;
11068 space
= isl_map_get_space(map
);
11069 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11070 isl_space_free(space
);
11071 if (equal
< 0 || !equal
)
11074 id
= isl_map_identity(isl_map_get_space(map
));
11075 is_identity
= isl_map_is_subset(map
, id
);
11078 return is_identity
;
11081 int isl_map_is_translation(__isl_keep isl_map
*map
)
11086 delta
= isl_map_deltas(isl_map_copy(map
));
11087 ok
= isl_set_is_singleton(delta
);
11088 isl_set_free(delta
);
11093 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11095 if (isl_seq_first_non_zero(p
, pos
) != -1)
11097 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11102 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11109 return isl_bool_error
;
11111 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11112 return isl_bool_false
;
11114 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11115 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11116 for (j
= 0; j
< nvar
; ++j
) {
11117 int lower
= 0, upper
= 0;
11118 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11119 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11121 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11122 return isl_bool_false
;
11125 if (i
< bset
->n_eq
)
11127 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11128 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11130 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11131 return isl_bool_false
;
11132 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11137 if (!lower
|| !upper
)
11138 return isl_bool_false
;
11141 return isl_bool_true
;
11144 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11147 return isl_bool_error
;
11149 return isl_bool_false
;
11151 return isl_basic_set_is_box(set
->p
[0]);
11154 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11157 return isl_bool_error
;
11159 return isl_space_is_wrapping(bset
->dim
);
11162 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11165 return isl_bool_error
;
11167 return isl_space_is_wrapping(set
->dim
);
11170 /* Modify the space of "map" through a call to "change".
11171 * If "can_change" is set (not NULL), then first call it to check
11172 * if the modification is allowed, printing the error message "cannot_change"
11175 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11176 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11177 const char *cannot_change
,
11178 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11186 ok
= can_change
? can_change(map
) : isl_bool_true
;
11188 return isl_map_free(map
);
11190 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11191 return isl_map_free(map
));
11193 space
= change(isl_map_get_space(map
));
11194 map
= isl_map_reset_space(map
, space
);
11199 /* Is the domain of "map" a wrapped relation?
11201 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11204 return isl_bool_error
;
11206 return isl_space_domain_is_wrapping(map
->dim
);
11209 /* Is the range of "map" a wrapped relation?
11211 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11214 return isl_bool_error
;
11216 return isl_space_range_is_wrapping(map
->dim
);
11219 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11221 bmap
= isl_basic_map_cow(bmap
);
11225 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11229 bmap
= isl_basic_map_finalize(bmap
);
11231 return bset_from_bmap(bmap
);
11233 isl_basic_map_free(bmap
);
11237 /* Given a map A -> B, return the set (A -> B).
11239 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11241 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11244 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11246 bset
= isl_basic_set_cow(bset
);
11250 bset
->dim
= isl_space_unwrap(bset
->dim
);
11254 bset
= isl_basic_set_finalize(bset
);
11256 return bset_to_bmap(bset
);
11258 isl_basic_set_free(bset
);
11262 /* Given a set (A -> B), return the map A -> B.
11263 * Error out if "set" is not of the form (A -> B).
11265 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11267 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11268 "not a wrapping set", &isl_space_unwrap
);
11271 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11272 enum isl_dim_type type
)
11277 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11280 bmap
= isl_basic_map_cow(bmap
);
11284 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11288 bmap
= isl_basic_map_finalize(bmap
);
11292 isl_basic_map_free(bmap
);
11296 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11297 enum isl_dim_type type
)
11304 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11307 map
= isl_map_cow(map
);
11311 for (i
= 0; i
< map
->n
; ++i
) {
11312 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11316 map
->dim
= isl_space_reset(map
->dim
, type
);
11326 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11331 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11334 bmap
= isl_basic_map_cow(bmap
);
11338 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11342 bmap
= isl_basic_map_finalize(bmap
);
11346 isl_basic_map_free(bmap
);
11350 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11352 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11355 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11356 __isl_take isl_basic_map
*bmap
)
11361 if (!bmap
->dim
->nested
[0])
11364 bmap
= isl_basic_map_cow(bmap
);
11368 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11372 bmap
= isl_basic_map_finalize(bmap
);
11376 isl_basic_map_free(bmap
);
11380 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11381 __isl_take isl_basic_map
*bmap
)
11386 if (!bmap
->dim
->nested
[1])
11389 bmap
= isl_basic_map_cow(bmap
);
11393 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11397 bmap
= isl_basic_map_finalize(bmap
);
11401 isl_basic_map_free(bmap
);
11405 /* Remove any internal structure from the spaces of domain and range of "map".
11407 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11412 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11415 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11418 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11420 return set_from_map(isl_map_flatten(set_to_map(set
)));
11423 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11425 isl_space
*dim
, *flat_dim
;
11428 dim
= isl_set_get_space(set
);
11429 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11430 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11431 map
= isl_map_intersect_domain(map
, set
);
11436 /* Remove any internal structure from the space of the domain of "map".
11438 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11443 if (!map
->dim
->nested
[0])
11446 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11449 /* Remove any internal structure from the space of the range of "map".
11451 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11456 if (!map
->dim
->nested
[1])
11459 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11462 /* Reorder the dimensions of "bmap" according to the given dim_map
11463 * and set the dimension specification to "dim" and
11464 * perform Gaussian elimination on the result.
11466 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11467 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11469 isl_basic_map
*res
;
11472 bmap
= isl_basic_map_cow(bmap
);
11473 if (!bmap
|| !dim
|| !dim_map
)
11476 flags
= bmap
->flags
;
11477 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11478 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11479 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11480 res
= isl_basic_map_alloc_space(dim
,
11481 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11482 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11484 res
->flags
= flags
;
11485 res
= isl_basic_map_gauss(res
, NULL
);
11486 res
= isl_basic_map_finalize(res
);
11490 isl_basic_map_free(bmap
);
11491 isl_space_free(dim
);
11495 /* Reorder the dimensions of "map" according to given reordering.
11497 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11498 __isl_take isl_reordering
*r
)
11501 struct isl_dim_map
*dim_map
;
11503 map
= isl_map_cow(map
);
11504 dim_map
= isl_dim_map_from_reordering(r
);
11505 if (!map
|| !r
|| !dim_map
)
11508 for (i
= 0; i
< map
->n
; ++i
) {
11509 struct isl_dim_map
*dim_map_i
;
11511 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11513 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11514 isl_space_copy(r
->dim
), dim_map_i
);
11520 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11522 isl_reordering_free(r
);
11528 isl_reordering_free(r
);
11532 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11533 __isl_take isl_reordering
*r
)
11535 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11538 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11539 __isl_take isl_space
*model
)
11544 if (!map
|| !model
)
11547 ctx
= isl_space_get_ctx(model
);
11548 if (!isl_space_has_named_params(model
))
11549 isl_die(ctx
, isl_error_invalid
,
11550 "model has unnamed parameters", goto error
);
11551 if (isl_map_check_named_params(map
) < 0)
11553 aligned
= isl_map_space_has_equal_params(map
, model
);
11557 isl_reordering
*exp
;
11559 model
= isl_space_drop_dims(model
, isl_dim_in
,
11560 0, isl_space_dim(model
, isl_dim_in
));
11561 model
= isl_space_drop_dims(model
, isl_dim_out
,
11562 0, isl_space_dim(model
, isl_dim_out
));
11563 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11564 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11565 map
= isl_map_realign(map
, exp
);
11568 isl_space_free(model
);
11571 isl_space_free(model
);
11576 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11577 __isl_take isl_space
*model
)
11579 return isl_map_align_params(set
, model
);
11582 /* Align the parameters of "bmap" to those of "model", introducing
11583 * additional parameters if needed.
11585 __isl_give isl_basic_map
*isl_basic_map_align_params(
11586 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11589 isl_bool equal_params
;
11591 if (!bmap
|| !model
)
11594 ctx
= isl_space_get_ctx(model
);
11595 if (!isl_space_has_named_params(model
))
11596 isl_die(ctx
, isl_error_invalid
,
11597 "model has unnamed parameters", goto error
);
11598 if (!isl_space_has_named_params(bmap
->dim
))
11599 isl_die(ctx
, isl_error_invalid
,
11600 "relation has unnamed parameters", goto error
);
11601 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
11602 if (equal_params
< 0)
11604 if (!equal_params
) {
11605 isl_reordering
*exp
;
11606 struct isl_dim_map
*dim_map
;
11608 model
= isl_space_drop_dims(model
, isl_dim_in
,
11609 0, isl_space_dim(model
, isl_dim_in
));
11610 model
= isl_space_drop_dims(model
, isl_dim_out
,
11611 0, isl_space_dim(model
, isl_dim_out
));
11612 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11613 exp
= isl_reordering_extend_space(exp
,
11614 isl_basic_map_get_space(bmap
));
11615 dim_map
= isl_dim_map_from_reordering(exp
);
11616 bmap
= isl_basic_map_realign(bmap
,
11617 exp
? isl_space_copy(exp
->dim
) : NULL
,
11618 isl_dim_map_extend(dim_map
, bmap
));
11619 isl_reordering_free(exp
);
11623 isl_space_free(model
);
11626 isl_space_free(model
);
11627 isl_basic_map_free(bmap
);
11631 /* Do "bset" and "space" have the same parameters?
11633 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
11634 __isl_keep isl_space
*space
)
11636 isl_space
*bset_space
;
11638 bset_space
= isl_basic_set_peek_space(bset
);
11639 return isl_space_has_equal_params(bset_space
, space
);
11642 /* Do "map" and "space" have the same parameters?
11644 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
11645 __isl_keep isl_space
*space
)
11647 isl_space
*map_space
;
11649 map_space
= isl_map_peek_space(map
);
11650 return isl_space_has_equal_params(map_space
, space
);
11653 /* Do "set" and "space" have the same parameters?
11655 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
11656 __isl_keep isl_space
*space
)
11658 return isl_map_space_has_equal_params(set_to_map(set
), space
);
11661 /* Align the parameters of "bset" to those of "model", introducing
11662 * additional parameters if needed.
11664 __isl_give isl_basic_set
*isl_basic_set_align_params(
11665 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11667 return isl_basic_map_align_params(bset
, model
);
11670 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11671 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11672 enum isl_dim_type c2
, enum isl_dim_type c3
,
11673 enum isl_dim_type c4
, enum isl_dim_type c5
)
11675 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11676 struct isl_mat
*mat
;
11682 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11683 isl_basic_map_total_dim(bmap
) + 1);
11686 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11687 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11688 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11689 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11690 isl_int_set(mat
->row
[i
][pos
],
11691 bmap
->eq
[i
][off
+ k
]);
11699 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11700 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11701 enum isl_dim_type c2
, enum isl_dim_type c3
,
11702 enum isl_dim_type c4
, enum isl_dim_type c5
)
11704 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11705 struct isl_mat
*mat
;
11711 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11712 isl_basic_map_total_dim(bmap
) + 1);
11715 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11716 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11717 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11718 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11719 isl_int_set(mat
->row
[i
][pos
],
11720 bmap
->ineq
[i
][off
+ k
]);
11728 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11729 __isl_take isl_space
*dim
,
11730 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11731 enum isl_dim_type c2
, enum isl_dim_type c3
,
11732 enum isl_dim_type c4
, enum isl_dim_type c5
)
11734 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11735 isl_basic_map
*bmap
;
11741 if (!dim
|| !eq
|| !ineq
)
11744 if (eq
->n_col
!= ineq
->n_col
)
11745 isl_die(dim
->ctx
, isl_error_invalid
,
11746 "equalities and inequalities matrices should have "
11747 "same number of columns", goto error
);
11749 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11751 if (eq
->n_col
< total
)
11752 isl_die(dim
->ctx
, isl_error_invalid
,
11753 "number of columns too small", goto error
);
11755 extra
= eq
->n_col
- total
;
11757 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11758 eq
->n_row
, ineq
->n_row
);
11761 for (i
= 0; i
< extra
; ++i
) {
11762 k
= isl_basic_map_alloc_div(bmap
);
11765 isl_int_set_si(bmap
->div
[k
][0], 0);
11767 for (i
= 0; i
< eq
->n_row
; ++i
) {
11768 l
= isl_basic_map_alloc_equality(bmap
);
11771 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11772 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11773 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11774 isl_int_set(bmap
->eq
[l
][off
+ k
],
11780 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11781 l
= isl_basic_map_alloc_inequality(bmap
);
11784 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11785 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11786 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11787 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11788 ineq
->row
[i
][pos
]);
11794 isl_space_free(dim
);
11796 isl_mat_free(ineq
);
11798 bmap
= isl_basic_map_simplify(bmap
);
11799 return isl_basic_map_finalize(bmap
);
11801 isl_space_free(dim
);
11803 isl_mat_free(ineq
);
11807 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11808 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11809 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11811 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11812 c1
, c2
, c3
, c4
, isl_dim_in
);
11815 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11816 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11817 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11819 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11820 c1
, c2
, c3
, c4
, isl_dim_in
);
11823 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11824 __isl_take isl_space
*dim
,
11825 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11826 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11828 isl_basic_map
*bmap
;
11829 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11830 c1
, c2
, c3
, c4
, isl_dim_in
);
11831 return bset_from_bmap(bmap
);
11834 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11837 return isl_bool_error
;
11839 return isl_space_can_zip(bmap
->dim
);
11842 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11845 return isl_bool_error
;
11847 return isl_space_can_zip(map
->dim
);
11850 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11851 * (A -> C) -> (B -> D).
11853 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11862 if (!isl_basic_map_can_zip(bmap
))
11863 isl_die(bmap
->ctx
, isl_error_invalid
,
11864 "basic map cannot be zipped", goto error
);
11865 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11866 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11867 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11868 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11869 bmap
= isl_basic_map_cow(bmap
);
11870 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11873 bmap
->dim
= isl_space_zip(bmap
->dim
);
11876 bmap
= isl_basic_map_mark_final(bmap
);
11879 isl_basic_map_free(bmap
);
11883 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11884 * (A -> C) -> (B -> D).
11886 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11893 if (!isl_map_can_zip(map
))
11894 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11897 map
= isl_map_cow(map
);
11901 for (i
= 0; i
< map
->n
; ++i
) {
11902 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11907 map
->dim
= isl_space_zip(map
->dim
);
11917 /* Can we apply isl_basic_map_curry to "bmap"?
11918 * That is, does it have a nested relation in its domain?
11920 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11923 return isl_bool_error
;
11925 return isl_space_can_curry(bmap
->dim
);
11928 /* Can we apply isl_map_curry to "map"?
11929 * That is, does it have a nested relation in its domain?
11931 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11934 return isl_bool_error
;
11936 return isl_space_can_curry(map
->dim
);
11939 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11942 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11948 if (!isl_basic_map_can_curry(bmap
))
11949 isl_die(bmap
->ctx
, isl_error_invalid
,
11950 "basic map cannot be curried", goto error
);
11951 bmap
= isl_basic_map_cow(bmap
);
11954 bmap
->dim
= isl_space_curry(bmap
->dim
);
11957 bmap
= isl_basic_map_mark_final(bmap
);
11960 isl_basic_map_free(bmap
);
11964 /* Given a map (A -> B) -> C, return the corresponding map
11967 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11969 return isl_map_change_space(map
, &isl_map_can_curry
,
11970 "map cannot be curried", &isl_space_curry
);
11973 /* Can isl_map_range_curry be applied to "map"?
11974 * That is, does it have a nested relation in its range,
11975 * the domain of which is itself a nested relation?
11977 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11980 return isl_bool_error
;
11982 return isl_space_can_range_curry(map
->dim
);
11985 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11986 * A -> (B -> (C -> D)).
11988 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11990 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11991 "map range cannot be curried",
11992 &isl_space_range_curry
);
11995 /* Can we apply isl_basic_map_uncurry to "bmap"?
11996 * That is, does it have a nested relation in its domain?
11998 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12001 return isl_bool_error
;
12003 return isl_space_can_uncurry(bmap
->dim
);
12006 /* Can we apply isl_map_uncurry to "map"?
12007 * That is, does it have a nested relation in its domain?
12009 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12012 return isl_bool_error
;
12014 return isl_space_can_uncurry(map
->dim
);
12017 /* Given a basic map A -> (B -> C), return the corresponding basic map
12020 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12026 if (!isl_basic_map_can_uncurry(bmap
))
12027 isl_die(bmap
->ctx
, isl_error_invalid
,
12028 "basic map cannot be uncurried",
12029 return isl_basic_map_free(bmap
));
12030 bmap
= isl_basic_map_cow(bmap
);
12033 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12035 return isl_basic_map_free(bmap
);
12036 bmap
= isl_basic_map_mark_final(bmap
);
12040 /* Given a map A -> (B -> C), return the corresponding map
12043 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12045 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12046 "map cannot be uncurried", &isl_space_uncurry
);
12049 /* Construct a basic map mapping the domain of the affine expression
12050 * to a one-dimensional range prescribed by the affine expression.
12051 * If "rational" is set, then construct a rational basic map.
12053 * A NaN affine expression cannot be converted to a basic map.
12055 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12056 __isl_take isl_aff
*aff
, int rational
)
12061 isl_local_space
*ls
;
12062 isl_basic_map
*bmap
= NULL
;
12066 is_nan
= isl_aff_is_nan(aff
);
12070 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12071 "cannot convert NaN", goto error
);
12073 ls
= isl_aff_get_local_space(aff
);
12074 bmap
= isl_basic_map_from_local_space(ls
);
12075 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12076 k
= isl_basic_map_alloc_equality(bmap
);
12080 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12081 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12082 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12083 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12084 aff
->v
->size
- (pos
+ 1));
12088 bmap
= isl_basic_map_set_rational(bmap
);
12089 bmap
= isl_basic_map_gauss(bmap
, NULL
);
12090 bmap
= isl_basic_map_finalize(bmap
);
12094 isl_basic_map_free(bmap
);
12098 /* Construct a basic map mapping the domain of the affine expression
12099 * to a one-dimensional range prescribed by the affine expression.
12101 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12103 return isl_basic_map_from_aff2(aff
, 0);
12106 /* Construct a map mapping the domain of the affine expression
12107 * to a one-dimensional range prescribed by the affine expression.
12109 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12111 isl_basic_map
*bmap
;
12113 bmap
= isl_basic_map_from_aff(aff
);
12114 return isl_map_from_basic_map(bmap
);
12117 /* Construct a basic map mapping the domain the multi-affine expression
12118 * to its range, with each dimension in the range equated to the
12119 * corresponding affine expression.
12120 * If "rational" is set, then construct a rational basic map.
12122 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12123 __isl_take isl_multi_aff
*maff
, int rational
)
12127 isl_basic_map
*bmap
;
12132 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12133 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12134 "invalid space", goto error
);
12136 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12137 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12139 bmap
= isl_basic_map_set_rational(bmap
);
12141 for (i
= 0; i
< maff
->n
; ++i
) {
12143 isl_basic_map
*bmap_i
;
12145 aff
= isl_aff_copy(maff
->p
[i
]);
12146 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12148 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12151 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12153 isl_multi_aff_free(maff
);
12156 isl_multi_aff_free(maff
);
12160 /* Construct a basic map mapping the domain the multi-affine expression
12161 * to its range, with each dimension in the range equated to the
12162 * corresponding affine expression.
12164 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12165 __isl_take isl_multi_aff
*ma
)
12167 return isl_basic_map_from_multi_aff2(ma
, 0);
12170 /* Construct a map mapping the domain the multi-affine expression
12171 * to its range, with each dimension in the range equated to the
12172 * corresponding affine expression.
12174 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12176 isl_basic_map
*bmap
;
12178 bmap
= isl_basic_map_from_multi_aff(maff
);
12179 return isl_map_from_basic_map(bmap
);
12182 /* Construct a basic map mapping a domain in the given space to
12183 * to an n-dimensional range, with n the number of elements in the list,
12184 * where each coordinate in the range is prescribed by the
12185 * corresponding affine expression.
12186 * The domains of all affine expressions in the list are assumed to match
12189 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12190 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12194 isl_basic_map
*bmap
;
12199 dim
= isl_space_from_domain(domain_dim
);
12200 bmap
= isl_basic_map_universe(dim
);
12202 for (i
= 0; i
< list
->n
; ++i
) {
12204 isl_basic_map
*bmap_i
;
12206 aff
= isl_aff_copy(list
->p
[i
]);
12207 bmap_i
= isl_basic_map_from_aff(aff
);
12209 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12212 isl_aff_list_free(list
);
12216 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12217 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12219 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12222 /* Construct a basic map where the given dimensions are equal to each other.
12224 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12225 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12227 isl_basic_map
*bmap
= NULL
;
12233 if (pos1
>= isl_space_dim(space
, type1
))
12234 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12235 "index out of bounds", goto error
);
12236 if (pos2
>= isl_space_dim(space
, type2
))
12237 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12238 "index out of bounds", goto error
);
12240 if (type1
== type2
&& pos1
== pos2
)
12241 return isl_basic_map_universe(space
);
12243 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12244 i
= isl_basic_map_alloc_equality(bmap
);
12247 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12248 pos1
+= isl_basic_map_offset(bmap
, type1
);
12249 pos2
+= isl_basic_map_offset(bmap
, type2
);
12250 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12251 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12252 bmap
= isl_basic_map_finalize(bmap
);
12253 isl_space_free(space
);
12256 isl_space_free(space
);
12257 isl_basic_map_free(bmap
);
12261 /* Add a constraint imposing that the given two dimensions are equal.
12263 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12264 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12268 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12270 bmap
= isl_basic_map_intersect(bmap
, eq
);
12275 /* Add a constraint imposing that the given two dimensions are equal.
12277 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12278 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12280 isl_basic_map
*bmap
;
12282 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12284 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12289 /* Add a constraint imposing that the given two dimensions have opposite values.
12291 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12292 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12294 isl_basic_map
*bmap
= NULL
;
12300 if (pos1
>= isl_map_dim(map
, type1
))
12301 isl_die(map
->ctx
, isl_error_invalid
,
12302 "index out of bounds", goto error
);
12303 if (pos2
>= isl_map_dim(map
, type2
))
12304 isl_die(map
->ctx
, isl_error_invalid
,
12305 "index out of bounds", goto error
);
12307 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12308 i
= isl_basic_map_alloc_equality(bmap
);
12311 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12312 pos1
+= isl_basic_map_offset(bmap
, type1
);
12313 pos2
+= isl_basic_map_offset(bmap
, type2
);
12314 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12315 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12316 bmap
= isl_basic_map_finalize(bmap
);
12318 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12322 isl_basic_map_free(bmap
);
12327 /* Construct a constraint imposing that the value of the first dimension is
12328 * greater than or equal to that of the second.
12330 static __isl_give isl_constraint
*constraint_order_ge(
12331 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12332 enum isl_dim_type type2
, int pos2
)
12339 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12341 if (pos1
>= isl_constraint_dim(c
, type1
))
12342 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12343 "index out of bounds", return isl_constraint_free(c
));
12344 if (pos2
>= isl_constraint_dim(c
, type2
))
12345 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12346 "index out of bounds", return isl_constraint_free(c
));
12348 if (type1
== type2
&& pos1
== pos2
)
12351 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12352 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12357 /* Add a constraint imposing that the value of the first dimension is
12358 * greater than or equal to that of the second.
12360 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12361 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12366 if (type1
== type2
&& pos1
== pos2
)
12368 space
= isl_basic_map_get_space(bmap
);
12369 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12370 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12375 /* Add a constraint imposing that the value of the first dimension is
12376 * greater than or equal to that of the second.
12378 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12379 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12384 if (type1
== type2
&& pos1
== pos2
)
12386 space
= isl_map_get_space(map
);
12387 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12388 map
= isl_map_add_constraint(map
, c
);
12393 /* Add a constraint imposing that the value of the first dimension is
12394 * less than or equal to that of the second.
12396 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12397 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12399 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12402 /* Construct a basic map where the value of the first dimension is
12403 * greater than that of the second.
12405 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12406 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12408 isl_basic_map
*bmap
= NULL
;
12414 if (pos1
>= isl_space_dim(space
, type1
))
12415 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12416 "index out of bounds", goto error
);
12417 if (pos2
>= isl_space_dim(space
, type2
))
12418 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12419 "index out of bounds", goto error
);
12421 if (type1
== type2
&& pos1
== pos2
)
12422 return isl_basic_map_empty(space
);
12424 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12425 i
= isl_basic_map_alloc_inequality(bmap
);
12427 return isl_basic_map_free(bmap
);
12428 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12429 pos1
+= isl_basic_map_offset(bmap
, type1
);
12430 pos2
+= isl_basic_map_offset(bmap
, type2
);
12431 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12432 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12433 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12434 bmap
= isl_basic_map_finalize(bmap
);
12438 isl_space_free(space
);
12439 isl_basic_map_free(bmap
);
12443 /* Add a constraint imposing that the value of the first dimension is
12444 * greater than that of the second.
12446 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12447 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12451 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12453 bmap
= isl_basic_map_intersect(bmap
, gt
);
12458 /* Add a constraint imposing that the value of the first dimension is
12459 * greater than that of the second.
12461 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12462 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12464 isl_basic_map
*bmap
;
12466 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12468 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12473 /* Add a constraint imposing that the value of the first dimension is
12474 * smaller than that of the second.
12476 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12477 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12479 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12482 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12486 isl_local_space
*ls
;
12491 if (!isl_basic_map_divs_known(bmap
))
12492 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12493 "some divs are unknown", return NULL
);
12495 ls
= isl_basic_map_get_local_space(bmap
);
12496 div
= isl_local_space_get_div(ls
, pos
);
12497 isl_local_space_free(ls
);
12502 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12505 return isl_basic_map_get_div(bset
, pos
);
12508 /* Plug in "subs" for dimension "type", "pos" of "bset".
12510 * Let i be the dimension to replace and let "subs" be of the form
12514 * Any integer division with a non-zero coefficient for i,
12516 * floor((a i + g)/m)
12520 * floor((a f + d g)/(m d))
12522 * Constraints of the form
12530 * We currently require that "subs" is an integral expression.
12531 * Handling rational expressions may require us to add stride constraints
12532 * as we do in isl_basic_set_preimage_multi_aff.
12534 __isl_give isl_basic_set
*isl_basic_set_substitute(
12535 __isl_take isl_basic_set
*bset
,
12536 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12542 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12545 bset
= isl_basic_set_cow(bset
);
12546 if (!bset
|| !subs
)
12549 ctx
= isl_basic_set_get_ctx(bset
);
12550 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12551 isl_die(ctx
, isl_error_invalid
,
12552 "spaces don't match", goto error
);
12553 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12554 isl_die(ctx
, isl_error_unsupported
,
12555 "cannot handle divs yet", goto error
);
12556 if (!isl_int_is_one(subs
->v
->el
[0]))
12557 isl_die(ctx
, isl_error_invalid
,
12558 "can only substitute integer expressions", goto error
);
12560 pos
+= isl_basic_set_offset(bset
, type
);
12564 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12565 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12567 isl_int_set(v
, bset
->eq
[i
][pos
]);
12568 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12569 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12570 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12573 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12574 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12576 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12577 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12578 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12579 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12582 for (i
= 0; i
< bset
->n_div
; ++i
) {
12583 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12585 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12586 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12587 isl_seq_combine(bset
->div
[i
] + 1,
12588 subs
->v
->el
[0], bset
->div
[i
] + 1,
12589 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12590 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12595 bset
= isl_basic_set_simplify(bset
);
12596 return isl_basic_set_finalize(bset
);
12598 isl_basic_set_free(bset
);
12602 /* Plug in "subs" for dimension "type", "pos" of "set".
12604 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12605 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12609 if (set
&& isl_set_plain_is_empty(set
))
12612 set
= isl_set_cow(set
);
12616 for (i
= set
->n
- 1; i
>= 0; --i
) {
12617 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12618 if (remove_if_empty(set
, i
) < 0)
12628 /* Check if the range of "ma" is compatible with the domain or range
12629 * (depending on "type") of "bmap".
12631 static isl_stat
check_basic_map_compatible_range_multi_aff(
12632 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12633 __isl_keep isl_multi_aff
*ma
)
12636 isl_space
*ma_space
;
12638 ma_space
= isl_multi_aff_get_space(ma
);
12640 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
12644 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12645 "parameters don't match", goto error
);
12646 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12650 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12651 "spaces don't match", goto error
);
12653 isl_space_free(ma_space
);
12654 return isl_stat_ok
;
12656 isl_space_free(ma_space
);
12657 return isl_stat_error
;
12660 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12661 * coefficients before the transformed range of dimensions,
12662 * the "n_after" coefficients after the transformed range of dimensions
12663 * and the coefficients of the other divs in "bmap".
12665 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12666 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12671 isl_local_space
*ls
;
12676 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12680 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12681 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12682 for (i
= 0; i
< n_div
; ++i
) {
12683 int o_bmap
= 0, o_ls
= 0;
12685 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12686 o_bmap
+= 1 + 1 + n_param
;
12687 o_ls
+= 1 + 1 + n_param
;
12688 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12689 o_bmap
+= n_before
;
12690 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12691 ls
->div
->row
[i
] + o_ls
, n_set
);
12694 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12696 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12697 ls
->div
->row
[i
] + o_ls
, n_div
);
12700 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12701 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12705 isl_local_space_free(ls
);
12708 isl_local_space_free(ls
);
12712 /* How many stride constraints does "ma" enforce?
12713 * That is, how many of the affine expressions have a denominator
12714 * different from one?
12716 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12721 for (i
= 0; i
< ma
->n
; ++i
)
12722 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12728 /* For each affine expression in ma of the form
12730 * x_i = (f_i y + h_i)/m_i
12732 * with m_i different from one, add a constraint to "bmap"
12735 * f_i y + h_i = m_i alpha_i
12737 * with alpha_i an additional existentially quantified variable.
12739 * The input variables of "ma" correspond to a subset of the variables
12740 * of "bmap". There are "n_before" variables in "bmap" before this
12741 * subset and "n_after" variables after this subset.
12742 * The integer divisions of the affine expressions in "ma" are assumed
12743 * to have been aligned. There are "n_div_ma" of them and
12744 * they appear first in "bmap", straight after the "n_after" variables.
12746 static __isl_give isl_basic_map
*add_ma_strides(
12747 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12748 int n_before
, int n_after
, int n_div_ma
)
12756 total
= isl_basic_map_total_dim(bmap
);
12757 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12758 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12759 for (i
= 0; i
< ma
->n
; ++i
) {
12760 int o_bmap
= 0, o_ma
= 1;
12762 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12764 div
= isl_basic_map_alloc_div(bmap
);
12765 k
= isl_basic_map_alloc_equality(bmap
);
12766 if (div
< 0 || k
< 0)
12768 isl_int_set_si(bmap
->div
[div
][0], 0);
12769 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12770 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12771 o_bmap
+= 1 + n_param
;
12772 o_ma
+= 1 + n_param
;
12773 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12774 o_bmap
+= n_before
;
12775 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12776 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12779 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12781 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12782 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12783 o_bmap
+= n_div_ma
;
12785 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12786 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12792 isl_basic_map_free(bmap
);
12796 /* Replace the domain or range space (depending on "type) of "space" by "set".
12798 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12799 enum isl_dim_type type
, __isl_take isl_space
*set
)
12801 if (type
== isl_dim_in
) {
12802 space
= isl_space_range(space
);
12803 space
= isl_space_map_from_domain_and_range(set
, space
);
12805 space
= isl_space_domain(space
);
12806 space
= isl_space_map_from_domain_and_range(space
, set
);
12812 /* Compute the preimage of the domain or range (depending on "type")
12813 * of "bmap" under the function represented by "ma".
12814 * In other words, plug in "ma" in the domain or range of "bmap".
12815 * The result is a basic map that lives in the same space as "bmap"
12816 * except that the domain or range has been replaced by
12817 * the domain space of "ma".
12819 * If bmap is represented by
12821 * A(p) + S u + B x + T v + C(divs) >= 0,
12823 * where u and x are input and output dimensions if type == isl_dim_out
12824 * while x and v are input and output dimensions if type == isl_dim_in,
12825 * and ma is represented by
12827 * x = D(p) + F(y) + G(divs')
12829 * then the result is
12831 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12833 * The divs in the input set are similarly adjusted.
12836 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12840 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12841 * B_i G(divs') + c_i(divs))/n_i)
12843 * If bmap is not a rational map and if F(y) involves any denominators
12845 * x_i = (f_i y + h_i)/m_i
12847 * then additional constraints are added to ensure that we only
12848 * map back integer points. That is we enforce
12850 * f_i y + h_i = m_i alpha_i
12852 * with alpha_i an additional existentially quantified variable.
12854 * We first copy over the divs from "ma".
12855 * Then we add the modified constraints and divs from "bmap".
12856 * Finally, we add the stride constraints, if needed.
12858 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12859 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12860 __isl_take isl_multi_aff
*ma
)
12864 isl_basic_map
*res
= NULL
;
12865 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12866 isl_int f
, c1
, c2
, g
;
12875 ma
= isl_multi_aff_align_divs(ma
);
12878 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12881 if (type
== isl_dim_in
) {
12883 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12885 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12888 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12889 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12891 space
= isl_multi_aff_get_domain_space(ma
);
12892 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12893 rational
= isl_basic_map_is_rational(bmap
);
12894 strides
= rational
? 0 : multi_aff_strides(ma
);
12895 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12896 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12898 res
= isl_basic_map_set_rational(res
);
12900 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12901 if (isl_basic_map_alloc_div(res
) < 0)
12904 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12907 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12908 k
= isl_basic_map_alloc_equality(res
);
12911 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12912 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12915 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12916 k
= isl_basic_map_alloc_inequality(res
);
12919 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12920 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12923 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12924 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12925 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12928 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12929 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12934 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12940 isl_basic_map_free(bmap
);
12941 isl_multi_aff_free(ma
);
12942 res
= isl_basic_map_simplify(res
);
12943 return isl_basic_map_finalize(res
);
12949 isl_basic_map_free(bmap
);
12950 isl_multi_aff_free(ma
);
12951 isl_basic_map_free(res
);
12955 /* Compute the preimage of "bset" under the function represented by "ma".
12956 * In other words, plug in "ma" in "bset". The result is a basic set
12957 * that lives in the domain space of "ma".
12959 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12960 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12962 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12965 /* Compute the preimage of the domain of "bmap" under the function
12966 * represented by "ma".
12967 * In other words, plug in "ma" in the domain of "bmap".
12968 * The result is a basic map that lives in the same space as "bmap"
12969 * except that the domain has been replaced by the domain space of "ma".
12971 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12972 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12974 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12977 /* Compute the preimage of the range of "bmap" under the function
12978 * represented by "ma".
12979 * In other words, plug in "ma" in the range of "bmap".
12980 * The result is a basic map that lives in the same space as "bmap"
12981 * except that the range has been replaced by the domain space of "ma".
12983 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12984 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12986 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12989 /* Check if the range of "ma" is compatible with the domain or range
12990 * (depending on "type") of "map".
12991 * Return isl_stat_error if anything is wrong.
12993 static isl_stat
check_map_compatible_range_multi_aff(
12994 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12995 __isl_keep isl_multi_aff
*ma
)
12998 isl_space
*ma_space
;
13000 ma_space
= isl_multi_aff_get_space(ma
);
13001 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13002 isl_space_free(ma_space
);
13004 return isl_stat_error
;
13006 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13007 "spaces don't match", return isl_stat_error
);
13008 return isl_stat_ok
;
13011 /* Compute the preimage of the domain or range (depending on "type")
13012 * of "map" under the function represented by "ma".
13013 * In other words, plug in "ma" in the domain or range of "map".
13014 * The result is a map that lives in the same space as "map"
13015 * except that the domain or range has been replaced by
13016 * the domain space of "ma".
13018 * The parameters are assumed to have been aligned.
13020 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13021 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13026 map
= isl_map_cow(map
);
13027 ma
= isl_multi_aff_align_divs(ma
);
13030 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13033 for (i
= 0; i
< map
->n
; ++i
) {
13034 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13035 isl_multi_aff_copy(ma
));
13040 space
= isl_multi_aff_get_domain_space(ma
);
13041 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13043 isl_space_free(map
->dim
);
13048 isl_multi_aff_free(ma
);
13050 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13051 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13054 isl_multi_aff_free(ma
);
13059 /* Compute the preimage of the domain or range (depending on "type")
13060 * of "map" under the function represented by "ma".
13061 * In other words, plug in "ma" in the domain or range of "map".
13062 * The result is a map that lives in the same space as "map"
13063 * except that the domain or range has been replaced by
13064 * the domain space of "ma".
13066 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13067 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13074 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13078 return map_preimage_multi_aff(map
, type
, ma
);
13080 if (isl_map_check_named_params(map
) < 0)
13082 if (!isl_space_has_named_params(ma
->space
))
13083 isl_die(map
->ctx
, isl_error_invalid
,
13084 "unaligned unnamed parameters", goto error
);
13085 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13086 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13088 return map_preimage_multi_aff(map
, type
, ma
);
13090 isl_multi_aff_free(ma
);
13091 return isl_map_free(map
);
13094 /* Compute the preimage of "set" under the function represented by "ma".
13095 * In other words, plug in "ma" in "set". The result is a set
13096 * that lives in the domain space of "ma".
13098 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13099 __isl_take isl_multi_aff
*ma
)
13101 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13104 /* Compute the preimage of the domain of "map" under the function
13105 * represented by "ma".
13106 * In other words, plug in "ma" in the domain of "map".
13107 * The result is a map that lives in the same space as "map"
13108 * except that the domain has been replaced by the domain space of "ma".
13110 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13111 __isl_take isl_multi_aff
*ma
)
13113 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13116 /* Compute the preimage of the range of "map" under the function
13117 * represented by "ma".
13118 * In other words, plug in "ma" in the range of "map".
13119 * The result is a map that lives in the same space as "map"
13120 * except that the range has been replaced by the domain space of "ma".
13122 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13123 __isl_take isl_multi_aff
*ma
)
13125 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13128 /* Compute the preimage of "map" under the function represented by "pma".
13129 * In other words, plug in "pma" in the domain or range of "map".
13130 * The result is a map that lives in the same space as "map",
13131 * except that the space of type "type" has been replaced by
13132 * the domain space of "pma".
13134 * The parameters of "map" and "pma" are assumed to have been aligned.
13136 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13137 __isl_take isl_map
*map
, enum isl_dim_type type
,
13138 __isl_take isl_pw_multi_aff
*pma
)
13147 isl_pw_multi_aff_free(pma
);
13148 res
= isl_map_empty(isl_map_get_space(map
));
13153 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13154 isl_multi_aff_copy(pma
->p
[0].maff
));
13155 if (type
== isl_dim_in
)
13156 res
= isl_map_intersect_domain(res
,
13157 isl_map_copy(pma
->p
[0].set
));
13159 res
= isl_map_intersect_range(res
,
13160 isl_map_copy(pma
->p
[0].set
));
13162 for (i
= 1; i
< pma
->n
; ++i
) {
13165 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13166 isl_multi_aff_copy(pma
->p
[i
].maff
));
13167 if (type
== isl_dim_in
)
13168 res_i
= isl_map_intersect_domain(res_i
,
13169 isl_map_copy(pma
->p
[i
].set
));
13171 res_i
= isl_map_intersect_range(res_i
,
13172 isl_map_copy(pma
->p
[i
].set
));
13173 res
= isl_map_union(res
, res_i
);
13176 isl_pw_multi_aff_free(pma
);
13180 isl_pw_multi_aff_free(pma
);
13185 /* Compute the preimage of "map" under the function represented by "pma".
13186 * In other words, plug in "pma" in the domain or range of "map".
13187 * The result is a map that lives in the same space as "map",
13188 * except that the space of type "type" has been replaced by
13189 * the domain space of "pma".
13191 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13192 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13199 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13203 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13205 if (isl_map_check_named_params(map
) < 0)
13207 if (!isl_space_has_named_params(pma
->dim
))
13208 isl_die(map
->ctx
, isl_error_invalid
,
13209 "unaligned unnamed parameters", goto error
);
13210 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13211 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13213 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13215 isl_pw_multi_aff_free(pma
);
13216 return isl_map_free(map
);
13219 /* Compute the preimage of "set" under the function represented by "pma".
13220 * In other words, plug in "pma" in "set". The result is a set
13221 * that lives in the domain space of "pma".
13223 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13224 __isl_take isl_pw_multi_aff
*pma
)
13226 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13229 /* Compute the preimage of the domain of "map" under the function
13230 * represented by "pma".
13231 * In other words, plug in "pma" in the domain of "map".
13232 * The result is a map that lives in the same space as "map",
13233 * except that domain space has been replaced by the domain space of "pma".
13235 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13236 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13238 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13241 /* Compute the preimage of the range of "map" under the function
13242 * represented by "pma".
13243 * In other words, plug in "pma" in the range of "map".
13244 * The result is a map that lives in the same space as "map",
13245 * except that range space has been replaced by the domain space of "pma".
13247 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13248 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13250 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13253 /* Compute the preimage of "map" under the function represented by "mpa".
13254 * In other words, plug in "mpa" in the domain or range of "map".
13255 * The result is a map that lives in the same space as "map",
13256 * except that the space of type "type" has been replaced by
13257 * the domain space of "mpa".
13259 * If the map does not involve any constraints that refer to the
13260 * dimensions of the substituted space, then the only possible
13261 * effect of "mpa" on the map is to map the space to a different space.
13262 * We create a separate isl_multi_aff to effectuate this change
13263 * in order to avoid spurious splitting of the map along the pieces
13266 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13267 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13270 isl_pw_multi_aff
*pma
;
13275 n
= isl_map_dim(map
, type
);
13276 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13280 space
= isl_multi_pw_aff_get_space(mpa
);
13281 isl_multi_pw_aff_free(mpa
);
13282 ma
= isl_multi_aff_zero(space
);
13283 return isl_map_preimage_multi_aff(map
, type
, ma
);
13286 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13287 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13290 isl_multi_pw_aff_free(mpa
);
13294 /* Compute the preimage of "map" under the function represented by "mpa".
13295 * In other words, plug in "mpa" in the domain "map".
13296 * The result is a map that lives in the same space as "map",
13297 * except that domain space has been replaced by the domain space of "mpa".
13299 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13300 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13302 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13305 /* Compute the preimage of "set" by the function represented by "mpa".
13306 * In other words, plug in "mpa" in "set".
13308 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13309 __isl_take isl_multi_pw_aff
*mpa
)
13311 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13314 /* Are the "n" "coefficients" starting at "first" of the integer division
13315 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13317 * The "coefficient" at position 0 is the denominator.
13318 * The "coefficient" at position 1 is the constant term.
13320 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13321 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13322 unsigned first
, unsigned n
)
13324 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13325 return isl_bool_error
;
13326 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13327 return isl_bool_error
;
13328 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13329 bmap2
->div
[pos2
] + first
, n
);
13332 /* Are the integer division expressions at position "pos1" in "bmap1" and
13333 * "pos2" in "bmap2" equal to each other, except that the constant terms
13336 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13337 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13338 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13343 if (!bmap1
|| !bmap2
)
13344 return isl_bool_error
;
13345 total
= isl_basic_map_total_dim(bmap1
);
13346 if (total
!= isl_basic_map_total_dim(bmap2
))
13347 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13348 "incomparable div expressions", return isl_bool_error
);
13349 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13351 if (equal
< 0 || !equal
)
13353 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13355 if (equal
< 0 || equal
)
13356 return isl_bool_not(equal
);
13357 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13361 /* Replace the numerator of the constant term of the integer division
13362 * expression at position "div" in "bmap" by "value".
13363 * The caller guarantees that this does not change the meaning
13366 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13367 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13369 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13370 return isl_basic_map_free(bmap
);
13372 isl_int_set_si(bmap
->div
[div
][1], value
);
13377 /* Is the point "inner" internal to inequality constraint "ineq"
13379 * The point is considered to be internal to the inequality constraint,
13380 * if it strictly lies on the positive side of the inequality constraint,
13381 * or if it lies on the constraint and the constraint is lexico-positive.
13383 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13384 __isl_keep isl_basic_set
*bset
, int ineq
)
13390 if (!inner
|| !bset
)
13391 return isl_bool_error
;
13393 ctx
= isl_basic_set_get_ctx(bset
);
13394 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13395 &ctx
->normalize_gcd
);
13396 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13397 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13399 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13400 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13401 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13404 /* Tighten the inequality constraints of "bset" that are outward with respect
13405 * to the point "vec".
13406 * That is, tighten the constraints that are not satisfied by "vec".
13408 * "vec" is a point internal to some superset S of "bset" that is used
13409 * to make the subsets of S disjoint, by tightening one half of the constraints
13410 * that separate two subsets. In particular, the constraints of S
13411 * are all satisfied by "vec" and should not be tightened.
13412 * Of the internal constraints, those that have "vec" on the outside
13413 * are tightened. The shared facet is included in the adjacent subset
13414 * with the opposite constraint.
13415 * For constraints that saturate "vec", this criterion cannot be used
13416 * to determine which of the two sides should be tightened.
13417 * Instead, the sign of the first non-zero coefficient is used
13418 * to make this choice. Note that this second criterion is never used
13419 * on the constraints of S since "vec" is interior to "S".
13421 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13422 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13426 bset
= isl_basic_set_cow(bset
);
13429 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13432 internal
= is_internal(vec
, bset
, j
);
13434 return isl_basic_set_free(bset
);
13437 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
13443 /* Replace the variables x of type "type" starting at "first" in "bmap"
13444 * by x' with x = M x' with M the matrix trans.
13445 * That is, replace the corresponding coefficients c by c M.
13447 * The transformation matrix should be a square matrix.
13449 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
13450 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
13451 __isl_take isl_mat
*trans
)
13455 bmap
= isl_basic_map_cow(bmap
);
13456 if (!bmap
|| !trans
)
13459 if (trans
->n_row
!= trans
->n_col
)
13460 isl_die(trans
->ctx
, isl_error_invalid
,
13461 "expecting square transformation matrix", goto error
);
13462 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
13463 isl_die(trans
->ctx
, isl_error_invalid
,
13464 "oversized transformation matrix", goto error
);
13466 pos
= isl_basic_map_offset(bmap
, type
) + first
;
13468 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
13469 isl_mat_copy(trans
)) < 0)
13471 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
13472 isl_mat_copy(trans
)) < 0)
13474 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
13475 isl_mat_copy(trans
)) < 0)
13478 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
13479 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
13481 isl_mat_free(trans
);
13484 isl_mat_free(trans
);
13485 isl_basic_map_free(bmap
);
13489 /* Replace the variables x of type "type" starting at "first" in "bset"
13490 * by x' with x = M x' with M the matrix trans.
13491 * That is, replace the corresponding coefficients c by c M.
13493 * The transformation matrix should be a square matrix.
13495 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
13496 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
13497 __isl_take isl_mat
*trans
)
13499 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);