2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
22 #include "isl_space_private.h"
23 #include "isl_equalities.h"
24 #include <isl_lp_private.h>
28 #include <isl_reordering.h>
29 #include "isl_sample.h"
32 #include <isl_mat_private.h>
33 #include <isl_vec_private.h>
34 #include <isl_dim_map.h>
35 #include <isl_local_space_private.h>
36 #include <isl_aff_private.h>
37 #include <isl_options_private.h>
38 #include <isl_morph.h>
39 #include <isl_val_private.h>
40 #include <isl/deprecated/map_int.h>
41 #include <isl/deprecated/set_int.h>
43 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
46 case isl_dim_param
: return dim
->nparam
;
47 case isl_dim_in
: return dim
->n_in
;
48 case isl_dim_out
: return dim
->n_out
;
49 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
54 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return 1;
58 case isl_dim_in
: return 1 + dim
->nparam
;
59 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
64 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
65 enum isl_dim_type type
)
70 case isl_dim_cst
: return 1;
73 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
74 case isl_dim_div
: return bmap
->n_div
;
75 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
80 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
82 return map
? n(map
->dim
, type
) : 0;
85 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
87 return set
? n(set
->dim
, type
) : 0;
90 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
91 enum isl_dim_type type
)
93 isl_space
*dim
= bmap
->dim
;
95 case isl_dim_cst
: return 0;
96 case isl_dim_param
: return 1;
97 case isl_dim_in
: return 1 + dim
->nparam
;
98 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
99 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
104 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
105 enum isl_dim_type type
)
107 return isl_basic_map_offset(bset
, type
);
110 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
112 return pos(map
->dim
, type
);
115 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
116 enum isl_dim_type type
)
118 return isl_basic_map_dim(bset
, type
);
121 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
123 return isl_basic_set_dim(bset
, isl_dim_set
);
126 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
128 return isl_basic_set_dim(bset
, isl_dim_param
);
131 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
135 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
138 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
140 return isl_set_dim(set
, isl_dim_set
);
143 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
145 return isl_set_dim(set
, isl_dim_param
);
148 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
150 return bmap
? bmap
->dim
->n_in
: 0;
153 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
155 return bmap
? bmap
->dim
->n_out
: 0;
158 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
160 return bmap
? bmap
->dim
->nparam
: 0;
163 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
165 return bmap
? bmap
->n_div
: 0;
168 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
170 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
173 unsigned isl_map_n_in(const struct isl_map
*map
)
175 return map
? map
->dim
->n_in
: 0;
178 unsigned isl_map_n_out(const struct isl_map
*map
)
180 return map
? map
->dim
->n_out
: 0;
183 unsigned isl_map_n_param(const struct isl_map
*map
)
185 return map
? map
->dim
->nparam
: 0;
188 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
193 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
196 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
199 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
200 struct isl_basic_set
*bset
)
205 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
208 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
211 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
216 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
219 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
222 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
223 struct isl_basic_set
*bset
)
228 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
231 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
234 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
236 return bmap
? bmap
->ctx
: NULL
;
239 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
241 return bset
? bset
->ctx
: NULL
;
244 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
246 return map
? map
->ctx
: NULL
;
249 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
251 return set
? set
->ctx
: NULL
;
254 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
258 return isl_space_copy(bmap
->dim
);
261 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
265 return isl_space_copy(bset
->dim
);
268 /* Extract the divs in "bmap" as a matrix.
270 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
281 ctx
= isl_basic_map_get_ctx(bmap
);
282 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
283 cols
= 1 + 1 + total
+ bmap
->n_div
;
284 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
288 for (i
= 0; i
< bmap
->n_div
; ++i
)
289 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
294 /* Extract the divs in "bset" as a matrix.
296 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
298 return isl_basic_map_get_divs(bset
);
301 __isl_give isl_local_space
*isl_basic_map_get_local_space(
302 __isl_keep isl_basic_map
*bmap
)
309 div
= isl_basic_map_get_divs(bmap
);
310 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
313 __isl_give isl_local_space
*isl_basic_set_get_local_space(
314 __isl_keep isl_basic_set
*bset
)
316 return isl_basic_map_get_local_space(bset
);
319 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
320 __isl_take isl_local_space
*ls
)
329 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
330 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
331 n_div
, 0, 2 * n_div
);
333 for (i
= 0; i
< n_div
; ++i
)
334 if (isl_basic_map_alloc_div(bmap
) < 0)
337 for (i
= 0; i
< n_div
; ++i
) {
338 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
339 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
343 isl_local_space_free(ls
);
346 isl_local_space_free(ls
);
347 isl_basic_map_free(bmap
);
351 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
352 __isl_take isl_local_space
*ls
)
354 return isl_basic_map_from_local_space(ls
);
357 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
361 return isl_space_copy(map
->dim
);
364 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
368 return isl_space_copy(set
->dim
);
371 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
372 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
374 bmap
= isl_basic_map_cow(bmap
);
377 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
380 bmap
= isl_basic_map_finalize(bmap
);
383 isl_basic_map_free(bmap
);
387 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
388 __isl_take isl_basic_set
*bset
, const char *s
)
390 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
393 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
394 enum isl_dim_type type
)
396 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
399 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
400 enum isl_dim_type type
, const char *s
)
404 map
= isl_map_cow(map
);
408 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
412 for (i
= 0; i
< map
->n
; ++i
) {
413 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
424 /* Replace the identifier of the tuple of type "type" by "id".
426 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
427 __isl_take isl_basic_map
*bmap
,
428 enum isl_dim_type type
, __isl_take isl_id
*id
)
430 bmap
= isl_basic_map_cow(bmap
);
433 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
435 return isl_basic_map_free(bmap
);
436 bmap
= isl_basic_map_finalize(bmap
);
443 /* Replace the identifier of the tuple by "id".
445 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
446 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
448 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
451 /* Does the input or output tuple have a name?
453 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
455 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
458 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
459 enum isl_dim_type type
)
461 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
464 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
467 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
470 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
471 enum isl_dim_type type
, __isl_take isl_id
*id
)
473 map
= isl_map_cow(map
);
477 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
479 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
485 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
486 __isl_take isl_id
*id
)
488 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
491 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
492 enum isl_dim_type type
)
494 map
= isl_map_cow(map
);
498 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
500 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
503 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
505 return isl_map_reset_tuple_id(set
, isl_dim_set
);
508 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
510 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
513 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
514 enum isl_dim_type type
)
516 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
519 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
521 return isl_map_has_tuple_id(set
, isl_dim_set
);
524 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
526 return isl_map_get_tuple_id(set
, isl_dim_set
);
529 /* Does the set tuple have a name?
531 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
533 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
537 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
539 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
542 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
544 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
547 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
548 enum isl_dim_type type
, unsigned pos
)
550 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
553 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
554 enum isl_dim_type type
, unsigned pos
)
556 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
559 /* Does the given dimension have a name?
561 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
562 enum isl_dim_type type
, unsigned pos
)
564 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
567 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
568 enum isl_dim_type type
, unsigned pos
)
570 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
573 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
574 enum isl_dim_type type
, unsigned pos
)
576 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
579 /* Does the given dimension have a name?
581 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
582 enum isl_dim_type type
, unsigned pos
)
584 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
587 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
588 __isl_take isl_basic_map
*bmap
,
589 enum isl_dim_type type
, unsigned pos
, const char *s
)
591 bmap
= isl_basic_map_cow(bmap
);
594 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
597 return isl_basic_map_finalize(bmap
);
599 isl_basic_map_free(bmap
);
603 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
604 enum isl_dim_type type
, unsigned pos
, const char *s
)
608 map
= isl_map_cow(map
);
612 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
616 for (i
= 0; i
< map
->n
; ++i
) {
617 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
628 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
629 __isl_take isl_basic_set
*bset
,
630 enum isl_dim_type type
, unsigned pos
, const char *s
)
632 return (isl_basic_set
*)isl_basic_map_set_dim_name(
633 (isl_basic_map
*)bset
, type
, pos
, s
);
636 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
637 enum isl_dim_type type
, unsigned pos
, const char *s
)
639 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
642 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
643 enum isl_dim_type type
, unsigned pos
)
645 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
648 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
649 enum isl_dim_type type
, unsigned pos
)
651 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
654 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
655 enum isl_dim_type type
, unsigned pos
)
657 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
660 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
661 enum isl_dim_type type
, unsigned pos
)
663 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
666 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
667 enum isl_dim_type type
, unsigned pos
)
669 return isl_map_has_dim_id(set
, type
, pos
);
672 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
673 enum isl_dim_type type
, unsigned pos
)
675 return isl_map_get_dim_id(set
, type
, pos
);
678 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
679 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
681 map
= isl_map_cow(map
);
685 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
687 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
693 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
694 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
696 return isl_map_set_dim_id(set
, type
, pos
, id
);
699 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
700 __isl_keep isl_id
*id
)
704 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
707 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
708 __isl_keep isl_id
*id
)
710 return isl_map_find_dim_by_id(set
, type
, id
);
713 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
718 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
721 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
724 return isl_map_find_dim_by_name(set
, type
, name
);
727 /* Reset the user pointer on all identifiers of parameters and tuples
728 * of the space of "map".
730 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
734 space
= isl_map_get_space(map
);
735 space
= isl_space_reset_user(space
);
736 map
= isl_map_reset_space(map
, space
);
741 /* Reset the user pointer on all identifiers of parameters and tuples
742 * of the space of "set".
744 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
746 return isl_map_reset_user(set
);
749 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
753 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
756 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
758 return isl_basic_map_is_rational(bset
);
761 /* Does "bmap" contain any rational points?
763 * If "bmap" has an equality for each dimension, equating the dimension
764 * to an integer constant, then it has no rational points, even if it
765 * is marked as rational.
767 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
769 int has_rational
= 1;
774 if (isl_basic_map_plain_is_empty(bmap
))
776 if (!isl_basic_map_is_rational(bmap
))
778 bmap
= isl_basic_map_copy(bmap
);
779 bmap
= isl_basic_map_implicit_equalities(bmap
);
782 total
= isl_basic_map_total_dim(bmap
);
783 if (bmap
->n_eq
== total
) {
785 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
786 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
789 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
790 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
792 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
800 isl_basic_map_free(bmap
);
805 /* Does "map" contain any rational points?
807 int isl_map_has_rational(__isl_keep isl_map
*map
)
814 for (i
= 0; i
< map
->n
; ++i
) {
815 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
816 if (has_rational
< 0)
824 /* Does "set" contain any rational points?
826 int isl_set_has_rational(__isl_keep isl_set
*set
)
828 return isl_map_has_rational(set
);
831 /* Is this basic set a parameter domain?
833 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
837 return isl_space_is_params(bset
->dim
);
840 /* Is this set a parameter domain?
842 int isl_set_is_params(__isl_keep isl_set
*set
)
846 return isl_space_is_params(set
->dim
);
849 /* Is this map actually a parameter domain?
850 * Users should never call this function. Outside of isl,
851 * a map can never be a parameter domain.
853 int isl_map_is_params(__isl_keep isl_map
*map
)
857 return isl_space_is_params(map
->dim
);
860 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
861 struct isl_basic_map
*bmap
, unsigned extra
,
862 unsigned n_eq
, unsigned n_ineq
)
865 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
870 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
871 if (isl_blk_is_error(bmap
->block
))
874 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
875 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
879 bmap
->block2
= isl_blk_empty();
882 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
883 if (isl_blk_is_error(bmap
->block2
))
886 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
891 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
892 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
894 for (i
= 0; i
< extra
; ++i
)
895 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
899 bmap
->c_size
= n_eq
+ n_ineq
;
900 bmap
->eq
= bmap
->ineq
+ n_ineq
;
909 isl_basic_map_free(bmap
);
913 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
914 unsigned nparam
, unsigned dim
, unsigned extra
,
915 unsigned n_eq
, unsigned n_ineq
)
917 struct isl_basic_map
*bmap
;
920 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
924 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
925 return (struct isl_basic_set
*)bmap
;
928 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
929 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
931 struct isl_basic_map
*bmap
;
934 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
935 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
936 return (struct isl_basic_set
*)bmap
;
942 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
943 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
945 struct isl_basic_map
*bmap
;
949 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
954 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
960 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
961 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
962 unsigned n_eq
, unsigned n_ineq
)
964 struct isl_basic_map
*bmap
;
967 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
971 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
975 static void dup_constraints(
976 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
979 unsigned total
= isl_basic_map_total_dim(src
);
981 for (i
= 0; i
< src
->n_eq
; ++i
) {
982 int j
= isl_basic_map_alloc_equality(dst
);
983 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
986 for (i
= 0; i
< src
->n_ineq
; ++i
) {
987 int j
= isl_basic_map_alloc_inequality(dst
);
988 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
991 for (i
= 0; i
< src
->n_div
; ++i
) {
992 int j
= isl_basic_map_alloc_div(dst
);
993 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
995 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
998 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1000 struct isl_basic_map
*dup
;
1004 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1005 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1008 dup_constraints(dup
, bmap
);
1009 dup
->flags
= bmap
->flags
;
1010 dup
->sample
= isl_vec_copy(bmap
->sample
);
1014 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1016 struct isl_basic_map
*dup
;
1018 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1019 return (struct isl_basic_set
*)dup
;
1022 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1027 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1031 return isl_basic_set_dup(bset
);
1034 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1043 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1048 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1052 bmap
= isl_basic_map_dup(bmap
);
1054 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1058 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1067 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1072 if (--bmap
->ref
> 0)
1075 isl_ctx_deref(bmap
->ctx
);
1077 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1079 isl_blk_free(bmap
->ctx
, bmap
->block
);
1080 isl_vec_free(bmap
->sample
);
1081 isl_space_free(bmap
->dim
);
1087 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1089 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1092 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1094 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1097 __isl_give isl_map
*isl_map_align_params_map_map_and(
1098 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1099 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1100 __isl_take isl_map
*map2
))
1104 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1105 return fn(map1
, map2
);
1106 if (!isl_space_has_named_params(map1
->dim
) ||
1107 !isl_space_has_named_params(map2
->dim
))
1108 isl_die(map1
->ctx
, isl_error_invalid
,
1109 "unaligned unnamed parameters", goto error
);
1110 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1111 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1112 return fn(map1
, map2
);
1119 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1120 __isl_keep isl_map
*map2
,
1121 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1127 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1128 return fn(map1
, map2
);
1129 if (!isl_space_has_named_params(map1
->dim
) ||
1130 !isl_space_has_named_params(map2
->dim
))
1131 isl_die(map1
->ctx
, isl_error_invalid
,
1132 "unaligned unnamed parameters", return -1);
1133 map1
= isl_map_copy(map1
);
1134 map2
= isl_map_copy(map2
);
1135 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1136 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1143 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1145 struct isl_ctx
*ctx
;
1149 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1150 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1152 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1153 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1154 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1155 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1156 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1157 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1159 int j
= isl_basic_map_alloc_inequality(bmap
);
1163 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1164 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1171 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1172 bmap
->extra
- bmap
->n_div
);
1173 return bmap
->n_eq
++;
1176 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1178 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1181 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1185 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1190 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1192 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1195 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1200 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1202 if (pos
!= bmap
->n_eq
- 1) {
1204 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1205 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1211 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1213 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1216 /* Turn inequality "pos" of "bmap" into an equality.
1218 * In particular, we move the inequality in front of the equalities
1219 * and move the last inequality in the position of the moved inequality.
1220 * Note that isl_tab_make_equalities_explicit depends on this particular
1221 * change in the ordering of the constraints.
1223 void isl_basic_map_inequality_to_equality(
1224 struct isl_basic_map
*bmap
, unsigned pos
)
1228 t
= bmap
->ineq
[pos
];
1229 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1230 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1235 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1236 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1237 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1238 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1241 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1243 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1246 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1248 struct isl_ctx
*ctx
;
1252 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1253 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1254 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1256 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1257 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1258 1 + isl_basic_map_total_dim(bmap
),
1259 bmap
->extra
- bmap
->n_div
);
1260 return bmap
->n_ineq
++;
1263 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1265 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1268 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1272 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1277 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1279 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1282 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1287 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1289 if (pos
!= bmap
->n_ineq
- 1) {
1290 t
= bmap
->ineq
[pos
];
1291 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1292 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1293 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1299 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1301 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1304 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1309 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1312 k
= isl_basic_map_alloc_equality(bmap
);
1315 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1318 isl_basic_map_free(bmap
);
1322 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1325 return (isl_basic_set
*)
1326 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1329 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1334 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1337 k
= isl_basic_map_alloc_inequality(bmap
);
1340 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1343 isl_basic_map_free(bmap
);
1347 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1350 return (isl_basic_set
*)
1351 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1354 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1358 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1359 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1360 1 + 1 + isl_basic_map_total_dim(bmap
),
1361 bmap
->extra
- bmap
->n_div
);
1362 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1363 return bmap
->n_div
++;
1366 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1368 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1371 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1375 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1380 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1382 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1385 /* Copy constraint from src to dst, putting the vars of src at offset
1386 * dim_off in dst and the divs of src at offset div_off in dst.
1387 * If both sets are actually map, then dim_off applies to the input
1390 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1391 struct isl_basic_map
*src_map
, isl_int
*src
,
1392 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1394 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1395 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1396 unsigned src_in
= isl_basic_map_n_in(src_map
);
1397 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1398 unsigned src_out
= isl_basic_map_n_out(src_map
);
1399 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1400 isl_int_set(dst
[0], src
[0]);
1401 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1402 if (dst_nparam
> src_nparam
)
1403 isl_seq_clr(dst
+1+src_nparam
,
1404 dst_nparam
- src_nparam
);
1405 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1406 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1408 isl_min(dst_in
-in_off
, src_in
));
1409 if (dst_in
-in_off
> src_in
)
1410 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1411 dst_in
- in_off
- src_in
);
1412 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1413 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1414 src
+1+src_nparam
+src_in
,
1415 isl_min(dst_out
-out_off
, src_out
));
1416 if (dst_out
-out_off
> src_out
)
1417 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1418 dst_out
- out_off
- src_out
);
1419 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1420 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1421 src
+1+src_nparam
+src_in
+src_out
,
1422 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1423 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1424 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1425 div_off
+src_map
->n_div
,
1426 dst_map
->n_div
- div_off
- src_map
->n_div
);
1429 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1430 struct isl_basic_map
*src_map
, isl_int
*src
,
1431 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1433 isl_int_set(dst
[0], src
[0]);
1434 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1437 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1438 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1443 if (!bmap1
|| !bmap2
)
1446 div_off
= bmap1
->n_div
;
1448 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1449 int i1
= isl_basic_map_alloc_equality(bmap1
);
1452 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1453 i_pos
, o_pos
, div_off
);
1456 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1457 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1460 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1461 i_pos
, o_pos
, div_off
);
1464 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1465 int i1
= isl_basic_map_alloc_div(bmap1
);
1468 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1469 i_pos
, o_pos
, div_off
);
1472 isl_basic_map_free(bmap2
);
1477 isl_basic_map_free(bmap1
);
1478 isl_basic_map_free(bmap2
);
1482 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1483 struct isl_basic_set
*bset2
, unsigned pos
)
1485 return (struct isl_basic_set
*)
1486 add_constraints((struct isl_basic_map
*)bset1
,
1487 (struct isl_basic_map
*)bset2
, 0, pos
);
1490 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1491 __isl_take isl_space
*dim
, unsigned extra
,
1492 unsigned n_eq
, unsigned n_ineq
)
1494 struct isl_basic_map
*ext
;
1504 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1505 base
->extra
>= base
->n_div
+ extra
;
1507 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1508 room_for_ineq(base
, n_ineq
)) {
1509 isl_space_free(dim
);
1513 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1514 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1515 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1516 extra
+= base
->extra
;
1518 n_ineq
+= base
->n_ineq
;
1520 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1526 ext
->sample
= isl_vec_copy(base
->sample
);
1527 flags
= base
->flags
;
1528 ext
= add_constraints(ext
, base
, 0, 0);
1531 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1537 isl_space_free(dim
);
1538 isl_basic_map_free(base
);
1542 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1543 __isl_take isl_space
*dim
, unsigned extra
,
1544 unsigned n_eq
, unsigned n_ineq
)
1546 return (struct isl_basic_set
*)
1547 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1548 extra
, n_eq
, n_ineq
);
1551 struct isl_basic_map
*isl_basic_map_extend_constraints(
1552 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1556 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1560 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1561 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1562 unsigned n_eq
, unsigned n_ineq
)
1564 struct isl_basic_map
*bmap
;
1569 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1573 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1576 isl_basic_map_free(base
);
1580 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1581 unsigned nparam
, unsigned dim
, unsigned extra
,
1582 unsigned n_eq
, unsigned n_ineq
)
1584 return (struct isl_basic_set
*)
1585 isl_basic_map_extend((struct isl_basic_map
*)base
,
1586 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1589 struct isl_basic_set
*isl_basic_set_extend_constraints(
1590 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1592 return (struct isl_basic_set
*)
1593 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1597 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1599 return (struct isl_basic_set
*)
1600 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1603 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1608 if (bmap
->ref
> 1) {
1610 bmap
= isl_basic_map_dup(bmap
);
1613 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1617 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1625 return isl_set_dup(set
);
1628 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1636 return isl_map_dup(map
);
1639 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1640 unsigned a_len
, unsigned b_len
)
1642 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1643 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1644 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1647 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1648 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1656 isl_assert(bmap
->ctx
,
1657 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1659 if (n1
== 0 || n2
== 0)
1662 bmap
= isl_basic_map_cow(bmap
);
1666 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1667 if (isl_blk_is_error(blk
))
1670 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1672 bmap
->eq
[i
] + pos
, n1
, n2
);
1674 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1676 bmap
->ineq
[i
] + pos
, n1
, n2
);
1678 for (i
= 0; i
< bmap
->n_div
; ++i
)
1680 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1682 isl_blk_free(bmap
->ctx
, blk
);
1684 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1685 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1686 return isl_basic_map_finalize(bmap
);
1688 isl_basic_map_free(bmap
);
1692 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1693 __isl_take isl_basic_set
*bset
, unsigned n
)
1701 nparam
= isl_basic_set_n_param(bset
);
1702 dim
= isl_basic_set_n_dim(bset
);
1703 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1705 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1707 isl_basic_set_free(bset
);
1711 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1717 total
= isl_basic_map_total_dim(bmap
);
1718 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1719 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1721 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1723 i
= isl_basic_map_alloc_equality(bmap
);
1727 isl_int_set_si(bmap
->eq
[i
][0], 1);
1728 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1729 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1730 isl_vec_free(bmap
->sample
);
1731 bmap
->sample
= NULL
;
1732 return isl_basic_map_finalize(bmap
);
1734 isl_basic_map_free(bmap
);
1738 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1740 return (struct isl_basic_set
*)
1741 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1744 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1747 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1749 isl_int
*t
= bmap
->div
[a
];
1750 bmap
->div
[a
] = bmap
->div
[b
];
1754 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1755 * div definitions accordingly.
1757 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1760 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1762 swap_div(bmap
, a
, b
);
1764 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1765 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1767 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1768 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1770 for (i
= 0; i
< bmap
->n_div
; ++i
)
1771 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1772 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1775 /* Eliminate the specified n dimensions starting at first from the
1776 * constraints, without removing the dimensions from the space.
1777 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1779 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1780 enum isl_dim_type type
, unsigned first
, unsigned n
)
1789 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1790 isl_die(map
->ctx
, isl_error_invalid
,
1791 "index out of bounds", goto error
);
1793 map
= isl_map_cow(map
);
1797 for (i
= 0; i
< map
->n
; ++i
) {
1798 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1808 /* Eliminate the specified n dimensions starting at first from the
1809 * constraints, without removing the dimensions from the space.
1810 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1812 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1813 enum isl_dim_type type
, unsigned first
, unsigned n
)
1815 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1818 /* Eliminate the specified n dimensions starting at first from the
1819 * constraints, without removing the dimensions from the space.
1820 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1822 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1823 unsigned first
, unsigned n
)
1825 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1828 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1829 __isl_take isl_basic_map
*bmap
)
1833 bmap
= isl_basic_map_eliminate_vars(bmap
,
1834 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1838 return isl_basic_map_finalize(bmap
);
1841 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1842 __isl_take isl_basic_set
*bset
)
1844 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1845 (struct isl_basic_map
*)bset
);
1848 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1857 map
= isl_map_cow(map
);
1861 for (i
= 0; i
< map
->n
; ++i
) {
1862 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1872 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1874 return isl_map_remove_divs(set
);
1877 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1878 enum isl_dim_type type
, unsigned first
, unsigned n
)
1882 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1884 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1886 bmap
= isl_basic_map_eliminate_vars(bmap
,
1887 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1890 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1892 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1895 isl_basic_map_free(bmap
);
1899 /* Return true if the definition of the given div (recursively) involves
1900 * any of the given variables.
1902 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1903 unsigned first
, unsigned n
)
1906 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1908 if (isl_int_is_zero(bmap
->div
[div
][0]))
1910 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1913 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1914 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1916 if (div_involves_vars(bmap
, i
, first
, n
))
1923 /* Try and add a lower and/or upper bound on "div" to "bmap"
1924 * based on inequality "i".
1925 * "total" is the total number of variables (excluding the divs).
1926 * "v" is a temporary object that can be used during the calculations.
1927 * If "lb" is set, then a lower bound should be constructed.
1928 * If "ub" is set, then an upper bound should be constructed.
1930 * The calling function has already checked that the inequality does not
1931 * reference "div", but we still need to check that the inequality is
1932 * of the right form. We'll consider the case where we want to construct
1933 * a lower bound. The construction of upper bounds is similar.
1935 * Let "div" be of the form
1937 * q = floor((a + f(x))/d)
1939 * We essentially check if constraint "i" is of the form
1943 * so that we can use it to derive a lower bound on "div".
1944 * However, we allow a slightly more general form
1948 * with the condition that the coefficients of g(x) - f(x) are all
1950 * Rewriting this constraint as
1954 * adding a + f(x) to both sides and dividing by d, we obtain
1956 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1958 * Taking the floor on both sides, we obtain
1960 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1964 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1966 * In the case of an upper bound, we construct the constraint
1968 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1971 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1972 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1973 unsigned total
, isl_int v
, int lb
, int ub
)
1977 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1979 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1980 bmap
->div
[div
][1 + 1 + j
]);
1981 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1984 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1985 bmap
->div
[div
][1 + 1 + j
]);
1986 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1992 bmap
= isl_basic_map_cow(bmap
);
1993 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1995 int k
= isl_basic_map_alloc_inequality(bmap
);
1998 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1999 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2000 bmap
->div
[div
][1 + j
]);
2001 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2002 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2004 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2007 int k
= isl_basic_map_alloc_inequality(bmap
);
2010 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2011 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2012 bmap
->div
[div
][1 + j
]);
2013 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2014 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2016 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2019 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2022 isl_basic_map_free(bmap
);
2026 /* This function is called right before "div" is eliminated from "bmap"
2027 * using Fourier-Motzkin.
2028 * Look through the constraints of "bmap" for constraints on the argument
2029 * of the integer division and use them to construct constraints on the
2030 * integer division itself. These constraints can then be combined
2031 * during the Fourier-Motzkin elimination.
2032 * Note that it is only useful to introduce lower bounds on "div"
2033 * if "bmap" already contains upper bounds on "div" as the newly
2034 * introduce lower bounds can then be combined with the pre-existing
2035 * upper bounds. Similarly for upper bounds.
2036 * We therefore first check if "bmap" contains any lower and/or upper bounds
2039 * It is interesting to note that the introduction of these constraints
2040 * can indeed lead to more accurate results, even when compared to
2041 * deriving constraints on the argument of "div" from constraints on "div".
2042 * Consider, for example, the set
2044 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2046 * The second constraint can be rewritten as
2048 * 2 * [(-i-2j+3)/4] + k >= 0
2050 * from which we can derive
2052 * -i - 2j + 3 >= -2k
2058 * Combined with the first constraint, we obtain
2060 * -3 <= 3 + 2k or k >= -3
2062 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2063 * the first constraint, we obtain
2065 * [(i + 2j)/4] >= [-3/4] = -1
2067 * Combining this constraint with the second constraint, we obtain
2071 static __isl_give isl_basic_map
*insert_bounds_on_div(
2072 __isl_take isl_basic_map
*bmap
, int div
)
2075 int check_lb
, check_ub
;
2082 if (isl_int_is_zero(bmap
->div
[div
][0]))
2085 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2089 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2090 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2097 if (!check_lb
&& !check_ub
)
2102 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2103 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2106 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2107 check_lb
, check_ub
);
2115 /* Remove all divs (recursively) involving any of the given dimensions
2116 * in their definitions.
2118 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2119 __isl_take isl_basic_map
*bmap
,
2120 enum isl_dim_type type
, unsigned first
, unsigned n
)
2126 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2128 first
+= isl_basic_map_offset(bmap
, type
);
2130 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2131 if (!div_involves_vars(bmap
, i
, first
, n
))
2133 bmap
= insert_bounds_on_div(bmap
, i
);
2134 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2142 isl_basic_map_free(bmap
);
2146 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2147 __isl_take isl_basic_set
*bset
,
2148 enum isl_dim_type type
, unsigned first
, unsigned n
)
2150 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2153 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2154 enum isl_dim_type type
, unsigned first
, unsigned n
)
2163 map
= isl_map_cow(map
);
2167 for (i
= 0; i
< map
->n
; ++i
) {
2168 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2179 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2180 enum isl_dim_type type
, unsigned first
, unsigned n
)
2182 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2186 /* Does the desciption of "bmap" depend on the specified dimensions?
2187 * We also check whether the dimensions appear in any of the div definitions.
2188 * In principle there is no need for this check. If the dimensions appear
2189 * in a div definition, they also appear in the defining constraints of that
2192 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2193 enum isl_dim_type type
, unsigned first
, unsigned n
)
2200 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2201 isl_die(bmap
->ctx
, isl_error_invalid
,
2202 "index out of bounds", return -1);
2204 first
+= isl_basic_map_offset(bmap
, type
);
2205 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2206 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2208 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2209 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2211 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2212 if (isl_int_is_zero(bmap
->div
[i
][0]))
2214 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2221 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2222 enum isl_dim_type type
, unsigned first
, unsigned n
)
2229 if (first
+ n
> isl_map_dim(map
, type
))
2230 isl_die(map
->ctx
, isl_error_invalid
,
2231 "index out of bounds", return -1);
2233 for (i
= 0; i
< map
->n
; ++i
) {
2234 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2236 if (involves
< 0 || involves
)
2243 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2244 enum isl_dim_type type
, unsigned first
, unsigned n
)
2246 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2249 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2250 enum isl_dim_type type
, unsigned first
, unsigned n
)
2252 return isl_map_involves_dims(set
, type
, first
, n
);
2255 /* Return true if the definition of the given div is unknown or depends
2258 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2261 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2263 if (isl_int_is_zero(bmap
->div
[div
][0]))
2266 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2267 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2269 if (div_is_unknown(bmap
, i
))
2276 /* Remove all divs that are unknown or defined in terms of unknown divs.
2278 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2279 __isl_take isl_basic_map
*bmap
)
2286 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2287 if (!div_is_unknown(bmap
, i
))
2289 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2298 /* Remove all divs that are unknown or defined in terms of unknown divs.
2300 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2301 __isl_take isl_basic_set
*bset
)
2303 return isl_basic_map_remove_unknown_divs(bset
);
2306 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2315 map
= isl_map_cow(map
);
2319 for (i
= 0; i
< map
->n
; ++i
) {
2320 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2330 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2332 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2335 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2336 __isl_take isl_basic_set
*bset
,
2337 enum isl_dim_type type
, unsigned first
, unsigned n
)
2339 return (isl_basic_set
*)
2340 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2343 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2344 enum isl_dim_type type
, unsigned first
, unsigned n
)
2351 map
= isl_map_cow(map
);
2354 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2356 for (i
= 0; i
< map
->n
; ++i
) {
2357 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2358 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2362 map
= isl_map_drop(map
, type
, first
, n
);
2369 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2370 enum isl_dim_type type
, unsigned first
, unsigned n
)
2372 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2375 /* Project out n inputs starting at first using Fourier-Motzkin */
2376 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2377 unsigned first
, unsigned n
)
2379 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2382 static void dump_term(struct isl_basic_map
*bmap
,
2383 isl_int c
, int pos
, FILE *out
)
2386 unsigned in
= isl_basic_map_n_in(bmap
);
2387 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2388 unsigned nparam
= isl_basic_map_n_param(bmap
);
2390 isl_int_print(out
, c
, 0);
2392 if (!isl_int_is_one(c
))
2393 isl_int_print(out
, c
, 0);
2394 if (pos
< 1 + nparam
) {
2395 name
= isl_space_get_dim_name(bmap
->dim
,
2396 isl_dim_param
, pos
- 1);
2398 fprintf(out
, "%s", name
);
2400 fprintf(out
, "p%d", pos
- 1);
2401 } else if (pos
< 1 + nparam
+ in
)
2402 fprintf(out
, "i%d", pos
- 1 - nparam
);
2403 else if (pos
< 1 + nparam
+ dim
)
2404 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2406 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2410 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2411 int sign
, FILE *out
)
2415 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2419 for (i
= 0, first
= 1; i
< len
; ++i
) {
2420 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2423 fprintf(out
, " + ");
2425 isl_int_abs(v
, c
[i
]);
2426 dump_term(bmap
, v
, i
, out
);
2433 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2434 const char *op
, FILE *out
, int indent
)
2438 fprintf(out
, "%*s", indent
, "");
2440 dump_constraint_sign(bmap
, c
, 1, out
);
2441 fprintf(out
, " %s ", op
);
2442 dump_constraint_sign(bmap
, c
, -1, out
);
2446 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2447 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2449 fprintf(out
, "%*s", indent
, "");
2450 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2455 static void dump_constraints(struct isl_basic_map
*bmap
,
2456 isl_int
**c
, unsigned n
,
2457 const char *op
, FILE *out
, int indent
)
2461 for (i
= 0; i
< n
; ++i
)
2462 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2465 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2469 unsigned total
= isl_basic_map_total_dim(bmap
);
2471 for (j
= 0; j
< 1 + total
; ++j
) {
2472 if (isl_int_is_zero(exp
[j
]))
2474 if (!first
&& isl_int_is_pos(exp
[j
]))
2476 dump_term(bmap
, exp
[j
], j
, out
);
2481 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2485 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2486 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2488 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2489 fprintf(out
, "%*s", indent
, "");
2490 fprintf(out
, "e%d = [(", i
);
2491 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2493 isl_int_print(out
, bmap
->div
[i
][0], 0);
2494 fprintf(out
, "]\n");
2498 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2499 FILE *out
, int indent
)
2502 fprintf(out
, "null basic set\n");
2506 fprintf(out
, "%*s", indent
, "");
2507 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2508 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2509 bset
->extra
, bset
->flags
);
2510 dump((struct isl_basic_map
*)bset
, out
, indent
);
2513 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2514 FILE *out
, int indent
)
2517 fprintf(out
, "null basic map\n");
2521 fprintf(out
, "%*s", indent
, "");
2522 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2523 "flags: %x, n_name: %d\n",
2525 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2526 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2527 dump(bmap
, out
, indent
);
2530 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2535 total
= isl_basic_map_total_dim(bmap
);
2536 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2537 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2538 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2539 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2543 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2546 struct isl_set
*set
;
2550 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2551 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2552 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2553 sizeof(struct isl_set
) +
2554 (n
- 1) * sizeof(struct isl_basic_set
*));
2558 set
->ctx
= dim
->ctx
;
2559 isl_ctx_ref(set
->ctx
);
2567 isl_space_free(dim
);
2571 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2572 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2574 struct isl_set
*set
;
2577 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2581 set
= isl_set_alloc_space(dims
, n
, flags
);
2585 /* Make sure "map" has room for at least "n" more basic maps.
2587 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2590 struct isl_map
*grown
= NULL
;
2594 isl_assert(map
->ctx
, n
>= 0, goto error
);
2595 if (map
->n
+ n
<= map
->size
)
2597 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2600 for (i
= 0; i
< map
->n
; ++i
) {
2601 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2609 isl_map_free(grown
);
2614 /* Make sure "set" has room for at least "n" more basic sets.
2616 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2618 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2621 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2624 struct isl_set
*dup
;
2629 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2632 for (i
= 0; i
< set
->n
; ++i
)
2633 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2637 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2639 return isl_map_from_basic_map(bset
);
2642 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2644 struct isl_map
*map
;
2649 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2650 return isl_map_add_basic_map(map
, bmap
);
2653 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2654 __isl_take isl_basic_set
*bset
)
2656 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2657 (struct isl_basic_map
*)bset
);
2660 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2670 isl_ctx_deref(set
->ctx
);
2671 for (i
= 0; i
< set
->n
; ++i
)
2672 isl_basic_set_free(set
->p
[i
]);
2673 isl_space_free(set
->dim
);
2679 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2684 fprintf(out
, "null set\n");
2688 fprintf(out
, "%*s", indent
, "");
2689 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2690 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2692 for (i
= 0; i
< set
->n
; ++i
) {
2693 fprintf(out
, "%*s", indent
, "");
2694 fprintf(out
, "basic set %d:\n", i
);
2695 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2699 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2704 fprintf(out
, "null map\n");
2708 fprintf(out
, "%*s", indent
, "");
2709 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2710 "flags: %x, n_name: %d\n",
2711 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2712 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2713 for (i
= 0; i
< map
->n
; ++i
) {
2714 fprintf(out
, "%*s", indent
, "");
2715 fprintf(out
, "basic map %d:\n", i
);
2716 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2720 struct isl_basic_map
*isl_basic_map_intersect_domain(
2721 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2723 struct isl_basic_map
*bmap_domain
;
2728 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2729 bset
->dim
, isl_dim_param
), goto error
);
2731 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2732 isl_assert(bset
->ctx
,
2733 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2735 bmap
= isl_basic_map_cow(bmap
);
2738 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2739 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2740 bmap_domain
= isl_basic_map_from_domain(bset
);
2741 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2743 bmap
= isl_basic_map_simplify(bmap
);
2744 return isl_basic_map_finalize(bmap
);
2746 isl_basic_map_free(bmap
);
2747 isl_basic_set_free(bset
);
2751 struct isl_basic_map
*isl_basic_map_intersect_range(
2752 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2754 struct isl_basic_map
*bmap_range
;
2759 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2760 bset
->dim
, isl_dim_param
), goto error
);
2762 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2763 isl_assert(bset
->ctx
,
2764 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2766 if (isl_basic_set_is_universe(bset
)) {
2767 isl_basic_set_free(bset
);
2771 bmap
= isl_basic_map_cow(bmap
);
2774 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2775 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2776 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2777 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2779 bmap
= isl_basic_map_simplify(bmap
);
2780 return isl_basic_map_finalize(bmap
);
2782 isl_basic_map_free(bmap
);
2783 isl_basic_set_free(bset
);
2787 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2796 total
= 1 + isl_basic_map_total_dim(bmap
);
2797 if (total
!= vec
->size
)
2802 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2803 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2804 if (!isl_int_is_zero(s
)) {
2810 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2811 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2812 if (isl_int_is_neg(s
)) {
2823 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2825 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2828 struct isl_basic_map
*isl_basic_map_intersect(
2829 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2831 struct isl_vec
*sample
= NULL
;
2833 if (!bmap1
|| !bmap2
)
2836 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2837 bmap2
->dim
, isl_dim_param
), goto error
);
2838 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2839 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2840 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2841 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2842 return isl_basic_map_intersect(bmap2
, bmap1
);
2844 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2845 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2846 isl_assert(bmap1
->ctx
,
2847 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2849 if (bmap1
->sample
&&
2850 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2851 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2852 sample
= isl_vec_copy(bmap1
->sample
);
2853 else if (bmap2
->sample
&&
2854 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2855 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2856 sample
= isl_vec_copy(bmap2
->sample
);
2858 bmap1
= isl_basic_map_cow(bmap1
);
2861 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2862 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2863 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2866 isl_vec_free(sample
);
2868 isl_vec_free(bmap1
->sample
);
2869 bmap1
->sample
= sample
;
2872 bmap1
= isl_basic_map_simplify(bmap1
);
2873 return isl_basic_map_finalize(bmap1
);
2876 isl_vec_free(sample
);
2877 isl_basic_map_free(bmap1
);
2878 isl_basic_map_free(bmap2
);
2882 struct isl_basic_set
*isl_basic_set_intersect(
2883 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2885 return (struct isl_basic_set
*)
2886 isl_basic_map_intersect(
2887 (struct isl_basic_map
*)bset1
,
2888 (struct isl_basic_map
*)bset2
);
2891 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2892 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2894 return isl_basic_set_intersect(bset1
, bset2
);
2897 /* Special case of isl_map_intersect, where both map1 and map2
2898 * are convex, without any divs and such that either map1 or map2
2899 * contains a single constraint. This constraint is then simply
2900 * added to the other map.
2902 static __isl_give isl_map
*map_intersect_add_constraint(
2903 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2905 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2906 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2907 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2908 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2910 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2911 return isl_map_intersect(map2
, map1
);
2913 isl_assert(map2
->ctx
,
2914 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2916 map1
= isl_map_cow(map1
);
2919 if (isl_map_plain_is_empty(map1
)) {
2923 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2924 if (map2
->p
[0]->n_eq
== 1)
2925 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2927 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2928 map2
->p
[0]->ineq
[0]);
2930 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2931 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2935 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2936 isl_basic_map_free(map1
->p
[0]);
2949 /* map2 may be either a parameter domain or a map living in the same
2952 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2953 __isl_take isl_map
*map2
)
2962 if ((isl_map_plain_is_empty(map1
) ||
2963 isl_map_plain_is_universe(map2
)) &&
2964 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2968 if ((isl_map_plain_is_empty(map2
) ||
2969 isl_map_plain_is_universe(map1
)) &&
2970 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2975 if (map1
->n
== 1 && map2
->n
== 1 &&
2976 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2977 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2978 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2979 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2980 return map_intersect_add_constraint(map1
, map2
);
2982 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2983 isl_space_dim(map2
->dim
, isl_dim_param
))
2984 isl_assert(map1
->ctx
,
2985 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2987 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2988 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2989 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2991 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2992 map1
->n
* map2
->n
, flags
);
2995 for (i
= 0; i
< map1
->n
; ++i
)
2996 for (j
= 0; j
< map2
->n
; ++j
) {
2997 struct isl_basic_map
*part
;
2998 part
= isl_basic_map_intersect(
2999 isl_basic_map_copy(map1
->p
[i
]),
3000 isl_basic_map_copy(map2
->p
[j
]));
3001 if (isl_basic_map_is_empty(part
) < 0)
3002 part
= isl_basic_map_free(part
);
3003 result
= isl_map_add_basic_map(result
, part
);
3016 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3017 __isl_take isl_map
*map2
)
3021 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3022 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3023 "spaces don't match", goto error
);
3024 return map_intersect_internal(map1
, map2
);
3031 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3032 __isl_take isl_map
*map2
)
3034 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3037 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3039 return (struct isl_set
*)
3040 isl_map_intersect((struct isl_map
*)set1
,
3041 (struct isl_map
*)set2
);
3044 /* map_intersect_internal accepts intersections
3045 * with parameter domains, so we can just call that function.
3047 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3048 __isl_take isl_set
*params
)
3050 return map_intersect_internal(map
, params
);
3053 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3054 __isl_take isl_map
*map2
)
3056 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3059 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3060 __isl_take isl_set
*params
)
3062 return isl_map_intersect_params(set
, params
);
3065 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3068 struct isl_basic_set
*bset
;
3073 bmap
= isl_basic_map_cow(bmap
);
3076 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3077 in
= isl_basic_map_n_in(bmap
);
3078 bset
= isl_basic_set_from_basic_map(bmap
);
3079 bset
= isl_basic_set_swap_vars(bset
, in
);
3080 return isl_basic_map_from_basic_set(bset
, dim
);
3083 static __isl_give isl_basic_map
*basic_map_space_reset(
3084 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3090 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3093 space
= isl_basic_map_get_space(bmap
);
3094 space
= isl_space_reset(space
, type
);
3095 bmap
= isl_basic_map_reset_space(bmap
, space
);
3099 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3100 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3101 unsigned pos
, unsigned n
)
3104 struct isl_basic_map
*res
;
3105 struct isl_dim_map
*dim_map
;
3106 unsigned total
, off
;
3107 enum isl_dim_type t
;
3110 return basic_map_space_reset(bmap
, type
);
3115 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3117 total
= isl_basic_map_total_dim(bmap
) + n
;
3118 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3120 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3122 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3124 unsigned size
= isl_basic_map_dim(bmap
, t
);
3125 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3127 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3128 pos
, size
- pos
, off
+ pos
+ n
);
3130 off
+= isl_space_dim(res_dim
, t
);
3132 isl_dim_map_div(dim_map
, bmap
, off
);
3134 res
= isl_basic_map_alloc_space(res_dim
,
3135 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3136 if (isl_basic_map_is_rational(bmap
))
3137 res
= isl_basic_map_set_rational(res
);
3138 if (isl_basic_map_plain_is_empty(bmap
)) {
3139 isl_basic_map_free(bmap
);
3141 return isl_basic_map_set_to_empty(res
);
3143 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3144 return isl_basic_map_finalize(res
);
3147 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3148 __isl_take isl_basic_set
*bset
,
3149 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3151 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3154 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3155 enum isl_dim_type type
, unsigned n
)
3159 return isl_basic_map_insert_dims(bmap
, type
,
3160 isl_basic_map_dim(bmap
, type
), n
);
3163 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3164 enum isl_dim_type type
, unsigned n
)
3168 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3169 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3171 isl_basic_set_free(bset
);
3175 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3176 enum isl_dim_type type
)
3180 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3183 space
= isl_map_get_space(map
);
3184 space
= isl_space_reset(space
, type
);
3185 map
= isl_map_reset_space(map
, space
);
3189 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3190 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3195 return map_space_reset(map
, type
);
3197 map
= isl_map_cow(map
);
3201 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3205 for (i
= 0; i
< map
->n
; ++i
) {
3206 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3217 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3218 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3220 return isl_map_insert_dims(set
, type
, pos
, n
);
3223 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3224 enum isl_dim_type type
, unsigned n
)
3228 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3231 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3232 enum isl_dim_type type
, unsigned n
)
3236 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3237 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3243 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3244 __isl_take isl_basic_map
*bmap
,
3245 enum isl_dim_type dst_type
, unsigned dst_pos
,
3246 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3248 struct isl_dim_map
*dim_map
;
3249 struct isl_basic_map
*res
;
3250 enum isl_dim_type t
;
3251 unsigned total
, off
;
3258 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3261 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3264 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3266 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3267 pos(bmap
->dim
, src_type
) + src_pos
+
3268 ((src_type
< dst_type
) ? n
: 0)) {
3269 bmap
= isl_basic_map_cow(bmap
);
3273 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3274 src_type
, src_pos
, n
);
3278 bmap
= isl_basic_map_finalize(bmap
);
3283 total
= isl_basic_map_total_dim(bmap
);
3284 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3287 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3288 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3289 if (t
== dst_type
) {
3290 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3293 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3296 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3297 dst_pos
, size
- dst_pos
, off
);
3298 off
+= size
- dst_pos
;
3299 } else if (t
== src_type
) {
3300 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3303 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3304 src_pos
+ n
, size
- src_pos
- n
, off
);
3305 off
+= size
- src_pos
- n
;
3307 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3311 isl_dim_map_div(dim_map
, bmap
, off
);
3313 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3314 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3315 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3319 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3320 src_type
, src_pos
, n
);
3324 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3325 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3326 bmap
= isl_basic_map_finalize(bmap
);
3330 isl_basic_map_free(bmap
);
3334 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3335 enum isl_dim_type dst_type
, unsigned dst_pos
,
3336 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3338 return (isl_basic_set
*)isl_basic_map_move_dims(
3339 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3342 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3343 enum isl_dim_type dst_type
, unsigned dst_pos
,
3344 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3348 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3349 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3350 src_type
, src_pos
, n
);
3356 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3357 enum isl_dim_type dst_type
, unsigned dst_pos
,
3358 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3367 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3370 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3373 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3375 map
= isl_map_cow(map
);
3379 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3383 for (i
= 0; i
< map
->n
; ++i
) {
3384 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3386 src_type
, src_pos
, n
);
3397 /* Move the specified dimensions to the last columns right before
3398 * the divs. Don't change the dimension specification of bmap.
3399 * That's the responsibility of the caller.
3401 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3402 enum isl_dim_type type
, unsigned first
, unsigned n
)
3404 struct isl_dim_map
*dim_map
;
3405 struct isl_basic_map
*res
;
3406 enum isl_dim_type t
;
3407 unsigned total
, off
;
3411 if (pos(bmap
->dim
, type
) + first
+ n
==
3412 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3415 total
= isl_basic_map_total_dim(bmap
);
3416 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3419 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3420 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3422 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3425 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3426 first
, n
, total
- bmap
->n_div
- n
);
3427 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3428 first
+ n
, size
- (first
+ n
), off
);
3429 off
+= size
- (first
+ n
);
3431 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3435 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3437 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3438 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3439 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3443 /* Insert "n" rows in the divs of "bmap".
3445 * The number of columns is not changed, which means that the last
3446 * dimensions of "bmap" are being reintepreted as the new divs.
3447 * The space of "bmap" is not adjusted, however, which means
3448 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3449 * from the space of "bmap" is the responsibility of the caller.
3451 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3459 bmap
= isl_basic_map_cow(bmap
);
3463 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3464 old
= bmap
->block2
.data
;
3465 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3466 (bmap
->extra
+ n
) * (1 + row_size
));
3467 if (!bmap
->block2
.data
)
3468 return isl_basic_map_free(bmap
);
3469 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3471 return isl_basic_map_free(bmap
);
3472 for (i
= 0; i
< n
; ++i
) {
3473 new_div
[i
] = bmap
->block2
.data
+
3474 (bmap
->extra
+ i
) * (1 + row_size
);
3475 isl_seq_clr(new_div
[i
], 1 + row_size
);
3477 for (i
= 0; i
< bmap
->extra
; ++i
)
3478 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3480 bmap
->div
= new_div
;
3487 /* Turn the n dimensions of type type, starting at first
3488 * into existentially quantified variables.
3490 __isl_give isl_basic_map
*isl_basic_map_project_out(
3491 __isl_take isl_basic_map
*bmap
,
3492 enum isl_dim_type type
, unsigned first
, unsigned n
)
3495 return basic_map_space_reset(bmap
, type
);
3500 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3501 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3503 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3506 bmap
= move_last(bmap
, type
, first
, n
);
3507 bmap
= isl_basic_map_cow(bmap
);
3508 bmap
= insert_div_rows(bmap
, n
);
3512 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3515 bmap
= isl_basic_map_simplify(bmap
);
3516 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3517 return isl_basic_map_finalize(bmap
);
3519 isl_basic_map_free(bmap
);
3523 /* Turn the n dimensions of type type, starting at first
3524 * into existentially quantified variables.
3526 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3527 enum isl_dim_type type
, unsigned first
, unsigned n
)
3529 return (isl_basic_set
*)isl_basic_map_project_out(
3530 (isl_basic_map
*)bset
, type
, first
, n
);
3533 /* Turn the n dimensions of type type, starting at first
3534 * into existentially quantified variables.
3536 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3537 enum isl_dim_type type
, unsigned first
, unsigned n
)
3545 return map_space_reset(map
, type
);
3547 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3549 map
= isl_map_cow(map
);
3553 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3557 for (i
= 0; i
< map
->n
; ++i
) {
3558 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3569 /* Turn the n dimensions of type type, starting at first
3570 * into existentially quantified variables.
3572 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3573 enum isl_dim_type type
, unsigned first
, unsigned n
)
3575 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3578 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3582 for (i
= 0; i
< n
; ++i
) {
3583 j
= isl_basic_map_alloc_div(bmap
);
3586 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3590 isl_basic_map_free(bmap
);
3594 struct isl_basic_map
*isl_basic_map_apply_range(
3595 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3597 isl_space
*dim_result
= NULL
;
3598 struct isl_basic_map
*bmap
;
3599 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3600 struct isl_dim_map
*dim_map1
, *dim_map2
;
3602 if (!bmap1
|| !bmap2
)
3604 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3605 bmap2
->dim
, isl_dim_param
))
3606 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3607 "parameters don't match", goto error
);
3608 if (!isl_space_tuple_match(bmap1
->dim
, isl_dim_out
,
3609 bmap2
->dim
, isl_dim_in
))
3610 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3611 "spaces don't match", goto error
);
3613 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3614 isl_space_copy(bmap2
->dim
));
3616 n_in
= isl_basic_map_n_in(bmap1
);
3617 n_out
= isl_basic_map_n_out(bmap2
);
3618 n
= isl_basic_map_n_out(bmap1
);
3619 nparam
= isl_basic_map_n_param(bmap1
);
3621 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3622 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3623 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3624 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3625 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3626 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3627 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3628 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3629 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3630 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3631 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3633 bmap
= isl_basic_map_alloc_space(dim_result
,
3634 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3635 bmap1
->n_eq
+ bmap2
->n_eq
,
3636 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3637 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3638 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3639 bmap
= add_divs(bmap
, n
);
3640 bmap
= isl_basic_map_simplify(bmap
);
3641 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3642 return isl_basic_map_finalize(bmap
);
3644 isl_basic_map_free(bmap1
);
3645 isl_basic_map_free(bmap2
);
3649 struct isl_basic_set
*isl_basic_set_apply(
3650 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3655 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3658 return (struct isl_basic_set
*)
3659 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3661 isl_basic_set_free(bset
);
3662 isl_basic_map_free(bmap
);
3666 struct isl_basic_map
*isl_basic_map_apply_domain(
3667 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3669 if (!bmap1
|| !bmap2
)
3672 isl_assert(bmap1
->ctx
,
3673 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3674 isl_assert(bmap1
->ctx
,
3675 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3678 bmap1
= isl_basic_map_reverse(bmap1
);
3679 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3680 return isl_basic_map_reverse(bmap1
);
3682 isl_basic_map_free(bmap1
);
3683 isl_basic_map_free(bmap2
);
3687 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3688 * A \cap B -> f(A) + f(B)
3690 struct isl_basic_map
*isl_basic_map_sum(
3691 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3693 unsigned n_in
, n_out
, nparam
, total
, pos
;
3694 struct isl_basic_map
*bmap
= NULL
;
3695 struct isl_dim_map
*dim_map1
, *dim_map2
;
3698 if (!bmap1
|| !bmap2
)
3701 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3704 nparam
= isl_basic_map_n_param(bmap1
);
3705 n_in
= isl_basic_map_n_in(bmap1
);
3706 n_out
= isl_basic_map_n_out(bmap1
);
3708 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3709 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3710 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3711 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3712 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3713 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3714 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3715 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3716 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3717 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3718 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3720 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3721 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3722 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3723 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3724 for (i
= 0; i
< n_out
; ++i
) {
3725 int j
= isl_basic_map_alloc_equality(bmap
);
3728 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3729 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3730 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3731 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3733 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3734 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3735 bmap
= add_divs(bmap
, 2 * n_out
);
3737 bmap
= isl_basic_map_simplify(bmap
);
3738 return isl_basic_map_finalize(bmap
);
3740 isl_basic_map_free(bmap
);
3741 isl_basic_map_free(bmap1
);
3742 isl_basic_map_free(bmap2
);
3746 /* Given two maps A -> f(A) and B -> g(B), construct a map
3747 * A \cap B -> f(A) + f(B)
3749 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3751 struct isl_map
*result
;
3757 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3759 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3760 map1
->n
* map2
->n
, 0);
3763 for (i
= 0; i
< map1
->n
; ++i
)
3764 for (j
= 0; j
< map2
->n
; ++j
) {
3765 struct isl_basic_map
*part
;
3766 part
= isl_basic_map_sum(
3767 isl_basic_map_copy(map1
->p
[i
]),
3768 isl_basic_map_copy(map2
->p
[j
]));
3769 if (isl_basic_map_is_empty(part
))
3770 isl_basic_map_free(part
);
3772 result
= isl_map_add_basic_map(result
, part
);
3785 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3786 __isl_take isl_set
*set2
)
3788 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3791 /* Given a basic map A -> f(A), construct A -> -f(A).
3793 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3798 bmap
= isl_basic_map_cow(bmap
);
3802 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3803 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3804 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3805 for (j
= 0; j
< n
; ++j
)
3806 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3807 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3808 for (j
= 0; j
< n
; ++j
)
3809 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3810 for (i
= 0; i
< bmap
->n_div
; ++i
)
3811 for (j
= 0; j
< n
; ++j
)
3812 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3813 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3814 return isl_basic_map_finalize(bmap
);
3817 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3819 return isl_basic_map_neg(bset
);
3822 /* Given a map A -> f(A), construct A -> -f(A).
3824 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3828 map
= isl_map_cow(map
);
3832 for (i
= 0; i
< map
->n
; ++i
) {
3833 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3844 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3846 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3849 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3850 * A -> floor(f(A)/d).
3852 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3855 unsigned n_in
, n_out
, nparam
, total
, pos
;
3856 struct isl_basic_map
*result
= NULL
;
3857 struct isl_dim_map
*dim_map
;
3863 nparam
= isl_basic_map_n_param(bmap
);
3864 n_in
= isl_basic_map_n_in(bmap
);
3865 n_out
= isl_basic_map_n_out(bmap
);
3867 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3868 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3869 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3870 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3871 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3872 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3874 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3875 bmap
->n_div
+ n_out
,
3876 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3877 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3878 result
= add_divs(result
, n_out
);
3879 for (i
= 0; i
< n_out
; ++i
) {
3881 j
= isl_basic_map_alloc_inequality(result
);
3884 isl_seq_clr(result
->ineq
[j
], 1+total
);
3885 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3886 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3887 j
= isl_basic_map_alloc_inequality(result
);
3890 isl_seq_clr(result
->ineq
[j
], 1+total
);
3891 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3892 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3893 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3896 result
= isl_basic_map_simplify(result
);
3897 return isl_basic_map_finalize(result
);
3899 isl_basic_map_free(result
);
3903 /* Given a map A -> f(A) and an integer d, construct a map
3904 * A -> floor(f(A)/d).
3906 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3910 map
= isl_map_cow(map
);
3914 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3915 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3916 for (i
= 0; i
< map
->n
; ++i
) {
3917 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3928 /* Given a map A -> f(A) and an integer d, construct a map
3929 * A -> floor(f(A)/d).
3931 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3932 __isl_take isl_val
*d
)
3936 if (!isl_val_is_int(d
))
3937 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3938 "expecting integer denominator", goto error
);
3939 map
= isl_map_floordiv(map
, d
->n
);
3948 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3954 i
= isl_basic_map_alloc_equality(bmap
);
3957 nparam
= isl_basic_map_n_param(bmap
);
3958 n_in
= isl_basic_map_n_in(bmap
);
3959 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3960 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3961 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3962 return isl_basic_map_finalize(bmap
);
3964 isl_basic_map_free(bmap
);
3968 /* Add a constraints to "bmap" expressing i_pos < o_pos
3970 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3976 i
= isl_basic_map_alloc_inequality(bmap
);
3979 nparam
= isl_basic_map_n_param(bmap
);
3980 n_in
= isl_basic_map_n_in(bmap
);
3981 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3982 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3983 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3984 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3985 return isl_basic_map_finalize(bmap
);
3987 isl_basic_map_free(bmap
);
3991 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3993 static __isl_give isl_basic_map
*var_less_or_equal(
3994 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4000 i
= isl_basic_map_alloc_inequality(bmap
);
4003 nparam
= isl_basic_map_n_param(bmap
);
4004 n_in
= isl_basic_map_n_in(bmap
);
4005 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4006 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4007 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4008 return isl_basic_map_finalize(bmap
);
4010 isl_basic_map_free(bmap
);
4014 /* Add a constraints to "bmap" expressing i_pos > o_pos
4016 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4022 i
= isl_basic_map_alloc_inequality(bmap
);
4025 nparam
= isl_basic_map_n_param(bmap
);
4026 n_in
= isl_basic_map_n_in(bmap
);
4027 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4028 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4029 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4030 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4031 return isl_basic_map_finalize(bmap
);
4033 isl_basic_map_free(bmap
);
4037 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4039 static __isl_give isl_basic_map
*var_more_or_equal(
4040 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4046 i
= isl_basic_map_alloc_inequality(bmap
);
4049 nparam
= isl_basic_map_n_param(bmap
);
4050 n_in
= isl_basic_map_n_in(bmap
);
4051 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4052 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4053 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4054 return isl_basic_map_finalize(bmap
);
4056 isl_basic_map_free(bmap
);
4060 __isl_give isl_basic_map
*isl_basic_map_equal(
4061 __isl_take isl_space
*dim
, unsigned n_equal
)
4064 struct isl_basic_map
*bmap
;
4065 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4068 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4069 bmap
= var_equal(bmap
, i
);
4070 return isl_basic_map_finalize(bmap
);
4073 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4075 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4079 struct isl_basic_map
*bmap
;
4080 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4083 for (i
= 0; i
< pos
&& bmap
; ++i
)
4084 bmap
= var_equal(bmap
, i
);
4086 bmap
= var_less(bmap
, pos
);
4087 return isl_basic_map_finalize(bmap
);
4090 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4092 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4093 __isl_take isl_space
*dim
, unsigned pos
)
4096 isl_basic_map
*bmap
;
4098 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4099 for (i
= 0; i
< pos
; ++i
)
4100 bmap
= var_equal(bmap
, i
);
4101 bmap
= var_less_or_equal(bmap
, pos
);
4102 return isl_basic_map_finalize(bmap
);
4105 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4107 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4111 struct isl_basic_map
*bmap
;
4112 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4115 for (i
= 0; i
< pos
&& bmap
; ++i
)
4116 bmap
= var_equal(bmap
, i
);
4118 bmap
= var_more(bmap
, pos
);
4119 return isl_basic_map_finalize(bmap
);
4122 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4124 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4125 __isl_take isl_space
*dim
, unsigned pos
)
4128 isl_basic_map
*bmap
;
4130 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4131 for (i
= 0; i
< pos
; ++i
)
4132 bmap
= var_equal(bmap
, i
);
4133 bmap
= var_more_or_equal(bmap
, pos
);
4134 return isl_basic_map_finalize(bmap
);
4137 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4138 unsigned n
, int equal
)
4140 struct isl_map
*map
;
4143 if (n
== 0 && equal
)
4144 return isl_map_universe(dims
);
4146 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4148 for (i
= 0; i
+ 1 < n
; ++i
)
4149 map
= isl_map_add_basic_map(map
,
4150 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4153 map
= isl_map_add_basic_map(map
,
4154 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4156 map
= isl_map_add_basic_map(map
,
4157 isl_basic_map_less_at(dims
, n
- 1));
4159 isl_space_free(dims
);
4164 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4168 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4171 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4173 return map_lex_lte_first(dim
, n
, 0);
4176 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4178 return map_lex_lte_first(dim
, n
, 1);
4181 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4183 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4186 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4188 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4191 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4192 unsigned n
, int equal
)
4194 struct isl_map
*map
;
4197 if (n
== 0 && equal
)
4198 return isl_map_universe(dims
);
4200 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4202 for (i
= 0; i
+ 1 < n
; ++i
)
4203 map
= isl_map_add_basic_map(map
,
4204 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4207 map
= isl_map_add_basic_map(map
,
4208 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4210 map
= isl_map_add_basic_map(map
,
4211 isl_basic_map_more_at(dims
, n
- 1));
4213 isl_space_free(dims
);
4218 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4222 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4225 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4227 return map_lex_gte_first(dim
, n
, 0);
4230 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4232 return map_lex_gte_first(dim
, n
, 1);
4235 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4237 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4240 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4242 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4245 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4246 __isl_take isl_set
*set2
)
4249 map
= isl_map_lex_le(isl_set_get_space(set1
));
4250 map
= isl_map_intersect_domain(map
, set1
);
4251 map
= isl_map_intersect_range(map
, set2
);
4255 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4256 __isl_take isl_set
*set2
)
4259 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4260 map
= isl_map_intersect_domain(map
, set1
);
4261 map
= isl_map_intersect_range(map
, set2
);
4265 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4266 __isl_take isl_set
*set2
)
4269 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4270 map
= isl_map_intersect_domain(map
, set1
);
4271 map
= isl_map_intersect_range(map
, set2
);
4275 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4276 __isl_take isl_set
*set2
)
4279 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4280 map
= isl_map_intersect_domain(map
, set1
);
4281 map
= isl_map_intersect_range(map
, set2
);
4285 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4286 __isl_take isl_map
*map2
)
4289 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4290 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4291 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4295 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4296 __isl_take isl_map
*map2
)
4299 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4300 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4301 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4305 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4306 __isl_take isl_map
*map2
)
4309 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4310 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4311 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4315 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4316 __isl_take isl_map
*map2
)
4319 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4320 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4321 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4325 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4326 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4328 struct isl_basic_map
*bmap
;
4330 bset
= isl_basic_set_cow(bset
);
4334 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4335 isl_space_free(bset
->dim
);
4336 bmap
= (struct isl_basic_map
*) bset
;
4338 return isl_basic_map_finalize(bmap
);
4340 isl_basic_set_free(bset
);
4341 isl_space_free(dim
);
4345 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4349 if (bmap
->dim
->n_in
== 0)
4350 return (struct isl_basic_set
*)bmap
;
4351 bmap
= isl_basic_map_cow(bmap
);
4354 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4357 bmap
= isl_basic_map_finalize(bmap
);
4358 return (struct isl_basic_set
*)bmap
;
4360 isl_basic_map_free(bmap
);
4364 /* For a div d = floor(f/m), add the constraint
4368 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4369 unsigned pos
, isl_int
*div
)
4372 unsigned total
= isl_basic_map_total_dim(bmap
);
4374 i
= isl_basic_map_alloc_inequality(bmap
);
4377 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4378 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4383 /* For a div d = floor(f/m), add the constraint
4385 * -(f-(n-1)) + m d >= 0
4387 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4388 unsigned pos
, isl_int
*div
)
4391 unsigned total
= isl_basic_map_total_dim(bmap
);
4393 i
= isl_basic_map_alloc_inequality(bmap
);
4396 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4397 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4398 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4399 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4404 /* For a div d = floor(f/m), add the constraints
4407 * -(f-(n-1)) + m d >= 0
4409 * Note that the second constraint is the negation of
4413 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4414 unsigned pos
, isl_int
*div
)
4416 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4418 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4423 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4424 unsigned pos
, isl_int
*div
)
4426 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4430 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4432 unsigned total
= isl_basic_map_total_dim(bmap
);
4433 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4435 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4439 /* For each known div d = floor(f/m), add the constraints
4442 * -(f-(n-1)) + m d >= 0
4444 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4445 __isl_take isl_basic_map
*bmap
)
4452 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4455 bmap
= isl_basic_map_cow(bmap
);
4456 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4459 for (i
= 0; i
< n_div
; ++i
) {
4460 if (isl_int_is_zero(bmap
->div
[i
][0]))
4462 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4463 return isl_basic_map_free(bmap
);
4466 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4467 bmap
= isl_basic_map_finalize(bmap
);
4471 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4473 * In particular, if this div is of the form d = floor(f/m),
4474 * then add the constraint
4478 * if sign < 0 or the constraint
4480 * -(f-(n-1)) + m d >= 0
4484 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4485 unsigned div
, int sign
)
4493 total
= isl_basic_map_total_dim(bmap
);
4494 div_pos
= total
- bmap
->n_div
+ div
;
4497 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4499 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4502 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4504 return isl_basic_map_add_div_constraints(bset
, div
);
4507 struct isl_basic_set
*isl_basic_map_underlying_set(
4508 struct isl_basic_map
*bmap
)
4512 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4514 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4515 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4516 return (struct isl_basic_set
*)bmap
;
4517 bmap
= isl_basic_map_cow(bmap
);
4520 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4523 bmap
->extra
-= bmap
->n_div
;
4525 bmap
= isl_basic_map_finalize(bmap
);
4526 return (struct isl_basic_set
*)bmap
;
4528 isl_basic_map_free(bmap
);
4532 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4533 __isl_take isl_basic_set
*bset
)
4535 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4538 /* Replace each element in "list" by the result of applying
4539 * isl_basic_set_underlying_set to the element.
4541 __isl_give isl_basic_set_list
*isl_basic_set_list_underlying_set(
4542 __isl_take isl_basic_set_list
*list
)
4549 n
= isl_basic_set_list_n_basic_set(list
);
4550 for (i
= 0; i
< n
; ++i
) {
4551 isl_basic_set
*bset
;
4553 bset
= isl_basic_set_list_get_basic_set(list
, i
);
4554 bset
= isl_basic_set_underlying_set(bset
);
4555 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4561 struct isl_basic_map
*isl_basic_map_overlying_set(
4562 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4564 struct isl_basic_map
*bmap
;
4565 struct isl_ctx
*ctx
;
4572 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4573 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4574 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4576 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4577 isl_basic_map_free(like
);
4578 return (struct isl_basic_map
*)bset
;
4580 bset
= isl_basic_set_cow(bset
);
4583 total
= bset
->dim
->n_out
+ bset
->extra
;
4584 bmap
= (struct isl_basic_map
*)bset
;
4585 isl_space_free(bmap
->dim
);
4586 bmap
->dim
= isl_space_copy(like
->dim
);
4589 bmap
->n_div
= like
->n_div
;
4590 bmap
->extra
+= like
->n_div
;
4594 ltotal
= total
- bmap
->extra
+ like
->extra
;
4597 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4598 bmap
->extra
* (1 + 1 + total
));
4599 if (isl_blk_is_error(bmap
->block2
))
4601 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4605 for (i
= 0; i
< bmap
->extra
; ++i
)
4606 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4607 for (i
= 0; i
< like
->n_div
; ++i
) {
4608 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4609 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4611 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4613 isl_basic_map_free(like
);
4614 bmap
= isl_basic_map_simplify(bmap
);
4615 bmap
= isl_basic_map_finalize(bmap
);
4618 isl_basic_map_free(like
);
4619 isl_basic_set_free(bset
);
4623 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4624 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4626 return (struct isl_basic_set
*)
4627 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4630 struct isl_set
*isl_set_from_underlying_set(
4631 struct isl_set
*set
, struct isl_basic_set
*like
)
4637 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4639 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4640 isl_basic_set_free(like
);
4643 set
= isl_set_cow(set
);
4646 for (i
= 0; i
< set
->n
; ++i
) {
4647 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4648 isl_basic_set_copy(like
));
4652 isl_space_free(set
->dim
);
4653 set
->dim
= isl_space_copy(like
->dim
);
4656 isl_basic_set_free(like
);
4659 isl_basic_set_free(like
);
4664 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4668 map
= isl_map_cow(map
);
4671 map
->dim
= isl_space_cow(map
->dim
);
4675 for (i
= 1; i
< map
->n
; ++i
)
4676 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4678 for (i
= 0; i
< map
->n
; ++i
) {
4679 map
->p
[i
] = (struct isl_basic_map
*)
4680 isl_basic_map_underlying_set(map
->p
[i
]);
4685 map
->dim
= isl_space_underlying(map
->dim
, 0);
4687 isl_space_free(map
->dim
);
4688 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4692 return (struct isl_set
*)map
;
4698 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4700 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4703 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4704 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4706 bmap
= isl_basic_map_cow(bmap
);
4710 isl_space_free(bmap
->dim
);
4713 bmap
= isl_basic_map_finalize(bmap
);
4717 isl_basic_map_free(bmap
);
4718 isl_space_free(dim
);
4722 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4723 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4725 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4729 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4730 __isl_take isl_space
*dim
)
4734 map
= isl_map_cow(map
);
4738 for (i
= 0; i
< map
->n
; ++i
) {
4739 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4740 isl_space_copy(dim
));
4744 isl_space_free(map
->dim
);
4750 isl_space_free(dim
);
4754 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4755 __isl_take isl_space
*dim
)
4757 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4760 /* Compute the parameter domain of the given basic set.
4762 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4767 if (isl_basic_set_is_params(bset
))
4770 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4771 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4772 space
= isl_basic_set_get_space(bset
);
4773 space
= isl_space_params(space
);
4774 bset
= isl_basic_set_reset_space(bset
, space
);
4778 /* Construct a zero-dimensional basic set with the given parameter domain.
4780 __isl_give isl_basic_set
*isl_basic_set_from_params(
4781 __isl_take isl_basic_set
*bset
)
4784 space
= isl_basic_set_get_space(bset
);
4785 space
= isl_space_set_from_params(space
);
4786 bset
= isl_basic_set_reset_space(bset
, space
);
4790 /* Compute the parameter domain of the given set.
4792 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4797 if (isl_set_is_params(set
))
4800 n
= isl_set_dim(set
, isl_dim_set
);
4801 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4802 space
= isl_set_get_space(set
);
4803 space
= isl_space_params(space
);
4804 set
= isl_set_reset_space(set
, space
);
4808 /* Construct a zero-dimensional set with the given parameter domain.
4810 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4813 space
= isl_set_get_space(set
);
4814 space
= isl_space_set_from_params(space
);
4815 set
= isl_set_reset_space(set
, space
);
4819 /* Compute the parameter domain of the given map.
4821 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4826 n
= isl_map_dim(map
, isl_dim_in
);
4827 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4828 n
= isl_map_dim(map
, isl_dim_out
);
4829 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4830 space
= isl_map_get_space(map
);
4831 space
= isl_space_params(space
);
4832 map
= isl_map_reset_space(map
, space
);
4836 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4839 struct isl_basic_set
*domain
;
4845 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4847 n_in
= isl_basic_map_n_in(bmap
);
4848 n_out
= isl_basic_map_n_out(bmap
);
4849 domain
= isl_basic_set_from_basic_map(bmap
);
4850 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4852 domain
= isl_basic_set_reset_space(domain
, dim
);
4857 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4861 return isl_space_may_be_set(bmap
->dim
);
4864 /* Is this basic map actually a set?
4865 * Users should never call this function. Outside of isl,
4866 * the type should indicate whether something is a set or a map.
4868 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4872 return isl_space_is_set(bmap
->dim
);
4875 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4879 if (isl_basic_map_is_set(bmap
))
4881 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4884 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4885 __isl_take isl_basic_map
*bmap
)
4889 isl_basic_map
*domain
;
4890 int nparam
, n_in
, n_out
;
4893 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4894 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4895 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4897 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4898 domain
= isl_basic_map_universe(dim
);
4900 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4901 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4902 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4904 total
= isl_basic_map_total_dim(bmap
);
4906 for (i
= 0; i
< n_in
; ++i
) {
4907 k
= isl_basic_map_alloc_equality(bmap
);
4910 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4911 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4912 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4915 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4916 return isl_basic_map_finalize(bmap
);
4918 isl_basic_map_free(bmap
);
4922 __isl_give isl_basic_map
*isl_basic_map_range_map(
4923 __isl_take isl_basic_map
*bmap
)
4927 isl_basic_map
*range
;
4928 int nparam
, n_in
, n_out
;
4931 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4932 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4933 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4935 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4936 range
= isl_basic_map_universe(dim
);
4938 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4939 bmap
= isl_basic_map_apply_range(bmap
, range
);
4940 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4942 total
= isl_basic_map_total_dim(bmap
);
4944 for (i
= 0; i
< n_out
; ++i
) {
4945 k
= isl_basic_map_alloc_equality(bmap
);
4948 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4949 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4950 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4953 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4954 return isl_basic_map_finalize(bmap
);
4956 isl_basic_map_free(bmap
);
4960 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4964 return isl_space_may_be_set(map
->dim
);
4967 /* Is this map actually a set?
4968 * Users should never call this function. Outside of isl,
4969 * the type should indicate whether something is a set or a map.
4971 int isl_map_is_set(__isl_keep isl_map
*map
)
4975 return isl_space_is_set(map
->dim
);
4978 struct isl_set
*isl_map_range(struct isl_map
*map
)
4981 struct isl_set
*set
;
4985 if (isl_map_is_set(map
))
4986 return (isl_set
*)map
;
4988 map
= isl_map_cow(map
);
4992 set
= (struct isl_set
*) map
;
4993 set
->dim
= isl_space_range(set
->dim
);
4996 for (i
= 0; i
< map
->n
; ++i
) {
4997 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5001 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5002 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5009 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5013 map
= isl_map_cow(map
);
5017 map
->dim
= isl_space_domain_map(map
->dim
);
5020 for (i
= 0; i
< map
->n
; ++i
) {
5021 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5025 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5026 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5033 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5036 isl_space
*range_dim
;
5038 map
= isl_map_cow(map
);
5042 range_dim
= isl_space_range(isl_map_get_space(map
));
5043 range_dim
= isl_space_from_range(range_dim
);
5044 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5045 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5048 for (i
= 0; i
< map
->n
; ++i
) {
5049 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5053 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5054 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5061 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5062 __isl_take isl_space
*dim
)
5065 struct isl_map
*map
= NULL
;
5067 set
= isl_set_cow(set
);
5070 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5071 map
= (struct isl_map
*)set
;
5072 for (i
= 0; i
< set
->n
; ++i
) {
5073 map
->p
[i
] = isl_basic_map_from_basic_set(
5074 set
->p
[i
], isl_space_copy(dim
));
5078 isl_space_free(map
->dim
);
5082 isl_space_free(dim
);
5087 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5088 __isl_take isl_basic_set
*bset
)
5090 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5093 __isl_give isl_basic_map
*isl_basic_map_from_range(
5094 __isl_take isl_basic_set
*bset
)
5097 space
= isl_basic_set_get_space(bset
);
5098 space
= isl_space_from_range(space
);
5099 bset
= isl_basic_set_reset_space(bset
, space
);
5100 return (isl_basic_map
*)bset
;
5103 /* Create a relation with the given set as range.
5104 * The domain of the created relation is a zero-dimensional
5105 * flat anonymous space.
5107 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5110 space
= isl_set_get_space(set
);
5111 space
= isl_space_from_range(space
);
5112 set
= isl_set_reset_space(set
, space
);
5113 return (struct isl_map
*)set
;
5116 /* Create a relation with the given set as domain.
5117 * The range of the created relation is a zero-dimensional
5118 * flat anonymous space.
5120 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5122 return isl_map_reverse(isl_map_from_range(set
));
5125 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5126 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5128 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5131 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5132 __isl_take isl_set
*range
)
5134 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5137 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
5140 struct isl_set
*set
= NULL
;
5144 map
= isl_map_cow(map
);
5147 map
->dim
= isl_space_as_set_space(map
->dim
);
5150 set
= (struct isl_set
*)map
;
5151 for (i
= 0; i
< map
->n
; ++i
) {
5152 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5162 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5165 struct isl_map
*map
;
5170 isl_die(dim
->ctx
, isl_error_internal
,
5171 "negative number of basic maps", goto error
);
5172 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5173 sizeof(struct isl_map
) +
5174 (n
- 1) * sizeof(struct isl_basic_map
*));
5178 map
->ctx
= dim
->ctx
;
5179 isl_ctx_ref(map
->ctx
);
5187 isl_space_free(dim
);
5191 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5192 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5195 struct isl_map
*map
;
5198 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5202 map
= isl_map_alloc_space(dims
, n
, flags
);
5206 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5208 struct isl_basic_map
*bmap
;
5209 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5210 bmap
= isl_basic_map_set_to_empty(bmap
);
5214 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5216 struct isl_basic_set
*bset
;
5217 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5218 bset
= isl_basic_set_set_to_empty(bset
);
5222 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5224 struct isl_basic_map
*bmap
;
5227 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5228 bmap
= isl_basic_map_set_to_empty(bmap
);
5232 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5234 struct isl_basic_map
*bmap
;
5237 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5238 bmap
= isl_basic_map_set_to_empty(bmap
);
5242 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5244 struct isl_basic_set
*bset
;
5247 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5248 bset
= isl_basic_set_set_to_empty(bset
);
5252 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5254 struct isl_basic_map
*bmap
;
5255 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5256 bmap
= isl_basic_map_finalize(bmap
);
5260 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5262 struct isl_basic_set
*bset
;
5263 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5264 bset
= isl_basic_set_finalize(bset
);
5268 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5271 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5272 isl_basic_map
*bmap
;
5274 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5275 for (i
= 0; i
< total
; ++i
) {
5276 int k
= isl_basic_map_alloc_inequality(bmap
);
5279 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5280 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5284 isl_basic_map_free(bmap
);
5288 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5290 return isl_basic_map_nat_universe(dim
);
5293 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5295 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5298 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5300 return isl_map_nat_universe(dim
);
5303 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5304 __isl_keep isl_basic_map
*model
)
5308 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5311 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5315 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5318 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5319 __isl_keep isl_set
*model
)
5323 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5326 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5328 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5331 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5335 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5338 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5342 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5345 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5347 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5350 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5354 return isl_set_empty(isl_space_copy(model
->dim
));
5357 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5359 struct isl_map
*map
;
5362 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5363 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5367 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5369 struct isl_set
*set
;
5372 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5373 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5377 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5381 return isl_set_universe(isl_space_copy(model
->dim
));
5384 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5387 struct isl_map
*dup
;
5391 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5392 for (i
= 0; i
< map
->n
; ++i
)
5393 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5397 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5398 __isl_take isl_basic_map
*bmap
)
5402 if (isl_basic_map_plain_is_empty(bmap
)) {
5403 isl_basic_map_free(bmap
);
5406 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5407 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5408 map
->p
[map
->n
] = bmap
;
5410 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5416 isl_basic_map_free(bmap
);
5420 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5430 isl_ctx_deref(map
->ctx
);
5431 for (i
= 0; i
< map
->n
; ++i
)
5432 isl_basic_map_free(map
->p
[i
]);
5433 isl_space_free(map
->dim
);
5439 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5440 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5444 base
= isl_map_cow(base
);
5448 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5451 for (i
= 0; i
< base
->n
; ++i
) {
5452 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5453 isl_space_copy(base
->dim
), 0, 0, 0);
5463 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5464 unsigned nparam
, unsigned dim
)
5466 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5470 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5471 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5475 bmap
= isl_basic_map_cow(bmap
);
5476 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5477 j
= isl_basic_map_alloc_equality(bmap
);
5480 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5481 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5482 isl_int_set_si(bmap
->eq
[j
][0], value
);
5483 bmap
= isl_basic_map_simplify(bmap
);
5484 return isl_basic_map_finalize(bmap
);
5486 isl_basic_map_free(bmap
);
5490 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5491 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5495 bmap
= isl_basic_map_cow(bmap
);
5496 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5497 j
= isl_basic_map_alloc_equality(bmap
);
5500 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5501 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5502 isl_int_set(bmap
->eq
[j
][0], value
);
5503 bmap
= isl_basic_map_simplify(bmap
);
5504 return isl_basic_map_finalize(bmap
);
5506 isl_basic_map_free(bmap
);
5510 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5511 enum isl_dim_type type
, unsigned pos
, int value
)
5515 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5516 return isl_basic_map_fix_pos_si(bmap
,
5517 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5519 isl_basic_map_free(bmap
);
5523 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5524 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5528 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5529 return isl_basic_map_fix_pos(bmap
,
5530 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5532 isl_basic_map_free(bmap
);
5536 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5537 * to be equal to "v".
5539 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5540 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5544 if (!isl_val_is_int(v
))
5545 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5546 "expecting integer value", goto error
);
5547 if (pos
>= isl_basic_map_dim(bmap
, type
))
5548 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5549 "index out of bounds", goto error
);
5550 pos
+= isl_basic_map_offset(bmap
, type
);
5551 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5555 isl_basic_map_free(bmap
);
5560 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5561 * to be equal to "v".
5563 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5564 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5566 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5569 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5570 enum isl_dim_type type
, unsigned pos
, int value
)
5572 return (struct isl_basic_set
*)
5573 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5577 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5578 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5580 return (struct isl_basic_set
*)
5581 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5585 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5586 unsigned input
, int value
)
5588 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5591 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5592 unsigned dim
, int value
)
5594 return (struct isl_basic_set
*)
5595 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5596 isl_dim_set
, dim
, value
);
5599 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5601 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5608 isl_basic_map_free(map
->p
[i
]);
5609 if (i
!= map
->n
- 1) {
5610 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5611 map
->p
[i
] = map
->p
[map
->n
- 1];
5618 /* Perform "fn" on each basic map of "map", where we may not be holding
5619 * the only reference to "map".
5620 * In particular, "fn" should be a semantics preserving operation
5621 * that we want to apply to all copies of "map". We therefore need
5622 * to be careful not to modify "map" in a way that breaks "map"
5623 * in case anything goes wrong.
5625 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5626 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5628 struct isl_basic_map
*bmap
;
5634 for (i
= map
->n
- 1; i
>= 0; --i
) {
5635 bmap
= isl_basic_map_copy(map
->p
[i
]);
5639 isl_basic_map_free(map
->p
[i
]);
5641 if (remove_if_empty(map
, i
) < 0)
5651 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5652 enum isl_dim_type type
, unsigned pos
, int value
)
5656 map
= isl_map_cow(map
);
5660 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5661 for (i
= map
->n
- 1; i
>= 0; --i
) {
5662 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5663 if (remove_if_empty(map
, i
) < 0)
5666 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5673 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5674 enum isl_dim_type type
, unsigned pos
, int value
)
5676 return (struct isl_set
*)
5677 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5680 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5681 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5685 map
= isl_map_cow(map
);
5689 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5690 for (i
= 0; i
< map
->n
; ++i
) {
5691 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5695 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5702 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5703 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5705 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5708 /* Fix the value of the variable at position "pos" of type "type" of "map"
5709 * to be equal to "v".
5711 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5712 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5716 map
= isl_map_cow(map
);
5720 if (!isl_val_is_int(v
))
5721 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5722 "expecting integer value", goto error
);
5723 if (pos
>= isl_map_dim(map
, type
))
5724 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5725 "index out of bounds", goto error
);
5726 for (i
= map
->n
- 1; i
>= 0; --i
) {
5727 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5729 if (remove_if_empty(map
, i
) < 0)
5732 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5741 /* Fix the value of the variable at position "pos" of type "type" of "set"
5742 * to be equal to "v".
5744 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5745 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5747 return isl_map_fix_val(set
, type
, pos
, v
);
5750 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5751 unsigned input
, int value
)
5753 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5756 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5758 return (struct isl_set
*)
5759 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5762 static __isl_give isl_basic_map
*basic_map_bound_si(
5763 __isl_take isl_basic_map
*bmap
,
5764 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5770 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5771 pos
+= isl_basic_map_offset(bmap
, type
);
5772 bmap
= isl_basic_map_cow(bmap
);
5773 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5774 j
= isl_basic_map_alloc_inequality(bmap
);
5777 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5779 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5780 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5782 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5783 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5785 bmap
= isl_basic_map_simplify(bmap
);
5786 return isl_basic_map_finalize(bmap
);
5788 isl_basic_map_free(bmap
);
5792 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5793 __isl_take isl_basic_map
*bmap
,
5794 enum isl_dim_type type
, unsigned pos
, int value
)
5796 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5799 /* Constrain the values of the given dimension to be no greater than "value".
5801 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5802 __isl_take isl_basic_map
*bmap
,
5803 enum isl_dim_type type
, unsigned pos
, int value
)
5805 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5808 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5809 unsigned dim
, isl_int value
)
5813 bset
= isl_basic_set_cow(bset
);
5814 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5815 j
= isl_basic_set_alloc_inequality(bset
);
5818 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5819 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5820 isl_int_neg(bset
->ineq
[j
][0], value
);
5821 bset
= isl_basic_set_simplify(bset
);
5822 return isl_basic_set_finalize(bset
);
5824 isl_basic_set_free(bset
);
5828 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5829 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5833 map
= isl_map_cow(map
);
5837 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5838 for (i
= 0; i
< map
->n
; ++i
) {
5839 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5840 type
, pos
, value
, upper
);
5844 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5851 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5852 enum isl_dim_type type
, unsigned pos
, int value
)
5854 return map_bound_si(map
, type
, pos
, value
, 0);
5857 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5858 enum isl_dim_type type
, unsigned pos
, int value
)
5860 return map_bound_si(map
, type
, pos
, value
, 1);
5863 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5864 enum isl_dim_type type
, unsigned pos
, int value
)
5866 return (struct isl_set
*)
5867 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5870 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5871 enum isl_dim_type type
, unsigned pos
, int value
)
5873 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5876 /* Bound the given variable of "bmap" from below (or above is "upper"
5877 * is set) to "value".
5879 static __isl_give isl_basic_map
*basic_map_bound(
5880 __isl_take isl_basic_map
*bmap
,
5881 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5887 if (pos
>= isl_basic_map_dim(bmap
, type
))
5888 isl_die(bmap
->ctx
, isl_error_invalid
,
5889 "index out of bounds", goto error
);
5890 pos
+= isl_basic_map_offset(bmap
, type
);
5891 bmap
= isl_basic_map_cow(bmap
);
5892 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5893 j
= isl_basic_map_alloc_inequality(bmap
);
5896 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5898 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5899 isl_int_set(bmap
->ineq
[j
][0], value
);
5901 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5902 isl_int_neg(bmap
->ineq
[j
][0], value
);
5904 bmap
= isl_basic_map_simplify(bmap
);
5905 return isl_basic_map_finalize(bmap
);
5907 isl_basic_map_free(bmap
);
5911 /* Bound the given variable of "map" from below (or above is "upper"
5912 * is set) to "value".
5914 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5915 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5919 map
= isl_map_cow(map
);
5923 if (pos
>= isl_map_dim(map
, type
))
5924 isl_die(map
->ctx
, isl_error_invalid
,
5925 "index out of bounds", goto error
);
5926 for (i
= map
->n
- 1; i
>= 0; --i
) {
5927 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5928 if (remove_if_empty(map
, i
) < 0)
5931 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5938 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5939 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5941 return map_bound(map
, type
, pos
, value
, 0);
5944 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5945 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5947 return map_bound(map
, type
, pos
, value
, 1);
5950 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5951 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5953 return isl_map_lower_bound(set
, type
, pos
, value
);
5956 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5957 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5959 return isl_map_upper_bound(set
, type
, pos
, value
);
5962 /* Force the values of the variable at position "pos" of type "type" of "set"
5963 * to be no smaller than "value".
5965 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5966 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5970 if (!isl_val_is_int(value
))
5971 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5972 "expecting integer value", goto error
);
5973 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5974 isl_val_free(value
);
5977 isl_val_free(value
);
5982 /* Force the values of the variable at position "pos" of type "type" of "set"
5983 * to be no greater than "value".
5985 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5986 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5990 if (!isl_val_is_int(value
))
5991 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5992 "expecting integer value", goto error
);
5993 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5994 isl_val_free(value
);
5997 isl_val_free(value
);
6002 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6007 set
= isl_set_cow(set
);
6011 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6012 for (i
= 0; i
< set
->n
; ++i
) {
6013 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6023 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6027 map
= isl_map_cow(map
);
6031 map
->dim
= isl_space_reverse(map
->dim
);
6034 for (i
= 0; i
< map
->n
; ++i
) {
6035 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6039 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6046 static struct isl_map
*isl_basic_map_partial_lexopt(
6047 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6048 struct isl_set
**empty
, int max
)
6050 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6053 struct isl_map
*isl_basic_map_partial_lexmax(
6054 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6055 struct isl_set
**empty
)
6057 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6060 struct isl_map
*isl_basic_map_partial_lexmin(
6061 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6062 struct isl_set
**empty
)
6064 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6067 struct isl_set
*isl_basic_set_partial_lexmin(
6068 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6069 struct isl_set
**empty
)
6071 return (struct isl_set
*)
6072 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6076 struct isl_set
*isl_basic_set_partial_lexmax(
6077 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6078 struct isl_set
**empty
)
6080 return (struct isl_set
*)
6081 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6085 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6086 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6087 __isl_give isl_set
**empty
)
6089 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6092 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6093 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6094 __isl_give isl_set
**empty
)
6096 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6099 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6100 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6101 __isl_give isl_set
**empty
)
6103 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6106 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6107 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6108 __isl_give isl_set
**empty
)
6110 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6113 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6114 __isl_take isl_basic_map
*bmap
, int max
)
6116 isl_basic_set
*dom
= NULL
;
6117 isl_space
*dom_space
;
6121 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6122 dom
= isl_basic_set_universe(dom_space
);
6123 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6125 isl_basic_map_free(bmap
);
6129 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6130 __isl_take isl_basic_map
*bmap
)
6132 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6136 #define TYPE isl_pw_multi_aff
6138 #define SUFFIX _pw_multi_aff
6140 #define EMPTY isl_pw_multi_aff_empty
6142 #define ADD isl_pw_multi_aff_union_add
6143 #include "isl_map_lexopt_templ.c"
6145 /* Given a map "map", compute the lexicographically minimal
6146 * (or maximal) image element for each domain element in dom,
6147 * in the form of an isl_pw_multi_aff.
6148 * Set *empty to those elements in dom that do not have an image element.
6150 * We first compute the lexicographically minimal or maximal element
6151 * in the first basic map. This results in a partial solution "res"
6152 * and a subset "todo" of dom that still need to be handled.
6153 * We then consider each of the remaining maps in "map" and successively
6154 * update both "res" and "todo".
6156 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6157 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6158 __isl_give isl_set
**empty
, int max
)
6161 isl_pw_multi_aff
*res
;
6167 if (isl_map_plain_is_empty(map
)) {
6172 return isl_pw_multi_aff_from_map(map
);
6175 res
= basic_map_partial_lexopt_pw_multi_aff(
6176 isl_basic_map_copy(map
->p
[0]),
6177 isl_set_copy(dom
), &todo
, max
);
6179 for (i
= 1; i
< map
->n
; ++i
) {
6180 isl_pw_multi_aff
*res_i
;
6183 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6184 isl_basic_map_copy(map
->p
[i
]),
6185 isl_set_copy(dom
), &todo_i
, max
);
6188 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6190 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6192 todo
= isl_set_intersect(todo
, todo_i
);
6213 #define TYPE isl_map
6217 #define EMPTY isl_map_empty
6219 #define ADD isl_map_union_disjoint
6220 #include "isl_map_lexopt_templ.c"
6222 /* Given a map "map", compute the lexicographically minimal
6223 * (or maximal) image element for each domain element in dom.
6224 * Set *empty to those elements in dom that do not have an image element.
6226 * We first compute the lexicographically minimal or maximal element
6227 * in the first basic map. This results in a partial solution "res"
6228 * and a subset "todo" of dom that still need to be handled.
6229 * We then consider each of the remaining maps in "map" and successively
6230 * update both "res" and "todo".
6232 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6233 * Assume we are computing the lexicographical maximum.
6234 * We first compute the lexicographically maximal element in basic map i.
6235 * This results in a partial solution res_i and a subset todo_i.
6236 * Then we combine these results with those obtain for the first k basic maps
6237 * to obtain a result that is valid for the first k+1 basic maps.
6238 * In particular, the set where there is no solution is the set where
6239 * there is no solution for the first k basic maps and also no solution
6240 * for the ith basic map, i.e.,
6242 * todo^i = todo^k * todo_i
6244 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6245 * solutions, arbitrarily breaking ties in favor of res^k.
6246 * That is, when res^k(a) >= res_i(a), we pick res^k and
6247 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6248 * the lexicographic order.)
6249 * In practice, we compute
6251 * res^k * (res_i . "<=")
6255 * res_i * (res^k . "<")
6257 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6258 * where only one of res^k and res_i provides a solution and we simply pick
6265 * Note that we only compute these intersections when dom(res^k) intersects
6266 * dom(res_i). Otherwise, the only effect of these intersections is to
6267 * potentially break up res^k and res_i into smaller pieces.
6268 * We want to avoid such splintering as much as possible.
6269 * In fact, an earlier implementation of this function would look for
6270 * better results in the domain of res^k and for extra results in todo^k,
6271 * but this would always result in a splintering according to todo^k,
6272 * even when the domain of basic map i is disjoint from the domains of
6273 * the previous basic maps.
6275 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6276 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6277 __isl_give isl_set
**empty
, int max
)
6280 struct isl_map
*res
;
6281 struct isl_set
*todo
;
6286 if (isl_map_plain_is_empty(map
)) {
6294 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6295 isl_set_copy(dom
), &todo
, max
);
6297 for (i
= 1; i
< map
->n
; ++i
) {
6301 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6303 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6304 isl_set_copy(dom
), &todo_i
, max
);
6307 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6308 le
= isl_map_lex_le(dim
);
6310 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6311 le
= isl_map_lex_ge(dim
);
6313 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6314 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6315 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6316 le
= isl_map_intersect(le
, isl_map_copy(res
));
6318 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6319 res
= isl_map_intersect_domain(res
,
6320 isl_set_copy(todo_i
));
6321 res_i
= isl_map_intersect_domain(res_i
,
6322 isl_set_copy(todo
));
6325 res
= isl_map_union_disjoint(res
, res_i
);
6326 res
= isl_map_union_disjoint(res
, lt
);
6327 res
= isl_map_union_disjoint(res
, le
);
6329 todo
= isl_set_intersect(todo
, todo_i
);
6349 __isl_give isl_map
*isl_map_partial_lexmax(
6350 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6351 __isl_give isl_set
**empty
)
6353 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6356 __isl_give isl_map
*isl_map_partial_lexmin(
6357 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6358 __isl_give isl_set
**empty
)
6360 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6363 __isl_give isl_set
*isl_set_partial_lexmin(
6364 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6365 __isl_give isl_set
**empty
)
6367 return (struct isl_set
*)
6368 isl_map_partial_lexmin((struct isl_map
*)set
,
6372 __isl_give isl_set
*isl_set_partial_lexmax(
6373 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6374 __isl_give isl_set
**empty
)
6376 return (struct isl_set
*)
6377 isl_map_partial_lexmax((struct isl_map
*)set
,
6381 /* Compute the lexicographic minimum (or maximum if "max" is set)
6382 * of "bmap" over its domain.
6384 * Since we are not interested in the part of the domain space where
6385 * there is no solution, we initialize the domain to those constraints
6386 * of "bmap" that only involve the parameters and the input dimensions.
6387 * This relieves the parametric programming engine from detecting those
6388 * inequalities and transferring them to the context. More importantly,
6389 * it ensures that those inequalities are transferred first and not
6390 * intermixed with inequalities that actually split the domain.
6392 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6396 isl_basic_map
*copy
;
6399 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6400 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6401 copy
= isl_basic_map_copy(bmap
);
6402 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6403 isl_dim_div
, 0, n_div
);
6404 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6405 isl_dim_out
, 0, n_out
);
6406 dom
= isl_basic_map_domain(copy
);
6407 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6410 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6412 return isl_basic_map_lexopt(bmap
, 0);
6415 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6417 return isl_basic_map_lexopt(bmap
, 1);
6420 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6422 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6425 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6427 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6430 /* Extract the first and only affine expression from list
6431 * and then add it to *pwaff with the given dom.
6432 * This domain is known to be disjoint from other domains
6433 * because of the way isl_basic_map_foreach_lexmax works.
6435 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6436 __isl_take isl_aff_list
*list
, void *user
)
6438 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6440 isl_pw_aff
**pwaff
= user
;
6441 isl_pw_aff
*pwaff_i
;
6445 if (isl_aff_list_n_aff(list
) != 1)
6446 isl_die(ctx
, isl_error_internal
,
6447 "expecting single element list", goto error
);
6449 aff
= isl_aff_list_get_aff(list
, 0);
6450 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6452 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6454 isl_aff_list_free(list
);
6458 isl_basic_set_free(dom
);
6459 isl_aff_list_free(list
);
6463 /* Given a basic map with one output dimension, compute the minimum or
6464 * maximum of that dimension as an isl_pw_aff.
6466 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6467 * call update_dim_opt on each leaf of the result.
6469 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6472 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6476 dim
= isl_space_from_domain(isl_space_domain(dim
));
6477 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6478 pwaff
= isl_pw_aff_empty(dim
);
6480 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6482 return isl_pw_aff_free(pwaff
);
6487 /* Compute the minimum or maximum of the given output dimension
6488 * as a function of the parameters and the input dimensions,
6489 * but independently of the other output dimensions.
6491 * We first project out the other output dimension and then compute
6492 * the "lexicographic" maximum in each basic map, combining the results
6493 * using isl_pw_aff_union_max.
6495 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6502 n_out
= isl_map_dim(map
, isl_dim_out
);
6503 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6504 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6509 isl_space
*dim
= isl_map_get_space(map
);
6511 return isl_pw_aff_empty(dim
);
6514 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6515 for (i
= 1; i
< map
->n
; ++i
) {
6516 isl_pw_aff
*pwaff_i
;
6518 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6519 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6527 /* Compute the maximum of the given output dimension as a function of the
6528 * parameters and input dimensions, but independently of
6529 * the other output dimensions.
6531 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6533 return map_dim_opt(map
, pos
, 1);
6536 /* Compute the minimum or maximum of the given set dimension
6537 * as a function of the parameters,
6538 * but independently of the other set dimensions.
6540 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6543 return map_dim_opt(set
, pos
, max
);
6546 /* Compute the maximum of the given set dimension as a function of the
6547 * parameters, but independently of the other set dimensions.
6549 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6551 return set_dim_opt(set
, pos
, 1);
6554 /* Compute the minimum of the given set dimension as a function of the
6555 * parameters, but independently of the other set dimensions.
6557 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6559 return set_dim_opt(set
, pos
, 0);
6562 /* Apply a preimage specified by "mat" on the parameters of "bset".
6563 * bset is assumed to have only parameters and divs.
6565 static struct isl_basic_set
*basic_set_parameter_preimage(
6566 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6573 bset
->dim
= isl_space_cow(bset
->dim
);
6577 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6579 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6581 bset
->dim
->nparam
= 0;
6582 bset
->dim
->n_out
= nparam
;
6583 bset
= isl_basic_set_preimage(bset
, mat
);
6585 bset
->dim
->nparam
= bset
->dim
->n_out
;
6586 bset
->dim
->n_out
= 0;
6591 isl_basic_set_free(bset
);
6595 /* Apply a preimage specified by "mat" on the parameters of "set".
6596 * set is assumed to have only parameters and divs.
6598 static struct isl_set
*set_parameter_preimage(
6599 struct isl_set
*set
, struct isl_mat
*mat
)
6601 isl_space
*dim
= NULL
;
6607 dim
= isl_space_copy(set
->dim
);
6608 dim
= isl_space_cow(dim
);
6612 nparam
= isl_set_dim(set
, isl_dim_param
);
6614 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6617 dim
->n_out
= nparam
;
6618 isl_set_reset_space(set
, dim
);
6619 set
= isl_set_preimage(set
, mat
);
6622 dim
= isl_space_copy(set
->dim
);
6623 dim
= isl_space_cow(dim
);
6626 dim
->nparam
= dim
->n_out
;
6628 isl_set_reset_space(set
, dim
);
6631 isl_space_free(dim
);
6638 /* Intersect the basic set "bset" with the affine space specified by the
6639 * equalities in "eq".
6641 static struct isl_basic_set
*basic_set_append_equalities(
6642 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6650 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6655 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6656 for (i
= 0; i
< eq
->n_row
; ++i
) {
6657 k
= isl_basic_set_alloc_equality(bset
);
6660 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6661 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6665 bset
= isl_basic_set_gauss(bset
, NULL
);
6666 bset
= isl_basic_set_finalize(bset
);
6671 isl_basic_set_free(bset
);
6675 /* Intersect the set "set" with the affine space specified by the
6676 * equalities in "eq".
6678 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6686 for (i
= 0; i
< set
->n
; ++i
) {
6687 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6700 /* Given a basic set "bset" that only involves parameters and existentially
6701 * quantified variables, return the index of the first equality
6702 * that only involves parameters. If there is no such equality then
6703 * return bset->n_eq.
6705 * This function assumes that isl_basic_set_gauss has been called on "bset".
6707 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6710 unsigned nparam
, n_div
;
6715 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6716 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6718 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6719 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6726 /* Compute an explicit representation for the existentially quantified
6727 * variables in "bset" by computing the "minimal value" of the set
6728 * variables. Since there are no set variables, the computation of
6729 * the minimal value essentially computes an explicit representation
6730 * of the non-empty part(s) of "bset".
6732 * The input only involves parameters and existentially quantified variables.
6733 * All equalities among parameters have been removed.
6735 * Since the existentially quantified variables in the result are in general
6736 * going to be different from those in the input, we first replace
6737 * them by the minimal number of variables based on their equalities.
6738 * This should simplify the parametric integer programming.
6740 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6742 isl_morph
*morph1
, *morph2
;
6748 if (bset
->n_eq
== 0)
6749 return isl_basic_set_lexmin(bset
);
6751 morph1
= isl_basic_set_parameter_compression(bset
);
6752 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6753 bset
= isl_basic_set_lift(bset
);
6754 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6755 bset
= isl_morph_basic_set(morph2
, bset
);
6756 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6757 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6759 set
= isl_basic_set_lexmin(bset
);
6761 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6766 /* Project the given basic set onto its parameter domain, possibly introducing
6767 * new, explicit, existential variables in the constraints.
6768 * The input has parameters and (possibly implicit) existential variables.
6769 * The output has the same parameters, but only
6770 * explicit existentially quantified variables.
6772 * The actual projection is performed by pip, but pip doesn't seem
6773 * to like equalities very much, so we first remove the equalities
6774 * among the parameters by performing a variable compression on
6775 * the parameters. Afterward, an inverse transformation is performed
6776 * and the equalities among the parameters are inserted back in.
6778 * The variable compression on the parameters may uncover additional
6779 * equalities that were only implicit before. We therefore check
6780 * if there are any new parameter equalities in the result and
6781 * if so recurse. The removal of parameter equalities is required
6782 * for the parameter compression performed by base_compute_divs.
6784 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6788 struct isl_mat
*T
, *T2
;
6789 struct isl_set
*set
;
6792 bset
= isl_basic_set_cow(bset
);
6796 if (bset
->n_eq
== 0)
6797 return base_compute_divs(bset
);
6799 bset
= isl_basic_set_gauss(bset
, NULL
);
6802 if (isl_basic_set_plain_is_empty(bset
))
6803 return isl_set_from_basic_set(bset
);
6805 i
= first_parameter_equality(bset
);
6806 if (i
== bset
->n_eq
)
6807 return base_compute_divs(bset
);
6809 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6810 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6812 eq
= isl_mat_cow(eq
);
6813 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6814 if (T
&& T
->n_col
== 0) {
6818 bset
= isl_basic_set_set_to_empty(bset
);
6819 return isl_set_from_basic_set(bset
);
6821 bset
= basic_set_parameter_preimage(bset
, T
);
6823 i
= first_parameter_equality(bset
);
6826 else if (i
== bset
->n_eq
)
6827 set
= base_compute_divs(bset
);
6829 set
= parameter_compute_divs(bset
);
6830 set
= set_parameter_preimage(set
, T2
);
6831 set
= set_append_equalities(set
, eq
);
6835 /* Insert the divs from "ls" before those of "bmap".
6837 * The number of columns is not changed, which means that the last
6838 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6839 * The caller is responsible for removing the same number of dimensions
6840 * from the space of "bmap".
6842 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6843 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6849 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6853 old_n_div
= bmap
->n_div
;
6854 bmap
= insert_div_rows(bmap
, n_div
);
6858 for (i
= 0; i
< n_div
; ++i
) {
6859 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6860 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6866 /* Replace the space of "bmap" by the space and divs of "ls".
6868 * If "ls" has any divs, then we simplify the result since we may
6869 * have discovered some additional equalities that could simplify
6870 * the div expressions.
6872 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6873 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6877 bmap
= isl_basic_map_cow(bmap
);
6881 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6882 bmap
= insert_divs_from_local_space(bmap
, ls
);
6886 isl_space_free(bmap
->dim
);
6887 bmap
->dim
= isl_local_space_get_space(ls
);
6891 isl_local_space_free(ls
);
6893 bmap
= isl_basic_map_simplify(bmap
);
6894 bmap
= isl_basic_map_finalize(bmap
);
6897 isl_basic_map_free(bmap
);
6898 isl_local_space_free(ls
);
6902 /* Replace the space of "map" by the space and divs of "ls".
6904 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6905 __isl_take isl_local_space
*ls
)
6909 map
= isl_map_cow(map
);
6913 for (i
= 0; i
< map
->n
; ++i
) {
6914 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6915 isl_local_space_copy(ls
));
6919 isl_space_free(map
->dim
);
6920 map
->dim
= isl_local_space_get_space(ls
);
6924 isl_local_space_free(ls
);
6927 isl_local_space_free(ls
);
6932 /* Compute an explicit representation for the existentially
6933 * quantified variables for which do not know any explicit representation yet.
6935 * We first sort the existentially quantified variables so that the
6936 * existentially quantified variables for which we already have an explicit
6937 * representation are placed before those for which we do not.
6938 * The input dimensions, the output dimensions and the existentially
6939 * quantified variables for which we already have an explicit
6940 * representation are then turned into parameters.
6941 * compute_divs returns a map with the same parameters and
6942 * no input or output dimensions and the dimension specification
6943 * is reset to that of the input, including the existentially quantified
6944 * variables for which we already had an explicit representation.
6946 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6948 struct isl_basic_set
*bset
;
6949 struct isl_set
*set
;
6950 struct isl_map
*map
;
6952 isl_local_space
*ls
;
6959 bmap
= isl_basic_map_sort_divs(bmap
);
6960 bmap
= isl_basic_map_cow(bmap
);
6964 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6965 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6968 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6969 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6970 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6971 dim
= isl_space_set_alloc(bmap
->ctx
,
6972 nparam
+ n_in
+ n_out
+ n_known
, 0);
6976 ls
= isl_basic_map_get_local_space(bmap
);
6977 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6978 n_known
, bmap
->n_div
- n_known
);
6980 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6981 swap_div(bmap
, i
- n_known
, i
);
6982 bmap
->n_div
-= n_known
;
6983 bmap
->extra
-= n_known
;
6985 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6986 bset
= (struct isl_basic_set
*)bmap
;
6988 set
= parameter_compute_divs(bset
);
6989 map
= (struct isl_map
*)set
;
6990 map
= replace_space_by_local_space(map
, ls
);
6994 isl_basic_map_free(bmap
);
6998 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7006 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7007 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7008 if (isl_int_is_zero(bmap
->div
[i
][0]))
7010 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
7016 static int map_divs_known(__isl_keep isl_map
*map
)
7023 for (i
= 0; i
< map
->n
; ++i
) {
7024 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7032 /* If bmap contains any unknown divs, then compute explicit
7033 * expressions for them. However, this computation may be
7034 * quite expensive, so first try to remove divs that aren't
7037 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7040 struct isl_map
*map
;
7042 known
= isl_basic_map_divs_known(bmap
);
7046 return isl_map_from_basic_map(bmap
);
7048 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7050 known
= isl_basic_map_divs_known(bmap
);
7054 return isl_map_from_basic_map(bmap
);
7056 map
= compute_divs(bmap
);
7059 isl_basic_map_free(bmap
);
7063 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7067 struct isl_map
*res
;
7074 known
= map_divs_known(map
);
7082 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7083 for (i
= 1 ; i
< map
->n
; ++i
) {
7085 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7086 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7087 res
= isl_map_union_disjoint(res
, r2
);
7089 res
= isl_map_union(res
, r2
);
7096 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7098 return (struct isl_set
*)
7099 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7102 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7104 return (struct isl_set
*)
7105 isl_map_compute_divs((struct isl_map
*)set
);
7108 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7111 struct isl_set
*set
;
7116 map
= isl_map_cow(map
);
7120 set
= (struct isl_set
*)map
;
7121 set
->dim
= isl_space_domain(set
->dim
);
7124 for (i
= 0; i
< map
->n
; ++i
) {
7125 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7129 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7130 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7137 /* Return the union of "map1" and "map2", where we assume for now that
7138 * "map1" and "map2" are disjoint. Note that the basic maps inside
7139 * "map1" or "map2" may not be disjoint from each other.
7140 * Also note that this function is also called from isl_map_union,
7141 * which takes care of handling the situation where "map1" and "map2"
7142 * may not be disjoint.
7144 * If one of the inputs is empty, we can simply return the other input.
7145 * Similarly, if one of the inputs is universal, then it is equal to the union.
7147 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7148 __isl_take isl_map
*map2
)
7152 struct isl_map
*map
= NULL
;
7158 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7159 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7160 "spaces don't match", goto error
);
7171 is_universe
= isl_map_plain_is_universe(map1
);
7172 if (is_universe
< 0)
7179 is_universe
= isl_map_plain_is_universe(map2
);
7180 if (is_universe
< 0)
7187 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7188 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7189 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7191 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7192 map1
->n
+ map2
->n
, flags
);
7195 for (i
= 0; i
< map1
->n
; ++i
) {
7196 map
= isl_map_add_basic_map(map
,
7197 isl_basic_map_copy(map1
->p
[i
]));
7201 for (i
= 0; i
< map2
->n
; ++i
) {
7202 map
= isl_map_add_basic_map(map
,
7203 isl_basic_map_copy(map2
->p
[i
]));
7217 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7218 * guaranteed to be disjoint by the caller.
7220 * Note that this functions is called from within isl_map_make_disjoint,
7221 * so we have to be careful not to touch the constraints of the inputs
7224 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7225 __isl_take isl_map
*map2
)
7227 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7230 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7231 * not be disjoint. The parameters are assumed to have been aligned.
7233 * We currently simply call map_union_disjoint, the internal operation
7234 * of which does not really depend on the inputs being disjoint.
7235 * If the result contains more than one basic map, then we clear
7236 * the disjoint flag since the result may contain basic maps from
7237 * both inputs and these are not guaranteed to be disjoint.
7239 * As a special case, if "map1" and "map2" are obviously equal,
7240 * then we simply return "map1".
7242 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7243 __isl_take isl_map
*map2
)
7250 equal
= isl_map_plain_is_equal(map1
, map2
);
7258 map1
= map_union_disjoint(map1
, map2
);
7262 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7270 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7273 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7274 __isl_take isl_map
*map2
)
7276 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7279 struct isl_set
*isl_set_union_disjoint(
7280 struct isl_set
*set1
, struct isl_set
*set2
)
7282 return (struct isl_set
*)
7283 isl_map_union_disjoint(
7284 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7287 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7289 return (struct isl_set
*)
7290 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7293 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7296 * "map" and "set" are assumed to be compatible and non-NULL.
7298 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7299 __isl_take isl_set
*set
,
7300 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7301 __isl_take isl_basic_set
*bset
))
7304 struct isl_map
*result
;
7307 if (isl_set_plain_is_universe(set
)) {
7312 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7313 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7314 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7316 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7317 map
->n
* set
->n
, flags
);
7318 for (i
= 0; result
&& i
< map
->n
; ++i
)
7319 for (j
= 0; j
< set
->n
; ++j
) {
7320 result
= isl_map_add_basic_map(result
,
7321 fn(isl_basic_map_copy(map
->p
[i
]),
7322 isl_basic_set_copy(set
->p
[j
])));
7332 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7333 __isl_take isl_set
*set
)
7338 if (!isl_map_compatible_range(map
, set
))
7339 isl_die(set
->ctx
, isl_error_invalid
,
7340 "incompatible spaces", goto error
);
7342 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7349 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7350 __isl_take isl_set
*set
)
7352 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7355 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7356 __isl_take isl_set
*set
)
7361 if (!isl_map_compatible_domain(map
, set
))
7362 isl_die(set
->ctx
, isl_error_invalid
,
7363 "incompatible spaces", goto error
);
7365 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7372 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7373 __isl_take isl_set
*set
)
7375 return isl_map_align_params_map_map_and(map
, set
,
7376 &map_intersect_domain
);
7379 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7380 __isl_take isl_map
*map2
)
7384 map1
= isl_map_reverse(map1
);
7385 map1
= isl_map_apply_range(map1
, map2
);
7386 return isl_map_reverse(map1
);
7393 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7394 __isl_take isl_map
*map2
)
7396 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7399 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7400 __isl_take isl_map
*map2
)
7402 isl_space
*dim_result
;
7403 struct isl_map
*result
;
7409 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7410 isl_space_copy(map2
->dim
));
7412 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7415 for (i
= 0; i
< map1
->n
; ++i
)
7416 for (j
= 0; j
< map2
->n
; ++j
) {
7417 result
= isl_map_add_basic_map(result
,
7418 isl_basic_map_apply_range(
7419 isl_basic_map_copy(map1
->p
[i
]),
7420 isl_basic_map_copy(map2
->p
[j
])));
7426 if (result
&& result
->n
<= 1)
7427 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7435 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7436 __isl_take isl_map
*map2
)
7438 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7442 * returns range - domain
7444 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7446 isl_space
*dims
, *target_dim
;
7447 struct isl_basic_set
*bset
;
7454 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7455 bmap
->dim
, isl_dim_out
),
7457 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7458 dim
= isl_basic_map_n_in(bmap
);
7459 nparam
= isl_basic_map_n_param(bmap
);
7460 bset
= isl_basic_set_from_basic_map(bmap
);
7461 bset
= isl_basic_set_cow(bset
);
7462 dims
= isl_basic_set_get_space(bset
);
7463 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7464 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7465 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7466 for (i
= 0; i
< dim
; ++i
) {
7467 int j
= isl_basic_map_alloc_equality(
7468 (struct isl_basic_map
*)bset
);
7470 bset
= isl_basic_set_free(bset
);
7473 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7474 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7475 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7476 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7478 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7479 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7482 isl_basic_map_free(bmap
);
7487 * returns range - domain
7489 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7493 struct isl_set
*result
;
7498 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7499 map
->dim
, isl_dim_out
),
7501 dim
= isl_map_get_space(map
);
7502 dim
= isl_space_domain(dim
);
7503 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7506 for (i
= 0; i
< map
->n
; ++i
)
7507 result
= isl_set_add_basic_set(result
,
7508 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7517 * returns [domain -> range] -> range - domain
7519 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7520 __isl_take isl_basic_map
*bmap
)
7524 isl_basic_map
*domain
;
7528 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7529 isl_die(bmap
->ctx
, isl_error_invalid
,
7530 "domain and range don't match", goto error
);
7532 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7533 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7535 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7536 domain
= isl_basic_map_universe(dim
);
7538 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7539 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7540 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7542 total
= isl_basic_map_total_dim(bmap
);
7544 for (i
= 0; i
< n
; ++i
) {
7545 k
= isl_basic_map_alloc_equality(bmap
);
7548 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7549 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7550 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7551 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7554 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7555 return isl_basic_map_finalize(bmap
);
7557 isl_basic_map_free(bmap
);
7562 * returns [domain -> range] -> range - domain
7564 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7567 isl_space
*domain_dim
;
7572 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7573 isl_die(map
->ctx
, isl_error_invalid
,
7574 "domain and range don't match", goto error
);
7576 map
= isl_map_cow(map
);
7580 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7581 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7582 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7585 for (i
= 0; i
< map
->n
; ++i
) {
7586 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7590 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7597 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7599 struct isl_basic_map
*bmap
;
7607 nparam
= dims
->nparam
;
7609 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7613 for (i
= 0; i
< dim
; ++i
) {
7614 int j
= isl_basic_map_alloc_equality(bmap
);
7617 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7618 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7619 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7621 return isl_basic_map_finalize(bmap
);
7623 isl_basic_map_free(bmap
);
7627 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7631 if (dim
->n_in
!= dim
->n_out
)
7632 isl_die(dim
->ctx
, isl_error_invalid
,
7633 "number of input and output dimensions needs to be "
7634 "the same", goto error
);
7635 return basic_map_identity(dim
);
7637 isl_space_free(dim
);
7641 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7643 if (!model
|| !model
->dim
)
7645 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7648 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7650 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7653 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7655 if (!model
|| !model
->dim
)
7657 return isl_map_identity(isl_space_copy(model
->dim
));
7660 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7662 if (!model
|| !model
->dim
)
7664 return isl_map_identity(isl_space_copy(model
->dim
));
7667 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7669 isl_space
*dim
= isl_set_get_space(set
);
7671 id
= isl_map_identity(isl_space_map_from_set(dim
));
7672 return isl_map_intersect_range(id
, set
);
7675 /* Construct a basic set with all set dimensions having only non-negative
7678 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7679 __isl_take isl_space
*space
)
7684 struct isl_basic_set
*bset
;
7688 nparam
= space
->nparam
;
7690 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7693 for (i
= 0; i
< dim
; ++i
) {
7694 int k
= isl_basic_set_alloc_inequality(bset
);
7697 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7698 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7702 isl_basic_set_free(bset
);
7706 /* Construct the half-space x_pos >= 0.
7708 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7712 isl_basic_set
*nonneg
;
7714 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7715 k
= isl_basic_set_alloc_inequality(nonneg
);
7718 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7719 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7721 return isl_basic_set_finalize(nonneg
);
7723 isl_basic_set_free(nonneg
);
7727 /* Construct the half-space x_pos <= -1.
7729 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7734 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7735 k
= isl_basic_set_alloc_inequality(neg
);
7738 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7739 isl_int_set_si(neg
->ineq
[k
][0], -1);
7740 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7742 return isl_basic_set_finalize(neg
);
7744 isl_basic_set_free(neg
);
7748 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7749 enum isl_dim_type type
, unsigned first
, unsigned n
)
7752 isl_basic_set
*nonneg
;
7760 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7762 for (i
= 0; i
< n
; ++i
) {
7763 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7764 pos(set
->dim
, type
) + first
+ i
);
7765 neg
= neg_halfspace(isl_set_get_space(set
),
7766 pos(set
->dim
, type
) + first
+ i
);
7768 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7777 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7778 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7785 if (isl_set_plain_is_empty(set
)) {
7790 return fn(set
, signs
, user
);
7793 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7795 half
= isl_set_intersect(half
, isl_set_copy(set
));
7796 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7800 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7802 half
= isl_set_intersect(half
, set
);
7803 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7809 /* Call "fn" on the intersections of "set" with each of the orthants
7810 * (except for obviously empty intersections). The orthant is identified
7811 * by the signs array, with each entry having value 1 or -1 according
7812 * to the sign of the corresponding variable.
7814 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7815 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7825 if (isl_set_plain_is_empty(set
))
7828 nparam
= isl_set_dim(set
, isl_dim_param
);
7829 nvar
= isl_set_dim(set
, isl_dim_set
);
7831 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7833 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7841 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7843 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7846 int isl_basic_map_is_subset(
7847 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7850 struct isl_map
*map1
;
7851 struct isl_map
*map2
;
7853 if (!bmap1
|| !bmap2
)
7856 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7857 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7859 is_subset
= isl_map_is_subset(map1
, map2
);
7867 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7868 __isl_keep isl_basic_set
*bset2
)
7870 return isl_basic_map_is_subset(bset1
, bset2
);
7873 int isl_basic_map_is_equal(
7874 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7878 if (!bmap1
|| !bmap2
)
7880 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7883 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7887 int isl_basic_set_is_equal(
7888 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7890 return isl_basic_map_is_equal(
7891 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7894 int isl_map_is_empty(struct isl_map
*map
)
7901 for (i
= 0; i
< map
->n
; ++i
) {
7902 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7911 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7913 return map
? map
->n
== 0 : -1;
7916 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7918 return isl_map_plain_is_empty(map
);
7921 int isl_set_plain_is_empty(struct isl_set
*set
)
7923 return set
? set
->n
== 0 : -1;
7926 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7928 return isl_set_plain_is_empty(set
);
7931 int isl_set_is_empty(struct isl_set
*set
)
7933 return isl_map_is_empty((struct isl_map
*)set
);
7936 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7941 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7944 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7949 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7952 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7958 is_subset
= isl_map_is_subset(map1
, map2
);
7961 is_subset
= isl_map_is_subset(map2
, map1
);
7965 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7967 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7970 int isl_basic_map_is_strict_subset(
7971 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7975 if (!bmap1
|| !bmap2
)
7977 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7980 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7981 if (is_subset
== -1)
7986 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7992 is_subset
= isl_map_is_subset(map1
, map2
);
7995 is_subset
= isl_map_is_subset(map2
, map1
);
7996 if (is_subset
== -1)
8001 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8003 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
8006 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
8010 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8013 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
8017 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
8020 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8027 for (i
= 0; i
< map
->n
; ++i
) {
8028 int r
= isl_basic_map_is_universe(map
->p
[i
]);
8036 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8038 return isl_map_plain_is_universe((isl_map
*) set
);
8041 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
8043 return isl_set_plain_is_universe(set
);
8046 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
8048 struct isl_basic_set
*bset
= NULL
;
8049 struct isl_vec
*sample
= NULL
;
8056 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8059 if (isl_basic_map_is_universe(bmap
))
8062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8063 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8064 copy
= isl_basic_map_remove_redundancies(copy
);
8065 empty
= isl_basic_map_plain_is_empty(copy
);
8066 isl_basic_map_free(copy
);
8070 total
= 1 + isl_basic_map_total_dim(bmap
);
8071 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
8072 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
8078 isl_vec_free(bmap
->sample
);
8079 bmap
->sample
= NULL
;
8080 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8083 sample
= isl_basic_set_sample_vec(bset
);
8086 empty
= sample
->size
== 0;
8087 isl_vec_free(bmap
->sample
);
8088 bmap
->sample
= sample
;
8090 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8095 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8099 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8102 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
8104 return isl_basic_map_plain_is_empty(bmap
);
8107 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8111 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8114 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8116 return isl_basic_set_plain_is_empty(bset
);
8119 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8121 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8124 struct isl_map
*isl_basic_map_union(
8125 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8127 struct isl_map
*map
;
8128 if (!bmap1
|| !bmap2
)
8131 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8133 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8136 map
= isl_map_add_basic_map(map
, bmap1
);
8137 map
= isl_map_add_basic_map(map
, bmap2
);
8140 isl_basic_map_free(bmap1
);
8141 isl_basic_map_free(bmap2
);
8145 struct isl_set
*isl_basic_set_union(
8146 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8148 return (struct isl_set
*)isl_basic_map_union(
8149 (struct isl_basic_map
*)bset1
,
8150 (struct isl_basic_map
*)bset2
);
8153 /* Order divs such that any div only depends on previous divs */
8154 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8162 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8164 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8166 if (isl_int_is_zero(bmap
->div
[i
][0]))
8168 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8172 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8178 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8180 return (struct isl_basic_set
*)
8181 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8184 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8191 for (i
= 0; i
< map
->n
; ++i
) {
8192 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8203 /* Apply the expansion computed by isl_merge_divs.
8204 * The expansion itself is given by "exp" while the resulting
8205 * list of divs is given by "div".
8207 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8208 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8213 bset
= isl_basic_set_cow(bset
);
8217 if (div
->n_row
< bset
->n_div
)
8218 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8219 "not an expansion", goto error
);
8221 n_div
= bset
->n_div
;
8222 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8223 div
->n_row
- n_div
, 0,
8224 2 * (div
->n_row
- n_div
));
8226 for (i
= n_div
; i
< div
->n_row
; ++i
)
8227 if (isl_basic_set_alloc_div(bset
) < 0)
8231 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8232 if (j
>= 0 && exp
[j
] == i
) {
8234 isl_basic_map_swap_div(bset
, i
, j
);
8237 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8238 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8246 isl_basic_set_free(bset
);
8251 /* Look for a div in dst that corresponds to the div "div" in src.
8252 * The divs before "div" in src and dst are assumed to be the same.
8254 * Returns -1 if no corresponding div was found and the position
8255 * of the corresponding div in dst otherwise.
8257 static int find_div(struct isl_basic_map
*dst
,
8258 struct isl_basic_map
*src
, unsigned div
)
8262 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8264 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8265 for (i
= div
; i
< dst
->n_div
; ++i
)
8266 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8267 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8268 dst
->n_div
- div
) == -1)
8273 struct isl_basic_map
*isl_basic_map_align_divs(
8274 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
8282 if (src
->n_div
== 0)
8285 for (i
= 0; i
< src
->n_div
; ++i
)
8286 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
8288 src
= isl_basic_map_order_divs(src
);
8289 dst
= isl_basic_map_cow(dst
);
8292 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
8293 src
->n_div
, 0, 2 * src
->n_div
);
8296 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8297 for (i
= 0; i
< src
->n_div
; ++i
) {
8298 int j
= find_div(dst
, src
, i
);
8300 j
= isl_basic_map_alloc_div(dst
);
8303 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8304 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8305 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8309 isl_basic_map_swap_div(dst
, i
, j
);
8313 isl_basic_map_free(dst
);
8317 struct isl_basic_set
*isl_basic_set_align_divs(
8318 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8320 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8321 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8324 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8332 map
= isl_map_compute_divs(map
);
8333 map
= isl_map_cow(map
);
8337 for (i
= 1; i
< map
->n
; ++i
)
8338 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8339 for (i
= 1; i
< map
->n
; ++i
) {
8340 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8342 return isl_map_free(map
);
8345 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8349 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8351 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8354 /* Align the divs of the basic sets in "set" to those
8355 * of the basic sets in "list", as well as to the other basic sets in "set".
8356 * The elements in "list" are assumed to have known divs.
8358 __isl_give isl_set
*isl_set_align_divs_to_basic_set_list(
8359 __isl_take isl_set
*set
, __isl_keep isl_basic_set_list
*list
)
8363 set
= isl_set_compute_divs(set
);
8364 set
= isl_set_cow(set
);
8366 return isl_set_free(set
);
8370 n
= isl_basic_set_list_n_basic_set(list
);
8371 for (i
= 0; i
< n
; ++i
) {
8372 isl_basic_set
*bset
;
8374 bset
= isl_basic_set_list_get_basic_set(list
, i
);
8375 set
->p
[0] = isl_basic_set_align_divs(set
->p
[0], bset
);
8376 isl_basic_set_free(bset
);
8379 return isl_set_free(set
);
8381 return isl_set_align_divs(set
);
8384 /* Align the divs of each element of "list" to those of "bset".
8385 * Both "bset" and the elements of "list" are assumed to have known divs.
8387 __isl_give isl_basic_set_list
*isl_basic_set_list_align_divs_to_basic_set(
8388 __isl_take isl_basic_set_list
*list
, __isl_keep isl_basic_set
*bset
)
8393 return isl_basic_set_list_free(list
);
8395 n
= isl_basic_set_list_n_basic_set(list
);
8396 for (i
= 0; i
< n
; ++i
) {
8397 isl_basic_set
*bset_i
;
8399 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
8400 bset_i
= isl_basic_set_align_divs(bset_i
, bset
);
8401 list
= isl_basic_set_list_set_basic_set(list
, i
, bset_i
);
8407 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8408 __isl_take isl_map
*map
)
8412 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8413 map
= isl_map_intersect_domain(map
, set
);
8414 set
= isl_map_range(map
);
8422 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8423 __isl_take isl_map
*map
)
8425 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8428 /* There is no need to cow as removing empty parts doesn't change
8429 * the meaning of the set.
8431 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8438 for (i
= map
->n
- 1; i
>= 0; --i
)
8439 remove_if_empty(map
, i
);
8444 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8446 return (struct isl_set
*)
8447 isl_map_remove_empty_parts((struct isl_map
*)set
);
8450 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8452 struct isl_basic_map
*bmap
;
8453 if (!map
|| map
->n
== 0)
8455 bmap
= map
->p
[map
->n
-1];
8456 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8457 return isl_basic_map_copy(bmap
);
8460 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8462 return (struct isl_basic_set
*)
8463 isl_map_copy_basic_map((struct isl_map
*)set
);
8466 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8467 __isl_keep isl_basic_map
*bmap
)
8473 for (i
= map
->n
-1; i
>= 0; --i
) {
8474 if (map
->p
[i
] != bmap
)
8476 map
= isl_map_cow(map
);
8479 isl_basic_map_free(map
->p
[i
]);
8480 if (i
!= map
->n
-1) {
8481 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8482 map
->p
[i
] = map
->p
[map
->n
-1];
8493 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8494 struct isl_basic_set
*bset
)
8496 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8497 (struct isl_basic_map
*)bset
);
8500 /* Given two basic sets bset1 and bset2, compute the maximal difference
8501 * between the values of dimension pos in bset1 and those in bset2
8502 * for any common value of the parameters and dimensions preceding pos.
8504 static enum isl_lp_result
basic_set_maximal_difference_at(
8505 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8506 int pos
, isl_int
*opt
)
8509 struct isl_basic_map
*bmap1
= NULL
;
8510 struct isl_basic_map
*bmap2
= NULL
;
8511 struct isl_ctx
*ctx
;
8512 struct isl_vec
*obj
;
8515 unsigned dim1
, dim2
;
8516 enum isl_lp_result res
;
8518 if (!bset1
|| !bset2
)
8519 return isl_lp_error
;
8521 nparam
= isl_basic_set_n_param(bset1
);
8522 dim1
= isl_basic_set_n_dim(bset1
);
8523 dim2
= isl_basic_set_n_dim(bset2
);
8524 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8525 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8526 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8527 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8528 if (!bmap1
|| !bmap2
)
8530 bmap1
= isl_basic_map_cow(bmap1
);
8531 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8532 pos
, (dim1
- pos
) + (dim2
- pos
),
8533 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8534 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8537 total
= isl_basic_map_total_dim(bmap1
);
8539 obj
= isl_vec_alloc(ctx
, 1 + total
);
8542 isl_seq_clr(obj
->block
.data
, 1 + total
);
8543 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8544 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8545 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8547 isl_basic_map_free(bmap1
);
8551 isl_basic_map_free(bmap2
);
8553 isl_basic_map_free(bmap1
);
8554 return isl_lp_error
;
8557 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8558 * for any common value of the parameters and dimensions preceding pos
8559 * in both basic sets, the values of dimension pos in bset1 are
8560 * smaller or larger than those in bset2.
8563 * 1 if bset1 follows bset2
8564 * -1 if bset1 precedes bset2
8565 * 0 if bset1 and bset2 are incomparable
8566 * -2 if some error occurred.
8568 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8569 struct isl_basic_set
*bset2
, int pos
)
8572 enum isl_lp_result res
;
8577 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8579 if (res
== isl_lp_empty
)
8581 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8582 res
== isl_lp_unbounded
)
8584 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8593 /* Given two basic sets bset1 and bset2, check whether
8594 * for any common value of the parameters and dimensions preceding pos
8595 * there is a value of dimension pos in bset1 that is larger
8596 * than a value of the same dimension in bset2.
8599 * 1 if there exists such a pair
8600 * 0 if there is no such pair, but there is a pair of equal values
8602 * -2 if some error occurred.
8604 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8605 __isl_keep isl_basic_set
*bset2
, int pos
)
8608 enum isl_lp_result res
;
8613 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8615 if (res
== isl_lp_empty
)
8617 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8618 res
== isl_lp_unbounded
)
8620 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8622 else if (res
== isl_lp_ok
)
8631 /* Given two sets set1 and set2, check whether
8632 * for any common value of the parameters and dimensions preceding pos
8633 * there is a value of dimension pos in set1 that is larger
8634 * than a value of the same dimension in set2.
8637 * 1 if there exists such a pair
8638 * 0 if there is no such pair, but there is a pair of equal values
8640 * -2 if some error occurred.
8642 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8643 __isl_keep isl_set
*set2
, int pos
)
8651 for (i
= 0; i
< set1
->n
; ++i
)
8652 for (j
= 0; j
< set2
->n
; ++j
) {
8654 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8655 if (f
== 1 || f
== -2)
8664 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8665 unsigned pos
, isl_int
*val
)
8673 total
= isl_basic_map_total_dim(bmap
);
8674 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8675 for (; d
+1 > pos
; --d
)
8676 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8680 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8682 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8684 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8687 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8693 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8694 unsigned pos
, isl_int
*val
)
8706 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8709 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8710 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8711 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8712 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8716 isl_int_set(*val
, v
);
8722 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8723 unsigned pos
, isl_int
*val
)
8725 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8729 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8732 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8735 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8736 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8738 if (pos
>= isl_basic_map_dim(bmap
, type
))
8740 return isl_basic_map_plain_has_fixed_var(bmap
,
8741 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8744 /* If "bmap" obviously lies on a hyperplane where the given dimension
8745 * has a fixed value, then return that value.
8746 * Otherwise return NaN.
8748 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8749 __isl_keep isl_basic_map
*bmap
,
8750 enum isl_dim_type type
, unsigned pos
)
8758 ctx
= isl_basic_map_get_ctx(bmap
);
8759 v
= isl_val_alloc(ctx
);
8762 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8764 return isl_val_free(v
);
8766 isl_int_set_si(v
->d
, 1);
8770 return isl_val_nan(ctx
);
8773 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8774 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8776 if (pos
>= isl_map_dim(map
, type
))
8778 return isl_map_plain_has_fixed_var(map
,
8779 map_offset(map
, type
) - 1 + pos
, val
);
8782 /* If "map" obviously lies on a hyperplane where the given dimension
8783 * has a fixed value, then return that value.
8784 * Otherwise return NaN.
8786 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8787 enum isl_dim_type type
, unsigned pos
)
8795 ctx
= isl_map_get_ctx(map
);
8796 v
= isl_val_alloc(ctx
);
8799 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8801 return isl_val_free(v
);
8803 isl_int_set_si(v
->d
, 1);
8807 return isl_val_nan(ctx
);
8810 /* If "set" obviously lies on a hyperplane where the given dimension
8811 * has a fixed value, then return that value.
8812 * Otherwise return NaN.
8814 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8815 enum isl_dim_type type
, unsigned pos
)
8817 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8820 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8821 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8823 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8826 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8827 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8829 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8832 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8833 * then return this fixed value in *val.
8835 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8836 unsigned dim
, isl_int
*val
)
8838 return isl_basic_set_plain_has_fixed_var(bset
,
8839 isl_basic_set_n_param(bset
) + dim
, val
);
8842 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8843 * then return this fixed value in *val.
8845 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8846 unsigned dim
, isl_int
*val
)
8848 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8851 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8852 unsigned dim
, isl_int
*val
)
8854 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8857 /* Check if input variable in has fixed value and if so and if val is not NULL,
8858 * then return this fixed value in *val.
8860 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8861 unsigned in
, isl_int
*val
)
8863 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8866 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8867 * and if val is not NULL, then return this lower bound in *val.
8869 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8870 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8872 int i
, i_eq
= -1, i_ineq
= -1;
8879 total
= isl_basic_set_total_dim(bset
);
8880 nparam
= isl_basic_set_n_param(bset
);
8881 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8882 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8888 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8889 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8891 if (i_eq
!= -1 || i_ineq
!= -1)
8895 if (i_eq
== -1 && i_ineq
== -1)
8897 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8898 /* The coefficient should always be one due to normalization. */
8899 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8901 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8903 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8904 total
- nparam
- dim
- 1) != -1)
8907 isl_int_neg(*val
, c
[0]);
8911 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8912 unsigned dim
, isl_int
*val
)
8924 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8928 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8930 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8931 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8933 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8937 isl_int_set(*val
, v
);
8948 /* uset_gist depends on constraints without existentially quantified
8949 * variables sorting first.
8951 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8953 const struct constraint
*c1
= (const struct constraint
*)p1
;
8954 const struct constraint
*c2
= (const struct constraint
*)p2
;
8956 unsigned size
= isl_min(c1
->size
, c2
->size
);
8958 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8959 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8964 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8967 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8968 struct isl_basic_map
*bmap
)
8971 struct constraint
*c
;
8976 if (bmap
->n_ineq
== 0)
8978 total
= isl_basic_map_total_dim(bmap
);
8979 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8982 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8984 c
[i
].c
= bmap
->ineq
[i
];
8986 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8987 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8988 bmap
->ineq
[i
] = c
[i
].c
;
8992 isl_basic_map_free(bmap
);
8996 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8997 __isl_take isl_basic_set
*bset
)
8999 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9000 (struct isl_basic_map
*)bset
);
9003 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9007 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9009 bmap
= isl_basic_map_remove_redundancies(bmap
);
9010 bmap
= isl_basic_map_sort_constraints(bmap
);
9012 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9016 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9018 return (struct isl_basic_set
*)isl_basic_map_normalize(
9019 (struct isl_basic_map
*)bset
);
9022 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9023 const __isl_keep isl_basic_map
*bmap2
)
9030 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9031 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9032 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9033 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9034 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9035 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9036 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9037 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9038 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9039 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9040 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9042 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9044 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9046 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9047 return bmap1
->n_eq
- bmap2
->n_eq
;
9048 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9049 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9050 if (bmap1
->n_div
!= bmap2
->n_div
)
9051 return bmap1
->n_div
- bmap2
->n_div
;
9052 total
= isl_basic_map_total_dim(bmap1
);
9053 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9054 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9058 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9059 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9063 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9064 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9071 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9072 const __isl_keep isl_basic_set
*bset2
)
9074 return isl_basic_map_plain_cmp(bset1
, bset2
);
9077 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9083 if (set1
->n
!= set2
->n
)
9084 return set1
->n
- set2
->n
;
9086 for (i
= 0; i
< set1
->n
; ++i
) {
9087 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9095 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9096 __isl_keep isl_basic_map
*bmap2
)
9098 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9101 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9102 __isl_keep isl_basic_set
*bset2
)
9104 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9105 (isl_basic_map
*)bset2
);
9108 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9110 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9111 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9113 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9116 /* We normalize in place, but if anything goes wrong we need
9117 * to return NULL, so we need to make sure we don't change the
9118 * meaning of any possible other copies of map.
9120 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
9123 struct isl_basic_map
*bmap
;
9127 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9129 for (i
= 0; i
< map
->n
; ++i
) {
9130 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9133 isl_basic_map_free(map
->p
[i
]);
9136 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9137 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9138 map
= isl_map_remove_empty_parts(map
);
9141 for (i
= map
->n
- 1; i
>= 1; --i
) {
9142 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
9144 isl_basic_map_free(map
->p
[i
-1]);
9145 for (j
= i
; j
< map
->n
; ++j
)
9146 map
->p
[j
-1] = map
->p
[j
];
9155 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9157 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9160 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9170 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9173 map1
= isl_map_copy(map1
);
9174 map2
= isl_map_copy(map2
);
9175 map1
= isl_map_normalize(map1
);
9176 map2
= isl_map_normalize(map2
);
9179 equal
= map1
->n
== map2
->n
;
9180 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9181 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9194 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9196 return isl_map_plain_is_equal(map1
, map2
);
9199 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9201 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9202 (struct isl_map
*)set2
);
9205 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9207 return isl_set_plain_is_equal(set1
, set2
);
9210 /* Return an interval that ranges from min to max (inclusive)
9212 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9213 isl_int min
, isl_int max
)
9216 struct isl_basic_set
*bset
= NULL
;
9218 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9222 k
= isl_basic_set_alloc_inequality(bset
);
9225 isl_int_set_si(bset
->ineq
[k
][1], 1);
9226 isl_int_neg(bset
->ineq
[k
][0], min
);
9228 k
= isl_basic_set_alloc_inequality(bset
);
9231 isl_int_set_si(bset
->ineq
[k
][1], -1);
9232 isl_int_set(bset
->ineq
[k
][0], max
);
9236 isl_basic_set_free(bset
);
9240 /* Return the basic sets in "set" as a list.
9242 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9243 __isl_keep isl_set
*set
)
9247 isl_basic_set_list
*list
;
9251 ctx
= isl_set_get_ctx(set
);
9252 list
= isl_basic_set_list_alloc(ctx
, set
->n
);
9254 for (i
= 0; i
< set
->n
; ++i
) {
9255 isl_basic_set
*bset
;
9257 bset
= isl_basic_set_copy(set
->p
[i
]);
9258 list
= isl_basic_set_list_add(list
, bset
);
9264 /* Return the intersection of the elements in the non-empty list "list".
9265 * All elements are assumed to live in the same space.
9267 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9268 __isl_take
struct isl_basic_set_list
*list
)
9271 isl_basic_set
*bset
;
9275 n
= isl_basic_set_list_n_basic_set(list
);
9277 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9278 "expecting non-empty list", goto error
);
9280 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9281 for (i
= 1; i
< n
; ++i
) {
9282 isl_basic_set
*bset_i
;
9284 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
9285 bset
= isl_basic_set_intersect(bset
, bset_i
);
9288 isl_basic_set_list_free(list
);
9291 isl_basic_set_list_free(list
);
9295 /* Return the Cartesian product of the basic sets in list (in the given order).
9297 __isl_give isl_basic_set
*isl_basic_set_list_product(
9298 __isl_take
struct isl_basic_set_list
*list
)
9306 struct isl_basic_set
*product
= NULL
;
9310 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9311 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9312 nparam
= isl_basic_set_n_param(list
->p
[0]);
9313 dim
= isl_basic_set_n_dim(list
->p
[0]);
9314 extra
= list
->p
[0]->n_div
;
9315 n_eq
= list
->p
[0]->n_eq
;
9316 n_ineq
= list
->p
[0]->n_ineq
;
9317 for (i
= 1; i
< list
->n
; ++i
) {
9318 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9319 isl_assert(list
->ctx
,
9320 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9321 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9322 extra
+= list
->p
[i
]->n_div
;
9323 n_eq
+= list
->p
[i
]->n_eq
;
9324 n_ineq
+= list
->p
[i
]->n_ineq
;
9326 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9331 for (i
= 0; i
< list
->n
; ++i
) {
9332 isl_basic_set_add_constraints(product
,
9333 isl_basic_set_copy(list
->p
[i
]), dim
);
9334 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9336 isl_basic_set_list_free(list
);
9339 isl_basic_set_free(product
);
9340 isl_basic_set_list_free(list
);
9344 struct isl_basic_map
*isl_basic_map_product(
9345 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9347 isl_space
*dim_result
= NULL
;
9348 struct isl_basic_map
*bmap
;
9349 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9350 struct isl_dim_map
*dim_map1
, *dim_map2
;
9352 if (!bmap1
|| !bmap2
)
9355 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9356 bmap2
->dim
, isl_dim_param
), goto error
);
9357 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9358 isl_space_copy(bmap2
->dim
));
9360 in1
= isl_basic_map_n_in(bmap1
);
9361 in2
= isl_basic_map_n_in(bmap2
);
9362 out1
= isl_basic_map_n_out(bmap1
);
9363 out2
= isl_basic_map_n_out(bmap2
);
9364 nparam
= isl_basic_map_n_param(bmap1
);
9366 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9367 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9368 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9369 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9370 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9371 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9372 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9373 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9374 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9375 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9376 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9378 bmap
= isl_basic_map_alloc_space(dim_result
,
9379 bmap1
->n_div
+ bmap2
->n_div
,
9380 bmap1
->n_eq
+ bmap2
->n_eq
,
9381 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9382 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9383 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9384 bmap
= isl_basic_map_simplify(bmap
);
9385 return isl_basic_map_finalize(bmap
);
9387 isl_basic_map_free(bmap1
);
9388 isl_basic_map_free(bmap2
);
9392 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9393 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9395 isl_basic_map
*prod
;
9397 prod
= isl_basic_map_product(bmap1
, bmap2
);
9398 prod
= isl_basic_map_flatten(prod
);
9402 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9403 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9405 return isl_basic_map_flat_range_product(bset1
, bset2
);
9408 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9409 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9411 isl_space
*space_result
= NULL
;
9412 isl_basic_map
*bmap
;
9413 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9414 struct isl_dim_map
*dim_map1
, *dim_map2
;
9416 if (!bmap1
|| !bmap2
)
9419 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9420 isl_space_copy(bmap2
->dim
));
9422 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9423 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9424 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9425 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9427 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9428 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9429 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9430 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9431 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9432 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9433 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9434 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9435 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9436 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9437 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9439 bmap
= isl_basic_map_alloc_space(space_result
,
9440 bmap1
->n_div
+ bmap2
->n_div
,
9441 bmap1
->n_eq
+ bmap2
->n_eq
,
9442 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9443 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9444 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9445 bmap
= isl_basic_map_simplify(bmap
);
9446 return isl_basic_map_finalize(bmap
);
9448 isl_basic_map_free(bmap1
);
9449 isl_basic_map_free(bmap2
);
9453 __isl_give isl_basic_map
*isl_basic_map_range_product(
9454 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9456 isl_space
*dim_result
= NULL
;
9457 isl_basic_map
*bmap
;
9458 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9459 struct isl_dim_map
*dim_map1
, *dim_map2
;
9461 if (!bmap1
|| !bmap2
)
9464 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9465 bmap2
->dim
, isl_dim_param
))
9466 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9467 "parameters don't match", goto error
);
9469 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9470 isl_space_copy(bmap2
->dim
));
9472 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9473 out1
= isl_basic_map_n_out(bmap1
);
9474 out2
= isl_basic_map_n_out(bmap2
);
9475 nparam
= isl_basic_map_n_param(bmap1
);
9477 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9478 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9479 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9480 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9481 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9482 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9483 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9484 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9485 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9486 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9487 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9489 bmap
= isl_basic_map_alloc_space(dim_result
,
9490 bmap1
->n_div
+ bmap2
->n_div
,
9491 bmap1
->n_eq
+ bmap2
->n_eq
,
9492 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9493 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9494 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9495 bmap
= isl_basic_map_simplify(bmap
);
9496 return isl_basic_map_finalize(bmap
);
9498 isl_basic_map_free(bmap1
);
9499 isl_basic_map_free(bmap2
);
9503 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9504 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9506 isl_basic_map
*prod
;
9508 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9509 prod
= isl_basic_map_flatten_range(prod
);
9513 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9514 __isl_take isl_map
*map2
,
9515 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
9516 __isl_take isl_space
*right
),
9517 __isl_give isl_basic_map
*(*basic_map_product
)(
9518 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
9521 struct isl_map
*result
;
9527 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9528 map2
->dim
, isl_dim_param
), goto error
);
9530 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9531 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9532 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9534 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
9535 isl_space_copy(map2
->dim
)),
9536 map1
->n
* map2
->n
, flags
);
9539 for (i
= 0; i
< map1
->n
; ++i
)
9540 for (j
= 0; j
< map2
->n
; ++j
) {
9541 struct isl_basic_map
*part
;
9542 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9543 isl_basic_map_copy(map2
->p
[j
]));
9544 if (isl_basic_map_is_empty(part
))
9545 isl_basic_map_free(part
);
9547 result
= isl_map_add_basic_map(result
, part
);
9560 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9562 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9563 __isl_take isl_map
*map2
)
9565 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9568 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9569 __isl_take isl_map
*map2
)
9571 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9574 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9576 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9577 __isl_take isl_map
*map2
)
9581 prod
= isl_map_product(map1
, map2
);
9582 prod
= isl_map_flatten(prod
);
9586 /* Given two set A and B, construct its Cartesian product A x B.
9588 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9590 return isl_map_range_product(set1
, set2
);
9593 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9594 __isl_take isl_set
*set2
)
9596 return isl_map_flat_range_product(set1
, set2
);
9599 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9601 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9602 __isl_take isl_map
*map2
)
9604 return map_product(map1
, map2
, &isl_space_domain_product
,
9605 &isl_basic_map_domain_product
);
9608 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9610 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9611 __isl_take isl_map
*map2
)
9613 return map_product(map1
, map2
, &isl_space_range_product
,
9614 &isl_basic_map_range_product
);
9617 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9618 __isl_take isl_map
*map2
)
9620 return isl_map_align_params_map_map_and(map1
, map2
,
9621 &map_domain_product_aligned
);
9624 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9625 __isl_take isl_map
*map2
)
9627 return isl_map_align_params_map_map_and(map1
, map2
,
9628 &map_range_product_aligned
);
9631 /* Given a map A -> [B -> C], extract the map A -> B.
9633 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9640 if (!isl_space_range_is_wrapping(map
->dim
))
9641 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9642 "range is not a product", return isl_map_free(map
));
9644 space
= isl_map_get_space(map
);
9645 total
= isl_space_dim(space
, isl_dim_out
);
9646 space
= isl_space_range_factor_domain(space
);
9647 keep
= isl_space_dim(space
, isl_dim_out
);
9648 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9649 map
= isl_map_reset_space(map
, space
);
9654 /* Given a map A -> [B -> C], extract the map A -> C.
9656 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9663 if (!isl_space_range_is_wrapping(map
->dim
))
9664 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9665 "range is not a product", return isl_map_free(map
));
9667 space
= isl_map_get_space(map
);
9668 total
= isl_space_dim(space
, isl_dim_out
);
9669 space
= isl_space_range_factor_range(space
);
9670 keep
= isl_space_dim(space
, isl_dim_out
);
9671 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9672 map
= isl_map_reset_space(map
, space
);
9677 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9679 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9680 __isl_take isl_map
*map2
)
9684 prod
= isl_map_domain_product(map1
, map2
);
9685 prod
= isl_map_flatten_domain(prod
);
9689 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9691 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9692 __isl_take isl_map
*map2
)
9696 prod
= isl_map_range_product(map1
, map2
);
9697 prod
= isl_map_flatten_range(prod
);
9701 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9704 uint32_t hash
= isl_hash_init();
9709 bmap
= isl_basic_map_copy(bmap
);
9710 bmap
= isl_basic_map_normalize(bmap
);
9713 total
= isl_basic_map_total_dim(bmap
);
9714 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9715 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9717 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9718 isl_hash_hash(hash
, c_hash
);
9720 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9721 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9723 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9724 isl_hash_hash(hash
, c_hash
);
9726 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9727 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9729 if (isl_int_is_zero(bmap
->div
[i
][0]))
9731 isl_hash_byte(hash
, i
& 0xFF);
9732 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9733 isl_hash_hash(hash
, c_hash
);
9735 isl_basic_map_free(bmap
);
9739 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9741 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9744 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9751 map
= isl_map_copy(map
);
9752 map
= isl_map_normalize(map
);
9756 hash
= isl_hash_init();
9757 for (i
= 0; i
< map
->n
; ++i
) {
9759 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9760 isl_hash_hash(hash
, bmap_hash
);
9768 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9770 return isl_map_get_hash((isl_map
*)set
);
9773 /* Check if the value for dimension dim is completely determined
9774 * by the values of the other parameters and variables.
9775 * That is, check if dimension dim is involved in an equality.
9777 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9784 nparam
= isl_basic_set_n_param(bset
);
9785 for (i
= 0; i
< bset
->n_eq
; ++i
)
9786 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9791 /* Check if the value for dimension dim is completely determined
9792 * by the values of the other parameters and variables.
9793 * That is, check if dimension dim is involved in an equality
9794 * for each of the subsets.
9796 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9802 for (i
= 0; i
< set
->n
; ++i
) {
9804 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9811 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9813 return set
? set
->n
: 0;
9816 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9817 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9824 for (i
= 0; i
< map
->n
; ++i
)
9825 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9831 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9832 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9839 for (i
= 0; i
< set
->n
; ++i
)
9840 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9846 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9853 bset
= isl_basic_set_cow(bset
);
9857 dim
= isl_basic_set_get_space(bset
);
9858 dim
= isl_space_lift(dim
, bset
->n_div
);
9861 isl_space_free(bset
->dim
);
9863 bset
->extra
-= bset
->n_div
;
9866 bset
= isl_basic_set_finalize(bset
);
9870 isl_basic_set_free(bset
);
9874 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9880 set
= isl_set_align_divs(set
);
9885 set
= isl_set_cow(set
);
9889 n_div
= set
->p
[0]->n_div
;
9890 dim
= isl_set_get_space(set
);
9891 dim
= isl_space_lift(dim
, n_div
);
9894 isl_space_free(set
->dim
);
9897 for (i
= 0; i
< set
->n
; ++i
) {
9898 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9909 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9912 struct isl_basic_map
*bmap
;
9919 set
= isl_set_align_divs(set
);
9924 dim
= isl_set_get_space(set
);
9925 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9927 return isl_map_identity(isl_space_map_from_set(dim
));
9930 n_div
= set
->p
[0]->n_div
;
9931 dim
= isl_space_map_from_set(dim
);
9932 n_param
= isl_space_dim(dim
, isl_dim_param
);
9933 n_set
= isl_space_dim(dim
, isl_dim_in
);
9934 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9935 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9936 for (i
= 0; i
< n_set
; ++i
)
9937 bmap
= var_equal(bmap
, i
);
9939 total
= n_param
+ n_set
+ n_set
+ n_div
;
9940 for (i
= 0; i
< n_div
; ++i
) {
9941 k
= isl_basic_map_alloc_inequality(bmap
);
9944 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9945 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9946 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9947 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9948 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9949 set
->p
[0]->div
[i
][0]);
9951 l
= isl_basic_map_alloc_inequality(bmap
);
9954 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9955 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9956 set
->p
[0]->div
[i
][0]);
9957 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9961 bmap
= isl_basic_map_simplify(bmap
);
9962 bmap
= isl_basic_map_finalize(bmap
);
9963 return isl_map_from_basic_map(bmap
);
9966 isl_basic_map_free(bmap
);
9970 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9978 dim
= isl_basic_set_total_dim(bset
);
9979 size
+= bset
->n_eq
* (1 + dim
);
9980 size
+= bset
->n_ineq
* (1 + dim
);
9981 size
+= bset
->n_div
* (2 + dim
);
9986 int isl_set_size(__isl_keep isl_set
*set
)
9994 for (i
= 0; i
< set
->n
; ++i
)
9995 size
+= isl_basic_set_size(set
->p
[i
]);
10000 /* Check if there is any lower bound (if lower == 0) and/or upper
10001 * bound (if upper == 0) on the specified dim.
10003 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10004 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10011 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
10013 pos
+= isl_basic_map_offset(bmap
, type
);
10015 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10016 if (isl_int_is_zero(bmap
->div
[i
][0]))
10018 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10022 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10023 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10026 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10027 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10034 return lower
&& upper
;
10037 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10038 enum isl_dim_type type
, unsigned pos
)
10040 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10043 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10044 enum isl_dim_type type
, unsigned pos
)
10046 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10049 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10050 enum isl_dim_type type
, unsigned pos
)
10052 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10055 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10056 enum isl_dim_type type
, unsigned pos
)
10063 for (i
= 0; i
< map
->n
; ++i
) {
10065 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10066 if (bounded
< 0 || !bounded
)
10073 /* Return 1 if the specified dim is involved in both an upper bound
10074 * and a lower bound.
10076 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10077 enum isl_dim_type type
, unsigned pos
)
10079 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10082 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10084 static int has_any_bound(__isl_keep isl_map
*map
,
10085 enum isl_dim_type type
, unsigned pos
,
10086 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10087 enum isl_dim_type type
, unsigned pos
))
10094 for (i
= 0; i
< map
->n
; ++i
) {
10096 bounded
= fn(map
->p
[i
], type
, pos
);
10097 if (bounded
< 0 || bounded
)
10104 /* Return 1 if the specified dim is involved in any lower bound.
10106 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10107 enum isl_dim_type type
, unsigned pos
)
10109 return has_any_bound(set
, type
, pos
,
10110 &isl_basic_map_dim_has_lower_bound
);
10113 /* Return 1 if the specified dim is involved in any upper bound.
10115 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10116 enum isl_dim_type type
, unsigned pos
)
10118 return has_any_bound(set
, type
, pos
,
10119 &isl_basic_map_dim_has_upper_bound
);
10122 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10124 static int has_bound(__isl_keep isl_map
*map
,
10125 enum isl_dim_type type
, unsigned pos
,
10126 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10127 enum isl_dim_type type
, unsigned pos
))
10134 for (i
= 0; i
< map
->n
; ++i
) {
10136 bounded
= fn(map
->p
[i
], type
, pos
);
10137 if (bounded
< 0 || !bounded
)
10144 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10146 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10147 enum isl_dim_type type
, unsigned pos
)
10149 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10152 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10154 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10155 enum isl_dim_type type
, unsigned pos
)
10157 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10160 /* For each of the "n" variables starting at "first", determine
10161 * the sign of the variable and put the results in the first "n"
10162 * elements of the array "signs".
10164 * 1 means that the variable is non-negative
10165 * -1 means that the variable is non-positive
10166 * 0 means the variable attains both positive and negative values.
10168 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10169 unsigned first
, unsigned n
, int *signs
)
10171 isl_vec
*bound
= NULL
;
10172 struct isl_tab
*tab
= NULL
;
10173 struct isl_tab_undo
*snap
;
10176 if (!bset
|| !signs
)
10179 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10180 tab
= isl_tab_from_basic_set(bset
, 0);
10181 if (!bound
|| !tab
)
10184 isl_seq_clr(bound
->el
, bound
->size
);
10185 isl_int_set_si(bound
->el
[0], -1);
10187 snap
= isl_tab_snap(tab
);
10188 for (i
= 0; i
< n
; ++i
) {
10191 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10192 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10194 empty
= tab
->empty
;
10195 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10196 if (isl_tab_rollback(tab
, snap
) < 0)
10204 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10205 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10207 empty
= tab
->empty
;
10208 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10209 if (isl_tab_rollback(tab
, snap
) < 0)
10212 signs
[i
] = empty
? -1 : 0;
10216 isl_vec_free(bound
);
10220 isl_vec_free(bound
);
10224 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10225 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10227 if (!bset
|| !signs
)
10229 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10232 first
+= pos(bset
->dim
, type
) - 1;
10233 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10236 /* Check if the given basic map is obviously single-valued.
10237 * In particular, for each output dimension, check that there is
10238 * an equality that defines the output dimension in terms of
10239 * earlier dimensions.
10241 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10251 total
= 1 + isl_basic_map_total_dim(bmap
);
10252 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10253 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10255 for (i
= 0; i
< n_out
; ++i
) {
10256 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10257 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
10259 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
10260 total
- (o_out
+ i
+ 1)) == -1)
10263 if (j
>= bmap
->n_eq
)
10270 /* Check if the given basic map is single-valued.
10271 * We simply compute
10275 * and check if the result is a subset of the identity mapping.
10277 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10280 isl_basic_map
*test
;
10284 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10288 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10289 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10291 space
= isl_basic_map_get_space(bmap
);
10292 space
= isl_space_map_from_set(isl_space_range(space
));
10293 id
= isl_basic_map_identity(space
);
10295 sv
= isl_basic_map_is_subset(test
, id
);
10297 isl_basic_map_free(test
);
10298 isl_basic_map_free(id
);
10303 /* Check if the given map is obviously single-valued.
10305 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10314 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10317 /* Check if the given map is single-valued.
10318 * We simply compute
10322 * and check if the result is a subset of the identity mapping.
10324 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10331 sv
= isl_map_plain_is_single_valued(map
);
10335 test
= isl_map_reverse(isl_map_copy(map
));
10336 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10338 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10339 id
= isl_map_identity(dim
);
10341 sv
= isl_map_is_subset(test
, id
);
10343 isl_map_free(test
);
10349 int isl_map_is_injective(__isl_keep isl_map
*map
)
10353 map
= isl_map_copy(map
);
10354 map
= isl_map_reverse(map
);
10355 in
= isl_map_is_single_valued(map
);
10361 /* Check if the given map is obviously injective.
10363 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10367 map
= isl_map_copy(map
);
10368 map
= isl_map_reverse(map
);
10369 in
= isl_map_plain_is_single_valued(map
);
10375 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10379 sv
= isl_map_is_single_valued(map
);
10383 return isl_map_is_injective(map
);
10386 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10388 return isl_map_is_single_valued((isl_map
*)set
);
10391 int isl_map_is_translation(__isl_keep isl_map
*map
)
10396 delta
= isl_map_deltas(isl_map_copy(map
));
10397 ok
= isl_set_is_singleton(delta
);
10398 isl_set_free(delta
);
10403 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10405 if (isl_seq_first_non_zero(p
, pos
) != -1)
10407 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10412 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10421 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10424 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10425 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10426 for (j
= 0; j
< nvar
; ++j
) {
10427 int lower
= 0, upper
= 0;
10428 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10429 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10431 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10435 if (i
< bset
->n_eq
)
10437 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10438 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10440 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10442 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10447 if (!lower
|| !upper
)
10454 int isl_set_is_box(__isl_keep isl_set
*set
)
10461 return isl_basic_set_is_box(set
->p
[0]);
10464 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10469 return isl_space_is_wrapping(bset
->dim
);
10472 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10477 return isl_space_is_wrapping(set
->dim
);
10480 /* Is the domain of "map" a wrapped relation?
10482 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10487 return isl_space_domain_is_wrapping(map
->dim
);
10490 /* Is the range of "map" a wrapped relation?
10492 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10497 return isl_space_range_is_wrapping(map
->dim
);
10500 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10502 bmap
= isl_basic_map_cow(bmap
);
10506 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10510 bmap
= isl_basic_map_finalize(bmap
);
10512 return (isl_basic_set
*)bmap
;
10514 isl_basic_map_free(bmap
);
10518 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10522 map
= isl_map_cow(map
);
10526 for (i
= 0; i
< map
->n
; ++i
) {
10527 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10531 map
->dim
= isl_space_wrap(map
->dim
);
10535 return (isl_set
*)map
;
10541 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10543 bset
= isl_basic_set_cow(bset
);
10547 bset
->dim
= isl_space_unwrap(bset
->dim
);
10551 bset
= isl_basic_set_finalize(bset
);
10553 return (isl_basic_map
*)bset
;
10555 isl_basic_set_free(bset
);
10559 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10566 if (!isl_set_is_wrapping(set
))
10567 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10570 set
= isl_set_cow(set
);
10574 for (i
= 0; i
< set
->n
; ++i
) {
10575 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10580 set
->dim
= isl_space_unwrap(set
->dim
);
10584 return (isl_map
*)set
;
10590 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10591 enum isl_dim_type type
)
10596 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10599 bmap
= isl_basic_map_cow(bmap
);
10603 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10607 bmap
= isl_basic_map_finalize(bmap
);
10611 isl_basic_map_free(bmap
);
10615 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10616 enum isl_dim_type type
)
10623 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10626 map
= isl_map_cow(map
);
10630 for (i
= 0; i
< map
->n
; ++i
) {
10631 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10635 map
->dim
= isl_space_reset(map
->dim
, type
);
10645 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10650 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10653 bmap
= isl_basic_map_cow(bmap
);
10657 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10661 bmap
= isl_basic_map_finalize(bmap
);
10665 isl_basic_map_free(bmap
);
10669 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10671 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10674 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10675 __isl_take isl_basic_map
*bmap
)
10680 if (!bmap
->dim
->nested
[0])
10683 bmap
= isl_basic_map_cow(bmap
);
10687 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10691 bmap
= isl_basic_map_finalize(bmap
);
10695 isl_basic_map_free(bmap
);
10699 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10700 __isl_take isl_basic_map
*bmap
)
10705 if (!bmap
->dim
->nested
[1])
10708 bmap
= isl_basic_map_cow(bmap
);
10712 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10716 bmap
= isl_basic_map_finalize(bmap
);
10720 isl_basic_map_free(bmap
);
10724 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10731 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10734 map
= isl_map_cow(map
);
10738 for (i
= 0; i
< map
->n
; ++i
) {
10739 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10743 map
->dim
= isl_space_flatten(map
->dim
);
10753 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10755 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10758 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10760 isl_space
*dim
, *flat_dim
;
10763 dim
= isl_set_get_space(set
);
10764 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10765 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10766 map
= isl_map_intersect_domain(map
, set
);
10771 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10778 if (!map
->dim
->nested
[0])
10781 map
= isl_map_cow(map
);
10785 for (i
= 0; i
< map
->n
; ++i
) {
10786 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10790 map
->dim
= isl_space_flatten_domain(map
->dim
);
10800 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10807 if (!map
->dim
->nested
[1])
10810 map
= isl_map_cow(map
);
10814 for (i
= 0; i
< map
->n
; ++i
) {
10815 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10819 map
->dim
= isl_space_flatten_range(map
->dim
);
10829 /* Reorder the dimensions of "bmap" according to the given dim_map
10830 * and set the dimension specification to "dim".
10832 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10833 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10835 isl_basic_map
*res
;
10838 bmap
= isl_basic_map_cow(bmap
);
10839 if (!bmap
|| !dim
|| !dim_map
)
10842 flags
= bmap
->flags
;
10843 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10844 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10845 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10846 res
= isl_basic_map_alloc_space(dim
,
10847 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10848 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10850 res
->flags
= flags
;
10851 res
= isl_basic_map_finalize(res
);
10855 isl_basic_map_free(bmap
);
10856 isl_space_free(dim
);
10860 /* Reorder the dimensions of "map" according to given reordering.
10862 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10863 __isl_take isl_reordering
*r
)
10866 struct isl_dim_map
*dim_map
;
10868 map
= isl_map_cow(map
);
10869 dim_map
= isl_dim_map_from_reordering(r
);
10870 if (!map
|| !r
|| !dim_map
)
10873 for (i
= 0; i
< map
->n
; ++i
) {
10874 struct isl_dim_map
*dim_map_i
;
10876 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10878 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10879 isl_space_copy(r
->dim
), dim_map_i
);
10885 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10887 isl_reordering_free(r
);
10893 isl_reordering_free(r
);
10897 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10898 __isl_take isl_reordering
*r
)
10900 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10903 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10904 __isl_take isl_space
*model
)
10908 if (!map
|| !model
)
10911 ctx
= isl_space_get_ctx(model
);
10912 if (!isl_space_has_named_params(model
))
10913 isl_die(ctx
, isl_error_invalid
,
10914 "model has unnamed parameters", goto error
);
10915 if (!isl_space_has_named_params(map
->dim
))
10916 isl_die(ctx
, isl_error_invalid
,
10917 "relation has unnamed parameters", goto error
);
10918 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10919 isl_reordering
*exp
;
10921 model
= isl_space_drop_dims(model
, isl_dim_in
,
10922 0, isl_space_dim(model
, isl_dim_in
));
10923 model
= isl_space_drop_dims(model
, isl_dim_out
,
10924 0, isl_space_dim(model
, isl_dim_out
));
10925 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10926 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10927 map
= isl_map_realign(map
, exp
);
10930 isl_space_free(model
);
10933 isl_space_free(model
);
10938 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10939 __isl_take isl_space
*model
)
10941 return isl_map_align_params(set
, model
);
10944 /* Align the parameters of "bmap" to those of "model", introducing
10945 * additional parameters if needed.
10947 __isl_give isl_basic_map
*isl_basic_map_align_params(
10948 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10952 if (!bmap
|| !model
)
10955 ctx
= isl_space_get_ctx(model
);
10956 if (!isl_space_has_named_params(model
))
10957 isl_die(ctx
, isl_error_invalid
,
10958 "model has unnamed parameters", goto error
);
10959 if (!isl_space_has_named_params(bmap
->dim
))
10960 isl_die(ctx
, isl_error_invalid
,
10961 "relation has unnamed parameters", goto error
);
10962 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10963 isl_reordering
*exp
;
10964 struct isl_dim_map
*dim_map
;
10966 model
= isl_space_drop_dims(model
, isl_dim_in
,
10967 0, isl_space_dim(model
, isl_dim_in
));
10968 model
= isl_space_drop_dims(model
, isl_dim_out
,
10969 0, isl_space_dim(model
, isl_dim_out
));
10970 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10971 exp
= isl_reordering_extend_space(exp
,
10972 isl_basic_map_get_space(bmap
));
10973 dim_map
= isl_dim_map_from_reordering(exp
);
10974 bmap
= isl_basic_map_realign(bmap
,
10975 exp
? isl_space_copy(exp
->dim
) : NULL
,
10976 isl_dim_map_extend(dim_map
, bmap
));
10977 isl_reordering_free(exp
);
10981 isl_space_free(model
);
10984 isl_space_free(model
);
10985 isl_basic_map_free(bmap
);
10989 /* Align the parameters of "bset" to those of "model", introducing
10990 * additional parameters if needed.
10992 __isl_give isl_basic_set
*isl_basic_set_align_params(
10993 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10995 return isl_basic_map_align_params(bset
, model
);
10998 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10999 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11000 enum isl_dim_type c2
, enum isl_dim_type c3
,
11001 enum isl_dim_type c4
, enum isl_dim_type c5
)
11003 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11004 struct isl_mat
*mat
;
11010 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11011 isl_basic_map_total_dim(bmap
) + 1);
11014 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11015 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11016 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11017 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11018 isl_int_set(mat
->row
[i
][pos
],
11019 bmap
->eq
[i
][off
+ k
]);
11027 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11028 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11029 enum isl_dim_type c2
, enum isl_dim_type c3
,
11030 enum isl_dim_type c4
, enum isl_dim_type c5
)
11032 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11033 struct isl_mat
*mat
;
11039 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11040 isl_basic_map_total_dim(bmap
) + 1);
11043 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11044 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11045 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11046 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11047 isl_int_set(mat
->row
[i
][pos
],
11048 bmap
->ineq
[i
][off
+ k
]);
11056 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11057 __isl_take isl_space
*dim
,
11058 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11059 enum isl_dim_type c2
, enum isl_dim_type c3
,
11060 enum isl_dim_type c4
, enum isl_dim_type c5
)
11062 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11063 isl_basic_map
*bmap
;
11069 if (!dim
|| !eq
|| !ineq
)
11072 if (eq
->n_col
!= ineq
->n_col
)
11073 isl_die(dim
->ctx
, isl_error_invalid
,
11074 "equalities and inequalities matrices should have "
11075 "same number of columns", goto error
);
11077 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11079 if (eq
->n_col
< total
)
11080 isl_die(dim
->ctx
, isl_error_invalid
,
11081 "number of columns too small", goto error
);
11083 extra
= eq
->n_col
- total
;
11085 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11086 eq
->n_row
, ineq
->n_row
);
11089 for (i
= 0; i
< extra
; ++i
) {
11090 k
= isl_basic_map_alloc_div(bmap
);
11093 isl_int_set_si(bmap
->div
[k
][0], 0);
11095 for (i
= 0; i
< eq
->n_row
; ++i
) {
11096 l
= isl_basic_map_alloc_equality(bmap
);
11099 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11100 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11101 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11102 isl_int_set(bmap
->eq
[l
][off
+ k
],
11108 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11109 l
= isl_basic_map_alloc_inequality(bmap
);
11112 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11113 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11114 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11115 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11116 ineq
->row
[i
][pos
]);
11122 isl_space_free(dim
);
11124 isl_mat_free(ineq
);
11126 bmap
= isl_basic_map_simplify(bmap
);
11127 return isl_basic_map_finalize(bmap
);
11129 isl_space_free(dim
);
11131 isl_mat_free(ineq
);
11135 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11136 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11137 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11139 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11140 c1
, c2
, c3
, c4
, isl_dim_in
);
11143 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11144 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11145 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11147 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11148 c1
, c2
, c3
, c4
, isl_dim_in
);
11151 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11152 __isl_take isl_space
*dim
,
11153 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11154 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11156 return (isl_basic_set
*)
11157 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11158 c1
, c2
, c3
, c4
, isl_dim_in
);
11161 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11166 return isl_space_can_zip(bmap
->dim
);
11169 int isl_map_can_zip(__isl_keep isl_map
*map
)
11174 return isl_space_can_zip(map
->dim
);
11177 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11178 * (A -> C) -> (B -> D).
11180 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11189 if (!isl_basic_map_can_zip(bmap
))
11190 isl_die(bmap
->ctx
, isl_error_invalid
,
11191 "basic map cannot be zipped", goto error
);
11192 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11193 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11194 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11195 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11196 bmap
= isl_basic_map_cow(bmap
);
11197 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11200 bmap
->dim
= isl_space_zip(bmap
->dim
);
11205 isl_basic_map_free(bmap
);
11209 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11210 * (A -> C) -> (B -> D).
11212 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11219 if (!isl_map_can_zip(map
))
11220 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11223 map
= isl_map_cow(map
);
11227 for (i
= 0; i
< map
->n
; ++i
) {
11228 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11233 map
->dim
= isl_space_zip(map
->dim
);
11243 /* Can we apply isl_basic_map_curry to "bmap"?
11244 * That is, does it have a nested relation in its domain?
11246 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11251 return isl_space_can_curry(bmap
->dim
);
11254 /* Can we apply isl_map_curry to "map"?
11255 * That is, does it have a nested relation in its domain?
11257 int isl_map_can_curry(__isl_keep isl_map
*map
)
11262 return isl_space_can_curry(map
->dim
);
11265 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11268 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11274 if (!isl_basic_map_can_curry(bmap
))
11275 isl_die(bmap
->ctx
, isl_error_invalid
,
11276 "basic map cannot be curried", goto error
);
11277 bmap
= isl_basic_map_cow(bmap
);
11280 bmap
->dim
= isl_space_curry(bmap
->dim
);
11285 isl_basic_map_free(bmap
);
11289 /* Given a map (A -> B) -> C, return the corresponding map
11292 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11299 if (!isl_map_can_curry(map
))
11300 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11303 map
= isl_map_cow(map
);
11307 for (i
= 0; i
< map
->n
; ++i
) {
11308 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11313 map
->dim
= isl_space_curry(map
->dim
);
11323 /* Can we apply isl_basic_map_uncurry to "bmap"?
11324 * That is, does it have a nested relation in its domain?
11326 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11331 return isl_space_can_uncurry(bmap
->dim
);
11334 /* Can we apply isl_map_uncurry to "map"?
11335 * That is, does it have a nested relation in its domain?
11337 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11342 return isl_space_can_uncurry(map
->dim
);
11345 /* Given a basic map A -> (B -> C), return the corresponding basic map
11348 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11354 if (!isl_basic_map_can_uncurry(bmap
))
11355 isl_die(bmap
->ctx
, isl_error_invalid
,
11356 "basic map cannot be uncurried",
11357 return isl_basic_map_free(bmap
));
11358 bmap
= isl_basic_map_cow(bmap
);
11361 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11363 return isl_basic_map_free(bmap
);
11367 /* Given a map A -> (B -> C), return the corresponding map
11370 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11377 if (!isl_map_can_uncurry(map
))
11378 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11379 return isl_map_free(map
));
11381 map
= isl_map_cow(map
);
11385 for (i
= 0; i
< map
->n
; ++i
) {
11386 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11388 return isl_map_free(map
);
11391 map
->dim
= isl_space_uncurry(map
->dim
);
11393 return isl_map_free(map
);
11398 /* Construct a basic map mapping the domain of the affine expression
11399 * to a one-dimensional range prescribed by the affine expression.
11401 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11405 isl_local_space
*ls
;
11406 isl_basic_map
*bmap
;
11411 ls
= isl_aff_get_local_space(aff
);
11412 bmap
= isl_basic_map_from_local_space(ls
);
11413 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11414 k
= isl_basic_map_alloc_equality(bmap
);
11418 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11419 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11420 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11421 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11422 aff
->v
->size
- (pos
+ 1));
11425 bmap
= isl_basic_map_finalize(bmap
);
11429 isl_basic_map_free(bmap
);
11433 /* Construct a map mapping the domain of the affine expression
11434 * to a one-dimensional range prescribed by the affine expression.
11436 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11438 isl_basic_map
*bmap
;
11440 bmap
= isl_basic_map_from_aff(aff
);
11441 return isl_map_from_basic_map(bmap
);
11444 /* Construct a basic map mapping the domain the multi-affine expression
11445 * to its range, with each dimension in the range equated to the
11446 * corresponding affine expression.
11448 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11449 __isl_take isl_multi_aff
*maff
)
11453 isl_basic_map
*bmap
;
11458 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11459 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11460 "invalid space", goto error
);
11462 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11463 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11465 for (i
= 0; i
< maff
->n
; ++i
) {
11467 isl_basic_map
*bmap_i
;
11469 aff
= isl_aff_copy(maff
->p
[i
]);
11470 bmap_i
= isl_basic_map_from_aff(aff
);
11472 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11475 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11477 isl_multi_aff_free(maff
);
11480 isl_multi_aff_free(maff
);
11484 /* Construct a map mapping the domain the multi-affine expression
11485 * to its range, with each dimension in the range equated to the
11486 * corresponding affine expression.
11488 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11490 isl_basic_map
*bmap
;
11492 bmap
= isl_basic_map_from_multi_aff(maff
);
11493 return isl_map_from_basic_map(bmap
);
11496 /* Construct a basic map mapping a domain in the given space to
11497 * to an n-dimensional range, with n the number of elements in the list,
11498 * where each coordinate in the range is prescribed by the
11499 * corresponding affine expression.
11500 * The domains of all affine expressions in the list are assumed to match
11503 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11504 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11508 isl_basic_map
*bmap
;
11513 dim
= isl_space_from_domain(domain_dim
);
11514 bmap
= isl_basic_map_universe(dim
);
11516 for (i
= 0; i
< list
->n
; ++i
) {
11518 isl_basic_map
*bmap_i
;
11520 aff
= isl_aff_copy(list
->p
[i
]);
11521 bmap_i
= isl_basic_map_from_aff(aff
);
11523 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11526 isl_aff_list_free(list
);
11530 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11531 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11533 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11536 /* Construct a basic map where the given dimensions are equal to each other.
11538 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11539 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11541 isl_basic_map
*bmap
= NULL
;
11547 if (pos1
>= isl_space_dim(space
, type1
))
11548 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11549 "index out of bounds", goto error
);
11550 if (pos2
>= isl_space_dim(space
, type2
))
11551 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11552 "index out of bounds", goto error
);
11554 if (type1
== type2
&& pos1
== pos2
)
11555 return isl_basic_map_universe(space
);
11557 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11558 i
= isl_basic_map_alloc_equality(bmap
);
11561 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11562 pos1
+= isl_basic_map_offset(bmap
, type1
);
11563 pos2
+= isl_basic_map_offset(bmap
, type2
);
11564 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11565 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11566 bmap
= isl_basic_map_finalize(bmap
);
11567 isl_space_free(space
);
11570 isl_space_free(space
);
11571 isl_basic_map_free(bmap
);
11575 /* Add a constraint imposing that the given two dimensions are equal.
11577 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11578 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11582 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11584 bmap
= isl_basic_map_intersect(bmap
, eq
);
11589 /* Add a constraint imposing that the given two dimensions are equal.
11591 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11592 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11594 isl_basic_map
*bmap
;
11596 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11598 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11603 /* Add a constraint imposing that the given two dimensions have opposite values.
11605 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11606 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11608 isl_basic_map
*bmap
= NULL
;
11614 if (pos1
>= isl_map_dim(map
, type1
))
11615 isl_die(map
->ctx
, isl_error_invalid
,
11616 "index out of bounds", goto error
);
11617 if (pos2
>= isl_map_dim(map
, type2
))
11618 isl_die(map
->ctx
, isl_error_invalid
,
11619 "index out of bounds", goto error
);
11621 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11622 i
= isl_basic_map_alloc_equality(bmap
);
11625 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11626 pos1
+= isl_basic_map_offset(bmap
, type1
);
11627 pos2
+= isl_basic_map_offset(bmap
, type2
);
11628 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11629 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11630 bmap
= isl_basic_map_finalize(bmap
);
11632 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11636 isl_basic_map_free(bmap
);
11641 /* Construct a constraint imposing that the value of the first dimension is
11642 * greater than or equal to that of the second.
11644 static __isl_give isl_constraint
*constraint_order_ge(
11645 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11646 enum isl_dim_type type2
, int pos2
)
11653 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11655 if (pos1
>= isl_constraint_dim(c
, type1
))
11656 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11657 "index out of bounds", return isl_constraint_free(c
));
11658 if (pos2
>= isl_constraint_dim(c
, type2
))
11659 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11660 "index out of bounds", return isl_constraint_free(c
));
11662 if (type1
== type2
&& pos1
== pos2
)
11665 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11666 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11671 /* Add a constraint imposing that the value of the first dimension is
11672 * greater than or equal to that of the second.
11674 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11675 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11680 if (type1
== type2
&& pos1
== pos2
)
11682 space
= isl_basic_map_get_space(bmap
);
11683 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11684 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11689 /* Add a constraint imposing that the value of the first dimension is
11690 * greater than or equal to that of the second.
11692 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11693 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11698 if (type1
== type2
&& pos1
== pos2
)
11700 space
= isl_map_get_space(map
);
11701 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11702 map
= isl_map_add_constraint(map
, c
);
11707 /* Add a constraint imposing that the value of the first dimension is
11708 * less than or equal to that of the second.
11710 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11711 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11713 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11716 /* Construct a basic map where the value of the first dimension is
11717 * greater than that of the second.
11719 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11720 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11722 isl_basic_map
*bmap
= NULL
;
11728 if (pos1
>= isl_space_dim(space
, type1
))
11729 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11730 "index out of bounds", goto error
);
11731 if (pos2
>= isl_space_dim(space
, type2
))
11732 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11733 "index out of bounds", goto error
);
11735 if (type1
== type2
&& pos1
== pos2
)
11736 return isl_basic_map_empty(space
);
11738 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11739 i
= isl_basic_map_alloc_inequality(bmap
);
11741 return isl_basic_map_free(bmap
);
11742 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11743 pos1
+= isl_basic_map_offset(bmap
, type1
);
11744 pos2
+= isl_basic_map_offset(bmap
, type2
);
11745 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11746 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11747 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11748 bmap
= isl_basic_map_finalize(bmap
);
11752 isl_space_free(space
);
11753 isl_basic_map_free(bmap
);
11757 /* Add a constraint imposing that the value of the first dimension is
11758 * greater than that of the second.
11760 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11761 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11765 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11767 bmap
= isl_basic_map_intersect(bmap
, gt
);
11772 /* Add a constraint imposing that the value of the first dimension is
11773 * greater than that of the second.
11775 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11776 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11778 isl_basic_map
*bmap
;
11780 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11782 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11787 /* Add a constraint imposing that the value of the first dimension is
11788 * smaller than that of the second.
11790 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11791 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11793 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11796 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11800 isl_local_space
*ls
;
11805 if (!isl_basic_map_divs_known(bmap
))
11806 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11807 "some divs are unknown", return NULL
);
11809 ls
= isl_basic_map_get_local_space(bmap
);
11810 div
= isl_local_space_get_div(ls
, pos
);
11811 isl_local_space_free(ls
);
11816 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11819 return isl_basic_map_get_div(bset
, pos
);
11822 /* Plug in "subs" for dimension "type", "pos" of "bset".
11824 * Let i be the dimension to replace and let "subs" be of the form
11828 * Any integer division with a non-zero coefficient for i,
11830 * floor((a i + g)/m)
11834 * floor((a f + d g)/(m d))
11836 * Constraints of the form
11844 * We currently require that "subs" is an integral expression.
11845 * Handling rational expressions may require us to add stride constraints
11846 * as we do in isl_basic_set_preimage_multi_aff.
11848 __isl_give isl_basic_set
*isl_basic_set_substitute(
11849 __isl_take isl_basic_set
*bset
,
11850 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11856 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11859 bset
= isl_basic_set_cow(bset
);
11860 if (!bset
|| !subs
)
11863 ctx
= isl_basic_set_get_ctx(bset
);
11864 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11865 isl_die(ctx
, isl_error_invalid
,
11866 "spaces don't match", goto error
);
11867 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11868 isl_die(ctx
, isl_error_unsupported
,
11869 "cannot handle divs yet", goto error
);
11870 if (!isl_int_is_one(subs
->v
->el
[0]))
11871 isl_die(ctx
, isl_error_invalid
,
11872 "can only substitute integer expressions", goto error
);
11874 pos
+= isl_basic_set_offset(bset
, type
);
11878 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11879 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11881 isl_int_set(v
, bset
->eq
[i
][pos
]);
11882 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11883 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11884 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11887 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11888 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11890 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11891 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11892 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11893 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11896 for (i
= 0; i
< bset
->n_div
; ++i
) {
11897 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11899 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11900 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11901 isl_seq_combine(bset
->div
[i
] + 1,
11902 subs
->v
->el
[0], bset
->div
[i
] + 1,
11903 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11904 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11909 bset
= isl_basic_set_simplify(bset
);
11910 return isl_basic_set_finalize(bset
);
11912 isl_basic_set_free(bset
);
11916 /* Plug in "subs" for dimension "type", "pos" of "set".
11918 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11919 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11923 if (set
&& isl_set_plain_is_empty(set
))
11926 set
= isl_set_cow(set
);
11930 for (i
= set
->n
- 1; i
>= 0; --i
) {
11931 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11932 if (remove_if_empty(set
, i
) < 0)
11942 /* Check if the range of "ma" is compatible with the domain or range
11943 * (depending on "type") of "bmap".
11944 * Return -1 if anything is wrong.
11946 static int check_basic_map_compatible_range_multi_aff(
11947 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11948 __isl_keep isl_multi_aff
*ma
)
11951 isl_space
*ma_space
;
11953 ma_space
= isl_multi_aff_get_space(ma
);
11955 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
11959 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11960 "parameters don't match", goto error
);
11961 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11965 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11966 "spaces don't match", goto error
);
11968 isl_space_free(ma_space
);
11971 isl_space_free(ma_space
);
11975 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11976 * coefficients before the transformed range of dimensions,
11977 * the "n_after" coefficients after the transformed range of dimensions
11978 * and the coefficients of the other divs in "bmap".
11980 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11981 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11986 isl_local_space
*ls
;
11991 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11995 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11996 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11997 for (i
= 0; i
< n_div
; ++i
) {
11998 int o_bmap
= 0, o_ls
= 0;
12000 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12001 o_bmap
+= 1 + 1 + n_param
;
12002 o_ls
+= 1 + 1 + n_param
;
12003 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12004 o_bmap
+= n_before
;
12005 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12006 ls
->div
->row
[i
] + o_ls
, n_set
);
12009 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12011 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12012 ls
->div
->row
[i
] + o_ls
, n_div
);
12015 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12016 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12020 isl_local_space_free(ls
);
12023 isl_local_space_free(ls
);
12027 /* How many stride constraints does "ma" enforce?
12028 * That is, how many of the affine expressions have a denominator
12029 * different from one?
12031 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12036 for (i
= 0; i
< ma
->n
; ++i
)
12037 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12043 /* For each affine expression in ma of the form
12045 * x_i = (f_i y + h_i)/m_i
12047 * with m_i different from one, add a constraint to "bmap"
12050 * f_i y + h_i = m_i alpha_i
12052 * with alpha_i an additional existentially quantified variable.
12054 static __isl_give isl_basic_map
*add_ma_strides(
12055 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12056 int n_before
, int n_after
)
12065 total
= isl_basic_map_total_dim(bmap
);
12066 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12067 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12068 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12069 for (i
= 0; i
< ma
->n
; ++i
) {
12070 int o_bmap
= 0, o_ma
= 1;
12072 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12074 div
= isl_basic_map_alloc_div(bmap
);
12075 k
= isl_basic_map_alloc_equality(bmap
);
12076 if (div
< 0 || k
< 0)
12078 isl_int_set_si(bmap
->div
[div
][0], 0);
12079 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12080 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12081 o_bmap
+= 1 + n_param
;
12082 o_ma
+= 1 + n_param
;
12083 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12084 o_bmap
+= n_before
;
12085 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12086 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12089 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12091 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12092 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12095 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12096 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12102 isl_basic_map_free(bmap
);
12106 /* Replace the domain or range space (depending on "type) of "space" by "set".
12108 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12109 enum isl_dim_type type
, __isl_take isl_space
*set
)
12111 if (type
== isl_dim_in
) {
12112 space
= isl_space_range(space
);
12113 space
= isl_space_map_from_domain_and_range(set
, space
);
12115 space
= isl_space_domain(space
);
12116 space
= isl_space_map_from_domain_and_range(space
, set
);
12122 /* Compute the preimage of the domain or range (depending on "type")
12123 * of "bmap" under the function represented by "ma".
12124 * In other words, plug in "ma" in the domain or range of "bmap".
12125 * The result is a basic map that lives in the same space as "bmap"
12126 * except that the domain or range has been replaced by
12127 * the domain space of "ma".
12129 * If bmap is represented by
12131 * A(p) + S u + B x + T v + C(divs) >= 0,
12133 * where u and x are input and output dimensions if type == isl_dim_out
12134 * while x and v are input and output dimensions if type == isl_dim_in,
12135 * and ma is represented by
12137 * x = D(p) + F(y) + G(divs')
12139 * then the result is
12141 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12143 * The divs in the input set are similarly adjusted.
12146 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12150 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12151 * B_i G(divs') + c_i(divs))/n_i)
12153 * If bmap is not a rational map and if F(y) involves any denominators
12155 * x_i = (f_i y + h_i)/m_i
12157 * then additional constraints are added to ensure that we only
12158 * map back integer points. That is we enforce
12160 * f_i y + h_i = m_i alpha_i
12162 * with alpha_i an additional existentially quantified variable.
12164 * We first copy over the divs from "ma".
12165 * Then we add the modified constraints and divs from "bmap".
12166 * Finally, we add the stride constraints, if needed.
12168 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12169 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12170 __isl_take isl_multi_aff
*ma
)
12174 isl_basic_map
*res
= NULL
;
12175 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12176 isl_int f
, c1
, c2
, g
;
12177 int rational
, strides
;
12184 ma
= isl_multi_aff_align_divs(ma
);
12187 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12190 if (type
== isl_dim_in
) {
12192 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12194 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12197 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12198 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12200 space
= isl_multi_aff_get_domain_space(ma
);
12201 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12202 rational
= isl_basic_map_is_rational(bmap
);
12203 strides
= rational
? 0 : multi_aff_strides(ma
);
12204 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12205 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12207 res
= isl_basic_map_set_rational(res
);
12209 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12210 if (isl_basic_map_alloc_div(res
) < 0)
12213 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12216 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12217 k
= isl_basic_map_alloc_equality(res
);
12220 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12221 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12224 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12225 k
= isl_basic_map_alloc_inequality(res
);
12228 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12229 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12232 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12233 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12234 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12237 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12238 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12243 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12249 isl_basic_map_free(bmap
);
12250 isl_multi_aff_free(ma
);
12251 res
= isl_basic_set_simplify(res
);
12252 return isl_basic_map_finalize(res
);
12258 isl_basic_map_free(bmap
);
12259 isl_multi_aff_free(ma
);
12260 isl_basic_map_free(res
);
12264 /* Compute the preimage of "bset" under the function represented by "ma".
12265 * In other words, plug in "ma" in "bset". The result is a basic set
12266 * that lives in the domain space of "ma".
12268 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12269 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12271 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12274 /* Compute the preimage of the domain of "bmap" under the function
12275 * represented by "ma".
12276 * In other words, plug in "ma" in the domain of "bmap".
12277 * The result is a basic map that lives in the same space as "bmap"
12278 * except that the domain has been replaced by the domain space of "ma".
12280 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12281 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12283 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12286 /* Compute the preimage of the range of "bmap" under the function
12287 * represented by "ma".
12288 * In other words, plug in "ma" in the range of "bmap".
12289 * The result is a basic map that lives in the same space as "bmap"
12290 * except that the range has been replaced by the domain space of "ma".
12292 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12293 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12295 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12298 /* Check if the range of "ma" is compatible with the domain or range
12299 * (depending on "type") of "map".
12300 * Return -1 if anything is wrong.
12302 static int check_map_compatible_range_multi_aff(
12303 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12304 __isl_keep isl_multi_aff
*ma
)
12307 isl_space
*ma_space
;
12309 ma_space
= isl_multi_aff_get_space(ma
);
12310 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
12311 isl_space_free(ma_space
);
12313 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12314 "spaces don't match", return -1);
12318 /* Compute the preimage of the domain or range (depending on "type")
12319 * of "map" under the function represented by "ma".
12320 * In other words, plug in "ma" in the domain or range of "map".
12321 * The result is a map that lives in the same space as "map"
12322 * except that the domain or range has been replaced by
12323 * the domain space of "ma".
12325 * The parameters are assumed to have been aligned.
12327 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12328 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12333 map
= isl_map_cow(map
);
12334 ma
= isl_multi_aff_align_divs(ma
);
12337 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12340 for (i
= 0; i
< map
->n
; ++i
) {
12341 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12342 isl_multi_aff_copy(ma
));
12347 space
= isl_multi_aff_get_domain_space(ma
);
12348 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12350 isl_space_free(map
->dim
);
12355 isl_multi_aff_free(ma
);
12357 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12358 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12361 isl_multi_aff_free(ma
);
12366 /* Compute the preimage of the domain or range (depending on "type")
12367 * of "map" under the function represented by "ma".
12368 * In other words, plug in "ma" in the domain or range of "map".
12369 * The result is a map that lives in the same space as "map"
12370 * except that the domain or range has been replaced by
12371 * the domain space of "ma".
12373 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12374 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12379 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12380 return map_preimage_multi_aff(map
, type
, ma
);
12382 if (!isl_space_has_named_params(map
->dim
) ||
12383 !isl_space_has_named_params(ma
->space
))
12384 isl_die(map
->ctx
, isl_error_invalid
,
12385 "unaligned unnamed parameters", goto error
);
12386 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12387 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12389 return map_preimage_multi_aff(map
, type
, ma
);
12391 isl_multi_aff_free(ma
);
12392 return isl_map_free(map
);
12395 /* Compute the preimage of "set" under the function represented by "ma".
12396 * In other words, plug in "ma" in "set". The result is a set
12397 * that lives in the domain space of "ma".
12399 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12400 __isl_take isl_multi_aff
*ma
)
12402 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12405 /* Compute the preimage of the domain of "map" under the function
12406 * represented by "ma".
12407 * In other words, plug in "ma" in the domain of "map".
12408 * The result is a map that lives in the same space as "map"
12409 * except that the domain has been replaced by the domain space of "ma".
12411 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12412 __isl_take isl_multi_aff
*ma
)
12414 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12417 /* Compute the preimage of the range of "map" under the function
12418 * represented by "ma".
12419 * In other words, plug in "ma" in the range of "map".
12420 * The result is a map that lives in the same space as "map"
12421 * except that the range has been replaced by the domain space of "ma".
12423 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12424 __isl_take isl_multi_aff
*ma
)
12426 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12429 /* Compute the preimage of "map" under the function represented by "pma".
12430 * In other words, plug in "pma" in the domain or range of "map".
12431 * The result is a map that lives in the same space as "map",
12432 * except that the space of type "type" has been replaced by
12433 * the domain space of "pma".
12435 * The parameters of "map" and "pma" are assumed to have been aligned.
12437 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12438 __isl_take isl_map
*map
, enum isl_dim_type type
,
12439 __isl_take isl_pw_multi_aff
*pma
)
12448 isl_pw_multi_aff_free(pma
);
12449 res
= isl_map_empty(isl_map_get_space(map
));
12454 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12455 isl_multi_aff_copy(pma
->p
[0].maff
));
12456 if (type
== isl_dim_in
)
12457 res
= isl_map_intersect_domain(res
,
12458 isl_map_copy(pma
->p
[0].set
));
12460 res
= isl_map_intersect_range(res
,
12461 isl_map_copy(pma
->p
[0].set
));
12463 for (i
= 1; i
< pma
->n
; ++i
) {
12466 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12467 isl_multi_aff_copy(pma
->p
[i
].maff
));
12468 if (type
== isl_dim_in
)
12469 res_i
= isl_map_intersect_domain(res_i
,
12470 isl_map_copy(pma
->p
[i
].set
));
12472 res_i
= isl_map_intersect_range(res_i
,
12473 isl_map_copy(pma
->p
[i
].set
));
12474 res
= isl_map_union(res
, res_i
);
12477 isl_pw_multi_aff_free(pma
);
12481 isl_pw_multi_aff_free(pma
);
12486 /* Compute the preimage of "map" under the function represented by "pma".
12487 * In other words, plug in "pma" in the domain or range of "map".
12488 * The result is a map that lives in the same space as "map",
12489 * except that the space of type "type" has been replaced by
12490 * the domain space of "pma".
12492 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12493 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12498 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12499 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12501 if (!isl_space_has_named_params(map
->dim
) ||
12502 !isl_space_has_named_params(pma
->dim
))
12503 isl_die(map
->ctx
, isl_error_invalid
,
12504 "unaligned unnamed parameters", goto error
);
12505 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12506 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12508 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12510 isl_pw_multi_aff_free(pma
);
12511 return isl_map_free(map
);
12514 /* Compute the preimage of "set" under the function represented by "pma".
12515 * In other words, plug in "pma" in "set". The result is a set
12516 * that lives in the domain space of "pma".
12518 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12519 __isl_take isl_pw_multi_aff
*pma
)
12521 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12524 /* Compute the preimage of the domain of "map" under the function
12525 * represented by "pma".
12526 * In other words, plug in "pma" in the domain of "map".
12527 * The result is a map that lives in the same space as "map",
12528 * except that domain space has been replaced by the domain space of "pma".
12530 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12531 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12533 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12536 /* Compute the preimage of the range of "map" under the function
12537 * represented by "pma".
12538 * In other words, plug in "pma" in the range of "map".
12539 * The result is a map that lives in the same space as "map",
12540 * except that range space has been replaced by the domain space of "pma".
12542 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12543 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12545 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12548 /* Compute the preimage of "map" under the function represented by "mpa".
12549 * In other words, plug in "mpa" in the domain or range of "map".
12550 * The result is a map that lives in the same space as "map",
12551 * except that the space of type "type" has been replaced by
12552 * the domain space of "mpa".
12554 * If the map does not involve any constraints that refer to the
12555 * dimensions of the substituted space, then the only possible
12556 * effect of "mpa" on the map is to map the space to a different space.
12557 * We create a separate isl_multi_aff to effectuate this change
12558 * in order to avoid spurious splitting of the map along the pieces
12561 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12562 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12565 isl_pw_multi_aff
*pma
;
12570 n
= isl_map_dim(map
, type
);
12571 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12575 space
= isl_multi_pw_aff_get_space(mpa
);
12576 isl_multi_pw_aff_free(mpa
);
12577 ma
= isl_multi_aff_zero(space
);
12578 return isl_map_preimage_multi_aff(map
, type
, ma
);
12581 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12582 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12585 isl_multi_pw_aff_free(mpa
);
12589 /* Compute the preimage of "map" under the function represented by "mpa".
12590 * In other words, plug in "mpa" in the domain "map".
12591 * The result is a map that lives in the same space as "map",
12592 * except that domain space has been replaced by the domain space of "mpa".
12594 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12595 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12597 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12600 /* Compute the preimage of "set" by the function represented by "mpa".
12601 * In other words, plug in "mpa" in "set".
12603 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12604 __isl_take isl_multi_pw_aff
*mpa
)
12606 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);