2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
22 #include "isl_space_private.h"
23 #include "isl_equalities.h"
24 #include <isl_lp_private.h>
28 #include <isl_reordering.h>
29 #include "isl_sample.h"
33 #include <isl_mat_private.h>
34 #include <isl_vec_private.h>
35 #include <isl_dim_map.h>
36 #include <isl_local_space_private.h>
37 #include <isl_aff_private.h>
38 #include <isl_options_private.h>
39 #include <isl_morph.h>
40 #include <isl_val_private.h>
41 #include <isl/deprecated/map_int.h>
42 #include <isl/deprecated/set_int.h>
44 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
47 case isl_dim_param
: return dim
->nparam
;
48 case isl_dim_in
: return dim
->n_in
;
49 case isl_dim_out
: return dim
->n_out
;
50 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
55 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
58 case isl_dim_param
: return 1;
59 case isl_dim_in
: return 1 + dim
->nparam
;
60 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
65 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
66 enum isl_dim_type type
)
71 case isl_dim_cst
: return 1;
74 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
75 case isl_dim_div
: return bmap
->n_div
;
76 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
81 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
83 return map
? n(map
->dim
, type
) : 0;
86 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
88 return set
? n(set
->dim
, type
) : 0;
91 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
92 enum isl_dim_type type
)
94 isl_space
*dim
= bmap
->dim
;
96 case isl_dim_cst
: return 0;
97 case isl_dim_param
: return 1;
98 case isl_dim_in
: return 1 + dim
->nparam
;
99 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
100 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
105 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
106 enum isl_dim_type type
)
108 return isl_basic_map_offset(bset
, type
);
111 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
113 return pos(map
->dim
, type
);
116 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
117 enum isl_dim_type type
)
119 return isl_basic_map_dim(bset
, type
);
122 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
124 return isl_basic_set_dim(bset
, isl_dim_set
);
127 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
129 return isl_basic_set_dim(bset
, isl_dim_param
);
132 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
136 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
139 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
141 return isl_set_dim(set
, isl_dim_set
);
144 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
146 return isl_set_dim(set
, isl_dim_param
);
149 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
151 return bmap
? bmap
->dim
->n_in
: 0;
154 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
156 return bmap
? bmap
->dim
->n_out
: 0;
159 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
161 return bmap
? bmap
->dim
->nparam
: 0;
164 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
166 return bmap
? bmap
->n_div
: 0;
169 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
171 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
174 unsigned isl_map_n_in(const struct isl_map
*map
)
176 return map
? map
->dim
->n_in
: 0;
179 unsigned isl_map_n_out(const struct isl_map
*map
)
181 return map
? map
->dim
->n_out
: 0;
184 unsigned isl_map_n_param(const struct isl_map
*map
)
186 return map
? map
->dim
->nparam
: 0;
189 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
194 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
197 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
200 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
201 struct isl_basic_set
*bset
)
206 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
209 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
212 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
217 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
220 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
223 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
224 struct isl_basic_set
*bset
)
229 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
232 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
235 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
237 return bmap
? bmap
->ctx
: NULL
;
240 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
242 return bset
? bset
->ctx
: NULL
;
245 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
247 return map
? map
->ctx
: NULL
;
250 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
252 return set
? set
->ctx
: NULL
;
255 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
259 return isl_space_copy(bmap
->dim
);
262 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
266 return isl_space_copy(bset
->dim
);
269 /* Extract the divs in "bmap" as a matrix.
271 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
282 ctx
= isl_basic_map_get_ctx(bmap
);
283 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
284 cols
= 1 + 1 + total
+ bmap
->n_div
;
285 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
289 for (i
= 0; i
< bmap
->n_div
; ++i
)
290 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
295 /* Extract the divs in "bset" as a matrix.
297 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
299 return isl_basic_map_get_divs(bset
);
302 __isl_give isl_local_space
*isl_basic_map_get_local_space(
303 __isl_keep isl_basic_map
*bmap
)
310 div
= isl_basic_map_get_divs(bmap
);
311 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
314 __isl_give isl_local_space
*isl_basic_set_get_local_space(
315 __isl_keep isl_basic_set
*bset
)
317 return isl_basic_map_get_local_space(bset
);
320 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
321 __isl_take isl_local_space
*ls
)
330 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
331 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
332 n_div
, 0, 2 * n_div
);
334 for (i
= 0; i
< n_div
; ++i
)
335 if (isl_basic_map_alloc_div(bmap
) < 0)
338 for (i
= 0; i
< n_div
; ++i
) {
339 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
340 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
344 isl_local_space_free(ls
);
347 isl_local_space_free(ls
);
348 isl_basic_map_free(bmap
);
352 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
353 __isl_take isl_local_space
*ls
)
355 return isl_basic_map_from_local_space(ls
);
358 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
362 return isl_space_copy(map
->dim
);
365 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
369 return isl_space_copy(set
->dim
);
372 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
373 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
375 bmap
= isl_basic_map_cow(bmap
);
378 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
381 bmap
= isl_basic_map_finalize(bmap
);
384 isl_basic_map_free(bmap
);
388 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
389 __isl_take isl_basic_set
*bset
, const char *s
)
391 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
394 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
395 enum isl_dim_type type
)
397 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
400 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
401 enum isl_dim_type type
, const char *s
)
405 map
= isl_map_cow(map
);
409 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
413 for (i
= 0; i
< map
->n
; ++i
) {
414 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
425 /* Replace the identifier of the tuple of type "type" by "id".
427 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
428 __isl_take isl_basic_map
*bmap
,
429 enum isl_dim_type type
, __isl_take isl_id
*id
)
431 bmap
= isl_basic_map_cow(bmap
);
434 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
436 return isl_basic_map_free(bmap
);
437 bmap
= isl_basic_map_finalize(bmap
);
444 /* Replace the identifier of the tuple by "id".
446 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
447 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
449 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
452 /* Does the input or output tuple have a name?
454 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
456 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
459 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
460 enum isl_dim_type type
)
462 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
465 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
468 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
471 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
472 enum isl_dim_type type
, __isl_take isl_id
*id
)
474 map
= isl_map_cow(map
);
478 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
480 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
486 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
487 __isl_take isl_id
*id
)
489 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
492 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
493 enum isl_dim_type type
)
495 map
= isl_map_cow(map
);
499 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
501 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
504 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
506 return isl_map_reset_tuple_id(set
, isl_dim_set
);
509 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
511 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
514 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
515 enum isl_dim_type type
)
517 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
520 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
522 return isl_map_has_tuple_id(set
, isl_dim_set
);
525 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
527 return isl_map_get_tuple_id(set
, isl_dim_set
);
530 /* Does the set tuple have a name?
532 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
534 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
538 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
540 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
543 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
545 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
548 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
549 enum isl_dim_type type
, unsigned pos
)
551 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
554 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
555 enum isl_dim_type type
, unsigned pos
)
557 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
560 /* Does the given dimension have a name?
562 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
563 enum isl_dim_type type
, unsigned pos
)
565 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
568 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
569 enum isl_dim_type type
, unsigned pos
)
571 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
574 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
575 enum isl_dim_type type
, unsigned pos
)
577 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
580 /* Does the given dimension have a name?
582 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
583 enum isl_dim_type type
, unsigned pos
)
585 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
588 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
589 __isl_take isl_basic_map
*bmap
,
590 enum isl_dim_type type
, unsigned pos
, const char *s
)
592 bmap
= isl_basic_map_cow(bmap
);
595 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
598 return isl_basic_map_finalize(bmap
);
600 isl_basic_map_free(bmap
);
604 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
605 enum isl_dim_type type
, unsigned pos
, const char *s
)
609 map
= isl_map_cow(map
);
613 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
617 for (i
= 0; i
< map
->n
; ++i
) {
618 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
629 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
630 __isl_take isl_basic_set
*bset
,
631 enum isl_dim_type type
, unsigned pos
, const char *s
)
633 return (isl_basic_set
*)isl_basic_map_set_dim_name(
634 (isl_basic_map
*)bset
, type
, pos
, s
);
637 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
638 enum isl_dim_type type
, unsigned pos
, const char *s
)
640 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
643 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
644 enum isl_dim_type type
, unsigned pos
)
646 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
649 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
650 enum isl_dim_type type
, unsigned pos
)
652 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
655 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
656 enum isl_dim_type type
, unsigned pos
)
658 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
661 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
662 enum isl_dim_type type
, unsigned pos
)
664 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
667 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
668 enum isl_dim_type type
, unsigned pos
)
670 return isl_map_has_dim_id(set
, type
, pos
);
673 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
674 enum isl_dim_type type
, unsigned pos
)
676 return isl_map_get_dim_id(set
, type
, pos
);
679 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
680 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
682 map
= isl_map_cow(map
);
686 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
688 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
694 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
695 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
697 return isl_map_set_dim_id(set
, type
, pos
, id
);
700 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
701 __isl_keep isl_id
*id
)
705 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
708 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
709 __isl_keep isl_id
*id
)
711 return isl_map_find_dim_by_id(set
, type
, id
);
714 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
719 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
722 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
725 return isl_map_find_dim_by_name(set
, type
, name
);
728 /* Reset the user pointer on all identifiers of parameters and tuples
729 * of the space of "map".
731 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
735 space
= isl_map_get_space(map
);
736 space
= isl_space_reset_user(space
);
737 map
= isl_map_reset_space(map
, space
);
742 /* Reset the user pointer on all identifiers of parameters and tuples
743 * of the space of "set".
745 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
747 return isl_map_reset_user(set
);
750 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
754 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
757 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
759 return isl_basic_map_is_rational(bset
);
762 /* Does "bmap" contain any rational points?
764 * If "bmap" has an equality for each dimension, equating the dimension
765 * to an integer constant, then it has no rational points, even if it
766 * is marked as rational.
768 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
770 int has_rational
= 1;
775 if (isl_basic_map_plain_is_empty(bmap
))
777 if (!isl_basic_map_is_rational(bmap
))
779 bmap
= isl_basic_map_copy(bmap
);
780 bmap
= isl_basic_map_implicit_equalities(bmap
);
783 total
= isl_basic_map_total_dim(bmap
);
784 if (bmap
->n_eq
== total
) {
786 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
787 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
790 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
791 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
793 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
801 isl_basic_map_free(bmap
);
806 /* Does "map" contain any rational points?
808 int isl_map_has_rational(__isl_keep isl_map
*map
)
815 for (i
= 0; i
< map
->n
; ++i
) {
816 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
817 if (has_rational
< 0)
825 /* Does "set" contain any rational points?
827 int isl_set_has_rational(__isl_keep isl_set
*set
)
829 return isl_map_has_rational(set
);
832 /* Is this basic set a parameter domain?
834 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
838 return isl_space_is_params(bset
->dim
);
841 /* Is this set a parameter domain?
843 int isl_set_is_params(__isl_keep isl_set
*set
)
847 return isl_space_is_params(set
->dim
);
850 /* Is this map actually a parameter domain?
851 * Users should never call this function. Outside of isl,
852 * a map can never be a parameter domain.
854 int isl_map_is_params(__isl_keep isl_map
*map
)
858 return isl_space_is_params(map
->dim
);
861 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
862 struct isl_basic_map
*bmap
, unsigned extra
,
863 unsigned n_eq
, unsigned n_ineq
)
866 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
871 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
872 if (isl_blk_is_error(bmap
->block
))
875 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
876 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
880 bmap
->block2
= isl_blk_empty();
883 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
884 if (isl_blk_is_error(bmap
->block2
))
887 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
892 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
893 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
895 for (i
= 0; i
< extra
; ++i
)
896 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
900 bmap
->c_size
= n_eq
+ n_ineq
;
901 bmap
->eq
= bmap
->ineq
+ n_ineq
;
910 isl_basic_map_free(bmap
);
914 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
915 unsigned nparam
, unsigned dim
, unsigned extra
,
916 unsigned n_eq
, unsigned n_ineq
)
918 struct isl_basic_map
*bmap
;
921 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
925 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
926 return (struct isl_basic_set
*)bmap
;
929 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
930 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
932 struct isl_basic_map
*bmap
;
935 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
936 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
937 return (struct isl_basic_set
*)bmap
;
943 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
944 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
946 struct isl_basic_map
*bmap
;
950 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
955 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
961 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
962 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
963 unsigned n_eq
, unsigned n_ineq
)
965 struct isl_basic_map
*bmap
;
968 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
972 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
976 static void dup_constraints(
977 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
980 unsigned total
= isl_basic_map_total_dim(src
);
982 for (i
= 0; i
< src
->n_eq
; ++i
) {
983 int j
= isl_basic_map_alloc_equality(dst
);
984 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
987 for (i
= 0; i
< src
->n_ineq
; ++i
) {
988 int j
= isl_basic_map_alloc_inequality(dst
);
989 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
992 for (i
= 0; i
< src
->n_div
; ++i
) {
993 int j
= isl_basic_map_alloc_div(dst
);
994 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
996 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
999 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1001 struct isl_basic_map
*dup
;
1005 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1006 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1009 dup_constraints(dup
, bmap
);
1010 dup
->flags
= bmap
->flags
;
1011 dup
->sample
= isl_vec_copy(bmap
->sample
);
1015 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1017 struct isl_basic_map
*dup
;
1019 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1020 return (struct isl_basic_set
*)dup
;
1023 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1028 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1032 return isl_basic_set_dup(bset
);
1035 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1044 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1049 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1053 bmap
= isl_basic_map_dup(bmap
);
1055 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1059 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1068 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1073 if (--bmap
->ref
> 0)
1076 isl_ctx_deref(bmap
->ctx
);
1078 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1080 isl_blk_free(bmap
->ctx
, bmap
->block
);
1081 isl_vec_free(bmap
->sample
);
1082 isl_space_free(bmap
->dim
);
1088 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1090 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1093 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1095 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1098 __isl_give isl_map
*isl_map_align_params_map_map_and(
1099 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1100 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1101 __isl_take isl_map
*map2
))
1105 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1106 return fn(map1
, map2
);
1107 if (!isl_space_has_named_params(map1
->dim
) ||
1108 !isl_space_has_named_params(map2
->dim
))
1109 isl_die(map1
->ctx
, isl_error_invalid
,
1110 "unaligned unnamed parameters", goto error
);
1111 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1112 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1113 return fn(map1
, map2
);
1120 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1121 __isl_keep isl_map
*map2
,
1122 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1128 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1129 return fn(map1
, map2
);
1130 if (!isl_space_has_named_params(map1
->dim
) ||
1131 !isl_space_has_named_params(map2
->dim
))
1132 isl_die(map1
->ctx
, isl_error_invalid
,
1133 "unaligned unnamed parameters", return -1);
1134 map1
= isl_map_copy(map1
);
1135 map2
= isl_map_copy(map2
);
1136 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1137 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1144 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1146 struct isl_ctx
*ctx
;
1150 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1151 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1153 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1154 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1155 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1156 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1157 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1158 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1160 int j
= isl_basic_map_alloc_inequality(bmap
);
1164 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1165 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1172 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1173 bmap
->extra
- bmap
->n_div
);
1174 return bmap
->n_eq
++;
1177 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1179 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1182 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1186 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1191 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1193 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1196 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1201 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1203 if (pos
!= bmap
->n_eq
- 1) {
1205 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1206 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1212 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1214 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1217 /* Turn inequality "pos" of "bmap" into an equality.
1219 * In particular, we move the inequality in front of the equalities
1220 * and move the last inequality in the position of the moved inequality.
1221 * Note that isl_tab_make_equalities_explicit depends on this particular
1222 * change in the ordering of the constraints.
1224 void isl_basic_map_inequality_to_equality(
1225 struct isl_basic_map
*bmap
, unsigned pos
)
1229 t
= bmap
->ineq
[pos
];
1230 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1231 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1236 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1237 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1238 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1239 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1242 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1244 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1247 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1249 struct isl_ctx
*ctx
;
1253 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1254 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1256 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1257 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1258 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1259 1 + isl_basic_map_total_dim(bmap
),
1260 bmap
->extra
- bmap
->n_div
);
1261 return bmap
->n_ineq
++;
1264 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1266 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1269 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1273 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1278 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1280 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1283 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1288 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1290 if (pos
!= bmap
->n_ineq
- 1) {
1291 t
= bmap
->ineq
[pos
];
1292 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1293 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1294 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1300 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1302 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1305 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1310 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1313 k
= isl_basic_map_alloc_equality(bmap
);
1316 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1319 isl_basic_map_free(bmap
);
1323 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1326 return (isl_basic_set
*)
1327 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1330 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1335 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1338 k
= isl_basic_map_alloc_inequality(bmap
);
1341 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1344 isl_basic_map_free(bmap
);
1348 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1351 return (isl_basic_set
*)
1352 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1355 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1359 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1360 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1361 1 + 1 + isl_basic_map_total_dim(bmap
),
1362 bmap
->extra
- bmap
->n_div
);
1363 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1364 return bmap
->n_div
++;
1367 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1369 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1372 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1376 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1381 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1383 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1386 /* Copy constraint from src to dst, putting the vars of src at offset
1387 * dim_off in dst and the divs of src at offset div_off in dst.
1388 * If both sets are actually map, then dim_off applies to the input
1391 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1392 struct isl_basic_map
*src_map
, isl_int
*src
,
1393 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1395 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1396 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1397 unsigned src_in
= isl_basic_map_n_in(src_map
);
1398 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1399 unsigned src_out
= isl_basic_map_n_out(src_map
);
1400 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1401 isl_int_set(dst
[0], src
[0]);
1402 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1403 if (dst_nparam
> src_nparam
)
1404 isl_seq_clr(dst
+1+src_nparam
,
1405 dst_nparam
- src_nparam
);
1406 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1407 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1409 isl_min(dst_in
-in_off
, src_in
));
1410 if (dst_in
-in_off
> src_in
)
1411 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1412 dst_in
- in_off
- src_in
);
1413 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1414 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1415 src
+1+src_nparam
+src_in
,
1416 isl_min(dst_out
-out_off
, src_out
));
1417 if (dst_out
-out_off
> src_out
)
1418 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1419 dst_out
- out_off
- src_out
);
1420 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1421 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1422 src
+1+src_nparam
+src_in
+src_out
,
1423 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1424 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1425 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1426 div_off
+src_map
->n_div
,
1427 dst_map
->n_div
- div_off
- src_map
->n_div
);
1430 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1431 struct isl_basic_map
*src_map
, isl_int
*src
,
1432 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1434 isl_int_set(dst
[0], src
[0]);
1435 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1438 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1439 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1444 if (!bmap1
|| !bmap2
)
1447 div_off
= bmap1
->n_div
;
1449 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1450 int i1
= isl_basic_map_alloc_equality(bmap1
);
1453 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1454 i_pos
, o_pos
, div_off
);
1457 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1458 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1461 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1462 i_pos
, o_pos
, div_off
);
1465 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1466 int i1
= isl_basic_map_alloc_div(bmap1
);
1469 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1470 i_pos
, o_pos
, div_off
);
1473 isl_basic_map_free(bmap2
);
1478 isl_basic_map_free(bmap1
);
1479 isl_basic_map_free(bmap2
);
1483 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1484 struct isl_basic_set
*bset2
, unsigned pos
)
1486 return (struct isl_basic_set
*)
1487 add_constraints((struct isl_basic_map
*)bset1
,
1488 (struct isl_basic_map
*)bset2
, 0, pos
);
1491 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1492 __isl_take isl_space
*dim
, unsigned extra
,
1493 unsigned n_eq
, unsigned n_ineq
)
1495 struct isl_basic_map
*ext
;
1505 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1506 base
->extra
>= base
->n_div
+ extra
;
1508 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1509 room_for_ineq(base
, n_ineq
)) {
1510 isl_space_free(dim
);
1514 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1515 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1516 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1517 extra
+= base
->extra
;
1519 n_ineq
+= base
->n_ineq
;
1521 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1527 ext
->sample
= isl_vec_copy(base
->sample
);
1528 flags
= base
->flags
;
1529 ext
= add_constraints(ext
, base
, 0, 0);
1532 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1538 isl_space_free(dim
);
1539 isl_basic_map_free(base
);
1543 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1544 __isl_take isl_space
*dim
, unsigned extra
,
1545 unsigned n_eq
, unsigned n_ineq
)
1547 return (struct isl_basic_set
*)
1548 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1549 extra
, n_eq
, n_ineq
);
1552 struct isl_basic_map
*isl_basic_map_extend_constraints(
1553 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1557 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1561 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1562 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1563 unsigned n_eq
, unsigned n_ineq
)
1565 struct isl_basic_map
*bmap
;
1570 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1574 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1577 isl_basic_map_free(base
);
1581 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1582 unsigned nparam
, unsigned dim
, unsigned extra
,
1583 unsigned n_eq
, unsigned n_ineq
)
1585 return (struct isl_basic_set
*)
1586 isl_basic_map_extend((struct isl_basic_map
*)base
,
1587 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1590 struct isl_basic_set
*isl_basic_set_extend_constraints(
1591 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1593 return (struct isl_basic_set
*)
1594 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1598 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1600 return (struct isl_basic_set
*)
1601 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1604 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1609 if (bmap
->ref
> 1) {
1611 bmap
= isl_basic_map_dup(bmap
);
1614 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1618 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1626 return isl_set_dup(set
);
1629 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1637 return isl_map_dup(map
);
1640 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1641 unsigned a_len
, unsigned b_len
)
1643 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1644 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1645 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1648 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1649 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1657 isl_assert(bmap
->ctx
,
1658 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1660 if (n1
== 0 || n2
== 0)
1663 bmap
= isl_basic_map_cow(bmap
);
1667 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1668 if (isl_blk_is_error(blk
))
1671 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1673 bmap
->eq
[i
] + pos
, n1
, n2
);
1675 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1677 bmap
->ineq
[i
] + pos
, n1
, n2
);
1679 for (i
= 0; i
< bmap
->n_div
; ++i
)
1681 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1683 isl_blk_free(bmap
->ctx
, blk
);
1685 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1686 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1687 return isl_basic_map_finalize(bmap
);
1689 isl_basic_map_free(bmap
);
1693 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1694 __isl_take isl_basic_set
*bset
, unsigned n
)
1702 nparam
= isl_basic_set_n_param(bset
);
1703 dim
= isl_basic_set_n_dim(bset
);
1704 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1706 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1708 isl_basic_set_free(bset
);
1712 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1718 total
= isl_basic_map_total_dim(bmap
);
1719 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1720 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1722 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1724 i
= isl_basic_map_alloc_equality(bmap
);
1728 isl_int_set_si(bmap
->eq
[i
][0], 1);
1729 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1730 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1731 isl_vec_free(bmap
->sample
);
1732 bmap
->sample
= NULL
;
1733 return isl_basic_map_finalize(bmap
);
1735 isl_basic_map_free(bmap
);
1739 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1741 return (struct isl_basic_set
*)
1742 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1745 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1748 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1750 isl_int
*t
= bmap
->div
[a
];
1751 bmap
->div
[a
] = bmap
->div
[b
];
1755 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1756 * div definitions accordingly.
1758 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1761 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1763 swap_div(bmap
, a
, b
);
1765 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1766 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1768 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1769 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1771 for (i
= 0; i
< bmap
->n_div
; ++i
)
1772 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1773 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1776 /* Eliminate the specified n dimensions starting at first from the
1777 * constraints, without removing the dimensions from the space.
1778 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1780 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1781 enum isl_dim_type type
, unsigned first
, unsigned n
)
1790 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1791 isl_die(map
->ctx
, isl_error_invalid
,
1792 "index out of bounds", goto error
);
1794 map
= isl_map_cow(map
);
1798 for (i
= 0; i
< map
->n
; ++i
) {
1799 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1809 /* Eliminate the specified n dimensions starting at first from the
1810 * constraints, without removing the dimensions from the space.
1811 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1813 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1814 enum isl_dim_type type
, unsigned first
, unsigned n
)
1816 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1819 /* Eliminate the specified n dimensions starting at first from the
1820 * constraints, without removing the dimensions from the space.
1821 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1823 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1824 unsigned first
, unsigned n
)
1826 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1829 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1830 __isl_take isl_basic_map
*bmap
)
1834 bmap
= isl_basic_map_eliminate_vars(bmap
,
1835 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1839 return isl_basic_map_finalize(bmap
);
1842 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1843 __isl_take isl_basic_set
*bset
)
1845 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1846 (struct isl_basic_map
*)bset
);
1849 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1858 map
= isl_map_cow(map
);
1862 for (i
= 0; i
< map
->n
; ++i
) {
1863 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1873 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1875 return isl_map_remove_divs(set
);
1878 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1879 enum isl_dim_type type
, unsigned first
, unsigned n
)
1883 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1885 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1887 bmap
= isl_basic_map_eliminate_vars(bmap
,
1888 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1891 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1893 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1896 isl_basic_map_free(bmap
);
1900 /* Return true if the definition of the given div (recursively) involves
1901 * any of the given variables.
1903 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1904 unsigned first
, unsigned n
)
1907 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1909 if (isl_int_is_zero(bmap
->div
[div
][0]))
1911 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1914 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1915 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1917 if (div_involves_vars(bmap
, i
, first
, n
))
1924 /* Try and add a lower and/or upper bound on "div" to "bmap"
1925 * based on inequality "i".
1926 * "total" is the total number of variables (excluding the divs).
1927 * "v" is a temporary object that can be used during the calculations.
1928 * If "lb" is set, then a lower bound should be constructed.
1929 * If "ub" is set, then an upper bound should be constructed.
1931 * The calling function has already checked that the inequality does not
1932 * reference "div", but we still need to check that the inequality is
1933 * of the right form. We'll consider the case where we want to construct
1934 * a lower bound. The construction of upper bounds is similar.
1936 * Let "div" be of the form
1938 * q = floor((a + f(x))/d)
1940 * We essentially check if constraint "i" is of the form
1944 * so that we can use it to derive a lower bound on "div".
1945 * However, we allow a slightly more general form
1949 * with the condition that the coefficients of g(x) - f(x) are all
1951 * Rewriting this constraint as
1955 * adding a + f(x) to both sides and dividing by d, we obtain
1957 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1959 * Taking the floor on both sides, we obtain
1961 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1965 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1967 * In the case of an upper bound, we construct the constraint
1969 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1972 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1973 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1974 unsigned total
, isl_int v
, int lb
, int ub
)
1978 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1980 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1981 bmap
->div
[div
][1 + 1 + j
]);
1982 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1985 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1986 bmap
->div
[div
][1 + 1 + j
]);
1987 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1993 bmap
= isl_basic_map_cow(bmap
);
1994 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1996 int k
= isl_basic_map_alloc_inequality(bmap
);
1999 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2000 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2001 bmap
->div
[div
][1 + j
]);
2002 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2003 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2005 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2008 int k
= isl_basic_map_alloc_inequality(bmap
);
2011 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2012 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2013 bmap
->div
[div
][1 + j
]);
2014 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2015 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2017 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2020 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2023 isl_basic_map_free(bmap
);
2027 /* This function is called right before "div" is eliminated from "bmap"
2028 * using Fourier-Motzkin.
2029 * Look through the constraints of "bmap" for constraints on the argument
2030 * of the integer division and use them to construct constraints on the
2031 * integer division itself. These constraints can then be combined
2032 * during the Fourier-Motzkin elimination.
2033 * Note that it is only useful to introduce lower bounds on "div"
2034 * if "bmap" already contains upper bounds on "div" as the newly
2035 * introduce lower bounds can then be combined with the pre-existing
2036 * upper bounds. Similarly for upper bounds.
2037 * We therefore first check if "bmap" contains any lower and/or upper bounds
2040 * It is interesting to note that the introduction of these constraints
2041 * can indeed lead to more accurate results, even when compared to
2042 * deriving constraints on the argument of "div" from constraints on "div".
2043 * Consider, for example, the set
2045 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2047 * The second constraint can be rewritten as
2049 * 2 * [(-i-2j+3)/4] + k >= 0
2051 * from which we can derive
2053 * -i - 2j + 3 >= -2k
2059 * Combined with the first constraint, we obtain
2061 * -3 <= 3 + 2k or k >= -3
2063 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2064 * the first constraint, we obtain
2066 * [(i + 2j)/4] >= [-3/4] = -1
2068 * Combining this constraint with the second constraint, we obtain
2072 static __isl_give isl_basic_map
*insert_bounds_on_div(
2073 __isl_take isl_basic_map
*bmap
, int div
)
2076 int check_lb
, check_ub
;
2083 if (isl_int_is_zero(bmap
->div
[div
][0]))
2086 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2090 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2091 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2098 if (!check_lb
&& !check_ub
)
2103 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2104 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2107 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2108 check_lb
, check_ub
);
2116 /* Remove all divs (recursively) involving any of the given dimensions
2117 * in their definitions.
2119 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2120 __isl_take isl_basic_map
*bmap
,
2121 enum isl_dim_type type
, unsigned first
, unsigned n
)
2127 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2129 first
+= isl_basic_map_offset(bmap
, type
);
2131 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2132 if (!div_involves_vars(bmap
, i
, first
, n
))
2134 bmap
= insert_bounds_on_div(bmap
, i
);
2135 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2143 isl_basic_map_free(bmap
);
2147 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2148 __isl_take isl_basic_set
*bset
,
2149 enum isl_dim_type type
, unsigned first
, unsigned n
)
2151 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2154 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2155 enum isl_dim_type type
, unsigned first
, unsigned n
)
2164 map
= isl_map_cow(map
);
2168 for (i
= 0; i
< map
->n
; ++i
) {
2169 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2180 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2181 enum isl_dim_type type
, unsigned first
, unsigned n
)
2183 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2187 /* Does the desciption of "bmap" depend on the specified dimensions?
2188 * We also check whether the dimensions appear in any of the div definitions.
2189 * In principle there is no need for this check. If the dimensions appear
2190 * in a div definition, they also appear in the defining constraints of that
2193 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2194 enum isl_dim_type type
, unsigned first
, unsigned n
)
2201 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2202 isl_die(bmap
->ctx
, isl_error_invalid
,
2203 "index out of bounds", return -1);
2205 first
+= isl_basic_map_offset(bmap
, type
);
2206 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2207 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2209 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2210 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2212 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2213 if (isl_int_is_zero(bmap
->div
[i
][0]))
2215 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2222 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2223 enum isl_dim_type type
, unsigned first
, unsigned n
)
2230 if (first
+ n
> isl_map_dim(map
, type
))
2231 isl_die(map
->ctx
, isl_error_invalid
,
2232 "index out of bounds", return -1);
2234 for (i
= 0; i
< map
->n
; ++i
) {
2235 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2237 if (involves
< 0 || involves
)
2244 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2245 enum isl_dim_type type
, unsigned first
, unsigned n
)
2247 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2250 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2251 enum isl_dim_type type
, unsigned first
, unsigned n
)
2253 return isl_map_involves_dims(set
, type
, first
, n
);
2256 /* Return true if the definition of the given div is unknown or depends
2259 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2262 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2264 if (isl_int_is_zero(bmap
->div
[div
][0]))
2267 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2268 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2270 if (div_is_unknown(bmap
, i
))
2277 /* Remove all divs that are unknown or defined in terms of unknown divs.
2279 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2280 __isl_take isl_basic_map
*bmap
)
2287 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2288 if (!div_is_unknown(bmap
, i
))
2290 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2299 /* Remove all divs that are unknown or defined in terms of unknown divs.
2301 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2302 __isl_take isl_basic_set
*bset
)
2304 return isl_basic_map_remove_unknown_divs(bset
);
2307 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2316 map
= isl_map_cow(map
);
2320 for (i
= 0; i
< map
->n
; ++i
) {
2321 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2331 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2333 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2336 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2337 __isl_take isl_basic_set
*bset
,
2338 enum isl_dim_type type
, unsigned first
, unsigned n
)
2340 return (isl_basic_set
*)
2341 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2344 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2345 enum isl_dim_type type
, unsigned first
, unsigned n
)
2352 map
= isl_map_cow(map
);
2355 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2357 for (i
= 0; i
< map
->n
; ++i
) {
2358 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2359 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2363 map
= isl_map_drop(map
, type
, first
, n
);
2370 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2371 enum isl_dim_type type
, unsigned first
, unsigned n
)
2373 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2376 /* Project out n inputs starting at first using Fourier-Motzkin */
2377 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2378 unsigned first
, unsigned n
)
2380 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2383 static void dump_term(struct isl_basic_map
*bmap
,
2384 isl_int c
, int pos
, FILE *out
)
2387 unsigned in
= isl_basic_map_n_in(bmap
);
2388 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2389 unsigned nparam
= isl_basic_map_n_param(bmap
);
2391 isl_int_print(out
, c
, 0);
2393 if (!isl_int_is_one(c
))
2394 isl_int_print(out
, c
, 0);
2395 if (pos
< 1 + nparam
) {
2396 name
= isl_space_get_dim_name(bmap
->dim
,
2397 isl_dim_param
, pos
- 1);
2399 fprintf(out
, "%s", name
);
2401 fprintf(out
, "p%d", pos
- 1);
2402 } else if (pos
< 1 + nparam
+ in
)
2403 fprintf(out
, "i%d", pos
- 1 - nparam
);
2404 else if (pos
< 1 + nparam
+ dim
)
2405 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2407 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2411 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2412 int sign
, FILE *out
)
2416 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2420 for (i
= 0, first
= 1; i
< len
; ++i
) {
2421 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2424 fprintf(out
, " + ");
2426 isl_int_abs(v
, c
[i
]);
2427 dump_term(bmap
, v
, i
, out
);
2434 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2435 const char *op
, FILE *out
, int indent
)
2439 fprintf(out
, "%*s", indent
, "");
2441 dump_constraint_sign(bmap
, c
, 1, out
);
2442 fprintf(out
, " %s ", op
);
2443 dump_constraint_sign(bmap
, c
, -1, out
);
2447 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2448 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2450 fprintf(out
, "%*s", indent
, "");
2451 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2456 static void dump_constraints(struct isl_basic_map
*bmap
,
2457 isl_int
**c
, unsigned n
,
2458 const char *op
, FILE *out
, int indent
)
2462 for (i
= 0; i
< n
; ++i
)
2463 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2466 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2470 unsigned total
= isl_basic_map_total_dim(bmap
);
2472 for (j
= 0; j
< 1 + total
; ++j
) {
2473 if (isl_int_is_zero(exp
[j
]))
2475 if (!first
&& isl_int_is_pos(exp
[j
]))
2477 dump_term(bmap
, exp
[j
], j
, out
);
2482 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2486 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2487 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2489 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2490 fprintf(out
, "%*s", indent
, "");
2491 fprintf(out
, "e%d = [(", i
);
2492 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2494 isl_int_print(out
, bmap
->div
[i
][0], 0);
2495 fprintf(out
, "]\n");
2499 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2500 FILE *out
, int indent
)
2503 fprintf(out
, "null basic set\n");
2507 fprintf(out
, "%*s", indent
, "");
2508 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2509 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2510 bset
->extra
, bset
->flags
);
2511 dump((struct isl_basic_map
*)bset
, out
, indent
);
2514 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2515 FILE *out
, int indent
)
2518 fprintf(out
, "null basic map\n");
2522 fprintf(out
, "%*s", indent
, "");
2523 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2524 "flags: %x, n_name: %d\n",
2526 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2527 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2528 dump(bmap
, out
, indent
);
2531 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2536 total
= isl_basic_map_total_dim(bmap
);
2537 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2538 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2539 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2540 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2544 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2547 struct isl_set
*set
;
2551 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2552 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2553 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2554 sizeof(struct isl_set
) +
2555 (n
- 1) * sizeof(struct isl_basic_set
*));
2559 set
->ctx
= dim
->ctx
;
2560 isl_ctx_ref(set
->ctx
);
2568 isl_space_free(dim
);
2572 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2573 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2575 struct isl_set
*set
;
2578 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2582 set
= isl_set_alloc_space(dims
, n
, flags
);
2586 /* Make sure "map" has room for at least "n" more basic maps.
2588 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2591 struct isl_map
*grown
= NULL
;
2595 isl_assert(map
->ctx
, n
>= 0, goto error
);
2596 if (map
->n
+ n
<= map
->size
)
2598 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2601 for (i
= 0; i
< map
->n
; ++i
) {
2602 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2610 isl_map_free(grown
);
2615 /* Make sure "set" has room for at least "n" more basic sets.
2617 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2619 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2622 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2625 struct isl_set
*dup
;
2630 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2633 for (i
= 0; i
< set
->n
; ++i
)
2634 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2638 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2640 return isl_map_from_basic_map(bset
);
2643 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2645 struct isl_map
*map
;
2650 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2651 return isl_map_add_basic_map(map
, bmap
);
2654 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2655 __isl_take isl_basic_set
*bset
)
2657 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2658 (struct isl_basic_map
*)bset
);
2661 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2671 isl_ctx_deref(set
->ctx
);
2672 for (i
= 0; i
< set
->n
; ++i
)
2673 isl_basic_set_free(set
->p
[i
]);
2674 isl_space_free(set
->dim
);
2680 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2685 fprintf(out
, "null set\n");
2689 fprintf(out
, "%*s", indent
, "");
2690 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2691 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2693 for (i
= 0; i
< set
->n
; ++i
) {
2694 fprintf(out
, "%*s", indent
, "");
2695 fprintf(out
, "basic set %d:\n", i
);
2696 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2700 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2705 fprintf(out
, "null map\n");
2709 fprintf(out
, "%*s", indent
, "");
2710 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2711 "flags: %x, n_name: %d\n",
2712 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2713 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2714 for (i
= 0; i
< map
->n
; ++i
) {
2715 fprintf(out
, "%*s", indent
, "");
2716 fprintf(out
, "basic map %d:\n", i
);
2717 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2721 struct isl_basic_map
*isl_basic_map_intersect_domain(
2722 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2724 struct isl_basic_map
*bmap_domain
;
2729 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2730 bset
->dim
, isl_dim_param
), goto error
);
2732 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2733 isl_assert(bset
->ctx
,
2734 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2736 bmap
= isl_basic_map_cow(bmap
);
2739 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2740 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2741 bmap_domain
= isl_basic_map_from_domain(bset
);
2742 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2744 bmap
= isl_basic_map_simplify(bmap
);
2745 return isl_basic_map_finalize(bmap
);
2747 isl_basic_map_free(bmap
);
2748 isl_basic_set_free(bset
);
2752 struct isl_basic_map
*isl_basic_map_intersect_range(
2753 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2755 struct isl_basic_map
*bmap_range
;
2760 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2761 bset
->dim
, isl_dim_param
), goto error
);
2763 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2764 isl_assert(bset
->ctx
,
2765 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2767 if (isl_basic_set_is_universe(bset
)) {
2768 isl_basic_set_free(bset
);
2772 bmap
= isl_basic_map_cow(bmap
);
2775 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2776 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2777 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2778 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2780 bmap
= isl_basic_map_simplify(bmap
);
2781 return isl_basic_map_finalize(bmap
);
2783 isl_basic_map_free(bmap
);
2784 isl_basic_set_free(bset
);
2788 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2797 total
= 1 + isl_basic_map_total_dim(bmap
);
2798 if (total
!= vec
->size
)
2803 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2804 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2805 if (!isl_int_is_zero(s
)) {
2811 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2812 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2813 if (isl_int_is_neg(s
)) {
2824 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2826 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2829 struct isl_basic_map
*isl_basic_map_intersect(
2830 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2832 struct isl_vec
*sample
= NULL
;
2834 if (!bmap1
|| !bmap2
)
2837 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2838 bmap2
->dim
, isl_dim_param
), goto error
);
2839 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2840 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2841 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2842 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2843 return isl_basic_map_intersect(bmap2
, bmap1
);
2845 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2846 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2847 isl_assert(bmap1
->ctx
,
2848 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2850 if (bmap1
->sample
&&
2851 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2852 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2853 sample
= isl_vec_copy(bmap1
->sample
);
2854 else if (bmap2
->sample
&&
2855 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2856 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2857 sample
= isl_vec_copy(bmap2
->sample
);
2859 bmap1
= isl_basic_map_cow(bmap1
);
2862 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2863 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2864 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2867 isl_vec_free(sample
);
2869 isl_vec_free(bmap1
->sample
);
2870 bmap1
->sample
= sample
;
2873 bmap1
= isl_basic_map_simplify(bmap1
);
2874 return isl_basic_map_finalize(bmap1
);
2877 isl_vec_free(sample
);
2878 isl_basic_map_free(bmap1
);
2879 isl_basic_map_free(bmap2
);
2883 struct isl_basic_set
*isl_basic_set_intersect(
2884 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2886 return (struct isl_basic_set
*)
2887 isl_basic_map_intersect(
2888 (struct isl_basic_map
*)bset1
,
2889 (struct isl_basic_map
*)bset2
);
2892 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2893 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2895 return isl_basic_set_intersect(bset1
, bset2
);
2898 /* Special case of isl_map_intersect, where both map1 and map2
2899 * are convex, without any divs and such that either map1 or map2
2900 * contains a single constraint. This constraint is then simply
2901 * added to the other map.
2903 static __isl_give isl_map
*map_intersect_add_constraint(
2904 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2906 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2907 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2908 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2909 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2911 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2912 return isl_map_intersect(map2
, map1
);
2914 isl_assert(map2
->ctx
,
2915 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2917 map1
= isl_map_cow(map1
);
2920 if (isl_map_plain_is_empty(map1
)) {
2924 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2925 if (map2
->p
[0]->n_eq
== 1)
2926 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2928 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2929 map2
->p
[0]->ineq
[0]);
2931 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2932 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2936 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2937 isl_basic_map_free(map1
->p
[0]);
2950 /* map2 may be either a parameter domain or a map living in the same
2953 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2954 __isl_take isl_map
*map2
)
2963 if ((isl_map_plain_is_empty(map1
) ||
2964 isl_map_plain_is_universe(map2
)) &&
2965 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2969 if ((isl_map_plain_is_empty(map2
) ||
2970 isl_map_plain_is_universe(map1
)) &&
2971 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2976 if (map1
->n
== 1 && map2
->n
== 1 &&
2977 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2978 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2979 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2980 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2981 return map_intersect_add_constraint(map1
, map2
);
2983 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2984 isl_space_dim(map2
->dim
, isl_dim_param
))
2985 isl_assert(map1
->ctx
,
2986 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2988 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2989 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2990 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2992 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2993 map1
->n
* map2
->n
, flags
);
2996 for (i
= 0; i
< map1
->n
; ++i
)
2997 for (j
= 0; j
< map2
->n
; ++j
) {
2998 struct isl_basic_map
*part
;
2999 part
= isl_basic_map_intersect(
3000 isl_basic_map_copy(map1
->p
[i
]),
3001 isl_basic_map_copy(map2
->p
[j
]));
3002 if (isl_basic_map_is_empty(part
) < 0)
3003 part
= isl_basic_map_free(part
);
3004 result
= isl_map_add_basic_map(result
, part
);
3017 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3018 __isl_take isl_map
*map2
)
3022 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3023 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3024 "spaces don't match", goto error
);
3025 return map_intersect_internal(map1
, map2
);
3032 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3033 __isl_take isl_map
*map2
)
3035 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3038 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3040 return (struct isl_set
*)
3041 isl_map_intersect((struct isl_map
*)set1
,
3042 (struct isl_map
*)set2
);
3045 /* map_intersect_internal accepts intersections
3046 * with parameter domains, so we can just call that function.
3048 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3049 __isl_take isl_set
*params
)
3051 return map_intersect_internal(map
, params
);
3054 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3055 __isl_take isl_map
*map2
)
3057 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3060 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3061 __isl_take isl_set
*params
)
3063 return isl_map_intersect_params(set
, params
);
3066 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3069 struct isl_basic_set
*bset
;
3074 bmap
= isl_basic_map_cow(bmap
);
3077 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3078 in
= isl_basic_map_n_in(bmap
);
3079 bset
= isl_basic_set_from_basic_map(bmap
);
3080 bset
= isl_basic_set_swap_vars(bset
, in
);
3081 return isl_basic_map_from_basic_set(bset
, dim
);
3084 static __isl_give isl_basic_map
*basic_map_space_reset(
3085 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3091 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3094 space
= isl_basic_map_get_space(bmap
);
3095 space
= isl_space_reset(space
, type
);
3096 bmap
= isl_basic_map_reset_space(bmap
, space
);
3100 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3101 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3102 unsigned pos
, unsigned n
)
3105 struct isl_basic_map
*res
;
3106 struct isl_dim_map
*dim_map
;
3107 unsigned total
, off
;
3108 enum isl_dim_type t
;
3111 return basic_map_space_reset(bmap
, type
);
3116 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3118 total
= isl_basic_map_total_dim(bmap
) + n
;
3119 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3121 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3123 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3125 unsigned size
= isl_basic_map_dim(bmap
, t
);
3126 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3128 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3129 pos
, size
- pos
, off
+ pos
+ n
);
3131 off
+= isl_space_dim(res_dim
, t
);
3133 isl_dim_map_div(dim_map
, bmap
, off
);
3135 res
= isl_basic_map_alloc_space(res_dim
,
3136 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3137 if (isl_basic_map_is_rational(bmap
))
3138 res
= isl_basic_map_set_rational(res
);
3139 if (isl_basic_map_plain_is_empty(bmap
)) {
3140 isl_basic_map_free(bmap
);
3142 return isl_basic_map_set_to_empty(res
);
3144 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3145 return isl_basic_map_finalize(res
);
3148 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3149 __isl_take isl_basic_set
*bset
,
3150 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3152 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3155 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3156 enum isl_dim_type type
, unsigned n
)
3160 return isl_basic_map_insert_dims(bmap
, type
,
3161 isl_basic_map_dim(bmap
, type
), n
);
3164 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3165 enum isl_dim_type type
, unsigned n
)
3169 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3170 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3172 isl_basic_set_free(bset
);
3176 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3177 enum isl_dim_type type
)
3181 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3184 space
= isl_map_get_space(map
);
3185 space
= isl_space_reset(space
, type
);
3186 map
= isl_map_reset_space(map
, space
);
3190 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3191 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3196 return map_space_reset(map
, type
);
3198 map
= isl_map_cow(map
);
3202 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3206 for (i
= 0; i
< map
->n
; ++i
) {
3207 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3218 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3219 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3221 return isl_map_insert_dims(set
, type
, pos
, n
);
3224 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3225 enum isl_dim_type type
, unsigned n
)
3229 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3232 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3233 enum isl_dim_type type
, unsigned n
)
3237 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3238 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3244 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3245 __isl_take isl_basic_map
*bmap
,
3246 enum isl_dim_type dst_type
, unsigned dst_pos
,
3247 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3249 struct isl_dim_map
*dim_map
;
3250 struct isl_basic_map
*res
;
3251 enum isl_dim_type t
;
3252 unsigned total
, off
;
3259 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3262 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3265 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3267 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3268 pos(bmap
->dim
, src_type
) + src_pos
+
3269 ((src_type
< dst_type
) ? n
: 0)) {
3270 bmap
= isl_basic_map_cow(bmap
);
3274 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3275 src_type
, src_pos
, n
);
3279 bmap
= isl_basic_map_finalize(bmap
);
3284 total
= isl_basic_map_total_dim(bmap
);
3285 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3288 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3289 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3290 if (t
== dst_type
) {
3291 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3294 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3297 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3298 dst_pos
, size
- dst_pos
, off
);
3299 off
+= size
- dst_pos
;
3300 } else if (t
== src_type
) {
3301 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3304 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3305 src_pos
+ n
, size
- src_pos
- n
, off
);
3306 off
+= size
- src_pos
- n
;
3308 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3312 isl_dim_map_div(dim_map
, bmap
, off
);
3314 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3315 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3316 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3320 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3321 src_type
, src_pos
, n
);
3325 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3326 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3327 bmap
= isl_basic_map_finalize(bmap
);
3331 isl_basic_map_free(bmap
);
3335 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3336 enum isl_dim_type dst_type
, unsigned dst_pos
,
3337 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3339 return (isl_basic_set
*)isl_basic_map_move_dims(
3340 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3343 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3344 enum isl_dim_type dst_type
, unsigned dst_pos
,
3345 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3349 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3350 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3351 src_type
, src_pos
, n
);
3357 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3358 enum isl_dim_type dst_type
, unsigned dst_pos
,
3359 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3368 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3371 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3374 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3376 map
= isl_map_cow(map
);
3380 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3384 for (i
= 0; i
< map
->n
; ++i
) {
3385 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3387 src_type
, src_pos
, n
);
3398 /* Move the specified dimensions to the last columns right before
3399 * the divs. Don't change the dimension specification of bmap.
3400 * That's the responsibility of the caller.
3402 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3403 enum isl_dim_type type
, unsigned first
, unsigned n
)
3405 struct isl_dim_map
*dim_map
;
3406 struct isl_basic_map
*res
;
3407 enum isl_dim_type t
;
3408 unsigned total
, off
;
3412 if (pos(bmap
->dim
, type
) + first
+ n
==
3413 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3416 total
= isl_basic_map_total_dim(bmap
);
3417 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3420 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3421 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3423 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3426 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3427 first
, n
, total
- bmap
->n_div
- n
);
3428 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3429 first
+ n
, size
- (first
+ n
), off
);
3430 off
+= size
- (first
+ n
);
3432 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3436 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3438 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3439 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3440 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3444 /* Insert "n" rows in the divs of "bmap".
3446 * The number of columns is not changed, which means that the last
3447 * dimensions of "bmap" are being reintepreted as the new divs.
3448 * The space of "bmap" is not adjusted, however, which means
3449 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3450 * from the space of "bmap" is the responsibility of the caller.
3452 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3460 bmap
= isl_basic_map_cow(bmap
);
3464 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3465 old
= bmap
->block2
.data
;
3466 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3467 (bmap
->extra
+ n
) * (1 + row_size
));
3468 if (!bmap
->block2
.data
)
3469 return isl_basic_map_free(bmap
);
3470 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3472 return isl_basic_map_free(bmap
);
3473 for (i
= 0; i
< n
; ++i
) {
3474 new_div
[i
] = bmap
->block2
.data
+
3475 (bmap
->extra
+ i
) * (1 + row_size
);
3476 isl_seq_clr(new_div
[i
], 1 + row_size
);
3478 for (i
= 0; i
< bmap
->extra
; ++i
)
3479 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3481 bmap
->div
= new_div
;
3488 /* Turn the n dimensions of type type, starting at first
3489 * into existentially quantified variables.
3491 __isl_give isl_basic_map
*isl_basic_map_project_out(
3492 __isl_take isl_basic_map
*bmap
,
3493 enum isl_dim_type type
, unsigned first
, unsigned n
)
3496 return basic_map_space_reset(bmap
, type
);
3501 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3502 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3504 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3507 bmap
= move_last(bmap
, type
, first
, n
);
3508 bmap
= isl_basic_map_cow(bmap
);
3509 bmap
= insert_div_rows(bmap
, n
);
3513 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3516 bmap
= isl_basic_map_simplify(bmap
);
3517 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3518 return isl_basic_map_finalize(bmap
);
3520 isl_basic_map_free(bmap
);
3524 /* Turn the n dimensions of type type, starting at first
3525 * into existentially quantified variables.
3527 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3528 enum isl_dim_type type
, unsigned first
, unsigned n
)
3530 return (isl_basic_set
*)isl_basic_map_project_out(
3531 (isl_basic_map
*)bset
, type
, first
, n
);
3534 /* Turn the n dimensions of type type, starting at first
3535 * into existentially quantified variables.
3537 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3538 enum isl_dim_type type
, unsigned first
, unsigned n
)
3546 return map_space_reset(map
, type
);
3548 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3550 map
= isl_map_cow(map
);
3554 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3558 for (i
= 0; i
< map
->n
; ++i
) {
3559 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3570 /* Turn the n dimensions of type type, starting at first
3571 * into existentially quantified variables.
3573 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3574 enum isl_dim_type type
, unsigned first
, unsigned n
)
3576 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3579 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3583 for (i
= 0; i
< n
; ++i
) {
3584 j
= isl_basic_map_alloc_div(bmap
);
3587 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3591 isl_basic_map_free(bmap
);
3595 struct isl_basic_map
*isl_basic_map_apply_range(
3596 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3598 isl_space
*dim_result
= NULL
;
3599 struct isl_basic_map
*bmap
;
3600 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3601 struct isl_dim_map
*dim_map1
, *dim_map2
;
3603 if (!bmap1
|| !bmap2
)
3605 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3606 bmap2
->dim
, isl_dim_param
))
3607 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3608 "parameters don't match", goto error
);
3609 if (!isl_space_tuple_match(bmap1
->dim
, isl_dim_out
,
3610 bmap2
->dim
, isl_dim_in
))
3611 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3612 "spaces don't match", goto error
);
3614 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3615 isl_space_copy(bmap2
->dim
));
3617 n_in
= isl_basic_map_n_in(bmap1
);
3618 n_out
= isl_basic_map_n_out(bmap2
);
3619 n
= isl_basic_map_n_out(bmap1
);
3620 nparam
= isl_basic_map_n_param(bmap1
);
3622 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3623 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3624 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3625 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3626 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3627 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3628 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3629 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3630 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3631 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3632 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3634 bmap
= isl_basic_map_alloc_space(dim_result
,
3635 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3636 bmap1
->n_eq
+ bmap2
->n_eq
,
3637 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3638 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3639 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3640 bmap
= add_divs(bmap
, n
);
3641 bmap
= isl_basic_map_simplify(bmap
);
3642 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3643 return isl_basic_map_finalize(bmap
);
3645 isl_basic_map_free(bmap1
);
3646 isl_basic_map_free(bmap2
);
3650 struct isl_basic_set
*isl_basic_set_apply(
3651 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3656 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3659 return (struct isl_basic_set
*)
3660 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3662 isl_basic_set_free(bset
);
3663 isl_basic_map_free(bmap
);
3667 struct isl_basic_map
*isl_basic_map_apply_domain(
3668 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3670 if (!bmap1
|| !bmap2
)
3673 isl_assert(bmap1
->ctx
,
3674 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3675 isl_assert(bmap1
->ctx
,
3676 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3679 bmap1
= isl_basic_map_reverse(bmap1
);
3680 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3681 return isl_basic_map_reverse(bmap1
);
3683 isl_basic_map_free(bmap1
);
3684 isl_basic_map_free(bmap2
);
3688 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3689 * A \cap B -> f(A) + f(B)
3691 struct isl_basic_map
*isl_basic_map_sum(
3692 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3694 unsigned n_in
, n_out
, nparam
, total
, pos
;
3695 struct isl_basic_map
*bmap
= NULL
;
3696 struct isl_dim_map
*dim_map1
, *dim_map2
;
3699 if (!bmap1
|| !bmap2
)
3702 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3705 nparam
= isl_basic_map_n_param(bmap1
);
3706 n_in
= isl_basic_map_n_in(bmap1
);
3707 n_out
= isl_basic_map_n_out(bmap1
);
3709 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3710 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3711 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3712 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3713 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3714 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3715 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3716 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3717 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3718 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3719 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3721 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3722 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3723 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3724 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3725 for (i
= 0; i
< n_out
; ++i
) {
3726 int j
= isl_basic_map_alloc_equality(bmap
);
3729 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3730 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3731 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3732 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3734 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3735 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3736 bmap
= add_divs(bmap
, 2 * n_out
);
3738 bmap
= isl_basic_map_simplify(bmap
);
3739 return isl_basic_map_finalize(bmap
);
3741 isl_basic_map_free(bmap
);
3742 isl_basic_map_free(bmap1
);
3743 isl_basic_map_free(bmap2
);
3747 /* Given two maps A -> f(A) and B -> g(B), construct a map
3748 * A \cap B -> f(A) + f(B)
3750 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3752 struct isl_map
*result
;
3758 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3760 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3761 map1
->n
* map2
->n
, 0);
3764 for (i
= 0; i
< map1
->n
; ++i
)
3765 for (j
= 0; j
< map2
->n
; ++j
) {
3766 struct isl_basic_map
*part
;
3767 part
= isl_basic_map_sum(
3768 isl_basic_map_copy(map1
->p
[i
]),
3769 isl_basic_map_copy(map2
->p
[j
]));
3770 if (isl_basic_map_is_empty(part
))
3771 isl_basic_map_free(part
);
3773 result
= isl_map_add_basic_map(result
, part
);
3786 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3787 __isl_take isl_set
*set2
)
3789 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3792 /* Given a basic map A -> f(A), construct A -> -f(A).
3794 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3799 bmap
= isl_basic_map_cow(bmap
);
3803 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3804 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3805 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3806 for (j
= 0; j
< n
; ++j
)
3807 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3808 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3809 for (j
= 0; j
< n
; ++j
)
3810 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3811 for (i
= 0; i
< bmap
->n_div
; ++i
)
3812 for (j
= 0; j
< n
; ++j
)
3813 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3814 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3815 return isl_basic_map_finalize(bmap
);
3818 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3820 return isl_basic_map_neg(bset
);
3823 /* Given a map A -> f(A), construct A -> -f(A).
3825 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3829 map
= isl_map_cow(map
);
3833 for (i
= 0; i
< map
->n
; ++i
) {
3834 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3845 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3847 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3850 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3851 * A -> floor(f(A)/d).
3853 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3856 unsigned n_in
, n_out
, nparam
, total
, pos
;
3857 struct isl_basic_map
*result
= NULL
;
3858 struct isl_dim_map
*dim_map
;
3864 nparam
= isl_basic_map_n_param(bmap
);
3865 n_in
= isl_basic_map_n_in(bmap
);
3866 n_out
= isl_basic_map_n_out(bmap
);
3868 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3869 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3870 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3871 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3872 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3873 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3875 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3876 bmap
->n_div
+ n_out
,
3877 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3878 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3879 result
= add_divs(result
, n_out
);
3880 for (i
= 0; i
< n_out
; ++i
) {
3882 j
= isl_basic_map_alloc_inequality(result
);
3885 isl_seq_clr(result
->ineq
[j
], 1+total
);
3886 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3887 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3888 j
= isl_basic_map_alloc_inequality(result
);
3891 isl_seq_clr(result
->ineq
[j
], 1+total
);
3892 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3893 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3894 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3897 result
= isl_basic_map_simplify(result
);
3898 return isl_basic_map_finalize(result
);
3900 isl_basic_map_free(result
);
3904 /* Given a map A -> f(A) and an integer d, construct a map
3905 * A -> floor(f(A)/d).
3907 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3911 map
= isl_map_cow(map
);
3915 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3916 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3917 for (i
= 0; i
< map
->n
; ++i
) {
3918 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3929 /* Given a map A -> f(A) and an integer d, construct a map
3930 * A -> floor(f(A)/d).
3932 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3933 __isl_take isl_val
*d
)
3937 if (!isl_val_is_int(d
))
3938 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3939 "expecting integer denominator", goto error
);
3940 map
= isl_map_floordiv(map
, d
->n
);
3949 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3955 i
= isl_basic_map_alloc_equality(bmap
);
3958 nparam
= isl_basic_map_n_param(bmap
);
3959 n_in
= isl_basic_map_n_in(bmap
);
3960 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3961 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3962 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3963 return isl_basic_map_finalize(bmap
);
3965 isl_basic_map_free(bmap
);
3969 /* Add a constraints to "bmap" expressing i_pos < o_pos
3971 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3977 i
= isl_basic_map_alloc_inequality(bmap
);
3980 nparam
= isl_basic_map_n_param(bmap
);
3981 n_in
= isl_basic_map_n_in(bmap
);
3982 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3983 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3984 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3985 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3986 return isl_basic_map_finalize(bmap
);
3988 isl_basic_map_free(bmap
);
3992 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3994 static __isl_give isl_basic_map
*var_less_or_equal(
3995 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4001 i
= isl_basic_map_alloc_inequality(bmap
);
4004 nparam
= isl_basic_map_n_param(bmap
);
4005 n_in
= isl_basic_map_n_in(bmap
);
4006 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4007 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4008 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4009 return isl_basic_map_finalize(bmap
);
4011 isl_basic_map_free(bmap
);
4015 /* Add a constraints to "bmap" expressing i_pos > o_pos
4017 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4023 i
= isl_basic_map_alloc_inequality(bmap
);
4026 nparam
= isl_basic_map_n_param(bmap
);
4027 n_in
= isl_basic_map_n_in(bmap
);
4028 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4029 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4030 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4031 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4032 return isl_basic_map_finalize(bmap
);
4034 isl_basic_map_free(bmap
);
4038 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4040 static __isl_give isl_basic_map
*var_more_or_equal(
4041 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4047 i
= isl_basic_map_alloc_inequality(bmap
);
4050 nparam
= isl_basic_map_n_param(bmap
);
4051 n_in
= isl_basic_map_n_in(bmap
);
4052 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4053 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4054 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4055 return isl_basic_map_finalize(bmap
);
4057 isl_basic_map_free(bmap
);
4061 __isl_give isl_basic_map
*isl_basic_map_equal(
4062 __isl_take isl_space
*dim
, unsigned n_equal
)
4065 struct isl_basic_map
*bmap
;
4066 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4069 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4070 bmap
= var_equal(bmap
, i
);
4071 return isl_basic_map_finalize(bmap
);
4074 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4076 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4080 struct isl_basic_map
*bmap
;
4081 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4084 for (i
= 0; i
< pos
&& bmap
; ++i
)
4085 bmap
= var_equal(bmap
, i
);
4087 bmap
= var_less(bmap
, pos
);
4088 return isl_basic_map_finalize(bmap
);
4091 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4093 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4094 __isl_take isl_space
*dim
, unsigned pos
)
4097 isl_basic_map
*bmap
;
4099 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4100 for (i
= 0; i
< pos
; ++i
)
4101 bmap
= var_equal(bmap
, i
);
4102 bmap
= var_less_or_equal(bmap
, pos
);
4103 return isl_basic_map_finalize(bmap
);
4106 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4108 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4112 struct isl_basic_map
*bmap
;
4113 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4116 for (i
= 0; i
< pos
&& bmap
; ++i
)
4117 bmap
= var_equal(bmap
, i
);
4119 bmap
= var_more(bmap
, pos
);
4120 return isl_basic_map_finalize(bmap
);
4123 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4125 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4126 __isl_take isl_space
*dim
, unsigned pos
)
4129 isl_basic_map
*bmap
;
4131 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4132 for (i
= 0; i
< pos
; ++i
)
4133 bmap
= var_equal(bmap
, i
);
4134 bmap
= var_more_or_equal(bmap
, pos
);
4135 return isl_basic_map_finalize(bmap
);
4138 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4139 unsigned n
, int equal
)
4141 struct isl_map
*map
;
4144 if (n
== 0 && equal
)
4145 return isl_map_universe(dims
);
4147 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4149 for (i
= 0; i
+ 1 < n
; ++i
)
4150 map
= isl_map_add_basic_map(map
,
4151 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4154 map
= isl_map_add_basic_map(map
,
4155 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4157 map
= isl_map_add_basic_map(map
,
4158 isl_basic_map_less_at(dims
, n
- 1));
4160 isl_space_free(dims
);
4165 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4169 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4172 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4174 return map_lex_lte_first(dim
, n
, 0);
4177 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4179 return map_lex_lte_first(dim
, n
, 1);
4182 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4184 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4187 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4189 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4192 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4193 unsigned n
, int equal
)
4195 struct isl_map
*map
;
4198 if (n
== 0 && equal
)
4199 return isl_map_universe(dims
);
4201 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4203 for (i
= 0; i
+ 1 < n
; ++i
)
4204 map
= isl_map_add_basic_map(map
,
4205 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4208 map
= isl_map_add_basic_map(map
,
4209 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4211 map
= isl_map_add_basic_map(map
,
4212 isl_basic_map_more_at(dims
, n
- 1));
4214 isl_space_free(dims
);
4219 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4223 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4226 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4228 return map_lex_gte_first(dim
, n
, 0);
4231 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4233 return map_lex_gte_first(dim
, n
, 1);
4236 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4238 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4241 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4243 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4246 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4247 __isl_take isl_set
*set2
)
4250 map
= isl_map_lex_le(isl_set_get_space(set1
));
4251 map
= isl_map_intersect_domain(map
, set1
);
4252 map
= isl_map_intersect_range(map
, set2
);
4256 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4257 __isl_take isl_set
*set2
)
4260 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4261 map
= isl_map_intersect_domain(map
, set1
);
4262 map
= isl_map_intersect_range(map
, set2
);
4266 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4267 __isl_take isl_set
*set2
)
4270 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4271 map
= isl_map_intersect_domain(map
, set1
);
4272 map
= isl_map_intersect_range(map
, set2
);
4276 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4277 __isl_take isl_set
*set2
)
4280 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4281 map
= isl_map_intersect_domain(map
, set1
);
4282 map
= isl_map_intersect_range(map
, set2
);
4286 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4287 __isl_take isl_map
*map2
)
4290 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4291 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4292 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4296 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4297 __isl_take isl_map
*map2
)
4300 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4301 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4302 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4306 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4307 __isl_take isl_map
*map2
)
4310 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4311 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4312 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4316 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4317 __isl_take isl_map
*map2
)
4320 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4321 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4322 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4326 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4327 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4329 struct isl_basic_map
*bmap
;
4331 bset
= isl_basic_set_cow(bset
);
4335 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4336 isl_space_free(bset
->dim
);
4337 bmap
= (struct isl_basic_map
*) bset
;
4339 return isl_basic_map_finalize(bmap
);
4341 isl_basic_set_free(bset
);
4342 isl_space_free(dim
);
4346 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4350 if (bmap
->dim
->n_in
== 0)
4351 return (struct isl_basic_set
*)bmap
;
4352 bmap
= isl_basic_map_cow(bmap
);
4355 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4358 bmap
= isl_basic_map_finalize(bmap
);
4359 return (struct isl_basic_set
*)bmap
;
4361 isl_basic_map_free(bmap
);
4365 /* For a div d = floor(f/m), add the constraint
4369 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4370 unsigned pos
, isl_int
*div
)
4373 unsigned total
= isl_basic_map_total_dim(bmap
);
4375 i
= isl_basic_map_alloc_inequality(bmap
);
4378 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4379 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4384 /* For a div d = floor(f/m), add the constraint
4386 * -(f-(n-1)) + m d >= 0
4388 static int add_lower_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_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4398 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4399 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4400 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4405 /* For a div d = floor(f/m), add the constraints
4408 * -(f-(n-1)) + m d >= 0
4410 * Note that the second constraint is the negation of
4414 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4415 unsigned pos
, isl_int
*div
)
4417 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4419 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4424 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4425 unsigned pos
, isl_int
*div
)
4427 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4431 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4433 unsigned total
= isl_basic_map_total_dim(bmap
);
4434 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4436 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4440 /* For each known div d = floor(f/m), add the constraints
4443 * -(f-(n-1)) + m d >= 0
4445 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4446 __isl_take isl_basic_map
*bmap
)
4453 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4456 bmap
= isl_basic_map_cow(bmap
);
4457 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4460 for (i
= 0; i
< n_div
; ++i
) {
4461 if (isl_int_is_zero(bmap
->div
[i
][0]))
4463 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4464 return isl_basic_map_free(bmap
);
4467 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4468 bmap
= isl_basic_map_finalize(bmap
);
4472 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4474 * In particular, if this div is of the form d = floor(f/m),
4475 * then add the constraint
4479 * if sign < 0 or the constraint
4481 * -(f-(n-1)) + m d >= 0
4485 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4486 unsigned div
, int sign
)
4494 total
= isl_basic_map_total_dim(bmap
);
4495 div_pos
= total
- bmap
->n_div
+ div
;
4498 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4500 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4503 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4505 return isl_basic_map_add_div_constraints(bset
, div
);
4508 struct isl_basic_set
*isl_basic_map_underlying_set(
4509 struct isl_basic_map
*bmap
)
4513 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4515 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4516 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4517 return (struct isl_basic_set
*)bmap
;
4518 bmap
= isl_basic_map_cow(bmap
);
4521 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4524 bmap
->extra
-= bmap
->n_div
;
4526 bmap
= isl_basic_map_finalize(bmap
);
4527 return (struct isl_basic_set
*)bmap
;
4529 isl_basic_map_free(bmap
);
4533 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4534 __isl_take isl_basic_set
*bset
)
4536 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4539 /* Replace each element in "list" by the result of applying
4540 * isl_basic_set_underlying_set to the element.
4542 __isl_give isl_basic_set_list
*isl_basic_set_list_underlying_set(
4543 __isl_take isl_basic_set_list
*list
)
4550 n
= isl_basic_set_list_n_basic_set(list
);
4551 for (i
= 0; i
< n
; ++i
) {
4552 isl_basic_set
*bset
;
4554 bset
= isl_basic_set_list_get_basic_set(list
, i
);
4555 bset
= isl_basic_set_underlying_set(bset
);
4556 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4562 struct isl_basic_map
*isl_basic_map_overlying_set(
4563 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4565 struct isl_basic_map
*bmap
;
4566 struct isl_ctx
*ctx
;
4573 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4574 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4575 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4577 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4578 isl_basic_map_free(like
);
4579 return (struct isl_basic_map
*)bset
;
4581 bset
= isl_basic_set_cow(bset
);
4584 total
= bset
->dim
->n_out
+ bset
->extra
;
4585 bmap
= (struct isl_basic_map
*)bset
;
4586 isl_space_free(bmap
->dim
);
4587 bmap
->dim
= isl_space_copy(like
->dim
);
4590 bmap
->n_div
= like
->n_div
;
4591 bmap
->extra
+= like
->n_div
;
4595 ltotal
= total
- bmap
->extra
+ like
->extra
;
4598 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4599 bmap
->extra
* (1 + 1 + total
));
4600 if (isl_blk_is_error(bmap
->block2
))
4602 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4606 for (i
= 0; i
< bmap
->extra
; ++i
)
4607 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4608 for (i
= 0; i
< like
->n_div
; ++i
) {
4609 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4610 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4612 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4614 isl_basic_map_free(like
);
4615 bmap
= isl_basic_map_simplify(bmap
);
4616 bmap
= isl_basic_map_finalize(bmap
);
4619 isl_basic_map_free(like
);
4620 isl_basic_set_free(bset
);
4624 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4625 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4627 return (struct isl_basic_set
*)
4628 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4631 struct isl_set
*isl_set_from_underlying_set(
4632 struct isl_set
*set
, struct isl_basic_set
*like
)
4638 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4640 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4641 isl_basic_set_free(like
);
4644 set
= isl_set_cow(set
);
4647 for (i
= 0; i
< set
->n
; ++i
) {
4648 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4649 isl_basic_set_copy(like
));
4653 isl_space_free(set
->dim
);
4654 set
->dim
= isl_space_copy(like
->dim
);
4657 isl_basic_set_free(like
);
4660 isl_basic_set_free(like
);
4665 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4669 map
= isl_map_cow(map
);
4672 map
->dim
= isl_space_cow(map
->dim
);
4676 for (i
= 1; i
< map
->n
; ++i
)
4677 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4679 for (i
= 0; i
< map
->n
; ++i
) {
4680 map
->p
[i
] = (struct isl_basic_map
*)
4681 isl_basic_map_underlying_set(map
->p
[i
]);
4686 map
->dim
= isl_space_underlying(map
->dim
, 0);
4688 isl_space_free(map
->dim
);
4689 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4693 return (struct isl_set
*)map
;
4699 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4701 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4704 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4705 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4707 bmap
= isl_basic_map_cow(bmap
);
4711 isl_space_free(bmap
->dim
);
4714 bmap
= isl_basic_map_finalize(bmap
);
4718 isl_basic_map_free(bmap
);
4719 isl_space_free(dim
);
4723 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4724 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4726 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4730 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4731 __isl_take isl_space
*dim
)
4735 map
= isl_map_cow(map
);
4739 for (i
= 0; i
< map
->n
; ++i
) {
4740 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4741 isl_space_copy(dim
));
4745 isl_space_free(map
->dim
);
4751 isl_space_free(dim
);
4755 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4756 __isl_take isl_space
*dim
)
4758 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4761 /* Compute the parameter domain of the given basic set.
4763 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4768 if (isl_basic_set_is_params(bset
))
4771 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4772 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4773 space
= isl_basic_set_get_space(bset
);
4774 space
= isl_space_params(space
);
4775 bset
= isl_basic_set_reset_space(bset
, space
);
4779 /* Construct a zero-dimensional basic set with the given parameter domain.
4781 __isl_give isl_basic_set
*isl_basic_set_from_params(
4782 __isl_take isl_basic_set
*bset
)
4785 space
= isl_basic_set_get_space(bset
);
4786 space
= isl_space_set_from_params(space
);
4787 bset
= isl_basic_set_reset_space(bset
, space
);
4791 /* Compute the parameter domain of the given set.
4793 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4798 if (isl_set_is_params(set
))
4801 n
= isl_set_dim(set
, isl_dim_set
);
4802 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4803 space
= isl_set_get_space(set
);
4804 space
= isl_space_params(space
);
4805 set
= isl_set_reset_space(set
, space
);
4809 /* Construct a zero-dimensional set with the given parameter domain.
4811 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4814 space
= isl_set_get_space(set
);
4815 space
= isl_space_set_from_params(space
);
4816 set
= isl_set_reset_space(set
, space
);
4820 /* Compute the parameter domain of the given map.
4822 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4827 n
= isl_map_dim(map
, isl_dim_in
);
4828 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4829 n
= isl_map_dim(map
, isl_dim_out
);
4830 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4831 space
= isl_map_get_space(map
);
4832 space
= isl_space_params(space
);
4833 map
= isl_map_reset_space(map
, space
);
4837 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4840 struct isl_basic_set
*domain
;
4846 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4848 n_in
= isl_basic_map_n_in(bmap
);
4849 n_out
= isl_basic_map_n_out(bmap
);
4850 domain
= isl_basic_set_from_basic_map(bmap
);
4851 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4853 domain
= isl_basic_set_reset_space(domain
, dim
);
4858 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4862 return isl_space_may_be_set(bmap
->dim
);
4865 /* Is this basic map actually a set?
4866 * Users should never call this function. Outside of isl,
4867 * the type should indicate whether something is a set or a map.
4869 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4873 return isl_space_is_set(bmap
->dim
);
4876 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4880 if (isl_basic_map_is_set(bmap
))
4882 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4885 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4886 __isl_take isl_basic_map
*bmap
)
4890 isl_basic_map
*domain
;
4891 int nparam
, n_in
, n_out
;
4894 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4895 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4896 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4898 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4899 domain
= isl_basic_map_universe(dim
);
4901 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4902 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4903 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4905 total
= isl_basic_map_total_dim(bmap
);
4907 for (i
= 0; i
< n_in
; ++i
) {
4908 k
= isl_basic_map_alloc_equality(bmap
);
4911 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4912 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4913 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4916 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4917 return isl_basic_map_finalize(bmap
);
4919 isl_basic_map_free(bmap
);
4923 __isl_give isl_basic_map
*isl_basic_map_range_map(
4924 __isl_take isl_basic_map
*bmap
)
4928 isl_basic_map
*range
;
4929 int nparam
, n_in
, n_out
;
4932 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4933 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4934 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4936 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4937 range
= isl_basic_map_universe(dim
);
4939 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4940 bmap
= isl_basic_map_apply_range(bmap
, range
);
4941 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4943 total
= isl_basic_map_total_dim(bmap
);
4945 for (i
= 0; i
< n_out
; ++i
) {
4946 k
= isl_basic_map_alloc_equality(bmap
);
4949 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4950 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4951 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4954 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4955 return isl_basic_map_finalize(bmap
);
4957 isl_basic_map_free(bmap
);
4961 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4965 return isl_space_may_be_set(map
->dim
);
4968 /* Is this map actually a set?
4969 * Users should never call this function. Outside of isl,
4970 * the type should indicate whether something is a set or a map.
4972 int isl_map_is_set(__isl_keep isl_map
*map
)
4976 return isl_space_is_set(map
->dim
);
4979 struct isl_set
*isl_map_range(struct isl_map
*map
)
4982 struct isl_set
*set
;
4986 if (isl_map_is_set(map
))
4987 return (isl_set
*)map
;
4989 map
= isl_map_cow(map
);
4993 set
= (struct isl_set
*) map
;
4994 set
->dim
= isl_space_range(set
->dim
);
4997 for (i
= 0; i
< map
->n
; ++i
) {
4998 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5002 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5003 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5010 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5014 map
= isl_map_cow(map
);
5018 map
->dim
= isl_space_domain_map(map
->dim
);
5021 for (i
= 0; i
< map
->n
; ++i
) {
5022 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5026 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5027 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5034 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5037 isl_space
*range_dim
;
5039 map
= isl_map_cow(map
);
5043 range_dim
= isl_space_range(isl_map_get_space(map
));
5044 range_dim
= isl_space_from_range(range_dim
);
5045 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5046 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5049 for (i
= 0; i
< map
->n
; ++i
) {
5050 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5054 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5055 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5062 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5063 __isl_take isl_space
*dim
)
5066 struct isl_map
*map
= NULL
;
5068 set
= isl_set_cow(set
);
5071 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5072 map
= (struct isl_map
*)set
;
5073 for (i
= 0; i
< set
->n
; ++i
) {
5074 map
->p
[i
] = isl_basic_map_from_basic_set(
5075 set
->p
[i
], isl_space_copy(dim
));
5079 isl_space_free(map
->dim
);
5083 isl_space_free(dim
);
5088 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5089 __isl_take isl_basic_set
*bset
)
5091 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5094 __isl_give isl_basic_map
*isl_basic_map_from_range(
5095 __isl_take isl_basic_set
*bset
)
5098 space
= isl_basic_set_get_space(bset
);
5099 space
= isl_space_from_range(space
);
5100 bset
= isl_basic_set_reset_space(bset
, space
);
5101 return (isl_basic_map
*)bset
;
5104 /* Create a relation with the given set as range.
5105 * The domain of the created relation is a zero-dimensional
5106 * flat anonymous space.
5108 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5111 space
= isl_set_get_space(set
);
5112 space
= isl_space_from_range(space
);
5113 set
= isl_set_reset_space(set
, space
);
5114 return (struct isl_map
*)set
;
5117 /* Create a relation with the given set as domain.
5118 * The range of the created relation is a zero-dimensional
5119 * flat anonymous space.
5121 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5123 return isl_map_reverse(isl_map_from_range(set
));
5126 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5127 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5129 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5132 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5133 __isl_take isl_set
*range
)
5135 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5138 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
5141 struct isl_set
*set
= NULL
;
5145 map
= isl_map_cow(map
);
5148 map
->dim
= isl_space_as_set_space(map
->dim
);
5151 set
= (struct isl_set
*)map
;
5152 for (i
= 0; i
< map
->n
; ++i
) {
5153 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5163 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5166 struct isl_map
*map
;
5171 isl_die(dim
->ctx
, isl_error_internal
,
5172 "negative number of basic maps", goto error
);
5173 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5174 sizeof(struct isl_map
) +
5175 (n
- 1) * sizeof(struct isl_basic_map
*));
5179 map
->ctx
= dim
->ctx
;
5180 isl_ctx_ref(map
->ctx
);
5188 isl_space_free(dim
);
5192 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5193 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5196 struct isl_map
*map
;
5199 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5203 map
= isl_map_alloc_space(dims
, n
, flags
);
5207 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5209 struct isl_basic_map
*bmap
;
5210 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5211 bmap
= isl_basic_map_set_to_empty(bmap
);
5215 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5217 struct isl_basic_set
*bset
;
5218 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5219 bset
= isl_basic_set_set_to_empty(bset
);
5223 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5225 struct isl_basic_map
*bmap
;
5228 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5229 bmap
= isl_basic_map_set_to_empty(bmap
);
5233 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5235 struct isl_basic_map
*bmap
;
5238 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5239 bmap
= isl_basic_map_set_to_empty(bmap
);
5243 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5245 struct isl_basic_set
*bset
;
5248 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5249 bset
= isl_basic_set_set_to_empty(bset
);
5253 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5255 struct isl_basic_map
*bmap
;
5256 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5257 bmap
= isl_basic_map_finalize(bmap
);
5261 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5263 struct isl_basic_set
*bset
;
5264 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5265 bset
= isl_basic_set_finalize(bset
);
5269 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5272 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5273 isl_basic_map
*bmap
;
5275 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5276 for (i
= 0; i
< total
; ++i
) {
5277 int k
= isl_basic_map_alloc_inequality(bmap
);
5280 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5281 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5285 isl_basic_map_free(bmap
);
5289 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5291 return isl_basic_map_nat_universe(dim
);
5294 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5296 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5299 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5301 return isl_map_nat_universe(dim
);
5304 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5305 __isl_keep isl_basic_map
*model
)
5309 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5312 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5316 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5319 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5320 __isl_keep isl_set
*model
)
5324 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5327 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5329 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5332 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5336 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5339 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5343 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5346 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5348 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5351 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5355 return isl_set_empty(isl_space_copy(model
->dim
));
5358 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5360 struct isl_map
*map
;
5363 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5364 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5368 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5370 struct isl_set
*set
;
5373 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5374 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5378 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5382 return isl_set_universe(isl_space_copy(model
->dim
));
5385 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5388 struct isl_map
*dup
;
5392 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5393 for (i
= 0; i
< map
->n
; ++i
)
5394 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5398 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5399 __isl_take isl_basic_map
*bmap
)
5403 if (isl_basic_map_plain_is_empty(bmap
)) {
5404 isl_basic_map_free(bmap
);
5407 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5408 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5409 map
->p
[map
->n
] = bmap
;
5411 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5417 isl_basic_map_free(bmap
);
5421 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5431 isl_ctx_deref(map
->ctx
);
5432 for (i
= 0; i
< map
->n
; ++i
)
5433 isl_basic_map_free(map
->p
[i
]);
5434 isl_space_free(map
->dim
);
5440 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5441 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5445 base
= isl_map_cow(base
);
5449 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5452 for (i
= 0; i
< base
->n
; ++i
) {
5453 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5454 isl_space_copy(base
->dim
), 0, 0, 0);
5464 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5465 unsigned nparam
, unsigned dim
)
5467 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5471 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5472 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5476 bmap
= isl_basic_map_cow(bmap
);
5477 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5478 j
= isl_basic_map_alloc_equality(bmap
);
5481 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5482 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5483 isl_int_set_si(bmap
->eq
[j
][0], value
);
5484 bmap
= isl_basic_map_simplify(bmap
);
5485 return isl_basic_map_finalize(bmap
);
5487 isl_basic_map_free(bmap
);
5491 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5492 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5496 bmap
= isl_basic_map_cow(bmap
);
5497 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5498 j
= isl_basic_map_alloc_equality(bmap
);
5501 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5502 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5503 isl_int_set(bmap
->eq
[j
][0], value
);
5504 bmap
= isl_basic_map_simplify(bmap
);
5505 return isl_basic_map_finalize(bmap
);
5507 isl_basic_map_free(bmap
);
5511 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5512 enum isl_dim_type type
, unsigned pos
, int value
)
5516 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5517 return isl_basic_map_fix_pos_si(bmap
,
5518 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5520 isl_basic_map_free(bmap
);
5524 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5525 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5529 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5530 return isl_basic_map_fix_pos(bmap
,
5531 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5533 isl_basic_map_free(bmap
);
5537 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5538 * to be equal to "v".
5540 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5541 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5545 if (!isl_val_is_int(v
))
5546 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5547 "expecting integer value", goto error
);
5548 if (pos
>= isl_basic_map_dim(bmap
, type
))
5549 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5550 "index out of bounds", goto error
);
5551 pos
+= isl_basic_map_offset(bmap
, type
);
5552 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5556 isl_basic_map_free(bmap
);
5561 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5562 * to be equal to "v".
5564 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5565 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5567 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5570 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5571 enum isl_dim_type type
, unsigned pos
, int value
)
5573 return (struct isl_basic_set
*)
5574 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5578 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5579 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5581 return (struct isl_basic_set
*)
5582 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5586 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5587 unsigned input
, int value
)
5589 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5592 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5593 unsigned dim
, int value
)
5595 return (struct isl_basic_set
*)
5596 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5597 isl_dim_set
, dim
, value
);
5600 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5602 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5609 isl_basic_map_free(map
->p
[i
]);
5610 if (i
!= map
->n
- 1) {
5611 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5612 map
->p
[i
] = map
->p
[map
->n
- 1];
5619 /* Perform "fn" on each basic map of "map", where we may not be holding
5620 * the only reference to "map".
5621 * In particular, "fn" should be a semantics preserving operation
5622 * that we want to apply to all copies of "map". We therefore need
5623 * to be careful not to modify "map" in a way that breaks "map"
5624 * in case anything goes wrong.
5626 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5627 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5629 struct isl_basic_map
*bmap
;
5635 for (i
= map
->n
- 1; i
>= 0; --i
) {
5636 bmap
= isl_basic_map_copy(map
->p
[i
]);
5640 isl_basic_map_free(map
->p
[i
]);
5642 if (remove_if_empty(map
, i
) < 0)
5652 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5653 enum isl_dim_type type
, unsigned pos
, int value
)
5657 map
= isl_map_cow(map
);
5661 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5662 for (i
= map
->n
- 1; i
>= 0; --i
) {
5663 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5664 if (remove_if_empty(map
, i
) < 0)
5667 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5674 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5675 enum isl_dim_type type
, unsigned pos
, int value
)
5677 return (struct isl_set
*)
5678 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5681 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5682 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5686 map
= isl_map_cow(map
);
5690 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5691 for (i
= 0; i
< map
->n
; ++i
) {
5692 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5696 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5703 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5704 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5706 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5709 /* Fix the value of the variable at position "pos" of type "type" of "map"
5710 * to be equal to "v".
5712 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5713 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5717 map
= isl_map_cow(map
);
5721 if (!isl_val_is_int(v
))
5722 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5723 "expecting integer value", goto error
);
5724 if (pos
>= isl_map_dim(map
, type
))
5725 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5726 "index out of bounds", goto error
);
5727 for (i
= map
->n
- 1; i
>= 0; --i
) {
5728 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5730 if (remove_if_empty(map
, i
) < 0)
5733 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5742 /* Fix the value of the variable at position "pos" of type "type" of "set"
5743 * to be equal to "v".
5745 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5746 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5748 return isl_map_fix_val(set
, type
, pos
, v
);
5751 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5752 unsigned input
, int value
)
5754 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5757 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5759 return (struct isl_set
*)
5760 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5763 static __isl_give isl_basic_map
*basic_map_bound_si(
5764 __isl_take isl_basic_map
*bmap
,
5765 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5771 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5772 pos
+= isl_basic_map_offset(bmap
, type
);
5773 bmap
= isl_basic_map_cow(bmap
);
5774 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5775 j
= isl_basic_map_alloc_inequality(bmap
);
5778 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5780 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5781 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5783 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5784 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5786 bmap
= isl_basic_map_simplify(bmap
);
5787 return isl_basic_map_finalize(bmap
);
5789 isl_basic_map_free(bmap
);
5793 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5794 __isl_take isl_basic_map
*bmap
,
5795 enum isl_dim_type type
, unsigned pos
, int value
)
5797 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5800 /* Constrain the values of the given dimension to be no greater than "value".
5802 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5803 __isl_take isl_basic_map
*bmap
,
5804 enum isl_dim_type type
, unsigned pos
, int value
)
5806 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5809 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5810 unsigned dim
, isl_int value
)
5814 bset
= isl_basic_set_cow(bset
);
5815 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5816 j
= isl_basic_set_alloc_inequality(bset
);
5819 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5820 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5821 isl_int_neg(bset
->ineq
[j
][0], value
);
5822 bset
= isl_basic_set_simplify(bset
);
5823 return isl_basic_set_finalize(bset
);
5825 isl_basic_set_free(bset
);
5829 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5830 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5834 map
= isl_map_cow(map
);
5838 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5839 for (i
= 0; i
< map
->n
; ++i
) {
5840 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5841 type
, pos
, value
, upper
);
5845 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5852 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5853 enum isl_dim_type type
, unsigned pos
, int value
)
5855 return map_bound_si(map
, type
, pos
, value
, 0);
5858 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5859 enum isl_dim_type type
, unsigned pos
, int value
)
5861 return map_bound_si(map
, type
, pos
, value
, 1);
5864 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5865 enum isl_dim_type type
, unsigned pos
, int value
)
5867 return (struct isl_set
*)
5868 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5871 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5872 enum isl_dim_type type
, unsigned pos
, int value
)
5874 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5877 /* Bound the given variable of "bmap" from below (or above is "upper"
5878 * is set) to "value".
5880 static __isl_give isl_basic_map
*basic_map_bound(
5881 __isl_take isl_basic_map
*bmap
,
5882 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5888 if (pos
>= isl_basic_map_dim(bmap
, type
))
5889 isl_die(bmap
->ctx
, isl_error_invalid
,
5890 "index out of bounds", goto error
);
5891 pos
+= isl_basic_map_offset(bmap
, type
);
5892 bmap
= isl_basic_map_cow(bmap
);
5893 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5894 j
= isl_basic_map_alloc_inequality(bmap
);
5897 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5899 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5900 isl_int_set(bmap
->ineq
[j
][0], value
);
5902 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5903 isl_int_neg(bmap
->ineq
[j
][0], value
);
5905 bmap
= isl_basic_map_simplify(bmap
);
5906 return isl_basic_map_finalize(bmap
);
5908 isl_basic_map_free(bmap
);
5912 /* Bound the given variable of "map" from below (or above is "upper"
5913 * is set) to "value".
5915 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5916 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5920 map
= isl_map_cow(map
);
5924 if (pos
>= isl_map_dim(map
, type
))
5925 isl_die(map
->ctx
, isl_error_invalid
,
5926 "index out of bounds", goto error
);
5927 for (i
= map
->n
- 1; i
>= 0; --i
) {
5928 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5929 if (remove_if_empty(map
, i
) < 0)
5932 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5939 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5940 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5942 return map_bound(map
, type
, pos
, value
, 0);
5945 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5946 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5948 return map_bound(map
, type
, pos
, value
, 1);
5951 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5952 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5954 return isl_map_lower_bound(set
, type
, pos
, value
);
5957 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5958 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5960 return isl_map_upper_bound(set
, type
, pos
, value
);
5963 /* Force the values of the variable at position "pos" of type "type" of "set"
5964 * to be no smaller than "value".
5966 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5967 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5971 if (!isl_val_is_int(value
))
5972 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5973 "expecting integer value", goto error
);
5974 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5975 isl_val_free(value
);
5978 isl_val_free(value
);
5983 /* Force the values of the variable at position "pos" of type "type" of "set"
5984 * to be no greater than "value".
5986 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5987 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5991 if (!isl_val_is_int(value
))
5992 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5993 "expecting integer value", goto error
);
5994 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5995 isl_val_free(value
);
5998 isl_val_free(value
);
6003 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6008 set
= isl_set_cow(set
);
6012 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6013 for (i
= 0; i
< set
->n
; ++i
) {
6014 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6024 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6028 map
= isl_map_cow(map
);
6032 map
->dim
= isl_space_reverse(map
->dim
);
6035 for (i
= 0; i
< map
->n
; ++i
) {
6036 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6040 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6047 static struct isl_map
*isl_basic_map_partial_lexopt(
6048 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6049 struct isl_set
**empty
, int max
)
6051 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6054 struct isl_map
*isl_basic_map_partial_lexmax(
6055 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6056 struct isl_set
**empty
)
6058 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6061 struct isl_map
*isl_basic_map_partial_lexmin(
6062 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6063 struct isl_set
**empty
)
6065 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6068 struct isl_set
*isl_basic_set_partial_lexmin(
6069 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6070 struct isl_set
**empty
)
6072 return (struct isl_set
*)
6073 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6077 struct isl_set
*isl_basic_set_partial_lexmax(
6078 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6079 struct isl_set
**empty
)
6081 return (struct isl_set
*)
6082 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6086 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6087 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6088 __isl_give isl_set
**empty
)
6090 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6093 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6094 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6095 __isl_give isl_set
**empty
)
6097 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6100 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6101 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6102 __isl_give isl_set
**empty
)
6104 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6107 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6108 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6109 __isl_give isl_set
**empty
)
6111 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6114 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6115 __isl_take isl_basic_map
*bmap
, int max
)
6117 isl_basic_set
*dom
= NULL
;
6118 isl_space
*dom_space
;
6122 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6123 dom
= isl_basic_set_universe(dom_space
);
6124 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6126 isl_basic_map_free(bmap
);
6130 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6131 __isl_take isl_basic_map
*bmap
)
6133 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6137 #define TYPE isl_pw_multi_aff
6139 #define SUFFIX _pw_multi_aff
6141 #define EMPTY isl_pw_multi_aff_empty
6143 #define ADD isl_pw_multi_aff_union_add
6144 #include "isl_map_lexopt_templ.c"
6146 /* Given a map "map", compute the lexicographically minimal
6147 * (or maximal) image element for each domain element in dom,
6148 * in the form of an isl_pw_multi_aff.
6149 * Set *empty to those elements in dom that do not have an image element.
6151 * We first compute the lexicographically minimal or maximal element
6152 * in the first basic map. This results in a partial solution "res"
6153 * and a subset "todo" of dom that still need to be handled.
6154 * We then consider each of the remaining maps in "map" and successively
6155 * update both "res" and "todo".
6157 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6158 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6159 __isl_give isl_set
**empty
, int max
)
6162 isl_pw_multi_aff
*res
;
6168 if (isl_map_plain_is_empty(map
)) {
6173 return isl_pw_multi_aff_from_map(map
);
6176 res
= basic_map_partial_lexopt_pw_multi_aff(
6177 isl_basic_map_copy(map
->p
[0]),
6178 isl_set_copy(dom
), &todo
, max
);
6180 for (i
= 1; i
< map
->n
; ++i
) {
6181 isl_pw_multi_aff
*res_i
;
6184 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6185 isl_basic_map_copy(map
->p
[i
]),
6186 isl_set_copy(dom
), &todo_i
, max
);
6189 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6191 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6193 todo
= isl_set_intersect(todo
, todo_i
);
6214 #define TYPE isl_map
6218 #define EMPTY isl_map_empty
6220 #define ADD isl_map_union_disjoint
6221 #include "isl_map_lexopt_templ.c"
6223 /* Given a map "map", compute the lexicographically minimal
6224 * (or maximal) image element for each domain element in dom.
6225 * Set *empty to those elements in dom that do not have an image element.
6227 * We first compute the lexicographically minimal or maximal element
6228 * in the first basic map. This results in a partial solution "res"
6229 * and a subset "todo" of dom that still need to be handled.
6230 * We then consider each of the remaining maps in "map" and successively
6231 * update both "res" and "todo".
6233 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6234 * Assume we are computing the lexicographical maximum.
6235 * We first compute the lexicographically maximal element in basic map i.
6236 * This results in a partial solution res_i and a subset todo_i.
6237 * Then we combine these results with those obtain for the first k basic maps
6238 * to obtain a result that is valid for the first k+1 basic maps.
6239 * In particular, the set where there is no solution is the set where
6240 * there is no solution for the first k basic maps and also no solution
6241 * for the ith basic map, i.e.,
6243 * todo^i = todo^k * todo_i
6245 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6246 * solutions, arbitrarily breaking ties in favor of res^k.
6247 * That is, when res^k(a) >= res_i(a), we pick res^k and
6248 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6249 * the lexicographic order.)
6250 * In practice, we compute
6252 * res^k * (res_i . "<=")
6256 * res_i * (res^k . "<")
6258 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6259 * where only one of res^k and res_i provides a solution and we simply pick
6266 * Note that we only compute these intersections when dom(res^k) intersects
6267 * dom(res_i). Otherwise, the only effect of these intersections is to
6268 * potentially break up res^k and res_i into smaller pieces.
6269 * We want to avoid such splintering as much as possible.
6270 * In fact, an earlier implementation of this function would look for
6271 * better results in the domain of res^k and for extra results in todo^k,
6272 * but this would always result in a splintering according to todo^k,
6273 * even when the domain of basic map i is disjoint from the domains of
6274 * the previous basic maps.
6276 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6277 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6278 __isl_give isl_set
**empty
, int max
)
6281 struct isl_map
*res
;
6282 struct isl_set
*todo
;
6287 if (isl_map_plain_is_empty(map
)) {
6295 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6296 isl_set_copy(dom
), &todo
, max
);
6298 for (i
= 1; i
< map
->n
; ++i
) {
6302 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6304 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6305 isl_set_copy(dom
), &todo_i
, max
);
6308 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6309 le
= isl_map_lex_le(dim
);
6311 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6312 le
= isl_map_lex_ge(dim
);
6314 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6315 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6316 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6317 le
= isl_map_intersect(le
, isl_map_copy(res
));
6319 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6320 res
= isl_map_intersect_domain(res
,
6321 isl_set_copy(todo_i
));
6322 res_i
= isl_map_intersect_domain(res_i
,
6323 isl_set_copy(todo
));
6326 res
= isl_map_union_disjoint(res
, res_i
);
6327 res
= isl_map_union_disjoint(res
, lt
);
6328 res
= isl_map_union_disjoint(res
, le
);
6330 todo
= isl_set_intersect(todo
, todo_i
);
6350 __isl_give isl_map
*isl_map_partial_lexmax(
6351 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6352 __isl_give isl_set
**empty
)
6354 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6357 __isl_give isl_map
*isl_map_partial_lexmin(
6358 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6359 __isl_give isl_set
**empty
)
6361 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6364 __isl_give isl_set
*isl_set_partial_lexmin(
6365 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6366 __isl_give isl_set
**empty
)
6368 return (struct isl_set
*)
6369 isl_map_partial_lexmin((struct isl_map
*)set
,
6373 __isl_give isl_set
*isl_set_partial_lexmax(
6374 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6375 __isl_give isl_set
**empty
)
6377 return (struct isl_set
*)
6378 isl_map_partial_lexmax((struct isl_map
*)set
,
6382 /* Compute the lexicographic minimum (or maximum if "max" is set)
6383 * of "bmap" over its domain.
6385 * Since we are not interested in the part of the domain space where
6386 * there is no solution, we initialize the domain to those constraints
6387 * of "bmap" that only involve the parameters and the input dimensions.
6388 * This relieves the parametric programming engine from detecting those
6389 * inequalities and transferring them to the context. More importantly,
6390 * it ensures that those inequalities are transferred first and not
6391 * intermixed with inequalities that actually split the domain.
6393 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6397 isl_basic_map
*copy
;
6400 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6401 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6402 copy
= isl_basic_map_copy(bmap
);
6403 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6404 isl_dim_div
, 0, n_div
);
6405 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6406 isl_dim_out
, 0, n_out
);
6407 dom
= isl_basic_map_domain(copy
);
6408 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6411 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6413 return isl_basic_map_lexopt(bmap
, 0);
6416 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6418 return isl_basic_map_lexopt(bmap
, 1);
6421 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6423 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6426 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6428 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6431 /* Extract the first and only affine expression from list
6432 * and then add it to *pwaff with the given dom.
6433 * This domain is known to be disjoint from other domains
6434 * because of the way isl_basic_map_foreach_lexmax works.
6436 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6437 __isl_take isl_aff_list
*list
, void *user
)
6439 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6441 isl_pw_aff
**pwaff
= user
;
6442 isl_pw_aff
*pwaff_i
;
6446 if (isl_aff_list_n_aff(list
) != 1)
6447 isl_die(ctx
, isl_error_internal
,
6448 "expecting single element list", goto error
);
6450 aff
= isl_aff_list_get_aff(list
, 0);
6451 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6453 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6455 isl_aff_list_free(list
);
6459 isl_basic_set_free(dom
);
6460 isl_aff_list_free(list
);
6464 /* Given a basic map with one output dimension, compute the minimum or
6465 * maximum of that dimension as an isl_pw_aff.
6467 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6468 * call update_dim_opt on each leaf of the result.
6470 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6473 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6477 dim
= isl_space_from_domain(isl_space_domain(dim
));
6478 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6479 pwaff
= isl_pw_aff_empty(dim
);
6481 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6483 return isl_pw_aff_free(pwaff
);
6488 /* Compute the minimum or maximum of the given output dimension
6489 * as a function of the parameters and the input dimensions,
6490 * but independently of the other output dimensions.
6492 * We first project out the other output dimension and then compute
6493 * the "lexicographic" maximum in each basic map, combining the results
6494 * using isl_pw_aff_union_max.
6496 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6503 n_out
= isl_map_dim(map
, isl_dim_out
);
6504 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6505 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6510 isl_space
*dim
= isl_map_get_space(map
);
6512 return isl_pw_aff_empty(dim
);
6515 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6516 for (i
= 1; i
< map
->n
; ++i
) {
6517 isl_pw_aff
*pwaff_i
;
6519 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6520 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6528 /* Compute the maximum of the given output dimension as a function of the
6529 * parameters and input dimensions, but independently of
6530 * the other output dimensions.
6532 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6534 return map_dim_opt(map
, pos
, 1);
6537 /* Compute the minimum or maximum of the given set dimension
6538 * as a function of the parameters,
6539 * but independently of the other set dimensions.
6541 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6544 return map_dim_opt(set
, pos
, max
);
6547 /* Compute the maximum of the given set dimension as a function of the
6548 * parameters, but independently of the other set dimensions.
6550 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6552 return set_dim_opt(set
, pos
, 1);
6555 /* Compute the minimum of the given set dimension as a function of the
6556 * parameters, but independently of the other set dimensions.
6558 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6560 return set_dim_opt(set
, pos
, 0);
6563 /* Apply a preimage specified by "mat" on the parameters of "bset".
6564 * bset is assumed to have only parameters and divs.
6566 static struct isl_basic_set
*basic_set_parameter_preimage(
6567 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6574 bset
->dim
= isl_space_cow(bset
->dim
);
6578 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6580 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6582 bset
->dim
->nparam
= 0;
6583 bset
->dim
->n_out
= nparam
;
6584 bset
= isl_basic_set_preimage(bset
, mat
);
6586 bset
->dim
->nparam
= bset
->dim
->n_out
;
6587 bset
->dim
->n_out
= 0;
6592 isl_basic_set_free(bset
);
6596 /* Apply a preimage specified by "mat" on the parameters of "set".
6597 * set is assumed to have only parameters and divs.
6599 static struct isl_set
*set_parameter_preimage(
6600 struct isl_set
*set
, struct isl_mat
*mat
)
6602 isl_space
*dim
= NULL
;
6608 dim
= isl_space_copy(set
->dim
);
6609 dim
= isl_space_cow(dim
);
6613 nparam
= isl_set_dim(set
, isl_dim_param
);
6615 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6618 dim
->n_out
= nparam
;
6619 isl_set_reset_space(set
, dim
);
6620 set
= isl_set_preimage(set
, mat
);
6623 dim
= isl_space_copy(set
->dim
);
6624 dim
= isl_space_cow(dim
);
6627 dim
->nparam
= dim
->n_out
;
6629 isl_set_reset_space(set
, dim
);
6632 isl_space_free(dim
);
6639 /* Intersect the basic set "bset" with the affine space specified by the
6640 * equalities in "eq".
6642 static struct isl_basic_set
*basic_set_append_equalities(
6643 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6651 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6656 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6657 for (i
= 0; i
< eq
->n_row
; ++i
) {
6658 k
= isl_basic_set_alloc_equality(bset
);
6661 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6662 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6666 bset
= isl_basic_set_gauss(bset
, NULL
);
6667 bset
= isl_basic_set_finalize(bset
);
6672 isl_basic_set_free(bset
);
6676 /* Intersect the set "set" with the affine space specified by the
6677 * equalities in "eq".
6679 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6687 for (i
= 0; i
< set
->n
; ++i
) {
6688 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6701 /* Given a basic set "bset" that only involves parameters and existentially
6702 * quantified variables, return the index of the first equality
6703 * that only involves parameters. If there is no such equality then
6704 * return bset->n_eq.
6706 * This function assumes that isl_basic_set_gauss has been called on "bset".
6708 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6711 unsigned nparam
, n_div
;
6716 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6717 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6719 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6720 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6727 /* Compute an explicit representation for the existentially quantified
6728 * variables in "bset" by computing the "minimal value" of the set
6729 * variables. Since there are no set variables, the computation of
6730 * the minimal value essentially computes an explicit representation
6731 * of the non-empty part(s) of "bset".
6733 * The input only involves parameters and existentially quantified variables.
6734 * All equalities among parameters have been removed.
6736 * Since the existentially quantified variables in the result are in general
6737 * going to be different from those in the input, we first replace
6738 * them by the minimal number of variables based on their equalities.
6739 * This should simplify the parametric integer programming.
6741 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6743 isl_morph
*morph1
, *morph2
;
6749 if (bset
->n_eq
== 0)
6750 return isl_basic_set_lexmin(bset
);
6752 morph1
= isl_basic_set_parameter_compression(bset
);
6753 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6754 bset
= isl_basic_set_lift(bset
);
6755 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6756 bset
= isl_morph_basic_set(morph2
, bset
);
6757 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6758 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6760 set
= isl_basic_set_lexmin(bset
);
6762 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6767 /* Project the given basic set onto its parameter domain, possibly introducing
6768 * new, explicit, existential variables in the constraints.
6769 * The input has parameters and (possibly implicit) existential variables.
6770 * The output has the same parameters, but only
6771 * explicit existentially quantified variables.
6773 * The actual projection is performed by pip, but pip doesn't seem
6774 * to like equalities very much, so we first remove the equalities
6775 * among the parameters by performing a variable compression on
6776 * the parameters. Afterward, an inverse transformation is performed
6777 * and the equalities among the parameters are inserted back in.
6779 * The variable compression on the parameters may uncover additional
6780 * equalities that were only implicit before. We therefore check
6781 * if there are any new parameter equalities in the result and
6782 * if so recurse. The removal of parameter equalities is required
6783 * for the parameter compression performed by base_compute_divs.
6785 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6789 struct isl_mat
*T
, *T2
;
6790 struct isl_set
*set
;
6793 bset
= isl_basic_set_cow(bset
);
6797 if (bset
->n_eq
== 0)
6798 return base_compute_divs(bset
);
6800 bset
= isl_basic_set_gauss(bset
, NULL
);
6803 if (isl_basic_set_plain_is_empty(bset
))
6804 return isl_set_from_basic_set(bset
);
6806 i
= first_parameter_equality(bset
);
6807 if (i
== bset
->n_eq
)
6808 return base_compute_divs(bset
);
6810 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6811 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6813 eq
= isl_mat_cow(eq
);
6814 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6815 if (T
&& T
->n_col
== 0) {
6819 bset
= isl_basic_set_set_to_empty(bset
);
6820 return isl_set_from_basic_set(bset
);
6822 bset
= basic_set_parameter_preimage(bset
, T
);
6824 i
= first_parameter_equality(bset
);
6827 else if (i
== bset
->n_eq
)
6828 set
= base_compute_divs(bset
);
6830 set
= parameter_compute_divs(bset
);
6831 set
= set_parameter_preimage(set
, T2
);
6832 set
= set_append_equalities(set
, eq
);
6836 /* Insert the divs from "ls" before those of "bmap".
6838 * The number of columns is not changed, which means that the last
6839 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6840 * The caller is responsible for removing the same number of dimensions
6841 * from the space of "bmap".
6843 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6844 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6850 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6854 old_n_div
= bmap
->n_div
;
6855 bmap
= insert_div_rows(bmap
, n_div
);
6859 for (i
= 0; i
< n_div
; ++i
) {
6860 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6861 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6867 /* Replace the space of "bmap" by the space and divs of "ls".
6869 * If "ls" has any divs, then we simplify the result since we may
6870 * have discovered some additional equalities that could simplify
6871 * the div expressions.
6873 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6874 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6878 bmap
= isl_basic_map_cow(bmap
);
6882 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6883 bmap
= insert_divs_from_local_space(bmap
, ls
);
6887 isl_space_free(bmap
->dim
);
6888 bmap
->dim
= isl_local_space_get_space(ls
);
6892 isl_local_space_free(ls
);
6894 bmap
= isl_basic_map_simplify(bmap
);
6895 bmap
= isl_basic_map_finalize(bmap
);
6898 isl_basic_map_free(bmap
);
6899 isl_local_space_free(ls
);
6903 /* Replace the space of "map" by the space and divs of "ls".
6905 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6906 __isl_take isl_local_space
*ls
)
6910 map
= isl_map_cow(map
);
6914 for (i
= 0; i
< map
->n
; ++i
) {
6915 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6916 isl_local_space_copy(ls
));
6920 isl_space_free(map
->dim
);
6921 map
->dim
= isl_local_space_get_space(ls
);
6925 isl_local_space_free(ls
);
6928 isl_local_space_free(ls
);
6933 /* Compute an explicit representation for the existentially
6934 * quantified variables for which do not know any explicit representation yet.
6936 * We first sort the existentially quantified variables so that the
6937 * existentially quantified variables for which we already have an explicit
6938 * representation are placed before those for which we do not.
6939 * The input dimensions, the output dimensions and the existentially
6940 * quantified variables for which we already have an explicit
6941 * representation are then turned into parameters.
6942 * compute_divs returns a map with the same parameters and
6943 * no input or output dimensions and the dimension specification
6944 * is reset to that of the input, including the existentially quantified
6945 * variables for which we already had an explicit representation.
6947 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6949 struct isl_basic_set
*bset
;
6950 struct isl_set
*set
;
6951 struct isl_map
*map
;
6953 isl_local_space
*ls
;
6960 bmap
= isl_basic_map_sort_divs(bmap
);
6961 bmap
= isl_basic_map_cow(bmap
);
6965 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6966 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6969 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6970 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6971 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6972 dim
= isl_space_set_alloc(bmap
->ctx
,
6973 nparam
+ n_in
+ n_out
+ n_known
, 0);
6977 ls
= isl_basic_map_get_local_space(bmap
);
6978 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6979 n_known
, bmap
->n_div
- n_known
);
6981 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6982 swap_div(bmap
, i
- n_known
, i
);
6983 bmap
->n_div
-= n_known
;
6984 bmap
->extra
-= n_known
;
6986 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6987 bset
= (struct isl_basic_set
*)bmap
;
6989 set
= parameter_compute_divs(bset
);
6990 map
= (struct isl_map
*)set
;
6991 map
= replace_space_by_local_space(map
, ls
);
6995 isl_basic_map_free(bmap
);
6999 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7007 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7008 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7009 if (isl_int_is_zero(bmap
->div
[i
][0]))
7011 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
7017 static int map_divs_known(__isl_keep isl_map
*map
)
7024 for (i
= 0; i
< map
->n
; ++i
) {
7025 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7033 /* If bmap contains any unknown divs, then compute explicit
7034 * expressions for them. However, this computation may be
7035 * quite expensive, so first try to remove divs that aren't
7038 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7041 struct isl_map
*map
;
7043 known
= isl_basic_map_divs_known(bmap
);
7047 return isl_map_from_basic_map(bmap
);
7049 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7051 known
= isl_basic_map_divs_known(bmap
);
7055 return isl_map_from_basic_map(bmap
);
7057 map
= compute_divs(bmap
);
7060 isl_basic_map_free(bmap
);
7064 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7068 struct isl_map
*res
;
7075 known
= map_divs_known(map
);
7083 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7084 for (i
= 1 ; i
< map
->n
; ++i
) {
7086 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7087 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7088 res
= isl_map_union_disjoint(res
, r2
);
7090 res
= isl_map_union(res
, r2
);
7097 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7099 return (struct isl_set
*)
7100 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7103 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7105 return (struct isl_set
*)
7106 isl_map_compute_divs((struct isl_map
*)set
);
7109 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7112 struct isl_set
*set
;
7117 map
= isl_map_cow(map
);
7121 set
= (struct isl_set
*)map
;
7122 set
->dim
= isl_space_domain(set
->dim
);
7125 for (i
= 0; i
< map
->n
; ++i
) {
7126 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7130 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7131 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7138 /* Return the union of "map1" and "map2", where we assume for now that
7139 * "map1" and "map2" are disjoint. Note that the basic maps inside
7140 * "map1" or "map2" may not be disjoint from each other.
7141 * Also note that this function is also called from isl_map_union,
7142 * which takes care of handling the situation where "map1" and "map2"
7143 * may not be disjoint.
7145 * If one of the inputs is empty, we can simply return the other input.
7146 * Similarly, if one of the inputs is universal, then it is equal to the union.
7148 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7149 __isl_take isl_map
*map2
)
7153 struct isl_map
*map
= NULL
;
7159 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7160 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7161 "spaces don't match", goto error
);
7172 is_universe
= isl_map_plain_is_universe(map1
);
7173 if (is_universe
< 0)
7180 is_universe
= isl_map_plain_is_universe(map2
);
7181 if (is_universe
< 0)
7188 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7189 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7190 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7192 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7193 map1
->n
+ map2
->n
, flags
);
7196 for (i
= 0; i
< map1
->n
; ++i
) {
7197 map
= isl_map_add_basic_map(map
,
7198 isl_basic_map_copy(map1
->p
[i
]));
7202 for (i
= 0; i
< map2
->n
; ++i
) {
7203 map
= isl_map_add_basic_map(map
,
7204 isl_basic_map_copy(map2
->p
[i
]));
7218 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7219 * guaranteed to be disjoint by the caller.
7221 * Note that this functions is called from within isl_map_make_disjoint,
7222 * so we have to be careful not to touch the constraints of the inputs
7225 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7226 __isl_take isl_map
*map2
)
7228 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7231 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7232 * not be disjoint. The parameters are assumed to have been aligned.
7234 * We currently simply call map_union_disjoint, the internal operation
7235 * of which does not really depend on the inputs being disjoint.
7236 * If the result contains more than one basic map, then we clear
7237 * the disjoint flag since the result may contain basic maps from
7238 * both inputs and these are not guaranteed to be disjoint.
7240 * As a special case, if "map1" and "map2" are obviously equal,
7241 * then we simply return "map1".
7243 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7244 __isl_take isl_map
*map2
)
7251 equal
= isl_map_plain_is_equal(map1
, map2
);
7259 map1
= map_union_disjoint(map1
, map2
);
7263 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7271 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7274 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7275 __isl_take isl_map
*map2
)
7277 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7280 struct isl_set
*isl_set_union_disjoint(
7281 struct isl_set
*set1
, struct isl_set
*set2
)
7283 return (struct isl_set
*)
7284 isl_map_union_disjoint(
7285 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7288 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7290 return (struct isl_set
*)
7291 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7294 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7297 * "map" and "set" are assumed to be compatible and non-NULL.
7299 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7300 __isl_take isl_set
*set
,
7301 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7302 __isl_take isl_basic_set
*bset
))
7305 struct isl_map
*result
;
7308 if (isl_set_plain_is_universe(set
)) {
7313 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7314 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7315 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7317 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7318 map
->n
* set
->n
, flags
);
7319 for (i
= 0; result
&& i
< map
->n
; ++i
)
7320 for (j
= 0; j
< set
->n
; ++j
) {
7321 result
= isl_map_add_basic_map(result
,
7322 fn(isl_basic_map_copy(map
->p
[i
]),
7323 isl_basic_set_copy(set
->p
[j
])));
7333 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7334 __isl_take isl_set
*set
)
7339 if (!isl_map_compatible_range(map
, set
))
7340 isl_die(set
->ctx
, isl_error_invalid
,
7341 "incompatible spaces", goto error
);
7343 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7350 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7351 __isl_take isl_set
*set
)
7353 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7356 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7357 __isl_take isl_set
*set
)
7362 if (!isl_map_compatible_domain(map
, set
))
7363 isl_die(set
->ctx
, isl_error_invalid
,
7364 "incompatible spaces", goto error
);
7366 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7373 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7374 __isl_take isl_set
*set
)
7376 return isl_map_align_params_map_map_and(map
, set
,
7377 &map_intersect_domain
);
7380 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7381 __isl_take isl_map
*map2
)
7385 map1
= isl_map_reverse(map1
);
7386 map1
= isl_map_apply_range(map1
, map2
);
7387 return isl_map_reverse(map1
);
7394 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7395 __isl_take isl_map
*map2
)
7397 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7400 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7401 __isl_take isl_map
*map2
)
7403 isl_space
*dim_result
;
7404 struct isl_map
*result
;
7410 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7411 isl_space_copy(map2
->dim
));
7413 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7416 for (i
= 0; i
< map1
->n
; ++i
)
7417 for (j
= 0; j
< map2
->n
; ++j
) {
7418 result
= isl_map_add_basic_map(result
,
7419 isl_basic_map_apply_range(
7420 isl_basic_map_copy(map1
->p
[i
]),
7421 isl_basic_map_copy(map2
->p
[j
])));
7427 if (result
&& result
->n
<= 1)
7428 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7436 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7437 __isl_take isl_map
*map2
)
7439 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7443 * returns range - domain
7445 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7447 isl_space
*dims
, *target_dim
;
7448 struct isl_basic_set
*bset
;
7455 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7456 bmap
->dim
, isl_dim_out
),
7458 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7459 dim
= isl_basic_map_n_in(bmap
);
7460 nparam
= isl_basic_map_n_param(bmap
);
7461 bset
= isl_basic_set_from_basic_map(bmap
);
7462 bset
= isl_basic_set_cow(bset
);
7463 dims
= isl_basic_set_get_space(bset
);
7464 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7465 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7466 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7467 for (i
= 0; i
< dim
; ++i
) {
7468 int j
= isl_basic_map_alloc_equality(
7469 (struct isl_basic_map
*)bset
);
7471 bset
= isl_basic_set_free(bset
);
7474 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7475 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7476 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7477 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7479 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7480 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7483 isl_basic_map_free(bmap
);
7488 * returns range - domain
7490 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7494 struct isl_set
*result
;
7499 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7500 map
->dim
, isl_dim_out
),
7502 dim
= isl_map_get_space(map
);
7503 dim
= isl_space_domain(dim
);
7504 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7507 for (i
= 0; i
< map
->n
; ++i
)
7508 result
= isl_set_add_basic_set(result
,
7509 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7518 * returns [domain -> range] -> range - domain
7520 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7521 __isl_take isl_basic_map
*bmap
)
7525 isl_basic_map
*domain
;
7529 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7530 isl_die(bmap
->ctx
, isl_error_invalid
,
7531 "domain and range don't match", goto error
);
7533 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7534 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7536 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7537 domain
= isl_basic_map_universe(dim
);
7539 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7540 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7541 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7543 total
= isl_basic_map_total_dim(bmap
);
7545 for (i
= 0; i
< n
; ++i
) {
7546 k
= isl_basic_map_alloc_equality(bmap
);
7549 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7550 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7551 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7552 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7555 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7556 return isl_basic_map_finalize(bmap
);
7558 isl_basic_map_free(bmap
);
7563 * returns [domain -> range] -> range - domain
7565 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7568 isl_space
*domain_dim
;
7573 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7574 isl_die(map
->ctx
, isl_error_invalid
,
7575 "domain and range don't match", goto error
);
7577 map
= isl_map_cow(map
);
7581 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7582 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7583 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7586 for (i
= 0; i
< map
->n
; ++i
) {
7587 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7591 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7598 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7600 struct isl_basic_map
*bmap
;
7608 nparam
= dims
->nparam
;
7610 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7614 for (i
= 0; i
< dim
; ++i
) {
7615 int j
= isl_basic_map_alloc_equality(bmap
);
7618 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7619 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7620 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7622 return isl_basic_map_finalize(bmap
);
7624 isl_basic_map_free(bmap
);
7628 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7632 if (dim
->n_in
!= dim
->n_out
)
7633 isl_die(dim
->ctx
, isl_error_invalid
,
7634 "number of input and output dimensions needs to be "
7635 "the same", goto error
);
7636 return basic_map_identity(dim
);
7638 isl_space_free(dim
);
7642 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7644 if (!model
|| !model
->dim
)
7646 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7649 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7651 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7654 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7656 if (!model
|| !model
->dim
)
7658 return isl_map_identity(isl_space_copy(model
->dim
));
7661 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7663 if (!model
|| !model
->dim
)
7665 return isl_map_identity(isl_space_copy(model
->dim
));
7668 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7670 isl_space
*dim
= isl_set_get_space(set
);
7672 id
= isl_map_identity(isl_space_map_from_set(dim
));
7673 return isl_map_intersect_range(id
, set
);
7676 /* Construct a basic set with all set dimensions having only non-negative
7679 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7680 __isl_take isl_space
*space
)
7685 struct isl_basic_set
*bset
;
7689 nparam
= space
->nparam
;
7691 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7694 for (i
= 0; i
< dim
; ++i
) {
7695 int k
= isl_basic_set_alloc_inequality(bset
);
7698 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7699 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7703 isl_basic_set_free(bset
);
7707 /* Construct the half-space x_pos >= 0.
7709 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7713 isl_basic_set
*nonneg
;
7715 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7716 k
= isl_basic_set_alloc_inequality(nonneg
);
7719 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7720 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7722 return isl_basic_set_finalize(nonneg
);
7724 isl_basic_set_free(nonneg
);
7728 /* Construct the half-space x_pos <= -1.
7730 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7735 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7736 k
= isl_basic_set_alloc_inequality(neg
);
7739 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7740 isl_int_set_si(neg
->ineq
[k
][0], -1);
7741 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7743 return isl_basic_set_finalize(neg
);
7745 isl_basic_set_free(neg
);
7749 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7750 enum isl_dim_type type
, unsigned first
, unsigned n
)
7753 isl_basic_set
*nonneg
;
7761 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7763 for (i
= 0; i
< n
; ++i
) {
7764 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7765 pos(set
->dim
, type
) + first
+ i
);
7766 neg
= neg_halfspace(isl_set_get_space(set
),
7767 pos(set
->dim
, type
) + first
+ i
);
7769 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7778 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7779 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7786 if (isl_set_plain_is_empty(set
)) {
7791 return fn(set
, signs
, user
);
7794 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7796 half
= isl_set_intersect(half
, isl_set_copy(set
));
7797 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7801 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7803 half
= isl_set_intersect(half
, set
);
7804 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7810 /* Call "fn" on the intersections of "set" with each of the orthants
7811 * (except for obviously empty intersections). The orthant is identified
7812 * by the signs array, with each entry having value 1 or -1 according
7813 * to the sign of the corresponding variable.
7815 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7816 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7826 if (isl_set_plain_is_empty(set
))
7829 nparam
= isl_set_dim(set
, isl_dim_param
);
7830 nvar
= isl_set_dim(set
, isl_dim_set
);
7832 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7834 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7842 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7844 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7847 int isl_basic_map_is_subset(
7848 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7851 struct isl_map
*map1
;
7852 struct isl_map
*map2
;
7854 if (!bmap1
|| !bmap2
)
7857 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7858 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7860 is_subset
= isl_map_is_subset(map1
, map2
);
7868 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7869 __isl_keep isl_basic_set
*bset2
)
7871 return isl_basic_map_is_subset(bset1
, bset2
);
7874 int isl_basic_map_is_equal(
7875 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7879 if (!bmap1
|| !bmap2
)
7881 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7884 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7888 int isl_basic_set_is_equal(
7889 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7891 return isl_basic_map_is_equal(
7892 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7895 int isl_map_is_empty(struct isl_map
*map
)
7902 for (i
= 0; i
< map
->n
; ++i
) {
7903 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7912 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7914 return map
? map
->n
== 0 : -1;
7917 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7919 return isl_map_plain_is_empty(map
);
7922 int isl_set_plain_is_empty(struct isl_set
*set
)
7924 return set
? set
->n
== 0 : -1;
7927 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7929 return isl_set_plain_is_empty(set
);
7932 int isl_set_is_empty(struct isl_set
*set
)
7934 return isl_map_is_empty((struct isl_map
*)set
);
7937 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7942 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7945 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7950 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7953 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7959 is_subset
= isl_map_is_subset(map1
, map2
);
7962 is_subset
= isl_map_is_subset(map2
, map1
);
7966 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7968 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7971 int isl_basic_map_is_strict_subset(
7972 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7976 if (!bmap1
|| !bmap2
)
7978 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7981 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7982 if (is_subset
== -1)
7987 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7993 is_subset
= isl_map_is_subset(map1
, map2
);
7996 is_subset
= isl_map_is_subset(map2
, map1
);
7997 if (is_subset
== -1)
8002 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8004 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
8007 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
8011 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8014 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
8018 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
8021 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8028 for (i
= 0; i
< map
->n
; ++i
) {
8029 int r
= isl_basic_map_is_universe(map
->p
[i
]);
8037 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8039 return isl_map_plain_is_universe((isl_map
*) set
);
8042 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
8044 return isl_set_plain_is_universe(set
);
8047 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
8049 struct isl_basic_set
*bset
= NULL
;
8050 struct isl_vec
*sample
= NULL
;
8057 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8060 if (isl_basic_map_is_universe(bmap
))
8063 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8064 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8065 copy
= isl_basic_map_remove_redundancies(copy
);
8066 empty
= isl_basic_map_plain_is_empty(copy
);
8067 isl_basic_map_free(copy
);
8071 total
= 1 + isl_basic_map_total_dim(bmap
);
8072 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
8073 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
8079 isl_vec_free(bmap
->sample
);
8080 bmap
->sample
= NULL
;
8081 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8084 sample
= isl_basic_set_sample_vec(bset
);
8087 empty
= sample
->size
== 0;
8088 isl_vec_free(bmap
->sample
);
8089 bmap
->sample
= sample
;
8091 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8096 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8100 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8103 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
8105 return isl_basic_map_plain_is_empty(bmap
);
8108 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8112 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8115 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8117 return isl_basic_set_plain_is_empty(bset
);
8120 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8122 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8125 struct isl_map
*isl_basic_map_union(
8126 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8128 struct isl_map
*map
;
8129 if (!bmap1
|| !bmap2
)
8132 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8134 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8137 map
= isl_map_add_basic_map(map
, bmap1
);
8138 map
= isl_map_add_basic_map(map
, bmap2
);
8141 isl_basic_map_free(bmap1
);
8142 isl_basic_map_free(bmap2
);
8146 struct isl_set
*isl_basic_set_union(
8147 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8149 return (struct isl_set
*)isl_basic_map_union(
8150 (struct isl_basic_map
*)bset1
,
8151 (struct isl_basic_map
*)bset2
);
8154 /* Order divs such that any div only depends on previous divs */
8155 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8163 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8165 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8167 if (isl_int_is_zero(bmap
->div
[i
][0]))
8169 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8173 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8179 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8181 return (struct isl_basic_set
*)
8182 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8185 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8192 for (i
= 0; i
< map
->n
; ++i
) {
8193 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8204 /* Apply the expansion computed by isl_merge_divs.
8205 * The expansion itself is given by "exp" while the resulting
8206 * list of divs is given by "div".
8208 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8209 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8214 bset
= isl_basic_set_cow(bset
);
8218 if (div
->n_row
< bset
->n_div
)
8219 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8220 "not an expansion", goto error
);
8222 n_div
= bset
->n_div
;
8223 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8224 div
->n_row
- n_div
, 0,
8225 2 * (div
->n_row
- n_div
));
8227 for (i
= n_div
; i
< div
->n_row
; ++i
)
8228 if (isl_basic_set_alloc_div(bset
) < 0)
8232 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8233 if (j
>= 0 && exp
[j
] == i
) {
8235 isl_basic_map_swap_div(bset
, i
, j
);
8238 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8239 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8247 isl_basic_set_free(bset
);
8252 /* Look for a div in dst that corresponds to the div "div" in src.
8253 * The divs before "div" in src and dst are assumed to be the same.
8255 * Returns -1 if no corresponding div was found and the position
8256 * of the corresponding div in dst otherwise.
8258 static int find_div(struct isl_basic_map
*dst
,
8259 struct isl_basic_map
*src
, unsigned div
)
8263 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8265 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8266 for (i
= div
; i
< dst
->n_div
; ++i
)
8267 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8268 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8269 dst
->n_div
- div
) == -1)
8274 /* Align the divs of "dst" to those of "src", adding divs from "src"
8275 * if needed. That is, make sure that the first src->n_div divs
8276 * of the result are equal to those of src.
8278 * The result is not finalized as by design it will have redundant
8279 * divs if any divs from "src" were copied.
8281 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8282 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8285 int known
, extended
;
8289 return isl_basic_map_free(dst
);
8291 if (src
->n_div
== 0)
8294 known
= isl_basic_map_divs_known(src
);
8296 return isl_basic_map_free(dst
);
8298 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8299 "some src divs are unknown",
8300 return isl_basic_map_free(dst
));
8302 src
= isl_basic_map_order_divs(src
);
8305 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8306 for (i
= 0; i
< src
->n_div
; ++i
) {
8307 int j
= find_div(dst
, src
, i
);
8310 int extra
= src
->n_div
- i
;
8311 dst
= isl_basic_map_cow(dst
);
8314 dst
= isl_basic_map_extend_space(dst
,
8315 isl_space_copy(dst
->dim
),
8316 extra
, 0, 2 * extra
);
8319 j
= isl_basic_map_alloc_div(dst
);
8321 return isl_basic_map_free(dst
);
8322 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8323 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8324 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8325 return isl_basic_map_free(dst
);
8328 isl_basic_map_swap_div(dst
, i
, j
);
8333 struct isl_basic_set
*isl_basic_set_align_divs(
8334 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8336 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8337 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8340 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8348 map
= isl_map_compute_divs(map
);
8349 map
= isl_map_cow(map
);
8353 for (i
= 1; i
< map
->n
; ++i
)
8354 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8355 for (i
= 1; i
< map
->n
; ++i
) {
8356 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8358 return isl_map_free(map
);
8361 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8365 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8367 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8370 /* Align the divs of the basic sets in "set" to those
8371 * of the basic sets in "list", as well as to the other basic sets in "set".
8372 * The elements in "list" are assumed to have known divs.
8374 __isl_give isl_set
*isl_set_align_divs_to_basic_set_list(
8375 __isl_take isl_set
*set
, __isl_keep isl_basic_set_list
*list
)
8379 set
= isl_set_compute_divs(set
);
8380 set
= isl_set_cow(set
);
8382 return isl_set_free(set
);
8386 n
= isl_basic_set_list_n_basic_set(list
);
8387 for (i
= 0; i
< n
; ++i
) {
8388 isl_basic_set
*bset
;
8390 bset
= isl_basic_set_list_get_basic_set(list
, i
);
8391 set
->p
[0] = isl_basic_set_align_divs(set
->p
[0], bset
);
8392 isl_basic_set_free(bset
);
8395 return isl_set_free(set
);
8397 return isl_set_align_divs(set
);
8400 /* Align the divs of each element of "list" to those of "bset".
8401 * Both "bset" and the elements of "list" are assumed to have known divs.
8403 __isl_give isl_basic_set_list
*isl_basic_set_list_align_divs_to_basic_set(
8404 __isl_take isl_basic_set_list
*list
, __isl_keep isl_basic_set
*bset
)
8409 return isl_basic_set_list_free(list
);
8411 n
= isl_basic_set_list_n_basic_set(list
);
8412 for (i
= 0; i
< n
; ++i
) {
8413 isl_basic_set
*bset_i
;
8415 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
8416 bset_i
= isl_basic_set_align_divs(bset_i
, bset
);
8417 list
= isl_basic_set_list_set_basic_set(list
, i
, bset_i
);
8423 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8424 __isl_take isl_map
*map
)
8428 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8429 map
= isl_map_intersect_domain(map
, set
);
8430 set
= isl_map_range(map
);
8438 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8439 __isl_take isl_map
*map
)
8441 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8444 /* There is no need to cow as removing empty parts doesn't change
8445 * the meaning of the set.
8447 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8454 for (i
= map
->n
- 1; i
>= 0; --i
)
8455 remove_if_empty(map
, i
);
8460 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8462 return (struct isl_set
*)
8463 isl_map_remove_empty_parts((struct isl_map
*)set
);
8466 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8468 struct isl_basic_map
*bmap
;
8469 if (!map
|| map
->n
== 0)
8471 bmap
= map
->p
[map
->n
-1];
8472 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8473 return isl_basic_map_copy(bmap
);
8476 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8478 return (struct isl_basic_set
*)
8479 isl_map_copy_basic_map((struct isl_map
*)set
);
8482 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8483 __isl_keep isl_basic_map
*bmap
)
8489 for (i
= map
->n
-1; i
>= 0; --i
) {
8490 if (map
->p
[i
] != bmap
)
8492 map
= isl_map_cow(map
);
8495 isl_basic_map_free(map
->p
[i
]);
8496 if (i
!= map
->n
-1) {
8497 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8498 map
->p
[i
] = map
->p
[map
->n
-1];
8509 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8510 struct isl_basic_set
*bset
)
8512 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8513 (struct isl_basic_map
*)bset
);
8516 /* Given two basic sets bset1 and bset2, compute the maximal difference
8517 * between the values of dimension pos in bset1 and those in bset2
8518 * for any common value of the parameters and dimensions preceding pos.
8520 static enum isl_lp_result
basic_set_maximal_difference_at(
8521 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8522 int pos
, isl_int
*opt
)
8525 struct isl_basic_map
*bmap1
= NULL
;
8526 struct isl_basic_map
*bmap2
= NULL
;
8527 struct isl_ctx
*ctx
;
8528 struct isl_vec
*obj
;
8531 unsigned dim1
, dim2
;
8532 enum isl_lp_result res
;
8534 if (!bset1
|| !bset2
)
8535 return isl_lp_error
;
8537 nparam
= isl_basic_set_n_param(bset1
);
8538 dim1
= isl_basic_set_n_dim(bset1
);
8539 dim2
= isl_basic_set_n_dim(bset2
);
8540 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8541 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8542 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8543 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8544 if (!bmap1
|| !bmap2
)
8546 bmap1
= isl_basic_map_cow(bmap1
);
8547 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8548 pos
, (dim1
- pos
) + (dim2
- pos
),
8549 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8550 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8553 total
= isl_basic_map_total_dim(bmap1
);
8555 obj
= isl_vec_alloc(ctx
, 1 + total
);
8558 isl_seq_clr(obj
->block
.data
, 1 + total
);
8559 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8560 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8561 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8563 isl_basic_map_free(bmap1
);
8567 isl_basic_map_free(bmap2
);
8569 isl_basic_map_free(bmap1
);
8570 return isl_lp_error
;
8573 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8574 * for any common value of the parameters and dimensions preceding pos
8575 * in both basic sets, the values of dimension pos in bset1 are
8576 * smaller or larger than those in bset2.
8579 * 1 if bset1 follows bset2
8580 * -1 if bset1 precedes bset2
8581 * 0 if bset1 and bset2 are incomparable
8582 * -2 if some error occurred.
8584 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8585 struct isl_basic_set
*bset2
, int pos
)
8588 enum isl_lp_result res
;
8593 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8595 if (res
== isl_lp_empty
)
8597 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8598 res
== isl_lp_unbounded
)
8600 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8609 /* Given two basic sets bset1 and bset2, check whether
8610 * for any common value of the parameters and dimensions preceding pos
8611 * there is a value of dimension pos in bset1 that is larger
8612 * than a value of the same dimension in bset2.
8615 * 1 if there exists such a pair
8616 * 0 if there is no such pair, but there is a pair of equal values
8618 * -2 if some error occurred.
8620 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8621 __isl_keep isl_basic_set
*bset2
, int pos
)
8624 enum isl_lp_result res
;
8629 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8631 if (res
== isl_lp_empty
)
8633 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8634 res
== isl_lp_unbounded
)
8636 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8638 else if (res
== isl_lp_ok
)
8647 /* Given two sets set1 and set2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * there is a value of dimension pos in set1 that is larger
8650 * than a value of the same dimension in set2.
8653 * 1 if there exists such a pair
8654 * 0 if there is no such pair, but there is a pair of equal values
8656 * -2 if some error occurred.
8658 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8659 __isl_keep isl_set
*set2
, int pos
)
8667 for (i
= 0; i
< set1
->n
; ++i
)
8668 for (j
= 0; j
< set2
->n
; ++j
) {
8670 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8671 if (f
== 1 || f
== -2)
8680 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8681 unsigned pos
, isl_int
*val
)
8689 total
= isl_basic_map_total_dim(bmap
);
8690 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8691 for (; d
+1 > pos
; --d
)
8692 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8696 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8698 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8700 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8703 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8709 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8710 unsigned pos
, isl_int
*val
)
8722 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8725 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8726 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8727 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8728 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8732 isl_int_set(*val
, v
);
8738 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8739 unsigned pos
, isl_int
*val
)
8741 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8745 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8748 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8751 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8752 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8754 if (pos
>= isl_basic_map_dim(bmap
, type
))
8756 return isl_basic_map_plain_has_fixed_var(bmap
,
8757 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8760 /* If "bmap" obviously lies on a hyperplane where the given dimension
8761 * has a fixed value, then return that value.
8762 * Otherwise return NaN.
8764 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8765 __isl_keep isl_basic_map
*bmap
,
8766 enum isl_dim_type type
, unsigned pos
)
8774 ctx
= isl_basic_map_get_ctx(bmap
);
8775 v
= isl_val_alloc(ctx
);
8778 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8780 return isl_val_free(v
);
8782 isl_int_set_si(v
->d
, 1);
8786 return isl_val_nan(ctx
);
8789 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8790 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8792 if (pos
>= isl_map_dim(map
, type
))
8794 return isl_map_plain_has_fixed_var(map
,
8795 map_offset(map
, type
) - 1 + pos
, val
);
8798 /* If "map" obviously lies on a hyperplane where the given dimension
8799 * has a fixed value, then return that value.
8800 * Otherwise return NaN.
8802 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8803 enum isl_dim_type type
, unsigned pos
)
8811 ctx
= isl_map_get_ctx(map
);
8812 v
= isl_val_alloc(ctx
);
8815 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8817 return isl_val_free(v
);
8819 isl_int_set_si(v
->d
, 1);
8823 return isl_val_nan(ctx
);
8826 /* If "set" obviously lies on a hyperplane where the given dimension
8827 * has a fixed value, then return that value.
8828 * Otherwise return NaN.
8830 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8831 enum isl_dim_type type
, unsigned pos
)
8833 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8836 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8837 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8839 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8842 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8843 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8845 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8848 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8849 * then return this fixed value in *val.
8851 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8852 unsigned dim
, isl_int
*val
)
8854 return isl_basic_set_plain_has_fixed_var(bset
,
8855 isl_basic_set_n_param(bset
) + dim
, val
);
8858 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8859 * then return this fixed value in *val.
8861 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8862 unsigned dim
, isl_int
*val
)
8864 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8867 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8868 unsigned dim
, isl_int
*val
)
8870 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8873 /* Check if input variable in has fixed value and if so and if val is not NULL,
8874 * then return this fixed value in *val.
8876 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8877 unsigned in
, isl_int
*val
)
8879 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8882 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8883 * and if val is not NULL, then return this lower bound in *val.
8885 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8886 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8888 int i
, i_eq
= -1, i_ineq
= -1;
8895 total
= isl_basic_set_total_dim(bset
);
8896 nparam
= isl_basic_set_n_param(bset
);
8897 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8898 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8904 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8905 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8907 if (i_eq
!= -1 || i_ineq
!= -1)
8911 if (i_eq
== -1 && i_ineq
== -1)
8913 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8914 /* The coefficient should always be one due to normalization. */
8915 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8917 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8919 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8920 total
- nparam
- dim
- 1) != -1)
8923 isl_int_neg(*val
, c
[0]);
8927 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8928 unsigned dim
, isl_int
*val
)
8940 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8944 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8946 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8947 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8949 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8953 isl_int_set(*val
, v
);
8959 /* uset_gist depends on constraints without existentially quantified
8960 * variables sorting first.
8962 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8964 isl_int
**c1
= (isl_int
**) p1
;
8965 isl_int
**c2
= (isl_int
**) p2
;
8967 unsigned size
= *(unsigned *) arg
;
8969 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8970 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8975 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8978 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8979 struct isl_basic_map
*bmap
)
8985 if (bmap
->n_ineq
== 0)
8987 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8989 total
= isl_basic_map_total_dim(bmap
);
8990 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8991 &sort_constraint_cmp
, &total
) < 0)
8992 return isl_basic_map_free(bmap
);
8996 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8997 __isl_take isl_basic_set
*bset
)
8999 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9000 (struct isl_basic_map
*)bset
);
9003 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9007 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9009 bmap
= isl_basic_map_remove_redundancies(bmap
);
9010 bmap
= isl_basic_map_sort_constraints(bmap
);
9012 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9016 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9018 return (struct isl_basic_set
*)isl_basic_map_normalize(
9019 (struct isl_basic_map
*)bset
);
9022 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9023 const __isl_keep isl_basic_map
*bmap2
)
9030 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9031 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9032 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9033 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9034 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9035 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9036 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9037 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9038 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9039 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9040 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9042 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9044 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9046 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9047 return bmap1
->n_eq
- bmap2
->n_eq
;
9048 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9049 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9050 if (bmap1
->n_div
!= bmap2
->n_div
)
9051 return bmap1
->n_div
- bmap2
->n_div
;
9052 total
= isl_basic_map_total_dim(bmap1
);
9053 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9054 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9058 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9059 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9063 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9064 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9071 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9072 const __isl_keep isl_basic_set
*bset2
)
9074 return isl_basic_map_plain_cmp(bset1
, bset2
);
9077 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9083 if (set1
->n
!= set2
->n
)
9084 return set1
->n
- set2
->n
;
9086 for (i
= 0; i
< set1
->n
; ++i
) {
9087 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9095 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9096 __isl_keep isl_basic_map
*bmap2
)
9098 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9101 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9102 __isl_keep isl_basic_set
*bset2
)
9104 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9105 (isl_basic_map
*)bset2
);
9108 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9110 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9111 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9113 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9116 /* Sort the basic maps of "map" and remove duplicate basic maps.
9118 * While removing basic maps, we make sure that the basic maps remain
9119 * sorted because isl_map_normalize expects the basic maps of the result
9122 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9126 map
= isl_map_remove_empty_parts(map
);
9129 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9130 for (i
= map
->n
- 1; i
>= 1; --i
) {
9131 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9133 isl_basic_map_free(map
->p
[i
-1]);
9134 for (j
= i
; j
< map
->n
; ++j
)
9135 map
->p
[j
- 1] = map
->p
[j
];
9142 /* Remove obvious duplicates among the basic maps of "map".
9144 * Unlike isl_map_normalize, this function does not remove redundant
9145 * constraints and only removes duplicates that have exactly the same
9146 * constraints in the input. It does sort the constraints and
9147 * the basic maps to ease the detection of duplicates.
9149 * If "map" has already been normalized or if the basic maps are
9150 * disjoint, then there can be no duplicates.
9152 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9155 isl_basic_map
*bmap
;
9161 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9163 for (i
= 0; i
< map
->n
; ++i
) {
9164 bmap
= isl_basic_map_copy(map
->p
[i
]);
9165 bmap
= isl_basic_map_sort_constraints(bmap
);
9167 return isl_map_free(map
);
9168 isl_basic_map_free(map
->p
[i
]);
9172 map
= sort_and_remove_duplicates(map
);
9176 /* We normalize in place, but if anything goes wrong we need
9177 * to return NULL, so we need to make sure we don't change the
9178 * meaning of any possible other copies of map.
9180 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9183 struct isl_basic_map
*bmap
;
9187 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9189 for (i
= 0; i
< map
->n
; ++i
) {
9190 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9193 isl_basic_map_free(map
->p
[i
]);
9197 map
= sort_and_remove_duplicates(map
);
9199 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9206 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9208 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9211 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9221 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9224 map1
= isl_map_copy(map1
);
9225 map2
= isl_map_copy(map2
);
9226 map1
= isl_map_normalize(map1
);
9227 map2
= isl_map_normalize(map2
);
9230 equal
= map1
->n
== map2
->n
;
9231 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9232 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9245 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9247 return isl_map_plain_is_equal(map1
, map2
);
9250 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9252 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9253 (struct isl_map
*)set2
);
9256 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9258 return isl_set_plain_is_equal(set1
, set2
);
9261 /* Return an interval that ranges from min to max (inclusive)
9263 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9264 isl_int min
, isl_int max
)
9267 struct isl_basic_set
*bset
= NULL
;
9269 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9273 k
= isl_basic_set_alloc_inequality(bset
);
9276 isl_int_set_si(bset
->ineq
[k
][1], 1);
9277 isl_int_neg(bset
->ineq
[k
][0], min
);
9279 k
= isl_basic_set_alloc_inequality(bset
);
9282 isl_int_set_si(bset
->ineq
[k
][1], -1);
9283 isl_int_set(bset
->ineq
[k
][0], max
);
9287 isl_basic_set_free(bset
);
9291 /* Return the basic sets in "set" as a list.
9293 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9294 __isl_keep isl_set
*set
)
9298 isl_basic_set_list
*list
;
9302 ctx
= isl_set_get_ctx(set
);
9303 list
= isl_basic_set_list_alloc(ctx
, set
->n
);
9305 for (i
= 0; i
< set
->n
; ++i
) {
9306 isl_basic_set
*bset
;
9308 bset
= isl_basic_set_copy(set
->p
[i
]);
9309 list
= isl_basic_set_list_add(list
, bset
);
9315 /* Return the intersection of the elements in the non-empty list "list".
9316 * All elements are assumed to live in the same space.
9318 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9319 __isl_take
struct isl_basic_set_list
*list
)
9322 isl_basic_set
*bset
;
9326 n
= isl_basic_set_list_n_basic_set(list
);
9328 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9329 "expecting non-empty list", goto error
);
9331 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9332 for (i
= 1; i
< n
; ++i
) {
9333 isl_basic_set
*bset_i
;
9335 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
9336 bset
= isl_basic_set_intersect(bset
, bset_i
);
9339 isl_basic_set_list_free(list
);
9342 isl_basic_set_list_free(list
);
9346 /* Return the Cartesian product of the basic sets in list (in the given order).
9348 __isl_give isl_basic_set
*isl_basic_set_list_product(
9349 __isl_take
struct isl_basic_set_list
*list
)
9357 struct isl_basic_set
*product
= NULL
;
9361 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9362 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9363 nparam
= isl_basic_set_n_param(list
->p
[0]);
9364 dim
= isl_basic_set_n_dim(list
->p
[0]);
9365 extra
= list
->p
[0]->n_div
;
9366 n_eq
= list
->p
[0]->n_eq
;
9367 n_ineq
= list
->p
[0]->n_ineq
;
9368 for (i
= 1; i
< list
->n
; ++i
) {
9369 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9370 isl_assert(list
->ctx
,
9371 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9372 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9373 extra
+= list
->p
[i
]->n_div
;
9374 n_eq
+= list
->p
[i
]->n_eq
;
9375 n_ineq
+= list
->p
[i
]->n_ineq
;
9377 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9382 for (i
= 0; i
< list
->n
; ++i
) {
9383 isl_basic_set_add_constraints(product
,
9384 isl_basic_set_copy(list
->p
[i
]), dim
);
9385 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9387 isl_basic_set_list_free(list
);
9390 isl_basic_set_free(product
);
9391 isl_basic_set_list_free(list
);
9395 struct isl_basic_map
*isl_basic_map_product(
9396 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9398 isl_space
*dim_result
= NULL
;
9399 struct isl_basic_map
*bmap
;
9400 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9401 struct isl_dim_map
*dim_map1
, *dim_map2
;
9403 if (!bmap1
|| !bmap2
)
9406 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9407 bmap2
->dim
, isl_dim_param
), goto error
);
9408 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9409 isl_space_copy(bmap2
->dim
));
9411 in1
= isl_basic_map_n_in(bmap1
);
9412 in2
= isl_basic_map_n_in(bmap2
);
9413 out1
= isl_basic_map_n_out(bmap1
);
9414 out2
= isl_basic_map_n_out(bmap2
);
9415 nparam
= isl_basic_map_n_param(bmap1
);
9417 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9418 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9419 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9420 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9421 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9422 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9423 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9424 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9425 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9426 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9427 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9429 bmap
= isl_basic_map_alloc_space(dim_result
,
9430 bmap1
->n_div
+ bmap2
->n_div
,
9431 bmap1
->n_eq
+ bmap2
->n_eq
,
9432 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9433 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9434 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9435 bmap
= isl_basic_map_simplify(bmap
);
9436 return isl_basic_map_finalize(bmap
);
9438 isl_basic_map_free(bmap1
);
9439 isl_basic_map_free(bmap2
);
9443 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9444 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9446 isl_basic_map
*prod
;
9448 prod
= isl_basic_map_product(bmap1
, bmap2
);
9449 prod
= isl_basic_map_flatten(prod
);
9453 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9454 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9456 return isl_basic_map_flat_range_product(bset1
, bset2
);
9459 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9460 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9462 isl_space
*space_result
= NULL
;
9463 isl_basic_map
*bmap
;
9464 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9465 struct isl_dim_map
*dim_map1
, *dim_map2
;
9467 if (!bmap1
|| !bmap2
)
9470 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9471 isl_space_copy(bmap2
->dim
));
9473 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9474 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9475 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9476 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9478 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9479 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9480 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9481 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9482 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9483 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9484 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9485 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9486 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9487 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9488 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9490 bmap
= isl_basic_map_alloc_space(space_result
,
9491 bmap1
->n_div
+ bmap2
->n_div
,
9492 bmap1
->n_eq
+ bmap2
->n_eq
,
9493 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9494 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9495 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9496 bmap
= isl_basic_map_simplify(bmap
);
9497 return isl_basic_map_finalize(bmap
);
9499 isl_basic_map_free(bmap1
);
9500 isl_basic_map_free(bmap2
);
9504 __isl_give isl_basic_map
*isl_basic_map_range_product(
9505 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9507 isl_space
*dim_result
= NULL
;
9508 isl_basic_map
*bmap
;
9509 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9510 struct isl_dim_map
*dim_map1
, *dim_map2
;
9512 if (!bmap1
|| !bmap2
)
9515 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9516 bmap2
->dim
, isl_dim_param
))
9517 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9518 "parameters don't match", goto error
);
9520 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9521 isl_space_copy(bmap2
->dim
));
9523 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9524 out1
= isl_basic_map_n_out(bmap1
);
9525 out2
= isl_basic_map_n_out(bmap2
);
9526 nparam
= isl_basic_map_n_param(bmap1
);
9528 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9529 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9530 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9531 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9532 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9533 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9534 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9535 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9536 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9537 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9538 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9540 bmap
= isl_basic_map_alloc_space(dim_result
,
9541 bmap1
->n_div
+ bmap2
->n_div
,
9542 bmap1
->n_eq
+ bmap2
->n_eq
,
9543 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9544 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9545 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9546 bmap
= isl_basic_map_simplify(bmap
);
9547 return isl_basic_map_finalize(bmap
);
9549 isl_basic_map_free(bmap1
);
9550 isl_basic_map_free(bmap2
);
9554 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9555 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9557 isl_basic_map
*prod
;
9559 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9560 prod
= isl_basic_map_flatten_range(prod
);
9564 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9565 * and collect the results.
9566 * The result live in the space obtained by calling "space_product"
9567 * on the spaces of "map1" and "map2".
9568 * If "remove_duplicates" is set then the result may contain duplicates
9569 * (even if the inputs do not) and so we try and remove the obvious
9572 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9573 __isl_take isl_map
*map2
,
9574 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9575 __isl_take isl_space
*right
),
9576 __isl_give isl_basic_map
*(*basic_map_product
)(
9577 __isl_take isl_basic_map
*left
,
9578 __isl_take isl_basic_map
*right
),
9579 int remove_duplicates
)
9582 struct isl_map
*result
;
9588 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9589 map2
->dim
, isl_dim_param
), goto error
);
9591 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9592 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9593 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9595 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9596 isl_space_copy(map2
->dim
)),
9597 map1
->n
* map2
->n
, flags
);
9600 for (i
= 0; i
< map1
->n
; ++i
)
9601 for (j
= 0; j
< map2
->n
; ++j
) {
9602 struct isl_basic_map
*part
;
9603 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9604 isl_basic_map_copy(map2
->p
[j
]));
9605 if (isl_basic_map_is_empty(part
))
9606 isl_basic_map_free(part
);
9608 result
= isl_map_add_basic_map(result
, part
);
9612 if (remove_duplicates
)
9613 result
= isl_map_remove_obvious_duplicates(result
);
9623 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9625 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9626 __isl_take isl_map
*map2
)
9628 return map_product(map1
, map2
, &isl_space_product
,
9629 &isl_basic_map_product
, 0);
9632 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9633 __isl_take isl_map
*map2
)
9635 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9638 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9640 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9641 __isl_take isl_map
*map2
)
9645 prod
= isl_map_product(map1
, map2
);
9646 prod
= isl_map_flatten(prod
);
9650 /* Given two set A and B, construct its Cartesian product A x B.
9652 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9654 return isl_map_range_product(set1
, set2
);
9657 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9658 __isl_take isl_set
*set2
)
9660 return isl_map_flat_range_product(set1
, set2
);
9663 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9665 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9666 __isl_take isl_map
*map2
)
9668 return map_product(map1
, map2
, &isl_space_domain_product
,
9669 &isl_basic_map_domain_product
, 1);
9672 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9674 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9675 __isl_take isl_map
*map2
)
9677 return map_product(map1
, map2
, &isl_space_range_product
,
9678 &isl_basic_map_range_product
, 1);
9681 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9682 __isl_take isl_map
*map2
)
9684 return isl_map_align_params_map_map_and(map1
, map2
,
9685 &map_domain_product_aligned
);
9688 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9689 __isl_take isl_map
*map2
)
9691 return isl_map_align_params_map_map_and(map1
, map2
,
9692 &map_range_product_aligned
);
9695 /* Given a map A -> [B -> C], extract the map A -> B.
9697 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9704 if (!isl_space_range_is_wrapping(map
->dim
))
9705 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9706 "range is not a product", return isl_map_free(map
));
9708 space
= isl_map_get_space(map
);
9709 total
= isl_space_dim(space
, isl_dim_out
);
9710 space
= isl_space_range_factor_domain(space
);
9711 keep
= isl_space_dim(space
, isl_dim_out
);
9712 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9713 map
= isl_map_reset_space(map
, space
);
9718 /* Given a map A -> [B -> C], extract the map A -> C.
9720 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9727 if (!isl_space_range_is_wrapping(map
->dim
))
9728 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9729 "range is not a product", return isl_map_free(map
));
9731 space
= isl_map_get_space(map
);
9732 total
= isl_space_dim(space
, isl_dim_out
);
9733 space
= isl_space_range_factor_range(space
);
9734 keep
= isl_space_dim(space
, isl_dim_out
);
9735 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9736 map
= isl_map_reset_space(map
, space
);
9741 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9743 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9744 __isl_take isl_map
*map2
)
9748 prod
= isl_map_domain_product(map1
, map2
);
9749 prod
= isl_map_flatten_domain(prod
);
9753 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9755 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9756 __isl_take isl_map
*map2
)
9760 prod
= isl_map_range_product(map1
, map2
);
9761 prod
= isl_map_flatten_range(prod
);
9765 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9768 uint32_t hash
= isl_hash_init();
9773 bmap
= isl_basic_map_copy(bmap
);
9774 bmap
= isl_basic_map_normalize(bmap
);
9777 total
= isl_basic_map_total_dim(bmap
);
9778 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9779 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9781 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9782 isl_hash_hash(hash
, c_hash
);
9784 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9785 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9787 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9788 isl_hash_hash(hash
, c_hash
);
9790 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9791 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9793 if (isl_int_is_zero(bmap
->div
[i
][0]))
9795 isl_hash_byte(hash
, i
& 0xFF);
9796 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9797 isl_hash_hash(hash
, c_hash
);
9799 isl_basic_map_free(bmap
);
9803 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9805 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9808 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9815 map
= isl_map_copy(map
);
9816 map
= isl_map_normalize(map
);
9820 hash
= isl_hash_init();
9821 for (i
= 0; i
< map
->n
; ++i
) {
9823 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9824 isl_hash_hash(hash
, bmap_hash
);
9832 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9834 return isl_map_get_hash((isl_map
*)set
);
9837 /* Check if the value for dimension dim is completely determined
9838 * by the values of the other parameters and variables.
9839 * That is, check if dimension dim is involved in an equality.
9841 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9848 nparam
= isl_basic_set_n_param(bset
);
9849 for (i
= 0; i
< bset
->n_eq
; ++i
)
9850 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9855 /* Check if the value for dimension dim is completely determined
9856 * by the values of the other parameters and variables.
9857 * That is, check if dimension dim is involved in an equality
9858 * for each of the subsets.
9860 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9866 for (i
= 0; i
< set
->n
; ++i
) {
9868 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9875 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9877 return set
? set
->n
: 0;
9880 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9881 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9888 for (i
= 0; i
< map
->n
; ++i
)
9889 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9895 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9896 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9903 for (i
= 0; i
< set
->n
; ++i
)
9904 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9910 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9917 bset
= isl_basic_set_cow(bset
);
9921 dim
= isl_basic_set_get_space(bset
);
9922 dim
= isl_space_lift(dim
, bset
->n_div
);
9925 isl_space_free(bset
->dim
);
9927 bset
->extra
-= bset
->n_div
;
9930 bset
= isl_basic_set_finalize(bset
);
9934 isl_basic_set_free(bset
);
9938 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9944 set
= isl_set_align_divs(set
);
9949 set
= isl_set_cow(set
);
9953 n_div
= set
->p
[0]->n_div
;
9954 dim
= isl_set_get_space(set
);
9955 dim
= isl_space_lift(dim
, n_div
);
9958 isl_space_free(set
->dim
);
9961 for (i
= 0; i
< set
->n
; ++i
) {
9962 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9973 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9976 struct isl_basic_map
*bmap
;
9983 set
= isl_set_align_divs(set
);
9988 dim
= isl_set_get_space(set
);
9989 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9991 return isl_map_identity(isl_space_map_from_set(dim
));
9994 n_div
= set
->p
[0]->n_div
;
9995 dim
= isl_space_map_from_set(dim
);
9996 n_param
= isl_space_dim(dim
, isl_dim_param
);
9997 n_set
= isl_space_dim(dim
, isl_dim_in
);
9998 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9999 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10000 for (i
= 0; i
< n_set
; ++i
)
10001 bmap
= var_equal(bmap
, i
);
10003 total
= n_param
+ n_set
+ n_set
+ n_div
;
10004 for (i
= 0; i
< n_div
; ++i
) {
10005 k
= isl_basic_map_alloc_inequality(bmap
);
10008 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10009 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10010 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10011 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10012 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10013 set
->p
[0]->div
[i
][0]);
10015 l
= isl_basic_map_alloc_inequality(bmap
);
10018 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10019 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10020 set
->p
[0]->div
[i
][0]);
10021 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10025 bmap
= isl_basic_map_simplify(bmap
);
10026 bmap
= isl_basic_map_finalize(bmap
);
10027 return isl_map_from_basic_map(bmap
);
10030 isl_basic_map_free(bmap
);
10034 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10042 dim
= isl_basic_set_total_dim(bset
);
10043 size
+= bset
->n_eq
* (1 + dim
);
10044 size
+= bset
->n_ineq
* (1 + dim
);
10045 size
+= bset
->n_div
* (2 + dim
);
10050 int isl_set_size(__isl_keep isl_set
*set
)
10058 for (i
= 0; i
< set
->n
; ++i
)
10059 size
+= isl_basic_set_size(set
->p
[i
]);
10064 /* Check if there is any lower bound (if lower == 0) and/or upper
10065 * bound (if upper == 0) on the specified dim.
10067 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10068 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10075 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
10077 pos
+= isl_basic_map_offset(bmap
, type
);
10079 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10080 if (isl_int_is_zero(bmap
->div
[i
][0]))
10082 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10086 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10087 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10090 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10091 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10098 return lower
&& upper
;
10101 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10102 enum isl_dim_type type
, unsigned pos
)
10104 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10107 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10108 enum isl_dim_type type
, unsigned pos
)
10110 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10113 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10114 enum isl_dim_type type
, unsigned pos
)
10116 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10119 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10120 enum isl_dim_type type
, unsigned pos
)
10127 for (i
= 0; i
< map
->n
; ++i
) {
10129 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10130 if (bounded
< 0 || !bounded
)
10137 /* Return 1 if the specified dim is involved in both an upper bound
10138 * and a lower bound.
10140 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10141 enum isl_dim_type type
, unsigned pos
)
10143 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10146 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10148 static int has_any_bound(__isl_keep isl_map
*map
,
10149 enum isl_dim_type type
, unsigned pos
,
10150 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10151 enum isl_dim_type type
, unsigned pos
))
10158 for (i
= 0; i
< map
->n
; ++i
) {
10160 bounded
= fn(map
->p
[i
], type
, pos
);
10161 if (bounded
< 0 || bounded
)
10168 /* Return 1 if the specified dim is involved in any lower bound.
10170 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10171 enum isl_dim_type type
, unsigned pos
)
10173 return has_any_bound(set
, type
, pos
,
10174 &isl_basic_map_dim_has_lower_bound
);
10177 /* Return 1 if the specified dim is involved in any upper bound.
10179 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10180 enum isl_dim_type type
, unsigned pos
)
10182 return has_any_bound(set
, type
, pos
,
10183 &isl_basic_map_dim_has_upper_bound
);
10186 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10188 static int has_bound(__isl_keep isl_map
*map
,
10189 enum isl_dim_type type
, unsigned pos
,
10190 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10191 enum isl_dim_type type
, unsigned pos
))
10198 for (i
= 0; i
< map
->n
; ++i
) {
10200 bounded
= fn(map
->p
[i
], type
, pos
);
10201 if (bounded
< 0 || !bounded
)
10208 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10210 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10211 enum isl_dim_type type
, unsigned pos
)
10213 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10216 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10218 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10219 enum isl_dim_type type
, unsigned pos
)
10221 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10224 /* For each of the "n" variables starting at "first", determine
10225 * the sign of the variable and put the results in the first "n"
10226 * elements of the array "signs".
10228 * 1 means that the variable is non-negative
10229 * -1 means that the variable is non-positive
10230 * 0 means the variable attains both positive and negative values.
10232 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10233 unsigned first
, unsigned n
, int *signs
)
10235 isl_vec
*bound
= NULL
;
10236 struct isl_tab
*tab
= NULL
;
10237 struct isl_tab_undo
*snap
;
10240 if (!bset
|| !signs
)
10243 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10244 tab
= isl_tab_from_basic_set(bset
, 0);
10245 if (!bound
|| !tab
)
10248 isl_seq_clr(bound
->el
, bound
->size
);
10249 isl_int_set_si(bound
->el
[0], -1);
10251 snap
= isl_tab_snap(tab
);
10252 for (i
= 0; i
< n
; ++i
) {
10255 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10256 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10258 empty
= tab
->empty
;
10259 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10260 if (isl_tab_rollback(tab
, snap
) < 0)
10268 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10269 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10271 empty
= tab
->empty
;
10272 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10273 if (isl_tab_rollback(tab
, snap
) < 0)
10276 signs
[i
] = empty
? -1 : 0;
10280 isl_vec_free(bound
);
10284 isl_vec_free(bound
);
10288 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10289 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10291 if (!bset
|| !signs
)
10293 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10296 first
+= pos(bset
->dim
, type
) - 1;
10297 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10300 /* Is it possible for the integer division "div" to depend (possibly
10301 * indirectly) on any output dimensions?
10303 * If the div is undefined, then we conservatively assume that it
10304 * may depend on them.
10305 * Otherwise, we check if it actually depends on them or on any integer
10306 * divisions that may depend on them.
10308 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10311 unsigned n_out
, o_out
;
10312 unsigned n_div
, o_div
;
10314 if (isl_int_is_zero(bmap
->div
[div
][0]))
10317 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10318 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10320 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10323 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10324 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10326 for (i
= 0; i
< n_div
; ++i
) {
10327 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10329 if (div_may_involve_output(bmap
, i
))
10336 /* Return the index of the equality of "bmap" that defines
10337 * the output dimension "pos" in terms of earlier dimensions.
10338 * The equality may also involve integer divisions, as long
10339 * as those integer divisions are defined in terms of
10340 * parameters or input dimensions.
10341 * Return bmap->n_eq if there is no such equality.
10342 * Return -1 on error.
10344 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10348 unsigned n_out
, o_out
;
10349 unsigned n_div
, o_div
;
10354 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10355 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10356 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10357 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10359 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10360 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10362 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10363 n_out
- (pos
+ 1)) != -1)
10365 for (k
= 0; k
< n_div
; ++k
) {
10366 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10368 if (div_may_involve_output(bmap
, k
))
10378 /* Check if the given basic map is obviously single-valued.
10379 * In particular, for each output dimension, check that there is
10380 * an equality that defines the output dimension in terms of
10381 * earlier dimensions.
10383 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10391 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10393 for (i
= 0; i
< n_out
; ++i
) {
10396 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10399 if (eq
>= bmap
->n_eq
)
10406 /* Check if the given basic map is single-valued.
10407 * We simply compute
10411 * and check if the result is a subset of the identity mapping.
10413 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10416 isl_basic_map
*test
;
10420 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10424 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10425 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10427 space
= isl_basic_map_get_space(bmap
);
10428 space
= isl_space_map_from_set(isl_space_range(space
));
10429 id
= isl_basic_map_identity(space
);
10431 sv
= isl_basic_map_is_subset(test
, id
);
10433 isl_basic_map_free(test
);
10434 isl_basic_map_free(id
);
10439 /* Check if the given map is obviously single-valued.
10441 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10450 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10453 /* Check if the given map is single-valued.
10454 * We simply compute
10458 * and check if the result is a subset of the identity mapping.
10460 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10467 sv
= isl_map_plain_is_single_valued(map
);
10471 test
= isl_map_reverse(isl_map_copy(map
));
10472 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10474 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10475 id
= isl_map_identity(dim
);
10477 sv
= isl_map_is_subset(test
, id
);
10479 isl_map_free(test
);
10485 int isl_map_is_injective(__isl_keep isl_map
*map
)
10489 map
= isl_map_copy(map
);
10490 map
= isl_map_reverse(map
);
10491 in
= isl_map_is_single_valued(map
);
10497 /* Check if the given map is obviously injective.
10499 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10503 map
= isl_map_copy(map
);
10504 map
= isl_map_reverse(map
);
10505 in
= isl_map_plain_is_single_valued(map
);
10511 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10515 sv
= isl_map_is_single_valued(map
);
10519 return isl_map_is_injective(map
);
10522 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10524 return isl_map_is_single_valued((isl_map
*)set
);
10527 int isl_map_is_translation(__isl_keep isl_map
*map
)
10532 delta
= isl_map_deltas(isl_map_copy(map
));
10533 ok
= isl_set_is_singleton(delta
);
10534 isl_set_free(delta
);
10539 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10541 if (isl_seq_first_non_zero(p
, pos
) != -1)
10543 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10548 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10557 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10560 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10561 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10562 for (j
= 0; j
< nvar
; ++j
) {
10563 int lower
= 0, upper
= 0;
10564 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10565 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10567 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10571 if (i
< bset
->n_eq
)
10573 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10574 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10576 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10578 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10583 if (!lower
|| !upper
)
10590 int isl_set_is_box(__isl_keep isl_set
*set
)
10597 return isl_basic_set_is_box(set
->p
[0]);
10600 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10605 return isl_space_is_wrapping(bset
->dim
);
10608 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10613 return isl_space_is_wrapping(set
->dim
);
10616 /* Is the domain of "map" a wrapped relation?
10618 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10623 return isl_space_domain_is_wrapping(map
->dim
);
10626 /* Is the range of "map" a wrapped relation?
10628 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10633 return isl_space_range_is_wrapping(map
->dim
);
10636 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10638 bmap
= isl_basic_map_cow(bmap
);
10642 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10646 bmap
= isl_basic_map_finalize(bmap
);
10648 return (isl_basic_set
*)bmap
;
10650 isl_basic_map_free(bmap
);
10654 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10658 map
= isl_map_cow(map
);
10662 for (i
= 0; i
< map
->n
; ++i
) {
10663 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10667 map
->dim
= isl_space_wrap(map
->dim
);
10671 return (isl_set
*)map
;
10677 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10679 bset
= isl_basic_set_cow(bset
);
10683 bset
->dim
= isl_space_unwrap(bset
->dim
);
10687 bset
= isl_basic_set_finalize(bset
);
10689 return (isl_basic_map
*)bset
;
10691 isl_basic_set_free(bset
);
10695 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10702 if (!isl_set_is_wrapping(set
))
10703 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10706 set
= isl_set_cow(set
);
10710 for (i
= 0; i
< set
->n
; ++i
) {
10711 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10716 set
->dim
= isl_space_unwrap(set
->dim
);
10720 return (isl_map
*)set
;
10726 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10727 enum isl_dim_type type
)
10732 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10735 bmap
= isl_basic_map_cow(bmap
);
10739 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10743 bmap
= isl_basic_map_finalize(bmap
);
10747 isl_basic_map_free(bmap
);
10751 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10752 enum isl_dim_type type
)
10759 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10762 map
= isl_map_cow(map
);
10766 for (i
= 0; i
< map
->n
; ++i
) {
10767 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10771 map
->dim
= isl_space_reset(map
->dim
, type
);
10781 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10786 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10789 bmap
= isl_basic_map_cow(bmap
);
10793 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10797 bmap
= isl_basic_map_finalize(bmap
);
10801 isl_basic_map_free(bmap
);
10805 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10807 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10810 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10811 __isl_take isl_basic_map
*bmap
)
10816 if (!bmap
->dim
->nested
[0])
10819 bmap
= isl_basic_map_cow(bmap
);
10823 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10827 bmap
= isl_basic_map_finalize(bmap
);
10831 isl_basic_map_free(bmap
);
10835 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10836 __isl_take isl_basic_map
*bmap
)
10841 if (!bmap
->dim
->nested
[1])
10844 bmap
= isl_basic_map_cow(bmap
);
10848 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10852 bmap
= isl_basic_map_finalize(bmap
);
10856 isl_basic_map_free(bmap
);
10860 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10867 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10870 map
= isl_map_cow(map
);
10874 for (i
= 0; i
< map
->n
; ++i
) {
10875 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10879 map
->dim
= isl_space_flatten(map
->dim
);
10889 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10891 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10894 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10896 isl_space
*dim
, *flat_dim
;
10899 dim
= isl_set_get_space(set
);
10900 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10901 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10902 map
= isl_map_intersect_domain(map
, set
);
10907 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10914 if (!map
->dim
->nested
[0])
10917 map
= isl_map_cow(map
);
10921 for (i
= 0; i
< map
->n
; ++i
) {
10922 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10926 map
->dim
= isl_space_flatten_domain(map
->dim
);
10936 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10943 if (!map
->dim
->nested
[1])
10946 map
= isl_map_cow(map
);
10950 for (i
= 0; i
< map
->n
; ++i
) {
10951 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10955 map
->dim
= isl_space_flatten_range(map
->dim
);
10965 /* Reorder the dimensions of "bmap" according to the given dim_map
10966 * and set the dimension specification to "dim".
10968 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10969 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10971 isl_basic_map
*res
;
10974 bmap
= isl_basic_map_cow(bmap
);
10975 if (!bmap
|| !dim
|| !dim_map
)
10978 flags
= bmap
->flags
;
10979 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10980 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10981 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10982 res
= isl_basic_map_alloc_space(dim
,
10983 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10984 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10986 res
->flags
= flags
;
10987 res
= isl_basic_map_finalize(res
);
10991 isl_basic_map_free(bmap
);
10992 isl_space_free(dim
);
10996 /* Reorder the dimensions of "map" according to given reordering.
10998 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10999 __isl_take isl_reordering
*r
)
11002 struct isl_dim_map
*dim_map
;
11004 map
= isl_map_cow(map
);
11005 dim_map
= isl_dim_map_from_reordering(r
);
11006 if (!map
|| !r
|| !dim_map
)
11009 for (i
= 0; i
< map
->n
; ++i
) {
11010 struct isl_dim_map
*dim_map_i
;
11012 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11014 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11015 isl_space_copy(r
->dim
), dim_map_i
);
11021 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11023 isl_reordering_free(r
);
11029 isl_reordering_free(r
);
11033 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11034 __isl_take isl_reordering
*r
)
11036 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11039 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11040 __isl_take isl_space
*model
)
11044 if (!map
|| !model
)
11047 ctx
= isl_space_get_ctx(model
);
11048 if (!isl_space_has_named_params(model
))
11049 isl_die(ctx
, isl_error_invalid
,
11050 "model has unnamed parameters", goto error
);
11051 if (!isl_space_has_named_params(map
->dim
))
11052 isl_die(ctx
, isl_error_invalid
,
11053 "relation has unnamed parameters", goto error
);
11054 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11055 isl_reordering
*exp
;
11057 model
= isl_space_drop_dims(model
, isl_dim_in
,
11058 0, isl_space_dim(model
, isl_dim_in
));
11059 model
= isl_space_drop_dims(model
, isl_dim_out
,
11060 0, isl_space_dim(model
, isl_dim_out
));
11061 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11062 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11063 map
= isl_map_realign(map
, exp
);
11066 isl_space_free(model
);
11069 isl_space_free(model
);
11074 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11075 __isl_take isl_space
*model
)
11077 return isl_map_align_params(set
, model
);
11080 /* Align the parameters of "bmap" to those of "model", introducing
11081 * additional parameters if needed.
11083 __isl_give isl_basic_map
*isl_basic_map_align_params(
11084 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11088 if (!bmap
|| !model
)
11091 ctx
= isl_space_get_ctx(model
);
11092 if (!isl_space_has_named_params(model
))
11093 isl_die(ctx
, isl_error_invalid
,
11094 "model has unnamed parameters", goto error
);
11095 if (!isl_space_has_named_params(bmap
->dim
))
11096 isl_die(ctx
, isl_error_invalid
,
11097 "relation has unnamed parameters", goto error
);
11098 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11099 isl_reordering
*exp
;
11100 struct isl_dim_map
*dim_map
;
11102 model
= isl_space_drop_dims(model
, isl_dim_in
,
11103 0, isl_space_dim(model
, isl_dim_in
));
11104 model
= isl_space_drop_dims(model
, isl_dim_out
,
11105 0, isl_space_dim(model
, isl_dim_out
));
11106 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11107 exp
= isl_reordering_extend_space(exp
,
11108 isl_basic_map_get_space(bmap
));
11109 dim_map
= isl_dim_map_from_reordering(exp
);
11110 bmap
= isl_basic_map_realign(bmap
,
11111 exp
? isl_space_copy(exp
->dim
) : NULL
,
11112 isl_dim_map_extend(dim_map
, bmap
));
11113 isl_reordering_free(exp
);
11117 isl_space_free(model
);
11120 isl_space_free(model
);
11121 isl_basic_map_free(bmap
);
11125 /* Align the parameters of "bset" to those of "model", introducing
11126 * additional parameters if needed.
11128 __isl_give isl_basic_set
*isl_basic_set_align_params(
11129 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11131 return isl_basic_map_align_params(bset
, model
);
11134 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11135 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11136 enum isl_dim_type c2
, enum isl_dim_type c3
,
11137 enum isl_dim_type c4
, enum isl_dim_type c5
)
11139 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11140 struct isl_mat
*mat
;
11146 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11147 isl_basic_map_total_dim(bmap
) + 1);
11150 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11151 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11152 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11153 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11154 isl_int_set(mat
->row
[i
][pos
],
11155 bmap
->eq
[i
][off
+ k
]);
11163 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11164 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11165 enum isl_dim_type c2
, enum isl_dim_type c3
,
11166 enum isl_dim_type c4
, enum isl_dim_type c5
)
11168 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11169 struct isl_mat
*mat
;
11175 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11176 isl_basic_map_total_dim(bmap
) + 1);
11179 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11180 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11181 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11182 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11183 isl_int_set(mat
->row
[i
][pos
],
11184 bmap
->ineq
[i
][off
+ k
]);
11192 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11193 __isl_take isl_space
*dim
,
11194 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11195 enum isl_dim_type c2
, enum isl_dim_type c3
,
11196 enum isl_dim_type c4
, enum isl_dim_type c5
)
11198 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11199 isl_basic_map
*bmap
;
11205 if (!dim
|| !eq
|| !ineq
)
11208 if (eq
->n_col
!= ineq
->n_col
)
11209 isl_die(dim
->ctx
, isl_error_invalid
,
11210 "equalities and inequalities matrices should have "
11211 "same number of columns", goto error
);
11213 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11215 if (eq
->n_col
< total
)
11216 isl_die(dim
->ctx
, isl_error_invalid
,
11217 "number of columns too small", goto error
);
11219 extra
= eq
->n_col
- total
;
11221 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11222 eq
->n_row
, ineq
->n_row
);
11225 for (i
= 0; i
< extra
; ++i
) {
11226 k
= isl_basic_map_alloc_div(bmap
);
11229 isl_int_set_si(bmap
->div
[k
][0], 0);
11231 for (i
= 0; i
< eq
->n_row
; ++i
) {
11232 l
= isl_basic_map_alloc_equality(bmap
);
11235 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11236 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11237 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11238 isl_int_set(bmap
->eq
[l
][off
+ k
],
11244 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11245 l
= isl_basic_map_alloc_inequality(bmap
);
11248 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11249 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11250 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11251 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11252 ineq
->row
[i
][pos
]);
11258 isl_space_free(dim
);
11260 isl_mat_free(ineq
);
11262 bmap
= isl_basic_map_simplify(bmap
);
11263 return isl_basic_map_finalize(bmap
);
11265 isl_space_free(dim
);
11267 isl_mat_free(ineq
);
11271 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11272 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11273 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11275 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11276 c1
, c2
, c3
, c4
, isl_dim_in
);
11279 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11280 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11281 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11283 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11284 c1
, c2
, c3
, c4
, isl_dim_in
);
11287 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11288 __isl_take isl_space
*dim
,
11289 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11290 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11292 return (isl_basic_set
*)
11293 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11294 c1
, c2
, c3
, c4
, isl_dim_in
);
11297 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11302 return isl_space_can_zip(bmap
->dim
);
11305 int isl_map_can_zip(__isl_keep isl_map
*map
)
11310 return isl_space_can_zip(map
->dim
);
11313 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11314 * (A -> C) -> (B -> D).
11316 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11325 if (!isl_basic_map_can_zip(bmap
))
11326 isl_die(bmap
->ctx
, isl_error_invalid
,
11327 "basic map cannot be zipped", goto error
);
11328 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11329 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11330 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11331 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11332 bmap
= isl_basic_map_cow(bmap
);
11333 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11336 bmap
->dim
= isl_space_zip(bmap
->dim
);
11341 isl_basic_map_free(bmap
);
11345 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11346 * (A -> C) -> (B -> D).
11348 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11355 if (!isl_map_can_zip(map
))
11356 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11359 map
= isl_map_cow(map
);
11363 for (i
= 0; i
< map
->n
; ++i
) {
11364 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11369 map
->dim
= isl_space_zip(map
->dim
);
11379 /* Can we apply isl_basic_map_curry to "bmap"?
11380 * That is, does it have a nested relation in its domain?
11382 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11387 return isl_space_can_curry(bmap
->dim
);
11390 /* Can we apply isl_map_curry to "map"?
11391 * That is, does it have a nested relation in its domain?
11393 int isl_map_can_curry(__isl_keep isl_map
*map
)
11398 return isl_space_can_curry(map
->dim
);
11401 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11404 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11410 if (!isl_basic_map_can_curry(bmap
))
11411 isl_die(bmap
->ctx
, isl_error_invalid
,
11412 "basic map cannot be curried", goto error
);
11413 bmap
= isl_basic_map_cow(bmap
);
11416 bmap
->dim
= isl_space_curry(bmap
->dim
);
11421 isl_basic_map_free(bmap
);
11425 /* Given a map (A -> B) -> C, return the corresponding map
11428 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11435 if (!isl_map_can_curry(map
))
11436 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11439 map
= isl_map_cow(map
);
11443 for (i
= 0; i
< map
->n
; ++i
) {
11444 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11449 map
->dim
= isl_space_curry(map
->dim
);
11459 /* Can we apply isl_basic_map_uncurry to "bmap"?
11460 * That is, does it have a nested relation in its domain?
11462 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11467 return isl_space_can_uncurry(bmap
->dim
);
11470 /* Can we apply isl_map_uncurry to "map"?
11471 * That is, does it have a nested relation in its domain?
11473 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11478 return isl_space_can_uncurry(map
->dim
);
11481 /* Given a basic map A -> (B -> C), return the corresponding basic map
11484 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11490 if (!isl_basic_map_can_uncurry(bmap
))
11491 isl_die(bmap
->ctx
, isl_error_invalid
,
11492 "basic map cannot be uncurried",
11493 return isl_basic_map_free(bmap
));
11494 bmap
= isl_basic_map_cow(bmap
);
11497 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11499 return isl_basic_map_free(bmap
);
11503 /* Given a map A -> (B -> C), return the corresponding map
11506 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11513 if (!isl_map_can_uncurry(map
))
11514 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11515 return isl_map_free(map
));
11517 map
= isl_map_cow(map
);
11521 for (i
= 0; i
< map
->n
; ++i
) {
11522 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11524 return isl_map_free(map
);
11527 map
->dim
= isl_space_uncurry(map
->dim
);
11529 return isl_map_free(map
);
11534 /* Construct a basic map mapping the domain of the affine expression
11535 * to a one-dimensional range prescribed by the affine expression.
11537 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11541 isl_local_space
*ls
;
11542 isl_basic_map
*bmap
;
11547 ls
= isl_aff_get_local_space(aff
);
11548 bmap
= isl_basic_map_from_local_space(ls
);
11549 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11550 k
= isl_basic_map_alloc_equality(bmap
);
11554 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11555 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11556 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11557 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11558 aff
->v
->size
- (pos
+ 1));
11561 bmap
= isl_basic_map_finalize(bmap
);
11565 isl_basic_map_free(bmap
);
11569 /* Construct a map mapping the domain of the affine expression
11570 * to a one-dimensional range prescribed by the affine expression.
11572 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11574 isl_basic_map
*bmap
;
11576 bmap
= isl_basic_map_from_aff(aff
);
11577 return isl_map_from_basic_map(bmap
);
11580 /* Construct a basic map mapping the domain the multi-affine expression
11581 * to its range, with each dimension in the range equated to the
11582 * corresponding affine expression.
11584 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11585 __isl_take isl_multi_aff
*maff
)
11589 isl_basic_map
*bmap
;
11594 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11595 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11596 "invalid space", goto error
);
11598 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11599 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11601 for (i
= 0; i
< maff
->n
; ++i
) {
11603 isl_basic_map
*bmap_i
;
11605 aff
= isl_aff_copy(maff
->p
[i
]);
11606 bmap_i
= isl_basic_map_from_aff(aff
);
11608 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11611 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11613 isl_multi_aff_free(maff
);
11616 isl_multi_aff_free(maff
);
11620 /* Construct a map mapping the domain the multi-affine expression
11621 * to its range, with each dimension in the range equated to the
11622 * corresponding affine expression.
11624 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11626 isl_basic_map
*bmap
;
11628 bmap
= isl_basic_map_from_multi_aff(maff
);
11629 return isl_map_from_basic_map(bmap
);
11632 /* Construct a basic map mapping a domain in the given space to
11633 * to an n-dimensional range, with n the number of elements in the list,
11634 * where each coordinate in the range is prescribed by the
11635 * corresponding affine expression.
11636 * The domains of all affine expressions in the list are assumed to match
11639 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11640 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11644 isl_basic_map
*bmap
;
11649 dim
= isl_space_from_domain(domain_dim
);
11650 bmap
= isl_basic_map_universe(dim
);
11652 for (i
= 0; i
< list
->n
; ++i
) {
11654 isl_basic_map
*bmap_i
;
11656 aff
= isl_aff_copy(list
->p
[i
]);
11657 bmap_i
= isl_basic_map_from_aff(aff
);
11659 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11662 isl_aff_list_free(list
);
11666 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11667 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11669 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11672 /* Construct a basic map where the given dimensions are equal to each other.
11674 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11675 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11677 isl_basic_map
*bmap
= NULL
;
11683 if (pos1
>= isl_space_dim(space
, type1
))
11684 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11685 "index out of bounds", goto error
);
11686 if (pos2
>= isl_space_dim(space
, type2
))
11687 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11688 "index out of bounds", goto error
);
11690 if (type1
== type2
&& pos1
== pos2
)
11691 return isl_basic_map_universe(space
);
11693 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11694 i
= isl_basic_map_alloc_equality(bmap
);
11697 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11698 pos1
+= isl_basic_map_offset(bmap
, type1
);
11699 pos2
+= isl_basic_map_offset(bmap
, type2
);
11700 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11701 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11702 bmap
= isl_basic_map_finalize(bmap
);
11703 isl_space_free(space
);
11706 isl_space_free(space
);
11707 isl_basic_map_free(bmap
);
11711 /* Add a constraint imposing that the given two dimensions are equal.
11713 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11714 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11718 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11720 bmap
= isl_basic_map_intersect(bmap
, eq
);
11725 /* Add a constraint imposing that the given two dimensions are equal.
11727 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11728 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11730 isl_basic_map
*bmap
;
11732 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11734 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11739 /* Add a constraint imposing that the given two dimensions have opposite values.
11741 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11742 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11744 isl_basic_map
*bmap
= NULL
;
11750 if (pos1
>= isl_map_dim(map
, type1
))
11751 isl_die(map
->ctx
, isl_error_invalid
,
11752 "index out of bounds", goto error
);
11753 if (pos2
>= isl_map_dim(map
, type2
))
11754 isl_die(map
->ctx
, isl_error_invalid
,
11755 "index out of bounds", goto error
);
11757 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11758 i
= isl_basic_map_alloc_equality(bmap
);
11761 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11762 pos1
+= isl_basic_map_offset(bmap
, type1
);
11763 pos2
+= isl_basic_map_offset(bmap
, type2
);
11764 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11765 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11766 bmap
= isl_basic_map_finalize(bmap
);
11768 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11772 isl_basic_map_free(bmap
);
11777 /* Construct a constraint imposing that the value of the first dimension is
11778 * greater than or equal to that of the second.
11780 static __isl_give isl_constraint
*constraint_order_ge(
11781 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11782 enum isl_dim_type type2
, int pos2
)
11789 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11791 if (pos1
>= isl_constraint_dim(c
, type1
))
11792 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11793 "index out of bounds", return isl_constraint_free(c
));
11794 if (pos2
>= isl_constraint_dim(c
, type2
))
11795 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11796 "index out of bounds", return isl_constraint_free(c
));
11798 if (type1
== type2
&& pos1
== pos2
)
11801 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11802 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11807 /* Add a constraint imposing that the value of the first dimension is
11808 * greater than or equal to that of the second.
11810 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11811 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11816 if (type1
== type2
&& pos1
== pos2
)
11818 space
= isl_basic_map_get_space(bmap
);
11819 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11820 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11825 /* Add a constraint imposing that the value of the first dimension is
11826 * greater than or equal to that of the second.
11828 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11829 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11834 if (type1
== type2
&& pos1
== pos2
)
11836 space
= isl_map_get_space(map
);
11837 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11838 map
= isl_map_add_constraint(map
, c
);
11843 /* Add a constraint imposing that the value of the first dimension is
11844 * less than or equal to that of the second.
11846 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11847 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11849 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11852 /* Construct a basic map where the value of the first dimension is
11853 * greater than that of the second.
11855 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11856 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11858 isl_basic_map
*bmap
= NULL
;
11864 if (pos1
>= isl_space_dim(space
, type1
))
11865 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11866 "index out of bounds", goto error
);
11867 if (pos2
>= isl_space_dim(space
, type2
))
11868 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11869 "index out of bounds", goto error
);
11871 if (type1
== type2
&& pos1
== pos2
)
11872 return isl_basic_map_empty(space
);
11874 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11875 i
= isl_basic_map_alloc_inequality(bmap
);
11877 return isl_basic_map_free(bmap
);
11878 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11879 pos1
+= isl_basic_map_offset(bmap
, type1
);
11880 pos2
+= isl_basic_map_offset(bmap
, type2
);
11881 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11882 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11883 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11884 bmap
= isl_basic_map_finalize(bmap
);
11888 isl_space_free(space
);
11889 isl_basic_map_free(bmap
);
11893 /* Add a constraint imposing that the value of the first dimension is
11894 * greater than that of the second.
11896 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11897 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11901 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11903 bmap
= isl_basic_map_intersect(bmap
, gt
);
11908 /* Add a constraint imposing that the value of the first dimension is
11909 * greater than that of the second.
11911 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11912 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11914 isl_basic_map
*bmap
;
11916 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11918 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11923 /* Add a constraint imposing that the value of the first dimension is
11924 * smaller than that of the second.
11926 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11927 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11929 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11932 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11936 isl_local_space
*ls
;
11941 if (!isl_basic_map_divs_known(bmap
))
11942 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11943 "some divs are unknown", return NULL
);
11945 ls
= isl_basic_map_get_local_space(bmap
);
11946 div
= isl_local_space_get_div(ls
, pos
);
11947 isl_local_space_free(ls
);
11952 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11955 return isl_basic_map_get_div(bset
, pos
);
11958 /* Plug in "subs" for dimension "type", "pos" of "bset".
11960 * Let i be the dimension to replace and let "subs" be of the form
11964 * Any integer division with a non-zero coefficient for i,
11966 * floor((a i + g)/m)
11970 * floor((a f + d g)/(m d))
11972 * Constraints of the form
11980 * We currently require that "subs" is an integral expression.
11981 * Handling rational expressions may require us to add stride constraints
11982 * as we do in isl_basic_set_preimage_multi_aff.
11984 __isl_give isl_basic_set
*isl_basic_set_substitute(
11985 __isl_take isl_basic_set
*bset
,
11986 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11992 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11995 bset
= isl_basic_set_cow(bset
);
11996 if (!bset
|| !subs
)
11999 ctx
= isl_basic_set_get_ctx(bset
);
12000 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12001 isl_die(ctx
, isl_error_invalid
,
12002 "spaces don't match", goto error
);
12003 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12004 isl_die(ctx
, isl_error_unsupported
,
12005 "cannot handle divs yet", goto error
);
12006 if (!isl_int_is_one(subs
->v
->el
[0]))
12007 isl_die(ctx
, isl_error_invalid
,
12008 "can only substitute integer expressions", goto error
);
12010 pos
+= isl_basic_set_offset(bset
, type
);
12014 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12015 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12017 isl_int_set(v
, bset
->eq
[i
][pos
]);
12018 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12019 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12020 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12023 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12024 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12026 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12027 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12028 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12029 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12032 for (i
= 0; i
< bset
->n_div
; ++i
) {
12033 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12035 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12036 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12037 isl_seq_combine(bset
->div
[i
] + 1,
12038 subs
->v
->el
[0], bset
->div
[i
] + 1,
12039 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12040 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12045 bset
= isl_basic_set_simplify(bset
);
12046 return isl_basic_set_finalize(bset
);
12048 isl_basic_set_free(bset
);
12052 /* Plug in "subs" for dimension "type", "pos" of "set".
12054 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12055 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12059 if (set
&& isl_set_plain_is_empty(set
))
12062 set
= isl_set_cow(set
);
12066 for (i
= set
->n
- 1; i
>= 0; --i
) {
12067 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12068 if (remove_if_empty(set
, i
) < 0)
12078 /* Check if the range of "ma" is compatible with the domain or range
12079 * (depending on "type") of "bmap".
12080 * Return -1 if anything is wrong.
12082 static int check_basic_map_compatible_range_multi_aff(
12083 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12084 __isl_keep isl_multi_aff
*ma
)
12087 isl_space
*ma_space
;
12089 ma_space
= isl_multi_aff_get_space(ma
);
12091 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12095 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12096 "parameters don't match", goto error
);
12097 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12101 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12102 "spaces don't match", goto error
);
12104 isl_space_free(ma_space
);
12107 isl_space_free(ma_space
);
12111 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12112 * coefficients before the transformed range of dimensions,
12113 * the "n_after" coefficients after the transformed range of dimensions
12114 * and the coefficients of the other divs in "bmap".
12116 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12117 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12122 isl_local_space
*ls
;
12127 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12131 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12132 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12133 for (i
= 0; i
< n_div
; ++i
) {
12134 int o_bmap
= 0, o_ls
= 0;
12136 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12137 o_bmap
+= 1 + 1 + n_param
;
12138 o_ls
+= 1 + 1 + n_param
;
12139 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12140 o_bmap
+= n_before
;
12141 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12142 ls
->div
->row
[i
] + o_ls
, n_set
);
12145 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12147 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12148 ls
->div
->row
[i
] + o_ls
, n_div
);
12151 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12152 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12156 isl_local_space_free(ls
);
12159 isl_local_space_free(ls
);
12163 /* How many stride constraints does "ma" enforce?
12164 * That is, how many of the affine expressions have a denominator
12165 * different from one?
12167 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12172 for (i
= 0; i
< ma
->n
; ++i
)
12173 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12179 /* For each affine expression in ma of the form
12181 * x_i = (f_i y + h_i)/m_i
12183 * with m_i different from one, add a constraint to "bmap"
12186 * f_i y + h_i = m_i alpha_i
12188 * with alpha_i an additional existentially quantified variable.
12190 static __isl_give isl_basic_map
*add_ma_strides(
12191 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12192 int n_before
, int n_after
)
12201 total
= isl_basic_map_total_dim(bmap
);
12202 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12203 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12204 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12205 for (i
= 0; i
< ma
->n
; ++i
) {
12206 int o_bmap
= 0, o_ma
= 1;
12208 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12210 div
= isl_basic_map_alloc_div(bmap
);
12211 k
= isl_basic_map_alloc_equality(bmap
);
12212 if (div
< 0 || k
< 0)
12214 isl_int_set_si(bmap
->div
[div
][0], 0);
12215 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12216 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12217 o_bmap
+= 1 + n_param
;
12218 o_ma
+= 1 + n_param
;
12219 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12220 o_bmap
+= n_before
;
12221 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12222 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12225 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12227 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12228 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12231 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12232 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12238 isl_basic_map_free(bmap
);
12242 /* Replace the domain or range space (depending on "type) of "space" by "set".
12244 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12245 enum isl_dim_type type
, __isl_take isl_space
*set
)
12247 if (type
== isl_dim_in
) {
12248 space
= isl_space_range(space
);
12249 space
= isl_space_map_from_domain_and_range(set
, space
);
12251 space
= isl_space_domain(space
);
12252 space
= isl_space_map_from_domain_and_range(space
, set
);
12258 /* Compute the preimage of the domain or range (depending on "type")
12259 * of "bmap" under the function represented by "ma".
12260 * In other words, plug in "ma" in the domain or range of "bmap".
12261 * The result is a basic map that lives in the same space as "bmap"
12262 * except that the domain or range has been replaced by
12263 * the domain space of "ma".
12265 * If bmap is represented by
12267 * A(p) + S u + B x + T v + C(divs) >= 0,
12269 * where u and x are input and output dimensions if type == isl_dim_out
12270 * while x and v are input and output dimensions if type == isl_dim_in,
12271 * and ma is represented by
12273 * x = D(p) + F(y) + G(divs')
12275 * then the result is
12277 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12279 * The divs in the input set are similarly adjusted.
12282 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12286 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12287 * B_i G(divs') + c_i(divs))/n_i)
12289 * If bmap is not a rational map and if F(y) involves any denominators
12291 * x_i = (f_i y + h_i)/m_i
12293 * then additional constraints are added to ensure that we only
12294 * map back integer points. That is we enforce
12296 * f_i y + h_i = m_i alpha_i
12298 * with alpha_i an additional existentially quantified variable.
12300 * We first copy over the divs from "ma".
12301 * Then we add the modified constraints and divs from "bmap".
12302 * Finally, we add the stride constraints, if needed.
12304 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12305 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12306 __isl_take isl_multi_aff
*ma
)
12310 isl_basic_map
*res
= NULL
;
12311 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12312 isl_int f
, c1
, c2
, g
;
12313 int rational
, strides
;
12320 ma
= isl_multi_aff_align_divs(ma
);
12323 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12326 if (type
== isl_dim_in
) {
12328 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12330 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12333 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12334 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12336 space
= isl_multi_aff_get_domain_space(ma
);
12337 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12338 rational
= isl_basic_map_is_rational(bmap
);
12339 strides
= rational
? 0 : multi_aff_strides(ma
);
12340 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12341 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12343 res
= isl_basic_map_set_rational(res
);
12345 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12346 if (isl_basic_map_alloc_div(res
) < 0)
12349 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12352 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12353 k
= isl_basic_map_alloc_equality(res
);
12356 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12357 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12360 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12361 k
= isl_basic_map_alloc_inequality(res
);
12364 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12365 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12368 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12369 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12370 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12373 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12374 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12379 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12385 isl_basic_map_free(bmap
);
12386 isl_multi_aff_free(ma
);
12387 res
= isl_basic_set_simplify(res
);
12388 return isl_basic_map_finalize(res
);
12394 isl_basic_map_free(bmap
);
12395 isl_multi_aff_free(ma
);
12396 isl_basic_map_free(res
);
12400 /* Compute the preimage of "bset" under the function represented by "ma".
12401 * In other words, plug in "ma" in "bset". The result is a basic set
12402 * that lives in the domain space of "ma".
12404 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12405 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12407 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12410 /* Compute the preimage of the domain of "bmap" under the function
12411 * represented by "ma".
12412 * In other words, plug in "ma" in the domain of "bmap".
12413 * The result is a basic map that lives in the same space as "bmap"
12414 * except that the domain has been replaced by the domain space of "ma".
12416 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12417 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12419 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12422 /* Compute the preimage of the range of "bmap" under the function
12423 * represented by "ma".
12424 * In other words, plug in "ma" in the range of "bmap".
12425 * The result is a basic map that lives in the same space as "bmap"
12426 * except that the range has been replaced by the domain space of "ma".
12428 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12429 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12431 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12434 /* Check if the range of "ma" is compatible with the domain or range
12435 * (depending on "type") of "map".
12436 * Return -1 if anything is wrong.
12438 static int check_map_compatible_range_multi_aff(
12439 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12440 __isl_keep isl_multi_aff
*ma
)
12443 isl_space
*ma_space
;
12445 ma_space
= isl_multi_aff_get_space(ma
);
12446 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
12447 isl_space_free(ma_space
);
12449 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12450 "spaces don't match", return -1);
12454 /* Compute the preimage of the domain or range (depending on "type")
12455 * of "map" under the function represented by "ma".
12456 * In other words, plug in "ma" in the domain or range of "map".
12457 * The result is a map that lives in the same space as "map"
12458 * except that the domain or range has been replaced by
12459 * the domain space of "ma".
12461 * The parameters are assumed to have been aligned.
12463 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12464 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12469 map
= isl_map_cow(map
);
12470 ma
= isl_multi_aff_align_divs(ma
);
12473 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12476 for (i
= 0; i
< map
->n
; ++i
) {
12477 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12478 isl_multi_aff_copy(ma
));
12483 space
= isl_multi_aff_get_domain_space(ma
);
12484 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12486 isl_space_free(map
->dim
);
12491 isl_multi_aff_free(ma
);
12493 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12494 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12497 isl_multi_aff_free(ma
);
12502 /* Compute the preimage of the domain or range (depending on "type")
12503 * of "map" under the function represented by "ma".
12504 * In other words, plug in "ma" in the domain or range of "map".
12505 * The result is a map that lives in the same space as "map"
12506 * except that the domain or range has been replaced by
12507 * the domain space of "ma".
12509 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12510 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12515 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12516 return map_preimage_multi_aff(map
, type
, ma
);
12518 if (!isl_space_has_named_params(map
->dim
) ||
12519 !isl_space_has_named_params(ma
->space
))
12520 isl_die(map
->ctx
, isl_error_invalid
,
12521 "unaligned unnamed parameters", goto error
);
12522 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12523 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12525 return map_preimage_multi_aff(map
, type
, ma
);
12527 isl_multi_aff_free(ma
);
12528 return isl_map_free(map
);
12531 /* Compute the preimage of "set" under the function represented by "ma".
12532 * In other words, plug in "ma" in "set". The result is a set
12533 * that lives in the domain space of "ma".
12535 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12536 __isl_take isl_multi_aff
*ma
)
12538 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12541 /* Compute the preimage of the domain of "map" under the function
12542 * represented by "ma".
12543 * In other words, plug in "ma" in the domain of "map".
12544 * The result is a map that lives in the same space as "map"
12545 * except that the domain has been replaced by the domain space of "ma".
12547 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12548 __isl_take isl_multi_aff
*ma
)
12550 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12553 /* Compute the preimage of the range of "map" under the function
12554 * represented by "ma".
12555 * In other words, plug in "ma" in the range of "map".
12556 * The result is a map that lives in the same space as "map"
12557 * except that the range has been replaced by the domain space of "ma".
12559 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12560 __isl_take isl_multi_aff
*ma
)
12562 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12565 /* Compute the preimage of "map" under the function represented by "pma".
12566 * In other words, plug in "pma" in the domain or range of "map".
12567 * The result is a map that lives in the same space as "map",
12568 * except that the space of type "type" has been replaced by
12569 * the domain space of "pma".
12571 * The parameters of "map" and "pma" are assumed to have been aligned.
12573 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12574 __isl_take isl_map
*map
, enum isl_dim_type type
,
12575 __isl_take isl_pw_multi_aff
*pma
)
12584 isl_pw_multi_aff_free(pma
);
12585 res
= isl_map_empty(isl_map_get_space(map
));
12590 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12591 isl_multi_aff_copy(pma
->p
[0].maff
));
12592 if (type
== isl_dim_in
)
12593 res
= isl_map_intersect_domain(res
,
12594 isl_map_copy(pma
->p
[0].set
));
12596 res
= isl_map_intersect_range(res
,
12597 isl_map_copy(pma
->p
[0].set
));
12599 for (i
= 1; i
< pma
->n
; ++i
) {
12602 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12603 isl_multi_aff_copy(pma
->p
[i
].maff
));
12604 if (type
== isl_dim_in
)
12605 res_i
= isl_map_intersect_domain(res_i
,
12606 isl_map_copy(pma
->p
[i
].set
));
12608 res_i
= isl_map_intersect_range(res_i
,
12609 isl_map_copy(pma
->p
[i
].set
));
12610 res
= isl_map_union(res
, res_i
);
12613 isl_pw_multi_aff_free(pma
);
12617 isl_pw_multi_aff_free(pma
);
12622 /* Compute the preimage of "map" under the function represented by "pma".
12623 * In other words, plug in "pma" in the domain or range of "map".
12624 * The result is a map that lives in the same space as "map",
12625 * except that the space of type "type" has been replaced by
12626 * the domain space of "pma".
12628 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12629 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12634 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12635 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12637 if (!isl_space_has_named_params(map
->dim
) ||
12638 !isl_space_has_named_params(pma
->dim
))
12639 isl_die(map
->ctx
, isl_error_invalid
,
12640 "unaligned unnamed parameters", goto error
);
12641 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12642 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12644 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12646 isl_pw_multi_aff_free(pma
);
12647 return isl_map_free(map
);
12650 /* Compute the preimage of "set" under the function represented by "pma".
12651 * In other words, plug in "pma" in "set". The result is a set
12652 * that lives in the domain space of "pma".
12654 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12655 __isl_take isl_pw_multi_aff
*pma
)
12657 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12660 /* Compute the preimage of the domain of "map" under the function
12661 * represented by "pma".
12662 * In other words, plug in "pma" in the domain of "map".
12663 * The result is a map that lives in the same space as "map",
12664 * except that domain space has been replaced by the domain space of "pma".
12666 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12667 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12669 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12672 /* Compute the preimage of the range of "map" under the function
12673 * represented by "pma".
12674 * In other words, plug in "pma" in the range of "map".
12675 * The result is a map that lives in the same space as "map",
12676 * except that range space has been replaced by the domain space of "pma".
12678 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12679 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12681 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12684 /* Compute the preimage of "map" under the function represented by "mpa".
12685 * In other words, plug in "mpa" in the domain or range of "map".
12686 * The result is a map that lives in the same space as "map",
12687 * except that the space of type "type" has been replaced by
12688 * the domain space of "mpa".
12690 * If the map does not involve any constraints that refer to the
12691 * dimensions of the substituted space, then the only possible
12692 * effect of "mpa" on the map is to map the space to a different space.
12693 * We create a separate isl_multi_aff to effectuate this change
12694 * in order to avoid spurious splitting of the map along the pieces
12697 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12698 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12701 isl_pw_multi_aff
*pma
;
12706 n
= isl_map_dim(map
, type
);
12707 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12711 space
= isl_multi_pw_aff_get_space(mpa
);
12712 isl_multi_pw_aff_free(mpa
);
12713 ma
= isl_multi_aff_zero(space
);
12714 return isl_map_preimage_multi_aff(map
, type
, ma
);
12717 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12718 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12721 isl_multi_pw_aff_free(mpa
);
12725 /* Compute the preimage of "map" under the function represented by "mpa".
12726 * In other words, plug in "mpa" in the domain "map".
12727 * The result is a map that lives in the same space as "map",
12728 * except that domain space has been replaced by the domain space of "mpa".
12730 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12731 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12733 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12736 /* Compute the preimage of "set" by the function represented by "mpa".
12737 * In other words, plug in "mpa" in "set".
12739 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12740 __isl_take isl_multi_pw_aff
*mpa
)
12742 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);