2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
22 #include <isl/constraint.h>
23 #include "isl_space_private.h"
24 #include "isl_equalities.h"
25 #include <isl_lp_private.h>
29 #include <isl_reordering.h>
30 #include "isl_sample.h"
34 #include <isl_mat_private.h>
35 #include <isl_vec_private.h>
36 #include <isl_dim_map.h>
37 #include <isl_local_space_private.h>
38 #include <isl_aff_private.h>
39 #include <isl_options_private.h>
40 #include <isl_morph.h>
41 #include <isl_val_private.h>
42 #include <isl/deprecated/map_int.h>
43 #include <isl/deprecated/set_int.h>
45 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
48 case isl_dim_param
: return dim
->nparam
;
49 case isl_dim_in
: return dim
->n_in
;
50 case isl_dim_out
: return dim
->n_out
;
51 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
56 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
59 case isl_dim_param
: return 1;
60 case isl_dim_in
: return 1 + dim
->nparam
;
61 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
66 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
67 enum isl_dim_type type
)
72 case isl_dim_cst
: return 1;
75 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
76 case isl_dim_div
: return bmap
->n_div
;
77 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
82 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
84 return map
? n(map
->dim
, type
) : 0;
87 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
89 return set
? n(set
->dim
, type
) : 0;
92 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
93 enum isl_dim_type type
)
95 isl_space
*dim
= bmap
->dim
;
97 case isl_dim_cst
: return 0;
98 case isl_dim_param
: return 1;
99 case isl_dim_in
: return 1 + dim
->nparam
;
100 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
101 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
106 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
107 enum isl_dim_type type
)
109 return isl_basic_map_offset(bset
, type
);
112 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
114 return pos(map
->dim
, type
);
117 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
118 enum isl_dim_type type
)
120 return isl_basic_map_dim(bset
, type
);
123 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
125 return isl_basic_set_dim(bset
, isl_dim_set
);
128 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
130 return isl_basic_set_dim(bset
, isl_dim_param
);
133 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
137 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
140 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
142 return isl_set_dim(set
, isl_dim_set
);
145 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
147 return isl_set_dim(set
, isl_dim_param
);
150 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
152 return bmap
? bmap
->dim
->n_in
: 0;
155 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
157 return bmap
? bmap
->dim
->n_out
: 0;
160 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
162 return bmap
? bmap
->dim
->nparam
: 0;
165 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
167 return bmap
? bmap
->n_div
: 0;
170 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
172 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
175 unsigned isl_map_n_in(const struct isl_map
*map
)
177 return map
? map
->dim
->n_in
: 0;
180 unsigned isl_map_n_out(const struct isl_map
*map
)
182 return map
? map
->dim
->n_out
: 0;
185 unsigned isl_map_n_param(const struct isl_map
*map
)
187 return map
? map
->dim
->nparam
: 0;
190 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
195 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
198 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
199 set
->dim
, isl_dim_set
);
202 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
203 struct isl_basic_set
*bset
)
208 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
211 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
212 bset
->dim
, isl_dim_set
);
215 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
220 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
223 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
224 set
->dim
, isl_dim_set
);
227 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
228 struct isl_basic_set
*bset
)
233 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
236 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
237 bset
->dim
, isl_dim_set
);
240 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
242 return bmap
? bmap
->ctx
: NULL
;
245 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
247 return bset
? bset
->ctx
: NULL
;
250 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
252 return map
? map
->ctx
: NULL
;
255 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
257 return set
? set
->ctx
: NULL
;
260 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
264 return isl_space_copy(bmap
->dim
);
267 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
271 return isl_space_copy(bset
->dim
);
274 /* Extract the divs in "bmap" as a matrix.
276 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
287 ctx
= isl_basic_map_get_ctx(bmap
);
288 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
289 cols
= 1 + 1 + total
+ bmap
->n_div
;
290 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
294 for (i
= 0; i
< bmap
->n_div
; ++i
)
295 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
300 /* Extract the divs in "bset" as a matrix.
302 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
304 return isl_basic_map_get_divs(bset
);
307 __isl_give isl_local_space
*isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map
*bmap
)
315 div
= isl_basic_map_get_divs(bmap
);
316 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
319 __isl_give isl_local_space
*isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set
*bset
)
322 return isl_basic_map_get_local_space(bset
);
325 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
326 __isl_take isl_local_space
*ls
)
335 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
336 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
337 n_div
, 0, 2 * n_div
);
339 for (i
= 0; i
< n_div
; ++i
)
340 if (isl_basic_map_alloc_div(bmap
) < 0)
343 for (i
= 0; i
< n_div
; ++i
) {
344 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
345 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
349 isl_local_space_free(ls
);
352 isl_local_space_free(ls
);
353 isl_basic_map_free(bmap
);
357 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
358 __isl_take isl_local_space
*ls
)
360 return isl_basic_map_from_local_space(ls
);
363 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
367 return isl_space_copy(map
->dim
);
370 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
374 return isl_space_copy(set
->dim
);
377 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
378 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
380 bmap
= isl_basic_map_cow(bmap
);
383 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
386 bmap
= isl_basic_map_finalize(bmap
);
389 isl_basic_map_free(bmap
);
393 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
394 __isl_take isl_basic_set
*bset
, const char *s
)
396 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
399 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
400 enum isl_dim_type type
)
402 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
405 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
406 enum isl_dim_type type
, const char *s
)
410 map
= isl_map_cow(map
);
414 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
418 for (i
= 0; i
< map
->n
; ++i
) {
419 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
430 /* Replace the identifier of the tuple of type "type" by "id".
432 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
433 __isl_take isl_basic_map
*bmap
,
434 enum isl_dim_type type
, __isl_take isl_id
*id
)
436 bmap
= isl_basic_map_cow(bmap
);
439 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
441 return isl_basic_map_free(bmap
);
442 bmap
= isl_basic_map_finalize(bmap
);
449 /* Replace the identifier of the tuple by "id".
451 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
452 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
454 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
457 /* Does the input or output tuple have a name?
459 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
461 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
464 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
465 enum isl_dim_type type
)
467 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
470 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
473 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
476 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
477 enum isl_dim_type type
, __isl_take isl_id
*id
)
479 map
= isl_map_cow(map
);
483 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
485 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
491 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
492 __isl_take isl_id
*id
)
494 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
497 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
498 enum isl_dim_type type
)
500 map
= isl_map_cow(map
);
504 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
506 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
509 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
511 return isl_map_reset_tuple_id(set
, isl_dim_set
);
514 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
516 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
519 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
520 enum isl_dim_type type
)
522 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
525 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
527 return isl_map_has_tuple_id(set
, isl_dim_set
);
530 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
532 return isl_map_get_tuple_id(set
, isl_dim_set
);
535 /* Does the set tuple have a name?
537 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
540 return isl_bool_error
;
541 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
545 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
547 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
550 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
552 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
555 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
556 enum isl_dim_type type
, unsigned pos
)
558 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
561 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
562 enum isl_dim_type type
, unsigned pos
)
564 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
567 /* Does the given dimension have a name?
569 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
570 enum isl_dim_type type
, unsigned pos
)
573 return isl_bool_error
;
574 return isl_space_has_dim_name(map
->dim
, type
, pos
);
577 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
578 enum isl_dim_type type
, unsigned pos
)
580 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
583 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
584 enum isl_dim_type type
, unsigned pos
)
586 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
589 /* Does the given dimension have a name?
591 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
592 enum isl_dim_type type
, unsigned pos
)
595 return isl_bool_error
;
596 return isl_space_has_dim_name(set
->dim
, type
, pos
);
599 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
600 __isl_take isl_basic_map
*bmap
,
601 enum isl_dim_type type
, unsigned pos
, const char *s
)
603 bmap
= isl_basic_map_cow(bmap
);
606 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
609 return isl_basic_map_finalize(bmap
);
611 isl_basic_map_free(bmap
);
615 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
616 enum isl_dim_type type
, unsigned pos
, const char *s
)
620 map
= isl_map_cow(map
);
624 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
628 for (i
= 0; i
< map
->n
; ++i
) {
629 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
640 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
641 __isl_take isl_basic_set
*bset
,
642 enum isl_dim_type type
, unsigned pos
, const char *s
)
644 return (isl_basic_set
*)isl_basic_map_set_dim_name(
645 (isl_basic_map
*)bset
, type
, pos
, s
);
648 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
649 enum isl_dim_type type
, unsigned pos
, const char *s
)
651 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
654 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
655 enum isl_dim_type type
, unsigned pos
)
658 return isl_bool_error
;
659 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
662 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
663 enum isl_dim_type type
, unsigned pos
)
665 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
668 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
669 enum isl_dim_type type
, unsigned pos
)
671 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
674 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
675 enum isl_dim_type type
, unsigned pos
)
677 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
680 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
681 enum isl_dim_type type
, unsigned pos
)
683 return isl_map_has_dim_id(set
, type
, pos
);
686 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
687 enum isl_dim_type type
, unsigned pos
)
689 return isl_map_get_dim_id(set
, type
, pos
);
692 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
693 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
695 map
= isl_map_cow(map
);
699 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
701 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
707 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
708 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
710 return isl_map_set_dim_id(set
, type
, pos
, id
);
713 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
714 __isl_keep isl_id
*id
)
718 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
721 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
722 __isl_keep isl_id
*id
)
724 return isl_map_find_dim_by_id(set
, type
, id
);
727 /* Return the position of the dimension of the given type and name
729 * Return -1 if no such dimension can be found.
731 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
732 enum isl_dim_type type
, const char *name
)
736 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
739 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
744 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
747 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
750 return isl_map_find_dim_by_name(set
, type
, name
);
753 /* Reset the user pointer on all identifiers of parameters and tuples
754 * of the space of "map".
756 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
760 space
= isl_map_get_space(map
);
761 space
= isl_space_reset_user(space
);
762 map
= isl_map_reset_space(map
, space
);
767 /* Reset the user pointer on all identifiers of parameters and tuples
768 * of the space of "set".
770 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
772 return isl_map_reset_user(set
);
775 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
779 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
782 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
784 return isl_basic_map_is_rational(bset
);
787 /* Does "bmap" contain any rational points?
789 * If "bmap" has an equality for each dimension, equating the dimension
790 * to an integer constant, then it has no rational points, even if it
791 * is marked as rational.
793 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
795 int has_rational
= 1;
800 if (isl_basic_map_plain_is_empty(bmap
))
802 if (!isl_basic_map_is_rational(bmap
))
804 bmap
= isl_basic_map_copy(bmap
);
805 bmap
= isl_basic_map_implicit_equalities(bmap
);
808 total
= isl_basic_map_total_dim(bmap
);
809 if (bmap
->n_eq
== total
) {
811 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
812 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
815 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
816 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
818 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
826 isl_basic_map_free(bmap
);
831 /* Does "map" contain any rational points?
833 int isl_map_has_rational(__isl_keep isl_map
*map
)
840 for (i
= 0; i
< map
->n
; ++i
) {
841 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
842 if (has_rational
< 0)
850 /* Does "set" contain any rational points?
852 int isl_set_has_rational(__isl_keep isl_set
*set
)
854 return isl_map_has_rational(set
);
857 /* Is this basic set a parameter domain?
859 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
863 return isl_space_is_params(bset
->dim
);
866 /* Is this set a parameter domain?
868 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
871 return isl_bool_error
;
872 return isl_space_is_params(set
->dim
);
875 /* Is this map actually a parameter domain?
876 * Users should never call this function. Outside of isl,
877 * a map can never be a parameter domain.
879 int isl_map_is_params(__isl_keep isl_map
*map
)
883 return isl_space_is_params(map
->dim
);
886 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
887 struct isl_basic_map
*bmap
, unsigned extra
,
888 unsigned n_eq
, unsigned n_ineq
)
891 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
896 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
897 if (isl_blk_is_error(bmap
->block
))
900 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
901 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
905 bmap
->block2
= isl_blk_empty();
908 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
909 if (isl_blk_is_error(bmap
->block2
))
912 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
917 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
918 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
920 for (i
= 0; i
< extra
; ++i
)
921 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
925 bmap
->c_size
= n_eq
+ n_ineq
;
926 bmap
->eq
= bmap
->ineq
+ n_ineq
;
935 isl_basic_map_free(bmap
);
939 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
940 unsigned nparam
, unsigned dim
, unsigned extra
,
941 unsigned n_eq
, unsigned n_ineq
)
943 struct isl_basic_map
*bmap
;
946 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
950 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
951 return (struct isl_basic_set
*)bmap
;
954 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
955 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
957 struct isl_basic_map
*bmap
;
960 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
961 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
962 return (struct isl_basic_set
*)bmap
;
968 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
969 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
971 struct isl_basic_map
*bmap
;
975 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
980 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
986 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
987 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
988 unsigned n_eq
, unsigned n_ineq
)
990 struct isl_basic_map
*bmap
;
993 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
997 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1001 static void dup_constraints(
1002 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1005 unsigned total
= isl_basic_map_total_dim(src
);
1007 for (i
= 0; i
< src
->n_eq
; ++i
) {
1008 int j
= isl_basic_map_alloc_equality(dst
);
1009 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1012 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1013 int j
= isl_basic_map_alloc_inequality(dst
);
1014 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1017 for (i
= 0; i
< src
->n_div
; ++i
) {
1018 int j
= isl_basic_map_alloc_div(dst
);
1019 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1021 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1024 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1026 struct isl_basic_map
*dup
;
1030 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1031 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1034 dup_constraints(dup
, bmap
);
1035 dup
->flags
= bmap
->flags
;
1036 dup
->sample
= isl_vec_copy(bmap
->sample
);
1040 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1042 struct isl_basic_map
*dup
;
1044 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1045 return (struct isl_basic_set
*)dup
;
1048 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1053 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1057 return isl_basic_set_dup(bset
);
1060 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1069 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1074 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1078 bmap
= isl_basic_map_dup(bmap
);
1080 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1084 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1093 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1098 if (--bmap
->ref
> 0)
1101 isl_ctx_deref(bmap
->ctx
);
1103 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1105 isl_blk_free(bmap
->ctx
, bmap
->block
);
1106 isl_vec_free(bmap
->sample
);
1107 isl_space_free(bmap
->dim
);
1113 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1115 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1118 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1120 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1123 __isl_give isl_map
*isl_map_align_params_map_map_and(
1124 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1125 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1126 __isl_take isl_map
*map2
))
1130 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1131 return fn(map1
, map2
);
1132 if (!isl_space_has_named_params(map1
->dim
) ||
1133 !isl_space_has_named_params(map2
->dim
))
1134 isl_die(map1
->ctx
, isl_error_invalid
,
1135 "unaligned unnamed parameters", goto error
);
1136 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1137 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1138 return fn(map1
, map2
);
1145 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1146 __isl_keep isl_map
*map2
,
1147 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1152 return isl_bool_error
;
1153 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1154 return fn(map1
, map2
);
1155 if (!isl_space_has_named_params(map1
->dim
) ||
1156 !isl_space_has_named_params(map2
->dim
))
1157 isl_die(map1
->ctx
, isl_error_invalid
,
1158 "unaligned unnamed parameters", return isl_bool_error
);
1159 map1
= isl_map_copy(map1
);
1160 map2
= isl_map_copy(map2
);
1161 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1162 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1169 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1171 struct isl_ctx
*ctx
;
1175 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1176 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1178 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1179 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1180 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1181 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1182 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1183 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1185 int j
= isl_basic_map_alloc_inequality(bmap
);
1189 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1190 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1197 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1198 bmap
->extra
- bmap
->n_div
);
1199 return bmap
->n_eq
++;
1202 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1204 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1207 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1211 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1216 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1218 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1221 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1226 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1228 if (pos
!= bmap
->n_eq
- 1) {
1230 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1231 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1237 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1239 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1242 /* Turn inequality "pos" of "bmap" into an equality.
1244 * In particular, we move the inequality in front of the equalities
1245 * and move the last inequality in the position of the moved inequality.
1246 * Note that isl_tab_make_equalities_explicit depends on this particular
1247 * change in the ordering of the constraints.
1249 void isl_basic_map_inequality_to_equality(
1250 struct isl_basic_map
*bmap
, unsigned pos
)
1254 t
= bmap
->ineq
[pos
];
1255 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1256 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1261 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1262 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1263 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1264 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1267 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1269 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1272 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1274 struct isl_ctx
*ctx
;
1278 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1279 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1280 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1281 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1282 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1283 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1284 1 + isl_basic_map_total_dim(bmap
),
1285 bmap
->extra
- bmap
->n_div
);
1286 return bmap
->n_ineq
++;
1289 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1291 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1294 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1298 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1303 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1305 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1308 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1313 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1315 if (pos
!= bmap
->n_ineq
- 1) {
1316 t
= bmap
->ineq
[pos
];
1317 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1318 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1319 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1325 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1327 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1330 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1335 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1338 k
= isl_basic_map_alloc_equality(bmap
);
1341 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1344 isl_basic_map_free(bmap
);
1348 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1351 return (isl_basic_set
*)
1352 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1355 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1360 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1363 k
= isl_basic_map_alloc_inequality(bmap
);
1366 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1369 isl_basic_map_free(bmap
);
1373 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1376 return (isl_basic_set
*)
1377 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1380 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1384 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1385 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1386 1 + 1 + isl_basic_map_total_dim(bmap
),
1387 bmap
->extra
- bmap
->n_div
);
1388 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1389 return bmap
->n_div
++;
1392 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1394 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1397 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1401 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1406 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1408 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1411 /* Copy constraint from src to dst, putting the vars of src at offset
1412 * dim_off in dst and the divs of src at offset div_off in dst.
1413 * If both sets are actually map, then dim_off applies to the input
1416 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1417 struct isl_basic_map
*src_map
, isl_int
*src
,
1418 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1420 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1421 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1422 unsigned src_in
= isl_basic_map_n_in(src_map
);
1423 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1424 unsigned src_out
= isl_basic_map_n_out(src_map
);
1425 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1426 isl_int_set(dst
[0], src
[0]);
1427 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1428 if (dst_nparam
> src_nparam
)
1429 isl_seq_clr(dst
+1+src_nparam
,
1430 dst_nparam
- src_nparam
);
1431 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1432 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1434 isl_min(dst_in
-in_off
, src_in
));
1435 if (dst_in
-in_off
> src_in
)
1436 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1437 dst_in
- in_off
- src_in
);
1438 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1439 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1440 src
+1+src_nparam
+src_in
,
1441 isl_min(dst_out
-out_off
, src_out
));
1442 if (dst_out
-out_off
> src_out
)
1443 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1444 dst_out
- out_off
- src_out
);
1445 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1446 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1447 src
+1+src_nparam
+src_in
+src_out
,
1448 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1449 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1450 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1451 div_off
+src_map
->n_div
,
1452 dst_map
->n_div
- div_off
- src_map
->n_div
);
1455 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1456 struct isl_basic_map
*src_map
, isl_int
*src
,
1457 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1459 isl_int_set(dst
[0], src
[0]);
1460 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1463 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1464 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1469 if (!bmap1
|| !bmap2
)
1472 div_off
= bmap1
->n_div
;
1474 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1475 int i1
= isl_basic_map_alloc_equality(bmap1
);
1478 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1479 i_pos
, o_pos
, div_off
);
1482 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1483 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1486 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1487 i_pos
, o_pos
, div_off
);
1490 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1491 int i1
= isl_basic_map_alloc_div(bmap1
);
1494 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1495 i_pos
, o_pos
, div_off
);
1498 isl_basic_map_free(bmap2
);
1503 isl_basic_map_free(bmap1
);
1504 isl_basic_map_free(bmap2
);
1508 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1509 struct isl_basic_set
*bset2
, unsigned pos
)
1511 return (struct isl_basic_set
*)
1512 add_constraints((struct isl_basic_map
*)bset1
,
1513 (struct isl_basic_map
*)bset2
, 0, pos
);
1516 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1517 __isl_take isl_space
*dim
, unsigned extra
,
1518 unsigned n_eq
, unsigned n_ineq
)
1520 struct isl_basic_map
*ext
;
1530 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1531 base
->extra
>= base
->n_div
+ extra
;
1533 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1534 room_for_ineq(base
, n_ineq
)) {
1535 isl_space_free(dim
);
1539 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1540 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1541 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1542 extra
+= base
->extra
;
1544 n_ineq
+= base
->n_ineq
;
1546 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1552 ext
->sample
= isl_vec_copy(base
->sample
);
1553 flags
= base
->flags
;
1554 ext
= add_constraints(ext
, base
, 0, 0);
1557 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1563 isl_space_free(dim
);
1564 isl_basic_map_free(base
);
1568 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1569 __isl_take isl_space
*dim
, unsigned extra
,
1570 unsigned n_eq
, unsigned n_ineq
)
1572 return (struct isl_basic_set
*)
1573 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1574 extra
, n_eq
, n_ineq
);
1577 struct isl_basic_map
*isl_basic_map_extend_constraints(
1578 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1582 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1586 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1587 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1588 unsigned n_eq
, unsigned n_ineq
)
1590 struct isl_basic_map
*bmap
;
1595 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1599 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1602 isl_basic_map_free(base
);
1606 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1607 unsigned nparam
, unsigned dim
, unsigned extra
,
1608 unsigned n_eq
, unsigned n_ineq
)
1610 return (struct isl_basic_set
*)
1611 isl_basic_map_extend((struct isl_basic_map
*)base
,
1612 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1615 struct isl_basic_set
*isl_basic_set_extend_constraints(
1616 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1618 return (struct isl_basic_set
*)
1619 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1623 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1625 return (struct isl_basic_set
*)
1626 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1629 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1634 if (bmap
->ref
> 1) {
1636 bmap
= isl_basic_map_dup(bmap
);
1639 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1640 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1645 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1653 return isl_set_dup(set
);
1656 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1664 return isl_map_dup(map
);
1667 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1668 unsigned a_len
, unsigned b_len
)
1670 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1671 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1672 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1675 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1676 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1684 isl_assert(bmap
->ctx
,
1685 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1687 if (n1
== 0 || n2
== 0)
1690 bmap
= isl_basic_map_cow(bmap
);
1694 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1695 if (isl_blk_is_error(blk
))
1698 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1700 bmap
->eq
[i
] + pos
, n1
, n2
);
1702 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1704 bmap
->ineq
[i
] + pos
, n1
, n2
);
1706 for (i
= 0; i
< bmap
->n_div
; ++i
)
1708 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1710 isl_blk_free(bmap
->ctx
, blk
);
1712 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1713 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1714 return isl_basic_map_finalize(bmap
);
1716 isl_basic_map_free(bmap
);
1720 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1726 total
= isl_basic_map_total_dim(bmap
);
1727 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1728 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1730 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1732 i
= isl_basic_map_alloc_equality(bmap
);
1736 isl_int_set_si(bmap
->eq
[i
][0], 1);
1737 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1738 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1739 isl_vec_free(bmap
->sample
);
1740 bmap
->sample
= NULL
;
1741 return isl_basic_map_finalize(bmap
);
1743 isl_basic_map_free(bmap
);
1747 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1749 return (struct isl_basic_set
*)
1750 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1753 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1756 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1758 isl_int
*t
= bmap
->div
[a
];
1759 bmap
->div
[a
] = bmap
->div
[b
];
1763 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1764 * div definitions accordingly.
1766 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1769 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1771 swap_div(bmap
, a
, b
);
1773 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1774 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1776 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1777 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1779 for (i
= 0; i
< bmap
->n_div
; ++i
)
1780 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1781 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1784 /* Eliminate the specified n dimensions starting at first from the
1785 * constraints, without removing the dimensions from the space.
1786 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1788 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1789 enum isl_dim_type type
, unsigned first
, unsigned n
)
1798 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1799 isl_die(map
->ctx
, isl_error_invalid
,
1800 "index out of bounds", goto error
);
1802 map
= isl_map_cow(map
);
1806 for (i
= 0; i
< map
->n
; ++i
) {
1807 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1817 /* Eliminate the specified n dimensions starting at first from the
1818 * constraints, without removing the dimensions from the space.
1819 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1821 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1822 enum isl_dim_type type
, unsigned first
, unsigned n
)
1824 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1827 /* Eliminate the specified n dimensions starting at first from the
1828 * constraints, without removing the dimensions from the space.
1829 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1831 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1832 unsigned first
, unsigned n
)
1834 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1837 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1838 __isl_take isl_basic_map
*bmap
)
1842 bmap
= isl_basic_map_eliminate_vars(bmap
,
1843 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1847 return isl_basic_map_finalize(bmap
);
1850 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1851 __isl_take isl_basic_set
*bset
)
1853 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1854 (struct isl_basic_map
*)bset
);
1857 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1866 map
= isl_map_cow(map
);
1870 for (i
= 0; i
< map
->n
; ++i
) {
1871 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1881 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1883 return isl_map_remove_divs(set
);
1886 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1887 enum isl_dim_type type
, unsigned first
, unsigned n
)
1891 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1893 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1895 bmap
= isl_basic_map_eliminate_vars(bmap
,
1896 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1899 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1901 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1904 isl_basic_map_free(bmap
);
1908 /* Return true if the definition of the given div (recursively) involves
1909 * any of the given variables.
1911 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1912 unsigned first
, unsigned n
)
1915 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1917 if (isl_int_is_zero(bmap
->div
[div
][0]))
1919 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1922 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1923 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1925 if (div_involves_vars(bmap
, i
, first
, n
))
1932 /* Try and add a lower and/or upper bound on "div" to "bmap"
1933 * based on inequality "i".
1934 * "total" is the total number of variables (excluding the divs).
1935 * "v" is a temporary object that can be used during the calculations.
1936 * If "lb" is set, then a lower bound should be constructed.
1937 * If "ub" is set, then an upper bound should be constructed.
1939 * The calling function has already checked that the inequality does not
1940 * reference "div", but we still need to check that the inequality is
1941 * of the right form. We'll consider the case where we want to construct
1942 * a lower bound. The construction of upper bounds is similar.
1944 * Let "div" be of the form
1946 * q = floor((a + f(x))/d)
1948 * We essentially check if constraint "i" is of the form
1952 * so that we can use it to derive a lower bound on "div".
1953 * However, we allow a slightly more general form
1957 * with the condition that the coefficients of g(x) - f(x) are all
1959 * Rewriting this constraint as
1963 * adding a + f(x) to both sides and dividing by d, we obtain
1965 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1967 * Taking the floor on both sides, we obtain
1969 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1973 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1975 * In the case of an upper bound, we construct the constraint
1977 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1980 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1981 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1982 unsigned total
, isl_int v
, int lb
, int ub
)
1986 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1988 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1989 bmap
->div
[div
][1 + 1 + j
]);
1990 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1993 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1994 bmap
->div
[div
][1 + 1 + j
]);
1995 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2001 bmap
= isl_basic_map_cow(bmap
);
2002 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2004 int k
= isl_basic_map_alloc_inequality(bmap
);
2007 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2008 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2009 bmap
->div
[div
][1 + j
]);
2010 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2011 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2013 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2016 int k
= isl_basic_map_alloc_inequality(bmap
);
2019 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2020 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2021 bmap
->div
[div
][1 + j
]);
2022 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2023 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2025 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2028 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2031 isl_basic_map_free(bmap
);
2035 /* This function is called right before "div" is eliminated from "bmap"
2036 * using Fourier-Motzkin.
2037 * Look through the constraints of "bmap" for constraints on the argument
2038 * of the integer division and use them to construct constraints on the
2039 * integer division itself. These constraints can then be combined
2040 * during the Fourier-Motzkin elimination.
2041 * Note that it is only useful to introduce lower bounds on "div"
2042 * if "bmap" already contains upper bounds on "div" as the newly
2043 * introduce lower bounds can then be combined with the pre-existing
2044 * upper bounds. Similarly for upper bounds.
2045 * We therefore first check if "bmap" contains any lower and/or upper bounds
2048 * It is interesting to note that the introduction of these constraints
2049 * can indeed lead to more accurate results, even when compared to
2050 * deriving constraints on the argument of "div" from constraints on "div".
2051 * Consider, for example, the set
2053 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2055 * The second constraint can be rewritten as
2057 * 2 * [(-i-2j+3)/4] + k >= 0
2059 * from which we can derive
2061 * -i - 2j + 3 >= -2k
2067 * Combined with the first constraint, we obtain
2069 * -3 <= 3 + 2k or k >= -3
2071 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2072 * the first constraint, we obtain
2074 * [(i + 2j)/4] >= [-3/4] = -1
2076 * Combining this constraint with the second constraint, we obtain
2080 static __isl_give isl_basic_map
*insert_bounds_on_div(
2081 __isl_take isl_basic_map
*bmap
, int div
)
2084 int check_lb
, check_ub
;
2091 if (isl_int_is_zero(bmap
->div
[div
][0]))
2094 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2098 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2099 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2106 if (!check_lb
&& !check_ub
)
2111 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2112 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2115 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2116 check_lb
, check_ub
);
2124 /* Remove all divs (recursively) involving any of the given dimensions
2125 * in their definitions.
2127 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2128 __isl_take isl_basic_map
*bmap
,
2129 enum isl_dim_type type
, unsigned first
, unsigned n
)
2135 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2137 first
+= isl_basic_map_offset(bmap
, type
);
2139 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2140 if (!div_involves_vars(bmap
, i
, first
, n
))
2142 bmap
= insert_bounds_on_div(bmap
, i
);
2143 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2151 isl_basic_map_free(bmap
);
2155 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2156 __isl_take isl_basic_set
*bset
,
2157 enum isl_dim_type type
, unsigned first
, unsigned n
)
2159 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2162 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2163 enum isl_dim_type type
, unsigned first
, unsigned n
)
2172 map
= isl_map_cow(map
);
2176 for (i
= 0; i
< map
->n
; ++i
) {
2177 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2188 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2189 enum isl_dim_type type
, unsigned first
, unsigned n
)
2191 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2195 /* Does the desciption of "bmap" depend on the specified dimensions?
2196 * We also check whether the dimensions appear in any of the div definitions.
2197 * In principle there is no need for this check. If the dimensions appear
2198 * in a div definition, they also appear in the defining constraints of that
2201 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2202 enum isl_dim_type type
, unsigned first
, unsigned n
)
2207 return isl_bool_error
;
2209 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2210 isl_die(bmap
->ctx
, isl_error_invalid
,
2211 "index out of bounds", return isl_bool_error
);
2213 first
+= isl_basic_map_offset(bmap
, type
);
2214 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2215 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2216 return isl_bool_true
;
2217 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2218 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2219 return isl_bool_true
;
2220 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2221 if (isl_int_is_zero(bmap
->div
[i
][0]))
2223 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2224 return isl_bool_true
;
2227 return isl_bool_false
;
2230 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2231 enum isl_dim_type type
, unsigned first
, unsigned n
)
2236 return isl_bool_error
;
2238 if (first
+ n
> isl_map_dim(map
, type
))
2239 isl_die(map
->ctx
, isl_error_invalid
,
2240 "index out of bounds", return isl_bool_error
);
2242 for (i
= 0; i
< map
->n
; ++i
) {
2243 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2245 if (involves
< 0 || involves
)
2249 return isl_bool_false
;
2252 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2253 enum isl_dim_type type
, unsigned first
, unsigned n
)
2255 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2258 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2259 enum isl_dim_type type
, unsigned first
, unsigned n
)
2261 return isl_map_involves_dims(set
, type
, first
, n
);
2264 /* Return true if the definition of the given div is unknown or depends
2267 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2270 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2272 if (isl_int_is_zero(bmap
->div
[div
][0]))
2275 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2276 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2278 if (div_is_unknown(bmap
, i
))
2285 /* Remove all divs that are unknown or defined in terms of unknown divs.
2287 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2288 __isl_take isl_basic_map
*bmap
)
2295 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2296 if (!div_is_unknown(bmap
, i
))
2298 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2307 /* Remove all divs that are unknown or defined in terms of unknown divs.
2309 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2310 __isl_take isl_basic_set
*bset
)
2312 return isl_basic_map_remove_unknown_divs(bset
);
2315 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2324 map
= isl_map_cow(map
);
2328 for (i
= 0; i
< map
->n
; ++i
) {
2329 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2339 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2341 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2344 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2345 __isl_take isl_basic_set
*bset
,
2346 enum isl_dim_type type
, unsigned first
, unsigned n
)
2348 return (isl_basic_set
*)
2349 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2352 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2353 enum isl_dim_type type
, unsigned first
, unsigned n
)
2360 map
= isl_map_cow(map
);
2363 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2365 for (i
= 0; i
< map
->n
; ++i
) {
2366 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2367 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2371 map
= isl_map_drop(map
, type
, first
, n
);
2378 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2379 enum isl_dim_type type
, unsigned first
, unsigned n
)
2381 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2384 /* Project out n inputs starting at first using Fourier-Motzkin */
2385 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2386 unsigned first
, unsigned n
)
2388 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2391 static void dump_term(struct isl_basic_map
*bmap
,
2392 isl_int c
, int pos
, FILE *out
)
2395 unsigned in
= isl_basic_map_n_in(bmap
);
2396 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2397 unsigned nparam
= isl_basic_map_n_param(bmap
);
2399 isl_int_print(out
, c
, 0);
2401 if (!isl_int_is_one(c
))
2402 isl_int_print(out
, c
, 0);
2403 if (pos
< 1 + nparam
) {
2404 name
= isl_space_get_dim_name(bmap
->dim
,
2405 isl_dim_param
, pos
- 1);
2407 fprintf(out
, "%s", name
);
2409 fprintf(out
, "p%d", pos
- 1);
2410 } else if (pos
< 1 + nparam
+ in
)
2411 fprintf(out
, "i%d", pos
- 1 - nparam
);
2412 else if (pos
< 1 + nparam
+ dim
)
2413 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2415 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2419 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2420 int sign
, FILE *out
)
2424 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2428 for (i
= 0, first
= 1; i
< len
; ++i
) {
2429 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2432 fprintf(out
, " + ");
2434 isl_int_abs(v
, c
[i
]);
2435 dump_term(bmap
, v
, i
, out
);
2442 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2443 const char *op
, FILE *out
, int indent
)
2447 fprintf(out
, "%*s", indent
, "");
2449 dump_constraint_sign(bmap
, c
, 1, out
);
2450 fprintf(out
, " %s ", op
);
2451 dump_constraint_sign(bmap
, c
, -1, out
);
2455 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2456 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2458 fprintf(out
, "%*s", indent
, "");
2459 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2464 static void dump_constraints(struct isl_basic_map
*bmap
,
2465 isl_int
**c
, unsigned n
,
2466 const char *op
, FILE *out
, int indent
)
2470 for (i
= 0; i
< n
; ++i
)
2471 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2474 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2478 unsigned total
= isl_basic_map_total_dim(bmap
);
2480 for (j
= 0; j
< 1 + total
; ++j
) {
2481 if (isl_int_is_zero(exp
[j
]))
2483 if (!first
&& isl_int_is_pos(exp
[j
]))
2485 dump_term(bmap
, exp
[j
], j
, out
);
2490 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2494 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2495 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2497 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2498 fprintf(out
, "%*s", indent
, "");
2499 fprintf(out
, "e%d = [(", i
);
2500 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2502 isl_int_print(out
, bmap
->div
[i
][0], 0);
2503 fprintf(out
, "]\n");
2507 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2508 FILE *out
, int indent
)
2511 fprintf(out
, "null basic set\n");
2515 fprintf(out
, "%*s", indent
, "");
2516 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2517 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2518 bset
->extra
, bset
->flags
);
2519 dump((struct isl_basic_map
*)bset
, out
, indent
);
2522 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2523 FILE *out
, int indent
)
2526 fprintf(out
, "null basic map\n");
2530 fprintf(out
, "%*s", indent
, "");
2531 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2532 "flags: %x, n_name: %d\n",
2534 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2535 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2536 dump(bmap
, out
, indent
);
2539 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2544 total
= isl_basic_map_total_dim(bmap
);
2545 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2546 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2547 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2548 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2552 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2555 struct isl_set
*set
;
2559 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2560 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2561 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2562 sizeof(struct isl_set
) +
2563 (n
- 1) * sizeof(struct isl_basic_set
*));
2567 set
->ctx
= dim
->ctx
;
2568 isl_ctx_ref(set
->ctx
);
2576 isl_space_free(dim
);
2580 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2581 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2583 struct isl_set
*set
;
2586 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2590 set
= isl_set_alloc_space(dims
, n
, flags
);
2594 /* Make sure "map" has room for at least "n" more basic maps.
2596 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2599 struct isl_map
*grown
= NULL
;
2603 isl_assert(map
->ctx
, n
>= 0, goto error
);
2604 if (map
->n
+ n
<= map
->size
)
2606 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2609 for (i
= 0; i
< map
->n
; ++i
) {
2610 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2618 isl_map_free(grown
);
2623 /* Make sure "set" has room for at least "n" more basic sets.
2625 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2627 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2630 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2633 struct isl_set
*dup
;
2638 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2641 for (i
= 0; i
< set
->n
; ++i
)
2642 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2646 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2648 return isl_map_from_basic_map(bset
);
2651 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2653 struct isl_map
*map
;
2658 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2659 return isl_map_add_basic_map(map
, bmap
);
2662 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2663 __isl_take isl_basic_set
*bset
)
2665 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2666 (struct isl_basic_map
*)bset
);
2669 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2679 isl_ctx_deref(set
->ctx
);
2680 for (i
= 0; i
< set
->n
; ++i
)
2681 isl_basic_set_free(set
->p
[i
]);
2682 isl_space_free(set
->dim
);
2688 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2693 fprintf(out
, "null set\n");
2697 fprintf(out
, "%*s", indent
, "");
2698 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2699 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2701 for (i
= 0; i
< set
->n
; ++i
) {
2702 fprintf(out
, "%*s", indent
, "");
2703 fprintf(out
, "basic set %d:\n", i
);
2704 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2708 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2713 fprintf(out
, "null map\n");
2717 fprintf(out
, "%*s", indent
, "");
2718 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2719 "flags: %x, n_name: %d\n",
2720 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2721 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2722 for (i
= 0; i
< map
->n
; ++i
) {
2723 fprintf(out
, "%*s", indent
, "");
2724 fprintf(out
, "basic map %d:\n", i
);
2725 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2729 struct isl_basic_map
*isl_basic_map_intersect_domain(
2730 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2732 struct isl_basic_map
*bmap_domain
;
2737 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2738 bset
->dim
, isl_dim_param
), goto error
);
2740 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2741 isl_assert(bset
->ctx
,
2742 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2744 bmap
= isl_basic_map_cow(bmap
);
2747 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2748 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2749 bmap_domain
= isl_basic_map_from_domain(bset
);
2750 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2752 bmap
= isl_basic_map_simplify(bmap
);
2753 return isl_basic_map_finalize(bmap
);
2755 isl_basic_map_free(bmap
);
2756 isl_basic_set_free(bset
);
2760 struct isl_basic_map
*isl_basic_map_intersect_range(
2761 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2763 struct isl_basic_map
*bmap_range
;
2768 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2769 bset
->dim
, isl_dim_param
), goto error
);
2771 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2772 isl_assert(bset
->ctx
,
2773 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2775 if (isl_basic_set_is_universe(bset
)) {
2776 isl_basic_set_free(bset
);
2780 bmap
= isl_basic_map_cow(bmap
);
2783 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2784 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2785 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2786 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2788 bmap
= isl_basic_map_simplify(bmap
);
2789 return isl_basic_map_finalize(bmap
);
2791 isl_basic_map_free(bmap
);
2792 isl_basic_set_free(bset
);
2796 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2797 __isl_keep isl_vec
*vec
)
2804 return isl_bool_error
;
2806 total
= 1 + isl_basic_map_total_dim(bmap
);
2807 if (total
!= vec
->size
)
2808 return isl_bool_error
;
2812 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2813 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2814 if (!isl_int_is_zero(s
)) {
2816 return isl_bool_false
;
2820 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2821 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2822 if (isl_int_is_neg(s
)) {
2824 return isl_bool_false
;
2830 return isl_bool_true
;
2833 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2834 __isl_keep isl_vec
*vec
)
2836 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2839 struct isl_basic_map
*isl_basic_map_intersect(
2840 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2842 struct isl_vec
*sample
= NULL
;
2844 if (!bmap1
|| !bmap2
)
2847 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2848 bmap2
->dim
, isl_dim_param
), goto error
);
2849 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2850 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2851 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2852 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2853 return isl_basic_map_intersect(bmap2
, bmap1
);
2855 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2856 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2857 isl_assert(bmap1
->ctx
,
2858 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2860 if (isl_basic_map_plain_is_empty(bmap1
)) {
2861 isl_basic_map_free(bmap2
);
2864 if (isl_basic_map_plain_is_empty(bmap2
)) {
2865 isl_basic_map_free(bmap1
);
2869 if (bmap1
->sample
&&
2870 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2871 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2872 sample
= isl_vec_copy(bmap1
->sample
);
2873 else if (bmap2
->sample
&&
2874 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2875 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2876 sample
= isl_vec_copy(bmap2
->sample
);
2878 bmap1
= isl_basic_map_cow(bmap1
);
2881 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2882 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2883 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2886 isl_vec_free(sample
);
2888 isl_vec_free(bmap1
->sample
);
2889 bmap1
->sample
= sample
;
2892 bmap1
= isl_basic_map_simplify(bmap1
);
2893 return isl_basic_map_finalize(bmap1
);
2896 isl_vec_free(sample
);
2897 isl_basic_map_free(bmap1
);
2898 isl_basic_map_free(bmap2
);
2902 struct isl_basic_set
*isl_basic_set_intersect(
2903 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2905 return (struct isl_basic_set
*)
2906 isl_basic_map_intersect(
2907 (struct isl_basic_map
*)bset1
,
2908 (struct isl_basic_map
*)bset2
);
2911 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2912 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2914 return isl_basic_set_intersect(bset1
, bset2
);
2917 /* Special case of isl_map_intersect, where both map1 and map2
2918 * are convex, without any divs and such that either map1 or map2
2919 * contains a single constraint. This constraint is then simply
2920 * added to the other map.
2922 static __isl_give isl_map
*map_intersect_add_constraint(
2923 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2925 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2926 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2927 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2928 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2930 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2931 return isl_map_intersect(map2
, map1
);
2933 isl_assert(map2
->ctx
,
2934 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2936 map1
= isl_map_cow(map1
);
2939 if (isl_map_plain_is_empty(map1
)) {
2943 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2944 if (map2
->p
[0]->n_eq
== 1)
2945 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2947 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2948 map2
->p
[0]->ineq
[0]);
2950 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2951 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2955 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2956 isl_basic_map_free(map1
->p
[0]);
2969 /* map2 may be either a parameter domain or a map living in the same
2972 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2973 __isl_take isl_map
*map2
)
2982 if ((isl_map_plain_is_empty(map1
) ||
2983 isl_map_plain_is_universe(map2
)) &&
2984 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2988 if ((isl_map_plain_is_empty(map2
) ||
2989 isl_map_plain_is_universe(map1
)) &&
2990 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2995 if (map1
->n
== 1 && map2
->n
== 1 &&
2996 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2997 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2998 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2999 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3000 return map_intersect_add_constraint(map1
, map2
);
3002 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3003 isl_space_dim(map2
->dim
, isl_dim_param
))
3004 isl_assert(map1
->ctx
,
3005 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3007 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3008 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3009 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3011 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3012 map1
->n
* map2
->n
, flags
);
3015 for (i
= 0; i
< map1
->n
; ++i
)
3016 for (j
= 0; j
< map2
->n
; ++j
) {
3017 struct isl_basic_map
*part
;
3018 part
= isl_basic_map_intersect(
3019 isl_basic_map_copy(map1
->p
[i
]),
3020 isl_basic_map_copy(map2
->p
[j
]));
3021 if (isl_basic_map_is_empty(part
) < 0)
3022 part
= isl_basic_map_free(part
);
3023 result
= isl_map_add_basic_map(result
, part
);
3036 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3037 __isl_take isl_map
*map2
)
3041 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3042 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3043 "spaces don't match", goto error
);
3044 return map_intersect_internal(map1
, map2
);
3051 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3052 __isl_take isl_map
*map2
)
3054 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3057 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3059 return (struct isl_set
*)
3060 isl_map_intersect((struct isl_map
*)set1
,
3061 (struct isl_map
*)set2
);
3064 /* map_intersect_internal accepts intersections
3065 * with parameter domains, so we can just call that function.
3067 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3068 __isl_take isl_set
*params
)
3070 return map_intersect_internal(map
, params
);
3073 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3074 __isl_take isl_map
*map2
)
3076 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3079 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3080 __isl_take isl_set
*params
)
3082 return isl_map_intersect_params(set
, params
);
3085 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3088 unsigned pos
, n1
, n2
;
3092 bmap
= isl_basic_map_cow(bmap
);
3095 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3096 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3097 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3098 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3099 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3100 return isl_basic_map_reset_space(bmap
, space
);
3103 static __isl_give isl_basic_map
*basic_map_space_reset(
3104 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3110 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3113 space
= isl_basic_map_get_space(bmap
);
3114 space
= isl_space_reset(space
, type
);
3115 bmap
= isl_basic_map_reset_space(bmap
, space
);
3119 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3120 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3121 unsigned pos
, unsigned n
)
3124 struct isl_basic_map
*res
;
3125 struct isl_dim_map
*dim_map
;
3126 unsigned total
, off
;
3127 enum isl_dim_type t
;
3130 return basic_map_space_reset(bmap
, type
);
3135 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3137 total
= isl_basic_map_total_dim(bmap
) + n
;
3138 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3140 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3142 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3144 unsigned size
= isl_basic_map_dim(bmap
, t
);
3145 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3147 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3148 pos
, size
- pos
, off
+ pos
+ n
);
3150 off
+= isl_space_dim(res_dim
, t
);
3152 isl_dim_map_div(dim_map
, bmap
, off
);
3154 res
= isl_basic_map_alloc_space(res_dim
,
3155 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3156 if (isl_basic_map_is_rational(bmap
))
3157 res
= isl_basic_map_set_rational(res
);
3158 if (isl_basic_map_plain_is_empty(bmap
)) {
3159 isl_basic_map_free(bmap
);
3161 return isl_basic_map_set_to_empty(res
);
3163 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3164 return isl_basic_map_finalize(res
);
3167 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3168 __isl_take isl_basic_set
*bset
,
3169 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3171 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3174 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3175 enum isl_dim_type type
, unsigned n
)
3179 return isl_basic_map_insert_dims(bmap
, type
,
3180 isl_basic_map_dim(bmap
, type
), n
);
3183 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3184 enum isl_dim_type type
, unsigned n
)
3188 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3189 return isl_basic_map_add_dims(bset
, type
, n
);
3191 isl_basic_set_free(bset
);
3195 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3196 enum isl_dim_type type
)
3200 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3203 space
= isl_map_get_space(map
);
3204 space
= isl_space_reset(space
, type
);
3205 map
= isl_map_reset_space(map
, space
);
3209 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3210 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3215 return map_space_reset(map
, type
);
3217 map
= isl_map_cow(map
);
3221 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3225 for (i
= 0; i
< map
->n
; ++i
) {
3226 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3237 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3238 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3240 return isl_map_insert_dims(set
, type
, pos
, n
);
3243 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3244 enum isl_dim_type type
, unsigned n
)
3248 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3251 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3252 enum isl_dim_type type
, unsigned n
)
3256 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3257 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3263 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3264 __isl_take isl_basic_map
*bmap
,
3265 enum isl_dim_type dst_type
, unsigned dst_pos
,
3266 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3268 struct isl_dim_map
*dim_map
;
3269 struct isl_basic_map
*res
;
3270 enum isl_dim_type t
;
3271 unsigned total
, off
;
3278 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3281 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3284 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3286 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3287 pos(bmap
->dim
, src_type
) + src_pos
+
3288 ((src_type
< dst_type
) ? n
: 0)) {
3289 bmap
= isl_basic_map_cow(bmap
);
3293 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3294 src_type
, src_pos
, n
);
3298 bmap
= isl_basic_map_finalize(bmap
);
3303 total
= isl_basic_map_total_dim(bmap
);
3304 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3307 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3308 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3309 if (t
== dst_type
) {
3310 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3313 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3316 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3317 dst_pos
, size
- dst_pos
, off
);
3318 off
+= size
- dst_pos
;
3319 } else if (t
== src_type
) {
3320 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3323 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3324 src_pos
+ n
, size
- src_pos
- n
, off
);
3325 off
+= size
- src_pos
- n
;
3327 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3331 isl_dim_map_div(dim_map
, bmap
, off
);
3333 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3334 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3335 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3339 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3340 src_type
, src_pos
, n
);
3344 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3345 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3346 bmap
= isl_basic_map_finalize(bmap
);
3350 isl_basic_map_free(bmap
);
3354 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3355 enum isl_dim_type dst_type
, unsigned dst_pos
,
3356 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3358 return (isl_basic_set
*)isl_basic_map_move_dims(
3359 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3362 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3363 enum isl_dim_type dst_type
, unsigned dst_pos
,
3364 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3368 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3369 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3370 src_type
, src_pos
, n
);
3376 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3377 enum isl_dim_type dst_type
, unsigned dst_pos
,
3378 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3387 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3390 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3393 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3395 map
= isl_map_cow(map
);
3399 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3403 for (i
= 0; i
< map
->n
; ++i
) {
3404 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3406 src_type
, src_pos
, n
);
3417 /* Move the specified dimensions to the last columns right before
3418 * the divs. Don't change the dimension specification of bmap.
3419 * That's the responsibility of the caller.
3421 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3422 enum isl_dim_type type
, unsigned first
, unsigned n
)
3424 struct isl_dim_map
*dim_map
;
3425 struct isl_basic_map
*res
;
3426 enum isl_dim_type t
;
3427 unsigned total
, off
;
3431 if (pos(bmap
->dim
, type
) + first
+ n
==
3432 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3435 total
= isl_basic_map_total_dim(bmap
);
3436 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3439 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3440 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3442 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3445 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3446 first
, n
, total
- bmap
->n_div
- n
);
3447 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3448 first
+ n
, size
- (first
+ n
), off
);
3449 off
+= size
- (first
+ n
);
3451 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3455 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3457 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3458 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3459 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3463 /* Insert "n" rows in the divs of "bmap".
3465 * The number of columns is not changed, which means that the last
3466 * dimensions of "bmap" are being reintepreted as the new divs.
3467 * The space of "bmap" is not adjusted, however, which means
3468 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3469 * from the space of "bmap" is the responsibility of the caller.
3471 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3479 bmap
= isl_basic_map_cow(bmap
);
3483 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3484 old
= bmap
->block2
.data
;
3485 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3486 (bmap
->extra
+ n
) * (1 + row_size
));
3487 if (!bmap
->block2
.data
)
3488 return isl_basic_map_free(bmap
);
3489 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3491 return isl_basic_map_free(bmap
);
3492 for (i
= 0; i
< n
; ++i
) {
3493 new_div
[i
] = bmap
->block2
.data
+
3494 (bmap
->extra
+ i
) * (1 + row_size
);
3495 isl_seq_clr(new_div
[i
], 1 + row_size
);
3497 for (i
= 0; i
< bmap
->extra
; ++i
)
3498 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3500 bmap
->div
= new_div
;
3507 /* Turn the n dimensions of type type, starting at first
3508 * into existentially quantified variables.
3510 __isl_give isl_basic_map
*isl_basic_map_project_out(
3511 __isl_take isl_basic_map
*bmap
,
3512 enum isl_dim_type type
, unsigned first
, unsigned n
)
3515 return basic_map_space_reset(bmap
, type
);
3520 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3521 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3523 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3526 bmap
= move_last(bmap
, type
, first
, n
);
3527 bmap
= isl_basic_map_cow(bmap
);
3528 bmap
= insert_div_rows(bmap
, n
);
3532 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3535 bmap
= isl_basic_map_simplify(bmap
);
3536 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3537 return isl_basic_map_finalize(bmap
);
3539 isl_basic_map_free(bmap
);
3543 /* Turn the n dimensions of type type, starting at first
3544 * into existentially quantified variables.
3546 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3547 enum isl_dim_type type
, unsigned first
, unsigned n
)
3549 return (isl_basic_set
*)isl_basic_map_project_out(
3550 (isl_basic_map
*)bset
, type
, first
, n
);
3553 /* Turn the n dimensions of type type, starting at first
3554 * into existentially quantified variables.
3556 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3557 enum isl_dim_type type
, unsigned first
, unsigned n
)
3565 return map_space_reset(map
, type
);
3567 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3569 map
= isl_map_cow(map
);
3573 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3577 for (i
= 0; i
< map
->n
; ++i
) {
3578 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3589 /* Turn the n dimensions of type type, starting at first
3590 * into existentially quantified variables.
3592 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3593 enum isl_dim_type type
, unsigned first
, unsigned n
)
3595 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3598 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3602 for (i
= 0; i
< n
; ++i
) {
3603 j
= isl_basic_map_alloc_div(bmap
);
3606 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3610 isl_basic_map_free(bmap
);
3614 struct isl_basic_map
*isl_basic_map_apply_range(
3615 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3617 isl_space
*dim_result
= NULL
;
3618 struct isl_basic_map
*bmap
;
3619 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3620 struct isl_dim_map
*dim_map1
, *dim_map2
;
3622 if (!bmap1
|| !bmap2
)
3624 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3625 bmap2
->dim
, isl_dim_param
))
3626 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3627 "parameters don't match", goto error
);
3628 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3629 bmap2
->dim
, isl_dim_in
))
3630 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3631 "spaces don't match", goto error
);
3633 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3634 isl_space_copy(bmap2
->dim
));
3636 n_in
= isl_basic_map_n_in(bmap1
);
3637 n_out
= isl_basic_map_n_out(bmap2
);
3638 n
= isl_basic_map_n_out(bmap1
);
3639 nparam
= isl_basic_map_n_param(bmap1
);
3641 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3642 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3643 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3644 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3645 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3646 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3647 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3648 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3649 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3650 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3651 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3653 bmap
= isl_basic_map_alloc_space(dim_result
,
3654 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3655 bmap1
->n_eq
+ bmap2
->n_eq
,
3656 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3657 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3658 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3659 bmap
= add_divs(bmap
, n
);
3660 bmap
= isl_basic_map_simplify(bmap
);
3661 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3662 return isl_basic_map_finalize(bmap
);
3664 isl_basic_map_free(bmap1
);
3665 isl_basic_map_free(bmap2
);
3669 struct isl_basic_set
*isl_basic_set_apply(
3670 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3675 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3678 return (struct isl_basic_set
*)
3679 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3681 isl_basic_set_free(bset
);
3682 isl_basic_map_free(bmap
);
3686 struct isl_basic_map
*isl_basic_map_apply_domain(
3687 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3689 if (!bmap1
|| !bmap2
)
3692 isl_assert(bmap1
->ctx
,
3693 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3694 isl_assert(bmap1
->ctx
,
3695 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3698 bmap1
= isl_basic_map_reverse(bmap1
);
3699 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3700 return isl_basic_map_reverse(bmap1
);
3702 isl_basic_map_free(bmap1
);
3703 isl_basic_map_free(bmap2
);
3707 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3708 * A \cap B -> f(A) + f(B)
3710 struct isl_basic_map
*isl_basic_map_sum(
3711 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3713 unsigned n_in
, n_out
, nparam
, total
, pos
;
3714 struct isl_basic_map
*bmap
= NULL
;
3715 struct isl_dim_map
*dim_map1
, *dim_map2
;
3718 if (!bmap1
|| !bmap2
)
3721 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3724 nparam
= isl_basic_map_n_param(bmap1
);
3725 n_in
= isl_basic_map_n_in(bmap1
);
3726 n_out
= isl_basic_map_n_out(bmap1
);
3728 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3729 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3730 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3731 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3732 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3733 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3734 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3735 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3736 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3737 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3738 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3740 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3741 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3742 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3743 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3744 for (i
= 0; i
< n_out
; ++i
) {
3745 int j
= isl_basic_map_alloc_equality(bmap
);
3748 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3749 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3750 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3751 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3753 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3754 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3755 bmap
= add_divs(bmap
, 2 * n_out
);
3757 bmap
= isl_basic_map_simplify(bmap
);
3758 return isl_basic_map_finalize(bmap
);
3760 isl_basic_map_free(bmap
);
3761 isl_basic_map_free(bmap1
);
3762 isl_basic_map_free(bmap2
);
3766 /* Given two maps A -> f(A) and B -> g(B), construct a map
3767 * A \cap B -> f(A) + f(B)
3769 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3771 struct isl_map
*result
;
3777 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3779 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3780 map1
->n
* map2
->n
, 0);
3783 for (i
= 0; i
< map1
->n
; ++i
)
3784 for (j
= 0; j
< map2
->n
; ++j
) {
3785 struct isl_basic_map
*part
;
3786 part
= isl_basic_map_sum(
3787 isl_basic_map_copy(map1
->p
[i
]),
3788 isl_basic_map_copy(map2
->p
[j
]));
3789 if (isl_basic_map_is_empty(part
))
3790 isl_basic_map_free(part
);
3792 result
= isl_map_add_basic_map(result
, part
);
3805 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3806 __isl_take isl_set
*set2
)
3808 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3811 /* Given a basic map A -> f(A), construct A -> -f(A).
3813 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3818 bmap
= isl_basic_map_cow(bmap
);
3822 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3823 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3824 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3825 for (j
= 0; j
< n
; ++j
)
3826 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3827 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3828 for (j
= 0; j
< n
; ++j
)
3829 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3830 for (i
= 0; i
< bmap
->n_div
; ++i
)
3831 for (j
= 0; j
< n
; ++j
)
3832 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3833 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3834 return isl_basic_map_finalize(bmap
);
3837 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3839 return isl_basic_map_neg(bset
);
3842 /* Given a map A -> f(A), construct A -> -f(A).
3844 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3848 map
= isl_map_cow(map
);
3852 for (i
= 0; i
< map
->n
; ++i
) {
3853 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3864 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3866 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3869 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3870 * A -> floor(f(A)/d).
3872 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3875 unsigned n_in
, n_out
, nparam
, total
, pos
;
3876 struct isl_basic_map
*result
= NULL
;
3877 struct isl_dim_map
*dim_map
;
3883 nparam
= isl_basic_map_n_param(bmap
);
3884 n_in
= isl_basic_map_n_in(bmap
);
3885 n_out
= isl_basic_map_n_out(bmap
);
3887 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3888 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3889 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3890 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3891 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3892 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3894 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3895 bmap
->n_div
+ n_out
,
3896 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3897 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3898 result
= add_divs(result
, n_out
);
3899 for (i
= 0; i
< n_out
; ++i
) {
3901 j
= isl_basic_map_alloc_inequality(result
);
3904 isl_seq_clr(result
->ineq
[j
], 1+total
);
3905 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3906 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3907 j
= isl_basic_map_alloc_inequality(result
);
3910 isl_seq_clr(result
->ineq
[j
], 1+total
);
3911 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3912 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3913 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3916 result
= isl_basic_map_simplify(result
);
3917 return isl_basic_map_finalize(result
);
3919 isl_basic_map_free(result
);
3923 /* Given a map A -> f(A) and an integer d, construct a map
3924 * A -> floor(f(A)/d).
3926 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3930 map
= isl_map_cow(map
);
3934 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3935 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3936 for (i
= 0; i
< map
->n
; ++i
) {
3937 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3948 /* Given a map A -> f(A) and an integer d, construct a map
3949 * A -> floor(f(A)/d).
3951 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3952 __isl_take isl_val
*d
)
3956 if (!isl_val_is_int(d
))
3957 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3958 "expecting integer denominator", goto error
);
3959 map
= isl_map_floordiv(map
, d
->n
);
3968 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3974 i
= isl_basic_map_alloc_equality(bmap
);
3977 nparam
= isl_basic_map_n_param(bmap
);
3978 n_in
= isl_basic_map_n_in(bmap
);
3979 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3980 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3981 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3982 return isl_basic_map_finalize(bmap
);
3984 isl_basic_map_free(bmap
);
3988 /* Add a constraints to "bmap" expressing i_pos < o_pos
3990 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3996 i
= isl_basic_map_alloc_inequality(bmap
);
3999 nparam
= isl_basic_map_n_param(bmap
);
4000 n_in
= isl_basic_map_n_in(bmap
);
4001 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4002 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4003 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4004 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4005 return isl_basic_map_finalize(bmap
);
4007 isl_basic_map_free(bmap
);
4011 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4013 static __isl_give isl_basic_map
*var_less_or_equal(
4014 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4020 i
= isl_basic_map_alloc_inequality(bmap
);
4023 nparam
= isl_basic_map_n_param(bmap
);
4024 n_in
= isl_basic_map_n_in(bmap
);
4025 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4026 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4027 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4028 return isl_basic_map_finalize(bmap
);
4030 isl_basic_map_free(bmap
);
4034 /* Add a constraints to "bmap" expressing i_pos > o_pos
4036 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4042 i
= isl_basic_map_alloc_inequality(bmap
);
4045 nparam
= isl_basic_map_n_param(bmap
);
4046 n_in
= isl_basic_map_n_in(bmap
);
4047 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4048 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4049 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4050 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4051 return isl_basic_map_finalize(bmap
);
4053 isl_basic_map_free(bmap
);
4057 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4059 static __isl_give isl_basic_map
*var_more_or_equal(
4060 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4066 i
= isl_basic_map_alloc_inequality(bmap
);
4069 nparam
= isl_basic_map_n_param(bmap
);
4070 n_in
= isl_basic_map_n_in(bmap
);
4071 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4072 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4073 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4074 return isl_basic_map_finalize(bmap
);
4076 isl_basic_map_free(bmap
);
4080 __isl_give isl_basic_map
*isl_basic_map_equal(
4081 __isl_take isl_space
*dim
, unsigned n_equal
)
4084 struct isl_basic_map
*bmap
;
4085 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4088 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4089 bmap
= var_equal(bmap
, i
);
4090 return isl_basic_map_finalize(bmap
);
4093 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4095 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4099 struct isl_basic_map
*bmap
;
4100 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4103 for (i
= 0; i
< pos
&& bmap
; ++i
)
4104 bmap
= var_equal(bmap
, i
);
4106 bmap
= var_less(bmap
, pos
);
4107 return isl_basic_map_finalize(bmap
);
4110 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4112 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4113 __isl_take isl_space
*dim
, unsigned pos
)
4116 isl_basic_map
*bmap
;
4118 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4119 for (i
= 0; i
< pos
; ++i
)
4120 bmap
= var_equal(bmap
, i
);
4121 bmap
= var_less_or_equal(bmap
, pos
);
4122 return isl_basic_map_finalize(bmap
);
4125 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4127 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4131 struct isl_basic_map
*bmap
;
4132 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4135 for (i
= 0; i
< pos
&& bmap
; ++i
)
4136 bmap
= var_equal(bmap
, i
);
4138 bmap
= var_more(bmap
, pos
);
4139 return isl_basic_map_finalize(bmap
);
4142 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4144 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4145 __isl_take isl_space
*dim
, unsigned pos
)
4148 isl_basic_map
*bmap
;
4150 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4151 for (i
= 0; i
< pos
; ++i
)
4152 bmap
= var_equal(bmap
, i
);
4153 bmap
= var_more_or_equal(bmap
, pos
);
4154 return isl_basic_map_finalize(bmap
);
4157 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4158 unsigned n
, int equal
)
4160 struct isl_map
*map
;
4163 if (n
== 0 && equal
)
4164 return isl_map_universe(dims
);
4166 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4168 for (i
= 0; i
+ 1 < n
; ++i
)
4169 map
= isl_map_add_basic_map(map
,
4170 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4173 map
= isl_map_add_basic_map(map
,
4174 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4176 map
= isl_map_add_basic_map(map
,
4177 isl_basic_map_less_at(dims
, n
- 1));
4179 isl_space_free(dims
);
4184 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4188 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4191 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4193 return map_lex_lte_first(dim
, n
, 0);
4196 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4198 return map_lex_lte_first(dim
, n
, 1);
4201 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4203 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4206 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4208 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4211 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4212 unsigned n
, int equal
)
4214 struct isl_map
*map
;
4217 if (n
== 0 && equal
)
4218 return isl_map_universe(dims
);
4220 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4222 for (i
= 0; i
+ 1 < n
; ++i
)
4223 map
= isl_map_add_basic_map(map
,
4224 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4227 map
= isl_map_add_basic_map(map
,
4228 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4230 map
= isl_map_add_basic_map(map
,
4231 isl_basic_map_more_at(dims
, n
- 1));
4233 isl_space_free(dims
);
4238 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4242 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4245 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4247 return map_lex_gte_first(dim
, n
, 0);
4250 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4252 return map_lex_gte_first(dim
, n
, 1);
4255 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4257 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4260 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4262 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4265 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4266 __isl_take isl_set
*set2
)
4269 map
= isl_map_lex_le(isl_set_get_space(set1
));
4270 map
= isl_map_intersect_domain(map
, set1
);
4271 map
= isl_map_intersect_range(map
, set2
);
4275 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4276 __isl_take isl_set
*set2
)
4279 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4280 map
= isl_map_intersect_domain(map
, set1
);
4281 map
= isl_map_intersect_range(map
, set2
);
4285 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4286 __isl_take isl_set
*set2
)
4289 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4290 map
= isl_map_intersect_domain(map
, set1
);
4291 map
= isl_map_intersect_range(map
, set2
);
4295 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4296 __isl_take isl_set
*set2
)
4299 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4300 map
= isl_map_intersect_domain(map
, set1
);
4301 map
= isl_map_intersect_range(map
, set2
);
4305 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4306 __isl_take isl_map
*map2
)
4309 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4310 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4311 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4315 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4316 __isl_take isl_map
*map2
)
4319 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4320 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4321 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4325 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4326 __isl_take isl_map
*map2
)
4329 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4330 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4331 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4335 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4336 __isl_take isl_map
*map2
)
4339 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4340 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4341 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4345 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4346 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4348 struct isl_basic_map
*bmap
;
4350 bset
= isl_basic_set_cow(bset
);
4354 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4355 isl_space_free(bset
->dim
);
4356 bmap
= (struct isl_basic_map
*) bset
;
4358 return isl_basic_map_finalize(bmap
);
4360 isl_basic_set_free(bset
);
4361 isl_space_free(dim
);
4365 /* For a div d = floor(f/m), add the constraint
4369 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4370 unsigned pos
, isl_int
*div
)
4373 unsigned total
= isl_basic_map_total_dim(bmap
);
4375 i
= isl_basic_map_alloc_inequality(bmap
);
4378 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4379 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4384 /* For a div d = floor(f/m), add the constraint
4386 * -(f-(n-1)) + m d >= 0
4388 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4389 unsigned pos
, isl_int
*div
)
4392 unsigned total
= isl_basic_map_total_dim(bmap
);
4394 i
= isl_basic_map_alloc_inequality(bmap
);
4397 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4398 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4399 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4400 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4405 /* For a div d = floor(f/m), add the constraints
4408 * -(f-(n-1)) + m d >= 0
4410 * Note that the second constraint is the negation of
4414 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4415 unsigned pos
, isl_int
*div
)
4417 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4419 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4424 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4425 unsigned pos
, isl_int
*div
)
4427 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4431 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4433 unsigned total
= isl_basic_map_total_dim(bmap
);
4434 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4436 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4440 /* For each known div d = floor(f/m), add the constraints
4443 * -(f-(n-1)) + m d >= 0
4445 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4446 __isl_take isl_basic_map
*bmap
)
4453 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4456 bmap
= isl_basic_map_cow(bmap
);
4457 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4460 for (i
= 0; i
< n_div
; ++i
) {
4461 if (isl_int_is_zero(bmap
->div
[i
][0]))
4463 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4464 return isl_basic_map_free(bmap
);
4467 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4468 bmap
= isl_basic_map_finalize(bmap
);
4472 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4474 * In particular, if this div is of the form d = floor(f/m),
4475 * then add the constraint
4479 * if sign < 0 or the constraint
4481 * -(f-(n-1)) + m d >= 0
4485 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4486 unsigned div
, int sign
)
4494 total
= isl_basic_map_total_dim(bmap
);
4495 div_pos
= total
- bmap
->n_div
+ div
;
4498 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4500 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4503 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4505 return isl_basic_map_add_div_constraints(bset
, div
);
4508 struct isl_basic_set
*isl_basic_map_underlying_set(
4509 struct isl_basic_map
*bmap
)
4513 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4515 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4516 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4517 return (struct isl_basic_set
*)bmap
;
4518 bmap
= isl_basic_map_cow(bmap
);
4521 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4524 bmap
->extra
-= bmap
->n_div
;
4526 bmap
= isl_basic_map_finalize(bmap
);
4527 return (struct isl_basic_set
*)bmap
;
4529 isl_basic_map_free(bmap
);
4533 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4534 __isl_take isl_basic_set
*bset
)
4536 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4539 /* Replace each element in "list" by the result of applying
4540 * isl_basic_map_underlying_set to the element.
4542 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4543 __isl_take isl_basic_map_list
*list
)
4550 n
= isl_basic_map_list_n_basic_map(list
);
4551 for (i
= 0; i
< n
; ++i
) {
4552 isl_basic_map
*bmap
;
4553 isl_basic_set
*bset
;
4555 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4556 bset
= isl_basic_set_underlying_set(bmap
);
4557 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4563 struct isl_basic_map
*isl_basic_map_overlying_set(
4564 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4566 struct isl_basic_map
*bmap
;
4567 struct isl_ctx
*ctx
;
4574 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4575 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4576 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4578 if (like
->n_div
== 0) {
4579 isl_space
*space
= isl_basic_map_get_space(like
);
4580 isl_basic_map_free(like
);
4581 return isl_basic_map_reset_space(bset
, space
);
4583 bset
= isl_basic_set_cow(bset
);
4586 total
= bset
->dim
->n_out
+ bset
->extra
;
4587 bmap
= (struct isl_basic_map
*)bset
;
4588 isl_space_free(bmap
->dim
);
4589 bmap
->dim
= isl_space_copy(like
->dim
);
4592 bmap
->n_div
= like
->n_div
;
4593 bmap
->extra
+= like
->n_div
;
4597 ltotal
= total
- bmap
->extra
+ like
->extra
;
4600 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4601 bmap
->extra
* (1 + 1 + total
));
4602 if (isl_blk_is_error(bmap
->block2
))
4604 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4608 for (i
= 0; i
< bmap
->extra
; ++i
)
4609 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4610 for (i
= 0; i
< like
->n_div
; ++i
) {
4611 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4612 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4614 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4616 isl_basic_map_free(like
);
4617 bmap
= isl_basic_map_simplify(bmap
);
4618 bmap
= isl_basic_map_finalize(bmap
);
4621 isl_basic_map_free(like
);
4622 isl_basic_set_free(bset
);
4626 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4627 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4629 return (struct isl_basic_set
*)
4630 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4633 struct isl_set
*isl_set_from_underlying_set(
4634 struct isl_set
*set
, struct isl_basic_set
*like
)
4640 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4642 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4643 isl_basic_set_free(like
);
4646 set
= isl_set_cow(set
);
4649 for (i
= 0; i
< set
->n
; ++i
) {
4650 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4651 isl_basic_set_copy(like
));
4655 isl_space_free(set
->dim
);
4656 set
->dim
= isl_space_copy(like
->dim
);
4659 isl_basic_set_free(like
);
4662 isl_basic_set_free(like
);
4667 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4671 map
= isl_map_cow(map
);
4674 map
->dim
= isl_space_cow(map
->dim
);
4678 for (i
= 1; i
< map
->n
; ++i
)
4679 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4681 for (i
= 0; i
< map
->n
; ++i
) {
4682 map
->p
[i
] = (struct isl_basic_map
*)
4683 isl_basic_map_underlying_set(map
->p
[i
]);
4688 map
->dim
= isl_space_underlying(map
->dim
, 0);
4690 isl_space_free(map
->dim
);
4691 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4695 return (struct isl_set
*)map
;
4701 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4703 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4706 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4707 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4713 equal
= isl_space_is_equal(bmap
->dim
, space
);
4717 isl_space_free(space
);
4720 bmap
= isl_basic_map_cow(bmap
);
4721 if (!bmap
|| !space
)
4724 isl_space_free(bmap
->dim
);
4727 bmap
= isl_basic_map_finalize(bmap
);
4731 isl_basic_map_free(bmap
);
4732 isl_space_free(space
);
4736 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4737 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4739 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4743 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4744 __isl_take isl_space
*dim
)
4748 map
= isl_map_cow(map
);
4752 for (i
= 0; i
< map
->n
; ++i
) {
4753 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4754 isl_space_copy(dim
));
4758 isl_space_free(map
->dim
);
4764 isl_space_free(dim
);
4768 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4769 __isl_take isl_space
*dim
)
4771 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4774 /* Compute the parameter domain of the given basic set.
4776 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4781 if (isl_basic_set_is_params(bset
))
4784 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4785 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4786 space
= isl_basic_set_get_space(bset
);
4787 space
= isl_space_params(space
);
4788 bset
= isl_basic_set_reset_space(bset
, space
);
4792 /* Construct a zero-dimensional basic set with the given parameter domain.
4794 __isl_give isl_basic_set
*isl_basic_set_from_params(
4795 __isl_take isl_basic_set
*bset
)
4798 space
= isl_basic_set_get_space(bset
);
4799 space
= isl_space_set_from_params(space
);
4800 bset
= isl_basic_set_reset_space(bset
, space
);
4804 /* Compute the parameter domain of the given set.
4806 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4811 if (isl_set_is_params(set
))
4814 n
= isl_set_dim(set
, isl_dim_set
);
4815 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4816 space
= isl_set_get_space(set
);
4817 space
= isl_space_params(space
);
4818 set
= isl_set_reset_space(set
, space
);
4822 /* Construct a zero-dimensional set with the given parameter domain.
4824 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4827 space
= isl_set_get_space(set
);
4828 space
= isl_space_set_from_params(space
);
4829 set
= isl_set_reset_space(set
, space
);
4833 /* Compute the parameter domain of the given map.
4835 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4840 n
= isl_map_dim(map
, isl_dim_in
);
4841 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4842 n
= isl_map_dim(map
, isl_dim_out
);
4843 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4844 space
= isl_map_get_space(map
);
4845 space
= isl_space_params(space
);
4846 map
= isl_map_reset_space(map
, space
);
4850 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4857 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4859 n_out
= isl_basic_map_n_out(bmap
);
4860 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4862 return isl_basic_map_reset_space(bmap
, space
);
4865 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4869 return isl_space_may_be_set(bmap
->dim
);
4872 /* Is this basic map actually a set?
4873 * Users should never call this function. Outside of isl,
4874 * the type should indicate whether something is a set or a map.
4876 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4880 return isl_space_is_set(bmap
->dim
);
4883 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4887 if (isl_basic_map_is_set(bmap
))
4889 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4892 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4893 __isl_take isl_basic_map
*bmap
)
4897 isl_basic_map
*domain
;
4898 int nparam
, n_in
, n_out
;
4901 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4902 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4903 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4905 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4906 domain
= isl_basic_map_universe(dim
);
4908 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4909 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4910 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4912 total
= isl_basic_map_total_dim(bmap
);
4914 for (i
= 0; i
< n_in
; ++i
) {
4915 k
= isl_basic_map_alloc_equality(bmap
);
4918 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4919 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4920 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4923 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4924 return isl_basic_map_finalize(bmap
);
4926 isl_basic_map_free(bmap
);
4930 __isl_give isl_basic_map
*isl_basic_map_range_map(
4931 __isl_take isl_basic_map
*bmap
)
4935 isl_basic_map
*range
;
4936 int nparam
, n_in
, n_out
;
4939 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4940 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4941 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4943 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4944 range
= isl_basic_map_universe(dim
);
4946 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4947 bmap
= isl_basic_map_apply_range(bmap
, range
);
4948 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4950 total
= isl_basic_map_total_dim(bmap
);
4952 for (i
= 0; i
< n_out
; ++i
) {
4953 k
= isl_basic_map_alloc_equality(bmap
);
4956 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4957 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4958 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4961 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4962 return isl_basic_map_finalize(bmap
);
4964 isl_basic_map_free(bmap
);
4968 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4972 return isl_space_may_be_set(map
->dim
);
4975 /* Is this map actually a set?
4976 * Users should never call this function. Outside of isl,
4977 * the type should indicate whether something is a set or a map.
4979 int isl_map_is_set(__isl_keep isl_map
*map
)
4983 return isl_space_is_set(map
->dim
);
4986 struct isl_set
*isl_map_range(struct isl_map
*map
)
4989 struct isl_set
*set
;
4993 if (isl_map_is_set(map
))
4994 return (isl_set
*)map
;
4996 map
= isl_map_cow(map
);
5000 set
= (struct isl_set
*) map
;
5001 set
->dim
= isl_space_range(set
->dim
);
5004 for (i
= 0; i
< map
->n
; ++i
) {
5005 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5009 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5010 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5017 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5021 map
= isl_map_cow(map
);
5025 map
->dim
= isl_space_domain_map(map
->dim
);
5028 for (i
= 0; i
< map
->n
; ++i
) {
5029 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5033 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5034 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5041 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5044 isl_space
*range_dim
;
5046 map
= isl_map_cow(map
);
5050 range_dim
= isl_space_range(isl_map_get_space(map
));
5051 range_dim
= isl_space_from_range(range_dim
);
5052 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5053 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5056 for (i
= 0; i
< map
->n
; ++i
) {
5057 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5061 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5062 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5069 /* Given a wrapped map of the form A[B -> C],
5070 * return the map A[B -> C] -> B.
5072 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5079 if (!isl_set_has_tuple_id(set
))
5080 return isl_map_domain_map(isl_set_unwrap(set
));
5082 id
= isl_set_get_tuple_id(set
);
5083 map
= isl_map_domain_map(isl_set_unwrap(set
));
5084 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5089 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5090 __isl_take isl_space
*dim
)
5093 struct isl_map
*map
= NULL
;
5095 set
= isl_set_cow(set
);
5098 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5099 map
= (struct isl_map
*)set
;
5100 for (i
= 0; i
< set
->n
; ++i
) {
5101 map
->p
[i
] = isl_basic_map_from_basic_set(
5102 set
->p
[i
], isl_space_copy(dim
));
5106 isl_space_free(map
->dim
);
5110 isl_space_free(dim
);
5115 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5116 __isl_take isl_basic_set
*bset
)
5118 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5121 __isl_give isl_basic_map
*isl_basic_map_from_range(
5122 __isl_take isl_basic_set
*bset
)
5125 space
= isl_basic_set_get_space(bset
);
5126 space
= isl_space_from_range(space
);
5127 bset
= isl_basic_set_reset_space(bset
, space
);
5128 return (isl_basic_map
*)bset
;
5131 /* Create a relation with the given set as range.
5132 * The domain of the created relation is a zero-dimensional
5133 * flat anonymous space.
5135 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5138 space
= isl_set_get_space(set
);
5139 space
= isl_space_from_range(space
);
5140 set
= isl_set_reset_space(set
, space
);
5141 return (struct isl_map
*)set
;
5144 /* Create a relation with the given set as domain.
5145 * The range of the created relation is a zero-dimensional
5146 * flat anonymous space.
5148 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5150 return isl_map_reverse(isl_map_from_range(set
));
5153 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5154 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5156 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5159 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5160 __isl_take isl_set
*range
)
5162 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5165 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5168 struct isl_map
*map
;
5173 isl_die(dim
->ctx
, isl_error_internal
,
5174 "negative number of basic maps", goto error
);
5175 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5176 sizeof(struct isl_map
) +
5177 (n
- 1) * sizeof(struct isl_basic_map
*));
5181 map
->ctx
= dim
->ctx
;
5182 isl_ctx_ref(map
->ctx
);
5190 isl_space_free(dim
);
5194 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5195 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5198 struct isl_map
*map
;
5201 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5205 map
= isl_map_alloc_space(dims
, n
, flags
);
5209 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5211 struct isl_basic_map
*bmap
;
5212 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5213 bmap
= isl_basic_map_set_to_empty(bmap
);
5217 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5219 struct isl_basic_set
*bset
;
5220 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5221 bset
= isl_basic_set_set_to_empty(bset
);
5225 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5227 struct isl_basic_map
*bmap
;
5228 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5229 bmap
= isl_basic_map_finalize(bmap
);
5233 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5235 struct isl_basic_set
*bset
;
5236 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5237 bset
= isl_basic_set_finalize(bset
);
5241 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5244 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5245 isl_basic_map
*bmap
;
5247 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5248 for (i
= 0; i
< total
; ++i
) {
5249 int k
= isl_basic_map_alloc_inequality(bmap
);
5252 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5253 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5257 isl_basic_map_free(bmap
);
5261 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5263 return isl_basic_map_nat_universe(dim
);
5266 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5268 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5271 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5273 return isl_map_nat_universe(dim
);
5276 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5278 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5281 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5283 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5286 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5288 struct isl_map
*map
;
5291 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5292 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5296 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5298 struct isl_set
*set
;
5301 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5302 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5306 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5309 struct isl_map
*dup
;
5313 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5314 for (i
= 0; i
< map
->n
; ++i
)
5315 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5319 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5320 __isl_take isl_basic_map
*bmap
)
5324 if (isl_basic_map_plain_is_empty(bmap
)) {
5325 isl_basic_map_free(bmap
);
5328 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5329 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5330 map
->p
[map
->n
] = bmap
;
5332 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5338 isl_basic_map_free(bmap
);
5342 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5352 isl_ctx_deref(map
->ctx
);
5353 for (i
= 0; i
< map
->n
; ++i
)
5354 isl_basic_map_free(map
->p
[i
]);
5355 isl_space_free(map
->dim
);
5361 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5362 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5366 bmap
= isl_basic_map_cow(bmap
);
5367 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5368 j
= isl_basic_map_alloc_equality(bmap
);
5371 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5372 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5373 isl_int_set_si(bmap
->eq
[j
][0], value
);
5374 bmap
= isl_basic_map_simplify(bmap
);
5375 return isl_basic_map_finalize(bmap
);
5377 isl_basic_map_free(bmap
);
5381 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5382 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5386 bmap
= isl_basic_map_cow(bmap
);
5387 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5388 j
= isl_basic_map_alloc_equality(bmap
);
5391 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5392 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5393 isl_int_set(bmap
->eq
[j
][0], value
);
5394 bmap
= isl_basic_map_simplify(bmap
);
5395 return isl_basic_map_finalize(bmap
);
5397 isl_basic_map_free(bmap
);
5401 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5402 enum isl_dim_type type
, unsigned pos
, int value
)
5406 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5407 return isl_basic_map_fix_pos_si(bmap
,
5408 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5410 isl_basic_map_free(bmap
);
5414 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5415 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5419 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5420 return isl_basic_map_fix_pos(bmap
,
5421 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5423 isl_basic_map_free(bmap
);
5427 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5428 * to be equal to "v".
5430 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5431 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5435 if (!isl_val_is_int(v
))
5436 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5437 "expecting integer value", goto error
);
5438 if (pos
>= isl_basic_map_dim(bmap
, type
))
5439 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5440 "index out of bounds", goto error
);
5441 pos
+= isl_basic_map_offset(bmap
, type
);
5442 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5446 isl_basic_map_free(bmap
);
5451 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5452 * to be equal to "v".
5454 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5455 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5457 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5460 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5461 enum isl_dim_type type
, unsigned pos
, int value
)
5463 return (struct isl_basic_set
*)
5464 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5468 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5469 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5471 return (struct isl_basic_set
*)
5472 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5476 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5477 unsigned input
, int value
)
5479 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5482 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5483 unsigned dim
, int value
)
5485 return (struct isl_basic_set
*)
5486 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5487 isl_dim_set
, dim
, value
);
5490 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5492 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5499 isl_basic_map_free(map
->p
[i
]);
5500 if (i
!= map
->n
- 1) {
5501 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5502 map
->p
[i
] = map
->p
[map
->n
- 1];
5509 /* Perform "fn" on each basic map of "map", where we may not be holding
5510 * the only reference to "map".
5511 * In particular, "fn" should be a semantics preserving operation
5512 * that we want to apply to all copies of "map". We therefore need
5513 * to be careful not to modify "map" in a way that breaks "map"
5514 * in case anything goes wrong.
5516 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5517 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5519 struct isl_basic_map
*bmap
;
5525 for (i
= map
->n
- 1; i
>= 0; --i
) {
5526 bmap
= isl_basic_map_copy(map
->p
[i
]);
5530 isl_basic_map_free(map
->p
[i
]);
5532 if (remove_if_empty(map
, i
) < 0)
5542 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5543 enum isl_dim_type type
, unsigned pos
, int value
)
5547 map
= isl_map_cow(map
);
5551 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5552 for (i
= map
->n
- 1; i
>= 0; --i
) {
5553 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5554 if (remove_if_empty(map
, i
) < 0)
5557 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5564 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5565 enum isl_dim_type type
, unsigned pos
, int value
)
5567 return (struct isl_set
*)
5568 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5571 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5572 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5576 map
= isl_map_cow(map
);
5580 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5581 for (i
= 0; i
< map
->n
; ++i
) {
5582 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5586 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5593 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5594 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5596 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5599 /* Fix the value of the variable at position "pos" of type "type" of "map"
5600 * to be equal to "v".
5602 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5603 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5607 map
= isl_map_cow(map
);
5611 if (!isl_val_is_int(v
))
5612 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5613 "expecting integer value", goto error
);
5614 if (pos
>= isl_map_dim(map
, type
))
5615 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5616 "index out of bounds", goto error
);
5617 for (i
= map
->n
- 1; i
>= 0; --i
) {
5618 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5620 if (remove_if_empty(map
, i
) < 0)
5623 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5632 /* Fix the value of the variable at position "pos" of type "type" of "set"
5633 * to be equal to "v".
5635 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5636 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5638 return isl_map_fix_val(set
, type
, pos
, v
);
5641 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5642 unsigned input
, int value
)
5644 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5647 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5649 return (struct isl_set
*)
5650 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5653 static __isl_give isl_basic_map
*basic_map_bound_si(
5654 __isl_take isl_basic_map
*bmap
,
5655 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5661 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5662 pos
+= isl_basic_map_offset(bmap
, type
);
5663 bmap
= isl_basic_map_cow(bmap
);
5664 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5665 j
= isl_basic_map_alloc_inequality(bmap
);
5668 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5670 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5671 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5673 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5674 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5676 bmap
= isl_basic_map_simplify(bmap
);
5677 return isl_basic_map_finalize(bmap
);
5679 isl_basic_map_free(bmap
);
5683 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5684 __isl_take isl_basic_map
*bmap
,
5685 enum isl_dim_type type
, unsigned pos
, int value
)
5687 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5690 /* Constrain the values of the given dimension to be no greater than "value".
5692 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5693 __isl_take isl_basic_map
*bmap
,
5694 enum isl_dim_type type
, unsigned pos
, int value
)
5696 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5699 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5700 unsigned dim
, isl_int value
)
5704 bset
= isl_basic_set_cow(bset
);
5705 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5706 j
= isl_basic_set_alloc_inequality(bset
);
5709 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5710 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5711 isl_int_neg(bset
->ineq
[j
][0], value
);
5712 bset
= isl_basic_set_simplify(bset
);
5713 return isl_basic_set_finalize(bset
);
5715 isl_basic_set_free(bset
);
5719 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5720 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5724 map
= isl_map_cow(map
);
5728 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5729 for (i
= 0; i
< map
->n
; ++i
) {
5730 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5731 type
, pos
, value
, upper
);
5735 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5742 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5743 enum isl_dim_type type
, unsigned pos
, int value
)
5745 return map_bound_si(map
, type
, pos
, value
, 0);
5748 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5749 enum isl_dim_type type
, unsigned pos
, int value
)
5751 return map_bound_si(map
, type
, pos
, value
, 1);
5754 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5755 enum isl_dim_type type
, unsigned pos
, int value
)
5757 return (struct isl_set
*)
5758 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5761 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5762 enum isl_dim_type type
, unsigned pos
, int value
)
5764 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5767 /* Bound the given variable of "bmap" from below (or above is "upper"
5768 * is set) to "value".
5770 static __isl_give isl_basic_map
*basic_map_bound(
5771 __isl_take isl_basic_map
*bmap
,
5772 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5778 if (pos
>= isl_basic_map_dim(bmap
, type
))
5779 isl_die(bmap
->ctx
, isl_error_invalid
,
5780 "index out of bounds", goto error
);
5781 pos
+= isl_basic_map_offset(bmap
, type
);
5782 bmap
= isl_basic_map_cow(bmap
);
5783 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5784 j
= isl_basic_map_alloc_inequality(bmap
);
5787 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5789 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5790 isl_int_set(bmap
->ineq
[j
][0], value
);
5792 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5793 isl_int_neg(bmap
->ineq
[j
][0], value
);
5795 bmap
= isl_basic_map_simplify(bmap
);
5796 return isl_basic_map_finalize(bmap
);
5798 isl_basic_map_free(bmap
);
5802 /* Bound the given variable of "map" from below (or above is "upper"
5803 * is set) to "value".
5805 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5806 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5810 map
= isl_map_cow(map
);
5814 if (pos
>= isl_map_dim(map
, type
))
5815 isl_die(map
->ctx
, isl_error_invalid
,
5816 "index out of bounds", goto error
);
5817 for (i
= map
->n
- 1; i
>= 0; --i
) {
5818 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5819 if (remove_if_empty(map
, i
) < 0)
5822 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5829 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5830 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5832 return map_bound(map
, type
, pos
, value
, 0);
5835 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5836 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5838 return map_bound(map
, type
, pos
, value
, 1);
5841 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5842 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5844 return isl_map_lower_bound(set
, type
, pos
, value
);
5847 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5848 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5850 return isl_map_upper_bound(set
, type
, pos
, value
);
5853 /* Force the values of the variable at position "pos" of type "type" of "set"
5854 * to be no smaller than "value".
5856 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5857 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5861 if (!isl_val_is_int(value
))
5862 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5863 "expecting integer value", goto error
);
5864 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5865 isl_val_free(value
);
5868 isl_val_free(value
);
5873 /* Force the values of the variable at position "pos" of type "type" of "set"
5874 * to be no greater than "value".
5876 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5877 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5881 if (!isl_val_is_int(value
))
5882 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5883 "expecting integer value", goto error
);
5884 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5885 isl_val_free(value
);
5888 isl_val_free(value
);
5893 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5898 set
= isl_set_cow(set
);
5902 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5903 for (i
= 0; i
< set
->n
; ++i
) {
5904 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5914 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5918 map
= isl_map_cow(map
);
5922 map
->dim
= isl_space_reverse(map
->dim
);
5925 for (i
= 0; i
< map
->n
; ++i
) {
5926 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5930 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5937 static struct isl_map
*isl_basic_map_partial_lexopt(
5938 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5939 struct isl_set
**empty
, int max
)
5941 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5944 struct isl_map
*isl_basic_map_partial_lexmax(
5945 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5946 struct isl_set
**empty
)
5948 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5951 struct isl_map
*isl_basic_map_partial_lexmin(
5952 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5953 struct isl_set
**empty
)
5955 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5958 struct isl_set
*isl_basic_set_partial_lexmin(
5959 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5960 struct isl_set
**empty
)
5962 return (struct isl_set
*)
5963 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5967 struct isl_set
*isl_basic_set_partial_lexmax(
5968 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5969 struct isl_set
**empty
)
5971 return (struct isl_set
*)
5972 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5976 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5977 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5978 __isl_give isl_set
**empty
)
5980 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5983 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5984 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5985 __isl_give isl_set
**empty
)
5987 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5990 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5991 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5992 __isl_give isl_set
**empty
)
5994 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5997 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5998 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5999 __isl_give isl_set
**empty
)
6001 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6004 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6005 __isl_take isl_basic_map
*bmap
, int max
)
6007 isl_basic_set
*dom
= NULL
;
6008 isl_space
*dom_space
;
6012 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6013 dom
= isl_basic_set_universe(dom_space
);
6014 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6016 isl_basic_map_free(bmap
);
6020 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6021 __isl_take isl_basic_map
*bmap
)
6023 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6027 #define TYPE isl_pw_multi_aff
6029 #define SUFFIX _pw_multi_aff
6031 #define EMPTY isl_pw_multi_aff_empty
6033 #define ADD isl_pw_multi_aff_union_add
6034 #include "isl_map_lexopt_templ.c"
6036 /* Given a map "map", compute the lexicographically minimal
6037 * (or maximal) image element for each domain element in dom,
6038 * in the form of an isl_pw_multi_aff.
6039 * Set *empty to those elements in dom that do not have an image element.
6041 * We first compute the lexicographically minimal or maximal element
6042 * in the first basic map. This results in a partial solution "res"
6043 * and a subset "todo" of dom that still need to be handled.
6044 * We then consider each of the remaining maps in "map" and successively
6045 * update both "res" and "todo".
6047 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6048 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6049 __isl_give isl_set
**empty
, int max
)
6052 isl_pw_multi_aff
*res
;
6058 if (isl_map_plain_is_empty(map
)) {
6063 return isl_pw_multi_aff_from_map(map
);
6066 res
= basic_map_partial_lexopt_pw_multi_aff(
6067 isl_basic_map_copy(map
->p
[0]),
6068 isl_set_copy(dom
), &todo
, max
);
6070 for (i
= 1; i
< map
->n
; ++i
) {
6071 isl_pw_multi_aff
*res_i
;
6074 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6075 isl_basic_map_copy(map
->p
[i
]),
6076 isl_set_copy(dom
), &todo_i
, max
);
6079 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6081 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6083 todo
= isl_set_intersect(todo
, todo_i
);
6104 #define TYPE isl_map
6108 #define EMPTY isl_map_empty
6110 #define ADD isl_map_union_disjoint
6111 #include "isl_map_lexopt_templ.c"
6113 /* Given a map "map", compute the lexicographically minimal
6114 * (or maximal) image element for each domain element in dom.
6115 * Set *empty to those elements in dom that do not have an image element.
6117 * We first compute the lexicographically minimal or maximal element
6118 * in the first basic map. This results in a partial solution "res"
6119 * and a subset "todo" of dom that still need to be handled.
6120 * We then consider each of the remaining maps in "map" and successively
6121 * update both "res" and "todo".
6123 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6124 * Assume we are computing the lexicographical maximum.
6125 * We first compute the lexicographically maximal element in basic map i.
6126 * This results in a partial solution res_i and a subset todo_i.
6127 * Then we combine these results with those obtain for the first k basic maps
6128 * to obtain a result that is valid for the first k+1 basic maps.
6129 * In particular, the set where there is no solution is the set where
6130 * there is no solution for the first k basic maps and also no solution
6131 * for the ith basic map, i.e.,
6133 * todo^i = todo^k * todo_i
6135 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6136 * solutions, arbitrarily breaking ties in favor of res^k.
6137 * That is, when res^k(a) >= res_i(a), we pick res^k and
6138 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6139 * the lexicographic order.)
6140 * In practice, we compute
6142 * res^k * (res_i . "<=")
6146 * res_i * (res^k . "<")
6148 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6149 * where only one of res^k and res_i provides a solution and we simply pick
6156 * Note that we only compute these intersections when dom(res^k) intersects
6157 * dom(res_i). Otherwise, the only effect of these intersections is to
6158 * potentially break up res^k and res_i into smaller pieces.
6159 * We want to avoid such splintering as much as possible.
6160 * In fact, an earlier implementation of this function would look for
6161 * better results in the domain of res^k and for extra results in todo^k,
6162 * but this would always result in a splintering according to todo^k,
6163 * even when the domain of basic map i is disjoint from the domains of
6164 * the previous basic maps.
6166 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6167 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6168 __isl_give isl_set
**empty
, int max
)
6171 struct isl_map
*res
;
6172 struct isl_set
*todo
;
6177 if (isl_map_plain_is_empty(map
)) {
6185 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6186 isl_set_copy(dom
), &todo
, max
);
6188 for (i
= 1; i
< map
->n
; ++i
) {
6192 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6194 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6195 isl_set_copy(dom
), &todo_i
, max
);
6198 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6199 le
= isl_map_lex_le(dim
);
6201 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6202 le
= isl_map_lex_ge(dim
);
6204 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6205 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6206 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6207 le
= isl_map_intersect(le
, isl_map_copy(res
));
6209 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6210 res
= isl_map_intersect_domain(res
,
6211 isl_set_copy(todo_i
));
6212 res_i
= isl_map_intersect_domain(res_i
,
6213 isl_set_copy(todo
));
6216 res
= isl_map_union_disjoint(res
, res_i
);
6217 res
= isl_map_union_disjoint(res
, lt
);
6218 res
= isl_map_union_disjoint(res
, le
);
6220 todo
= isl_set_intersect(todo
, todo_i
);
6240 __isl_give isl_map
*isl_map_partial_lexmax(
6241 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6242 __isl_give isl_set
**empty
)
6244 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6247 __isl_give isl_map
*isl_map_partial_lexmin(
6248 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6249 __isl_give isl_set
**empty
)
6251 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6254 __isl_give isl_set
*isl_set_partial_lexmin(
6255 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6256 __isl_give isl_set
**empty
)
6258 return (struct isl_set
*)
6259 isl_map_partial_lexmin((struct isl_map
*)set
,
6263 __isl_give isl_set
*isl_set_partial_lexmax(
6264 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6265 __isl_give isl_set
**empty
)
6267 return (struct isl_set
*)
6268 isl_map_partial_lexmax((struct isl_map
*)set
,
6272 /* Compute the lexicographic minimum (or maximum if "max" is set)
6273 * of "bmap" over its domain.
6275 * Since we are not interested in the part of the domain space where
6276 * there is no solution, we initialize the domain to those constraints
6277 * of "bmap" that only involve the parameters and the input dimensions.
6278 * This relieves the parametric programming engine from detecting those
6279 * inequalities and transferring them to the context. More importantly,
6280 * it ensures that those inequalities are transferred first and not
6281 * intermixed with inequalities that actually split the domain.
6283 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6287 isl_basic_map
*copy
;
6290 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6291 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6292 copy
= isl_basic_map_copy(bmap
);
6293 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6294 isl_dim_div
, 0, n_div
);
6295 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6296 isl_dim_out
, 0, n_out
);
6297 dom
= isl_basic_map_domain(copy
);
6298 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6301 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6303 return isl_basic_map_lexopt(bmap
, 0);
6306 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6308 return isl_basic_map_lexopt(bmap
, 1);
6311 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6313 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6316 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6318 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6321 /* Extract the first and only affine expression from list
6322 * and then add it to *pwaff with the given dom.
6323 * This domain is known to be disjoint from other domains
6324 * because of the way isl_basic_map_foreach_lexmax works.
6326 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6327 __isl_take isl_aff_list
*list
, void *user
)
6329 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6331 isl_pw_aff
**pwaff
= user
;
6332 isl_pw_aff
*pwaff_i
;
6336 if (isl_aff_list_n_aff(list
) != 1)
6337 isl_die(ctx
, isl_error_internal
,
6338 "expecting single element list", goto error
);
6340 aff
= isl_aff_list_get_aff(list
, 0);
6341 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6343 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6345 isl_aff_list_free(list
);
6349 isl_basic_set_free(dom
);
6350 isl_aff_list_free(list
);
6354 /* Given a basic map with one output dimension, compute the minimum or
6355 * maximum of that dimension as an isl_pw_aff.
6357 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6358 * call update_dim_opt on each leaf of the result.
6360 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6363 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6367 dim
= isl_space_from_domain(isl_space_domain(dim
));
6368 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6369 pwaff
= isl_pw_aff_empty(dim
);
6371 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6373 return isl_pw_aff_free(pwaff
);
6378 /* Compute the minimum or maximum of the given output dimension
6379 * as a function of the parameters and the input dimensions,
6380 * but independently of the other output dimensions.
6382 * We first project out the other output dimension and then compute
6383 * the "lexicographic" maximum in each basic map, combining the results
6384 * using isl_pw_aff_union_max.
6386 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6393 n_out
= isl_map_dim(map
, isl_dim_out
);
6394 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6395 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6400 isl_space
*dim
= isl_map_get_space(map
);
6402 return isl_pw_aff_empty(dim
);
6405 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6406 for (i
= 1; i
< map
->n
; ++i
) {
6407 isl_pw_aff
*pwaff_i
;
6409 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6410 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6418 /* Compute the maximum of the given output dimension as a function of the
6419 * parameters and input dimensions, but independently of
6420 * the other output dimensions.
6422 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6424 return map_dim_opt(map
, pos
, 1);
6427 /* Compute the minimum or maximum of the given set dimension
6428 * as a function of the parameters,
6429 * but independently of the other set dimensions.
6431 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6434 return map_dim_opt(set
, pos
, max
);
6437 /* Compute the maximum of the given set dimension as a function of the
6438 * parameters, but independently of the other set dimensions.
6440 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6442 return set_dim_opt(set
, pos
, 1);
6445 /* Compute the minimum of the given set dimension as a function of the
6446 * parameters, but independently of the other set dimensions.
6448 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6450 return set_dim_opt(set
, pos
, 0);
6453 /* Apply a preimage specified by "mat" on the parameters of "bset".
6454 * bset is assumed to have only parameters and divs.
6456 static struct isl_basic_set
*basic_set_parameter_preimage(
6457 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6464 bset
->dim
= isl_space_cow(bset
->dim
);
6468 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6470 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6472 bset
->dim
->nparam
= 0;
6473 bset
->dim
->n_out
= nparam
;
6474 bset
= isl_basic_set_preimage(bset
, mat
);
6476 bset
->dim
->nparam
= bset
->dim
->n_out
;
6477 bset
->dim
->n_out
= 0;
6482 isl_basic_set_free(bset
);
6486 /* Apply a preimage specified by "mat" on the parameters of "set".
6487 * set is assumed to have only parameters and divs.
6489 static struct isl_set
*set_parameter_preimage(
6490 struct isl_set
*set
, struct isl_mat
*mat
)
6492 isl_space
*dim
= NULL
;
6498 dim
= isl_space_copy(set
->dim
);
6499 dim
= isl_space_cow(dim
);
6503 nparam
= isl_set_dim(set
, isl_dim_param
);
6505 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6508 dim
->n_out
= nparam
;
6509 isl_set_reset_space(set
, dim
);
6510 set
= isl_set_preimage(set
, mat
);
6513 dim
= isl_space_copy(set
->dim
);
6514 dim
= isl_space_cow(dim
);
6517 dim
->nparam
= dim
->n_out
;
6519 isl_set_reset_space(set
, dim
);
6522 isl_space_free(dim
);
6529 /* Intersect the basic set "bset" with the affine space specified by the
6530 * equalities in "eq".
6532 static struct isl_basic_set
*basic_set_append_equalities(
6533 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6541 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6546 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6547 for (i
= 0; i
< eq
->n_row
; ++i
) {
6548 k
= isl_basic_set_alloc_equality(bset
);
6551 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6552 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6556 bset
= isl_basic_set_gauss(bset
, NULL
);
6557 bset
= isl_basic_set_finalize(bset
);
6562 isl_basic_set_free(bset
);
6566 /* Intersect the set "set" with the affine space specified by the
6567 * equalities in "eq".
6569 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6577 for (i
= 0; i
< set
->n
; ++i
) {
6578 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6591 /* Given a basic set "bset" that only involves parameters and existentially
6592 * quantified variables, return the index of the first equality
6593 * that only involves parameters. If there is no such equality then
6594 * return bset->n_eq.
6596 * This function assumes that isl_basic_set_gauss has been called on "bset".
6598 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6601 unsigned nparam
, n_div
;
6606 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6607 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6609 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6610 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6617 /* Compute an explicit representation for the existentially quantified
6618 * variables in "bset" by computing the "minimal value" of the set
6619 * variables. Since there are no set variables, the computation of
6620 * the minimal value essentially computes an explicit representation
6621 * of the non-empty part(s) of "bset".
6623 * The input only involves parameters and existentially quantified variables.
6624 * All equalities among parameters have been removed.
6626 * Since the existentially quantified variables in the result are in general
6627 * going to be different from those in the input, we first replace
6628 * them by the minimal number of variables based on their equalities.
6629 * This should simplify the parametric integer programming.
6631 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6633 isl_morph
*morph1
, *morph2
;
6639 if (bset
->n_eq
== 0)
6640 return isl_basic_set_lexmin(bset
);
6642 morph1
= isl_basic_set_parameter_compression(bset
);
6643 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6644 bset
= isl_basic_set_lift(bset
);
6645 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6646 bset
= isl_morph_basic_set(morph2
, bset
);
6647 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6648 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6650 set
= isl_basic_set_lexmin(bset
);
6652 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6657 /* Project the given basic set onto its parameter domain, possibly introducing
6658 * new, explicit, existential variables in the constraints.
6659 * The input has parameters and (possibly implicit) existential variables.
6660 * The output has the same parameters, but only
6661 * explicit existentially quantified variables.
6663 * The actual projection is performed by pip, but pip doesn't seem
6664 * to like equalities very much, so we first remove the equalities
6665 * among the parameters by performing a variable compression on
6666 * the parameters. Afterward, an inverse transformation is performed
6667 * and the equalities among the parameters are inserted back in.
6669 * The variable compression on the parameters may uncover additional
6670 * equalities that were only implicit before. We therefore check
6671 * if there are any new parameter equalities in the result and
6672 * if so recurse. The removal of parameter equalities is required
6673 * for the parameter compression performed by base_compute_divs.
6675 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6679 struct isl_mat
*T
, *T2
;
6680 struct isl_set
*set
;
6683 bset
= isl_basic_set_cow(bset
);
6687 if (bset
->n_eq
== 0)
6688 return base_compute_divs(bset
);
6690 bset
= isl_basic_set_gauss(bset
, NULL
);
6693 if (isl_basic_set_plain_is_empty(bset
))
6694 return isl_set_from_basic_set(bset
);
6696 i
= first_parameter_equality(bset
);
6697 if (i
== bset
->n_eq
)
6698 return base_compute_divs(bset
);
6700 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6701 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6703 eq
= isl_mat_cow(eq
);
6704 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6705 if (T
&& T
->n_col
== 0) {
6709 bset
= isl_basic_set_set_to_empty(bset
);
6710 return isl_set_from_basic_set(bset
);
6712 bset
= basic_set_parameter_preimage(bset
, T
);
6714 i
= first_parameter_equality(bset
);
6717 else if (i
== bset
->n_eq
)
6718 set
= base_compute_divs(bset
);
6720 set
= parameter_compute_divs(bset
);
6721 set
= set_parameter_preimage(set
, T2
);
6722 set
= set_append_equalities(set
, eq
);
6726 /* Insert the divs from "ls" before those of "bmap".
6728 * The number of columns is not changed, which means that the last
6729 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6730 * The caller is responsible for removing the same number of dimensions
6731 * from the space of "bmap".
6733 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6734 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6740 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6744 old_n_div
= bmap
->n_div
;
6745 bmap
= insert_div_rows(bmap
, n_div
);
6749 for (i
= 0; i
< n_div
; ++i
) {
6750 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6751 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6757 /* Replace the space of "bmap" by the space and divs of "ls".
6759 * If "ls" has any divs, then we simplify the result since we may
6760 * have discovered some additional equalities that could simplify
6761 * the div expressions.
6763 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6764 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6768 bmap
= isl_basic_map_cow(bmap
);
6772 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6773 bmap
= insert_divs_from_local_space(bmap
, ls
);
6777 isl_space_free(bmap
->dim
);
6778 bmap
->dim
= isl_local_space_get_space(ls
);
6782 isl_local_space_free(ls
);
6784 bmap
= isl_basic_map_simplify(bmap
);
6785 bmap
= isl_basic_map_finalize(bmap
);
6788 isl_basic_map_free(bmap
);
6789 isl_local_space_free(ls
);
6793 /* Replace the space of "map" by the space and divs of "ls".
6795 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6796 __isl_take isl_local_space
*ls
)
6800 map
= isl_map_cow(map
);
6804 for (i
= 0; i
< map
->n
; ++i
) {
6805 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6806 isl_local_space_copy(ls
));
6810 isl_space_free(map
->dim
);
6811 map
->dim
= isl_local_space_get_space(ls
);
6815 isl_local_space_free(ls
);
6818 isl_local_space_free(ls
);
6823 /* Compute an explicit representation for the existentially
6824 * quantified variables for which do not know any explicit representation yet.
6826 * We first sort the existentially quantified variables so that the
6827 * existentially quantified variables for which we already have an explicit
6828 * representation are placed before those for which we do not.
6829 * The input dimensions, the output dimensions and the existentially
6830 * quantified variables for which we already have an explicit
6831 * representation are then turned into parameters.
6832 * compute_divs returns a map with the same parameters and
6833 * no input or output dimensions and the dimension specification
6834 * is reset to that of the input, including the existentially quantified
6835 * variables for which we already had an explicit representation.
6837 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6839 struct isl_basic_set
*bset
;
6840 struct isl_set
*set
;
6841 struct isl_map
*map
;
6843 isl_local_space
*ls
;
6850 bmap
= isl_basic_map_sort_divs(bmap
);
6851 bmap
= isl_basic_map_cow(bmap
);
6855 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6856 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6859 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6860 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6861 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6862 dim
= isl_space_set_alloc(bmap
->ctx
,
6863 nparam
+ n_in
+ n_out
+ n_known
, 0);
6867 ls
= isl_basic_map_get_local_space(bmap
);
6868 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6869 n_known
, bmap
->n_div
- n_known
);
6871 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6872 swap_div(bmap
, i
- n_known
, i
);
6873 bmap
->n_div
-= n_known
;
6874 bmap
->extra
-= n_known
;
6876 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6877 bset
= (struct isl_basic_set
*)bmap
;
6879 set
= parameter_compute_divs(bset
);
6880 map
= (struct isl_map
*)set
;
6881 map
= replace_space_by_local_space(map
, ls
);
6885 isl_basic_map_free(bmap
);
6889 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6897 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6898 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6899 if (isl_int_is_zero(bmap
->div
[i
][0]))
6901 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6907 static int map_divs_known(__isl_keep isl_map
*map
)
6914 for (i
= 0; i
< map
->n
; ++i
) {
6915 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6923 /* If bmap contains any unknown divs, then compute explicit
6924 * expressions for them. However, this computation may be
6925 * quite expensive, so first try to remove divs that aren't
6928 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6931 struct isl_map
*map
;
6933 known
= isl_basic_map_divs_known(bmap
);
6937 return isl_map_from_basic_map(bmap
);
6939 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6941 known
= isl_basic_map_divs_known(bmap
);
6945 return isl_map_from_basic_map(bmap
);
6947 map
= compute_divs(bmap
);
6950 isl_basic_map_free(bmap
);
6954 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6958 struct isl_map
*res
;
6965 known
= map_divs_known(map
);
6973 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6974 for (i
= 1 ; i
< map
->n
; ++i
) {
6976 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6977 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6978 res
= isl_map_union_disjoint(res
, r2
);
6980 res
= isl_map_union(res
, r2
);
6987 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6989 return (struct isl_set
*)
6990 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6993 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6995 return (struct isl_set
*)
6996 isl_map_compute_divs((struct isl_map
*)set
);
6999 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7002 struct isl_set
*set
;
7007 map
= isl_map_cow(map
);
7011 set
= (struct isl_set
*)map
;
7012 set
->dim
= isl_space_domain(set
->dim
);
7015 for (i
= 0; i
< map
->n
; ++i
) {
7016 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7020 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7021 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7028 /* Return the union of "map1" and "map2", where we assume for now that
7029 * "map1" and "map2" are disjoint. Note that the basic maps inside
7030 * "map1" or "map2" may not be disjoint from each other.
7031 * Also note that this function is also called from isl_map_union,
7032 * which takes care of handling the situation where "map1" and "map2"
7033 * may not be disjoint.
7035 * If one of the inputs is empty, we can simply return the other input.
7036 * Similarly, if one of the inputs is universal, then it is equal to the union.
7038 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7039 __isl_take isl_map
*map2
)
7043 struct isl_map
*map
= NULL
;
7049 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7050 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7051 "spaces don't match", goto error
);
7062 is_universe
= isl_map_plain_is_universe(map1
);
7063 if (is_universe
< 0)
7070 is_universe
= isl_map_plain_is_universe(map2
);
7071 if (is_universe
< 0)
7078 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7079 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7080 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7082 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7083 map1
->n
+ map2
->n
, flags
);
7086 for (i
= 0; i
< map1
->n
; ++i
) {
7087 map
= isl_map_add_basic_map(map
,
7088 isl_basic_map_copy(map1
->p
[i
]));
7092 for (i
= 0; i
< map2
->n
; ++i
) {
7093 map
= isl_map_add_basic_map(map
,
7094 isl_basic_map_copy(map2
->p
[i
]));
7108 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7109 * guaranteed to be disjoint by the caller.
7111 * Note that this functions is called from within isl_map_make_disjoint,
7112 * so we have to be careful not to touch the constraints of the inputs
7115 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7116 __isl_take isl_map
*map2
)
7118 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7121 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7122 * not be disjoint. The parameters are assumed to have been aligned.
7124 * We currently simply call map_union_disjoint, the internal operation
7125 * of which does not really depend on the inputs being disjoint.
7126 * If the result contains more than one basic map, then we clear
7127 * the disjoint flag since the result may contain basic maps from
7128 * both inputs and these are not guaranteed to be disjoint.
7130 * As a special case, if "map1" and "map2" are obviously equal,
7131 * then we simply return "map1".
7133 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7134 __isl_take isl_map
*map2
)
7141 equal
= isl_map_plain_is_equal(map1
, map2
);
7149 map1
= map_union_disjoint(map1
, map2
);
7153 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7161 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7164 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7165 __isl_take isl_map
*map2
)
7167 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7170 struct isl_set
*isl_set_union_disjoint(
7171 struct isl_set
*set1
, struct isl_set
*set2
)
7173 return (struct isl_set
*)
7174 isl_map_union_disjoint(
7175 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7178 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7180 return (struct isl_set
*)
7181 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7184 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7187 * "map" and "set" are assumed to be compatible and non-NULL.
7189 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7190 __isl_take isl_set
*set
,
7191 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7192 __isl_take isl_basic_set
*bset
))
7195 struct isl_map
*result
;
7198 if (isl_set_plain_is_universe(set
)) {
7203 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7204 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7205 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7207 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7208 map
->n
* set
->n
, flags
);
7209 for (i
= 0; result
&& i
< map
->n
; ++i
)
7210 for (j
= 0; j
< set
->n
; ++j
) {
7211 result
= isl_map_add_basic_map(result
,
7212 fn(isl_basic_map_copy(map
->p
[i
]),
7213 isl_basic_set_copy(set
->p
[j
])));
7223 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7224 __isl_take isl_set
*set
)
7229 if (!isl_map_compatible_range(map
, set
))
7230 isl_die(set
->ctx
, isl_error_invalid
,
7231 "incompatible spaces", goto error
);
7233 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7240 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7241 __isl_take isl_set
*set
)
7243 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7246 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7247 __isl_take isl_set
*set
)
7252 if (!isl_map_compatible_domain(map
, set
))
7253 isl_die(set
->ctx
, isl_error_invalid
,
7254 "incompatible spaces", goto error
);
7256 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7263 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7264 __isl_take isl_set
*set
)
7266 return isl_map_align_params_map_map_and(map
, set
,
7267 &map_intersect_domain
);
7270 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7271 __isl_take isl_map
*map2
)
7275 map1
= isl_map_reverse(map1
);
7276 map1
= isl_map_apply_range(map1
, map2
);
7277 return isl_map_reverse(map1
);
7284 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7285 __isl_take isl_map
*map2
)
7287 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7290 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7291 __isl_take isl_map
*map2
)
7293 isl_space
*dim_result
;
7294 struct isl_map
*result
;
7300 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7301 isl_space_copy(map2
->dim
));
7303 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7306 for (i
= 0; i
< map1
->n
; ++i
)
7307 for (j
= 0; j
< map2
->n
; ++j
) {
7308 result
= isl_map_add_basic_map(result
,
7309 isl_basic_map_apply_range(
7310 isl_basic_map_copy(map1
->p
[i
]),
7311 isl_basic_map_copy(map2
->p
[j
])));
7317 if (result
&& result
->n
<= 1)
7318 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7326 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7327 __isl_take isl_map
*map2
)
7329 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7333 * returns range - domain
7335 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7337 isl_space
*target_space
;
7338 struct isl_basic_set
*bset
;
7345 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7346 bmap
->dim
, isl_dim_out
),
7348 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7349 dim
= isl_basic_map_n_in(bmap
);
7350 nparam
= isl_basic_map_n_param(bmap
);
7351 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7352 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7353 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7354 for (i
= 0; i
< dim
; ++i
) {
7355 int j
= isl_basic_map_alloc_equality(bmap
);
7357 bmap
= isl_basic_map_free(bmap
);
7360 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7361 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7362 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7363 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7365 bset
= isl_basic_map_domain(bmap
);
7366 bset
= isl_basic_set_reset_space(bset
, target_space
);
7369 isl_basic_map_free(bmap
);
7374 * returns range - domain
7376 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7380 struct isl_set
*result
;
7385 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7386 map
->dim
, isl_dim_out
),
7388 dim
= isl_map_get_space(map
);
7389 dim
= isl_space_domain(dim
);
7390 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7393 for (i
= 0; i
< map
->n
; ++i
)
7394 result
= isl_set_add_basic_set(result
,
7395 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7404 * returns [domain -> range] -> range - domain
7406 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7407 __isl_take isl_basic_map
*bmap
)
7411 isl_basic_map
*domain
;
7415 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7416 bmap
->dim
, isl_dim_out
))
7417 isl_die(bmap
->ctx
, isl_error_invalid
,
7418 "domain and range don't match", goto error
);
7420 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7421 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7423 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7424 domain
= isl_basic_map_universe(dim
);
7426 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7427 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7428 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7430 total
= isl_basic_map_total_dim(bmap
);
7432 for (i
= 0; i
< n
; ++i
) {
7433 k
= isl_basic_map_alloc_equality(bmap
);
7436 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7437 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7438 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7439 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7442 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7443 return isl_basic_map_finalize(bmap
);
7445 isl_basic_map_free(bmap
);
7450 * returns [domain -> range] -> range - domain
7452 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7455 isl_space
*domain_dim
;
7460 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7461 map
->dim
, isl_dim_out
))
7462 isl_die(map
->ctx
, isl_error_invalid
,
7463 "domain and range don't match", goto error
);
7465 map
= isl_map_cow(map
);
7469 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7470 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7471 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7474 for (i
= 0; i
< map
->n
; ++i
) {
7475 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7479 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7486 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7488 struct isl_basic_map
*bmap
;
7496 nparam
= dims
->nparam
;
7498 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7502 for (i
= 0; i
< dim
; ++i
) {
7503 int j
= isl_basic_map_alloc_equality(bmap
);
7506 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7507 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7508 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7510 return isl_basic_map_finalize(bmap
);
7512 isl_basic_map_free(bmap
);
7516 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7520 if (dim
->n_in
!= dim
->n_out
)
7521 isl_die(dim
->ctx
, isl_error_invalid
,
7522 "number of input and output dimensions needs to be "
7523 "the same", goto error
);
7524 return basic_map_identity(dim
);
7526 isl_space_free(dim
);
7530 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7532 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7535 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7537 isl_space
*dim
= isl_set_get_space(set
);
7539 id
= isl_map_identity(isl_space_map_from_set(dim
));
7540 return isl_map_intersect_range(id
, set
);
7543 /* Construct a basic set with all set dimensions having only non-negative
7546 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7547 __isl_take isl_space
*space
)
7552 struct isl_basic_set
*bset
;
7556 nparam
= space
->nparam
;
7558 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7561 for (i
= 0; i
< dim
; ++i
) {
7562 int k
= isl_basic_set_alloc_inequality(bset
);
7565 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7566 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7570 isl_basic_set_free(bset
);
7574 /* Construct the half-space x_pos >= 0.
7576 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7580 isl_basic_set
*nonneg
;
7582 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7583 k
= isl_basic_set_alloc_inequality(nonneg
);
7586 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7587 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7589 return isl_basic_set_finalize(nonneg
);
7591 isl_basic_set_free(nonneg
);
7595 /* Construct the half-space x_pos <= -1.
7597 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7602 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7603 k
= isl_basic_set_alloc_inequality(neg
);
7606 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7607 isl_int_set_si(neg
->ineq
[k
][0], -1);
7608 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7610 return isl_basic_set_finalize(neg
);
7612 isl_basic_set_free(neg
);
7616 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7617 enum isl_dim_type type
, unsigned first
, unsigned n
)
7621 isl_basic_set
*nonneg
;
7629 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7631 offset
= pos(set
->dim
, type
);
7632 for (i
= 0; i
< n
; ++i
) {
7633 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7634 offset
+ first
+ i
);
7635 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7637 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7646 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7647 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7654 if (isl_set_plain_is_empty(set
)) {
7659 return fn(set
, signs
, user
);
7662 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7664 half
= isl_set_intersect(half
, isl_set_copy(set
));
7665 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7669 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7671 half
= isl_set_intersect(half
, set
);
7672 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7678 /* Call "fn" on the intersections of "set" with each of the orthants
7679 * (except for obviously empty intersections). The orthant is identified
7680 * by the signs array, with each entry having value 1 or -1 according
7681 * to the sign of the corresponding variable.
7683 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7684 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7694 if (isl_set_plain_is_empty(set
))
7697 nparam
= isl_set_dim(set
, isl_dim_param
);
7698 nvar
= isl_set_dim(set
, isl_dim_set
);
7700 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7702 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7710 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7712 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7715 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7716 __isl_keep isl_basic_map
*bmap2
)
7719 struct isl_map
*map1
;
7720 struct isl_map
*map2
;
7722 if (!bmap1
|| !bmap2
)
7723 return isl_bool_error
;
7725 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7726 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7728 is_subset
= isl_map_is_subset(map1
, map2
);
7736 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7737 __isl_keep isl_basic_set
*bset2
)
7739 return isl_basic_map_is_subset(bset1
, bset2
);
7742 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7743 __isl_keep isl_basic_map
*bmap2
)
7747 if (!bmap1
|| !bmap2
)
7748 return isl_bool_error
;
7749 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7750 if (is_subset
!= isl_bool_true
)
7752 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7756 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7757 __isl_keep isl_basic_set
*bset2
)
7759 return isl_basic_map_is_equal(
7760 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7763 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7769 return isl_bool_error
;
7770 for (i
= 0; i
< map
->n
; ++i
) {
7771 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7773 return isl_bool_error
;
7775 return isl_bool_false
;
7777 return isl_bool_true
;
7780 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7782 return map
? map
->n
== 0 : isl_bool_error
;
7785 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7787 return set
? set
->n
== 0 : isl_bool_error
;
7790 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7792 return isl_map_is_empty((struct isl_map
*)set
);
7795 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7800 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7803 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7808 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7811 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7816 return isl_bool_error
;
7817 is_subset
= isl_map_is_subset(map1
, map2
);
7818 if (is_subset
!= isl_bool_true
)
7820 is_subset
= isl_map_is_subset(map2
, map1
);
7824 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7826 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7829 isl_bool
isl_basic_map_is_strict_subset(
7830 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7834 if (!bmap1
|| !bmap2
)
7835 return isl_bool_error
;
7836 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7837 if (is_subset
!= isl_bool_true
)
7839 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7840 if (is_subset
== isl_bool_error
)
7845 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7846 __isl_keep isl_map
*map2
)
7851 return isl_bool_error
;
7852 is_subset
= isl_map_is_subset(map1
, map2
);
7853 if (is_subset
!= isl_bool_true
)
7855 is_subset
= isl_map_is_subset(map2
, map1
);
7856 if (is_subset
== isl_bool_error
)
7861 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
7862 __isl_keep isl_set
*set2
)
7864 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7867 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
7870 return isl_bool_error
;
7871 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7874 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
7877 return isl_bool_error
;
7878 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7881 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7886 return isl_bool_error
;
7888 for (i
= 0; i
< map
->n
; ++i
) {
7889 isl_bool r
= isl_basic_map_is_universe(map
->p
[i
]);
7894 return isl_bool_false
;
7897 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7899 return isl_map_plain_is_universe((isl_map
*) set
);
7902 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
7904 struct isl_basic_set
*bset
= NULL
;
7905 struct isl_vec
*sample
= NULL
;
7910 return isl_bool_error
;
7912 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7913 return isl_bool_true
;
7915 if (isl_basic_map_is_universe(bmap
))
7916 return isl_bool_false
;
7918 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7919 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7920 copy
= isl_basic_map_remove_redundancies(copy
);
7921 empty
= isl_basic_map_plain_is_empty(copy
);
7922 isl_basic_map_free(copy
);
7926 total
= 1 + isl_basic_map_total_dim(bmap
);
7927 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7928 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7930 return isl_bool_error
;
7932 return isl_bool_false
;
7934 isl_vec_free(bmap
->sample
);
7935 bmap
->sample
= NULL
;
7936 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7938 return isl_bool_error
;
7939 sample
= isl_basic_set_sample_vec(bset
);
7941 return isl_bool_error
;
7942 empty
= sample
->size
== 0;
7943 isl_vec_free(bmap
->sample
);
7944 bmap
->sample
= sample
;
7946 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7951 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7954 return isl_bool_error
;
7955 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7958 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7961 return isl_bool_error
;
7962 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7965 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
7967 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7970 struct isl_map
*isl_basic_map_union(
7971 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7973 struct isl_map
*map
;
7974 if (!bmap1
|| !bmap2
)
7977 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7979 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7982 map
= isl_map_add_basic_map(map
, bmap1
);
7983 map
= isl_map_add_basic_map(map
, bmap2
);
7986 isl_basic_map_free(bmap1
);
7987 isl_basic_map_free(bmap2
);
7991 struct isl_set
*isl_basic_set_union(
7992 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7994 return (struct isl_set
*)isl_basic_map_union(
7995 (struct isl_basic_map
*)bset1
,
7996 (struct isl_basic_map
*)bset2
);
7999 /* Order divs such that any div only depends on previous divs */
8000 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8008 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8010 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8012 if (isl_int_is_zero(bmap
->div
[i
][0]))
8014 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8018 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8024 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8026 return (struct isl_basic_set
*)
8027 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8030 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8037 for (i
= 0; i
< map
->n
; ++i
) {
8038 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8049 /* Apply the expansion computed by isl_merge_divs.
8050 * The expansion itself is given by "exp" while the resulting
8051 * list of divs is given by "div".
8053 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8054 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8059 bset
= isl_basic_set_cow(bset
);
8063 if (div
->n_row
< bset
->n_div
)
8064 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8065 "not an expansion", goto error
);
8067 n_div
= bset
->n_div
;
8068 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8069 div
->n_row
- n_div
, 0,
8070 2 * (div
->n_row
- n_div
));
8072 for (i
= n_div
; i
< div
->n_row
; ++i
)
8073 if (isl_basic_set_alloc_div(bset
) < 0)
8077 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8078 if (j
>= 0 && exp
[j
] == i
) {
8080 isl_basic_map_swap_div(bset
, i
, j
);
8083 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8084 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8092 isl_basic_set_free(bset
);
8097 /* Look for a div in dst that corresponds to the div "div" in src.
8098 * The divs before "div" in src and dst are assumed to be the same.
8100 * Returns -1 if no corresponding div was found and the position
8101 * of the corresponding div in dst otherwise.
8103 static int find_div(struct isl_basic_map
*dst
,
8104 struct isl_basic_map
*src
, unsigned div
)
8108 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8110 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8111 for (i
= div
; i
< dst
->n_div
; ++i
)
8112 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8113 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8114 dst
->n_div
- div
) == -1)
8119 /* Align the divs of "dst" to those of "src", adding divs from "src"
8120 * if needed. That is, make sure that the first src->n_div divs
8121 * of the result are equal to those of src.
8123 * The result is not finalized as by design it will have redundant
8124 * divs if any divs from "src" were copied.
8126 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8127 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8130 int known
, extended
;
8134 return isl_basic_map_free(dst
);
8136 if (src
->n_div
== 0)
8139 known
= isl_basic_map_divs_known(src
);
8141 return isl_basic_map_free(dst
);
8143 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8144 "some src divs are unknown",
8145 return isl_basic_map_free(dst
));
8147 src
= isl_basic_map_order_divs(src
);
8150 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8151 for (i
= 0; i
< src
->n_div
; ++i
) {
8152 int j
= find_div(dst
, src
, i
);
8155 int extra
= src
->n_div
- i
;
8156 dst
= isl_basic_map_cow(dst
);
8159 dst
= isl_basic_map_extend_space(dst
,
8160 isl_space_copy(dst
->dim
),
8161 extra
, 0, 2 * extra
);
8164 j
= isl_basic_map_alloc_div(dst
);
8166 return isl_basic_map_free(dst
);
8167 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8168 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8169 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8170 return isl_basic_map_free(dst
);
8173 isl_basic_map_swap_div(dst
, i
, j
);
8178 struct isl_basic_set
*isl_basic_set_align_divs(
8179 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8181 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8182 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8185 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8193 map
= isl_map_compute_divs(map
);
8194 map
= isl_map_cow(map
);
8198 for (i
= 1; i
< map
->n
; ++i
)
8199 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8200 for (i
= 1; i
< map
->n
; ++i
) {
8201 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8203 return isl_map_free(map
);
8206 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8210 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8212 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8215 /* Align the divs of the basic maps in "map" to those
8216 * of the basic maps in "list", as well as to the other basic maps in "map".
8217 * The elements in "list" are assumed to have known divs.
8219 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8220 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8224 map
= isl_map_compute_divs(map
);
8225 map
= isl_map_cow(map
);
8227 return isl_map_free(map
);
8231 n
= isl_basic_map_list_n_basic_map(list
);
8232 for (i
= 0; i
< n
; ++i
) {
8233 isl_basic_map
*bmap
;
8235 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8236 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8237 isl_basic_map_free(bmap
);
8240 return isl_map_free(map
);
8242 return isl_map_align_divs(map
);
8245 /* Align the divs of each element of "list" to those of "bmap".
8246 * Both "bmap" and the elements of "list" are assumed to have known divs.
8248 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8249 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8254 return isl_basic_map_list_free(list
);
8256 n
= isl_basic_map_list_n_basic_map(list
);
8257 for (i
= 0; i
< n
; ++i
) {
8258 isl_basic_map
*bmap_i
;
8260 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8261 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8262 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8268 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8269 __isl_take isl_map
*map
)
8273 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8274 map
= isl_map_intersect_domain(map
, set
);
8275 set
= isl_map_range(map
);
8283 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8284 __isl_take isl_map
*map
)
8286 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8289 /* There is no need to cow as removing empty parts doesn't change
8290 * the meaning of the set.
8292 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8299 for (i
= map
->n
- 1; i
>= 0; --i
)
8300 remove_if_empty(map
, i
);
8305 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8307 return (struct isl_set
*)
8308 isl_map_remove_empty_parts((struct isl_map
*)set
);
8311 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8313 struct isl_basic_map
*bmap
;
8314 if (!map
|| map
->n
== 0)
8316 bmap
= map
->p
[map
->n
-1];
8317 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8318 return isl_basic_map_copy(bmap
);
8321 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8323 return (struct isl_basic_set
*)
8324 isl_map_copy_basic_map((struct isl_map
*)set
);
8327 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8328 __isl_keep isl_basic_map
*bmap
)
8334 for (i
= map
->n
-1; i
>= 0; --i
) {
8335 if (map
->p
[i
] != bmap
)
8337 map
= isl_map_cow(map
);
8340 isl_basic_map_free(map
->p
[i
]);
8341 if (i
!= map
->n
-1) {
8342 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8343 map
->p
[i
] = map
->p
[map
->n
-1];
8354 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8355 struct isl_basic_set
*bset
)
8357 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8358 (struct isl_basic_map
*)bset
);
8361 /* Given two basic sets bset1 and bset2, compute the maximal difference
8362 * between the values of dimension pos in bset1 and those in bset2
8363 * for any common value of the parameters and dimensions preceding pos.
8365 static enum isl_lp_result
basic_set_maximal_difference_at(
8366 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8367 int pos
, isl_int
*opt
)
8370 struct isl_basic_map
*bmap1
= NULL
;
8371 struct isl_basic_map
*bmap2
= NULL
;
8372 struct isl_ctx
*ctx
;
8373 struct isl_vec
*obj
;
8376 unsigned dim1
, dim2
;
8377 enum isl_lp_result res
;
8379 if (!bset1
|| !bset2
)
8380 return isl_lp_error
;
8382 nparam
= isl_basic_set_n_param(bset1
);
8383 dim1
= isl_basic_set_n_dim(bset1
);
8384 dim2
= isl_basic_set_n_dim(bset2
);
8385 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8386 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8387 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8388 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8389 if (!bmap1
|| !bmap2
)
8391 bmap1
= isl_basic_map_cow(bmap1
);
8392 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8393 pos
, (dim1
- pos
) + (dim2
- pos
),
8394 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8395 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8398 total
= isl_basic_map_total_dim(bmap1
);
8400 obj
= isl_vec_alloc(ctx
, 1 + total
);
8403 isl_seq_clr(obj
->block
.data
, 1 + total
);
8404 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8405 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8406 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8408 isl_basic_map_free(bmap1
);
8412 isl_basic_map_free(bmap2
);
8414 isl_basic_map_free(bmap1
);
8415 return isl_lp_error
;
8418 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8419 * for any common value of the parameters and dimensions preceding pos
8420 * in both basic sets, the values of dimension pos in bset1 are
8421 * smaller or larger than those in bset2.
8424 * 1 if bset1 follows bset2
8425 * -1 if bset1 precedes bset2
8426 * 0 if bset1 and bset2 are incomparable
8427 * -2 if some error occurred.
8429 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8430 struct isl_basic_set
*bset2
, int pos
)
8433 enum isl_lp_result res
;
8438 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8440 if (res
== isl_lp_empty
)
8442 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8443 res
== isl_lp_unbounded
)
8445 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8454 /* Given two basic sets bset1 and bset2, check whether
8455 * for any common value of the parameters and dimensions preceding pos
8456 * there is a value of dimension pos in bset1 that is larger
8457 * than a value of the same dimension in bset2.
8460 * 1 if there exists such a pair
8461 * 0 if there is no such pair, but there is a pair of equal values
8463 * -2 if some error occurred.
8465 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8466 __isl_keep isl_basic_set
*bset2
, int pos
)
8469 enum isl_lp_result res
;
8474 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8476 if (res
== isl_lp_empty
)
8478 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8479 res
== isl_lp_unbounded
)
8481 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8483 else if (res
== isl_lp_ok
)
8492 /* Given two sets set1 and set2, check whether
8493 * for any common value of the parameters and dimensions preceding pos
8494 * there is a value of dimension pos in set1 that is larger
8495 * than a value of the same dimension in set2.
8498 * 1 if there exists such a pair
8499 * 0 if there is no such pair, but there is a pair of equal values
8501 * -2 if some error occurred.
8503 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8504 __isl_keep isl_set
*set2
, int pos
)
8512 for (i
= 0; i
< set1
->n
; ++i
)
8513 for (j
= 0; j
< set2
->n
; ++j
) {
8515 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8516 if (f
== 1 || f
== -2)
8525 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8526 unsigned pos
, isl_int
*val
)
8534 total
= isl_basic_map_total_dim(bmap
);
8535 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8536 for (; d
+1 > pos
; --d
)
8537 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8541 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8543 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8545 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8548 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8554 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8555 unsigned pos
, isl_int
*val
)
8567 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8570 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8571 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8572 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8573 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8577 isl_int_set(*val
, v
);
8583 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8584 unsigned pos
, isl_int
*val
)
8586 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8590 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8593 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8596 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8597 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8599 if (pos
>= isl_basic_map_dim(bmap
, type
))
8601 return isl_basic_map_plain_has_fixed_var(bmap
,
8602 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8605 /* If "bmap" obviously lies on a hyperplane where the given dimension
8606 * has a fixed value, then return that value.
8607 * Otherwise return NaN.
8609 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8610 __isl_keep isl_basic_map
*bmap
,
8611 enum isl_dim_type type
, unsigned pos
)
8619 ctx
= isl_basic_map_get_ctx(bmap
);
8620 v
= isl_val_alloc(ctx
);
8623 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8625 return isl_val_free(v
);
8627 isl_int_set_si(v
->d
, 1);
8631 return isl_val_nan(ctx
);
8634 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8635 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8637 if (pos
>= isl_map_dim(map
, type
))
8639 return isl_map_plain_has_fixed_var(map
,
8640 map_offset(map
, type
) - 1 + pos
, val
);
8643 /* If "map" obviously lies on a hyperplane where the given dimension
8644 * has a fixed value, then return that value.
8645 * Otherwise return NaN.
8647 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8648 enum isl_dim_type type
, unsigned pos
)
8656 ctx
= isl_map_get_ctx(map
);
8657 v
= isl_val_alloc(ctx
);
8660 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8662 return isl_val_free(v
);
8664 isl_int_set_si(v
->d
, 1);
8668 return isl_val_nan(ctx
);
8671 /* If "set" obviously lies on a hyperplane where the given dimension
8672 * has a fixed value, then return that value.
8673 * Otherwise return NaN.
8675 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8676 enum isl_dim_type type
, unsigned pos
)
8678 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8681 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8682 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8684 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8687 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8688 * then return this fixed value in *val.
8690 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8691 unsigned dim
, isl_int
*val
)
8693 return isl_basic_set_plain_has_fixed_var(bset
,
8694 isl_basic_set_n_param(bset
) + dim
, val
);
8697 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8698 * then return this fixed value in *val.
8700 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8701 unsigned dim
, isl_int
*val
)
8703 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8706 /* Check if input variable in has fixed value and if so and if val is not NULL,
8707 * then return this fixed value in *val.
8709 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8710 unsigned in
, isl_int
*val
)
8712 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8715 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8716 * and if val is not NULL, then return this lower bound in *val.
8718 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8719 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8721 int i
, i_eq
= -1, i_ineq
= -1;
8728 total
= isl_basic_set_total_dim(bset
);
8729 nparam
= isl_basic_set_n_param(bset
);
8730 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8731 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8737 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8738 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8740 if (i_eq
!= -1 || i_ineq
!= -1)
8744 if (i_eq
== -1 && i_ineq
== -1)
8746 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8747 /* The coefficient should always be one due to normalization. */
8748 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8750 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8752 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8753 total
- nparam
- dim
- 1) != -1)
8756 isl_int_neg(*val
, c
[0]);
8760 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8761 unsigned dim
, isl_int
*val
)
8773 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8777 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8779 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8780 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8782 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8786 isl_int_set(*val
, v
);
8792 /* uset_gist depends on constraints without existentially quantified
8793 * variables sorting first.
8795 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8797 isl_int
**c1
= (isl_int
**) p1
;
8798 isl_int
**c2
= (isl_int
**) p2
;
8800 unsigned size
= *(unsigned *) arg
;
8802 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8803 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8808 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8811 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8812 struct isl_basic_map
*bmap
)
8818 if (bmap
->n_ineq
== 0)
8820 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8822 total
= isl_basic_map_total_dim(bmap
);
8823 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8824 &sort_constraint_cmp
, &total
) < 0)
8825 return isl_basic_map_free(bmap
);
8829 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8830 __isl_take isl_basic_set
*bset
)
8832 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8833 (struct isl_basic_map
*)bset
);
8836 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8840 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8842 bmap
= isl_basic_map_remove_redundancies(bmap
);
8843 bmap
= isl_basic_map_sort_constraints(bmap
);
8845 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8849 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8851 return (struct isl_basic_set
*)isl_basic_map_normalize(
8852 (struct isl_basic_map
*)bset
);
8855 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8856 const __isl_keep isl_basic_map
*bmap2
)
8861 if (!bmap1
|| !bmap2
)
8866 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8867 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8868 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8869 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8870 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8871 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8872 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8873 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8874 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8875 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8876 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8878 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8880 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8882 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8883 return bmap1
->n_eq
- bmap2
->n_eq
;
8884 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8885 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8886 if (bmap1
->n_div
!= bmap2
->n_div
)
8887 return bmap1
->n_div
- bmap2
->n_div
;
8888 total
= isl_basic_map_total_dim(bmap1
);
8889 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8890 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8894 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8895 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8899 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8900 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8907 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8908 const __isl_keep isl_basic_set
*bset2
)
8910 return isl_basic_map_plain_cmp(bset1
, bset2
);
8913 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8919 if (set1
->n
!= set2
->n
)
8920 return set1
->n
- set2
->n
;
8922 for (i
= 0; i
< set1
->n
; ++i
) {
8923 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8931 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8932 __isl_keep isl_basic_map
*bmap2
)
8934 if (!bmap1
|| !bmap2
)
8935 return isl_bool_error
;
8936 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8939 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8940 __isl_keep isl_basic_set
*bset2
)
8942 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8943 (isl_basic_map
*)bset2
);
8946 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8948 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8949 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8951 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8954 /* Sort the basic maps of "map" and remove duplicate basic maps.
8956 * While removing basic maps, we make sure that the basic maps remain
8957 * sorted because isl_map_normalize expects the basic maps of the result
8960 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
8964 map
= isl_map_remove_empty_parts(map
);
8967 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8968 for (i
= map
->n
- 1; i
>= 1; --i
) {
8969 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
8971 isl_basic_map_free(map
->p
[i
-1]);
8972 for (j
= i
; j
< map
->n
; ++j
)
8973 map
->p
[j
- 1] = map
->p
[j
];
8980 /* Remove obvious duplicates among the basic maps of "map".
8982 * Unlike isl_map_normalize, this function does not remove redundant
8983 * constraints and only removes duplicates that have exactly the same
8984 * constraints in the input. It does sort the constraints and
8985 * the basic maps to ease the detection of duplicates.
8987 * If "map" has already been normalized or if the basic maps are
8988 * disjoint, then there can be no duplicates.
8990 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
8993 isl_basic_map
*bmap
;
8999 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9001 for (i
= 0; i
< map
->n
; ++i
) {
9002 bmap
= isl_basic_map_copy(map
->p
[i
]);
9003 bmap
= isl_basic_map_sort_constraints(bmap
);
9005 return isl_map_free(map
);
9006 isl_basic_map_free(map
->p
[i
]);
9010 map
= sort_and_remove_duplicates(map
);
9014 /* We normalize in place, but if anything goes wrong we need
9015 * to return NULL, so we need to make sure we don't change the
9016 * meaning of any possible other copies of map.
9018 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9021 struct isl_basic_map
*bmap
;
9025 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9027 for (i
= 0; i
< map
->n
; ++i
) {
9028 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9031 isl_basic_map_free(map
->p
[i
]);
9035 map
= sort_and_remove_duplicates(map
);
9037 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9044 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9046 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9049 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9050 __isl_keep isl_map
*map2
)
9056 return isl_bool_error
;
9059 return isl_bool_true
;
9060 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9061 return isl_bool_false
;
9063 map1
= isl_map_copy(map1
);
9064 map2
= isl_map_copy(map2
);
9065 map1
= isl_map_normalize(map1
);
9066 map2
= isl_map_normalize(map2
);
9069 equal
= map1
->n
== map2
->n
;
9070 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9071 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9081 return isl_bool_error
;
9084 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9085 __isl_keep isl_set
*set2
)
9087 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9088 (struct isl_map
*)set2
);
9091 /* Return an interval that ranges from min to max (inclusive)
9093 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9094 isl_int min
, isl_int max
)
9097 struct isl_basic_set
*bset
= NULL
;
9099 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9103 k
= isl_basic_set_alloc_inequality(bset
);
9106 isl_int_set_si(bset
->ineq
[k
][1], 1);
9107 isl_int_neg(bset
->ineq
[k
][0], min
);
9109 k
= isl_basic_set_alloc_inequality(bset
);
9112 isl_int_set_si(bset
->ineq
[k
][1], -1);
9113 isl_int_set(bset
->ineq
[k
][0], max
);
9117 isl_basic_set_free(bset
);
9121 /* Return the basic maps in "map" as a list.
9123 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9124 __isl_keep isl_map
*map
)
9128 isl_basic_map_list
*list
;
9132 ctx
= isl_map_get_ctx(map
);
9133 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9135 for (i
= 0; i
< map
->n
; ++i
) {
9136 isl_basic_map
*bmap
;
9138 bmap
= isl_basic_map_copy(map
->p
[i
]);
9139 list
= isl_basic_map_list_add(list
, bmap
);
9145 /* Return the intersection of the elements in the non-empty list "list".
9146 * All elements are assumed to live in the same space.
9148 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9149 __isl_take isl_basic_map_list
*list
)
9152 isl_basic_map
*bmap
;
9156 n
= isl_basic_map_list_n_basic_map(list
);
9158 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9159 "expecting non-empty list", goto error
);
9161 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9162 for (i
= 1; i
< n
; ++i
) {
9163 isl_basic_map
*bmap_i
;
9165 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9166 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9169 isl_basic_map_list_free(list
);
9172 isl_basic_map_list_free(list
);
9176 /* Return the intersection of the elements in the non-empty list "list".
9177 * All elements are assumed to live in the same space.
9179 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9180 __isl_take isl_basic_set_list
*list
)
9182 return isl_basic_map_list_intersect(list
);
9185 /* Return the Cartesian product of the basic sets in list (in the given order).
9187 __isl_give isl_basic_set
*isl_basic_set_list_product(
9188 __isl_take
struct isl_basic_set_list
*list
)
9196 struct isl_basic_set
*product
= NULL
;
9200 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9201 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9202 nparam
= isl_basic_set_n_param(list
->p
[0]);
9203 dim
= isl_basic_set_n_dim(list
->p
[0]);
9204 extra
= list
->p
[0]->n_div
;
9205 n_eq
= list
->p
[0]->n_eq
;
9206 n_ineq
= list
->p
[0]->n_ineq
;
9207 for (i
= 1; i
< list
->n
; ++i
) {
9208 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9209 isl_assert(list
->ctx
,
9210 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9211 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9212 extra
+= list
->p
[i
]->n_div
;
9213 n_eq
+= list
->p
[i
]->n_eq
;
9214 n_ineq
+= list
->p
[i
]->n_ineq
;
9216 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9221 for (i
= 0; i
< list
->n
; ++i
) {
9222 isl_basic_set_add_constraints(product
,
9223 isl_basic_set_copy(list
->p
[i
]), dim
);
9224 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9226 isl_basic_set_list_free(list
);
9229 isl_basic_set_free(product
);
9230 isl_basic_set_list_free(list
);
9234 struct isl_basic_map
*isl_basic_map_product(
9235 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9237 isl_space
*dim_result
= NULL
;
9238 struct isl_basic_map
*bmap
;
9239 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9240 struct isl_dim_map
*dim_map1
, *dim_map2
;
9242 if (!bmap1
|| !bmap2
)
9245 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9246 bmap2
->dim
, isl_dim_param
), goto error
);
9247 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9248 isl_space_copy(bmap2
->dim
));
9250 in1
= isl_basic_map_n_in(bmap1
);
9251 in2
= isl_basic_map_n_in(bmap2
);
9252 out1
= isl_basic_map_n_out(bmap1
);
9253 out2
= isl_basic_map_n_out(bmap2
);
9254 nparam
= isl_basic_map_n_param(bmap1
);
9256 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9257 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9258 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9259 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9260 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9261 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9262 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9263 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9264 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9265 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9266 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9268 bmap
= isl_basic_map_alloc_space(dim_result
,
9269 bmap1
->n_div
+ bmap2
->n_div
,
9270 bmap1
->n_eq
+ bmap2
->n_eq
,
9271 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9272 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9273 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9274 bmap
= isl_basic_map_simplify(bmap
);
9275 return isl_basic_map_finalize(bmap
);
9277 isl_basic_map_free(bmap1
);
9278 isl_basic_map_free(bmap2
);
9282 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9283 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9285 isl_basic_map
*prod
;
9287 prod
= isl_basic_map_product(bmap1
, bmap2
);
9288 prod
= isl_basic_map_flatten(prod
);
9292 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9293 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9295 return isl_basic_map_flat_range_product(bset1
, bset2
);
9298 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9299 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9301 isl_space
*space_result
= NULL
;
9302 isl_basic_map
*bmap
;
9303 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9304 struct isl_dim_map
*dim_map1
, *dim_map2
;
9306 if (!bmap1
|| !bmap2
)
9309 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9310 isl_space_copy(bmap2
->dim
));
9312 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9313 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9314 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9315 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9317 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9318 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9319 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9320 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9321 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9322 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9323 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9324 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9325 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9326 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9327 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9329 bmap
= isl_basic_map_alloc_space(space_result
,
9330 bmap1
->n_div
+ bmap2
->n_div
,
9331 bmap1
->n_eq
+ bmap2
->n_eq
,
9332 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9333 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9334 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9335 bmap
= isl_basic_map_simplify(bmap
);
9336 return isl_basic_map_finalize(bmap
);
9338 isl_basic_map_free(bmap1
);
9339 isl_basic_map_free(bmap2
);
9343 __isl_give isl_basic_map
*isl_basic_map_range_product(
9344 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9346 isl_space
*dim_result
= NULL
;
9347 isl_basic_map
*bmap
;
9348 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9349 struct isl_dim_map
*dim_map1
, *dim_map2
;
9351 if (!bmap1
|| !bmap2
)
9354 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9355 bmap2
->dim
, isl_dim_param
))
9356 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9357 "parameters don't match", goto error
);
9359 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9360 isl_space_copy(bmap2
->dim
));
9362 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9363 out1
= isl_basic_map_n_out(bmap1
);
9364 out2
= isl_basic_map_n_out(bmap2
);
9365 nparam
= isl_basic_map_n_param(bmap1
);
9367 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9368 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9369 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9370 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9371 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9372 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9373 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9374 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9375 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9376 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9377 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9379 bmap
= isl_basic_map_alloc_space(dim_result
,
9380 bmap1
->n_div
+ bmap2
->n_div
,
9381 bmap1
->n_eq
+ bmap2
->n_eq
,
9382 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9383 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9384 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9385 bmap
= isl_basic_map_simplify(bmap
);
9386 return isl_basic_map_finalize(bmap
);
9388 isl_basic_map_free(bmap1
);
9389 isl_basic_map_free(bmap2
);
9393 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9394 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9396 isl_basic_map
*prod
;
9398 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9399 prod
= isl_basic_map_flatten_range(prod
);
9403 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9404 * and collect the results.
9405 * The result live in the space obtained by calling "space_product"
9406 * on the spaces of "map1" and "map2".
9407 * If "remove_duplicates" is set then the result may contain duplicates
9408 * (even if the inputs do not) and so we try and remove the obvious
9411 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9412 __isl_take isl_map
*map2
,
9413 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9414 __isl_take isl_space
*right
),
9415 __isl_give isl_basic_map
*(*basic_map_product
)(
9416 __isl_take isl_basic_map
*left
,
9417 __isl_take isl_basic_map
*right
),
9418 int remove_duplicates
)
9421 struct isl_map
*result
;
9427 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9428 map2
->dim
, isl_dim_param
), goto error
);
9430 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9431 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9432 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9434 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9435 isl_space_copy(map2
->dim
)),
9436 map1
->n
* map2
->n
, flags
);
9439 for (i
= 0; i
< map1
->n
; ++i
)
9440 for (j
= 0; j
< map2
->n
; ++j
) {
9441 struct isl_basic_map
*part
;
9442 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9443 isl_basic_map_copy(map2
->p
[j
]));
9444 if (isl_basic_map_is_empty(part
))
9445 isl_basic_map_free(part
);
9447 result
= isl_map_add_basic_map(result
, part
);
9451 if (remove_duplicates
)
9452 result
= isl_map_remove_obvious_duplicates(result
);
9462 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9464 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9465 __isl_take isl_map
*map2
)
9467 return map_product(map1
, map2
, &isl_space_product
,
9468 &isl_basic_map_product
, 0);
9471 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9472 __isl_take isl_map
*map2
)
9474 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9477 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9479 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9480 __isl_take isl_map
*map2
)
9484 prod
= isl_map_product(map1
, map2
);
9485 prod
= isl_map_flatten(prod
);
9489 /* Given two set A and B, construct its Cartesian product A x B.
9491 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9493 return isl_map_range_product(set1
, set2
);
9496 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9497 __isl_take isl_set
*set2
)
9499 return isl_map_flat_range_product(set1
, set2
);
9502 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9504 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9505 __isl_take isl_map
*map2
)
9507 return map_product(map1
, map2
, &isl_space_domain_product
,
9508 &isl_basic_map_domain_product
, 1);
9511 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9513 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9514 __isl_take isl_map
*map2
)
9516 return map_product(map1
, map2
, &isl_space_range_product
,
9517 &isl_basic_map_range_product
, 1);
9520 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9521 __isl_take isl_map
*map2
)
9523 return isl_map_align_params_map_map_and(map1
, map2
,
9524 &map_domain_product_aligned
);
9527 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9528 __isl_take isl_map
*map2
)
9530 return isl_map_align_params_map_map_and(map1
, map2
,
9531 &map_range_product_aligned
);
9534 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9536 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9539 int total1
, keep1
, total2
, keep2
;
9543 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9544 !isl_space_range_is_wrapping(map
->dim
))
9545 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9546 "not a product", return isl_map_free(map
));
9548 space
= isl_map_get_space(map
);
9549 total1
= isl_space_dim(space
, isl_dim_in
);
9550 total2
= isl_space_dim(space
, isl_dim_out
);
9551 space
= isl_space_factor_domain(space
);
9552 keep1
= isl_space_dim(space
, isl_dim_in
);
9553 keep2
= isl_space_dim(space
, isl_dim_out
);
9554 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9555 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9556 map
= isl_map_reset_space(map
, space
);
9561 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9563 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9566 int total1
, keep1
, total2
, keep2
;
9570 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9571 !isl_space_range_is_wrapping(map
->dim
))
9572 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9573 "not a product", return isl_map_free(map
));
9575 space
= isl_map_get_space(map
);
9576 total1
= isl_space_dim(space
, isl_dim_in
);
9577 total2
= isl_space_dim(space
, isl_dim_out
);
9578 space
= isl_space_factor_range(space
);
9579 keep1
= isl_space_dim(space
, isl_dim_in
);
9580 keep2
= isl_space_dim(space
, isl_dim_out
);
9581 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9582 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9583 map
= isl_map_reset_space(map
, space
);
9588 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9590 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9597 if (!isl_space_domain_is_wrapping(map
->dim
))
9598 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9599 "domain is not a product", return isl_map_free(map
));
9601 space
= isl_map_get_space(map
);
9602 total
= isl_space_dim(space
, isl_dim_in
);
9603 space
= isl_space_domain_factor_domain(space
);
9604 keep
= isl_space_dim(space
, isl_dim_in
);
9605 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9606 map
= isl_map_reset_space(map
, space
);
9611 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9613 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9620 if (!isl_space_domain_is_wrapping(map
->dim
))
9621 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9622 "domain is not a product", return isl_map_free(map
));
9624 space
= isl_map_get_space(map
);
9625 total
= isl_space_dim(space
, isl_dim_in
);
9626 space
= isl_space_domain_factor_range(space
);
9627 keep
= isl_space_dim(space
, isl_dim_in
);
9628 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9629 map
= isl_map_reset_space(map
, space
);
9634 /* Given a map A -> [B -> C], extract the map A -> B.
9636 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9643 if (!isl_space_range_is_wrapping(map
->dim
))
9644 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9645 "range is not a product", return isl_map_free(map
));
9647 space
= isl_map_get_space(map
);
9648 total
= isl_space_dim(space
, isl_dim_out
);
9649 space
= isl_space_range_factor_domain(space
);
9650 keep
= isl_space_dim(space
, isl_dim_out
);
9651 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9652 map
= isl_map_reset_space(map
, space
);
9657 /* Given a map A -> [B -> C], extract the map A -> C.
9659 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9666 if (!isl_space_range_is_wrapping(map
->dim
))
9667 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9668 "range is not a product", return isl_map_free(map
));
9670 space
= isl_map_get_space(map
);
9671 total
= isl_space_dim(space
, isl_dim_out
);
9672 space
= isl_space_range_factor_range(space
);
9673 keep
= isl_space_dim(space
, isl_dim_out
);
9674 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9675 map
= isl_map_reset_space(map
, space
);
9680 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9682 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9683 __isl_take isl_map
*map2
)
9687 prod
= isl_map_domain_product(map1
, map2
);
9688 prod
= isl_map_flatten_domain(prod
);
9692 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9694 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9695 __isl_take isl_map
*map2
)
9699 prod
= isl_map_range_product(map1
, map2
);
9700 prod
= isl_map_flatten_range(prod
);
9704 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9707 uint32_t hash
= isl_hash_init();
9712 bmap
= isl_basic_map_copy(bmap
);
9713 bmap
= isl_basic_map_normalize(bmap
);
9716 total
= isl_basic_map_total_dim(bmap
);
9717 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9718 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9720 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9721 isl_hash_hash(hash
, c_hash
);
9723 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9724 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9726 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9727 isl_hash_hash(hash
, c_hash
);
9729 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9730 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9732 if (isl_int_is_zero(bmap
->div
[i
][0]))
9734 isl_hash_byte(hash
, i
& 0xFF);
9735 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9736 isl_hash_hash(hash
, c_hash
);
9738 isl_basic_map_free(bmap
);
9742 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9744 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9747 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9754 map
= isl_map_copy(map
);
9755 map
= isl_map_normalize(map
);
9759 hash
= isl_hash_init();
9760 for (i
= 0; i
< map
->n
; ++i
) {
9762 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9763 isl_hash_hash(hash
, bmap_hash
);
9771 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9773 return isl_map_get_hash((isl_map
*)set
);
9776 /* Check if the value for dimension dim is completely determined
9777 * by the values of the other parameters and variables.
9778 * That is, check if dimension dim is involved in an equality.
9780 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9787 nparam
= isl_basic_set_n_param(bset
);
9788 for (i
= 0; i
< bset
->n_eq
; ++i
)
9789 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9794 /* Check if the value for dimension dim is completely determined
9795 * by the values of the other parameters and variables.
9796 * That is, check if dimension dim is involved in an equality
9797 * for each of the subsets.
9799 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9805 for (i
= 0; i
< set
->n
; ++i
) {
9807 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9814 /* Return the number of basic maps in the (current) representation of "map".
9816 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9818 return map
? map
->n
: 0;
9821 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9823 return set
? set
->n
: 0;
9826 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9827 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9832 return isl_stat_error
;
9834 for (i
= 0; i
< map
->n
; ++i
)
9835 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9836 return isl_stat_error
;
9841 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9842 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9847 return isl_stat_error
;
9849 for (i
= 0; i
< set
->n
; ++i
)
9850 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9851 return isl_stat_error
;
9856 /* Return a list of basic sets, the union of which is equal to "set".
9858 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9859 __isl_keep isl_set
*set
)
9862 isl_basic_set_list
*list
;
9867 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
9868 for (i
= 0; i
< set
->n
; ++i
) {
9869 isl_basic_set
*bset
;
9871 bset
= isl_basic_set_copy(set
->p
[i
]);
9872 list
= isl_basic_set_list_add(list
, bset
);
9878 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9885 bset
= isl_basic_set_cow(bset
);
9889 dim
= isl_basic_set_get_space(bset
);
9890 dim
= isl_space_lift(dim
, bset
->n_div
);
9893 isl_space_free(bset
->dim
);
9895 bset
->extra
-= bset
->n_div
;
9898 bset
= isl_basic_set_finalize(bset
);
9902 isl_basic_set_free(bset
);
9906 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9912 set
= isl_set_align_divs(set
);
9917 set
= isl_set_cow(set
);
9921 n_div
= set
->p
[0]->n_div
;
9922 dim
= isl_set_get_space(set
);
9923 dim
= isl_space_lift(dim
, n_div
);
9926 isl_space_free(set
->dim
);
9929 for (i
= 0; i
< set
->n
; ++i
) {
9930 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9941 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9944 struct isl_basic_map
*bmap
;
9951 set
= isl_set_align_divs(set
);
9956 dim
= isl_set_get_space(set
);
9957 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9959 return isl_map_identity(isl_space_map_from_set(dim
));
9962 n_div
= set
->p
[0]->n_div
;
9963 dim
= isl_space_map_from_set(dim
);
9964 n_param
= isl_space_dim(dim
, isl_dim_param
);
9965 n_set
= isl_space_dim(dim
, isl_dim_in
);
9966 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9967 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9968 for (i
= 0; i
< n_set
; ++i
)
9969 bmap
= var_equal(bmap
, i
);
9971 total
= n_param
+ n_set
+ n_set
+ n_div
;
9972 for (i
= 0; i
< n_div
; ++i
) {
9973 k
= isl_basic_map_alloc_inequality(bmap
);
9976 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9977 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9978 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9979 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9980 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9981 set
->p
[0]->div
[i
][0]);
9983 l
= isl_basic_map_alloc_inequality(bmap
);
9986 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9987 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9988 set
->p
[0]->div
[i
][0]);
9989 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9993 bmap
= isl_basic_map_simplify(bmap
);
9994 bmap
= isl_basic_map_finalize(bmap
);
9995 return isl_map_from_basic_map(bmap
);
9998 isl_basic_map_free(bmap
);
10002 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10010 dim
= isl_basic_set_total_dim(bset
);
10011 size
+= bset
->n_eq
* (1 + dim
);
10012 size
+= bset
->n_ineq
* (1 + dim
);
10013 size
+= bset
->n_div
* (2 + dim
);
10018 int isl_set_size(__isl_keep isl_set
*set
)
10026 for (i
= 0; i
< set
->n
; ++i
)
10027 size
+= isl_basic_set_size(set
->p
[i
]);
10032 /* Check if there is any lower bound (if lower == 0) and/or upper
10033 * bound (if upper == 0) on the specified dim.
10035 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10036 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10041 return isl_bool_error
;
10043 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10044 return isl_bool_error
);
10046 pos
+= isl_basic_map_offset(bmap
, type
);
10048 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10049 if (isl_int_is_zero(bmap
->div
[i
][0]))
10051 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10052 return isl_bool_true
;
10055 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10056 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10057 return isl_bool_true
;
10059 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10060 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10067 return lower
&& upper
;
10070 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10071 enum isl_dim_type type
, unsigned pos
)
10073 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10076 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10077 enum isl_dim_type type
, unsigned pos
)
10079 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10082 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10083 enum isl_dim_type type
, unsigned pos
)
10085 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10088 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10089 enum isl_dim_type type
, unsigned pos
)
10096 for (i
= 0; i
< map
->n
; ++i
) {
10098 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10099 if (bounded
< 0 || !bounded
)
10106 /* Return 1 if the specified dim is involved in both an upper bound
10107 * and a lower bound.
10109 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10110 enum isl_dim_type type
, unsigned pos
)
10112 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10115 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10117 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10118 enum isl_dim_type type
, unsigned pos
,
10119 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10120 enum isl_dim_type type
, unsigned pos
))
10125 return isl_bool_error
;
10127 for (i
= 0; i
< map
->n
; ++i
) {
10129 bounded
= fn(map
->p
[i
], type
, pos
);
10130 if (bounded
< 0 || bounded
)
10134 return isl_bool_false
;
10137 /* Return 1 if the specified dim is involved in any lower bound.
10139 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10140 enum isl_dim_type type
, unsigned pos
)
10142 return has_any_bound(set
, type
, pos
,
10143 &isl_basic_map_dim_has_lower_bound
);
10146 /* Return 1 if the specified dim is involved in any upper bound.
10148 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10149 enum isl_dim_type type
, unsigned pos
)
10151 return has_any_bound(set
, type
, pos
,
10152 &isl_basic_map_dim_has_upper_bound
);
10155 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10157 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10158 enum isl_dim_type type
, unsigned pos
,
10159 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10160 enum isl_dim_type type
, unsigned pos
))
10165 return isl_bool_error
;
10167 for (i
= 0; i
< map
->n
; ++i
) {
10169 bounded
= fn(map
->p
[i
], type
, pos
);
10170 if (bounded
< 0 || !bounded
)
10174 return isl_bool_true
;
10177 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10179 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10180 enum isl_dim_type type
, unsigned pos
)
10182 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10185 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10187 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10188 enum isl_dim_type type
, unsigned pos
)
10190 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10193 /* For each of the "n" variables starting at "first", determine
10194 * the sign of the variable and put the results in the first "n"
10195 * elements of the array "signs".
10197 * 1 means that the variable is non-negative
10198 * -1 means that the variable is non-positive
10199 * 0 means the variable attains both positive and negative values.
10201 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10202 unsigned first
, unsigned n
, int *signs
)
10204 isl_vec
*bound
= NULL
;
10205 struct isl_tab
*tab
= NULL
;
10206 struct isl_tab_undo
*snap
;
10209 if (!bset
|| !signs
)
10212 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10213 tab
= isl_tab_from_basic_set(bset
, 0);
10214 if (!bound
|| !tab
)
10217 isl_seq_clr(bound
->el
, bound
->size
);
10218 isl_int_set_si(bound
->el
[0], -1);
10220 snap
= isl_tab_snap(tab
);
10221 for (i
= 0; i
< n
; ++i
) {
10224 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10225 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10227 empty
= tab
->empty
;
10228 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10229 if (isl_tab_rollback(tab
, snap
) < 0)
10237 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10238 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10240 empty
= tab
->empty
;
10241 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10242 if (isl_tab_rollback(tab
, snap
) < 0)
10245 signs
[i
] = empty
? -1 : 0;
10249 isl_vec_free(bound
);
10253 isl_vec_free(bound
);
10257 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10258 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10260 if (!bset
|| !signs
)
10262 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10265 first
+= pos(bset
->dim
, type
) - 1;
10266 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10269 /* Is it possible for the integer division "div" to depend (possibly
10270 * indirectly) on any output dimensions?
10272 * If the div is undefined, then we conservatively assume that it
10273 * may depend on them.
10274 * Otherwise, we check if it actually depends on them or on any integer
10275 * divisions that may depend on them.
10277 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10280 unsigned n_out
, o_out
;
10281 unsigned n_div
, o_div
;
10283 if (isl_int_is_zero(bmap
->div
[div
][0]))
10286 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10287 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10289 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10292 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10293 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10295 for (i
= 0; i
< n_div
; ++i
) {
10296 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10298 if (div_may_involve_output(bmap
, i
))
10305 /* Return the index of the equality of "bmap" that defines
10306 * the output dimension "pos" in terms of earlier dimensions.
10307 * The equality may also involve integer divisions, as long
10308 * as those integer divisions are defined in terms of
10309 * parameters or input dimensions.
10310 * Return bmap->n_eq if there is no such equality.
10311 * Return -1 on error.
10313 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10317 unsigned n_out
, o_out
;
10318 unsigned n_div
, o_div
;
10323 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10324 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10325 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10326 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10328 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10329 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10331 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10332 n_out
- (pos
+ 1)) != -1)
10334 for (k
= 0; k
< n_div
; ++k
) {
10335 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10337 if (div_may_involve_output(bmap
, k
))
10347 /* Check if the given basic map is obviously single-valued.
10348 * In particular, for each output dimension, check that there is
10349 * an equality that defines the output dimension in terms of
10350 * earlier dimensions.
10352 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10358 return isl_bool_error
;
10360 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10362 for (i
= 0; i
< n_out
; ++i
) {
10365 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10367 return isl_bool_error
;
10368 if (eq
>= bmap
->n_eq
)
10369 return isl_bool_false
;
10372 return isl_bool_true
;
10375 /* Check if the given basic map is single-valued.
10376 * We simply compute
10380 * and check if the result is a subset of the identity mapping.
10382 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10385 isl_basic_map
*test
;
10389 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10393 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10394 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10396 space
= isl_basic_map_get_space(bmap
);
10397 space
= isl_space_map_from_set(isl_space_range(space
));
10398 id
= isl_basic_map_identity(space
);
10400 sv
= isl_basic_map_is_subset(test
, id
);
10402 isl_basic_map_free(test
);
10403 isl_basic_map_free(id
);
10408 /* Check if the given map is obviously single-valued.
10410 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10413 return isl_bool_error
;
10415 return isl_bool_true
;
10417 return isl_bool_false
;
10419 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10422 /* Check if the given map is single-valued.
10423 * We simply compute
10427 * and check if the result is a subset of the identity mapping.
10429 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10436 sv
= isl_map_plain_is_single_valued(map
);
10440 test
= isl_map_reverse(isl_map_copy(map
));
10441 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10443 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10444 id
= isl_map_identity(dim
);
10446 sv
= isl_map_is_subset(test
, id
);
10448 isl_map_free(test
);
10454 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10458 map
= isl_map_copy(map
);
10459 map
= isl_map_reverse(map
);
10460 in
= isl_map_is_single_valued(map
);
10466 /* Check if the given map is obviously injective.
10468 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10472 map
= isl_map_copy(map
);
10473 map
= isl_map_reverse(map
);
10474 in
= isl_map_plain_is_single_valued(map
);
10480 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10484 sv
= isl_map_is_single_valued(map
);
10488 return isl_map_is_injective(map
);
10491 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10493 return isl_map_is_single_valued((isl_map
*)set
);
10496 int isl_map_is_translation(__isl_keep isl_map
*map
)
10501 delta
= isl_map_deltas(isl_map_copy(map
));
10502 ok
= isl_set_is_singleton(delta
);
10503 isl_set_free(delta
);
10508 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10510 if (isl_seq_first_non_zero(p
, pos
) != -1)
10512 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10517 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10526 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10529 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10530 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10531 for (j
= 0; j
< nvar
; ++j
) {
10532 int lower
= 0, upper
= 0;
10533 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10534 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10536 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10540 if (i
< bset
->n_eq
)
10542 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10543 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10545 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10547 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10552 if (!lower
|| !upper
)
10559 int isl_set_is_box(__isl_keep isl_set
*set
)
10566 return isl_basic_set_is_box(set
->p
[0]);
10569 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10572 return isl_bool_error
;
10574 return isl_space_is_wrapping(bset
->dim
);
10577 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10580 return isl_bool_error
;
10582 return isl_space_is_wrapping(set
->dim
);
10585 /* Is the domain of "map" a wrapped relation?
10587 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10590 return isl_bool_error
;
10592 return isl_space_domain_is_wrapping(map
->dim
);
10595 /* Is the range of "map" a wrapped relation?
10597 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10600 return isl_bool_error
;
10602 return isl_space_range_is_wrapping(map
->dim
);
10605 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10607 bmap
= isl_basic_map_cow(bmap
);
10611 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10615 bmap
= isl_basic_map_finalize(bmap
);
10617 return (isl_basic_set
*)bmap
;
10619 isl_basic_map_free(bmap
);
10623 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10627 map
= isl_map_cow(map
);
10631 for (i
= 0; i
< map
->n
; ++i
) {
10632 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10636 map
->dim
= isl_space_wrap(map
->dim
);
10640 return (isl_set
*)map
;
10646 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10648 bset
= isl_basic_set_cow(bset
);
10652 bset
->dim
= isl_space_unwrap(bset
->dim
);
10656 bset
= isl_basic_set_finalize(bset
);
10658 return (isl_basic_map
*)bset
;
10660 isl_basic_set_free(bset
);
10664 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10671 if (!isl_set_is_wrapping(set
))
10672 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10675 set
= isl_set_cow(set
);
10679 for (i
= 0; i
< set
->n
; ++i
) {
10680 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10685 set
->dim
= isl_space_unwrap(set
->dim
);
10689 return (isl_map
*)set
;
10695 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10696 enum isl_dim_type type
)
10701 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10704 bmap
= isl_basic_map_cow(bmap
);
10708 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10712 bmap
= isl_basic_map_finalize(bmap
);
10716 isl_basic_map_free(bmap
);
10720 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10721 enum isl_dim_type type
)
10728 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10731 map
= isl_map_cow(map
);
10735 for (i
= 0; i
< map
->n
; ++i
) {
10736 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10740 map
->dim
= isl_space_reset(map
->dim
, type
);
10750 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10755 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10758 bmap
= isl_basic_map_cow(bmap
);
10762 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10766 bmap
= isl_basic_map_finalize(bmap
);
10770 isl_basic_map_free(bmap
);
10774 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10776 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10779 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10780 __isl_take isl_basic_map
*bmap
)
10785 if (!bmap
->dim
->nested
[0])
10788 bmap
= isl_basic_map_cow(bmap
);
10792 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10796 bmap
= isl_basic_map_finalize(bmap
);
10800 isl_basic_map_free(bmap
);
10804 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10805 __isl_take isl_basic_map
*bmap
)
10810 if (!bmap
->dim
->nested
[1])
10813 bmap
= isl_basic_map_cow(bmap
);
10817 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10821 bmap
= isl_basic_map_finalize(bmap
);
10825 isl_basic_map_free(bmap
);
10829 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10836 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10839 map
= isl_map_cow(map
);
10843 for (i
= 0; i
< map
->n
; ++i
) {
10844 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10848 map
->dim
= isl_space_flatten(map
->dim
);
10858 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10860 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10863 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10865 isl_space
*dim
, *flat_dim
;
10868 dim
= isl_set_get_space(set
);
10869 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10870 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10871 map
= isl_map_intersect_domain(map
, set
);
10876 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10883 if (!map
->dim
->nested
[0])
10886 map
= isl_map_cow(map
);
10890 for (i
= 0; i
< map
->n
; ++i
) {
10891 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10895 map
->dim
= isl_space_flatten_domain(map
->dim
);
10905 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10912 if (!map
->dim
->nested
[1])
10915 map
= isl_map_cow(map
);
10919 for (i
= 0; i
< map
->n
; ++i
) {
10920 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10924 map
->dim
= isl_space_flatten_range(map
->dim
);
10934 /* Reorder the dimensions of "bmap" according to the given dim_map
10935 * and set the dimension specification to "dim".
10937 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10938 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10940 isl_basic_map
*res
;
10943 bmap
= isl_basic_map_cow(bmap
);
10944 if (!bmap
|| !dim
|| !dim_map
)
10947 flags
= bmap
->flags
;
10948 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10949 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10950 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10951 res
= isl_basic_map_alloc_space(dim
,
10952 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10953 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10955 res
->flags
= flags
;
10956 res
= isl_basic_map_finalize(res
);
10960 isl_basic_map_free(bmap
);
10961 isl_space_free(dim
);
10965 /* Reorder the dimensions of "map" according to given reordering.
10967 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10968 __isl_take isl_reordering
*r
)
10971 struct isl_dim_map
*dim_map
;
10973 map
= isl_map_cow(map
);
10974 dim_map
= isl_dim_map_from_reordering(r
);
10975 if (!map
|| !r
|| !dim_map
)
10978 for (i
= 0; i
< map
->n
; ++i
) {
10979 struct isl_dim_map
*dim_map_i
;
10981 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10983 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10984 isl_space_copy(r
->dim
), dim_map_i
);
10990 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10992 isl_reordering_free(r
);
10998 isl_reordering_free(r
);
11002 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11003 __isl_take isl_reordering
*r
)
11005 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11008 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11009 __isl_take isl_space
*model
)
11013 if (!map
|| !model
)
11016 ctx
= isl_space_get_ctx(model
);
11017 if (!isl_space_has_named_params(model
))
11018 isl_die(ctx
, isl_error_invalid
,
11019 "model has unnamed parameters", goto error
);
11020 if (!isl_space_has_named_params(map
->dim
))
11021 isl_die(ctx
, isl_error_invalid
,
11022 "relation has unnamed parameters", goto error
);
11023 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11024 isl_reordering
*exp
;
11026 model
= isl_space_drop_dims(model
, isl_dim_in
,
11027 0, isl_space_dim(model
, isl_dim_in
));
11028 model
= isl_space_drop_dims(model
, isl_dim_out
,
11029 0, isl_space_dim(model
, isl_dim_out
));
11030 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11031 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11032 map
= isl_map_realign(map
, exp
);
11035 isl_space_free(model
);
11038 isl_space_free(model
);
11043 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11044 __isl_take isl_space
*model
)
11046 return isl_map_align_params(set
, model
);
11049 /* Align the parameters of "bmap" to those of "model", introducing
11050 * additional parameters if needed.
11052 __isl_give isl_basic_map
*isl_basic_map_align_params(
11053 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11057 if (!bmap
|| !model
)
11060 ctx
= isl_space_get_ctx(model
);
11061 if (!isl_space_has_named_params(model
))
11062 isl_die(ctx
, isl_error_invalid
,
11063 "model has unnamed parameters", goto error
);
11064 if (!isl_space_has_named_params(bmap
->dim
))
11065 isl_die(ctx
, isl_error_invalid
,
11066 "relation has unnamed parameters", goto error
);
11067 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11068 isl_reordering
*exp
;
11069 struct isl_dim_map
*dim_map
;
11071 model
= isl_space_drop_dims(model
, isl_dim_in
,
11072 0, isl_space_dim(model
, isl_dim_in
));
11073 model
= isl_space_drop_dims(model
, isl_dim_out
,
11074 0, isl_space_dim(model
, isl_dim_out
));
11075 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11076 exp
= isl_reordering_extend_space(exp
,
11077 isl_basic_map_get_space(bmap
));
11078 dim_map
= isl_dim_map_from_reordering(exp
);
11079 bmap
= isl_basic_map_realign(bmap
,
11080 exp
? isl_space_copy(exp
->dim
) : NULL
,
11081 isl_dim_map_extend(dim_map
, bmap
));
11082 isl_reordering_free(exp
);
11086 isl_space_free(model
);
11089 isl_space_free(model
);
11090 isl_basic_map_free(bmap
);
11094 /* Align the parameters of "bset" to those of "model", introducing
11095 * additional parameters if needed.
11097 __isl_give isl_basic_set
*isl_basic_set_align_params(
11098 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11100 return isl_basic_map_align_params(bset
, model
);
11103 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11104 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11105 enum isl_dim_type c2
, enum isl_dim_type c3
,
11106 enum isl_dim_type c4
, enum isl_dim_type c5
)
11108 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11109 struct isl_mat
*mat
;
11115 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11116 isl_basic_map_total_dim(bmap
) + 1);
11119 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11120 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11121 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11122 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11123 isl_int_set(mat
->row
[i
][pos
],
11124 bmap
->eq
[i
][off
+ k
]);
11132 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11133 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11134 enum isl_dim_type c2
, enum isl_dim_type c3
,
11135 enum isl_dim_type c4
, enum isl_dim_type c5
)
11137 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11138 struct isl_mat
*mat
;
11144 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11145 isl_basic_map_total_dim(bmap
) + 1);
11148 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11149 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11150 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11151 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11152 isl_int_set(mat
->row
[i
][pos
],
11153 bmap
->ineq
[i
][off
+ k
]);
11161 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11162 __isl_take isl_space
*dim
,
11163 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11164 enum isl_dim_type c2
, enum isl_dim_type c3
,
11165 enum isl_dim_type c4
, enum isl_dim_type c5
)
11167 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11168 isl_basic_map
*bmap
;
11174 if (!dim
|| !eq
|| !ineq
)
11177 if (eq
->n_col
!= ineq
->n_col
)
11178 isl_die(dim
->ctx
, isl_error_invalid
,
11179 "equalities and inequalities matrices should have "
11180 "same number of columns", goto error
);
11182 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11184 if (eq
->n_col
< total
)
11185 isl_die(dim
->ctx
, isl_error_invalid
,
11186 "number of columns too small", goto error
);
11188 extra
= eq
->n_col
- total
;
11190 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11191 eq
->n_row
, ineq
->n_row
);
11194 for (i
= 0; i
< extra
; ++i
) {
11195 k
= isl_basic_map_alloc_div(bmap
);
11198 isl_int_set_si(bmap
->div
[k
][0], 0);
11200 for (i
= 0; i
< eq
->n_row
; ++i
) {
11201 l
= isl_basic_map_alloc_equality(bmap
);
11204 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11205 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11206 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11207 isl_int_set(bmap
->eq
[l
][off
+ k
],
11213 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11214 l
= isl_basic_map_alloc_inequality(bmap
);
11217 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11218 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11219 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11220 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11221 ineq
->row
[i
][pos
]);
11227 isl_space_free(dim
);
11229 isl_mat_free(ineq
);
11231 bmap
= isl_basic_map_simplify(bmap
);
11232 return isl_basic_map_finalize(bmap
);
11234 isl_space_free(dim
);
11236 isl_mat_free(ineq
);
11240 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11241 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11242 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11244 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11245 c1
, c2
, c3
, c4
, isl_dim_in
);
11248 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11249 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11250 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11252 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11253 c1
, c2
, c3
, c4
, isl_dim_in
);
11256 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11257 __isl_take isl_space
*dim
,
11258 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11259 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11261 return (isl_basic_set
*)
11262 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11263 c1
, c2
, c3
, c4
, isl_dim_in
);
11266 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11269 return isl_bool_error
;
11271 return isl_space_can_zip(bmap
->dim
);
11274 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11277 return isl_bool_error
;
11279 return isl_space_can_zip(map
->dim
);
11282 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11283 * (A -> C) -> (B -> D).
11285 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11294 if (!isl_basic_map_can_zip(bmap
))
11295 isl_die(bmap
->ctx
, isl_error_invalid
,
11296 "basic map cannot be zipped", goto error
);
11297 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11298 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11299 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11300 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11301 bmap
= isl_basic_map_cow(bmap
);
11302 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11305 bmap
->dim
= isl_space_zip(bmap
->dim
);
11310 isl_basic_map_free(bmap
);
11314 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11315 * (A -> C) -> (B -> D).
11317 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11324 if (!isl_map_can_zip(map
))
11325 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11328 map
= isl_map_cow(map
);
11332 for (i
= 0; i
< map
->n
; ++i
) {
11333 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11338 map
->dim
= isl_space_zip(map
->dim
);
11348 /* Can we apply isl_basic_map_curry to "bmap"?
11349 * That is, does it have a nested relation in its domain?
11351 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11354 return isl_bool_error
;
11356 return isl_space_can_curry(bmap
->dim
);
11359 /* Can we apply isl_map_curry to "map"?
11360 * That is, does it have a nested relation in its domain?
11362 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11365 return isl_bool_error
;
11367 return isl_space_can_curry(map
->dim
);
11370 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11373 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11379 if (!isl_basic_map_can_curry(bmap
))
11380 isl_die(bmap
->ctx
, isl_error_invalid
,
11381 "basic map cannot be curried", goto error
);
11382 bmap
= isl_basic_map_cow(bmap
);
11385 bmap
->dim
= isl_space_curry(bmap
->dim
);
11390 isl_basic_map_free(bmap
);
11394 /* Given a map (A -> B) -> C, return the corresponding map
11397 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11404 if (!isl_map_can_curry(map
))
11405 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11408 map
= isl_map_cow(map
);
11412 for (i
= 0; i
< map
->n
; ++i
) {
11413 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11418 map
->dim
= isl_space_curry(map
->dim
);
11428 /* Can we apply isl_basic_map_uncurry to "bmap"?
11429 * That is, does it have a nested relation in its domain?
11431 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11434 return isl_bool_error
;
11436 return isl_space_can_uncurry(bmap
->dim
);
11439 /* Can we apply isl_map_uncurry to "map"?
11440 * That is, does it have a nested relation in its domain?
11442 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11445 return isl_bool_error
;
11447 return isl_space_can_uncurry(map
->dim
);
11450 /* Given a basic map A -> (B -> C), return the corresponding basic map
11453 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11459 if (!isl_basic_map_can_uncurry(bmap
))
11460 isl_die(bmap
->ctx
, isl_error_invalid
,
11461 "basic map cannot be uncurried",
11462 return isl_basic_map_free(bmap
));
11463 bmap
= isl_basic_map_cow(bmap
);
11466 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11468 return isl_basic_map_free(bmap
);
11472 /* Given a map A -> (B -> C), return the corresponding map
11475 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11482 if (!isl_map_can_uncurry(map
))
11483 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11484 return isl_map_free(map
));
11486 map
= isl_map_cow(map
);
11490 for (i
= 0; i
< map
->n
; ++i
) {
11491 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11493 return isl_map_free(map
);
11496 map
->dim
= isl_space_uncurry(map
->dim
);
11498 return isl_map_free(map
);
11503 /* Construct a basic map mapping the domain of the affine expression
11504 * to a one-dimensional range prescribed by the affine expression.
11506 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11510 isl_local_space
*ls
;
11511 isl_basic_map
*bmap
;
11516 ls
= isl_aff_get_local_space(aff
);
11517 bmap
= isl_basic_map_from_local_space(ls
);
11518 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11519 k
= isl_basic_map_alloc_equality(bmap
);
11523 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11524 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11525 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11526 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11527 aff
->v
->size
- (pos
+ 1));
11530 bmap
= isl_basic_map_finalize(bmap
);
11534 isl_basic_map_free(bmap
);
11538 /* Construct a map mapping the domain of the affine expression
11539 * to a one-dimensional range prescribed by the affine expression.
11541 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11543 isl_basic_map
*bmap
;
11545 bmap
= isl_basic_map_from_aff(aff
);
11546 return isl_map_from_basic_map(bmap
);
11549 /* Construct a basic map mapping the domain the multi-affine expression
11550 * to its range, with each dimension in the range equated to the
11551 * corresponding affine expression.
11553 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11554 __isl_take isl_multi_aff
*maff
)
11558 isl_basic_map
*bmap
;
11563 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11564 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11565 "invalid space", goto error
);
11567 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11568 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11570 for (i
= 0; i
< maff
->n
; ++i
) {
11572 isl_basic_map
*bmap_i
;
11574 aff
= isl_aff_copy(maff
->p
[i
]);
11575 bmap_i
= isl_basic_map_from_aff(aff
);
11577 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11580 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11582 isl_multi_aff_free(maff
);
11585 isl_multi_aff_free(maff
);
11589 /* Construct a map mapping the domain the multi-affine expression
11590 * to its range, with each dimension in the range equated to the
11591 * corresponding affine expression.
11593 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11595 isl_basic_map
*bmap
;
11597 bmap
= isl_basic_map_from_multi_aff(maff
);
11598 return isl_map_from_basic_map(bmap
);
11601 /* Construct a basic map mapping a domain in the given space to
11602 * to an n-dimensional range, with n the number of elements in the list,
11603 * where each coordinate in the range is prescribed by the
11604 * corresponding affine expression.
11605 * The domains of all affine expressions in the list are assumed to match
11608 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11609 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11613 isl_basic_map
*bmap
;
11618 dim
= isl_space_from_domain(domain_dim
);
11619 bmap
= isl_basic_map_universe(dim
);
11621 for (i
= 0; i
< list
->n
; ++i
) {
11623 isl_basic_map
*bmap_i
;
11625 aff
= isl_aff_copy(list
->p
[i
]);
11626 bmap_i
= isl_basic_map_from_aff(aff
);
11628 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11631 isl_aff_list_free(list
);
11635 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11636 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11638 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11641 /* Construct a basic map where the given dimensions are equal to each other.
11643 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11644 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11646 isl_basic_map
*bmap
= NULL
;
11652 if (pos1
>= isl_space_dim(space
, type1
))
11653 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11654 "index out of bounds", goto error
);
11655 if (pos2
>= isl_space_dim(space
, type2
))
11656 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11657 "index out of bounds", goto error
);
11659 if (type1
== type2
&& pos1
== pos2
)
11660 return isl_basic_map_universe(space
);
11662 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11663 i
= isl_basic_map_alloc_equality(bmap
);
11666 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11667 pos1
+= isl_basic_map_offset(bmap
, type1
);
11668 pos2
+= isl_basic_map_offset(bmap
, type2
);
11669 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11670 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11671 bmap
= isl_basic_map_finalize(bmap
);
11672 isl_space_free(space
);
11675 isl_space_free(space
);
11676 isl_basic_map_free(bmap
);
11680 /* Add a constraint imposing that the given two dimensions are equal.
11682 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11683 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11687 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11689 bmap
= isl_basic_map_intersect(bmap
, eq
);
11694 /* Add a constraint imposing that the given two dimensions are equal.
11696 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11697 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11699 isl_basic_map
*bmap
;
11701 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11703 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11708 /* Add a constraint imposing that the given two dimensions have opposite values.
11710 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11711 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11713 isl_basic_map
*bmap
= NULL
;
11719 if (pos1
>= isl_map_dim(map
, type1
))
11720 isl_die(map
->ctx
, isl_error_invalid
,
11721 "index out of bounds", goto error
);
11722 if (pos2
>= isl_map_dim(map
, type2
))
11723 isl_die(map
->ctx
, isl_error_invalid
,
11724 "index out of bounds", goto error
);
11726 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11727 i
= isl_basic_map_alloc_equality(bmap
);
11730 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11731 pos1
+= isl_basic_map_offset(bmap
, type1
);
11732 pos2
+= isl_basic_map_offset(bmap
, type2
);
11733 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11734 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11735 bmap
= isl_basic_map_finalize(bmap
);
11737 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11741 isl_basic_map_free(bmap
);
11746 /* Construct a constraint imposing that the value of the first dimension is
11747 * greater than or equal to that of the second.
11749 static __isl_give isl_constraint
*constraint_order_ge(
11750 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11751 enum isl_dim_type type2
, int pos2
)
11758 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
11760 if (pos1
>= isl_constraint_dim(c
, type1
))
11761 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11762 "index out of bounds", return isl_constraint_free(c
));
11763 if (pos2
>= isl_constraint_dim(c
, type2
))
11764 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11765 "index out of bounds", return isl_constraint_free(c
));
11767 if (type1
== type2
&& pos1
== pos2
)
11770 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11771 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11776 /* Add a constraint imposing that the value of the first dimension is
11777 * greater than or equal to that of the second.
11779 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11780 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11785 if (type1
== type2
&& pos1
== pos2
)
11787 space
= isl_basic_map_get_space(bmap
);
11788 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11789 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11794 /* Add a constraint imposing that the value of the first dimension is
11795 * greater than or equal to that of the second.
11797 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11798 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11803 if (type1
== type2
&& pos1
== pos2
)
11805 space
= isl_map_get_space(map
);
11806 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11807 map
= isl_map_add_constraint(map
, c
);
11812 /* Add a constraint imposing that the value of the first dimension is
11813 * less than or equal to that of the second.
11815 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11816 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11818 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11821 /* Construct a basic map where the value of the first dimension is
11822 * greater than that of the second.
11824 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11825 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11827 isl_basic_map
*bmap
= NULL
;
11833 if (pos1
>= isl_space_dim(space
, type1
))
11834 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11835 "index out of bounds", goto error
);
11836 if (pos2
>= isl_space_dim(space
, type2
))
11837 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11838 "index out of bounds", goto error
);
11840 if (type1
== type2
&& pos1
== pos2
)
11841 return isl_basic_map_empty(space
);
11843 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11844 i
= isl_basic_map_alloc_inequality(bmap
);
11846 return isl_basic_map_free(bmap
);
11847 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11848 pos1
+= isl_basic_map_offset(bmap
, type1
);
11849 pos2
+= isl_basic_map_offset(bmap
, type2
);
11850 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11851 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11852 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11853 bmap
= isl_basic_map_finalize(bmap
);
11857 isl_space_free(space
);
11858 isl_basic_map_free(bmap
);
11862 /* Add a constraint imposing that the value of the first dimension is
11863 * greater than that of the second.
11865 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11866 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11870 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11872 bmap
= isl_basic_map_intersect(bmap
, gt
);
11877 /* Add a constraint imposing that the value of the first dimension is
11878 * greater than that of the second.
11880 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11881 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11883 isl_basic_map
*bmap
;
11885 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11887 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11892 /* Add a constraint imposing that the value of the first dimension is
11893 * smaller than that of the second.
11895 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11896 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11898 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11901 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11905 isl_local_space
*ls
;
11910 if (!isl_basic_map_divs_known(bmap
))
11911 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11912 "some divs are unknown", return NULL
);
11914 ls
= isl_basic_map_get_local_space(bmap
);
11915 div
= isl_local_space_get_div(ls
, pos
);
11916 isl_local_space_free(ls
);
11921 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11924 return isl_basic_map_get_div(bset
, pos
);
11927 /* Plug in "subs" for dimension "type", "pos" of "bset".
11929 * Let i be the dimension to replace and let "subs" be of the form
11933 * Any integer division with a non-zero coefficient for i,
11935 * floor((a i + g)/m)
11939 * floor((a f + d g)/(m d))
11941 * Constraints of the form
11949 * We currently require that "subs" is an integral expression.
11950 * Handling rational expressions may require us to add stride constraints
11951 * as we do in isl_basic_set_preimage_multi_aff.
11953 __isl_give isl_basic_set
*isl_basic_set_substitute(
11954 __isl_take isl_basic_set
*bset
,
11955 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11961 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11964 bset
= isl_basic_set_cow(bset
);
11965 if (!bset
|| !subs
)
11968 ctx
= isl_basic_set_get_ctx(bset
);
11969 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11970 isl_die(ctx
, isl_error_invalid
,
11971 "spaces don't match", goto error
);
11972 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11973 isl_die(ctx
, isl_error_unsupported
,
11974 "cannot handle divs yet", goto error
);
11975 if (!isl_int_is_one(subs
->v
->el
[0]))
11976 isl_die(ctx
, isl_error_invalid
,
11977 "can only substitute integer expressions", goto error
);
11979 pos
+= isl_basic_set_offset(bset
, type
);
11983 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11984 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11986 isl_int_set(v
, bset
->eq
[i
][pos
]);
11987 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11988 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11989 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11992 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11993 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11995 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11996 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11997 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11998 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12001 for (i
= 0; i
< bset
->n_div
; ++i
) {
12002 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12004 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12005 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12006 isl_seq_combine(bset
->div
[i
] + 1,
12007 subs
->v
->el
[0], bset
->div
[i
] + 1,
12008 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12009 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12014 bset
= isl_basic_set_simplify(bset
);
12015 return isl_basic_set_finalize(bset
);
12017 isl_basic_set_free(bset
);
12021 /* Plug in "subs" for dimension "type", "pos" of "set".
12023 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12024 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12028 if (set
&& isl_set_plain_is_empty(set
))
12031 set
= isl_set_cow(set
);
12035 for (i
= set
->n
- 1; i
>= 0; --i
) {
12036 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12037 if (remove_if_empty(set
, i
) < 0)
12047 /* Check if the range of "ma" is compatible with the domain or range
12048 * (depending on "type") of "bmap".
12049 * Return -1 if anything is wrong.
12051 static int check_basic_map_compatible_range_multi_aff(
12052 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12053 __isl_keep isl_multi_aff
*ma
)
12056 isl_space
*ma_space
;
12058 ma_space
= isl_multi_aff_get_space(ma
);
12060 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12064 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12065 "parameters don't match", goto error
);
12066 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12070 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12071 "spaces don't match", goto error
);
12073 isl_space_free(ma_space
);
12076 isl_space_free(ma_space
);
12080 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12081 * coefficients before the transformed range of dimensions,
12082 * the "n_after" coefficients after the transformed range of dimensions
12083 * and the coefficients of the other divs in "bmap".
12085 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12086 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12091 isl_local_space
*ls
;
12096 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12100 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12101 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12102 for (i
= 0; i
< n_div
; ++i
) {
12103 int o_bmap
= 0, o_ls
= 0;
12105 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12106 o_bmap
+= 1 + 1 + n_param
;
12107 o_ls
+= 1 + 1 + n_param
;
12108 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12109 o_bmap
+= n_before
;
12110 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12111 ls
->div
->row
[i
] + o_ls
, n_set
);
12114 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12116 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12117 ls
->div
->row
[i
] + o_ls
, n_div
);
12120 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12121 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12125 isl_local_space_free(ls
);
12128 isl_local_space_free(ls
);
12132 /* How many stride constraints does "ma" enforce?
12133 * That is, how many of the affine expressions have a denominator
12134 * different from one?
12136 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12141 for (i
= 0; i
< ma
->n
; ++i
)
12142 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12148 /* For each affine expression in ma of the form
12150 * x_i = (f_i y + h_i)/m_i
12152 * with m_i different from one, add a constraint to "bmap"
12155 * f_i y + h_i = m_i alpha_i
12157 * with alpha_i an additional existentially quantified variable.
12159 static __isl_give isl_basic_map
*add_ma_strides(
12160 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12161 int n_before
, int n_after
)
12170 total
= isl_basic_map_total_dim(bmap
);
12171 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12172 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12173 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12174 for (i
= 0; i
< ma
->n
; ++i
) {
12175 int o_bmap
= 0, o_ma
= 1;
12177 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12179 div
= isl_basic_map_alloc_div(bmap
);
12180 k
= isl_basic_map_alloc_equality(bmap
);
12181 if (div
< 0 || k
< 0)
12183 isl_int_set_si(bmap
->div
[div
][0], 0);
12184 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12185 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12186 o_bmap
+= 1 + n_param
;
12187 o_ma
+= 1 + n_param
;
12188 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12189 o_bmap
+= n_before
;
12190 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12191 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12194 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12196 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12197 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12200 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12201 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12207 isl_basic_map_free(bmap
);
12211 /* Replace the domain or range space (depending on "type) of "space" by "set".
12213 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12214 enum isl_dim_type type
, __isl_take isl_space
*set
)
12216 if (type
== isl_dim_in
) {
12217 space
= isl_space_range(space
);
12218 space
= isl_space_map_from_domain_and_range(set
, space
);
12220 space
= isl_space_domain(space
);
12221 space
= isl_space_map_from_domain_and_range(space
, set
);
12227 /* Compute the preimage of the domain or range (depending on "type")
12228 * of "bmap" under the function represented by "ma".
12229 * In other words, plug in "ma" in the domain or range of "bmap".
12230 * The result is a basic map that lives in the same space as "bmap"
12231 * except that the domain or range has been replaced by
12232 * the domain space of "ma".
12234 * If bmap is represented by
12236 * A(p) + S u + B x + T v + C(divs) >= 0,
12238 * where u and x are input and output dimensions if type == isl_dim_out
12239 * while x and v are input and output dimensions if type == isl_dim_in,
12240 * and ma is represented by
12242 * x = D(p) + F(y) + G(divs')
12244 * then the result is
12246 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12248 * The divs in the input set are similarly adjusted.
12251 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12255 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12256 * B_i G(divs') + c_i(divs))/n_i)
12258 * If bmap is not a rational map and if F(y) involves any denominators
12260 * x_i = (f_i y + h_i)/m_i
12262 * then additional constraints are added to ensure that we only
12263 * map back integer points. That is we enforce
12265 * f_i y + h_i = m_i alpha_i
12267 * with alpha_i an additional existentially quantified variable.
12269 * We first copy over the divs from "ma".
12270 * Then we add the modified constraints and divs from "bmap".
12271 * Finally, we add the stride constraints, if needed.
12273 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12274 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12275 __isl_take isl_multi_aff
*ma
)
12279 isl_basic_map
*res
= NULL
;
12280 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12281 isl_int f
, c1
, c2
, g
;
12282 int rational
, strides
;
12289 ma
= isl_multi_aff_align_divs(ma
);
12292 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12295 if (type
== isl_dim_in
) {
12297 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12299 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12302 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12303 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12305 space
= isl_multi_aff_get_domain_space(ma
);
12306 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12307 rational
= isl_basic_map_is_rational(bmap
);
12308 strides
= rational
? 0 : multi_aff_strides(ma
);
12309 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12310 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12312 res
= isl_basic_map_set_rational(res
);
12314 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12315 if (isl_basic_map_alloc_div(res
) < 0)
12318 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12321 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12322 k
= isl_basic_map_alloc_equality(res
);
12325 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12326 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12329 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12330 k
= isl_basic_map_alloc_inequality(res
);
12333 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12334 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12337 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12338 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12339 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12342 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12343 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12348 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12354 isl_basic_map_free(bmap
);
12355 isl_multi_aff_free(ma
);
12356 res
= isl_basic_set_simplify(res
);
12357 return isl_basic_map_finalize(res
);
12363 isl_basic_map_free(bmap
);
12364 isl_multi_aff_free(ma
);
12365 isl_basic_map_free(res
);
12369 /* Compute the preimage of "bset" under the function represented by "ma".
12370 * In other words, plug in "ma" in "bset". The result is a basic set
12371 * that lives in the domain space of "ma".
12373 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12374 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12376 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12379 /* Compute the preimage of the domain of "bmap" under the function
12380 * represented by "ma".
12381 * In other words, plug in "ma" in the domain of "bmap".
12382 * The result is a basic map that lives in the same space as "bmap"
12383 * except that the domain has been replaced by the domain space of "ma".
12385 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12386 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12388 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12391 /* Compute the preimage of the range of "bmap" under the function
12392 * represented by "ma".
12393 * In other words, plug in "ma" in the range of "bmap".
12394 * The result is a basic map that lives in the same space as "bmap"
12395 * except that the range has been replaced by the domain space of "ma".
12397 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12398 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12400 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12403 /* Check if the range of "ma" is compatible with the domain or range
12404 * (depending on "type") of "map".
12405 * Return -1 if anything is wrong.
12407 static int check_map_compatible_range_multi_aff(
12408 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12409 __isl_keep isl_multi_aff
*ma
)
12412 isl_space
*ma_space
;
12414 ma_space
= isl_multi_aff_get_space(ma
);
12415 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12416 isl_space_free(ma_space
);
12418 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12419 "spaces don't match", return -1);
12423 /* Compute the preimage of the domain or range (depending on "type")
12424 * of "map" under the function represented by "ma".
12425 * In other words, plug in "ma" in the domain or range of "map".
12426 * The result is a map that lives in the same space as "map"
12427 * except that the domain or range has been replaced by
12428 * the domain space of "ma".
12430 * The parameters are assumed to have been aligned.
12432 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12433 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12438 map
= isl_map_cow(map
);
12439 ma
= isl_multi_aff_align_divs(ma
);
12442 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12445 for (i
= 0; i
< map
->n
; ++i
) {
12446 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12447 isl_multi_aff_copy(ma
));
12452 space
= isl_multi_aff_get_domain_space(ma
);
12453 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12455 isl_space_free(map
->dim
);
12460 isl_multi_aff_free(ma
);
12462 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12463 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12466 isl_multi_aff_free(ma
);
12471 /* Compute the preimage of the domain or range (depending on "type")
12472 * of "map" under the function represented by "ma".
12473 * In other words, plug in "ma" in the domain or range of "map".
12474 * The result is a map that lives in the same space as "map"
12475 * except that the domain or range has been replaced by
12476 * the domain space of "ma".
12478 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12479 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12484 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12485 return map_preimage_multi_aff(map
, type
, ma
);
12487 if (!isl_space_has_named_params(map
->dim
) ||
12488 !isl_space_has_named_params(ma
->space
))
12489 isl_die(map
->ctx
, isl_error_invalid
,
12490 "unaligned unnamed parameters", goto error
);
12491 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12492 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12494 return map_preimage_multi_aff(map
, type
, ma
);
12496 isl_multi_aff_free(ma
);
12497 return isl_map_free(map
);
12500 /* Compute the preimage of "set" under the function represented by "ma".
12501 * In other words, plug in "ma" in "set". The result is a set
12502 * that lives in the domain space of "ma".
12504 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12505 __isl_take isl_multi_aff
*ma
)
12507 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12510 /* Compute the preimage of the domain of "map" under the function
12511 * represented by "ma".
12512 * In other words, plug in "ma" in the domain of "map".
12513 * The result is a map that lives in the same space as "map"
12514 * except that the domain has been replaced by the domain space of "ma".
12516 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12517 __isl_take isl_multi_aff
*ma
)
12519 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12522 /* Compute the preimage of the range of "map" under the function
12523 * represented by "ma".
12524 * In other words, plug in "ma" in the range of "map".
12525 * The result is a map that lives in the same space as "map"
12526 * except that the range has been replaced by the domain space of "ma".
12528 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12529 __isl_take isl_multi_aff
*ma
)
12531 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12534 /* Compute the preimage of "map" under the function represented by "pma".
12535 * In other words, plug in "pma" in the domain or range of "map".
12536 * The result is a map that lives in the same space as "map",
12537 * except that the space of type "type" has been replaced by
12538 * the domain space of "pma".
12540 * The parameters of "map" and "pma" are assumed to have been aligned.
12542 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12543 __isl_take isl_map
*map
, enum isl_dim_type type
,
12544 __isl_take isl_pw_multi_aff
*pma
)
12553 isl_pw_multi_aff_free(pma
);
12554 res
= isl_map_empty(isl_map_get_space(map
));
12559 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12560 isl_multi_aff_copy(pma
->p
[0].maff
));
12561 if (type
== isl_dim_in
)
12562 res
= isl_map_intersect_domain(res
,
12563 isl_map_copy(pma
->p
[0].set
));
12565 res
= isl_map_intersect_range(res
,
12566 isl_map_copy(pma
->p
[0].set
));
12568 for (i
= 1; i
< pma
->n
; ++i
) {
12571 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12572 isl_multi_aff_copy(pma
->p
[i
].maff
));
12573 if (type
== isl_dim_in
)
12574 res_i
= isl_map_intersect_domain(res_i
,
12575 isl_map_copy(pma
->p
[i
].set
));
12577 res_i
= isl_map_intersect_range(res_i
,
12578 isl_map_copy(pma
->p
[i
].set
));
12579 res
= isl_map_union(res
, res_i
);
12582 isl_pw_multi_aff_free(pma
);
12586 isl_pw_multi_aff_free(pma
);
12591 /* Compute the preimage of "map" under the function represented by "pma".
12592 * In other words, plug in "pma" in the domain or range of "map".
12593 * The result is a map that lives in the same space as "map",
12594 * except that the space of type "type" has been replaced by
12595 * the domain space of "pma".
12597 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12598 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12603 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12604 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12606 if (!isl_space_has_named_params(map
->dim
) ||
12607 !isl_space_has_named_params(pma
->dim
))
12608 isl_die(map
->ctx
, isl_error_invalid
,
12609 "unaligned unnamed parameters", goto error
);
12610 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12611 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12613 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12615 isl_pw_multi_aff_free(pma
);
12616 return isl_map_free(map
);
12619 /* Compute the preimage of "set" under the function represented by "pma".
12620 * In other words, plug in "pma" in "set". The result is a set
12621 * that lives in the domain space of "pma".
12623 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12624 __isl_take isl_pw_multi_aff
*pma
)
12626 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12629 /* Compute the preimage of the domain of "map" under the function
12630 * represented by "pma".
12631 * In other words, plug in "pma" in the domain of "map".
12632 * The result is a map that lives in the same space as "map",
12633 * except that domain space has been replaced by the domain space of "pma".
12635 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12636 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12638 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12641 /* Compute the preimage of the range of "map" under the function
12642 * represented by "pma".
12643 * In other words, plug in "pma" in the range of "map".
12644 * The result is a map that lives in the same space as "map",
12645 * except that range space has been replaced by the domain space of "pma".
12647 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12648 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12650 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12653 /* Compute the preimage of "map" under the function represented by "mpa".
12654 * In other words, plug in "mpa" in the domain or range of "map".
12655 * The result is a map that lives in the same space as "map",
12656 * except that the space of type "type" has been replaced by
12657 * the domain space of "mpa".
12659 * If the map does not involve any constraints that refer to the
12660 * dimensions of the substituted space, then the only possible
12661 * effect of "mpa" on the map is to map the space to a different space.
12662 * We create a separate isl_multi_aff to effectuate this change
12663 * in order to avoid spurious splitting of the map along the pieces
12666 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12667 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12670 isl_pw_multi_aff
*pma
;
12675 n
= isl_map_dim(map
, type
);
12676 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12680 space
= isl_multi_pw_aff_get_space(mpa
);
12681 isl_multi_pw_aff_free(mpa
);
12682 ma
= isl_multi_aff_zero(space
);
12683 return isl_map_preimage_multi_aff(map
, type
, ma
);
12686 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12687 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12690 isl_multi_pw_aff_free(mpa
);
12694 /* Compute the preimage of "map" under the function represented by "mpa".
12695 * In other words, plug in "mpa" in the domain "map".
12696 * The result is a map that lives in the same space as "map",
12697 * except that domain space has been replaced by the domain space of "mpa".
12699 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12700 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12702 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12705 /* Compute the preimage of "set" by the function represented by "mpa".
12706 * In other words, plug in "mpa" in "set".
12708 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12709 __isl_take isl_multi_pw_aff
*mpa
)
12711 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);