2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
22 #include <isl/constraint.h>
23 #include "isl_space_private.h"
24 #include "isl_equalities.h"
25 #include <isl_lp_private.h>
29 #include <isl_reordering.h>
30 #include "isl_sample.h"
34 #include <isl_mat_private.h>
35 #include <isl_vec_private.h>
36 #include <isl_dim_map.h>
37 #include <isl_local_space_private.h>
38 #include <isl_aff_private.h>
39 #include <isl_options_private.h>
40 #include <isl_morph.h>
41 #include <isl_val_private.h>
42 #include <isl/deprecated/map_int.h>
43 #include <isl/deprecated/set_int.h>
45 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
48 case isl_dim_param
: return dim
->nparam
;
49 case isl_dim_in
: return dim
->n_in
;
50 case isl_dim_out
: return dim
->n_out
;
51 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
56 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
59 case isl_dim_param
: return 1;
60 case isl_dim_in
: return 1 + dim
->nparam
;
61 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
66 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
67 enum isl_dim_type type
)
72 case isl_dim_cst
: return 1;
75 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
76 case isl_dim_div
: return bmap
->n_div
;
77 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
82 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
84 return map
? n(map
->dim
, type
) : 0;
87 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
89 return set
? n(set
->dim
, type
) : 0;
92 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
93 enum isl_dim_type type
)
95 isl_space
*dim
= bmap
->dim
;
97 case isl_dim_cst
: return 0;
98 case isl_dim_param
: return 1;
99 case isl_dim_in
: return 1 + dim
->nparam
;
100 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
101 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
106 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
107 enum isl_dim_type type
)
109 return isl_basic_map_offset(bset
, type
);
112 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
114 return pos(map
->dim
, type
);
117 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
118 enum isl_dim_type type
)
120 return isl_basic_map_dim(bset
, type
);
123 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
125 return isl_basic_set_dim(bset
, isl_dim_set
);
128 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
130 return isl_basic_set_dim(bset
, isl_dim_param
);
133 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
137 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
140 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
142 return isl_set_dim(set
, isl_dim_set
);
145 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
147 return isl_set_dim(set
, isl_dim_param
);
150 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
152 return bmap
? bmap
->dim
->n_in
: 0;
155 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
157 return bmap
? bmap
->dim
->n_out
: 0;
160 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
162 return bmap
? bmap
->dim
->nparam
: 0;
165 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
167 return bmap
? bmap
->n_div
: 0;
170 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
172 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
175 unsigned isl_map_n_in(const struct isl_map
*map
)
177 return map
? map
->dim
->n_in
: 0;
180 unsigned isl_map_n_out(const struct isl_map
*map
)
182 return map
? map
->dim
->n_out
: 0;
185 unsigned isl_map_n_param(const struct isl_map
*map
)
187 return map
? map
->dim
->nparam
: 0;
190 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
195 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
198 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
199 set
->dim
, isl_dim_set
);
202 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
203 struct isl_basic_set
*bset
)
208 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
211 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
212 bset
->dim
, isl_dim_set
);
215 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
220 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
223 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
224 set
->dim
, isl_dim_set
);
227 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
228 struct isl_basic_set
*bset
)
233 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
236 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
237 bset
->dim
, isl_dim_set
);
240 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
242 return bmap
? bmap
->ctx
: NULL
;
245 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
247 return bset
? bset
->ctx
: NULL
;
250 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
252 return map
? map
->ctx
: NULL
;
255 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
257 return set
? set
->ctx
: NULL
;
260 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
264 return isl_space_copy(bmap
->dim
);
267 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
271 return isl_space_copy(bset
->dim
);
274 /* Extract the divs in "bmap" as a matrix.
276 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
287 ctx
= isl_basic_map_get_ctx(bmap
);
288 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
289 cols
= 1 + 1 + total
+ bmap
->n_div
;
290 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
294 for (i
= 0; i
< bmap
->n_div
; ++i
)
295 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
300 /* Extract the divs in "bset" as a matrix.
302 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
304 return isl_basic_map_get_divs(bset
);
307 __isl_give isl_local_space
*isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map
*bmap
)
315 div
= isl_basic_map_get_divs(bmap
);
316 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
319 __isl_give isl_local_space
*isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set
*bset
)
322 return isl_basic_map_get_local_space(bset
);
325 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
326 __isl_take isl_local_space
*ls
)
335 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
336 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
337 n_div
, 0, 2 * n_div
);
339 for (i
= 0; i
< n_div
; ++i
)
340 if (isl_basic_map_alloc_div(bmap
) < 0)
343 for (i
= 0; i
< n_div
; ++i
) {
344 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
345 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
349 isl_local_space_free(ls
);
352 isl_local_space_free(ls
);
353 isl_basic_map_free(bmap
);
357 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
358 __isl_take isl_local_space
*ls
)
360 return isl_basic_map_from_local_space(ls
);
363 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
367 return isl_space_copy(map
->dim
);
370 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
374 return isl_space_copy(set
->dim
);
377 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
378 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
380 bmap
= isl_basic_map_cow(bmap
);
383 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
386 bmap
= isl_basic_map_finalize(bmap
);
389 isl_basic_map_free(bmap
);
393 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
394 __isl_take isl_basic_set
*bset
, const char *s
)
396 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
399 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
400 enum isl_dim_type type
)
402 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
405 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
406 enum isl_dim_type type
, const char *s
)
410 map
= isl_map_cow(map
);
414 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
418 for (i
= 0; i
< map
->n
; ++i
) {
419 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
430 /* Replace the identifier of the tuple of type "type" by "id".
432 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
433 __isl_take isl_basic_map
*bmap
,
434 enum isl_dim_type type
, __isl_take isl_id
*id
)
436 bmap
= isl_basic_map_cow(bmap
);
439 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
441 return isl_basic_map_free(bmap
);
442 bmap
= isl_basic_map_finalize(bmap
);
449 /* Replace the identifier of the tuple by "id".
451 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
452 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
454 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
457 /* Does the input or output tuple have a name?
459 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
461 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
464 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
465 enum isl_dim_type type
)
467 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
470 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
473 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
476 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
477 enum isl_dim_type type
, __isl_take isl_id
*id
)
479 map
= isl_map_cow(map
);
483 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
485 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
491 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
492 __isl_take isl_id
*id
)
494 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
497 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
498 enum isl_dim_type type
)
500 map
= isl_map_cow(map
);
504 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
506 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
509 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
511 return isl_map_reset_tuple_id(set
, isl_dim_set
);
514 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
516 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
519 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
520 enum isl_dim_type type
)
522 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
525 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
527 return isl_map_has_tuple_id(set
, isl_dim_set
);
530 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
532 return isl_map_get_tuple_id(set
, isl_dim_set
);
535 /* Does the set tuple have a name?
537 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
539 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
543 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
545 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
548 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
550 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
553 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
554 enum isl_dim_type type
, unsigned pos
)
556 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
559 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
560 enum isl_dim_type type
, unsigned pos
)
562 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
565 /* Does the given dimension have a name?
567 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
568 enum isl_dim_type type
, unsigned pos
)
570 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
573 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
574 enum isl_dim_type type
, unsigned pos
)
576 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
579 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
580 enum isl_dim_type type
, unsigned pos
)
582 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
585 /* Does the given dimension have a name?
587 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
588 enum isl_dim_type type
, unsigned pos
)
590 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
593 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
594 __isl_take isl_basic_map
*bmap
,
595 enum isl_dim_type type
, unsigned pos
, const char *s
)
597 bmap
= isl_basic_map_cow(bmap
);
600 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
603 return isl_basic_map_finalize(bmap
);
605 isl_basic_map_free(bmap
);
609 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
610 enum isl_dim_type type
, unsigned pos
, const char *s
)
614 map
= isl_map_cow(map
);
618 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
622 for (i
= 0; i
< map
->n
; ++i
) {
623 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
634 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
635 __isl_take isl_basic_set
*bset
,
636 enum isl_dim_type type
, unsigned pos
, const char *s
)
638 return (isl_basic_set
*)isl_basic_map_set_dim_name(
639 (isl_basic_map
*)bset
, type
, pos
, s
);
642 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
643 enum isl_dim_type type
, unsigned pos
, const char *s
)
645 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
648 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
649 enum isl_dim_type type
, unsigned pos
)
651 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
654 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
655 enum isl_dim_type type
, unsigned pos
)
657 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
660 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
661 enum isl_dim_type type
, unsigned pos
)
663 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
666 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
667 enum isl_dim_type type
, unsigned pos
)
669 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
672 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
673 enum isl_dim_type type
, unsigned pos
)
675 return isl_map_has_dim_id(set
, type
, pos
);
678 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
679 enum isl_dim_type type
, unsigned pos
)
681 return isl_map_get_dim_id(set
, type
, pos
);
684 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
685 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
687 map
= isl_map_cow(map
);
691 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
693 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
699 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
700 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
702 return isl_map_set_dim_id(set
, type
, pos
, id
);
705 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
706 __isl_keep isl_id
*id
)
710 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
713 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
714 __isl_keep isl_id
*id
)
716 return isl_map_find_dim_by_id(set
, type
, id
);
719 /* Return the position of the dimension of the given type and name
721 * Return -1 if no such dimension can be found.
723 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
724 enum isl_dim_type type
, const char *name
)
728 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
731 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
736 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
739 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
742 return isl_map_find_dim_by_name(set
, type
, name
);
745 /* Reset the user pointer on all identifiers of parameters and tuples
746 * of the space of "map".
748 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
752 space
= isl_map_get_space(map
);
753 space
= isl_space_reset_user(space
);
754 map
= isl_map_reset_space(map
, space
);
759 /* Reset the user pointer on all identifiers of parameters and tuples
760 * of the space of "set".
762 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
764 return isl_map_reset_user(set
);
767 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
771 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
774 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
776 return isl_basic_map_is_rational(bset
);
779 /* Does "bmap" contain any rational points?
781 * If "bmap" has an equality for each dimension, equating the dimension
782 * to an integer constant, then it has no rational points, even if it
783 * is marked as rational.
785 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
787 int has_rational
= 1;
792 if (isl_basic_map_plain_is_empty(bmap
))
794 if (!isl_basic_map_is_rational(bmap
))
796 bmap
= isl_basic_map_copy(bmap
);
797 bmap
= isl_basic_map_implicit_equalities(bmap
);
800 total
= isl_basic_map_total_dim(bmap
);
801 if (bmap
->n_eq
== total
) {
803 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
804 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
807 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
808 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
810 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
818 isl_basic_map_free(bmap
);
823 /* Does "map" contain any rational points?
825 int isl_map_has_rational(__isl_keep isl_map
*map
)
832 for (i
= 0; i
< map
->n
; ++i
) {
833 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
834 if (has_rational
< 0)
842 /* Does "set" contain any rational points?
844 int isl_set_has_rational(__isl_keep isl_set
*set
)
846 return isl_map_has_rational(set
);
849 /* Is this basic set a parameter domain?
851 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
855 return isl_space_is_params(bset
->dim
);
858 /* Is this set a parameter domain?
860 int isl_set_is_params(__isl_keep isl_set
*set
)
864 return isl_space_is_params(set
->dim
);
867 /* Is this map actually a parameter domain?
868 * Users should never call this function. Outside of isl,
869 * a map can never be a parameter domain.
871 int isl_map_is_params(__isl_keep isl_map
*map
)
875 return isl_space_is_params(map
->dim
);
878 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
879 struct isl_basic_map
*bmap
, unsigned extra
,
880 unsigned n_eq
, unsigned n_ineq
)
883 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
888 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
889 if (isl_blk_is_error(bmap
->block
))
892 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
893 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
897 bmap
->block2
= isl_blk_empty();
900 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
901 if (isl_blk_is_error(bmap
->block2
))
904 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
909 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
910 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
912 for (i
= 0; i
< extra
; ++i
)
913 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
917 bmap
->c_size
= n_eq
+ n_ineq
;
918 bmap
->eq
= bmap
->ineq
+ n_ineq
;
927 isl_basic_map_free(bmap
);
931 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
932 unsigned nparam
, unsigned dim
, unsigned extra
,
933 unsigned n_eq
, unsigned n_ineq
)
935 struct isl_basic_map
*bmap
;
938 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
942 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
943 return (struct isl_basic_set
*)bmap
;
946 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
947 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
949 struct isl_basic_map
*bmap
;
952 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
953 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
954 return (struct isl_basic_set
*)bmap
;
960 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
961 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
963 struct isl_basic_map
*bmap
;
967 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
972 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
978 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
979 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
980 unsigned n_eq
, unsigned n_ineq
)
982 struct isl_basic_map
*bmap
;
985 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
989 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
993 static void dup_constraints(
994 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
997 unsigned total
= isl_basic_map_total_dim(src
);
999 for (i
= 0; i
< src
->n_eq
; ++i
) {
1000 int j
= isl_basic_map_alloc_equality(dst
);
1001 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1004 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1005 int j
= isl_basic_map_alloc_inequality(dst
);
1006 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1009 for (i
= 0; i
< src
->n_div
; ++i
) {
1010 int j
= isl_basic_map_alloc_div(dst
);
1011 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1013 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1016 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1018 struct isl_basic_map
*dup
;
1022 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1023 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1026 dup_constraints(dup
, bmap
);
1027 dup
->flags
= bmap
->flags
;
1028 dup
->sample
= isl_vec_copy(bmap
->sample
);
1032 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1034 struct isl_basic_map
*dup
;
1036 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1037 return (struct isl_basic_set
*)dup
;
1040 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1045 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1049 return isl_basic_set_dup(bset
);
1052 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1061 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1066 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1070 bmap
= isl_basic_map_dup(bmap
);
1072 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1076 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1085 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1090 if (--bmap
->ref
> 0)
1093 isl_ctx_deref(bmap
->ctx
);
1095 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1097 isl_blk_free(bmap
->ctx
, bmap
->block
);
1098 isl_vec_free(bmap
->sample
);
1099 isl_space_free(bmap
->dim
);
1105 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1107 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1110 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1112 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1115 __isl_give isl_map
*isl_map_align_params_map_map_and(
1116 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1117 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1118 __isl_take isl_map
*map2
))
1122 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1123 return fn(map1
, map2
);
1124 if (!isl_space_has_named_params(map1
->dim
) ||
1125 !isl_space_has_named_params(map2
->dim
))
1126 isl_die(map1
->ctx
, isl_error_invalid
,
1127 "unaligned unnamed parameters", goto error
);
1128 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1129 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1130 return fn(map1
, map2
);
1137 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1138 __isl_keep isl_map
*map2
,
1139 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1145 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1146 return fn(map1
, map2
);
1147 if (!isl_space_has_named_params(map1
->dim
) ||
1148 !isl_space_has_named_params(map2
->dim
))
1149 isl_die(map1
->ctx
, isl_error_invalid
,
1150 "unaligned unnamed parameters", return -1);
1151 map1
= isl_map_copy(map1
);
1152 map2
= isl_map_copy(map2
);
1153 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1154 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1161 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1163 struct isl_ctx
*ctx
;
1167 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1168 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1170 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1171 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1172 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1173 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1174 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1175 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1177 int j
= isl_basic_map_alloc_inequality(bmap
);
1181 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1182 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1189 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1190 bmap
->extra
- bmap
->n_div
);
1191 return bmap
->n_eq
++;
1194 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1196 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1199 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1203 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1208 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1210 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1213 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1218 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1220 if (pos
!= bmap
->n_eq
- 1) {
1222 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1223 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1229 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1231 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1234 /* Turn inequality "pos" of "bmap" into an equality.
1236 * In particular, we move the inequality in front of the equalities
1237 * and move the last inequality in the position of the moved inequality.
1238 * Note that isl_tab_make_equalities_explicit depends on this particular
1239 * change in the ordering of the constraints.
1241 void isl_basic_map_inequality_to_equality(
1242 struct isl_basic_map
*bmap
, unsigned pos
)
1246 t
= bmap
->ineq
[pos
];
1247 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1248 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1253 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1254 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1256 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1259 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1261 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1264 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1266 struct isl_ctx
*ctx
;
1270 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1274 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1275 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1276 1 + isl_basic_map_total_dim(bmap
),
1277 bmap
->extra
- bmap
->n_div
);
1278 return bmap
->n_ineq
++;
1281 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1283 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1286 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1290 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1295 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1297 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1300 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1305 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1307 if (pos
!= bmap
->n_ineq
- 1) {
1308 t
= bmap
->ineq
[pos
];
1309 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1310 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1311 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1317 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1319 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1322 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1327 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1330 k
= isl_basic_map_alloc_equality(bmap
);
1333 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1336 isl_basic_map_free(bmap
);
1340 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1343 return (isl_basic_set
*)
1344 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1347 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1352 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1355 k
= isl_basic_map_alloc_inequality(bmap
);
1358 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1361 isl_basic_map_free(bmap
);
1365 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1368 return (isl_basic_set
*)
1369 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1372 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1376 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1377 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1378 1 + 1 + isl_basic_map_total_dim(bmap
),
1379 bmap
->extra
- bmap
->n_div
);
1380 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1381 return bmap
->n_div
++;
1384 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1386 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1389 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1393 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1398 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1400 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1403 /* Copy constraint from src to dst, putting the vars of src at offset
1404 * dim_off in dst and the divs of src at offset div_off in dst.
1405 * If both sets are actually map, then dim_off applies to the input
1408 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1409 struct isl_basic_map
*src_map
, isl_int
*src
,
1410 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1412 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1413 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1414 unsigned src_in
= isl_basic_map_n_in(src_map
);
1415 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1416 unsigned src_out
= isl_basic_map_n_out(src_map
);
1417 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1418 isl_int_set(dst
[0], src
[0]);
1419 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1420 if (dst_nparam
> src_nparam
)
1421 isl_seq_clr(dst
+1+src_nparam
,
1422 dst_nparam
- src_nparam
);
1423 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1424 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1426 isl_min(dst_in
-in_off
, src_in
));
1427 if (dst_in
-in_off
> src_in
)
1428 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1429 dst_in
- in_off
- src_in
);
1430 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1431 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1432 src
+1+src_nparam
+src_in
,
1433 isl_min(dst_out
-out_off
, src_out
));
1434 if (dst_out
-out_off
> src_out
)
1435 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1436 dst_out
- out_off
- src_out
);
1437 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1438 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1439 src
+1+src_nparam
+src_in
+src_out
,
1440 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1441 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1442 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1443 div_off
+src_map
->n_div
,
1444 dst_map
->n_div
- div_off
- src_map
->n_div
);
1447 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1448 struct isl_basic_map
*src_map
, isl_int
*src
,
1449 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1451 isl_int_set(dst
[0], src
[0]);
1452 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1455 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1456 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1461 if (!bmap1
|| !bmap2
)
1464 div_off
= bmap1
->n_div
;
1466 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1467 int i1
= isl_basic_map_alloc_equality(bmap1
);
1470 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1471 i_pos
, o_pos
, div_off
);
1474 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1475 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1478 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1479 i_pos
, o_pos
, div_off
);
1482 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1483 int i1
= isl_basic_map_alloc_div(bmap1
);
1486 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1487 i_pos
, o_pos
, div_off
);
1490 isl_basic_map_free(bmap2
);
1495 isl_basic_map_free(bmap1
);
1496 isl_basic_map_free(bmap2
);
1500 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1501 struct isl_basic_set
*bset2
, unsigned pos
)
1503 return (struct isl_basic_set
*)
1504 add_constraints((struct isl_basic_map
*)bset1
,
1505 (struct isl_basic_map
*)bset2
, 0, pos
);
1508 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1509 __isl_take isl_space
*dim
, unsigned extra
,
1510 unsigned n_eq
, unsigned n_ineq
)
1512 struct isl_basic_map
*ext
;
1522 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1523 base
->extra
>= base
->n_div
+ extra
;
1525 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1526 room_for_ineq(base
, n_ineq
)) {
1527 isl_space_free(dim
);
1531 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1532 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1533 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1534 extra
+= base
->extra
;
1536 n_ineq
+= base
->n_ineq
;
1538 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1544 ext
->sample
= isl_vec_copy(base
->sample
);
1545 flags
= base
->flags
;
1546 ext
= add_constraints(ext
, base
, 0, 0);
1549 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1555 isl_space_free(dim
);
1556 isl_basic_map_free(base
);
1560 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1561 __isl_take isl_space
*dim
, unsigned extra
,
1562 unsigned n_eq
, unsigned n_ineq
)
1564 return (struct isl_basic_set
*)
1565 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1566 extra
, n_eq
, n_ineq
);
1569 struct isl_basic_map
*isl_basic_map_extend_constraints(
1570 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1574 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1578 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1579 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1580 unsigned n_eq
, unsigned n_ineq
)
1582 struct isl_basic_map
*bmap
;
1587 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1591 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1594 isl_basic_map_free(base
);
1598 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1599 unsigned nparam
, unsigned dim
, unsigned extra
,
1600 unsigned n_eq
, unsigned n_ineq
)
1602 return (struct isl_basic_set
*)
1603 isl_basic_map_extend((struct isl_basic_map
*)base
,
1604 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1607 struct isl_basic_set
*isl_basic_set_extend_constraints(
1608 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1610 return (struct isl_basic_set
*)
1611 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1615 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1617 return (struct isl_basic_set
*)
1618 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1621 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1626 if (bmap
->ref
> 1) {
1628 bmap
= isl_basic_map_dup(bmap
);
1631 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1632 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1637 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1645 return isl_set_dup(set
);
1648 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1656 return isl_map_dup(map
);
1659 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1660 unsigned a_len
, unsigned b_len
)
1662 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1663 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1664 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1667 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1668 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1676 isl_assert(bmap
->ctx
,
1677 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1679 if (n1
== 0 || n2
== 0)
1682 bmap
= isl_basic_map_cow(bmap
);
1686 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1687 if (isl_blk_is_error(blk
))
1690 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1692 bmap
->eq
[i
] + pos
, n1
, n2
);
1694 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1696 bmap
->ineq
[i
] + pos
, n1
, n2
);
1698 for (i
= 0; i
< bmap
->n_div
; ++i
)
1700 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1702 isl_blk_free(bmap
->ctx
, blk
);
1704 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1705 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1706 return isl_basic_map_finalize(bmap
);
1708 isl_basic_map_free(bmap
);
1712 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1718 total
= isl_basic_map_total_dim(bmap
);
1719 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1720 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1722 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1724 i
= isl_basic_map_alloc_equality(bmap
);
1728 isl_int_set_si(bmap
->eq
[i
][0], 1);
1729 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1730 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1731 isl_vec_free(bmap
->sample
);
1732 bmap
->sample
= NULL
;
1733 return isl_basic_map_finalize(bmap
);
1735 isl_basic_map_free(bmap
);
1739 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1741 return (struct isl_basic_set
*)
1742 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1745 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1748 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1750 isl_int
*t
= bmap
->div
[a
];
1751 bmap
->div
[a
] = bmap
->div
[b
];
1755 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1756 * div definitions accordingly.
1758 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1761 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1763 swap_div(bmap
, a
, b
);
1765 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1766 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1768 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1769 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1771 for (i
= 0; i
< bmap
->n_div
; ++i
)
1772 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1773 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1776 /* Eliminate the specified n dimensions starting at first from the
1777 * constraints, without removing the dimensions from the space.
1778 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1780 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1781 enum isl_dim_type type
, unsigned first
, unsigned n
)
1790 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1791 isl_die(map
->ctx
, isl_error_invalid
,
1792 "index out of bounds", goto error
);
1794 map
= isl_map_cow(map
);
1798 for (i
= 0; i
< map
->n
; ++i
) {
1799 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1809 /* Eliminate the specified n dimensions starting at first from the
1810 * constraints, without removing the dimensions from the space.
1811 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1813 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1814 enum isl_dim_type type
, unsigned first
, unsigned n
)
1816 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1819 /* Eliminate the specified n dimensions starting at first from the
1820 * constraints, without removing the dimensions from the space.
1821 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1823 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1824 unsigned first
, unsigned n
)
1826 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1829 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1830 __isl_take isl_basic_map
*bmap
)
1834 bmap
= isl_basic_map_eliminate_vars(bmap
,
1835 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1839 return isl_basic_map_finalize(bmap
);
1842 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1843 __isl_take isl_basic_set
*bset
)
1845 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1846 (struct isl_basic_map
*)bset
);
1849 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1858 map
= isl_map_cow(map
);
1862 for (i
= 0; i
< map
->n
; ++i
) {
1863 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1873 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1875 return isl_map_remove_divs(set
);
1878 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1879 enum isl_dim_type type
, unsigned first
, unsigned n
)
1883 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1885 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1887 bmap
= isl_basic_map_eliminate_vars(bmap
,
1888 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1891 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1893 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1896 isl_basic_map_free(bmap
);
1900 /* Return true if the definition of the given div (recursively) involves
1901 * any of the given variables.
1903 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1904 unsigned first
, unsigned n
)
1907 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1909 if (isl_int_is_zero(bmap
->div
[div
][0]))
1911 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1914 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1915 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1917 if (div_involves_vars(bmap
, i
, first
, n
))
1924 /* Try and add a lower and/or upper bound on "div" to "bmap"
1925 * based on inequality "i".
1926 * "total" is the total number of variables (excluding the divs).
1927 * "v" is a temporary object that can be used during the calculations.
1928 * If "lb" is set, then a lower bound should be constructed.
1929 * If "ub" is set, then an upper bound should be constructed.
1931 * The calling function has already checked that the inequality does not
1932 * reference "div", but we still need to check that the inequality is
1933 * of the right form. We'll consider the case where we want to construct
1934 * a lower bound. The construction of upper bounds is similar.
1936 * Let "div" be of the form
1938 * q = floor((a + f(x))/d)
1940 * We essentially check if constraint "i" is of the form
1944 * so that we can use it to derive a lower bound on "div".
1945 * However, we allow a slightly more general form
1949 * with the condition that the coefficients of g(x) - f(x) are all
1951 * Rewriting this constraint as
1955 * adding a + f(x) to both sides and dividing by d, we obtain
1957 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1959 * Taking the floor on both sides, we obtain
1961 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1965 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1967 * In the case of an upper bound, we construct the constraint
1969 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1972 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1973 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1974 unsigned total
, isl_int v
, int lb
, int ub
)
1978 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1980 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1981 bmap
->div
[div
][1 + 1 + j
]);
1982 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1985 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1986 bmap
->div
[div
][1 + 1 + j
]);
1987 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1993 bmap
= isl_basic_map_cow(bmap
);
1994 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1996 int k
= isl_basic_map_alloc_inequality(bmap
);
1999 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2000 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2001 bmap
->div
[div
][1 + j
]);
2002 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2003 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2005 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2008 int k
= isl_basic_map_alloc_inequality(bmap
);
2011 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2012 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2013 bmap
->div
[div
][1 + j
]);
2014 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2015 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2017 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2020 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2023 isl_basic_map_free(bmap
);
2027 /* This function is called right before "div" is eliminated from "bmap"
2028 * using Fourier-Motzkin.
2029 * Look through the constraints of "bmap" for constraints on the argument
2030 * of the integer division and use them to construct constraints on the
2031 * integer division itself. These constraints can then be combined
2032 * during the Fourier-Motzkin elimination.
2033 * Note that it is only useful to introduce lower bounds on "div"
2034 * if "bmap" already contains upper bounds on "div" as the newly
2035 * introduce lower bounds can then be combined with the pre-existing
2036 * upper bounds. Similarly for upper bounds.
2037 * We therefore first check if "bmap" contains any lower and/or upper bounds
2040 * It is interesting to note that the introduction of these constraints
2041 * can indeed lead to more accurate results, even when compared to
2042 * deriving constraints on the argument of "div" from constraints on "div".
2043 * Consider, for example, the set
2045 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2047 * The second constraint can be rewritten as
2049 * 2 * [(-i-2j+3)/4] + k >= 0
2051 * from which we can derive
2053 * -i - 2j + 3 >= -2k
2059 * Combined with the first constraint, we obtain
2061 * -3 <= 3 + 2k or k >= -3
2063 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2064 * the first constraint, we obtain
2066 * [(i + 2j)/4] >= [-3/4] = -1
2068 * Combining this constraint with the second constraint, we obtain
2072 static __isl_give isl_basic_map
*insert_bounds_on_div(
2073 __isl_take isl_basic_map
*bmap
, int div
)
2076 int check_lb
, check_ub
;
2083 if (isl_int_is_zero(bmap
->div
[div
][0]))
2086 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2090 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2091 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2098 if (!check_lb
&& !check_ub
)
2103 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2104 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2107 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2108 check_lb
, check_ub
);
2116 /* Remove all divs (recursively) involving any of the given dimensions
2117 * in their definitions.
2119 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2120 __isl_take isl_basic_map
*bmap
,
2121 enum isl_dim_type type
, unsigned first
, unsigned n
)
2127 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2129 first
+= isl_basic_map_offset(bmap
, type
);
2131 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2132 if (!div_involves_vars(bmap
, i
, first
, n
))
2134 bmap
= insert_bounds_on_div(bmap
, i
);
2135 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2143 isl_basic_map_free(bmap
);
2147 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2148 __isl_take isl_basic_set
*bset
,
2149 enum isl_dim_type type
, unsigned first
, unsigned n
)
2151 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2154 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2155 enum isl_dim_type type
, unsigned first
, unsigned n
)
2164 map
= isl_map_cow(map
);
2168 for (i
= 0; i
< map
->n
; ++i
) {
2169 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2180 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2181 enum isl_dim_type type
, unsigned first
, unsigned n
)
2183 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2187 /* Does the desciption of "bmap" depend on the specified dimensions?
2188 * We also check whether the dimensions appear in any of the div definitions.
2189 * In principle there is no need for this check. If the dimensions appear
2190 * in a div definition, they also appear in the defining constraints of that
2193 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2194 enum isl_dim_type type
, unsigned first
, unsigned n
)
2201 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2202 isl_die(bmap
->ctx
, isl_error_invalid
,
2203 "index out of bounds", return -1);
2205 first
+= isl_basic_map_offset(bmap
, type
);
2206 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2207 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2209 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2210 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2212 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2213 if (isl_int_is_zero(bmap
->div
[i
][0]))
2215 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2222 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2223 enum isl_dim_type type
, unsigned first
, unsigned n
)
2230 if (first
+ n
> isl_map_dim(map
, type
))
2231 isl_die(map
->ctx
, isl_error_invalid
,
2232 "index out of bounds", return -1);
2234 for (i
= 0; i
< map
->n
; ++i
) {
2235 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2237 if (involves
< 0 || involves
)
2244 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2245 enum isl_dim_type type
, unsigned first
, unsigned n
)
2247 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2250 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2251 enum isl_dim_type type
, unsigned first
, unsigned n
)
2253 return isl_map_involves_dims(set
, type
, first
, n
);
2256 /* Return true if the definition of the given div is unknown or depends
2259 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2262 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2264 if (isl_int_is_zero(bmap
->div
[div
][0]))
2267 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2268 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2270 if (div_is_unknown(bmap
, i
))
2277 /* Remove all divs that are unknown or defined in terms of unknown divs.
2279 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2280 __isl_take isl_basic_map
*bmap
)
2287 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2288 if (!div_is_unknown(bmap
, i
))
2290 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2299 /* Remove all divs that are unknown or defined in terms of unknown divs.
2301 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2302 __isl_take isl_basic_set
*bset
)
2304 return isl_basic_map_remove_unknown_divs(bset
);
2307 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2316 map
= isl_map_cow(map
);
2320 for (i
= 0; i
< map
->n
; ++i
) {
2321 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2331 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2333 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2336 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2337 __isl_take isl_basic_set
*bset
,
2338 enum isl_dim_type type
, unsigned first
, unsigned n
)
2340 return (isl_basic_set
*)
2341 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2344 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2345 enum isl_dim_type type
, unsigned first
, unsigned n
)
2352 map
= isl_map_cow(map
);
2355 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2357 for (i
= 0; i
< map
->n
; ++i
) {
2358 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2359 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2363 map
= isl_map_drop(map
, type
, first
, n
);
2370 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2371 enum isl_dim_type type
, unsigned first
, unsigned n
)
2373 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2376 /* Project out n inputs starting at first using Fourier-Motzkin */
2377 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2378 unsigned first
, unsigned n
)
2380 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2383 static void dump_term(struct isl_basic_map
*bmap
,
2384 isl_int c
, int pos
, FILE *out
)
2387 unsigned in
= isl_basic_map_n_in(bmap
);
2388 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2389 unsigned nparam
= isl_basic_map_n_param(bmap
);
2391 isl_int_print(out
, c
, 0);
2393 if (!isl_int_is_one(c
))
2394 isl_int_print(out
, c
, 0);
2395 if (pos
< 1 + nparam
) {
2396 name
= isl_space_get_dim_name(bmap
->dim
,
2397 isl_dim_param
, pos
- 1);
2399 fprintf(out
, "%s", name
);
2401 fprintf(out
, "p%d", pos
- 1);
2402 } else if (pos
< 1 + nparam
+ in
)
2403 fprintf(out
, "i%d", pos
- 1 - nparam
);
2404 else if (pos
< 1 + nparam
+ dim
)
2405 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2407 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2411 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2412 int sign
, FILE *out
)
2416 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2420 for (i
= 0, first
= 1; i
< len
; ++i
) {
2421 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2424 fprintf(out
, " + ");
2426 isl_int_abs(v
, c
[i
]);
2427 dump_term(bmap
, v
, i
, out
);
2434 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2435 const char *op
, FILE *out
, int indent
)
2439 fprintf(out
, "%*s", indent
, "");
2441 dump_constraint_sign(bmap
, c
, 1, out
);
2442 fprintf(out
, " %s ", op
);
2443 dump_constraint_sign(bmap
, c
, -1, out
);
2447 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2448 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2450 fprintf(out
, "%*s", indent
, "");
2451 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2456 static void dump_constraints(struct isl_basic_map
*bmap
,
2457 isl_int
**c
, unsigned n
,
2458 const char *op
, FILE *out
, int indent
)
2462 for (i
= 0; i
< n
; ++i
)
2463 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2466 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2470 unsigned total
= isl_basic_map_total_dim(bmap
);
2472 for (j
= 0; j
< 1 + total
; ++j
) {
2473 if (isl_int_is_zero(exp
[j
]))
2475 if (!first
&& isl_int_is_pos(exp
[j
]))
2477 dump_term(bmap
, exp
[j
], j
, out
);
2482 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2486 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2487 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2489 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2490 fprintf(out
, "%*s", indent
, "");
2491 fprintf(out
, "e%d = [(", i
);
2492 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2494 isl_int_print(out
, bmap
->div
[i
][0], 0);
2495 fprintf(out
, "]\n");
2499 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2500 FILE *out
, int indent
)
2503 fprintf(out
, "null basic set\n");
2507 fprintf(out
, "%*s", indent
, "");
2508 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2509 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2510 bset
->extra
, bset
->flags
);
2511 dump((struct isl_basic_map
*)bset
, out
, indent
);
2514 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2515 FILE *out
, int indent
)
2518 fprintf(out
, "null basic map\n");
2522 fprintf(out
, "%*s", indent
, "");
2523 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2524 "flags: %x, n_name: %d\n",
2526 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2527 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2528 dump(bmap
, out
, indent
);
2531 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2536 total
= isl_basic_map_total_dim(bmap
);
2537 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2538 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2539 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2540 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2544 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2547 struct isl_set
*set
;
2551 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2552 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2553 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2554 sizeof(struct isl_set
) +
2555 (n
- 1) * sizeof(struct isl_basic_set
*));
2559 set
->ctx
= dim
->ctx
;
2560 isl_ctx_ref(set
->ctx
);
2568 isl_space_free(dim
);
2572 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2573 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2575 struct isl_set
*set
;
2578 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2582 set
= isl_set_alloc_space(dims
, n
, flags
);
2586 /* Make sure "map" has room for at least "n" more basic maps.
2588 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2591 struct isl_map
*grown
= NULL
;
2595 isl_assert(map
->ctx
, n
>= 0, goto error
);
2596 if (map
->n
+ n
<= map
->size
)
2598 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2601 for (i
= 0; i
< map
->n
; ++i
) {
2602 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2610 isl_map_free(grown
);
2615 /* Make sure "set" has room for at least "n" more basic sets.
2617 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2619 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2622 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2625 struct isl_set
*dup
;
2630 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2633 for (i
= 0; i
< set
->n
; ++i
)
2634 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2638 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2640 return isl_map_from_basic_map(bset
);
2643 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2645 struct isl_map
*map
;
2650 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2651 return isl_map_add_basic_map(map
, bmap
);
2654 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2655 __isl_take isl_basic_set
*bset
)
2657 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2658 (struct isl_basic_map
*)bset
);
2661 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2671 isl_ctx_deref(set
->ctx
);
2672 for (i
= 0; i
< set
->n
; ++i
)
2673 isl_basic_set_free(set
->p
[i
]);
2674 isl_space_free(set
->dim
);
2680 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2685 fprintf(out
, "null set\n");
2689 fprintf(out
, "%*s", indent
, "");
2690 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2691 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2693 for (i
= 0; i
< set
->n
; ++i
) {
2694 fprintf(out
, "%*s", indent
, "");
2695 fprintf(out
, "basic set %d:\n", i
);
2696 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2700 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2705 fprintf(out
, "null map\n");
2709 fprintf(out
, "%*s", indent
, "");
2710 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2711 "flags: %x, n_name: %d\n",
2712 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2713 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2714 for (i
= 0; i
< map
->n
; ++i
) {
2715 fprintf(out
, "%*s", indent
, "");
2716 fprintf(out
, "basic map %d:\n", i
);
2717 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2721 struct isl_basic_map
*isl_basic_map_intersect_domain(
2722 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2724 struct isl_basic_map
*bmap_domain
;
2729 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2730 bset
->dim
, isl_dim_param
), goto error
);
2732 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2733 isl_assert(bset
->ctx
,
2734 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2736 bmap
= isl_basic_map_cow(bmap
);
2739 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2740 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2741 bmap_domain
= isl_basic_map_from_domain(bset
);
2742 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2744 bmap
= isl_basic_map_simplify(bmap
);
2745 return isl_basic_map_finalize(bmap
);
2747 isl_basic_map_free(bmap
);
2748 isl_basic_set_free(bset
);
2752 struct isl_basic_map
*isl_basic_map_intersect_range(
2753 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2755 struct isl_basic_map
*bmap_range
;
2760 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2761 bset
->dim
, isl_dim_param
), goto error
);
2763 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2764 isl_assert(bset
->ctx
,
2765 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2767 if (isl_basic_set_is_universe(bset
)) {
2768 isl_basic_set_free(bset
);
2772 bmap
= isl_basic_map_cow(bmap
);
2775 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2776 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2777 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2778 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2780 bmap
= isl_basic_map_simplify(bmap
);
2781 return isl_basic_map_finalize(bmap
);
2783 isl_basic_map_free(bmap
);
2784 isl_basic_set_free(bset
);
2788 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2797 total
= 1 + isl_basic_map_total_dim(bmap
);
2798 if (total
!= vec
->size
)
2803 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2804 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2805 if (!isl_int_is_zero(s
)) {
2811 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2812 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2813 if (isl_int_is_neg(s
)) {
2824 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2826 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2829 struct isl_basic_map
*isl_basic_map_intersect(
2830 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2832 struct isl_vec
*sample
= NULL
;
2834 if (!bmap1
|| !bmap2
)
2837 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2838 bmap2
->dim
, isl_dim_param
), goto error
);
2839 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2840 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2841 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2842 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2843 return isl_basic_map_intersect(bmap2
, bmap1
);
2845 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2846 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2847 isl_assert(bmap1
->ctx
,
2848 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2850 if (bmap1
->sample
&&
2851 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2852 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2853 sample
= isl_vec_copy(bmap1
->sample
);
2854 else if (bmap2
->sample
&&
2855 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2856 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2857 sample
= isl_vec_copy(bmap2
->sample
);
2859 bmap1
= isl_basic_map_cow(bmap1
);
2862 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2863 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2864 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2867 isl_vec_free(sample
);
2869 isl_vec_free(bmap1
->sample
);
2870 bmap1
->sample
= sample
;
2873 bmap1
= isl_basic_map_simplify(bmap1
);
2874 return isl_basic_map_finalize(bmap1
);
2877 isl_vec_free(sample
);
2878 isl_basic_map_free(bmap1
);
2879 isl_basic_map_free(bmap2
);
2883 struct isl_basic_set
*isl_basic_set_intersect(
2884 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2886 return (struct isl_basic_set
*)
2887 isl_basic_map_intersect(
2888 (struct isl_basic_map
*)bset1
,
2889 (struct isl_basic_map
*)bset2
);
2892 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2893 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2895 return isl_basic_set_intersect(bset1
, bset2
);
2898 /* Special case of isl_map_intersect, where both map1 and map2
2899 * are convex, without any divs and such that either map1 or map2
2900 * contains a single constraint. This constraint is then simply
2901 * added to the other map.
2903 static __isl_give isl_map
*map_intersect_add_constraint(
2904 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2906 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2907 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2908 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2909 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2911 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2912 return isl_map_intersect(map2
, map1
);
2914 isl_assert(map2
->ctx
,
2915 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2917 map1
= isl_map_cow(map1
);
2920 if (isl_map_plain_is_empty(map1
)) {
2924 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2925 if (map2
->p
[0]->n_eq
== 1)
2926 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2928 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2929 map2
->p
[0]->ineq
[0]);
2931 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2932 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2936 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2937 isl_basic_map_free(map1
->p
[0]);
2950 /* map2 may be either a parameter domain or a map living in the same
2953 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2954 __isl_take isl_map
*map2
)
2963 if ((isl_map_plain_is_empty(map1
) ||
2964 isl_map_plain_is_universe(map2
)) &&
2965 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2969 if ((isl_map_plain_is_empty(map2
) ||
2970 isl_map_plain_is_universe(map1
)) &&
2971 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2976 if (map1
->n
== 1 && map2
->n
== 1 &&
2977 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2978 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2979 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2980 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2981 return map_intersect_add_constraint(map1
, map2
);
2983 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2984 isl_space_dim(map2
->dim
, isl_dim_param
))
2985 isl_assert(map1
->ctx
,
2986 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2988 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2989 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2990 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2992 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2993 map1
->n
* map2
->n
, flags
);
2996 for (i
= 0; i
< map1
->n
; ++i
)
2997 for (j
= 0; j
< map2
->n
; ++j
) {
2998 struct isl_basic_map
*part
;
2999 part
= isl_basic_map_intersect(
3000 isl_basic_map_copy(map1
->p
[i
]),
3001 isl_basic_map_copy(map2
->p
[j
]));
3002 if (isl_basic_map_is_empty(part
) < 0)
3003 part
= isl_basic_map_free(part
);
3004 result
= isl_map_add_basic_map(result
, part
);
3017 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3018 __isl_take isl_map
*map2
)
3022 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3023 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3024 "spaces don't match", goto error
);
3025 return map_intersect_internal(map1
, map2
);
3032 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3033 __isl_take isl_map
*map2
)
3035 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3038 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3040 return (struct isl_set
*)
3041 isl_map_intersect((struct isl_map
*)set1
,
3042 (struct isl_map
*)set2
);
3045 /* map_intersect_internal accepts intersections
3046 * with parameter domains, so we can just call that function.
3048 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3049 __isl_take isl_set
*params
)
3051 return map_intersect_internal(map
, params
);
3054 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3055 __isl_take isl_map
*map2
)
3057 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3060 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3061 __isl_take isl_set
*params
)
3063 return isl_map_intersect_params(set
, params
);
3066 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3069 unsigned pos
, n1
, n2
;
3073 bmap
= isl_basic_map_cow(bmap
);
3076 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3077 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3078 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3079 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3080 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3081 return isl_basic_map_reset_space(bmap
, space
);
3084 static __isl_give isl_basic_map
*basic_map_space_reset(
3085 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3091 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3094 space
= isl_basic_map_get_space(bmap
);
3095 space
= isl_space_reset(space
, type
);
3096 bmap
= isl_basic_map_reset_space(bmap
, space
);
3100 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3101 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3102 unsigned pos
, unsigned n
)
3105 struct isl_basic_map
*res
;
3106 struct isl_dim_map
*dim_map
;
3107 unsigned total
, off
;
3108 enum isl_dim_type t
;
3111 return basic_map_space_reset(bmap
, type
);
3116 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3118 total
= isl_basic_map_total_dim(bmap
) + n
;
3119 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3121 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3123 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3125 unsigned size
= isl_basic_map_dim(bmap
, t
);
3126 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3128 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3129 pos
, size
- pos
, off
+ pos
+ n
);
3131 off
+= isl_space_dim(res_dim
, t
);
3133 isl_dim_map_div(dim_map
, bmap
, off
);
3135 res
= isl_basic_map_alloc_space(res_dim
,
3136 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3137 if (isl_basic_map_is_rational(bmap
))
3138 res
= isl_basic_map_set_rational(res
);
3139 if (isl_basic_map_plain_is_empty(bmap
)) {
3140 isl_basic_map_free(bmap
);
3142 return isl_basic_map_set_to_empty(res
);
3144 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3145 return isl_basic_map_finalize(res
);
3148 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3149 __isl_take isl_basic_set
*bset
,
3150 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3152 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3155 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3156 enum isl_dim_type type
, unsigned n
)
3160 return isl_basic_map_insert_dims(bmap
, type
,
3161 isl_basic_map_dim(bmap
, type
), n
);
3164 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3165 enum isl_dim_type type
, unsigned n
)
3169 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3170 return isl_basic_map_add_dims(bset
, type
, n
);
3172 isl_basic_set_free(bset
);
3176 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3177 enum isl_dim_type type
)
3181 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3184 space
= isl_map_get_space(map
);
3185 space
= isl_space_reset(space
, type
);
3186 map
= isl_map_reset_space(map
, space
);
3190 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3191 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3196 return map_space_reset(map
, type
);
3198 map
= isl_map_cow(map
);
3202 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3206 for (i
= 0; i
< map
->n
; ++i
) {
3207 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3218 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3219 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3221 return isl_map_insert_dims(set
, type
, pos
, n
);
3224 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3225 enum isl_dim_type type
, unsigned n
)
3229 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3232 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3233 enum isl_dim_type type
, unsigned n
)
3237 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3238 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3244 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3245 __isl_take isl_basic_map
*bmap
,
3246 enum isl_dim_type dst_type
, unsigned dst_pos
,
3247 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3249 struct isl_dim_map
*dim_map
;
3250 struct isl_basic_map
*res
;
3251 enum isl_dim_type t
;
3252 unsigned total
, off
;
3259 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3262 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3265 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3267 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3268 pos(bmap
->dim
, src_type
) + src_pos
+
3269 ((src_type
< dst_type
) ? n
: 0)) {
3270 bmap
= isl_basic_map_cow(bmap
);
3274 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3275 src_type
, src_pos
, n
);
3279 bmap
= isl_basic_map_finalize(bmap
);
3284 total
= isl_basic_map_total_dim(bmap
);
3285 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3288 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3289 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3290 if (t
== dst_type
) {
3291 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3294 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3297 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3298 dst_pos
, size
- dst_pos
, off
);
3299 off
+= size
- dst_pos
;
3300 } else if (t
== src_type
) {
3301 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3304 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3305 src_pos
+ n
, size
- src_pos
- n
, off
);
3306 off
+= size
- src_pos
- n
;
3308 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3312 isl_dim_map_div(dim_map
, bmap
, off
);
3314 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3315 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3316 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3320 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3321 src_type
, src_pos
, n
);
3325 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3326 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3327 bmap
= isl_basic_map_finalize(bmap
);
3331 isl_basic_map_free(bmap
);
3335 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3336 enum isl_dim_type dst_type
, unsigned dst_pos
,
3337 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3339 return (isl_basic_set
*)isl_basic_map_move_dims(
3340 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3343 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3344 enum isl_dim_type dst_type
, unsigned dst_pos
,
3345 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3349 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3350 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3351 src_type
, src_pos
, n
);
3357 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3358 enum isl_dim_type dst_type
, unsigned dst_pos
,
3359 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3368 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3371 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3374 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3376 map
= isl_map_cow(map
);
3380 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3384 for (i
= 0; i
< map
->n
; ++i
) {
3385 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3387 src_type
, src_pos
, n
);
3398 /* Move the specified dimensions to the last columns right before
3399 * the divs. Don't change the dimension specification of bmap.
3400 * That's the responsibility of the caller.
3402 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3403 enum isl_dim_type type
, unsigned first
, unsigned n
)
3405 struct isl_dim_map
*dim_map
;
3406 struct isl_basic_map
*res
;
3407 enum isl_dim_type t
;
3408 unsigned total
, off
;
3412 if (pos(bmap
->dim
, type
) + first
+ n
==
3413 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3416 total
= isl_basic_map_total_dim(bmap
);
3417 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3420 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3421 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3423 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3426 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3427 first
, n
, total
- bmap
->n_div
- n
);
3428 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3429 first
+ n
, size
- (first
+ n
), off
);
3430 off
+= size
- (first
+ n
);
3432 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3436 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3438 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3439 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3440 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3444 /* Insert "n" rows in the divs of "bmap".
3446 * The number of columns is not changed, which means that the last
3447 * dimensions of "bmap" are being reintepreted as the new divs.
3448 * The space of "bmap" is not adjusted, however, which means
3449 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3450 * from the space of "bmap" is the responsibility of the caller.
3452 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3460 bmap
= isl_basic_map_cow(bmap
);
3464 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3465 old
= bmap
->block2
.data
;
3466 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3467 (bmap
->extra
+ n
) * (1 + row_size
));
3468 if (!bmap
->block2
.data
)
3469 return isl_basic_map_free(bmap
);
3470 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3472 return isl_basic_map_free(bmap
);
3473 for (i
= 0; i
< n
; ++i
) {
3474 new_div
[i
] = bmap
->block2
.data
+
3475 (bmap
->extra
+ i
) * (1 + row_size
);
3476 isl_seq_clr(new_div
[i
], 1 + row_size
);
3478 for (i
= 0; i
< bmap
->extra
; ++i
)
3479 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3481 bmap
->div
= new_div
;
3488 /* Turn the n dimensions of type type, starting at first
3489 * into existentially quantified variables.
3491 __isl_give isl_basic_map
*isl_basic_map_project_out(
3492 __isl_take isl_basic_map
*bmap
,
3493 enum isl_dim_type type
, unsigned first
, unsigned n
)
3496 return basic_map_space_reset(bmap
, type
);
3501 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3502 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3504 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3507 bmap
= move_last(bmap
, type
, first
, n
);
3508 bmap
= isl_basic_map_cow(bmap
);
3509 bmap
= insert_div_rows(bmap
, n
);
3513 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3516 bmap
= isl_basic_map_simplify(bmap
);
3517 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3518 return isl_basic_map_finalize(bmap
);
3520 isl_basic_map_free(bmap
);
3524 /* Turn the n dimensions of type type, starting at first
3525 * into existentially quantified variables.
3527 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3528 enum isl_dim_type type
, unsigned first
, unsigned n
)
3530 return (isl_basic_set
*)isl_basic_map_project_out(
3531 (isl_basic_map
*)bset
, type
, first
, n
);
3534 /* Turn the n dimensions of type type, starting at first
3535 * into existentially quantified variables.
3537 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3538 enum isl_dim_type type
, unsigned first
, unsigned n
)
3546 return map_space_reset(map
, type
);
3548 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3550 map
= isl_map_cow(map
);
3554 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3558 for (i
= 0; i
< map
->n
; ++i
) {
3559 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3570 /* Turn the n dimensions of type type, starting at first
3571 * into existentially quantified variables.
3573 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3574 enum isl_dim_type type
, unsigned first
, unsigned n
)
3576 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3579 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3583 for (i
= 0; i
< n
; ++i
) {
3584 j
= isl_basic_map_alloc_div(bmap
);
3587 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3591 isl_basic_map_free(bmap
);
3595 struct isl_basic_map
*isl_basic_map_apply_range(
3596 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3598 isl_space
*dim_result
= NULL
;
3599 struct isl_basic_map
*bmap
;
3600 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3601 struct isl_dim_map
*dim_map1
, *dim_map2
;
3603 if (!bmap1
|| !bmap2
)
3605 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3606 bmap2
->dim
, isl_dim_param
))
3607 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3608 "parameters don't match", goto error
);
3609 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3610 bmap2
->dim
, isl_dim_in
))
3611 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3612 "spaces don't match", goto error
);
3614 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3615 isl_space_copy(bmap2
->dim
));
3617 n_in
= isl_basic_map_n_in(bmap1
);
3618 n_out
= isl_basic_map_n_out(bmap2
);
3619 n
= isl_basic_map_n_out(bmap1
);
3620 nparam
= isl_basic_map_n_param(bmap1
);
3622 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3623 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3624 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3625 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3626 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3627 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3628 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3629 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3630 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3631 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3632 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3634 bmap
= isl_basic_map_alloc_space(dim_result
,
3635 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3636 bmap1
->n_eq
+ bmap2
->n_eq
,
3637 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3638 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3639 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3640 bmap
= add_divs(bmap
, n
);
3641 bmap
= isl_basic_map_simplify(bmap
);
3642 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3643 return isl_basic_map_finalize(bmap
);
3645 isl_basic_map_free(bmap1
);
3646 isl_basic_map_free(bmap2
);
3650 struct isl_basic_set
*isl_basic_set_apply(
3651 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3656 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3659 return (struct isl_basic_set
*)
3660 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3662 isl_basic_set_free(bset
);
3663 isl_basic_map_free(bmap
);
3667 struct isl_basic_map
*isl_basic_map_apply_domain(
3668 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3670 if (!bmap1
|| !bmap2
)
3673 isl_assert(bmap1
->ctx
,
3674 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3675 isl_assert(bmap1
->ctx
,
3676 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3679 bmap1
= isl_basic_map_reverse(bmap1
);
3680 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3681 return isl_basic_map_reverse(bmap1
);
3683 isl_basic_map_free(bmap1
);
3684 isl_basic_map_free(bmap2
);
3688 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3689 * A \cap B -> f(A) + f(B)
3691 struct isl_basic_map
*isl_basic_map_sum(
3692 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3694 unsigned n_in
, n_out
, nparam
, total
, pos
;
3695 struct isl_basic_map
*bmap
= NULL
;
3696 struct isl_dim_map
*dim_map1
, *dim_map2
;
3699 if (!bmap1
|| !bmap2
)
3702 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3705 nparam
= isl_basic_map_n_param(bmap1
);
3706 n_in
= isl_basic_map_n_in(bmap1
);
3707 n_out
= isl_basic_map_n_out(bmap1
);
3709 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3710 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3711 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3712 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3713 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3714 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3715 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3716 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3717 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3718 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3719 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3721 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3722 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3723 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3724 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3725 for (i
= 0; i
< n_out
; ++i
) {
3726 int j
= isl_basic_map_alloc_equality(bmap
);
3729 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3730 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3731 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3732 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3734 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3735 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3736 bmap
= add_divs(bmap
, 2 * n_out
);
3738 bmap
= isl_basic_map_simplify(bmap
);
3739 return isl_basic_map_finalize(bmap
);
3741 isl_basic_map_free(bmap
);
3742 isl_basic_map_free(bmap1
);
3743 isl_basic_map_free(bmap2
);
3747 /* Given two maps A -> f(A) and B -> g(B), construct a map
3748 * A \cap B -> f(A) + f(B)
3750 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3752 struct isl_map
*result
;
3758 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3760 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3761 map1
->n
* map2
->n
, 0);
3764 for (i
= 0; i
< map1
->n
; ++i
)
3765 for (j
= 0; j
< map2
->n
; ++j
) {
3766 struct isl_basic_map
*part
;
3767 part
= isl_basic_map_sum(
3768 isl_basic_map_copy(map1
->p
[i
]),
3769 isl_basic_map_copy(map2
->p
[j
]));
3770 if (isl_basic_map_is_empty(part
))
3771 isl_basic_map_free(part
);
3773 result
= isl_map_add_basic_map(result
, part
);
3786 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3787 __isl_take isl_set
*set2
)
3789 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3792 /* Given a basic map A -> f(A), construct A -> -f(A).
3794 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3799 bmap
= isl_basic_map_cow(bmap
);
3803 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3804 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3805 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3806 for (j
= 0; j
< n
; ++j
)
3807 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3808 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3809 for (j
= 0; j
< n
; ++j
)
3810 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3811 for (i
= 0; i
< bmap
->n_div
; ++i
)
3812 for (j
= 0; j
< n
; ++j
)
3813 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3814 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3815 return isl_basic_map_finalize(bmap
);
3818 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3820 return isl_basic_map_neg(bset
);
3823 /* Given a map A -> f(A), construct A -> -f(A).
3825 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3829 map
= isl_map_cow(map
);
3833 for (i
= 0; i
< map
->n
; ++i
) {
3834 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3845 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3847 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3850 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3851 * A -> floor(f(A)/d).
3853 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3856 unsigned n_in
, n_out
, nparam
, total
, pos
;
3857 struct isl_basic_map
*result
= NULL
;
3858 struct isl_dim_map
*dim_map
;
3864 nparam
= isl_basic_map_n_param(bmap
);
3865 n_in
= isl_basic_map_n_in(bmap
);
3866 n_out
= isl_basic_map_n_out(bmap
);
3868 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3869 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3870 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3871 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3872 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3873 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3875 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3876 bmap
->n_div
+ n_out
,
3877 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3878 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3879 result
= add_divs(result
, n_out
);
3880 for (i
= 0; i
< n_out
; ++i
) {
3882 j
= isl_basic_map_alloc_inequality(result
);
3885 isl_seq_clr(result
->ineq
[j
], 1+total
);
3886 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3887 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3888 j
= isl_basic_map_alloc_inequality(result
);
3891 isl_seq_clr(result
->ineq
[j
], 1+total
);
3892 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3893 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3894 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3897 result
= isl_basic_map_simplify(result
);
3898 return isl_basic_map_finalize(result
);
3900 isl_basic_map_free(result
);
3904 /* Given a map A -> f(A) and an integer d, construct a map
3905 * A -> floor(f(A)/d).
3907 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3911 map
= isl_map_cow(map
);
3915 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3916 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3917 for (i
= 0; i
< map
->n
; ++i
) {
3918 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3929 /* Given a map A -> f(A) and an integer d, construct a map
3930 * A -> floor(f(A)/d).
3932 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3933 __isl_take isl_val
*d
)
3937 if (!isl_val_is_int(d
))
3938 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3939 "expecting integer denominator", goto error
);
3940 map
= isl_map_floordiv(map
, d
->n
);
3949 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3955 i
= isl_basic_map_alloc_equality(bmap
);
3958 nparam
= isl_basic_map_n_param(bmap
);
3959 n_in
= isl_basic_map_n_in(bmap
);
3960 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3961 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3962 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3963 return isl_basic_map_finalize(bmap
);
3965 isl_basic_map_free(bmap
);
3969 /* Add a constraints to "bmap" expressing i_pos < o_pos
3971 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3977 i
= isl_basic_map_alloc_inequality(bmap
);
3980 nparam
= isl_basic_map_n_param(bmap
);
3981 n_in
= isl_basic_map_n_in(bmap
);
3982 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3983 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3984 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3985 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3986 return isl_basic_map_finalize(bmap
);
3988 isl_basic_map_free(bmap
);
3992 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3994 static __isl_give isl_basic_map
*var_less_or_equal(
3995 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4001 i
= isl_basic_map_alloc_inequality(bmap
);
4004 nparam
= isl_basic_map_n_param(bmap
);
4005 n_in
= isl_basic_map_n_in(bmap
);
4006 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4007 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4008 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4009 return isl_basic_map_finalize(bmap
);
4011 isl_basic_map_free(bmap
);
4015 /* Add a constraints to "bmap" expressing i_pos > o_pos
4017 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4023 i
= isl_basic_map_alloc_inequality(bmap
);
4026 nparam
= isl_basic_map_n_param(bmap
);
4027 n_in
= isl_basic_map_n_in(bmap
);
4028 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4029 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4030 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4031 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4032 return isl_basic_map_finalize(bmap
);
4034 isl_basic_map_free(bmap
);
4038 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4040 static __isl_give isl_basic_map
*var_more_or_equal(
4041 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4047 i
= isl_basic_map_alloc_inequality(bmap
);
4050 nparam
= isl_basic_map_n_param(bmap
);
4051 n_in
= isl_basic_map_n_in(bmap
);
4052 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4053 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4054 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4055 return isl_basic_map_finalize(bmap
);
4057 isl_basic_map_free(bmap
);
4061 __isl_give isl_basic_map
*isl_basic_map_equal(
4062 __isl_take isl_space
*dim
, unsigned n_equal
)
4065 struct isl_basic_map
*bmap
;
4066 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4069 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4070 bmap
= var_equal(bmap
, i
);
4071 return isl_basic_map_finalize(bmap
);
4074 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4076 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4080 struct isl_basic_map
*bmap
;
4081 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4084 for (i
= 0; i
< pos
&& bmap
; ++i
)
4085 bmap
= var_equal(bmap
, i
);
4087 bmap
= var_less(bmap
, pos
);
4088 return isl_basic_map_finalize(bmap
);
4091 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4093 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4094 __isl_take isl_space
*dim
, unsigned pos
)
4097 isl_basic_map
*bmap
;
4099 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4100 for (i
= 0; i
< pos
; ++i
)
4101 bmap
= var_equal(bmap
, i
);
4102 bmap
= var_less_or_equal(bmap
, pos
);
4103 return isl_basic_map_finalize(bmap
);
4106 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4108 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4112 struct isl_basic_map
*bmap
;
4113 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4116 for (i
= 0; i
< pos
&& bmap
; ++i
)
4117 bmap
= var_equal(bmap
, i
);
4119 bmap
= var_more(bmap
, pos
);
4120 return isl_basic_map_finalize(bmap
);
4123 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4125 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4126 __isl_take isl_space
*dim
, unsigned pos
)
4129 isl_basic_map
*bmap
;
4131 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4132 for (i
= 0; i
< pos
; ++i
)
4133 bmap
= var_equal(bmap
, i
);
4134 bmap
= var_more_or_equal(bmap
, pos
);
4135 return isl_basic_map_finalize(bmap
);
4138 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4139 unsigned n
, int equal
)
4141 struct isl_map
*map
;
4144 if (n
== 0 && equal
)
4145 return isl_map_universe(dims
);
4147 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4149 for (i
= 0; i
+ 1 < n
; ++i
)
4150 map
= isl_map_add_basic_map(map
,
4151 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4154 map
= isl_map_add_basic_map(map
,
4155 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4157 map
= isl_map_add_basic_map(map
,
4158 isl_basic_map_less_at(dims
, n
- 1));
4160 isl_space_free(dims
);
4165 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4169 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4172 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4174 return map_lex_lte_first(dim
, n
, 0);
4177 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4179 return map_lex_lte_first(dim
, n
, 1);
4182 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4184 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4187 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4189 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4192 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4193 unsigned n
, int equal
)
4195 struct isl_map
*map
;
4198 if (n
== 0 && equal
)
4199 return isl_map_universe(dims
);
4201 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4203 for (i
= 0; i
+ 1 < n
; ++i
)
4204 map
= isl_map_add_basic_map(map
,
4205 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4208 map
= isl_map_add_basic_map(map
,
4209 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4211 map
= isl_map_add_basic_map(map
,
4212 isl_basic_map_more_at(dims
, n
- 1));
4214 isl_space_free(dims
);
4219 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4223 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4226 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4228 return map_lex_gte_first(dim
, n
, 0);
4231 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4233 return map_lex_gte_first(dim
, n
, 1);
4236 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4238 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4241 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4243 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4246 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4247 __isl_take isl_set
*set2
)
4250 map
= isl_map_lex_le(isl_set_get_space(set1
));
4251 map
= isl_map_intersect_domain(map
, set1
);
4252 map
= isl_map_intersect_range(map
, set2
);
4256 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4257 __isl_take isl_set
*set2
)
4260 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4261 map
= isl_map_intersect_domain(map
, set1
);
4262 map
= isl_map_intersect_range(map
, set2
);
4266 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4267 __isl_take isl_set
*set2
)
4270 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4271 map
= isl_map_intersect_domain(map
, set1
);
4272 map
= isl_map_intersect_range(map
, set2
);
4276 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4277 __isl_take isl_set
*set2
)
4280 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4281 map
= isl_map_intersect_domain(map
, set1
);
4282 map
= isl_map_intersect_range(map
, set2
);
4286 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4287 __isl_take isl_map
*map2
)
4290 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4291 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4292 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4296 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4297 __isl_take isl_map
*map2
)
4300 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4301 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4302 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4306 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4307 __isl_take isl_map
*map2
)
4310 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4311 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4312 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4316 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4317 __isl_take isl_map
*map2
)
4320 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4321 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4322 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4326 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4327 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4329 struct isl_basic_map
*bmap
;
4331 bset
= isl_basic_set_cow(bset
);
4335 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4336 isl_space_free(bset
->dim
);
4337 bmap
= (struct isl_basic_map
*) bset
;
4339 return isl_basic_map_finalize(bmap
);
4341 isl_basic_set_free(bset
);
4342 isl_space_free(dim
);
4346 /* For a div d = floor(f/m), add the constraint
4350 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4351 unsigned pos
, isl_int
*div
)
4354 unsigned total
= isl_basic_map_total_dim(bmap
);
4356 i
= isl_basic_map_alloc_inequality(bmap
);
4359 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4360 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4365 /* For a div d = floor(f/m), add the constraint
4367 * -(f-(n-1)) + m d >= 0
4369 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4370 unsigned pos
, isl_int
*div
)
4373 unsigned total
= isl_basic_map_total_dim(bmap
);
4375 i
= isl_basic_map_alloc_inequality(bmap
);
4378 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4379 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4380 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4381 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4386 /* For a div d = floor(f/m), add the constraints
4389 * -(f-(n-1)) + m d >= 0
4391 * Note that the second constraint is the negation of
4395 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4396 unsigned pos
, isl_int
*div
)
4398 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4400 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4405 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4406 unsigned pos
, isl_int
*div
)
4408 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4412 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4414 unsigned total
= isl_basic_map_total_dim(bmap
);
4415 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4417 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4421 /* For each known div d = floor(f/m), add the constraints
4424 * -(f-(n-1)) + m d >= 0
4426 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4427 __isl_take isl_basic_map
*bmap
)
4434 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4437 bmap
= isl_basic_map_cow(bmap
);
4438 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4441 for (i
= 0; i
< n_div
; ++i
) {
4442 if (isl_int_is_zero(bmap
->div
[i
][0]))
4444 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4445 return isl_basic_map_free(bmap
);
4448 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4449 bmap
= isl_basic_map_finalize(bmap
);
4453 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4455 * In particular, if this div is of the form d = floor(f/m),
4456 * then add the constraint
4460 * if sign < 0 or the constraint
4462 * -(f-(n-1)) + m d >= 0
4466 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4467 unsigned div
, int sign
)
4475 total
= isl_basic_map_total_dim(bmap
);
4476 div_pos
= total
- bmap
->n_div
+ div
;
4479 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4481 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4484 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4486 return isl_basic_map_add_div_constraints(bset
, div
);
4489 struct isl_basic_set
*isl_basic_map_underlying_set(
4490 struct isl_basic_map
*bmap
)
4494 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4496 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4497 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4498 return (struct isl_basic_set
*)bmap
;
4499 bmap
= isl_basic_map_cow(bmap
);
4502 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4505 bmap
->extra
-= bmap
->n_div
;
4507 bmap
= isl_basic_map_finalize(bmap
);
4508 return (struct isl_basic_set
*)bmap
;
4510 isl_basic_map_free(bmap
);
4514 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4515 __isl_take isl_basic_set
*bset
)
4517 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4520 /* Replace each element in "list" by the result of applying
4521 * isl_basic_map_underlying_set to the element.
4523 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4524 __isl_take isl_basic_map_list
*list
)
4531 n
= isl_basic_map_list_n_basic_map(list
);
4532 for (i
= 0; i
< n
; ++i
) {
4533 isl_basic_map
*bmap
;
4534 isl_basic_set
*bset
;
4536 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4537 bset
= isl_basic_set_underlying_set(bmap
);
4538 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4544 struct isl_basic_map
*isl_basic_map_overlying_set(
4545 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4547 struct isl_basic_map
*bmap
;
4548 struct isl_ctx
*ctx
;
4555 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4556 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4557 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4559 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4560 isl_basic_map_free(like
);
4561 return (struct isl_basic_map
*)bset
;
4563 bset
= isl_basic_set_cow(bset
);
4566 total
= bset
->dim
->n_out
+ bset
->extra
;
4567 bmap
= (struct isl_basic_map
*)bset
;
4568 isl_space_free(bmap
->dim
);
4569 bmap
->dim
= isl_space_copy(like
->dim
);
4572 bmap
->n_div
= like
->n_div
;
4573 bmap
->extra
+= like
->n_div
;
4577 ltotal
= total
- bmap
->extra
+ like
->extra
;
4580 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4581 bmap
->extra
* (1 + 1 + total
));
4582 if (isl_blk_is_error(bmap
->block2
))
4584 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4588 for (i
= 0; i
< bmap
->extra
; ++i
)
4589 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4590 for (i
= 0; i
< like
->n_div
; ++i
) {
4591 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4592 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4594 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4596 isl_basic_map_free(like
);
4597 bmap
= isl_basic_map_simplify(bmap
);
4598 bmap
= isl_basic_map_finalize(bmap
);
4601 isl_basic_map_free(like
);
4602 isl_basic_set_free(bset
);
4606 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4607 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4609 return (struct isl_basic_set
*)
4610 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4613 struct isl_set
*isl_set_from_underlying_set(
4614 struct isl_set
*set
, struct isl_basic_set
*like
)
4620 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4622 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4623 isl_basic_set_free(like
);
4626 set
= isl_set_cow(set
);
4629 for (i
= 0; i
< set
->n
; ++i
) {
4630 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4631 isl_basic_set_copy(like
));
4635 isl_space_free(set
->dim
);
4636 set
->dim
= isl_space_copy(like
->dim
);
4639 isl_basic_set_free(like
);
4642 isl_basic_set_free(like
);
4647 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4651 map
= isl_map_cow(map
);
4654 map
->dim
= isl_space_cow(map
->dim
);
4658 for (i
= 1; i
< map
->n
; ++i
)
4659 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4661 for (i
= 0; i
< map
->n
; ++i
) {
4662 map
->p
[i
] = (struct isl_basic_map
*)
4663 isl_basic_map_underlying_set(map
->p
[i
]);
4668 map
->dim
= isl_space_underlying(map
->dim
, 0);
4670 isl_space_free(map
->dim
);
4671 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4675 return (struct isl_set
*)map
;
4681 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4683 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4686 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4687 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4689 bmap
= isl_basic_map_cow(bmap
);
4693 isl_space_free(bmap
->dim
);
4696 bmap
= isl_basic_map_finalize(bmap
);
4700 isl_basic_map_free(bmap
);
4701 isl_space_free(dim
);
4705 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4706 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4708 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4712 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4713 __isl_take isl_space
*dim
)
4717 map
= isl_map_cow(map
);
4721 for (i
= 0; i
< map
->n
; ++i
) {
4722 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4723 isl_space_copy(dim
));
4727 isl_space_free(map
->dim
);
4733 isl_space_free(dim
);
4737 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4738 __isl_take isl_space
*dim
)
4740 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4743 /* Compute the parameter domain of the given basic set.
4745 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4750 if (isl_basic_set_is_params(bset
))
4753 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4754 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4755 space
= isl_basic_set_get_space(bset
);
4756 space
= isl_space_params(space
);
4757 bset
= isl_basic_set_reset_space(bset
, space
);
4761 /* Construct a zero-dimensional basic set with the given parameter domain.
4763 __isl_give isl_basic_set
*isl_basic_set_from_params(
4764 __isl_take isl_basic_set
*bset
)
4767 space
= isl_basic_set_get_space(bset
);
4768 space
= isl_space_set_from_params(space
);
4769 bset
= isl_basic_set_reset_space(bset
, space
);
4773 /* Compute the parameter domain of the given set.
4775 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4780 if (isl_set_is_params(set
))
4783 n
= isl_set_dim(set
, isl_dim_set
);
4784 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4785 space
= isl_set_get_space(set
);
4786 space
= isl_space_params(space
);
4787 set
= isl_set_reset_space(set
, space
);
4791 /* Construct a zero-dimensional set with the given parameter domain.
4793 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4796 space
= isl_set_get_space(set
);
4797 space
= isl_space_set_from_params(space
);
4798 set
= isl_set_reset_space(set
, space
);
4802 /* Compute the parameter domain of the given map.
4804 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4809 n
= isl_map_dim(map
, isl_dim_in
);
4810 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4811 n
= isl_map_dim(map
, isl_dim_out
);
4812 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4813 space
= isl_map_get_space(map
);
4814 space
= isl_space_params(space
);
4815 map
= isl_map_reset_space(map
, space
);
4819 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4826 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
4828 n_out
= isl_basic_map_n_out(bmap
);
4829 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
4831 return isl_basic_map_reset_space(bmap
, space
);
4834 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4838 return isl_space_may_be_set(bmap
->dim
);
4841 /* Is this basic map actually a set?
4842 * Users should never call this function. Outside of isl,
4843 * the type should indicate whether something is a set or a map.
4845 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4849 return isl_space_is_set(bmap
->dim
);
4852 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4856 if (isl_basic_map_is_set(bmap
))
4858 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4861 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4862 __isl_take isl_basic_map
*bmap
)
4866 isl_basic_map
*domain
;
4867 int nparam
, n_in
, n_out
;
4870 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4871 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4872 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4874 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4875 domain
= isl_basic_map_universe(dim
);
4877 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4878 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4879 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4881 total
= isl_basic_map_total_dim(bmap
);
4883 for (i
= 0; i
< n_in
; ++i
) {
4884 k
= isl_basic_map_alloc_equality(bmap
);
4887 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4888 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4889 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4892 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4893 return isl_basic_map_finalize(bmap
);
4895 isl_basic_map_free(bmap
);
4899 __isl_give isl_basic_map
*isl_basic_map_range_map(
4900 __isl_take isl_basic_map
*bmap
)
4904 isl_basic_map
*range
;
4905 int nparam
, n_in
, n_out
;
4908 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4909 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4910 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4912 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4913 range
= isl_basic_map_universe(dim
);
4915 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4916 bmap
= isl_basic_map_apply_range(bmap
, range
);
4917 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4919 total
= isl_basic_map_total_dim(bmap
);
4921 for (i
= 0; i
< n_out
; ++i
) {
4922 k
= isl_basic_map_alloc_equality(bmap
);
4925 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4926 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4927 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4930 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4931 return isl_basic_map_finalize(bmap
);
4933 isl_basic_map_free(bmap
);
4937 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4941 return isl_space_may_be_set(map
->dim
);
4944 /* Is this map actually a set?
4945 * Users should never call this function. Outside of isl,
4946 * the type should indicate whether something is a set or a map.
4948 int isl_map_is_set(__isl_keep isl_map
*map
)
4952 return isl_space_is_set(map
->dim
);
4955 struct isl_set
*isl_map_range(struct isl_map
*map
)
4958 struct isl_set
*set
;
4962 if (isl_map_is_set(map
))
4963 return (isl_set
*)map
;
4965 map
= isl_map_cow(map
);
4969 set
= (struct isl_set
*) map
;
4970 set
->dim
= isl_space_range(set
->dim
);
4973 for (i
= 0; i
< map
->n
; ++i
) {
4974 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4978 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4979 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4986 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4990 map
= isl_map_cow(map
);
4994 map
->dim
= isl_space_domain_map(map
->dim
);
4997 for (i
= 0; i
< map
->n
; ++i
) {
4998 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5002 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5003 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5010 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5013 isl_space
*range_dim
;
5015 map
= isl_map_cow(map
);
5019 range_dim
= isl_space_range(isl_map_get_space(map
));
5020 range_dim
= isl_space_from_range(range_dim
);
5021 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5022 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5025 for (i
= 0; i
< map
->n
; ++i
) {
5026 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5030 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5031 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5038 /* Given a wrapped map of the form A[B -> C],
5039 * return the map A[B -> C] -> B.
5041 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5048 if (!isl_set_has_tuple_id(set
))
5049 return isl_map_domain_map(isl_set_unwrap(set
));
5051 id
= isl_set_get_tuple_id(set
);
5052 map
= isl_map_domain_map(isl_set_unwrap(set
));
5053 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5058 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5059 __isl_take isl_space
*dim
)
5062 struct isl_map
*map
= NULL
;
5064 set
= isl_set_cow(set
);
5067 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5068 map
= (struct isl_map
*)set
;
5069 for (i
= 0; i
< set
->n
; ++i
) {
5070 map
->p
[i
] = isl_basic_map_from_basic_set(
5071 set
->p
[i
], isl_space_copy(dim
));
5075 isl_space_free(map
->dim
);
5079 isl_space_free(dim
);
5084 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5085 __isl_take isl_basic_set
*bset
)
5087 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5090 __isl_give isl_basic_map
*isl_basic_map_from_range(
5091 __isl_take isl_basic_set
*bset
)
5094 space
= isl_basic_set_get_space(bset
);
5095 space
= isl_space_from_range(space
);
5096 bset
= isl_basic_set_reset_space(bset
, space
);
5097 return (isl_basic_map
*)bset
;
5100 /* Create a relation with the given set as range.
5101 * The domain of the created relation is a zero-dimensional
5102 * flat anonymous space.
5104 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5107 space
= isl_set_get_space(set
);
5108 space
= isl_space_from_range(space
);
5109 set
= isl_set_reset_space(set
, space
);
5110 return (struct isl_map
*)set
;
5113 /* Create a relation with the given set as domain.
5114 * The range of the created relation is a zero-dimensional
5115 * flat anonymous space.
5117 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5119 return isl_map_reverse(isl_map_from_range(set
));
5122 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5123 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5125 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5128 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5129 __isl_take isl_set
*range
)
5131 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5134 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5137 struct isl_map
*map
;
5142 isl_die(dim
->ctx
, isl_error_internal
,
5143 "negative number of basic maps", goto error
);
5144 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5145 sizeof(struct isl_map
) +
5146 (n
- 1) * sizeof(struct isl_basic_map
*));
5150 map
->ctx
= dim
->ctx
;
5151 isl_ctx_ref(map
->ctx
);
5159 isl_space_free(dim
);
5163 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5164 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5167 struct isl_map
*map
;
5170 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5174 map
= isl_map_alloc_space(dims
, n
, flags
);
5178 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5180 struct isl_basic_map
*bmap
;
5181 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5182 bmap
= isl_basic_map_set_to_empty(bmap
);
5186 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5188 struct isl_basic_set
*bset
;
5189 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5190 bset
= isl_basic_set_set_to_empty(bset
);
5194 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5196 struct isl_basic_map
*bmap
;
5197 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5198 bmap
= isl_basic_map_finalize(bmap
);
5202 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5204 struct isl_basic_set
*bset
;
5205 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5206 bset
= isl_basic_set_finalize(bset
);
5210 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5213 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5214 isl_basic_map
*bmap
;
5216 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5217 for (i
= 0; i
< total
; ++i
) {
5218 int k
= isl_basic_map_alloc_inequality(bmap
);
5221 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5222 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5226 isl_basic_map_free(bmap
);
5230 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5232 return isl_basic_map_nat_universe(dim
);
5235 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5237 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5240 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5242 return isl_map_nat_universe(dim
);
5245 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5247 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5250 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5252 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5255 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5257 struct isl_map
*map
;
5260 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5261 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5265 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5267 struct isl_set
*set
;
5270 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5271 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5275 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5278 struct isl_map
*dup
;
5282 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5283 for (i
= 0; i
< map
->n
; ++i
)
5284 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5288 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5289 __isl_take isl_basic_map
*bmap
)
5293 if (isl_basic_map_plain_is_empty(bmap
)) {
5294 isl_basic_map_free(bmap
);
5297 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5298 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5299 map
->p
[map
->n
] = bmap
;
5301 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5307 isl_basic_map_free(bmap
);
5311 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5321 isl_ctx_deref(map
->ctx
);
5322 for (i
= 0; i
< map
->n
; ++i
)
5323 isl_basic_map_free(map
->p
[i
]);
5324 isl_space_free(map
->dim
);
5330 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5331 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5335 bmap
= isl_basic_map_cow(bmap
);
5336 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5337 j
= isl_basic_map_alloc_equality(bmap
);
5340 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5341 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5342 isl_int_set_si(bmap
->eq
[j
][0], value
);
5343 bmap
= isl_basic_map_simplify(bmap
);
5344 return isl_basic_map_finalize(bmap
);
5346 isl_basic_map_free(bmap
);
5350 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5351 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5355 bmap
= isl_basic_map_cow(bmap
);
5356 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5357 j
= isl_basic_map_alloc_equality(bmap
);
5360 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5361 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5362 isl_int_set(bmap
->eq
[j
][0], value
);
5363 bmap
= isl_basic_map_simplify(bmap
);
5364 return isl_basic_map_finalize(bmap
);
5366 isl_basic_map_free(bmap
);
5370 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5371 enum isl_dim_type type
, unsigned pos
, int value
)
5375 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5376 return isl_basic_map_fix_pos_si(bmap
,
5377 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5379 isl_basic_map_free(bmap
);
5383 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5384 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5388 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5389 return isl_basic_map_fix_pos(bmap
,
5390 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5392 isl_basic_map_free(bmap
);
5396 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5397 * to be equal to "v".
5399 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5400 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5404 if (!isl_val_is_int(v
))
5405 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5406 "expecting integer value", goto error
);
5407 if (pos
>= isl_basic_map_dim(bmap
, type
))
5408 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5409 "index out of bounds", goto error
);
5410 pos
+= isl_basic_map_offset(bmap
, type
);
5411 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5415 isl_basic_map_free(bmap
);
5420 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5421 * to be equal to "v".
5423 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5424 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5426 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5429 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5430 enum isl_dim_type type
, unsigned pos
, int value
)
5432 return (struct isl_basic_set
*)
5433 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5437 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5438 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5440 return (struct isl_basic_set
*)
5441 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5445 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5446 unsigned input
, int value
)
5448 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5451 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5452 unsigned dim
, int value
)
5454 return (struct isl_basic_set
*)
5455 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5456 isl_dim_set
, dim
, value
);
5459 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5461 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5468 isl_basic_map_free(map
->p
[i
]);
5469 if (i
!= map
->n
- 1) {
5470 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5471 map
->p
[i
] = map
->p
[map
->n
- 1];
5478 /* Perform "fn" on each basic map of "map", where we may not be holding
5479 * the only reference to "map".
5480 * In particular, "fn" should be a semantics preserving operation
5481 * that we want to apply to all copies of "map". We therefore need
5482 * to be careful not to modify "map" in a way that breaks "map"
5483 * in case anything goes wrong.
5485 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5486 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5488 struct isl_basic_map
*bmap
;
5494 for (i
= map
->n
- 1; i
>= 0; --i
) {
5495 bmap
= isl_basic_map_copy(map
->p
[i
]);
5499 isl_basic_map_free(map
->p
[i
]);
5501 if (remove_if_empty(map
, i
) < 0)
5511 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5512 enum isl_dim_type type
, unsigned pos
, int value
)
5516 map
= isl_map_cow(map
);
5520 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5521 for (i
= map
->n
- 1; i
>= 0; --i
) {
5522 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5523 if (remove_if_empty(map
, i
) < 0)
5526 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5533 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5534 enum isl_dim_type type
, unsigned pos
, int value
)
5536 return (struct isl_set
*)
5537 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5540 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5541 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5545 map
= isl_map_cow(map
);
5549 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5550 for (i
= 0; i
< map
->n
; ++i
) {
5551 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5555 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5562 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5563 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5565 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5568 /* Fix the value of the variable at position "pos" of type "type" of "map"
5569 * to be equal to "v".
5571 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5572 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5576 map
= isl_map_cow(map
);
5580 if (!isl_val_is_int(v
))
5581 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5582 "expecting integer value", goto error
);
5583 if (pos
>= isl_map_dim(map
, type
))
5584 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5585 "index out of bounds", goto error
);
5586 for (i
= map
->n
- 1; i
>= 0; --i
) {
5587 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5589 if (remove_if_empty(map
, i
) < 0)
5592 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5601 /* Fix the value of the variable at position "pos" of type "type" of "set"
5602 * to be equal to "v".
5604 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5605 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5607 return isl_map_fix_val(set
, type
, pos
, v
);
5610 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5611 unsigned input
, int value
)
5613 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5616 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5618 return (struct isl_set
*)
5619 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5622 static __isl_give isl_basic_map
*basic_map_bound_si(
5623 __isl_take isl_basic_map
*bmap
,
5624 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5630 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5631 pos
+= isl_basic_map_offset(bmap
, type
);
5632 bmap
= isl_basic_map_cow(bmap
);
5633 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5634 j
= isl_basic_map_alloc_inequality(bmap
);
5637 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5639 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5640 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5642 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5643 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5645 bmap
= isl_basic_map_simplify(bmap
);
5646 return isl_basic_map_finalize(bmap
);
5648 isl_basic_map_free(bmap
);
5652 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5653 __isl_take isl_basic_map
*bmap
,
5654 enum isl_dim_type type
, unsigned pos
, int value
)
5656 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5659 /* Constrain the values of the given dimension to be no greater than "value".
5661 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5662 __isl_take isl_basic_map
*bmap
,
5663 enum isl_dim_type type
, unsigned pos
, int value
)
5665 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5668 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5669 unsigned dim
, isl_int value
)
5673 bset
= isl_basic_set_cow(bset
);
5674 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5675 j
= isl_basic_set_alloc_inequality(bset
);
5678 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5679 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5680 isl_int_neg(bset
->ineq
[j
][0], value
);
5681 bset
= isl_basic_set_simplify(bset
);
5682 return isl_basic_set_finalize(bset
);
5684 isl_basic_set_free(bset
);
5688 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5689 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5693 map
= isl_map_cow(map
);
5697 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5698 for (i
= 0; i
< map
->n
; ++i
) {
5699 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5700 type
, pos
, value
, upper
);
5704 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5711 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5712 enum isl_dim_type type
, unsigned pos
, int value
)
5714 return map_bound_si(map
, type
, pos
, value
, 0);
5717 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5718 enum isl_dim_type type
, unsigned pos
, int value
)
5720 return map_bound_si(map
, type
, pos
, value
, 1);
5723 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5724 enum isl_dim_type type
, unsigned pos
, int value
)
5726 return (struct isl_set
*)
5727 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5730 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5731 enum isl_dim_type type
, unsigned pos
, int value
)
5733 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5736 /* Bound the given variable of "bmap" from below (or above is "upper"
5737 * is set) to "value".
5739 static __isl_give isl_basic_map
*basic_map_bound(
5740 __isl_take isl_basic_map
*bmap
,
5741 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5747 if (pos
>= isl_basic_map_dim(bmap
, type
))
5748 isl_die(bmap
->ctx
, isl_error_invalid
,
5749 "index out of bounds", goto error
);
5750 pos
+= isl_basic_map_offset(bmap
, type
);
5751 bmap
= isl_basic_map_cow(bmap
);
5752 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5753 j
= isl_basic_map_alloc_inequality(bmap
);
5756 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5758 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5759 isl_int_set(bmap
->ineq
[j
][0], value
);
5761 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5762 isl_int_neg(bmap
->ineq
[j
][0], value
);
5764 bmap
= isl_basic_map_simplify(bmap
);
5765 return isl_basic_map_finalize(bmap
);
5767 isl_basic_map_free(bmap
);
5771 /* Bound the given variable of "map" from below (or above is "upper"
5772 * is set) to "value".
5774 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5775 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5779 map
= isl_map_cow(map
);
5783 if (pos
>= isl_map_dim(map
, type
))
5784 isl_die(map
->ctx
, isl_error_invalid
,
5785 "index out of bounds", goto error
);
5786 for (i
= map
->n
- 1; i
>= 0; --i
) {
5787 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5788 if (remove_if_empty(map
, i
) < 0)
5791 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5798 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5799 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5801 return map_bound(map
, type
, pos
, value
, 0);
5804 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5805 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5807 return map_bound(map
, type
, pos
, value
, 1);
5810 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5811 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5813 return isl_map_lower_bound(set
, type
, pos
, value
);
5816 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5817 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5819 return isl_map_upper_bound(set
, type
, pos
, value
);
5822 /* Force the values of the variable at position "pos" of type "type" of "set"
5823 * to be no smaller than "value".
5825 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5826 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5830 if (!isl_val_is_int(value
))
5831 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5832 "expecting integer value", goto error
);
5833 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5834 isl_val_free(value
);
5837 isl_val_free(value
);
5842 /* Force the values of the variable at position "pos" of type "type" of "set"
5843 * to be no greater than "value".
5845 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5846 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5850 if (!isl_val_is_int(value
))
5851 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5852 "expecting integer value", goto error
);
5853 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5854 isl_val_free(value
);
5857 isl_val_free(value
);
5862 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5867 set
= isl_set_cow(set
);
5871 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5872 for (i
= 0; i
< set
->n
; ++i
) {
5873 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5883 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5887 map
= isl_map_cow(map
);
5891 map
->dim
= isl_space_reverse(map
->dim
);
5894 for (i
= 0; i
< map
->n
; ++i
) {
5895 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5899 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5906 static struct isl_map
*isl_basic_map_partial_lexopt(
5907 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5908 struct isl_set
**empty
, int max
)
5910 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5913 struct isl_map
*isl_basic_map_partial_lexmax(
5914 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5915 struct isl_set
**empty
)
5917 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5920 struct isl_map
*isl_basic_map_partial_lexmin(
5921 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5922 struct isl_set
**empty
)
5924 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5927 struct isl_set
*isl_basic_set_partial_lexmin(
5928 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5929 struct isl_set
**empty
)
5931 return (struct isl_set
*)
5932 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5936 struct isl_set
*isl_basic_set_partial_lexmax(
5937 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5938 struct isl_set
**empty
)
5940 return (struct isl_set
*)
5941 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5945 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5946 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5947 __isl_give isl_set
**empty
)
5949 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5952 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5953 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5954 __isl_give isl_set
**empty
)
5956 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5959 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5960 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5961 __isl_give isl_set
**empty
)
5963 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5966 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5967 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5968 __isl_give isl_set
**empty
)
5970 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5973 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5974 __isl_take isl_basic_map
*bmap
, int max
)
5976 isl_basic_set
*dom
= NULL
;
5977 isl_space
*dom_space
;
5981 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5982 dom
= isl_basic_set_universe(dom_space
);
5983 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5985 isl_basic_map_free(bmap
);
5989 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5990 __isl_take isl_basic_map
*bmap
)
5992 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5996 #define TYPE isl_pw_multi_aff
5998 #define SUFFIX _pw_multi_aff
6000 #define EMPTY isl_pw_multi_aff_empty
6002 #define ADD isl_pw_multi_aff_union_add
6003 #include "isl_map_lexopt_templ.c"
6005 /* Given a map "map", compute the lexicographically minimal
6006 * (or maximal) image element for each domain element in dom,
6007 * in the form of an isl_pw_multi_aff.
6008 * Set *empty to those elements in dom that do not have an image element.
6010 * We first compute the lexicographically minimal or maximal element
6011 * in the first basic map. This results in a partial solution "res"
6012 * and a subset "todo" of dom that still need to be handled.
6013 * We then consider each of the remaining maps in "map" and successively
6014 * update both "res" and "todo".
6016 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6017 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6018 __isl_give isl_set
**empty
, int max
)
6021 isl_pw_multi_aff
*res
;
6027 if (isl_map_plain_is_empty(map
)) {
6032 return isl_pw_multi_aff_from_map(map
);
6035 res
= basic_map_partial_lexopt_pw_multi_aff(
6036 isl_basic_map_copy(map
->p
[0]),
6037 isl_set_copy(dom
), &todo
, max
);
6039 for (i
= 1; i
< map
->n
; ++i
) {
6040 isl_pw_multi_aff
*res_i
;
6043 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6044 isl_basic_map_copy(map
->p
[i
]),
6045 isl_set_copy(dom
), &todo_i
, max
);
6048 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6050 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6052 todo
= isl_set_intersect(todo
, todo_i
);
6073 #define TYPE isl_map
6077 #define EMPTY isl_map_empty
6079 #define ADD isl_map_union_disjoint
6080 #include "isl_map_lexopt_templ.c"
6082 /* Given a map "map", compute the lexicographically minimal
6083 * (or maximal) image element for each domain element in dom.
6084 * Set *empty to those elements in dom that do not have an image element.
6086 * We first compute the lexicographically minimal or maximal element
6087 * in the first basic map. This results in a partial solution "res"
6088 * and a subset "todo" of dom that still need to be handled.
6089 * We then consider each of the remaining maps in "map" and successively
6090 * update both "res" and "todo".
6092 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6093 * Assume we are computing the lexicographical maximum.
6094 * We first compute the lexicographically maximal element in basic map i.
6095 * This results in a partial solution res_i and a subset todo_i.
6096 * Then we combine these results with those obtain for the first k basic maps
6097 * to obtain a result that is valid for the first k+1 basic maps.
6098 * In particular, the set where there is no solution is the set where
6099 * there is no solution for the first k basic maps and also no solution
6100 * for the ith basic map, i.e.,
6102 * todo^i = todo^k * todo_i
6104 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6105 * solutions, arbitrarily breaking ties in favor of res^k.
6106 * That is, when res^k(a) >= res_i(a), we pick res^k and
6107 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6108 * the lexicographic order.)
6109 * In practice, we compute
6111 * res^k * (res_i . "<=")
6115 * res_i * (res^k . "<")
6117 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6118 * where only one of res^k and res_i provides a solution and we simply pick
6125 * Note that we only compute these intersections when dom(res^k) intersects
6126 * dom(res_i). Otherwise, the only effect of these intersections is to
6127 * potentially break up res^k and res_i into smaller pieces.
6128 * We want to avoid such splintering as much as possible.
6129 * In fact, an earlier implementation of this function would look for
6130 * better results in the domain of res^k and for extra results in todo^k,
6131 * but this would always result in a splintering according to todo^k,
6132 * even when the domain of basic map i is disjoint from the domains of
6133 * the previous basic maps.
6135 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6136 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6137 __isl_give isl_set
**empty
, int max
)
6140 struct isl_map
*res
;
6141 struct isl_set
*todo
;
6146 if (isl_map_plain_is_empty(map
)) {
6154 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6155 isl_set_copy(dom
), &todo
, max
);
6157 for (i
= 1; i
< map
->n
; ++i
) {
6161 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6163 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6164 isl_set_copy(dom
), &todo_i
, max
);
6167 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6168 le
= isl_map_lex_le(dim
);
6170 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6171 le
= isl_map_lex_ge(dim
);
6173 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6174 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6175 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6176 le
= isl_map_intersect(le
, isl_map_copy(res
));
6178 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6179 res
= isl_map_intersect_domain(res
,
6180 isl_set_copy(todo_i
));
6181 res_i
= isl_map_intersect_domain(res_i
,
6182 isl_set_copy(todo
));
6185 res
= isl_map_union_disjoint(res
, res_i
);
6186 res
= isl_map_union_disjoint(res
, lt
);
6187 res
= isl_map_union_disjoint(res
, le
);
6189 todo
= isl_set_intersect(todo
, todo_i
);
6209 __isl_give isl_map
*isl_map_partial_lexmax(
6210 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6211 __isl_give isl_set
**empty
)
6213 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6216 __isl_give isl_map
*isl_map_partial_lexmin(
6217 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6218 __isl_give isl_set
**empty
)
6220 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6223 __isl_give isl_set
*isl_set_partial_lexmin(
6224 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6225 __isl_give isl_set
**empty
)
6227 return (struct isl_set
*)
6228 isl_map_partial_lexmin((struct isl_map
*)set
,
6232 __isl_give isl_set
*isl_set_partial_lexmax(
6233 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6234 __isl_give isl_set
**empty
)
6236 return (struct isl_set
*)
6237 isl_map_partial_lexmax((struct isl_map
*)set
,
6241 /* Compute the lexicographic minimum (or maximum if "max" is set)
6242 * of "bmap" over its domain.
6244 * Since we are not interested in the part of the domain space where
6245 * there is no solution, we initialize the domain to those constraints
6246 * of "bmap" that only involve the parameters and the input dimensions.
6247 * This relieves the parametric programming engine from detecting those
6248 * inequalities and transferring them to the context. More importantly,
6249 * it ensures that those inequalities are transferred first and not
6250 * intermixed with inequalities that actually split the domain.
6252 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6256 isl_basic_map
*copy
;
6259 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6260 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6261 copy
= isl_basic_map_copy(bmap
);
6262 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6263 isl_dim_div
, 0, n_div
);
6264 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6265 isl_dim_out
, 0, n_out
);
6266 dom
= isl_basic_map_domain(copy
);
6267 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6270 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6272 return isl_basic_map_lexopt(bmap
, 0);
6275 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6277 return isl_basic_map_lexopt(bmap
, 1);
6280 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6282 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6285 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6287 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6290 /* Extract the first and only affine expression from list
6291 * and then add it to *pwaff with the given dom.
6292 * This domain is known to be disjoint from other domains
6293 * because of the way isl_basic_map_foreach_lexmax works.
6295 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6296 __isl_take isl_aff_list
*list
, void *user
)
6298 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6300 isl_pw_aff
**pwaff
= user
;
6301 isl_pw_aff
*pwaff_i
;
6305 if (isl_aff_list_n_aff(list
) != 1)
6306 isl_die(ctx
, isl_error_internal
,
6307 "expecting single element list", goto error
);
6309 aff
= isl_aff_list_get_aff(list
, 0);
6310 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6312 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6314 isl_aff_list_free(list
);
6318 isl_basic_set_free(dom
);
6319 isl_aff_list_free(list
);
6323 /* Given a basic map with one output dimension, compute the minimum or
6324 * maximum of that dimension as an isl_pw_aff.
6326 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6327 * call update_dim_opt on each leaf of the result.
6329 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6332 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6336 dim
= isl_space_from_domain(isl_space_domain(dim
));
6337 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6338 pwaff
= isl_pw_aff_empty(dim
);
6340 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6342 return isl_pw_aff_free(pwaff
);
6347 /* Compute the minimum or maximum of the given output dimension
6348 * as a function of the parameters and the input dimensions,
6349 * but independently of the other output dimensions.
6351 * We first project out the other output dimension and then compute
6352 * the "lexicographic" maximum in each basic map, combining the results
6353 * using isl_pw_aff_union_max.
6355 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6362 n_out
= isl_map_dim(map
, isl_dim_out
);
6363 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6364 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6369 isl_space
*dim
= isl_map_get_space(map
);
6371 return isl_pw_aff_empty(dim
);
6374 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6375 for (i
= 1; i
< map
->n
; ++i
) {
6376 isl_pw_aff
*pwaff_i
;
6378 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6379 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6387 /* Compute the maximum of the given output dimension as a function of the
6388 * parameters and input dimensions, but independently of
6389 * the other output dimensions.
6391 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6393 return map_dim_opt(map
, pos
, 1);
6396 /* Compute the minimum or maximum of the given set dimension
6397 * as a function of the parameters,
6398 * but independently of the other set dimensions.
6400 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6403 return map_dim_opt(set
, pos
, max
);
6406 /* Compute the maximum of the given set dimension as a function of the
6407 * parameters, but independently of the other set dimensions.
6409 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6411 return set_dim_opt(set
, pos
, 1);
6414 /* Compute the minimum of the given set dimension as a function of the
6415 * parameters, but independently of the other set dimensions.
6417 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6419 return set_dim_opt(set
, pos
, 0);
6422 /* Apply a preimage specified by "mat" on the parameters of "bset".
6423 * bset is assumed to have only parameters and divs.
6425 static struct isl_basic_set
*basic_set_parameter_preimage(
6426 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6433 bset
->dim
= isl_space_cow(bset
->dim
);
6437 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6439 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6441 bset
->dim
->nparam
= 0;
6442 bset
->dim
->n_out
= nparam
;
6443 bset
= isl_basic_set_preimage(bset
, mat
);
6445 bset
->dim
->nparam
= bset
->dim
->n_out
;
6446 bset
->dim
->n_out
= 0;
6451 isl_basic_set_free(bset
);
6455 /* Apply a preimage specified by "mat" on the parameters of "set".
6456 * set is assumed to have only parameters and divs.
6458 static struct isl_set
*set_parameter_preimage(
6459 struct isl_set
*set
, struct isl_mat
*mat
)
6461 isl_space
*dim
= NULL
;
6467 dim
= isl_space_copy(set
->dim
);
6468 dim
= isl_space_cow(dim
);
6472 nparam
= isl_set_dim(set
, isl_dim_param
);
6474 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6477 dim
->n_out
= nparam
;
6478 isl_set_reset_space(set
, dim
);
6479 set
= isl_set_preimage(set
, mat
);
6482 dim
= isl_space_copy(set
->dim
);
6483 dim
= isl_space_cow(dim
);
6486 dim
->nparam
= dim
->n_out
;
6488 isl_set_reset_space(set
, dim
);
6491 isl_space_free(dim
);
6498 /* Intersect the basic set "bset" with the affine space specified by the
6499 * equalities in "eq".
6501 static struct isl_basic_set
*basic_set_append_equalities(
6502 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6510 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6515 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6516 for (i
= 0; i
< eq
->n_row
; ++i
) {
6517 k
= isl_basic_set_alloc_equality(bset
);
6520 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6521 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6525 bset
= isl_basic_set_gauss(bset
, NULL
);
6526 bset
= isl_basic_set_finalize(bset
);
6531 isl_basic_set_free(bset
);
6535 /* Intersect the set "set" with the affine space specified by the
6536 * equalities in "eq".
6538 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6546 for (i
= 0; i
< set
->n
; ++i
) {
6547 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6560 /* Given a basic set "bset" that only involves parameters and existentially
6561 * quantified variables, return the index of the first equality
6562 * that only involves parameters. If there is no such equality then
6563 * return bset->n_eq.
6565 * This function assumes that isl_basic_set_gauss has been called on "bset".
6567 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6570 unsigned nparam
, n_div
;
6575 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6576 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6578 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6579 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6586 /* Compute an explicit representation for the existentially quantified
6587 * variables in "bset" by computing the "minimal value" of the set
6588 * variables. Since there are no set variables, the computation of
6589 * the minimal value essentially computes an explicit representation
6590 * of the non-empty part(s) of "bset".
6592 * The input only involves parameters and existentially quantified variables.
6593 * All equalities among parameters have been removed.
6595 * Since the existentially quantified variables in the result are in general
6596 * going to be different from those in the input, we first replace
6597 * them by the minimal number of variables based on their equalities.
6598 * This should simplify the parametric integer programming.
6600 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6602 isl_morph
*morph1
, *morph2
;
6608 if (bset
->n_eq
== 0)
6609 return isl_basic_set_lexmin(bset
);
6611 morph1
= isl_basic_set_parameter_compression(bset
);
6612 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6613 bset
= isl_basic_set_lift(bset
);
6614 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6615 bset
= isl_morph_basic_set(morph2
, bset
);
6616 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6617 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6619 set
= isl_basic_set_lexmin(bset
);
6621 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6626 /* Project the given basic set onto its parameter domain, possibly introducing
6627 * new, explicit, existential variables in the constraints.
6628 * The input has parameters and (possibly implicit) existential variables.
6629 * The output has the same parameters, but only
6630 * explicit existentially quantified variables.
6632 * The actual projection is performed by pip, but pip doesn't seem
6633 * to like equalities very much, so we first remove the equalities
6634 * among the parameters by performing a variable compression on
6635 * the parameters. Afterward, an inverse transformation is performed
6636 * and the equalities among the parameters are inserted back in.
6638 * The variable compression on the parameters may uncover additional
6639 * equalities that were only implicit before. We therefore check
6640 * if there are any new parameter equalities in the result and
6641 * if so recurse. The removal of parameter equalities is required
6642 * for the parameter compression performed by base_compute_divs.
6644 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6648 struct isl_mat
*T
, *T2
;
6649 struct isl_set
*set
;
6652 bset
= isl_basic_set_cow(bset
);
6656 if (bset
->n_eq
== 0)
6657 return base_compute_divs(bset
);
6659 bset
= isl_basic_set_gauss(bset
, NULL
);
6662 if (isl_basic_set_plain_is_empty(bset
))
6663 return isl_set_from_basic_set(bset
);
6665 i
= first_parameter_equality(bset
);
6666 if (i
== bset
->n_eq
)
6667 return base_compute_divs(bset
);
6669 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6670 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6672 eq
= isl_mat_cow(eq
);
6673 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6674 if (T
&& T
->n_col
== 0) {
6678 bset
= isl_basic_set_set_to_empty(bset
);
6679 return isl_set_from_basic_set(bset
);
6681 bset
= basic_set_parameter_preimage(bset
, T
);
6683 i
= first_parameter_equality(bset
);
6686 else if (i
== bset
->n_eq
)
6687 set
= base_compute_divs(bset
);
6689 set
= parameter_compute_divs(bset
);
6690 set
= set_parameter_preimage(set
, T2
);
6691 set
= set_append_equalities(set
, eq
);
6695 /* Insert the divs from "ls" before those of "bmap".
6697 * The number of columns is not changed, which means that the last
6698 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6699 * The caller is responsible for removing the same number of dimensions
6700 * from the space of "bmap".
6702 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6703 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6709 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6713 old_n_div
= bmap
->n_div
;
6714 bmap
= insert_div_rows(bmap
, n_div
);
6718 for (i
= 0; i
< n_div
; ++i
) {
6719 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6720 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6726 /* Replace the space of "bmap" by the space and divs of "ls".
6728 * If "ls" has any divs, then we simplify the result since we may
6729 * have discovered some additional equalities that could simplify
6730 * the div expressions.
6732 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6733 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6737 bmap
= isl_basic_map_cow(bmap
);
6741 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6742 bmap
= insert_divs_from_local_space(bmap
, ls
);
6746 isl_space_free(bmap
->dim
);
6747 bmap
->dim
= isl_local_space_get_space(ls
);
6751 isl_local_space_free(ls
);
6753 bmap
= isl_basic_map_simplify(bmap
);
6754 bmap
= isl_basic_map_finalize(bmap
);
6757 isl_basic_map_free(bmap
);
6758 isl_local_space_free(ls
);
6762 /* Replace the space of "map" by the space and divs of "ls".
6764 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6765 __isl_take isl_local_space
*ls
)
6769 map
= isl_map_cow(map
);
6773 for (i
= 0; i
< map
->n
; ++i
) {
6774 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6775 isl_local_space_copy(ls
));
6779 isl_space_free(map
->dim
);
6780 map
->dim
= isl_local_space_get_space(ls
);
6784 isl_local_space_free(ls
);
6787 isl_local_space_free(ls
);
6792 /* Compute an explicit representation for the existentially
6793 * quantified variables for which do not know any explicit representation yet.
6795 * We first sort the existentially quantified variables so that the
6796 * existentially quantified variables for which we already have an explicit
6797 * representation are placed before those for which we do not.
6798 * The input dimensions, the output dimensions and the existentially
6799 * quantified variables for which we already have an explicit
6800 * representation are then turned into parameters.
6801 * compute_divs returns a map with the same parameters and
6802 * no input or output dimensions and the dimension specification
6803 * is reset to that of the input, including the existentially quantified
6804 * variables for which we already had an explicit representation.
6806 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6808 struct isl_basic_set
*bset
;
6809 struct isl_set
*set
;
6810 struct isl_map
*map
;
6812 isl_local_space
*ls
;
6819 bmap
= isl_basic_map_sort_divs(bmap
);
6820 bmap
= isl_basic_map_cow(bmap
);
6824 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6825 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6828 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6829 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6830 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6831 dim
= isl_space_set_alloc(bmap
->ctx
,
6832 nparam
+ n_in
+ n_out
+ n_known
, 0);
6836 ls
= isl_basic_map_get_local_space(bmap
);
6837 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6838 n_known
, bmap
->n_div
- n_known
);
6840 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6841 swap_div(bmap
, i
- n_known
, i
);
6842 bmap
->n_div
-= n_known
;
6843 bmap
->extra
-= n_known
;
6845 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6846 bset
= (struct isl_basic_set
*)bmap
;
6848 set
= parameter_compute_divs(bset
);
6849 map
= (struct isl_map
*)set
;
6850 map
= replace_space_by_local_space(map
, ls
);
6854 isl_basic_map_free(bmap
);
6858 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6866 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6867 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6868 if (isl_int_is_zero(bmap
->div
[i
][0]))
6870 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6876 static int map_divs_known(__isl_keep isl_map
*map
)
6883 for (i
= 0; i
< map
->n
; ++i
) {
6884 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6892 /* If bmap contains any unknown divs, then compute explicit
6893 * expressions for them. However, this computation may be
6894 * quite expensive, so first try to remove divs that aren't
6897 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6900 struct isl_map
*map
;
6902 known
= isl_basic_map_divs_known(bmap
);
6906 return isl_map_from_basic_map(bmap
);
6908 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6910 known
= isl_basic_map_divs_known(bmap
);
6914 return isl_map_from_basic_map(bmap
);
6916 map
= compute_divs(bmap
);
6919 isl_basic_map_free(bmap
);
6923 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6927 struct isl_map
*res
;
6934 known
= map_divs_known(map
);
6942 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6943 for (i
= 1 ; i
< map
->n
; ++i
) {
6945 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6946 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6947 res
= isl_map_union_disjoint(res
, r2
);
6949 res
= isl_map_union(res
, r2
);
6956 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6958 return (struct isl_set
*)
6959 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6962 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6964 return (struct isl_set
*)
6965 isl_map_compute_divs((struct isl_map
*)set
);
6968 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6971 struct isl_set
*set
;
6976 map
= isl_map_cow(map
);
6980 set
= (struct isl_set
*)map
;
6981 set
->dim
= isl_space_domain(set
->dim
);
6984 for (i
= 0; i
< map
->n
; ++i
) {
6985 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6989 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6990 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6997 /* Return the union of "map1" and "map2", where we assume for now that
6998 * "map1" and "map2" are disjoint. Note that the basic maps inside
6999 * "map1" or "map2" may not be disjoint from each other.
7000 * Also note that this function is also called from isl_map_union,
7001 * which takes care of handling the situation where "map1" and "map2"
7002 * may not be disjoint.
7004 * If one of the inputs is empty, we can simply return the other input.
7005 * Similarly, if one of the inputs is universal, then it is equal to the union.
7007 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7008 __isl_take isl_map
*map2
)
7012 struct isl_map
*map
= NULL
;
7018 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7019 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7020 "spaces don't match", goto error
);
7031 is_universe
= isl_map_plain_is_universe(map1
);
7032 if (is_universe
< 0)
7039 is_universe
= isl_map_plain_is_universe(map2
);
7040 if (is_universe
< 0)
7047 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7048 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7049 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7051 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7052 map1
->n
+ map2
->n
, flags
);
7055 for (i
= 0; i
< map1
->n
; ++i
) {
7056 map
= isl_map_add_basic_map(map
,
7057 isl_basic_map_copy(map1
->p
[i
]));
7061 for (i
= 0; i
< map2
->n
; ++i
) {
7062 map
= isl_map_add_basic_map(map
,
7063 isl_basic_map_copy(map2
->p
[i
]));
7077 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7078 * guaranteed to be disjoint by the caller.
7080 * Note that this functions is called from within isl_map_make_disjoint,
7081 * so we have to be careful not to touch the constraints of the inputs
7084 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7085 __isl_take isl_map
*map2
)
7087 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7090 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7091 * not be disjoint. The parameters are assumed to have been aligned.
7093 * We currently simply call map_union_disjoint, the internal operation
7094 * of which does not really depend on the inputs being disjoint.
7095 * If the result contains more than one basic map, then we clear
7096 * the disjoint flag since the result may contain basic maps from
7097 * both inputs and these are not guaranteed to be disjoint.
7099 * As a special case, if "map1" and "map2" are obviously equal,
7100 * then we simply return "map1".
7102 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7103 __isl_take isl_map
*map2
)
7110 equal
= isl_map_plain_is_equal(map1
, map2
);
7118 map1
= map_union_disjoint(map1
, map2
);
7122 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7130 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7133 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7134 __isl_take isl_map
*map2
)
7136 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7139 struct isl_set
*isl_set_union_disjoint(
7140 struct isl_set
*set1
, struct isl_set
*set2
)
7142 return (struct isl_set
*)
7143 isl_map_union_disjoint(
7144 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7147 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7149 return (struct isl_set
*)
7150 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7153 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7156 * "map" and "set" are assumed to be compatible and non-NULL.
7158 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7159 __isl_take isl_set
*set
,
7160 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7161 __isl_take isl_basic_set
*bset
))
7164 struct isl_map
*result
;
7167 if (isl_set_plain_is_universe(set
)) {
7172 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7173 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7174 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7176 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7177 map
->n
* set
->n
, flags
);
7178 for (i
= 0; result
&& i
< map
->n
; ++i
)
7179 for (j
= 0; j
< set
->n
; ++j
) {
7180 result
= isl_map_add_basic_map(result
,
7181 fn(isl_basic_map_copy(map
->p
[i
]),
7182 isl_basic_set_copy(set
->p
[j
])));
7192 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7193 __isl_take isl_set
*set
)
7198 if (!isl_map_compatible_range(map
, set
))
7199 isl_die(set
->ctx
, isl_error_invalid
,
7200 "incompatible spaces", goto error
);
7202 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7209 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7210 __isl_take isl_set
*set
)
7212 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7215 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7216 __isl_take isl_set
*set
)
7221 if (!isl_map_compatible_domain(map
, set
))
7222 isl_die(set
->ctx
, isl_error_invalid
,
7223 "incompatible spaces", goto error
);
7225 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7232 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7233 __isl_take isl_set
*set
)
7235 return isl_map_align_params_map_map_and(map
, set
,
7236 &map_intersect_domain
);
7239 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7240 __isl_take isl_map
*map2
)
7244 map1
= isl_map_reverse(map1
);
7245 map1
= isl_map_apply_range(map1
, map2
);
7246 return isl_map_reverse(map1
);
7253 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7254 __isl_take isl_map
*map2
)
7256 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7259 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7260 __isl_take isl_map
*map2
)
7262 isl_space
*dim_result
;
7263 struct isl_map
*result
;
7269 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7270 isl_space_copy(map2
->dim
));
7272 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7275 for (i
= 0; i
< map1
->n
; ++i
)
7276 for (j
= 0; j
< map2
->n
; ++j
) {
7277 result
= isl_map_add_basic_map(result
,
7278 isl_basic_map_apply_range(
7279 isl_basic_map_copy(map1
->p
[i
]),
7280 isl_basic_map_copy(map2
->p
[j
])));
7286 if (result
&& result
->n
<= 1)
7287 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7295 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7296 __isl_take isl_map
*map2
)
7298 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7302 * returns range - domain
7304 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7306 isl_space
*target_space
;
7307 struct isl_basic_set
*bset
;
7314 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7315 bmap
->dim
, isl_dim_out
),
7317 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7318 dim
= isl_basic_map_n_in(bmap
);
7319 nparam
= isl_basic_map_n_param(bmap
);
7320 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7321 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7322 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7323 for (i
= 0; i
< dim
; ++i
) {
7324 int j
= isl_basic_map_alloc_equality(bmap
);
7326 bmap
= isl_basic_map_free(bmap
);
7329 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7330 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7331 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7332 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7334 bset
= isl_basic_map_domain(bmap
);
7335 bset
= isl_basic_set_reset_space(bset
, target_space
);
7338 isl_basic_map_free(bmap
);
7343 * returns range - domain
7345 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7349 struct isl_set
*result
;
7354 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7355 map
->dim
, isl_dim_out
),
7357 dim
= isl_map_get_space(map
);
7358 dim
= isl_space_domain(dim
);
7359 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7362 for (i
= 0; i
< map
->n
; ++i
)
7363 result
= isl_set_add_basic_set(result
,
7364 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7373 * returns [domain -> range] -> range - domain
7375 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7376 __isl_take isl_basic_map
*bmap
)
7380 isl_basic_map
*domain
;
7384 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7385 bmap
->dim
, isl_dim_out
))
7386 isl_die(bmap
->ctx
, isl_error_invalid
,
7387 "domain and range don't match", goto error
);
7389 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7390 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7392 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7393 domain
= isl_basic_map_universe(dim
);
7395 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7396 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7397 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7399 total
= isl_basic_map_total_dim(bmap
);
7401 for (i
= 0; i
< n
; ++i
) {
7402 k
= isl_basic_map_alloc_equality(bmap
);
7405 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7406 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7407 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7408 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7411 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7412 return isl_basic_map_finalize(bmap
);
7414 isl_basic_map_free(bmap
);
7419 * returns [domain -> range] -> range - domain
7421 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7424 isl_space
*domain_dim
;
7429 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7430 map
->dim
, isl_dim_out
))
7431 isl_die(map
->ctx
, isl_error_invalid
,
7432 "domain and range don't match", goto error
);
7434 map
= isl_map_cow(map
);
7438 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7439 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7440 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7443 for (i
= 0; i
< map
->n
; ++i
) {
7444 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7448 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7455 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7457 struct isl_basic_map
*bmap
;
7465 nparam
= dims
->nparam
;
7467 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7471 for (i
= 0; i
< dim
; ++i
) {
7472 int j
= isl_basic_map_alloc_equality(bmap
);
7475 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7476 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7477 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7479 return isl_basic_map_finalize(bmap
);
7481 isl_basic_map_free(bmap
);
7485 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7489 if (dim
->n_in
!= dim
->n_out
)
7490 isl_die(dim
->ctx
, isl_error_invalid
,
7491 "number of input and output dimensions needs to be "
7492 "the same", goto error
);
7493 return basic_map_identity(dim
);
7495 isl_space_free(dim
);
7499 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7501 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7504 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7506 isl_space
*dim
= isl_set_get_space(set
);
7508 id
= isl_map_identity(isl_space_map_from_set(dim
));
7509 return isl_map_intersect_range(id
, set
);
7512 /* Construct a basic set with all set dimensions having only non-negative
7515 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7516 __isl_take isl_space
*space
)
7521 struct isl_basic_set
*bset
;
7525 nparam
= space
->nparam
;
7527 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7530 for (i
= 0; i
< dim
; ++i
) {
7531 int k
= isl_basic_set_alloc_inequality(bset
);
7534 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7535 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7539 isl_basic_set_free(bset
);
7543 /* Construct the half-space x_pos >= 0.
7545 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7549 isl_basic_set
*nonneg
;
7551 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7552 k
= isl_basic_set_alloc_inequality(nonneg
);
7555 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7556 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7558 return isl_basic_set_finalize(nonneg
);
7560 isl_basic_set_free(nonneg
);
7564 /* Construct the half-space x_pos <= -1.
7566 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7571 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7572 k
= isl_basic_set_alloc_inequality(neg
);
7575 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7576 isl_int_set_si(neg
->ineq
[k
][0], -1);
7577 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7579 return isl_basic_set_finalize(neg
);
7581 isl_basic_set_free(neg
);
7585 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7586 enum isl_dim_type type
, unsigned first
, unsigned n
)
7590 isl_basic_set
*nonneg
;
7598 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7600 offset
= pos(set
->dim
, type
);
7601 for (i
= 0; i
< n
; ++i
) {
7602 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7603 offset
+ first
+ i
);
7604 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7606 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7615 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7616 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7623 if (isl_set_plain_is_empty(set
)) {
7628 return fn(set
, signs
, user
);
7631 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7633 half
= isl_set_intersect(half
, isl_set_copy(set
));
7634 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7638 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7640 half
= isl_set_intersect(half
, set
);
7641 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7647 /* Call "fn" on the intersections of "set" with each of the orthants
7648 * (except for obviously empty intersections). The orthant is identified
7649 * by the signs array, with each entry having value 1 or -1 according
7650 * to the sign of the corresponding variable.
7652 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7653 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7663 if (isl_set_plain_is_empty(set
))
7666 nparam
= isl_set_dim(set
, isl_dim_param
);
7667 nvar
= isl_set_dim(set
, isl_dim_set
);
7669 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7671 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7679 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7681 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7684 int isl_basic_map_is_subset(
7685 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7688 struct isl_map
*map1
;
7689 struct isl_map
*map2
;
7691 if (!bmap1
|| !bmap2
)
7694 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7695 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7697 is_subset
= isl_map_is_subset(map1
, map2
);
7705 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7706 __isl_keep isl_basic_set
*bset2
)
7708 return isl_basic_map_is_subset(bset1
, bset2
);
7711 int isl_basic_map_is_equal(
7712 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7716 if (!bmap1
|| !bmap2
)
7718 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7721 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7725 int isl_basic_set_is_equal(
7726 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7728 return isl_basic_map_is_equal(
7729 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7732 int isl_map_is_empty(struct isl_map
*map
)
7739 for (i
= 0; i
< map
->n
; ++i
) {
7740 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7749 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7751 return map
? map
->n
== 0 : -1;
7754 int isl_set_plain_is_empty(struct isl_set
*set
)
7756 return set
? set
->n
== 0 : -1;
7759 int isl_set_is_empty(struct isl_set
*set
)
7761 return isl_map_is_empty((struct isl_map
*)set
);
7764 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7769 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7772 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7777 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7780 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7786 is_subset
= isl_map_is_subset(map1
, map2
);
7789 is_subset
= isl_map_is_subset(map2
, map1
);
7793 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7795 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7798 int isl_basic_map_is_strict_subset(
7799 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7803 if (!bmap1
|| !bmap2
)
7805 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7808 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7809 if (is_subset
== -1)
7814 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7820 is_subset
= isl_map_is_subset(map1
, map2
);
7823 is_subset
= isl_map_is_subset(map2
, map1
);
7824 if (is_subset
== -1)
7829 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7831 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7834 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7838 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7841 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7845 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7848 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7855 for (i
= 0; i
< map
->n
; ++i
) {
7856 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7864 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7866 return isl_map_plain_is_universe((isl_map
*) set
);
7869 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7871 struct isl_basic_set
*bset
= NULL
;
7872 struct isl_vec
*sample
= NULL
;
7879 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7882 if (isl_basic_map_is_universe(bmap
))
7885 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7886 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7887 copy
= isl_basic_map_remove_redundancies(copy
);
7888 empty
= isl_basic_map_plain_is_empty(copy
);
7889 isl_basic_map_free(copy
);
7893 total
= 1 + isl_basic_map_total_dim(bmap
);
7894 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7895 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7901 isl_vec_free(bmap
->sample
);
7902 bmap
->sample
= NULL
;
7903 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7906 sample
= isl_basic_set_sample_vec(bset
);
7909 empty
= sample
->size
== 0;
7910 isl_vec_free(bmap
->sample
);
7911 bmap
->sample
= sample
;
7913 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7918 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7922 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7925 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7929 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7932 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7934 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7937 struct isl_map
*isl_basic_map_union(
7938 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7940 struct isl_map
*map
;
7941 if (!bmap1
|| !bmap2
)
7944 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7946 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7949 map
= isl_map_add_basic_map(map
, bmap1
);
7950 map
= isl_map_add_basic_map(map
, bmap2
);
7953 isl_basic_map_free(bmap1
);
7954 isl_basic_map_free(bmap2
);
7958 struct isl_set
*isl_basic_set_union(
7959 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7961 return (struct isl_set
*)isl_basic_map_union(
7962 (struct isl_basic_map
*)bset1
,
7963 (struct isl_basic_map
*)bset2
);
7966 /* Order divs such that any div only depends on previous divs */
7967 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7975 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7977 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7979 if (isl_int_is_zero(bmap
->div
[i
][0]))
7981 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7985 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7991 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7993 return (struct isl_basic_set
*)
7994 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7997 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8004 for (i
= 0; i
< map
->n
; ++i
) {
8005 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8016 /* Apply the expansion computed by isl_merge_divs.
8017 * The expansion itself is given by "exp" while the resulting
8018 * list of divs is given by "div".
8020 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8021 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8026 bset
= isl_basic_set_cow(bset
);
8030 if (div
->n_row
< bset
->n_div
)
8031 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8032 "not an expansion", goto error
);
8034 n_div
= bset
->n_div
;
8035 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8036 div
->n_row
- n_div
, 0,
8037 2 * (div
->n_row
- n_div
));
8039 for (i
= n_div
; i
< div
->n_row
; ++i
)
8040 if (isl_basic_set_alloc_div(bset
) < 0)
8044 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8045 if (j
>= 0 && exp
[j
] == i
) {
8047 isl_basic_map_swap_div(bset
, i
, j
);
8050 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8051 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8059 isl_basic_set_free(bset
);
8064 /* Look for a div in dst that corresponds to the div "div" in src.
8065 * The divs before "div" in src and dst are assumed to be the same.
8067 * Returns -1 if no corresponding div was found and the position
8068 * of the corresponding div in dst otherwise.
8070 static int find_div(struct isl_basic_map
*dst
,
8071 struct isl_basic_map
*src
, unsigned div
)
8075 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8077 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8078 for (i
= div
; i
< dst
->n_div
; ++i
)
8079 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8080 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8081 dst
->n_div
- div
) == -1)
8086 /* Align the divs of "dst" to those of "src", adding divs from "src"
8087 * if needed. That is, make sure that the first src->n_div divs
8088 * of the result are equal to those of src.
8090 * The result is not finalized as by design it will have redundant
8091 * divs if any divs from "src" were copied.
8093 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8094 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8097 int known
, extended
;
8101 return isl_basic_map_free(dst
);
8103 if (src
->n_div
== 0)
8106 known
= isl_basic_map_divs_known(src
);
8108 return isl_basic_map_free(dst
);
8110 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8111 "some src divs are unknown",
8112 return isl_basic_map_free(dst
));
8114 src
= isl_basic_map_order_divs(src
);
8117 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8118 for (i
= 0; i
< src
->n_div
; ++i
) {
8119 int j
= find_div(dst
, src
, i
);
8122 int extra
= src
->n_div
- i
;
8123 dst
= isl_basic_map_cow(dst
);
8126 dst
= isl_basic_map_extend_space(dst
,
8127 isl_space_copy(dst
->dim
),
8128 extra
, 0, 2 * extra
);
8131 j
= isl_basic_map_alloc_div(dst
);
8133 return isl_basic_map_free(dst
);
8134 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8135 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8136 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8137 return isl_basic_map_free(dst
);
8140 isl_basic_map_swap_div(dst
, i
, j
);
8145 struct isl_basic_set
*isl_basic_set_align_divs(
8146 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8148 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8149 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8152 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8160 map
= isl_map_compute_divs(map
);
8161 map
= isl_map_cow(map
);
8165 for (i
= 1; i
< map
->n
; ++i
)
8166 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8167 for (i
= 1; i
< map
->n
; ++i
) {
8168 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8170 return isl_map_free(map
);
8173 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8177 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8179 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8182 /* Align the divs of the basic maps in "map" to those
8183 * of the basic maps in "list", as well as to the other basic maps in "map".
8184 * The elements in "list" are assumed to have known divs.
8186 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8187 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8191 map
= isl_map_compute_divs(map
);
8192 map
= isl_map_cow(map
);
8194 return isl_map_free(map
);
8198 n
= isl_basic_map_list_n_basic_map(list
);
8199 for (i
= 0; i
< n
; ++i
) {
8200 isl_basic_map
*bmap
;
8202 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8203 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8204 isl_basic_map_free(bmap
);
8207 return isl_map_free(map
);
8209 return isl_map_align_divs(map
);
8212 /* Align the divs of each element of "list" to those of "bmap".
8213 * Both "bmap" and the elements of "list" are assumed to have known divs.
8215 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8216 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8221 return isl_basic_map_list_free(list
);
8223 n
= isl_basic_map_list_n_basic_map(list
);
8224 for (i
= 0; i
< n
; ++i
) {
8225 isl_basic_map
*bmap_i
;
8227 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8228 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8229 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8235 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8236 __isl_take isl_map
*map
)
8240 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8241 map
= isl_map_intersect_domain(map
, set
);
8242 set
= isl_map_range(map
);
8250 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8251 __isl_take isl_map
*map
)
8253 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8256 /* There is no need to cow as removing empty parts doesn't change
8257 * the meaning of the set.
8259 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8266 for (i
= map
->n
- 1; i
>= 0; --i
)
8267 remove_if_empty(map
, i
);
8272 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8274 return (struct isl_set
*)
8275 isl_map_remove_empty_parts((struct isl_map
*)set
);
8278 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8280 struct isl_basic_map
*bmap
;
8281 if (!map
|| map
->n
== 0)
8283 bmap
= map
->p
[map
->n
-1];
8284 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8285 return isl_basic_map_copy(bmap
);
8288 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8290 return (struct isl_basic_set
*)
8291 isl_map_copy_basic_map((struct isl_map
*)set
);
8294 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8295 __isl_keep isl_basic_map
*bmap
)
8301 for (i
= map
->n
-1; i
>= 0; --i
) {
8302 if (map
->p
[i
] != bmap
)
8304 map
= isl_map_cow(map
);
8307 isl_basic_map_free(map
->p
[i
]);
8308 if (i
!= map
->n
-1) {
8309 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8310 map
->p
[i
] = map
->p
[map
->n
-1];
8321 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8322 struct isl_basic_set
*bset
)
8324 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8325 (struct isl_basic_map
*)bset
);
8328 /* Given two basic sets bset1 and bset2, compute the maximal difference
8329 * between the values of dimension pos in bset1 and those in bset2
8330 * for any common value of the parameters and dimensions preceding pos.
8332 static enum isl_lp_result
basic_set_maximal_difference_at(
8333 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8334 int pos
, isl_int
*opt
)
8337 struct isl_basic_map
*bmap1
= NULL
;
8338 struct isl_basic_map
*bmap2
= NULL
;
8339 struct isl_ctx
*ctx
;
8340 struct isl_vec
*obj
;
8343 unsigned dim1
, dim2
;
8344 enum isl_lp_result res
;
8346 if (!bset1
|| !bset2
)
8347 return isl_lp_error
;
8349 nparam
= isl_basic_set_n_param(bset1
);
8350 dim1
= isl_basic_set_n_dim(bset1
);
8351 dim2
= isl_basic_set_n_dim(bset2
);
8352 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8353 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8354 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8355 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8356 if (!bmap1
|| !bmap2
)
8358 bmap1
= isl_basic_map_cow(bmap1
);
8359 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8360 pos
, (dim1
- pos
) + (dim2
- pos
),
8361 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8362 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8365 total
= isl_basic_map_total_dim(bmap1
);
8367 obj
= isl_vec_alloc(ctx
, 1 + total
);
8370 isl_seq_clr(obj
->block
.data
, 1 + total
);
8371 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8372 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8373 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8375 isl_basic_map_free(bmap1
);
8379 isl_basic_map_free(bmap2
);
8381 isl_basic_map_free(bmap1
);
8382 return isl_lp_error
;
8385 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8386 * for any common value of the parameters and dimensions preceding pos
8387 * in both basic sets, the values of dimension pos in bset1 are
8388 * smaller or larger than those in bset2.
8391 * 1 if bset1 follows bset2
8392 * -1 if bset1 precedes bset2
8393 * 0 if bset1 and bset2 are incomparable
8394 * -2 if some error occurred.
8396 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8397 struct isl_basic_set
*bset2
, int pos
)
8400 enum isl_lp_result res
;
8405 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8407 if (res
== isl_lp_empty
)
8409 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8410 res
== isl_lp_unbounded
)
8412 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8421 /* Given two basic sets bset1 and bset2, check whether
8422 * for any common value of the parameters and dimensions preceding pos
8423 * there is a value of dimension pos in bset1 that is larger
8424 * than a value of the same dimension in bset2.
8427 * 1 if there exists such a pair
8428 * 0 if there is no such pair, but there is a pair of equal values
8430 * -2 if some error occurred.
8432 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8433 __isl_keep isl_basic_set
*bset2
, int pos
)
8436 enum isl_lp_result res
;
8441 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8443 if (res
== isl_lp_empty
)
8445 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8446 res
== isl_lp_unbounded
)
8448 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8450 else if (res
== isl_lp_ok
)
8459 /* Given two sets set1 and set2, check whether
8460 * for any common value of the parameters and dimensions preceding pos
8461 * there is a value of dimension pos in set1 that is larger
8462 * than a value of the same dimension in set2.
8465 * 1 if there exists such a pair
8466 * 0 if there is no such pair, but there is a pair of equal values
8468 * -2 if some error occurred.
8470 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8471 __isl_keep isl_set
*set2
, int pos
)
8479 for (i
= 0; i
< set1
->n
; ++i
)
8480 for (j
= 0; j
< set2
->n
; ++j
) {
8482 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8483 if (f
== 1 || f
== -2)
8492 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8493 unsigned pos
, isl_int
*val
)
8501 total
= isl_basic_map_total_dim(bmap
);
8502 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8503 for (; d
+1 > pos
; --d
)
8504 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8508 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8510 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8512 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8515 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8521 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8522 unsigned pos
, isl_int
*val
)
8534 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8537 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8538 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8539 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8540 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8544 isl_int_set(*val
, v
);
8550 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8551 unsigned pos
, isl_int
*val
)
8553 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8557 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8560 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8563 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8564 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8566 if (pos
>= isl_basic_map_dim(bmap
, type
))
8568 return isl_basic_map_plain_has_fixed_var(bmap
,
8569 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8572 /* If "bmap" obviously lies on a hyperplane where the given dimension
8573 * has a fixed value, then return that value.
8574 * Otherwise return NaN.
8576 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8577 __isl_keep isl_basic_map
*bmap
,
8578 enum isl_dim_type type
, unsigned pos
)
8586 ctx
= isl_basic_map_get_ctx(bmap
);
8587 v
= isl_val_alloc(ctx
);
8590 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8592 return isl_val_free(v
);
8594 isl_int_set_si(v
->d
, 1);
8598 return isl_val_nan(ctx
);
8601 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8602 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8604 if (pos
>= isl_map_dim(map
, type
))
8606 return isl_map_plain_has_fixed_var(map
,
8607 map_offset(map
, type
) - 1 + pos
, val
);
8610 /* If "map" obviously lies on a hyperplane where the given dimension
8611 * has a fixed value, then return that value.
8612 * Otherwise return NaN.
8614 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8615 enum isl_dim_type type
, unsigned pos
)
8623 ctx
= isl_map_get_ctx(map
);
8624 v
= isl_val_alloc(ctx
);
8627 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8629 return isl_val_free(v
);
8631 isl_int_set_si(v
->d
, 1);
8635 return isl_val_nan(ctx
);
8638 /* If "set" obviously lies on a hyperplane where the given dimension
8639 * has a fixed value, then return that value.
8640 * Otherwise return NaN.
8642 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8643 enum isl_dim_type type
, unsigned pos
)
8645 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8648 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8649 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8651 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8654 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8655 * then return this fixed value in *val.
8657 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8658 unsigned dim
, isl_int
*val
)
8660 return isl_basic_set_plain_has_fixed_var(bset
,
8661 isl_basic_set_n_param(bset
) + dim
, val
);
8664 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8665 * then return this fixed value in *val.
8667 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8668 unsigned dim
, isl_int
*val
)
8670 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8673 /* Check if input variable in has fixed value and if so and if val is not NULL,
8674 * then return this fixed value in *val.
8676 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8677 unsigned in
, isl_int
*val
)
8679 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8682 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8683 * and if val is not NULL, then return this lower bound in *val.
8685 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8686 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8688 int i
, i_eq
= -1, i_ineq
= -1;
8695 total
= isl_basic_set_total_dim(bset
);
8696 nparam
= isl_basic_set_n_param(bset
);
8697 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8698 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8704 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8705 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8707 if (i_eq
!= -1 || i_ineq
!= -1)
8711 if (i_eq
== -1 && i_ineq
== -1)
8713 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8714 /* The coefficient should always be one due to normalization. */
8715 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8717 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8719 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8720 total
- nparam
- dim
- 1) != -1)
8723 isl_int_neg(*val
, c
[0]);
8727 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8728 unsigned dim
, isl_int
*val
)
8740 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8744 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8746 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8747 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8749 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8753 isl_int_set(*val
, v
);
8759 /* uset_gist depends on constraints without existentially quantified
8760 * variables sorting first.
8762 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8764 isl_int
**c1
= (isl_int
**) p1
;
8765 isl_int
**c2
= (isl_int
**) p2
;
8767 unsigned size
= *(unsigned *) arg
;
8769 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8770 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8775 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8778 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8779 struct isl_basic_map
*bmap
)
8785 if (bmap
->n_ineq
== 0)
8787 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8789 total
= isl_basic_map_total_dim(bmap
);
8790 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8791 &sort_constraint_cmp
, &total
) < 0)
8792 return isl_basic_map_free(bmap
);
8796 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8797 __isl_take isl_basic_set
*bset
)
8799 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8800 (struct isl_basic_map
*)bset
);
8803 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8807 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8809 bmap
= isl_basic_map_remove_redundancies(bmap
);
8810 bmap
= isl_basic_map_sort_constraints(bmap
);
8812 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8816 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8818 return (struct isl_basic_set
*)isl_basic_map_normalize(
8819 (struct isl_basic_map
*)bset
);
8822 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8823 const __isl_keep isl_basic_map
*bmap2
)
8828 if (!bmap1
|| !bmap2
)
8833 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8834 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8835 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8836 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8837 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8838 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8839 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8840 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8841 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8842 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8843 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8845 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8847 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8849 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8850 return bmap1
->n_eq
- bmap2
->n_eq
;
8851 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8852 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8853 if (bmap1
->n_div
!= bmap2
->n_div
)
8854 return bmap1
->n_div
- bmap2
->n_div
;
8855 total
= isl_basic_map_total_dim(bmap1
);
8856 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8857 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8861 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8862 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8866 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8867 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8874 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8875 const __isl_keep isl_basic_set
*bset2
)
8877 return isl_basic_map_plain_cmp(bset1
, bset2
);
8880 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8886 if (set1
->n
!= set2
->n
)
8887 return set1
->n
- set2
->n
;
8889 for (i
= 0; i
< set1
->n
; ++i
) {
8890 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8898 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8899 __isl_keep isl_basic_map
*bmap2
)
8901 if (!bmap1
|| !bmap2
)
8903 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8906 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8907 __isl_keep isl_basic_set
*bset2
)
8909 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8910 (isl_basic_map
*)bset2
);
8913 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8915 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8916 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8918 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8921 /* Sort the basic maps of "map" and remove duplicate basic maps.
8923 * While removing basic maps, we make sure that the basic maps remain
8924 * sorted because isl_map_normalize expects the basic maps of the result
8927 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
8931 map
= isl_map_remove_empty_parts(map
);
8934 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8935 for (i
= map
->n
- 1; i
>= 1; --i
) {
8936 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
8938 isl_basic_map_free(map
->p
[i
-1]);
8939 for (j
= i
; j
< map
->n
; ++j
)
8940 map
->p
[j
- 1] = map
->p
[j
];
8947 /* Remove obvious duplicates among the basic maps of "map".
8949 * Unlike isl_map_normalize, this function does not remove redundant
8950 * constraints and only removes duplicates that have exactly the same
8951 * constraints in the input. It does sort the constraints and
8952 * the basic maps to ease the detection of duplicates.
8954 * If "map" has already been normalized or if the basic maps are
8955 * disjoint, then there can be no duplicates.
8957 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
8960 isl_basic_map
*bmap
;
8966 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
8968 for (i
= 0; i
< map
->n
; ++i
) {
8969 bmap
= isl_basic_map_copy(map
->p
[i
]);
8970 bmap
= isl_basic_map_sort_constraints(bmap
);
8972 return isl_map_free(map
);
8973 isl_basic_map_free(map
->p
[i
]);
8977 map
= sort_and_remove_duplicates(map
);
8981 /* We normalize in place, but if anything goes wrong we need
8982 * to return NULL, so we need to make sure we don't change the
8983 * meaning of any possible other copies of map.
8985 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
8988 struct isl_basic_map
*bmap
;
8992 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8994 for (i
= 0; i
< map
->n
; ++i
) {
8995 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8998 isl_basic_map_free(map
->p
[i
]);
9002 map
= sort_and_remove_duplicates(map
);
9004 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9011 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9013 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9016 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9026 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9029 map1
= isl_map_copy(map1
);
9030 map2
= isl_map_copy(map2
);
9031 map1
= isl_map_normalize(map1
);
9032 map2
= isl_map_normalize(map2
);
9035 equal
= map1
->n
== map2
->n
;
9036 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9037 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9050 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9052 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9053 (struct isl_map
*)set2
);
9056 /* Return an interval that ranges from min to max (inclusive)
9058 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9059 isl_int min
, isl_int max
)
9062 struct isl_basic_set
*bset
= NULL
;
9064 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9068 k
= isl_basic_set_alloc_inequality(bset
);
9071 isl_int_set_si(bset
->ineq
[k
][1], 1);
9072 isl_int_neg(bset
->ineq
[k
][0], min
);
9074 k
= isl_basic_set_alloc_inequality(bset
);
9077 isl_int_set_si(bset
->ineq
[k
][1], -1);
9078 isl_int_set(bset
->ineq
[k
][0], max
);
9082 isl_basic_set_free(bset
);
9086 /* Return the basic maps in "map" as a list.
9088 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9089 __isl_keep isl_map
*map
)
9093 isl_basic_map_list
*list
;
9097 ctx
= isl_map_get_ctx(map
);
9098 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9100 for (i
= 0; i
< map
->n
; ++i
) {
9101 isl_basic_map
*bmap
;
9103 bmap
= isl_basic_map_copy(map
->p
[i
]);
9104 list
= isl_basic_map_list_add(list
, bmap
);
9110 /* Return the intersection of the elements in the non-empty list "list".
9111 * All elements are assumed to live in the same space.
9113 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9114 __isl_take isl_basic_map_list
*list
)
9117 isl_basic_map
*bmap
;
9121 n
= isl_basic_map_list_n_basic_map(list
);
9123 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9124 "expecting non-empty list", goto error
);
9126 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9127 for (i
= 1; i
< n
; ++i
) {
9128 isl_basic_map
*bmap_i
;
9130 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9131 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9134 isl_basic_map_list_free(list
);
9137 isl_basic_map_list_free(list
);
9141 /* Return the intersection of the elements in the non-empty list "list".
9142 * All elements are assumed to live in the same space.
9144 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9145 __isl_take isl_basic_set_list
*list
)
9147 return isl_basic_map_list_intersect(list
);
9150 /* Return the Cartesian product of the basic sets in list (in the given order).
9152 __isl_give isl_basic_set
*isl_basic_set_list_product(
9153 __isl_take
struct isl_basic_set_list
*list
)
9161 struct isl_basic_set
*product
= NULL
;
9165 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9166 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9167 nparam
= isl_basic_set_n_param(list
->p
[0]);
9168 dim
= isl_basic_set_n_dim(list
->p
[0]);
9169 extra
= list
->p
[0]->n_div
;
9170 n_eq
= list
->p
[0]->n_eq
;
9171 n_ineq
= list
->p
[0]->n_ineq
;
9172 for (i
= 1; i
< list
->n
; ++i
) {
9173 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9174 isl_assert(list
->ctx
,
9175 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9176 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9177 extra
+= list
->p
[i
]->n_div
;
9178 n_eq
+= list
->p
[i
]->n_eq
;
9179 n_ineq
+= list
->p
[i
]->n_ineq
;
9181 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9186 for (i
= 0; i
< list
->n
; ++i
) {
9187 isl_basic_set_add_constraints(product
,
9188 isl_basic_set_copy(list
->p
[i
]), dim
);
9189 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9191 isl_basic_set_list_free(list
);
9194 isl_basic_set_free(product
);
9195 isl_basic_set_list_free(list
);
9199 struct isl_basic_map
*isl_basic_map_product(
9200 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9202 isl_space
*dim_result
= NULL
;
9203 struct isl_basic_map
*bmap
;
9204 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9205 struct isl_dim_map
*dim_map1
, *dim_map2
;
9207 if (!bmap1
|| !bmap2
)
9210 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9211 bmap2
->dim
, isl_dim_param
), goto error
);
9212 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9213 isl_space_copy(bmap2
->dim
));
9215 in1
= isl_basic_map_n_in(bmap1
);
9216 in2
= isl_basic_map_n_in(bmap2
);
9217 out1
= isl_basic_map_n_out(bmap1
);
9218 out2
= isl_basic_map_n_out(bmap2
);
9219 nparam
= isl_basic_map_n_param(bmap1
);
9221 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9222 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9223 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9224 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9225 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9226 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9227 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9228 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9229 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9230 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9231 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9233 bmap
= isl_basic_map_alloc_space(dim_result
,
9234 bmap1
->n_div
+ bmap2
->n_div
,
9235 bmap1
->n_eq
+ bmap2
->n_eq
,
9236 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9237 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9238 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9239 bmap
= isl_basic_map_simplify(bmap
);
9240 return isl_basic_map_finalize(bmap
);
9242 isl_basic_map_free(bmap1
);
9243 isl_basic_map_free(bmap2
);
9247 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9248 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9250 isl_basic_map
*prod
;
9252 prod
= isl_basic_map_product(bmap1
, bmap2
);
9253 prod
= isl_basic_map_flatten(prod
);
9257 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9258 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9260 return isl_basic_map_flat_range_product(bset1
, bset2
);
9263 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9264 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9266 isl_space
*space_result
= NULL
;
9267 isl_basic_map
*bmap
;
9268 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9269 struct isl_dim_map
*dim_map1
, *dim_map2
;
9271 if (!bmap1
|| !bmap2
)
9274 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9275 isl_space_copy(bmap2
->dim
));
9277 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9278 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9279 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9280 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9282 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9283 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9284 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9285 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9286 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9287 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9288 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9289 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9290 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9291 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9292 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9294 bmap
= isl_basic_map_alloc_space(space_result
,
9295 bmap1
->n_div
+ bmap2
->n_div
,
9296 bmap1
->n_eq
+ bmap2
->n_eq
,
9297 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9298 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9299 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9300 bmap
= isl_basic_map_simplify(bmap
);
9301 return isl_basic_map_finalize(bmap
);
9303 isl_basic_map_free(bmap1
);
9304 isl_basic_map_free(bmap2
);
9308 __isl_give isl_basic_map
*isl_basic_map_range_product(
9309 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9311 isl_space
*dim_result
= NULL
;
9312 isl_basic_map
*bmap
;
9313 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9314 struct isl_dim_map
*dim_map1
, *dim_map2
;
9316 if (!bmap1
|| !bmap2
)
9319 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9320 bmap2
->dim
, isl_dim_param
))
9321 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9322 "parameters don't match", goto error
);
9324 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9325 isl_space_copy(bmap2
->dim
));
9327 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9328 out1
= isl_basic_map_n_out(bmap1
);
9329 out2
= isl_basic_map_n_out(bmap2
);
9330 nparam
= isl_basic_map_n_param(bmap1
);
9332 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9333 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9334 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9335 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9336 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9337 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9338 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9339 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9340 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9341 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9342 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9344 bmap
= isl_basic_map_alloc_space(dim_result
,
9345 bmap1
->n_div
+ bmap2
->n_div
,
9346 bmap1
->n_eq
+ bmap2
->n_eq
,
9347 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9348 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9349 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9350 bmap
= isl_basic_map_simplify(bmap
);
9351 return isl_basic_map_finalize(bmap
);
9353 isl_basic_map_free(bmap1
);
9354 isl_basic_map_free(bmap2
);
9358 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9359 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9361 isl_basic_map
*prod
;
9363 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9364 prod
= isl_basic_map_flatten_range(prod
);
9368 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9369 * and collect the results.
9370 * The result live in the space obtained by calling "space_product"
9371 * on the spaces of "map1" and "map2".
9372 * If "remove_duplicates" is set then the result may contain duplicates
9373 * (even if the inputs do not) and so we try and remove the obvious
9376 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9377 __isl_take isl_map
*map2
,
9378 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9379 __isl_take isl_space
*right
),
9380 __isl_give isl_basic_map
*(*basic_map_product
)(
9381 __isl_take isl_basic_map
*left
,
9382 __isl_take isl_basic_map
*right
),
9383 int remove_duplicates
)
9386 struct isl_map
*result
;
9392 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9393 map2
->dim
, isl_dim_param
), goto error
);
9395 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9396 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9397 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9399 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9400 isl_space_copy(map2
->dim
)),
9401 map1
->n
* map2
->n
, flags
);
9404 for (i
= 0; i
< map1
->n
; ++i
)
9405 for (j
= 0; j
< map2
->n
; ++j
) {
9406 struct isl_basic_map
*part
;
9407 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9408 isl_basic_map_copy(map2
->p
[j
]));
9409 if (isl_basic_map_is_empty(part
))
9410 isl_basic_map_free(part
);
9412 result
= isl_map_add_basic_map(result
, part
);
9416 if (remove_duplicates
)
9417 result
= isl_map_remove_obvious_duplicates(result
);
9427 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9429 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9430 __isl_take isl_map
*map2
)
9432 return map_product(map1
, map2
, &isl_space_product
,
9433 &isl_basic_map_product
, 0);
9436 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9437 __isl_take isl_map
*map2
)
9439 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9442 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9444 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9445 __isl_take isl_map
*map2
)
9449 prod
= isl_map_product(map1
, map2
);
9450 prod
= isl_map_flatten(prod
);
9454 /* Given two set A and B, construct its Cartesian product A x B.
9456 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9458 return isl_map_range_product(set1
, set2
);
9461 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9462 __isl_take isl_set
*set2
)
9464 return isl_map_flat_range_product(set1
, set2
);
9467 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9469 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9470 __isl_take isl_map
*map2
)
9472 return map_product(map1
, map2
, &isl_space_domain_product
,
9473 &isl_basic_map_domain_product
, 1);
9476 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9478 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9479 __isl_take isl_map
*map2
)
9481 return map_product(map1
, map2
, &isl_space_range_product
,
9482 &isl_basic_map_range_product
, 1);
9485 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9486 __isl_take isl_map
*map2
)
9488 return isl_map_align_params_map_map_and(map1
, map2
,
9489 &map_domain_product_aligned
);
9492 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9493 __isl_take isl_map
*map2
)
9495 return isl_map_align_params_map_map_and(map1
, map2
,
9496 &map_range_product_aligned
);
9499 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9501 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9504 int total1
, keep1
, total2
, keep2
;
9508 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9509 !isl_space_range_is_wrapping(map
->dim
))
9510 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9511 "not a product", return isl_map_free(map
));
9513 space
= isl_map_get_space(map
);
9514 total1
= isl_space_dim(space
, isl_dim_in
);
9515 total2
= isl_space_dim(space
, isl_dim_out
);
9516 space
= isl_space_factor_domain(space
);
9517 keep1
= isl_space_dim(space
, isl_dim_in
);
9518 keep2
= isl_space_dim(space
, isl_dim_out
);
9519 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9520 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9521 map
= isl_map_reset_space(map
, space
);
9526 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9528 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9531 int total1
, keep1
, total2
, keep2
;
9535 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9536 !isl_space_range_is_wrapping(map
->dim
))
9537 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9538 "not a product", return isl_map_free(map
));
9540 space
= isl_map_get_space(map
);
9541 total1
= isl_space_dim(space
, isl_dim_in
);
9542 total2
= isl_space_dim(space
, isl_dim_out
);
9543 space
= isl_space_factor_range(space
);
9544 keep1
= isl_space_dim(space
, isl_dim_in
);
9545 keep2
= isl_space_dim(space
, isl_dim_out
);
9546 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9547 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9548 map
= isl_map_reset_space(map
, space
);
9553 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9555 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9562 if (!isl_space_domain_is_wrapping(map
->dim
))
9563 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9564 "domain is not a product", return isl_map_free(map
));
9566 space
= isl_map_get_space(map
);
9567 total
= isl_space_dim(space
, isl_dim_in
);
9568 space
= isl_space_domain_factor_domain(space
);
9569 keep
= isl_space_dim(space
, isl_dim_in
);
9570 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9571 map
= isl_map_reset_space(map
, space
);
9576 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9578 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9585 if (!isl_space_domain_is_wrapping(map
->dim
))
9586 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9587 "domain is not a product", return isl_map_free(map
));
9589 space
= isl_map_get_space(map
);
9590 total
= isl_space_dim(space
, isl_dim_in
);
9591 space
= isl_space_domain_factor_range(space
);
9592 keep
= isl_space_dim(space
, isl_dim_in
);
9593 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9594 map
= isl_map_reset_space(map
, space
);
9599 /* Given a map A -> [B -> C], extract the map A -> B.
9601 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9608 if (!isl_space_range_is_wrapping(map
->dim
))
9609 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9610 "range is not a product", return isl_map_free(map
));
9612 space
= isl_map_get_space(map
);
9613 total
= isl_space_dim(space
, isl_dim_out
);
9614 space
= isl_space_range_factor_domain(space
);
9615 keep
= isl_space_dim(space
, isl_dim_out
);
9616 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9617 map
= isl_map_reset_space(map
, space
);
9622 /* Given a map A -> [B -> C], extract the map A -> C.
9624 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9631 if (!isl_space_range_is_wrapping(map
->dim
))
9632 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9633 "range is not a product", return isl_map_free(map
));
9635 space
= isl_map_get_space(map
);
9636 total
= isl_space_dim(space
, isl_dim_out
);
9637 space
= isl_space_range_factor_range(space
);
9638 keep
= isl_space_dim(space
, isl_dim_out
);
9639 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9640 map
= isl_map_reset_space(map
, space
);
9645 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9647 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9648 __isl_take isl_map
*map2
)
9652 prod
= isl_map_domain_product(map1
, map2
);
9653 prod
= isl_map_flatten_domain(prod
);
9657 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9659 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9660 __isl_take isl_map
*map2
)
9664 prod
= isl_map_range_product(map1
, map2
);
9665 prod
= isl_map_flatten_range(prod
);
9669 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9672 uint32_t hash
= isl_hash_init();
9677 bmap
= isl_basic_map_copy(bmap
);
9678 bmap
= isl_basic_map_normalize(bmap
);
9681 total
= isl_basic_map_total_dim(bmap
);
9682 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9683 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9685 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9686 isl_hash_hash(hash
, c_hash
);
9688 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9689 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9691 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9692 isl_hash_hash(hash
, c_hash
);
9694 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9695 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9697 if (isl_int_is_zero(bmap
->div
[i
][0]))
9699 isl_hash_byte(hash
, i
& 0xFF);
9700 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9701 isl_hash_hash(hash
, c_hash
);
9703 isl_basic_map_free(bmap
);
9707 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9709 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9712 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9719 map
= isl_map_copy(map
);
9720 map
= isl_map_normalize(map
);
9724 hash
= isl_hash_init();
9725 for (i
= 0; i
< map
->n
; ++i
) {
9727 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9728 isl_hash_hash(hash
, bmap_hash
);
9736 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9738 return isl_map_get_hash((isl_map
*)set
);
9741 /* Check if the value for dimension dim is completely determined
9742 * by the values of the other parameters and variables.
9743 * That is, check if dimension dim is involved in an equality.
9745 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9752 nparam
= isl_basic_set_n_param(bset
);
9753 for (i
= 0; i
< bset
->n_eq
; ++i
)
9754 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9759 /* Check if the value for dimension dim is completely determined
9760 * by the values of the other parameters and variables.
9761 * That is, check if dimension dim is involved in an equality
9762 * for each of the subsets.
9764 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9770 for (i
= 0; i
< set
->n
; ++i
) {
9772 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9779 /* Return the number of basic maps in the (current) representation of "map".
9781 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9783 return map
? map
->n
: 0;
9786 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9788 return set
? set
->n
: 0;
9791 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9792 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9799 for (i
= 0; i
< map
->n
; ++i
)
9800 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9806 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9807 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9814 for (i
= 0; i
< set
->n
; ++i
)
9815 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9821 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9828 bset
= isl_basic_set_cow(bset
);
9832 dim
= isl_basic_set_get_space(bset
);
9833 dim
= isl_space_lift(dim
, bset
->n_div
);
9836 isl_space_free(bset
->dim
);
9838 bset
->extra
-= bset
->n_div
;
9841 bset
= isl_basic_set_finalize(bset
);
9845 isl_basic_set_free(bset
);
9849 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9855 set
= isl_set_align_divs(set
);
9860 set
= isl_set_cow(set
);
9864 n_div
= set
->p
[0]->n_div
;
9865 dim
= isl_set_get_space(set
);
9866 dim
= isl_space_lift(dim
, n_div
);
9869 isl_space_free(set
->dim
);
9872 for (i
= 0; i
< set
->n
; ++i
) {
9873 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9884 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9887 struct isl_basic_map
*bmap
;
9894 set
= isl_set_align_divs(set
);
9899 dim
= isl_set_get_space(set
);
9900 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9902 return isl_map_identity(isl_space_map_from_set(dim
));
9905 n_div
= set
->p
[0]->n_div
;
9906 dim
= isl_space_map_from_set(dim
);
9907 n_param
= isl_space_dim(dim
, isl_dim_param
);
9908 n_set
= isl_space_dim(dim
, isl_dim_in
);
9909 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9910 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9911 for (i
= 0; i
< n_set
; ++i
)
9912 bmap
= var_equal(bmap
, i
);
9914 total
= n_param
+ n_set
+ n_set
+ n_div
;
9915 for (i
= 0; i
< n_div
; ++i
) {
9916 k
= isl_basic_map_alloc_inequality(bmap
);
9919 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9920 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9921 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9922 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9923 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9924 set
->p
[0]->div
[i
][0]);
9926 l
= isl_basic_map_alloc_inequality(bmap
);
9929 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9930 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9931 set
->p
[0]->div
[i
][0]);
9932 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9936 bmap
= isl_basic_map_simplify(bmap
);
9937 bmap
= isl_basic_map_finalize(bmap
);
9938 return isl_map_from_basic_map(bmap
);
9941 isl_basic_map_free(bmap
);
9945 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9953 dim
= isl_basic_set_total_dim(bset
);
9954 size
+= bset
->n_eq
* (1 + dim
);
9955 size
+= bset
->n_ineq
* (1 + dim
);
9956 size
+= bset
->n_div
* (2 + dim
);
9961 int isl_set_size(__isl_keep isl_set
*set
)
9969 for (i
= 0; i
< set
->n
; ++i
)
9970 size
+= isl_basic_set_size(set
->p
[i
]);
9975 /* Check if there is any lower bound (if lower == 0) and/or upper
9976 * bound (if upper == 0) on the specified dim.
9978 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9979 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9986 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9988 pos
+= isl_basic_map_offset(bmap
, type
);
9990 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9991 if (isl_int_is_zero(bmap
->div
[i
][0]))
9993 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9997 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9998 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10001 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10002 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10009 return lower
&& upper
;
10012 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10013 enum isl_dim_type type
, unsigned pos
)
10015 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10018 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10019 enum isl_dim_type type
, unsigned pos
)
10021 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10024 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10025 enum isl_dim_type type
, unsigned pos
)
10027 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10030 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10031 enum isl_dim_type type
, unsigned pos
)
10038 for (i
= 0; i
< map
->n
; ++i
) {
10040 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10041 if (bounded
< 0 || !bounded
)
10048 /* Return 1 if the specified dim is involved in both an upper bound
10049 * and a lower bound.
10051 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10052 enum isl_dim_type type
, unsigned pos
)
10054 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10057 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10059 static int has_any_bound(__isl_keep isl_map
*map
,
10060 enum isl_dim_type type
, unsigned pos
,
10061 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10062 enum isl_dim_type type
, unsigned pos
))
10069 for (i
= 0; i
< map
->n
; ++i
) {
10071 bounded
= fn(map
->p
[i
], type
, pos
);
10072 if (bounded
< 0 || bounded
)
10079 /* Return 1 if the specified dim is involved in any lower bound.
10081 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10082 enum isl_dim_type type
, unsigned pos
)
10084 return has_any_bound(set
, type
, pos
,
10085 &isl_basic_map_dim_has_lower_bound
);
10088 /* Return 1 if the specified dim is involved in any upper bound.
10090 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10091 enum isl_dim_type type
, unsigned pos
)
10093 return has_any_bound(set
, type
, pos
,
10094 &isl_basic_map_dim_has_upper_bound
);
10097 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10099 static int has_bound(__isl_keep isl_map
*map
,
10100 enum isl_dim_type type
, unsigned pos
,
10101 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10102 enum isl_dim_type type
, unsigned pos
))
10109 for (i
= 0; i
< map
->n
; ++i
) {
10111 bounded
= fn(map
->p
[i
], type
, pos
);
10112 if (bounded
< 0 || !bounded
)
10119 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10121 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10122 enum isl_dim_type type
, unsigned pos
)
10124 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10127 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10129 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10130 enum isl_dim_type type
, unsigned pos
)
10132 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10135 /* For each of the "n" variables starting at "first", determine
10136 * the sign of the variable and put the results in the first "n"
10137 * elements of the array "signs".
10139 * 1 means that the variable is non-negative
10140 * -1 means that the variable is non-positive
10141 * 0 means the variable attains both positive and negative values.
10143 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10144 unsigned first
, unsigned n
, int *signs
)
10146 isl_vec
*bound
= NULL
;
10147 struct isl_tab
*tab
= NULL
;
10148 struct isl_tab_undo
*snap
;
10151 if (!bset
|| !signs
)
10154 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10155 tab
= isl_tab_from_basic_set(bset
, 0);
10156 if (!bound
|| !tab
)
10159 isl_seq_clr(bound
->el
, bound
->size
);
10160 isl_int_set_si(bound
->el
[0], -1);
10162 snap
= isl_tab_snap(tab
);
10163 for (i
= 0; i
< n
; ++i
) {
10166 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10167 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10169 empty
= tab
->empty
;
10170 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10171 if (isl_tab_rollback(tab
, snap
) < 0)
10179 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10180 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10182 empty
= tab
->empty
;
10183 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10184 if (isl_tab_rollback(tab
, snap
) < 0)
10187 signs
[i
] = empty
? -1 : 0;
10191 isl_vec_free(bound
);
10195 isl_vec_free(bound
);
10199 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10200 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10202 if (!bset
|| !signs
)
10204 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10207 first
+= pos(bset
->dim
, type
) - 1;
10208 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10211 /* Is it possible for the integer division "div" to depend (possibly
10212 * indirectly) on any output dimensions?
10214 * If the div is undefined, then we conservatively assume that it
10215 * may depend on them.
10216 * Otherwise, we check if it actually depends on them or on any integer
10217 * divisions that may depend on them.
10219 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10222 unsigned n_out
, o_out
;
10223 unsigned n_div
, o_div
;
10225 if (isl_int_is_zero(bmap
->div
[div
][0]))
10228 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10229 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10231 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10234 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10235 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10237 for (i
= 0; i
< n_div
; ++i
) {
10238 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10240 if (div_may_involve_output(bmap
, i
))
10247 /* Return the index of the equality of "bmap" that defines
10248 * the output dimension "pos" in terms of earlier dimensions.
10249 * The equality may also involve integer divisions, as long
10250 * as those integer divisions are defined in terms of
10251 * parameters or input dimensions.
10252 * Return bmap->n_eq if there is no such equality.
10253 * Return -1 on error.
10255 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10259 unsigned n_out
, o_out
;
10260 unsigned n_div
, o_div
;
10265 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10266 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10267 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10268 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10270 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10271 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10273 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10274 n_out
- (pos
+ 1)) != -1)
10276 for (k
= 0; k
< n_div
; ++k
) {
10277 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10279 if (div_may_involve_output(bmap
, k
))
10289 /* Check if the given basic map is obviously single-valued.
10290 * In particular, for each output dimension, check that there is
10291 * an equality that defines the output dimension in terms of
10292 * earlier dimensions.
10294 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10302 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10304 for (i
= 0; i
< n_out
; ++i
) {
10307 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10310 if (eq
>= bmap
->n_eq
)
10317 /* Check if the given basic map is single-valued.
10318 * We simply compute
10322 * and check if the result is a subset of the identity mapping.
10324 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10327 isl_basic_map
*test
;
10331 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10335 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10336 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10338 space
= isl_basic_map_get_space(bmap
);
10339 space
= isl_space_map_from_set(isl_space_range(space
));
10340 id
= isl_basic_map_identity(space
);
10342 sv
= isl_basic_map_is_subset(test
, id
);
10344 isl_basic_map_free(test
);
10345 isl_basic_map_free(id
);
10350 /* Check if the given map is obviously single-valued.
10352 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10361 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10364 /* Check if the given map is single-valued.
10365 * We simply compute
10369 * and check if the result is a subset of the identity mapping.
10371 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10378 sv
= isl_map_plain_is_single_valued(map
);
10382 test
= isl_map_reverse(isl_map_copy(map
));
10383 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10385 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10386 id
= isl_map_identity(dim
);
10388 sv
= isl_map_is_subset(test
, id
);
10390 isl_map_free(test
);
10396 int isl_map_is_injective(__isl_keep isl_map
*map
)
10400 map
= isl_map_copy(map
);
10401 map
= isl_map_reverse(map
);
10402 in
= isl_map_is_single_valued(map
);
10408 /* Check if the given map is obviously injective.
10410 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10414 map
= isl_map_copy(map
);
10415 map
= isl_map_reverse(map
);
10416 in
= isl_map_plain_is_single_valued(map
);
10422 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10426 sv
= isl_map_is_single_valued(map
);
10430 return isl_map_is_injective(map
);
10433 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10435 return isl_map_is_single_valued((isl_map
*)set
);
10438 int isl_map_is_translation(__isl_keep isl_map
*map
)
10443 delta
= isl_map_deltas(isl_map_copy(map
));
10444 ok
= isl_set_is_singleton(delta
);
10445 isl_set_free(delta
);
10450 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10452 if (isl_seq_first_non_zero(p
, pos
) != -1)
10454 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10459 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10468 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10471 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10472 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10473 for (j
= 0; j
< nvar
; ++j
) {
10474 int lower
= 0, upper
= 0;
10475 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10476 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10478 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10482 if (i
< bset
->n_eq
)
10484 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10485 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10487 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10489 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10494 if (!lower
|| !upper
)
10501 int isl_set_is_box(__isl_keep isl_set
*set
)
10508 return isl_basic_set_is_box(set
->p
[0]);
10511 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10516 return isl_space_is_wrapping(bset
->dim
);
10519 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10524 return isl_space_is_wrapping(set
->dim
);
10527 /* Is the domain of "map" a wrapped relation?
10529 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10534 return isl_space_domain_is_wrapping(map
->dim
);
10537 /* Is the range of "map" a wrapped relation?
10539 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10544 return isl_space_range_is_wrapping(map
->dim
);
10547 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10549 bmap
= isl_basic_map_cow(bmap
);
10553 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10557 bmap
= isl_basic_map_finalize(bmap
);
10559 return (isl_basic_set
*)bmap
;
10561 isl_basic_map_free(bmap
);
10565 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10569 map
= isl_map_cow(map
);
10573 for (i
= 0; i
< map
->n
; ++i
) {
10574 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10578 map
->dim
= isl_space_wrap(map
->dim
);
10582 return (isl_set
*)map
;
10588 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10590 bset
= isl_basic_set_cow(bset
);
10594 bset
->dim
= isl_space_unwrap(bset
->dim
);
10598 bset
= isl_basic_set_finalize(bset
);
10600 return (isl_basic_map
*)bset
;
10602 isl_basic_set_free(bset
);
10606 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10613 if (!isl_set_is_wrapping(set
))
10614 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10617 set
= isl_set_cow(set
);
10621 for (i
= 0; i
< set
->n
; ++i
) {
10622 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10627 set
->dim
= isl_space_unwrap(set
->dim
);
10631 return (isl_map
*)set
;
10637 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10638 enum isl_dim_type type
)
10643 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10646 bmap
= isl_basic_map_cow(bmap
);
10650 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10654 bmap
= isl_basic_map_finalize(bmap
);
10658 isl_basic_map_free(bmap
);
10662 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10663 enum isl_dim_type type
)
10670 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10673 map
= isl_map_cow(map
);
10677 for (i
= 0; i
< map
->n
; ++i
) {
10678 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10682 map
->dim
= isl_space_reset(map
->dim
, type
);
10692 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10697 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10700 bmap
= isl_basic_map_cow(bmap
);
10704 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10708 bmap
= isl_basic_map_finalize(bmap
);
10712 isl_basic_map_free(bmap
);
10716 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10718 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10721 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10722 __isl_take isl_basic_map
*bmap
)
10727 if (!bmap
->dim
->nested
[0])
10730 bmap
= isl_basic_map_cow(bmap
);
10734 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10738 bmap
= isl_basic_map_finalize(bmap
);
10742 isl_basic_map_free(bmap
);
10746 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10747 __isl_take isl_basic_map
*bmap
)
10752 if (!bmap
->dim
->nested
[1])
10755 bmap
= isl_basic_map_cow(bmap
);
10759 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10763 bmap
= isl_basic_map_finalize(bmap
);
10767 isl_basic_map_free(bmap
);
10771 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10778 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10781 map
= isl_map_cow(map
);
10785 for (i
= 0; i
< map
->n
; ++i
) {
10786 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10790 map
->dim
= isl_space_flatten(map
->dim
);
10800 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10802 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10805 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10807 isl_space
*dim
, *flat_dim
;
10810 dim
= isl_set_get_space(set
);
10811 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10812 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10813 map
= isl_map_intersect_domain(map
, set
);
10818 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10825 if (!map
->dim
->nested
[0])
10828 map
= isl_map_cow(map
);
10832 for (i
= 0; i
< map
->n
; ++i
) {
10833 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10837 map
->dim
= isl_space_flatten_domain(map
->dim
);
10847 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10854 if (!map
->dim
->nested
[1])
10857 map
= isl_map_cow(map
);
10861 for (i
= 0; i
< map
->n
; ++i
) {
10862 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10866 map
->dim
= isl_space_flatten_range(map
->dim
);
10876 /* Reorder the dimensions of "bmap" according to the given dim_map
10877 * and set the dimension specification to "dim".
10879 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10880 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10882 isl_basic_map
*res
;
10885 bmap
= isl_basic_map_cow(bmap
);
10886 if (!bmap
|| !dim
|| !dim_map
)
10889 flags
= bmap
->flags
;
10890 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10891 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10892 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10893 res
= isl_basic_map_alloc_space(dim
,
10894 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10895 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10897 res
->flags
= flags
;
10898 res
= isl_basic_map_finalize(res
);
10902 isl_basic_map_free(bmap
);
10903 isl_space_free(dim
);
10907 /* Reorder the dimensions of "map" according to given reordering.
10909 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10910 __isl_take isl_reordering
*r
)
10913 struct isl_dim_map
*dim_map
;
10915 map
= isl_map_cow(map
);
10916 dim_map
= isl_dim_map_from_reordering(r
);
10917 if (!map
|| !r
|| !dim_map
)
10920 for (i
= 0; i
< map
->n
; ++i
) {
10921 struct isl_dim_map
*dim_map_i
;
10923 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10925 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10926 isl_space_copy(r
->dim
), dim_map_i
);
10932 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10934 isl_reordering_free(r
);
10940 isl_reordering_free(r
);
10944 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10945 __isl_take isl_reordering
*r
)
10947 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10950 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10951 __isl_take isl_space
*model
)
10955 if (!map
|| !model
)
10958 ctx
= isl_space_get_ctx(model
);
10959 if (!isl_space_has_named_params(model
))
10960 isl_die(ctx
, isl_error_invalid
,
10961 "model has unnamed parameters", goto error
);
10962 if (!isl_space_has_named_params(map
->dim
))
10963 isl_die(ctx
, isl_error_invalid
,
10964 "relation has unnamed parameters", goto error
);
10965 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10966 isl_reordering
*exp
;
10968 model
= isl_space_drop_dims(model
, isl_dim_in
,
10969 0, isl_space_dim(model
, isl_dim_in
));
10970 model
= isl_space_drop_dims(model
, isl_dim_out
,
10971 0, isl_space_dim(model
, isl_dim_out
));
10972 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10973 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10974 map
= isl_map_realign(map
, exp
);
10977 isl_space_free(model
);
10980 isl_space_free(model
);
10985 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10986 __isl_take isl_space
*model
)
10988 return isl_map_align_params(set
, model
);
10991 /* Align the parameters of "bmap" to those of "model", introducing
10992 * additional parameters if needed.
10994 __isl_give isl_basic_map
*isl_basic_map_align_params(
10995 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10999 if (!bmap
|| !model
)
11002 ctx
= isl_space_get_ctx(model
);
11003 if (!isl_space_has_named_params(model
))
11004 isl_die(ctx
, isl_error_invalid
,
11005 "model has unnamed parameters", goto error
);
11006 if (!isl_space_has_named_params(bmap
->dim
))
11007 isl_die(ctx
, isl_error_invalid
,
11008 "relation has unnamed parameters", goto error
);
11009 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11010 isl_reordering
*exp
;
11011 struct isl_dim_map
*dim_map
;
11013 model
= isl_space_drop_dims(model
, isl_dim_in
,
11014 0, isl_space_dim(model
, isl_dim_in
));
11015 model
= isl_space_drop_dims(model
, isl_dim_out
,
11016 0, isl_space_dim(model
, isl_dim_out
));
11017 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11018 exp
= isl_reordering_extend_space(exp
,
11019 isl_basic_map_get_space(bmap
));
11020 dim_map
= isl_dim_map_from_reordering(exp
);
11021 bmap
= isl_basic_map_realign(bmap
,
11022 exp
? isl_space_copy(exp
->dim
) : NULL
,
11023 isl_dim_map_extend(dim_map
, bmap
));
11024 isl_reordering_free(exp
);
11028 isl_space_free(model
);
11031 isl_space_free(model
);
11032 isl_basic_map_free(bmap
);
11036 /* Align the parameters of "bset" to those of "model", introducing
11037 * additional parameters if needed.
11039 __isl_give isl_basic_set
*isl_basic_set_align_params(
11040 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11042 return isl_basic_map_align_params(bset
, model
);
11045 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11046 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11047 enum isl_dim_type c2
, enum isl_dim_type c3
,
11048 enum isl_dim_type c4
, enum isl_dim_type c5
)
11050 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11051 struct isl_mat
*mat
;
11057 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11058 isl_basic_map_total_dim(bmap
) + 1);
11061 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11062 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11063 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11064 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11065 isl_int_set(mat
->row
[i
][pos
],
11066 bmap
->eq
[i
][off
+ k
]);
11074 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11075 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11076 enum isl_dim_type c2
, enum isl_dim_type c3
,
11077 enum isl_dim_type c4
, enum isl_dim_type c5
)
11079 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11080 struct isl_mat
*mat
;
11086 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11087 isl_basic_map_total_dim(bmap
) + 1);
11090 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11091 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11092 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11093 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11094 isl_int_set(mat
->row
[i
][pos
],
11095 bmap
->ineq
[i
][off
+ k
]);
11103 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11104 __isl_take isl_space
*dim
,
11105 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11106 enum isl_dim_type c2
, enum isl_dim_type c3
,
11107 enum isl_dim_type c4
, enum isl_dim_type c5
)
11109 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11110 isl_basic_map
*bmap
;
11116 if (!dim
|| !eq
|| !ineq
)
11119 if (eq
->n_col
!= ineq
->n_col
)
11120 isl_die(dim
->ctx
, isl_error_invalid
,
11121 "equalities and inequalities matrices should have "
11122 "same number of columns", goto error
);
11124 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11126 if (eq
->n_col
< total
)
11127 isl_die(dim
->ctx
, isl_error_invalid
,
11128 "number of columns too small", goto error
);
11130 extra
= eq
->n_col
- total
;
11132 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11133 eq
->n_row
, ineq
->n_row
);
11136 for (i
= 0; i
< extra
; ++i
) {
11137 k
= isl_basic_map_alloc_div(bmap
);
11140 isl_int_set_si(bmap
->div
[k
][0], 0);
11142 for (i
= 0; i
< eq
->n_row
; ++i
) {
11143 l
= isl_basic_map_alloc_equality(bmap
);
11146 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11147 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11148 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11149 isl_int_set(bmap
->eq
[l
][off
+ k
],
11155 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11156 l
= isl_basic_map_alloc_inequality(bmap
);
11159 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11160 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11161 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11162 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11163 ineq
->row
[i
][pos
]);
11169 isl_space_free(dim
);
11171 isl_mat_free(ineq
);
11173 bmap
= isl_basic_map_simplify(bmap
);
11174 return isl_basic_map_finalize(bmap
);
11176 isl_space_free(dim
);
11178 isl_mat_free(ineq
);
11182 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11183 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11184 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11186 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11187 c1
, c2
, c3
, c4
, isl_dim_in
);
11190 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11191 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11192 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11194 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11195 c1
, c2
, c3
, c4
, isl_dim_in
);
11198 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11199 __isl_take isl_space
*dim
,
11200 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11201 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11203 return (isl_basic_set
*)
11204 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11205 c1
, c2
, c3
, c4
, isl_dim_in
);
11208 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11213 return isl_space_can_zip(bmap
->dim
);
11216 int isl_map_can_zip(__isl_keep isl_map
*map
)
11221 return isl_space_can_zip(map
->dim
);
11224 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11225 * (A -> C) -> (B -> D).
11227 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11236 if (!isl_basic_map_can_zip(bmap
))
11237 isl_die(bmap
->ctx
, isl_error_invalid
,
11238 "basic map cannot be zipped", goto error
);
11239 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11240 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11241 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11242 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11243 bmap
= isl_basic_map_cow(bmap
);
11244 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11247 bmap
->dim
= isl_space_zip(bmap
->dim
);
11252 isl_basic_map_free(bmap
);
11256 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11257 * (A -> C) -> (B -> D).
11259 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11266 if (!isl_map_can_zip(map
))
11267 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11270 map
= isl_map_cow(map
);
11274 for (i
= 0; i
< map
->n
; ++i
) {
11275 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11280 map
->dim
= isl_space_zip(map
->dim
);
11290 /* Can we apply isl_basic_map_curry to "bmap"?
11291 * That is, does it have a nested relation in its domain?
11293 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11298 return isl_space_can_curry(bmap
->dim
);
11301 /* Can we apply isl_map_curry to "map"?
11302 * That is, does it have a nested relation in its domain?
11304 int isl_map_can_curry(__isl_keep isl_map
*map
)
11309 return isl_space_can_curry(map
->dim
);
11312 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11315 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11321 if (!isl_basic_map_can_curry(bmap
))
11322 isl_die(bmap
->ctx
, isl_error_invalid
,
11323 "basic map cannot be curried", goto error
);
11324 bmap
= isl_basic_map_cow(bmap
);
11327 bmap
->dim
= isl_space_curry(bmap
->dim
);
11332 isl_basic_map_free(bmap
);
11336 /* Given a map (A -> B) -> C, return the corresponding map
11339 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11346 if (!isl_map_can_curry(map
))
11347 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11350 map
= isl_map_cow(map
);
11354 for (i
= 0; i
< map
->n
; ++i
) {
11355 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11360 map
->dim
= isl_space_curry(map
->dim
);
11370 /* Can we apply isl_basic_map_uncurry to "bmap"?
11371 * That is, does it have a nested relation in its domain?
11373 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11378 return isl_space_can_uncurry(bmap
->dim
);
11381 /* Can we apply isl_map_uncurry to "map"?
11382 * That is, does it have a nested relation in its domain?
11384 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11389 return isl_space_can_uncurry(map
->dim
);
11392 /* Given a basic map A -> (B -> C), return the corresponding basic map
11395 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11401 if (!isl_basic_map_can_uncurry(bmap
))
11402 isl_die(bmap
->ctx
, isl_error_invalid
,
11403 "basic map cannot be uncurried",
11404 return isl_basic_map_free(bmap
));
11405 bmap
= isl_basic_map_cow(bmap
);
11408 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11410 return isl_basic_map_free(bmap
);
11414 /* Given a map A -> (B -> C), return the corresponding map
11417 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11424 if (!isl_map_can_uncurry(map
))
11425 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11426 return isl_map_free(map
));
11428 map
= isl_map_cow(map
);
11432 for (i
= 0; i
< map
->n
; ++i
) {
11433 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11435 return isl_map_free(map
);
11438 map
->dim
= isl_space_uncurry(map
->dim
);
11440 return isl_map_free(map
);
11445 /* Construct a basic map mapping the domain of the affine expression
11446 * to a one-dimensional range prescribed by the affine expression.
11448 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11452 isl_local_space
*ls
;
11453 isl_basic_map
*bmap
;
11458 ls
= isl_aff_get_local_space(aff
);
11459 bmap
= isl_basic_map_from_local_space(ls
);
11460 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11461 k
= isl_basic_map_alloc_equality(bmap
);
11465 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11466 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11467 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11468 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11469 aff
->v
->size
- (pos
+ 1));
11472 bmap
= isl_basic_map_finalize(bmap
);
11476 isl_basic_map_free(bmap
);
11480 /* Construct a map mapping the domain of the affine expression
11481 * to a one-dimensional range prescribed by the affine expression.
11483 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11485 isl_basic_map
*bmap
;
11487 bmap
= isl_basic_map_from_aff(aff
);
11488 return isl_map_from_basic_map(bmap
);
11491 /* Construct a basic map mapping the domain the multi-affine expression
11492 * to its range, with each dimension in the range equated to the
11493 * corresponding affine expression.
11495 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11496 __isl_take isl_multi_aff
*maff
)
11500 isl_basic_map
*bmap
;
11505 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11506 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11507 "invalid space", goto error
);
11509 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11510 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11512 for (i
= 0; i
< maff
->n
; ++i
) {
11514 isl_basic_map
*bmap_i
;
11516 aff
= isl_aff_copy(maff
->p
[i
]);
11517 bmap_i
= isl_basic_map_from_aff(aff
);
11519 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11522 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11524 isl_multi_aff_free(maff
);
11527 isl_multi_aff_free(maff
);
11531 /* Construct a map mapping the domain the multi-affine expression
11532 * to its range, with each dimension in the range equated to the
11533 * corresponding affine expression.
11535 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11537 isl_basic_map
*bmap
;
11539 bmap
= isl_basic_map_from_multi_aff(maff
);
11540 return isl_map_from_basic_map(bmap
);
11543 /* Construct a basic map mapping a domain in the given space to
11544 * to an n-dimensional range, with n the number of elements in the list,
11545 * where each coordinate in the range is prescribed by the
11546 * corresponding affine expression.
11547 * The domains of all affine expressions in the list are assumed to match
11550 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11551 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11555 isl_basic_map
*bmap
;
11560 dim
= isl_space_from_domain(domain_dim
);
11561 bmap
= isl_basic_map_universe(dim
);
11563 for (i
= 0; i
< list
->n
; ++i
) {
11565 isl_basic_map
*bmap_i
;
11567 aff
= isl_aff_copy(list
->p
[i
]);
11568 bmap_i
= isl_basic_map_from_aff(aff
);
11570 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11573 isl_aff_list_free(list
);
11577 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11578 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11580 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11583 /* Construct a basic map where the given dimensions are equal to each other.
11585 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11586 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11588 isl_basic_map
*bmap
= NULL
;
11594 if (pos1
>= isl_space_dim(space
, type1
))
11595 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11596 "index out of bounds", goto error
);
11597 if (pos2
>= isl_space_dim(space
, type2
))
11598 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11599 "index out of bounds", goto error
);
11601 if (type1
== type2
&& pos1
== pos2
)
11602 return isl_basic_map_universe(space
);
11604 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11605 i
= isl_basic_map_alloc_equality(bmap
);
11608 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11609 pos1
+= isl_basic_map_offset(bmap
, type1
);
11610 pos2
+= isl_basic_map_offset(bmap
, type2
);
11611 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11612 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11613 bmap
= isl_basic_map_finalize(bmap
);
11614 isl_space_free(space
);
11617 isl_space_free(space
);
11618 isl_basic_map_free(bmap
);
11622 /* Add a constraint imposing that the given two dimensions are equal.
11624 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11625 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11629 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11631 bmap
= isl_basic_map_intersect(bmap
, eq
);
11636 /* Add a constraint imposing that the given two dimensions are equal.
11638 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11639 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11641 isl_basic_map
*bmap
;
11643 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11645 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11650 /* Add a constraint imposing that the given two dimensions have opposite values.
11652 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11653 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11655 isl_basic_map
*bmap
= NULL
;
11661 if (pos1
>= isl_map_dim(map
, type1
))
11662 isl_die(map
->ctx
, isl_error_invalid
,
11663 "index out of bounds", goto error
);
11664 if (pos2
>= isl_map_dim(map
, type2
))
11665 isl_die(map
->ctx
, isl_error_invalid
,
11666 "index out of bounds", goto error
);
11668 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11669 i
= isl_basic_map_alloc_equality(bmap
);
11672 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11673 pos1
+= isl_basic_map_offset(bmap
, type1
);
11674 pos2
+= isl_basic_map_offset(bmap
, type2
);
11675 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11676 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11677 bmap
= isl_basic_map_finalize(bmap
);
11679 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11683 isl_basic_map_free(bmap
);
11688 /* Construct a constraint imposing that the value of the first dimension is
11689 * greater than or equal to that of the second.
11691 static __isl_give isl_constraint
*constraint_order_ge(
11692 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11693 enum isl_dim_type type2
, int pos2
)
11700 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11702 if (pos1
>= isl_constraint_dim(c
, type1
))
11703 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11704 "index out of bounds", return isl_constraint_free(c
));
11705 if (pos2
>= isl_constraint_dim(c
, type2
))
11706 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11707 "index out of bounds", return isl_constraint_free(c
));
11709 if (type1
== type2
&& pos1
== pos2
)
11712 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11713 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11718 /* Add a constraint imposing that the value of the first dimension is
11719 * greater than or equal to that of the second.
11721 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11722 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11727 if (type1
== type2
&& pos1
== pos2
)
11729 space
= isl_basic_map_get_space(bmap
);
11730 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11731 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11736 /* Add a constraint imposing that the value of the first dimension is
11737 * greater than or equal to that of the second.
11739 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11740 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11745 if (type1
== type2
&& pos1
== pos2
)
11747 space
= isl_map_get_space(map
);
11748 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11749 map
= isl_map_add_constraint(map
, c
);
11754 /* Add a constraint imposing that the value of the first dimension is
11755 * less than or equal to that of the second.
11757 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11758 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11760 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11763 /* Construct a basic map where the value of the first dimension is
11764 * greater than that of the second.
11766 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11767 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11769 isl_basic_map
*bmap
= NULL
;
11775 if (pos1
>= isl_space_dim(space
, type1
))
11776 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11777 "index out of bounds", goto error
);
11778 if (pos2
>= isl_space_dim(space
, type2
))
11779 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11780 "index out of bounds", goto error
);
11782 if (type1
== type2
&& pos1
== pos2
)
11783 return isl_basic_map_empty(space
);
11785 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11786 i
= isl_basic_map_alloc_inequality(bmap
);
11788 return isl_basic_map_free(bmap
);
11789 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11790 pos1
+= isl_basic_map_offset(bmap
, type1
);
11791 pos2
+= isl_basic_map_offset(bmap
, type2
);
11792 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11793 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11794 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11795 bmap
= isl_basic_map_finalize(bmap
);
11799 isl_space_free(space
);
11800 isl_basic_map_free(bmap
);
11804 /* Add a constraint imposing that the value of the first dimension is
11805 * greater than that of the second.
11807 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11808 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11812 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11814 bmap
= isl_basic_map_intersect(bmap
, gt
);
11819 /* Add a constraint imposing that the value of the first dimension is
11820 * greater than that of the second.
11822 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11823 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11825 isl_basic_map
*bmap
;
11827 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11829 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11834 /* Add a constraint imposing that the value of the first dimension is
11835 * smaller than that of the second.
11837 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11838 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11840 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11843 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11847 isl_local_space
*ls
;
11852 if (!isl_basic_map_divs_known(bmap
))
11853 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11854 "some divs are unknown", return NULL
);
11856 ls
= isl_basic_map_get_local_space(bmap
);
11857 div
= isl_local_space_get_div(ls
, pos
);
11858 isl_local_space_free(ls
);
11863 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11866 return isl_basic_map_get_div(bset
, pos
);
11869 /* Plug in "subs" for dimension "type", "pos" of "bset".
11871 * Let i be the dimension to replace and let "subs" be of the form
11875 * Any integer division with a non-zero coefficient for i,
11877 * floor((a i + g)/m)
11881 * floor((a f + d g)/(m d))
11883 * Constraints of the form
11891 * We currently require that "subs" is an integral expression.
11892 * Handling rational expressions may require us to add stride constraints
11893 * as we do in isl_basic_set_preimage_multi_aff.
11895 __isl_give isl_basic_set
*isl_basic_set_substitute(
11896 __isl_take isl_basic_set
*bset
,
11897 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11903 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11906 bset
= isl_basic_set_cow(bset
);
11907 if (!bset
|| !subs
)
11910 ctx
= isl_basic_set_get_ctx(bset
);
11911 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11912 isl_die(ctx
, isl_error_invalid
,
11913 "spaces don't match", goto error
);
11914 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11915 isl_die(ctx
, isl_error_unsupported
,
11916 "cannot handle divs yet", goto error
);
11917 if (!isl_int_is_one(subs
->v
->el
[0]))
11918 isl_die(ctx
, isl_error_invalid
,
11919 "can only substitute integer expressions", goto error
);
11921 pos
+= isl_basic_set_offset(bset
, type
);
11925 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11926 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11928 isl_int_set(v
, bset
->eq
[i
][pos
]);
11929 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11930 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11931 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11934 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11935 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11937 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11938 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11939 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11940 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11943 for (i
= 0; i
< bset
->n_div
; ++i
) {
11944 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11946 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11947 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11948 isl_seq_combine(bset
->div
[i
] + 1,
11949 subs
->v
->el
[0], bset
->div
[i
] + 1,
11950 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11951 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11956 bset
= isl_basic_set_simplify(bset
);
11957 return isl_basic_set_finalize(bset
);
11959 isl_basic_set_free(bset
);
11963 /* Plug in "subs" for dimension "type", "pos" of "set".
11965 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11966 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11970 if (set
&& isl_set_plain_is_empty(set
))
11973 set
= isl_set_cow(set
);
11977 for (i
= set
->n
- 1; i
>= 0; --i
) {
11978 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11979 if (remove_if_empty(set
, i
) < 0)
11989 /* Check if the range of "ma" is compatible with the domain or range
11990 * (depending on "type") of "bmap".
11991 * Return -1 if anything is wrong.
11993 static int check_basic_map_compatible_range_multi_aff(
11994 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11995 __isl_keep isl_multi_aff
*ma
)
11998 isl_space
*ma_space
;
12000 ma_space
= isl_multi_aff_get_space(ma
);
12002 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12006 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12007 "parameters don't match", goto error
);
12008 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12012 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12013 "spaces don't match", goto error
);
12015 isl_space_free(ma_space
);
12018 isl_space_free(ma_space
);
12022 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12023 * coefficients before the transformed range of dimensions,
12024 * the "n_after" coefficients after the transformed range of dimensions
12025 * and the coefficients of the other divs in "bmap".
12027 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12028 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12033 isl_local_space
*ls
;
12038 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12042 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12043 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12044 for (i
= 0; i
< n_div
; ++i
) {
12045 int o_bmap
= 0, o_ls
= 0;
12047 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12048 o_bmap
+= 1 + 1 + n_param
;
12049 o_ls
+= 1 + 1 + n_param
;
12050 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12051 o_bmap
+= n_before
;
12052 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12053 ls
->div
->row
[i
] + o_ls
, n_set
);
12056 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12058 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12059 ls
->div
->row
[i
] + o_ls
, n_div
);
12062 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12063 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12067 isl_local_space_free(ls
);
12070 isl_local_space_free(ls
);
12074 /* How many stride constraints does "ma" enforce?
12075 * That is, how many of the affine expressions have a denominator
12076 * different from one?
12078 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12083 for (i
= 0; i
< ma
->n
; ++i
)
12084 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12090 /* For each affine expression in ma of the form
12092 * x_i = (f_i y + h_i)/m_i
12094 * with m_i different from one, add a constraint to "bmap"
12097 * f_i y + h_i = m_i alpha_i
12099 * with alpha_i an additional existentially quantified variable.
12101 static __isl_give isl_basic_map
*add_ma_strides(
12102 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12103 int n_before
, int n_after
)
12112 total
= isl_basic_map_total_dim(bmap
);
12113 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12114 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12115 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12116 for (i
= 0; i
< ma
->n
; ++i
) {
12117 int o_bmap
= 0, o_ma
= 1;
12119 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12121 div
= isl_basic_map_alloc_div(bmap
);
12122 k
= isl_basic_map_alloc_equality(bmap
);
12123 if (div
< 0 || k
< 0)
12125 isl_int_set_si(bmap
->div
[div
][0], 0);
12126 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12127 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12128 o_bmap
+= 1 + n_param
;
12129 o_ma
+= 1 + n_param
;
12130 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12131 o_bmap
+= n_before
;
12132 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12133 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12136 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12138 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12139 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12142 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12143 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12149 isl_basic_map_free(bmap
);
12153 /* Replace the domain or range space (depending on "type) of "space" by "set".
12155 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12156 enum isl_dim_type type
, __isl_take isl_space
*set
)
12158 if (type
== isl_dim_in
) {
12159 space
= isl_space_range(space
);
12160 space
= isl_space_map_from_domain_and_range(set
, space
);
12162 space
= isl_space_domain(space
);
12163 space
= isl_space_map_from_domain_and_range(space
, set
);
12169 /* Compute the preimage of the domain or range (depending on "type")
12170 * of "bmap" under the function represented by "ma".
12171 * In other words, plug in "ma" in the domain or range of "bmap".
12172 * The result is a basic map that lives in the same space as "bmap"
12173 * except that the domain or range has been replaced by
12174 * the domain space of "ma".
12176 * If bmap is represented by
12178 * A(p) + S u + B x + T v + C(divs) >= 0,
12180 * where u and x are input and output dimensions if type == isl_dim_out
12181 * while x and v are input and output dimensions if type == isl_dim_in,
12182 * and ma is represented by
12184 * x = D(p) + F(y) + G(divs')
12186 * then the result is
12188 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12190 * The divs in the input set are similarly adjusted.
12193 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12197 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12198 * B_i G(divs') + c_i(divs))/n_i)
12200 * If bmap is not a rational map and if F(y) involves any denominators
12202 * x_i = (f_i y + h_i)/m_i
12204 * then additional constraints are added to ensure that we only
12205 * map back integer points. That is we enforce
12207 * f_i y + h_i = m_i alpha_i
12209 * with alpha_i an additional existentially quantified variable.
12211 * We first copy over the divs from "ma".
12212 * Then we add the modified constraints and divs from "bmap".
12213 * Finally, we add the stride constraints, if needed.
12215 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12216 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12217 __isl_take isl_multi_aff
*ma
)
12221 isl_basic_map
*res
= NULL
;
12222 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12223 isl_int f
, c1
, c2
, g
;
12224 int rational
, strides
;
12231 ma
= isl_multi_aff_align_divs(ma
);
12234 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12237 if (type
== isl_dim_in
) {
12239 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12241 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12244 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12245 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12247 space
= isl_multi_aff_get_domain_space(ma
);
12248 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12249 rational
= isl_basic_map_is_rational(bmap
);
12250 strides
= rational
? 0 : multi_aff_strides(ma
);
12251 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12252 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12254 res
= isl_basic_map_set_rational(res
);
12256 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12257 if (isl_basic_map_alloc_div(res
) < 0)
12260 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12263 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12264 k
= isl_basic_map_alloc_equality(res
);
12267 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12268 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12271 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12272 k
= isl_basic_map_alloc_inequality(res
);
12275 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12276 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12279 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12280 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12281 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12284 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12285 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12290 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12296 isl_basic_map_free(bmap
);
12297 isl_multi_aff_free(ma
);
12298 res
= isl_basic_set_simplify(res
);
12299 return isl_basic_map_finalize(res
);
12305 isl_basic_map_free(bmap
);
12306 isl_multi_aff_free(ma
);
12307 isl_basic_map_free(res
);
12311 /* Compute the preimage of "bset" under the function represented by "ma".
12312 * In other words, plug in "ma" in "bset". The result is a basic set
12313 * that lives in the domain space of "ma".
12315 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12316 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12318 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12321 /* Compute the preimage of the domain of "bmap" under the function
12322 * represented by "ma".
12323 * In other words, plug in "ma" in the domain of "bmap".
12324 * The result is a basic map that lives in the same space as "bmap"
12325 * except that the domain has been replaced by the domain space of "ma".
12327 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12328 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12330 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12333 /* Compute the preimage of the range of "bmap" under the function
12334 * represented by "ma".
12335 * In other words, plug in "ma" in the range of "bmap".
12336 * The result is a basic map that lives in the same space as "bmap"
12337 * except that the range has been replaced by the domain space of "ma".
12339 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12340 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12342 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12345 /* Check if the range of "ma" is compatible with the domain or range
12346 * (depending on "type") of "map".
12347 * Return -1 if anything is wrong.
12349 static int check_map_compatible_range_multi_aff(
12350 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12351 __isl_keep isl_multi_aff
*ma
)
12354 isl_space
*ma_space
;
12356 ma_space
= isl_multi_aff_get_space(ma
);
12357 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12358 isl_space_free(ma_space
);
12360 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12361 "spaces don't match", return -1);
12365 /* Compute the preimage of the domain or range (depending on "type")
12366 * of "map" under the function represented by "ma".
12367 * In other words, plug in "ma" in the domain or range of "map".
12368 * The result is a map that lives in the same space as "map"
12369 * except that the domain or range has been replaced by
12370 * the domain space of "ma".
12372 * The parameters are assumed to have been aligned.
12374 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12375 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12380 map
= isl_map_cow(map
);
12381 ma
= isl_multi_aff_align_divs(ma
);
12384 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12387 for (i
= 0; i
< map
->n
; ++i
) {
12388 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12389 isl_multi_aff_copy(ma
));
12394 space
= isl_multi_aff_get_domain_space(ma
);
12395 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12397 isl_space_free(map
->dim
);
12402 isl_multi_aff_free(ma
);
12404 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12405 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12408 isl_multi_aff_free(ma
);
12413 /* Compute the preimage of the domain or range (depending on "type")
12414 * of "map" under the function represented by "ma".
12415 * In other words, plug in "ma" in the domain or range of "map".
12416 * The result is a map that lives in the same space as "map"
12417 * except that the domain or range has been replaced by
12418 * the domain space of "ma".
12420 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12421 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12426 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12427 return map_preimage_multi_aff(map
, type
, ma
);
12429 if (!isl_space_has_named_params(map
->dim
) ||
12430 !isl_space_has_named_params(ma
->space
))
12431 isl_die(map
->ctx
, isl_error_invalid
,
12432 "unaligned unnamed parameters", goto error
);
12433 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12434 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12436 return map_preimage_multi_aff(map
, type
, ma
);
12438 isl_multi_aff_free(ma
);
12439 return isl_map_free(map
);
12442 /* Compute the preimage of "set" under the function represented by "ma".
12443 * In other words, plug in "ma" in "set". The result is a set
12444 * that lives in the domain space of "ma".
12446 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12447 __isl_take isl_multi_aff
*ma
)
12449 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12452 /* Compute the preimage of the domain of "map" under the function
12453 * represented by "ma".
12454 * In other words, plug in "ma" in the domain of "map".
12455 * The result is a map that lives in the same space as "map"
12456 * except that the domain has been replaced by the domain space of "ma".
12458 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12459 __isl_take isl_multi_aff
*ma
)
12461 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12464 /* Compute the preimage of the range of "map" under the function
12465 * represented by "ma".
12466 * In other words, plug in "ma" in the range of "map".
12467 * The result is a map that lives in the same space as "map"
12468 * except that the range has been replaced by the domain space of "ma".
12470 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12471 __isl_take isl_multi_aff
*ma
)
12473 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12476 /* Compute the preimage of "map" under the function represented by "pma".
12477 * In other words, plug in "pma" in the domain or range of "map".
12478 * The result is a map that lives in the same space as "map",
12479 * except that the space of type "type" has been replaced by
12480 * the domain space of "pma".
12482 * The parameters of "map" and "pma" are assumed to have been aligned.
12484 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12485 __isl_take isl_map
*map
, enum isl_dim_type type
,
12486 __isl_take isl_pw_multi_aff
*pma
)
12495 isl_pw_multi_aff_free(pma
);
12496 res
= isl_map_empty(isl_map_get_space(map
));
12501 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12502 isl_multi_aff_copy(pma
->p
[0].maff
));
12503 if (type
== isl_dim_in
)
12504 res
= isl_map_intersect_domain(res
,
12505 isl_map_copy(pma
->p
[0].set
));
12507 res
= isl_map_intersect_range(res
,
12508 isl_map_copy(pma
->p
[0].set
));
12510 for (i
= 1; i
< pma
->n
; ++i
) {
12513 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12514 isl_multi_aff_copy(pma
->p
[i
].maff
));
12515 if (type
== isl_dim_in
)
12516 res_i
= isl_map_intersect_domain(res_i
,
12517 isl_map_copy(pma
->p
[i
].set
));
12519 res_i
= isl_map_intersect_range(res_i
,
12520 isl_map_copy(pma
->p
[i
].set
));
12521 res
= isl_map_union(res
, res_i
);
12524 isl_pw_multi_aff_free(pma
);
12528 isl_pw_multi_aff_free(pma
);
12533 /* Compute the preimage of "map" under the function represented by "pma".
12534 * In other words, plug in "pma" in the domain or range of "map".
12535 * The result is a map that lives in the same space as "map",
12536 * except that the space of type "type" has been replaced by
12537 * the domain space of "pma".
12539 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12540 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12545 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12546 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12548 if (!isl_space_has_named_params(map
->dim
) ||
12549 !isl_space_has_named_params(pma
->dim
))
12550 isl_die(map
->ctx
, isl_error_invalid
,
12551 "unaligned unnamed parameters", goto error
);
12552 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12553 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12555 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12557 isl_pw_multi_aff_free(pma
);
12558 return isl_map_free(map
);
12561 /* Compute the preimage of "set" under the function represented by "pma".
12562 * In other words, plug in "pma" in "set". The result is a set
12563 * that lives in the domain space of "pma".
12565 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12566 __isl_take isl_pw_multi_aff
*pma
)
12568 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12571 /* Compute the preimage of the domain of "map" under the function
12572 * represented by "pma".
12573 * In other words, plug in "pma" in the domain of "map".
12574 * The result is a map that lives in the same space as "map",
12575 * except that domain space has been replaced by the domain space of "pma".
12577 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12578 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12580 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12583 /* Compute the preimage of the range of "map" under the function
12584 * represented by "pma".
12585 * In other words, plug in "pma" in the range of "map".
12586 * The result is a map that lives in the same space as "map",
12587 * except that range space has been replaced by the domain space of "pma".
12589 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12590 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12592 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12595 /* Compute the preimage of "map" under the function represented by "mpa".
12596 * In other words, plug in "mpa" in the domain or range of "map".
12597 * The result is a map that lives in the same space as "map",
12598 * except that the space of type "type" has been replaced by
12599 * the domain space of "mpa".
12601 * If the map does not involve any constraints that refer to the
12602 * dimensions of the substituted space, then the only possible
12603 * effect of "mpa" on the map is to map the space to a different space.
12604 * We create a separate isl_multi_aff to effectuate this change
12605 * in order to avoid spurious splitting of the map along the pieces
12608 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12609 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12612 isl_pw_multi_aff
*pma
;
12617 n
= isl_map_dim(map
, type
);
12618 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12622 space
= isl_multi_pw_aff_get_space(mpa
);
12623 isl_multi_pw_aff_free(mpa
);
12624 ma
= isl_multi_aff_zero(space
);
12625 return isl_map_preimage_multi_aff(map
, type
, ma
);
12628 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12629 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12632 isl_multi_pw_aff_free(mpa
);
12636 /* Compute the preimage of "map" under the function represented by "mpa".
12637 * In other words, plug in "mpa" in the domain "map".
12638 * The result is a map that lives in the same space as "map",
12639 * except that domain space has been replaced by the domain space of "mpa".
12641 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12642 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12644 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12647 /* Compute the preimage of "set" by the function represented by "mpa".
12648 * In other words, plug in "mpa" in "set".
12650 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12651 __isl_take isl_multi_pw_aff
*mpa
)
12653 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);