2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_lp_private.h>
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_vec_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.h>
36 #include <isl_val_private.h>
37 #include <isl/deprecated/map_int.h>
38 #include <isl/deprecated/set_int.h>
40 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
43 case isl_dim_param
: return dim
->nparam
;
44 case isl_dim_in
: return dim
->n_in
;
45 case isl_dim_out
: return dim
->n_out
;
46 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
51 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
54 case isl_dim_param
: return 1;
55 case isl_dim_in
: return 1 + dim
->nparam
;
56 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
61 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
62 enum isl_dim_type type
)
67 case isl_dim_cst
: return 1;
70 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
71 case isl_dim_div
: return bmap
->n_div
;
72 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
77 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
79 return map
? n(map
->dim
, type
) : 0;
82 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
84 return set
? n(set
->dim
, type
) : 0;
87 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
88 enum isl_dim_type type
)
90 isl_space
*dim
= bmap
->dim
;
92 case isl_dim_cst
: return 0;
93 case isl_dim_param
: return 1;
94 case isl_dim_in
: return 1 + dim
->nparam
;
95 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
96 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
101 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
102 enum isl_dim_type type
)
104 return isl_basic_map_offset(bset
, type
);
107 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
109 return pos(map
->dim
, type
);
112 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
113 enum isl_dim_type type
)
115 return isl_basic_map_dim(bset
, type
);
118 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
120 return isl_basic_set_dim(bset
, isl_dim_set
);
123 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
125 return isl_basic_set_dim(bset
, isl_dim_param
);
128 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
132 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
135 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
137 return isl_set_dim(set
, isl_dim_set
);
140 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
142 return isl_set_dim(set
, isl_dim_param
);
145 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
147 return bmap
? bmap
->dim
->n_in
: 0;
150 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
152 return bmap
? bmap
->dim
->n_out
: 0;
155 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
157 return bmap
? bmap
->dim
->nparam
: 0;
160 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
162 return bmap
? bmap
->n_div
: 0;
165 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
167 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
170 unsigned isl_map_n_in(const struct isl_map
*map
)
172 return map
? map
->dim
->n_in
: 0;
175 unsigned isl_map_n_out(const struct isl_map
*map
)
177 return map
? map
->dim
->n_out
: 0;
180 unsigned isl_map_n_param(const struct isl_map
*map
)
182 return map
? map
->dim
->nparam
: 0;
185 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
190 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
193 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
196 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
197 struct isl_basic_set
*bset
)
202 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
205 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
208 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
213 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
216 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
219 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
220 struct isl_basic_set
*bset
)
225 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
228 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
231 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
233 return bmap
? bmap
->ctx
: NULL
;
236 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
238 return bset
? bset
->ctx
: NULL
;
241 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
243 return map
? map
->ctx
: NULL
;
246 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
248 return set
? set
->ctx
: NULL
;
251 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
255 return isl_space_copy(bmap
->dim
);
258 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
262 return isl_space_copy(bset
->dim
);
265 /* Extract the divs in "bmap" as a matrix.
267 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
278 ctx
= isl_basic_map_get_ctx(bmap
);
279 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
280 cols
= 1 + 1 + total
+ bmap
->n_div
;
281 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
285 for (i
= 0; i
< bmap
->n_div
; ++i
)
286 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
291 /* Extract the divs in "bset" as a matrix.
293 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
295 return isl_basic_map_get_divs(bset
);
298 __isl_give isl_local_space
*isl_basic_map_get_local_space(
299 __isl_keep isl_basic_map
*bmap
)
306 div
= isl_basic_map_get_divs(bmap
);
307 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
310 __isl_give isl_local_space
*isl_basic_set_get_local_space(
311 __isl_keep isl_basic_set
*bset
)
313 return isl_basic_map_get_local_space(bset
);
316 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
317 __isl_take isl_local_space
*ls
)
326 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
327 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
328 n_div
, 0, 2 * n_div
);
330 for (i
= 0; i
< n_div
; ++i
)
331 if (isl_basic_map_alloc_div(bmap
) < 0)
334 for (i
= 0; i
< n_div
; ++i
) {
335 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
336 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
340 isl_local_space_free(ls
);
343 isl_local_space_free(ls
);
344 isl_basic_map_free(bmap
);
348 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
349 __isl_take isl_local_space
*ls
)
351 return isl_basic_map_from_local_space(ls
);
354 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
358 return isl_space_copy(map
->dim
);
361 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
365 return isl_space_copy(set
->dim
);
368 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
369 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
371 bmap
= isl_basic_map_cow(bmap
);
374 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
377 bmap
= isl_basic_map_finalize(bmap
);
380 isl_basic_map_free(bmap
);
384 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
385 __isl_take isl_basic_set
*bset
, const char *s
)
387 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
390 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
391 enum isl_dim_type type
)
393 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
396 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
397 enum isl_dim_type type
, const char *s
)
401 map
= isl_map_cow(map
);
405 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
409 for (i
= 0; i
< map
->n
; ++i
) {
410 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
421 /* Replace the identifier of the tuple of type "type" by "id".
423 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
424 __isl_take isl_basic_map
*bmap
,
425 enum isl_dim_type type
, __isl_take isl_id
*id
)
427 bmap
= isl_basic_map_cow(bmap
);
429 return isl_id_free(id
);
430 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
432 return isl_basic_map_free(bmap
);
433 bmap
= isl_basic_map_finalize(bmap
);
437 /* Replace the identifier of the tuple by "id".
439 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
440 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
442 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
445 /* Does the input or output tuple have a name?
447 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
449 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
452 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
453 enum isl_dim_type type
)
455 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
458 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
461 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
464 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
465 enum isl_dim_type type
, __isl_take isl_id
*id
)
467 map
= isl_map_cow(map
);
469 return isl_id_free(id
);
471 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
473 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
476 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
477 __isl_take isl_id
*id
)
479 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
482 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
483 enum isl_dim_type type
)
485 map
= isl_map_cow(map
);
489 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
491 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
494 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
496 return isl_map_reset_tuple_id(set
, isl_dim_set
);
499 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
501 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
504 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
505 enum isl_dim_type type
)
507 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
510 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
512 return isl_map_has_tuple_id(set
, isl_dim_set
);
515 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
517 return isl_map_get_tuple_id(set
, isl_dim_set
);
520 /* Does the set tuple have a name?
522 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
524 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
528 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
530 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
533 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
535 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
538 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
539 enum isl_dim_type type
, unsigned pos
)
541 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
544 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
545 enum isl_dim_type type
, unsigned pos
)
547 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
550 /* Does the given dimension have a name?
552 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
553 enum isl_dim_type type
, unsigned pos
)
555 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
558 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
559 enum isl_dim_type type
, unsigned pos
)
561 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
564 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
565 enum isl_dim_type type
, unsigned pos
)
567 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
570 /* Does the given dimension have a name?
572 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
573 enum isl_dim_type type
, unsigned pos
)
575 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
578 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
579 __isl_take isl_basic_map
*bmap
,
580 enum isl_dim_type type
, unsigned pos
, const char *s
)
582 bmap
= isl_basic_map_cow(bmap
);
585 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
588 return isl_basic_map_finalize(bmap
);
590 isl_basic_map_free(bmap
);
594 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
595 enum isl_dim_type type
, unsigned pos
, const char *s
)
599 map
= isl_map_cow(map
);
603 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
607 for (i
= 0; i
< map
->n
; ++i
) {
608 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
619 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
620 __isl_take isl_basic_set
*bset
,
621 enum isl_dim_type type
, unsigned pos
, const char *s
)
623 return (isl_basic_set
*)isl_basic_map_set_dim_name(
624 (isl_basic_map
*)bset
, type
, pos
, s
);
627 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
628 enum isl_dim_type type
, unsigned pos
, const char *s
)
630 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
633 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
634 enum isl_dim_type type
, unsigned pos
)
636 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
639 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
640 enum isl_dim_type type
, unsigned pos
)
642 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
645 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
646 enum isl_dim_type type
, unsigned pos
)
648 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
651 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
652 enum isl_dim_type type
, unsigned pos
)
654 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
657 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
658 enum isl_dim_type type
, unsigned pos
)
660 return isl_map_has_dim_id(set
, type
, pos
);
663 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
664 enum isl_dim_type type
, unsigned pos
)
666 return isl_map_get_dim_id(set
, type
, pos
);
669 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
670 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
672 map
= isl_map_cow(map
);
674 return isl_id_free(id
);
676 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
678 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
681 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
682 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
684 return isl_map_set_dim_id(set
, type
, pos
, id
);
687 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
688 __isl_keep isl_id
*id
)
692 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
695 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
696 __isl_keep isl_id
*id
)
698 return isl_map_find_dim_by_id(set
, type
, id
);
701 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
706 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
709 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
712 return isl_map_find_dim_by_name(set
, type
, name
);
715 /* Reset the user pointer on all identifiers of parameters and tuples
716 * of the space of "map".
718 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
722 space
= isl_map_get_space(map
);
723 space
= isl_space_reset_user(space
);
724 map
= isl_map_reset_space(map
, space
);
729 /* Reset the user pointer on all identifiers of parameters and tuples
730 * of the space of "set".
732 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
734 return isl_map_reset_user(set
);
737 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
741 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
744 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
746 return isl_basic_map_is_rational(bset
);
749 /* Does "bmap" contain any rational points?
751 * If "bmap" has an equality for each dimension, equating the dimension
752 * to an integer constant, then it has no rational points, even if it
753 * is marked as rational.
755 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
757 int has_rational
= 1;
762 if (isl_basic_map_plain_is_empty(bmap
))
764 if (!isl_basic_map_is_rational(bmap
))
766 bmap
= isl_basic_map_copy(bmap
);
767 bmap
= isl_basic_map_implicit_equalities(bmap
);
770 total
= isl_basic_map_total_dim(bmap
);
771 if (bmap
->n_eq
== total
) {
773 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
774 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
777 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
778 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
780 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
788 isl_basic_map_free(bmap
);
793 /* Does "map" contain any rational points?
795 int isl_map_has_rational(__isl_keep isl_map
*map
)
802 for (i
= 0; i
< map
->n
; ++i
) {
803 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
804 if (has_rational
< 0)
812 /* Does "set" contain any rational points?
814 int isl_set_has_rational(__isl_keep isl_set
*set
)
816 return isl_map_has_rational(set
);
819 /* Is this basic set a parameter domain?
821 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
825 return isl_space_is_params(bset
->dim
);
828 /* Is this set a parameter domain?
830 int isl_set_is_params(__isl_keep isl_set
*set
)
834 return isl_space_is_params(set
->dim
);
837 /* Is this map actually a parameter domain?
838 * Users should never call this function. Outside of isl,
839 * a map can never be a parameter domain.
841 int isl_map_is_params(__isl_keep isl_map
*map
)
845 return isl_space_is_params(map
->dim
);
848 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
849 struct isl_basic_map
*bmap
, unsigned extra
,
850 unsigned n_eq
, unsigned n_ineq
)
853 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
858 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
859 if (isl_blk_is_error(bmap
->block
))
862 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
863 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
867 bmap
->block2
= isl_blk_empty();
870 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
871 if (isl_blk_is_error(bmap
->block2
))
874 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
879 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
880 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
882 for (i
= 0; i
< extra
; ++i
)
883 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
887 bmap
->c_size
= n_eq
+ n_ineq
;
888 bmap
->eq
= bmap
->ineq
+ n_ineq
;
897 isl_basic_map_free(bmap
);
901 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
902 unsigned nparam
, unsigned dim
, unsigned extra
,
903 unsigned n_eq
, unsigned n_ineq
)
905 struct isl_basic_map
*bmap
;
908 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
912 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
913 return (struct isl_basic_set
*)bmap
;
916 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
917 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
919 struct isl_basic_map
*bmap
;
922 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
923 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
924 return (struct isl_basic_set
*)bmap
;
930 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
931 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
933 struct isl_basic_map
*bmap
;
937 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
942 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
948 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
949 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
950 unsigned n_eq
, unsigned n_ineq
)
952 struct isl_basic_map
*bmap
;
955 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
959 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
963 static void dup_constraints(
964 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
967 unsigned total
= isl_basic_map_total_dim(src
);
969 for (i
= 0; i
< src
->n_eq
; ++i
) {
970 int j
= isl_basic_map_alloc_equality(dst
);
971 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
974 for (i
= 0; i
< src
->n_ineq
; ++i
) {
975 int j
= isl_basic_map_alloc_inequality(dst
);
976 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
979 for (i
= 0; i
< src
->n_div
; ++i
) {
980 int j
= isl_basic_map_alloc_div(dst
);
981 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
983 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
986 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
988 struct isl_basic_map
*dup
;
992 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
993 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
996 dup_constraints(dup
, bmap
);
997 dup
->flags
= bmap
->flags
;
998 dup
->sample
= isl_vec_copy(bmap
->sample
);
1002 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1004 struct isl_basic_map
*dup
;
1006 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1007 return (struct isl_basic_set
*)dup
;
1010 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1015 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1019 return isl_basic_set_dup(bset
);
1022 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1031 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1036 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1040 bmap
= isl_basic_map_dup(bmap
);
1042 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1046 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1055 void *isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1060 if (--bmap
->ref
> 0)
1063 isl_ctx_deref(bmap
->ctx
);
1065 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1067 isl_blk_free(bmap
->ctx
, bmap
->block
);
1068 isl_vec_free(bmap
->sample
);
1069 isl_space_free(bmap
->dim
);
1075 void *isl_basic_set_free(struct isl_basic_set
*bset
)
1077 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1080 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1082 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1085 __isl_give isl_map
*isl_map_align_params_map_map_and(
1086 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1087 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1088 __isl_take isl_map
*map2
))
1092 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1093 return fn(map1
, map2
);
1094 if (!isl_space_has_named_params(map1
->dim
) ||
1095 !isl_space_has_named_params(map2
->dim
))
1096 isl_die(map1
->ctx
, isl_error_invalid
,
1097 "unaligned unnamed parameters", goto error
);
1098 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1099 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1100 return fn(map1
, map2
);
1107 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1108 __isl_keep isl_map
*map2
,
1109 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1115 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1116 return fn(map1
, map2
);
1117 if (!isl_space_has_named_params(map1
->dim
) ||
1118 !isl_space_has_named_params(map2
->dim
))
1119 isl_die(map1
->ctx
, isl_error_invalid
,
1120 "unaligned unnamed parameters", return -1);
1121 map1
= isl_map_copy(map1
);
1122 map2
= isl_map_copy(map2
);
1123 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1124 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1131 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1133 struct isl_ctx
*ctx
;
1137 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1138 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1140 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1141 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1142 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1143 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1144 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1145 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1147 int j
= isl_basic_map_alloc_inequality(bmap
);
1151 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1152 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1159 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1160 bmap
->extra
- bmap
->n_div
);
1161 return bmap
->n_eq
++;
1164 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1166 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1169 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1173 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1178 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1180 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1183 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1188 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1190 if (pos
!= bmap
->n_eq
- 1) {
1192 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1193 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1199 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1201 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1204 /* Turn inequality "pos" of "bmap" into an equality.
1206 * In particular, we move the inequality in front of the equalities
1207 * and move the last inequality in the position of the moved inequality.
1208 * Note that isl_tab_make_equalities_explicit depends on this particular
1209 * change in the ordering of the constraints.
1211 void isl_basic_map_inequality_to_equality(
1212 struct isl_basic_map
*bmap
, unsigned pos
)
1216 t
= bmap
->ineq
[pos
];
1217 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1218 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1223 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1224 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1225 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1226 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1229 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1231 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1234 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1236 struct isl_ctx
*ctx
;
1240 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1241 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1242 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1243 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1244 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1245 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1246 1 + isl_basic_map_total_dim(bmap
),
1247 bmap
->extra
- bmap
->n_div
);
1248 return bmap
->n_ineq
++;
1251 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1253 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1256 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1260 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1265 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1267 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1270 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1275 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1277 if (pos
!= bmap
->n_ineq
- 1) {
1278 t
= bmap
->ineq
[pos
];
1279 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1280 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1281 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1287 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1289 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1292 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1297 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1300 k
= isl_basic_map_alloc_equality(bmap
);
1303 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1306 isl_basic_map_free(bmap
);
1310 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1313 return (isl_basic_set
*)
1314 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1317 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1322 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1325 k
= isl_basic_map_alloc_inequality(bmap
);
1328 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1331 isl_basic_map_free(bmap
);
1335 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1338 return (isl_basic_set
*)
1339 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1342 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1346 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1347 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1348 1 + 1 + isl_basic_map_total_dim(bmap
),
1349 bmap
->extra
- bmap
->n_div
);
1350 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1351 return bmap
->n_div
++;
1354 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1356 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1359 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1363 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1368 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1370 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1373 /* Copy constraint from src to dst, putting the vars of src at offset
1374 * dim_off in dst and the divs of src at offset div_off in dst.
1375 * If both sets are actually map, then dim_off applies to the input
1378 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1379 struct isl_basic_map
*src_map
, isl_int
*src
,
1380 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1382 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1383 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1384 unsigned src_in
= isl_basic_map_n_in(src_map
);
1385 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1386 unsigned src_out
= isl_basic_map_n_out(src_map
);
1387 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1388 isl_int_set(dst
[0], src
[0]);
1389 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1390 if (dst_nparam
> src_nparam
)
1391 isl_seq_clr(dst
+1+src_nparam
,
1392 dst_nparam
- src_nparam
);
1393 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1394 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1396 isl_min(dst_in
-in_off
, src_in
));
1397 if (dst_in
-in_off
> src_in
)
1398 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1399 dst_in
- in_off
- src_in
);
1400 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1401 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1402 src
+1+src_nparam
+src_in
,
1403 isl_min(dst_out
-out_off
, src_out
));
1404 if (dst_out
-out_off
> src_out
)
1405 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1406 dst_out
- out_off
- src_out
);
1407 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1408 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1409 src
+1+src_nparam
+src_in
+src_out
,
1410 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1411 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1412 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1413 div_off
+src_map
->n_div
,
1414 dst_map
->n_div
- div_off
- src_map
->n_div
);
1417 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1418 struct isl_basic_map
*src_map
, isl_int
*src
,
1419 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1421 isl_int_set(dst
[0], src
[0]);
1422 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1425 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1426 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1431 if (!bmap1
|| !bmap2
)
1434 div_off
= bmap1
->n_div
;
1436 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1437 int i1
= isl_basic_map_alloc_equality(bmap1
);
1440 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1441 i_pos
, o_pos
, div_off
);
1444 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1445 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1448 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1449 i_pos
, o_pos
, div_off
);
1452 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1453 int i1
= isl_basic_map_alloc_div(bmap1
);
1456 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1457 i_pos
, o_pos
, div_off
);
1460 isl_basic_map_free(bmap2
);
1465 isl_basic_map_free(bmap1
);
1466 isl_basic_map_free(bmap2
);
1470 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1471 struct isl_basic_set
*bset2
, unsigned pos
)
1473 return (struct isl_basic_set
*)
1474 add_constraints((struct isl_basic_map
*)bset1
,
1475 (struct isl_basic_map
*)bset2
, 0, pos
);
1478 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1479 __isl_take isl_space
*dim
, unsigned extra
,
1480 unsigned n_eq
, unsigned n_ineq
)
1482 struct isl_basic_map
*ext
;
1492 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1493 base
->extra
>= base
->n_div
+ extra
;
1495 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1496 room_for_ineq(base
, n_ineq
)) {
1497 isl_space_free(dim
);
1501 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1502 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1503 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1504 extra
+= base
->extra
;
1506 n_ineq
+= base
->n_ineq
;
1508 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1514 ext
->sample
= isl_vec_copy(base
->sample
);
1515 flags
= base
->flags
;
1516 ext
= add_constraints(ext
, base
, 0, 0);
1519 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1525 isl_space_free(dim
);
1526 isl_basic_map_free(base
);
1530 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1531 __isl_take isl_space
*dim
, unsigned extra
,
1532 unsigned n_eq
, unsigned n_ineq
)
1534 return (struct isl_basic_set
*)
1535 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1536 extra
, n_eq
, n_ineq
);
1539 struct isl_basic_map
*isl_basic_map_extend_constraints(
1540 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1544 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1548 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1549 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1550 unsigned n_eq
, unsigned n_ineq
)
1552 struct isl_basic_map
*bmap
;
1557 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1561 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1564 isl_basic_map_free(base
);
1568 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1569 unsigned nparam
, unsigned dim
, unsigned extra
,
1570 unsigned n_eq
, unsigned n_ineq
)
1572 return (struct isl_basic_set
*)
1573 isl_basic_map_extend((struct isl_basic_map
*)base
,
1574 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1577 struct isl_basic_set
*isl_basic_set_extend_constraints(
1578 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1580 return (struct isl_basic_set
*)
1581 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1585 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1587 return (struct isl_basic_set
*)
1588 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1591 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1596 if (bmap
->ref
> 1) {
1598 bmap
= isl_basic_map_dup(bmap
);
1601 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1605 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1613 return isl_set_dup(set
);
1616 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1624 return isl_map_dup(map
);
1627 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1628 unsigned a_len
, unsigned b_len
)
1630 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1631 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1632 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1635 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1636 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1644 isl_assert(bmap
->ctx
,
1645 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1647 if (n1
== 0 || n2
== 0)
1650 bmap
= isl_basic_map_cow(bmap
);
1654 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1655 if (isl_blk_is_error(blk
))
1658 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1660 bmap
->eq
[i
] + pos
, n1
, n2
);
1662 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1664 bmap
->ineq
[i
] + pos
, n1
, n2
);
1666 for (i
= 0; i
< bmap
->n_div
; ++i
)
1668 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1670 isl_blk_free(bmap
->ctx
, blk
);
1672 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1673 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1674 return isl_basic_map_finalize(bmap
);
1676 isl_basic_map_free(bmap
);
1680 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1681 __isl_take isl_basic_set
*bset
, unsigned n
)
1689 nparam
= isl_basic_set_n_param(bset
);
1690 dim
= isl_basic_set_n_dim(bset
);
1691 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1693 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1695 isl_basic_set_free(bset
);
1699 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1705 total
= isl_basic_map_total_dim(bmap
);
1706 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1707 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1709 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1711 i
= isl_basic_map_alloc_equality(bmap
);
1715 isl_int_set_si(bmap
->eq
[i
][0], 1);
1716 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1717 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1718 isl_vec_free(bmap
->sample
);
1719 bmap
->sample
= NULL
;
1720 return isl_basic_map_finalize(bmap
);
1722 isl_basic_map_free(bmap
);
1726 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1728 return (struct isl_basic_set
*)
1729 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1732 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1735 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1737 isl_int
*t
= bmap
->div
[a
];
1738 bmap
->div
[a
] = bmap
->div
[b
];
1742 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1743 * div definitions accordingly.
1745 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1748 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1750 swap_div(bmap
, a
, b
);
1752 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1753 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1755 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1756 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1758 for (i
= 0; i
< bmap
->n_div
; ++i
)
1759 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1760 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1763 /* Eliminate the specified n dimensions starting at first from the
1764 * constraints, without removing the dimensions from the space.
1765 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1767 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1768 enum isl_dim_type type
, unsigned first
, unsigned n
)
1777 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1778 isl_die(map
->ctx
, isl_error_invalid
,
1779 "index out of bounds", goto error
);
1781 map
= isl_map_cow(map
);
1785 for (i
= 0; i
< map
->n
; ++i
) {
1786 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1796 /* Eliminate the specified n dimensions starting at first from the
1797 * constraints, without removing the dimensions from the space.
1798 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1800 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1801 enum isl_dim_type type
, unsigned first
, unsigned n
)
1803 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1806 /* Eliminate the specified n dimensions starting at first from the
1807 * constraints, without removing the dimensions from the space.
1808 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1810 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1811 unsigned first
, unsigned n
)
1813 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1816 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1817 __isl_take isl_basic_map
*bmap
)
1821 bmap
= isl_basic_map_eliminate_vars(bmap
,
1822 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1826 return isl_basic_map_finalize(bmap
);
1829 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1830 __isl_take isl_basic_set
*bset
)
1832 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1833 (struct isl_basic_map
*)bset
);
1836 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1845 map
= isl_map_cow(map
);
1849 for (i
= 0; i
< map
->n
; ++i
) {
1850 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1860 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1862 return isl_map_remove_divs(set
);
1865 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1866 enum isl_dim_type type
, unsigned first
, unsigned n
)
1870 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1872 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1874 bmap
= isl_basic_map_eliminate_vars(bmap
,
1875 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1878 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1880 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1883 isl_basic_map_free(bmap
);
1887 /* Return true if the definition of the given div (recursively) involves
1888 * any of the given variables.
1890 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1891 unsigned first
, unsigned n
)
1894 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1896 if (isl_int_is_zero(bmap
->div
[div
][0]))
1898 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1901 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1902 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1904 if (div_involves_vars(bmap
, i
, first
, n
))
1911 /* Try and add a lower and/or upper bound on "div" to "bmap"
1912 * based on inequality "i".
1913 * "total" is the total number of variables (excluding the divs).
1914 * "v" is a temporary object that can be used during the calculations.
1915 * If "lb" is set, then a lower bound should be constructed.
1916 * If "ub" is set, then an upper bound should be constructed.
1918 * The calling function has already checked that the inequality does not
1919 * reference "div", but we still need to check that the inequality is
1920 * of the right form. We'll consider the case where we want to construct
1921 * a lower bound. The construction of upper bounds is similar.
1923 * Let "div" be of the form
1925 * q = floor((a + f(x))/d)
1927 * We essentially check if constraint "i" is of the form
1931 * so that we can use it to derive a lower bound on "div".
1932 * However, we allow a slightly more general form
1936 * with the condition that the coefficients of g(x) - f(x) are all
1938 * Rewriting this constraint as
1942 * adding a + f(x) to both sides and dividing by d, we obtain
1944 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1946 * Taking the floor on both sides, we obtain
1948 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1952 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1954 * In the case of an upper bound, we construct the constraint
1956 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1959 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1960 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1961 unsigned total
, isl_int v
, int lb
, int ub
)
1965 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1967 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1968 bmap
->div
[div
][1 + 1 + j
]);
1969 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1972 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1973 bmap
->div
[div
][1 + 1 + j
]);
1974 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1980 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1982 int k
= isl_basic_map_alloc_inequality(bmap
);
1985 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1986 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1987 bmap
->div
[div
][1 + j
]);
1988 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1989 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1991 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
1994 int k
= isl_basic_map_alloc_inequality(bmap
);
1997 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1998 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1999 bmap
->div
[div
][1 + j
]);
2000 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2001 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2003 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2008 isl_basic_map_free(bmap
);
2012 /* This function is called right before "div" is eliminated from "bmap"
2013 * using Fourier-Motzkin.
2014 * Look through the constraints of "bmap" for constraints on the argument
2015 * of the integer division and use them to construct constraints on the
2016 * integer division itself. These constraints can then be combined
2017 * during the Fourier-Motzkin elimination.
2018 * Note that it is only useful to introduce lower bounds on "div"
2019 * if "bmap" already contains upper bounds on "div" as the newly
2020 * introduce lower bounds can then be combined with the pre-existing
2021 * upper bounds. Similarly for upper bounds.
2022 * We therefore first check if "bmap" contains any lower and/or upper bounds
2025 * It is interesting to note that the introduction of these constraints
2026 * can indeed lead to more accurate results, even when compared to
2027 * deriving constraints on the argument of "div" from constraints on "div".
2028 * Consider, for example, the set
2030 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2032 * The second constraint can be rewritten as
2034 * 2 * [(-i-2j+3)/4] + k >= 0
2036 * from which we can derive
2038 * -i - 2j + 3 >= -2k
2044 * Combined with the first constraint, we obtain
2046 * -3 <= 3 + 2k or k >= -3
2048 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2049 * the first constraint, we obtain
2051 * [(i + 2j)/4] >= [-3/4] = -1
2053 * Combining this constraint with the second constraint, we obtain
2057 static __isl_give isl_basic_map
*insert_bounds_on_div(
2058 __isl_take isl_basic_map
*bmap
, int div
)
2061 int check_lb
, check_ub
;
2068 if (isl_int_is_zero(bmap
->div
[div
][0]))
2071 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2075 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2076 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2083 if (!check_lb
&& !check_ub
)
2088 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2089 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2092 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2093 check_lb
, check_ub
);
2101 /* Remove all divs (recursively) involving any of the given dimensions
2102 * in their definitions.
2104 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2105 __isl_take isl_basic_map
*bmap
,
2106 enum isl_dim_type type
, unsigned first
, unsigned n
)
2112 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2114 first
+= isl_basic_map_offset(bmap
, type
);
2116 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2117 if (!div_involves_vars(bmap
, i
, first
, n
))
2119 bmap
= insert_bounds_on_div(bmap
, i
);
2120 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2128 isl_basic_map_free(bmap
);
2132 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2133 __isl_take isl_basic_set
*bset
,
2134 enum isl_dim_type type
, unsigned first
, unsigned n
)
2136 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2139 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2140 enum isl_dim_type type
, unsigned first
, unsigned n
)
2149 map
= isl_map_cow(map
);
2153 for (i
= 0; i
< map
->n
; ++i
) {
2154 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2165 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2166 enum isl_dim_type type
, unsigned first
, unsigned n
)
2168 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2172 /* Does the desciption of "bmap" depend on the specified dimensions?
2173 * We also check whether the dimensions appear in any of the div definitions.
2174 * In principle there is no need for this check. If the dimensions appear
2175 * in a div definition, they also appear in the defining constraints of that
2178 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2179 enum isl_dim_type type
, unsigned first
, unsigned n
)
2186 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2187 isl_die(bmap
->ctx
, isl_error_invalid
,
2188 "index out of bounds", return -1);
2190 first
+= isl_basic_map_offset(bmap
, type
);
2191 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2192 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2194 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2195 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2197 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2198 if (isl_int_is_zero(bmap
->div
[i
][0]))
2200 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2207 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2208 enum isl_dim_type type
, unsigned first
, unsigned n
)
2215 if (first
+ n
> isl_map_dim(map
, type
))
2216 isl_die(map
->ctx
, isl_error_invalid
,
2217 "index out of bounds", return -1);
2219 for (i
= 0; i
< map
->n
; ++i
) {
2220 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2222 if (involves
< 0 || involves
)
2229 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2230 enum isl_dim_type type
, unsigned first
, unsigned n
)
2232 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2235 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2236 enum isl_dim_type type
, unsigned first
, unsigned n
)
2238 return isl_map_involves_dims(set
, type
, first
, n
);
2241 /* Return true if the definition of the given div is unknown or depends
2244 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2247 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2249 if (isl_int_is_zero(bmap
->div
[div
][0]))
2252 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2253 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2255 if (div_is_unknown(bmap
, i
))
2262 /* Remove all divs that are unknown or defined in terms of unknown divs.
2264 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2265 __isl_take isl_basic_map
*bmap
)
2272 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2273 if (!div_is_unknown(bmap
, i
))
2275 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2284 /* Remove all divs that are unknown or defined in terms of unknown divs.
2286 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2287 __isl_take isl_basic_set
*bset
)
2289 return isl_basic_map_remove_unknown_divs(bset
);
2292 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2301 map
= isl_map_cow(map
);
2305 for (i
= 0; i
< map
->n
; ++i
) {
2306 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2316 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2318 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2321 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2322 __isl_take isl_basic_set
*bset
,
2323 enum isl_dim_type type
, unsigned first
, unsigned n
)
2325 return (isl_basic_set
*)
2326 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2329 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2330 enum isl_dim_type type
, unsigned first
, unsigned n
)
2337 map
= isl_map_cow(map
);
2340 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2342 for (i
= 0; i
< map
->n
; ++i
) {
2343 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2344 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2348 map
= isl_map_drop(map
, type
, first
, n
);
2355 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2356 enum isl_dim_type type
, unsigned first
, unsigned n
)
2358 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2361 /* Project out n inputs starting at first using Fourier-Motzkin */
2362 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2363 unsigned first
, unsigned n
)
2365 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2368 static void dump_term(struct isl_basic_map
*bmap
,
2369 isl_int c
, int pos
, FILE *out
)
2372 unsigned in
= isl_basic_map_n_in(bmap
);
2373 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2374 unsigned nparam
= isl_basic_map_n_param(bmap
);
2376 isl_int_print(out
, c
, 0);
2378 if (!isl_int_is_one(c
))
2379 isl_int_print(out
, c
, 0);
2380 if (pos
< 1 + nparam
) {
2381 name
= isl_space_get_dim_name(bmap
->dim
,
2382 isl_dim_param
, pos
- 1);
2384 fprintf(out
, "%s", name
);
2386 fprintf(out
, "p%d", pos
- 1);
2387 } else if (pos
< 1 + nparam
+ in
)
2388 fprintf(out
, "i%d", pos
- 1 - nparam
);
2389 else if (pos
< 1 + nparam
+ dim
)
2390 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2392 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2396 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2397 int sign
, FILE *out
)
2401 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2405 for (i
= 0, first
= 1; i
< len
; ++i
) {
2406 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2409 fprintf(out
, " + ");
2411 isl_int_abs(v
, c
[i
]);
2412 dump_term(bmap
, v
, i
, out
);
2419 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2420 const char *op
, FILE *out
, int indent
)
2424 fprintf(out
, "%*s", indent
, "");
2426 dump_constraint_sign(bmap
, c
, 1, out
);
2427 fprintf(out
, " %s ", op
);
2428 dump_constraint_sign(bmap
, c
, -1, out
);
2432 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2433 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2435 fprintf(out
, "%*s", indent
, "");
2436 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2441 static void dump_constraints(struct isl_basic_map
*bmap
,
2442 isl_int
**c
, unsigned n
,
2443 const char *op
, FILE *out
, int indent
)
2447 for (i
= 0; i
< n
; ++i
)
2448 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2451 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2455 unsigned total
= isl_basic_map_total_dim(bmap
);
2457 for (j
= 0; j
< 1 + total
; ++j
) {
2458 if (isl_int_is_zero(exp
[j
]))
2460 if (!first
&& isl_int_is_pos(exp
[j
]))
2462 dump_term(bmap
, exp
[j
], j
, out
);
2467 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2471 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2472 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2474 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2475 fprintf(out
, "%*s", indent
, "");
2476 fprintf(out
, "e%d = [(", i
);
2477 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2479 isl_int_print(out
, bmap
->div
[i
][0], 0);
2480 fprintf(out
, "]\n");
2484 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2485 FILE *out
, int indent
)
2488 fprintf(out
, "null basic set\n");
2492 fprintf(out
, "%*s", indent
, "");
2493 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2494 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2495 bset
->extra
, bset
->flags
);
2496 dump((struct isl_basic_map
*)bset
, out
, indent
);
2499 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2500 FILE *out
, int indent
)
2503 fprintf(out
, "null basic map\n");
2507 fprintf(out
, "%*s", indent
, "");
2508 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2509 "flags: %x, n_name: %d\n",
2511 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2512 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2513 dump(bmap
, out
, indent
);
2516 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2521 total
= isl_basic_map_total_dim(bmap
);
2522 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2523 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2524 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2525 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2529 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2532 struct isl_set
*set
;
2536 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2537 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2538 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2539 sizeof(struct isl_set
) +
2540 (n
- 1) * sizeof(struct isl_basic_set
*));
2544 set
->ctx
= dim
->ctx
;
2545 isl_ctx_ref(set
->ctx
);
2553 isl_space_free(dim
);
2557 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2558 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2560 struct isl_set
*set
;
2563 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2567 set
= isl_set_alloc_space(dims
, n
, flags
);
2571 /* Make sure "map" has room for at least "n" more basic maps.
2573 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2576 struct isl_map
*grown
= NULL
;
2580 isl_assert(map
->ctx
, n
>= 0, goto error
);
2581 if (map
->n
+ n
<= map
->size
)
2583 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2586 for (i
= 0; i
< map
->n
; ++i
) {
2587 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2595 isl_map_free(grown
);
2600 /* Make sure "set" has room for at least "n" more basic sets.
2602 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2604 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2607 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2610 struct isl_set
*dup
;
2615 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2618 for (i
= 0; i
< set
->n
; ++i
)
2619 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2623 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2625 return isl_map_from_basic_map(bset
);
2628 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2630 struct isl_map
*map
;
2635 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2636 return isl_map_add_basic_map(map
, bmap
);
2639 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2640 __isl_take isl_basic_set
*bset
)
2642 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2643 (struct isl_basic_map
*)bset
);
2646 void *isl_set_free(__isl_take isl_set
*set
)
2656 isl_ctx_deref(set
->ctx
);
2657 for (i
= 0; i
< set
->n
; ++i
)
2658 isl_basic_set_free(set
->p
[i
]);
2659 isl_space_free(set
->dim
);
2665 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2670 fprintf(out
, "null set\n");
2674 fprintf(out
, "%*s", indent
, "");
2675 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2676 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2678 for (i
= 0; i
< set
->n
; ++i
) {
2679 fprintf(out
, "%*s", indent
, "");
2680 fprintf(out
, "basic set %d:\n", i
);
2681 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2685 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2690 fprintf(out
, "null map\n");
2694 fprintf(out
, "%*s", indent
, "");
2695 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2696 "flags: %x, n_name: %d\n",
2697 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2698 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2699 for (i
= 0; i
< map
->n
; ++i
) {
2700 fprintf(out
, "%*s", indent
, "");
2701 fprintf(out
, "basic map %d:\n", i
);
2702 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2706 struct isl_basic_map
*isl_basic_map_intersect_domain(
2707 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2709 struct isl_basic_map
*bmap_domain
;
2714 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2715 bset
->dim
, isl_dim_param
), goto error
);
2717 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2718 isl_assert(bset
->ctx
,
2719 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2721 bmap
= isl_basic_map_cow(bmap
);
2724 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2725 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2726 bmap_domain
= isl_basic_map_from_domain(bset
);
2727 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2729 bmap
= isl_basic_map_simplify(bmap
);
2730 return isl_basic_map_finalize(bmap
);
2732 isl_basic_map_free(bmap
);
2733 isl_basic_set_free(bset
);
2737 struct isl_basic_map
*isl_basic_map_intersect_range(
2738 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2740 struct isl_basic_map
*bmap_range
;
2745 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2746 bset
->dim
, isl_dim_param
), goto error
);
2748 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2749 isl_assert(bset
->ctx
,
2750 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2752 if (isl_basic_set_is_universe(bset
)) {
2753 isl_basic_set_free(bset
);
2757 bmap
= isl_basic_map_cow(bmap
);
2760 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2761 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2762 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2763 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2765 bmap
= isl_basic_map_simplify(bmap
);
2766 return isl_basic_map_finalize(bmap
);
2768 isl_basic_map_free(bmap
);
2769 isl_basic_set_free(bset
);
2773 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2782 total
= 1 + isl_basic_map_total_dim(bmap
);
2783 if (total
!= vec
->size
)
2788 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2789 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2790 if (!isl_int_is_zero(s
)) {
2796 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2797 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2798 if (isl_int_is_neg(s
)) {
2809 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2811 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2814 struct isl_basic_map
*isl_basic_map_intersect(
2815 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2817 struct isl_vec
*sample
= NULL
;
2819 if (!bmap1
|| !bmap2
)
2822 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2823 bmap2
->dim
, isl_dim_param
), goto error
);
2824 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2825 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2826 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2827 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2828 return isl_basic_map_intersect(bmap2
, bmap1
);
2830 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2831 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2832 isl_assert(bmap1
->ctx
,
2833 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2835 if (bmap1
->sample
&&
2836 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2837 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2838 sample
= isl_vec_copy(bmap1
->sample
);
2839 else if (bmap2
->sample
&&
2840 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2841 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2842 sample
= isl_vec_copy(bmap2
->sample
);
2844 bmap1
= isl_basic_map_cow(bmap1
);
2847 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2848 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2849 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2852 isl_vec_free(sample
);
2854 isl_vec_free(bmap1
->sample
);
2855 bmap1
->sample
= sample
;
2858 bmap1
= isl_basic_map_simplify(bmap1
);
2859 return isl_basic_map_finalize(bmap1
);
2862 isl_vec_free(sample
);
2863 isl_basic_map_free(bmap1
);
2864 isl_basic_map_free(bmap2
);
2868 struct isl_basic_set
*isl_basic_set_intersect(
2869 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2871 return (struct isl_basic_set
*)
2872 isl_basic_map_intersect(
2873 (struct isl_basic_map
*)bset1
,
2874 (struct isl_basic_map
*)bset2
);
2877 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2878 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2880 return isl_basic_set_intersect(bset1
, bset2
);
2883 /* Special case of isl_map_intersect, where both map1 and map2
2884 * are convex, without any divs and such that either map1 or map2
2885 * contains a single constraint. This constraint is then simply
2886 * added to the other map.
2888 static __isl_give isl_map
*map_intersect_add_constraint(
2889 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2891 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2892 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2893 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2894 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2896 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2897 return isl_map_intersect(map2
, map1
);
2899 isl_assert(map2
->ctx
,
2900 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2902 map1
= isl_map_cow(map1
);
2905 if (isl_map_plain_is_empty(map1
)) {
2909 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2910 if (map2
->p
[0]->n_eq
== 1)
2911 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2913 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2914 map2
->p
[0]->ineq
[0]);
2916 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2917 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2921 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2922 isl_basic_map_free(map1
->p
[0]);
2935 /* map2 may be either a parameter domain or a map living in the same
2938 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2939 __isl_take isl_map
*map2
)
2948 if ((isl_map_plain_is_empty(map1
) ||
2949 isl_map_plain_is_universe(map2
)) &&
2950 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2954 if ((isl_map_plain_is_empty(map2
) ||
2955 isl_map_plain_is_universe(map1
)) &&
2956 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2961 if (map1
->n
== 1 && map2
->n
== 1 &&
2962 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2963 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2964 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2965 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2966 return map_intersect_add_constraint(map1
, map2
);
2968 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2969 isl_space_dim(map2
->dim
, isl_dim_param
))
2970 isl_assert(map1
->ctx
,
2971 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2973 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2974 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2975 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2977 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2978 map1
->n
* map2
->n
, flags
);
2981 for (i
= 0; i
< map1
->n
; ++i
)
2982 for (j
= 0; j
< map2
->n
; ++j
) {
2983 struct isl_basic_map
*part
;
2984 part
= isl_basic_map_intersect(
2985 isl_basic_map_copy(map1
->p
[i
]),
2986 isl_basic_map_copy(map2
->p
[j
]));
2987 if (isl_basic_map_is_empty(part
) < 0)
2988 part
= isl_basic_map_free(part
);
2989 result
= isl_map_add_basic_map(result
, part
);
3002 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3003 __isl_take isl_map
*map2
)
3007 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3008 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3009 "spaces don't match", goto error
);
3010 return map_intersect_internal(map1
, map2
);
3017 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3018 __isl_take isl_map
*map2
)
3020 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3023 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3025 return (struct isl_set
*)
3026 isl_map_intersect((struct isl_map
*)set1
,
3027 (struct isl_map
*)set2
);
3030 /* map_intersect_internal accepts intersections
3031 * with parameter domains, so we can just call that function.
3033 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3034 __isl_take isl_set
*params
)
3036 return map_intersect_internal(map
, params
);
3039 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3040 __isl_take isl_map
*map2
)
3042 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3045 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3046 __isl_take isl_set
*params
)
3048 return isl_map_intersect_params(set
, params
);
3051 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3054 struct isl_basic_set
*bset
;
3059 bmap
= isl_basic_map_cow(bmap
);
3062 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3063 in
= isl_basic_map_n_in(bmap
);
3064 bset
= isl_basic_set_from_basic_map(bmap
);
3065 bset
= isl_basic_set_swap_vars(bset
, in
);
3066 return isl_basic_map_from_basic_set(bset
, dim
);
3069 static __isl_give isl_basic_map
*basic_map_space_reset(
3070 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3076 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3079 space
= isl_basic_map_get_space(bmap
);
3080 space
= isl_space_reset(space
, type
);
3081 bmap
= isl_basic_map_reset_space(bmap
, space
);
3085 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3086 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3087 unsigned pos
, unsigned n
)
3090 struct isl_basic_map
*res
;
3091 struct isl_dim_map
*dim_map
;
3092 unsigned total
, off
;
3093 enum isl_dim_type t
;
3096 return basic_map_space_reset(bmap
, type
);
3101 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3103 total
= isl_basic_map_total_dim(bmap
) + n
;
3104 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3106 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3108 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3110 unsigned size
= isl_basic_map_dim(bmap
, t
);
3111 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3113 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3114 pos
, size
- pos
, off
+ pos
+ n
);
3116 off
+= isl_space_dim(res_dim
, t
);
3118 isl_dim_map_div(dim_map
, bmap
, off
);
3120 res
= isl_basic_map_alloc_space(res_dim
,
3121 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3122 if (isl_basic_map_is_rational(bmap
))
3123 res
= isl_basic_map_set_rational(res
);
3124 if (isl_basic_map_plain_is_empty(bmap
)) {
3125 isl_basic_map_free(bmap
);
3127 return isl_basic_map_set_to_empty(res
);
3129 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3130 return isl_basic_map_finalize(res
);
3133 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3134 __isl_take isl_basic_set
*bset
,
3135 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3137 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3140 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3141 enum isl_dim_type type
, unsigned n
)
3145 return isl_basic_map_insert_dims(bmap
, type
,
3146 isl_basic_map_dim(bmap
, type
), n
);
3149 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3150 enum isl_dim_type type
, unsigned n
)
3154 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3155 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3157 isl_basic_set_free(bset
);
3161 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3162 enum isl_dim_type type
)
3166 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3169 space
= isl_map_get_space(map
);
3170 space
= isl_space_reset(space
, type
);
3171 map
= isl_map_reset_space(map
, space
);
3175 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3176 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3181 return map_space_reset(map
, type
);
3183 map
= isl_map_cow(map
);
3187 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3191 for (i
= 0; i
< map
->n
; ++i
) {
3192 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3203 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3204 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3206 return isl_map_insert_dims(set
, type
, pos
, n
);
3209 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3210 enum isl_dim_type type
, unsigned n
)
3214 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3217 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3218 enum isl_dim_type type
, unsigned n
)
3222 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3223 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3229 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3230 __isl_take isl_basic_map
*bmap
,
3231 enum isl_dim_type dst_type
, unsigned dst_pos
,
3232 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3234 struct isl_dim_map
*dim_map
;
3235 struct isl_basic_map
*res
;
3236 enum isl_dim_type t
;
3237 unsigned total
, off
;
3244 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3247 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3250 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3252 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3253 pos(bmap
->dim
, src_type
) + src_pos
+
3254 ((src_type
< dst_type
) ? n
: 0)) {
3255 bmap
= isl_basic_map_cow(bmap
);
3259 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3260 src_type
, src_pos
, n
);
3264 bmap
= isl_basic_map_finalize(bmap
);
3269 total
= isl_basic_map_total_dim(bmap
);
3270 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3273 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3274 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3275 if (t
== dst_type
) {
3276 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3279 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3282 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3283 dst_pos
, size
- dst_pos
, off
);
3284 off
+= size
- dst_pos
;
3285 } else if (t
== src_type
) {
3286 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3289 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3290 src_pos
+ n
, size
- src_pos
- n
, off
);
3291 off
+= size
- src_pos
- n
;
3293 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3297 isl_dim_map_div(dim_map
, bmap
, off
);
3299 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3300 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3301 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3305 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3306 src_type
, src_pos
, n
);
3310 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3311 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3312 bmap
= isl_basic_map_finalize(bmap
);
3316 isl_basic_map_free(bmap
);
3320 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3321 enum isl_dim_type dst_type
, unsigned dst_pos
,
3322 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3324 return (isl_basic_set
*)isl_basic_map_move_dims(
3325 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3328 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3329 enum isl_dim_type dst_type
, unsigned dst_pos
,
3330 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3334 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3335 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3336 src_type
, src_pos
, n
);
3342 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3343 enum isl_dim_type dst_type
, unsigned dst_pos
,
3344 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3353 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3356 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3359 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3361 map
= isl_map_cow(map
);
3365 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3369 for (i
= 0; i
< map
->n
; ++i
) {
3370 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3372 src_type
, src_pos
, n
);
3383 /* Move the specified dimensions to the last columns right before
3384 * the divs. Don't change the dimension specification of bmap.
3385 * That's the responsibility of the caller.
3387 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3388 enum isl_dim_type type
, unsigned first
, unsigned n
)
3390 struct isl_dim_map
*dim_map
;
3391 struct isl_basic_map
*res
;
3392 enum isl_dim_type t
;
3393 unsigned total
, off
;
3397 if (pos(bmap
->dim
, type
) + first
+ n
==
3398 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3401 total
= isl_basic_map_total_dim(bmap
);
3402 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3405 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3406 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3408 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3411 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3412 first
, n
, total
- bmap
->n_div
- n
);
3413 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3414 first
+ n
, size
- (first
+ n
), off
);
3415 off
+= size
- (first
+ n
);
3417 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3421 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3423 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3424 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3425 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3429 /* Insert "n" rows in the divs of "bmap".
3431 * The number of columns is not changed, which means that the last
3432 * dimensions of "bmap" are being reintepreted as the new divs.
3433 * The space of "bmap" is not adjusted, however, which means
3434 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3435 * from the space of "bmap" is the responsibility of the caller.
3437 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3445 bmap
= isl_basic_map_cow(bmap
);
3449 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3450 old
= bmap
->block2
.data
;
3451 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3452 (bmap
->extra
+ n
) * (1 + row_size
));
3453 if (!bmap
->block2
.data
)
3454 return isl_basic_map_free(bmap
);
3455 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3457 return isl_basic_map_free(bmap
);
3458 for (i
= 0; i
< n
; ++i
) {
3459 new_div
[i
] = bmap
->block2
.data
+
3460 (bmap
->extra
+ i
) * (1 + row_size
);
3461 isl_seq_clr(new_div
[i
], 1 + row_size
);
3463 for (i
= 0; i
< bmap
->extra
; ++i
)
3464 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3466 bmap
->div
= new_div
;
3473 /* Turn the n dimensions of type type, starting at first
3474 * into existentially quantified variables.
3476 __isl_give isl_basic_map
*isl_basic_map_project_out(
3477 __isl_take isl_basic_map
*bmap
,
3478 enum isl_dim_type type
, unsigned first
, unsigned n
)
3481 return basic_map_space_reset(bmap
, type
);
3486 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3487 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3489 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3492 bmap
= move_last(bmap
, type
, first
, n
);
3493 bmap
= isl_basic_map_cow(bmap
);
3494 bmap
= insert_div_rows(bmap
, n
);
3498 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3501 bmap
= isl_basic_map_simplify(bmap
);
3502 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3503 return isl_basic_map_finalize(bmap
);
3505 isl_basic_map_free(bmap
);
3509 /* Turn the n dimensions of type type, starting at first
3510 * into existentially quantified variables.
3512 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3513 enum isl_dim_type type
, unsigned first
, unsigned n
)
3515 return (isl_basic_set
*)isl_basic_map_project_out(
3516 (isl_basic_map
*)bset
, type
, first
, n
);
3519 /* Turn the n dimensions of type type, starting at first
3520 * into existentially quantified variables.
3522 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3523 enum isl_dim_type type
, unsigned first
, unsigned n
)
3531 return map_space_reset(map
, type
);
3533 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3535 map
= isl_map_cow(map
);
3539 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3543 for (i
= 0; i
< map
->n
; ++i
) {
3544 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3555 /* Turn the n dimensions of type type, starting at first
3556 * into existentially quantified variables.
3558 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3559 enum isl_dim_type type
, unsigned first
, unsigned n
)
3561 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3564 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3568 for (i
= 0; i
< n
; ++i
) {
3569 j
= isl_basic_map_alloc_div(bmap
);
3572 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3576 isl_basic_map_free(bmap
);
3580 struct isl_basic_map
*isl_basic_map_apply_range(
3581 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3583 isl_space
*dim_result
= NULL
;
3584 struct isl_basic_map
*bmap
;
3585 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3586 struct isl_dim_map
*dim_map1
, *dim_map2
;
3588 if (!bmap1
|| !bmap2
)
3590 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3591 bmap2
->dim
, isl_dim_param
))
3592 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3593 "parameters don't match", goto error
);
3594 if (!isl_space_tuple_match(bmap1
->dim
, isl_dim_out
,
3595 bmap2
->dim
, isl_dim_in
))
3596 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3597 "spaces don't match", goto error
);
3599 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3600 isl_space_copy(bmap2
->dim
));
3602 n_in
= isl_basic_map_n_in(bmap1
);
3603 n_out
= isl_basic_map_n_out(bmap2
);
3604 n
= isl_basic_map_n_out(bmap1
);
3605 nparam
= isl_basic_map_n_param(bmap1
);
3607 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3608 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3609 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3610 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3611 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3612 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3613 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3614 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3615 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3616 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3617 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3619 bmap
= isl_basic_map_alloc_space(dim_result
,
3620 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3621 bmap1
->n_eq
+ bmap2
->n_eq
,
3622 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3623 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3624 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3625 bmap
= add_divs(bmap
, n
);
3626 bmap
= isl_basic_map_simplify(bmap
);
3627 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3628 return isl_basic_map_finalize(bmap
);
3630 isl_basic_map_free(bmap1
);
3631 isl_basic_map_free(bmap2
);
3635 struct isl_basic_set
*isl_basic_set_apply(
3636 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3641 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3644 return (struct isl_basic_set
*)
3645 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3647 isl_basic_set_free(bset
);
3648 isl_basic_map_free(bmap
);
3652 struct isl_basic_map
*isl_basic_map_apply_domain(
3653 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3655 if (!bmap1
|| !bmap2
)
3658 isl_assert(bmap1
->ctx
,
3659 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3660 isl_assert(bmap1
->ctx
,
3661 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3664 bmap1
= isl_basic_map_reverse(bmap1
);
3665 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3666 return isl_basic_map_reverse(bmap1
);
3668 isl_basic_map_free(bmap1
);
3669 isl_basic_map_free(bmap2
);
3673 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3674 * A \cap B -> f(A) + f(B)
3676 struct isl_basic_map
*isl_basic_map_sum(
3677 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3679 unsigned n_in
, n_out
, nparam
, total
, pos
;
3680 struct isl_basic_map
*bmap
= NULL
;
3681 struct isl_dim_map
*dim_map1
, *dim_map2
;
3684 if (!bmap1
|| !bmap2
)
3687 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3690 nparam
= isl_basic_map_n_param(bmap1
);
3691 n_in
= isl_basic_map_n_in(bmap1
);
3692 n_out
= isl_basic_map_n_out(bmap1
);
3694 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3695 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3696 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3697 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3698 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3699 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3700 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3701 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3702 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3703 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3704 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3706 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3707 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3708 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3709 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3710 for (i
= 0; i
< n_out
; ++i
) {
3711 int j
= isl_basic_map_alloc_equality(bmap
);
3714 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3715 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3716 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3717 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3719 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3720 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3721 bmap
= add_divs(bmap
, 2 * n_out
);
3723 bmap
= isl_basic_map_simplify(bmap
);
3724 return isl_basic_map_finalize(bmap
);
3726 isl_basic_map_free(bmap
);
3727 isl_basic_map_free(bmap1
);
3728 isl_basic_map_free(bmap2
);
3732 /* Given two maps A -> f(A) and B -> g(B), construct a map
3733 * A \cap B -> f(A) + f(B)
3735 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3737 struct isl_map
*result
;
3743 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3745 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3746 map1
->n
* map2
->n
, 0);
3749 for (i
= 0; i
< map1
->n
; ++i
)
3750 for (j
= 0; j
< map2
->n
; ++j
) {
3751 struct isl_basic_map
*part
;
3752 part
= isl_basic_map_sum(
3753 isl_basic_map_copy(map1
->p
[i
]),
3754 isl_basic_map_copy(map2
->p
[j
]));
3755 if (isl_basic_map_is_empty(part
))
3756 isl_basic_map_free(part
);
3758 result
= isl_map_add_basic_map(result
, part
);
3771 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3772 __isl_take isl_set
*set2
)
3774 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3777 /* Given a basic map A -> f(A), construct A -> -f(A).
3779 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3784 bmap
= isl_basic_map_cow(bmap
);
3788 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3789 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3790 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3791 for (j
= 0; j
< n
; ++j
)
3792 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3793 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3794 for (j
= 0; j
< n
; ++j
)
3795 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3796 for (i
= 0; i
< bmap
->n_div
; ++i
)
3797 for (j
= 0; j
< n
; ++j
)
3798 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3799 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3800 return isl_basic_map_finalize(bmap
);
3803 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3805 return isl_basic_map_neg(bset
);
3808 /* Given a map A -> f(A), construct A -> -f(A).
3810 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3814 map
= isl_map_cow(map
);
3818 for (i
= 0; i
< map
->n
; ++i
) {
3819 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3830 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3832 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3835 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3836 * A -> floor(f(A)/d).
3838 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3841 unsigned n_in
, n_out
, nparam
, total
, pos
;
3842 struct isl_basic_map
*result
= NULL
;
3843 struct isl_dim_map
*dim_map
;
3849 nparam
= isl_basic_map_n_param(bmap
);
3850 n_in
= isl_basic_map_n_in(bmap
);
3851 n_out
= isl_basic_map_n_out(bmap
);
3853 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3854 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3855 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3856 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3857 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3858 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3860 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3861 bmap
->n_div
+ n_out
,
3862 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3863 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3864 result
= add_divs(result
, n_out
);
3865 for (i
= 0; i
< n_out
; ++i
) {
3867 j
= isl_basic_map_alloc_inequality(result
);
3870 isl_seq_clr(result
->ineq
[j
], 1+total
);
3871 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3872 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3873 j
= isl_basic_map_alloc_inequality(result
);
3876 isl_seq_clr(result
->ineq
[j
], 1+total
);
3877 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3878 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3879 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3882 result
= isl_basic_map_simplify(result
);
3883 return isl_basic_map_finalize(result
);
3885 isl_basic_map_free(result
);
3889 /* Given a map A -> f(A) and an integer d, construct a map
3890 * A -> floor(f(A)/d).
3892 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3896 map
= isl_map_cow(map
);
3900 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3901 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3902 for (i
= 0; i
< map
->n
; ++i
) {
3903 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3914 /* Given a map A -> f(A) and an integer d, construct a map
3915 * A -> floor(f(A)/d).
3917 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3918 __isl_take isl_val
*d
)
3922 if (!isl_val_is_int(d
))
3923 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3924 "expecting integer denominator", goto error
);
3925 map
= isl_map_floordiv(map
, d
->n
);
3934 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3940 i
= isl_basic_map_alloc_equality(bmap
);
3943 nparam
= isl_basic_map_n_param(bmap
);
3944 n_in
= isl_basic_map_n_in(bmap
);
3945 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3946 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3947 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3948 return isl_basic_map_finalize(bmap
);
3950 isl_basic_map_free(bmap
);
3954 /* Add a constraints to "bmap" expressing i_pos < o_pos
3956 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3962 i
= isl_basic_map_alloc_inequality(bmap
);
3965 nparam
= isl_basic_map_n_param(bmap
);
3966 n_in
= isl_basic_map_n_in(bmap
);
3967 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3968 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3969 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3970 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3971 return isl_basic_map_finalize(bmap
);
3973 isl_basic_map_free(bmap
);
3977 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3979 static __isl_give isl_basic_map
*var_less_or_equal(
3980 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3986 i
= isl_basic_map_alloc_inequality(bmap
);
3989 nparam
= isl_basic_map_n_param(bmap
);
3990 n_in
= isl_basic_map_n_in(bmap
);
3991 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3992 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3993 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3994 return isl_basic_map_finalize(bmap
);
3996 isl_basic_map_free(bmap
);
4000 /* Add a constraints to "bmap" expressing i_pos > o_pos
4002 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4008 i
= isl_basic_map_alloc_inequality(bmap
);
4011 nparam
= isl_basic_map_n_param(bmap
);
4012 n_in
= isl_basic_map_n_in(bmap
);
4013 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4014 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4015 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4016 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4017 return isl_basic_map_finalize(bmap
);
4019 isl_basic_map_free(bmap
);
4023 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4025 static __isl_give isl_basic_map
*var_more_or_equal(
4026 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4032 i
= isl_basic_map_alloc_inequality(bmap
);
4035 nparam
= isl_basic_map_n_param(bmap
);
4036 n_in
= isl_basic_map_n_in(bmap
);
4037 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4038 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4039 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4040 return isl_basic_map_finalize(bmap
);
4042 isl_basic_map_free(bmap
);
4046 __isl_give isl_basic_map
*isl_basic_map_equal(
4047 __isl_take isl_space
*dim
, unsigned n_equal
)
4050 struct isl_basic_map
*bmap
;
4051 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4054 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4055 bmap
= var_equal(bmap
, i
);
4056 return isl_basic_map_finalize(bmap
);
4059 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4061 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4065 struct isl_basic_map
*bmap
;
4066 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4069 for (i
= 0; i
< pos
&& bmap
; ++i
)
4070 bmap
= var_equal(bmap
, i
);
4072 bmap
= var_less(bmap
, pos
);
4073 return isl_basic_map_finalize(bmap
);
4076 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4078 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4079 __isl_take isl_space
*dim
, unsigned pos
)
4082 isl_basic_map
*bmap
;
4084 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4085 for (i
= 0; i
< pos
; ++i
)
4086 bmap
= var_equal(bmap
, i
);
4087 bmap
= var_less_or_equal(bmap
, pos
);
4088 return isl_basic_map_finalize(bmap
);
4091 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4093 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4097 struct isl_basic_map
*bmap
;
4098 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4101 for (i
= 0; i
< pos
&& bmap
; ++i
)
4102 bmap
= var_equal(bmap
, i
);
4104 bmap
= var_more(bmap
, pos
);
4105 return isl_basic_map_finalize(bmap
);
4108 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4110 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4111 __isl_take isl_space
*dim
, unsigned pos
)
4114 isl_basic_map
*bmap
;
4116 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4117 for (i
= 0; i
< pos
; ++i
)
4118 bmap
= var_equal(bmap
, i
);
4119 bmap
= var_more_or_equal(bmap
, pos
);
4120 return isl_basic_map_finalize(bmap
);
4123 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4124 unsigned n
, int equal
)
4126 struct isl_map
*map
;
4129 if (n
== 0 && equal
)
4130 return isl_map_universe(dims
);
4132 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4134 for (i
= 0; i
+ 1 < n
; ++i
)
4135 map
= isl_map_add_basic_map(map
,
4136 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4139 map
= isl_map_add_basic_map(map
,
4140 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4142 map
= isl_map_add_basic_map(map
,
4143 isl_basic_map_less_at(dims
, n
- 1));
4145 isl_space_free(dims
);
4150 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4154 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4157 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4159 return map_lex_lte_first(dim
, n
, 0);
4162 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4164 return map_lex_lte_first(dim
, n
, 1);
4167 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4169 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4172 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4174 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4177 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4178 unsigned n
, int equal
)
4180 struct isl_map
*map
;
4183 if (n
== 0 && equal
)
4184 return isl_map_universe(dims
);
4186 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4188 for (i
= 0; i
+ 1 < n
; ++i
)
4189 map
= isl_map_add_basic_map(map
,
4190 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4193 map
= isl_map_add_basic_map(map
,
4194 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4196 map
= isl_map_add_basic_map(map
,
4197 isl_basic_map_more_at(dims
, n
- 1));
4199 isl_space_free(dims
);
4204 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4208 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4211 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4213 return map_lex_gte_first(dim
, n
, 0);
4216 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4218 return map_lex_gte_first(dim
, n
, 1);
4221 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4223 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4226 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4228 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4231 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4232 __isl_take isl_set
*set2
)
4235 map
= isl_map_lex_le(isl_set_get_space(set1
));
4236 map
= isl_map_intersect_domain(map
, set1
);
4237 map
= isl_map_intersect_range(map
, set2
);
4241 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4242 __isl_take isl_set
*set2
)
4245 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4246 map
= isl_map_intersect_domain(map
, set1
);
4247 map
= isl_map_intersect_range(map
, set2
);
4251 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4252 __isl_take isl_set
*set2
)
4255 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4256 map
= isl_map_intersect_domain(map
, set1
);
4257 map
= isl_map_intersect_range(map
, set2
);
4261 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4262 __isl_take isl_set
*set2
)
4265 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4266 map
= isl_map_intersect_domain(map
, set1
);
4267 map
= isl_map_intersect_range(map
, set2
);
4271 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4272 __isl_take isl_map
*map2
)
4275 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4276 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4277 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4281 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4282 __isl_take isl_map
*map2
)
4285 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4286 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4287 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4291 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4292 __isl_take isl_map
*map2
)
4295 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4296 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4297 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4301 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4302 __isl_take isl_map
*map2
)
4305 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4306 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4307 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4311 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4312 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4314 struct isl_basic_map
*bmap
;
4316 bset
= isl_basic_set_cow(bset
);
4320 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4321 isl_space_free(bset
->dim
);
4322 bmap
= (struct isl_basic_map
*) bset
;
4324 return isl_basic_map_finalize(bmap
);
4326 isl_basic_set_free(bset
);
4327 isl_space_free(dim
);
4331 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4335 if (bmap
->dim
->n_in
== 0)
4336 return (struct isl_basic_set
*)bmap
;
4337 bmap
= isl_basic_map_cow(bmap
);
4340 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4343 bmap
= isl_basic_map_finalize(bmap
);
4344 return (struct isl_basic_set
*)bmap
;
4346 isl_basic_map_free(bmap
);
4350 /* For a div d = floor(f/m), add the constraint
4354 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4355 unsigned pos
, isl_int
*div
)
4358 unsigned total
= isl_basic_map_total_dim(bmap
);
4360 i
= isl_basic_map_alloc_inequality(bmap
);
4363 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4364 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4369 /* For a div d = floor(f/m), add the constraint
4371 * -(f-(n-1)) + m d >= 0
4373 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4374 unsigned pos
, isl_int
*div
)
4377 unsigned total
= isl_basic_map_total_dim(bmap
);
4379 i
= isl_basic_map_alloc_inequality(bmap
);
4382 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4383 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4384 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4385 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4390 /* For a div d = floor(f/m), add the constraints
4393 * -(f-(n-1)) + m d >= 0
4395 * Note that the second constraint is the negation of
4399 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4400 unsigned pos
, isl_int
*div
)
4402 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4404 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4409 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4410 unsigned pos
, isl_int
*div
)
4412 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4416 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4418 unsigned total
= isl_basic_map_total_dim(bmap
);
4419 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4421 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4425 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4427 * In particular, if this div is of the form d = floor(f/m),
4428 * then add the constraint
4432 * if sign < 0 or the constraint
4434 * -(f-(n-1)) + m d >= 0
4438 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4439 unsigned div
, int sign
)
4447 total
= isl_basic_map_total_dim(bmap
);
4448 div_pos
= total
- bmap
->n_div
+ div
;
4451 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4453 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4456 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4458 return isl_basic_map_add_div_constraints(bset
, div
);
4461 struct isl_basic_set
*isl_basic_map_underlying_set(
4462 struct isl_basic_map
*bmap
)
4466 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4468 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4469 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4470 return (struct isl_basic_set
*)bmap
;
4471 bmap
= isl_basic_map_cow(bmap
);
4474 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4477 bmap
->extra
-= bmap
->n_div
;
4479 bmap
= isl_basic_map_finalize(bmap
);
4480 return (struct isl_basic_set
*)bmap
;
4482 isl_basic_map_free(bmap
);
4486 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4487 __isl_take isl_basic_set
*bset
)
4489 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4492 struct isl_basic_map
*isl_basic_map_overlying_set(
4493 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4495 struct isl_basic_map
*bmap
;
4496 struct isl_ctx
*ctx
;
4503 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4504 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4505 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4507 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4508 isl_basic_map_free(like
);
4509 return (struct isl_basic_map
*)bset
;
4511 bset
= isl_basic_set_cow(bset
);
4514 total
= bset
->dim
->n_out
+ bset
->extra
;
4515 bmap
= (struct isl_basic_map
*)bset
;
4516 isl_space_free(bmap
->dim
);
4517 bmap
->dim
= isl_space_copy(like
->dim
);
4520 bmap
->n_div
= like
->n_div
;
4521 bmap
->extra
+= like
->n_div
;
4525 ltotal
= total
- bmap
->extra
+ like
->extra
;
4528 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4529 bmap
->extra
* (1 + 1 + total
));
4530 if (isl_blk_is_error(bmap
->block2
))
4532 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4536 for (i
= 0; i
< bmap
->extra
; ++i
)
4537 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4538 for (i
= 0; i
< like
->n_div
; ++i
) {
4539 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4540 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4542 bmap
= isl_basic_map_extend_constraints(bmap
,
4543 0, 2 * like
->n_div
);
4544 for (i
= 0; i
< like
->n_div
; ++i
) {
4547 if (isl_int_is_zero(bmap
->div
[i
][0]))
4549 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4550 bmap
= isl_basic_map_free(bmap
);
4553 isl_basic_map_free(like
);
4554 bmap
= isl_basic_map_simplify(bmap
);
4555 bmap
= isl_basic_map_finalize(bmap
);
4558 isl_basic_map_free(like
);
4559 isl_basic_set_free(bset
);
4563 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4564 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4566 return (struct isl_basic_set
*)
4567 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4570 struct isl_set
*isl_set_from_underlying_set(
4571 struct isl_set
*set
, struct isl_basic_set
*like
)
4577 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4579 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4580 isl_basic_set_free(like
);
4583 set
= isl_set_cow(set
);
4586 for (i
= 0; i
< set
->n
; ++i
) {
4587 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4588 isl_basic_set_copy(like
));
4592 isl_space_free(set
->dim
);
4593 set
->dim
= isl_space_copy(like
->dim
);
4596 isl_basic_set_free(like
);
4599 isl_basic_set_free(like
);
4604 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4608 map
= isl_map_cow(map
);
4611 map
->dim
= isl_space_cow(map
->dim
);
4615 for (i
= 1; i
< map
->n
; ++i
)
4616 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4618 for (i
= 0; i
< map
->n
; ++i
) {
4619 map
->p
[i
] = (struct isl_basic_map
*)
4620 isl_basic_map_underlying_set(map
->p
[i
]);
4625 map
->dim
= isl_space_underlying(map
->dim
, 0);
4627 isl_space_free(map
->dim
);
4628 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4632 return (struct isl_set
*)map
;
4638 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4640 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4643 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4644 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4646 bmap
= isl_basic_map_cow(bmap
);
4650 isl_space_free(bmap
->dim
);
4653 bmap
= isl_basic_map_finalize(bmap
);
4657 isl_basic_map_free(bmap
);
4658 isl_space_free(dim
);
4662 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4663 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4665 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4669 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4670 __isl_take isl_space
*dim
)
4674 map
= isl_map_cow(map
);
4678 for (i
= 0; i
< map
->n
; ++i
) {
4679 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4680 isl_space_copy(dim
));
4684 isl_space_free(map
->dim
);
4690 isl_space_free(dim
);
4694 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4695 __isl_take isl_space
*dim
)
4697 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4700 /* Compute the parameter domain of the given basic set.
4702 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4707 if (isl_basic_set_is_params(bset
))
4710 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4711 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4712 space
= isl_basic_set_get_space(bset
);
4713 space
= isl_space_params(space
);
4714 bset
= isl_basic_set_reset_space(bset
, space
);
4718 /* Construct a zero-dimensional basic set with the given parameter domain.
4720 __isl_give isl_basic_set
*isl_basic_set_from_params(
4721 __isl_take isl_basic_set
*bset
)
4724 space
= isl_basic_set_get_space(bset
);
4725 space
= isl_space_set_from_params(space
);
4726 bset
= isl_basic_set_reset_space(bset
, space
);
4730 /* Compute the parameter domain of the given set.
4732 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4737 if (isl_set_is_params(set
))
4740 n
= isl_set_dim(set
, isl_dim_set
);
4741 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4742 space
= isl_set_get_space(set
);
4743 space
= isl_space_params(space
);
4744 set
= isl_set_reset_space(set
, space
);
4748 /* Construct a zero-dimensional set with the given parameter domain.
4750 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4753 space
= isl_set_get_space(set
);
4754 space
= isl_space_set_from_params(space
);
4755 set
= isl_set_reset_space(set
, space
);
4759 /* Compute the parameter domain of the given map.
4761 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4766 n
= isl_map_dim(map
, isl_dim_in
);
4767 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4768 n
= isl_map_dim(map
, isl_dim_out
);
4769 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4770 space
= isl_map_get_space(map
);
4771 space
= isl_space_params(space
);
4772 map
= isl_map_reset_space(map
, space
);
4776 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4779 struct isl_basic_set
*domain
;
4785 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4787 n_in
= isl_basic_map_n_in(bmap
);
4788 n_out
= isl_basic_map_n_out(bmap
);
4789 domain
= isl_basic_set_from_basic_map(bmap
);
4790 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4792 domain
= isl_basic_set_reset_space(domain
, dim
);
4797 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4801 return isl_space_may_be_set(bmap
->dim
);
4804 /* Is this basic map actually a set?
4805 * Users should never call this function. Outside of isl,
4806 * the type should indicate whether something is a set or a map.
4808 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4812 return isl_space_is_set(bmap
->dim
);
4815 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4819 if (isl_basic_map_is_set(bmap
))
4821 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4824 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4825 __isl_take isl_basic_map
*bmap
)
4829 isl_basic_map
*domain
;
4830 int nparam
, n_in
, n_out
;
4833 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4834 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4835 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4837 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4838 domain
= isl_basic_map_universe(dim
);
4840 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4841 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4842 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4844 total
= isl_basic_map_total_dim(bmap
);
4846 for (i
= 0; i
< n_in
; ++i
) {
4847 k
= isl_basic_map_alloc_equality(bmap
);
4850 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4851 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4852 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4855 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4856 return isl_basic_map_finalize(bmap
);
4858 isl_basic_map_free(bmap
);
4862 __isl_give isl_basic_map
*isl_basic_map_range_map(
4863 __isl_take isl_basic_map
*bmap
)
4867 isl_basic_map
*range
;
4868 int nparam
, n_in
, n_out
;
4871 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4872 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4873 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4875 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4876 range
= isl_basic_map_universe(dim
);
4878 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4879 bmap
= isl_basic_map_apply_range(bmap
, range
);
4880 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4882 total
= isl_basic_map_total_dim(bmap
);
4884 for (i
= 0; i
< n_out
; ++i
) {
4885 k
= isl_basic_map_alloc_equality(bmap
);
4888 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4889 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4890 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4893 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4894 return isl_basic_map_finalize(bmap
);
4896 isl_basic_map_free(bmap
);
4900 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4904 return isl_space_may_be_set(map
->dim
);
4907 /* Is this map actually a set?
4908 * Users should never call this function. Outside of isl,
4909 * the type should indicate whether something is a set or a map.
4911 int isl_map_is_set(__isl_keep isl_map
*map
)
4915 return isl_space_is_set(map
->dim
);
4918 struct isl_set
*isl_map_range(struct isl_map
*map
)
4921 struct isl_set
*set
;
4925 if (isl_map_is_set(map
))
4926 return (isl_set
*)map
;
4928 map
= isl_map_cow(map
);
4932 set
= (struct isl_set
*) map
;
4933 set
->dim
= isl_space_range(set
->dim
);
4936 for (i
= 0; i
< map
->n
; ++i
) {
4937 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4941 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4942 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4949 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4953 map
= isl_map_cow(map
);
4957 map
->dim
= isl_space_domain_map(map
->dim
);
4960 for (i
= 0; i
< map
->n
; ++i
) {
4961 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4965 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4966 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4973 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4976 isl_space
*range_dim
;
4978 map
= isl_map_cow(map
);
4982 range_dim
= isl_space_range(isl_map_get_space(map
));
4983 range_dim
= isl_space_from_range(range_dim
);
4984 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4985 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4988 for (i
= 0; i
< map
->n
; ++i
) {
4989 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4993 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4994 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5001 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5002 __isl_take isl_space
*dim
)
5005 struct isl_map
*map
= NULL
;
5007 set
= isl_set_cow(set
);
5010 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5011 map
= (struct isl_map
*)set
;
5012 for (i
= 0; i
< set
->n
; ++i
) {
5013 map
->p
[i
] = isl_basic_map_from_basic_set(
5014 set
->p
[i
], isl_space_copy(dim
));
5018 isl_space_free(map
->dim
);
5022 isl_space_free(dim
);
5027 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5028 __isl_take isl_basic_set
*bset
)
5030 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5033 __isl_give isl_basic_map
*isl_basic_map_from_range(
5034 __isl_take isl_basic_set
*bset
)
5037 space
= isl_basic_set_get_space(bset
);
5038 space
= isl_space_from_range(space
);
5039 bset
= isl_basic_set_reset_space(bset
, space
);
5040 return (isl_basic_map
*)bset
;
5043 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
5046 space
= isl_set_get_space(set
);
5047 space
= isl_space_from_range(space
);
5048 set
= isl_set_reset_space(set
, space
);
5049 return (struct isl_map
*)set
;
5052 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5054 return isl_map_reverse(isl_map_from_range(set
));
5057 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5058 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5060 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5063 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5064 __isl_take isl_set
*range
)
5066 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5069 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
5072 struct isl_set
*set
= NULL
;
5076 map
= isl_map_cow(map
);
5079 map
->dim
= isl_space_as_set_space(map
->dim
);
5082 set
= (struct isl_set
*)map
;
5083 for (i
= 0; i
< map
->n
; ++i
) {
5084 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5094 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5097 struct isl_map
*map
;
5102 isl_die(dim
->ctx
, isl_error_internal
,
5103 "negative number of basic maps", goto error
);
5104 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5105 sizeof(struct isl_map
) +
5106 (n
- 1) * sizeof(struct isl_basic_map
*));
5110 map
->ctx
= dim
->ctx
;
5111 isl_ctx_ref(map
->ctx
);
5119 isl_space_free(dim
);
5123 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5124 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5127 struct isl_map
*map
;
5130 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5134 map
= isl_map_alloc_space(dims
, n
, flags
);
5138 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5140 struct isl_basic_map
*bmap
;
5141 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5142 bmap
= isl_basic_map_set_to_empty(bmap
);
5146 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5148 struct isl_basic_set
*bset
;
5149 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5150 bset
= isl_basic_set_set_to_empty(bset
);
5154 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5156 struct isl_basic_map
*bmap
;
5159 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5160 bmap
= isl_basic_map_set_to_empty(bmap
);
5164 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5166 struct isl_basic_map
*bmap
;
5169 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5170 bmap
= isl_basic_map_set_to_empty(bmap
);
5174 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5176 struct isl_basic_set
*bset
;
5179 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5180 bset
= isl_basic_set_set_to_empty(bset
);
5184 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5186 struct isl_basic_map
*bmap
;
5187 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5188 bmap
= isl_basic_map_finalize(bmap
);
5192 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5194 struct isl_basic_set
*bset
;
5195 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5196 bset
= isl_basic_set_finalize(bset
);
5200 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5203 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5204 isl_basic_map
*bmap
;
5206 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5207 for (i
= 0; i
< total
; ++i
) {
5208 int k
= isl_basic_map_alloc_inequality(bmap
);
5211 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5212 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5216 isl_basic_map_free(bmap
);
5220 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5222 return isl_basic_map_nat_universe(dim
);
5225 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5227 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5230 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5232 return isl_map_nat_universe(dim
);
5235 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5236 __isl_keep isl_basic_map
*model
)
5240 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5243 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5247 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5250 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5251 __isl_keep isl_set
*model
)
5255 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5258 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5260 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5263 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5267 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5270 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5274 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5277 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5279 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5282 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5286 return isl_set_empty(isl_space_copy(model
->dim
));
5289 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5291 struct isl_map
*map
;
5294 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5295 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5299 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5301 struct isl_set
*set
;
5304 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5305 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5309 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5313 return isl_set_universe(isl_space_copy(model
->dim
));
5316 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5319 struct isl_map
*dup
;
5323 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5324 for (i
= 0; i
< map
->n
; ++i
)
5325 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5329 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5330 __isl_take isl_basic_map
*bmap
)
5334 if (isl_basic_map_plain_is_empty(bmap
)) {
5335 isl_basic_map_free(bmap
);
5338 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5339 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5340 map
->p
[map
->n
] = bmap
;
5342 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5348 isl_basic_map_free(bmap
);
5352 void *isl_map_free(struct isl_map
*map
)
5362 isl_ctx_deref(map
->ctx
);
5363 for (i
= 0; i
< map
->n
; ++i
)
5364 isl_basic_map_free(map
->p
[i
]);
5365 isl_space_free(map
->dim
);
5371 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5372 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5376 base
= isl_map_cow(base
);
5380 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5383 for (i
= 0; i
< base
->n
; ++i
) {
5384 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5385 isl_space_copy(base
->dim
), 0, 0, 0);
5395 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5396 unsigned nparam
, unsigned dim
)
5398 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5402 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5403 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5407 bmap
= isl_basic_map_cow(bmap
);
5408 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5409 j
= isl_basic_map_alloc_equality(bmap
);
5412 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5413 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5414 isl_int_set_si(bmap
->eq
[j
][0], value
);
5415 bmap
= isl_basic_map_simplify(bmap
);
5416 return isl_basic_map_finalize(bmap
);
5418 isl_basic_map_free(bmap
);
5422 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5423 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5427 bmap
= isl_basic_map_cow(bmap
);
5428 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5429 j
= isl_basic_map_alloc_equality(bmap
);
5432 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5433 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5434 isl_int_set(bmap
->eq
[j
][0], value
);
5435 bmap
= isl_basic_map_simplify(bmap
);
5436 return isl_basic_map_finalize(bmap
);
5438 isl_basic_map_free(bmap
);
5442 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5443 enum isl_dim_type type
, unsigned pos
, int value
)
5447 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5448 return isl_basic_map_fix_pos_si(bmap
,
5449 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5451 isl_basic_map_free(bmap
);
5455 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5456 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5460 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5461 return isl_basic_map_fix_pos(bmap
,
5462 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5464 isl_basic_map_free(bmap
);
5468 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5469 * to be equal to "v".
5471 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5472 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5476 if (!isl_val_is_int(v
))
5477 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5478 "expecting integer value", goto error
);
5479 if (pos
>= isl_basic_map_dim(bmap
, type
))
5480 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5481 "index out of bounds", goto error
);
5482 pos
+= isl_basic_map_offset(bmap
, type
);
5483 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5487 isl_basic_map_free(bmap
);
5492 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5493 * to be equal to "v".
5495 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5496 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5498 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5501 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5502 enum isl_dim_type type
, unsigned pos
, int value
)
5504 return (struct isl_basic_set
*)
5505 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5509 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5510 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5512 return (struct isl_basic_set
*)
5513 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5517 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5518 unsigned input
, int value
)
5520 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5523 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5524 unsigned dim
, int value
)
5526 return (struct isl_basic_set
*)
5527 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5528 isl_dim_set
, dim
, value
);
5531 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5533 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5540 isl_basic_map_free(map
->p
[i
]);
5541 if (i
!= map
->n
- 1) {
5542 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5543 map
->p
[i
] = map
->p
[map
->n
- 1];
5550 /* Perform "fn" on each basic map of "map", where we may not be holding
5551 * the only reference to "map".
5552 * In particular, "fn" should be a semantics preserving operation
5553 * that we want to apply to all copies of "map". We therefore need
5554 * to be careful not to modify "map" in a way that breaks "map"
5555 * in case anything goes wrong.
5557 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5558 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5560 struct isl_basic_map
*bmap
;
5566 for (i
= map
->n
- 1; i
>= 0; --i
) {
5567 bmap
= isl_basic_map_copy(map
->p
[i
]);
5571 isl_basic_map_free(map
->p
[i
]);
5573 if (remove_if_empty(map
, i
) < 0)
5583 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5584 enum isl_dim_type type
, unsigned pos
, int value
)
5588 map
= isl_map_cow(map
);
5592 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5593 for (i
= map
->n
- 1; i
>= 0; --i
) {
5594 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5595 if (remove_if_empty(map
, i
) < 0)
5598 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5605 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5606 enum isl_dim_type type
, unsigned pos
, int value
)
5608 return (struct isl_set
*)
5609 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5612 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5613 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5617 map
= isl_map_cow(map
);
5621 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5622 for (i
= 0; i
< map
->n
; ++i
) {
5623 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5627 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5634 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5635 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5637 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5640 /* Fix the value of the variable at position "pos" of type "type" of "map"
5641 * to be equal to "v".
5643 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5644 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5648 map
= isl_map_cow(map
);
5652 if (!isl_val_is_int(v
))
5653 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5654 "expecting integer value", goto error
);
5655 if (pos
>= isl_map_dim(map
, type
))
5656 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5657 "index out of bounds", goto error
);
5658 for (i
= map
->n
- 1; i
>= 0; --i
) {
5659 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5661 if (remove_if_empty(map
, i
) < 0)
5664 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5673 /* Fix the value of the variable at position "pos" of type "type" of "set"
5674 * to be equal to "v".
5676 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5677 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5679 return isl_map_fix_val(set
, type
, pos
, v
);
5682 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5683 unsigned input
, int value
)
5685 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5688 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5690 return (struct isl_set
*)
5691 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5694 static __isl_give isl_basic_map
*basic_map_bound_si(
5695 __isl_take isl_basic_map
*bmap
,
5696 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5702 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5703 pos
+= isl_basic_map_offset(bmap
, type
);
5704 bmap
= isl_basic_map_cow(bmap
);
5705 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5706 j
= isl_basic_map_alloc_inequality(bmap
);
5709 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5711 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5712 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5714 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5715 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5717 bmap
= isl_basic_map_simplify(bmap
);
5718 return isl_basic_map_finalize(bmap
);
5720 isl_basic_map_free(bmap
);
5724 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5725 __isl_take isl_basic_map
*bmap
,
5726 enum isl_dim_type type
, unsigned pos
, int value
)
5728 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5731 /* Constrain the values of the given dimension to be no greater than "value".
5733 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5734 __isl_take isl_basic_map
*bmap
,
5735 enum isl_dim_type type
, unsigned pos
, int value
)
5737 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5740 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5741 unsigned dim
, isl_int value
)
5745 bset
= isl_basic_set_cow(bset
);
5746 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5747 j
= isl_basic_set_alloc_inequality(bset
);
5750 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5751 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5752 isl_int_neg(bset
->ineq
[j
][0], value
);
5753 bset
= isl_basic_set_simplify(bset
);
5754 return isl_basic_set_finalize(bset
);
5756 isl_basic_set_free(bset
);
5760 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5761 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5765 map
= isl_map_cow(map
);
5769 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5770 for (i
= 0; i
< map
->n
; ++i
) {
5771 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5772 type
, pos
, value
, upper
);
5776 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5783 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5784 enum isl_dim_type type
, unsigned pos
, int value
)
5786 return map_bound_si(map
, type
, pos
, value
, 0);
5789 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5790 enum isl_dim_type type
, unsigned pos
, int value
)
5792 return map_bound_si(map
, type
, pos
, value
, 1);
5795 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5796 enum isl_dim_type type
, unsigned pos
, int value
)
5798 return (struct isl_set
*)
5799 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5802 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5803 enum isl_dim_type type
, unsigned pos
, int value
)
5805 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5808 /* Bound the given variable of "bmap" from below (or above is "upper"
5809 * is set) to "value".
5811 static __isl_give isl_basic_map
*basic_map_bound(
5812 __isl_take isl_basic_map
*bmap
,
5813 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5819 if (pos
>= isl_basic_map_dim(bmap
, type
))
5820 isl_die(bmap
->ctx
, isl_error_invalid
,
5821 "index out of bounds", goto error
);
5822 pos
+= isl_basic_map_offset(bmap
, type
);
5823 bmap
= isl_basic_map_cow(bmap
);
5824 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5825 j
= isl_basic_map_alloc_inequality(bmap
);
5828 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5830 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5831 isl_int_set(bmap
->ineq
[j
][0], value
);
5833 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5834 isl_int_neg(bmap
->ineq
[j
][0], value
);
5836 bmap
= isl_basic_map_simplify(bmap
);
5837 return isl_basic_map_finalize(bmap
);
5839 isl_basic_map_free(bmap
);
5843 /* Bound the given variable of "map" from below (or above is "upper"
5844 * is set) to "value".
5846 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5847 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5851 map
= isl_map_cow(map
);
5855 if (pos
>= isl_map_dim(map
, type
))
5856 isl_die(map
->ctx
, isl_error_invalid
,
5857 "index out of bounds", goto error
);
5858 for (i
= map
->n
- 1; i
>= 0; --i
) {
5859 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5860 if (remove_if_empty(map
, i
) < 0)
5863 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5870 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5871 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5873 return map_bound(map
, type
, pos
, value
, 0);
5876 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5877 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5879 return map_bound(map
, type
, pos
, value
, 1);
5882 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5883 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5885 return isl_map_lower_bound(set
, type
, pos
, value
);
5888 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5889 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5891 return isl_map_upper_bound(set
, type
, pos
, value
);
5894 /* Force the values of the variable at position "pos" of type "type" of "set"
5895 * to be no smaller than "value".
5897 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5898 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5902 if (!isl_val_is_int(value
))
5903 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5904 "expecting integer value", goto error
);
5905 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5906 isl_val_free(value
);
5909 isl_val_free(value
);
5914 /* Force the values of the variable at position "pos" of type "type" of "set"
5915 * to be no greater than "value".
5917 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5918 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5922 if (!isl_val_is_int(value
))
5923 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5924 "expecting integer value", goto error
);
5925 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5926 isl_val_free(value
);
5929 isl_val_free(value
);
5934 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5939 set
= isl_set_cow(set
);
5943 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5944 for (i
= 0; i
< set
->n
; ++i
) {
5945 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5955 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5959 map
= isl_map_cow(map
);
5963 map
->dim
= isl_space_reverse(map
->dim
);
5966 for (i
= 0; i
< map
->n
; ++i
) {
5967 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5971 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5978 static struct isl_map
*isl_basic_map_partial_lexopt(
5979 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5980 struct isl_set
**empty
, int max
)
5982 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5985 struct isl_map
*isl_basic_map_partial_lexmax(
5986 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5987 struct isl_set
**empty
)
5989 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5992 struct isl_map
*isl_basic_map_partial_lexmin(
5993 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5994 struct isl_set
**empty
)
5996 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5999 struct isl_set
*isl_basic_set_partial_lexmin(
6000 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6001 struct isl_set
**empty
)
6003 return (struct isl_set
*)
6004 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6008 struct isl_set
*isl_basic_set_partial_lexmax(
6009 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6010 struct isl_set
**empty
)
6012 return (struct isl_set
*)
6013 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6017 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6018 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6019 __isl_give isl_set
**empty
)
6021 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6024 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6025 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6026 __isl_give isl_set
**empty
)
6028 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6031 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6032 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6033 __isl_give isl_set
**empty
)
6035 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6038 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6039 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6040 __isl_give isl_set
**empty
)
6042 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6045 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6046 __isl_take isl_basic_map
*bmap
, int max
)
6048 isl_basic_set
*dom
= NULL
;
6049 isl_space
*dom_space
;
6053 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6054 dom
= isl_basic_set_universe(dom_space
);
6055 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6057 isl_basic_map_free(bmap
);
6061 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6062 __isl_take isl_basic_map
*bmap
)
6064 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6068 #define TYPE isl_pw_multi_aff
6070 #define SUFFIX _pw_multi_aff
6072 #define EMPTY isl_pw_multi_aff_empty
6074 #define ADD isl_pw_multi_aff_union_add
6075 #include "isl_map_lexopt_templ.c"
6077 /* Given a map "map", compute the lexicographically minimal
6078 * (or maximal) image element for each domain element in dom,
6079 * in the form of an isl_pw_multi_aff.
6080 * Set *empty to those elements in dom that do not have an image element.
6082 * We first compute the lexicographically minimal or maximal element
6083 * in the first basic map. This results in a partial solution "res"
6084 * and a subset "todo" of dom that still need to be handled.
6085 * We then consider each of the remaining maps in "map" and successively
6086 * update both "res" and "todo".
6088 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6089 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6090 __isl_give isl_set
**empty
, int max
)
6093 isl_pw_multi_aff
*res
;
6099 if (isl_map_plain_is_empty(map
)) {
6104 return isl_pw_multi_aff_from_map(map
);
6107 res
= basic_map_partial_lexopt_pw_multi_aff(
6108 isl_basic_map_copy(map
->p
[0]),
6109 isl_set_copy(dom
), &todo
, max
);
6111 for (i
= 1; i
< map
->n
; ++i
) {
6112 isl_pw_multi_aff
*res_i
;
6115 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6116 isl_basic_map_copy(map
->p
[i
]),
6117 isl_set_copy(dom
), &todo_i
, max
);
6120 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6122 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6124 todo
= isl_set_intersect(todo
, todo_i
);
6145 #define TYPE isl_map
6149 #define EMPTY isl_map_empty
6151 #define ADD isl_map_union_disjoint
6152 #include "isl_map_lexopt_templ.c"
6154 /* Given a map "map", compute the lexicographically minimal
6155 * (or maximal) image element for each domain element in dom.
6156 * Set *empty to those elements in dom that do not have an image element.
6158 * We first compute the lexicographically minimal or maximal element
6159 * in the first basic map. This results in a partial solution "res"
6160 * and a subset "todo" of dom that still need to be handled.
6161 * We then consider each of the remaining maps in "map" and successively
6162 * update both "res" and "todo".
6164 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6165 * Assume we are computing the lexicographical maximum.
6166 * We first compute the lexicographically maximal element in basic map i.
6167 * This results in a partial solution res_i and a subset todo_i.
6168 * Then we combine these results with those obtain for the first k basic maps
6169 * to obtain a result that is valid for the first k+1 basic maps.
6170 * In particular, the set where there is no solution is the set where
6171 * there is no solution for the first k basic maps and also no solution
6172 * for the ith basic map, i.e.,
6174 * todo^i = todo^k * todo_i
6176 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6177 * solutions, arbitrarily breaking ties in favor of res^k.
6178 * That is, when res^k(a) >= res_i(a), we pick res^k and
6179 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6180 * the lexicographic order.)
6181 * In practice, we compute
6183 * res^k * (res_i . "<=")
6187 * res_i * (res^k . "<")
6189 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6190 * where only one of res^k and res_i provides a solution and we simply pick
6197 * Note that we only compute these intersections when dom(res^k) intersects
6198 * dom(res_i). Otherwise, the only effect of these intersections is to
6199 * potentially break up res^k and res_i into smaller pieces.
6200 * We want to avoid such splintering as much as possible.
6201 * In fact, an earlier implementation of this function would look for
6202 * better results in the domain of res^k and for extra results in todo^k,
6203 * but this would always result in a splintering according to todo^k,
6204 * even when the domain of basic map i is disjoint from the domains of
6205 * the previous basic maps.
6207 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6208 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6209 __isl_give isl_set
**empty
, int max
)
6212 struct isl_map
*res
;
6213 struct isl_set
*todo
;
6218 if (isl_map_plain_is_empty(map
)) {
6226 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6227 isl_set_copy(dom
), &todo
, max
);
6229 for (i
= 1; i
< map
->n
; ++i
) {
6233 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6235 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6236 isl_set_copy(dom
), &todo_i
, max
);
6239 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6240 le
= isl_map_lex_le(dim
);
6242 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6243 le
= isl_map_lex_ge(dim
);
6245 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6246 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6247 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6248 le
= isl_map_intersect(le
, isl_map_copy(res
));
6250 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6251 res
= isl_map_intersect_domain(res
,
6252 isl_set_copy(todo_i
));
6253 res_i
= isl_map_intersect_domain(res_i
,
6254 isl_set_copy(todo
));
6257 res
= isl_map_union_disjoint(res
, res_i
);
6258 res
= isl_map_union_disjoint(res
, lt
);
6259 res
= isl_map_union_disjoint(res
, le
);
6261 todo
= isl_set_intersect(todo
, todo_i
);
6281 __isl_give isl_map
*isl_map_partial_lexmax(
6282 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6283 __isl_give isl_set
**empty
)
6285 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6288 __isl_give isl_map
*isl_map_partial_lexmin(
6289 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6290 __isl_give isl_set
**empty
)
6292 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6295 __isl_give isl_set
*isl_set_partial_lexmin(
6296 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6297 __isl_give isl_set
**empty
)
6299 return (struct isl_set
*)
6300 isl_map_partial_lexmin((struct isl_map
*)set
,
6304 __isl_give isl_set
*isl_set_partial_lexmax(
6305 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6306 __isl_give isl_set
**empty
)
6308 return (struct isl_set
*)
6309 isl_map_partial_lexmax((struct isl_map
*)set
,
6313 /* Compute the lexicographic minimum (or maximum if "max" is set)
6314 * of "bmap" over its domain.
6316 * Since we are not interested in the part of the domain space where
6317 * there is no solution, we initialize the domain to those constraints
6318 * of "bmap" that only involve the parameters and the input dimensions.
6319 * This relieves the parametric programming engine from detecting those
6320 * inequalities and transferring them to the context. More importantly,
6321 * it ensures that those inequalities are transferred first and not
6322 * intermixed with inequalities that actually split the domain.
6324 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6328 isl_basic_map
*copy
;
6331 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6332 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6333 copy
= isl_basic_map_copy(bmap
);
6334 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6335 isl_dim_div
, 0, n_div
);
6336 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6337 isl_dim_out
, 0, n_out
);
6338 dom
= isl_basic_map_domain(copy
);
6339 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6342 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6344 return isl_basic_map_lexopt(bmap
, 0);
6347 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6349 return isl_basic_map_lexopt(bmap
, 1);
6352 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6354 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6357 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6359 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6362 /* Extract the first and only affine expression from list
6363 * and then add it to *pwaff with the given dom.
6364 * This domain is known to be disjoint from other domains
6365 * because of the way isl_basic_map_foreach_lexmax works.
6367 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6368 __isl_take isl_aff_list
*list
, void *user
)
6370 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6372 isl_pw_aff
**pwaff
= user
;
6373 isl_pw_aff
*pwaff_i
;
6377 if (isl_aff_list_n_aff(list
) != 1)
6378 isl_die(ctx
, isl_error_internal
,
6379 "expecting single element list", goto error
);
6381 aff
= isl_aff_list_get_aff(list
, 0);
6382 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6384 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6386 isl_aff_list_free(list
);
6390 isl_basic_set_free(dom
);
6391 isl_aff_list_free(list
);
6395 /* Given a basic map with one output dimension, compute the minimum or
6396 * maximum of that dimension as an isl_pw_aff.
6398 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6399 * call update_dim_opt on each leaf of the result.
6401 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6404 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6408 dim
= isl_space_from_domain(isl_space_domain(dim
));
6409 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6410 pwaff
= isl_pw_aff_empty(dim
);
6412 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6414 return isl_pw_aff_free(pwaff
);
6419 /* Compute the minimum or maximum of the given output dimension
6420 * as a function of the parameters and the input dimensions,
6421 * but independently of the other output dimensions.
6423 * We first project out the other output dimension and then compute
6424 * the "lexicographic" maximum in each basic map, combining the results
6425 * using isl_pw_aff_union_max.
6427 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6434 n_out
= isl_map_dim(map
, isl_dim_out
);
6435 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6436 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6441 isl_space
*dim
= isl_map_get_space(map
);
6442 dim
= isl_space_domain(isl_space_from_range(dim
));
6444 return isl_pw_aff_empty(dim
);
6447 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6448 for (i
= 1; i
< map
->n
; ++i
) {
6449 isl_pw_aff
*pwaff_i
;
6451 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6452 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6460 /* Compute the maximum of the given output dimension as a function of the
6461 * parameters and input dimensions, but independently of
6462 * the other output dimensions.
6464 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6466 return map_dim_opt(map
, pos
, 1);
6469 /* Compute the minimum or maximum of the given set dimension
6470 * as a function of the parameters,
6471 * but independently of the other set dimensions.
6473 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6476 return map_dim_opt(set
, pos
, max
);
6479 /* Compute the maximum of the given set dimension as a function of the
6480 * parameters, but independently of the other set dimensions.
6482 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6484 return set_dim_opt(set
, pos
, 1);
6487 /* Compute the minimum of the given set dimension as a function of the
6488 * parameters, but independently of the other set dimensions.
6490 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6492 return set_dim_opt(set
, pos
, 0);
6495 /* Apply a preimage specified by "mat" on the parameters of "bset".
6496 * bset is assumed to have only parameters and divs.
6498 static struct isl_basic_set
*basic_set_parameter_preimage(
6499 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6506 bset
->dim
= isl_space_cow(bset
->dim
);
6510 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6512 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6514 bset
->dim
->nparam
= 0;
6515 bset
->dim
->n_out
= nparam
;
6516 bset
= isl_basic_set_preimage(bset
, mat
);
6518 bset
->dim
->nparam
= bset
->dim
->n_out
;
6519 bset
->dim
->n_out
= 0;
6524 isl_basic_set_free(bset
);
6528 /* Apply a preimage specified by "mat" on the parameters of "set".
6529 * set is assumed to have only parameters and divs.
6531 static struct isl_set
*set_parameter_preimage(
6532 struct isl_set
*set
, struct isl_mat
*mat
)
6534 isl_space
*dim
= NULL
;
6540 dim
= isl_space_copy(set
->dim
);
6541 dim
= isl_space_cow(dim
);
6545 nparam
= isl_set_dim(set
, isl_dim_param
);
6547 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6550 dim
->n_out
= nparam
;
6551 isl_set_reset_space(set
, dim
);
6552 set
= isl_set_preimage(set
, mat
);
6555 dim
= isl_space_copy(set
->dim
);
6556 dim
= isl_space_cow(dim
);
6559 dim
->nparam
= dim
->n_out
;
6561 isl_set_reset_space(set
, dim
);
6564 isl_space_free(dim
);
6571 /* Intersect the basic set "bset" with the affine space specified by the
6572 * equalities in "eq".
6574 static struct isl_basic_set
*basic_set_append_equalities(
6575 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6583 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6588 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6589 for (i
= 0; i
< eq
->n_row
; ++i
) {
6590 k
= isl_basic_set_alloc_equality(bset
);
6593 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6594 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6598 bset
= isl_basic_set_gauss(bset
, NULL
);
6599 bset
= isl_basic_set_finalize(bset
);
6604 isl_basic_set_free(bset
);
6608 /* Intersect the set "set" with the affine space specified by the
6609 * equalities in "eq".
6611 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6619 for (i
= 0; i
< set
->n
; ++i
) {
6620 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6633 /* Given a basic set "bset" that only involves parameters and existentially
6634 * quantified variables, return the index of the first equality
6635 * that only involves parameters. If there is no such equality then
6636 * return bset->n_eq.
6638 * This function assumes that isl_basic_set_gauss has been called on "bset".
6640 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6643 unsigned nparam
, n_div
;
6648 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6649 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6651 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6652 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6659 /* Compute an explicit representation for the existentially quantified
6660 * variables in "bset" by computing the "minimal value" of the set
6661 * variables. Since there are no set variables, the computation of
6662 * the minimal value essentially computes an explicit representation
6663 * of the non-empty part(s) of "bset".
6665 * The input only involves parameters and existentially quantified variables.
6666 * All equalities among parameters have been removed.
6668 * Since the existentially quantified variables in the result are in general
6669 * going to be different from those in the input, we first replace
6670 * them by the minimal number of variables based on their equalities.
6671 * This should simplify the parametric integer programming.
6673 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6675 isl_morph
*morph1
, *morph2
;
6681 if (bset
->n_eq
== 0)
6682 return isl_basic_set_lexmin(bset
);
6684 morph1
= isl_basic_set_parameter_compression(bset
);
6685 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6686 bset
= isl_basic_set_lift(bset
);
6687 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6688 bset
= isl_morph_basic_set(morph2
, bset
);
6689 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6690 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6692 set
= isl_basic_set_lexmin(bset
);
6694 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6699 /* Project the given basic set onto its parameter domain, possibly introducing
6700 * new, explicit, existential variables in the constraints.
6701 * The input has parameters and (possibly implicit) existential variables.
6702 * The output has the same parameters, but only
6703 * explicit existentially quantified variables.
6705 * The actual projection is performed by pip, but pip doesn't seem
6706 * to like equalities very much, so we first remove the equalities
6707 * among the parameters by performing a variable compression on
6708 * the parameters. Afterward, an inverse transformation is performed
6709 * and the equalities among the parameters are inserted back in.
6711 * The variable compression on the parameters may uncover additional
6712 * equalities that were only implicit before. We therefore check
6713 * if there are any new parameter equalities in the result and
6714 * if so recurse. The removal of parameter equalities is required
6715 * for the parameter compression performed by base_compute_divs.
6717 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6721 struct isl_mat
*T
, *T2
;
6722 struct isl_set
*set
;
6725 bset
= isl_basic_set_cow(bset
);
6729 if (bset
->n_eq
== 0)
6730 return base_compute_divs(bset
);
6732 bset
= isl_basic_set_gauss(bset
, NULL
);
6735 if (isl_basic_set_plain_is_empty(bset
))
6736 return isl_set_from_basic_set(bset
);
6738 i
= first_parameter_equality(bset
);
6739 if (i
== bset
->n_eq
)
6740 return base_compute_divs(bset
);
6742 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6743 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6745 eq
= isl_mat_cow(eq
);
6746 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6747 if (T
&& T
->n_col
== 0) {
6751 bset
= isl_basic_set_set_to_empty(bset
);
6752 return isl_set_from_basic_set(bset
);
6754 bset
= basic_set_parameter_preimage(bset
, T
);
6756 i
= first_parameter_equality(bset
);
6759 else if (i
== bset
->n_eq
)
6760 set
= base_compute_divs(bset
);
6762 set
= parameter_compute_divs(bset
);
6763 set
= set_parameter_preimage(set
, T2
);
6764 set
= set_append_equalities(set
, eq
);
6768 /* Insert the divs from "ls" before those of "bmap".
6770 * The number of columns is not changed, which means that the last
6771 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6772 * The caller is responsible for removing the same number of dimensions
6773 * from the space of "bmap".
6775 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6776 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6782 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6786 old_n_div
= bmap
->n_div
;
6787 bmap
= insert_div_rows(bmap
, n_div
);
6791 for (i
= 0; i
< n_div
; ++i
) {
6792 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6793 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6799 /* Replace the space of "bmap" by the space and divs of "ls".
6801 * If "ls" has any divs, then we simplify the result since we may
6802 * have discovered some additional equalities that could simplify
6803 * the div expressions.
6805 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6806 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6810 bmap
= isl_basic_map_cow(bmap
);
6814 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6815 bmap
= insert_divs_from_local_space(bmap
, ls
);
6819 isl_space_free(bmap
->dim
);
6820 bmap
->dim
= isl_local_space_get_space(ls
);
6824 isl_local_space_free(ls
);
6826 bmap
= isl_basic_map_simplify(bmap
);
6827 bmap
= isl_basic_map_finalize(bmap
);
6830 isl_basic_map_free(bmap
);
6831 isl_local_space_free(ls
);
6835 /* Replace the space of "map" by the space and divs of "ls".
6837 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6838 __isl_take isl_local_space
*ls
)
6842 map
= isl_map_cow(map
);
6846 for (i
= 0; i
< map
->n
; ++i
) {
6847 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6848 isl_local_space_copy(ls
));
6852 isl_space_free(map
->dim
);
6853 map
->dim
= isl_local_space_get_space(ls
);
6857 isl_local_space_free(ls
);
6860 isl_local_space_free(ls
);
6865 /* Compute an explicit representation for the existentially
6866 * quantified variables for which do not know any explicit representation yet.
6868 * We first sort the existentially quantified variables so that the
6869 * existentially quantified variables for which we already have an explicit
6870 * representation are placed before those for which we do not.
6871 * The input dimensions, the output dimensions and the existentially
6872 * quantified variables for which we already have an explicit
6873 * representation are then turned into parameters.
6874 * compute_divs returns a map with the same parameters and
6875 * no input or output dimensions and the dimension specification
6876 * is reset to that of the input, including the existentially quantified
6877 * variables for which we already had an explicit representation.
6879 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6881 struct isl_basic_set
*bset
;
6882 struct isl_set
*set
;
6883 struct isl_map
*map
;
6885 isl_local_space
*ls
;
6892 bmap
= isl_basic_map_sort_divs(bmap
);
6893 bmap
= isl_basic_map_cow(bmap
);
6897 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6898 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6901 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6902 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6903 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6904 dim
= isl_space_set_alloc(bmap
->ctx
,
6905 nparam
+ n_in
+ n_out
+ n_known
, 0);
6909 ls
= isl_basic_map_get_local_space(bmap
);
6910 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6911 n_known
, bmap
->n_div
- n_known
);
6913 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6914 swap_div(bmap
, i
- n_known
, i
);
6915 bmap
->n_div
-= n_known
;
6916 bmap
->extra
-= n_known
;
6918 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6919 bset
= (struct isl_basic_set
*)bmap
;
6921 set
= parameter_compute_divs(bset
);
6922 map
= (struct isl_map
*)set
;
6923 map
= replace_space_by_local_space(map
, ls
);
6927 isl_basic_map_free(bmap
);
6931 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6939 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6940 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6941 if (isl_int_is_zero(bmap
->div
[i
][0]))
6943 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6949 static int map_divs_known(__isl_keep isl_map
*map
)
6956 for (i
= 0; i
< map
->n
; ++i
) {
6957 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6965 /* If bmap contains any unknown divs, then compute explicit
6966 * expressions for them. However, this computation may be
6967 * quite expensive, so first try to remove divs that aren't
6970 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6973 struct isl_map
*map
;
6975 known
= isl_basic_map_divs_known(bmap
);
6979 return isl_map_from_basic_map(bmap
);
6981 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6983 known
= isl_basic_map_divs_known(bmap
);
6987 return isl_map_from_basic_map(bmap
);
6989 map
= compute_divs(bmap
);
6992 isl_basic_map_free(bmap
);
6996 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7000 struct isl_map
*res
;
7007 known
= map_divs_known(map
);
7015 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7016 for (i
= 1 ; i
< map
->n
; ++i
) {
7018 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7019 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7020 res
= isl_map_union_disjoint(res
, r2
);
7022 res
= isl_map_union(res
, r2
);
7029 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7031 return (struct isl_set
*)
7032 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7035 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7037 return (struct isl_set
*)
7038 isl_map_compute_divs((struct isl_map
*)set
);
7041 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7044 struct isl_set
*set
;
7049 map
= isl_map_cow(map
);
7053 set
= (struct isl_set
*)map
;
7054 set
->dim
= isl_space_domain(set
->dim
);
7057 for (i
= 0; i
< map
->n
; ++i
) {
7058 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7062 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7063 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7070 /* Return the union of "map1" and "map2", where we assume for now that
7071 * "map1" and "map2" are disjoint. Note that the basic maps inside
7072 * "map1" or "map2" may not be disjoint from each other.
7073 * Also note that this function is also called from isl_map_union,
7074 * which takes care of handling the situation where "map1" and "map2"
7075 * may not be disjoint.
7077 * If one of the inputs is empty, we can simply return the other input.
7078 * Similarly, if one of the inputs is universal, then it is equal to the union.
7080 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7081 __isl_take isl_map
*map2
)
7085 struct isl_map
*map
= NULL
;
7100 is_universe
= isl_map_plain_is_universe(map1
);
7101 if (is_universe
< 0)
7108 is_universe
= isl_map_plain_is_universe(map2
);
7109 if (is_universe
< 0)
7116 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
7118 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7119 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7120 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7122 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7123 map1
->n
+ map2
->n
, flags
);
7126 for (i
= 0; i
< map1
->n
; ++i
) {
7127 map
= isl_map_add_basic_map(map
,
7128 isl_basic_map_copy(map1
->p
[i
]));
7132 for (i
= 0; i
< map2
->n
; ++i
) {
7133 map
= isl_map_add_basic_map(map
,
7134 isl_basic_map_copy(map2
->p
[i
]));
7148 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7149 __isl_take isl_map
*map2
)
7151 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7154 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
7156 map1
= isl_map_union_disjoint(map1
, map2
);
7160 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7164 struct isl_set
*isl_set_union_disjoint(
7165 struct isl_set
*set1
, struct isl_set
*set2
)
7167 return (struct isl_set
*)
7168 isl_map_union_disjoint(
7169 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7172 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7174 return (struct isl_set
*)
7175 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7178 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7181 * "map" and "set" are assumed to be compatible and non-NULL.
7183 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7184 __isl_take isl_set
*set
,
7185 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7186 __isl_take isl_basic_set
*bset
))
7189 struct isl_map
*result
;
7192 if (isl_set_plain_is_universe(set
)) {
7197 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7198 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7199 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7201 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7202 map
->n
* set
->n
, flags
);
7203 for (i
= 0; result
&& i
< map
->n
; ++i
)
7204 for (j
= 0; j
< set
->n
; ++j
) {
7205 result
= isl_map_add_basic_map(result
,
7206 fn(isl_basic_map_copy(map
->p
[i
]),
7207 isl_basic_set_copy(set
->p
[j
])));
7217 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7218 __isl_take isl_set
*set
)
7223 if (!isl_map_compatible_range(map
, set
))
7224 isl_die(set
->ctx
, isl_error_invalid
,
7225 "incompatible spaces", goto error
);
7227 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7234 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7235 __isl_take isl_set
*set
)
7237 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7240 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7241 __isl_take isl_set
*set
)
7246 if (!isl_map_compatible_domain(map
, set
))
7247 isl_die(set
->ctx
, isl_error_invalid
,
7248 "incompatible spaces", goto error
);
7250 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7257 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7258 __isl_take isl_set
*set
)
7260 return isl_map_align_params_map_map_and(map
, set
,
7261 &map_intersect_domain
);
7264 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7265 __isl_take isl_map
*map2
)
7269 map1
= isl_map_reverse(map1
);
7270 map1
= isl_map_apply_range(map1
, map2
);
7271 return isl_map_reverse(map1
);
7278 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7279 __isl_take isl_map
*map2
)
7281 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7284 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7285 __isl_take isl_map
*map2
)
7287 isl_space
*dim_result
;
7288 struct isl_map
*result
;
7294 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7295 isl_space_copy(map2
->dim
));
7297 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7300 for (i
= 0; i
< map1
->n
; ++i
)
7301 for (j
= 0; j
< map2
->n
; ++j
) {
7302 result
= isl_map_add_basic_map(result
,
7303 isl_basic_map_apply_range(
7304 isl_basic_map_copy(map1
->p
[i
]),
7305 isl_basic_map_copy(map2
->p
[j
])));
7311 if (result
&& result
->n
<= 1)
7312 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7320 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7321 __isl_take isl_map
*map2
)
7323 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7327 * returns range - domain
7329 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7331 isl_space
*dims
, *target_dim
;
7332 struct isl_basic_set
*bset
;
7339 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7340 bmap
->dim
, isl_dim_out
),
7342 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7343 dim
= isl_basic_map_n_in(bmap
);
7344 nparam
= isl_basic_map_n_param(bmap
);
7345 bset
= isl_basic_set_from_basic_map(bmap
);
7346 bset
= isl_basic_set_cow(bset
);
7347 dims
= isl_basic_set_get_space(bset
);
7348 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7349 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7350 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7351 for (i
= 0; i
< dim
; ++i
) {
7352 int j
= isl_basic_map_alloc_equality(
7353 (struct isl_basic_map
*)bset
);
7355 bset
= isl_basic_set_free(bset
);
7358 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7359 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7360 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7361 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7363 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7364 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7367 isl_basic_map_free(bmap
);
7372 * returns range - domain
7374 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7378 struct isl_set
*result
;
7383 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7384 map
->dim
, isl_dim_out
),
7386 dim
= isl_map_get_space(map
);
7387 dim
= isl_space_domain(dim
);
7388 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7391 for (i
= 0; i
< map
->n
; ++i
)
7392 result
= isl_set_add_basic_set(result
,
7393 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7402 * returns [domain -> range] -> range - domain
7404 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7405 __isl_take isl_basic_map
*bmap
)
7409 isl_basic_map
*domain
;
7413 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7414 isl_die(bmap
->ctx
, isl_error_invalid
,
7415 "domain and range don't match", goto error
);
7417 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7418 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7420 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7421 domain
= isl_basic_map_universe(dim
);
7423 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7424 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7425 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7427 total
= isl_basic_map_total_dim(bmap
);
7429 for (i
= 0; i
< n
; ++i
) {
7430 k
= isl_basic_map_alloc_equality(bmap
);
7433 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7434 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7435 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7436 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7439 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7440 return isl_basic_map_finalize(bmap
);
7442 isl_basic_map_free(bmap
);
7447 * returns [domain -> range] -> range - domain
7449 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7452 isl_space
*domain_dim
;
7457 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7458 isl_die(map
->ctx
, isl_error_invalid
,
7459 "domain and range don't match", goto error
);
7461 map
= isl_map_cow(map
);
7465 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7466 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7467 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7470 for (i
= 0; i
< map
->n
; ++i
) {
7471 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7475 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7482 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7484 struct isl_basic_map
*bmap
;
7492 nparam
= dims
->nparam
;
7494 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7498 for (i
= 0; i
< dim
; ++i
) {
7499 int j
= isl_basic_map_alloc_equality(bmap
);
7502 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7503 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7504 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7506 return isl_basic_map_finalize(bmap
);
7508 isl_basic_map_free(bmap
);
7512 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7516 if (dim
->n_in
!= dim
->n_out
)
7517 isl_die(dim
->ctx
, isl_error_invalid
,
7518 "number of input and output dimensions needs to be "
7519 "the same", goto error
);
7520 return basic_map_identity(dim
);
7522 isl_space_free(dim
);
7526 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7528 if (!model
|| !model
->dim
)
7530 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7533 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7535 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7538 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7540 if (!model
|| !model
->dim
)
7542 return isl_map_identity(isl_space_copy(model
->dim
));
7545 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7547 if (!model
|| !model
->dim
)
7549 return isl_map_identity(isl_space_copy(model
->dim
));
7552 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7554 isl_space
*dim
= isl_set_get_space(set
);
7556 id
= isl_map_identity(isl_space_map_from_set(dim
));
7557 return isl_map_intersect_range(id
, set
);
7560 /* Construct a basic set with all set dimensions having only non-negative
7563 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7564 __isl_take isl_space
*space
)
7569 struct isl_basic_set
*bset
;
7573 nparam
= space
->nparam
;
7575 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7578 for (i
= 0; i
< dim
; ++i
) {
7579 int k
= isl_basic_set_alloc_inequality(bset
);
7582 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7583 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7587 isl_basic_set_free(bset
);
7591 /* Construct the half-space x_pos >= 0.
7593 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7597 isl_basic_set
*nonneg
;
7599 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7600 k
= isl_basic_set_alloc_inequality(nonneg
);
7603 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7604 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7606 return isl_basic_set_finalize(nonneg
);
7608 isl_basic_set_free(nonneg
);
7612 /* Construct the half-space x_pos <= -1.
7614 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7619 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7620 k
= isl_basic_set_alloc_inequality(neg
);
7623 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7624 isl_int_set_si(neg
->ineq
[k
][0], -1);
7625 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7627 return isl_basic_set_finalize(neg
);
7629 isl_basic_set_free(neg
);
7633 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7634 enum isl_dim_type type
, unsigned first
, unsigned n
)
7637 isl_basic_set
*nonneg
;
7645 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7647 for (i
= 0; i
< n
; ++i
) {
7648 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7649 pos(set
->dim
, type
) + first
+ i
);
7650 neg
= neg_halfspace(isl_set_get_space(set
),
7651 pos(set
->dim
, type
) + first
+ i
);
7653 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7662 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7663 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7670 if (isl_set_plain_is_empty(set
)) {
7675 return fn(set
, signs
, user
);
7678 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7680 half
= isl_set_intersect(half
, isl_set_copy(set
));
7681 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7685 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7687 half
= isl_set_intersect(half
, set
);
7688 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7694 /* Call "fn" on the intersections of "set" with each of the orthants
7695 * (except for obviously empty intersections). The orthant is identified
7696 * by the signs array, with each entry having value 1 or -1 according
7697 * to the sign of the corresponding variable.
7699 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7700 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7710 if (isl_set_plain_is_empty(set
))
7713 nparam
= isl_set_dim(set
, isl_dim_param
);
7714 nvar
= isl_set_dim(set
, isl_dim_set
);
7716 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7718 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7726 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7728 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7731 int isl_basic_map_is_subset(
7732 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7735 struct isl_map
*map1
;
7736 struct isl_map
*map2
;
7738 if (!bmap1
|| !bmap2
)
7741 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7742 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7744 is_subset
= isl_map_is_subset(map1
, map2
);
7752 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7753 __isl_keep isl_basic_set
*bset2
)
7755 return isl_basic_map_is_subset(bset1
, bset2
);
7758 int isl_basic_map_is_equal(
7759 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7763 if (!bmap1
|| !bmap2
)
7765 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7768 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7772 int isl_basic_set_is_equal(
7773 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7775 return isl_basic_map_is_equal(
7776 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7779 int isl_map_is_empty(struct isl_map
*map
)
7786 for (i
= 0; i
< map
->n
; ++i
) {
7787 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7796 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7798 return map
? map
->n
== 0 : -1;
7801 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7803 return isl_map_plain_is_empty(map
);
7806 int isl_set_plain_is_empty(struct isl_set
*set
)
7808 return set
? set
->n
== 0 : -1;
7811 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7813 return isl_set_plain_is_empty(set
);
7816 int isl_set_is_empty(struct isl_set
*set
)
7818 return isl_map_is_empty((struct isl_map
*)set
);
7821 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7826 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7829 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7834 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7837 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7843 is_subset
= isl_map_is_subset(map1
, map2
);
7846 is_subset
= isl_map_is_subset(map2
, map1
);
7850 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7852 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7855 int isl_basic_map_is_strict_subset(
7856 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7860 if (!bmap1
|| !bmap2
)
7862 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7865 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7866 if (is_subset
== -1)
7871 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7877 is_subset
= isl_map_is_subset(map1
, map2
);
7880 is_subset
= isl_map_is_subset(map2
, map1
);
7881 if (is_subset
== -1)
7886 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7888 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7891 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7895 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7898 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7902 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7905 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7912 for (i
= 0; i
< map
->n
; ++i
) {
7913 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7921 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7923 return isl_map_plain_is_universe((isl_map
*) set
);
7926 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7928 return isl_set_plain_is_universe(set
);
7931 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7933 struct isl_basic_set
*bset
= NULL
;
7934 struct isl_vec
*sample
= NULL
;
7941 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7944 if (isl_basic_map_is_universe(bmap
))
7947 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7948 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7949 copy
= isl_basic_map_remove_redundancies(copy
);
7950 empty
= isl_basic_map_plain_is_empty(copy
);
7951 isl_basic_map_free(copy
);
7955 total
= 1 + isl_basic_map_total_dim(bmap
);
7956 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7957 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7963 isl_vec_free(bmap
->sample
);
7964 bmap
->sample
= NULL
;
7965 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7968 sample
= isl_basic_set_sample_vec(bset
);
7971 empty
= sample
->size
== 0;
7972 isl_vec_free(bmap
->sample
);
7973 bmap
->sample
= sample
;
7975 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7980 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7984 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7987 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7989 return isl_basic_map_plain_is_empty(bmap
);
7992 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7996 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7999 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8001 return isl_basic_set_plain_is_empty(bset
);
8004 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8006 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8009 struct isl_map
*isl_basic_map_union(
8010 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8012 struct isl_map
*map
;
8013 if (!bmap1
|| !bmap2
)
8016 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8018 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8021 map
= isl_map_add_basic_map(map
, bmap1
);
8022 map
= isl_map_add_basic_map(map
, bmap2
);
8025 isl_basic_map_free(bmap1
);
8026 isl_basic_map_free(bmap2
);
8030 struct isl_set
*isl_basic_set_union(
8031 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8033 return (struct isl_set
*)isl_basic_map_union(
8034 (struct isl_basic_map
*)bset1
,
8035 (struct isl_basic_map
*)bset2
);
8038 /* Order divs such that any div only depends on previous divs */
8039 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8047 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8049 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8051 if (isl_int_is_zero(bmap
->div
[i
][0]))
8053 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8057 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8063 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8065 return (struct isl_basic_set
*)
8066 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8069 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8076 for (i
= 0; i
< map
->n
; ++i
) {
8077 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8088 /* Apply the expansion computed by isl_merge_divs.
8089 * The expansion itself is given by "exp" while the resulting
8090 * list of divs is given by "div".
8092 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8093 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8098 bset
= isl_basic_set_cow(bset
);
8102 if (div
->n_row
< bset
->n_div
)
8103 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8104 "not an expansion", goto error
);
8106 n_div
= bset
->n_div
;
8107 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8108 div
->n_row
- n_div
, 0,
8109 2 * (div
->n_row
- n_div
));
8111 for (i
= n_div
; i
< div
->n_row
; ++i
)
8112 if (isl_basic_set_alloc_div(bset
) < 0)
8116 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8117 if (j
>= 0 && exp
[j
] == i
) {
8119 isl_basic_map_swap_div(bset
, i
, j
);
8122 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8123 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8131 isl_basic_set_free(bset
);
8136 /* Look for a div in dst that corresponds to the div "div" in src.
8137 * The divs before "div" in src and dst are assumed to be the same.
8139 * Returns -1 if no corresponding div was found and the position
8140 * of the corresponding div in dst otherwise.
8142 static int find_div(struct isl_basic_map
*dst
,
8143 struct isl_basic_map
*src
, unsigned div
)
8147 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8149 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8150 for (i
= div
; i
< dst
->n_div
; ++i
)
8151 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8152 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8153 dst
->n_div
- div
) == -1)
8158 struct isl_basic_map
*isl_basic_map_align_divs(
8159 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
8167 if (src
->n_div
== 0)
8170 for (i
= 0; i
< src
->n_div
; ++i
)
8171 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
8173 src
= isl_basic_map_order_divs(src
);
8174 dst
= isl_basic_map_cow(dst
);
8177 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
8178 src
->n_div
, 0, 2 * src
->n_div
);
8181 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8182 for (i
= 0; i
< src
->n_div
; ++i
) {
8183 int j
= find_div(dst
, src
, i
);
8185 j
= isl_basic_map_alloc_div(dst
);
8188 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8189 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8190 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8194 isl_basic_map_swap_div(dst
, i
, j
);
8198 isl_basic_map_free(dst
);
8202 struct isl_basic_set
*isl_basic_set_align_divs(
8203 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8205 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8206 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8209 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8217 map
= isl_map_compute_divs(map
);
8218 map
= isl_map_cow(map
);
8222 for (i
= 1; i
< map
->n
; ++i
)
8223 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8224 for (i
= 1; i
< map
->n
; ++i
) {
8225 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8227 return isl_map_free(map
);
8230 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8234 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8236 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8239 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8240 __isl_take isl_map
*map
)
8244 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8245 map
= isl_map_intersect_domain(map
, set
);
8246 set
= isl_map_range(map
);
8254 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8255 __isl_take isl_map
*map
)
8257 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8260 /* There is no need to cow as removing empty parts doesn't change
8261 * the meaning of the set.
8263 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8270 for (i
= map
->n
- 1; i
>= 0; --i
)
8271 remove_if_empty(map
, i
);
8276 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8278 return (struct isl_set
*)
8279 isl_map_remove_empty_parts((struct isl_map
*)set
);
8282 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8284 struct isl_basic_map
*bmap
;
8285 if (!map
|| map
->n
== 0)
8287 bmap
= map
->p
[map
->n
-1];
8288 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8289 return isl_basic_map_copy(bmap
);
8292 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8294 return (struct isl_basic_set
*)
8295 isl_map_copy_basic_map((struct isl_map
*)set
);
8298 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8299 __isl_keep isl_basic_map
*bmap
)
8305 for (i
= map
->n
-1; i
>= 0; --i
) {
8306 if (map
->p
[i
] != bmap
)
8308 map
= isl_map_cow(map
);
8311 isl_basic_map_free(map
->p
[i
]);
8312 if (i
!= map
->n
-1) {
8313 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8314 map
->p
[i
] = map
->p
[map
->n
-1];
8325 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8326 struct isl_basic_set
*bset
)
8328 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8329 (struct isl_basic_map
*)bset
);
8332 /* Given two basic sets bset1 and bset2, compute the maximal difference
8333 * between the values of dimension pos in bset1 and those in bset2
8334 * for any common value of the parameters and dimensions preceding pos.
8336 static enum isl_lp_result
basic_set_maximal_difference_at(
8337 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8338 int pos
, isl_int
*opt
)
8341 struct isl_basic_map
*bmap1
= NULL
;
8342 struct isl_basic_map
*bmap2
= NULL
;
8343 struct isl_ctx
*ctx
;
8344 struct isl_vec
*obj
;
8347 unsigned dim1
, dim2
;
8348 enum isl_lp_result res
;
8350 if (!bset1
|| !bset2
)
8351 return isl_lp_error
;
8353 nparam
= isl_basic_set_n_param(bset1
);
8354 dim1
= isl_basic_set_n_dim(bset1
);
8355 dim2
= isl_basic_set_n_dim(bset2
);
8356 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8357 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8358 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8359 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8360 if (!bmap1
|| !bmap2
)
8362 bmap1
= isl_basic_map_cow(bmap1
);
8363 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8364 pos
, (dim1
- pos
) + (dim2
- pos
),
8365 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8366 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8369 total
= isl_basic_map_total_dim(bmap1
);
8371 obj
= isl_vec_alloc(ctx
, 1 + total
);
8374 isl_seq_clr(obj
->block
.data
, 1 + total
);
8375 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8376 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8377 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8379 isl_basic_map_free(bmap1
);
8383 isl_basic_map_free(bmap2
);
8385 isl_basic_map_free(bmap1
);
8386 return isl_lp_error
;
8389 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8390 * for any common value of the parameters and dimensions preceding pos
8391 * in both basic sets, the values of dimension pos in bset1 are
8392 * smaller or larger than those in bset2.
8395 * 1 if bset1 follows bset2
8396 * -1 if bset1 precedes bset2
8397 * 0 if bset1 and bset2 are incomparable
8398 * -2 if some error occurred.
8400 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8401 struct isl_basic_set
*bset2
, int pos
)
8404 enum isl_lp_result res
;
8409 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8411 if (res
== isl_lp_empty
)
8413 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8414 res
== isl_lp_unbounded
)
8416 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8425 /* Given two basic sets bset1 and bset2, check whether
8426 * for any common value of the parameters and dimensions preceding pos
8427 * there is a value of dimension pos in bset1 that is larger
8428 * than a value of the same dimension in bset2.
8431 * 1 if there exists such a pair
8432 * 0 if there is no such pair, but there is a pair of equal values
8434 * -2 if some error occurred.
8436 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8437 __isl_keep isl_basic_set
*bset2
, int pos
)
8440 enum isl_lp_result res
;
8445 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8447 if (res
== isl_lp_empty
)
8449 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8450 res
== isl_lp_unbounded
)
8452 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8454 else if (res
== isl_lp_ok
)
8463 /* Given two sets set1 and set2, check whether
8464 * for any common value of the parameters and dimensions preceding pos
8465 * there is a value of dimension pos in set1 that is larger
8466 * than a value of the same dimension in set2.
8469 * 1 if there exists such a pair
8470 * 0 if there is no such pair, but there is a pair of equal values
8472 * -2 if some error occurred.
8474 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8475 __isl_keep isl_set
*set2
, int pos
)
8483 for (i
= 0; i
< set1
->n
; ++i
)
8484 for (j
= 0; j
< set2
->n
; ++j
) {
8486 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8487 if (f
== 1 || f
== -2)
8496 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8497 unsigned pos
, isl_int
*val
)
8505 total
= isl_basic_map_total_dim(bmap
);
8506 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8507 for (; d
+1 > pos
; --d
)
8508 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8512 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8514 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8516 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8519 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8525 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8526 unsigned pos
, isl_int
*val
)
8538 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8541 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8542 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8543 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8544 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8548 isl_int_set(*val
, v
);
8554 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8555 unsigned pos
, isl_int
*val
)
8557 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8561 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8564 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8567 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8568 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8570 if (pos
>= isl_basic_map_dim(bmap
, type
))
8572 return isl_basic_map_plain_has_fixed_var(bmap
,
8573 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8576 /* If "bmap" obviously lies on a hyperplane where the given dimension
8577 * has a fixed value, then return that value.
8578 * Otherwise return NaN.
8580 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8581 __isl_keep isl_basic_map
*bmap
,
8582 enum isl_dim_type type
, unsigned pos
)
8590 ctx
= isl_basic_map_get_ctx(bmap
);
8591 v
= isl_val_alloc(ctx
);
8594 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8596 return isl_val_free(v
);
8598 isl_int_set_si(v
->d
, 1);
8602 return isl_val_nan(ctx
);
8605 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8606 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8608 if (pos
>= isl_map_dim(map
, type
))
8610 return isl_map_plain_has_fixed_var(map
,
8611 map_offset(map
, type
) - 1 + pos
, val
);
8614 /* If "map" obviously lies on a hyperplane where the given dimension
8615 * has a fixed value, then return that value.
8616 * Otherwise return NaN.
8618 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8619 enum isl_dim_type type
, unsigned pos
)
8627 ctx
= isl_map_get_ctx(map
);
8628 v
= isl_val_alloc(ctx
);
8631 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8633 return isl_val_free(v
);
8635 isl_int_set_si(v
->d
, 1);
8639 return isl_val_nan(ctx
);
8642 /* If "set" obviously lies on a hyperplane where the given dimension
8643 * has a fixed value, then return that value.
8644 * Otherwise return NaN.
8646 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8647 enum isl_dim_type type
, unsigned pos
)
8649 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8652 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8653 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8655 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8658 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8659 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8661 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8664 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8665 * then return this fixed value in *val.
8667 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8668 unsigned dim
, isl_int
*val
)
8670 return isl_basic_set_plain_has_fixed_var(bset
,
8671 isl_basic_set_n_param(bset
) + dim
, val
);
8674 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8675 * then return this fixed value in *val.
8677 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8678 unsigned dim
, isl_int
*val
)
8680 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8683 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8684 unsigned dim
, isl_int
*val
)
8686 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8689 /* Check if input variable in has fixed value and if so and if val is not NULL,
8690 * then return this fixed value in *val.
8692 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8693 unsigned in
, isl_int
*val
)
8695 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8698 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8699 * and if val is not NULL, then return this lower bound in *val.
8701 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8702 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8704 int i
, i_eq
= -1, i_ineq
= -1;
8711 total
= isl_basic_set_total_dim(bset
);
8712 nparam
= isl_basic_set_n_param(bset
);
8713 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8714 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8720 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8721 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8723 if (i_eq
!= -1 || i_ineq
!= -1)
8727 if (i_eq
== -1 && i_ineq
== -1)
8729 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8730 /* The coefficient should always be one due to normalization. */
8731 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8733 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8735 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8736 total
- nparam
- dim
- 1) != -1)
8739 isl_int_neg(*val
, c
[0]);
8743 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8744 unsigned dim
, isl_int
*val
)
8756 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8760 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8762 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8763 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8765 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8769 isl_int_set(*val
, v
);
8780 /* uset_gist depends on constraints without existentially quantified
8781 * variables sorting first.
8783 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8785 const struct constraint
*c1
= (const struct constraint
*)p1
;
8786 const struct constraint
*c2
= (const struct constraint
*)p2
;
8788 unsigned size
= isl_min(c1
->size
, c2
->size
);
8790 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8791 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8796 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8799 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8800 struct isl_basic_map
*bmap
)
8803 struct constraint
*c
;
8808 if (bmap
->n_ineq
== 0)
8810 total
= isl_basic_map_total_dim(bmap
);
8811 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8814 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8816 c
[i
].c
= bmap
->ineq
[i
];
8818 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8819 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8820 bmap
->ineq
[i
] = c
[i
].c
;
8824 isl_basic_map_free(bmap
);
8828 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8829 __isl_take isl_basic_set
*bset
)
8831 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8832 (struct isl_basic_map
*)bset
);
8835 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8839 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8841 bmap
= isl_basic_map_remove_redundancies(bmap
);
8842 bmap
= isl_basic_map_sort_constraints(bmap
);
8844 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8848 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8850 return (struct isl_basic_set
*)isl_basic_map_normalize(
8851 (struct isl_basic_map
*)bset
);
8854 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8855 const __isl_keep isl_basic_map
*bmap2
)
8862 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8863 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8864 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8865 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8866 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8867 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8868 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8869 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8870 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8871 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8872 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8874 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8876 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8878 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8879 return bmap1
->n_eq
- bmap2
->n_eq
;
8880 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8881 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8882 if (bmap1
->n_div
!= bmap2
->n_div
)
8883 return bmap1
->n_div
- bmap2
->n_div
;
8884 total
= isl_basic_map_total_dim(bmap1
);
8885 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8886 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8890 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8891 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8895 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8896 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8903 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8904 const __isl_keep isl_basic_set
*bset2
)
8906 return isl_basic_map_plain_cmp(bset1
, bset2
);
8909 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8915 if (set1
->n
!= set2
->n
)
8916 return set1
->n
- set2
->n
;
8918 for (i
= 0; i
< set1
->n
; ++i
) {
8919 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8927 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8928 __isl_keep isl_basic_map
*bmap2
)
8930 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8933 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8934 __isl_keep isl_basic_set
*bset2
)
8936 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8937 (isl_basic_map
*)bset2
);
8940 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8942 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8943 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8945 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8948 /* We normalize in place, but if anything goes wrong we need
8949 * to return NULL, so we need to make sure we don't change the
8950 * meaning of any possible other copies of map.
8952 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
8955 struct isl_basic_map
*bmap
;
8959 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8961 for (i
= 0; i
< map
->n
; ++i
) {
8962 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8965 isl_basic_map_free(map
->p
[i
]);
8968 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8969 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
8970 map
= isl_map_remove_empty_parts(map
);
8973 for (i
= map
->n
- 1; i
>= 1; --i
) {
8974 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
8976 isl_basic_map_free(map
->p
[i
-1]);
8977 for (j
= i
; j
< map
->n
; ++j
)
8978 map
->p
[j
-1] = map
->p
[j
];
8988 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8990 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8993 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9003 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9006 map1
= isl_map_copy(map1
);
9007 map2
= isl_map_copy(map2
);
9008 map1
= isl_map_normalize(map1
);
9009 map2
= isl_map_normalize(map2
);
9012 equal
= map1
->n
== map2
->n
;
9013 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9014 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9027 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9029 return isl_map_plain_is_equal(map1
, map2
);
9032 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9034 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9035 (struct isl_map
*)set2
);
9038 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9040 return isl_set_plain_is_equal(set1
, set2
);
9043 /* Return an interval that ranges from min to max (inclusive)
9045 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9046 isl_int min
, isl_int max
)
9049 struct isl_basic_set
*bset
= NULL
;
9051 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9055 k
= isl_basic_set_alloc_inequality(bset
);
9058 isl_int_set_si(bset
->ineq
[k
][1], 1);
9059 isl_int_neg(bset
->ineq
[k
][0], min
);
9061 k
= isl_basic_set_alloc_inequality(bset
);
9064 isl_int_set_si(bset
->ineq
[k
][1], -1);
9065 isl_int_set(bset
->ineq
[k
][0], max
);
9069 isl_basic_set_free(bset
);
9073 /* Return the Cartesian product of the basic sets in list (in the given order).
9075 __isl_give isl_basic_set
*isl_basic_set_list_product(
9076 __isl_take
struct isl_basic_set_list
*list
)
9084 struct isl_basic_set
*product
= NULL
;
9088 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9089 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9090 nparam
= isl_basic_set_n_param(list
->p
[0]);
9091 dim
= isl_basic_set_n_dim(list
->p
[0]);
9092 extra
= list
->p
[0]->n_div
;
9093 n_eq
= list
->p
[0]->n_eq
;
9094 n_ineq
= list
->p
[0]->n_ineq
;
9095 for (i
= 1; i
< list
->n
; ++i
) {
9096 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9097 isl_assert(list
->ctx
,
9098 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9099 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9100 extra
+= list
->p
[i
]->n_div
;
9101 n_eq
+= list
->p
[i
]->n_eq
;
9102 n_ineq
+= list
->p
[i
]->n_ineq
;
9104 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9109 for (i
= 0; i
< list
->n
; ++i
) {
9110 isl_basic_set_add_constraints(product
,
9111 isl_basic_set_copy(list
->p
[i
]), dim
);
9112 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9114 isl_basic_set_list_free(list
);
9117 isl_basic_set_free(product
);
9118 isl_basic_set_list_free(list
);
9122 struct isl_basic_map
*isl_basic_map_product(
9123 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9125 isl_space
*dim_result
= NULL
;
9126 struct isl_basic_map
*bmap
;
9127 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9128 struct isl_dim_map
*dim_map1
, *dim_map2
;
9130 if (!bmap1
|| !bmap2
)
9133 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9134 bmap2
->dim
, isl_dim_param
), goto error
);
9135 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9136 isl_space_copy(bmap2
->dim
));
9138 in1
= isl_basic_map_n_in(bmap1
);
9139 in2
= isl_basic_map_n_in(bmap2
);
9140 out1
= isl_basic_map_n_out(bmap1
);
9141 out2
= isl_basic_map_n_out(bmap2
);
9142 nparam
= isl_basic_map_n_param(bmap1
);
9144 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9145 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9146 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9147 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9148 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9149 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9150 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9151 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9152 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9153 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9154 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9156 bmap
= isl_basic_map_alloc_space(dim_result
,
9157 bmap1
->n_div
+ bmap2
->n_div
,
9158 bmap1
->n_eq
+ bmap2
->n_eq
,
9159 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9160 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9161 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9162 bmap
= isl_basic_map_simplify(bmap
);
9163 return isl_basic_map_finalize(bmap
);
9165 isl_basic_map_free(bmap1
);
9166 isl_basic_map_free(bmap2
);
9170 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9171 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9173 isl_basic_map
*prod
;
9175 prod
= isl_basic_map_product(bmap1
, bmap2
);
9176 prod
= isl_basic_map_flatten(prod
);
9180 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9181 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9183 return isl_basic_map_flat_range_product(bset1
, bset2
);
9186 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9187 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9189 isl_space
*space_result
= NULL
;
9190 isl_basic_map
*bmap
;
9191 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9192 struct isl_dim_map
*dim_map1
, *dim_map2
;
9194 if (!bmap1
|| !bmap2
)
9197 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9198 isl_space_copy(bmap2
->dim
));
9200 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9201 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9202 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9203 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9205 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9206 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9207 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9208 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9209 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9210 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9211 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9212 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9213 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9214 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9215 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9217 bmap
= isl_basic_map_alloc_space(space_result
,
9218 bmap1
->n_div
+ bmap2
->n_div
,
9219 bmap1
->n_eq
+ bmap2
->n_eq
,
9220 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9221 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9222 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9223 bmap
= isl_basic_map_simplify(bmap
);
9224 return isl_basic_map_finalize(bmap
);
9226 isl_basic_map_free(bmap1
);
9227 isl_basic_map_free(bmap2
);
9231 __isl_give isl_basic_map
*isl_basic_map_range_product(
9232 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9234 isl_space
*dim_result
= NULL
;
9235 isl_basic_map
*bmap
;
9236 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9237 struct isl_dim_map
*dim_map1
, *dim_map2
;
9239 if (!bmap1
|| !bmap2
)
9242 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9243 bmap2
->dim
, isl_dim_param
))
9244 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9245 "parameters don't match", goto error
);
9247 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9248 isl_space_copy(bmap2
->dim
));
9250 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9251 out1
= isl_basic_map_n_out(bmap1
);
9252 out2
= isl_basic_map_n_out(bmap2
);
9253 nparam
= isl_basic_map_n_param(bmap1
);
9255 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9256 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9257 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9258 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9259 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9260 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9261 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9262 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9263 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9264 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9265 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9267 bmap
= isl_basic_map_alloc_space(dim_result
,
9268 bmap1
->n_div
+ bmap2
->n_div
,
9269 bmap1
->n_eq
+ bmap2
->n_eq
,
9270 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9271 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9272 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9273 bmap
= isl_basic_map_simplify(bmap
);
9274 return isl_basic_map_finalize(bmap
);
9276 isl_basic_map_free(bmap1
);
9277 isl_basic_map_free(bmap2
);
9281 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9282 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9284 isl_basic_map
*prod
;
9286 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9287 prod
= isl_basic_map_flatten_range(prod
);
9291 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9292 __isl_take isl_map
*map2
,
9293 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
9294 __isl_take isl_space
*right
),
9295 __isl_give isl_basic_map
*(*basic_map_product
)(
9296 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
9299 struct isl_map
*result
;
9305 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9306 map2
->dim
, isl_dim_param
), goto error
);
9308 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9309 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9310 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9312 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
9313 isl_space_copy(map2
->dim
)),
9314 map1
->n
* map2
->n
, flags
);
9317 for (i
= 0; i
< map1
->n
; ++i
)
9318 for (j
= 0; j
< map2
->n
; ++j
) {
9319 struct isl_basic_map
*part
;
9320 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9321 isl_basic_map_copy(map2
->p
[j
]));
9322 if (isl_basic_map_is_empty(part
))
9323 isl_basic_map_free(part
);
9325 result
= isl_map_add_basic_map(result
, part
);
9338 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9340 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9341 __isl_take isl_map
*map2
)
9343 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9346 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9347 __isl_take isl_map
*map2
)
9349 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9352 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9354 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9355 __isl_take isl_map
*map2
)
9359 prod
= isl_map_product(map1
, map2
);
9360 prod
= isl_map_flatten(prod
);
9364 /* Given two set A and B, construct its Cartesian product A x B.
9366 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9368 return isl_map_range_product(set1
, set2
);
9371 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9372 __isl_take isl_set
*set2
)
9374 return isl_map_flat_range_product(set1
, set2
);
9377 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9379 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9380 __isl_take isl_map
*map2
)
9382 return map_product(map1
, map2
, &isl_space_domain_product
,
9383 &isl_basic_map_domain_product
);
9386 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9388 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9389 __isl_take isl_map
*map2
)
9391 return map_product(map1
, map2
, &isl_space_range_product
,
9392 &isl_basic_map_range_product
);
9395 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9396 __isl_take isl_map
*map2
)
9398 return isl_map_align_params_map_map_and(map1
, map2
,
9399 &map_domain_product_aligned
);
9402 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9403 __isl_take isl_map
*map2
)
9405 return isl_map_align_params_map_map_and(map1
, map2
,
9406 &map_range_product_aligned
);
9409 /* Given a map A -> [B -> C], extract the map A -> B.
9411 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9418 if (!isl_space_range_is_wrapping(map
->dim
))
9419 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9420 "range is not a product", return isl_map_free(map
));
9422 space
= isl_map_get_space(map
);
9423 total
= isl_space_dim(space
, isl_dim_out
);
9424 space
= isl_space_range_factor_domain(space
);
9425 keep
= isl_space_dim(space
, isl_dim_out
);
9426 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9427 map
= isl_map_reset_space(map
, space
);
9432 /* Given a map A -> [B -> C], extract the map A -> C.
9434 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9441 if (!isl_space_range_is_wrapping(map
->dim
))
9442 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9443 "range is not a product", return isl_map_free(map
));
9445 space
= isl_map_get_space(map
);
9446 total
= isl_space_dim(space
, isl_dim_out
);
9447 space
= isl_space_range_factor_range(space
);
9448 keep
= isl_space_dim(space
, isl_dim_out
);
9449 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9450 map
= isl_map_reset_space(map
, space
);
9455 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9457 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9458 __isl_take isl_map
*map2
)
9462 prod
= isl_map_domain_product(map1
, map2
);
9463 prod
= isl_map_flatten_domain(prod
);
9467 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9469 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9470 __isl_take isl_map
*map2
)
9474 prod
= isl_map_range_product(map1
, map2
);
9475 prod
= isl_map_flatten_range(prod
);
9479 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9482 uint32_t hash
= isl_hash_init();
9487 bmap
= isl_basic_map_copy(bmap
);
9488 bmap
= isl_basic_map_normalize(bmap
);
9491 total
= isl_basic_map_total_dim(bmap
);
9492 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9493 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9495 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9496 isl_hash_hash(hash
, c_hash
);
9498 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9499 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9501 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9502 isl_hash_hash(hash
, c_hash
);
9504 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9505 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9507 if (isl_int_is_zero(bmap
->div
[i
][0]))
9509 isl_hash_byte(hash
, i
& 0xFF);
9510 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9511 isl_hash_hash(hash
, c_hash
);
9513 isl_basic_map_free(bmap
);
9517 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9519 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9522 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9529 map
= isl_map_copy(map
);
9530 map
= isl_map_normalize(map
);
9534 hash
= isl_hash_init();
9535 for (i
= 0; i
< map
->n
; ++i
) {
9537 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9538 isl_hash_hash(hash
, bmap_hash
);
9546 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9548 return isl_map_get_hash((isl_map
*)set
);
9551 /* Check if the value for dimension dim is completely determined
9552 * by the values of the other parameters and variables.
9553 * That is, check if dimension dim is involved in an equality.
9555 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9562 nparam
= isl_basic_set_n_param(bset
);
9563 for (i
= 0; i
< bset
->n_eq
; ++i
)
9564 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9569 /* Check if the value for dimension dim is completely determined
9570 * by the values of the other parameters and variables.
9571 * That is, check if dimension dim is involved in an equality
9572 * for each of the subsets.
9574 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9580 for (i
= 0; i
< set
->n
; ++i
) {
9582 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9589 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9591 return set
? set
->n
: 0;
9594 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9595 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9602 for (i
= 0; i
< map
->n
; ++i
)
9603 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9609 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9610 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9617 for (i
= 0; i
< set
->n
; ++i
)
9618 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9624 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9631 bset
= isl_basic_set_cow(bset
);
9635 dim
= isl_basic_set_get_space(bset
);
9636 dim
= isl_space_lift(dim
, bset
->n_div
);
9639 isl_space_free(bset
->dim
);
9641 bset
->extra
-= bset
->n_div
;
9644 bset
= isl_basic_set_finalize(bset
);
9648 isl_basic_set_free(bset
);
9652 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9658 set
= isl_set_align_divs(set
);
9663 set
= isl_set_cow(set
);
9667 n_div
= set
->p
[0]->n_div
;
9668 dim
= isl_set_get_space(set
);
9669 dim
= isl_space_lift(dim
, n_div
);
9672 isl_space_free(set
->dim
);
9675 for (i
= 0; i
< set
->n
; ++i
) {
9676 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9687 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9690 struct isl_basic_map
*bmap
;
9697 set
= isl_set_align_divs(set
);
9702 dim
= isl_set_get_space(set
);
9703 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9705 return isl_map_identity(isl_space_map_from_set(dim
));
9708 n_div
= set
->p
[0]->n_div
;
9709 dim
= isl_space_map_from_set(dim
);
9710 n_param
= isl_space_dim(dim
, isl_dim_param
);
9711 n_set
= isl_space_dim(dim
, isl_dim_in
);
9712 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9713 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9714 for (i
= 0; i
< n_set
; ++i
)
9715 bmap
= var_equal(bmap
, i
);
9717 total
= n_param
+ n_set
+ n_set
+ n_div
;
9718 for (i
= 0; i
< n_div
; ++i
) {
9719 k
= isl_basic_map_alloc_inequality(bmap
);
9722 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9723 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9724 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9725 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9726 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9727 set
->p
[0]->div
[i
][0]);
9729 l
= isl_basic_map_alloc_inequality(bmap
);
9732 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9733 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9734 set
->p
[0]->div
[i
][0]);
9735 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9739 bmap
= isl_basic_map_simplify(bmap
);
9740 bmap
= isl_basic_map_finalize(bmap
);
9741 return isl_map_from_basic_map(bmap
);
9744 isl_basic_map_free(bmap
);
9748 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9756 dim
= isl_basic_set_total_dim(bset
);
9757 size
+= bset
->n_eq
* (1 + dim
);
9758 size
+= bset
->n_ineq
* (1 + dim
);
9759 size
+= bset
->n_div
* (2 + dim
);
9764 int isl_set_size(__isl_keep isl_set
*set
)
9772 for (i
= 0; i
< set
->n
; ++i
)
9773 size
+= isl_basic_set_size(set
->p
[i
]);
9778 /* Check if there is any lower bound (if lower == 0) and/or upper
9779 * bound (if upper == 0) on the specified dim.
9781 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9782 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9789 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9791 pos
+= isl_basic_map_offset(bmap
, type
);
9793 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9794 if (isl_int_is_zero(bmap
->div
[i
][0]))
9796 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9800 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9801 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
9804 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9805 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
9812 return lower
&& upper
;
9815 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9816 enum isl_dim_type type
, unsigned pos
)
9818 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9821 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9822 enum isl_dim_type type
, unsigned pos
)
9824 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9827 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9828 enum isl_dim_type type
, unsigned pos
)
9830 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9833 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9834 enum isl_dim_type type
, unsigned pos
)
9841 for (i
= 0; i
< map
->n
; ++i
) {
9843 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9844 if (bounded
< 0 || !bounded
)
9851 /* Return 1 if the specified dim is involved in both an upper bound
9852 * and a lower bound.
9854 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9855 enum isl_dim_type type
, unsigned pos
)
9857 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9860 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9862 static int has_any_bound(__isl_keep isl_map
*map
,
9863 enum isl_dim_type type
, unsigned pos
,
9864 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9865 enum isl_dim_type type
, unsigned pos
))
9872 for (i
= 0; i
< map
->n
; ++i
) {
9874 bounded
= fn(map
->p
[i
], type
, pos
);
9875 if (bounded
< 0 || bounded
)
9882 /* Return 1 if the specified dim is involved in any lower bound.
9884 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
9885 enum isl_dim_type type
, unsigned pos
)
9887 return has_any_bound(set
, type
, pos
,
9888 &isl_basic_map_dim_has_lower_bound
);
9891 /* Return 1 if the specified dim is involved in any upper bound.
9893 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
9894 enum isl_dim_type type
, unsigned pos
)
9896 return has_any_bound(set
, type
, pos
,
9897 &isl_basic_map_dim_has_upper_bound
);
9900 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9902 static int has_bound(__isl_keep isl_map
*map
,
9903 enum isl_dim_type type
, unsigned pos
,
9904 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9905 enum isl_dim_type type
, unsigned pos
))
9912 for (i
= 0; i
< map
->n
; ++i
) {
9914 bounded
= fn(map
->p
[i
], type
, pos
);
9915 if (bounded
< 0 || !bounded
)
9922 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9924 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9925 enum isl_dim_type type
, unsigned pos
)
9927 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9930 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9932 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9933 enum isl_dim_type type
, unsigned pos
)
9935 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
9938 /* For each of the "n" variables starting at "first", determine
9939 * the sign of the variable and put the results in the first "n"
9940 * elements of the array "signs".
9942 * 1 means that the variable is non-negative
9943 * -1 means that the variable is non-positive
9944 * 0 means the variable attains both positive and negative values.
9946 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
9947 unsigned first
, unsigned n
, int *signs
)
9949 isl_vec
*bound
= NULL
;
9950 struct isl_tab
*tab
= NULL
;
9951 struct isl_tab_undo
*snap
;
9954 if (!bset
|| !signs
)
9957 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
9958 tab
= isl_tab_from_basic_set(bset
, 0);
9962 isl_seq_clr(bound
->el
, bound
->size
);
9963 isl_int_set_si(bound
->el
[0], -1);
9965 snap
= isl_tab_snap(tab
);
9966 for (i
= 0; i
< n
; ++i
) {
9969 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
9970 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9973 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9974 if (isl_tab_rollback(tab
, snap
) < 0)
9982 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
9983 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9986 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9987 if (isl_tab_rollback(tab
, snap
) < 0)
9990 signs
[i
] = empty
? -1 : 0;
9994 isl_vec_free(bound
);
9998 isl_vec_free(bound
);
10002 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10003 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10005 if (!bset
|| !signs
)
10007 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10010 first
+= pos(bset
->dim
, type
) - 1;
10011 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10014 /* Check if the given basic map is obviously single-valued.
10015 * In particular, for each output dimension, check that there is
10016 * an equality that defines the output dimension in terms of
10017 * earlier dimensions.
10019 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10029 total
= 1 + isl_basic_map_total_dim(bmap
);
10030 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10031 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10033 for (i
= 0; i
< n_out
; ++i
) {
10034 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10035 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
10037 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
10038 total
- (o_out
+ i
+ 1)) == -1)
10041 if (j
>= bmap
->n_eq
)
10048 /* Check if the given basic map is single-valued.
10049 * We simply compute
10053 * and check if the result is a subset of the identity mapping.
10055 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10058 isl_basic_map
*test
;
10062 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10066 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10067 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10069 space
= isl_basic_map_get_space(bmap
);
10070 space
= isl_space_map_from_set(isl_space_range(space
));
10071 id
= isl_basic_map_identity(space
);
10073 sv
= isl_basic_map_is_subset(test
, id
);
10075 isl_basic_map_free(test
);
10076 isl_basic_map_free(id
);
10081 /* Check if the given map is obviously single-valued.
10083 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10092 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10095 /* Check if the given map is single-valued.
10096 * We simply compute
10100 * and check if the result is a subset of the identity mapping.
10102 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10109 sv
= isl_map_plain_is_single_valued(map
);
10113 test
= isl_map_reverse(isl_map_copy(map
));
10114 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10116 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10117 id
= isl_map_identity(dim
);
10119 sv
= isl_map_is_subset(test
, id
);
10121 isl_map_free(test
);
10127 int isl_map_is_injective(__isl_keep isl_map
*map
)
10131 map
= isl_map_copy(map
);
10132 map
= isl_map_reverse(map
);
10133 in
= isl_map_is_single_valued(map
);
10139 /* Check if the given map is obviously injective.
10141 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10145 map
= isl_map_copy(map
);
10146 map
= isl_map_reverse(map
);
10147 in
= isl_map_plain_is_single_valued(map
);
10153 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10157 sv
= isl_map_is_single_valued(map
);
10161 return isl_map_is_injective(map
);
10164 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10166 return isl_map_is_single_valued((isl_map
*)set
);
10169 int isl_map_is_translation(__isl_keep isl_map
*map
)
10174 delta
= isl_map_deltas(isl_map_copy(map
));
10175 ok
= isl_set_is_singleton(delta
);
10176 isl_set_free(delta
);
10181 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10183 if (isl_seq_first_non_zero(p
, pos
) != -1)
10185 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10190 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10199 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10202 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10203 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10204 for (j
= 0; j
< nvar
; ++j
) {
10205 int lower
= 0, upper
= 0;
10206 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10207 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10209 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10213 if (i
< bset
->n_eq
)
10215 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10216 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10218 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10220 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10225 if (!lower
|| !upper
)
10232 int isl_set_is_box(__isl_keep isl_set
*set
)
10239 return isl_basic_set_is_box(set
->p
[0]);
10242 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10247 return isl_space_is_wrapping(bset
->dim
);
10250 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10255 return isl_space_is_wrapping(set
->dim
);
10258 /* Is the range of "map" a wrapped relation?
10260 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10265 return isl_space_range_is_wrapping(map
->dim
);
10268 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10270 bmap
= isl_basic_map_cow(bmap
);
10274 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10278 bmap
= isl_basic_map_finalize(bmap
);
10280 return (isl_basic_set
*)bmap
;
10282 isl_basic_map_free(bmap
);
10286 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10290 map
= isl_map_cow(map
);
10294 for (i
= 0; i
< map
->n
; ++i
) {
10295 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10299 map
->dim
= isl_space_wrap(map
->dim
);
10303 return (isl_set
*)map
;
10309 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10311 bset
= isl_basic_set_cow(bset
);
10315 bset
->dim
= isl_space_unwrap(bset
->dim
);
10319 bset
= isl_basic_set_finalize(bset
);
10321 return (isl_basic_map
*)bset
;
10323 isl_basic_set_free(bset
);
10327 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10334 if (!isl_set_is_wrapping(set
))
10335 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10338 set
= isl_set_cow(set
);
10342 for (i
= 0; i
< set
->n
; ++i
) {
10343 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10348 set
->dim
= isl_space_unwrap(set
->dim
);
10352 return (isl_map
*)set
;
10358 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10359 enum isl_dim_type type
)
10364 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10367 bmap
= isl_basic_map_cow(bmap
);
10371 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10375 bmap
= isl_basic_map_finalize(bmap
);
10379 isl_basic_map_free(bmap
);
10383 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10384 enum isl_dim_type type
)
10391 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10394 map
= isl_map_cow(map
);
10398 for (i
= 0; i
< map
->n
; ++i
) {
10399 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10403 map
->dim
= isl_space_reset(map
->dim
, type
);
10413 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10418 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10421 bmap
= isl_basic_map_cow(bmap
);
10425 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10429 bmap
= isl_basic_map_finalize(bmap
);
10433 isl_basic_map_free(bmap
);
10437 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10439 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10442 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10443 __isl_take isl_basic_map
*bmap
)
10448 if (!bmap
->dim
->nested
[0])
10451 bmap
= isl_basic_map_cow(bmap
);
10455 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10459 bmap
= isl_basic_map_finalize(bmap
);
10463 isl_basic_map_free(bmap
);
10467 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10468 __isl_take isl_basic_map
*bmap
)
10473 if (!bmap
->dim
->nested
[1])
10476 bmap
= isl_basic_map_cow(bmap
);
10480 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10484 bmap
= isl_basic_map_finalize(bmap
);
10488 isl_basic_map_free(bmap
);
10492 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10499 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10502 map
= isl_map_cow(map
);
10506 for (i
= 0; i
< map
->n
; ++i
) {
10507 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10511 map
->dim
= isl_space_flatten(map
->dim
);
10521 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10523 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10526 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10528 isl_space
*dim
, *flat_dim
;
10531 dim
= isl_set_get_space(set
);
10532 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10533 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10534 map
= isl_map_intersect_domain(map
, set
);
10539 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10546 if (!map
->dim
->nested
[0])
10549 map
= isl_map_cow(map
);
10553 for (i
= 0; i
< map
->n
; ++i
) {
10554 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10558 map
->dim
= isl_space_flatten_domain(map
->dim
);
10568 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10575 if (!map
->dim
->nested
[1])
10578 map
= isl_map_cow(map
);
10582 for (i
= 0; i
< map
->n
; ++i
) {
10583 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10587 map
->dim
= isl_space_flatten_range(map
->dim
);
10597 /* Reorder the dimensions of "bmap" according to the given dim_map
10598 * and set the dimension specification to "dim".
10600 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10601 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10603 isl_basic_map
*res
;
10606 bmap
= isl_basic_map_cow(bmap
);
10607 if (!bmap
|| !dim
|| !dim_map
)
10610 flags
= bmap
->flags
;
10611 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10612 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10613 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10614 res
= isl_basic_map_alloc_space(dim
,
10615 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10616 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10618 res
->flags
= flags
;
10619 res
= isl_basic_map_finalize(res
);
10623 isl_basic_map_free(bmap
);
10624 isl_space_free(dim
);
10628 /* Reorder the dimensions of "map" according to given reordering.
10630 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10631 __isl_take isl_reordering
*r
)
10634 struct isl_dim_map
*dim_map
;
10636 map
= isl_map_cow(map
);
10637 dim_map
= isl_dim_map_from_reordering(r
);
10638 if (!map
|| !r
|| !dim_map
)
10641 for (i
= 0; i
< map
->n
; ++i
) {
10642 struct isl_dim_map
*dim_map_i
;
10644 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10646 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10647 isl_space_copy(r
->dim
), dim_map_i
);
10653 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10655 isl_reordering_free(r
);
10661 isl_reordering_free(r
);
10665 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10666 __isl_take isl_reordering
*r
)
10668 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10671 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10672 __isl_take isl_space
*model
)
10676 if (!map
|| !model
)
10679 ctx
= isl_space_get_ctx(model
);
10680 if (!isl_space_has_named_params(model
))
10681 isl_die(ctx
, isl_error_invalid
,
10682 "model has unnamed parameters", goto error
);
10683 if (!isl_space_has_named_params(map
->dim
))
10684 isl_die(ctx
, isl_error_invalid
,
10685 "relation has unnamed parameters", goto error
);
10686 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10687 isl_reordering
*exp
;
10689 model
= isl_space_drop_dims(model
, isl_dim_in
,
10690 0, isl_space_dim(model
, isl_dim_in
));
10691 model
= isl_space_drop_dims(model
, isl_dim_out
,
10692 0, isl_space_dim(model
, isl_dim_out
));
10693 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10694 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10695 map
= isl_map_realign(map
, exp
);
10698 isl_space_free(model
);
10701 isl_space_free(model
);
10706 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10707 __isl_take isl_space
*model
)
10709 return isl_map_align_params(set
, model
);
10712 /* Align the parameters of "bmap" to those of "model", introducing
10713 * additional parameters if needed.
10715 __isl_give isl_basic_map
*isl_basic_map_align_params(
10716 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10720 if (!bmap
|| !model
)
10723 ctx
= isl_space_get_ctx(model
);
10724 if (!isl_space_has_named_params(model
))
10725 isl_die(ctx
, isl_error_invalid
,
10726 "model has unnamed parameters", goto error
);
10727 if (!isl_space_has_named_params(bmap
->dim
))
10728 isl_die(ctx
, isl_error_invalid
,
10729 "relation has unnamed parameters", goto error
);
10730 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10731 isl_reordering
*exp
;
10732 struct isl_dim_map
*dim_map
;
10734 model
= isl_space_drop_dims(model
, isl_dim_in
,
10735 0, isl_space_dim(model
, isl_dim_in
));
10736 model
= isl_space_drop_dims(model
, isl_dim_out
,
10737 0, isl_space_dim(model
, isl_dim_out
));
10738 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10739 exp
= isl_reordering_extend_space(exp
,
10740 isl_basic_map_get_space(bmap
));
10741 dim_map
= isl_dim_map_from_reordering(exp
);
10742 bmap
= isl_basic_map_realign(bmap
,
10743 exp
? isl_space_copy(exp
->dim
) : NULL
,
10744 isl_dim_map_extend(dim_map
, bmap
));
10745 isl_reordering_free(exp
);
10749 isl_space_free(model
);
10752 isl_space_free(model
);
10753 isl_basic_map_free(bmap
);
10757 /* Align the parameters of "bset" to those of "model", introducing
10758 * additional parameters if needed.
10760 __isl_give isl_basic_set
*isl_basic_set_align_params(
10761 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10763 return isl_basic_map_align_params(bset
, model
);
10766 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10767 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10768 enum isl_dim_type c2
, enum isl_dim_type c3
,
10769 enum isl_dim_type c4
, enum isl_dim_type c5
)
10771 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10772 struct isl_mat
*mat
;
10778 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
10779 isl_basic_map_total_dim(bmap
) + 1);
10782 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10783 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10784 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10785 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10786 isl_int_set(mat
->row
[i
][pos
],
10787 bmap
->eq
[i
][off
+ k
]);
10795 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
10796 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10797 enum isl_dim_type c2
, enum isl_dim_type c3
,
10798 enum isl_dim_type c4
, enum isl_dim_type c5
)
10800 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10801 struct isl_mat
*mat
;
10807 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
10808 isl_basic_map_total_dim(bmap
) + 1);
10811 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
10812 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10813 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10814 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10815 isl_int_set(mat
->row
[i
][pos
],
10816 bmap
->ineq
[i
][off
+ k
]);
10824 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
10825 __isl_take isl_space
*dim
,
10826 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10827 enum isl_dim_type c2
, enum isl_dim_type c3
,
10828 enum isl_dim_type c4
, enum isl_dim_type c5
)
10830 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10831 isl_basic_map
*bmap
;
10837 if (!dim
|| !eq
|| !ineq
)
10840 if (eq
->n_col
!= ineq
->n_col
)
10841 isl_die(dim
->ctx
, isl_error_invalid
,
10842 "equalities and inequalities matrices should have "
10843 "same number of columns", goto error
);
10845 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
10847 if (eq
->n_col
< total
)
10848 isl_die(dim
->ctx
, isl_error_invalid
,
10849 "number of columns too small", goto error
);
10851 extra
= eq
->n_col
- total
;
10853 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
10854 eq
->n_row
, ineq
->n_row
);
10857 for (i
= 0; i
< extra
; ++i
) {
10858 k
= isl_basic_map_alloc_div(bmap
);
10861 isl_int_set_si(bmap
->div
[k
][0], 0);
10863 for (i
= 0; i
< eq
->n_row
; ++i
) {
10864 l
= isl_basic_map_alloc_equality(bmap
);
10867 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10868 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10869 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10870 isl_int_set(bmap
->eq
[l
][off
+ k
],
10876 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10877 l
= isl_basic_map_alloc_inequality(bmap
);
10880 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10881 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10882 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10883 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10884 ineq
->row
[i
][pos
]);
10890 isl_space_free(dim
);
10892 isl_mat_free(ineq
);
10894 bmap
= isl_basic_map_simplify(bmap
);
10895 return isl_basic_map_finalize(bmap
);
10897 isl_space_free(dim
);
10899 isl_mat_free(ineq
);
10903 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10904 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10905 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10907 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10908 c1
, c2
, c3
, c4
, isl_dim_in
);
10911 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10912 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10913 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10915 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10916 c1
, c2
, c3
, c4
, isl_dim_in
);
10919 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10920 __isl_take isl_space
*dim
,
10921 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10922 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10924 return (isl_basic_set
*)
10925 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
10926 c1
, c2
, c3
, c4
, isl_dim_in
);
10929 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
10934 return isl_space_can_zip(bmap
->dim
);
10937 int isl_map_can_zip(__isl_keep isl_map
*map
)
10942 return isl_space_can_zip(map
->dim
);
10945 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10946 * (A -> C) -> (B -> D).
10948 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
10957 if (!isl_basic_map_can_zip(bmap
))
10958 isl_die(bmap
->ctx
, isl_error_invalid
,
10959 "basic map cannot be zipped", goto error
);
10960 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
10961 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
10962 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
10963 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
10964 bmap
= isl_basic_map_cow(bmap
);
10965 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
10968 bmap
->dim
= isl_space_zip(bmap
->dim
);
10973 isl_basic_map_free(bmap
);
10977 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10978 * (A -> C) -> (B -> D).
10980 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
10987 if (!isl_map_can_zip(map
))
10988 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
10991 map
= isl_map_cow(map
);
10995 for (i
= 0; i
< map
->n
; ++i
) {
10996 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11001 map
->dim
= isl_space_zip(map
->dim
);
11011 /* Can we apply isl_basic_map_curry to "bmap"?
11012 * That is, does it have a nested relation in its domain?
11014 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11019 return isl_space_can_curry(bmap
->dim
);
11022 /* Can we apply isl_map_curry to "map"?
11023 * That is, does it have a nested relation in its domain?
11025 int isl_map_can_curry(__isl_keep isl_map
*map
)
11030 return isl_space_can_curry(map
->dim
);
11033 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11036 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11042 if (!isl_basic_map_can_curry(bmap
))
11043 isl_die(bmap
->ctx
, isl_error_invalid
,
11044 "basic map cannot be curried", goto error
);
11045 bmap
= isl_basic_map_cow(bmap
);
11048 bmap
->dim
= isl_space_curry(bmap
->dim
);
11053 isl_basic_map_free(bmap
);
11057 /* Given a map (A -> B) -> C, return the corresponding map
11060 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11067 if (!isl_map_can_curry(map
))
11068 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11071 map
= isl_map_cow(map
);
11075 for (i
= 0; i
< map
->n
; ++i
) {
11076 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11081 map
->dim
= isl_space_curry(map
->dim
);
11091 /* Can we apply isl_basic_map_uncurry to "bmap"?
11092 * That is, does it have a nested relation in its domain?
11094 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11099 return isl_space_can_uncurry(bmap
->dim
);
11102 /* Can we apply isl_map_uncurry to "map"?
11103 * That is, does it have a nested relation in its domain?
11105 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11110 return isl_space_can_uncurry(map
->dim
);
11113 /* Given a basic map A -> (B -> C), return the corresponding basic map
11116 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11122 if (!isl_basic_map_can_uncurry(bmap
))
11123 isl_die(bmap
->ctx
, isl_error_invalid
,
11124 "basic map cannot be uncurried",
11125 return isl_basic_map_free(bmap
));
11126 bmap
= isl_basic_map_cow(bmap
);
11129 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11131 return isl_basic_map_free(bmap
);
11135 /* Given a map A -> (B -> C), return the corresponding map
11138 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11145 if (!isl_map_can_uncurry(map
))
11146 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11147 return isl_map_free(map
));
11149 map
= isl_map_cow(map
);
11153 for (i
= 0; i
< map
->n
; ++i
) {
11154 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11156 return isl_map_free(map
);
11159 map
->dim
= isl_space_uncurry(map
->dim
);
11161 return isl_map_free(map
);
11166 /* Construct a basic map mapping the domain of the affine expression
11167 * to a one-dimensional range prescribed by the affine expression.
11169 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11173 isl_local_space
*ls
;
11174 isl_basic_map
*bmap
;
11179 ls
= isl_aff_get_local_space(aff
);
11180 bmap
= isl_basic_map_from_local_space(ls
);
11181 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11182 k
= isl_basic_map_alloc_equality(bmap
);
11186 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11187 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11188 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11189 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11190 aff
->v
->size
- (pos
+ 1));
11193 bmap
= isl_basic_map_finalize(bmap
);
11197 isl_basic_map_free(bmap
);
11201 /* Construct a map mapping the domain of the affine expression
11202 * to a one-dimensional range prescribed by the affine expression.
11204 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11206 isl_basic_map
*bmap
;
11208 bmap
= isl_basic_map_from_aff(aff
);
11209 return isl_map_from_basic_map(bmap
);
11212 /* Construct a basic map mapping the domain the multi-affine expression
11213 * to its range, with each dimension in the range equated to the
11214 * corresponding affine expression.
11216 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11217 __isl_take isl_multi_aff
*maff
)
11221 isl_basic_map
*bmap
;
11226 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11227 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11228 "invalid space", return isl_multi_aff_free(maff
));
11230 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11231 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11233 for (i
= 0; i
< maff
->n
; ++i
) {
11235 isl_basic_map
*bmap_i
;
11237 aff
= isl_aff_copy(maff
->p
[i
]);
11238 bmap_i
= isl_basic_map_from_aff(aff
);
11240 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11243 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11245 isl_multi_aff_free(maff
);
11249 /* Construct a map mapping the domain the multi-affine expression
11250 * to its range, with each dimension in the range equated to the
11251 * corresponding affine expression.
11253 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11255 isl_basic_map
*bmap
;
11257 bmap
= isl_basic_map_from_multi_aff(maff
);
11258 return isl_map_from_basic_map(bmap
);
11261 /* Construct a basic map mapping a domain in the given space to
11262 * to an n-dimensional range, with n the number of elements in the list,
11263 * where each coordinate in the range is prescribed by the
11264 * corresponding affine expression.
11265 * The domains of all affine expressions in the list are assumed to match
11268 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11269 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11273 isl_basic_map
*bmap
;
11278 dim
= isl_space_from_domain(domain_dim
);
11279 bmap
= isl_basic_map_universe(dim
);
11281 for (i
= 0; i
< list
->n
; ++i
) {
11283 isl_basic_map
*bmap_i
;
11285 aff
= isl_aff_copy(list
->p
[i
]);
11286 bmap_i
= isl_basic_map_from_aff(aff
);
11288 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11291 isl_aff_list_free(list
);
11295 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11296 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11298 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11301 /* Construct a basic map where the given dimensions are equal to each other.
11303 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11304 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11306 isl_basic_map
*bmap
= NULL
;
11312 if (pos1
>= isl_space_dim(space
, type1
))
11313 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11314 "index out of bounds", goto error
);
11315 if (pos2
>= isl_space_dim(space
, type2
))
11316 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11317 "index out of bounds", goto error
);
11319 if (type1
== type2
&& pos1
== pos2
)
11320 return isl_basic_map_universe(space
);
11322 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11323 i
= isl_basic_map_alloc_equality(bmap
);
11326 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11327 pos1
+= isl_basic_map_offset(bmap
, type1
);
11328 pos2
+= isl_basic_map_offset(bmap
, type2
);
11329 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11330 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11331 bmap
= isl_basic_map_finalize(bmap
);
11332 isl_space_free(space
);
11335 isl_space_free(space
);
11336 isl_basic_map_free(bmap
);
11340 /* Add a constraint imposing that the given two dimensions are equal.
11342 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11343 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11347 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11349 bmap
= isl_basic_map_intersect(bmap
, eq
);
11354 /* Add a constraint imposing that the given two dimensions are equal.
11356 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11357 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11359 isl_basic_map
*bmap
;
11361 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11363 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11368 /* Add a constraint imposing that the given two dimensions have opposite values.
11370 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11371 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11373 isl_basic_map
*bmap
= NULL
;
11379 if (pos1
>= isl_map_dim(map
, type1
))
11380 isl_die(map
->ctx
, isl_error_invalid
,
11381 "index out of bounds", goto error
);
11382 if (pos2
>= isl_map_dim(map
, type2
))
11383 isl_die(map
->ctx
, isl_error_invalid
,
11384 "index out of bounds", goto error
);
11386 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11387 i
= isl_basic_map_alloc_equality(bmap
);
11390 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11391 pos1
+= isl_basic_map_offset(bmap
, type1
);
11392 pos2
+= isl_basic_map_offset(bmap
, type2
);
11393 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11394 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11395 bmap
= isl_basic_map_finalize(bmap
);
11397 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11401 isl_basic_map_free(bmap
);
11406 /* Add a constraint imposing that the value of the first dimension is
11407 * greater than or equal to that of the second.
11409 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11410 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11413 isl_local_space
*ls
;
11418 if (pos1
>= isl_basic_map_dim(bmap
, type1
))
11419 isl_die(bmap
->ctx
, isl_error_invalid
,
11420 "index out of bounds", return isl_basic_map_free(bmap
));
11421 if (pos2
>= isl_basic_map_dim(bmap
, type2
))
11422 isl_die(bmap
->ctx
, isl_error_invalid
,
11423 "index out of bounds", return isl_basic_map_free(bmap
));
11425 if (type1
== type2
&& pos1
== pos2
)
11428 ls
= isl_local_space_from_space(isl_basic_map_get_space(bmap
));
11429 c
= isl_inequality_alloc(ls
);
11430 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11431 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11432 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11437 /* Construct a basic map where the value of the first dimension is
11438 * greater than that of the second.
11440 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11441 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11443 isl_basic_map
*bmap
= NULL
;
11449 if (pos1
>= isl_space_dim(space
, type1
))
11450 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11451 "index out of bounds", goto error
);
11452 if (pos2
>= isl_space_dim(space
, type2
))
11453 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11454 "index out of bounds", goto error
);
11456 if (type1
== type2
&& pos1
== pos2
)
11457 return isl_basic_map_empty(space
);
11459 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11460 i
= isl_basic_map_alloc_inequality(bmap
);
11463 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11464 pos1
+= isl_basic_map_offset(bmap
, type1
);
11465 pos2
+= isl_basic_map_offset(bmap
, type2
);
11466 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11467 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11468 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11469 bmap
= isl_basic_map_finalize(bmap
);
11473 isl_space_free(space
);
11474 isl_basic_map_free(bmap
);
11478 /* Add a constraint imposing that the value of the first dimension is
11479 * greater than that of the second.
11481 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11482 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11486 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11488 bmap
= isl_basic_map_intersect(bmap
, gt
);
11493 /* Add a constraint imposing that the value of the first dimension is
11494 * greater than that of the second.
11496 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11497 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11499 isl_basic_map
*bmap
;
11501 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11503 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11508 /* Add a constraint imposing that the value of the first dimension is
11509 * smaller than that of the second.
11511 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11512 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11514 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11517 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11521 isl_local_space
*ls
;
11526 if (!isl_basic_map_divs_known(bmap
))
11527 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11528 "some divs are unknown", return NULL
);
11530 ls
= isl_basic_map_get_local_space(bmap
);
11531 div
= isl_local_space_get_div(ls
, pos
);
11532 isl_local_space_free(ls
);
11537 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11540 return isl_basic_map_get_div(bset
, pos
);
11543 /* Plug in "subs" for dimension "type", "pos" of "bset".
11545 * Let i be the dimension to replace and let "subs" be of the form
11549 * Any integer division with a non-zero coefficient for i,
11551 * floor((a i + g)/m)
11555 * floor((a f + d g)/(m d))
11557 * Constraints of the form
11565 * We currently require that "subs" is an integral expression.
11566 * Handling rational expressions may require us to add stride constraints
11567 * as we do in isl_basic_set_preimage_multi_aff.
11569 __isl_give isl_basic_set
*isl_basic_set_substitute(
11570 __isl_take isl_basic_set
*bset
,
11571 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11577 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11580 bset
= isl_basic_set_cow(bset
);
11581 if (!bset
|| !subs
)
11584 ctx
= isl_basic_set_get_ctx(bset
);
11585 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11586 isl_die(ctx
, isl_error_invalid
,
11587 "spaces don't match", goto error
);
11588 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11589 isl_die(ctx
, isl_error_unsupported
,
11590 "cannot handle divs yet", goto error
);
11591 if (!isl_int_is_one(subs
->v
->el
[0]))
11592 isl_die(ctx
, isl_error_invalid
,
11593 "can only substitute integer expressions", goto error
);
11595 pos
+= isl_basic_set_offset(bset
, type
);
11599 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11600 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11602 isl_int_set(v
, bset
->eq
[i
][pos
]);
11603 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11604 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11605 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11608 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11609 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11611 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11612 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11613 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11614 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11617 for (i
= 0; i
< bset
->n_div
; ++i
) {
11618 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11620 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11621 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11622 isl_seq_combine(bset
->div
[i
] + 1,
11623 subs
->v
->el
[0], bset
->div
[i
] + 1,
11624 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11625 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11630 bset
= isl_basic_set_simplify(bset
);
11631 return isl_basic_set_finalize(bset
);
11633 isl_basic_set_free(bset
);
11637 /* Plug in "subs" for dimension "type", "pos" of "set".
11639 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11640 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11644 if (set
&& isl_set_plain_is_empty(set
))
11647 set
= isl_set_cow(set
);
11651 for (i
= set
->n
- 1; i
>= 0; --i
) {
11652 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11653 if (remove_if_empty(set
, i
) < 0)
11663 /* Check if the range of "ma" is compatible with the domain or range
11664 * (depending on "type") of "bmap".
11665 * Return -1 if anything is wrong.
11667 static int check_basic_map_compatible_range_multi_aff(
11668 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11669 __isl_keep isl_multi_aff
*ma
)
11672 isl_space
*ma_space
;
11674 ma_space
= isl_multi_aff_get_space(ma
);
11675 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11676 isl_space_free(ma_space
);
11678 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11679 "spaces don't match", return -1);
11683 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11684 * coefficients before the transformed range of dimensions,
11685 * the "n_after" coefficients after the transformed range of dimensions
11686 * and the coefficients of the other divs in "bmap".
11688 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11689 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11694 isl_local_space
*ls
;
11699 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11703 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11704 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11705 for (i
= 0; i
< n_div
; ++i
) {
11706 int o_bmap
= 0, o_ls
= 0;
11708 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
11709 o_bmap
+= 1 + 1 + n_param
;
11710 o_ls
+= 1 + 1 + n_param
;
11711 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
11712 o_bmap
+= n_before
;
11713 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11714 ls
->div
->row
[i
] + o_ls
, n_set
);
11717 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
11719 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11720 ls
->div
->row
[i
] + o_ls
, n_div
);
11723 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
11724 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
11728 isl_local_space_free(ls
);
11731 isl_local_space_free(ls
);
11735 /* How many stride constraints does "ma" enforce?
11736 * That is, how many of the affine expressions have a denominator
11737 * different from one?
11739 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
11744 for (i
= 0; i
< ma
->n
; ++i
)
11745 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11751 /* For each affine expression in ma of the form
11753 * x_i = (f_i y + h_i)/m_i
11755 * with m_i different from one, add a constraint to "bmap"
11758 * f_i y + h_i = m_i alpha_i
11760 * with alpha_i an additional existentially quantified variable.
11762 static __isl_give isl_basic_map
*add_ma_strides(
11763 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
11764 int n_before
, int n_after
)
11773 total
= isl_basic_map_total_dim(bmap
);
11774 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
11775 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
11776 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
11777 for (i
= 0; i
< ma
->n
; ++i
) {
11778 int o_bmap
= 0, o_ma
= 1;
11780 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11782 div
= isl_basic_map_alloc_div(bmap
);
11783 k
= isl_basic_map_alloc_equality(bmap
);
11784 if (div
< 0 || k
< 0)
11786 isl_int_set_si(bmap
->div
[div
][0], 0);
11787 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11788 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
11789 o_bmap
+= 1 + n_param
;
11790 o_ma
+= 1 + n_param
;
11791 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
11792 o_bmap
+= n_before
;
11793 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11794 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
11797 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
11799 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11800 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
11803 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
11804 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
11810 isl_basic_map_free(bmap
);
11814 /* Replace the domain or range space (depending on "type) of "space" by "set".
11816 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
11817 enum isl_dim_type type
, __isl_take isl_space
*set
)
11819 if (type
== isl_dim_in
) {
11820 space
= isl_space_range(space
);
11821 space
= isl_space_map_from_domain_and_range(set
, space
);
11823 space
= isl_space_domain(space
);
11824 space
= isl_space_map_from_domain_and_range(space
, set
);
11830 /* Compute the preimage of the domain or range (depending on "type")
11831 * of "bmap" under the function represented by "ma".
11832 * In other words, plug in "ma" in the domain or range of "bmap".
11833 * The result is a basic map that lives in the same space as "bmap"
11834 * except that the domain or range has been replaced by
11835 * the domain space of "ma".
11837 * If bmap is represented by
11839 * A(p) + S u + B x + T v + C(divs) >= 0,
11841 * where u and x are input and output dimensions if type == isl_dim_out
11842 * while x and v are input and output dimensions if type == isl_dim_in,
11843 * and ma is represented by
11845 * x = D(p) + F(y) + G(divs')
11847 * then the result is
11849 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11851 * The divs in the input set are similarly adjusted.
11854 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11858 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11859 * B_i G(divs') + c_i(divs))/n_i)
11861 * If bmap is not a rational map and if F(y) involves any denominators
11863 * x_i = (f_i y + h_i)/m_i
11865 * then additional constraints are added to ensure that we only
11866 * map back integer points. That is we enforce
11868 * f_i y + h_i = m_i alpha_i
11870 * with alpha_i an additional existentially quantified variable.
11872 * We first copy over the divs from "ma".
11873 * Then we add the modified constraints and divs from "bmap".
11874 * Finally, we add the stride constraints, if needed.
11876 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
11877 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
11878 __isl_take isl_multi_aff
*ma
)
11882 isl_basic_map
*res
= NULL
;
11883 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
11884 isl_int f
, c1
, c2
, g
;
11885 int rational
, strides
;
11892 ma
= isl_multi_aff_align_divs(ma
);
11895 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
11898 if (type
== isl_dim_in
) {
11900 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
11902 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
11905 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
11906 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
11908 space
= isl_multi_aff_get_domain_space(ma
);
11909 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
11910 rational
= isl_basic_map_is_rational(bmap
);
11911 strides
= rational
? 0 : multi_aff_strides(ma
);
11912 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
11913 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
11915 res
= isl_basic_map_set_rational(res
);
11917 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
11918 if (isl_basic_map_alloc_div(res
) < 0)
11921 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
11924 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11925 k
= isl_basic_map_alloc_equality(res
);
11928 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
11929 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11932 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11933 k
= isl_basic_map_alloc_inequality(res
);
11936 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
11937 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11940 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11941 if (isl_int_is_zero(bmap
->div
[i
][0])) {
11942 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
11945 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
11946 n_before
, n_after
, n_div_ma
, n_div_bmap
,
11951 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
11957 isl_basic_map_free(bmap
);
11958 isl_multi_aff_free(ma
);
11959 res
= isl_basic_set_simplify(res
);
11960 return isl_basic_map_finalize(res
);
11966 isl_basic_map_free(bmap
);
11967 isl_multi_aff_free(ma
);
11968 isl_basic_map_free(res
);
11972 /* Compute the preimage of "bset" under the function represented by "ma".
11973 * In other words, plug in "ma" in "bset". The result is a basic set
11974 * that lives in the domain space of "ma".
11976 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
11977 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
11979 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
11982 /* Compute the preimage of the domain of "bmap" under the function
11983 * represented by "ma".
11984 * In other words, plug in "ma" in the domain of "bmap".
11985 * The result is a basic map that lives in the same space as "bmap"
11986 * except that the domain has been replaced by the domain space of "ma".
11988 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
11989 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
11991 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
11994 /* Compute the preimage of the range of "bmap" under the function
11995 * represented by "ma".
11996 * In other words, plug in "ma" in the range of "bmap".
11997 * The result is a basic map that lives in the same space as "bmap"
11998 * except that the range has been replaced by the domain space of "ma".
12000 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12001 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12003 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12006 /* Check if the range of "ma" is compatible with the domain or range
12007 * (depending on "type") of "map".
12008 * Return -1 if anything is wrong.
12010 static int check_map_compatible_range_multi_aff(
12011 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12012 __isl_keep isl_multi_aff
*ma
)
12015 isl_space
*ma_space
;
12017 ma_space
= isl_multi_aff_get_space(ma
);
12018 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
12019 isl_space_free(ma_space
);
12021 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12022 "spaces don't match", return -1);
12026 /* Compute the preimage of the domain or range (depending on "type")
12027 * of "map" under the function represented by "ma".
12028 * In other words, plug in "ma" in the domain or range of "map".
12029 * The result is a map that lives in the same space as "map"
12030 * except that the domain or range has been replaced by
12031 * the domain space of "ma".
12033 * The parameters are assumed to have been aligned.
12035 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12036 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12041 map
= isl_map_cow(map
);
12042 ma
= isl_multi_aff_align_divs(ma
);
12045 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12048 for (i
= 0; i
< map
->n
; ++i
) {
12049 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12050 isl_multi_aff_copy(ma
));
12055 space
= isl_multi_aff_get_domain_space(ma
);
12056 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12058 isl_space_free(map
->dim
);
12063 isl_multi_aff_free(ma
);
12065 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12066 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12069 isl_multi_aff_free(ma
);
12074 /* Compute the preimage of the domain or range (depending on "type")
12075 * of "map" under the function represented by "ma".
12076 * In other words, plug in "ma" in the domain or range of "map".
12077 * The result is a map that lives in the same space as "map"
12078 * except that the domain or range has been replaced by
12079 * the domain space of "ma".
12081 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12082 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12087 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12088 return map_preimage_multi_aff(map
, type
, ma
);
12090 if (!isl_space_has_named_params(map
->dim
) ||
12091 !isl_space_has_named_params(ma
->space
))
12092 isl_die(map
->ctx
, isl_error_invalid
,
12093 "unaligned unnamed parameters", goto error
);
12094 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12095 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12097 return map_preimage_multi_aff(map
, type
, ma
);
12099 isl_multi_aff_free(ma
);
12100 return isl_map_free(map
);
12103 /* Compute the preimage of "set" under the function represented by "ma".
12104 * In other words, plug in "ma" "set". The result is a set
12105 * that lives in the domain space of "ma".
12107 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12108 __isl_take isl_multi_aff
*ma
)
12110 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12113 /* Compute the preimage of the domain of "map" under the function
12114 * represented by "ma".
12115 * In other words, plug in "ma" in the domain of "map".
12116 * The result is a map that lives in the same space as "map"
12117 * except that the domain has been replaced by the domain space of "ma".
12119 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12120 __isl_take isl_multi_aff
*ma
)
12122 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12125 /* Compute the preimage of "map" under the function represented by "pma".
12126 * In other words, plug in "pma" in the domain or range of "map".
12127 * The result is a map that lives in the same space as "map",
12128 * except that the space of type "type" has been replaced by
12129 * the domain space of "pma".
12131 * The parameters of "map" and "pma" are assumed to have been aligned.
12133 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12134 __isl_take isl_map
*map
, enum isl_dim_type type
,
12135 __isl_take isl_pw_multi_aff
*pma
)
12144 isl_pw_multi_aff_free(pma
);
12145 res
= isl_map_empty(isl_map_get_space(map
));
12150 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12151 isl_multi_aff_copy(pma
->p
[0].maff
));
12152 if (type
== isl_dim_in
)
12153 res
= isl_map_intersect_domain(res
,
12154 isl_map_copy(pma
->p
[0].set
));
12156 res
= isl_map_intersect_range(res
,
12157 isl_map_copy(pma
->p
[0].set
));
12159 for (i
= 1; i
< pma
->n
; ++i
) {
12162 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12163 isl_multi_aff_copy(pma
->p
[i
].maff
));
12164 if (type
== isl_dim_in
)
12165 res_i
= isl_map_intersect_domain(res_i
,
12166 isl_map_copy(pma
->p
[i
].set
));
12168 res_i
= isl_map_intersect_range(res_i
,
12169 isl_map_copy(pma
->p
[i
].set
));
12170 res
= isl_map_union(res
, res_i
);
12173 isl_pw_multi_aff_free(pma
);
12177 isl_pw_multi_aff_free(pma
);
12182 /* Compute the preimage of "map" under the function represented by "pma".
12183 * In other words, plug in "pma" in the domain or range of "map".
12184 * The result is a map that lives in the same space as "map",
12185 * except that the space of type "type" has been replaced by
12186 * the domain space of "pma".
12188 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12189 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12194 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12195 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12197 if (!isl_space_has_named_params(map
->dim
) ||
12198 !isl_space_has_named_params(pma
->dim
))
12199 isl_die(map
->ctx
, isl_error_invalid
,
12200 "unaligned unnamed parameters", goto error
);
12201 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12202 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12204 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12206 isl_pw_multi_aff_free(pma
);
12207 return isl_map_free(map
);
12210 /* Compute the preimage of "set" under the function represented by "pma".
12211 * In other words, plug in "pma" in "set". The result is a set
12212 * that lives in the domain space of "pma".
12214 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12215 __isl_take isl_pw_multi_aff
*pma
)
12217 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12220 /* Compute the preimage of "map" under the function represented by "pma".
12221 * In other words, plug in "pma" in the domain "map".
12222 * The result is a map that lives in the same space as "map",
12223 * except that domain space has been replaced by the domain space of "pma".
12225 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12226 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12228 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12231 /* Compute the preimage of "map" under the function represented by "mpa".
12232 * In other words, plug in "mpa" in the domain or range of "map".
12233 * The result is a map that lives in the same space as "map",
12234 * except that the space of type "type" has been replaced by
12235 * the domain space of "mpa".
12237 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12238 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12240 isl_pw_multi_aff
*pma
;
12242 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12243 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12246 /* Compute the preimage of "map" under the function represented by "mpa".
12247 * In other words, plug in "mpa" in the domain "map".
12248 * The result is a map that lives in the same space as "map",
12249 * except that domain space has been replaced by the domain space of "mpa".
12251 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12252 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12254 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12257 /* Compute the preimage of "set" by the function represented by "mpa".
12258 * In other words, plug in "mpa" in "set".
12260 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12261 __isl_take isl_multi_pw_aff
*mpa
)
12263 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);