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 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1713 __isl_take isl_basic_set
*bset
, unsigned n
)
1721 nparam
= isl_basic_set_n_param(bset
);
1722 dim
= isl_basic_set_n_dim(bset
);
1723 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1725 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1727 isl_basic_set_free(bset
);
1731 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1737 total
= isl_basic_map_total_dim(bmap
);
1738 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1739 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1741 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1743 i
= isl_basic_map_alloc_equality(bmap
);
1747 isl_int_set_si(bmap
->eq
[i
][0], 1);
1748 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1749 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1750 isl_vec_free(bmap
->sample
);
1751 bmap
->sample
= NULL
;
1752 return isl_basic_map_finalize(bmap
);
1754 isl_basic_map_free(bmap
);
1758 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1760 return (struct isl_basic_set
*)
1761 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1764 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1767 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1769 isl_int
*t
= bmap
->div
[a
];
1770 bmap
->div
[a
] = bmap
->div
[b
];
1774 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1775 * div definitions accordingly.
1777 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1780 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1782 swap_div(bmap
, a
, b
);
1784 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1785 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1787 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1788 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1790 for (i
= 0; i
< bmap
->n_div
; ++i
)
1791 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1792 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1795 /* Eliminate the specified n dimensions starting at first from the
1796 * constraints, without removing the dimensions from the space.
1797 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1799 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1800 enum isl_dim_type type
, unsigned first
, unsigned n
)
1809 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1810 isl_die(map
->ctx
, isl_error_invalid
,
1811 "index out of bounds", goto error
);
1813 map
= isl_map_cow(map
);
1817 for (i
= 0; i
< map
->n
; ++i
) {
1818 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1828 /* Eliminate the specified n dimensions starting at first from the
1829 * constraints, without removing the dimensions from the space.
1830 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1832 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1833 enum isl_dim_type type
, unsigned first
, unsigned n
)
1835 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1838 /* Eliminate the specified n dimensions starting at first from the
1839 * constraints, without removing the dimensions from the space.
1840 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1842 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1843 unsigned first
, unsigned n
)
1845 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1848 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1849 __isl_take isl_basic_map
*bmap
)
1853 bmap
= isl_basic_map_eliminate_vars(bmap
,
1854 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1858 return isl_basic_map_finalize(bmap
);
1861 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1862 __isl_take isl_basic_set
*bset
)
1864 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1865 (struct isl_basic_map
*)bset
);
1868 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1877 map
= isl_map_cow(map
);
1881 for (i
= 0; i
< map
->n
; ++i
) {
1882 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1892 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1894 return isl_map_remove_divs(set
);
1897 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1898 enum isl_dim_type type
, unsigned first
, unsigned n
)
1902 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1904 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1906 bmap
= isl_basic_map_eliminate_vars(bmap
,
1907 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1910 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1912 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1915 isl_basic_map_free(bmap
);
1919 /* Return true if the definition of the given div (recursively) involves
1920 * any of the given variables.
1922 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1923 unsigned first
, unsigned n
)
1926 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1928 if (isl_int_is_zero(bmap
->div
[div
][0]))
1930 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1933 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1934 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1936 if (div_involves_vars(bmap
, i
, first
, n
))
1943 /* Try and add a lower and/or upper bound on "div" to "bmap"
1944 * based on inequality "i".
1945 * "total" is the total number of variables (excluding the divs).
1946 * "v" is a temporary object that can be used during the calculations.
1947 * If "lb" is set, then a lower bound should be constructed.
1948 * If "ub" is set, then an upper bound should be constructed.
1950 * The calling function has already checked that the inequality does not
1951 * reference "div", but we still need to check that the inequality is
1952 * of the right form. We'll consider the case where we want to construct
1953 * a lower bound. The construction of upper bounds is similar.
1955 * Let "div" be of the form
1957 * q = floor((a + f(x))/d)
1959 * We essentially check if constraint "i" is of the form
1963 * so that we can use it to derive a lower bound on "div".
1964 * However, we allow a slightly more general form
1968 * with the condition that the coefficients of g(x) - f(x) are all
1970 * Rewriting this constraint as
1974 * adding a + f(x) to both sides and dividing by d, we obtain
1976 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1978 * Taking the floor on both sides, we obtain
1980 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1984 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1986 * In the case of an upper bound, we construct the constraint
1988 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1991 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1992 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1993 unsigned total
, isl_int v
, int lb
, int ub
)
1997 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1999 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2000 bmap
->div
[div
][1 + 1 + j
]);
2001 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2004 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2005 bmap
->div
[div
][1 + 1 + j
]);
2006 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2012 bmap
= isl_basic_map_cow(bmap
);
2013 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2015 int k
= isl_basic_map_alloc_inequality(bmap
);
2018 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2019 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2020 bmap
->div
[div
][1 + j
]);
2021 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2022 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2024 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2027 int k
= isl_basic_map_alloc_inequality(bmap
);
2030 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2031 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2032 bmap
->div
[div
][1 + j
]);
2033 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2034 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2036 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2039 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2042 isl_basic_map_free(bmap
);
2046 /* This function is called right before "div" is eliminated from "bmap"
2047 * using Fourier-Motzkin.
2048 * Look through the constraints of "bmap" for constraints on the argument
2049 * of the integer division and use them to construct constraints on the
2050 * integer division itself. These constraints can then be combined
2051 * during the Fourier-Motzkin elimination.
2052 * Note that it is only useful to introduce lower bounds on "div"
2053 * if "bmap" already contains upper bounds on "div" as the newly
2054 * introduce lower bounds can then be combined with the pre-existing
2055 * upper bounds. Similarly for upper bounds.
2056 * We therefore first check if "bmap" contains any lower and/or upper bounds
2059 * It is interesting to note that the introduction of these constraints
2060 * can indeed lead to more accurate results, even when compared to
2061 * deriving constraints on the argument of "div" from constraints on "div".
2062 * Consider, for example, the set
2064 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2066 * The second constraint can be rewritten as
2068 * 2 * [(-i-2j+3)/4] + k >= 0
2070 * from which we can derive
2072 * -i - 2j + 3 >= -2k
2078 * Combined with the first constraint, we obtain
2080 * -3 <= 3 + 2k or k >= -3
2082 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2083 * the first constraint, we obtain
2085 * [(i + 2j)/4] >= [-3/4] = -1
2087 * Combining this constraint with the second constraint, we obtain
2091 static __isl_give isl_basic_map
*insert_bounds_on_div(
2092 __isl_take isl_basic_map
*bmap
, int div
)
2095 int check_lb
, check_ub
;
2102 if (isl_int_is_zero(bmap
->div
[div
][0]))
2105 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2109 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2110 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2117 if (!check_lb
&& !check_ub
)
2122 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2123 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2126 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2127 check_lb
, check_ub
);
2135 /* Remove all divs (recursively) involving any of the given dimensions
2136 * in their definitions.
2138 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2139 __isl_take isl_basic_map
*bmap
,
2140 enum isl_dim_type type
, unsigned first
, unsigned n
)
2146 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2148 first
+= isl_basic_map_offset(bmap
, type
);
2150 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2151 if (!div_involves_vars(bmap
, i
, first
, n
))
2153 bmap
= insert_bounds_on_div(bmap
, i
);
2154 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2162 isl_basic_map_free(bmap
);
2166 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2167 __isl_take isl_basic_set
*bset
,
2168 enum isl_dim_type type
, unsigned first
, unsigned n
)
2170 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2173 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2174 enum isl_dim_type type
, unsigned first
, unsigned n
)
2183 map
= isl_map_cow(map
);
2187 for (i
= 0; i
< map
->n
; ++i
) {
2188 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2199 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2200 enum isl_dim_type type
, unsigned first
, unsigned n
)
2202 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2206 /* Does the desciption of "bmap" depend on the specified dimensions?
2207 * We also check whether the dimensions appear in any of the div definitions.
2208 * In principle there is no need for this check. If the dimensions appear
2209 * in a div definition, they also appear in the defining constraints of that
2212 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2213 enum isl_dim_type type
, unsigned first
, unsigned n
)
2220 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2221 isl_die(bmap
->ctx
, isl_error_invalid
,
2222 "index out of bounds", return -1);
2224 first
+= isl_basic_map_offset(bmap
, type
);
2225 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2226 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2228 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2229 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2231 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2232 if (isl_int_is_zero(bmap
->div
[i
][0]))
2234 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2241 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2242 enum isl_dim_type type
, unsigned first
, unsigned n
)
2249 if (first
+ n
> isl_map_dim(map
, type
))
2250 isl_die(map
->ctx
, isl_error_invalid
,
2251 "index out of bounds", return -1);
2253 for (i
= 0; i
< map
->n
; ++i
) {
2254 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2256 if (involves
< 0 || involves
)
2263 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2264 enum isl_dim_type type
, unsigned first
, unsigned n
)
2266 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2269 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2270 enum isl_dim_type type
, unsigned first
, unsigned n
)
2272 return isl_map_involves_dims(set
, type
, first
, n
);
2275 /* Return true if the definition of the given div is unknown or depends
2278 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2281 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2283 if (isl_int_is_zero(bmap
->div
[div
][0]))
2286 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2287 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2289 if (div_is_unknown(bmap
, i
))
2296 /* Remove all divs that are unknown or defined in terms of unknown divs.
2298 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2299 __isl_take isl_basic_map
*bmap
)
2306 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2307 if (!div_is_unknown(bmap
, i
))
2309 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2318 /* Remove all divs that are unknown or defined in terms of unknown divs.
2320 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2321 __isl_take isl_basic_set
*bset
)
2323 return isl_basic_map_remove_unknown_divs(bset
);
2326 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2335 map
= isl_map_cow(map
);
2339 for (i
= 0; i
< map
->n
; ++i
) {
2340 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2350 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2352 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2355 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2356 __isl_take isl_basic_set
*bset
,
2357 enum isl_dim_type type
, unsigned first
, unsigned n
)
2359 return (isl_basic_set
*)
2360 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2363 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2364 enum isl_dim_type type
, unsigned first
, unsigned n
)
2371 map
= isl_map_cow(map
);
2374 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2376 for (i
= 0; i
< map
->n
; ++i
) {
2377 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2378 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2382 map
= isl_map_drop(map
, type
, first
, n
);
2389 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2390 enum isl_dim_type type
, unsigned first
, unsigned n
)
2392 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2395 /* Project out n inputs starting at first using Fourier-Motzkin */
2396 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2397 unsigned first
, unsigned n
)
2399 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2402 static void dump_term(struct isl_basic_map
*bmap
,
2403 isl_int c
, int pos
, FILE *out
)
2406 unsigned in
= isl_basic_map_n_in(bmap
);
2407 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2408 unsigned nparam
= isl_basic_map_n_param(bmap
);
2410 isl_int_print(out
, c
, 0);
2412 if (!isl_int_is_one(c
))
2413 isl_int_print(out
, c
, 0);
2414 if (pos
< 1 + nparam
) {
2415 name
= isl_space_get_dim_name(bmap
->dim
,
2416 isl_dim_param
, pos
- 1);
2418 fprintf(out
, "%s", name
);
2420 fprintf(out
, "p%d", pos
- 1);
2421 } else if (pos
< 1 + nparam
+ in
)
2422 fprintf(out
, "i%d", pos
- 1 - nparam
);
2423 else if (pos
< 1 + nparam
+ dim
)
2424 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2426 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2430 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2431 int sign
, FILE *out
)
2435 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2439 for (i
= 0, first
= 1; i
< len
; ++i
) {
2440 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2443 fprintf(out
, " + ");
2445 isl_int_abs(v
, c
[i
]);
2446 dump_term(bmap
, v
, i
, out
);
2453 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2454 const char *op
, FILE *out
, int indent
)
2458 fprintf(out
, "%*s", indent
, "");
2460 dump_constraint_sign(bmap
, c
, 1, out
);
2461 fprintf(out
, " %s ", op
);
2462 dump_constraint_sign(bmap
, c
, -1, out
);
2466 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2467 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2469 fprintf(out
, "%*s", indent
, "");
2470 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2475 static void dump_constraints(struct isl_basic_map
*bmap
,
2476 isl_int
**c
, unsigned n
,
2477 const char *op
, FILE *out
, int indent
)
2481 for (i
= 0; i
< n
; ++i
)
2482 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2485 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2489 unsigned total
= isl_basic_map_total_dim(bmap
);
2491 for (j
= 0; j
< 1 + total
; ++j
) {
2492 if (isl_int_is_zero(exp
[j
]))
2494 if (!first
&& isl_int_is_pos(exp
[j
]))
2496 dump_term(bmap
, exp
[j
], j
, out
);
2501 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2505 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2506 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2508 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2509 fprintf(out
, "%*s", indent
, "");
2510 fprintf(out
, "e%d = [(", i
);
2511 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2513 isl_int_print(out
, bmap
->div
[i
][0], 0);
2514 fprintf(out
, "]\n");
2518 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2519 FILE *out
, int indent
)
2522 fprintf(out
, "null basic set\n");
2526 fprintf(out
, "%*s", indent
, "");
2527 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2528 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2529 bset
->extra
, bset
->flags
);
2530 dump((struct isl_basic_map
*)bset
, out
, indent
);
2533 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2534 FILE *out
, int indent
)
2537 fprintf(out
, "null basic map\n");
2541 fprintf(out
, "%*s", indent
, "");
2542 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2543 "flags: %x, n_name: %d\n",
2545 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2546 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2547 dump(bmap
, out
, indent
);
2550 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2555 total
= isl_basic_map_total_dim(bmap
);
2556 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2557 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2558 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2559 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2563 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2566 struct isl_set
*set
;
2570 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2571 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2572 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2573 sizeof(struct isl_set
) +
2574 (n
- 1) * sizeof(struct isl_basic_set
*));
2578 set
->ctx
= dim
->ctx
;
2579 isl_ctx_ref(set
->ctx
);
2587 isl_space_free(dim
);
2591 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2592 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2594 struct isl_set
*set
;
2597 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2601 set
= isl_set_alloc_space(dims
, n
, flags
);
2605 /* Make sure "map" has room for at least "n" more basic maps.
2607 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2610 struct isl_map
*grown
= NULL
;
2614 isl_assert(map
->ctx
, n
>= 0, goto error
);
2615 if (map
->n
+ n
<= map
->size
)
2617 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2620 for (i
= 0; i
< map
->n
; ++i
) {
2621 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2629 isl_map_free(grown
);
2634 /* Make sure "set" has room for at least "n" more basic sets.
2636 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2638 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2641 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2644 struct isl_set
*dup
;
2649 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2652 for (i
= 0; i
< set
->n
; ++i
)
2653 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2657 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2659 return isl_map_from_basic_map(bset
);
2662 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2664 struct isl_map
*map
;
2669 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2670 return isl_map_add_basic_map(map
, bmap
);
2673 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2674 __isl_take isl_basic_set
*bset
)
2676 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2677 (struct isl_basic_map
*)bset
);
2680 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2690 isl_ctx_deref(set
->ctx
);
2691 for (i
= 0; i
< set
->n
; ++i
)
2692 isl_basic_set_free(set
->p
[i
]);
2693 isl_space_free(set
->dim
);
2699 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2704 fprintf(out
, "null set\n");
2708 fprintf(out
, "%*s", indent
, "");
2709 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2710 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2712 for (i
= 0; i
< set
->n
; ++i
) {
2713 fprintf(out
, "%*s", indent
, "");
2714 fprintf(out
, "basic set %d:\n", i
);
2715 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2719 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2724 fprintf(out
, "null map\n");
2728 fprintf(out
, "%*s", indent
, "");
2729 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2730 "flags: %x, n_name: %d\n",
2731 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2732 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2733 for (i
= 0; i
< map
->n
; ++i
) {
2734 fprintf(out
, "%*s", indent
, "");
2735 fprintf(out
, "basic map %d:\n", i
);
2736 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2740 struct isl_basic_map
*isl_basic_map_intersect_domain(
2741 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2743 struct isl_basic_map
*bmap_domain
;
2748 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2749 bset
->dim
, isl_dim_param
), goto error
);
2751 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2752 isl_assert(bset
->ctx
,
2753 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2755 bmap
= isl_basic_map_cow(bmap
);
2758 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2759 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2760 bmap_domain
= isl_basic_map_from_domain(bset
);
2761 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2763 bmap
= isl_basic_map_simplify(bmap
);
2764 return isl_basic_map_finalize(bmap
);
2766 isl_basic_map_free(bmap
);
2767 isl_basic_set_free(bset
);
2771 struct isl_basic_map
*isl_basic_map_intersect_range(
2772 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2774 struct isl_basic_map
*bmap_range
;
2779 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2780 bset
->dim
, isl_dim_param
), goto error
);
2782 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2783 isl_assert(bset
->ctx
,
2784 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2786 if (isl_basic_set_is_universe(bset
)) {
2787 isl_basic_set_free(bset
);
2791 bmap
= isl_basic_map_cow(bmap
);
2794 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2795 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2796 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2797 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2799 bmap
= isl_basic_map_simplify(bmap
);
2800 return isl_basic_map_finalize(bmap
);
2802 isl_basic_map_free(bmap
);
2803 isl_basic_set_free(bset
);
2807 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2816 total
= 1 + isl_basic_map_total_dim(bmap
);
2817 if (total
!= vec
->size
)
2822 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2823 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2824 if (!isl_int_is_zero(s
)) {
2830 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2831 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2832 if (isl_int_is_neg(s
)) {
2843 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2845 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2848 struct isl_basic_map
*isl_basic_map_intersect(
2849 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2851 struct isl_vec
*sample
= NULL
;
2853 if (!bmap1
|| !bmap2
)
2856 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2857 bmap2
->dim
, isl_dim_param
), goto error
);
2858 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2859 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2860 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2861 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2862 return isl_basic_map_intersect(bmap2
, bmap1
);
2864 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2865 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2866 isl_assert(bmap1
->ctx
,
2867 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2869 if (bmap1
->sample
&&
2870 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2871 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2872 sample
= isl_vec_copy(bmap1
->sample
);
2873 else if (bmap2
->sample
&&
2874 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2875 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2876 sample
= isl_vec_copy(bmap2
->sample
);
2878 bmap1
= isl_basic_map_cow(bmap1
);
2881 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2882 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2883 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2886 isl_vec_free(sample
);
2888 isl_vec_free(bmap1
->sample
);
2889 bmap1
->sample
= sample
;
2892 bmap1
= isl_basic_map_simplify(bmap1
);
2893 return isl_basic_map_finalize(bmap1
);
2896 isl_vec_free(sample
);
2897 isl_basic_map_free(bmap1
);
2898 isl_basic_map_free(bmap2
);
2902 struct isl_basic_set
*isl_basic_set_intersect(
2903 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2905 return (struct isl_basic_set
*)
2906 isl_basic_map_intersect(
2907 (struct isl_basic_map
*)bset1
,
2908 (struct isl_basic_map
*)bset2
);
2911 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2912 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2914 return isl_basic_set_intersect(bset1
, bset2
);
2917 /* Special case of isl_map_intersect, where both map1 and map2
2918 * are convex, without any divs and such that either map1 or map2
2919 * contains a single constraint. This constraint is then simply
2920 * added to the other map.
2922 static __isl_give isl_map
*map_intersect_add_constraint(
2923 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2925 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2926 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2927 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2928 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2930 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2931 return isl_map_intersect(map2
, map1
);
2933 isl_assert(map2
->ctx
,
2934 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2936 map1
= isl_map_cow(map1
);
2939 if (isl_map_plain_is_empty(map1
)) {
2943 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2944 if (map2
->p
[0]->n_eq
== 1)
2945 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2947 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2948 map2
->p
[0]->ineq
[0]);
2950 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2951 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2955 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2956 isl_basic_map_free(map1
->p
[0]);
2969 /* map2 may be either a parameter domain or a map living in the same
2972 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2973 __isl_take isl_map
*map2
)
2982 if ((isl_map_plain_is_empty(map1
) ||
2983 isl_map_plain_is_universe(map2
)) &&
2984 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2988 if ((isl_map_plain_is_empty(map2
) ||
2989 isl_map_plain_is_universe(map1
)) &&
2990 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2995 if (map1
->n
== 1 && map2
->n
== 1 &&
2996 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2997 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2998 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2999 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3000 return map_intersect_add_constraint(map1
, map2
);
3002 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3003 isl_space_dim(map2
->dim
, isl_dim_param
))
3004 isl_assert(map1
->ctx
,
3005 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3007 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3008 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3009 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3011 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3012 map1
->n
* map2
->n
, flags
);
3015 for (i
= 0; i
< map1
->n
; ++i
)
3016 for (j
= 0; j
< map2
->n
; ++j
) {
3017 struct isl_basic_map
*part
;
3018 part
= isl_basic_map_intersect(
3019 isl_basic_map_copy(map1
->p
[i
]),
3020 isl_basic_map_copy(map2
->p
[j
]));
3021 if (isl_basic_map_is_empty(part
) < 0)
3022 part
= isl_basic_map_free(part
);
3023 result
= isl_map_add_basic_map(result
, part
);
3036 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3037 __isl_take isl_map
*map2
)
3041 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3042 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3043 "spaces don't match", goto error
);
3044 return map_intersect_internal(map1
, map2
);
3051 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3052 __isl_take isl_map
*map2
)
3054 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3057 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3059 return (struct isl_set
*)
3060 isl_map_intersect((struct isl_map
*)set1
,
3061 (struct isl_map
*)set2
);
3064 /* map_intersect_internal accepts intersections
3065 * with parameter domains, so we can just call that function.
3067 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3068 __isl_take isl_set
*params
)
3070 return map_intersect_internal(map
, params
);
3073 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3074 __isl_take isl_map
*map2
)
3076 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3079 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3080 __isl_take isl_set
*params
)
3082 return isl_map_intersect_params(set
, params
);
3085 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3088 struct isl_basic_set
*bset
;
3093 bmap
= isl_basic_map_cow(bmap
);
3096 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3097 in
= isl_basic_map_n_in(bmap
);
3098 bset
= isl_basic_set_from_basic_map(bmap
);
3099 bset
= isl_basic_set_swap_vars(bset
, in
);
3100 return isl_basic_map_from_basic_set(bset
, dim
);
3103 static __isl_give isl_basic_map
*basic_map_space_reset(
3104 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3110 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3113 space
= isl_basic_map_get_space(bmap
);
3114 space
= isl_space_reset(space
, type
);
3115 bmap
= isl_basic_map_reset_space(bmap
, space
);
3119 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3120 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3121 unsigned pos
, unsigned n
)
3124 struct isl_basic_map
*res
;
3125 struct isl_dim_map
*dim_map
;
3126 unsigned total
, off
;
3127 enum isl_dim_type t
;
3130 return basic_map_space_reset(bmap
, type
);
3135 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3137 total
= isl_basic_map_total_dim(bmap
) + n
;
3138 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3140 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3142 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3144 unsigned size
= isl_basic_map_dim(bmap
, t
);
3145 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3147 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3148 pos
, size
- pos
, off
+ pos
+ n
);
3150 off
+= isl_space_dim(res_dim
, t
);
3152 isl_dim_map_div(dim_map
, bmap
, off
);
3154 res
= isl_basic_map_alloc_space(res_dim
,
3155 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3156 if (isl_basic_map_is_rational(bmap
))
3157 res
= isl_basic_map_set_rational(res
);
3158 if (isl_basic_map_plain_is_empty(bmap
)) {
3159 isl_basic_map_free(bmap
);
3161 return isl_basic_map_set_to_empty(res
);
3163 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3164 return isl_basic_map_finalize(res
);
3167 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3168 __isl_take isl_basic_set
*bset
,
3169 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3171 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3174 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3175 enum isl_dim_type type
, unsigned n
)
3179 return isl_basic_map_insert_dims(bmap
, type
,
3180 isl_basic_map_dim(bmap
, type
), n
);
3183 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3184 enum isl_dim_type type
, unsigned n
)
3188 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3189 return isl_basic_map_add_dims(bset
, type
, n
);
3191 isl_basic_set_free(bset
);
3195 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3196 enum isl_dim_type type
)
3200 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3203 space
= isl_map_get_space(map
);
3204 space
= isl_space_reset(space
, type
);
3205 map
= isl_map_reset_space(map
, space
);
3209 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3210 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3215 return map_space_reset(map
, type
);
3217 map
= isl_map_cow(map
);
3221 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3225 for (i
= 0; i
< map
->n
; ++i
) {
3226 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3237 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3238 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3240 return isl_map_insert_dims(set
, type
, pos
, n
);
3243 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3244 enum isl_dim_type type
, unsigned n
)
3248 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3251 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3252 enum isl_dim_type type
, unsigned n
)
3256 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3257 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3263 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3264 __isl_take isl_basic_map
*bmap
,
3265 enum isl_dim_type dst_type
, unsigned dst_pos
,
3266 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3268 struct isl_dim_map
*dim_map
;
3269 struct isl_basic_map
*res
;
3270 enum isl_dim_type t
;
3271 unsigned total
, off
;
3278 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3281 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3284 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3286 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3287 pos(bmap
->dim
, src_type
) + src_pos
+
3288 ((src_type
< dst_type
) ? n
: 0)) {
3289 bmap
= isl_basic_map_cow(bmap
);
3293 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3294 src_type
, src_pos
, n
);
3298 bmap
= isl_basic_map_finalize(bmap
);
3303 total
= isl_basic_map_total_dim(bmap
);
3304 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3307 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3308 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3309 if (t
== dst_type
) {
3310 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3313 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3316 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3317 dst_pos
, size
- dst_pos
, off
);
3318 off
+= size
- dst_pos
;
3319 } else if (t
== src_type
) {
3320 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3323 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3324 src_pos
+ n
, size
- src_pos
- n
, off
);
3325 off
+= size
- src_pos
- n
;
3327 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3331 isl_dim_map_div(dim_map
, bmap
, off
);
3333 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3334 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3335 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3339 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3340 src_type
, src_pos
, n
);
3344 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3345 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3346 bmap
= isl_basic_map_finalize(bmap
);
3350 isl_basic_map_free(bmap
);
3354 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3355 enum isl_dim_type dst_type
, unsigned dst_pos
,
3356 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3358 return (isl_basic_set
*)isl_basic_map_move_dims(
3359 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3362 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3363 enum isl_dim_type dst_type
, unsigned dst_pos
,
3364 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3368 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3369 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3370 src_type
, src_pos
, n
);
3376 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3377 enum isl_dim_type dst_type
, unsigned dst_pos
,
3378 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3387 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3390 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3393 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3395 map
= isl_map_cow(map
);
3399 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3403 for (i
= 0; i
< map
->n
; ++i
) {
3404 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3406 src_type
, src_pos
, n
);
3417 /* Move the specified dimensions to the last columns right before
3418 * the divs. Don't change the dimension specification of bmap.
3419 * That's the responsibility of the caller.
3421 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3422 enum isl_dim_type type
, unsigned first
, unsigned n
)
3424 struct isl_dim_map
*dim_map
;
3425 struct isl_basic_map
*res
;
3426 enum isl_dim_type t
;
3427 unsigned total
, off
;
3431 if (pos(bmap
->dim
, type
) + first
+ n
==
3432 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3435 total
= isl_basic_map_total_dim(bmap
);
3436 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3439 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3440 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3442 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3445 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3446 first
, n
, total
- bmap
->n_div
- n
);
3447 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3448 first
+ n
, size
- (first
+ n
), off
);
3449 off
+= size
- (first
+ n
);
3451 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3455 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3457 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3458 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3459 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3463 /* Insert "n" rows in the divs of "bmap".
3465 * The number of columns is not changed, which means that the last
3466 * dimensions of "bmap" are being reintepreted as the new divs.
3467 * The space of "bmap" is not adjusted, however, which means
3468 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3469 * from the space of "bmap" is the responsibility of the caller.
3471 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3479 bmap
= isl_basic_map_cow(bmap
);
3483 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3484 old
= bmap
->block2
.data
;
3485 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3486 (bmap
->extra
+ n
) * (1 + row_size
));
3487 if (!bmap
->block2
.data
)
3488 return isl_basic_map_free(bmap
);
3489 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3491 return isl_basic_map_free(bmap
);
3492 for (i
= 0; i
< n
; ++i
) {
3493 new_div
[i
] = bmap
->block2
.data
+
3494 (bmap
->extra
+ i
) * (1 + row_size
);
3495 isl_seq_clr(new_div
[i
], 1 + row_size
);
3497 for (i
= 0; i
< bmap
->extra
; ++i
)
3498 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3500 bmap
->div
= new_div
;
3507 /* Turn the n dimensions of type type, starting at first
3508 * into existentially quantified variables.
3510 __isl_give isl_basic_map
*isl_basic_map_project_out(
3511 __isl_take isl_basic_map
*bmap
,
3512 enum isl_dim_type type
, unsigned first
, unsigned n
)
3515 return basic_map_space_reset(bmap
, type
);
3520 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3521 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3523 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3526 bmap
= move_last(bmap
, type
, first
, n
);
3527 bmap
= isl_basic_map_cow(bmap
);
3528 bmap
= insert_div_rows(bmap
, n
);
3532 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3535 bmap
= isl_basic_map_simplify(bmap
);
3536 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3537 return isl_basic_map_finalize(bmap
);
3539 isl_basic_map_free(bmap
);
3543 /* Turn the n dimensions of type type, starting at first
3544 * into existentially quantified variables.
3546 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3547 enum isl_dim_type type
, unsigned first
, unsigned n
)
3549 return (isl_basic_set
*)isl_basic_map_project_out(
3550 (isl_basic_map
*)bset
, type
, first
, n
);
3553 /* Turn the n dimensions of type type, starting at first
3554 * into existentially quantified variables.
3556 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3557 enum isl_dim_type type
, unsigned first
, unsigned n
)
3565 return map_space_reset(map
, type
);
3567 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3569 map
= isl_map_cow(map
);
3573 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3577 for (i
= 0; i
< map
->n
; ++i
) {
3578 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3589 /* Turn the n dimensions of type type, starting at first
3590 * into existentially quantified variables.
3592 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3593 enum isl_dim_type type
, unsigned first
, unsigned n
)
3595 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3598 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3602 for (i
= 0; i
< n
; ++i
) {
3603 j
= isl_basic_map_alloc_div(bmap
);
3606 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3610 isl_basic_map_free(bmap
);
3614 struct isl_basic_map
*isl_basic_map_apply_range(
3615 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3617 isl_space
*dim_result
= NULL
;
3618 struct isl_basic_map
*bmap
;
3619 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3620 struct isl_dim_map
*dim_map1
, *dim_map2
;
3622 if (!bmap1
|| !bmap2
)
3624 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3625 bmap2
->dim
, isl_dim_param
))
3626 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3627 "parameters don't match", goto error
);
3628 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3629 bmap2
->dim
, isl_dim_in
))
3630 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3631 "spaces don't match", goto error
);
3633 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3634 isl_space_copy(bmap2
->dim
));
3636 n_in
= isl_basic_map_n_in(bmap1
);
3637 n_out
= isl_basic_map_n_out(bmap2
);
3638 n
= isl_basic_map_n_out(bmap1
);
3639 nparam
= isl_basic_map_n_param(bmap1
);
3641 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3642 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3643 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3644 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3645 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3646 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3647 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3648 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3649 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3650 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3651 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3653 bmap
= isl_basic_map_alloc_space(dim_result
,
3654 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3655 bmap1
->n_eq
+ bmap2
->n_eq
,
3656 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3657 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3658 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3659 bmap
= add_divs(bmap
, n
);
3660 bmap
= isl_basic_map_simplify(bmap
);
3661 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3662 return isl_basic_map_finalize(bmap
);
3664 isl_basic_map_free(bmap1
);
3665 isl_basic_map_free(bmap2
);
3669 struct isl_basic_set
*isl_basic_set_apply(
3670 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3675 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3678 return (struct isl_basic_set
*)
3679 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3681 isl_basic_set_free(bset
);
3682 isl_basic_map_free(bmap
);
3686 struct isl_basic_map
*isl_basic_map_apply_domain(
3687 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3689 if (!bmap1
|| !bmap2
)
3692 isl_assert(bmap1
->ctx
,
3693 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3694 isl_assert(bmap1
->ctx
,
3695 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3698 bmap1
= isl_basic_map_reverse(bmap1
);
3699 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3700 return isl_basic_map_reverse(bmap1
);
3702 isl_basic_map_free(bmap1
);
3703 isl_basic_map_free(bmap2
);
3707 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3708 * A \cap B -> f(A) + f(B)
3710 struct isl_basic_map
*isl_basic_map_sum(
3711 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3713 unsigned n_in
, n_out
, nparam
, total
, pos
;
3714 struct isl_basic_map
*bmap
= NULL
;
3715 struct isl_dim_map
*dim_map1
, *dim_map2
;
3718 if (!bmap1
|| !bmap2
)
3721 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3724 nparam
= isl_basic_map_n_param(bmap1
);
3725 n_in
= isl_basic_map_n_in(bmap1
);
3726 n_out
= isl_basic_map_n_out(bmap1
);
3728 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3729 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3730 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3731 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3732 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3733 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3734 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3735 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3736 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3737 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3738 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3740 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3741 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3742 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3743 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3744 for (i
= 0; i
< n_out
; ++i
) {
3745 int j
= isl_basic_map_alloc_equality(bmap
);
3748 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3749 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3750 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3751 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3753 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3754 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3755 bmap
= add_divs(bmap
, 2 * n_out
);
3757 bmap
= isl_basic_map_simplify(bmap
);
3758 return isl_basic_map_finalize(bmap
);
3760 isl_basic_map_free(bmap
);
3761 isl_basic_map_free(bmap1
);
3762 isl_basic_map_free(bmap2
);
3766 /* Given two maps A -> f(A) and B -> g(B), construct a map
3767 * A \cap B -> f(A) + f(B)
3769 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3771 struct isl_map
*result
;
3777 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3779 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3780 map1
->n
* map2
->n
, 0);
3783 for (i
= 0; i
< map1
->n
; ++i
)
3784 for (j
= 0; j
< map2
->n
; ++j
) {
3785 struct isl_basic_map
*part
;
3786 part
= isl_basic_map_sum(
3787 isl_basic_map_copy(map1
->p
[i
]),
3788 isl_basic_map_copy(map2
->p
[j
]));
3789 if (isl_basic_map_is_empty(part
))
3790 isl_basic_map_free(part
);
3792 result
= isl_map_add_basic_map(result
, part
);
3805 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3806 __isl_take isl_set
*set2
)
3808 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3811 /* Given a basic map A -> f(A), construct A -> -f(A).
3813 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3818 bmap
= isl_basic_map_cow(bmap
);
3822 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3823 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3824 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3825 for (j
= 0; j
< n
; ++j
)
3826 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3827 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3828 for (j
= 0; j
< n
; ++j
)
3829 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3830 for (i
= 0; i
< bmap
->n_div
; ++i
)
3831 for (j
= 0; j
< n
; ++j
)
3832 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3833 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3834 return isl_basic_map_finalize(bmap
);
3837 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3839 return isl_basic_map_neg(bset
);
3842 /* Given a map A -> f(A), construct A -> -f(A).
3844 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3848 map
= isl_map_cow(map
);
3852 for (i
= 0; i
< map
->n
; ++i
) {
3853 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3864 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3866 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3869 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3870 * A -> floor(f(A)/d).
3872 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3875 unsigned n_in
, n_out
, nparam
, total
, pos
;
3876 struct isl_basic_map
*result
= NULL
;
3877 struct isl_dim_map
*dim_map
;
3883 nparam
= isl_basic_map_n_param(bmap
);
3884 n_in
= isl_basic_map_n_in(bmap
);
3885 n_out
= isl_basic_map_n_out(bmap
);
3887 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3888 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3889 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3890 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3891 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3892 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3894 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3895 bmap
->n_div
+ n_out
,
3896 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3897 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3898 result
= add_divs(result
, n_out
);
3899 for (i
= 0; i
< n_out
; ++i
) {
3901 j
= isl_basic_map_alloc_inequality(result
);
3904 isl_seq_clr(result
->ineq
[j
], 1+total
);
3905 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3906 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3907 j
= isl_basic_map_alloc_inequality(result
);
3910 isl_seq_clr(result
->ineq
[j
], 1+total
);
3911 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3912 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3913 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3916 result
= isl_basic_map_simplify(result
);
3917 return isl_basic_map_finalize(result
);
3919 isl_basic_map_free(result
);
3923 /* Given a map A -> f(A) and an integer d, construct a map
3924 * A -> floor(f(A)/d).
3926 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3930 map
= isl_map_cow(map
);
3934 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3935 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3936 for (i
= 0; i
< map
->n
; ++i
) {
3937 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3948 /* Given a map A -> f(A) and an integer d, construct a map
3949 * A -> floor(f(A)/d).
3951 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3952 __isl_take isl_val
*d
)
3956 if (!isl_val_is_int(d
))
3957 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3958 "expecting integer denominator", goto error
);
3959 map
= isl_map_floordiv(map
, d
->n
);
3968 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3974 i
= isl_basic_map_alloc_equality(bmap
);
3977 nparam
= isl_basic_map_n_param(bmap
);
3978 n_in
= isl_basic_map_n_in(bmap
);
3979 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3980 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3981 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3982 return isl_basic_map_finalize(bmap
);
3984 isl_basic_map_free(bmap
);
3988 /* Add a constraints to "bmap" expressing i_pos < o_pos
3990 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3996 i
= isl_basic_map_alloc_inequality(bmap
);
3999 nparam
= isl_basic_map_n_param(bmap
);
4000 n_in
= isl_basic_map_n_in(bmap
);
4001 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4002 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4003 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4004 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4005 return isl_basic_map_finalize(bmap
);
4007 isl_basic_map_free(bmap
);
4011 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4013 static __isl_give isl_basic_map
*var_less_or_equal(
4014 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4020 i
= isl_basic_map_alloc_inequality(bmap
);
4023 nparam
= isl_basic_map_n_param(bmap
);
4024 n_in
= isl_basic_map_n_in(bmap
);
4025 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4026 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4027 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4028 return isl_basic_map_finalize(bmap
);
4030 isl_basic_map_free(bmap
);
4034 /* Add a constraints to "bmap" expressing i_pos > o_pos
4036 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4042 i
= isl_basic_map_alloc_inequality(bmap
);
4045 nparam
= isl_basic_map_n_param(bmap
);
4046 n_in
= isl_basic_map_n_in(bmap
);
4047 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4048 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4049 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4050 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4051 return isl_basic_map_finalize(bmap
);
4053 isl_basic_map_free(bmap
);
4057 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4059 static __isl_give isl_basic_map
*var_more_or_equal(
4060 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4066 i
= isl_basic_map_alloc_inequality(bmap
);
4069 nparam
= isl_basic_map_n_param(bmap
);
4070 n_in
= isl_basic_map_n_in(bmap
);
4071 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4072 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4073 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4074 return isl_basic_map_finalize(bmap
);
4076 isl_basic_map_free(bmap
);
4080 __isl_give isl_basic_map
*isl_basic_map_equal(
4081 __isl_take isl_space
*dim
, unsigned n_equal
)
4084 struct isl_basic_map
*bmap
;
4085 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4088 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4089 bmap
= var_equal(bmap
, i
);
4090 return isl_basic_map_finalize(bmap
);
4093 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4095 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4099 struct isl_basic_map
*bmap
;
4100 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4103 for (i
= 0; i
< pos
&& bmap
; ++i
)
4104 bmap
= var_equal(bmap
, i
);
4106 bmap
= var_less(bmap
, pos
);
4107 return isl_basic_map_finalize(bmap
);
4110 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4112 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4113 __isl_take isl_space
*dim
, unsigned pos
)
4116 isl_basic_map
*bmap
;
4118 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4119 for (i
= 0; i
< pos
; ++i
)
4120 bmap
= var_equal(bmap
, i
);
4121 bmap
= var_less_or_equal(bmap
, pos
);
4122 return isl_basic_map_finalize(bmap
);
4125 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4127 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4131 struct isl_basic_map
*bmap
;
4132 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4135 for (i
= 0; i
< pos
&& bmap
; ++i
)
4136 bmap
= var_equal(bmap
, i
);
4138 bmap
= var_more(bmap
, pos
);
4139 return isl_basic_map_finalize(bmap
);
4142 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4144 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4145 __isl_take isl_space
*dim
, unsigned pos
)
4148 isl_basic_map
*bmap
;
4150 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4151 for (i
= 0; i
< pos
; ++i
)
4152 bmap
= var_equal(bmap
, i
);
4153 bmap
= var_more_or_equal(bmap
, pos
);
4154 return isl_basic_map_finalize(bmap
);
4157 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4158 unsigned n
, int equal
)
4160 struct isl_map
*map
;
4163 if (n
== 0 && equal
)
4164 return isl_map_universe(dims
);
4166 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4168 for (i
= 0; i
+ 1 < n
; ++i
)
4169 map
= isl_map_add_basic_map(map
,
4170 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4173 map
= isl_map_add_basic_map(map
,
4174 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4176 map
= isl_map_add_basic_map(map
,
4177 isl_basic_map_less_at(dims
, n
- 1));
4179 isl_space_free(dims
);
4184 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4188 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4191 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4193 return map_lex_lte_first(dim
, n
, 0);
4196 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4198 return map_lex_lte_first(dim
, n
, 1);
4201 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4203 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4206 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4208 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4211 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4212 unsigned n
, int equal
)
4214 struct isl_map
*map
;
4217 if (n
== 0 && equal
)
4218 return isl_map_universe(dims
);
4220 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4222 for (i
= 0; i
+ 1 < n
; ++i
)
4223 map
= isl_map_add_basic_map(map
,
4224 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4227 map
= isl_map_add_basic_map(map
,
4228 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4230 map
= isl_map_add_basic_map(map
,
4231 isl_basic_map_more_at(dims
, n
- 1));
4233 isl_space_free(dims
);
4238 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4242 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4245 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4247 return map_lex_gte_first(dim
, n
, 0);
4250 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4252 return map_lex_gte_first(dim
, n
, 1);
4255 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4257 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4260 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4262 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4265 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4266 __isl_take isl_set
*set2
)
4269 map
= isl_map_lex_le(isl_set_get_space(set1
));
4270 map
= isl_map_intersect_domain(map
, set1
);
4271 map
= isl_map_intersect_range(map
, set2
);
4275 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4276 __isl_take isl_set
*set2
)
4279 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4280 map
= isl_map_intersect_domain(map
, set1
);
4281 map
= isl_map_intersect_range(map
, set2
);
4285 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4286 __isl_take isl_set
*set2
)
4289 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4290 map
= isl_map_intersect_domain(map
, set1
);
4291 map
= isl_map_intersect_range(map
, set2
);
4295 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4296 __isl_take isl_set
*set2
)
4299 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4300 map
= isl_map_intersect_domain(map
, set1
);
4301 map
= isl_map_intersect_range(map
, set2
);
4305 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4306 __isl_take isl_map
*map2
)
4309 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4310 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4311 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4315 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4316 __isl_take isl_map
*map2
)
4319 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4320 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4321 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4325 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4326 __isl_take isl_map
*map2
)
4329 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4330 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4331 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4335 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4336 __isl_take isl_map
*map2
)
4339 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4340 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4341 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4345 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4346 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4348 struct isl_basic_map
*bmap
;
4350 bset
= isl_basic_set_cow(bset
);
4354 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4355 isl_space_free(bset
->dim
);
4356 bmap
= (struct isl_basic_map
*) bset
;
4358 return isl_basic_map_finalize(bmap
);
4360 isl_basic_set_free(bset
);
4361 isl_space_free(dim
);
4365 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4369 if (bmap
->dim
->n_in
== 0)
4370 return (struct isl_basic_set
*)bmap
;
4371 bmap
= isl_basic_map_cow(bmap
);
4374 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4377 bmap
= isl_basic_map_finalize(bmap
);
4378 return (struct isl_basic_set
*)bmap
;
4380 isl_basic_map_free(bmap
);
4384 /* For a div d = floor(f/m), add the constraint
4388 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4389 unsigned pos
, isl_int
*div
)
4392 unsigned total
= isl_basic_map_total_dim(bmap
);
4394 i
= isl_basic_map_alloc_inequality(bmap
);
4397 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4398 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4403 /* For a div d = floor(f/m), add the constraint
4405 * -(f-(n-1)) + m d >= 0
4407 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4408 unsigned pos
, isl_int
*div
)
4411 unsigned total
= isl_basic_map_total_dim(bmap
);
4413 i
= isl_basic_map_alloc_inequality(bmap
);
4416 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4417 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4418 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4419 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4424 /* For a div d = floor(f/m), add the constraints
4427 * -(f-(n-1)) + m d >= 0
4429 * Note that the second constraint is the negation of
4433 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4434 unsigned pos
, isl_int
*div
)
4436 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4438 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4443 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4444 unsigned pos
, isl_int
*div
)
4446 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4450 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4452 unsigned total
= isl_basic_map_total_dim(bmap
);
4453 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4455 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4459 /* For each known div d = floor(f/m), add the constraints
4462 * -(f-(n-1)) + m d >= 0
4464 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4465 __isl_take isl_basic_map
*bmap
)
4472 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4475 bmap
= isl_basic_map_cow(bmap
);
4476 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4479 for (i
= 0; i
< n_div
; ++i
) {
4480 if (isl_int_is_zero(bmap
->div
[i
][0]))
4482 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4483 return isl_basic_map_free(bmap
);
4486 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4487 bmap
= isl_basic_map_finalize(bmap
);
4491 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4493 * In particular, if this div is of the form d = floor(f/m),
4494 * then add the constraint
4498 * if sign < 0 or the constraint
4500 * -(f-(n-1)) + m d >= 0
4504 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4505 unsigned div
, int sign
)
4513 total
= isl_basic_map_total_dim(bmap
);
4514 div_pos
= total
- bmap
->n_div
+ div
;
4517 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4519 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4522 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4524 return isl_basic_map_add_div_constraints(bset
, div
);
4527 struct isl_basic_set
*isl_basic_map_underlying_set(
4528 struct isl_basic_map
*bmap
)
4532 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4534 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4535 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4536 return (struct isl_basic_set
*)bmap
;
4537 bmap
= isl_basic_map_cow(bmap
);
4540 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4543 bmap
->extra
-= bmap
->n_div
;
4545 bmap
= isl_basic_map_finalize(bmap
);
4546 return (struct isl_basic_set
*)bmap
;
4548 isl_basic_map_free(bmap
);
4552 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4553 __isl_take isl_basic_set
*bset
)
4555 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4558 /* Replace each element in "list" by the result of applying
4559 * isl_basic_map_underlying_set to the element.
4561 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4562 __isl_take isl_basic_map_list
*list
)
4569 n
= isl_basic_map_list_n_basic_map(list
);
4570 for (i
= 0; i
< n
; ++i
) {
4571 isl_basic_map
*bmap
;
4572 isl_basic_set
*bset
;
4574 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4575 bset
= isl_basic_set_underlying_set(bmap
);
4576 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4582 struct isl_basic_map
*isl_basic_map_overlying_set(
4583 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4585 struct isl_basic_map
*bmap
;
4586 struct isl_ctx
*ctx
;
4593 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4594 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4595 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4597 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4598 isl_basic_map_free(like
);
4599 return (struct isl_basic_map
*)bset
;
4601 bset
= isl_basic_set_cow(bset
);
4604 total
= bset
->dim
->n_out
+ bset
->extra
;
4605 bmap
= (struct isl_basic_map
*)bset
;
4606 isl_space_free(bmap
->dim
);
4607 bmap
->dim
= isl_space_copy(like
->dim
);
4610 bmap
->n_div
= like
->n_div
;
4611 bmap
->extra
+= like
->n_div
;
4615 ltotal
= total
- bmap
->extra
+ like
->extra
;
4618 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4619 bmap
->extra
* (1 + 1 + total
));
4620 if (isl_blk_is_error(bmap
->block2
))
4622 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4626 for (i
= 0; i
< bmap
->extra
; ++i
)
4627 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4628 for (i
= 0; i
< like
->n_div
; ++i
) {
4629 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4630 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4632 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4634 isl_basic_map_free(like
);
4635 bmap
= isl_basic_map_simplify(bmap
);
4636 bmap
= isl_basic_map_finalize(bmap
);
4639 isl_basic_map_free(like
);
4640 isl_basic_set_free(bset
);
4644 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4645 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4647 return (struct isl_basic_set
*)
4648 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4651 struct isl_set
*isl_set_from_underlying_set(
4652 struct isl_set
*set
, struct isl_basic_set
*like
)
4658 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4660 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4661 isl_basic_set_free(like
);
4664 set
= isl_set_cow(set
);
4667 for (i
= 0; i
< set
->n
; ++i
) {
4668 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4669 isl_basic_set_copy(like
));
4673 isl_space_free(set
->dim
);
4674 set
->dim
= isl_space_copy(like
->dim
);
4677 isl_basic_set_free(like
);
4680 isl_basic_set_free(like
);
4685 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4689 map
= isl_map_cow(map
);
4692 map
->dim
= isl_space_cow(map
->dim
);
4696 for (i
= 1; i
< map
->n
; ++i
)
4697 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4699 for (i
= 0; i
< map
->n
; ++i
) {
4700 map
->p
[i
] = (struct isl_basic_map
*)
4701 isl_basic_map_underlying_set(map
->p
[i
]);
4706 map
->dim
= isl_space_underlying(map
->dim
, 0);
4708 isl_space_free(map
->dim
);
4709 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4713 return (struct isl_set
*)map
;
4719 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4721 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4724 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4725 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4727 bmap
= isl_basic_map_cow(bmap
);
4731 isl_space_free(bmap
->dim
);
4734 bmap
= isl_basic_map_finalize(bmap
);
4738 isl_basic_map_free(bmap
);
4739 isl_space_free(dim
);
4743 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4744 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4746 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4750 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4751 __isl_take isl_space
*dim
)
4755 map
= isl_map_cow(map
);
4759 for (i
= 0; i
< map
->n
; ++i
) {
4760 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4761 isl_space_copy(dim
));
4765 isl_space_free(map
->dim
);
4771 isl_space_free(dim
);
4775 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4776 __isl_take isl_space
*dim
)
4778 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4781 /* Compute the parameter domain of the given basic set.
4783 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4788 if (isl_basic_set_is_params(bset
))
4791 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4792 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4793 space
= isl_basic_set_get_space(bset
);
4794 space
= isl_space_params(space
);
4795 bset
= isl_basic_set_reset_space(bset
, space
);
4799 /* Construct a zero-dimensional basic set with the given parameter domain.
4801 __isl_give isl_basic_set
*isl_basic_set_from_params(
4802 __isl_take isl_basic_set
*bset
)
4805 space
= isl_basic_set_get_space(bset
);
4806 space
= isl_space_set_from_params(space
);
4807 bset
= isl_basic_set_reset_space(bset
, space
);
4811 /* Compute the parameter domain of the given set.
4813 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4818 if (isl_set_is_params(set
))
4821 n
= isl_set_dim(set
, isl_dim_set
);
4822 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4823 space
= isl_set_get_space(set
);
4824 space
= isl_space_params(space
);
4825 set
= isl_set_reset_space(set
, space
);
4829 /* Construct a zero-dimensional set with the given parameter domain.
4831 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4834 space
= isl_set_get_space(set
);
4835 space
= isl_space_set_from_params(space
);
4836 set
= isl_set_reset_space(set
, space
);
4840 /* Compute the parameter domain of the given map.
4842 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4847 n
= isl_map_dim(map
, isl_dim_in
);
4848 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4849 n
= isl_map_dim(map
, isl_dim_out
);
4850 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4851 space
= isl_map_get_space(map
);
4852 space
= isl_space_params(space
);
4853 map
= isl_map_reset_space(map
, space
);
4857 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4860 struct isl_basic_set
*domain
;
4866 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4868 n_in
= isl_basic_map_n_in(bmap
);
4869 n_out
= isl_basic_map_n_out(bmap
);
4870 domain
= isl_basic_set_from_basic_map(bmap
);
4871 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4873 domain
= isl_basic_set_reset_space(domain
, dim
);
4878 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4882 return isl_space_may_be_set(bmap
->dim
);
4885 /* Is this basic map actually a set?
4886 * Users should never call this function. Outside of isl,
4887 * the type should indicate whether something is a set or a map.
4889 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4893 return isl_space_is_set(bmap
->dim
);
4896 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4900 if (isl_basic_map_is_set(bmap
))
4902 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4905 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4906 __isl_take isl_basic_map
*bmap
)
4910 isl_basic_map
*domain
;
4911 int nparam
, n_in
, n_out
;
4914 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4915 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4916 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4918 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4919 domain
= isl_basic_map_universe(dim
);
4921 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4922 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4923 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4925 total
= isl_basic_map_total_dim(bmap
);
4927 for (i
= 0; i
< n_in
; ++i
) {
4928 k
= isl_basic_map_alloc_equality(bmap
);
4931 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4932 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4933 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4936 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4937 return isl_basic_map_finalize(bmap
);
4939 isl_basic_map_free(bmap
);
4943 __isl_give isl_basic_map
*isl_basic_map_range_map(
4944 __isl_take isl_basic_map
*bmap
)
4948 isl_basic_map
*range
;
4949 int nparam
, n_in
, n_out
;
4952 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4953 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4954 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4956 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4957 range
= isl_basic_map_universe(dim
);
4959 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4960 bmap
= isl_basic_map_apply_range(bmap
, range
);
4961 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4963 total
= isl_basic_map_total_dim(bmap
);
4965 for (i
= 0; i
< n_out
; ++i
) {
4966 k
= isl_basic_map_alloc_equality(bmap
);
4969 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4970 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4971 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4974 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4975 return isl_basic_map_finalize(bmap
);
4977 isl_basic_map_free(bmap
);
4981 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4985 return isl_space_may_be_set(map
->dim
);
4988 /* Is this map actually a set?
4989 * Users should never call this function. Outside of isl,
4990 * the type should indicate whether something is a set or a map.
4992 int isl_map_is_set(__isl_keep isl_map
*map
)
4996 return isl_space_is_set(map
->dim
);
4999 struct isl_set
*isl_map_range(struct isl_map
*map
)
5002 struct isl_set
*set
;
5006 if (isl_map_is_set(map
))
5007 return (isl_set
*)map
;
5009 map
= isl_map_cow(map
);
5013 set
= (struct isl_set
*) map
;
5014 set
->dim
= isl_space_range(set
->dim
);
5017 for (i
= 0; i
< map
->n
; ++i
) {
5018 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5022 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5023 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5030 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5034 map
= isl_map_cow(map
);
5038 map
->dim
= isl_space_domain_map(map
->dim
);
5041 for (i
= 0; i
< map
->n
; ++i
) {
5042 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5046 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5047 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5054 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5057 isl_space
*range_dim
;
5059 map
= isl_map_cow(map
);
5063 range_dim
= isl_space_range(isl_map_get_space(map
));
5064 range_dim
= isl_space_from_range(range_dim
);
5065 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5066 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5069 for (i
= 0; i
< map
->n
; ++i
) {
5070 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5074 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5075 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5082 /* Given a wrapped map of the form A[B -> C],
5083 * return the map A[B -> C] -> B.
5085 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5092 if (!isl_set_has_tuple_id(set
))
5093 return isl_map_domain_map(isl_set_unwrap(set
));
5095 id
= isl_set_get_tuple_id(set
);
5096 map
= isl_map_domain_map(isl_set_unwrap(set
));
5097 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5102 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5103 __isl_take isl_space
*dim
)
5106 struct isl_map
*map
= NULL
;
5108 set
= isl_set_cow(set
);
5111 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5112 map
= (struct isl_map
*)set
;
5113 for (i
= 0; i
< set
->n
; ++i
) {
5114 map
->p
[i
] = isl_basic_map_from_basic_set(
5115 set
->p
[i
], isl_space_copy(dim
));
5119 isl_space_free(map
->dim
);
5123 isl_space_free(dim
);
5128 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5129 __isl_take isl_basic_set
*bset
)
5131 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5134 __isl_give isl_basic_map
*isl_basic_map_from_range(
5135 __isl_take isl_basic_set
*bset
)
5138 space
= isl_basic_set_get_space(bset
);
5139 space
= isl_space_from_range(space
);
5140 bset
= isl_basic_set_reset_space(bset
, space
);
5141 return (isl_basic_map
*)bset
;
5144 /* Create a relation with the given set as range.
5145 * The domain of the created relation is a zero-dimensional
5146 * flat anonymous space.
5148 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5151 space
= isl_set_get_space(set
);
5152 space
= isl_space_from_range(space
);
5153 set
= isl_set_reset_space(set
, space
);
5154 return (struct isl_map
*)set
;
5157 /* Create a relation with the given set as domain.
5158 * The range of the created relation is a zero-dimensional
5159 * flat anonymous space.
5161 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5163 return isl_map_reverse(isl_map_from_range(set
));
5166 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5167 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5169 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5172 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5173 __isl_take isl_set
*range
)
5175 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5178 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5181 struct isl_map
*map
;
5186 isl_die(dim
->ctx
, isl_error_internal
,
5187 "negative number of basic maps", goto error
);
5188 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5189 sizeof(struct isl_map
) +
5190 (n
- 1) * sizeof(struct isl_basic_map
*));
5194 map
->ctx
= dim
->ctx
;
5195 isl_ctx_ref(map
->ctx
);
5203 isl_space_free(dim
);
5207 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5208 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5211 struct isl_map
*map
;
5214 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5218 map
= isl_map_alloc_space(dims
, n
, flags
);
5222 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5224 struct isl_basic_map
*bmap
;
5225 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5226 bmap
= isl_basic_map_set_to_empty(bmap
);
5230 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5232 struct isl_basic_set
*bset
;
5233 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5234 bset
= isl_basic_set_set_to_empty(bset
);
5238 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5240 struct isl_basic_map
*bmap
;
5241 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5242 bmap
= isl_basic_map_finalize(bmap
);
5246 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5248 struct isl_basic_set
*bset
;
5249 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5250 bset
= isl_basic_set_finalize(bset
);
5254 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5257 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5258 isl_basic_map
*bmap
;
5260 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5261 for (i
= 0; i
< total
; ++i
) {
5262 int k
= isl_basic_map_alloc_inequality(bmap
);
5265 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5266 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5270 isl_basic_map_free(bmap
);
5274 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5276 return isl_basic_map_nat_universe(dim
);
5279 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5281 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5284 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5286 return isl_map_nat_universe(dim
);
5289 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5291 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5294 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5296 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5299 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5301 struct isl_map
*map
;
5304 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5305 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5309 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5311 struct isl_set
*set
;
5314 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5315 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5319 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5322 struct isl_map
*dup
;
5326 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5327 for (i
= 0; i
< map
->n
; ++i
)
5328 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5332 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5333 __isl_take isl_basic_map
*bmap
)
5337 if (isl_basic_map_plain_is_empty(bmap
)) {
5338 isl_basic_map_free(bmap
);
5341 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5342 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5343 map
->p
[map
->n
] = bmap
;
5345 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5351 isl_basic_map_free(bmap
);
5355 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5365 isl_ctx_deref(map
->ctx
);
5366 for (i
= 0; i
< map
->n
; ++i
)
5367 isl_basic_map_free(map
->p
[i
]);
5368 isl_space_free(map
->dim
);
5374 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5375 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5379 bmap
= isl_basic_map_cow(bmap
);
5380 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5381 j
= isl_basic_map_alloc_equality(bmap
);
5384 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5385 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5386 isl_int_set_si(bmap
->eq
[j
][0], value
);
5387 bmap
= isl_basic_map_simplify(bmap
);
5388 return isl_basic_map_finalize(bmap
);
5390 isl_basic_map_free(bmap
);
5394 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5395 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5399 bmap
= isl_basic_map_cow(bmap
);
5400 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5401 j
= isl_basic_map_alloc_equality(bmap
);
5404 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5405 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5406 isl_int_set(bmap
->eq
[j
][0], value
);
5407 bmap
= isl_basic_map_simplify(bmap
);
5408 return isl_basic_map_finalize(bmap
);
5410 isl_basic_map_free(bmap
);
5414 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5415 enum isl_dim_type type
, unsigned pos
, int value
)
5419 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5420 return isl_basic_map_fix_pos_si(bmap
,
5421 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5423 isl_basic_map_free(bmap
);
5427 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5428 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5432 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5433 return isl_basic_map_fix_pos(bmap
,
5434 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5436 isl_basic_map_free(bmap
);
5440 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5441 * to be equal to "v".
5443 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5444 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5448 if (!isl_val_is_int(v
))
5449 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5450 "expecting integer value", goto error
);
5451 if (pos
>= isl_basic_map_dim(bmap
, type
))
5452 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5453 "index out of bounds", goto error
);
5454 pos
+= isl_basic_map_offset(bmap
, type
);
5455 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5459 isl_basic_map_free(bmap
);
5464 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5465 * to be equal to "v".
5467 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5468 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5470 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5473 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5474 enum isl_dim_type type
, unsigned pos
, int value
)
5476 return (struct isl_basic_set
*)
5477 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5481 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5482 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5484 return (struct isl_basic_set
*)
5485 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5489 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5490 unsigned input
, int value
)
5492 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5495 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5496 unsigned dim
, int value
)
5498 return (struct isl_basic_set
*)
5499 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5500 isl_dim_set
, dim
, value
);
5503 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5505 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5512 isl_basic_map_free(map
->p
[i
]);
5513 if (i
!= map
->n
- 1) {
5514 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5515 map
->p
[i
] = map
->p
[map
->n
- 1];
5522 /* Perform "fn" on each basic map of "map", where we may not be holding
5523 * the only reference to "map".
5524 * In particular, "fn" should be a semantics preserving operation
5525 * that we want to apply to all copies of "map". We therefore need
5526 * to be careful not to modify "map" in a way that breaks "map"
5527 * in case anything goes wrong.
5529 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5530 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5532 struct isl_basic_map
*bmap
;
5538 for (i
= map
->n
- 1; i
>= 0; --i
) {
5539 bmap
= isl_basic_map_copy(map
->p
[i
]);
5543 isl_basic_map_free(map
->p
[i
]);
5545 if (remove_if_empty(map
, i
) < 0)
5555 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5556 enum isl_dim_type type
, unsigned pos
, int value
)
5560 map
= isl_map_cow(map
);
5564 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5565 for (i
= map
->n
- 1; i
>= 0; --i
) {
5566 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5567 if (remove_if_empty(map
, i
) < 0)
5570 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5577 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5578 enum isl_dim_type type
, unsigned pos
, int value
)
5580 return (struct isl_set
*)
5581 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5584 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5585 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5589 map
= isl_map_cow(map
);
5593 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5594 for (i
= 0; i
< map
->n
; ++i
) {
5595 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5599 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5606 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5607 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5609 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5612 /* Fix the value of the variable at position "pos" of type "type" of "map"
5613 * to be equal to "v".
5615 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5616 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5620 map
= isl_map_cow(map
);
5624 if (!isl_val_is_int(v
))
5625 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5626 "expecting integer value", goto error
);
5627 if (pos
>= isl_map_dim(map
, type
))
5628 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5629 "index out of bounds", goto error
);
5630 for (i
= map
->n
- 1; i
>= 0; --i
) {
5631 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5633 if (remove_if_empty(map
, i
) < 0)
5636 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5645 /* Fix the value of the variable at position "pos" of type "type" of "set"
5646 * to be equal to "v".
5648 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5649 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5651 return isl_map_fix_val(set
, type
, pos
, v
);
5654 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5655 unsigned input
, int value
)
5657 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5660 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5662 return (struct isl_set
*)
5663 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5666 static __isl_give isl_basic_map
*basic_map_bound_si(
5667 __isl_take isl_basic_map
*bmap
,
5668 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5674 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5675 pos
+= isl_basic_map_offset(bmap
, type
);
5676 bmap
= isl_basic_map_cow(bmap
);
5677 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5678 j
= isl_basic_map_alloc_inequality(bmap
);
5681 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5683 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5684 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5686 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5687 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5689 bmap
= isl_basic_map_simplify(bmap
);
5690 return isl_basic_map_finalize(bmap
);
5692 isl_basic_map_free(bmap
);
5696 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5697 __isl_take isl_basic_map
*bmap
,
5698 enum isl_dim_type type
, unsigned pos
, int value
)
5700 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5703 /* Constrain the values of the given dimension to be no greater than "value".
5705 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5706 __isl_take isl_basic_map
*bmap
,
5707 enum isl_dim_type type
, unsigned pos
, int value
)
5709 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5712 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5713 unsigned dim
, isl_int value
)
5717 bset
= isl_basic_set_cow(bset
);
5718 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5719 j
= isl_basic_set_alloc_inequality(bset
);
5722 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5723 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5724 isl_int_neg(bset
->ineq
[j
][0], value
);
5725 bset
= isl_basic_set_simplify(bset
);
5726 return isl_basic_set_finalize(bset
);
5728 isl_basic_set_free(bset
);
5732 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5733 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5737 map
= isl_map_cow(map
);
5741 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5742 for (i
= 0; i
< map
->n
; ++i
) {
5743 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5744 type
, pos
, value
, upper
);
5748 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5755 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5756 enum isl_dim_type type
, unsigned pos
, int value
)
5758 return map_bound_si(map
, type
, pos
, value
, 0);
5761 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5762 enum isl_dim_type type
, unsigned pos
, int value
)
5764 return map_bound_si(map
, type
, pos
, value
, 1);
5767 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5768 enum isl_dim_type type
, unsigned pos
, int value
)
5770 return (struct isl_set
*)
5771 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5774 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5775 enum isl_dim_type type
, unsigned pos
, int value
)
5777 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5780 /* Bound the given variable of "bmap" from below (or above is "upper"
5781 * is set) to "value".
5783 static __isl_give isl_basic_map
*basic_map_bound(
5784 __isl_take isl_basic_map
*bmap
,
5785 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5791 if (pos
>= isl_basic_map_dim(bmap
, type
))
5792 isl_die(bmap
->ctx
, isl_error_invalid
,
5793 "index out of bounds", goto error
);
5794 pos
+= isl_basic_map_offset(bmap
, type
);
5795 bmap
= isl_basic_map_cow(bmap
);
5796 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5797 j
= isl_basic_map_alloc_inequality(bmap
);
5800 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5802 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5803 isl_int_set(bmap
->ineq
[j
][0], value
);
5805 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5806 isl_int_neg(bmap
->ineq
[j
][0], value
);
5808 bmap
= isl_basic_map_simplify(bmap
);
5809 return isl_basic_map_finalize(bmap
);
5811 isl_basic_map_free(bmap
);
5815 /* Bound the given variable of "map" from below (or above is "upper"
5816 * is set) to "value".
5818 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5819 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5823 map
= isl_map_cow(map
);
5827 if (pos
>= isl_map_dim(map
, type
))
5828 isl_die(map
->ctx
, isl_error_invalid
,
5829 "index out of bounds", goto error
);
5830 for (i
= map
->n
- 1; i
>= 0; --i
) {
5831 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5832 if (remove_if_empty(map
, i
) < 0)
5835 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5842 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5843 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5845 return map_bound(map
, type
, pos
, value
, 0);
5848 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5849 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5851 return map_bound(map
, type
, pos
, value
, 1);
5854 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5855 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5857 return isl_map_lower_bound(set
, type
, pos
, value
);
5860 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5861 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5863 return isl_map_upper_bound(set
, type
, pos
, value
);
5866 /* Force the values of the variable at position "pos" of type "type" of "set"
5867 * to be no smaller than "value".
5869 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5870 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5874 if (!isl_val_is_int(value
))
5875 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5876 "expecting integer value", goto error
);
5877 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5878 isl_val_free(value
);
5881 isl_val_free(value
);
5886 /* Force the values of the variable at position "pos" of type "type" of "set"
5887 * to be no greater than "value".
5889 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5890 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5894 if (!isl_val_is_int(value
))
5895 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5896 "expecting integer value", goto error
);
5897 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5898 isl_val_free(value
);
5901 isl_val_free(value
);
5906 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5911 set
= isl_set_cow(set
);
5915 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5916 for (i
= 0; i
< set
->n
; ++i
) {
5917 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5927 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5931 map
= isl_map_cow(map
);
5935 map
->dim
= isl_space_reverse(map
->dim
);
5938 for (i
= 0; i
< map
->n
; ++i
) {
5939 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5943 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5950 static struct isl_map
*isl_basic_map_partial_lexopt(
5951 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5952 struct isl_set
**empty
, int max
)
5954 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5957 struct isl_map
*isl_basic_map_partial_lexmax(
5958 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5959 struct isl_set
**empty
)
5961 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5964 struct isl_map
*isl_basic_map_partial_lexmin(
5965 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5966 struct isl_set
**empty
)
5968 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5971 struct isl_set
*isl_basic_set_partial_lexmin(
5972 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5973 struct isl_set
**empty
)
5975 return (struct isl_set
*)
5976 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5980 struct isl_set
*isl_basic_set_partial_lexmax(
5981 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5982 struct isl_set
**empty
)
5984 return (struct isl_set
*)
5985 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5989 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5990 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5991 __isl_give isl_set
**empty
)
5993 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5996 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5997 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5998 __isl_give isl_set
**empty
)
6000 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6003 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6004 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6005 __isl_give isl_set
**empty
)
6007 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6010 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6011 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6012 __isl_give isl_set
**empty
)
6014 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6017 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6018 __isl_take isl_basic_map
*bmap
, int max
)
6020 isl_basic_set
*dom
= NULL
;
6021 isl_space
*dom_space
;
6025 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6026 dom
= isl_basic_set_universe(dom_space
);
6027 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6029 isl_basic_map_free(bmap
);
6033 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6034 __isl_take isl_basic_map
*bmap
)
6036 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6040 #define TYPE isl_pw_multi_aff
6042 #define SUFFIX _pw_multi_aff
6044 #define EMPTY isl_pw_multi_aff_empty
6046 #define ADD isl_pw_multi_aff_union_add
6047 #include "isl_map_lexopt_templ.c"
6049 /* Given a map "map", compute the lexicographically minimal
6050 * (or maximal) image element for each domain element in dom,
6051 * in the form of an isl_pw_multi_aff.
6052 * Set *empty to those elements in dom that do not have an image element.
6054 * We first compute the lexicographically minimal or maximal element
6055 * in the first basic map. This results in a partial solution "res"
6056 * and a subset "todo" of dom that still need to be handled.
6057 * We then consider each of the remaining maps in "map" and successively
6058 * update both "res" and "todo".
6060 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6061 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6062 __isl_give isl_set
**empty
, int max
)
6065 isl_pw_multi_aff
*res
;
6071 if (isl_map_plain_is_empty(map
)) {
6076 return isl_pw_multi_aff_from_map(map
);
6079 res
= basic_map_partial_lexopt_pw_multi_aff(
6080 isl_basic_map_copy(map
->p
[0]),
6081 isl_set_copy(dom
), &todo
, max
);
6083 for (i
= 1; i
< map
->n
; ++i
) {
6084 isl_pw_multi_aff
*res_i
;
6087 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6088 isl_basic_map_copy(map
->p
[i
]),
6089 isl_set_copy(dom
), &todo_i
, max
);
6092 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6094 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6096 todo
= isl_set_intersect(todo
, todo_i
);
6117 #define TYPE isl_map
6121 #define EMPTY isl_map_empty
6123 #define ADD isl_map_union_disjoint
6124 #include "isl_map_lexopt_templ.c"
6126 /* Given a map "map", compute the lexicographically minimal
6127 * (or maximal) image element for each domain element in dom.
6128 * Set *empty to those elements in dom that do not have an image element.
6130 * We first compute the lexicographically minimal or maximal element
6131 * in the first basic map. This results in a partial solution "res"
6132 * and a subset "todo" of dom that still need to be handled.
6133 * We then consider each of the remaining maps in "map" and successively
6134 * update both "res" and "todo".
6136 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6137 * Assume we are computing the lexicographical maximum.
6138 * We first compute the lexicographically maximal element in basic map i.
6139 * This results in a partial solution res_i and a subset todo_i.
6140 * Then we combine these results with those obtain for the first k basic maps
6141 * to obtain a result that is valid for the first k+1 basic maps.
6142 * In particular, the set where there is no solution is the set where
6143 * there is no solution for the first k basic maps and also no solution
6144 * for the ith basic map, i.e.,
6146 * todo^i = todo^k * todo_i
6148 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6149 * solutions, arbitrarily breaking ties in favor of res^k.
6150 * That is, when res^k(a) >= res_i(a), we pick res^k and
6151 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6152 * the lexicographic order.)
6153 * In practice, we compute
6155 * res^k * (res_i . "<=")
6159 * res_i * (res^k . "<")
6161 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6162 * where only one of res^k and res_i provides a solution and we simply pick
6169 * Note that we only compute these intersections when dom(res^k) intersects
6170 * dom(res_i). Otherwise, the only effect of these intersections is to
6171 * potentially break up res^k and res_i into smaller pieces.
6172 * We want to avoid such splintering as much as possible.
6173 * In fact, an earlier implementation of this function would look for
6174 * better results in the domain of res^k and for extra results in todo^k,
6175 * but this would always result in a splintering according to todo^k,
6176 * even when the domain of basic map i is disjoint from the domains of
6177 * the previous basic maps.
6179 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6180 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6181 __isl_give isl_set
**empty
, int max
)
6184 struct isl_map
*res
;
6185 struct isl_set
*todo
;
6190 if (isl_map_plain_is_empty(map
)) {
6198 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6199 isl_set_copy(dom
), &todo
, max
);
6201 for (i
= 1; i
< map
->n
; ++i
) {
6205 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6207 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6208 isl_set_copy(dom
), &todo_i
, max
);
6211 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6212 le
= isl_map_lex_le(dim
);
6214 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6215 le
= isl_map_lex_ge(dim
);
6217 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6218 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6219 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6220 le
= isl_map_intersect(le
, isl_map_copy(res
));
6222 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6223 res
= isl_map_intersect_domain(res
,
6224 isl_set_copy(todo_i
));
6225 res_i
= isl_map_intersect_domain(res_i
,
6226 isl_set_copy(todo
));
6229 res
= isl_map_union_disjoint(res
, res_i
);
6230 res
= isl_map_union_disjoint(res
, lt
);
6231 res
= isl_map_union_disjoint(res
, le
);
6233 todo
= isl_set_intersect(todo
, todo_i
);
6253 __isl_give isl_map
*isl_map_partial_lexmax(
6254 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6255 __isl_give isl_set
**empty
)
6257 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6260 __isl_give isl_map
*isl_map_partial_lexmin(
6261 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6262 __isl_give isl_set
**empty
)
6264 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6267 __isl_give isl_set
*isl_set_partial_lexmin(
6268 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6269 __isl_give isl_set
**empty
)
6271 return (struct isl_set
*)
6272 isl_map_partial_lexmin((struct isl_map
*)set
,
6276 __isl_give isl_set
*isl_set_partial_lexmax(
6277 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6278 __isl_give isl_set
**empty
)
6280 return (struct isl_set
*)
6281 isl_map_partial_lexmax((struct isl_map
*)set
,
6285 /* Compute the lexicographic minimum (or maximum if "max" is set)
6286 * of "bmap" over its domain.
6288 * Since we are not interested in the part of the domain space where
6289 * there is no solution, we initialize the domain to those constraints
6290 * of "bmap" that only involve the parameters and the input dimensions.
6291 * This relieves the parametric programming engine from detecting those
6292 * inequalities and transferring them to the context. More importantly,
6293 * it ensures that those inequalities are transferred first and not
6294 * intermixed with inequalities that actually split the domain.
6296 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6300 isl_basic_map
*copy
;
6303 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6304 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6305 copy
= isl_basic_map_copy(bmap
);
6306 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6307 isl_dim_div
, 0, n_div
);
6308 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6309 isl_dim_out
, 0, n_out
);
6310 dom
= isl_basic_map_domain(copy
);
6311 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6314 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6316 return isl_basic_map_lexopt(bmap
, 0);
6319 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6321 return isl_basic_map_lexopt(bmap
, 1);
6324 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6326 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6329 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6331 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6334 /* Extract the first and only affine expression from list
6335 * and then add it to *pwaff with the given dom.
6336 * This domain is known to be disjoint from other domains
6337 * because of the way isl_basic_map_foreach_lexmax works.
6339 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6340 __isl_take isl_aff_list
*list
, void *user
)
6342 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6344 isl_pw_aff
**pwaff
= user
;
6345 isl_pw_aff
*pwaff_i
;
6349 if (isl_aff_list_n_aff(list
) != 1)
6350 isl_die(ctx
, isl_error_internal
,
6351 "expecting single element list", goto error
);
6353 aff
= isl_aff_list_get_aff(list
, 0);
6354 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6356 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6358 isl_aff_list_free(list
);
6362 isl_basic_set_free(dom
);
6363 isl_aff_list_free(list
);
6367 /* Given a basic map with one output dimension, compute the minimum or
6368 * maximum of that dimension as an isl_pw_aff.
6370 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6371 * call update_dim_opt on each leaf of the result.
6373 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6376 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6380 dim
= isl_space_from_domain(isl_space_domain(dim
));
6381 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6382 pwaff
= isl_pw_aff_empty(dim
);
6384 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6386 return isl_pw_aff_free(pwaff
);
6391 /* Compute the minimum or maximum of the given output dimension
6392 * as a function of the parameters and the input dimensions,
6393 * but independently of the other output dimensions.
6395 * We first project out the other output dimension and then compute
6396 * the "lexicographic" maximum in each basic map, combining the results
6397 * using isl_pw_aff_union_max.
6399 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6406 n_out
= isl_map_dim(map
, isl_dim_out
);
6407 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6408 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6413 isl_space
*dim
= isl_map_get_space(map
);
6415 return isl_pw_aff_empty(dim
);
6418 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6419 for (i
= 1; i
< map
->n
; ++i
) {
6420 isl_pw_aff
*pwaff_i
;
6422 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6423 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6431 /* Compute the maximum of the given output dimension as a function of the
6432 * parameters and input dimensions, but independently of
6433 * the other output dimensions.
6435 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6437 return map_dim_opt(map
, pos
, 1);
6440 /* Compute the minimum or maximum of the given set dimension
6441 * as a function of the parameters,
6442 * but independently of the other set dimensions.
6444 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6447 return map_dim_opt(set
, pos
, max
);
6450 /* Compute the maximum of the given set dimension as a function of the
6451 * parameters, but independently of the other set dimensions.
6453 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6455 return set_dim_opt(set
, pos
, 1);
6458 /* Compute the minimum of the given set dimension as a function of the
6459 * parameters, but independently of the other set dimensions.
6461 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6463 return set_dim_opt(set
, pos
, 0);
6466 /* Apply a preimage specified by "mat" on the parameters of "bset".
6467 * bset is assumed to have only parameters and divs.
6469 static struct isl_basic_set
*basic_set_parameter_preimage(
6470 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6477 bset
->dim
= isl_space_cow(bset
->dim
);
6481 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6483 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6485 bset
->dim
->nparam
= 0;
6486 bset
->dim
->n_out
= nparam
;
6487 bset
= isl_basic_set_preimage(bset
, mat
);
6489 bset
->dim
->nparam
= bset
->dim
->n_out
;
6490 bset
->dim
->n_out
= 0;
6495 isl_basic_set_free(bset
);
6499 /* Apply a preimage specified by "mat" on the parameters of "set".
6500 * set is assumed to have only parameters and divs.
6502 static struct isl_set
*set_parameter_preimage(
6503 struct isl_set
*set
, struct isl_mat
*mat
)
6505 isl_space
*dim
= NULL
;
6511 dim
= isl_space_copy(set
->dim
);
6512 dim
= isl_space_cow(dim
);
6516 nparam
= isl_set_dim(set
, isl_dim_param
);
6518 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6521 dim
->n_out
= nparam
;
6522 isl_set_reset_space(set
, dim
);
6523 set
= isl_set_preimage(set
, mat
);
6526 dim
= isl_space_copy(set
->dim
);
6527 dim
= isl_space_cow(dim
);
6530 dim
->nparam
= dim
->n_out
;
6532 isl_set_reset_space(set
, dim
);
6535 isl_space_free(dim
);
6542 /* Intersect the basic set "bset" with the affine space specified by the
6543 * equalities in "eq".
6545 static struct isl_basic_set
*basic_set_append_equalities(
6546 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6554 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6559 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6560 for (i
= 0; i
< eq
->n_row
; ++i
) {
6561 k
= isl_basic_set_alloc_equality(bset
);
6564 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6565 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6569 bset
= isl_basic_set_gauss(bset
, NULL
);
6570 bset
= isl_basic_set_finalize(bset
);
6575 isl_basic_set_free(bset
);
6579 /* Intersect the set "set" with the affine space specified by the
6580 * equalities in "eq".
6582 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6590 for (i
= 0; i
< set
->n
; ++i
) {
6591 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6604 /* Given a basic set "bset" that only involves parameters and existentially
6605 * quantified variables, return the index of the first equality
6606 * that only involves parameters. If there is no such equality then
6607 * return bset->n_eq.
6609 * This function assumes that isl_basic_set_gauss has been called on "bset".
6611 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6614 unsigned nparam
, n_div
;
6619 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6620 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6622 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6623 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6630 /* Compute an explicit representation for the existentially quantified
6631 * variables in "bset" by computing the "minimal value" of the set
6632 * variables. Since there are no set variables, the computation of
6633 * the minimal value essentially computes an explicit representation
6634 * of the non-empty part(s) of "bset".
6636 * The input only involves parameters and existentially quantified variables.
6637 * All equalities among parameters have been removed.
6639 * Since the existentially quantified variables in the result are in general
6640 * going to be different from those in the input, we first replace
6641 * them by the minimal number of variables based on their equalities.
6642 * This should simplify the parametric integer programming.
6644 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6646 isl_morph
*morph1
, *morph2
;
6652 if (bset
->n_eq
== 0)
6653 return isl_basic_set_lexmin(bset
);
6655 morph1
= isl_basic_set_parameter_compression(bset
);
6656 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6657 bset
= isl_basic_set_lift(bset
);
6658 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6659 bset
= isl_morph_basic_set(morph2
, bset
);
6660 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6661 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6663 set
= isl_basic_set_lexmin(bset
);
6665 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6670 /* Project the given basic set onto its parameter domain, possibly introducing
6671 * new, explicit, existential variables in the constraints.
6672 * The input has parameters and (possibly implicit) existential variables.
6673 * The output has the same parameters, but only
6674 * explicit existentially quantified variables.
6676 * The actual projection is performed by pip, but pip doesn't seem
6677 * to like equalities very much, so we first remove the equalities
6678 * among the parameters by performing a variable compression on
6679 * the parameters. Afterward, an inverse transformation is performed
6680 * and the equalities among the parameters are inserted back in.
6682 * The variable compression on the parameters may uncover additional
6683 * equalities that were only implicit before. We therefore check
6684 * if there are any new parameter equalities in the result and
6685 * if so recurse. The removal of parameter equalities is required
6686 * for the parameter compression performed by base_compute_divs.
6688 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6692 struct isl_mat
*T
, *T2
;
6693 struct isl_set
*set
;
6696 bset
= isl_basic_set_cow(bset
);
6700 if (bset
->n_eq
== 0)
6701 return base_compute_divs(bset
);
6703 bset
= isl_basic_set_gauss(bset
, NULL
);
6706 if (isl_basic_set_plain_is_empty(bset
))
6707 return isl_set_from_basic_set(bset
);
6709 i
= first_parameter_equality(bset
);
6710 if (i
== bset
->n_eq
)
6711 return base_compute_divs(bset
);
6713 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6714 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6716 eq
= isl_mat_cow(eq
);
6717 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6718 if (T
&& T
->n_col
== 0) {
6722 bset
= isl_basic_set_set_to_empty(bset
);
6723 return isl_set_from_basic_set(bset
);
6725 bset
= basic_set_parameter_preimage(bset
, T
);
6727 i
= first_parameter_equality(bset
);
6730 else if (i
== bset
->n_eq
)
6731 set
= base_compute_divs(bset
);
6733 set
= parameter_compute_divs(bset
);
6734 set
= set_parameter_preimage(set
, T2
);
6735 set
= set_append_equalities(set
, eq
);
6739 /* Insert the divs from "ls" before those of "bmap".
6741 * The number of columns is not changed, which means that the last
6742 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6743 * The caller is responsible for removing the same number of dimensions
6744 * from the space of "bmap".
6746 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6747 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6753 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6757 old_n_div
= bmap
->n_div
;
6758 bmap
= insert_div_rows(bmap
, n_div
);
6762 for (i
= 0; i
< n_div
; ++i
) {
6763 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6764 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6770 /* Replace the space of "bmap" by the space and divs of "ls".
6772 * If "ls" has any divs, then we simplify the result since we may
6773 * have discovered some additional equalities that could simplify
6774 * the div expressions.
6776 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6777 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6781 bmap
= isl_basic_map_cow(bmap
);
6785 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6786 bmap
= insert_divs_from_local_space(bmap
, ls
);
6790 isl_space_free(bmap
->dim
);
6791 bmap
->dim
= isl_local_space_get_space(ls
);
6795 isl_local_space_free(ls
);
6797 bmap
= isl_basic_map_simplify(bmap
);
6798 bmap
= isl_basic_map_finalize(bmap
);
6801 isl_basic_map_free(bmap
);
6802 isl_local_space_free(ls
);
6806 /* Replace the space of "map" by the space and divs of "ls".
6808 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6809 __isl_take isl_local_space
*ls
)
6813 map
= isl_map_cow(map
);
6817 for (i
= 0; i
< map
->n
; ++i
) {
6818 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6819 isl_local_space_copy(ls
));
6823 isl_space_free(map
->dim
);
6824 map
->dim
= isl_local_space_get_space(ls
);
6828 isl_local_space_free(ls
);
6831 isl_local_space_free(ls
);
6836 /* Compute an explicit representation for the existentially
6837 * quantified variables for which do not know any explicit representation yet.
6839 * We first sort the existentially quantified variables so that the
6840 * existentially quantified variables for which we already have an explicit
6841 * representation are placed before those for which we do not.
6842 * The input dimensions, the output dimensions and the existentially
6843 * quantified variables for which we already have an explicit
6844 * representation are then turned into parameters.
6845 * compute_divs returns a map with the same parameters and
6846 * no input or output dimensions and the dimension specification
6847 * is reset to that of the input, including the existentially quantified
6848 * variables for which we already had an explicit representation.
6850 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6852 struct isl_basic_set
*bset
;
6853 struct isl_set
*set
;
6854 struct isl_map
*map
;
6856 isl_local_space
*ls
;
6863 bmap
= isl_basic_map_sort_divs(bmap
);
6864 bmap
= isl_basic_map_cow(bmap
);
6868 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6869 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6872 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6873 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6874 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6875 dim
= isl_space_set_alloc(bmap
->ctx
,
6876 nparam
+ n_in
+ n_out
+ n_known
, 0);
6880 ls
= isl_basic_map_get_local_space(bmap
);
6881 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6882 n_known
, bmap
->n_div
- n_known
);
6884 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6885 swap_div(bmap
, i
- n_known
, i
);
6886 bmap
->n_div
-= n_known
;
6887 bmap
->extra
-= n_known
;
6889 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6890 bset
= (struct isl_basic_set
*)bmap
;
6892 set
= parameter_compute_divs(bset
);
6893 map
= (struct isl_map
*)set
;
6894 map
= replace_space_by_local_space(map
, ls
);
6898 isl_basic_map_free(bmap
);
6902 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6910 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6911 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6912 if (isl_int_is_zero(bmap
->div
[i
][0]))
6914 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6920 static int map_divs_known(__isl_keep isl_map
*map
)
6927 for (i
= 0; i
< map
->n
; ++i
) {
6928 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6936 /* If bmap contains any unknown divs, then compute explicit
6937 * expressions for them. However, this computation may be
6938 * quite expensive, so first try to remove divs that aren't
6941 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6944 struct isl_map
*map
;
6946 known
= isl_basic_map_divs_known(bmap
);
6950 return isl_map_from_basic_map(bmap
);
6952 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6954 known
= isl_basic_map_divs_known(bmap
);
6958 return isl_map_from_basic_map(bmap
);
6960 map
= compute_divs(bmap
);
6963 isl_basic_map_free(bmap
);
6967 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6971 struct isl_map
*res
;
6978 known
= map_divs_known(map
);
6986 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6987 for (i
= 1 ; i
< map
->n
; ++i
) {
6989 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6990 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6991 res
= isl_map_union_disjoint(res
, r2
);
6993 res
= isl_map_union(res
, r2
);
7000 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7002 return (struct isl_set
*)
7003 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7006 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7008 return (struct isl_set
*)
7009 isl_map_compute_divs((struct isl_map
*)set
);
7012 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7015 struct isl_set
*set
;
7020 map
= isl_map_cow(map
);
7024 set
= (struct isl_set
*)map
;
7025 set
->dim
= isl_space_domain(set
->dim
);
7028 for (i
= 0; i
< map
->n
; ++i
) {
7029 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7033 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7034 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7041 /* Return the union of "map1" and "map2", where we assume for now that
7042 * "map1" and "map2" are disjoint. Note that the basic maps inside
7043 * "map1" or "map2" may not be disjoint from each other.
7044 * Also note that this function is also called from isl_map_union,
7045 * which takes care of handling the situation where "map1" and "map2"
7046 * may not be disjoint.
7048 * If one of the inputs is empty, we can simply return the other input.
7049 * Similarly, if one of the inputs is universal, then it is equal to the union.
7051 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7052 __isl_take isl_map
*map2
)
7056 struct isl_map
*map
= NULL
;
7062 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7063 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7064 "spaces don't match", goto error
);
7075 is_universe
= isl_map_plain_is_universe(map1
);
7076 if (is_universe
< 0)
7083 is_universe
= isl_map_plain_is_universe(map2
);
7084 if (is_universe
< 0)
7091 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7092 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7093 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7095 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7096 map1
->n
+ map2
->n
, flags
);
7099 for (i
= 0; i
< map1
->n
; ++i
) {
7100 map
= isl_map_add_basic_map(map
,
7101 isl_basic_map_copy(map1
->p
[i
]));
7105 for (i
= 0; i
< map2
->n
; ++i
) {
7106 map
= isl_map_add_basic_map(map
,
7107 isl_basic_map_copy(map2
->p
[i
]));
7121 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7122 * guaranteed to be disjoint by the caller.
7124 * Note that this functions is called from within isl_map_make_disjoint,
7125 * so we have to be careful not to touch the constraints of the inputs
7128 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7129 __isl_take isl_map
*map2
)
7131 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7134 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7135 * not be disjoint. The parameters are assumed to have been aligned.
7137 * We currently simply call map_union_disjoint, the internal operation
7138 * of which does not really depend on the inputs being disjoint.
7139 * If the result contains more than one basic map, then we clear
7140 * the disjoint flag since the result may contain basic maps from
7141 * both inputs and these are not guaranteed to be disjoint.
7143 * As a special case, if "map1" and "map2" are obviously equal,
7144 * then we simply return "map1".
7146 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7147 __isl_take isl_map
*map2
)
7154 equal
= isl_map_plain_is_equal(map1
, map2
);
7162 map1
= map_union_disjoint(map1
, map2
);
7166 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7174 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7177 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7178 __isl_take isl_map
*map2
)
7180 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7183 struct isl_set
*isl_set_union_disjoint(
7184 struct isl_set
*set1
, struct isl_set
*set2
)
7186 return (struct isl_set
*)
7187 isl_map_union_disjoint(
7188 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7191 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7193 return (struct isl_set
*)
7194 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7197 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7200 * "map" and "set" are assumed to be compatible and non-NULL.
7202 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7203 __isl_take isl_set
*set
,
7204 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7205 __isl_take isl_basic_set
*bset
))
7208 struct isl_map
*result
;
7211 if (isl_set_plain_is_universe(set
)) {
7216 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7217 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7218 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7220 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7221 map
->n
* set
->n
, flags
);
7222 for (i
= 0; result
&& i
< map
->n
; ++i
)
7223 for (j
= 0; j
< set
->n
; ++j
) {
7224 result
= isl_map_add_basic_map(result
,
7225 fn(isl_basic_map_copy(map
->p
[i
]),
7226 isl_basic_set_copy(set
->p
[j
])));
7236 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7237 __isl_take isl_set
*set
)
7242 if (!isl_map_compatible_range(map
, set
))
7243 isl_die(set
->ctx
, isl_error_invalid
,
7244 "incompatible spaces", goto error
);
7246 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7253 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7254 __isl_take isl_set
*set
)
7256 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7259 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7260 __isl_take isl_set
*set
)
7265 if (!isl_map_compatible_domain(map
, set
))
7266 isl_die(set
->ctx
, isl_error_invalid
,
7267 "incompatible spaces", goto error
);
7269 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7276 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7277 __isl_take isl_set
*set
)
7279 return isl_map_align_params_map_map_and(map
, set
,
7280 &map_intersect_domain
);
7283 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7284 __isl_take isl_map
*map2
)
7288 map1
= isl_map_reverse(map1
);
7289 map1
= isl_map_apply_range(map1
, map2
);
7290 return isl_map_reverse(map1
);
7297 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7298 __isl_take isl_map
*map2
)
7300 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7303 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7304 __isl_take isl_map
*map2
)
7306 isl_space
*dim_result
;
7307 struct isl_map
*result
;
7313 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7314 isl_space_copy(map2
->dim
));
7316 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7319 for (i
= 0; i
< map1
->n
; ++i
)
7320 for (j
= 0; j
< map2
->n
; ++j
) {
7321 result
= isl_map_add_basic_map(result
,
7322 isl_basic_map_apply_range(
7323 isl_basic_map_copy(map1
->p
[i
]),
7324 isl_basic_map_copy(map2
->p
[j
])));
7330 if (result
&& result
->n
<= 1)
7331 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7339 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7340 __isl_take isl_map
*map2
)
7342 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7346 * returns range - domain
7348 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7350 isl_space
*dims
, *target_dim
;
7351 struct isl_basic_set
*bset
;
7358 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7359 bmap
->dim
, isl_dim_out
),
7361 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7362 dim
= isl_basic_map_n_in(bmap
);
7363 nparam
= isl_basic_map_n_param(bmap
);
7364 bset
= isl_basic_set_from_basic_map(bmap
);
7365 bset
= isl_basic_set_cow(bset
);
7366 dims
= isl_basic_set_get_space(bset
);
7367 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7368 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7369 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7370 for (i
= 0; i
< dim
; ++i
) {
7371 int j
= isl_basic_map_alloc_equality(
7372 (struct isl_basic_map
*)bset
);
7374 bset
= isl_basic_set_free(bset
);
7377 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7378 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7379 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7380 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7382 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7383 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7386 isl_basic_map_free(bmap
);
7391 * returns range - domain
7393 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7397 struct isl_set
*result
;
7402 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7403 map
->dim
, isl_dim_out
),
7405 dim
= isl_map_get_space(map
);
7406 dim
= isl_space_domain(dim
);
7407 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7410 for (i
= 0; i
< map
->n
; ++i
)
7411 result
= isl_set_add_basic_set(result
,
7412 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7421 * returns [domain -> range] -> range - domain
7423 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7424 __isl_take isl_basic_map
*bmap
)
7428 isl_basic_map
*domain
;
7432 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7433 bmap
->dim
, isl_dim_out
))
7434 isl_die(bmap
->ctx
, isl_error_invalid
,
7435 "domain and range don't match", goto error
);
7437 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7438 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7440 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7441 domain
= isl_basic_map_universe(dim
);
7443 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7444 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7445 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7447 total
= isl_basic_map_total_dim(bmap
);
7449 for (i
= 0; i
< n
; ++i
) {
7450 k
= isl_basic_map_alloc_equality(bmap
);
7453 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7454 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7455 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7456 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7459 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7460 return isl_basic_map_finalize(bmap
);
7462 isl_basic_map_free(bmap
);
7467 * returns [domain -> range] -> range - domain
7469 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7472 isl_space
*domain_dim
;
7477 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7478 map
->dim
, isl_dim_out
))
7479 isl_die(map
->ctx
, isl_error_invalid
,
7480 "domain and range don't match", goto error
);
7482 map
= isl_map_cow(map
);
7486 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7487 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7488 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7491 for (i
= 0; i
< map
->n
; ++i
) {
7492 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7496 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7503 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7505 struct isl_basic_map
*bmap
;
7513 nparam
= dims
->nparam
;
7515 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7519 for (i
= 0; i
< dim
; ++i
) {
7520 int j
= isl_basic_map_alloc_equality(bmap
);
7523 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7524 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7525 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7527 return isl_basic_map_finalize(bmap
);
7529 isl_basic_map_free(bmap
);
7533 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7537 if (dim
->n_in
!= dim
->n_out
)
7538 isl_die(dim
->ctx
, isl_error_invalid
,
7539 "number of input and output dimensions needs to be "
7540 "the same", goto error
);
7541 return basic_map_identity(dim
);
7543 isl_space_free(dim
);
7547 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7549 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7552 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7554 isl_space
*dim
= isl_set_get_space(set
);
7556 id
= isl_map_identity(isl_space_map_from_set(dim
));
7557 return isl_map_intersect_range(id
, set
);
7560 /* Construct a basic set with all set dimensions having only non-negative
7563 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7564 __isl_take isl_space
*space
)
7569 struct isl_basic_set
*bset
;
7573 nparam
= space
->nparam
;
7575 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7578 for (i
= 0; i
< dim
; ++i
) {
7579 int k
= isl_basic_set_alloc_inequality(bset
);
7582 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7583 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7587 isl_basic_set_free(bset
);
7591 /* Construct the half-space x_pos >= 0.
7593 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7597 isl_basic_set
*nonneg
;
7599 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7600 k
= isl_basic_set_alloc_inequality(nonneg
);
7603 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7604 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7606 return isl_basic_set_finalize(nonneg
);
7608 isl_basic_set_free(nonneg
);
7612 /* Construct the half-space x_pos <= -1.
7614 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7619 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7620 k
= isl_basic_set_alloc_inequality(neg
);
7623 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7624 isl_int_set_si(neg
->ineq
[k
][0], -1);
7625 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7627 return isl_basic_set_finalize(neg
);
7629 isl_basic_set_free(neg
);
7633 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7634 enum isl_dim_type type
, unsigned first
, unsigned n
)
7638 isl_basic_set
*nonneg
;
7646 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7648 offset
= pos(set
->dim
, type
);
7649 for (i
= 0; i
< n
; ++i
) {
7650 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7651 offset
+ first
+ i
);
7652 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7654 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7663 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7664 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7671 if (isl_set_plain_is_empty(set
)) {
7676 return fn(set
, signs
, user
);
7679 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7681 half
= isl_set_intersect(half
, isl_set_copy(set
));
7682 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7686 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7688 half
= isl_set_intersect(half
, set
);
7689 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7695 /* Call "fn" on the intersections of "set" with each of the orthants
7696 * (except for obviously empty intersections). The orthant is identified
7697 * by the signs array, with each entry having value 1 or -1 according
7698 * to the sign of the corresponding variable.
7700 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7701 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7711 if (isl_set_plain_is_empty(set
))
7714 nparam
= isl_set_dim(set
, isl_dim_param
);
7715 nvar
= isl_set_dim(set
, isl_dim_set
);
7717 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7719 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7727 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7729 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7732 int isl_basic_map_is_subset(
7733 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7736 struct isl_map
*map1
;
7737 struct isl_map
*map2
;
7739 if (!bmap1
|| !bmap2
)
7742 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7743 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7745 is_subset
= isl_map_is_subset(map1
, map2
);
7753 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7754 __isl_keep isl_basic_set
*bset2
)
7756 return isl_basic_map_is_subset(bset1
, bset2
);
7759 int isl_basic_map_is_equal(
7760 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7764 if (!bmap1
|| !bmap2
)
7766 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7769 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7773 int isl_basic_set_is_equal(
7774 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7776 return isl_basic_map_is_equal(
7777 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7780 int isl_map_is_empty(struct isl_map
*map
)
7787 for (i
= 0; i
< map
->n
; ++i
) {
7788 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7797 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7799 return map
? map
->n
== 0 : -1;
7802 int isl_set_plain_is_empty(struct isl_set
*set
)
7804 return set
? set
->n
== 0 : -1;
7807 int isl_set_is_empty(struct isl_set
*set
)
7809 return isl_map_is_empty((struct isl_map
*)set
);
7812 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7817 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7820 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7825 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7828 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7834 is_subset
= isl_map_is_subset(map1
, map2
);
7837 is_subset
= isl_map_is_subset(map2
, map1
);
7841 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7843 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7846 int isl_basic_map_is_strict_subset(
7847 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7851 if (!bmap1
|| !bmap2
)
7853 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7856 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7857 if (is_subset
== -1)
7862 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7868 is_subset
= isl_map_is_subset(map1
, map2
);
7871 is_subset
= isl_map_is_subset(map2
, map1
);
7872 if (is_subset
== -1)
7877 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7879 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7882 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7886 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7889 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7893 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7896 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7903 for (i
= 0; i
< map
->n
; ++i
) {
7904 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7912 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7914 return isl_map_plain_is_universe((isl_map
*) set
);
7917 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7919 struct isl_basic_set
*bset
= NULL
;
7920 struct isl_vec
*sample
= NULL
;
7927 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7930 if (isl_basic_map_is_universe(bmap
))
7933 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7934 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7935 copy
= isl_basic_map_remove_redundancies(copy
);
7936 empty
= isl_basic_map_plain_is_empty(copy
);
7937 isl_basic_map_free(copy
);
7941 total
= 1 + isl_basic_map_total_dim(bmap
);
7942 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7943 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7949 isl_vec_free(bmap
->sample
);
7950 bmap
->sample
= NULL
;
7951 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7954 sample
= isl_basic_set_sample_vec(bset
);
7957 empty
= sample
->size
== 0;
7958 isl_vec_free(bmap
->sample
);
7959 bmap
->sample
= sample
;
7961 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7966 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7970 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7973 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7977 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7980 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7982 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7985 struct isl_map
*isl_basic_map_union(
7986 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7988 struct isl_map
*map
;
7989 if (!bmap1
|| !bmap2
)
7992 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7994 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7997 map
= isl_map_add_basic_map(map
, bmap1
);
7998 map
= isl_map_add_basic_map(map
, bmap2
);
8001 isl_basic_map_free(bmap1
);
8002 isl_basic_map_free(bmap2
);
8006 struct isl_set
*isl_basic_set_union(
8007 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8009 return (struct isl_set
*)isl_basic_map_union(
8010 (struct isl_basic_map
*)bset1
,
8011 (struct isl_basic_map
*)bset2
);
8014 /* Order divs such that any div only depends on previous divs */
8015 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8023 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8025 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8027 if (isl_int_is_zero(bmap
->div
[i
][0]))
8029 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8033 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8039 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8041 return (struct isl_basic_set
*)
8042 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8045 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8052 for (i
= 0; i
< map
->n
; ++i
) {
8053 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8064 /* Apply the expansion computed by isl_merge_divs.
8065 * The expansion itself is given by "exp" while the resulting
8066 * list of divs is given by "div".
8068 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8069 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8074 bset
= isl_basic_set_cow(bset
);
8078 if (div
->n_row
< bset
->n_div
)
8079 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8080 "not an expansion", goto error
);
8082 n_div
= bset
->n_div
;
8083 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8084 div
->n_row
- n_div
, 0,
8085 2 * (div
->n_row
- n_div
));
8087 for (i
= n_div
; i
< div
->n_row
; ++i
)
8088 if (isl_basic_set_alloc_div(bset
) < 0)
8092 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8093 if (j
>= 0 && exp
[j
] == i
) {
8095 isl_basic_map_swap_div(bset
, i
, j
);
8098 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8099 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8107 isl_basic_set_free(bset
);
8112 /* Look for a div in dst that corresponds to the div "div" in src.
8113 * The divs before "div" in src and dst are assumed to be the same.
8115 * Returns -1 if no corresponding div was found and the position
8116 * of the corresponding div in dst otherwise.
8118 static int find_div(struct isl_basic_map
*dst
,
8119 struct isl_basic_map
*src
, unsigned div
)
8123 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8125 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8126 for (i
= div
; i
< dst
->n_div
; ++i
)
8127 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8128 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8129 dst
->n_div
- div
) == -1)
8134 /* Align the divs of "dst" to those of "src", adding divs from "src"
8135 * if needed. That is, make sure that the first src->n_div divs
8136 * of the result are equal to those of src.
8138 * The result is not finalized as by design it will have redundant
8139 * divs if any divs from "src" were copied.
8141 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8142 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8145 int known
, extended
;
8149 return isl_basic_map_free(dst
);
8151 if (src
->n_div
== 0)
8154 known
= isl_basic_map_divs_known(src
);
8156 return isl_basic_map_free(dst
);
8158 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8159 "some src divs are unknown",
8160 return isl_basic_map_free(dst
));
8162 src
= isl_basic_map_order_divs(src
);
8165 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8166 for (i
= 0; i
< src
->n_div
; ++i
) {
8167 int j
= find_div(dst
, src
, i
);
8170 int extra
= src
->n_div
- i
;
8171 dst
= isl_basic_map_cow(dst
);
8174 dst
= isl_basic_map_extend_space(dst
,
8175 isl_space_copy(dst
->dim
),
8176 extra
, 0, 2 * extra
);
8179 j
= isl_basic_map_alloc_div(dst
);
8181 return isl_basic_map_free(dst
);
8182 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8183 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8184 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8185 return isl_basic_map_free(dst
);
8188 isl_basic_map_swap_div(dst
, i
, j
);
8193 struct isl_basic_set
*isl_basic_set_align_divs(
8194 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8196 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8197 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8200 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8208 map
= isl_map_compute_divs(map
);
8209 map
= isl_map_cow(map
);
8213 for (i
= 1; i
< map
->n
; ++i
)
8214 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8215 for (i
= 1; i
< map
->n
; ++i
) {
8216 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8218 return isl_map_free(map
);
8221 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8225 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8227 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8230 /* Align the divs of the basic maps in "map" to those
8231 * of the basic maps in "list", as well as to the other basic maps in "map".
8232 * The elements in "list" are assumed to have known divs.
8234 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8235 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8239 map
= isl_map_compute_divs(map
);
8240 map
= isl_map_cow(map
);
8242 return isl_map_free(map
);
8246 n
= isl_basic_map_list_n_basic_map(list
);
8247 for (i
= 0; i
< n
; ++i
) {
8248 isl_basic_map
*bmap
;
8250 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8251 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8252 isl_basic_map_free(bmap
);
8255 return isl_map_free(map
);
8257 return isl_map_align_divs(map
);
8260 /* Align the divs of each element of "list" to those of "bmap".
8261 * Both "bmap" and the elements of "list" are assumed to have known divs.
8263 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8264 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8269 return isl_basic_map_list_free(list
);
8271 n
= isl_basic_map_list_n_basic_map(list
);
8272 for (i
= 0; i
< n
; ++i
) {
8273 isl_basic_map
*bmap_i
;
8275 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8276 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8277 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8283 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8284 __isl_take isl_map
*map
)
8288 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8289 map
= isl_map_intersect_domain(map
, set
);
8290 set
= isl_map_range(map
);
8298 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8299 __isl_take isl_map
*map
)
8301 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8304 /* There is no need to cow as removing empty parts doesn't change
8305 * the meaning of the set.
8307 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8314 for (i
= map
->n
- 1; i
>= 0; --i
)
8315 remove_if_empty(map
, i
);
8320 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8322 return (struct isl_set
*)
8323 isl_map_remove_empty_parts((struct isl_map
*)set
);
8326 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8328 struct isl_basic_map
*bmap
;
8329 if (!map
|| map
->n
== 0)
8331 bmap
= map
->p
[map
->n
-1];
8332 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8333 return isl_basic_map_copy(bmap
);
8336 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8338 return (struct isl_basic_set
*)
8339 isl_map_copy_basic_map((struct isl_map
*)set
);
8342 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8343 __isl_keep isl_basic_map
*bmap
)
8349 for (i
= map
->n
-1; i
>= 0; --i
) {
8350 if (map
->p
[i
] != bmap
)
8352 map
= isl_map_cow(map
);
8355 isl_basic_map_free(map
->p
[i
]);
8356 if (i
!= map
->n
-1) {
8357 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8358 map
->p
[i
] = map
->p
[map
->n
-1];
8369 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8370 struct isl_basic_set
*bset
)
8372 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8373 (struct isl_basic_map
*)bset
);
8376 /* Given two basic sets bset1 and bset2, compute the maximal difference
8377 * between the values of dimension pos in bset1 and those in bset2
8378 * for any common value of the parameters and dimensions preceding pos.
8380 static enum isl_lp_result
basic_set_maximal_difference_at(
8381 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8382 int pos
, isl_int
*opt
)
8385 struct isl_basic_map
*bmap1
= NULL
;
8386 struct isl_basic_map
*bmap2
= NULL
;
8387 struct isl_ctx
*ctx
;
8388 struct isl_vec
*obj
;
8391 unsigned dim1
, dim2
;
8392 enum isl_lp_result res
;
8394 if (!bset1
|| !bset2
)
8395 return isl_lp_error
;
8397 nparam
= isl_basic_set_n_param(bset1
);
8398 dim1
= isl_basic_set_n_dim(bset1
);
8399 dim2
= isl_basic_set_n_dim(bset2
);
8400 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8401 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8402 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8403 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8404 if (!bmap1
|| !bmap2
)
8406 bmap1
= isl_basic_map_cow(bmap1
);
8407 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8408 pos
, (dim1
- pos
) + (dim2
- pos
),
8409 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8410 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8413 total
= isl_basic_map_total_dim(bmap1
);
8415 obj
= isl_vec_alloc(ctx
, 1 + total
);
8418 isl_seq_clr(obj
->block
.data
, 1 + total
);
8419 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8420 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8421 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8423 isl_basic_map_free(bmap1
);
8427 isl_basic_map_free(bmap2
);
8429 isl_basic_map_free(bmap1
);
8430 return isl_lp_error
;
8433 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8434 * for any common value of the parameters and dimensions preceding pos
8435 * in both basic sets, the values of dimension pos in bset1 are
8436 * smaller or larger than those in bset2.
8439 * 1 if bset1 follows bset2
8440 * -1 if bset1 precedes bset2
8441 * 0 if bset1 and bset2 are incomparable
8442 * -2 if some error occurred.
8444 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8445 struct isl_basic_set
*bset2
, int pos
)
8448 enum isl_lp_result res
;
8453 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8455 if (res
== isl_lp_empty
)
8457 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8458 res
== isl_lp_unbounded
)
8460 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8469 /* Given two basic sets bset1 and bset2, check whether
8470 * for any common value of the parameters and dimensions preceding pos
8471 * there is a value of dimension pos in bset1 that is larger
8472 * than a value of the same dimension in bset2.
8475 * 1 if there exists such a pair
8476 * 0 if there is no such pair, but there is a pair of equal values
8478 * -2 if some error occurred.
8480 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8481 __isl_keep isl_basic_set
*bset2
, int pos
)
8484 enum isl_lp_result res
;
8489 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8491 if (res
== isl_lp_empty
)
8493 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8494 res
== isl_lp_unbounded
)
8496 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8498 else if (res
== isl_lp_ok
)
8507 /* Given two sets set1 and set2, check whether
8508 * for any common value of the parameters and dimensions preceding pos
8509 * there is a value of dimension pos in set1 that is larger
8510 * than a value of the same dimension in set2.
8513 * 1 if there exists such a pair
8514 * 0 if there is no such pair, but there is a pair of equal values
8516 * -2 if some error occurred.
8518 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8519 __isl_keep isl_set
*set2
, int pos
)
8527 for (i
= 0; i
< set1
->n
; ++i
)
8528 for (j
= 0; j
< set2
->n
; ++j
) {
8530 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8531 if (f
== 1 || f
== -2)
8540 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8541 unsigned pos
, isl_int
*val
)
8549 total
= isl_basic_map_total_dim(bmap
);
8550 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8551 for (; d
+1 > pos
; --d
)
8552 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8556 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8558 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8560 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8563 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8569 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8570 unsigned pos
, isl_int
*val
)
8582 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8585 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8586 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8587 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8588 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8592 isl_int_set(*val
, v
);
8598 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8599 unsigned pos
, isl_int
*val
)
8601 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8605 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8608 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8611 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8612 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8614 if (pos
>= isl_basic_map_dim(bmap
, type
))
8616 return isl_basic_map_plain_has_fixed_var(bmap
,
8617 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8620 /* If "bmap" obviously lies on a hyperplane where the given dimension
8621 * has a fixed value, then return that value.
8622 * Otherwise return NaN.
8624 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8625 __isl_keep isl_basic_map
*bmap
,
8626 enum isl_dim_type type
, unsigned pos
)
8634 ctx
= isl_basic_map_get_ctx(bmap
);
8635 v
= isl_val_alloc(ctx
);
8638 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8640 return isl_val_free(v
);
8642 isl_int_set_si(v
->d
, 1);
8646 return isl_val_nan(ctx
);
8649 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8650 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8652 if (pos
>= isl_map_dim(map
, type
))
8654 return isl_map_plain_has_fixed_var(map
,
8655 map_offset(map
, type
) - 1 + pos
, val
);
8658 /* If "map" obviously lies on a hyperplane where the given dimension
8659 * has a fixed value, then return that value.
8660 * Otherwise return NaN.
8662 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8663 enum isl_dim_type type
, unsigned pos
)
8671 ctx
= isl_map_get_ctx(map
);
8672 v
= isl_val_alloc(ctx
);
8675 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8677 return isl_val_free(v
);
8679 isl_int_set_si(v
->d
, 1);
8683 return isl_val_nan(ctx
);
8686 /* If "set" obviously lies on a hyperplane where the given dimension
8687 * has a fixed value, then return that value.
8688 * Otherwise return NaN.
8690 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8691 enum isl_dim_type type
, unsigned pos
)
8693 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8696 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8697 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8699 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8702 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8703 * then return this fixed value in *val.
8705 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8706 unsigned dim
, isl_int
*val
)
8708 return isl_basic_set_plain_has_fixed_var(bset
,
8709 isl_basic_set_n_param(bset
) + dim
, val
);
8712 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8713 * then return this fixed value in *val.
8715 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8716 unsigned dim
, isl_int
*val
)
8718 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8721 /* Check if input variable in has fixed value and if so and if val is not NULL,
8722 * then return this fixed value in *val.
8724 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8725 unsigned in
, isl_int
*val
)
8727 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8730 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8731 * and if val is not NULL, then return this lower bound in *val.
8733 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8734 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8736 int i
, i_eq
= -1, i_ineq
= -1;
8743 total
= isl_basic_set_total_dim(bset
);
8744 nparam
= isl_basic_set_n_param(bset
);
8745 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8746 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8752 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8753 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8755 if (i_eq
!= -1 || i_ineq
!= -1)
8759 if (i_eq
== -1 && i_ineq
== -1)
8761 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8762 /* The coefficient should always be one due to normalization. */
8763 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8765 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8767 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8768 total
- nparam
- dim
- 1) != -1)
8771 isl_int_neg(*val
, c
[0]);
8775 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8776 unsigned dim
, isl_int
*val
)
8788 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8792 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8794 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8795 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8797 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8801 isl_int_set(*val
, v
);
8807 /* uset_gist depends on constraints without existentially quantified
8808 * variables sorting first.
8810 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8812 isl_int
**c1
= (isl_int
**) p1
;
8813 isl_int
**c2
= (isl_int
**) p2
;
8815 unsigned size
= *(unsigned *) arg
;
8817 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8818 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8823 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8826 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8827 struct isl_basic_map
*bmap
)
8833 if (bmap
->n_ineq
== 0)
8835 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8837 total
= isl_basic_map_total_dim(bmap
);
8838 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8839 &sort_constraint_cmp
, &total
) < 0)
8840 return isl_basic_map_free(bmap
);
8844 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8845 __isl_take isl_basic_set
*bset
)
8847 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8848 (struct isl_basic_map
*)bset
);
8851 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8855 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8857 bmap
= isl_basic_map_remove_redundancies(bmap
);
8858 bmap
= isl_basic_map_sort_constraints(bmap
);
8860 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8864 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8866 return (struct isl_basic_set
*)isl_basic_map_normalize(
8867 (struct isl_basic_map
*)bset
);
8870 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8871 const __isl_keep isl_basic_map
*bmap2
)
8876 if (!bmap1
|| !bmap2
)
8881 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8882 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8883 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8884 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8885 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8886 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8887 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8888 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8889 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8890 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8891 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8893 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8895 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8897 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8898 return bmap1
->n_eq
- bmap2
->n_eq
;
8899 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8900 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8901 if (bmap1
->n_div
!= bmap2
->n_div
)
8902 return bmap1
->n_div
- bmap2
->n_div
;
8903 total
= isl_basic_map_total_dim(bmap1
);
8904 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8905 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8909 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8910 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8914 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8915 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8922 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8923 const __isl_keep isl_basic_set
*bset2
)
8925 return isl_basic_map_plain_cmp(bset1
, bset2
);
8928 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8934 if (set1
->n
!= set2
->n
)
8935 return set1
->n
- set2
->n
;
8937 for (i
= 0; i
< set1
->n
; ++i
) {
8938 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8946 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8947 __isl_keep isl_basic_map
*bmap2
)
8949 if (!bmap1
|| !bmap2
)
8951 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8954 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8955 __isl_keep isl_basic_set
*bset2
)
8957 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8958 (isl_basic_map
*)bset2
);
8961 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8963 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8964 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8966 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8969 /* Sort the basic maps of "map" and remove duplicate basic maps.
8971 * While removing basic maps, we make sure that the basic maps remain
8972 * sorted because isl_map_normalize expects the basic maps of the result
8975 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
8979 map
= isl_map_remove_empty_parts(map
);
8982 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8983 for (i
= map
->n
- 1; i
>= 1; --i
) {
8984 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
8986 isl_basic_map_free(map
->p
[i
-1]);
8987 for (j
= i
; j
< map
->n
; ++j
)
8988 map
->p
[j
- 1] = map
->p
[j
];
8995 /* Remove obvious duplicates among the basic maps of "map".
8997 * Unlike isl_map_normalize, this function does not remove redundant
8998 * constraints and only removes duplicates that have exactly the same
8999 * constraints in the input. It does sort the constraints and
9000 * the basic maps to ease the detection of duplicates.
9002 * If "map" has already been normalized or if the basic maps are
9003 * disjoint, then there can be no duplicates.
9005 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9008 isl_basic_map
*bmap
;
9014 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9016 for (i
= 0; i
< map
->n
; ++i
) {
9017 bmap
= isl_basic_map_copy(map
->p
[i
]);
9018 bmap
= isl_basic_map_sort_constraints(bmap
);
9020 return isl_map_free(map
);
9021 isl_basic_map_free(map
->p
[i
]);
9025 map
= sort_and_remove_duplicates(map
);
9029 /* We normalize in place, but if anything goes wrong we need
9030 * to return NULL, so we need to make sure we don't change the
9031 * meaning of any possible other copies of map.
9033 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9036 struct isl_basic_map
*bmap
;
9040 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9042 for (i
= 0; i
< map
->n
; ++i
) {
9043 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9046 isl_basic_map_free(map
->p
[i
]);
9050 map
= sort_and_remove_duplicates(map
);
9052 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9059 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9061 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9064 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9074 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9077 map1
= isl_map_copy(map1
);
9078 map2
= isl_map_copy(map2
);
9079 map1
= isl_map_normalize(map1
);
9080 map2
= isl_map_normalize(map2
);
9083 equal
= map1
->n
== map2
->n
;
9084 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9085 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9098 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9100 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9101 (struct isl_map
*)set2
);
9104 /* Return an interval that ranges from min to max (inclusive)
9106 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9107 isl_int min
, isl_int max
)
9110 struct isl_basic_set
*bset
= NULL
;
9112 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9116 k
= isl_basic_set_alloc_inequality(bset
);
9119 isl_int_set_si(bset
->ineq
[k
][1], 1);
9120 isl_int_neg(bset
->ineq
[k
][0], min
);
9122 k
= isl_basic_set_alloc_inequality(bset
);
9125 isl_int_set_si(bset
->ineq
[k
][1], -1);
9126 isl_int_set(bset
->ineq
[k
][0], max
);
9130 isl_basic_set_free(bset
);
9134 /* Return the basic maps in "map" as a list.
9136 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9137 __isl_keep isl_map
*map
)
9141 isl_basic_map_list
*list
;
9145 ctx
= isl_map_get_ctx(map
);
9146 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9148 for (i
= 0; i
< map
->n
; ++i
) {
9149 isl_basic_map
*bmap
;
9151 bmap
= isl_basic_map_copy(map
->p
[i
]);
9152 list
= isl_basic_map_list_add(list
, bmap
);
9158 /* Return the intersection of the elements in the non-empty list "list".
9159 * All elements are assumed to live in the same space.
9161 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9162 __isl_take isl_basic_map_list
*list
)
9165 isl_basic_map
*bmap
;
9169 n
= isl_basic_map_list_n_basic_map(list
);
9171 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9172 "expecting non-empty list", goto error
);
9174 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9175 for (i
= 1; i
< n
; ++i
) {
9176 isl_basic_map
*bmap_i
;
9178 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9179 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9182 isl_basic_map_list_free(list
);
9185 isl_basic_map_list_free(list
);
9189 /* Return the intersection of the elements in the non-empty list "list".
9190 * All elements are assumed to live in the same space.
9192 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9193 __isl_take isl_basic_set_list
*list
)
9195 return isl_basic_map_list_intersect(list
);
9198 /* Return the Cartesian product of the basic sets in list (in the given order).
9200 __isl_give isl_basic_set
*isl_basic_set_list_product(
9201 __isl_take
struct isl_basic_set_list
*list
)
9209 struct isl_basic_set
*product
= NULL
;
9213 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9214 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9215 nparam
= isl_basic_set_n_param(list
->p
[0]);
9216 dim
= isl_basic_set_n_dim(list
->p
[0]);
9217 extra
= list
->p
[0]->n_div
;
9218 n_eq
= list
->p
[0]->n_eq
;
9219 n_ineq
= list
->p
[0]->n_ineq
;
9220 for (i
= 1; i
< list
->n
; ++i
) {
9221 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9222 isl_assert(list
->ctx
,
9223 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9224 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9225 extra
+= list
->p
[i
]->n_div
;
9226 n_eq
+= list
->p
[i
]->n_eq
;
9227 n_ineq
+= list
->p
[i
]->n_ineq
;
9229 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9234 for (i
= 0; i
< list
->n
; ++i
) {
9235 isl_basic_set_add_constraints(product
,
9236 isl_basic_set_copy(list
->p
[i
]), dim
);
9237 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9239 isl_basic_set_list_free(list
);
9242 isl_basic_set_free(product
);
9243 isl_basic_set_list_free(list
);
9247 struct isl_basic_map
*isl_basic_map_product(
9248 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9250 isl_space
*dim_result
= NULL
;
9251 struct isl_basic_map
*bmap
;
9252 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9253 struct isl_dim_map
*dim_map1
, *dim_map2
;
9255 if (!bmap1
|| !bmap2
)
9258 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9259 bmap2
->dim
, isl_dim_param
), goto error
);
9260 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9261 isl_space_copy(bmap2
->dim
));
9263 in1
= isl_basic_map_n_in(bmap1
);
9264 in2
= isl_basic_map_n_in(bmap2
);
9265 out1
= isl_basic_map_n_out(bmap1
);
9266 out2
= isl_basic_map_n_out(bmap2
);
9267 nparam
= isl_basic_map_n_param(bmap1
);
9269 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9270 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9271 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9272 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9273 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9274 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9275 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9276 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9277 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9278 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9279 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9281 bmap
= isl_basic_map_alloc_space(dim_result
,
9282 bmap1
->n_div
+ bmap2
->n_div
,
9283 bmap1
->n_eq
+ bmap2
->n_eq
,
9284 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9285 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9286 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9287 bmap
= isl_basic_map_simplify(bmap
);
9288 return isl_basic_map_finalize(bmap
);
9290 isl_basic_map_free(bmap1
);
9291 isl_basic_map_free(bmap2
);
9295 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9296 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9298 isl_basic_map
*prod
;
9300 prod
= isl_basic_map_product(bmap1
, bmap2
);
9301 prod
= isl_basic_map_flatten(prod
);
9305 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9306 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9308 return isl_basic_map_flat_range_product(bset1
, bset2
);
9311 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9312 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9314 isl_space
*space_result
= NULL
;
9315 isl_basic_map
*bmap
;
9316 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9317 struct isl_dim_map
*dim_map1
, *dim_map2
;
9319 if (!bmap1
|| !bmap2
)
9322 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9323 isl_space_copy(bmap2
->dim
));
9325 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9326 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9327 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9328 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9330 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9331 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9332 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9333 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9334 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9335 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9336 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9337 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9338 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9339 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9340 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9342 bmap
= isl_basic_map_alloc_space(space_result
,
9343 bmap1
->n_div
+ bmap2
->n_div
,
9344 bmap1
->n_eq
+ bmap2
->n_eq
,
9345 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9346 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9347 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9348 bmap
= isl_basic_map_simplify(bmap
);
9349 return isl_basic_map_finalize(bmap
);
9351 isl_basic_map_free(bmap1
);
9352 isl_basic_map_free(bmap2
);
9356 __isl_give isl_basic_map
*isl_basic_map_range_product(
9357 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9359 isl_space
*dim_result
= NULL
;
9360 isl_basic_map
*bmap
;
9361 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9362 struct isl_dim_map
*dim_map1
, *dim_map2
;
9364 if (!bmap1
|| !bmap2
)
9367 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9368 bmap2
->dim
, isl_dim_param
))
9369 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9370 "parameters don't match", goto error
);
9372 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9373 isl_space_copy(bmap2
->dim
));
9375 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9376 out1
= isl_basic_map_n_out(bmap1
);
9377 out2
= isl_basic_map_n_out(bmap2
);
9378 nparam
= isl_basic_map_n_param(bmap1
);
9380 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9381 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9382 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9383 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9384 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9385 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9386 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9387 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9388 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9389 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9390 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9392 bmap
= isl_basic_map_alloc_space(dim_result
,
9393 bmap1
->n_div
+ bmap2
->n_div
,
9394 bmap1
->n_eq
+ bmap2
->n_eq
,
9395 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9396 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9397 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9398 bmap
= isl_basic_map_simplify(bmap
);
9399 return isl_basic_map_finalize(bmap
);
9401 isl_basic_map_free(bmap1
);
9402 isl_basic_map_free(bmap2
);
9406 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9407 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9409 isl_basic_map
*prod
;
9411 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9412 prod
= isl_basic_map_flatten_range(prod
);
9416 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9417 * and collect the results.
9418 * The result live in the space obtained by calling "space_product"
9419 * on the spaces of "map1" and "map2".
9420 * If "remove_duplicates" is set then the result may contain duplicates
9421 * (even if the inputs do not) and so we try and remove the obvious
9424 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9425 __isl_take isl_map
*map2
,
9426 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9427 __isl_take isl_space
*right
),
9428 __isl_give isl_basic_map
*(*basic_map_product
)(
9429 __isl_take isl_basic_map
*left
,
9430 __isl_take isl_basic_map
*right
),
9431 int remove_duplicates
)
9434 struct isl_map
*result
;
9440 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9441 map2
->dim
, isl_dim_param
), goto error
);
9443 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9444 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9445 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9447 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9448 isl_space_copy(map2
->dim
)),
9449 map1
->n
* map2
->n
, flags
);
9452 for (i
= 0; i
< map1
->n
; ++i
)
9453 for (j
= 0; j
< map2
->n
; ++j
) {
9454 struct isl_basic_map
*part
;
9455 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9456 isl_basic_map_copy(map2
->p
[j
]));
9457 if (isl_basic_map_is_empty(part
))
9458 isl_basic_map_free(part
);
9460 result
= isl_map_add_basic_map(result
, part
);
9464 if (remove_duplicates
)
9465 result
= isl_map_remove_obvious_duplicates(result
);
9475 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9477 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9478 __isl_take isl_map
*map2
)
9480 return map_product(map1
, map2
, &isl_space_product
,
9481 &isl_basic_map_product
, 0);
9484 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9485 __isl_take isl_map
*map2
)
9487 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9490 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9492 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9493 __isl_take isl_map
*map2
)
9497 prod
= isl_map_product(map1
, map2
);
9498 prod
= isl_map_flatten(prod
);
9502 /* Given two set A and B, construct its Cartesian product A x B.
9504 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9506 return isl_map_range_product(set1
, set2
);
9509 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9510 __isl_take isl_set
*set2
)
9512 return isl_map_flat_range_product(set1
, set2
);
9515 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9517 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9518 __isl_take isl_map
*map2
)
9520 return map_product(map1
, map2
, &isl_space_domain_product
,
9521 &isl_basic_map_domain_product
, 1);
9524 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9526 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9527 __isl_take isl_map
*map2
)
9529 return map_product(map1
, map2
, &isl_space_range_product
,
9530 &isl_basic_map_range_product
, 1);
9533 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9534 __isl_take isl_map
*map2
)
9536 return isl_map_align_params_map_map_and(map1
, map2
,
9537 &map_domain_product_aligned
);
9540 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9541 __isl_take isl_map
*map2
)
9543 return isl_map_align_params_map_map_and(map1
, map2
,
9544 &map_range_product_aligned
);
9547 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9549 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9552 int total1
, keep1
, total2
, keep2
;
9556 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9557 !isl_space_range_is_wrapping(map
->dim
))
9558 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9559 "not a product", return isl_map_free(map
));
9561 space
= isl_map_get_space(map
);
9562 total1
= isl_space_dim(space
, isl_dim_in
);
9563 total2
= isl_space_dim(space
, isl_dim_out
);
9564 space
= isl_space_factor_domain(space
);
9565 keep1
= isl_space_dim(space
, isl_dim_in
);
9566 keep2
= isl_space_dim(space
, isl_dim_out
);
9567 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9568 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9569 map
= isl_map_reset_space(map
, space
);
9574 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9576 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9579 int total1
, keep1
, total2
, keep2
;
9583 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9584 !isl_space_range_is_wrapping(map
->dim
))
9585 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9586 "not a product", return isl_map_free(map
));
9588 space
= isl_map_get_space(map
);
9589 total1
= isl_space_dim(space
, isl_dim_in
);
9590 total2
= isl_space_dim(space
, isl_dim_out
);
9591 space
= isl_space_factor_range(space
);
9592 keep1
= isl_space_dim(space
, isl_dim_in
);
9593 keep2
= isl_space_dim(space
, isl_dim_out
);
9594 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9595 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9596 map
= isl_map_reset_space(map
, space
);
9601 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9603 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9610 if (!isl_space_domain_is_wrapping(map
->dim
))
9611 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9612 "domain is not a product", return isl_map_free(map
));
9614 space
= isl_map_get_space(map
);
9615 total
= isl_space_dim(space
, isl_dim_in
);
9616 space
= isl_space_domain_factor_domain(space
);
9617 keep
= isl_space_dim(space
, isl_dim_in
);
9618 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9619 map
= isl_map_reset_space(map
, space
);
9624 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9626 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9633 if (!isl_space_domain_is_wrapping(map
->dim
))
9634 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9635 "domain is not a product", return isl_map_free(map
));
9637 space
= isl_map_get_space(map
);
9638 total
= isl_space_dim(space
, isl_dim_in
);
9639 space
= isl_space_domain_factor_range(space
);
9640 keep
= isl_space_dim(space
, isl_dim_in
);
9641 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9642 map
= isl_map_reset_space(map
, space
);
9647 /* Given a map A -> [B -> C], extract the map A -> B.
9649 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9656 if (!isl_space_range_is_wrapping(map
->dim
))
9657 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9658 "range is not a product", return isl_map_free(map
));
9660 space
= isl_map_get_space(map
);
9661 total
= isl_space_dim(space
, isl_dim_out
);
9662 space
= isl_space_range_factor_domain(space
);
9663 keep
= isl_space_dim(space
, isl_dim_out
);
9664 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9665 map
= isl_map_reset_space(map
, space
);
9670 /* Given a map A -> [B -> C], extract the map A -> C.
9672 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9679 if (!isl_space_range_is_wrapping(map
->dim
))
9680 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9681 "range is not a product", return isl_map_free(map
));
9683 space
= isl_map_get_space(map
);
9684 total
= isl_space_dim(space
, isl_dim_out
);
9685 space
= isl_space_range_factor_range(space
);
9686 keep
= isl_space_dim(space
, isl_dim_out
);
9687 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9688 map
= isl_map_reset_space(map
, space
);
9693 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9695 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9696 __isl_take isl_map
*map2
)
9700 prod
= isl_map_domain_product(map1
, map2
);
9701 prod
= isl_map_flatten_domain(prod
);
9705 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9707 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9708 __isl_take isl_map
*map2
)
9712 prod
= isl_map_range_product(map1
, map2
);
9713 prod
= isl_map_flatten_range(prod
);
9717 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9720 uint32_t hash
= isl_hash_init();
9725 bmap
= isl_basic_map_copy(bmap
);
9726 bmap
= isl_basic_map_normalize(bmap
);
9729 total
= isl_basic_map_total_dim(bmap
);
9730 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9731 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9733 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9734 isl_hash_hash(hash
, c_hash
);
9736 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9737 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9739 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9740 isl_hash_hash(hash
, c_hash
);
9742 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9743 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9745 if (isl_int_is_zero(bmap
->div
[i
][0]))
9747 isl_hash_byte(hash
, i
& 0xFF);
9748 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9749 isl_hash_hash(hash
, c_hash
);
9751 isl_basic_map_free(bmap
);
9755 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9757 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9760 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9767 map
= isl_map_copy(map
);
9768 map
= isl_map_normalize(map
);
9772 hash
= isl_hash_init();
9773 for (i
= 0; i
< map
->n
; ++i
) {
9775 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9776 isl_hash_hash(hash
, bmap_hash
);
9784 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9786 return isl_map_get_hash((isl_map
*)set
);
9789 /* Check if the value for dimension dim is completely determined
9790 * by the values of the other parameters and variables.
9791 * That is, check if dimension dim is involved in an equality.
9793 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9800 nparam
= isl_basic_set_n_param(bset
);
9801 for (i
= 0; i
< bset
->n_eq
; ++i
)
9802 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9807 /* Check if the value for dimension dim is completely determined
9808 * by the values of the other parameters and variables.
9809 * That is, check if dimension dim is involved in an equality
9810 * for each of the subsets.
9812 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9818 for (i
= 0; i
< set
->n
; ++i
) {
9820 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9827 /* Return the number of basic maps in the (current) representation of "map".
9829 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9831 return map
? map
->n
: 0;
9834 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9836 return set
? set
->n
: 0;
9839 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9840 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9847 for (i
= 0; i
< map
->n
; ++i
)
9848 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9854 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9855 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9862 for (i
= 0; i
< set
->n
; ++i
)
9863 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9869 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9876 bset
= isl_basic_set_cow(bset
);
9880 dim
= isl_basic_set_get_space(bset
);
9881 dim
= isl_space_lift(dim
, bset
->n_div
);
9884 isl_space_free(bset
->dim
);
9886 bset
->extra
-= bset
->n_div
;
9889 bset
= isl_basic_set_finalize(bset
);
9893 isl_basic_set_free(bset
);
9897 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9903 set
= isl_set_align_divs(set
);
9908 set
= isl_set_cow(set
);
9912 n_div
= set
->p
[0]->n_div
;
9913 dim
= isl_set_get_space(set
);
9914 dim
= isl_space_lift(dim
, n_div
);
9917 isl_space_free(set
->dim
);
9920 for (i
= 0; i
< set
->n
; ++i
) {
9921 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9932 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9935 struct isl_basic_map
*bmap
;
9942 set
= isl_set_align_divs(set
);
9947 dim
= isl_set_get_space(set
);
9948 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9950 return isl_map_identity(isl_space_map_from_set(dim
));
9953 n_div
= set
->p
[0]->n_div
;
9954 dim
= isl_space_map_from_set(dim
);
9955 n_param
= isl_space_dim(dim
, isl_dim_param
);
9956 n_set
= isl_space_dim(dim
, isl_dim_in
);
9957 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9958 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9959 for (i
= 0; i
< n_set
; ++i
)
9960 bmap
= var_equal(bmap
, i
);
9962 total
= n_param
+ n_set
+ n_set
+ n_div
;
9963 for (i
= 0; i
< n_div
; ++i
) {
9964 k
= isl_basic_map_alloc_inequality(bmap
);
9967 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9968 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9969 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9970 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9971 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9972 set
->p
[0]->div
[i
][0]);
9974 l
= isl_basic_map_alloc_inequality(bmap
);
9977 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9978 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9979 set
->p
[0]->div
[i
][0]);
9980 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9984 bmap
= isl_basic_map_simplify(bmap
);
9985 bmap
= isl_basic_map_finalize(bmap
);
9986 return isl_map_from_basic_map(bmap
);
9989 isl_basic_map_free(bmap
);
9993 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10001 dim
= isl_basic_set_total_dim(bset
);
10002 size
+= bset
->n_eq
* (1 + dim
);
10003 size
+= bset
->n_ineq
* (1 + dim
);
10004 size
+= bset
->n_div
* (2 + dim
);
10009 int isl_set_size(__isl_keep isl_set
*set
)
10017 for (i
= 0; i
< set
->n
; ++i
)
10018 size
+= isl_basic_set_size(set
->p
[i
]);
10023 /* Check if there is any lower bound (if lower == 0) and/or upper
10024 * bound (if upper == 0) on the specified dim.
10026 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10027 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10034 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
10036 pos
+= isl_basic_map_offset(bmap
, type
);
10038 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10039 if (isl_int_is_zero(bmap
->div
[i
][0]))
10041 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10045 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10046 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10049 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10050 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10057 return lower
&& upper
;
10060 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10061 enum isl_dim_type type
, unsigned pos
)
10063 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10066 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10067 enum isl_dim_type type
, unsigned pos
)
10069 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10072 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10073 enum isl_dim_type type
, unsigned pos
)
10075 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10078 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10079 enum isl_dim_type type
, unsigned pos
)
10086 for (i
= 0; i
< map
->n
; ++i
) {
10088 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10089 if (bounded
< 0 || !bounded
)
10096 /* Return 1 if the specified dim is involved in both an upper bound
10097 * and a lower bound.
10099 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10100 enum isl_dim_type type
, unsigned pos
)
10102 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10105 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10107 static int has_any_bound(__isl_keep isl_map
*map
,
10108 enum isl_dim_type type
, unsigned pos
,
10109 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10110 enum isl_dim_type type
, unsigned pos
))
10117 for (i
= 0; i
< map
->n
; ++i
) {
10119 bounded
= fn(map
->p
[i
], type
, pos
);
10120 if (bounded
< 0 || bounded
)
10127 /* Return 1 if the specified dim is involved in any lower bound.
10129 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10130 enum isl_dim_type type
, unsigned pos
)
10132 return has_any_bound(set
, type
, pos
,
10133 &isl_basic_map_dim_has_lower_bound
);
10136 /* Return 1 if the specified dim is involved in any upper bound.
10138 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10139 enum isl_dim_type type
, unsigned pos
)
10141 return has_any_bound(set
, type
, pos
,
10142 &isl_basic_map_dim_has_upper_bound
);
10145 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10147 static int has_bound(__isl_keep isl_map
*map
,
10148 enum isl_dim_type type
, unsigned pos
,
10149 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10150 enum isl_dim_type type
, unsigned pos
))
10157 for (i
= 0; i
< map
->n
; ++i
) {
10159 bounded
= fn(map
->p
[i
], type
, pos
);
10160 if (bounded
< 0 || !bounded
)
10167 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10169 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10170 enum isl_dim_type type
, unsigned pos
)
10172 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10175 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10177 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10178 enum isl_dim_type type
, unsigned pos
)
10180 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10183 /* For each of the "n" variables starting at "first", determine
10184 * the sign of the variable and put the results in the first "n"
10185 * elements of the array "signs".
10187 * 1 means that the variable is non-negative
10188 * -1 means that the variable is non-positive
10189 * 0 means the variable attains both positive and negative values.
10191 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10192 unsigned first
, unsigned n
, int *signs
)
10194 isl_vec
*bound
= NULL
;
10195 struct isl_tab
*tab
= NULL
;
10196 struct isl_tab_undo
*snap
;
10199 if (!bset
|| !signs
)
10202 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10203 tab
= isl_tab_from_basic_set(bset
, 0);
10204 if (!bound
|| !tab
)
10207 isl_seq_clr(bound
->el
, bound
->size
);
10208 isl_int_set_si(bound
->el
[0], -1);
10210 snap
= isl_tab_snap(tab
);
10211 for (i
= 0; i
< n
; ++i
) {
10214 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10215 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10217 empty
= tab
->empty
;
10218 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10219 if (isl_tab_rollback(tab
, snap
) < 0)
10227 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10228 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10230 empty
= tab
->empty
;
10231 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10232 if (isl_tab_rollback(tab
, snap
) < 0)
10235 signs
[i
] = empty
? -1 : 0;
10239 isl_vec_free(bound
);
10243 isl_vec_free(bound
);
10247 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10248 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10250 if (!bset
|| !signs
)
10252 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10255 first
+= pos(bset
->dim
, type
) - 1;
10256 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10259 /* Is it possible for the integer division "div" to depend (possibly
10260 * indirectly) on any output dimensions?
10262 * If the div is undefined, then we conservatively assume that it
10263 * may depend on them.
10264 * Otherwise, we check if it actually depends on them or on any integer
10265 * divisions that may depend on them.
10267 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10270 unsigned n_out
, o_out
;
10271 unsigned n_div
, o_div
;
10273 if (isl_int_is_zero(bmap
->div
[div
][0]))
10276 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10277 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10279 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10282 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10283 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10285 for (i
= 0; i
< n_div
; ++i
) {
10286 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10288 if (div_may_involve_output(bmap
, i
))
10295 /* Return the index of the equality of "bmap" that defines
10296 * the output dimension "pos" in terms of earlier dimensions.
10297 * The equality may also involve integer divisions, as long
10298 * as those integer divisions are defined in terms of
10299 * parameters or input dimensions.
10300 * Return bmap->n_eq if there is no such equality.
10301 * Return -1 on error.
10303 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10307 unsigned n_out
, o_out
;
10308 unsigned n_div
, o_div
;
10313 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10314 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10315 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10316 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10318 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10319 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10321 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10322 n_out
- (pos
+ 1)) != -1)
10324 for (k
= 0; k
< n_div
; ++k
) {
10325 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10327 if (div_may_involve_output(bmap
, k
))
10337 /* Check if the given basic map is obviously single-valued.
10338 * In particular, for each output dimension, check that there is
10339 * an equality that defines the output dimension in terms of
10340 * earlier dimensions.
10342 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10350 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10352 for (i
= 0; i
< n_out
; ++i
) {
10355 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10358 if (eq
>= bmap
->n_eq
)
10365 /* Check if the given basic map is single-valued.
10366 * We simply compute
10370 * and check if the result is a subset of the identity mapping.
10372 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10375 isl_basic_map
*test
;
10379 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10383 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10384 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10386 space
= isl_basic_map_get_space(bmap
);
10387 space
= isl_space_map_from_set(isl_space_range(space
));
10388 id
= isl_basic_map_identity(space
);
10390 sv
= isl_basic_map_is_subset(test
, id
);
10392 isl_basic_map_free(test
);
10393 isl_basic_map_free(id
);
10398 /* Check if the given map is obviously single-valued.
10400 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10409 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10412 /* Check if the given map is single-valued.
10413 * We simply compute
10417 * and check if the result is a subset of the identity mapping.
10419 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10426 sv
= isl_map_plain_is_single_valued(map
);
10430 test
= isl_map_reverse(isl_map_copy(map
));
10431 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10433 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10434 id
= isl_map_identity(dim
);
10436 sv
= isl_map_is_subset(test
, id
);
10438 isl_map_free(test
);
10444 int isl_map_is_injective(__isl_keep isl_map
*map
)
10448 map
= isl_map_copy(map
);
10449 map
= isl_map_reverse(map
);
10450 in
= isl_map_is_single_valued(map
);
10456 /* Check if the given map is obviously injective.
10458 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10462 map
= isl_map_copy(map
);
10463 map
= isl_map_reverse(map
);
10464 in
= isl_map_plain_is_single_valued(map
);
10470 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10474 sv
= isl_map_is_single_valued(map
);
10478 return isl_map_is_injective(map
);
10481 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10483 return isl_map_is_single_valued((isl_map
*)set
);
10486 int isl_map_is_translation(__isl_keep isl_map
*map
)
10491 delta
= isl_map_deltas(isl_map_copy(map
));
10492 ok
= isl_set_is_singleton(delta
);
10493 isl_set_free(delta
);
10498 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10500 if (isl_seq_first_non_zero(p
, pos
) != -1)
10502 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10507 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10516 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10519 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10520 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10521 for (j
= 0; j
< nvar
; ++j
) {
10522 int lower
= 0, upper
= 0;
10523 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10524 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10526 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10530 if (i
< bset
->n_eq
)
10532 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10533 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10535 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10537 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10542 if (!lower
|| !upper
)
10549 int isl_set_is_box(__isl_keep isl_set
*set
)
10556 return isl_basic_set_is_box(set
->p
[0]);
10559 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10564 return isl_space_is_wrapping(bset
->dim
);
10567 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10572 return isl_space_is_wrapping(set
->dim
);
10575 /* Is the domain of "map" a wrapped relation?
10577 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10582 return isl_space_domain_is_wrapping(map
->dim
);
10585 /* Is the range of "map" a wrapped relation?
10587 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10592 return isl_space_range_is_wrapping(map
->dim
);
10595 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10597 bmap
= isl_basic_map_cow(bmap
);
10601 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10605 bmap
= isl_basic_map_finalize(bmap
);
10607 return (isl_basic_set
*)bmap
;
10609 isl_basic_map_free(bmap
);
10613 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10617 map
= isl_map_cow(map
);
10621 for (i
= 0; i
< map
->n
; ++i
) {
10622 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10626 map
->dim
= isl_space_wrap(map
->dim
);
10630 return (isl_set
*)map
;
10636 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10638 bset
= isl_basic_set_cow(bset
);
10642 bset
->dim
= isl_space_unwrap(bset
->dim
);
10646 bset
= isl_basic_set_finalize(bset
);
10648 return (isl_basic_map
*)bset
;
10650 isl_basic_set_free(bset
);
10654 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10661 if (!isl_set_is_wrapping(set
))
10662 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10665 set
= isl_set_cow(set
);
10669 for (i
= 0; i
< set
->n
; ++i
) {
10670 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10675 set
->dim
= isl_space_unwrap(set
->dim
);
10679 return (isl_map
*)set
;
10685 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10686 enum isl_dim_type type
)
10691 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10694 bmap
= isl_basic_map_cow(bmap
);
10698 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10702 bmap
= isl_basic_map_finalize(bmap
);
10706 isl_basic_map_free(bmap
);
10710 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10711 enum isl_dim_type type
)
10718 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10721 map
= isl_map_cow(map
);
10725 for (i
= 0; i
< map
->n
; ++i
) {
10726 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10730 map
->dim
= isl_space_reset(map
->dim
, type
);
10740 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10745 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10748 bmap
= isl_basic_map_cow(bmap
);
10752 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10756 bmap
= isl_basic_map_finalize(bmap
);
10760 isl_basic_map_free(bmap
);
10764 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10766 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10769 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10770 __isl_take isl_basic_map
*bmap
)
10775 if (!bmap
->dim
->nested
[0])
10778 bmap
= isl_basic_map_cow(bmap
);
10782 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10786 bmap
= isl_basic_map_finalize(bmap
);
10790 isl_basic_map_free(bmap
);
10794 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10795 __isl_take isl_basic_map
*bmap
)
10800 if (!bmap
->dim
->nested
[1])
10803 bmap
= isl_basic_map_cow(bmap
);
10807 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10811 bmap
= isl_basic_map_finalize(bmap
);
10815 isl_basic_map_free(bmap
);
10819 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10826 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10829 map
= isl_map_cow(map
);
10833 for (i
= 0; i
< map
->n
; ++i
) {
10834 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10838 map
->dim
= isl_space_flatten(map
->dim
);
10848 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10850 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10853 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10855 isl_space
*dim
, *flat_dim
;
10858 dim
= isl_set_get_space(set
);
10859 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10860 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10861 map
= isl_map_intersect_domain(map
, set
);
10866 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10873 if (!map
->dim
->nested
[0])
10876 map
= isl_map_cow(map
);
10880 for (i
= 0; i
< map
->n
; ++i
) {
10881 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10885 map
->dim
= isl_space_flatten_domain(map
->dim
);
10895 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10902 if (!map
->dim
->nested
[1])
10905 map
= isl_map_cow(map
);
10909 for (i
= 0; i
< map
->n
; ++i
) {
10910 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10914 map
->dim
= isl_space_flatten_range(map
->dim
);
10924 /* Reorder the dimensions of "bmap" according to the given dim_map
10925 * and set the dimension specification to "dim".
10927 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10928 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10930 isl_basic_map
*res
;
10933 bmap
= isl_basic_map_cow(bmap
);
10934 if (!bmap
|| !dim
|| !dim_map
)
10937 flags
= bmap
->flags
;
10938 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10939 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10940 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10941 res
= isl_basic_map_alloc_space(dim
,
10942 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10943 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10945 res
->flags
= flags
;
10946 res
= isl_basic_map_finalize(res
);
10950 isl_basic_map_free(bmap
);
10951 isl_space_free(dim
);
10955 /* Reorder the dimensions of "map" according to given reordering.
10957 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10958 __isl_take isl_reordering
*r
)
10961 struct isl_dim_map
*dim_map
;
10963 map
= isl_map_cow(map
);
10964 dim_map
= isl_dim_map_from_reordering(r
);
10965 if (!map
|| !r
|| !dim_map
)
10968 for (i
= 0; i
< map
->n
; ++i
) {
10969 struct isl_dim_map
*dim_map_i
;
10971 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10973 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10974 isl_space_copy(r
->dim
), dim_map_i
);
10980 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10982 isl_reordering_free(r
);
10988 isl_reordering_free(r
);
10992 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10993 __isl_take isl_reordering
*r
)
10995 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10998 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10999 __isl_take isl_space
*model
)
11003 if (!map
|| !model
)
11006 ctx
= isl_space_get_ctx(model
);
11007 if (!isl_space_has_named_params(model
))
11008 isl_die(ctx
, isl_error_invalid
,
11009 "model has unnamed parameters", goto error
);
11010 if (!isl_space_has_named_params(map
->dim
))
11011 isl_die(ctx
, isl_error_invalid
,
11012 "relation has unnamed parameters", goto error
);
11013 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11014 isl_reordering
*exp
;
11016 model
= isl_space_drop_dims(model
, isl_dim_in
,
11017 0, isl_space_dim(model
, isl_dim_in
));
11018 model
= isl_space_drop_dims(model
, isl_dim_out
,
11019 0, isl_space_dim(model
, isl_dim_out
));
11020 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11021 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11022 map
= isl_map_realign(map
, exp
);
11025 isl_space_free(model
);
11028 isl_space_free(model
);
11033 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11034 __isl_take isl_space
*model
)
11036 return isl_map_align_params(set
, model
);
11039 /* Align the parameters of "bmap" to those of "model", introducing
11040 * additional parameters if needed.
11042 __isl_give isl_basic_map
*isl_basic_map_align_params(
11043 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11047 if (!bmap
|| !model
)
11050 ctx
= isl_space_get_ctx(model
);
11051 if (!isl_space_has_named_params(model
))
11052 isl_die(ctx
, isl_error_invalid
,
11053 "model has unnamed parameters", goto error
);
11054 if (!isl_space_has_named_params(bmap
->dim
))
11055 isl_die(ctx
, isl_error_invalid
,
11056 "relation has unnamed parameters", goto error
);
11057 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11058 isl_reordering
*exp
;
11059 struct isl_dim_map
*dim_map
;
11061 model
= isl_space_drop_dims(model
, isl_dim_in
,
11062 0, isl_space_dim(model
, isl_dim_in
));
11063 model
= isl_space_drop_dims(model
, isl_dim_out
,
11064 0, isl_space_dim(model
, isl_dim_out
));
11065 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11066 exp
= isl_reordering_extend_space(exp
,
11067 isl_basic_map_get_space(bmap
));
11068 dim_map
= isl_dim_map_from_reordering(exp
);
11069 bmap
= isl_basic_map_realign(bmap
,
11070 exp
? isl_space_copy(exp
->dim
) : NULL
,
11071 isl_dim_map_extend(dim_map
, bmap
));
11072 isl_reordering_free(exp
);
11076 isl_space_free(model
);
11079 isl_space_free(model
);
11080 isl_basic_map_free(bmap
);
11084 /* Align the parameters of "bset" to those of "model", introducing
11085 * additional parameters if needed.
11087 __isl_give isl_basic_set
*isl_basic_set_align_params(
11088 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11090 return isl_basic_map_align_params(bset
, model
);
11093 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11094 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11095 enum isl_dim_type c2
, enum isl_dim_type c3
,
11096 enum isl_dim_type c4
, enum isl_dim_type c5
)
11098 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11099 struct isl_mat
*mat
;
11105 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11106 isl_basic_map_total_dim(bmap
) + 1);
11109 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11110 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11111 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11112 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11113 isl_int_set(mat
->row
[i
][pos
],
11114 bmap
->eq
[i
][off
+ k
]);
11122 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11123 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11124 enum isl_dim_type c2
, enum isl_dim_type c3
,
11125 enum isl_dim_type c4
, enum isl_dim_type c5
)
11127 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11128 struct isl_mat
*mat
;
11134 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11135 isl_basic_map_total_dim(bmap
) + 1);
11138 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11139 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11140 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11141 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11142 isl_int_set(mat
->row
[i
][pos
],
11143 bmap
->ineq
[i
][off
+ k
]);
11151 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11152 __isl_take isl_space
*dim
,
11153 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11154 enum isl_dim_type c2
, enum isl_dim_type c3
,
11155 enum isl_dim_type c4
, enum isl_dim_type c5
)
11157 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11158 isl_basic_map
*bmap
;
11164 if (!dim
|| !eq
|| !ineq
)
11167 if (eq
->n_col
!= ineq
->n_col
)
11168 isl_die(dim
->ctx
, isl_error_invalid
,
11169 "equalities and inequalities matrices should have "
11170 "same number of columns", goto error
);
11172 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11174 if (eq
->n_col
< total
)
11175 isl_die(dim
->ctx
, isl_error_invalid
,
11176 "number of columns too small", goto error
);
11178 extra
= eq
->n_col
- total
;
11180 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11181 eq
->n_row
, ineq
->n_row
);
11184 for (i
= 0; i
< extra
; ++i
) {
11185 k
= isl_basic_map_alloc_div(bmap
);
11188 isl_int_set_si(bmap
->div
[k
][0], 0);
11190 for (i
= 0; i
< eq
->n_row
; ++i
) {
11191 l
= isl_basic_map_alloc_equality(bmap
);
11194 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11195 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11196 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11197 isl_int_set(bmap
->eq
[l
][off
+ k
],
11203 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11204 l
= isl_basic_map_alloc_inequality(bmap
);
11207 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11208 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11209 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11210 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11211 ineq
->row
[i
][pos
]);
11217 isl_space_free(dim
);
11219 isl_mat_free(ineq
);
11221 bmap
= isl_basic_map_simplify(bmap
);
11222 return isl_basic_map_finalize(bmap
);
11224 isl_space_free(dim
);
11226 isl_mat_free(ineq
);
11230 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11231 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11232 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11234 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11235 c1
, c2
, c3
, c4
, isl_dim_in
);
11238 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11239 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11240 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11242 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11243 c1
, c2
, c3
, c4
, isl_dim_in
);
11246 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11247 __isl_take isl_space
*dim
,
11248 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11249 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11251 return (isl_basic_set
*)
11252 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11253 c1
, c2
, c3
, c4
, isl_dim_in
);
11256 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11261 return isl_space_can_zip(bmap
->dim
);
11264 int isl_map_can_zip(__isl_keep isl_map
*map
)
11269 return isl_space_can_zip(map
->dim
);
11272 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11273 * (A -> C) -> (B -> D).
11275 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11284 if (!isl_basic_map_can_zip(bmap
))
11285 isl_die(bmap
->ctx
, isl_error_invalid
,
11286 "basic map cannot be zipped", goto error
);
11287 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11288 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11289 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11290 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11291 bmap
= isl_basic_map_cow(bmap
);
11292 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11295 bmap
->dim
= isl_space_zip(bmap
->dim
);
11300 isl_basic_map_free(bmap
);
11304 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11305 * (A -> C) -> (B -> D).
11307 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11314 if (!isl_map_can_zip(map
))
11315 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11318 map
= isl_map_cow(map
);
11322 for (i
= 0; i
< map
->n
; ++i
) {
11323 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11328 map
->dim
= isl_space_zip(map
->dim
);
11338 /* Can we apply isl_basic_map_curry to "bmap"?
11339 * That is, does it have a nested relation in its domain?
11341 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11346 return isl_space_can_curry(bmap
->dim
);
11349 /* Can we apply isl_map_curry to "map"?
11350 * That is, does it have a nested relation in its domain?
11352 int isl_map_can_curry(__isl_keep isl_map
*map
)
11357 return isl_space_can_curry(map
->dim
);
11360 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11363 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11369 if (!isl_basic_map_can_curry(bmap
))
11370 isl_die(bmap
->ctx
, isl_error_invalid
,
11371 "basic map cannot be curried", goto error
);
11372 bmap
= isl_basic_map_cow(bmap
);
11375 bmap
->dim
= isl_space_curry(bmap
->dim
);
11380 isl_basic_map_free(bmap
);
11384 /* Given a map (A -> B) -> C, return the corresponding map
11387 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11394 if (!isl_map_can_curry(map
))
11395 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11398 map
= isl_map_cow(map
);
11402 for (i
= 0; i
< map
->n
; ++i
) {
11403 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11408 map
->dim
= isl_space_curry(map
->dim
);
11418 /* Can we apply isl_basic_map_uncurry to "bmap"?
11419 * That is, does it have a nested relation in its domain?
11421 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11426 return isl_space_can_uncurry(bmap
->dim
);
11429 /* Can we apply isl_map_uncurry to "map"?
11430 * That is, does it have a nested relation in its domain?
11432 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11437 return isl_space_can_uncurry(map
->dim
);
11440 /* Given a basic map A -> (B -> C), return the corresponding basic map
11443 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11449 if (!isl_basic_map_can_uncurry(bmap
))
11450 isl_die(bmap
->ctx
, isl_error_invalid
,
11451 "basic map cannot be uncurried",
11452 return isl_basic_map_free(bmap
));
11453 bmap
= isl_basic_map_cow(bmap
);
11456 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11458 return isl_basic_map_free(bmap
);
11462 /* Given a map A -> (B -> C), return the corresponding map
11465 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11472 if (!isl_map_can_uncurry(map
))
11473 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11474 return isl_map_free(map
));
11476 map
= isl_map_cow(map
);
11480 for (i
= 0; i
< map
->n
; ++i
) {
11481 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11483 return isl_map_free(map
);
11486 map
->dim
= isl_space_uncurry(map
->dim
);
11488 return isl_map_free(map
);
11493 /* Construct a basic map mapping the domain of the affine expression
11494 * to a one-dimensional range prescribed by the affine expression.
11496 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11500 isl_local_space
*ls
;
11501 isl_basic_map
*bmap
;
11506 ls
= isl_aff_get_local_space(aff
);
11507 bmap
= isl_basic_map_from_local_space(ls
);
11508 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11509 k
= isl_basic_map_alloc_equality(bmap
);
11513 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11514 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11515 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11516 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11517 aff
->v
->size
- (pos
+ 1));
11520 bmap
= isl_basic_map_finalize(bmap
);
11524 isl_basic_map_free(bmap
);
11528 /* Construct a map mapping the domain of the affine expression
11529 * to a one-dimensional range prescribed by the affine expression.
11531 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11533 isl_basic_map
*bmap
;
11535 bmap
= isl_basic_map_from_aff(aff
);
11536 return isl_map_from_basic_map(bmap
);
11539 /* Construct a basic map mapping the domain the multi-affine expression
11540 * to its range, with each dimension in the range equated to the
11541 * corresponding affine expression.
11543 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11544 __isl_take isl_multi_aff
*maff
)
11548 isl_basic_map
*bmap
;
11553 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11554 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11555 "invalid space", goto error
);
11557 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11558 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11560 for (i
= 0; i
< maff
->n
; ++i
) {
11562 isl_basic_map
*bmap_i
;
11564 aff
= isl_aff_copy(maff
->p
[i
]);
11565 bmap_i
= isl_basic_map_from_aff(aff
);
11567 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11570 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11572 isl_multi_aff_free(maff
);
11575 isl_multi_aff_free(maff
);
11579 /* Construct a map mapping the domain the multi-affine expression
11580 * to its range, with each dimension in the range equated to the
11581 * corresponding affine expression.
11583 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11585 isl_basic_map
*bmap
;
11587 bmap
= isl_basic_map_from_multi_aff(maff
);
11588 return isl_map_from_basic_map(bmap
);
11591 /* Construct a basic map mapping a domain in the given space to
11592 * to an n-dimensional range, with n the number of elements in the list,
11593 * where each coordinate in the range is prescribed by the
11594 * corresponding affine expression.
11595 * The domains of all affine expressions in the list are assumed to match
11598 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11599 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11603 isl_basic_map
*bmap
;
11608 dim
= isl_space_from_domain(domain_dim
);
11609 bmap
= isl_basic_map_universe(dim
);
11611 for (i
= 0; i
< list
->n
; ++i
) {
11613 isl_basic_map
*bmap_i
;
11615 aff
= isl_aff_copy(list
->p
[i
]);
11616 bmap_i
= isl_basic_map_from_aff(aff
);
11618 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11621 isl_aff_list_free(list
);
11625 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11626 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11628 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11631 /* Construct a basic map where the given dimensions are equal to each other.
11633 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11634 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11636 isl_basic_map
*bmap
= NULL
;
11642 if (pos1
>= isl_space_dim(space
, type1
))
11643 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11644 "index out of bounds", goto error
);
11645 if (pos2
>= isl_space_dim(space
, type2
))
11646 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11647 "index out of bounds", goto error
);
11649 if (type1
== type2
&& pos1
== pos2
)
11650 return isl_basic_map_universe(space
);
11652 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11653 i
= isl_basic_map_alloc_equality(bmap
);
11656 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11657 pos1
+= isl_basic_map_offset(bmap
, type1
);
11658 pos2
+= isl_basic_map_offset(bmap
, type2
);
11659 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11660 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11661 bmap
= isl_basic_map_finalize(bmap
);
11662 isl_space_free(space
);
11665 isl_space_free(space
);
11666 isl_basic_map_free(bmap
);
11670 /* Add a constraint imposing that the given two dimensions are equal.
11672 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11673 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11677 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11679 bmap
= isl_basic_map_intersect(bmap
, eq
);
11684 /* Add a constraint imposing that the given two dimensions are equal.
11686 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11687 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11689 isl_basic_map
*bmap
;
11691 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11693 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11698 /* Add a constraint imposing that the given two dimensions have opposite values.
11700 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11701 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11703 isl_basic_map
*bmap
= NULL
;
11709 if (pos1
>= isl_map_dim(map
, type1
))
11710 isl_die(map
->ctx
, isl_error_invalid
,
11711 "index out of bounds", goto error
);
11712 if (pos2
>= isl_map_dim(map
, type2
))
11713 isl_die(map
->ctx
, isl_error_invalid
,
11714 "index out of bounds", goto error
);
11716 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11717 i
= isl_basic_map_alloc_equality(bmap
);
11720 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11721 pos1
+= isl_basic_map_offset(bmap
, type1
);
11722 pos2
+= isl_basic_map_offset(bmap
, type2
);
11723 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11724 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11725 bmap
= isl_basic_map_finalize(bmap
);
11727 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11731 isl_basic_map_free(bmap
);
11736 /* Construct a constraint imposing that the value of the first dimension is
11737 * greater than or equal to that of the second.
11739 static __isl_give isl_constraint
*constraint_order_ge(
11740 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11741 enum isl_dim_type type2
, int pos2
)
11748 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11750 if (pos1
>= isl_constraint_dim(c
, type1
))
11751 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11752 "index out of bounds", return isl_constraint_free(c
));
11753 if (pos2
>= isl_constraint_dim(c
, type2
))
11754 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11755 "index out of bounds", return isl_constraint_free(c
));
11757 if (type1
== type2
&& pos1
== pos2
)
11760 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11761 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11766 /* Add a constraint imposing that the value of the first dimension is
11767 * greater than or equal to that of the second.
11769 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11770 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11775 if (type1
== type2
&& pos1
== pos2
)
11777 space
= isl_basic_map_get_space(bmap
);
11778 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11779 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11784 /* Add a constraint imposing that the value of the first dimension is
11785 * greater than or equal to that of the second.
11787 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11788 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11793 if (type1
== type2
&& pos1
== pos2
)
11795 space
= isl_map_get_space(map
);
11796 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11797 map
= isl_map_add_constraint(map
, c
);
11802 /* Add a constraint imposing that the value of the first dimension is
11803 * less than or equal to that of the second.
11805 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11806 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11808 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11811 /* Construct a basic map where the value of the first dimension is
11812 * greater than that of the second.
11814 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11815 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11817 isl_basic_map
*bmap
= NULL
;
11823 if (pos1
>= isl_space_dim(space
, type1
))
11824 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11825 "index out of bounds", goto error
);
11826 if (pos2
>= isl_space_dim(space
, type2
))
11827 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11828 "index out of bounds", goto error
);
11830 if (type1
== type2
&& pos1
== pos2
)
11831 return isl_basic_map_empty(space
);
11833 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11834 i
= isl_basic_map_alloc_inequality(bmap
);
11836 return isl_basic_map_free(bmap
);
11837 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11838 pos1
+= isl_basic_map_offset(bmap
, type1
);
11839 pos2
+= isl_basic_map_offset(bmap
, type2
);
11840 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11841 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11842 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11843 bmap
= isl_basic_map_finalize(bmap
);
11847 isl_space_free(space
);
11848 isl_basic_map_free(bmap
);
11852 /* Add a constraint imposing that the value of the first dimension is
11853 * greater than that of the second.
11855 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11856 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11860 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11862 bmap
= isl_basic_map_intersect(bmap
, gt
);
11867 /* Add a constraint imposing that the value of the first dimension is
11868 * greater than that of the second.
11870 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11871 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11873 isl_basic_map
*bmap
;
11875 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11877 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11882 /* Add a constraint imposing that the value of the first dimension is
11883 * smaller than that of the second.
11885 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11886 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11888 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11891 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11895 isl_local_space
*ls
;
11900 if (!isl_basic_map_divs_known(bmap
))
11901 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11902 "some divs are unknown", return NULL
);
11904 ls
= isl_basic_map_get_local_space(bmap
);
11905 div
= isl_local_space_get_div(ls
, pos
);
11906 isl_local_space_free(ls
);
11911 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11914 return isl_basic_map_get_div(bset
, pos
);
11917 /* Plug in "subs" for dimension "type", "pos" of "bset".
11919 * Let i be the dimension to replace and let "subs" be of the form
11923 * Any integer division with a non-zero coefficient for i,
11925 * floor((a i + g)/m)
11929 * floor((a f + d g)/(m d))
11931 * Constraints of the form
11939 * We currently require that "subs" is an integral expression.
11940 * Handling rational expressions may require us to add stride constraints
11941 * as we do in isl_basic_set_preimage_multi_aff.
11943 __isl_give isl_basic_set
*isl_basic_set_substitute(
11944 __isl_take isl_basic_set
*bset
,
11945 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11951 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11954 bset
= isl_basic_set_cow(bset
);
11955 if (!bset
|| !subs
)
11958 ctx
= isl_basic_set_get_ctx(bset
);
11959 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11960 isl_die(ctx
, isl_error_invalid
,
11961 "spaces don't match", goto error
);
11962 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11963 isl_die(ctx
, isl_error_unsupported
,
11964 "cannot handle divs yet", goto error
);
11965 if (!isl_int_is_one(subs
->v
->el
[0]))
11966 isl_die(ctx
, isl_error_invalid
,
11967 "can only substitute integer expressions", goto error
);
11969 pos
+= isl_basic_set_offset(bset
, type
);
11973 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11974 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11976 isl_int_set(v
, bset
->eq
[i
][pos
]);
11977 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11978 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11979 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11982 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11983 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11985 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11986 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11987 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11988 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11991 for (i
= 0; i
< bset
->n_div
; ++i
) {
11992 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11994 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11995 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11996 isl_seq_combine(bset
->div
[i
] + 1,
11997 subs
->v
->el
[0], bset
->div
[i
] + 1,
11998 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11999 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12004 bset
= isl_basic_set_simplify(bset
);
12005 return isl_basic_set_finalize(bset
);
12007 isl_basic_set_free(bset
);
12011 /* Plug in "subs" for dimension "type", "pos" of "set".
12013 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12014 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12018 if (set
&& isl_set_plain_is_empty(set
))
12021 set
= isl_set_cow(set
);
12025 for (i
= set
->n
- 1; i
>= 0; --i
) {
12026 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12027 if (remove_if_empty(set
, i
) < 0)
12037 /* Check if the range of "ma" is compatible with the domain or range
12038 * (depending on "type") of "bmap".
12039 * Return -1 if anything is wrong.
12041 static int check_basic_map_compatible_range_multi_aff(
12042 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12043 __isl_keep isl_multi_aff
*ma
)
12046 isl_space
*ma_space
;
12048 ma_space
= isl_multi_aff_get_space(ma
);
12050 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12054 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12055 "parameters don't match", goto error
);
12056 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12060 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12061 "spaces don't match", goto error
);
12063 isl_space_free(ma_space
);
12066 isl_space_free(ma_space
);
12070 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12071 * coefficients before the transformed range of dimensions,
12072 * the "n_after" coefficients after the transformed range of dimensions
12073 * and the coefficients of the other divs in "bmap".
12075 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12076 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12081 isl_local_space
*ls
;
12086 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12090 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12091 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12092 for (i
= 0; i
< n_div
; ++i
) {
12093 int o_bmap
= 0, o_ls
= 0;
12095 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12096 o_bmap
+= 1 + 1 + n_param
;
12097 o_ls
+= 1 + 1 + n_param
;
12098 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12099 o_bmap
+= n_before
;
12100 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12101 ls
->div
->row
[i
] + o_ls
, n_set
);
12104 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12106 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12107 ls
->div
->row
[i
] + o_ls
, n_div
);
12110 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12111 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12115 isl_local_space_free(ls
);
12118 isl_local_space_free(ls
);
12122 /* How many stride constraints does "ma" enforce?
12123 * That is, how many of the affine expressions have a denominator
12124 * different from one?
12126 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12131 for (i
= 0; i
< ma
->n
; ++i
)
12132 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12138 /* For each affine expression in ma of the form
12140 * x_i = (f_i y + h_i)/m_i
12142 * with m_i different from one, add a constraint to "bmap"
12145 * f_i y + h_i = m_i alpha_i
12147 * with alpha_i an additional existentially quantified variable.
12149 static __isl_give isl_basic_map
*add_ma_strides(
12150 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12151 int n_before
, int n_after
)
12160 total
= isl_basic_map_total_dim(bmap
);
12161 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12162 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12163 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12164 for (i
= 0; i
< ma
->n
; ++i
) {
12165 int o_bmap
= 0, o_ma
= 1;
12167 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12169 div
= isl_basic_map_alloc_div(bmap
);
12170 k
= isl_basic_map_alloc_equality(bmap
);
12171 if (div
< 0 || k
< 0)
12173 isl_int_set_si(bmap
->div
[div
][0], 0);
12174 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12175 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12176 o_bmap
+= 1 + n_param
;
12177 o_ma
+= 1 + n_param
;
12178 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12179 o_bmap
+= n_before
;
12180 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12181 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12184 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12186 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12187 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12190 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12191 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12197 isl_basic_map_free(bmap
);
12201 /* Replace the domain or range space (depending on "type) of "space" by "set".
12203 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12204 enum isl_dim_type type
, __isl_take isl_space
*set
)
12206 if (type
== isl_dim_in
) {
12207 space
= isl_space_range(space
);
12208 space
= isl_space_map_from_domain_and_range(set
, space
);
12210 space
= isl_space_domain(space
);
12211 space
= isl_space_map_from_domain_and_range(space
, set
);
12217 /* Compute the preimage of the domain or range (depending on "type")
12218 * of "bmap" under the function represented by "ma".
12219 * In other words, plug in "ma" in the domain or range of "bmap".
12220 * The result is a basic map that lives in the same space as "bmap"
12221 * except that the domain or range has been replaced by
12222 * the domain space of "ma".
12224 * If bmap is represented by
12226 * A(p) + S u + B x + T v + C(divs) >= 0,
12228 * where u and x are input and output dimensions if type == isl_dim_out
12229 * while x and v are input and output dimensions if type == isl_dim_in,
12230 * and ma is represented by
12232 * x = D(p) + F(y) + G(divs')
12234 * then the result is
12236 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12238 * The divs in the input set are similarly adjusted.
12241 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12245 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12246 * B_i G(divs') + c_i(divs))/n_i)
12248 * If bmap is not a rational map and if F(y) involves any denominators
12250 * x_i = (f_i y + h_i)/m_i
12252 * then additional constraints are added to ensure that we only
12253 * map back integer points. That is we enforce
12255 * f_i y + h_i = m_i alpha_i
12257 * with alpha_i an additional existentially quantified variable.
12259 * We first copy over the divs from "ma".
12260 * Then we add the modified constraints and divs from "bmap".
12261 * Finally, we add the stride constraints, if needed.
12263 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12264 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12265 __isl_take isl_multi_aff
*ma
)
12269 isl_basic_map
*res
= NULL
;
12270 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12271 isl_int f
, c1
, c2
, g
;
12272 int rational
, strides
;
12279 ma
= isl_multi_aff_align_divs(ma
);
12282 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12285 if (type
== isl_dim_in
) {
12287 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12289 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12292 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12293 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12295 space
= isl_multi_aff_get_domain_space(ma
);
12296 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12297 rational
= isl_basic_map_is_rational(bmap
);
12298 strides
= rational
? 0 : multi_aff_strides(ma
);
12299 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12300 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12302 res
= isl_basic_map_set_rational(res
);
12304 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12305 if (isl_basic_map_alloc_div(res
) < 0)
12308 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12311 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12312 k
= isl_basic_map_alloc_equality(res
);
12315 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12316 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12319 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12320 k
= isl_basic_map_alloc_inequality(res
);
12323 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12324 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12327 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12328 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12329 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12332 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12333 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12338 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12344 isl_basic_map_free(bmap
);
12345 isl_multi_aff_free(ma
);
12346 res
= isl_basic_set_simplify(res
);
12347 return isl_basic_map_finalize(res
);
12353 isl_basic_map_free(bmap
);
12354 isl_multi_aff_free(ma
);
12355 isl_basic_map_free(res
);
12359 /* Compute the preimage of "bset" under the function represented by "ma".
12360 * In other words, plug in "ma" in "bset". The result is a basic set
12361 * that lives in the domain space of "ma".
12363 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12364 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12366 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12369 /* Compute the preimage of the domain of "bmap" under the function
12370 * represented by "ma".
12371 * In other words, plug in "ma" in the domain of "bmap".
12372 * The result is a basic map that lives in the same space as "bmap"
12373 * except that the domain has been replaced by the domain space of "ma".
12375 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12376 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12378 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12381 /* Compute the preimage of the range of "bmap" under the function
12382 * represented by "ma".
12383 * In other words, plug in "ma" in the range of "bmap".
12384 * The result is a basic map that lives in the same space as "bmap"
12385 * except that the range has been replaced by the domain space of "ma".
12387 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12388 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12390 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12393 /* Check if the range of "ma" is compatible with the domain or range
12394 * (depending on "type") of "map".
12395 * Return -1 if anything is wrong.
12397 static int check_map_compatible_range_multi_aff(
12398 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12399 __isl_keep isl_multi_aff
*ma
)
12402 isl_space
*ma_space
;
12404 ma_space
= isl_multi_aff_get_space(ma
);
12405 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12406 isl_space_free(ma_space
);
12408 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12409 "spaces don't match", return -1);
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 * The parameters are assumed to have been aligned.
12422 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12423 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12428 map
= isl_map_cow(map
);
12429 ma
= isl_multi_aff_align_divs(ma
);
12432 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12435 for (i
= 0; i
< map
->n
; ++i
) {
12436 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12437 isl_multi_aff_copy(ma
));
12442 space
= isl_multi_aff_get_domain_space(ma
);
12443 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12445 isl_space_free(map
->dim
);
12450 isl_multi_aff_free(ma
);
12452 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12453 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12456 isl_multi_aff_free(ma
);
12461 /* Compute the preimage of the domain or range (depending on "type")
12462 * of "map" under the function represented by "ma".
12463 * In other words, plug in "ma" in the domain or range of "map".
12464 * The result is a map that lives in the same space as "map"
12465 * except that the domain or range has been replaced by
12466 * the domain space of "ma".
12468 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12469 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12474 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12475 return map_preimage_multi_aff(map
, type
, ma
);
12477 if (!isl_space_has_named_params(map
->dim
) ||
12478 !isl_space_has_named_params(ma
->space
))
12479 isl_die(map
->ctx
, isl_error_invalid
,
12480 "unaligned unnamed parameters", goto error
);
12481 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12482 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12484 return map_preimage_multi_aff(map
, type
, ma
);
12486 isl_multi_aff_free(ma
);
12487 return isl_map_free(map
);
12490 /* Compute the preimage of "set" under the function represented by "ma".
12491 * In other words, plug in "ma" in "set". The result is a set
12492 * that lives in the domain space of "ma".
12494 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12495 __isl_take isl_multi_aff
*ma
)
12497 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12500 /* Compute the preimage of the domain of "map" under the function
12501 * represented by "ma".
12502 * In other words, plug in "ma" in the domain of "map".
12503 * The result is a map that lives in the same space as "map"
12504 * except that the domain has been replaced by the domain space of "ma".
12506 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12507 __isl_take isl_multi_aff
*ma
)
12509 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12512 /* Compute the preimage of the range of "map" under the function
12513 * represented by "ma".
12514 * In other words, plug in "ma" in the range of "map".
12515 * The result is a map that lives in the same space as "map"
12516 * except that the range has been replaced by the domain space of "ma".
12518 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12519 __isl_take isl_multi_aff
*ma
)
12521 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12524 /* Compute the preimage of "map" under the function represented by "pma".
12525 * In other words, plug in "pma" in the domain or range of "map".
12526 * The result is a map that lives in the same space as "map",
12527 * except that the space of type "type" has been replaced by
12528 * the domain space of "pma".
12530 * The parameters of "map" and "pma" are assumed to have been aligned.
12532 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12533 __isl_take isl_map
*map
, enum isl_dim_type type
,
12534 __isl_take isl_pw_multi_aff
*pma
)
12543 isl_pw_multi_aff_free(pma
);
12544 res
= isl_map_empty(isl_map_get_space(map
));
12549 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12550 isl_multi_aff_copy(pma
->p
[0].maff
));
12551 if (type
== isl_dim_in
)
12552 res
= isl_map_intersect_domain(res
,
12553 isl_map_copy(pma
->p
[0].set
));
12555 res
= isl_map_intersect_range(res
,
12556 isl_map_copy(pma
->p
[0].set
));
12558 for (i
= 1; i
< pma
->n
; ++i
) {
12561 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12562 isl_multi_aff_copy(pma
->p
[i
].maff
));
12563 if (type
== isl_dim_in
)
12564 res_i
= isl_map_intersect_domain(res_i
,
12565 isl_map_copy(pma
->p
[i
].set
));
12567 res_i
= isl_map_intersect_range(res_i
,
12568 isl_map_copy(pma
->p
[i
].set
));
12569 res
= isl_map_union(res
, res_i
);
12572 isl_pw_multi_aff_free(pma
);
12576 isl_pw_multi_aff_free(pma
);
12581 /* Compute the preimage of "map" under the function represented by "pma".
12582 * In other words, plug in "pma" in the domain or range of "map".
12583 * The result is a map that lives in the same space as "map",
12584 * except that the space of type "type" has been replaced by
12585 * the domain space of "pma".
12587 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12588 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12593 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12594 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12596 if (!isl_space_has_named_params(map
->dim
) ||
12597 !isl_space_has_named_params(pma
->dim
))
12598 isl_die(map
->ctx
, isl_error_invalid
,
12599 "unaligned unnamed parameters", goto error
);
12600 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12601 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12603 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12605 isl_pw_multi_aff_free(pma
);
12606 return isl_map_free(map
);
12609 /* Compute the preimage of "set" under the function represented by "pma".
12610 * In other words, plug in "pma" in "set". The result is a set
12611 * that lives in the domain space of "pma".
12613 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12614 __isl_take isl_pw_multi_aff
*pma
)
12616 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12619 /* Compute the preimage of the domain of "map" under the function
12620 * represented by "pma".
12621 * In other words, plug in "pma" in the domain of "map".
12622 * The result is a map that lives in the same space as "map",
12623 * except that domain space has been replaced by the domain space of "pma".
12625 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12626 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12628 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12631 /* Compute the preimage of the range of "map" under the function
12632 * represented by "pma".
12633 * In other words, plug in "pma" in the range of "map".
12634 * The result is a map that lives in the same space as "map",
12635 * except that range space has been replaced by the domain space of "pma".
12637 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12638 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12640 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12643 /* Compute the preimage of "map" under the function represented by "mpa".
12644 * In other words, plug in "mpa" in the domain or range of "map".
12645 * The result is a map that lives in the same space as "map",
12646 * except that the space of type "type" has been replaced by
12647 * the domain space of "mpa".
12649 * If the map does not involve any constraints that refer to the
12650 * dimensions of the substituted space, then the only possible
12651 * effect of "mpa" on the map is to map the space to a different space.
12652 * We create a separate isl_multi_aff to effectuate this change
12653 * in order to avoid spurious splitting of the map along the pieces
12656 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12657 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12660 isl_pw_multi_aff
*pma
;
12665 n
= isl_map_dim(map
, type
);
12666 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12670 space
= isl_multi_pw_aff_get_space(mpa
);
12671 isl_multi_pw_aff_free(mpa
);
12672 ma
= isl_multi_aff_zero(space
);
12673 return isl_map_preimage_multi_aff(map
, type
, ma
);
12676 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12677 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12680 isl_multi_pw_aff_free(mpa
);
12684 /* Compute the preimage of "map" under the function represented by "mpa".
12685 * In other words, plug in "mpa" in the domain "map".
12686 * The result is a map that lives in the same space as "map",
12687 * except that domain space has been replaced by the domain space of "mpa".
12689 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12690 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12692 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12695 /* Compute the preimage of "set" by the function represented by "mpa".
12696 * In other words, plug in "mpa" in "set".
12698 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12699 __isl_take isl_multi_pw_aff
*mpa
)
12701 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);