2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 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
);
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
);
440 /* Replace the identifier of the tuple by "id".
442 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
443 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
445 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
448 /* Does the input or output tuple have a name?
450 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
452 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
455 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
456 enum isl_dim_type type
)
458 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
461 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
464 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
467 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
468 enum isl_dim_type type
, __isl_take isl_id
*id
)
470 map
= isl_map_cow(map
);
474 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
476 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
482 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
483 __isl_take isl_id
*id
)
485 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
488 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
489 enum isl_dim_type type
)
491 map
= isl_map_cow(map
);
495 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
497 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
500 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
502 return isl_map_reset_tuple_id(set
, isl_dim_set
);
505 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
507 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
510 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
511 enum isl_dim_type type
)
513 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
516 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
518 return isl_map_has_tuple_id(set
, isl_dim_set
);
521 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
523 return isl_map_get_tuple_id(set
, isl_dim_set
);
526 /* Does the set tuple have a name?
528 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
530 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
534 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
536 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
539 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
541 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
544 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
545 enum isl_dim_type type
, unsigned pos
)
547 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
550 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
551 enum isl_dim_type type
, unsigned pos
)
553 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
556 /* Does the given dimension have a name?
558 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
559 enum isl_dim_type type
, unsigned pos
)
561 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
564 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
565 enum isl_dim_type type
, unsigned pos
)
567 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
570 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
571 enum isl_dim_type type
, unsigned pos
)
573 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
576 /* Does the given dimension have a name?
578 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
579 enum isl_dim_type type
, unsigned pos
)
581 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
584 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
585 __isl_take isl_basic_map
*bmap
,
586 enum isl_dim_type type
, unsigned pos
, const char *s
)
588 bmap
= isl_basic_map_cow(bmap
);
591 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
594 return isl_basic_map_finalize(bmap
);
596 isl_basic_map_free(bmap
);
600 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
601 enum isl_dim_type type
, unsigned pos
, const char *s
)
605 map
= isl_map_cow(map
);
609 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
613 for (i
= 0; i
< map
->n
; ++i
) {
614 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
625 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
626 __isl_take isl_basic_set
*bset
,
627 enum isl_dim_type type
, unsigned pos
, const char *s
)
629 return (isl_basic_set
*)isl_basic_map_set_dim_name(
630 (isl_basic_map
*)bset
, type
, pos
, s
);
633 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
634 enum isl_dim_type type
, unsigned pos
, const char *s
)
636 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
639 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
640 enum isl_dim_type type
, unsigned pos
)
642 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
645 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
646 enum isl_dim_type type
, unsigned pos
)
648 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
651 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
652 enum isl_dim_type type
, unsigned pos
)
654 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
657 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
658 enum isl_dim_type type
, unsigned pos
)
660 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
663 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
664 enum isl_dim_type type
, unsigned pos
)
666 return isl_map_has_dim_id(set
, type
, pos
);
669 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
670 enum isl_dim_type type
, unsigned pos
)
672 return isl_map_get_dim_id(set
, type
, pos
);
675 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
676 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
678 map
= isl_map_cow(map
);
682 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
684 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
690 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
691 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
693 return isl_map_set_dim_id(set
, type
, pos
, id
);
696 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
697 __isl_keep isl_id
*id
)
701 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
704 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
705 __isl_keep isl_id
*id
)
707 return isl_map_find_dim_by_id(set
, type
, id
);
710 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
715 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
718 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
721 return isl_map_find_dim_by_name(set
, type
, name
);
724 /* Reset the user pointer on all identifiers of parameters and tuples
725 * of the space of "map".
727 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
731 space
= isl_map_get_space(map
);
732 space
= isl_space_reset_user(space
);
733 map
= isl_map_reset_space(map
, space
);
738 /* Reset the user pointer on all identifiers of parameters and tuples
739 * of the space of "set".
741 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
743 return isl_map_reset_user(set
);
746 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
750 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
753 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
755 return isl_basic_map_is_rational(bset
);
758 /* Does "bmap" contain any rational points?
760 * If "bmap" has an equality for each dimension, equating the dimension
761 * to an integer constant, then it has no rational points, even if it
762 * is marked as rational.
764 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
766 int has_rational
= 1;
771 if (isl_basic_map_plain_is_empty(bmap
))
773 if (!isl_basic_map_is_rational(bmap
))
775 bmap
= isl_basic_map_copy(bmap
);
776 bmap
= isl_basic_map_implicit_equalities(bmap
);
779 total
= isl_basic_map_total_dim(bmap
);
780 if (bmap
->n_eq
== total
) {
782 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
783 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
786 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
787 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
789 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
797 isl_basic_map_free(bmap
);
802 /* Does "map" contain any rational points?
804 int isl_map_has_rational(__isl_keep isl_map
*map
)
811 for (i
= 0; i
< map
->n
; ++i
) {
812 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
813 if (has_rational
< 0)
821 /* Does "set" contain any rational points?
823 int isl_set_has_rational(__isl_keep isl_set
*set
)
825 return isl_map_has_rational(set
);
828 /* Is this basic set a parameter domain?
830 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
834 return isl_space_is_params(bset
->dim
);
837 /* Is this set a parameter domain?
839 int isl_set_is_params(__isl_keep isl_set
*set
)
843 return isl_space_is_params(set
->dim
);
846 /* Is this map actually a parameter domain?
847 * Users should never call this function. Outside of isl,
848 * a map can never be a parameter domain.
850 int isl_map_is_params(__isl_keep isl_map
*map
)
854 return isl_space_is_params(map
->dim
);
857 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
858 struct isl_basic_map
*bmap
, unsigned extra
,
859 unsigned n_eq
, unsigned n_ineq
)
862 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
867 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
868 if (isl_blk_is_error(bmap
->block
))
871 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
872 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
876 bmap
->block2
= isl_blk_empty();
879 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
880 if (isl_blk_is_error(bmap
->block2
))
883 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
888 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
889 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
891 for (i
= 0; i
< extra
; ++i
)
892 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
896 bmap
->c_size
= n_eq
+ n_ineq
;
897 bmap
->eq
= bmap
->ineq
+ n_ineq
;
906 isl_basic_map_free(bmap
);
910 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
911 unsigned nparam
, unsigned dim
, unsigned extra
,
912 unsigned n_eq
, unsigned n_ineq
)
914 struct isl_basic_map
*bmap
;
917 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
921 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
922 return (struct isl_basic_set
*)bmap
;
925 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
926 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
928 struct isl_basic_map
*bmap
;
931 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
932 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
933 return (struct isl_basic_set
*)bmap
;
939 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
940 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
942 struct isl_basic_map
*bmap
;
946 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
951 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
957 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
958 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
959 unsigned n_eq
, unsigned n_ineq
)
961 struct isl_basic_map
*bmap
;
964 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
968 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
972 static void dup_constraints(
973 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
976 unsigned total
= isl_basic_map_total_dim(src
);
978 for (i
= 0; i
< src
->n_eq
; ++i
) {
979 int j
= isl_basic_map_alloc_equality(dst
);
980 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
983 for (i
= 0; i
< src
->n_ineq
; ++i
) {
984 int j
= isl_basic_map_alloc_inequality(dst
);
985 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
988 for (i
= 0; i
< src
->n_div
; ++i
) {
989 int j
= isl_basic_map_alloc_div(dst
);
990 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
992 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
995 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
997 struct isl_basic_map
*dup
;
1001 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1002 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1005 dup_constraints(dup
, bmap
);
1006 dup
->flags
= bmap
->flags
;
1007 dup
->sample
= isl_vec_copy(bmap
->sample
);
1011 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1013 struct isl_basic_map
*dup
;
1015 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1016 return (struct isl_basic_set
*)dup
;
1019 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1024 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1028 return isl_basic_set_dup(bset
);
1031 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1040 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1045 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1049 bmap
= isl_basic_map_dup(bmap
);
1051 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1055 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1064 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1069 if (--bmap
->ref
> 0)
1072 isl_ctx_deref(bmap
->ctx
);
1074 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1076 isl_blk_free(bmap
->ctx
, bmap
->block
);
1077 isl_vec_free(bmap
->sample
);
1078 isl_space_free(bmap
->dim
);
1084 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1086 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1089 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1091 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1094 __isl_give isl_map
*isl_map_align_params_map_map_and(
1095 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1096 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1097 __isl_take isl_map
*map2
))
1101 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1102 return fn(map1
, map2
);
1103 if (!isl_space_has_named_params(map1
->dim
) ||
1104 !isl_space_has_named_params(map2
->dim
))
1105 isl_die(map1
->ctx
, isl_error_invalid
,
1106 "unaligned unnamed parameters", goto error
);
1107 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1108 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1109 return fn(map1
, map2
);
1116 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1117 __isl_keep isl_map
*map2
,
1118 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1124 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1125 return fn(map1
, map2
);
1126 if (!isl_space_has_named_params(map1
->dim
) ||
1127 !isl_space_has_named_params(map2
->dim
))
1128 isl_die(map1
->ctx
, isl_error_invalid
,
1129 "unaligned unnamed parameters", return -1);
1130 map1
= isl_map_copy(map1
);
1131 map2
= isl_map_copy(map2
);
1132 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1133 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1140 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1142 struct isl_ctx
*ctx
;
1146 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1147 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1149 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1150 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1151 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1152 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1153 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1154 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1156 int j
= isl_basic_map_alloc_inequality(bmap
);
1160 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1161 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1168 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1169 bmap
->extra
- bmap
->n_div
);
1170 return bmap
->n_eq
++;
1173 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1175 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1178 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1182 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1187 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1189 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1192 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1197 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1199 if (pos
!= bmap
->n_eq
- 1) {
1201 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1202 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1208 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1210 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1213 /* Turn inequality "pos" of "bmap" into an equality.
1215 * In particular, we move the inequality in front of the equalities
1216 * and move the last inequality in the position of the moved inequality.
1217 * Note that isl_tab_make_equalities_explicit depends on this particular
1218 * change in the ordering of the constraints.
1220 void isl_basic_map_inequality_to_equality(
1221 struct isl_basic_map
*bmap
, unsigned pos
)
1225 t
= bmap
->ineq
[pos
];
1226 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1227 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1232 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1233 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1234 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1235 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1238 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1240 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1243 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1245 struct isl_ctx
*ctx
;
1249 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1250 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1251 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1252 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1253 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1254 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1255 1 + isl_basic_map_total_dim(bmap
),
1256 bmap
->extra
- bmap
->n_div
);
1257 return bmap
->n_ineq
++;
1260 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1262 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1265 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1269 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1274 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1276 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1279 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1284 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1286 if (pos
!= bmap
->n_ineq
- 1) {
1287 t
= bmap
->ineq
[pos
];
1288 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1289 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1290 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1296 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1298 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1301 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1306 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1309 k
= isl_basic_map_alloc_equality(bmap
);
1312 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1315 isl_basic_map_free(bmap
);
1319 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1322 return (isl_basic_set
*)
1323 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1326 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1331 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1334 k
= isl_basic_map_alloc_inequality(bmap
);
1337 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1340 isl_basic_map_free(bmap
);
1344 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1347 return (isl_basic_set
*)
1348 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1351 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1355 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1356 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1357 1 + 1 + isl_basic_map_total_dim(bmap
),
1358 bmap
->extra
- bmap
->n_div
);
1359 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1360 return bmap
->n_div
++;
1363 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1365 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1368 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1372 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1377 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1379 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1382 /* Copy constraint from src to dst, putting the vars of src at offset
1383 * dim_off in dst and the divs of src at offset div_off in dst.
1384 * If both sets are actually map, then dim_off applies to the input
1387 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1388 struct isl_basic_map
*src_map
, isl_int
*src
,
1389 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1391 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1392 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1393 unsigned src_in
= isl_basic_map_n_in(src_map
);
1394 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1395 unsigned src_out
= isl_basic_map_n_out(src_map
);
1396 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1397 isl_int_set(dst
[0], src
[0]);
1398 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1399 if (dst_nparam
> src_nparam
)
1400 isl_seq_clr(dst
+1+src_nparam
,
1401 dst_nparam
- src_nparam
);
1402 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1403 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1405 isl_min(dst_in
-in_off
, src_in
));
1406 if (dst_in
-in_off
> src_in
)
1407 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1408 dst_in
- in_off
- src_in
);
1409 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1410 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1411 src
+1+src_nparam
+src_in
,
1412 isl_min(dst_out
-out_off
, src_out
));
1413 if (dst_out
-out_off
> src_out
)
1414 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1415 dst_out
- out_off
- src_out
);
1416 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1417 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1418 src
+1+src_nparam
+src_in
+src_out
,
1419 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1420 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1421 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1422 div_off
+src_map
->n_div
,
1423 dst_map
->n_div
- div_off
- src_map
->n_div
);
1426 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1427 struct isl_basic_map
*src_map
, isl_int
*src
,
1428 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1430 isl_int_set(dst
[0], src
[0]);
1431 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1434 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1435 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1440 if (!bmap1
|| !bmap2
)
1443 div_off
= bmap1
->n_div
;
1445 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1446 int i1
= isl_basic_map_alloc_equality(bmap1
);
1449 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1450 i_pos
, o_pos
, div_off
);
1453 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1454 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1457 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1458 i_pos
, o_pos
, div_off
);
1461 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1462 int i1
= isl_basic_map_alloc_div(bmap1
);
1465 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1466 i_pos
, o_pos
, div_off
);
1469 isl_basic_map_free(bmap2
);
1474 isl_basic_map_free(bmap1
);
1475 isl_basic_map_free(bmap2
);
1479 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1480 struct isl_basic_set
*bset2
, unsigned pos
)
1482 return (struct isl_basic_set
*)
1483 add_constraints((struct isl_basic_map
*)bset1
,
1484 (struct isl_basic_map
*)bset2
, 0, pos
);
1487 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1488 __isl_take isl_space
*dim
, unsigned extra
,
1489 unsigned n_eq
, unsigned n_ineq
)
1491 struct isl_basic_map
*ext
;
1501 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1502 base
->extra
>= base
->n_div
+ extra
;
1504 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1505 room_for_ineq(base
, n_ineq
)) {
1506 isl_space_free(dim
);
1510 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1511 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1512 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1513 extra
+= base
->extra
;
1515 n_ineq
+= base
->n_ineq
;
1517 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1523 ext
->sample
= isl_vec_copy(base
->sample
);
1524 flags
= base
->flags
;
1525 ext
= add_constraints(ext
, base
, 0, 0);
1528 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1534 isl_space_free(dim
);
1535 isl_basic_map_free(base
);
1539 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1540 __isl_take isl_space
*dim
, unsigned extra
,
1541 unsigned n_eq
, unsigned n_ineq
)
1543 return (struct isl_basic_set
*)
1544 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1545 extra
, n_eq
, n_ineq
);
1548 struct isl_basic_map
*isl_basic_map_extend_constraints(
1549 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1553 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1557 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1558 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1559 unsigned n_eq
, unsigned n_ineq
)
1561 struct isl_basic_map
*bmap
;
1566 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1570 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1573 isl_basic_map_free(base
);
1577 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1578 unsigned nparam
, unsigned dim
, unsigned extra
,
1579 unsigned n_eq
, unsigned n_ineq
)
1581 return (struct isl_basic_set
*)
1582 isl_basic_map_extend((struct isl_basic_map
*)base
,
1583 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1586 struct isl_basic_set
*isl_basic_set_extend_constraints(
1587 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1589 return (struct isl_basic_set
*)
1590 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1594 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1596 return (struct isl_basic_set
*)
1597 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1600 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1605 if (bmap
->ref
> 1) {
1607 bmap
= isl_basic_map_dup(bmap
);
1610 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1614 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1622 return isl_set_dup(set
);
1625 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1633 return isl_map_dup(map
);
1636 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1637 unsigned a_len
, unsigned b_len
)
1639 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1640 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1641 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1644 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1645 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1653 isl_assert(bmap
->ctx
,
1654 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1656 if (n1
== 0 || n2
== 0)
1659 bmap
= isl_basic_map_cow(bmap
);
1663 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1664 if (isl_blk_is_error(blk
))
1667 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1669 bmap
->eq
[i
] + pos
, n1
, n2
);
1671 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1673 bmap
->ineq
[i
] + pos
, n1
, n2
);
1675 for (i
= 0; i
< bmap
->n_div
; ++i
)
1677 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1679 isl_blk_free(bmap
->ctx
, blk
);
1681 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1682 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1683 return isl_basic_map_finalize(bmap
);
1685 isl_basic_map_free(bmap
);
1689 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1690 __isl_take isl_basic_set
*bset
, unsigned n
)
1698 nparam
= isl_basic_set_n_param(bset
);
1699 dim
= isl_basic_set_n_dim(bset
);
1700 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1702 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1704 isl_basic_set_free(bset
);
1708 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1714 total
= isl_basic_map_total_dim(bmap
);
1715 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1716 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1718 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1720 i
= isl_basic_map_alloc_equality(bmap
);
1724 isl_int_set_si(bmap
->eq
[i
][0], 1);
1725 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1726 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1727 isl_vec_free(bmap
->sample
);
1728 bmap
->sample
= NULL
;
1729 return isl_basic_map_finalize(bmap
);
1731 isl_basic_map_free(bmap
);
1735 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1737 return (struct isl_basic_set
*)
1738 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1741 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1744 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1746 isl_int
*t
= bmap
->div
[a
];
1747 bmap
->div
[a
] = bmap
->div
[b
];
1751 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1752 * div definitions accordingly.
1754 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1757 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1759 swap_div(bmap
, a
, b
);
1761 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1762 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1764 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1765 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1767 for (i
= 0; i
< bmap
->n_div
; ++i
)
1768 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1769 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1772 /* Eliminate the specified n dimensions starting at first from the
1773 * constraints, without removing the dimensions from the space.
1774 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1776 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1777 enum isl_dim_type type
, unsigned first
, unsigned n
)
1786 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1787 isl_die(map
->ctx
, isl_error_invalid
,
1788 "index out of bounds", goto error
);
1790 map
= isl_map_cow(map
);
1794 for (i
= 0; i
< map
->n
; ++i
) {
1795 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1805 /* Eliminate the specified n dimensions starting at first from the
1806 * constraints, without removing the dimensions from the space.
1807 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1809 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1810 enum isl_dim_type type
, unsigned first
, unsigned n
)
1812 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1815 /* Eliminate the specified n dimensions starting at first from the
1816 * constraints, without removing the dimensions from the space.
1817 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1819 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1820 unsigned first
, unsigned n
)
1822 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1825 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1826 __isl_take isl_basic_map
*bmap
)
1830 bmap
= isl_basic_map_eliminate_vars(bmap
,
1831 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1835 return isl_basic_map_finalize(bmap
);
1838 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1839 __isl_take isl_basic_set
*bset
)
1841 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1842 (struct isl_basic_map
*)bset
);
1845 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1854 map
= isl_map_cow(map
);
1858 for (i
= 0; i
< map
->n
; ++i
) {
1859 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1869 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1871 return isl_map_remove_divs(set
);
1874 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1875 enum isl_dim_type type
, unsigned first
, unsigned n
)
1879 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1881 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1883 bmap
= isl_basic_map_eliminate_vars(bmap
,
1884 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1887 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1889 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1892 isl_basic_map_free(bmap
);
1896 /* Return true if the definition of the given div (recursively) involves
1897 * any of the given variables.
1899 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1900 unsigned first
, unsigned n
)
1903 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1905 if (isl_int_is_zero(bmap
->div
[div
][0]))
1907 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1910 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1911 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1913 if (div_involves_vars(bmap
, i
, first
, n
))
1920 /* Try and add a lower and/or upper bound on "div" to "bmap"
1921 * based on inequality "i".
1922 * "total" is the total number of variables (excluding the divs).
1923 * "v" is a temporary object that can be used during the calculations.
1924 * If "lb" is set, then a lower bound should be constructed.
1925 * If "ub" is set, then an upper bound should be constructed.
1927 * The calling function has already checked that the inequality does not
1928 * reference "div", but we still need to check that the inequality is
1929 * of the right form. We'll consider the case where we want to construct
1930 * a lower bound. The construction of upper bounds is similar.
1932 * Let "div" be of the form
1934 * q = floor((a + f(x))/d)
1936 * We essentially check if constraint "i" is of the form
1940 * so that we can use it to derive a lower bound on "div".
1941 * However, we allow a slightly more general form
1945 * with the condition that the coefficients of g(x) - f(x) are all
1947 * Rewriting this constraint as
1951 * adding a + f(x) to both sides and dividing by d, we obtain
1953 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1955 * Taking the floor on both sides, we obtain
1957 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1961 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1963 * In the case of an upper bound, we construct the constraint
1965 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1968 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1969 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1970 unsigned total
, isl_int v
, int lb
, int ub
)
1974 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1976 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1977 bmap
->div
[div
][1 + 1 + j
]);
1978 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1981 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1982 bmap
->div
[div
][1 + 1 + j
]);
1983 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1989 bmap
= isl_basic_map_cow(bmap
);
1990 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1992 int k
= isl_basic_map_alloc_inequality(bmap
);
1995 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1996 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1997 bmap
->div
[div
][1 + j
]);
1998 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1999 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2001 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2004 int k
= isl_basic_map_alloc_inequality(bmap
);
2007 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2008 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2009 bmap
->div
[div
][1 + j
]);
2010 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2011 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2013 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2016 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2019 isl_basic_map_free(bmap
);
2023 /* This function is called right before "div" is eliminated from "bmap"
2024 * using Fourier-Motzkin.
2025 * Look through the constraints of "bmap" for constraints on the argument
2026 * of the integer division and use them to construct constraints on the
2027 * integer division itself. These constraints can then be combined
2028 * during the Fourier-Motzkin elimination.
2029 * Note that it is only useful to introduce lower bounds on "div"
2030 * if "bmap" already contains upper bounds on "div" as the newly
2031 * introduce lower bounds can then be combined with the pre-existing
2032 * upper bounds. Similarly for upper bounds.
2033 * We therefore first check if "bmap" contains any lower and/or upper bounds
2036 * It is interesting to note that the introduction of these constraints
2037 * can indeed lead to more accurate results, even when compared to
2038 * deriving constraints on the argument of "div" from constraints on "div".
2039 * Consider, for example, the set
2041 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2043 * The second constraint can be rewritten as
2045 * 2 * [(-i-2j+3)/4] + k >= 0
2047 * from which we can derive
2049 * -i - 2j + 3 >= -2k
2055 * Combined with the first constraint, we obtain
2057 * -3 <= 3 + 2k or k >= -3
2059 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2060 * the first constraint, we obtain
2062 * [(i + 2j)/4] >= [-3/4] = -1
2064 * Combining this constraint with the second constraint, we obtain
2068 static __isl_give isl_basic_map
*insert_bounds_on_div(
2069 __isl_take isl_basic_map
*bmap
, int div
)
2072 int check_lb
, check_ub
;
2079 if (isl_int_is_zero(bmap
->div
[div
][0]))
2082 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2086 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2087 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2094 if (!check_lb
&& !check_ub
)
2099 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2100 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2103 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2104 check_lb
, check_ub
);
2112 /* Remove all divs (recursively) involving any of the given dimensions
2113 * in their definitions.
2115 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2116 __isl_take isl_basic_map
*bmap
,
2117 enum isl_dim_type type
, unsigned first
, unsigned n
)
2123 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2125 first
+= isl_basic_map_offset(bmap
, type
);
2127 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2128 if (!div_involves_vars(bmap
, i
, first
, n
))
2130 bmap
= insert_bounds_on_div(bmap
, i
);
2131 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2139 isl_basic_map_free(bmap
);
2143 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2144 __isl_take isl_basic_set
*bset
,
2145 enum isl_dim_type type
, unsigned first
, unsigned n
)
2147 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2150 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2151 enum isl_dim_type type
, unsigned first
, unsigned n
)
2160 map
= isl_map_cow(map
);
2164 for (i
= 0; i
< map
->n
; ++i
) {
2165 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2176 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2177 enum isl_dim_type type
, unsigned first
, unsigned n
)
2179 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2183 /* Does the desciption of "bmap" depend on the specified dimensions?
2184 * We also check whether the dimensions appear in any of the div definitions.
2185 * In principle there is no need for this check. If the dimensions appear
2186 * in a div definition, they also appear in the defining constraints of that
2189 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2190 enum isl_dim_type type
, unsigned first
, unsigned n
)
2197 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2198 isl_die(bmap
->ctx
, isl_error_invalid
,
2199 "index out of bounds", return -1);
2201 first
+= isl_basic_map_offset(bmap
, type
);
2202 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2203 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2205 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2206 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2208 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2209 if (isl_int_is_zero(bmap
->div
[i
][0]))
2211 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2218 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2219 enum isl_dim_type type
, unsigned first
, unsigned n
)
2226 if (first
+ n
> isl_map_dim(map
, type
))
2227 isl_die(map
->ctx
, isl_error_invalid
,
2228 "index out of bounds", return -1);
2230 for (i
= 0; i
< map
->n
; ++i
) {
2231 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2233 if (involves
< 0 || involves
)
2240 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2241 enum isl_dim_type type
, unsigned first
, unsigned n
)
2243 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2246 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2247 enum isl_dim_type type
, unsigned first
, unsigned n
)
2249 return isl_map_involves_dims(set
, type
, first
, n
);
2252 /* Return true if the definition of the given div is unknown or depends
2255 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2258 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2260 if (isl_int_is_zero(bmap
->div
[div
][0]))
2263 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2264 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2266 if (div_is_unknown(bmap
, i
))
2273 /* Remove all divs that are unknown or defined in terms of unknown divs.
2275 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2276 __isl_take isl_basic_map
*bmap
)
2283 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2284 if (!div_is_unknown(bmap
, i
))
2286 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2295 /* Remove all divs that are unknown or defined in terms of unknown divs.
2297 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2298 __isl_take isl_basic_set
*bset
)
2300 return isl_basic_map_remove_unknown_divs(bset
);
2303 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2312 map
= isl_map_cow(map
);
2316 for (i
= 0; i
< map
->n
; ++i
) {
2317 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2327 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2329 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2332 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2333 __isl_take isl_basic_set
*bset
,
2334 enum isl_dim_type type
, unsigned first
, unsigned n
)
2336 return (isl_basic_set
*)
2337 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2340 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2341 enum isl_dim_type type
, unsigned first
, unsigned n
)
2348 map
= isl_map_cow(map
);
2351 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2353 for (i
= 0; i
< map
->n
; ++i
) {
2354 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2355 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2359 map
= isl_map_drop(map
, type
, first
, n
);
2366 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2367 enum isl_dim_type type
, unsigned first
, unsigned n
)
2369 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2372 /* Project out n inputs starting at first using Fourier-Motzkin */
2373 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2374 unsigned first
, unsigned n
)
2376 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2379 static void dump_term(struct isl_basic_map
*bmap
,
2380 isl_int c
, int pos
, FILE *out
)
2383 unsigned in
= isl_basic_map_n_in(bmap
);
2384 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2385 unsigned nparam
= isl_basic_map_n_param(bmap
);
2387 isl_int_print(out
, c
, 0);
2389 if (!isl_int_is_one(c
))
2390 isl_int_print(out
, c
, 0);
2391 if (pos
< 1 + nparam
) {
2392 name
= isl_space_get_dim_name(bmap
->dim
,
2393 isl_dim_param
, pos
- 1);
2395 fprintf(out
, "%s", name
);
2397 fprintf(out
, "p%d", pos
- 1);
2398 } else if (pos
< 1 + nparam
+ in
)
2399 fprintf(out
, "i%d", pos
- 1 - nparam
);
2400 else if (pos
< 1 + nparam
+ dim
)
2401 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2403 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2407 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2408 int sign
, FILE *out
)
2412 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2416 for (i
= 0, first
= 1; i
< len
; ++i
) {
2417 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2420 fprintf(out
, " + ");
2422 isl_int_abs(v
, c
[i
]);
2423 dump_term(bmap
, v
, i
, out
);
2430 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2431 const char *op
, FILE *out
, int indent
)
2435 fprintf(out
, "%*s", indent
, "");
2437 dump_constraint_sign(bmap
, c
, 1, out
);
2438 fprintf(out
, " %s ", op
);
2439 dump_constraint_sign(bmap
, c
, -1, out
);
2443 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2444 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2446 fprintf(out
, "%*s", indent
, "");
2447 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2452 static void dump_constraints(struct isl_basic_map
*bmap
,
2453 isl_int
**c
, unsigned n
,
2454 const char *op
, FILE *out
, int indent
)
2458 for (i
= 0; i
< n
; ++i
)
2459 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2462 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2466 unsigned total
= isl_basic_map_total_dim(bmap
);
2468 for (j
= 0; j
< 1 + total
; ++j
) {
2469 if (isl_int_is_zero(exp
[j
]))
2471 if (!first
&& isl_int_is_pos(exp
[j
]))
2473 dump_term(bmap
, exp
[j
], j
, out
);
2478 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2482 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2483 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2485 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2486 fprintf(out
, "%*s", indent
, "");
2487 fprintf(out
, "e%d = [(", i
);
2488 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2490 isl_int_print(out
, bmap
->div
[i
][0], 0);
2491 fprintf(out
, "]\n");
2495 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2496 FILE *out
, int indent
)
2499 fprintf(out
, "null basic set\n");
2503 fprintf(out
, "%*s", indent
, "");
2504 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2505 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2506 bset
->extra
, bset
->flags
);
2507 dump((struct isl_basic_map
*)bset
, out
, indent
);
2510 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2511 FILE *out
, int indent
)
2514 fprintf(out
, "null basic map\n");
2518 fprintf(out
, "%*s", indent
, "");
2519 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2520 "flags: %x, n_name: %d\n",
2522 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2523 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2524 dump(bmap
, out
, indent
);
2527 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2532 total
= isl_basic_map_total_dim(bmap
);
2533 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2534 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2535 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2536 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2540 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2543 struct isl_set
*set
;
2547 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2548 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2549 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2550 sizeof(struct isl_set
) +
2551 (n
- 1) * sizeof(struct isl_basic_set
*));
2555 set
->ctx
= dim
->ctx
;
2556 isl_ctx_ref(set
->ctx
);
2564 isl_space_free(dim
);
2568 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2569 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2571 struct isl_set
*set
;
2574 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2578 set
= isl_set_alloc_space(dims
, n
, flags
);
2582 /* Make sure "map" has room for at least "n" more basic maps.
2584 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2587 struct isl_map
*grown
= NULL
;
2591 isl_assert(map
->ctx
, n
>= 0, goto error
);
2592 if (map
->n
+ n
<= map
->size
)
2594 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2597 for (i
= 0; i
< map
->n
; ++i
) {
2598 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2606 isl_map_free(grown
);
2611 /* Make sure "set" has room for at least "n" more basic sets.
2613 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2615 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2618 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2621 struct isl_set
*dup
;
2626 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2629 for (i
= 0; i
< set
->n
; ++i
)
2630 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2634 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2636 return isl_map_from_basic_map(bset
);
2639 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2641 struct isl_map
*map
;
2646 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2647 return isl_map_add_basic_map(map
, bmap
);
2650 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2651 __isl_take isl_basic_set
*bset
)
2653 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2654 (struct isl_basic_map
*)bset
);
2657 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2667 isl_ctx_deref(set
->ctx
);
2668 for (i
= 0; i
< set
->n
; ++i
)
2669 isl_basic_set_free(set
->p
[i
]);
2670 isl_space_free(set
->dim
);
2676 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2681 fprintf(out
, "null set\n");
2685 fprintf(out
, "%*s", indent
, "");
2686 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2687 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2689 for (i
= 0; i
< set
->n
; ++i
) {
2690 fprintf(out
, "%*s", indent
, "");
2691 fprintf(out
, "basic set %d:\n", i
);
2692 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2696 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2701 fprintf(out
, "null map\n");
2705 fprintf(out
, "%*s", indent
, "");
2706 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2707 "flags: %x, n_name: %d\n",
2708 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2709 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2710 for (i
= 0; i
< map
->n
; ++i
) {
2711 fprintf(out
, "%*s", indent
, "");
2712 fprintf(out
, "basic map %d:\n", i
);
2713 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2717 struct isl_basic_map
*isl_basic_map_intersect_domain(
2718 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2720 struct isl_basic_map
*bmap_domain
;
2725 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2726 bset
->dim
, isl_dim_param
), goto error
);
2728 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2729 isl_assert(bset
->ctx
,
2730 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2732 bmap
= isl_basic_map_cow(bmap
);
2735 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2736 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2737 bmap_domain
= isl_basic_map_from_domain(bset
);
2738 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2740 bmap
= isl_basic_map_simplify(bmap
);
2741 return isl_basic_map_finalize(bmap
);
2743 isl_basic_map_free(bmap
);
2744 isl_basic_set_free(bset
);
2748 struct isl_basic_map
*isl_basic_map_intersect_range(
2749 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2751 struct isl_basic_map
*bmap_range
;
2756 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2757 bset
->dim
, isl_dim_param
), goto error
);
2759 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2760 isl_assert(bset
->ctx
,
2761 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2763 if (isl_basic_set_is_universe(bset
)) {
2764 isl_basic_set_free(bset
);
2768 bmap
= isl_basic_map_cow(bmap
);
2771 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2772 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2773 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2774 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2776 bmap
= isl_basic_map_simplify(bmap
);
2777 return isl_basic_map_finalize(bmap
);
2779 isl_basic_map_free(bmap
);
2780 isl_basic_set_free(bset
);
2784 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2793 total
= 1 + isl_basic_map_total_dim(bmap
);
2794 if (total
!= vec
->size
)
2799 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2800 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2801 if (!isl_int_is_zero(s
)) {
2807 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2808 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2809 if (isl_int_is_neg(s
)) {
2820 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2822 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2825 struct isl_basic_map
*isl_basic_map_intersect(
2826 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2828 struct isl_vec
*sample
= NULL
;
2830 if (!bmap1
|| !bmap2
)
2833 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2834 bmap2
->dim
, isl_dim_param
), goto error
);
2835 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2836 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2837 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2838 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2839 return isl_basic_map_intersect(bmap2
, bmap1
);
2841 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2842 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2843 isl_assert(bmap1
->ctx
,
2844 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2846 if (bmap1
->sample
&&
2847 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2848 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2849 sample
= isl_vec_copy(bmap1
->sample
);
2850 else if (bmap2
->sample
&&
2851 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2852 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2853 sample
= isl_vec_copy(bmap2
->sample
);
2855 bmap1
= isl_basic_map_cow(bmap1
);
2858 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2859 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2860 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2863 isl_vec_free(sample
);
2865 isl_vec_free(bmap1
->sample
);
2866 bmap1
->sample
= sample
;
2869 bmap1
= isl_basic_map_simplify(bmap1
);
2870 return isl_basic_map_finalize(bmap1
);
2873 isl_vec_free(sample
);
2874 isl_basic_map_free(bmap1
);
2875 isl_basic_map_free(bmap2
);
2879 struct isl_basic_set
*isl_basic_set_intersect(
2880 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2882 return (struct isl_basic_set
*)
2883 isl_basic_map_intersect(
2884 (struct isl_basic_map
*)bset1
,
2885 (struct isl_basic_map
*)bset2
);
2888 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2889 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2891 return isl_basic_set_intersect(bset1
, bset2
);
2894 /* Special case of isl_map_intersect, where both map1 and map2
2895 * are convex, without any divs and such that either map1 or map2
2896 * contains a single constraint. This constraint is then simply
2897 * added to the other map.
2899 static __isl_give isl_map
*map_intersect_add_constraint(
2900 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2902 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2903 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2904 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2905 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2907 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2908 return isl_map_intersect(map2
, map1
);
2910 isl_assert(map2
->ctx
,
2911 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2913 map1
= isl_map_cow(map1
);
2916 if (isl_map_plain_is_empty(map1
)) {
2920 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2921 if (map2
->p
[0]->n_eq
== 1)
2922 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2924 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2925 map2
->p
[0]->ineq
[0]);
2927 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2928 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2932 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2933 isl_basic_map_free(map1
->p
[0]);
2946 /* map2 may be either a parameter domain or a map living in the same
2949 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2950 __isl_take isl_map
*map2
)
2959 if ((isl_map_plain_is_empty(map1
) ||
2960 isl_map_plain_is_universe(map2
)) &&
2961 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2965 if ((isl_map_plain_is_empty(map2
) ||
2966 isl_map_plain_is_universe(map1
)) &&
2967 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2972 if (map1
->n
== 1 && map2
->n
== 1 &&
2973 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2974 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2975 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2976 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2977 return map_intersect_add_constraint(map1
, map2
);
2979 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2980 isl_space_dim(map2
->dim
, isl_dim_param
))
2981 isl_assert(map1
->ctx
,
2982 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2984 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2985 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2986 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2988 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2989 map1
->n
* map2
->n
, flags
);
2992 for (i
= 0; i
< map1
->n
; ++i
)
2993 for (j
= 0; j
< map2
->n
; ++j
) {
2994 struct isl_basic_map
*part
;
2995 part
= isl_basic_map_intersect(
2996 isl_basic_map_copy(map1
->p
[i
]),
2997 isl_basic_map_copy(map2
->p
[j
]));
2998 if (isl_basic_map_is_empty(part
) < 0)
2999 part
= isl_basic_map_free(part
);
3000 result
= isl_map_add_basic_map(result
, part
);
3013 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3014 __isl_take isl_map
*map2
)
3018 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3019 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3020 "spaces don't match", goto error
);
3021 return map_intersect_internal(map1
, map2
);
3028 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3029 __isl_take isl_map
*map2
)
3031 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3034 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3036 return (struct isl_set
*)
3037 isl_map_intersect((struct isl_map
*)set1
,
3038 (struct isl_map
*)set2
);
3041 /* map_intersect_internal accepts intersections
3042 * with parameter domains, so we can just call that function.
3044 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3045 __isl_take isl_set
*params
)
3047 return map_intersect_internal(map
, params
);
3050 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3051 __isl_take isl_map
*map2
)
3053 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3056 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3057 __isl_take isl_set
*params
)
3059 return isl_map_intersect_params(set
, params
);
3062 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3065 struct isl_basic_set
*bset
;
3070 bmap
= isl_basic_map_cow(bmap
);
3073 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3074 in
= isl_basic_map_n_in(bmap
);
3075 bset
= isl_basic_set_from_basic_map(bmap
);
3076 bset
= isl_basic_set_swap_vars(bset
, in
);
3077 return isl_basic_map_from_basic_set(bset
, dim
);
3080 static __isl_give isl_basic_map
*basic_map_space_reset(
3081 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3087 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3090 space
= isl_basic_map_get_space(bmap
);
3091 space
= isl_space_reset(space
, type
);
3092 bmap
= isl_basic_map_reset_space(bmap
, space
);
3096 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3097 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3098 unsigned pos
, unsigned n
)
3101 struct isl_basic_map
*res
;
3102 struct isl_dim_map
*dim_map
;
3103 unsigned total
, off
;
3104 enum isl_dim_type t
;
3107 return basic_map_space_reset(bmap
, type
);
3112 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3114 total
= isl_basic_map_total_dim(bmap
) + n
;
3115 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3117 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3119 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3121 unsigned size
= isl_basic_map_dim(bmap
, t
);
3122 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3124 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3125 pos
, size
- pos
, off
+ pos
+ n
);
3127 off
+= isl_space_dim(res_dim
, t
);
3129 isl_dim_map_div(dim_map
, bmap
, off
);
3131 res
= isl_basic_map_alloc_space(res_dim
,
3132 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3133 if (isl_basic_map_is_rational(bmap
))
3134 res
= isl_basic_map_set_rational(res
);
3135 if (isl_basic_map_plain_is_empty(bmap
)) {
3136 isl_basic_map_free(bmap
);
3138 return isl_basic_map_set_to_empty(res
);
3140 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3141 return isl_basic_map_finalize(res
);
3144 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3145 __isl_take isl_basic_set
*bset
,
3146 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3148 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3151 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3152 enum isl_dim_type type
, unsigned n
)
3156 return isl_basic_map_insert_dims(bmap
, type
,
3157 isl_basic_map_dim(bmap
, type
), n
);
3160 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3161 enum isl_dim_type type
, unsigned n
)
3165 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3166 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3168 isl_basic_set_free(bset
);
3172 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3173 enum isl_dim_type type
)
3177 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3180 space
= isl_map_get_space(map
);
3181 space
= isl_space_reset(space
, type
);
3182 map
= isl_map_reset_space(map
, space
);
3186 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3187 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3192 return map_space_reset(map
, type
);
3194 map
= isl_map_cow(map
);
3198 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3202 for (i
= 0; i
< map
->n
; ++i
) {
3203 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3214 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3215 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3217 return isl_map_insert_dims(set
, type
, pos
, n
);
3220 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3221 enum isl_dim_type type
, unsigned n
)
3225 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3228 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3229 enum isl_dim_type type
, unsigned n
)
3233 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3234 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3240 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3241 __isl_take isl_basic_map
*bmap
,
3242 enum isl_dim_type dst_type
, unsigned dst_pos
,
3243 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3245 struct isl_dim_map
*dim_map
;
3246 struct isl_basic_map
*res
;
3247 enum isl_dim_type t
;
3248 unsigned total
, off
;
3255 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3258 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3261 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3263 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3264 pos(bmap
->dim
, src_type
) + src_pos
+
3265 ((src_type
< dst_type
) ? n
: 0)) {
3266 bmap
= isl_basic_map_cow(bmap
);
3270 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3271 src_type
, src_pos
, n
);
3275 bmap
= isl_basic_map_finalize(bmap
);
3280 total
= isl_basic_map_total_dim(bmap
);
3281 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3284 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3285 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3286 if (t
== dst_type
) {
3287 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3290 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3293 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3294 dst_pos
, size
- dst_pos
, off
);
3295 off
+= size
- dst_pos
;
3296 } else if (t
== src_type
) {
3297 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3300 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3301 src_pos
+ n
, size
- src_pos
- n
, off
);
3302 off
+= size
- src_pos
- n
;
3304 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3308 isl_dim_map_div(dim_map
, bmap
, off
);
3310 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3311 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3312 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3316 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3317 src_type
, src_pos
, n
);
3321 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3322 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3323 bmap
= isl_basic_map_finalize(bmap
);
3327 isl_basic_map_free(bmap
);
3331 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3332 enum isl_dim_type dst_type
, unsigned dst_pos
,
3333 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3335 return (isl_basic_set
*)isl_basic_map_move_dims(
3336 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3339 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3340 enum isl_dim_type dst_type
, unsigned dst_pos
,
3341 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3345 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3346 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3347 src_type
, src_pos
, n
);
3353 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3354 enum isl_dim_type dst_type
, unsigned dst_pos
,
3355 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3364 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3367 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3370 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3372 map
= isl_map_cow(map
);
3376 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3380 for (i
= 0; i
< map
->n
; ++i
) {
3381 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3383 src_type
, src_pos
, n
);
3394 /* Move the specified dimensions to the last columns right before
3395 * the divs. Don't change the dimension specification of bmap.
3396 * That's the responsibility of the caller.
3398 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3399 enum isl_dim_type type
, unsigned first
, unsigned n
)
3401 struct isl_dim_map
*dim_map
;
3402 struct isl_basic_map
*res
;
3403 enum isl_dim_type t
;
3404 unsigned total
, off
;
3408 if (pos(bmap
->dim
, type
) + first
+ n
==
3409 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3412 total
= isl_basic_map_total_dim(bmap
);
3413 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3416 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3417 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3419 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3422 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3423 first
, n
, total
- bmap
->n_div
- n
);
3424 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3425 first
+ n
, size
- (first
+ n
), off
);
3426 off
+= size
- (first
+ n
);
3428 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3432 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3434 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3435 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3436 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3440 /* Insert "n" rows in the divs of "bmap".
3442 * The number of columns is not changed, which means that the last
3443 * dimensions of "bmap" are being reintepreted as the new divs.
3444 * The space of "bmap" is not adjusted, however, which means
3445 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3446 * from the space of "bmap" is the responsibility of the caller.
3448 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3456 bmap
= isl_basic_map_cow(bmap
);
3460 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3461 old
= bmap
->block2
.data
;
3462 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3463 (bmap
->extra
+ n
) * (1 + row_size
));
3464 if (!bmap
->block2
.data
)
3465 return isl_basic_map_free(bmap
);
3466 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3468 return isl_basic_map_free(bmap
);
3469 for (i
= 0; i
< n
; ++i
) {
3470 new_div
[i
] = bmap
->block2
.data
+
3471 (bmap
->extra
+ i
) * (1 + row_size
);
3472 isl_seq_clr(new_div
[i
], 1 + row_size
);
3474 for (i
= 0; i
< bmap
->extra
; ++i
)
3475 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3477 bmap
->div
= new_div
;
3484 /* Turn the n dimensions of type type, starting at first
3485 * into existentially quantified variables.
3487 __isl_give isl_basic_map
*isl_basic_map_project_out(
3488 __isl_take isl_basic_map
*bmap
,
3489 enum isl_dim_type type
, unsigned first
, unsigned n
)
3492 return basic_map_space_reset(bmap
, type
);
3497 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3498 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3500 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3503 bmap
= move_last(bmap
, type
, first
, n
);
3504 bmap
= isl_basic_map_cow(bmap
);
3505 bmap
= insert_div_rows(bmap
, n
);
3509 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3512 bmap
= isl_basic_map_simplify(bmap
);
3513 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3514 return isl_basic_map_finalize(bmap
);
3516 isl_basic_map_free(bmap
);
3520 /* Turn the n dimensions of type type, starting at first
3521 * into existentially quantified variables.
3523 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3524 enum isl_dim_type type
, unsigned first
, unsigned n
)
3526 return (isl_basic_set
*)isl_basic_map_project_out(
3527 (isl_basic_map
*)bset
, type
, first
, n
);
3530 /* Turn the n dimensions of type type, starting at first
3531 * into existentially quantified variables.
3533 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3534 enum isl_dim_type type
, unsigned first
, unsigned n
)
3542 return map_space_reset(map
, type
);
3544 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3546 map
= isl_map_cow(map
);
3550 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3554 for (i
= 0; i
< map
->n
; ++i
) {
3555 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3566 /* Turn the n dimensions of type type, starting at first
3567 * into existentially quantified variables.
3569 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3570 enum isl_dim_type type
, unsigned first
, unsigned n
)
3572 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3575 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3579 for (i
= 0; i
< n
; ++i
) {
3580 j
= isl_basic_map_alloc_div(bmap
);
3583 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3587 isl_basic_map_free(bmap
);
3591 struct isl_basic_map
*isl_basic_map_apply_range(
3592 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3594 isl_space
*dim_result
= NULL
;
3595 struct isl_basic_map
*bmap
;
3596 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3597 struct isl_dim_map
*dim_map1
, *dim_map2
;
3599 if (!bmap1
|| !bmap2
)
3601 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3602 bmap2
->dim
, isl_dim_param
))
3603 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3604 "parameters don't match", goto error
);
3605 if (!isl_space_tuple_match(bmap1
->dim
, isl_dim_out
,
3606 bmap2
->dim
, isl_dim_in
))
3607 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3608 "spaces don't match", goto error
);
3610 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3611 isl_space_copy(bmap2
->dim
));
3613 n_in
= isl_basic_map_n_in(bmap1
);
3614 n_out
= isl_basic_map_n_out(bmap2
);
3615 n
= isl_basic_map_n_out(bmap1
);
3616 nparam
= isl_basic_map_n_param(bmap1
);
3618 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3619 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3620 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3621 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3622 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3623 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3624 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3625 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3626 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3627 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3628 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3630 bmap
= isl_basic_map_alloc_space(dim_result
,
3631 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3632 bmap1
->n_eq
+ bmap2
->n_eq
,
3633 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3634 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3635 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3636 bmap
= add_divs(bmap
, n
);
3637 bmap
= isl_basic_map_simplify(bmap
);
3638 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3639 return isl_basic_map_finalize(bmap
);
3641 isl_basic_map_free(bmap1
);
3642 isl_basic_map_free(bmap2
);
3646 struct isl_basic_set
*isl_basic_set_apply(
3647 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3652 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3655 return (struct isl_basic_set
*)
3656 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3658 isl_basic_set_free(bset
);
3659 isl_basic_map_free(bmap
);
3663 struct isl_basic_map
*isl_basic_map_apply_domain(
3664 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3666 if (!bmap1
|| !bmap2
)
3669 isl_assert(bmap1
->ctx
,
3670 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3671 isl_assert(bmap1
->ctx
,
3672 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3675 bmap1
= isl_basic_map_reverse(bmap1
);
3676 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3677 return isl_basic_map_reverse(bmap1
);
3679 isl_basic_map_free(bmap1
);
3680 isl_basic_map_free(bmap2
);
3684 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3685 * A \cap B -> f(A) + f(B)
3687 struct isl_basic_map
*isl_basic_map_sum(
3688 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3690 unsigned n_in
, n_out
, nparam
, total
, pos
;
3691 struct isl_basic_map
*bmap
= NULL
;
3692 struct isl_dim_map
*dim_map1
, *dim_map2
;
3695 if (!bmap1
|| !bmap2
)
3698 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3701 nparam
= isl_basic_map_n_param(bmap1
);
3702 n_in
= isl_basic_map_n_in(bmap1
);
3703 n_out
= isl_basic_map_n_out(bmap1
);
3705 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3706 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3707 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3708 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3709 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3710 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3711 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3712 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3713 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3714 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3715 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3717 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3718 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3719 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3720 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3721 for (i
= 0; i
< n_out
; ++i
) {
3722 int j
= isl_basic_map_alloc_equality(bmap
);
3725 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3726 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3727 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3728 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3730 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3731 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3732 bmap
= add_divs(bmap
, 2 * n_out
);
3734 bmap
= isl_basic_map_simplify(bmap
);
3735 return isl_basic_map_finalize(bmap
);
3737 isl_basic_map_free(bmap
);
3738 isl_basic_map_free(bmap1
);
3739 isl_basic_map_free(bmap2
);
3743 /* Given two maps A -> f(A) and B -> g(B), construct a map
3744 * A \cap B -> f(A) + f(B)
3746 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3748 struct isl_map
*result
;
3754 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3756 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3757 map1
->n
* map2
->n
, 0);
3760 for (i
= 0; i
< map1
->n
; ++i
)
3761 for (j
= 0; j
< map2
->n
; ++j
) {
3762 struct isl_basic_map
*part
;
3763 part
= isl_basic_map_sum(
3764 isl_basic_map_copy(map1
->p
[i
]),
3765 isl_basic_map_copy(map2
->p
[j
]));
3766 if (isl_basic_map_is_empty(part
))
3767 isl_basic_map_free(part
);
3769 result
= isl_map_add_basic_map(result
, part
);
3782 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3783 __isl_take isl_set
*set2
)
3785 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3788 /* Given a basic map A -> f(A), construct A -> -f(A).
3790 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3795 bmap
= isl_basic_map_cow(bmap
);
3799 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3800 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3801 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3802 for (j
= 0; j
< n
; ++j
)
3803 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3804 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3805 for (j
= 0; j
< n
; ++j
)
3806 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3807 for (i
= 0; i
< bmap
->n_div
; ++i
)
3808 for (j
= 0; j
< n
; ++j
)
3809 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3810 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3811 return isl_basic_map_finalize(bmap
);
3814 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3816 return isl_basic_map_neg(bset
);
3819 /* Given a map A -> f(A), construct A -> -f(A).
3821 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3825 map
= isl_map_cow(map
);
3829 for (i
= 0; i
< map
->n
; ++i
) {
3830 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3841 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3843 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3846 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3847 * A -> floor(f(A)/d).
3849 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3852 unsigned n_in
, n_out
, nparam
, total
, pos
;
3853 struct isl_basic_map
*result
= NULL
;
3854 struct isl_dim_map
*dim_map
;
3860 nparam
= isl_basic_map_n_param(bmap
);
3861 n_in
= isl_basic_map_n_in(bmap
);
3862 n_out
= isl_basic_map_n_out(bmap
);
3864 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3865 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3866 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3867 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3868 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3869 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3871 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3872 bmap
->n_div
+ n_out
,
3873 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3874 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3875 result
= add_divs(result
, n_out
);
3876 for (i
= 0; i
< n_out
; ++i
) {
3878 j
= isl_basic_map_alloc_inequality(result
);
3881 isl_seq_clr(result
->ineq
[j
], 1+total
);
3882 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3883 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3884 j
= isl_basic_map_alloc_inequality(result
);
3887 isl_seq_clr(result
->ineq
[j
], 1+total
);
3888 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3889 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3890 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3893 result
= isl_basic_map_simplify(result
);
3894 return isl_basic_map_finalize(result
);
3896 isl_basic_map_free(result
);
3900 /* Given a map A -> f(A) and an integer d, construct a map
3901 * A -> floor(f(A)/d).
3903 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3907 map
= isl_map_cow(map
);
3911 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3912 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3913 for (i
= 0; i
< map
->n
; ++i
) {
3914 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3925 /* Given a map A -> f(A) and an integer d, construct a map
3926 * A -> floor(f(A)/d).
3928 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3929 __isl_take isl_val
*d
)
3933 if (!isl_val_is_int(d
))
3934 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3935 "expecting integer denominator", goto error
);
3936 map
= isl_map_floordiv(map
, d
->n
);
3945 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3951 i
= isl_basic_map_alloc_equality(bmap
);
3954 nparam
= isl_basic_map_n_param(bmap
);
3955 n_in
= isl_basic_map_n_in(bmap
);
3956 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3957 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3958 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3959 return isl_basic_map_finalize(bmap
);
3961 isl_basic_map_free(bmap
);
3965 /* Add a constraints to "bmap" expressing i_pos < o_pos
3967 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3973 i
= isl_basic_map_alloc_inequality(bmap
);
3976 nparam
= isl_basic_map_n_param(bmap
);
3977 n_in
= isl_basic_map_n_in(bmap
);
3978 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3979 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3980 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3981 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3982 return isl_basic_map_finalize(bmap
);
3984 isl_basic_map_free(bmap
);
3988 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3990 static __isl_give isl_basic_map
*var_less_or_equal(
3991 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3997 i
= isl_basic_map_alloc_inequality(bmap
);
4000 nparam
= isl_basic_map_n_param(bmap
);
4001 n_in
= isl_basic_map_n_in(bmap
);
4002 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4003 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4004 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4005 return isl_basic_map_finalize(bmap
);
4007 isl_basic_map_free(bmap
);
4011 /* Add a constraints to "bmap" expressing i_pos > o_pos
4013 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4019 i
= isl_basic_map_alloc_inequality(bmap
);
4022 nparam
= isl_basic_map_n_param(bmap
);
4023 n_in
= isl_basic_map_n_in(bmap
);
4024 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4025 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4026 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4027 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4028 return isl_basic_map_finalize(bmap
);
4030 isl_basic_map_free(bmap
);
4034 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4036 static __isl_give isl_basic_map
*var_more_or_equal(
4037 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4043 i
= isl_basic_map_alloc_inequality(bmap
);
4046 nparam
= isl_basic_map_n_param(bmap
);
4047 n_in
= isl_basic_map_n_in(bmap
);
4048 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4049 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4050 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4051 return isl_basic_map_finalize(bmap
);
4053 isl_basic_map_free(bmap
);
4057 __isl_give isl_basic_map
*isl_basic_map_equal(
4058 __isl_take isl_space
*dim
, unsigned n_equal
)
4061 struct isl_basic_map
*bmap
;
4062 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4065 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4066 bmap
= var_equal(bmap
, i
);
4067 return isl_basic_map_finalize(bmap
);
4070 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4072 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4076 struct isl_basic_map
*bmap
;
4077 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4080 for (i
= 0; i
< pos
&& bmap
; ++i
)
4081 bmap
= var_equal(bmap
, i
);
4083 bmap
= var_less(bmap
, pos
);
4084 return isl_basic_map_finalize(bmap
);
4087 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4089 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4090 __isl_take isl_space
*dim
, unsigned pos
)
4093 isl_basic_map
*bmap
;
4095 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4096 for (i
= 0; i
< pos
; ++i
)
4097 bmap
= var_equal(bmap
, i
);
4098 bmap
= var_less_or_equal(bmap
, pos
);
4099 return isl_basic_map_finalize(bmap
);
4102 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4104 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4108 struct isl_basic_map
*bmap
;
4109 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4112 for (i
= 0; i
< pos
&& bmap
; ++i
)
4113 bmap
= var_equal(bmap
, i
);
4115 bmap
= var_more(bmap
, pos
);
4116 return isl_basic_map_finalize(bmap
);
4119 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4121 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4122 __isl_take isl_space
*dim
, unsigned pos
)
4125 isl_basic_map
*bmap
;
4127 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4128 for (i
= 0; i
< pos
; ++i
)
4129 bmap
= var_equal(bmap
, i
);
4130 bmap
= var_more_or_equal(bmap
, pos
);
4131 return isl_basic_map_finalize(bmap
);
4134 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4135 unsigned n
, int equal
)
4137 struct isl_map
*map
;
4140 if (n
== 0 && equal
)
4141 return isl_map_universe(dims
);
4143 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4145 for (i
= 0; i
+ 1 < n
; ++i
)
4146 map
= isl_map_add_basic_map(map
,
4147 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4150 map
= isl_map_add_basic_map(map
,
4151 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4153 map
= isl_map_add_basic_map(map
,
4154 isl_basic_map_less_at(dims
, n
- 1));
4156 isl_space_free(dims
);
4161 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4165 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4168 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4170 return map_lex_lte_first(dim
, n
, 0);
4173 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4175 return map_lex_lte_first(dim
, n
, 1);
4178 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4180 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4183 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4185 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4188 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4189 unsigned n
, int equal
)
4191 struct isl_map
*map
;
4194 if (n
== 0 && equal
)
4195 return isl_map_universe(dims
);
4197 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4199 for (i
= 0; i
+ 1 < n
; ++i
)
4200 map
= isl_map_add_basic_map(map
,
4201 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4204 map
= isl_map_add_basic_map(map
,
4205 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4207 map
= isl_map_add_basic_map(map
,
4208 isl_basic_map_more_at(dims
, n
- 1));
4210 isl_space_free(dims
);
4215 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4219 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4222 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4224 return map_lex_gte_first(dim
, n
, 0);
4227 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4229 return map_lex_gte_first(dim
, n
, 1);
4232 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4234 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4237 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4239 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4242 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4243 __isl_take isl_set
*set2
)
4246 map
= isl_map_lex_le(isl_set_get_space(set1
));
4247 map
= isl_map_intersect_domain(map
, set1
);
4248 map
= isl_map_intersect_range(map
, set2
);
4252 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4253 __isl_take isl_set
*set2
)
4256 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4257 map
= isl_map_intersect_domain(map
, set1
);
4258 map
= isl_map_intersect_range(map
, set2
);
4262 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4263 __isl_take isl_set
*set2
)
4266 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4267 map
= isl_map_intersect_domain(map
, set1
);
4268 map
= isl_map_intersect_range(map
, set2
);
4272 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4273 __isl_take isl_set
*set2
)
4276 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4277 map
= isl_map_intersect_domain(map
, set1
);
4278 map
= isl_map_intersect_range(map
, set2
);
4282 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4283 __isl_take isl_map
*map2
)
4286 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4287 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4288 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4292 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4293 __isl_take isl_map
*map2
)
4296 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4297 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4298 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4302 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4303 __isl_take isl_map
*map2
)
4306 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4307 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4308 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4312 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4313 __isl_take isl_map
*map2
)
4316 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4317 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4318 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4322 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4323 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4325 struct isl_basic_map
*bmap
;
4327 bset
= isl_basic_set_cow(bset
);
4331 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4332 isl_space_free(bset
->dim
);
4333 bmap
= (struct isl_basic_map
*) bset
;
4335 return isl_basic_map_finalize(bmap
);
4337 isl_basic_set_free(bset
);
4338 isl_space_free(dim
);
4342 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4346 if (bmap
->dim
->n_in
== 0)
4347 return (struct isl_basic_set
*)bmap
;
4348 bmap
= isl_basic_map_cow(bmap
);
4351 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4354 bmap
= isl_basic_map_finalize(bmap
);
4355 return (struct isl_basic_set
*)bmap
;
4357 isl_basic_map_free(bmap
);
4361 /* For a div d = floor(f/m), add the constraint
4365 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4366 unsigned pos
, isl_int
*div
)
4369 unsigned total
= isl_basic_map_total_dim(bmap
);
4371 i
= isl_basic_map_alloc_inequality(bmap
);
4374 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4375 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4380 /* For a div d = floor(f/m), add the constraint
4382 * -(f-(n-1)) + m d >= 0
4384 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4385 unsigned pos
, isl_int
*div
)
4388 unsigned total
= isl_basic_map_total_dim(bmap
);
4390 i
= isl_basic_map_alloc_inequality(bmap
);
4393 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4394 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4395 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4396 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4401 /* For a div d = floor(f/m), add the constraints
4404 * -(f-(n-1)) + m d >= 0
4406 * Note that the second constraint is the negation of
4410 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4411 unsigned pos
, isl_int
*div
)
4413 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4415 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4420 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4421 unsigned pos
, isl_int
*div
)
4423 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4427 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4429 unsigned total
= isl_basic_map_total_dim(bmap
);
4430 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4432 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4436 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4438 * In particular, if this div is of the form d = floor(f/m),
4439 * then add the constraint
4443 * if sign < 0 or the constraint
4445 * -(f-(n-1)) + m d >= 0
4449 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4450 unsigned div
, int sign
)
4458 total
= isl_basic_map_total_dim(bmap
);
4459 div_pos
= total
- bmap
->n_div
+ div
;
4462 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4464 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4467 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4469 return isl_basic_map_add_div_constraints(bset
, div
);
4472 struct isl_basic_set
*isl_basic_map_underlying_set(
4473 struct isl_basic_map
*bmap
)
4477 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4479 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4480 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4481 return (struct isl_basic_set
*)bmap
;
4482 bmap
= isl_basic_map_cow(bmap
);
4485 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4488 bmap
->extra
-= bmap
->n_div
;
4490 bmap
= isl_basic_map_finalize(bmap
);
4491 return (struct isl_basic_set
*)bmap
;
4493 isl_basic_map_free(bmap
);
4497 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4498 __isl_take isl_basic_set
*bset
)
4500 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4503 struct isl_basic_map
*isl_basic_map_overlying_set(
4504 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4506 struct isl_basic_map
*bmap
;
4507 struct isl_ctx
*ctx
;
4514 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4515 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4516 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4518 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4519 isl_basic_map_free(like
);
4520 return (struct isl_basic_map
*)bset
;
4522 bset
= isl_basic_set_cow(bset
);
4525 total
= bset
->dim
->n_out
+ bset
->extra
;
4526 bmap
= (struct isl_basic_map
*)bset
;
4527 isl_space_free(bmap
->dim
);
4528 bmap
->dim
= isl_space_copy(like
->dim
);
4531 bmap
->n_div
= like
->n_div
;
4532 bmap
->extra
+= like
->n_div
;
4536 ltotal
= total
- bmap
->extra
+ like
->extra
;
4539 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4540 bmap
->extra
* (1 + 1 + total
));
4541 if (isl_blk_is_error(bmap
->block2
))
4543 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4547 for (i
= 0; i
< bmap
->extra
; ++i
)
4548 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4549 for (i
= 0; i
< like
->n_div
; ++i
) {
4550 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4551 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4553 bmap
= isl_basic_map_extend_constraints(bmap
,
4554 0, 2 * like
->n_div
);
4555 for (i
= 0; i
< like
->n_div
; ++i
) {
4558 if (isl_int_is_zero(bmap
->div
[i
][0]))
4560 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4561 bmap
= isl_basic_map_free(bmap
);
4564 isl_basic_map_free(like
);
4565 bmap
= isl_basic_map_simplify(bmap
);
4566 bmap
= isl_basic_map_finalize(bmap
);
4569 isl_basic_map_free(like
);
4570 isl_basic_set_free(bset
);
4574 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4575 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4577 return (struct isl_basic_set
*)
4578 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4581 struct isl_set
*isl_set_from_underlying_set(
4582 struct isl_set
*set
, struct isl_basic_set
*like
)
4588 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4590 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4591 isl_basic_set_free(like
);
4594 set
= isl_set_cow(set
);
4597 for (i
= 0; i
< set
->n
; ++i
) {
4598 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4599 isl_basic_set_copy(like
));
4603 isl_space_free(set
->dim
);
4604 set
->dim
= isl_space_copy(like
->dim
);
4607 isl_basic_set_free(like
);
4610 isl_basic_set_free(like
);
4615 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4619 map
= isl_map_cow(map
);
4622 map
->dim
= isl_space_cow(map
->dim
);
4626 for (i
= 1; i
< map
->n
; ++i
)
4627 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4629 for (i
= 0; i
< map
->n
; ++i
) {
4630 map
->p
[i
] = (struct isl_basic_map
*)
4631 isl_basic_map_underlying_set(map
->p
[i
]);
4636 map
->dim
= isl_space_underlying(map
->dim
, 0);
4638 isl_space_free(map
->dim
);
4639 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4643 return (struct isl_set
*)map
;
4649 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4651 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4654 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4655 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4657 bmap
= isl_basic_map_cow(bmap
);
4661 isl_space_free(bmap
->dim
);
4664 bmap
= isl_basic_map_finalize(bmap
);
4668 isl_basic_map_free(bmap
);
4669 isl_space_free(dim
);
4673 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4674 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4676 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4680 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4681 __isl_take isl_space
*dim
)
4685 map
= isl_map_cow(map
);
4689 for (i
= 0; i
< map
->n
; ++i
) {
4690 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4691 isl_space_copy(dim
));
4695 isl_space_free(map
->dim
);
4701 isl_space_free(dim
);
4705 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4706 __isl_take isl_space
*dim
)
4708 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4711 /* Compute the parameter domain of the given basic set.
4713 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4718 if (isl_basic_set_is_params(bset
))
4721 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4722 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4723 space
= isl_basic_set_get_space(bset
);
4724 space
= isl_space_params(space
);
4725 bset
= isl_basic_set_reset_space(bset
, space
);
4729 /* Construct a zero-dimensional basic set with the given parameter domain.
4731 __isl_give isl_basic_set
*isl_basic_set_from_params(
4732 __isl_take isl_basic_set
*bset
)
4735 space
= isl_basic_set_get_space(bset
);
4736 space
= isl_space_set_from_params(space
);
4737 bset
= isl_basic_set_reset_space(bset
, space
);
4741 /* Compute the parameter domain of the given set.
4743 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4748 if (isl_set_is_params(set
))
4751 n
= isl_set_dim(set
, isl_dim_set
);
4752 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4753 space
= isl_set_get_space(set
);
4754 space
= isl_space_params(space
);
4755 set
= isl_set_reset_space(set
, space
);
4759 /* Construct a zero-dimensional set with the given parameter domain.
4761 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4764 space
= isl_set_get_space(set
);
4765 space
= isl_space_set_from_params(space
);
4766 set
= isl_set_reset_space(set
, space
);
4770 /* Compute the parameter domain of the given map.
4772 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4777 n
= isl_map_dim(map
, isl_dim_in
);
4778 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4779 n
= isl_map_dim(map
, isl_dim_out
);
4780 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4781 space
= isl_map_get_space(map
);
4782 space
= isl_space_params(space
);
4783 map
= isl_map_reset_space(map
, space
);
4787 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4790 struct isl_basic_set
*domain
;
4796 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4798 n_in
= isl_basic_map_n_in(bmap
);
4799 n_out
= isl_basic_map_n_out(bmap
);
4800 domain
= isl_basic_set_from_basic_map(bmap
);
4801 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4803 domain
= isl_basic_set_reset_space(domain
, dim
);
4808 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4812 return isl_space_may_be_set(bmap
->dim
);
4815 /* Is this basic map actually a set?
4816 * Users should never call this function. Outside of isl,
4817 * the type should indicate whether something is a set or a map.
4819 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4823 return isl_space_is_set(bmap
->dim
);
4826 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4830 if (isl_basic_map_is_set(bmap
))
4832 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4835 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4836 __isl_take isl_basic_map
*bmap
)
4840 isl_basic_map
*domain
;
4841 int nparam
, n_in
, n_out
;
4844 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4845 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4846 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4848 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4849 domain
= isl_basic_map_universe(dim
);
4851 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4852 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4853 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4855 total
= isl_basic_map_total_dim(bmap
);
4857 for (i
= 0; i
< n_in
; ++i
) {
4858 k
= isl_basic_map_alloc_equality(bmap
);
4861 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4862 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4863 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4866 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4867 return isl_basic_map_finalize(bmap
);
4869 isl_basic_map_free(bmap
);
4873 __isl_give isl_basic_map
*isl_basic_map_range_map(
4874 __isl_take isl_basic_map
*bmap
)
4878 isl_basic_map
*range
;
4879 int nparam
, n_in
, n_out
;
4882 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4883 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4884 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4886 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4887 range
= isl_basic_map_universe(dim
);
4889 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4890 bmap
= isl_basic_map_apply_range(bmap
, range
);
4891 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4893 total
= isl_basic_map_total_dim(bmap
);
4895 for (i
= 0; i
< n_out
; ++i
) {
4896 k
= isl_basic_map_alloc_equality(bmap
);
4899 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4900 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4901 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4904 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4905 return isl_basic_map_finalize(bmap
);
4907 isl_basic_map_free(bmap
);
4911 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4915 return isl_space_may_be_set(map
->dim
);
4918 /* Is this map actually a set?
4919 * Users should never call this function. Outside of isl,
4920 * the type should indicate whether something is a set or a map.
4922 int isl_map_is_set(__isl_keep isl_map
*map
)
4926 return isl_space_is_set(map
->dim
);
4929 struct isl_set
*isl_map_range(struct isl_map
*map
)
4932 struct isl_set
*set
;
4936 if (isl_map_is_set(map
))
4937 return (isl_set
*)map
;
4939 map
= isl_map_cow(map
);
4943 set
= (struct isl_set
*) map
;
4944 set
->dim
= isl_space_range(set
->dim
);
4947 for (i
= 0; i
< map
->n
; ++i
) {
4948 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4952 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4953 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4960 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4964 map
= isl_map_cow(map
);
4968 map
->dim
= isl_space_domain_map(map
->dim
);
4971 for (i
= 0; i
< map
->n
; ++i
) {
4972 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4976 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4977 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4984 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4987 isl_space
*range_dim
;
4989 map
= isl_map_cow(map
);
4993 range_dim
= isl_space_range(isl_map_get_space(map
));
4994 range_dim
= isl_space_from_range(range_dim
);
4995 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4996 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4999 for (i
= 0; i
< map
->n
; ++i
) {
5000 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5004 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5005 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5012 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5013 __isl_take isl_space
*dim
)
5016 struct isl_map
*map
= NULL
;
5018 set
= isl_set_cow(set
);
5021 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5022 map
= (struct isl_map
*)set
;
5023 for (i
= 0; i
< set
->n
; ++i
) {
5024 map
->p
[i
] = isl_basic_map_from_basic_set(
5025 set
->p
[i
], isl_space_copy(dim
));
5029 isl_space_free(map
->dim
);
5033 isl_space_free(dim
);
5038 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5039 __isl_take isl_basic_set
*bset
)
5041 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5044 __isl_give isl_basic_map
*isl_basic_map_from_range(
5045 __isl_take isl_basic_set
*bset
)
5048 space
= isl_basic_set_get_space(bset
);
5049 space
= isl_space_from_range(space
);
5050 bset
= isl_basic_set_reset_space(bset
, space
);
5051 return (isl_basic_map
*)bset
;
5054 /* Create a relation with the given set as range.
5055 * The domain of the created relation is a zero-dimensional
5056 * flat anonymous space.
5058 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5061 space
= isl_set_get_space(set
);
5062 space
= isl_space_from_range(space
);
5063 set
= isl_set_reset_space(set
, space
);
5064 return (struct isl_map
*)set
;
5067 /* Create a relation with the given set as domain.
5068 * The range of the created relation is a zero-dimensional
5069 * flat anonymous space.
5071 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5073 return isl_map_reverse(isl_map_from_range(set
));
5076 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5077 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5079 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5082 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5083 __isl_take isl_set
*range
)
5085 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5088 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
5091 struct isl_set
*set
= NULL
;
5095 map
= isl_map_cow(map
);
5098 map
->dim
= isl_space_as_set_space(map
->dim
);
5101 set
= (struct isl_set
*)map
;
5102 for (i
= 0; i
< map
->n
; ++i
) {
5103 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5113 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5116 struct isl_map
*map
;
5121 isl_die(dim
->ctx
, isl_error_internal
,
5122 "negative number of basic maps", goto error
);
5123 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5124 sizeof(struct isl_map
) +
5125 (n
- 1) * sizeof(struct isl_basic_map
*));
5129 map
->ctx
= dim
->ctx
;
5130 isl_ctx_ref(map
->ctx
);
5138 isl_space_free(dim
);
5142 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5143 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5146 struct isl_map
*map
;
5149 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5153 map
= isl_map_alloc_space(dims
, n
, flags
);
5157 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5159 struct isl_basic_map
*bmap
;
5160 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5161 bmap
= isl_basic_map_set_to_empty(bmap
);
5165 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5167 struct isl_basic_set
*bset
;
5168 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5169 bset
= isl_basic_set_set_to_empty(bset
);
5173 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5175 struct isl_basic_map
*bmap
;
5178 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5179 bmap
= isl_basic_map_set_to_empty(bmap
);
5183 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5185 struct isl_basic_map
*bmap
;
5188 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5189 bmap
= isl_basic_map_set_to_empty(bmap
);
5193 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5195 struct isl_basic_set
*bset
;
5198 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5199 bset
= isl_basic_set_set_to_empty(bset
);
5203 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5205 struct isl_basic_map
*bmap
;
5206 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5207 bmap
= isl_basic_map_finalize(bmap
);
5211 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5213 struct isl_basic_set
*bset
;
5214 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5215 bset
= isl_basic_set_finalize(bset
);
5219 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5222 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5223 isl_basic_map
*bmap
;
5225 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5226 for (i
= 0; i
< total
; ++i
) {
5227 int k
= isl_basic_map_alloc_inequality(bmap
);
5230 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5231 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5235 isl_basic_map_free(bmap
);
5239 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5241 return isl_basic_map_nat_universe(dim
);
5244 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5246 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5249 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5251 return isl_map_nat_universe(dim
);
5254 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5255 __isl_keep isl_basic_map
*model
)
5259 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5262 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5266 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5269 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5270 __isl_keep isl_set
*model
)
5274 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5277 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5279 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5282 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5286 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5289 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5293 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5296 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5298 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5301 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5305 return isl_set_empty(isl_space_copy(model
->dim
));
5308 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5310 struct isl_map
*map
;
5313 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5314 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5318 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5320 struct isl_set
*set
;
5323 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5324 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5328 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5332 return isl_set_universe(isl_space_copy(model
->dim
));
5335 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5338 struct isl_map
*dup
;
5342 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5343 for (i
= 0; i
< map
->n
; ++i
)
5344 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5348 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5349 __isl_take isl_basic_map
*bmap
)
5353 if (isl_basic_map_plain_is_empty(bmap
)) {
5354 isl_basic_map_free(bmap
);
5357 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5358 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5359 map
->p
[map
->n
] = bmap
;
5361 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5367 isl_basic_map_free(bmap
);
5371 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5381 isl_ctx_deref(map
->ctx
);
5382 for (i
= 0; i
< map
->n
; ++i
)
5383 isl_basic_map_free(map
->p
[i
]);
5384 isl_space_free(map
->dim
);
5390 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5391 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5395 base
= isl_map_cow(base
);
5399 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5402 for (i
= 0; i
< base
->n
; ++i
) {
5403 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5404 isl_space_copy(base
->dim
), 0, 0, 0);
5414 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5415 unsigned nparam
, unsigned dim
)
5417 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5421 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5422 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5426 bmap
= isl_basic_map_cow(bmap
);
5427 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5428 j
= isl_basic_map_alloc_equality(bmap
);
5431 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5432 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5433 isl_int_set_si(bmap
->eq
[j
][0], value
);
5434 bmap
= isl_basic_map_simplify(bmap
);
5435 return isl_basic_map_finalize(bmap
);
5437 isl_basic_map_free(bmap
);
5441 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5442 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5446 bmap
= isl_basic_map_cow(bmap
);
5447 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5448 j
= isl_basic_map_alloc_equality(bmap
);
5451 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5452 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5453 isl_int_set(bmap
->eq
[j
][0], value
);
5454 bmap
= isl_basic_map_simplify(bmap
);
5455 return isl_basic_map_finalize(bmap
);
5457 isl_basic_map_free(bmap
);
5461 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5462 enum isl_dim_type type
, unsigned pos
, int value
)
5466 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5467 return isl_basic_map_fix_pos_si(bmap
,
5468 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5470 isl_basic_map_free(bmap
);
5474 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5475 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5479 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5480 return isl_basic_map_fix_pos(bmap
,
5481 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5483 isl_basic_map_free(bmap
);
5487 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5488 * to be equal to "v".
5490 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5491 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5495 if (!isl_val_is_int(v
))
5496 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5497 "expecting integer value", goto error
);
5498 if (pos
>= isl_basic_map_dim(bmap
, type
))
5499 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5500 "index out of bounds", goto error
);
5501 pos
+= isl_basic_map_offset(bmap
, type
);
5502 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5506 isl_basic_map_free(bmap
);
5511 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5512 * to be equal to "v".
5514 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5515 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5517 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5520 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5521 enum isl_dim_type type
, unsigned pos
, int value
)
5523 return (struct isl_basic_set
*)
5524 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5528 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5529 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5531 return (struct isl_basic_set
*)
5532 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5536 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5537 unsigned input
, int value
)
5539 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5542 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5543 unsigned dim
, int value
)
5545 return (struct isl_basic_set
*)
5546 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5547 isl_dim_set
, dim
, value
);
5550 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5552 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5559 isl_basic_map_free(map
->p
[i
]);
5560 if (i
!= map
->n
- 1) {
5561 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5562 map
->p
[i
] = map
->p
[map
->n
- 1];
5569 /* Perform "fn" on each basic map of "map", where we may not be holding
5570 * the only reference to "map".
5571 * In particular, "fn" should be a semantics preserving operation
5572 * that we want to apply to all copies of "map". We therefore need
5573 * to be careful not to modify "map" in a way that breaks "map"
5574 * in case anything goes wrong.
5576 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5577 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5579 struct isl_basic_map
*bmap
;
5585 for (i
= map
->n
- 1; i
>= 0; --i
) {
5586 bmap
= isl_basic_map_copy(map
->p
[i
]);
5590 isl_basic_map_free(map
->p
[i
]);
5592 if (remove_if_empty(map
, i
) < 0)
5602 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5603 enum isl_dim_type type
, unsigned pos
, int value
)
5607 map
= isl_map_cow(map
);
5611 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5612 for (i
= map
->n
- 1; i
>= 0; --i
) {
5613 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5614 if (remove_if_empty(map
, i
) < 0)
5617 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5624 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5625 enum isl_dim_type type
, unsigned pos
, int value
)
5627 return (struct isl_set
*)
5628 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5631 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5632 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5636 map
= isl_map_cow(map
);
5640 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5641 for (i
= 0; i
< map
->n
; ++i
) {
5642 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5646 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5653 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5654 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5656 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5659 /* Fix the value of the variable at position "pos" of type "type" of "map"
5660 * to be equal to "v".
5662 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5663 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5667 map
= isl_map_cow(map
);
5671 if (!isl_val_is_int(v
))
5672 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5673 "expecting integer value", goto error
);
5674 if (pos
>= isl_map_dim(map
, type
))
5675 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5676 "index out of bounds", goto error
);
5677 for (i
= map
->n
- 1; i
>= 0; --i
) {
5678 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5680 if (remove_if_empty(map
, i
) < 0)
5683 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5692 /* Fix the value of the variable at position "pos" of type "type" of "set"
5693 * to be equal to "v".
5695 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5696 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5698 return isl_map_fix_val(set
, type
, pos
, v
);
5701 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5702 unsigned input
, int value
)
5704 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5707 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5709 return (struct isl_set
*)
5710 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5713 static __isl_give isl_basic_map
*basic_map_bound_si(
5714 __isl_take isl_basic_map
*bmap
,
5715 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5721 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5722 pos
+= isl_basic_map_offset(bmap
, type
);
5723 bmap
= isl_basic_map_cow(bmap
);
5724 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5725 j
= isl_basic_map_alloc_inequality(bmap
);
5728 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5730 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5731 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5733 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5734 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5736 bmap
= isl_basic_map_simplify(bmap
);
5737 return isl_basic_map_finalize(bmap
);
5739 isl_basic_map_free(bmap
);
5743 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5744 __isl_take isl_basic_map
*bmap
,
5745 enum isl_dim_type type
, unsigned pos
, int value
)
5747 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5750 /* Constrain the values of the given dimension to be no greater than "value".
5752 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5753 __isl_take isl_basic_map
*bmap
,
5754 enum isl_dim_type type
, unsigned pos
, int value
)
5756 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5759 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5760 unsigned dim
, isl_int value
)
5764 bset
= isl_basic_set_cow(bset
);
5765 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5766 j
= isl_basic_set_alloc_inequality(bset
);
5769 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5770 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5771 isl_int_neg(bset
->ineq
[j
][0], value
);
5772 bset
= isl_basic_set_simplify(bset
);
5773 return isl_basic_set_finalize(bset
);
5775 isl_basic_set_free(bset
);
5779 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5780 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5784 map
= isl_map_cow(map
);
5788 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5789 for (i
= 0; i
< map
->n
; ++i
) {
5790 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5791 type
, pos
, value
, upper
);
5795 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5802 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5803 enum isl_dim_type type
, unsigned pos
, int value
)
5805 return map_bound_si(map
, type
, pos
, value
, 0);
5808 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5809 enum isl_dim_type type
, unsigned pos
, int value
)
5811 return map_bound_si(map
, type
, pos
, value
, 1);
5814 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5815 enum isl_dim_type type
, unsigned pos
, int value
)
5817 return (struct isl_set
*)
5818 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5821 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5822 enum isl_dim_type type
, unsigned pos
, int value
)
5824 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5827 /* Bound the given variable of "bmap" from below (or above is "upper"
5828 * is set) to "value".
5830 static __isl_give isl_basic_map
*basic_map_bound(
5831 __isl_take isl_basic_map
*bmap
,
5832 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5838 if (pos
>= isl_basic_map_dim(bmap
, type
))
5839 isl_die(bmap
->ctx
, isl_error_invalid
,
5840 "index out of bounds", goto error
);
5841 pos
+= isl_basic_map_offset(bmap
, type
);
5842 bmap
= isl_basic_map_cow(bmap
);
5843 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5844 j
= isl_basic_map_alloc_inequality(bmap
);
5847 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5849 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5850 isl_int_set(bmap
->ineq
[j
][0], value
);
5852 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5853 isl_int_neg(bmap
->ineq
[j
][0], value
);
5855 bmap
= isl_basic_map_simplify(bmap
);
5856 return isl_basic_map_finalize(bmap
);
5858 isl_basic_map_free(bmap
);
5862 /* Bound the given variable of "map" from below (or above is "upper"
5863 * is set) to "value".
5865 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5866 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5870 map
= isl_map_cow(map
);
5874 if (pos
>= isl_map_dim(map
, type
))
5875 isl_die(map
->ctx
, isl_error_invalid
,
5876 "index out of bounds", goto error
);
5877 for (i
= map
->n
- 1; i
>= 0; --i
) {
5878 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5879 if (remove_if_empty(map
, i
) < 0)
5882 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5889 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5890 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5892 return map_bound(map
, type
, pos
, value
, 0);
5895 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5896 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5898 return map_bound(map
, type
, pos
, value
, 1);
5901 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5902 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5904 return isl_map_lower_bound(set
, type
, pos
, value
);
5907 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5908 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5910 return isl_map_upper_bound(set
, type
, pos
, value
);
5913 /* Force the values of the variable at position "pos" of type "type" of "set"
5914 * to be no smaller than "value".
5916 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5917 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5921 if (!isl_val_is_int(value
))
5922 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5923 "expecting integer value", goto error
);
5924 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5925 isl_val_free(value
);
5928 isl_val_free(value
);
5933 /* Force the values of the variable at position "pos" of type "type" of "set"
5934 * to be no greater than "value".
5936 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5937 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5941 if (!isl_val_is_int(value
))
5942 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5943 "expecting integer value", goto error
);
5944 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5945 isl_val_free(value
);
5948 isl_val_free(value
);
5953 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5958 set
= isl_set_cow(set
);
5962 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5963 for (i
= 0; i
< set
->n
; ++i
) {
5964 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5974 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5978 map
= isl_map_cow(map
);
5982 map
->dim
= isl_space_reverse(map
->dim
);
5985 for (i
= 0; i
< map
->n
; ++i
) {
5986 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5990 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5997 static struct isl_map
*isl_basic_map_partial_lexopt(
5998 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5999 struct isl_set
**empty
, int max
)
6001 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6004 struct isl_map
*isl_basic_map_partial_lexmax(
6005 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6006 struct isl_set
**empty
)
6008 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6011 struct isl_map
*isl_basic_map_partial_lexmin(
6012 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6013 struct isl_set
**empty
)
6015 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6018 struct isl_set
*isl_basic_set_partial_lexmin(
6019 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6020 struct isl_set
**empty
)
6022 return (struct isl_set
*)
6023 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6027 struct isl_set
*isl_basic_set_partial_lexmax(
6028 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6029 struct isl_set
**empty
)
6031 return (struct isl_set
*)
6032 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6036 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6037 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6038 __isl_give isl_set
**empty
)
6040 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6043 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6044 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6045 __isl_give isl_set
**empty
)
6047 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6050 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6051 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6052 __isl_give isl_set
**empty
)
6054 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6057 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6058 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6059 __isl_give isl_set
**empty
)
6061 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6064 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6065 __isl_take isl_basic_map
*bmap
, int max
)
6067 isl_basic_set
*dom
= NULL
;
6068 isl_space
*dom_space
;
6072 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6073 dom
= isl_basic_set_universe(dom_space
);
6074 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6076 isl_basic_map_free(bmap
);
6080 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6081 __isl_take isl_basic_map
*bmap
)
6083 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6087 #define TYPE isl_pw_multi_aff
6089 #define SUFFIX _pw_multi_aff
6091 #define EMPTY isl_pw_multi_aff_empty
6093 #define ADD isl_pw_multi_aff_union_add
6094 #include "isl_map_lexopt_templ.c"
6096 /* Given a map "map", compute the lexicographically minimal
6097 * (or maximal) image element for each domain element in dom,
6098 * in the form of an isl_pw_multi_aff.
6099 * Set *empty to those elements in dom that do not have an image element.
6101 * We first compute the lexicographically minimal or maximal element
6102 * in the first basic map. This results in a partial solution "res"
6103 * and a subset "todo" of dom that still need to be handled.
6104 * We then consider each of the remaining maps in "map" and successively
6105 * update both "res" and "todo".
6107 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6108 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6109 __isl_give isl_set
**empty
, int max
)
6112 isl_pw_multi_aff
*res
;
6118 if (isl_map_plain_is_empty(map
)) {
6123 return isl_pw_multi_aff_from_map(map
);
6126 res
= basic_map_partial_lexopt_pw_multi_aff(
6127 isl_basic_map_copy(map
->p
[0]),
6128 isl_set_copy(dom
), &todo
, max
);
6130 for (i
= 1; i
< map
->n
; ++i
) {
6131 isl_pw_multi_aff
*res_i
;
6134 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6135 isl_basic_map_copy(map
->p
[i
]),
6136 isl_set_copy(dom
), &todo_i
, max
);
6139 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6141 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6143 todo
= isl_set_intersect(todo
, todo_i
);
6164 #define TYPE isl_map
6168 #define EMPTY isl_map_empty
6170 #define ADD isl_map_union_disjoint
6171 #include "isl_map_lexopt_templ.c"
6173 /* Given a map "map", compute the lexicographically minimal
6174 * (or maximal) image element for each domain element in dom.
6175 * Set *empty to those elements in dom that do not have an image element.
6177 * We first compute the lexicographically minimal or maximal element
6178 * in the first basic map. This results in a partial solution "res"
6179 * and a subset "todo" of dom that still need to be handled.
6180 * We then consider each of the remaining maps in "map" and successively
6181 * update both "res" and "todo".
6183 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6184 * Assume we are computing the lexicographical maximum.
6185 * We first compute the lexicographically maximal element in basic map i.
6186 * This results in a partial solution res_i and a subset todo_i.
6187 * Then we combine these results with those obtain for the first k basic maps
6188 * to obtain a result that is valid for the first k+1 basic maps.
6189 * In particular, the set where there is no solution is the set where
6190 * there is no solution for the first k basic maps and also no solution
6191 * for the ith basic map, i.e.,
6193 * todo^i = todo^k * todo_i
6195 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6196 * solutions, arbitrarily breaking ties in favor of res^k.
6197 * That is, when res^k(a) >= res_i(a), we pick res^k and
6198 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6199 * the lexicographic order.)
6200 * In practice, we compute
6202 * res^k * (res_i . "<=")
6206 * res_i * (res^k . "<")
6208 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6209 * where only one of res^k and res_i provides a solution and we simply pick
6216 * Note that we only compute these intersections when dom(res^k) intersects
6217 * dom(res_i). Otherwise, the only effect of these intersections is to
6218 * potentially break up res^k and res_i into smaller pieces.
6219 * We want to avoid such splintering as much as possible.
6220 * In fact, an earlier implementation of this function would look for
6221 * better results in the domain of res^k and for extra results in todo^k,
6222 * but this would always result in a splintering according to todo^k,
6223 * even when the domain of basic map i is disjoint from the domains of
6224 * the previous basic maps.
6226 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6227 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6228 __isl_give isl_set
**empty
, int max
)
6231 struct isl_map
*res
;
6232 struct isl_set
*todo
;
6237 if (isl_map_plain_is_empty(map
)) {
6245 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6246 isl_set_copy(dom
), &todo
, max
);
6248 for (i
= 1; i
< map
->n
; ++i
) {
6252 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6254 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6255 isl_set_copy(dom
), &todo_i
, max
);
6258 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6259 le
= isl_map_lex_le(dim
);
6261 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6262 le
= isl_map_lex_ge(dim
);
6264 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6265 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6266 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6267 le
= isl_map_intersect(le
, isl_map_copy(res
));
6269 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6270 res
= isl_map_intersect_domain(res
,
6271 isl_set_copy(todo_i
));
6272 res_i
= isl_map_intersect_domain(res_i
,
6273 isl_set_copy(todo
));
6276 res
= isl_map_union_disjoint(res
, res_i
);
6277 res
= isl_map_union_disjoint(res
, lt
);
6278 res
= isl_map_union_disjoint(res
, le
);
6280 todo
= isl_set_intersect(todo
, todo_i
);
6300 __isl_give isl_map
*isl_map_partial_lexmax(
6301 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6302 __isl_give isl_set
**empty
)
6304 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6307 __isl_give isl_map
*isl_map_partial_lexmin(
6308 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6309 __isl_give isl_set
**empty
)
6311 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6314 __isl_give isl_set
*isl_set_partial_lexmin(
6315 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6316 __isl_give isl_set
**empty
)
6318 return (struct isl_set
*)
6319 isl_map_partial_lexmin((struct isl_map
*)set
,
6323 __isl_give isl_set
*isl_set_partial_lexmax(
6324 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6325 __isl_give isl_set
**empty
)
6327 return (struct isl_set
*)
6328 isl_map_partial_lexmax((struct isl_map
*)set
,
6332 /* Compute the lexicographic minimum (or maximum if "max" is set)
6333 * of "bmap" over its domain.
6335 * Since we are not interested in the part of the domain space where
6336 * there is no solution, we initialize the domain to those constraints
6337 * of "bmap" that only involve the parameters and the input dimensions.
6338 * This relieves the parametric programming engine from detecting those
6339 * inequalities and transferring them to the context. More importantly,
6340 * it ensures that those inequalities are transferred first and not
6341 * intermixed with inequalities that actually split the domain.
6343 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6347 isl_basic_map
*copy
;
6350 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6351 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6352 copy
= isl_basic_map_copy(bmap
);
6353 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6354 isl_dim_div
, 0, n_div
);
6355 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6356 isl_dim_out
, 0, n_out
);
6357 dom
= isl_basic_map_domain(copy
);
6358 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6361 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6363 return isl_basic_map_lexopt(bmap
, 0);
6366 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6368 return isl_basic_map_lexopt(bmap
, 1);
6371 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6373 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6376 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6378 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6381 /* Extract the first and only affine expression from list
6382 * and then add it to *pwaff with the given dom.
6383 * This domain is known to be disjoint from other domains
6384 * because of the way isl_basic_map_foreach_lexmax works.
6386 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6387 __isl_take isl_aff_list
*list
, void *user
)
6389 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6391 isl_pw_aff
**pwaff
= user
;
6392 isl_pw_aff
*pwaff_i
;
6396 if (isl_aff_list_n_aff(list
) != 1)
6397 isl_die(ctx
, isl_error_internal
,
6398 "expecting single element list", goto error
);
6400 aff
= isl_aff_list_get_aff(list
, 0);
6401 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6403 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6405 isl_aff_list_free(list
);
6409 isl_basic_set_free(dom
);
6410 isl_aff_list_free(list
);
6414 /* Given a basic map with one output dimension, compute the minimum or
6415 * maximum of that dimension as an isl_pw_aff.
6417 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6418 * call update_dim_opt on each leaf of the result.
6420 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6423 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6427 dim
= isl_space_from_domain(isl_space_domain(dim
));
6428 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6429 pwaff
= isl_pw_aff_empty(dim
);
6431 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6433 return isl_pw_aff_free(pwaff
);
6438 /* Compute the minimum or maximum of the given output dimension
6439 * as a function of the parameters and the input dimensions,
6440 * but independently of the other output dimensions.
6442 * We first project out the other output dimension and then compute
6443 * the "lexicographic" maximum in each basic map, combining the results
6444 * using isl_pw_aff_union_max.
6446 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6453 n_out
= isl_map_dim(map
, isl_dim_out
);
6454 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6455 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6460 isl_space
*dim
= isl_map_get_space(map
);
6462 return isl_pw_aff_empty(dim
);
6465 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6466 for (i
= 1; i
< map
->n
; ++i
) {
6467 isl_pw_aff
*pwaff_i
;
6469 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6470 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6478 /* Compute the maximum of the given output dimension as a function of the
6479 * parameters and input dimensions, but independently of
6480 * the other output dimensions.
6482 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6484 return map_dim_opt(map
, pos
, 1);
6487 /* Compute the minimum or maximum of the given set dimension
6488 * as a function of the parameters,
6489 * but independently of the other set dimensions.
6491 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6494 return map_dim_opt(set
, pos
, max
);
6497 /* Compute the maximum of the given set dimension as a function of the
6498 * parameters, but independently of the other set dimensions.
6500 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6502 return set_dim_opt(set
, pos
, 1);
6505 /* Compute the minimum of the given set dimension as a function of the
6506 * parameters, but independently of the other set dimensions.
6508 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6510 return set_dim_opt(set
, pos
, 0);
6513 /* Apply a preimage specified by "mat" on the parameters of "bset".
6514 * bset is assumed to have only parameters and divs.
6516 static struct isl_basic_set
*basic_set_parameter_preimage(
6517 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6524 bset
->dim
= isl_space_cow(bset
->dim
);
6528 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6530 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6532 bset
->dim
->nparam
= 0;
6533 bset
->dim
->n_out
= nparam
;
6534 bset
= isl_basic_set_preimage(bset
, mat
);
6536 bset
->dim
->nparam
= bset
->dim
->n_out
;
6537 bset
->dim
->n_out
= 0;
6542 isl_basic_set_free(bset
);
6546 /* Apply a preimage specified by "mat" on the parameters of "set".
6547 * set is assumed to have only parameters and divs.
6549 static struct isl_set
*set_parameter_preimage(
6550 struct isl_set
*set
, struct isl_mat
*mat
)
6552 isl_space
*dim
= NULL
;
6558 dim
= isl_space_copy(set
->dim
);
6559 dim
= isl_space_cow(dim
);
6563 nparam
= isl_set_dim(set
, isl_dim_param
);
6565 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6568 dim
->n_out
= nparam
;
6569 isl_set_reset_space(set
, dim
);
6570 set
= isl_set_preimage(set
, mat
);
6573 dim
= isl_space_copy(set
->dim
);
6574 dim
= isl_space_cow(dim
);
6577 dim
->nparam
= dim
->n_out
;
6579 isl_set_reset_space(set
, dim
);
6582 isl_space_free(dim
);
6589 /* Intersect the basic set "bset" with the affine space specified by the
6590 * equalities in "eq".
6592 static struct isl_basic_set
*basic_set_append_equalities(
6593 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6601 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6606 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6607 for (i
= 0; i
< eq
->n_row
; ++i
) {
6608 k
= isl_basic_set_alloc_equality(bset
);
6611 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6612 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6616 bset
= isl_basic_set_gauss(bset
, NULL
);
6617 bset
= isl_basic_set_finalize(bset
);
6622 isl_basic_set_free(bset
);
6626 /* Intersect the set "set" with the affine space specified by the
6627 * equalities in "eq".
6629 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6637 for (i
= 0; i
< set
->n
; ++i
) {
6638 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6651 /* Given a basic set "bset" that only involves parameters and existentially
6652 * quantified variables, return the index of the first equality
6653 * that only involves parameters. If there is no such equality then
6654 * return bset->n_eq.
6656 * This function assumes that isl_basic_set_gauss has been called on "bset".
6658 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6661 unsigned nparam
, n_div
;
6666 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6667 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6669 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6670 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6677 /* Compute an explicit representation for the existentially quantified
6678 * variables in "bset" by computing the "minimal value" of the set
6679 * variables. Since there are no set variables, the computation of
6680 * the minimal value essentially computes an explicit representation
6681 * of the non-empty part(s) of "bset".
6683 * The input only involves parameters and existentially quantified variables.
6684 * All equalities among parameters have been removed.
6686 * Since the existentially quantified variables in the result are in general
6687 * going to be different from those in the input, we first replace
6688 * them by the minimal number of variables based on their equalities.
6689 * This should simplify the parametric integer programming.
6691 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6693 isl_morph
*morph1
, *morph2
;
6699 if (bset
->n_eq
== 0)
6700 return isl_basic_set_lexmin(bset
);
6702 morph1
= isl_basic_set_parameter_compression(bset
);
6703 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6704 bset
= isl_basic_set_lift(bset
);
6705 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6706 bset
= isl_morph_basic_set(morph2
, bset
);
6707 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6708 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6710 set
= isl_basic_set_lexmin(bset
);
6712 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6717 /* Project the given basic set onto its parameter domain, possibly introducing
6718 * new, explicit, existential variables in the constraints.
6719 * The input has parameters and (possibly implicit) existential variables.
6720 * The output has the same parameters, but only
6721 * explicit existentially quantified variables.
6723 * The actual projection is performed by pip, but pip doesn't seem
6724 * to like equalities very much, so we first remove the equalities
6725 * among the parameters by performing a variable compression on
6726 * the parameters. Afterward, an inverse transformation is performed
6727 * and the equalities among the parameters are inserted back in.
6729 * The variable compression on the parameters may uncover additional
6730 * equalities that were only implicit before. We therefore check
6731 * if there are any new parameter equalities in the result and
6732 * if so recurse. The removal of parameter equalities is required
6733 * for the parameter compression performed by base_compute_divs.
6735 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6739 struct isl_mat
*T
, *T2
;
6740 struct isl_set
*set
;
6743 bset
= isl_basic_set_cow(bset
);
6747 if (bset
->n_eq
== 0)
6748 return base_compute_divs(bset
);
6750 bset
= isl_basic_set_gauss(bset
, NULL
);
6753 if (isl_basic_set_plain_is_empty(bset
))
6754 return isl_set_from_basic_set(bset
);
6756 i
= first_parameter_equality(bset
);
6757 if (i
== bset
->n_eq
)
6758 return base_compute_divs(bset
);
6760 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6761 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6763 eq
= isl_mat_cow(eq
);
6764 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6765 if (T
&& T
->n_col
== 0) {
6769 bset
= isl_basic_set_set_to_empty(bset
);
6770 return isl_set_from_basic_set(bset
);
6772 bset
= basic_set_parameter_preimage(bset
, T
);
6774 i
= first_parameter_equality(bset
);
6777 else if (i
== bset
->n_eq
)
6778 set
= base_compute_divs(bset
);
6780 set
= parameter_compute_divs(bset
);
6781 set
= set_parameter_preimage(set
, T2
);
6782 set
= set_append_equalities(set
, eq
);
6786 /* Insert the divs from "ls" before those of "bmap".
6788 * The number of columns is not changed, which means that the last
6789 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6790 * The caller is responsible for removing the same number of dimensions
6791 * from the space of "bmap".
6793 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6794 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6800 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6804 old_n_div
= bmap
->n_div
;
6805 bmap
= insert_div_rows(bmap
, n_div
);
6809 for (i
= 0; i
< n_div
; ++i
) {
6810 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6811 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6817 /* Replace the space of "bmap" by the space and divs of "ls".
6819 * If "ls" has any divs, then we simplify the result since we may
6820 * have discovered some additional equalities that could simplify
6821 * the div expressions.
6823 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6824 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6828 bmap
= isl_basic_map_cow(bmap
);
6832 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6833 bmap
= insert_divs_from_local_space(bmap
, ls
);
6837 isl_space_free(bmap
->dim
);
6838 bmap
->dim
= isl_local_space_get_space(ls
);
6842 isl_local_space_free(ls
);
6844 bmap
= isl_basic_map_simplify(bmap
);
6845 bmap
= isl_basic_map_finalize(bmap
);
6848 isl_basic_map_free(bmap
);
6849 isl_local_space_free(ls
);
6853 /* Replace the space of "map" by the space and divs of "ls".
6855 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6856 __isl_take isl_local_space
*ls
)
6860 map
= isl_map_cow(map
);
6864 for (i
= 0; i
< map
->n
; ++i
) {
6865 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6866 isl_local_space_copy(ls
));
6870 isl_space_free(map
->dim
);
6871 map
->dim
= isl_local_space_get_space(ls
);
6875 isl_local_space_free(ls
);
6878 isl_local_space_free(ls
);
6883 /* Compute an explicit representation for the existentially
6884 * quantified variables for which do not know any explicit representation yet.
6886 * We first sort the existentially quantified variables so that the
6887 * existentially quantified variables for which we already have an explicit
6888 * representation are placed before those for which we do not.
6889 * The input dimensions, the output dimensions and the existentially
6890 * quantified variables for which we already have an explicit
6891 * representation are then turned into parameters.
6892 * compute_divs returns a map with the same parameters and
6893 * no input or output dimensions and the dimension specification
6894 * is reset to that of the input, including the existentially quantified
6895 * variables for which we already had an explicit representation.
6897 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6899 struct isl_basic_set
*bset
;
6900 struct isl_set
*set
;
6901 struct isl_map
*map
;
6903 isl_local_space
*ls
;
6910 bmap
= isl_basic_map_sort_divs(bmap
);
6911 bmap
= isl_basic_map_cow(bmap
);
6915 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6916 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6919 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6920 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6921 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6922 dim
= isl_space_set_alloc(bmap
->ctx
,
6923 nparam
+ n_in
+ n_out
+ n_known
, 0);
6927 ls
= isl_basic_map_get_local_space(bmap
);
6928 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6929 n_known
, bmap
->n_div
- n_known
);
6931 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6932 swap_div(bmap
, i
- n_known
, i
);
6933 bmap
->n_div
-= n_known
;
6934 bmap
->extra
-= n_known
;
6936 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6937 bset
= (struct isl_basic_set
*)bmap
;
6939 set
= parameter_compute_divs(bset
);
6940 map
= (struct isl_map
*)set
;
6941 map
= replace_space_by_local_space(map
, ls
);
6945 isl_basic_map_free(bmap
);
6949 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6957 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6958 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6959 if (isl_int_is_zero(bmap
->div
[i
][0]))
6961 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6967 static int map_divs_known(__isl_keep isl_map
*map
)
6974 for (i
= 0; i
< map
->n
; ++i
) {
6975 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6983 /* If bmap contains any unknown divs, then compute explicit
6984 * expressions for them. However, this computation may be
6985 * quite expensive, so first try to remove divs that aren't
6988 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6991 struct isl_map
*map
;
6993 known
= isl_basic_map_divs_known(bmap
);
6997 return isl_map_from_basic_map(bmap
);
6999 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7001 known
= isl_basic_map_divs_known(bmap
);
7005 return isl_map_from_basic_map(bmap
);
7007 map
= compute_divs(bmap
);
7010 isl_basic_map_free(bmap
);
7014 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7018 struct isl_map
*res
;
7025 known
= map_divs_known(map
);
7033 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7034 for (i
= 1 ; i
< map
->n
; ++i
) {
7036 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7037 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7038 res
= isl_map_union_disjoint(res
, r2
);
7040 res
= isl_map_union(res
, r2
);
7047 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7049 return (struct isl_set
*)
7050 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7053 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7055 return (struct isl_set
*)
7056 isl_map_compute_divs((struct isl_map
*)set
);
7059 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7062 struct isl_set
*set
;
7067 map
= isl_map_cow(map
);
7071 set
= (struct isl_set
*)map
;
7072 set
->dim
= isl_space_domain(set
->dim
);
7075 for (i
= 0; i
< map
->n
; ++i
) {
7076 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7080 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7081 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7088 /* Return the union of "map1" and "map2", where we assume for now that
7089 * "map1" and "map2" are disjoint. Note that the basic maps inside
7090 * "map1" or "map2" may not be disjoint from each other.
7091 * Also note that this function is also called from isl_map_union,
7092 * which takes care of handling the situation where "map1" and "map2"
7093 * may not be disjoint.
7095 * If one of the inputs is empty, we can simply return the other input.
7096 * Similarly, if one of the inputs is universal, then it is equal to the union.
7098 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7099 __isl_take isl_map
*map2
)
7103 struct isl_map
*map
= NULL
;
7118 is_universe
= isl_map_plain_is_universe(map1
);
7119 if (is_universe
< 0)
7126 is_universe
= isl_map_plain_is_universe(map2
);
7127 if (is_universe
< 0)
7134 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
7136 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7137 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7138 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7140 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7141 map1
->n
+ map2
->n
, flags
);
7144 for (i
= 0; i
< map1
->n
; ++i
) {
7145 map
= isl_map_add_basic_map(map
,
7146 isl_basic_map_copy(map1
->p
[i
]));
7150 for (i
= 0; i
< map2
->n
; ++i
) {
7151 map
= isl_map_add_basic_map(map
,
7152 isl_basic_map_copy(map2
->p
[i
]));
7166 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7167 * guaranteed to be disjoint by the caller.
7169 * Note that this functions is called from within isl_map_make_disjoint,
7170 * so we have to be careful not to touch the constraints of the inputs
7173 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7174 __isl_take isl_map
*map2
)
7176 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7179 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7180 * not be disjoint. The parameters are assumed to have been aligned.
7182 * We currently simply call map_union_disjoint, the internal operation
7183 * of which does not really depend on the inputs being disjoint.
7184 * If the result contains more than one basic map, then we clear
7185 * the disjoint flag since the result may contain basic maps from
7186 * both inputs and these are not guaranteed to be disjoint.
7188 * As a special case, if "map1" and "map2" are obviously equal,
7189 * then we simply return "map1".
7191 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7192 __isl_take isl_map
*map2
)
7199 equal
= isl_map_plain_is_equal(map1
, map2
);
7207 map1
= map_union_disjoint(map1
, map2
);
7211 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7219 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7222 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7223 __isl_take isl_map
*map2
)
7225 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7228 struct isl_set
*isl_set_union_disjoint(
7229 struct isl_set
*set1
, struct isl_set
*set2
)
7231 return (struct isl_set
*)
7232 isl_map_union_disjoint(
7233 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7236 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7238 return (struct isl_set
*)
7239 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7242 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7245 * "map" and "set" are assumed to be compatible and non-NULL.
7247 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7248 __isl_take isl_set
*set
,
7249 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7250 __isl_take isl_basic_set
*bset
))
7253 struct isl_map
*result
;
7256 if (isl_set_plain_is_universe(set
)) {
7261 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7262 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7263 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7265 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7266 map
->n
* set
->n
, flags
);
7267 for (i
= 0; result
&& i
< map
->n
; ++i
)
7268 for (j
= 0; j
< set
->n
; ++j
) {
7269 result
= isl_map_add_basic_map(result
,
7270 fn(isl_basic_map_copy(map
->p
[i
]),
7271 isl_basic_set_copy(set
->p
[j
])));
7281 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7282 __isl_take isl_set
*set
)
7287 if (!isl_map_compatible_range(map
, set
))
7288 isl_die(set
->ctx
, isl_error_invalid
,
7289 "incompatible spaces", goto error
);
7291 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7298 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7299 __isl_take isl_set
*set
)
7301 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7304 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7305 __isl_take isl_set
*set
)
7310 if (!isl_map_compatible_domain(map
, set
))
7311 isl_die(set
->ctx
, isl_error_invalid
,
7312 "incompatible spaces", goto error
);
7314 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7321 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7322 __isl_take isl_set
*set
)
7324 return isl_map_align_params_map_map_and(map
, set
,
7325 &map_intersect_domain
);
7328 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7329 __isl_take isl_map
*map2
)
7333 map1
= isl_map_reverse(map1
);
7334 map1
= isl_map_apply_range(map1
, map2
);
7335 return isl_map_reverse(map1
);
7342 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7343 __isl_take isl_map
*map2
)
7345 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7348 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7349 __isl_take isl_map
*map2
)
7351 isl_space
*dim_result
;
7352 struct isl_map
*result
;
7358 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7359 isl_space_copy(map2
->dim
));
7361 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7364 for (i
= 0; i
< map1
->n
; ++i
)
7365 for (j
= 0; j
< map2
->n
; ++j
) {
7366 result
= isl_map_add_basic_map(result
,
7367 isl_basic_map_apply_range(
7368 isl_basic_map_copy(map1
->p
[i
]),
7369 isl_basic_map_copy(map2
->p
[j
])));
7375 if (result
&& result
->n
<= 1)
7376 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7384 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7385 __isl_take isl_map
*map2
)
7387 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7391 * returns range - domain
7393 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7395 isl_space
*dims
, *target_dim
;
7396 struct isl_basic_set
*bset
;
7403 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7404 bmap
->dim
, isl_dim_out
),
7406 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7407 dim
= isl_basic_map_n_in(bmap
);
7408 nparam
= isl_basic_map_n_param(bmap
);
7409 bset
= isl_basic_set_from_basic_map(bmap
);
7410 bset
= isl_basic_set_cow(bset
);
7411 dims
= isl_basic_set_get_space(bset
);
7412 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7413 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7414 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7415 for (i
= 0; i
< dim
; ++i
) {
7416 int j
= isl_basic_map_alloc_equality(
7417 (struct isl_basic_map
*)bset
);
7419 bset
= isl_basic_set_free(bset
);
7422 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7423 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7424 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7425 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7427 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7428 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7431 isl_basic_map_free(bmap
);
7436 * returns range - domain
7438 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7442 struct isl_set
*result
;
7447 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7448 map
->dim
, isl_dim_out
),
7450 dim
= isl_map_get_space(map
);
7451 dim
= isl_space_domain(dim
);
7452 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7455 for (i
= 0; i
< map
->n
; ++i
)
7456 result
= isl_set_add_basic_set(result
,
7457 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7466 * returns [domain -> range] -> range - domain
7468 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7469 __isl_take isl_basic_map
*bmap
)
7473 isl_basic_map
*domain
;
7477 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7478 isl_die(bmap
->ctx
, isl_error_invalid
,
7479 "domain and range don't match", goto error
);
7481 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7482 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7484 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7485 domain
= isl_basic_map_universe(dim
);
7487 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7488 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7489 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7491 total
= isl_basic_map_total_dim(bmap
);
7493 for (i
= 0; i
< n
; ++i
) {
7494 k
= isl_basic_map_alloc_equality(bmap
);
7497 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7498 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7499 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7500 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7503 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7504 return isl_basic_map_finalize(bmap
);
7506 isl_basic_map_free(bmap
);
7511 * returns [domain -> range] -> range - domain
7513 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7516 isl_space
*domain_dim
;
7521 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7522 isl_die(map
->ctx
, isl_error_invalid
,
7523 "domain and range don't match", goto error
);
7525 map
= isl_map_cow(map
);
7529 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7530 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7531 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7534 for (i
= 0; i
< map
->n
; ++i
) {
7535 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7539 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7546 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7548 struct isl_basic_map
*bmap
;
7556 nparam
= dims
->nparam
;
7558 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7562 for (i
= 0; i
< dim
; ++i
) {
7563 int j
= isl_basic_map_alloc_equality(bmap
);
7566 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7567 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7568 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7570 return isl_basic_map_finalize(bmap
);
7572 isl_basic_map_free(bmap
);
7576 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7580 if (dim
->n_in
!= dim
->n_out
)
7581 isl_die(dim
->ctx
, isl_error_invalid
,
7582 "number of input and output dimensions needs to be "
7583 "the same", goto error
);
7584 return basic_map_identity(dim
);
7586 isl_space_free(dim
);
7590 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7592 if (!model
|| !model
->dim
)
7594 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7597 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7599 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7602 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7604 if (!model
|| !model
->dim
)
7606 return isl_map_identity(isl_space_copy(model
->dim
));
7609 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7611 if (!model
|| !model
->dim
)
7613 return isl_map_identity(isl_space_copy(model
->dim
));
7616 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7618 isl_space
*dim
= isl_set_get_space(set
);
7620 id
= isl_map_identity(isl_space_map_from_set(dim
));
7621 return isl_map_intersect_range(id
, set
);
7624 /* Construct a basic set with all set dimensions having only non-negative
7627 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7628 __isl_take isl_space
*space
)
7633 struct isl_basic_set
*bset
;
7637 nparam
= space
->nparam
;
7639 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7642 for (i
= 0; i
< dim
; ++i
) {
7643 int k
= isl_basic_set_alloc_inequality(bset
);
7646 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7647 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7651 isl_basic_set_free(bset
);
7655 /* Construct the half-space x_pos >= 0.
7657 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7661 isl_basic_set
*nonneg
;
7663 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7664 k
= isl_basic_set_alloc_inequality(nonneg
);
7667 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7668 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7670 return isl_basic_set_finalize(nonneg
);
7672 isl_basic_set_free(nonneg
);
7676 /* Construct the half-space x_pos <= -1.
7678 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7683 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7684 k
= isl_basic_set_alloc_inequality(neg
);
7687 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7688 isl_int_set_si(neg
->ineq
[k
][0], -1);
7689 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7691 return isl_basic_set_finalize(neg
);
7693 isl_basic_set_free(neg
);
7697 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7698 enum isl_dim_type type
, unsigned first
, unsigned n
)
7701 isl_basic_set
*nonneg
;
7709 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7711 for (i
= 0; i
< n
; ++i
) {
7712 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7713 pos(set
->dim
, type
) + first
+ i
);
7714 neg
= neg_halfspace(isl_set_get_space(set
),
7715 pos(set
->dim
, type
) + first
+ i
);
7717 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7726 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7727 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7734 if (isl_set_plain_is_empty(set
)) {
7739 return fn(set
, signs
, user
);
7742 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7744 half
= isl_set_intersect(half
, isl_set_copy(set
));
7745 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7749 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7751 half
= isl_set_intersect(half
, set
);
7752 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7758 /* Call "fn" on the intersections of "set" with each of the orthants
7759 * (except for obviously empty intersections). The orthant is identified
7760 * by the signs array, with each entry having value 1 or -1 according
7761 * to the sign of the corresponding variable.
7763 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7764 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7774 if (isl_set_plain_is_empty(set
))
7777 nparam
= isl_set_dim(set
, isl_dim_param
);
7778 nvar
= isl_set_dim(set
, isl_dim_set
);
7780 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7782 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7790 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7792 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7795 int isl_basic_map_is_subset(
7796 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7799 struct isl_map
*map1
;
7800 struct isl_map
*map2
;
7802 if (!bmap1
|| !bmap2
)
7805 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7806 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7808 is_subset
= isl_map_is_subset(map1
, map2
);
7816 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7817 __isl_keep isl_basic_set
*bset2
)
7819 return isl_basic_map_is_subset(bset1
, bset2
);
7822 int isl_basic_map_is_equal(
7823 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7827 if (!bmap1
|| !bmap2
)
7829 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7832 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7836 int isl_basic_set_is_equal(
7837 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7839 return isl_basic_map_is_equal(
7840 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7843 int isl_map_is_empty(struct isl_map
*map
)
7850 for (i
= 0; i
< map
->n
; ++i
) {
7851 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7860 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7862 return map
? map
->n
== 0 : -1;
7865 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7867 return isl_map_plain_is_empty(map
);
7870 int isl_set_plain_is_empty(struct isl_set
*set
)
7872 return set
? set
->n
== 0 : -1;
7875 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7877 return isl_set_plain_is_empty(set
);
7880 int isl_set_is_empty(struct isl_set
*set
)
7882 return isl_map_is_empty((struct isl_map
*)set
);
7885 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7890 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7893 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7898 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7901 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7907 is_subset
= isl_map_is_subset(map1
, map2
);
7910 is_subset
= isl_map_is_subset(map2
, map1
);
7914 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7916 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7919 int isl_basic_map_is_strict_subset(
7920 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7924 if (!bmap1
|| !bmap2
)
7926 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7929 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7930 if (is_subset
== -1)
7935 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7941 is_subset
= isl_map_is_subset(map1
, map2
);
7944 is_subset
= isl_map_is_subset(map2
, map1
);
7945 if (is_subset
== -1)
7950 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7952 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7955 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7959 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7962 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7966 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7969 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7976 for (i
= 0; i
< map
->n
; ++i
) {
7977 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7985 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7987 return isl_map_plain_is_universe((isl_map
*) set
);
7990 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7992 return isl_set_plain_is_universe(set
);
7995 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7997 struct isl_basic_set
*bset
= NULL
;
7998 struct isl_vec
*sample
= NULL
;
8005 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8008 if (isl_basic_map_is_universe(bmap
))
8011 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8012 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8013 copy
= isl_basic_map_remove_redundancies(copy
);
8014 empty
= isl_basic_map_plain_is_empty(copy
);
8015 isl_basic_map_free(copy
);
8019 total
= 1 + isl_basic_map_total_dim(bmap
);
8020 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
8021 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
8027 isl_vec_free(bmap
->sample
);
8028 bmap
->sample
= NULL
;
8029 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8032 sample
= isl_basic_set_sample_vec(bset
);
8035 empty
= sample
->size
== 0;
8036 isl_vec_free(bmap
->sample
);
8037 bmap
->sample
= sample
;
8039 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8044 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8048 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8051 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
8053 return isl_basic_map_plain_is_empty(bmap
);
8056 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8060 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8063 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8065 return isl_basic_set_plain_is_empty(bset
);
8068 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8070 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8073 struct isl_map
*isl_basic_map_union(
8074 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8076 struct isl_map
*map
;
8077 if (!bmap1
|| !bmap2
)
8080 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8082 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8085 map
= isl_map_add_basic_map(map
, bmap1
);
8086 map
= isl_map_add_basic_map(map
, bmap2
);
8089 isl_basic_map_free(bmap1
);
8090 isl_basic_map_free(bmap2
);
8094 struct isl_set
*isl_basic_set_union(
8095 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8097 return (struct isl_set
*)isl_basic_map_union(
8098 (struct isl_basic_map
*)bset1
,
8099 (struct isl_basic_map
*)bset2
);
8102 /* Order divs such that any div only depends on previous divs */
8103 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8111 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8113 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8115 if (isl_int_is_zero(bmap
->div
[i
][0]))
8117 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8121 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8127 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8129 return (struct isl_basic_set
*)
8130 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8133 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8140 for (i
= 0; i
< map
->n
; ++i
) {
8141 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8152 /* Apply the expansion computed by isl_merge_divs.
8153 * The expansion itself is given by "exp" while the resulting
8154 * list of divs is given by "div".
8156 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8157 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8162 bset
= isl_basic_set_cow(bset
);
8166 if (div
->n_row
< bset
->n_div
)
8167 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8168 "not an expansion", goto error
);
8170 n_div
= bset
->n_div
;
8171 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8172 div
->n_row
- n_div
, 0,
8173 2 * (div
->n_row
- n_div
));
8175 for (i
= n_div
; i
< div
->n_row
; ++i
)
8176 if (isl_basic_set_alloc_div(bset
) < 0)
8180 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8181 if (j
>= 0 && exp
[j
] == i
) {
8183 isl_basic_map_swap_div(bset
, i
, j
);
8186 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8187 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8195 isl_basic_set_free(bset
);
8200 /* Look for a div in dst that corresponds to the div "div" in src.
8201 * The divs before "div" in src and dst are assumed to be the same.
8203 * Returns -1 if no corresponding div was found and the position
8204 * of the corresponding div in dst otherwise.
8206 static int find_div(struct isl_basic_map
*dst
,
8207 struct isl_basic_map
*src
, unsigned div
)
8211 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8213 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8214 for (i
= div
; i
< dst
->n_div
; ++i
)
8215 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8216 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8217 dst
->n_div
- div
) == -1)
8222 struct isl_basic_map
*isl_basic_map_align_divs(
8223 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
8231 if (src
->n_div
== 0)
8234 for (i
= 0; i
< src
->n_div
; ++i
)
8235 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
8237 src
= isl_basic_map_order_divs(src
);
8238 dst
= isl_basic_map_cow(dst
);
8241 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
8242 src
->n_div
, 0, 2 * src
->n_div
);
8245 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8246 for (i
= 0; i
< src
->n_div
; ++i
) {
8247 int j
= find_div(dst
, src
, i
);
8249 j
= isl_basic_map_alloc_div(dst
);
8252 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8253 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8254 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8258 isl_basic_map_swap_div(dst
, i
, j
);
8262 isl_basic_map_free(dst
);
8266 struct isl_basic_set
*isl_basic_set_align_divs(
8267 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8269 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8270 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8273 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8281 map
= isl_map_compute_divs(map
);
8282 map
= isl_map_cow(map
);
8286 for (i
= 1; i
< map
->n
; ++i
)
8287 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8288 for (i
= 1; i
< map
->n
; ++i
) {
8289 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8291 return isl_map_free(map
);
8294 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8298 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8300 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8303 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8304 __isl_take isl_map
*map
)
8308 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8309 map
= isl_map_intersect_domain(map
, set
);
8310 set
= isl_map_range(map
);
8318 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8319 __isl_take isl_map
*map
)
8321 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8324 /* There is no need to cow as removing empty parts doesn't change
8325 * the meaning of the set.
8327 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8334 for (i
= map
->n
- 1; i
>= 0; --i
)
8335 remove_if_empty(map
, i
);
8340 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8342 return (struct isl_set
*)
8343 isl_map_remove_empty_parts((struct isl_map
*)set
);
8346 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8348 struct isl_basic_map
*bmap
;
8349 if (!map
|| map
->n
== 0)
8351 bmap
= map
->p
[map
->n
-1];
8352 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8353 return isl_basic_map_copy(bmap
);
8356 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8358 return (struct isl_basic_set
*)
8359 isl_map_copy_basic_map((struct isl_map
*)set
);
8362 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8363 __isl_keep isl_basic_map
*bmap
)
8369 for (i
= map
->n
-1; i
>= 0; --i
) {
8370 if (map
->p
[i
] != bmap
)
8372 map
= isl_map_cow(map
);
8375 isl_basic_map_free(map
->p
[i
]);
8376 if (i
!= map
->n
-1) {
8377 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8378 map
->p
[i
] = map
->p
[map
->n
-1];
8389 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8390 struct isl_basic_set
*bset
)
8392 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8393 (struct isl_basic_map
*)bset
);
8396 /* Given two basic sets bset1 and bset2, compute the maximal difference
8397 * between the values of dimension pos in bset1 and those in bset2
8398 * for any common value of the parameters and dimensions preceding pos.
8400 static enum isl_lp_result
basic_set_maximal_difference_at(
8401 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8402 int pos
, isl_int
*opt
)
8405 struct isl_basic_map
*bmap1
= NULL
;
8406 struct isl_basic_map
*bmap2
= NULL
;
8407 struct isl_ctx
*ctx
;
8408 struct isl_vec
*obj
;
8411 unsigned dim1
, dim2
;
8412 enum isl_lp_result res
;
8414 if (!bset1
|| !bset2
)
8415 return isl_lp_error
;
8417 nparam
= isl_basic_set_n_param(bset1
);
8418 dim1
= isl_basic_set_n_dim(bset1
);
8419 dim2
= isl_basic_set_n_dim(bset2
);
8420 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8421 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8422 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8423 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8424 if (!bmap1
|| !bmap2
)
8426 bmap1
= isl_basic_map_cow(bmap1
);
8427 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8428 pos
, (dim1
- pos
) + (dim2
- pos
),
8429 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8430 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8433 total
= isl_basic_map_total_dim(bmap1
);
8435 obj
= isl_vec_alloc(ctx
, 1 + total
);
8438 isl_seq_clr(obj
->block
.data
, 1 + total
);
8439 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8440 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8441 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8443 isl_basic_map_free(bmap1
);
8447 isl_basic_map_free(bmap2
);
8449 isl_basic_map_free(bmap1
);
8450 return isl_lp_error
;
8453 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8454 * for any common value of the parameters and dimensions preceding pos
8455 * in both basic sets, the values of dimension pos in bset1 are
8456 * smaller or larger than those in bset2.
8459 * 1 if bset1 follows bset2
8460 * -1 if bset1 precedes bset2
8461 * 0 if bset1 and bset2 are incomparable
8462 * -2 if some error occurred.
8464 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8465 struct isl_basic_set
*bset2
, int pos
)
8468 enum isl_lp_result res
;
8473 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8475 if (res
== isl_lp_empty
)
8477 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8478 res
== isl_lp_unbounded
)
8480 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8489 /* Given two basic sets bset1 and bset2, check whether
8490 * for any common value of the parameters and dimensions preceding pos
8491 * there is a value of dimension pos in bset1 that is larger
8492 * than a value of the same dimension in bset2.
8495 * 1 if there exists such a pair
8496 * 0 if there is no such pair, but there is a pair of equal values
8498 * -2 if some error occurred.
8500 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8501 __isl_keep isl_basic_set
*bset2
, int pos
)
8504 enum isl_lp_result res
;
8509 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8511 if (res
== isl_lp_empty
)
8513 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8514 res
== isl_lp_unbounded
)
8516 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8518 else if (res
== isl_lp_ok
)
8527 /* Given two sets set1 and set2, check whether
8528 * for any common value of the parameters and dimensions preceding pos
8529 * there is a value of dimension pos in set1 that is larger
8530 * than a value of the same dimension in set2.
8533 * 1 if there exists such a pair
8534 * 0 if there is no such pair, but there is a pair of equal values
8536 * -2 if some error occurred.
8538 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8539 __isl_keep isl_set
*set2
, int pos
)
8547 for (i
= 0; i
< set1
->n
; ++i
)
8548 for (j
= 0; j
< set2
->n
; ++j
) {
8550 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8551 if (f
== 1 || f
== -2)
8560 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8561 unsigned pos
, isl_int
*val
)
8569 total
= isl_basic_map_total_dim(bmap
);
8570 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8571 for (; d
+1 > pos
; --d
)
8572 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8576 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8578 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8580 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8583 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8589 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8590 unsigned pos
, isl_int
*val
)
8602 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8605 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8606 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8607 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8608 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8612 isl_int_set(*val
, v
);
8618 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8619 unsigned pos
, isl_int
*val
)
8621 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8625 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8628 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8631 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8632 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8634 if (pos
>= isl_basic_map_dim(bmap
, type
))
8636 return isl_basic_map_plain_has_fixed_var(bmap
,
8637 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8640 /* If "bmap" obviously lies on a hyperplane where the given dimension
8641 * has a fixed value, then return that value.
8642 * Otherwise return NaN.
8644 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8645 __isl_keep isl_basic_map
*bmap
,
8646 enum isl_dim_type type
, unsigned pos
)
8654 ctx
= isl_basic_map_get_ctx(bmap
);
8655 v
= isl_val_alloc(ctx
);
8658 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8660 return isl_val_free(v
);
8662 isl_int_set_si(v
->d
, 1);
8666 return isl_val_nan(ctx
);
8669 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8670 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8672 if (pos
>= isl_map_dim(map
, type
))
8674 return isl_map_plain_has_fixed_var(map
,
8675 map_offset(map
, type
) - 1 + pos
, val
);
8678 /* If "map" obviously lies on a hyperplane where the given dimension
8679 * has a fixed value, then return that value.
8680 * Otherwise return NaN.
8682 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8683 enum isl_dim_type type
, unsigned pos
)
8691 ctx
= isl_map_get_ctx(map
);
8692 v
= isl_val_alloc(ctx
);
8695 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8697 return isl_val_free(v
);
8699 isl_int_set_si(v
->d
, 1);
8703 return isl_val_nan(ctx
);
8706 /* If "set" obviously lies on a hyperplane where the given dimension
8707 * has a fixed value, then return that value.
8708 * Otherwise return NaN.
8710 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8711 enum isl_dim_type type
, unsigned pos
)
8713 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8716 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8717 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8719 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8722 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8723 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8725 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8728 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8729 * then return this fixed value in *val.
8731 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8732 unsigned dim
, isl_int
*val
)
8734 return isl_basic_set_plain_has_fixed_var(bset
,
8735 isl_basic_set_n_param(bset
) + dim
, val
);
8738 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8739 * then return this fixed value in *val.
8741 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8742 unsigned dim
, isl_int
*val
)
8744 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8747 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8748 unsigned dim
, isl_int
*val
)
8750 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8753 /* Check if input variable in has fixed value and if so and if val is not NULL,
8754 * then return this fixed value in *val.
8756 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8757 unsigned in
, isl_int
*val
)
8759 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8762 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8763 * and if val is not NULL, then return this lower bound in *val.
8765 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8766 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8768 int i
, i_eq
= -1, i_ineq
= -1;
8775 total
= isl_basic_set_total_dim(bset
);
8776 nparam
= isl_basic_set_n_param(bset
);
8777 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8778 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8784 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8785 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8787 if (i_eq
!= -1 || i_ineq
!= -1)
8791 if (i_eq
== -1 && i_ineq
== -1)
8793 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8794 /* The coefficient should always be one due to normalization. */
8795 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8797 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8799 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8800 total
- nparam
- dim
- 1) != -1)
8803 isl_int_neg(*val
, c
[0]);
8807 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8808 unsigned dim
, isl_int
*val
)
8820 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8824 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8826 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8827 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8829 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8833 isl_int_set(*val
, v
);
8844 /* uset_gist depends on constraints without existentially quantified
8845 * variables sorting first.
8847 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8849 const struct constraint
*c1
= (const struct constraint
*)p1
;
8850 const struct constraint
*c2
= (const struct constraint
*)p2
;
8852 unsigned size
= isl_min(c1
->size
, c2
->size
);
8854 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8855 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8860 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8863 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8864 struct isl_basic_map
*bmap
)
8867 struct constraint
*c
;
8872 if (bmap
->n_ineq
== 0)
8874 total
= isl_basic_map_total_dim(bmap
);
8875 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8878 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8880 c
[i
].c
= bmap
->ineq
[i
];
8882 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8883 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8884 bmap
->ineq
[i
] = c
[i
].c
;
8888 isl_basic_map_free(bmap
);
8892 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8893 __isl_take isl_basic_set
*bset
)
8895 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8896 (struct isl_basic_map
*)bset
);
8899 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8903 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8905 bmap
= isl_basic_map_remove_redundancies(bmap
);
8906 bmap
= isl_basic_map_sort_constraints(bmap
);
8908 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8912 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8914 return (struct isl_basic_set
*)isl_basic_map_normalize(
8915 (struct isl_basic_map
*)bset
);
8918 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8919 const __isl_keep isl_basic_map
*bmap2
)
8926 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8927 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8928 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8929 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8930 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8931 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8932 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8933 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8934 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8935 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8936 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8938 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8940 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8942 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8943 return bmap1
->n_eq
- bmap2
->n_eq
;
8944 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8945 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8946 if (bmap1
->n_div
!= bmap2
->n_div
)
8947 return bmap1
->n_div
- bmap2
->n_div
;
8948 total
= isl_basic_map_total_dim(bmap1
);
8949 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8950 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8954 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8955 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8959 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8960 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8967 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8968 const __isl_keep isl_basic_set
*bset2
)
8970 return isl_basic_map_plain_cmp(bset1
, bset2
);
8973 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8979 if (set1
->n
!= set2
->n
)
8980 return set1
->n
- set2
->n
;
8982 for (i
= 0; i
< set1
->n
; ++i
) {
8983 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8991 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8992 __isl_keep isl_basic_map
*bmap2
)
8994 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8997 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8998 __isl_keep isl_basic_set
*bset2
)
9000 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9001 (isl_basic_map
*)bset2
);
9004 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9006 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9007 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9009 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9012 /* We normalize in place, but if anything goes wrong we need
9013 * to return NULL, so we need to make sure we don't change the
9014 * meaning of any possible other copies of map.
9016 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
9019 struct isl_basic_map
*bmap
;
9023 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9025 for (i
= 0; i
< map
->n
; ++i
) {
9026 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9029 isl_basic_map_free(map
->p
[i
]);
9032 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9033 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9034 map
= isl_map_remove_empty_parts(map
);
9037 for (i
= map
->n
- 1; i
>= 1; --i
) {
9038 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
9040 isl_basic_map_free(map
->p
[i
-1]);
9041 for (j
= i
; j
< map
->n
; ++j
)
9042 map
->p
[j
-1] = map
->p
[j
];
9052 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9054 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9057 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9067 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9070 map1
= isl_map_copy(map1
);
9071 map2
= isl_map_copy(map2
);
9072 map1
= isl_map_normalize(map1
);
9073 map2
= isl_map_normalize(map2
);
9076 equal
= map1
->n
== map2
->n
;
9077 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9078 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9091 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9093 return isl_map_plain_is_equal(map1
, map2
);
9096 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9098 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9099 (struct isl_map
*)set2
);
9102 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9104 return isl_set_plain_is_equal(set1
, set2
);
9107 /* Return an interval that ranges from min to max (inclusive)
9109 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9110 isl_int min
, isl_int max
)
9113 struct isl_basic_set
*bset
= NULL
;
9115 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9119 k
= isl_basic_set_alloc_inequality(bset
);
9122 isl_int_set_si(bset
->ineq
[k
][1], 1);
9123 isl_int_neg(bset
->ineq
[k
][0], min
);
9125 k
= isl_basic_set_alloc_inequality(bset
);
9128 isl_int_set_si(bset
->ineq
[k
][1], -1);
9129 isl_int_set(bset
->ineq
[k
][0], max
);
9133 isl_basic_set_free(bset
);
9137 /* Return the Cartesian product of the basic sets in list (in the given order).
9139 __isl_give isl_basic_set
*isl_basic_set_list_product(
9140 __isl_take
struct isl_basic_set_list
*list
)
9148 struct isl_basic_set
*product
= NULL
;
9152 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9153 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9154 nparam
= isl_basic_set_n_param(list
->p
[0]);
9155 dim
= isl_basic_set_n_dim(list
->p
[0]);
9156 extra
= list
->p
[0]->n_div
;
9157 n_eq
= list
->p
[0]->n_eq
;
9158 n_ineq
= list
->p
[0]->n_ineq
;
9159 for (i
= 1; i
< list
->n
; ++i
) {
9160 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9161 isl_assert(list
->ctx
,
9162 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9163 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9164 extra
+= list
->p
[i
]->n_div
;
9165 n_eq
+= list
->p
[i
]->n_eq
;
9166 n_ineq
+= list
->p
[i
]->n_ineq
;
9168 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9173 for (i
= 0; i
< list
->n
; ++i
) {
9174 isl_basic_set_add_constraints(product
,
9175 isl_basic_set_copy(list
->p
[i
]), dim
);
9176 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9178 isl_basic_set_list_free(list
);
9181 isl_basic_set_free(product
);
9182 isl_basic_set_list_free(list
);
9186 struct isl_basic_map
*isl_basic_map_product(
9187 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9189 isl_space
*dim_result
= NULL
;
9190 struct isl_basic_map
*bmap
;
9191 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9192 struct isl_dim_map
*dim_map1
, *dim_map2
;
9194 if (!bmap1
|| !bmap2
)
9197 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9198 bmap2
->dim
, isl_dim_param
), goto error
);
9199 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9200 isl_space_copy(bmap2
->dim
));
9202 in1
= isl_basic_map_n_in(bmap1
);
9203 in2
= isl_basic_map_n_in(bmap2
);
9204 out1
= isl_basic_map_n_out(bmap1
);
9205 out2
= isl_basic_map_n_out(bmap2
);
9206 nparam
= isl_basic_map_n_param(bmap1
);
9208 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9209 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9210 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9211 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9212 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9213 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9214 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9215 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9216 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9217 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9218 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9220 bmap
= isl_basic_map_alloc_space(dim_result
,
9221 bmap1
->n_div
+ bmap2
->n_div
,
9222 bmap1
->n_eq
+ bmap2
->n_eq
,
9223 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9224 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9225 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9226 bmap
= isl_basic_map_simplify(bmap
);
9227 return isl_basic_map_finalize(bmap
);
9229 isl_basic_map_free(bmap1
);
9230 isl_basic_map_free(bmap2
);
9234 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9235 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9237 isl_basic_map
*prod
;
9239 prod
= isl_basic_map_product(bmap1
, bmap2
);
9240 prod
= isl_basic_map_flatten(prod
);
9244 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9245 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9247 return isl_basic_map_flat_range_product(bset1
, bset2
);
9250 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9251 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9253 isl_space
*space_result
= NULL
;
9254 isl_basic_map
*bmap
;
9255 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9256 struct isl_dim_map
*dim_map1
, *dim_map2
;
9258 if (!bmap1
|| !bmap2
)
9261 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9262 isl_space_copy(bmap2
->dim
));
9264 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9265 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9266 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9267 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9269 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9270 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9271 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9272 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9273 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9274 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9275 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9276 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9277 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9278 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9279 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9281 bmap
= isl_basic_map_alloc_space(space_result
,
9282 bmap1
->n_div
+ bmap2
->n_div
,
9283 bmap1
->n_eq
+ bmap2
->n_eq
,
9284 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9285 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9286 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9287 bmap
= isl_basic_map_simplify(bmap
);
9288 return isl_basic_map_finalize(bmap
);
9290 isl_basic_map_free(bmap1
);
9291 isl_basic_map_free(bmap2
);
9295 __isl_give isl_basic_map
*isl_basic_map_range_product(
9296 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9298 isl_space
*dim_result
= NULL
;
9299 isl_basic_map
*bmap
;
9300 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9301 struct isl_dim_map
*dim_map1
, *dim_map2
;
9303 if (!bmap1
|| !bmap2
)
9306 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9307 bmap2
->dim
, isl_dim_param
))
9308 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9309 "parameters don't match", goto error
);
9311 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9312 isl_space_copy(bmap2
->dim
));
9314 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9315 out1
= isl_basic_map_n_out(bmap1
);
9316 out2
= isl_basic_map_n_out(bmap2
);
9317 nparam
= isl_basic_map_n_param(bmap1
);
9319 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9320 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9321 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9322 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9323 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9324 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9325 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9326 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9327 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9328 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9329 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9331 bmap
= isl_basic_map_alloc_space(dim_result
,
9332 bmap1
->n_div
+ bmap2
->n_div
,
9333 bmap1
->n_eq
+ bmap2
->n_eq
,
9334 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9335 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9336 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9337 bmap
= isl_basic_map_simplify(bmap
);
9338 return isl_basic_map_finalize(bmap
);
9340 isl_basic_map_free(bmap1
);
9341 isl_basic_map_free(bmap2
);
9345 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9346 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9348 isl_basic_map
*prod
;
9350 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9351 prod
= isl_basic_map_flatten_range(prod
);
9355 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9356 __isl_take isl_map
*map2
,
9357 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
9358 __isl_take isl_space
*right
),
9359 __isl_give isl_basic_map
*(*basic_map_product
)(
9360 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
9363 struct isl_map
*result
;
9369 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9370 map2
->dim
, isl_dim_param
), goto error
);
9372 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9373 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9374 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9376 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
9377 isl_space_copy(map2
->dim
)),
9378 map1
->n
* map2
->n
, flags
);
9381 for (i
= 0; i
< map1
->n
; ++i
)
9382 for (j
= 0; j
< map2
->n
; ++j
) {
9383 struct isl_basic_map
*part
;
9384 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9385 isl_basic_map_copy(map2
->p
[j
]));
9386 if (isl_basic_map_is_empty(part
))
9387 isl_basic_map_free(part
);
9389 result
= isl_map_add_basic_map(result
, part
);
9402 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9404 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9405 __isl_take isl_map
*map2
)
9407 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9410 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9411 __isl_take isl_map
*map2
)
9413 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9416 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9418 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9419 __isl_take isl_map
*map2
)
9423 prod
= isl_map_product(map1
, map2
);
9424 prod
= isl_map_flatten(prod
);
9428 /* Given two set A and B, construct its Cartesian product A x B.
9430 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9432 return isl_map_range_product(set1
, set2
);
9435 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9436 __isl_take isl_set
*set2
)
9438 return isl_map_flat_range_product(set1
, set2
);
9441 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9443 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9444 __isl_take isl_map
*map2
)
9446 return map_product(map1
, map2
, &isl_space_domain_product
,
9447 &isl_basic_map_domain_product
);
9450 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9452 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9453 __isl_take isl_map
*map2
)
9455 return map_product(map1
, map2
, &isl_space_range_product
,
9456 &isl_basic_map_range_product
);
9459 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9460 __isl_take isl_map
*map2
)
9462 return isl_map_align_params_map_map_and(map1
, map2
,
9463 &map_domain_product_aligned
);
9466 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9467 __isl_take isl_map
*map2
)
9469 return isl_map_align_params_map_map_and(map1
, map2
,
9470 &map_range_product_aligned
);
9473 /* Given a map A -> [B -> C], extract the map A -> B.
9475 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9482 if (!isl_space_range_is_wrapping(map
->dim
))
9483 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9484 "range is not a product", return isl_map_free(map
));
9486 space
= isl_map_get_space(map
);
9487 total
= isl_space_dim(space
, isl_dim_out
);
9488 space
= isl_space_range_factor_domain(space
);
9489 keep
= isl_space_dim(space
, isl_dim_out
);
9490 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9491 map
= isl_map_reset_space(map
, space
);
9496 /* Given a map A -> [B -> C], extract the map A -> C.
9498 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9505 if (!isl_space_range_is_wrapping(map
->dim
))
9506 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9507 "range is not a product", return isl_map_free(map
));
9509 space
= isl_map_get_space(map
);
9510 total
= isl_space_dim(space
, isl_dim_out
);
9511 space
= isl_space_range_factor_range(space
);
9512 keep
= isl_space_dim(space
, isl_dim_out
);
9513 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9514 map
= isl_map_reset_space(map
, space
);
9519 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9521 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9522 __isl_take isl_map
*map2
)
9526 prod
= isl_map_domain_product(map1
, map2
);
9527 prod
= isl_map_flatten_domain(prod
);
9531 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9533 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9534 __isl_take isl_map
*map2
)
9538 prod
= isl_map_range_product(map1
, map2
);
9539 prod
= isl_map_flatten_range(prod
);
9543 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9546 uint32_t hash
= isl_hash_init();
9551 bmap
= isl_basic_map_copy(bmap
);
9552 bmap
= isl_basic_map_normalize(bmap
);
9555 total
= isl_basic_map_total_dim(bmap
);
9556 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9557 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9559 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9560 isl_hash_hash(hash
, c_hash
);
9562 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9563 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9565 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9566 isl_hash_hash(hash
, c_hash
);
9568 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9569 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9571 if (isl_int_is_zero(bmap
->div
[i
][0]))
9573 isl_hash_byte(hash
, i
& 0xFF);
9574 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9575 isl_hash_hash(hash
, c_hash
);
9577 isl_basic_map_free(bmap
);
9581 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9583 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9586 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9593 map
= isl_map_copy(map
);
9594 map
= isl_map_normalize(map
);
9598 hash
= isl_hash_init();
9599 for (i
= 0; i
< map
->n
; ++i
) {
9601 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9602 isl_hash_hash(hash
, bmap_hash
);
9610 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9612 return isl_map_get_hash((isl_map
*)set
);
9615 /* Check if the value for dimension dim is completely determined
9616 * by the values of the other parameters and variables.
9617 * That is, check if dimension dim is involved in an equality.
9619 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9626 nparam
= isl_basic_set_n_param(bset
);
9627 for (i
= 0; i
< bset
->n_eq
; ++i
)
9628 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9633 /* Check if the value for dimension dim is completely determined
9634 * by the values of the other parameters and variables.
9635 * That is, check if dimension dim is involved in an equality
9636 * for each of the subsets.
9638 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9644 for (i
= 0; i
< set
->n
; ++i
) {
9646 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9653 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9655 return set
? set
->n
: 0;
9658 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9659 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9666 for (i
= 0; i
< map
->n
; ++i
)
9667 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9673 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9674 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9681 for (i
= 0; i
< set
->n
; ++i
)
9682 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9688 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9695 bset
= isl_basic_set_cow(bset
);
9699 dim
= isl_basic_set_get_space(bset
);
9700 dim
= isl_space_lift(dim
, bset
->n_div
);
9703 isl_space_free(bset
->dim
);
9705 bset
->extra
-= bset
->n_div
;
9708 bset
= isl_basic_set_finalize(bset
);
9712 isl_basic_set_free(bset
);
9716 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9722 set
= isl_set_align_divs(set
);
9727 set
= isl_set_cow(set
);
9731 n_div
= set
->p
[0]->n_div
;
9732 dim
= isl_set_get_space(set
);
9733 dim
= isl_space_lift(dim
, n_div
);
9736 isl_space_free(set
->dim
);
9739 for (i
= 0; i
< set
->n
; ++i
) {
9740 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9751 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9754 struct isl_basic_map
*bmap
;
9761 set
= isl_set_align_divs(set
);
9766 dim
= isl_set_get_space(set
);
9767 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9769 return isl_map_identity(isl_space_map_from_set(dim
));
9772 n_div
= set
->p
[0]->n_div
;
9773 dim
= isl_space_map_from_set(dim
);
9774 n_param
= isl_space_dim(dim
, isl_dim_param
);
9775 n_set
= isl_space_dim(dim
, isl_dim_in
);
9776 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9777 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9778 for (i
= 0; i
< n_set
; ++i
)
9779 bmap
= var_equal(bmap
, i
);
9781 total
= n_param
+ n_set
+ n_set
+ n_div
;
9782 for (i
= 0; i
< n_div
; ++i
) {
9783 k
= isl_basic_map_alloc_inequality(bmap
);
9786 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9787 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9788 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9789 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9790 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9791 set
->p
[0]->div
[i
][0]);
9793 l
= isl_basic_map_alloc_inequality(bmap
);
9796 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9797 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9798 set
->p
[0]->div
[i
][0]);
9799 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9803 bmap
= isl_basic_map_simplify(bmap
);
9804 bmap
= isl_basic_map_finalize(bmap
);
9805 return isl_map_from_basic_map(bmap
);
9808 isl_basic_map_free(bmap
);
9812 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9820 dim
= isl_basic_set_total_dim(bset
);
9821 size
+= bset
->n_eq
* (1 + dim
);
9822 size
+= bset
->n_ineq
* (1 + dim
);
9823 size
+= bset
->n_div
* (2 + dim
);
9828 int isl_set_size(__isl_keep isl_set
*set
)
9836 for (i
= 0; i
< set
->n
; ++i
)
9837 size
+= isl_basic_set_size(set
->p
[i
]);
9842 /* Check if there is any lower bound (if lower == 0) and/or upper
9843 * bound (if upper == 0) on the specified dim.
9845 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9846 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9853 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9855 pos
+= isl_basic_map_offset(bmap
, type
);
9857 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9858 if (isl_int_is_zero(bmap
->div
[i
][0]))
9860 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9864 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9865 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
9868 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9869 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
9876 return lower
&& upper
;
9879 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9880 enum isl_dim_type type
, unsigned pos
)
9882 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9885 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9886 enum isl_dim_type type
, unsigned pos
)
9888 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9891 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9892 enum isl_dim_type type
, unsigned pos
)
9894 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9897 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9898 enum isl_dim_type type
, unsigned pos
)
9905 for (i
= 0; i
< map
->n
; ++i
) {
9907 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9908 if (bounded
< 0 || !bounded
)
9915 /* Return 1 if the specified dim is involved in both an upper bound
9916 * and a lower bound.
9918 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9919 enum isl_dim_type type
, unsigned pos
)
9921 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9924 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9926 static int has_any_bound(__isl_keep isl_map
*map
,
9927 enum isl_dim_type type
, unsigned pos
,
9928 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9929 enum isl_dim_type type
, unsigned pos
))
9936 for (i
= 0; i
< map
->n
; ++i
) {
9938 bounded
= fn(map
->p
[i
], type
, pos
);
9939 if (bounded
< 0 || bounded
)
9946 /* Return 1 if the specified dim is involved in any lower bound.
9948 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
9949 enum isl_dim_type type
, unsigned pos
)
9951 return has_any_bound(set
, type
, pos
,
9952 &isl_basic_map_dim_has_lower_bound
);
9955 /* Return 1 if the specified dim is involved in any upper bound.
9957 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
9958 enum isl_dim_type type
, unsigned pos
)
9960 return has_any_bound(set
, type
, pos
,
9961 &isl_basic_map_dim_has_upper_bound
);
9964 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9966 static int has_bound(__isl_keep isl_map
*map
,
9967 enum isl_dim_type type
, unsigned pos
,
9968 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9969 enum isl_dim_type type
, unsigned pos
))
9976 for (i
= 0; i
< map
->n
; ++i
) {
9978 bounded
= fn(map
->p
[i
], type
, pos
);
9979 if (bounded
< 0 || !bounded
)
9986 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9988 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9989 enum isl_dim_type type
, unsigned pos
)
9991 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9994 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9996 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9997 enum isl_dim_type type
, unsigned pos
)
9999 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10002 /* For each of the "n" variables starting at "first", determine
10003 * the sign of the variable and put the results in the first "n"
10004 * elements of the array "signs".
10006 * 1 means that the variable is non-negative
10007 * -1 means that the variable is non-positive
10008 * 0 means the variable attains both positive and negative values.
10010 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10011 unsigned first
, unsigned n
, int *signs
)
10013 isl_vec
*bound
= NULL
;
10014 struct isl_tab
*tab
= NULL
;
10015 struct isl_tab_undo
*snap
;
10018 if (!bset
|| !signs
)
10021 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10022 tab
= isl_tab_from_basic_set(bset
, 0);
10023 if (!bound
|| !tab
)
10026 isl_seq_clr(bound
->el
, bound
->size
);
10027 isl_int_set_si(bound
->el
[0], -1);
10029 snap
= isl_tab_snap(tab
);
10030 for (i
= 0; i
< n
; ++i
) {
10033 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10034 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10036 empty
= tab
->empty
;
10037 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10038 if (isl_tab_rollback(tab
, snap
) < 0)
10046 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10047 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10049 empty
= tab
->empty
;
10050 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10051 if (isl_tab_rollback(tab
, snap
) < 0)
10054 signs
[i
] = empty
? -1 : 0;
10058 isl_vec_free(bound
);
10062 isl_vec_free(bound
);
10066 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10067 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10069 if (!bset
|| !signs
)
10071 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10074 first
+= pos(bset
->dim
, type
) - 1;
10075 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10078 /* Check if the given basic map is obviously single-valued.
10079 * In particular, for each output dimension, check that there is
10080 * an equality that defines the output dimension in terms of
10081 * earlier dimensions.
10083 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10093 total
= 1 + isl_basic_map_total_dim(bmap
);
10094 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10095 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10097 for (i
= 0; i
< n_out
; ++i
) {
10098 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10099 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
10101 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
10102 total
- (o_out
+ i
+ 1)) == -1)
10105 if (j
>= bmap
->n_eq
)
10112 /* Check if the given basic map is single-valued.
10113 * We simply compute
10117 * and check if the result is a subset of the identity mapping.
10119 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10122 isl_basic_map
*test
;
10126 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10130 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10131 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10133 space
= isl_basic_map_get_space(bmap
);
10134 space
= isl_space_map_from_set(isl_space_range(space
));
10135 id
= isl_basic_map_identity(space
);
10137 sv
= isl_basic_map_is_subset(test
, id
);
10139 isl_basic_map_free(test
);
10140 isl_basic_map_free(id
);
10145 /* Check if the given map is obviously single-valued.
10147 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10156 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10159 /* Check if the given map is single-valued.
10160 * We simply compute
10164 * and check if the result is a subset of the identity mapping.
10166 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10173 sv
= isl_map_plain_is_single_valued(map
);
10177 test
= isl_map_reverse(isl_map_copy(map
));
10178 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10180 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10181 id
= isl_map_identity(dim
);
10183 sv
= isl_map_is_subset(test
, id
);
10185 isl_map_free(test
);
10191 int isl_map_is_injective(__isl_keep isl_map
*map
)
10195 map
= isl_map_copy(map
);
10196 map
= isl_map_reverse(map
);
10197 in
= isl_map_is_single_valued(map
);
10203 /* Check if the given map is obviously injective.
10205 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10209 map
= isl_map_copy(map
);
10210 map
= isl_map_reverse(map
);
10211 in
= isl_map_plain_is_single_valued(map
);
10217 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10221 sv
= isl_map_is_single_valued(map
);
10225 return isl_map_is_injective(map
);
10228 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10230 return isl_map_is_single_valued((isl_map
*)set
);
10233 int isl_map_is_translation(__isl_keep isl_map
*map
)
10238 delta
= isl_map_deltas(isl_map_copy(map
));
10239 ok
= isl_set_is_singleton(delta
);
10240 isl_set_free(delta
);
10245 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10247 if (isl_seq_first_non_zero(p
, pos
) != -1)
10249 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10254 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10263 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10266 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10267 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10268 for (j
= 0; j
< nvar
; ++j
) {
10269 int lower
= 0, upper
= 0;
10270 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10271 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10273 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10277 if (i
< bset
->n_eq
)
10279 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10280 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10282 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10284 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10289 if (!lower
|| !upper
)
10296 int isl_set_is_box(__isl_keep isl_set
*set
)
10303 return isl_basic_set_is_box(set
->p
[0]);
10306 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10311 return isl_space_is_wrapping(bset
->dim
);
10314 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10319 return isl_space_is_wrapping(set
->dim
);
10322 /* Is the domain of "map" a wrapped relation?
10324 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10329 return isl_space_domain_is_wrapping(map
->dim
);
10332 /* Is the range of "map" a wrapped relation?
10334 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10339 return isl_space_range_is_wrapping(map
->dim
);
10342 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10344 bmap
= isl_basic_map_cow(bmap
);
10348 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10352 bmap
= isl_basic_map_finalize(bmap
);
10354 return (isl_basic_set
*)bmap
;
10356 isl_basic_map_free(bmap
);
10360 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10364 map
= isl_map_cow(map
);
10368 for (i
= 0; i
< map
->n
; ++i
) {
10369 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10373 map
->dim
= isl_space_wrap(map
->dim
);
10377 return (isl_set
*)map
;
10383 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10385 bset
= isl_basic_set_cow(bset
);
10389 bset
->dim
= isl_space_unwrap(bset
->dim
);
10393 bset
= isl_basic_set_finalize(bset
);
10395 return (isl_basic_map
*)bset
;
10397 isl_basic_set_free(bset
);
10401 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10408 if (!isl_set_is_wrapping(set
))
10409 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10412 set
= isl_set_cow(set
);
10416 for (i
= 0; i
< set
->n
; ++i
) {
10417 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10422 set
->dim
= isl_space_unwrap(set
->dim
);
10426 return (isl_map
*)set
;
10432 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10433 enum isl_dim_type type
)
10438 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10441 bmap
= isl_basic_map_cow(bmap
);
10445 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10449 bmap
= isl_basic_map_finalize(bmap
);
10453 isl_basic_map_free(bmap
);
10457 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10458 enum isl_dim_type type
)
10465 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10468 map
= isl_map_cow(map
);
10472 for (i
= 0; i
< map
->n
; ++i
) {
10473 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10477 map
->dim
= isl_space_reset(map
->dim
, type
);
10487 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10492 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10495 bmap
= isl_basic_map_cow(bmap
);
10499 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10503 bmap
= isl_basic_map_finalize(bmap
);
10507 isl_basic_map_free(bmap
);
10511 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10513 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10516 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10517 __isl_take isl_basic_map
*bmap
)
10522 if (!bmap
->dim
->nested
[0])
10525 bmap
= isl_basic_map_cow(bmap
);
10529 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10533 bmap
= isl_basic_map_finalize(bmap
);
10537 isl_basic_map_free(bmap
);
10541 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10542 __isl_take isl_basic_map
*bmap
)
10547 if (!bmap
->dim
->nested
[1])
10550 bmap
= isl_basic_map_cow(bmap
);
10554 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10558 bmap
= isl_basic_map_finalize(bmap
);
10562 isl_basic_map_free(bmap
);
10566 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10573 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10576 map
= isl_map_cow(map
);
10580 for (i
= 0; i
< map
->n
; ++i
) {
10581 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10585 map
->dim
= isl_space_flatten(map
->dim
);
10595 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10597 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10600 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10602 isl_space
*dim
, *flat_dim
;
10605 dim
= isl_set_get_space(set
);
10606 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10607 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10608 map
= isl_map_intersect_domain(map
, set
);
10613 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10620 if (!map
->dim
->nested
[0])
10623 map
= isl_map_cow(map
);
10627 for (i
= 0; i
< map
->n
; ++i
) {
10628 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10632 map
->dim
= isl_space_flatten_domain(map
->dim
);
10642 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10649 if (!map
->dim
->nested
[1])
10652 map
= isl_map_cow(map
);
10656 for (i
= 0; i
< map
->n
; ++i
) {
10657 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10661 map
->dim
= isl_space_flatten_range(map
->dim
);
10671 /* Reorder the dimensions of "bmap" according to the given dim_map
10672 * and set the dimension specification to "dim".
10674 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10675 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10677 isl_basic_map
*res
;
10680 bmap
= isl_basic_map_cow(bmap
);
10681 if (!bmap
|| !dim
|| !dim_map
)
10684 flags
= bmap
->flags
;
10685 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10686 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10687 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10688 res
= isl_basic_map_alloc_space(dim
,
10689 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10690 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10692 res
->flags
= flags
;
10693 res
= isl_basic_map_finalize(res
);
10697 isl_basic_map_free(bmap
);
10698 isl_space_free(dim
);
10702 /* Reorder the dimensions of "map" according to given reordering.
10704 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10705 __isl_take isl_reordering
*r
)
10708 struct isl_dim_map
*dim_map
;
10710 map
= isl_map_cow(map
);
10711 dim_map
= isl_dim_map_from_reordering(r
);
10712 if (!map
|| !r
|| !dim_map
)
10715 for (i
= 0; i
< map
->n
; ++i
) {
10716 struct isl_dim_map
*dim_map_i
;
10718 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10720 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10721 isl_space_copy(r
->dim
), dim_map_i
);
10727 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10729 isl_reordering_free(r
);
10735 isl_reordering_free(r
);
10739 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10740 __isl_take isl_reordering
*r
)
10742 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10745 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10746 __isl_take isl_space
*model
)
10750 if (!map
|| !model
)
10753 ctx
= isl_space_get_ctx(model
);
10754 if (!isl_space_has_named_params(model
))
10755 isl_die(ctx
, isl_error_invalid
,
10756 "model has unnamed parameters", goto error
);
10757 if (!isl_space_has_named_params(map
->dim
))
10758 isl_die(ctx
, isl_error_invalid
,
10759 "relation has unnamed parameters", goto error
);
10760 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10761 isl_reordering
*exp
;
10763 model
= isl_space_drop_dims(model
, isl_dim_in
,
10764 0, isl_space_dim(model
, isl_dim_in
));
10765 model
= isl_space_drop_dims(model
, isl_dim_out
,
10766 0, isl_space_dim(model
, isl_dim_out
));
10767 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10768 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10769 map
= isl_map_realign(map
, exp
);
10772 isl_space_free(model
);
10775 isl_space_free(model
);
10780 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10781 __isl_take isl_space
*model
)
10783 return isl_map_align_params(set
, model
);
10786 /* Align the parameters of "bmap" to those of "model", introducing
10787 * additional parameters if needed.
10789 __isl_give isl_basic_map
*isl_basic_map_align_params(
10790 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10794 if (!bmap
|| !model
)
10797 ctx
= isl_space_get_ctx(model
);
10798 if (!isl_space_has_named_params(model
))
10799 isl_die(ctx
, isl_error_invalid
,
10800 "model has unnamed parameters", goto error
);
10801 if (!isl_space_has_named_params(bmap
->dim
))
10802 isl_die(ctx
, isl_error_invalid
,
10803 "relation has unnamed parameters", goto error
);
10804 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10805 isl_reordering
*exp
;
10806 struct isl_dim_map
*dim_map
;
10808 model
= isl_space_drop_dims(model
, isl_dim_in
,
10809 0, isl_space_dim(model
, isl_dim_in
));
10810 model
= isl_space_drop_dims(model
, isl_dim_out
,
10811 0, isl_space_dim(model
, isl_dim_out
));
10812 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10813 exp
= isl_reordering_extend_space(exp
,
10814 isl_basic_map_get_space(bmap
));
10815 dim_map
= isl_dim_map_from_reordering(exp
);
10816 bmap
= isl_basic_map_realign(bmap
,
10817 exp
? isl_space_copy(exp
->dim
) : NULL
,
10818 isl_dim_map_extend(dim_map
, bmap
));
10819 isl_reordering_free(exp
);
10823 isl_space_free(model
);
10826 isl_space_free(model
);
10827 isl_basic_map_free(bmap
);
10831 /* Align the parameters of "bset" to those of "model", introducing
10832 * additional parameters if needed.
10834 __isl_give isl_basic_set
*isl_basic_set_align_params(
10835 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10837 return isl_basic_map_align_params(bset
, model
);
10840 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10841 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10842 enum isl_dim_type c2
, enum isl_dim_type c3
,
10843 enum isl_dim_type c4
, enum isl_dim_type c5
)
10845 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10846 struct isl_mat
*mat
;
10852 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
10853 isl_basic_map_total_dim(bmap
) + 1);
10856 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10857 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10858 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10859 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10860 isl_int_set(mat
->row
[i
][pos
],
10861 bmap
->eq
[i
][off
+ k
]);
10869 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
10870 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10871 enum isl_dim_type c2
, enum isl_dim_type c3
,
10872 enum isl_dim_type c4
, enum isl_dim_type c5
)
10874 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10875 struct isl_mat
*mat
;
10881 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
10882 isl_basic_map_total_dim(bmap
) + 1);
10885 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
10886 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10887 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10888 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10889 isl_int_set(mat
->row
[i
][pos
],
10890 bmap
->ineq
[i
][off
+ k
]);
10898 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
10899 __isl_take isl_space
*dim
,
10900 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10901 enum isl_dim_type c2
, enum isl_dim_type c3
,
10902 enum isl_dim_type c4
, enum isl_dim_type c5
)
10904 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10905 isl_basic_map
*bmap
;
10911 if (!dim
|| !eq
|| !ineq
)
10914 if (eq
->n_col
!= ineq
->n_col
)
10915 isl_die(dim
->ctx
, isl_error_invalid
,
10916 "equalities and inequalities matrices should have "
10917 "same number of columns", goto error
);
10919 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
10921 if (eq
->n_col
< total
)
10922 isl_die(dim
->ctx
, isl_error_invalid
,
10923 "number of columns too small", goto error
);
10925 extra
= eq
->n_col
- total
;
10927 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
10928 eq
->n_row
, ineq
->n_row
);
10931 for (i
= 0; i
< extra
; ++i
) {
10932 k
= isl_basic_map_alloc_div(bmap
);
10935 isl_int_set_si(bmap
->div
[k
][0], 0);
10937 for (i
= 0; i
< eq
->n_row
; ++i
) {
10938 l
= isl_basic_map_alloc_equality(bmap
);
10941 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10942 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10943 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10944 isl_int_set(bmap
->eq
[l
][off
+ k
],
10950 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10951 l
= isl_basic_map_alloc_inequality(bmap
);
10954 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10955 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10956 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10957 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10958 ineq
->row
[i
][pos
]);
10964 isl_space_free(dim
);
10966 isl_mat_free(ineq
);
10968 bmap
= isl_basic_map_simplify(bmap
);
10969 return isl_basic_map_finalize(bmap
);
10971 isl_space_free(dim
);
10973 isl_mat_free(ineq
);
10977 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10978 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10979 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10981 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10982 c1
, c2
, c3
, c4
, isl_dim_in
);
10985 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10986 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10987 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10989 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10990 c1
, c2
, c3
, c4
, isl_dim_in
);
10993 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10994 __isl_take isl_space
*dim
,
10995 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10996 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10998 return (isl_basic_set
*)
10999 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11000 c1
, c2
, c3
, c4
, isl_dim_in
);
11003 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11008 return isl_space_can_zip(bmap
->dim
);
11011 int isl_map_can_zip(__isl_keep isl_map
*map
)
11016 return isl_space_can_zip(map
->dim
);
11019 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11020 * (A -> C) -> (B -> D).
11022 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11031 if (!isl_basic_map_can_zip(bmap
))
11032 isl_die(bmap
->ctx
, isl_error_invalid
,
11033 "basic map cannot be zipped", goto error
);
11034 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11035 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11036 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11037 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11038 bmap
= isl_basic_map_cow(bmap
);
11039 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11042 bmap
->dim
= isl_space_zip(bmap
->dim
);
11047 isl_basic_map_free(bmap
);
11051 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11052 * (A -> C) -> (B -> D).
11054 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11061 if (!isl_map_can_zip(map
))
11062 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11065 map
= isl_map_cow(map
);
11069 for (i
= 0; i
< map
->n
; ++i
) {
11070 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11075 map
->dim
= isl_space_zip(map
->dim
);
11085 /* Can we apply isl_basic_map_curry to "bmap"?
11086 * That is, does it have a nested relation in its domain?
11088 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11093 return isl_space_can_curry(bmap
->dim
);
11096 /* Can we apply isl_map_curry to "map"?
11097 * That is, does it have a nested relation in its domain?
11099 int isl_map_can_curry(__isl_keep isl_map
*map
)
11104 return isl_space_can_curry(map
->dim
);
11107 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11110 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11116 if (!isl_basic_map_can_curry(bmap
))
11117 isl_die(bmap
->ctx
, isl_error_invalid
,
11118 "basic map cannot be curried", goto error
);
11119 bmap
= isl_basic_map_cow(bmap
);
11122 bmap
->dim
= isl_space_curry(bmap
->dim
);
11127 isl_basic_map_free(bmap
);
11131 /* Given a map (A -> B) -> C, return the corresponding map
11134 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11141 if (!isl_map_can_curry(map
))
11142 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11145 map
= isl_map_cow(map
);
11149 for (i
= 0; i
< map
->n
; ++i
) {
11150 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11155 map
->dim
= isl_space_curry(map
->dim
);
11165 /* Can we apply isl_basic_map_uncurry to "bmap"?
11166 * That is, does it have a nested relation in its domain?
11168 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11173 return isl_space_can_uncurry(bmap
->dim
);
11176 /* Can we apply isl_map_uncurry to "map"?
11177 * That is, does it have a nested relation in its domain?
11179 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11184 return isl_space_can_uncurry(map
->dim
);
11187 /* Given a basic map A -> (B -> C), return the corresponding basic map
11190 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11196 if (!isl_basic_map_can_uncurry(bmap
))
11197 isl_die(bmap
->ctx
, isl_error_invalid
,
11198 "basic map cannot be uncurried",
11199 return isl_basic_map_free(bmap
));
11200 bmap
= isl_basic_map_cow(bmap
);
11203 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11205 return isl_basic_map_free(bmap
);
11209 /* Given a map A -> (B -> C), return the corresponding map
11212 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11219 if (!isl_map_can_uncurry(map
))
11220 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11221 return isl_map_free(map
));
11223 map
= isl_map_cow(map
);
11227 for (i
= 0; i
< map
->n
; ++i
) {
11228 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11230 return isl_map_free(map
);
11233 map
->dim
= isl_space_uncurry(map
->dim
);
11235 return isl_map_free(map
);
11240 /* Construct a basic map mapping the domain of the affine expression
11241 * to a one-dimensional range prescribed by the affine expression.
11243 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11247 isl_local_space
*ls
;
11248 isl_basic_map
*bmap
;
11253 ls
= isl_aff_get_local_space(aff
);
11254 bmap
= isl_basic_map_from_local_space(ls
);
11255 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11256 k
= isl_basic_map_alloc_equality(bmap
);
11260 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11261 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11262 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11263 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11264 aff
->v
->size
- (pos
+ 1));
11267 bmap
= isl_basic_map_finalize(bmap
);
11271 isl_basic_map_free(bmap
);
11275 /* Construct a map mapping the domain of the affine expression
11276 * to a one-dimensional range prescribed by the affine expression.
11278 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11280 isl_basic_map
*bmap
;
11282 bmap
= isl_basic_map_from_aff(aff
);
11283 return isl_map_from_basic_map(bmap
);
11286 /* Construct a basic map mapping the domain the multi-affine expression
11287 * to its range, with each dimension in the range equated to the
11288 * corresponding affine expression.
11290 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11291 __isl_take isl_multi_aff
*maff
)
11295 isl_basic_map
*bmap
;
11300 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11301 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11302 "invalid space", goto error
);
11304 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11305 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11307 for (i
= 0; i
< maff
->n
; ++i
) {
11309 isl_basic_map
*bmap_i
;
11311 aff
= isl_aff_copy(maff
->p
[i
]);
11312 bmap_i
= isl_basic_map_from_aff(aff
);
11314 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11317 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11319 isl_multi_aff_free(maff
);
11322 isl_multi_aff_free(maff
);
11326 /* Construct a map mapping the domain the multi-affine expression
11327 * to its range, with each dimension in the range equated to the
11328 * corresponding affine expression.
11330 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11332 isl_basic_map
*bmap
;
11334 bmap
= isl_basic_map_from_multi_aff(maff
);
11335 return isl_map_from_basic_map(bmap
);
11338 /* Construct a basic map mapping a domain in the given space to
11339 * to an n-dimensional range, with n the number of elements in the list,
11340 * where each coordinate in the range is prescribed by the
11341 * corresponding affine expression.
11342 * The domains of all affine expressions in the list are assumed to match
11345 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11346 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11350 isl_basic_map
*bmap
;
11355 dim
= isl_space_from_domain(domain_dim
);
11356 bmap
= isl_basic_map_universe(dim
);
11358 for (i
= 0; i
< list
->n
; ++i
) {
11360 isl_basic_map
*bmap_i
;
11362 aff
= isl_aff_copy(list
->p
[i
]);
11363 bmap_i
= isl_basic_map_from_aff(aff
);
11365 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11368 isl_aff_list_free(list
);
11372 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11373 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11375 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11378 /* Construct a basic map where the given dimensions are equal to each other.
11380 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11381 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11383 isl_basic_map
*bmap
= NULL
;
11389 if (pos1
>= isl_space_dim(space
, type1
))
11390 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11391 "index out of bounds", goto error
);
11392 if (pos2
>= isl_space_dim(space
, type2
))
11393 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11394 "index out of bounds", goto error
);
11396 if (type1
== type2
&& pos1
== pos2
)
11397 return isl_basic_map_universe(space
);
11399 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11400 i
= isl_basic_map_alloc_equality(bmap
);
11403 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11404 pos1
+= isl_basic_map_offset(bmap
, type1
);
11405 pos2
+= isl_basic_map_offset(bmap
, type2
);
11406 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11407 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11408 bmap
= isl_basic_map_finalize(bmap
);
11409 isl_space_free(space
);
11412 isl_space_free(space
);
11413 isl_basic_map_free(bmap
);
11417 /* Add a constraint imposing that the given two dimensions are equal.
11419 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11420 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11424 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11426 bmap
= isl_basic_map_intersect(bmap
, eq
);
11431 /* Add a constraint imposing that the given two dimensions are equal.
11433 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11434 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11436 isl_basic_map
*bmap
;
11438 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11440 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11445 /* Add a constraint imposing that the given two dimensions have opposite values.
11447 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11448 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11450 isl_basic_map
*bmap
= NULL
;
11456 if (pos1
>= isl_map_dim(map
, type1
))
11457 isl_die(map
->ctx
, isl_error_invalid
,
11458 "index out of bounds", goto error
);
11459 if (pos2
>= isl_map_dim(map
, type2
))
11460 isl_die(map
->ctx
, isl_error_invalid
,
11461 "index out of bounds", goto error
);
11463 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11464 i
= isl_basic_map_alloc_equality(bmap
);
11467 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11468 pos1
+= isl_basic_map_offset(bmap
, type1
);
11469 pos2
+= isl_basic_map_offset(bmap
, type2
);
11470 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11471 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11472 bmap
= isl_basic_map_finalize(bmap
);
11474 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11478 isl_basic_map_free(bmap
);
11483 /* Construct a constraint imposing that the value of the first dimension is
11484 * greater than or equal to that of the second.
11486 static __isl_give isl_constraint
*constraint_order_ge(
11487 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11488 enum isl_dim_type type2
, int pos2
)
11495 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11497 if (pos1
>= isl_constraint_dim(c
, type1
))
11498 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11499 "index out of bounds", return isl_constraint_free(c
));
11500 if (pos2
>= isl_constraint_dim(c
, type2
))
11501 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11502 "index out of bounds", return isl_constraint_free(c
));
11504 if (type1
== type2
&& pos1
== pos2
)
11507 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11508 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11513 /* Add a constraint imposing that the value of the first dimension is
11514 * greater than or equal to that of the second.
11516 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11517 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11522 if (type1
== type2
&& pos1
== pos2
)
11524 space
= isl_basic_map_get_space(bmap
);
11525 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11526 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11531 /* Add a constraint imposing that the value of the first dimension is
11532 * greater than or equal to that of the second.
11534 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11535 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11540 if (type1
== type2
&& pos1
== pos2
)
11542 space
= isl_map_get_space(map
);
11543 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11544 map
= isl_map_add_constraint(map
, c
);
11549 /* Add a constraint imposing that the value of the first dimension is
11550 * less than or equal to that of the second.
11552 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11553 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11555 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11558 /* Construct a basic map where the value of the first dimension is
11559 * greater than that of the second.
11561 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11562 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11564 isl_basic_map
*bmap
= NULL
;
11570 if (pos1
>= isl_space_dim(space
, type1
))
11571 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11572 "index out of bounds", goto error
);
11573 if (pos2
>= isl_space_dim(space
, type2
))
11574 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11575 "index out of bounds", goto error
);
11577 if (type1
== type2
&& pos1
== pos2
)
11578 return isl_basic_map_empty(space
);
11580 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11581 i
= isl_basic_map_alloc_inequality(bmap
);
11583 return isl_basic_map_free(bmap
);
11584 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11585 pos1
+= isl_basic_map_offset(bmap
, type1
);
11586 pos2
+= isl_basic_map_offset(bmap
, type2
);
11587 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11588 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11589 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11590 bmap
= isl_basic_map_finalize(bmap
);
11594 isl_space_free(space
);
11595 isl_basic_map_free(bmap
);
11599 /* Add a constraint imposing that the value of the first dimension is
11600 * greater than that of the second.
11602 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11603 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11607 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11609 bmap
= isl_basic_map_intersect(bmap
, gt
);
11614 /* Add a constraint imposing that the value of the first dimension is
11615 * greater than that of the second.
11617 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11618 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11620 isl_basic_map
*bmap
;
11622 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11624 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11629 /* Add a constraint imposing that the value of the first dimension is
11630 * smaller than that of the second.
11632 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11633 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11635 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11638 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11642 isl_local_space
*ls
;
11647 if (!isl_basic_map_divs_known(bmap
))
11648 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11649 "some divs are unknown", return NULL
);
11651 ls
= isl_basic_map_get_local_space(bmap
);
11652 div
= isl_local_space_get_div(ls
, pos
);
11653 isl_local_space_free(ls
);
11658 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11661 return isl_basic_map_get_div(bset
, pos
);
11664 /* Plug in "subs" for dimension "type", "pos" of "bset".
11666 * Let i be the dimension to replace and let "subs" be of the form
11670 * Any integer division with a non-zero coefficient for i,
11672 * floor((a i + g)/m)
11676 * floor((a f + d g)/(m d))
11678 * Constraints of the form
11686 * We currently require that "subs" is an integral expression.
11687 * Handling rational expressions may require us to add stride constraints
11688 * as we do in isl_basic_set_preimage_multi_aff.
11690 __isl_give isl_basic_set
*isl_basic_set_substitute(
11691 __isl_take isl_basic_set
*bset
,
11692 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11698 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11701 bset
= isl_basic_set_cow(bset
);
11702 if (!bset
|| !subs
)
11705 ctx
= isl_basic_set_get_ctx(bset
);
11706 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11707 isl_die(ctx
, isl_error_invalid
,
11708 "spaces don't match", goto error
);
11709 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11710 isl_die(ctx
, isl_error_unsupported
,
11711 "cannot handle divs yet", goto error
);
11712 if (!isl_int_is_one(subs
->v
->el
[0]))
11713 isl_die(ctx
, isl_error_invalid
,
11714 "can only substitute integer expressions", goto error
);
11716 pos
+= isl_basic_set_offset(bset
, type
);
11720 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11721 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11723 isl_int_set(v
, bset
->eq
[i
][pos
]);
11724 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11725 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11726 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11729 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11730 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11732 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11733 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11734 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11735 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11738 for (i
= 0; i
< bset
->n_div
; ++i
) {
11739 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11741 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11742 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11743 isl_seq_combine(bset
->div
[i
] + 1,
11744 subs
->v
->el
[0], bset
->div
[i
] + 1,
11745 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11746 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11751 bset
= isl_basic_set_simplify(bset
);
11752 return isl_basic_set_finalize(bset
);
11754 isl_basic_set_free(bset
);
11758 /* Plug in "subs" for dimension "type", "pos" of "set".
11760 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11761 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11765 if (set
&& isl_set_plain_is_empty(set
))
11768 set
= isl_set_cow(set
);
11772 for (i
= set
->n
- 1; i
>= 0; --i
) {
11773 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11774 if (remove_if_empty(set
, i
) < 0)
11784 /* Check if the range of "ma" is compatible with the domain or range
11785 * (depending on "type") of "bmap".
11786 * Return -1 if anything is wrong.
11788 static int check_basic_map_compatible_range_multi_aff(
11789 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11790 __isl_keep isl_multi_aff
*ma
)
11793 isl_space
*ma_space
;
11795 ma_space
= isl_multi_aff_get_space(ma
);
11797 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
11801 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11802 "parameters don't match", goto error
);
11803 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11807 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11808 "spaces don't match", goto error
);
11810 isl_space_free(ma_space
);
11813 isl_space_free(ma_space
);
11817 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11818 * coefficients before the transformed range of dimensions,
11819 * the "n_after" coefficients after the transformed range of dimensions
11820 * and the coefficients of the other divs in "bmap".
11822 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11823 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11828 isl_local_space
*ls
;
11833 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11837 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11838 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11839 for (i
= 0; i
< n_div
; ++i
) {
11840 int o_bmap
= 0, o_ls
= 0;
11842 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
11843 o_bmap
+= 1 + 1 + n_param
;
11844 o_ls
+= 1 + 1 + n_param
;
11845 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
11846 o_bmap
+= n_before
;
11847 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11848 ls
->div
->row
[i
] + o_ls
, n_set
);
11851 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
11853 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11854 ls
->div
->row
[i
] + o_ls
, n_div
);
11857 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
11858 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
11862 isl_local_space_free(ls
);
11865 isl_local_space_free(ls
);
11869 /* How many stride constraints does "ma" enforce?
11870 * That is, how many of the affine expressions have a denominator
11871 * different from one?
11873 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
11878 for (i
= 0; i
< ma
->n
; ++i
)
11879 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11885 /* For each affine expression in ma of the form
11887 * x_i = (f_i y + h_i)/m_i
11889 * with m_i different from one, add a constraint to "bmap"
11892 * f_i y + h_i = m_i alpha_i
11894 * with alpha_i an additional existentially quantified variable.
11896 static __isl_give isl_basic_map
*add_ma_strides(
11897 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
11898 int n_before
, int n_after
)
11907 total
= isl_basic_map_total_dim(bmap
);
11908 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
11909 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
11910 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
11911 for (i
= 0; i
< ma
->n
; ++i
) {
11912 int o_bmap
= 0, o_ma
= 1;
11914 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11916 div
= isl_basic_map_alloc_div(bmap
);
11917 k
= isl_basic_map_alloc_equality(bmap
);
11918 if (div
< 0 || k
< 0)
11920 isl_int_set_si(bmap
->div
[div
][0], 0);
11921 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11922 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
11923 o_bmap
+= 1 + n_param
;
11924 o_ma
+= 1 + n_param
;
11925 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
11926 o_bmap
+= n_before
;
11927 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11928 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
11931 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
11933 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11934 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
11937 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
11938 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
11944 isl_basic_map_free(bmap
);
11948 /* Replace the domain or range space (depending on "type) of "space" by "set".
11950 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
11951 enum isl_dim_type type
, __isl_take isl_space
*set
)
11953 if (type
== isl_dim_in
) {
11954 space
= isl_space_range(space
);
11955 space
= isl_space_map_from_domain_and_range(set
, space
);
11957 space
= isl_space_domain(space
);
11958 space
= isl_space_map_from_domain_and_range(space
, set
);
11964 /* Compute the preimage of the domain or range (depending on "type")
11965 * of "bmap" under the function represented by "ma".
11966 * In other words, plug in "ma" in the domain or range of "bmap".
11967 * The result is a basic map that lives in the same space as "bmap"
11968 * except that the domain or range has been replaced by
11969 * the domain space of "ma".
11971 * If bmap is represented by
11973 * A(p) + S u + B x + T v + C(divs) >= 0,
11975 * where u and x are input and output dimensions if type == isl_dim_out
11976 * while x and v are input and output dimensions if type == isl_dim_in,
11977 * and ma is represented by
11979 * x = D(p) + F(y) + G(divs')
11981 * then the result is
11983 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11985 * The divs in the input set are similarly adjusted.
11988 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11992 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11993 * B_i G(divs') + c_i(divs))/n_i)
11995 * If bmap is not a rational map and if F(y) involves any denominators
11997 * x_i = (f_i y + h_i)/m_i
11999 * then additional constraints are added to ensure that we only
12000 * map back integer points. That is we enforce
12002 * f_i y + h_i = m_i alpha_i
12004 * with alpha_i an additional existentially quantified variable.
12006 * We first copy over the divs from "ma".
12007 * Then we add the modified constraints and divs from "bmap".
12008 * Finally, we add the stride constraints, if needed.
12010 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12011 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12012 __isl_take isl_multi_aff
*ma
)
12016 isl_basic_map
*res
= NULL
;
12017 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12018 isl_int f
, c1
, c2
, g
;
12019 int rational
, strides
;
12026 ma
= isl_multi_aff_align_divs(ma
);
12029 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12032 if (type
== isl_dim_in
) {
12034 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12036 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12039 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12040 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12042 space
= isl_multi_aff_get_domain_space(ma
);
12043 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12044 rational
= isl_basic_map_is_rational(bmap
);
12045 strides
= rational
? 0 : multi_aff_strides(ma
);
12046 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12047 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12049 res
= isl_basic_map_set_rational(res
);
12051 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12052 if (isl_basic_map_alloc_div(res
) < 0)
12055 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12058 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12059 k
= isl_basic_map_alloc_equality(res
);
12062 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12063 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12066 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12067 k
= isl_basic_map_alloc_inequality(res
);
12070 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12071 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12074 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12075 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12076 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12079 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12080 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12085 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12091 isl_basic_map_free(bmap
);
12092 isl_multi_aff_free(ma
);
12093 res
= isl_basic_set_simplify(res
);
12094 return isl_basic_map_finalize(res
);
12100 isl_basic_map_free(bmap
);
12101 isl_multi_aff_free(ma
);
12102 isl_basic_map_free(res
);
12106 /* Compute the preimage of "bset" under the function represented by "ma".
12107 * In other words, plug in "ma" in "bset". The result is a basic set
12108 * that lives in the domain space of "ma".
12110 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12111 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12113 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12116 /* Compute the preimage of the domain of "bmap" under the function
12117 * represented by "ma".
12118 * In other words, plug in "ma" in the domain of "bmap".
12119 * The result is a basic map that lives in the same space as "bmap"
12120 * except that the domain has been replaced by the domain space of "ma".
12122 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12123 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12125 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12128 /* Compute the preimage of the range of "bmap" under the function
12129 * represented by "ma".
12130 * In other words, plug in "ma" in the range of "bmap".
12131 * The result is a basic map that lives in the same space as "bmap"
12132 * except that the range has been replaced by the domain space of "ma".
12134 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12135 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12137 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12140 /* Check if the range of "ma" is compatible with the domain or range
12141 * (depending on "type") of "map".
12142 * Return -1 if anything is wrong.
12144 static int check_map_compatible_range_multi_aff(
12145 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12146 __isl_keep isl_multi_aff
*ma
)
12149 isl_space
*ma_space
;
12151 ma_space
= isl_multi_aff_get_space(ma
);
12152 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
12153 isl_space_free(ma_space
);
12155 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12156 "spaces don't match", return -1);
12160 /* Compute the preimage of the domain or range (depending on "type")
12161 * of "map" under the function represented by "ma".
12162 * In other words, plug in "ma" in the domain or range of "map".
12163 * The result is a map that lives in the same space as "map"
12164 * except that the domain or range has been replaced by
12165 * the domain space of "ma".
12167 * The parameters are assumed to have been aligned.
12169 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12170 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12175 map
= isl_map_cow(map
);
12176 ma
= isl_multi_aff_align_divs(ma
);
12179 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12182 for (i
= 0; i
< map
->n
; ++i
) {
12183 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12184 isl_multi_aff_copy(ma
));
12189 space
= isl_multi_aff_get_domain_space(ma
);
12190 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12192 isl_space_free(map
->dim
);
12197 isl_multi_aff_free(ma
);
12199 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12200 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12203 isl_multi_aff_free(ma
);
12208 /* Compute the preimage of the domain or range (depending on "type")
12209 * of "map" under the function represented by "ma".
12210 * In other words, plug in "ma" in the domain or range of "map".
12211 * The result is a map that lives in the same space as "map"
12212 * except that the domain or range has been replaced by
12213 * the domain space of "ma".
12215 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12216 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12221 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12222 return map_preimage_multi_aff(map
, type
, ma
);
12224 if (!isl_space_has_named_params(map
->dim
) ||
12225 !isl_space_has_named_params(ma
->space
))
12226 isl_die(map
->ctx
, isl_error_invalid
,
12227 "unaligned unnamed parameters", goto error
);
12228 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12229 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12231 return map_preimage_multi_aff(map
, type
, ma
);
12233 isl_multi_aff_free(ma
);
12234 return isl_map_free(map
);
12237 /* Compute the preimage of "set" under the function represented by "ma".
12238 * In other words, plug in "ma" in "set". The result is a set
12239 * that lives in the domain space of "ma".
12241 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12242 __isl_take isl_multi_aff
*ma
)
12244 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12247 /* Compute the preimage of the domain of "map" under the function
12248 * represented by "ma".
12249 * In other words, plug in "ma" in the domain of "map".
12250 * The result is a map that lives in the same space as "map"
12251 * except that the domain has been replaced by the domain space of "ma".
12253 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12254 __isl_take isl_multi_aff
*ma
)
12256 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12259 /* Compute the preimage of the range of "map" under the function
12260 * represented by "ma".
12261 * In other words, plug in "ma" in the range of "map".
12262 * The result is a map that lives in the same space as "map"
12263 * except that the range has been replaced by the domain space of "ma".
12265 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12266 __isl_take isl_multi_aff
*ma
)
12268 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12271 /* Compute the preimage of "map" under the function represented by "pma".
12272 * In other words, plug in "pma" in the domain or range of "map".
12273 * The result is a map that lives in the same space as "map",
12274 * except that the space of type "type" has been replaced by
12275 * the domain space of "pma".
12277 * The parameters of "map" and "pma" are assumed to have been aligned.
12279 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12280 __isl_take isl_map
*map
, enum isl_dim_type type
,
12281 __isl_take isl_pw_multi_aff
*pma
)
12290 isl_pw_multi_aff_free(pma
);
12291 res
= isl_map_empty(isl_map_get_space(map
));
12296 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12297 isl_multi_aff_copy(pma
->p
[0].maff
));
12298 if (type
== isl_dim_in
)
12299 res
= isl_map_intersect_domain(res
,
12300 isl_map_copy(pma
->p
[0].set
));
12302 res
= isl_map_intersect_range(res
,
12303 isl_map_copy(pma
->p
[0].set
));
12305 for (i
= 1; i
< pma
->n
; ++i
) {
12308 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12309 isl_multi_aff_copy(pma
->p
[i
].maff
));
12310 if (type
== isl_dim_in
)
12311 res_i
= isl_map_intersect_domain(res_i
,
12312 isl_map_copy(pma
->p
[i
].set
));
12314 res_i
= isl_map_intersect_range(res_i
,
12315 isl_map_copy(pma
->p
[i
].set
));
12316 res
= isl_map_union(res
, res_i
);
12319 isl_pw_multi_aff_free(pma
);
12323 isl_pw_multi_aff_free(pma
);
12328 /* Compute the preimage of "map" under the function represented by "pma".
12329 * In other words, plug in "pma" in the domain or range of "map".
12330 * The result is a map that lives in the same space as "map",
12331 * except that the space of type "type" has been replaced by
12332 * the domain space of "pma".
12334 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12335 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12340 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12341 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12343 if (!isl_space_has_named_params(map
->dim
) ||
12344 !isl_space_has_named_params(pma
->dim
))
12345 isl_die(map
->ctx
, isl_error_invalid
,
12346 "unaligned unnamed parameters", goto error
);
12347 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12348 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12350 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12352 isl_pw_multi_aff_free(pma
);
12353 return isl_map_free(map
);
12356 /* Compute the preimage of "set" under the function represented by "pma".
12357 * In other words, plug in "pma" in "set". The result is a set
12358 * that lives in the domain space of "pma".
12360 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12361 __isl_take isl_pw_multi_aff
*pma
)
12363 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12366 /* Compute the preimage of the domain of "map" under the function
12367 * represented by "pma".
12368 * In other words, plug in "pma" in the domain of "map".
12369 * The result is a map that lives in the same space as "map",
12370 * except that domain space has been replaced by the domain space of "pma".
12372 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12373 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12375 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12378 /* Compute the preimage of the range of "map" under the function
12379 * represented by "pma".
12380 * In other words, plug in "pma" in the range of "map".
12381 * The result is a map that lives in the same space as "map",
12382 * except that range space has been replaced by the domain space of "pma".
12384 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12385 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12387 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12390 /* Compute the preimage of "map" under the function represented by "mpa".
12391 * In other words, plug in "mpa" in the domain or range of "map".
12392 * The result is a map that lives in the same space as "map",
12393 * except that the space of type "type" has been replaced by
12394 * the domain space of "mpa".
12396 * If the map does not involve any constraints that refer to the
12397 * dimensions of the substituted space, then the only possible
12398 * effect of "mpa" on the map is to map the space to a different space.
12399 * We create a separate isl_multi_aff to effectuate this change
12400 * in order to avoid spurious splitting of the map along the pieces
12403 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12404 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12407 isl_pw_multi_aff
*pma
;
12412 n
= isl_map_dim(map
, type
);
12413 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12417 space
= isl_multi_pw_aff_get_space(mpa
);
12418 isl_multi_pw_aff_free(mpa
);
12419 ma
= isl_multi_aff_zero(space
);
12420 return isl_map_preimage_multi_aff(map
, type
, ma
);
12423 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12424 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12427 isl_multi_pw_aff_free(mpa
);
12431 /* Compute the preimage of "map" under the function represented by "mpa".
12432 * In other words, plug in "mpa" in the domain "map".
12433 * The result is a map that lives in the same space as "map",
12434 * except that domain space has been replaced by the domain space of "mpa".
12436 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12437 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12439 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12442 /* Compute the preimage of "set" by the function represented by "mpa".
12443 * In other words, plug in "mpa" in "set".
12445 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12446 __isl_take isl_multi_pw_aff
*mpa
)
12448 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);