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_is_equal(map
->dim
, isl_dim_in
,
198 set
->dim
, isl_dim_set
);
201 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
202 struct isl_basic_set
*bset
)
207 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
210 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
211 bset
->dim
, isl_dim_set
);
214 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
219 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
222 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
223 set
->dim
, isl_dim_set
);
226 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
227 struct isl_basic_set
*bset
)
232 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
235 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
236 bset
->dim
, isl_dim_set
);
239 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
241 return bmap
? bmap
->ctx
: NULL
;
244 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
246 return bset
? bset
->ctx
: NULL
;
249 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
251 return map
? map
->ctx
: NULL
;
254 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
256 return set
? set
->ctx
: NULL
;
259 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
263 return isl_space_copy(bmap
->dim
);
266 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
270 return isl_space_copy(bset
->dim
);
273 /* Extract the divs in "bmap" as a matrix.
275 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
286 ctx
= isl_basic_map_get_ctx(bmap
);
287 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
288 cols
= 1 + 1 + total
+ bmap
->n_div
;
289 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
293 for (i
= 0; i
< bmap
->n_div
; ++i
)
294 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
299 /* Extract the divs in "bset" as a matrix.
301 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
303 return isl_basic_map_get_divs(bset
);
306 __isl_give isl_local_space
*isl_basic_map_get_local_space(
307 __isl_keep isl_basic_map
*bmap
)
314 div
= isl_basic_map_get_divs(bmap
);
315 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
318 __isl_give isl_local_space
*isl_basic_set_get_local_space(
319 __isl_keep isl_basic_set
*bset
)
321 return isl_basic_map_get_local_space(bset
);
324 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
325 __isl_take isl_local_space
*ls
)
334 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
335 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
336 n_div
, 0, 2 * n_div
);
338 for (i
= 0; i
< n_div
; ++i
)
339 if (isl_basic_map_alloc_div(bmap
) < 0)
342 for (i
= 0; i
< n_div
; ++i
) {
343 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
344 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
348 isl_local_space_free(ls
);
351 isl_local_space_free(ls
);
352 isl_basic_map_free(bmap
);
356 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
357 __isl_take isl_local_space
*ls
)
359 return isl_basic_map_from_local_space(ls
);
362 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
366 return isl_space_copy(map
->dim
);
369 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
373 return isl_space_copy(set
->dim
);
376 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
377 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
379 bmap
= isl_basic_map_cow(bmap
);
382 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
385 bmap
= isl_basic_map_finalize(bmap
);
388 isl_basic_map_free(bmap
);
392 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
393 __isl_take isl_basic_set
*bset
, const char *s
)
395 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
398 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
399 enum isl_dim_type type
)
401 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
404 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
405 enum isl_dim_type type
, const char *s
)
409 map
= isl_map_cow(map
);
413 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
417 for (i
= 0; i
< map
->n
; ++i
) {
418 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
429 /* Replace the identifier of the tuple of type "type" by "id".
431 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
432 __isl_take isl_basic_map
*bmap
,
433 enum isl_dim_type type
, __isl_take isl_id
*id
)
435 bmap
= isl_basic_map_cow(bmap
);
438 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
440 return isl_basic_map_free(bmap
);
441 bmap
= isl_basic_map_finalize(bmap
);
448 /* Replace the identifier of the tuple by "id".
450 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
451 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
453 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
456 /* Does the input or output tuple have a name?
458 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
460 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
463 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
464 enum isl_dim_type type
)
466 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
469 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
472 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
475 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
476 enum isl_dim_type type
, __isl_take isl_id
*id
)
478 map
= isl_map_cow(map
);
482 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
484 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
490 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
491 __isl_take isl_id
*id
)
493 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
496 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
497 enum isl_dim_type type
)
499 map
= isl_map_cow(map
);
503 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
505 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
508 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
510 return isl_map_reset_tuple_id(set
, isl_dim_set
);
513 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
515 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
518 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
519 enum isl_dim_type type
)
521 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
524 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
526 return isl_map_has_tuple_id(set
, isl_dim_set
);
529 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
531 return isl_map_get_tuple_id(set
, isl_dim_set
);
534 /* Does the set tuple have a name?
536 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
538 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
542 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
544 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
547 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
549 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
552 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
553 enum isl_dim_type type
, unsigned pos
)
555 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
558 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
559 enum isl_dim_type type
, unsigned pos
)
561 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
564 /* Does the given dimension have a name?
566 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
567 enum isl_dim_type type
, unsigned pos
)
569 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
572 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
573 enum isl_dim_type type
, unsigned pos
)
575 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
578 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
579 enum isl_dim_type type
, unsigned pos
)
581 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
584 /* Does the given dimension have a name?
586 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
587 enum isl_dim_type type
, unsigned pos
)
589 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
592 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
593 __isl_take isl_basic_map
*bmap
,
594 enum isl_dim_type type
, unsigned pos
, const char *s
)
596 bmap
= isl_basic_map_cow(bmap
);
599 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
602 return isl_basic_map_finalize(bmap
);
604 isl_basic_map_free(bmap
);
608 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
609 enum isl_dim_type type
, unsigned pos
, const char *s
)
613 map
= isl_map_cow(map
);
617 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
621 for (i
= 0; i
< map
->n
; ++i
) {
622 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
633 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
634 __isl_take isl_basic_set
*bset
,
635 enum isl_dim_type type
, unsigned pos
, const char *s
)
637 return (isl_basic_set
*)isl_basic_map_set_dim_name(
638 (isl_basic_map
*)bset
, type
, pos
, s
);
641 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
642 enum isl_dim_type type
, unsigned pos
, const char *s
)
644 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
647 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
648 enum isl_dim_type type
, unsigned pos
)
650 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
653 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
654 enum isl_dim_type type
, unsigned pos
)
656 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
659 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
660 enum isl_dim_type type
, unsigned pos
)
662 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
665 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
666 enum isl_dim_type type
, unsigned pos
)
668 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
671 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
672 enum isl_dim_type type
, unsigned pos
)
674 return isl_map_has_dim_id(set
, type
, pos
);
677 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
678 enum isl_dim_type type
, unsigned pos
)
680 return isl_map_get_dim_id(set
, type
, pos
);
683 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
684 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
686 map
= isl_map_cow(map
);
690 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
692 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
698 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
699 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
701 return isl_map_set_dim_id(set
, type
, pos
, id
);
704 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
705 __isl_keep isl_id
*id
)
709 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
712 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
713 __isl_keep isl_id
*id
)
715 return isl_map_find_dim_by_id(set
, type
, id
);
718 /* Return the position of the dimension of the given type and name
720 * Return -1 if no such dimension can be found.
722 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
723 enum isl_dim_type type
, const char *name
)
727 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
730 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
735 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
738 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
741 return isl_map_find_dim_by_name(set
, type
, name
);
744 /* Reset the user pointer on all identifiers of parameters and tuples
745 * of the space of "map".
747 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
751 space
= isl_map_get_space(map
);
752 space
= isl_space_reset_user(space
);
753 map
= isl_map_reset_space(map
, space
);
758 /* Reset the user pointer on all identifiers of parameters and tuples
759 * of the space of "set".
761 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
763 return isl_map_reset_user(set
);
766 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
770 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
773 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
775 return isl_basic_map_is_rational(bset
);
778 /* Does "bmap" contain any rational points?
780 * If "bmap" has an equality for each dimension, equating the dimension
781 * to an integer constant, then it has no rational points, even if it
782 * is marked as rational.
784 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
786 int has_rational
= 1;
791 if (isl_basic_map_plain_is_empty(bmap
))
793 if (!isl_basic_map_is_rational(bmap
))
795 bmap
= isl_basic_map_copy(bmap
);
796 bmap
= isl_basic_map_implicit_equalities(bmap
);
799 total
= isl_basic_map_total_dim(bmap
);
800 if (bmap
->n_eq
== total
) {
802 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
803 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
806 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
807 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
809 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
817 isl_basic_map_free(bmap
);
822 /* Does "map" contain any rational points?
824 int isl_map_has_rational(__isl_keep isl_map
*map
)
831 for (i
= 0; i
< map
->n
; ++i
) {
832 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
833 if (has_rational
< 0)
841 /* Does "set" contain any rational points?
843 int isl_set_has_rational(__isl_keep isl_set
*set
)
845 return isl_map_has_rational(set
);
848 /* Is this basic set a parameter domain?
850 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
854 return isl_space_is_params(bset
->dim
);
857 /* Is this set a parameter domain?
859 int isl_set_is_params(__isl_keep isl_set
*set
)
863 return isl_space_is_params(set
->dim
);
866 /* Is this map actually a parameter domain?
867 * Users should never call this function. Outside of isl,
868 * a map can never be a parameter domain.
870 int isl_map_is_params(__isl_keep isl_map
*map
)
874 return isl_space_is_params(map
->dim
);
877 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
878 struct isl_basic_map
*bmap
, unsigned extra
,
879 unsigned n_eq
, unsigned n_ineq
)
882 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
887 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
888 if (isl_blk_is_error(bmap
->block
))
891 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
892 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
896 bmap
->block2
= isl_blk_empty();
899 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
900 if (isl_blk_is_error(bmap
->block2
))
903 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
908 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
909 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
911 for (i
= 0; i
< extra
; ++i
)
912 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
916 bmap
->c_size
= n_eq
+ n_ineq
;
917 bmap
->eq
= bmap
->ineq
+ n_ineq
;
926 isl_basic_map_free(bmap
);
930 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
931 unsigned nparam
, unsigned dim
, unsigned extra
,
932 unsigned n_eq
, unsigned n_ineq
)
934 struct isl_basic_map
*bmap
;
937 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
941 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
942 return (struct isl_basic_set
*)bmap
;
945 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
946 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
948 struct isl_basic_map
*bmap
;
951 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
952 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
953 return (struct isl_basic_set
*)bmap
;
959 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
960 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
962 struct isl_basic_map
*bmap
;
966 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
971 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
977 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
978 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
979 unsigned n_eq
, unsigned n_ineq
)
981 struct isl_basic_map
*bmap
;
984 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
988 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
992 static void dup_constraints(
993 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
996 unsigned total
= isl_basic_map_total_dim(src
);
998 for (i
= 0; i
< src
->n_eq
; ++i
) {
999 int j
= isl_basic_map_alloc_equality(dst
);
1000 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1003 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1004 int j
= isl_basic_map_alloc_inequality(dst
);
1005 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1008 for (i
= 0; i
< src
->n_div
; ++i
) {
1009 int j
= isl_basic_map_alloc_div(dst
);
1010 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1012 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1015 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1017 struct isl_basic_map
*dup
;
1021 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1022 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1025 dup_constraints(dup
, bmap
);
1026 dup
->flags
= bmap
->flags
;
1027 dup
->sample
= isl_vec_copy(bmap
->sample
);
1031 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1033 struct isl_basic_map
*dup
;
1035 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
1036 return (struct isl_basic_set
*)dup
;
1039 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1044 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1048 return isl_basic_set_dup(bset
);
1051 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1060 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1065 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1069 bmap
= isl_basic_map_dup(bmap
);
1071 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1075 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1084 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1089 if (--bmap
->ref
> 0)
1092 isl_ctx_deref(bmap
->ctx
);
1094 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1096 isl_blk_free(bmap
->ctx
, bmap
->block
);
1097 isl_vec_free(bmap
->sample
);
1098 isl_space_free(bmap
->dim
);
1104 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1106 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1109 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1111 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1114 __isl_give isl_map
*isl_map_align_params_map_map_and(
1115 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1116 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1117 __isl_take isl_map
*map2
))
1121 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1122 return fn(map1
, map2
);
1123 if (!isl_space_has_named_params(map1
->dim
) ||
1124 !isl_space_has_named_params(map2
->dim
))
1125 isl_die(map1
->ctx
, isl_error_invalid
,
1126 "unaligned unnamed parameters", goto error
);
1127 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1128 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1129 return fn(map1
, map2
);
1136 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1137 __isl_keep isl_map
*map2
,
1138 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1144 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1145 return fn(map1
, map2
);
1146 if (!isl_space_has_named_params(map1
->dim
) ||
1147 !isl_space_has_named_params(map2
->dim
))
1148 isl_die(map1
->ctx
, isl_error_invalid
,
1149 "unaligned unnamed parameters", return -1);
1150 map1
= isl_map_copy(map1
);
1151 map2
= isl_map_copy(map2
);
1152 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1153 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1160 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1162 struct isl_ctx
*ctx
;
1166 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1167 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1169 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1170 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1171 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1172 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1173 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1174 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1176 int j
= isl_basic_map_alloc_inequality(bmap
);
1180 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1181 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1188 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1189 bmap
->extra
- bmap
->n_div
);
1190 return bmap
->n_eq
++;
1193 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1195 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1198 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1202 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1207 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1209 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1212 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1217 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1219 if (pos
!= bmap
->n_eq
- 1) {
1221 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1222 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1228 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1230 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1233 /* Turn inequality "pos" of "bmap" into an equality.
1235 * In particular, we move the inequality in front of the equalities
1236 * and move the last inequality in the position of the moved inequality.
1237 * Note that isl_tab_make_equalities_explicit depends on this particular
1238 * change in the ordering of the constraints.
1240 void isl_basic_map_inequality_to_equality(
1241 struct isl_basic_map
*bmap
, unsigned pos
)
1245 t
= bmap
->ineq
[pos
];
1246 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1247 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1252 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1253 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1254 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1255 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1258 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1260 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1263 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1265 struct isl_ctx
*ctx
;
1269 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1270 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1274 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1275 1 + isl_basic_map_total_dim(bmap
),
1276 bmap
->extra
- bmap
->n_div
);
1277 return bmap
->n_ineq
++;
1280 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1282 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1285 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1289 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1294 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1296 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1299 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1304 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1306 if (pos
!= bmap
->n_ineq
- 1) {
1307 t
= bmap
->ineq
[pos
];
1308 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1309 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1310 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1316 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1318 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1321 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1326 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1329 k
= isl_basic_map_alloc_equality(bmap
);
1332 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1335 isl_basic_map_free(bmap
);
1339 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1342 return (isl_basic_set
*)
1343 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1346 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1351 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1354 k
= isl_basic_map_alloc_inequality(bmap
);
1357 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1360 isl_basic_map_free(bmap
);
1364 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1367 return (isl_basic_set
*)
1368 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1371 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1375 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1376 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1377 1 + 1 + isl_basic_map_total_dim(bmap
),
1378 bmap
->extra
- bmap
->n_div
);
1379 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1380 return bmap
->n_div
++;
1383 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1385 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1388 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1392 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1397 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1399 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1402 /* Copy constraint from src to dst, putting the vars of src at offset
1403 * dim_off in dst and the divs of src at offset div_off in dst.
1404 * If both sets are actually map, then dim_off applies to the input
1407 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1408 struct isl_basic_map
*src_map
, isl_int
*src
,
1409 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1411 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1412 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1413 unsigned src_in
= isl_basic_map_n_in(src_map
);
1414 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1415 unsigned src_out
= isl_basic_map_n_out(src_map
);
1416 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1417 isl_int_set(dst
[0], src
[0]);
1418 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1419 if (dst_nparam
> src_nparam
)
1420 isl_seq_clr(dst
+1+src_nparam
,
1421 dst_nparam
- src_nparam
);
1422 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1423 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1425 isl_min(dst_in
-in_off
, src_in
));
1426 if (dst_in
-in_off
> src_in
)
1427 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1428 dst_in
- in_off
- src_in
);
1429 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1430 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1431 src
+1+src_nparam
+src_in
,
1432 isl_min(dst_out
-out_off
, src_out
));
1433 if (dst_out
-out_off
> src_out
)
1434 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1435 dst_out
- out_off
- src_out
);
1436 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1437 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1438 src
+1+src_nparam
+src_in
+src_out
,
1439 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1440 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1441 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1442 div_off
+src_map
->n_div
,
1443 dst_map
->n_div
- div_off
- src_map
->n_div
);
1446 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1447 struct isl_basic_map
*src_map
, isl_int
*src
,
1448 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1450 isl_int_set(dst
[0], src
[0]);
1451 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1454 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1455 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1460 if (!bmap1
|| !bmap2
)
1463 div_off
= bmap1
->n_div
;
1465 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1466 int i1
= isl_basic_map_alloc_equality(bmap1
);
1469 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1470 i_pos
, o_pos
, div_off
);
1473 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1474 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1477 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1478 i_pos
, o_pos
, div_off
);
1481 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1482 int i1
= isl_basic_map_alloc_div(bmap1
);
1485 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1486 i_pos
, o_pos
, div_off
);
1489 isl_basic_map_free(bmap2
);
1494 isl_basic_map_free(bmap1
);
1495 isl_basic_map_free(bmap2
);
1499 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1500 struct isl_basic_set
*bset2
, unsigned pos
)
1502 return (struct isl_basic_set
*)
1503 add_constraints((struct isl_basic_map
*)bset1
,
1504 (struct isl_basic_map
*)bset2
, 0, pos
);
1507 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1508 __isl_take isl_space
*dim
, unsigned extra
,
1509 unsigned n_eq
, unsigned n_ineq
)
1511 struct isl_basic_map
*ext
;
1521 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1522 base
->extra
>= base
->n_div
+ extra
;
1524 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1525 room_for_ineq(base
, n_ineq
)) {
1526 isl_space_free(dim
);
1530 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1531 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1532 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1533 extra
+= base
->extra
;
1535 n_ineq
+= base
->n_ineq
;
1537 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1543 ext
->sample
= isl_vec_copy(base
->sample
);
1544 flags
= base
->flags
;
1545 ext
= add_constraints(ext
, base
, 0, 0);
1548 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1554 isl_space_free(dim
);
1555 isl_basic_map_free(base
);
1559 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1560 __isl_take isl_space
*dim
, unsigned extra
,
1561 unsigned n_eq
, unsigned n_ineq
)
1563 return (struct isl_basic_set
*)
1564 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1565 extra
, n_eq
, n_ineq
);
1568 struct isl_basic_map
*isl_basic_map_extend_constraints(
1569 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1573 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1577 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1578 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1579 unsigned n_eq
, unsigned n_ineq
)
1581 struct isl_basic_map
*bmap
;
1586 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1590 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1593 isl_basic_map_free(base
);
1597 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1598 unsigned nparam
, unsigned dim
, unsigned extra
,
1599 unsigned n_eq
, unsigned n_ineq
)
1601 return (struct isl_basic_set
*)
1602 isl_basic_map_extend((struct isl_basic_map
*)base
,
1603 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1606 struct isl_basic_set
*isl_basic_set_extend_constraints(
1607 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1609 return (struct isl_basic_set
*)
1610 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1614 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1616 return (struct isl_basic_set
*)
1617 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1620 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1625 if (bmap
->ref
> 1) {
1627 bmap
= isl_basic_map_dup(bmap
);
1630 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1634 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1642 return isl_set_dup(set
);
1645 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1653 return isl_map_dup(map
);
1656 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1657 unsigned a_len
, unsigned b_len
)
1659 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1660 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1661 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1664 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1665 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1673 isl_assert(bmap
->ctx
,
1674 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1676 if (n1
== 0 || n2
== 0)
1679 bmap
= isl_basic_map_cow(bmap
);
1683 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1684 if (isl_blk_is_error(blk
))
1687 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1689 bmap
->eq
[i
] + pos
, n1
, n2
);
1691 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1693 bmap
->ineq
[i
] + pos
, n1
, n2
);
1695 for (i
= 0; i
< bmap
->n_div
; ++i
)
1697 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1699 isl_blk_free(bmap
->ctx
, blk
);
1701 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1702 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1703 return isl_basic_map_finalize(bmap
);
1705 isl_basic_map_free(bmap
);
1709 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1710 __isl_take isl_basic_set
*bset
, unsigned n
)
1718 nparam
= isl_basic_set_n_param(bset
);
1719 dim
= isl_basic_set_n_dim(bset
);
1720 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1722 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1724 isl_basic_set_free(bset
);
1728 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1734 total
= isl_basic_map_total_dim(bmap
);
1735 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1736 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1738 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1740 i
= isl_basic_map_alloc_equality(bmap
);
1744 isl_int_set_si(bmap
->eq
[i
][0], 1);
1745 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1746 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1747 isl_vec_free(bmap
->sample
);
1748 bmap
->sample
= NULL
;
1749 return isl_basic_map_finalize(bmap
);
1751 isl_basic_map_free(bmap
);
1755 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1757 return (struct isl_basic_set
*)
1758 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1761 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1764 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1766 isl_int
*t
= bmap
->div
[a
];
1767 bmap
->div
[a
] = bmap
->div
[b
];
1771 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1772 * div definitions accordingly.
1774 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1777 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1779 swap_div(bmap
, a
, b
);
1781 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1782 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1784 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1785 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1787 for (i
= 0; i
< bmap
->n_div
; ++i
)
1788 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1789 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1792 /* Eliminate the specified n dimensions starting at first from the
1793 * constraints, without removing the dimensions from the space.
1794 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1796 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1797 enum isl_dim_type type
, unsigned first
, unsigned n
)
1806 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1807 isl_die(map
->ctx
, isl_error_invalid
,
1808 "index out of bounds", goto error
);
1810 map
= isl_map_cow(map
);
1814 for (i
= 0; i
< map
->n
; ++i
) {
1815 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1825 /* Eliminate the specified n dimensions starting at first from the
1826 * constraints, without removing the dimensions from the space.
1827 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1829 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1830 enum isl_dim_type type
, unsigned first
, unsigned n
)
1832 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1835 /* Eliminate the specified n dimensions starting at first from the
1836 * constraints, without removing the dimensions from the space.
1837 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1839 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1840 unsigned first
, unsigned n
)
1842 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1845 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1846 __isl_take isl_basic_map
*bmap
)
1850 bmap
= isl_basic_map_eliminate_vars(bmap
,
1851 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1855 return isl_basic_map_finalize(bmap
);
1858 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1859 __isl_take isl_basic_set
*bset
)
1861 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1862 (struct isl_basic_map
*)bset
);
1865 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1874 map
= isl_map_cow(map
);
1878 for (i
= 0; i
< map
->n
; ++i
) {
1879 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1889 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1891 return isl_map_remove_divs(set
);
1894 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1895 enum isl_dim_type type
, unsigned first
, unsigned n
)
1899 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1901 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1903 bmap
= isl_basic_map_eliminate_vars(bmap
,
1904 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1907 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1909 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1912 isl_basic_map_free(bmap
);
1916 /* Return true if the definition of the given div (recursively) involves
1917 * any of the given variables.
1919 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1920 unsigned first
, unsigned n
)
1923 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1925 if (isl_int_is_zero(bmap
->div
[div
][0]))
1927 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1930 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1931 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1933 if (div_involves_vars(bmap
, i
, first
, n
))
1940 /* Try and add a lower and/or upper bound on "div" to "bmap"
1941 * based on inequality "i".
1942 * "total" is the total number of variables (excluding the divs).
1943 * "v" is a temporary object that can be used during the calculations.
1944 * If "lb" is set, then a lower bound should be constructed.
1945 * If "ub" is set, then an upper bound should be constructed.
1947 * The calling function has already checked that the inequality does not
1948 * reference "div", but we still need to check that the inequality is
1949 * of the right form. We'll consider the case where we want to construct
1950 * a lower bound. The construction of upper bounds is similar.
1952 * Let "div" be of the form
1954 * q = floor((a + f(x))/d)
1956 * We essentially check if constraint "i" is of the form
1960 * so that we can use it to derive a lower bound on "div".
1961 * However, we allow a slightly more general form
1965 * with the condition that the coefficients of g(x) - f(x) are all
1967 * Rewriting this constraint as
1971 * adding a + f(x) to both sides and dividing by d, we obtain
1973 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1975 * Taking the floor on both sides, we obtain
1977 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1981 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1983 * In the case of an upper bound, we construct the constraint
1985 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1988 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1989 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1990 unsigned total
, isl_int v
, int lb
, int ub
)
1994 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1996 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1997 bmap
->div
[div
][1 + 1 + j
]);
1998 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2001 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2002 bmap
->div
[div
][1 + 1 + j
]);
2003 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2009 bmap
= isl_basic_map_cow(bmap
);
2010 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2012 int k
= isl_basic_map_alloc_inequality(bmap
);
2015 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2016 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2017 bmap
->div
[div
][1 + j
]);
2018 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2019 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2021 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2024 int k
= isl_basic_map_alloc_inequality(bmap
);
2027 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2028 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2029 bmap
->div
[div
][1 + j
]);
2030 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2031 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2033 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2036 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2039 isl_basic_map_free(bmap
);
2043 /* This function is called right before "div" is eliminated from "bmap"
2044 * using Fourier-Motzkin.
2045 * Look through the constraints of "bmap" for constraints on the argument
2046 * of the integer division and use them to construct constraints on the
2047 * integer division itself. These constraints can then be combined
2048 * during the Fourier-Motzkin elimination.
2049 * Note that it is only useful to introduce lower bounds on "div"
2050 * if "bmap" already contains upper bounds on "div" as the newly
2051 * introduce lower bounds can then be combined with the pre-existing
2052 * upper bounds. Similarly for upper bounds.
2053 * We therefore first check if "bmap" contains any lower and/or upper bounds
2056 * It is interesting to note that the introduction of these constraints
2057 * can indeed lead to more accurate results, even when compared to
2058 * deriving constraints on the argument of "div" from constraints on "div".
2059 * Consider, for example, the set
2061 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2063 * The second constraint can be rewritten as
2065 * 2 * [(-i-2j+3)/4] + k >= 0
2067 * from which we can derive
2069 * -i - 2j + 3 >= -2k
2075 * Combined with the first constraint, we obtain
2077 * -3 <= 3 + 2k or k >= -3
2079 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2080 * the first constraint, we obtain
2082 * [(i + 2j)/4] >= [-3/4] = -1
2084 * Combining this constraint with the second constraint, we obtain
2088 static __isl_give isl_basic_map
*insert_bounds_on_div(
2089 __isl_take isl_basic_map
*bmap
, int div
)
2092 int check_lb
, check_ub
;
2099 if (isl_int_is_zero(bmap
->div
[div
][0]))
2102 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2106 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2107 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2114 if (!check_lb
&& !check_ub
)
2119 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2120 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2123 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2124 check_lb
, check_ub
);
2132 /* Remove all divs (recursively) involving any of the given dimensions
2133 * in their definitions.
2135 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2136 __isl_take isl_basic_map
*bmap
,
2137 enum isl_dim_type type
, unsigned first
, unsigned n
)
2143 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2145 first
+= isl_basic_map_offset(bmap
, type
);
2147 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2148 if (!div_involves_vars(bmap
, i
, first
, n
))
2150 bmap
= insert_bounds_on_div(bmap
, i
);
2151 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2159 isl_basic_map_free(bmap
);
2163 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2164 __isl_take isl_basic_set
*bset
,
2165 enum isl_dim_type type
, unsigned first
, unsigned n
)
2167 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2170 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2171 enum isl_dim_type type
, unsigned first
, unsigned n
)
2180 map
= isl_map_cow(map
);
2184 for (i
= 0; i
< map
->n
; ++i
) {
2185 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2196 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2197 enum isl_dim_type type
, unsigned first
, unsigned n
)
2199 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2203 /* Does the desciption of "bmap" depend on the specified dimensions?
2204 * We also check whether the dimensions appear in any of the div definitions.
2205 * In principle there is no need for this check. If the dimensions appear
2206 * in a div definition, they also appear in the defining constraints of that
2209 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2210 enum isl_dim_type type
, unsigned first
, unsigned n
)
2217 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2218 isl_die(bmap
->ctx
, isl_error_invalid
,
2219 "index out of bounds", return -1);
2221 first
+= isl_basic_map_offset(bmap
, type
);
2222 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2223 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2225 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2226 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2228 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2229 if (isl_int_is_zero(bmap
->div
[i
][0]))
2231 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2238 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2239 enum isl_dim_type type
, unsigned first
, unsigned n
)
2246 if (first
+ n
> isl_map_dim(map
, type
))
2247 isl_die(map
->ctx
, isl_error_invalid
,
2248 "index out of bounds", return -1);
2250 for (i
= 0; i
< map
->n
; ++i
) {
2251 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2253 if (involves
< 0 || involves
)
2260 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2261 enum isl_dim_type type
, unsigned first
, unsigned n
)
2263 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2266 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2267 enum isl_dim_type type
, unsigned first
, unsigned n
)
2269 return isl_map_involves_dims(set
, type
, first
, n
);
2272 /* Return true if the definition of the given div is unknown or depends
2275 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2278 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2280 if (isl_int_is_zero(bmap
->div
[div
][0]))
2283 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2284 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2286 if (div_is_unknown(bmap
, i
))
2293 /* Remove all divs that are unknown or defined in terms of unknown divs.
2295 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2296 __isl_take isl_basic_map
*bmap
)
2303 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2304 if (!div_is_unknown(bmap
, i
))
2306 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2315 /* Remove all divs that are unknown or defined in terms of unknown divs.
2317 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2318 __isl_take isl_basic_set
*bset
)
2320 return isl_basic_map_remove_unknown_divs(bset
);
2323 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2332 map
= isl_map_cow(map
);
2336 for (i
= 0; i
< map
->n
; ++i
) {
2337 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2347 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2349 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2352 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2353 __isl_take isl_basic_set
*bset
,
2354 enum isl_dim_type type
, unsigned first
, unsigned n
)
2356 return (isl_basic_set
*)
2357 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2360 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2361 enum isl_dim_type type
, unsigned first
, unsigned n
)
2368 map
= isl_map_cow(map
);
2371 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2373 for (i
= 0; i
< map
->n
; ++i
) {
2374 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2375 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2379 map
= isl_map_drop(map
, type
, first
, n
);
2386 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2387 enum isl_dim_type type
, unsigned first
, unsigned n
)
2389 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2392 /* Project out n inputs starting at first using Fourier-Motzkin */
2393 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2394 unsigned first
, unsigned n
)
2396 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2399 static void dump_term(struct isl_basic_map
*bmap
,
2400 isl_int c
, int pos
, FILE *out
)
2403 unsigned in
= isl_basic_map_n_in(bmap
);
2404 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2405 unsigned nparam
= isl_basic_map_n_param(bmap
);
2407 isl_int_print(out
, c
, 0);
2409 if (!isl_int_is_one(c
))
2410 isl_int_print(out
, c
, 0);
2411 if (pos
< 1 + nparam
) {
2412 name
= isl_space_get_dim_name(bmap
->dim
,
2413 isl_dim_param
, pos
- 1);
2415 fprintf(out
, "%s", name
);
2417 fprintf(out
, "p%d", pos
- 1);
2418 } else if (pos
< 1 + nparam
+ in
)
2419 fprintf(out
, "i%d", pos
- 1 - nparam
);
2420 else if (pos
< 1 + nparam
+ dim
)
2421 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2423 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2427 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2428 int sign
, FILE *out
)
2432 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2436 for (i
= 0, first
= 1; i
< len
; ++i
) {
2437 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2440 fprintf(out
, " + ");
2442 isl_int_abs(v
, c
[i
]);
2443 dump_term(bmap
, v
, i
, out
);
2450 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2451 const char *op
, FILE *out
, int indent
)
2455 fprintf(out
, "%*s", indent
, "");
2457 dump_constraint_sign(bmap
, c
, 1, out
);
2458 fprintf(out
, " %s ", op
);
2459 dump_constraint_sign(bmap
, c
, -1, out
);
2463 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2464 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2466 fprintf(out
, "%*s", indent
, "");
2467 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2472 static void dump_constraints(struct isl_basic_map
*bmap
,
2473 isl_int
**c
, unsigned n
,
2474 const char *op
, FILE *out
, int indent
)
2478 for (i
= 0; i
< n
; ++i
)
2479 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2482 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2486 unsigned total
= isl_basic_map_total_dim(bmap
);
2488 for (j
= 0; j
< 1 + total
; ++j
) {
2489 if (isl_int_is_zero(exp
[j
]))
2491 if (!first
&& isl_int_is_pos(exp
[j
]))
2493 dump_term(bmap
, exp
[j
], j
, out
);
2498 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2502 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2503 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2505 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2506 fprintf(out
, "%*s", indent
, "");
2507 fprintf(out
, "e%d = [(", i
);
2508 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2510 isl_int_print(out
, bmap
->div
[i
][0], 0);
2511 fprintf(out
, "]\n");
2515 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2516 FILE *out
, int indent
)
2519 fprintf(out
, "null basic set\n");
2523 fprintf(out
, "%*s", indent
, "");
2524 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2525 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2526 bset
->extra
, bset
->flags
);
2527 dump((struct isl_basic_map
*)bset
, out
, indent
);
2530 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2531 FILE *out
, int indent
)
2534 fprintf(out
, "null basic map\n");
2538 fprintf(out
, "%*s", indent
, "");
2539 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2540 "flags: %x, n_name: %d\n",
2542 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2543 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2544 dump(bmap
, out
, indent
);
2547 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2552 total
= isl_basic_map_total_dim(bmap
);
2553 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2554 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2555 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2556 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2560 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2563 struct isl_set
*set
;
2567 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2568 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2569 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2570 sizeof(struct isl_set
) +
2571 (n
- 1) * sizeof(struct isl_basic_set
*));
2575 set
->ctx
= dim
->ctx
;
2576 isl_ctx_ref(set
->ctx
);
2584 isl_space_free(dim
);
2588 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2589 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2591 struct isl_set
*set
;
2594 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2598 set
= isl_set_alloc_space(dims
, n
, flags
);
2602 /* Make sure "map" has room for at least "n" more basic maps.
2604 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2607 struct isl_map
*grown
= NULL
;
2611 isl_assert(map
->ctx
, n
>= 0, goto error
);
2612 if (map
->n
+ n
<= map
->size
)
2614 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2617 for (i
= 0; i
< map
->n
; ++i
) {
2618 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2626 isl_map_free(grown
);
2631 /* Make sure "set" has room for at least "n" more basic sets.
2633 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2635 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2638 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2641 struct isl_set
*dup
;
2646 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2649 for (i
= 0; i
< set
->n
; ++i
)
2650 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2654 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2656 return isl_map_from_basic_map(bset
);
2659 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2661 struct isl_map
*map
;
2666 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2667 return isl_map_add_basic_map(map
, bmap
);
2670 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2671 __isl_take isl_basic_set
*bset
)
2673 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2674 (struct isl_basic_map
*)bset
);
2677 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2687 isl_ctx_deref(set
->ctx
);
2688 for (i
= 0; i
< set
->n
; ++i
)
2689 isl_basic_set_free(set
->p
[i
]);
2690 isl_space_free(set
->dim
);
2696 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2701 fprintf(out
, "null set\n");
2705 fprintf(out
, "%*s", indent
, "");
2706 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2707 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2709 for (i
= 0; i
< set
->n
; ++i
) {
2710 fprintf(out
, "%*s", indent
, "");
2711 fprintf(out
, "basic set %d:\n", i
);
2712 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2716 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2721 fprintf(out
, "null map\n");
2725 fprintf(out
, "%*s", indent
, "");
2726 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2727 "flags: %x, n_name: %d\n",
2728 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2729 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2730 for (i
= 0; i
< map
->n
; ++i
) {
2731 fprintf(out
, "%*s", indent
, "");
2732 fprintf(out
, "basic map %d:\n", i
);
2733 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2737 struct isl_basic_map
*isl_basic_map_intersect_domain(
2738 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2740 struct isl_basic_map
*bmap_domain
;
2745 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2746 bset
->dim
, isl_dim_param
), goto error
);
2748 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2749 isl_assert(bset
->ctx
,
2750 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2752 bmap
= isl_basic_map_cow(bmap
);
2755 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2756 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2757 bmap_domain
= isl_basic_map_from_domain(bset
);
2758 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2760 bmap
= isl_basic_map_simplify(bmap
);
2761 return isl_basic_map_finalize(bmap
);
2763 isl_basic_map_free(bmap
);
2764 isl_basic_set_free(bset
);
2768 struct isl_basic_map
*isl_basic_map_intersect_range(
2769 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2771 struct isl_basic_map
*bmap_range
;
2776 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2777 bset
->dim
, isl_dim_param
), goto error
);
2779 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2780 isl_assert(bset
->ctx
,
2781 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2783 if (isl_basic_set_is_universe(bset
)) {
2784 isl_basic_set_free(bset
);
2788 bmap
= isl_basic_map_cow(bmap
);
2791 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2792 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2793 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2794 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2796 bmap
= isl_basic_map_simplify(bmap
);
2797 return isl_basic_map_finalize(bmap
);
2799 isl_basic_map_free(bmap
);
2800 isl_basic_set_free(bset
);
2804 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2813 total
= 1 + isl_basic_map_total_dim(bmap
);
2814 if (total
!= vec
->size
)
2819 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2820 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2821 if (!isl_int_is_zero(s
)) {
2827 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2828 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2829 if (isl_int_is_neg(s
)) {
2840 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2842 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2845 struct isl_basic_map
*isl_basic_map_intersect(
2846 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2848 struct isl_vec
*sample
= NULL
;
2850 if (!bmap1
|| !bmap2
)
2853 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2854 bmap2
->dim
, isl_dim_param
), goto error
);
2855 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2856 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2857 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2858 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2859 return isl_basic_map_intersect(bmap2
, bmap1
);
2861 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2862 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2863 isl_assert(bmap1
->ctx
,
2864 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2866 if (bmap1
->sample
&&
2867 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2868 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2869 sample
= isl_vec_copy(bmap1
->sample
);
2870 else if (bmap2
->sample
&&
2871 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2872 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2873 sample
= isl_vec_copy(bmap2
->sample
);
2875 bmap1
= isl_basic_map_cow(bmap1
);
2878 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2879 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2880 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2883 isl_vec_free(sample
);
2885 isl_vec_free(bmap1
->sample
);
2886 bmap1
->sample
= sample
;
2889 bmap1
= isl_basic_map_simplify(bmap1
);
2890 return isl_basic_map_finalize(bmap1
);
2893 isl_vec_free(sample
);
2894 isl_basic_map_free(bmap1
);
2895 isl_basic_map_free(bmap2
);
2899 struct isl_basic_set
*isl_basic_set_intersect(
2900 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2902 return (struct isl_basic_set
*)
2903 isl_basic_map_intersect(
2904 (struct isl_basic_map
*)bset1
,
2905 (struct isl_basic_map
*)bset2
);
2908 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2909 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2911 return isl_basic_set_intersect(bset1
, bset2
);
2914 /* Special case of isl_map_intersect, where both map1 and map2
2915 * are convex, without any divs and such that either map1 or map2
2916 * contains a single constraint. This constraint is then simply
2917 * added to the other map.
2919 static __isl_give isl_map
*map_intersect_add_constraint(
2920 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2922 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2923 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2924 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2925 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2927 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2928 return isl_map_intersect(map2
, map1
);
2930 isl_assert(map2
->ctx
,
2931 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2933 map1
= isl_map_cow(map1
);
2936 if (isl_map_plain_is_empty(map1
)) {
2940 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2941 if (map2
->p
[0]->n_eq
== 1)
2942 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2944 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2945 map2
->p
[0]->ineq
[0]);
2947 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2948 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2952 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2953 isl_basic_map_free(map1
->p
[0]);
2966 /* map2 may be either a parameter domain or a map living in the same
2969 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2970 __isl_take isl_map
*map2
)
2979 if ((isl_map_plain_is_empty(map1
) ||
2980 isl_map_plain_is_universe(map2
)) &&
2981 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2985 if ((isl_map_plain_is_empty(map2
) ||
2986 isl_map_plain_is_universe(map1
)) &&
2987 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2992 if (map1
->n
== 1 && map2
->n
== 1 &&
2993 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2994 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2995 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2996 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2997 return map_intersect_add_constraint(map1
, map2
);
2999 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3000 isl_space_dim(map2
->dim
, isl_dim_param
))
3001 isl_assert(map1
->ctx
,
3002 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3004 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3005 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3006 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3008 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3009 map1
->n
* map2
->n
, flags
);
3012 for (i
= 0; i
< map1
->n
; ++i
)
3013 for (j
= 0; j
< map2
->n
; ++j
) {
3014 struct isl_basic_map
*part
;
3015 part
= isl_basic_map_intersect(
3016 isl_basic_map_copy(map1
->p
[i
]),
3017 isl_basic_map_copy(map2
->p
[j
]));
3018 if (isl_basic_map_is_empty(part
) < 0)
3019 part
= isl_basic_map_free(part
);
3020 result
= isl_map_add_basic_map(result
, part
);
3033 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3034 __isl_take isl_map
*map2
)
3038 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3039 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3040 "spaces don't match", goto error
);
3041 return map_intersect_internal(map1
, map2
);
3048 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3049 __isl_take isl_map
*map2
)
3051 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3054 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3056 return (struct isl_set
*)
3057 isl_map_intersect((struct isl_map
*)set1
,
3058 (struct isl_map
*)set2
);
3061 /* map_intersect_internal accepts intersections
3062 * with parameter domains, so we can just call that function.
3064 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3065 __isl_take isl_set
*params
)
3067 return map_intersect_internal(map
, params
);
3070 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3071 __isl_take isl_map
*map2
)
3073 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3076 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3077 __isl_take isl_set
*params
)
3079 return isl_map_intersect_params(set
, params
);
3082 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3085 struct isl_basic_set
*bset
;
3090 bmap
= isl_basic_map_cow(bmap
);
3093 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3094 in
= isl_basic_map_n_in(bmap
);
3095 bset
= isl_basic_set_from_basic_map(bmap
);
3096 bset
= isl_basic_set_swap_vars(bset
, in
);
3097 return isl_basic_map_from_basic_set(bset
, dim
);
3100 static __isl_give isl_basic_map
*basic_map_space_reset(
3101 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3107 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3110 space
= isl_basic_map_get_space(bmap
);
3111 space
= isl_space_reset(space
, type
);
3112 bmap
= isl_basic_map_reset_space(bmap
, space
);
3116 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3117 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3118 unsigned pos
, unsigned n
)
3121 struct isl_basic_map
*res
;
3122 struct isl_dim_map
*dim_map
;
3123 unsigned total
, off
;
3124 enum isl_dim_type t
;
3127 return basic_map_space_reset(bmap
, type
);
3132 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3134 total
= isl_basic_map_total_dim(bmap
) + n
;
3135 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3137 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3139 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3141 unsigned size
= isl_basic_map_dim(bmap
, t
);
3142 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3144 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3145 pos
, size
- pos
, off
+ pos
+ n
);
3147 off
+= isl_space_dim(res_dim
, t
);
3149 isl_dim_map_div(dim_map
, bmap
, off
);
3151 res
= isl_basic_map_alloc_space(res_dim
,
3152 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3153 if (isl_basic_map_is_rational(bmap
))
3154 res
= isl_basic_map_set_rational(res
);
3155 if (isl_basic_map_plain_is_empty(bmap
)) {
3156 isl_basic_map_free(bmap
);
3158 return isl_basic_map_set_to_empty(res
);
3160 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3161 return isl_basic_map_finalize(res
);
3164 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3165 __isl_take isl_basic_set
*bset
,
3166 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3168 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3171 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3172 enum isl_dim_type type
, unsigned n
)
3176 return isl_basic_map_insert_dims(bmap
, type
,
3177 isl_basic_map_dim(bmap
, type
), n
);
3180 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3181 enum isl_dim_type type
, unsigned n
)
3185 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3186 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3188 isl_basic_set_free(bset
);
3192 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3193 enum isl_dim_type type
)
3197 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3200 space
= isl_map_get_space(map
);
3201 space
= isl_space_reset(space
, type
);
3202 map
= isl_map_reset_space(map
, space
);
3206 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3207 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3212 return map_space_reset(map
, type
);
3214 map
= isl_map_cow(map
);
3218 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3222 for (i
= 0; i
< map
->n
; ++i
) {
3223 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3234 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3235 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3237 return isl_map_insert_dims(set
, type
, pos
, n
);
3240 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3241 enum isl_dim_type type
, unsigned n
)
3245 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3248 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3249 enum isl_dim_type type
, unsigned n
)
3253 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3254 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3260 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3261 __isl_take isl_basic_map
*bmap
,
3262 enum isl_dim_type dst_type
, unsigned dst_pos
,
3263 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3265 struct isl_dim_map
*dim_map
;
3266 struct isl_basic_map
*res
;
3267 enum isl_dim_type t
;
3268 unsigned total
, off
;
3275 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3278 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3281 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3283 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3284 pos(bmap
->dim
, src_type
) + src_pos
+
3285 ((src_type
< dst_type
) ? n
: 0)) {
3286 bmap
= isl_basic_map_cow(bmap
);
3290 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3291 src_type
, src_pos
, n
);
3295 bmap
= isl_basic_map_finalize(bmap
);
3300 total
= isl_basic_map_total_dim(bmap
);
3301 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3304 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3305 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3306 if (t
== dst_type
) {
3307 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3310 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3313 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3314 dst_pos
, size
- dst_pos
, off
);
3315 off
+= size
- dst_pos
;
3316 } else if (t
== src_type
) {
3317 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3320 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3321 src_pos
+ n
, size
- src_pos
- n
, off
);
3322 off
+= size
- src_pos
- n
;
3324 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3328 isl_dim_map_div(dim_map
, bmap
, off
);
3330 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3331 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3332 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3336 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3337 src_type
, src_pos
, n
);
3341 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3342 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3343 bmap
= isl_basic_map_finalize(bmap
);
3347 isl_basic_map_free(bmap
);
3351 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3352 enum isl_dim_type dst_type
, unsigned dst_pos
,
3353 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3355 return (isl_basic_set
*)isl_basic_map_move_dims(
3356 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3359 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3360 enum isl_dim_type dst_type
, unsigned dst_pos
,
3361 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3365 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3366 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3367 src_type
, src_pos
, n
);
3373 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3374 enum isl_dim_type dst_type
, unsigned dst_pos
,
3375 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3384 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3387 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3390 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3392 map
= isl_map_cow(map
);
3396 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3400 for (i
= 0; i
< map
->n
; ++i
) {
3401 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3403 src_type
, src_pos
, n
);
3414 /* Move the specified dimensions to the last columns right before
3415 * the divs. Don't change the dimension specification of bmap.
3416 * That's the responsibility of the caller.
3418 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3419 enum isl_dim_type type
, unsigned first
, unsigned n
)
3421 struct isl_dim_map
*dim_map
;
3422 struct isl_basic_map
*res
;
3423 enum isl_dim_type t
;
3424 unsigned total
, off
;
3428 if (pos(bmap
->dim
, type
) + first
+ n
==
3429 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3432 total
= isl_basic_map_total_dim(bmap
);
3433 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3436 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3437 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3439 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3442 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3443 first
, n
, total
- bmap
->n_div
- n
);
3444 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3445 first
+ n
, size
- (first
+ n
), off
);
3446 off
+= size
- (first
+ n
);
3448 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3452 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3454 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3455 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3456 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3460 /* Insert "n" rows in the divs of "bmap".
3462 * The number of columns is not changed, which means that the last
3463 * dimensions of "bmap" are being reintepreted as the new divs.
3464 * The space of "bmap" is not adjusted, however, which means
3465 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3466 * from the space of "bmap" is the responsibility of the caller.
3468 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3476 bmap
= isl_basic_map_cow(bmap
);
3480 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3481 old
= bmap
->block2
.data
;
3482 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3483 (bmap
->extra
+ n
) * (1 + row_size
));
3484 if (!bmap
->block2
.data
)
3485 return isl_basic_map_free(bmap
);
3486 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3488 return isl_basic_map_free(bmap
);
3489 for (i
= 0; i
< n
; ++i
) {
3490 new_div
[i
] = bmap
->block2
.data
+
3491 (bmap
->extra
+ i
) * (1 + row_size
);
3492 isl_seq_clr(new_div
[i
], 1 + row_size
);
3494 for (i
= 0; i
< bmap
->extra
; ++i
)
3495 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3497 bmap
->div
= new_div
;
3504 /* Turn the n dimensions of type type, starting at first
3505 * into existentially quantified variables.
3507 __isl_give isl_basic_map
*isl_basic_map_project_out(
3508 __isl_take isl_basic_map
*bmap
,
3509 enum isl_dim_type type
, unsigned first
, unsigned n
)
3512 return basic_map_space_reset(bmap
, type
);
3517 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3518 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3520 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3523 bmap
= move_last(bmap
, type
, first
, n
);
3524 bmap
= isl_basic_map_cow(bmap
);
3525 bmap
= insert_div_rows(bmap
, n
);
3529 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3532 bmap
= isl_basic_map_simplify(bmap
);
3533 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3534 return isl_basic_map_finalize(bmap
);
3536 isl_basic_map_free(bmap
);
3540 /* Turn the n dimensions of type type, starting at first
3541 * into existentially quantified variables.
3543 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3544 enum isl_dim_type type
, unsigned first
, unsigned n
)
3546 return (isl_basic_set
*)isl_basic_map_project_out(
3547 (isl_basic_map
*)bset
, type
, first
, n
);
3550 /* Turn the n dimensions of type type, starting at first
3551 * into existentially quantified variables.
3553 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3554 enum isl_dim_type type
, unsigned first
, unsigned n
)
3562 return map_space_reset(map
, type
);
3564 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3566 map
= isl_map_cow(map
);
3570 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3574 for (i
= 0; i
< map
->n
; ++i
) {
3575 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3586 /* Turn the n dimensions of type type, starting at first
3587 * into existentially quantified variables.
3589 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3590 enum isl_dim_type type
, unsigned first
, unsigned n
)
3592 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3595 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3599 for (i
= 0; i
< n
; ++i
) {
3600 j
= isl_basic_map_alloc_div(bmap
);
3603 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3607 isl_basic_map_free(bmap
);
3611 struct isl_basic_map
*isl_basic_map_apply_range(
3612 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3614 isl_space
*dim_result
= NULL
;
3615 struct isl_basic_map
*bmap
;
3616 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3617 struct isl_dim_map
*dim_map1
, *dim_map2
;
3619 if (!bmap1
|| !bmap2
)
3621 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3622 bmap2
->dim
, isl_dim_param
))
3623 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3624 "parameters don't match", goto error
);
3625 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3626 bmap2
->dim
, isl_dim_in
))
3627 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3628 "spaces don't match", goto error
);
3630 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3631 isl_space_copy(bmap2
->dim
));
3633 n_in
= isl_basic_map_n_in(bmap1
);
3634 n_out
= isl_basic_map_n_out(bmap2
);
3635 n
= isl_basic_map_n_out(bmap1
);
3636 nparam
= isl_basic_map_n_param(bmap1
);
3638 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3639 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3640 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3641 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3642 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3643 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3644 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3645 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3646 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3647 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3648 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3650 bmap
= isl_basic_map_alloc_space(dim_result
,
3651 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3652 bmap1
->n_eq
+ bmap2
->n_eq
,
3653 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3654 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3655 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3656 bmap
= add_divs(bmap
, n
);
3657 bmap
= isl_basic_map_simplify(bmap
);
3658 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3659 return isl_basic_map_finalize(bmap
);
3661 isl_basic_map_free(bmap1
);
3662 isl_basic_map_free(bmap2
);
3666 struct isl_basic_set
*isl_basic_set_apply(
3667 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3672 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3675 return (struct isl_basic_set
*)
3676 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3678 isl_basic_set_free(bset
);
3679 isl_basic_map_free(bmap
);
3683 struct isl_basic_map
*isl_basic_map_apply_domain(
3684 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3686 if (!bmap1
|| !bmap2
)
3689 isl_assert(bmap1
->ctx
,
3690 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3691 isl_assert(bmap1
->ctx
,
3692 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3695 bmap1
= isl_basic_map_reverse(bmap1
);
3696 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3697 return isl_basic_map_reverse(bmap1
);
3699 isl_basic_map_free(bmap1
);
3700 isl_basic_map_free(bmap2
);
3704 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3705 * A \cap B -> f(A) + f(B)
3707 struct isl_basic_map
*isl_basic_map_sum(
3708 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3710 unsigned n_in
, n_out
, nparam
, total
, pos
;
3711 struct isl_basic_map
*bmap
= NULL
;
3712 struct isl_dim_map
*dim_map1
, *dim_map2
;
3715 if (!bmap1
|| !bmap2
)
3718 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3721 nparam
= isl_basic_map_n_param(bmap1
);
3722 n_in
= isl_basic_map_n_in(bmap1
);
3723 n_out
= isl_basic_map_n_out(bmap1
);
3725 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3726 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3727 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3728 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3729 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3730 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3731 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3732 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3733 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3734 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3735 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3737 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3738 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3739 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3740 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3741 for (i
= 0; i
< n_out
; ++i
) {
3742 int j
= isl_basic_map_alloc_equality(bmap
);
3745 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3746 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3747 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3748 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3750 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3751 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3752 bmap
= add_divs(bmap
, 2 * n_out
);
3754 bmap
= isl_basic_map_simplify(bmap
);
3755 return isl_basic_map_finalize(bmap
);
3757 isl_basic_map_free(bmap
);
3758 isl_basic_map_free(bmap1
);
3759 isl_basic_map_free(bmap2
);
3763 /* Given two maps A -> f(A) and B -> g(B), construct a map
3764 * A \cap B -> f(A) + f(B)
3766 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3768 struct isl_map
*result
;
3774 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3776 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3777 map1
->n
* map2
->n
, 0);
3780 for (i
= 0; i
< map1
->n
; ++i
)
3781 for (j
= 0; j
< map2
->n
; ++j
) {
3782 struct isl_basic_map
*part
;
3783 part
= isl_basic_map_sum(
3784 isl_basic_map_copy(map1
->p
[i
]),
3785 isl_basic_map_copy(map2
->p
[j
]));
3786 if (isl_basic_map_is_empty(part
))
3787 isl_basic_map_free(part
);
3789 result
= isl_map_add_basic_map(result
, part
);
3802 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3803 __isl_take isl_set
*set2
)
3805 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3808 /* Given a basic map A -> f(A), construct A -> -f(A).
3810 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3815 bmap
= isl_basic_map_cow(bmap
);
3819 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3820 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3821 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3822 for (j
= 0; j
< n
; ++j
)
3823 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3824 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3825 for (j
= 0; j
< n
; ++j
)
3826 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3827 for (i
= 0; i
< bmap
->n_div
; ++i
)
3828 for (j
= 0; j
< n
; ++j
)
3829 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3830 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3831 return isl_basic_map_finalize(bmap
);
3834 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3836 return isl_basic_map_neg(bset
);
3839 /* Given a map A -> f(A), construct A -> -f(A).
3841 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3845 map
= isl_map_cow(map
);
3849 for (i
= 0; i
< map
->n
; ++i
) {
3850 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3861 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3863 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3866 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3867 * A -> floor(f(A)/d).
3869 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3872 unsigned n_in
, n_out
, nparam
, total
, pos
;
3873 struct isl_basic_map
*result
= NULL
;
3874 struct isl_dim_map
*dim_map
;
3880 nparam
= isl_basic_map_n_param(bmap
);
3881 n_in
= isl_basic_map_n_in(bmap
);
3882 n_out
= isl_basic_map_n_out(bmap
);
3884 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3885 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3886 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3887 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3888 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3889 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3891 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3892 bmap
->n_div
+ n_out
,
3893 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3894 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3895 result
= add_divs(result
, n_out
);
3896 for (i
= 0; i
< n_out
; ++i
) {
3898 j
= isl_basic_map_alloc_inequality(result
);
3901 isl_seq_clr(result
->ineq
[j
], 1+total
);
3902 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3903 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3904 j
= isl_basic_map_alloc_inequality(result
);
3907 isl_seq_clr(result
->ineq
[j
], 1+total
);
3908 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3909 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3910 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3913 result
= isl_basic_map_simplify(result
);
3914 return isl_basic_map_finalize(result
);
3916 isl_basic_map_free(result
);
3920 /* Given a map A -> f(A) and an integer d, construct a map
3921 * A -> floor(f(A)/d).
3923 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3927 map
= isl_map_cow(map
);
3931 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3932 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3933 for (i
= 0; i
< map
->n
; ++i
) {
3934 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3945 /* Given a map A -> f(A) and an integer d, construct a map
3946 * A -> floor(f(A)/d).
3948 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3949 __isl_take isl_val
*d
)
3953 if (!isl_val_is_int(d
))
3954 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3955 "expecting integer denominator", goto error
);
3956 map
= isl_map_floordiv(map
, d
->n
);
3965 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3971 i
= isl_basic_map_alloc_equality(bmap
);
3974 nparam
= isl_basic_map_n_param(bmap
);
3975 n_in
= isl_basic_map_n_in(bmap
);
3976 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3977 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3978 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3979 return isl_basic_map_finalize(bmap
);
3981 isl_basic_map_free(bmap
);
3985 /* Add a constraints to "bmap" expressing i_pos < o_pos
3987 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3993 i
= isl_basic_map_alloc_inequality(bmap
);
3996 nparam
= isl_basic_map_n_param(bmap
);
3997 n_in
= isl_basic_map_n_in(bmap
);
3998 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3999 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4000 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4001 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4002 return isl_basic_map_finalize(bmap
);
4004 isl_basic_map_free(bmap
);
4008 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4010 static __isl_give isl_basic_map
*var_less_or_equal(
4011 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4017 i
= isl_basic_map_alloc_inequality(bmap
);
4020 nparam
= isl_basic_map_n_param(bmap
);
4021 n_in
= isl_basic_map_n_in(bmap
);
4022 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4023 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4024 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4025 return isl_basic_map_finalize(bmap
);
4027 isl_basic_map_free(bmap
);
4031 /* Add a constraints to "bmap" expressing i_pos > o_pos
4033 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4039 i
= isl_basic_map_alloc_inequality(bmap
);
4042 nparam
= isl_basic_map_n_param(bmap
);
4043 n_in
= isl_basic_map_n_in(bmap
);
4044 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4045 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4046 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4047 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4048 return isl_basic_map_finalize(bmap
);
4050 isl_basic_map_free(bmap
);
4054 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4056 static __isl_give isl_basic_map
*var_more_or_equal(
4057 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4063 i
= isl_basic_map_alloc_inequality(bmap
);
4066 nparam
= isl_basic_map_n_param(bmap
);
4067 n_in
= isl_basic_map_n_in(bmap
);
4068 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4069 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4070 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4071 return isl_basic_map_finalize(bmap
);
4073 isl_basic_map_free(bmap
);
4077 __isl_give isl_basic_map
*isl_basic_map_equal(
4078 __isl_take isl_space
*dim
, unsigned n_equal
)
4081 struct isl_basic_map
*bmap
;
4082 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4085 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4086 bmap
= var_equal(bmap
, i
);
4087 return isl_basic_map_finalize(bmap
);
4090 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4092 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4096 struct isl_basic_map
*bmap
;
4097 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4100 for (i
= 0; i
< pos
&& bmap
; ++i
)
4101 bmap
= var_equal(bmap
, i
);
4103 bmap
= var_less(bmap
, pos
);
4104 return isl_basic_map_finalize(bmap
);
4107 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4109 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4110 __isl_take isl_space
*dim
, unsigned pos
)
4113 isl_basic_map
*bmap
;
4115 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4116 for (i
= 0; i
< pos
; ++i
)
4117 bmap
= var_equal(bmap
, i
);
4118 bmap
= var_less_or_equal(bmap
, pos
);
4119 return isl_basic_map_finalize(bmap
);
4122 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4124 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4128 struct isl_basic_map
*bmap
;
4129 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4132 for (i
= 0; i
< pos
&& bmap
; ++i
)
4133 bmap
= var_equal(bmap
, i
);
4135 bmap
= var_more(bmap
, pos
);
4136 return isl_basic_map_finalize(bmap
);
4139 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4141 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4142 __isl_take isl_space
*dim
, unsigned pos
)
4145 isl_basic_map
*bmap
;
4147 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4148 for (i
= 0; i
< pos
; ++i
)
4149 bmap
= var_equal(bmap
, i
);
4150 bmap
= var_more_or_equal(bmap
, pos
);
4151 return isl_basic_map_finalize(bmap
);
4154 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4155 unsigned n
, int equal
)
4157 struct isl_map
*map
;
4160 if (n
== 0 && equal
)
4161 return isl_map_universe(dims
);
4163 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4165 for (i
= 0; i
+ 1 < n
; ++i
)
4166 map
= isl_map_add_basic_map(map
,
4167 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4170 map
= isl_map_add_basic_map(map
,
4171 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4173 map
= isl_map_add_basic_map(map
,
4174 isl_basic_map_less_at(dims
, n
- 1));
4176 isl_space_free(dims
);
4181 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4185 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4188 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4190 return map_lex_lte_first(dim
, n
, 0);
4193 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4195 return map_lex_lte_first(dim
, n
, 1);
4198 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4200 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4203 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4205 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4208 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4209 unsigned n
, int equal
)
4211 struct isl_map
*map
;
4214 if (n
== 0 && equal
)
4215 return isl_map_universe(dims
);
4217 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4219 for (i
= 0; i
+ 1 < n
; ++i
)
4220 map
= isl_map_add_basic_map(map
,
4221 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4224 map
= isl_map_add_basic_map(map
,
4225 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4227 map
= isl_map_add_basic_map(map
,
4228 isl_basic_map_more_at(dims
, n
- 1));
4230 isl_space_free(dims
);
4235 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4239 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4242 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4244 return map_lex_gte_first(dim
, n
, 0);
4247 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4249 return map_lex_gte_first(dim
, n
, 1);
4252 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4254 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4257 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4259 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4262 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4263 __isl_take isl_set
*set2
)
4266 map
= isl_map_lex_le(isl_set_get_space(set1
));
4267 map
= isl_map_intersect_domain(map
, set1
);
4268 map
= isl_map_intersect_range(map
, set2
);
4272 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4273 __isl_take isl_set
*set2
)
4276 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4277 map
= isl_map_intersect_domain(map
, set1
);
4278 map
= isl_map_intersect_range(map
, set2
);
4282 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4283 __isl_take isl_set
*set2
)
4286 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4287 map
= isl_map_intersect_domain(map
, set1
);
4288 map
= isl_map_intersect_range(map
, set2
);
4292 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4293 __isl_take isl_set
*set2
)
4296 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4297 map
= isl_map_intersect_domain(map
, set1
);
4298 map
= isl_map_intersect_range(map
, set2
);
4302 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4303 __isl_take isl_map
*map2
)
4306 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4307 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4308 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4312 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4313 __isl_take isl_map
*map2
)
4316 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4317 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4318 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4322 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4323 __isl_take isl_map
*map2
)
4326 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4327 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4328 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4332 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4333 __isl_take isl_map
*map2
)
4336 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4337 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4338 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4342 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4343 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4345 struct isl_basic_map
*bmap
;
4347 bset
= isl_basic_set_cow(bset
);
4351 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4352 isl_space_free(bset
->dim
);
4353 bmap
= (struct isl_basic_map
*) bset
;
4355 return isl_basic_map_finalize(bmap
);
4357 isl_basic_set_free(bset
);
4358 isl_space_free(dim
);
4362 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4366 if (bmap
->dim
->n_in
== 0)
4367 return (struct isl_basic_set
*)bmap
;
4368 bmap
= isl_basic_map_cow(bmap
);
4371 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4374 bmap
= isl_basic_map_finalize(bmap
);
4375 return (struct isl_basic_set
*)bmap
;
4377 isl_basic_map_free(bmap
);
4381 /* For a div d = floor(f/m), add the constraint
4385 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4386 unsigned pos
, isl_int
*div
)
4389 unsigned total
= isl_basic_map_total_dim(bmap
);
4391 i
= isl_basic_map_alloc_inequality(bmap
);
4394 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4395 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4400 /* For a div d = floor(f/m), add the constraint
4402 * -(f-(n-1)) + m d >= 0
4404 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4405 unsigned pos
, isl_int
*div
)
4408 unsigned total
= isl_basic_map_total_dim(bmap
);
4410 i
= isl_basic_map_alloc_inequality(bmap
);
4413 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4414 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4415 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4416 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4421 /* For a div d = floor(f/m), add the constraints
4424 * -(f-(n-1)) + m d >= 0
4426 * Note that the second constraint is the negation of
4430 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4431 unsigned pos
, isl_int
*div
)
4433 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4435 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4440 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4441 unsigned pos
, isl_int
*div
)
4443 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4447 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4449 unsigned total
= isl_basic_map_total_dim(bmap
);
4450 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4452 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4456 /* For each known div d = floor(f/m), add the constraints
4459 * -(f-(n-1)) + m d >= 0
4461 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4462 __isl_take isl_basic_map
*bmap
)
4469 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4472 bmap
= isl_basic_map_cow(bmap
);
4473 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
4476 for (i
= 0; i
< n_div
; ++i
) {
4477 if (isl_int_is_zero(bmap
->div
[i
][0]))
4479 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4480 return isl_basic_map_free(bmap
);
4483 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4484 bmap
= isl_basic_map_finalize(bmap
);
4488 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4490 * In particular, if this div is of the form d = floor(f/m),
4491 * then add the constraint
4495 * if sign < 0 or the constraint
4497 * -(f-(n-1)) + m d >= 0
4501 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4502 unsigned div
, int sign
)
4510 total
= isl_basic_map_total_dim(bmap
);
4511 div_pos
= total
- bmap
->n_div
+ div
;
4514 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4516 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4519 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4521 return isl_basic_map_add_div_constraints(bset
, div
);
4524 struct isl_basic_set
*isl_basic_map_underlying_set(
4525 struct isl_basic_map
*bmap
)
4529 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4531 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4532 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4533 return (struct isl_basic_set
*)bmap
;
4534 bmap
= isl_basic_map_cow(bmap
);
4537 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4540 bmap
->extra
-= bmap
->n_div
;
4542 bmap
= isl_basic_map_finalize(bmap
);
4543 return (struct isl_basic_set
*)bmap
;
4545 isl_basic_map_free(bmap
);
4549 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4550 __isl_take isl_basic_set
*bset
)
4552 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4555 /* Replace each element in "list" by the result of applying
4556 * isl_basic_set_underlying_set to the element.
4558 __isl_give isl_basic_set_list
*isl_basic_set_list_underlying_set(
4559 __isl_take isl_basic_set_list
*list
)
4566 n
= isl_basic_set_list_n_basic_set(list
);
4567 for (i
= 0; i
< n
; ++i
) {
4568 isl_basic_set
*bset
;
4570 bset
= isl_basic_set_list_get_basic_set(list
, i
);
4571 bset
= isl_basic_set_underlying_set(bset
);
4572 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4578 struct isl_basic_map
*isl_basic_map_overlying_set(
4579 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4581 struct isl_basic_map
*bmap
;
4582 struct isl_ctx
*ctx
;
4589 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4590 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4591 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4593 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4594 isl_basic_map_free(like
);
4595 return (struct isl_basic_map
*)bset
;
4597 bset
= isl_basic_set_cow(bset
);
4600 total
= bset
->dim
->n_out
+ bset
->extra
;
4601 bmap
= (struct isl_basic_map
*)bset
;
4602 isl_space_free(bmap
->dim
);
4603 bmap
->dim
= isl_space_copy(like
->dim
);
4606 bmap
->n_div
= like
->n_div
;
4607 bmap
->extra
+= like
->n_div
;
4611 ltotal
= total
- bmap
->extra
+ like
->extra
;
4614 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4615 bmap
->extra
* (1 + 1 + total
));
4616 if (isl_blk_is_error(bmap
->block2
))
4618 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4622 for (i
= 0; i
< bmap
->extra
; ++i
)
4623 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4624 for (i
= 0; i
< like
->n_div
; ++i
) {
4625 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4626 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4628 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4630 isl_basic_map_free(like
);
4631 bmap
= isl_basic_map_simplify(bmap
);
4632 bmap
= isl_basic_map_finalize(bmap
);
4635 isl_basic_map_free(like
);
4636 isl_basic_set_free(bset
);
4640 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4641 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4643 return (struct isl_basic_set
*)
4644 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4647 struct isl_set
*isl_set_from_underlying_set(
4648 struct isl_set
*set
, struct isl_basic_set
*like
)
4654 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4656 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4657 isl_basic_set_free(like
);
4660 set
= isl_set_cow(set
);
4663 for (i
= 0; i
< set
->n
; ++i
) {
4664 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4665 isl_basic_set_copy(like
));
4669 isl_space_free(set
->dim
);
4670 set
->dim
= isl_space_copy(like
->dim
);
4673 isl_basic_set_free(like
);
4676 isl_basic_set_free(like
);
4681 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4685 map
= isl_map_cow(map
);
4688 map
->dim
= isl_space_cow(map
->dim
);
4692 for (i
= 1; i
< map
->n
; ++i
)
4693 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4695 for (i
= 0; i
< map
->n
; ++i
) {
4696 map
->p
[i
] = (struct isl_basic_map
*)
4697 isl_basic_map_underlying_set(map
->p
[i
]);
4702 map
->dim
= isl_space_underlying(map
->dim
, 0);
4704 isl_space_free(map
->dim
);
4705 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4709 return (struct isl_set
*)map
;
4715 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4717 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4720 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4721 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4723 bmap
= isl_basic_map_cow(bmap
);
4727 isl_space_free(bmap
->dim
);
4730 bmap
= isl_basic_map_finalize(bmap
);
4734 isl_basic_map_free(bmap
);
4735 isl_space_free(dim
);
4739 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4740 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4742 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4746 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4747 __isl_take isl_space
*dim
)
4751 map
= isl_map_cow(map
);
4755 for (i
= 0; i
< map
->n
; ++i
) {
4756 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4757 isl_space_copy(dim
));
4761 isl_space_free(map
->dim
);
4767 isl_space_free(dim
);
4771 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4772 __isl_take isl_space
*dim
)
4774 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4777 /* Compute the parameter domain of the given basic set.
4779 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4784 if (isl_basic_set_is_params(bset
))
4787 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4788 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4789 space
= isl_basic_set_get_space(bset
);
4790 space
= isl_space_params(space
);
4791 bset
= isl_basic_set_reset_space(bset
, space
);
4795 /* Construct a zero-dimensional basic set with the given parameter domain.
4797 __isl_give isl_basic_set
*isl_basic_set_from_params(
4798 __isl_take isl_basic_set
*bset
)
4801 space
= isl_basic_set_get_space(bset
);
4802 space
= isl_space_set_from_params(space
);
4803 bset
= isl_basic_set_reset_space(bset
, space
);
4807 /* Compute the parameter domain of the given set.
4809 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4814 if (isl_set_is_params(set
))
4817 n
= isl_set_dim(set
, isl_dim_set
);
4818 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4819 space
= isl_set_get_space(set
);
4820 space
= isl_space_params(space
);
4821 set
= isl_set_reset_space(set
, space
);
4825 /* Construct a zero-dimensional set with the given parameter domain.
4827 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4830 space
= isl_set_get_space(set
);
4831 space
= isl_space_set_from_params(space
);
4832 set
= isl_set_reset_space(set
, space
);
4836 /* Compute the parameter domain of the given map.
4838 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4843 n
= isl_map_dim(map
, isl_dim_in
);
4844 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4845 n
= isl_map_dim(map
, isl_dim_out
);
4846 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4847 space
= isl_map_get_space(map
);
4848 space
= isl_space_params(space
);
4849 map
= isl_map_reset_space(map
, space
);
4853 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4856 struct isl_basic_set
*domain
;
4862 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4864 n_in
= isl_basic_map_n_in(bmap
);
4865 n_out
= isl_basic_map_n_out(bmap
);
4866 domain
= isl_basic_set_from_basic_map(bmap
);
4867 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4869 domain
= isl_basic_set_reset_space(domain
, dim
);
4874 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4878 return isl_space_may_be_set(bmap
->dim
);
4881 /* Is this basic map actually a set?
4882 * Users should never call this function. Outside of isl,
4883 * the type should indicate whether something is a set or a map.
4885 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4889 return isl_space_is_set(bmap
->dim
);
4892 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4896 if (isl_basic_map_is_set(bmap
))
4898 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4901 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4902 __isl_take isl_basic_map
*bmap
)
4906 isl_basic_map
*domain
;
4907 int nparam
, n_in
, n_out
;
4910 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4911 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4912 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4914 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4915 domain
= isl_basic_map_universe(dim
);
4917 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4918 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4919 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4921 total
= isl_basic_map_total_dim(bmap
);
4923 for (i
= 0; i
< n_in
; ++i
) {
4924 k
= isl_basic_map_alloc_equality(bmap
);
4927 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4928 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4929 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4932 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4933 return isl_basic_map_finalize(bmap
);
4935 isl_basic_map_free(bmap
);
4939 __isl_give isl_basic_map
*isl_basic_map_range_map(
4940 __isl_take isl_basic_map
*bmap
)
4944 isl_basic_map
*range
;
4945 int nparam
, n_in
, n_out
;
4948 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4949 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4950 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4952 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4953 range
= isl_basic_map_universe(dim
);
4955 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4956 bmap
= isl_basic_map_apply_range(bmap
, range
);
4957 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4959 total
= isl_basic_map_total_dim(bmap
);
4961 for (i
= 0; i
< n_out
; ++i
) {
4962 k
= isl_basic_map_alloc_equality(bmap
);
4965 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4966 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4967 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4970 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4971 return isl_basic_map_finalize(bmap
);
4973 isl_basic_map_free(bmap
);
4977 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4981 return isl_space_may_be_set(map
->dim
);
4984 /* Is this map actually a set?
4985 * Users should never call this function. Outside of isl,
4986 * the type should indicate whether something is a set or a map.
4988 int isl_map_is_set(__isl_keep isl_map
*map
)
4992 return isl_space_is_set(map
->dim
);
4995 struct isl_set
*isl_map_range(struct isl_map
*map
)
4998 struct isl_set
*set
;
5002 if (isl_map_is_set(map
))
5003 return (isl_set
*)map
;
5005 map
= isl_map_cow(map
);
5009 set
= (struct isl_set
*) map
;
5010 set
->dim
= isl_space_range(set
->dim
);
5013 for (i
= 0; i
< map
->n
; ++i
) {
5014 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5018 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5019 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5026 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5030 map
= isl_map_cow(map
);
5034 map
->dim
= isl_space_domain_map(map
->dim
);
5037 for (i
= 0; i
< map
->n
; ++i
) {
5038 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5042 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5043 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5050 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5053 isl_space
*range_dim
;
5055 map
= isl_map_cow(map
);
5059 range_dim
= isl_space_range(isl_map_get_space(map
));
5060 range_dim
= isl_space_from_range(range_dim
);
5061 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5062 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5065 for (i
= 0; i
< map
->n
; ++i
) {
5066 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5070 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5071 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5078 /* Given a wrapped map of the form A[B -> C],
5079 * return the map A[B -> C] -> B.
5081 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5088 if (!isl_set_has_tuple_id(set
))
5089 return isl_map_domain_map(isl_set_unwrap(set
));
5091 id
= isl_set_get_tuple_id(set
);
5092 map
= isl_map_domain_map(isl_set_unwrap(set
));
5093 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5098 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5099 __isl_take isl_space
*dim
)
5102 struct isl_map
*map
= NULL
;
5104 set
= isl_set_cow(set
);
5107 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
5108 map
= (struct isl_map
*)set
;
5109 for (i
= 0; i
< set
->n
; ++i
) {
5110 map
->p
[i
] = isl_basic_map_from_basic_set(
5111 set
->p
[i
], isl_space_copy(dim
));
5115 isl_space_free(map
->dim
);
5119 isl_space_free(dim
);
5124 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5125 __isl_take isl_basic_set
*bset
)
5127 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5130 __isl_give isl_basic_map
*isl_basic_map_from_range(
5131 __isl_take isl_basic_set
*bset
)
5134 space
= isl_basic_set_get_space(bset
);
5135 space
= isl_space_from_range(space
);
5136 bset
= isl_basic_set_reset_space(bset
, space
);
5137 return (isl_basic_map
*)bset
;
5140 /* Create a relation with the given set as range.
5141 * The domain of the created relation is a zero-dimensional
5142 * flat anonymous space.
5144 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5147 space
= isl_set_get_space(set
);
5148 space
= isl_space_from_range(space
);
5149 set
= isl_set_reset_space(set
, space
);
5150 return (struct isl_map
*)set
;
5153 /* Create a relation with the given set as domain.
5154 * The range of the created relation is a zero-dimensional
5155 * flat anonymous space.
5157 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5159 return isl_map_reverse(isl_map_from_range(set
));
5162 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5163 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5165 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5168 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5169 __isl_take isl_set
*range
)
5171 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5174 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
5177 struct isl_set
*set
= NULL
;
5181 map
= isl_map_cow(map
);
5184 map
->dim
= isl_space_as_set_space(map
->dim
);
5187 set
= (struct isl_set
*)map
;
5188 for (i
= 0; i
< map
->n
; ++i
) {
5189 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
5199 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
5202 struct isl_map
*map
;
5207 isl_die(dim
->ctx
, isl_error_internal
,
5208 "negative number of basic maps", goto error
);
5209 map
= isl_alloc(dim
->ctx
, struct isl_map
,
5210 sizeof(struct isl_map
) +
5211 (n
- 1) * sizeof(struct isl_basic_map
*));
5215 map
->ctx
= dim
->ctx
;
5216 isl_ctx_ref(map
->ctx
);
5224 isl_space_free(dim
);
5228 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5229 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5232 struct isl_map
*map
;
5235 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5239 map
= isl_map_alloc_space(dims
, n
, flags
);
5243 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5245 struct isl_basic_map
*bmap
;
5246 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5247 bmap
= isl_basic_map_set_to_empty(bmap
);
5251 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5253 struct isl_basic_set
*bset
;
5254 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5255 bset
= isl_basic_set_set_to_empty(bset
);
5259 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5261 struct isl_basic_map
*bmap
;
5264 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5265 bmap
= isl_basic_map_set_to_empty(bmap
);
5269 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5271 struct isl_basic_map
*bmap
;
5274 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5275 bmap
= isl_basic_map_set_to_empty(bmap
);
5279 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5281 struct isl_basic_set
*bset
;
5284 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5285 bset
= isl_basic_set_set_to_empty(bset
);
5289 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5291 struct isl_basic_map
*bmap
;
5292 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5293 bmap
= isl_basic_map_finalize(bmap
);
5297 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5299 struct isl_basic_set
*bset
;
5300 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5301 bset
= isl_basic_set_finalize(bset
);
5305 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5308 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5309 isl_basic_map
*bmap
;
5311 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5312 for (i
= 0; i
< total
; ++i
) {
5313 int k
= isl_basic_map_alloc_inequality(bmap
);
5316 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5317 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5321 isl_basic_map_free(bmap
);
5325 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5327 return isl_basic_map_nat_universe(dim
);
5330 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5332 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5335 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5337 return isl_map_nat_universe(dim
);
5340 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5341 __isl_keep isl_basic_map
*model
)
5345 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5348 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5352 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5355 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5356 __isl_keep isl_set
*model
)
5360 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5363 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5365 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5368 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5372 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5375 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5379 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5382 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5384 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5387 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5391 return isl_set_empty(isl_space_copy(model
->dim
));
5394 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5396 struct isl_map
*map
;
5399 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5400 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5404 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5406 struct isl_set
*set
;
5409 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5410 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5414 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5418 return isl_set_universe(isl_space_copy(model
->dim
));
5421 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5424 struct isl_map
*dup
;
5428 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5429 for (i
= 0; i
< map
->n
; ++i
)
5430 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5434 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5435 __isl_take isl_basic_map
*bmap
)
5439 if (isl_basic_map_plain_is_empty(bmap
)) {
5440 isl_basic_map_free(bmap
);
5443 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5444 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5445 map
->p
[map
->n
] = bmap
;
5447 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5453 isl_basic_map_free(bmap
);
5457 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5467 isl_ctx_deref(map
->ctx
);
5468 for (i
= 0; i
< map
->n
; ++i
)
5469 isl_basic_map_free(map
->p
[i
]);
5470 isl_space_free(map
->dim
);
5476 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5477 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5481 base
= isl_map_cow(base
);
5485 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5488 for (i
= 0; i
< base
->n
; ++i
) {
5489 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5490 isl_space_copy(base
->dim
), 0, 0, 0);
5500 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5501 unsigned nparam
, unsigned dim
)
5503 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5507 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5508 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5512 bmap
= isl_basic_map_cow(bmap
);
5513 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5514 j
= isl_basic_map_alloc_equality(bmap
);
5517 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5518 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5519 isl_int_set_si(bmap
->eq
[j
][0], value
);
5520 bmap
= isl_basic_map_simplify(bmap
);
5521 return isl_basic_map_finalize(bmap
);
5523 isl_basic_map_free(bmap
);
5527 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5528 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5532 bmap
= isl_basic_map_cow(bmap
);
5533 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5534 j
= isl_basic_map_alloc_equality(bmap
);
5537 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5538 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5539 isl_int_set(bmap
->eq
[j
][0], value
);
5540 bmap
= isl_basic_map_simplify(bmap
);
5541 return isl_basic_map_finalize(bmap
);
5543 isl_basic_map_free(bmap
);
5547 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5548 enum isl_dim_type type
, unsigned pos
, int value
)
5552 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5553 return isl_basic_map_fix_pos_si(bmap
,
5554 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5556 isl_basic_map_free(bmap
);
5560 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5561 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5565 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5566 return isl_basic_map_fix_pos(bmap
,
5567 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5569 isl_basic_map_free(bmap
);
5573 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5574 * to be equal to "v".
5576 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5577 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5581 if (!isl_val_is_int(v
))
5582 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5583 "expecting integer value", goto error
);
5584 if (pos
>= isl_basic_map_dim(bmap
, type
))
5585 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5586 "index out of bounds", goto error
);
5587 pos
+= isl_basic_map_offset(bmap
, type
);
5588 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5592 isl_basic_map_free(bmap
);
5597 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5598 * to be equal to "v".
5600 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5601 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5603 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5606 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5607 enum isl_dim_type type
, unsigned pos
, int value
)
5609 return (struct isl_basic_set
*)
5610 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5614 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5615 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5617 return (struct isl_basic_set
*)
5618 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5622 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5623 unsigned input
, int value
)
5625 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5628 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5629 unsigned dim
, int value
)
5631 return (struct isl_basic_set
*)
5632 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5633 isl_dim_set
, dim
, value
);
5636 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5638 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5645 isl_basic_map_free(map
->p
[i
]);
5646 if (i
!= map
->n
- 1) {
5647 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5648 map
->p
[i
] = map
->p
[map
->n
- 1];
5655 /* Perform "fn" on each basic map of "map", where we may not be holding
5656 * the only reference to "map".
5657 * In particular, "fn" should be a semantics preserving operation
5658 * that we want to apply to all copies of "map". We therefore need
5659 * to be careful not to modify "map" in a way that breaks "map"
5660 * in case anything goes wrong.
5662 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5663 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5665 struct isl_basic_map
*bmap
;
5671 for (i
= map
->n
- 1; i
>= 0; --i
) {
5672 bmap
= isl_basic_map_copy(map
->p
[i
]);
5676 isl_basic_map_free(map
->p
[i
]);
5678 if (remove_if_empty(map
, i
) < 0)
5688 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5689 enum isl_dim_type type
, unsigned pos
, int value
)
5693 map
= isl_map_cow(map
);
5697 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5698 for (i
= map
->n
- 1; i
>= 0; --i
) {
5699 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5700 if (remove_if_empty(map
, i
) < 0)
5703 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5710 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5711 enum isl_dim_type type
, unsigned pos
, int value
)
5713 return (struct isl_set
*)
5714 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5717 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5718 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5722 map
= isl_map_cow(map
);
5726 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5727 for (i
= 0; i
< map
->n
; ++i
) {
5728 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5732 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5739 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5740 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5742 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5745 /* Fix the value of the variable at position "pos" of type "type" of "map"
5746 * to be equal to "v".
5748 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5749 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5753 map
= isl_map_cow(map
);
5757 if (!isl_val_is_int(v
))
5758 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5759 "expecting integer value", goto error
);
5760 if (pos
>= isl_map_dim(map
, type
))
5761 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5762 "index out of bounds", goto error
);
5763 for (i
= map
->n
- 1; i
>= 0; --i
) {
5764 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5766 if (remove_if_empty(map
, i
) < 0)
5769 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5778 /* Fix the value of the variable at position "pos" of type "type" of "set"
5779 * to be equal to "v".
5781 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5782 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5784 return isl_map_fix_val(set
, type
, pos
, v
);
5787 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5788 unsigned input
, int value
)
5790 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5793 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5795 return (struct isl_set
*)
5796 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5799 static __isl_give isl_basic_map
*basic_map_bound_si(
5800 __isl_take isl_basic_map
*bmap
,
5801 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5807 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5808 pos
+= isl_basic_map_offset(bmap
, type
);
5809 bmap
= isl_basic_map_cow(bmap
);
5810 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5811 j
= isl_basic_map_alloc_inequality(bmap
);
5814 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5816 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5817 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5819 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5820 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5822 bmap
= isl_basic_map_simplify(bmap
);
5823 return isl_basic_map_finalize(bmap
);
5825 isl_basic_map_free(bmap
);
5829 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5830 __isl_take isl_basic_map
*bmap
,
5831 enum isl_dim_type type
, unsigned pos
, int value
)
5833 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5836 /* Constrain the values of the given dimension to be no greater than "value".
5838 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5839 __isl_take isl_basic_map
*bmap
,
5840 enum isl_dim_type type
, unsigned pos
, int value
)
5842 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5845 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5846 unsigned dim
, isl_int value
)
5850 bset
= isl_basic_set_cow(bset
);
5851 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5852 j
= isl_basic_set_alloc_inequality(bset
);
5855 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5856 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5857 isl_int_neg(bset
->ineq
[j
][0], value
);
5858 bset
= isl_basic_set_simplify(bset
);
5859 return isl_basic_set_finalize(bset
);
5861 isl_basic_set_free(bset
);
5865 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5866 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5870 map
= isl_map_cow(map
);
5874 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5875 for (i
= 0; i
< map
->n
; ++i
) {
5876 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5877 type
, pos
, value
, upper
);
5881 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5888 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5889 enum isl_dim_type type
, unsigned pos
, int value
)
5891 return map_bound_si(map
, type
, pos
, value
, 0);
5894 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5895 enum isl_dim_type type
, unsigned pos
, int value
)
5897 return map_bound_si(map
, type
, pos
, value
, 1);
5900 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5901 enum isl_dim_type type
, unsigned pos
, int value
)
5903 return (struct isl_set
*)
5904 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5907 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5908 enum isl_dim_type type
, unsigned pos
, int value
)
5910 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5913 /* Bound the given variable of "bmap" from below (or above is "upper"
5914 * is set) to "value".
5916 static __isl_give isl_basic_map
*basic_map_bound(
5917 __isl_take isl_basic_map
*bmap
,
5918 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5924 if (pos
>= isl_basic_map_dim(bmap
, type
))
5925 isl_die(bmap
->ctx
, isl_error_invalid
,
5926 "index out of bounds", goto error
);
5927 pos
+= isl_basic_map_offset(bmap
, type
);
5928 bmap
= isl_basic_map_cow(bmap
);
5929 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5930 j
= isl_basic_map_alloc_inequality(bmap
);
5933 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5935 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5936 isl_int_set(bmap
->ineq
[j
][0], value
);
5938 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5939 isl_int_neg(bmap
->ineq
[j
][0], value
);
5941 bmap
= isl_basic_map_simplify(bmap
);
5942 return isl_basic_map_finalize(bmap
);
5944 isl_basic_map_free(bmap
);
5948 /* Bound the given variable of "map" from below (or above is "upper"
5949 * is set) to "value".
5951 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5952 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5956 map
= isl_map_cow(map
);
5960 if (pos
>= isl_map_dim(map
, type
))
5961 isl_die(map
->ctx
, isl_error_invalid
,
5962 "index out of bounds", goto error
);
5963 for (i
= map
->n
- 1; i
>= 0; --i
) {
5964 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5965 if (remove_if_empty(map
, i
) < 0)
5968 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5975 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5976 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5978 return map_bound(map
, type
, pos
, value
, 0);
5981 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5982 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5984 return map_bound(map
, type
, pos
, value
, 1);
5987 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5988 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5990 return isl_map_lower_bound(set
, type
, pos
, value
);
5993 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5994 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5996 return isl_map_upper_bound(set
, type
, pos
, value
);
5999 /* Force the values of the variable at position "pos" of type "type" of "set"
6000 * to be no smaller than "value".
6002 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6003 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6007 if (!isl_val_is_int(value
))
6008 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6009 "expecting integer value", goto error
);
6010 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6011 isl_val_free(value
);
6014 isl_val_free(value
);
6019 /* Force the values of the variable at position "pos" of type "type" of "set"
6020 * to be no greater than "value".
6022 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6023 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6027 if (!isl_val_is_int(value
))
6028 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6029 "expecting integer value", goto error
);
6030 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6031 isl_val_free(value
);
6034 isl_val_free(value
);
6039 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6044 set
= isl_set_cow(set
);
6048 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6049 for (i
= 0; i
< set
->n
; ++i
) {
6050 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6060 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6064 map
= isl_map_cow(map
);
6068 map
->dim
= isl_space_reverse(map
->dim
);
6071 for (i
= 0; i
< map
->n
; ++i
) {
6072 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6076 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6083 static struct isl_map
*isl_basic_map_partial_lexopt(
6084 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6085 struct isl_set
**empty
, int max
)
6087 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
6090 struct isl_map
*isl_basic_map_partial_lexmax(
6091 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6092 struct isl_set
**empty
)
6094 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
6097 struct isl_map
*isl_basic_map_partial_lexmin(
6098 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
6099 struct isl_set
**empty
)
6101 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
6104 struct isl_set
*isl_basic_set_partial_lexmin(
6105 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6106 struct isl_set
**empty
)
6108 return (struct isl_set
*)
6109 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
6113 struct isl_set
*isl_basic_set_partial_lexmax(
6114 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
6115 struct isl_set
**empty
)
6117 return (struct isl_set
*)
6118 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
6122 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
6123 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6124 __isl_give isl_set
**empty
)
6126 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
6129 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
6130 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
6131 __isl_give isl_set
**empty
)
6133 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
6136 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
6137 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6138 __isl_give isl_set
**empty
)
6140 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
6143 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
6144 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
6145 __isl_give isl_set
**empty
)
6147 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
6150 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
6151 __isl_take isl_basic_map
*bmap
, int max
)
6153 isl_basic_set
*dom
= NULL
;
6154 isl_space
*dom_space
;
6158 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
6159 dom
= isl_basic_set_universe(dom_space
);
6160 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
6162 isl_basic_map_free(bmap
);
6166 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
6167 __isl_take isl_basic_map
*bmap
)
6169 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
6173 #define TYPE isl_pw_multi_aff
6175 #define SUFFIX _pw_multi_aff
6177 #define EMPTY isl_pw_multi_aff_empty
6179 #define ADD isl_pw_multi_aff_union_add
6180 #include "isl_map_lexopt_templ.c"
6182 /* Given a map "map", compute the lexicographically minimal
6183 * (or maximal) image element for each domain element in dom,
6184 * in the form of an isl_pw_multi_aff.
6185 * Set *empty to those elements in dom that do not have an image element.
6187 * We first compute the lexicographically minimal or maximal element
6188 * in the first basic map. This results in a partial solution "res"
6189 * and a subset "todo" of dom that still need to be handled.
6190 * We then consider each of the remaining maps in "map" and successively
6191 * update both "res" and "todo".
6193 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6194 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6195 __isl_give isl_set
**empty
, int max
)
6198 isl_pw_multi_aff
*res
;
6204 if (isl_map_plain_is_empty(map
)) {
6209 return isl_pw_multi_aff_from_map(map
);
6212 res
= basic_map_partial_lexopt_pw_multi_aff(
6213 isl_basic_map_copy(map
->p
[0]),
6214 isl_set_copy(dom
), &todo
, max
);
6216 for (i
= 1; i
< map
->n
; ++i
) {
6217 isl_pw_multi_aff
*res_i
;
6220 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6221 isl_basic_map_copy(map
->p
[i
]),
6222 isl_set_copy(dom
), &todo_i
, max
);
6225 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6227 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6229 todo
= isl_set_intersect(todo
, todo_i
);
6250 #define TYPE isl_map
6254 #define EMPTY isl_map_empty
6256 #define ADD isl_map_union_disjoint
6257 #include "isl_map_lexopt_templ.c"
6259 /* Given a map "map", compute the lexicographically minimal
6260 * (or maximal) image element for each domain element in dom.
6261 * Set *empty to those elements in dom that do not have an image element.
6263 * We first compute the lexicographically minimal or maximal element
6264 * in the first basic map. This results in a partial solution "res"
6265 * and a subset "todo" of dom that still need to be handled.
6266 * We then consider each of the remaining maps in "map" and successively
6267 * update both "res" and "todo".
6269 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6270 * Assume we are computing the lexicographical maximum.
6271 * We first compute the lexicographically maximal element in basic map i.
6272 * This results in a partial solution res_i and a subset todo_i.
6273 * Then we combine these results with those obtain for the first k basic maps
6274 * to obtain a result that is valid for the first k+1 basic maps.
6275 * In particular, the set where there is no solution is the set where
6276 * there is no solution for the first k basic maps and also no solution
6277 * for the ith basic map, i.e.,
6279 * todo^i = todo^k * todo_i
6281 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6282 * solutions, arbitrarily breaking ties in favor of res^k.
6283 * That is, when res^k(a) >= res_i(a), we pick res^k and
6284 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6285 * the lexicographic order.)
6286 * In practice, we compute
6288 * res^k * (res_i . "<=")
6292 * res_i * (res^k . "<")
6294 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6295 * where only one of res^k and res_i provides a solution and we simply pick
6302 * Note that we only compute these intersections when dom(res^k) intersects
6303 * dom(res_i). Otherwise, the only effect of these intersections is to
6304 * potentially break up res^k and res_i into smaller pieces.
6305 * We want to avoid such splintering as much as possible.
6306 * In fact, an earlier implementation of this function would look for
6307 * better results in the domain of res^k and for extra results in todo^k,
6308 * but this would always result in a splintering according to todo^k,
6309 * even when the domain of basic map i is disjoint from the domains of
6310 * the previous basic maps.
6312 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6313 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6314 __isl_give isl_set
**empty
, int max
)
6317 struct isl_map
*res
;
6318 struct isl_set
*todo
;
6323 if (isl_map_plain_is_empty(map
)) {
6331 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6332 isl_set_copy(dom
), &todo
, max
);
6334 for (i
= 1; i
< map
->n
; ++i
) {
6338 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6340 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6341 isl_set_copy(dom
), &todo_i
, max
);
6344 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6345 le
= isl_map_lex_le(dim
);
6347 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6348 le
= isl_map_lex_ge(dim
);
6350 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6351 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6352 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6353 le
= isl_map_intersect(le
, isl_map_copy(res
));
6355 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6356 res
= isl_map_intersect_domain(res
,
6357 isl_set_copy(todo_i
));
6358 res_i
= isl_map_intersect_domain(res_i
,
6359 isl_set_copy(todo
));
6362 res
= isl_map_union_disjoint(res
, res_i
);
6363 res
= isl_map_union_disjoint(res
, lt
);
6364 res
= isl_map_union_disjoint(res
, le
);
6366 todo
= isl_set_intersect(todo
, todo_i
);
6386 __isl_give isl_map
*isl_map_partial_lexmax(
6387 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6388 __isl_give isl_set
**empty
)
6390 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6393 __isl_give isl_map
*isl_map_partial_lexmin(
6394 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6395 __isl_give isl_set
**empty
)
6397 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6400 __isl_give isl_set
*isl_set_partial_lexmin(
6401 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6402 __isl_give isl_set
**empty
)
6404 return (struct isl_set
*)
6405 isl_map_partial_lexmin((struct isl_map
*)set
,
6409 __isl_give isl_set
*isl_set_partial_lexmax(
6410 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6411 __isl_give isl_set
**empty
)
6413 return (struct isl_set
*)
6414 isl_map_partial_lexmax((struct isl_map
*)set
,
6418 /* Compute the lexicographic minimum (or maximum if "max" is set)
6419 * of "bmap" over its domain.
6421 * Since we are not interested in the part of the domain space where
6422 * there is no solution, we initialize the domain to those constraints
6423 * of "bmap" that only involve the parameters and the input dimensions.
6424 * This relieves the parametric programming engine from detecting those
6425 * inequalities and transferring them to the context. More importantly,
6426 * it ensures that those inequalities are transferred first and not
6427 * intermixed with inequalities that actually split the domain.
6429 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6433 isl_basic_map
*copy
;
6436 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6437 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6438 copy
= isl_basic_map_copy(bmap
);
6439 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6440 isl_dim_div
, 0, n_div
);
6441 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6442 isl_dim_out
, 0, n_out
);
6443 dom
= isl_basic_map_domain(copy
);
6444 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6447 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6449 return isl_basic_map_lexopt(bmap
, 0);
6452 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6454 return isl_basic_map_lexopt(bmap
, 1);
6457 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6459 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6462 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6464 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6467 /* Extract the first and only affine expression from list
6468 * and then add it to *pwaff with the given dom.
6469 * This domain is known to be disjoint from other domains
6470 * because of the way isl_basic_map_foreach_lexmax works.
6472 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6473 __isl_take isl_aff_list
*list
, void *user
)
6475 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6477 isl_pw_aff
**pwaff
= user
;
6478 isl_pw_aff
*pwaff_i
;
6482 if (isl_aff_list_n_aff(list
) != 1)
6483 isl_die(ctx
, isl_error_internal
,
6484 "expecting single element list", goto error
);
6486 aff
= isl_aff_list_get_aff(list
, 0);
6487 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6489 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6491 isl_aff_list_free(list
);
6495 isl_basic_set_free(dom
);
6496 isl_aff_list_free(list
);
6500 /* Given a basic map with one output dimension, compute the minimum or
6501 * maximum of that dimension as an isl_pw_aff.
6503 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6504 * call update_dim_opt on each leaf of the result.
6506 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6509 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6513 dim
= isl_space_from_domain(isl_space_domain(dim
));
6514 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6515 pwaff
= isl_pw_aff_empty(dim
);
6517 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6519 return isl_pw_aff_free(pwaff
);
6524 /* Compute the minimum or maximum of the given output dimension
6525 * as a function of the parameters and the input dimensions,
6526 * but independently of the other output dimensions.
6528 * We first project out the other output dimension and then compute
6529 * the "lexicographic" maximum in each basic map, combining the results
6530 * using isl_pw_aff_union_max.
6532 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6539 n_out
= isl_map_dim(map
, isl_dim_out
);
6540 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6541 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6546 isl_space
*dim
= isl_map_get_space(map
);
6548 return isl_pw_aff_empty(dim
);
6551 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6552 for (i
= 1; i
< map
->n
; ++i
) {
6553 isl_pw_aff
*pwaff_i
;
6555 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6556 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6564 /* Compute the maximum of the given output dimension as a function of the
6565 * parameters and input dimensions, but independently of
6566 * the other output dimensions.
6568 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6570 return map_dim_opt(map
, pos
, 1);
6573 /* Compute the minimum or maximum of the given set dimension
6574 * as a function of the parameters,
6575 * but independently of the other set dimensions.
6577 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6580 return map_dim_opt(set
, pos
, max
);
6583 /* Compute the maximum of the given set dimension as a function of the
6584 * parameters, but independently of the other set dimensions.
6586 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6588 return set_dim_opt(set
, pos
, 1);
6591 /* Compute the minimum of the given set dimension as a function of the
6592 * parameters, but independently of the other set dimensions.
6594 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6596 return set_dim_opt(set
, pos
, 0);
6599 /* Apply a preimage specified by "mat" on the parameters of "bset".
6600 * bset is assumed to have only parameters and divs.
6602 static struct isl_basic_set
*basic_set_parameter_preimage(
6603 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6610 bset
->dim
= isl_space_cow(bset
->dim
);
6614 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6616 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6618 bset
->dim
->nparam
= 0;
6619 bset
->dim
->n_out
= nparam
;
6620 bset
= isl_basic_set_preimage(bset
, mat
);
6622 bset
->dim
->nparam
= bset
->dim
->n_out
;
6623 bset
->dim
->n_out
= 0;
6628 isl_basic_set_free(bset
);
6632 /* Apply a preimage specified by "mat" on the parameters of "set".
6633 * set is assumed to have only parameters and divs.
6635 static struct isl_set
*set_parameter_preimage(
6636 struct isl_set
*set
, struct isl_mat
*mat
)
6638 isl_space
*dim
= NULL
;
6644 dim
= isl_space_copy(set
->dim
);
6645 dim
= isl_space_cow(dim
);
6649 nparam
= isl_set_dim(set
, isl_dim_param
);
6651 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6654 dim
->n_out
= nparam
;
6655 isl_set_reset_space(set
, dim
);
6656 set
= isl_set_preimage(set
, mat
);
6659 dim
= isl_space_copy(set
->dim
);
6660 dim
= isl_space_cow(dim
);
6663 dim
->nparam
= dim
->n_out
;
6665 isl_set_reset_space(set
, dim
);
6668 isl_space_free(dim
);
6675 /* Intersect the basic set "bset" with the affine space specified by the
6676 * equalities in "eq".
6678 static struct isl_basic_set
*basic_set_append_equalities(
6679 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6687 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6692 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6693 for (i
= 0; i
< eq
->n_row
; ++i
) {
6694 k
= isl_basic_set_alloc_equality(bset
);
6697 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6698 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6702 bset
= isl_basic_set_gauss(bset
, NULL
);
6703 bset
= isl_basic_set_finalize(bset
);
6708 isl_basic_set_free(bset
);
6712 /* Intersect the set "set" with the affine space specified by the
6713 * equalities in "eq".
6715 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6723 for (i
= 0; i
< set
->n
; ++i
) {
6724 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6737 /* Given a basic set "bset" that only involves parameters and existentially
6738 * quantified variables, return the index of the first equality
6739 * that only involves parameters. If there is no such equality then
6740 * return bset->n_eq.
6742 * This function assumes that isl_basic_set_gauss has been called on "bset".
6744 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6747 unsigned nparam
, n_div
;
6752 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6753 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6755 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6756 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6763 /* Compute an explicit representation for the existentially quantified
6764 * variables in "bset" by computing the "minimal value" of the set
6765 * variables. Since there are no set variables, the computation of
6766 * the minimal value essentially computes an explicit representation
6767 * of the non-empty part(s) of "bset".
6769 * The input only involves parameters and existentially quantified variables.
6770 * All equalities among parameters have been removed.
6772 * Since the existentially quantified variables in the result are in general
6773 * going to be different from those in the input, we first replace
6774 * them by the minimal number of variables based on their equalities.
6775 * This should simplify the parametric integer programming.
6777 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6779 isl_morph
*morph1
, *morph2
;
6785 if (bset
->n_eq
== 0)
6786 return isl_basic_set_lexmin(bset
);
6788 morph1
= isl_basic_set_parameter_compression(bset
);
6789 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6790 bset
= isl_basic_set_lift(bset
);
6791 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6792 bset
= isl_morph_basic_set(morph2
, bset
);
6793 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6794 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6796 set
= isl_basic_set_lexmin(bset
);
6798 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6803 /* Project the given basic set onto its parameter domain, possibly introducing
6804 * new, explicit, existential variables in the constraints.
6805 * The input has parameters and (possibly implicit) existential variables.
6806 * The output has the same parameters, but only
6807 * explicit existentially quantified variables.
6809 * The actual projection is performed by pip, but pip doesn't seem
6810 * to like equalities very much, so we first remove the equalities
6811 * among the parameters by performing a variable compression on
6812 * the parameters. Afterward, an inverse transformation is performed
6813 * and the equalities among the parameters are inserted back in.
6815 * The variable compression on the parameters may uncover additional
6816 * equalities that were only implicit before. We therefore check
6817 * if there are any new parameter equalities in the result and
6818 * if so recurse. The removal of parameter equalities is required
6819 * for the parameter compression performed by base_compute_divs.
6821 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6825 struct isl_mat
*T
, *T2
;
6826 struct isl_set
*set
;
6829 bset
= isl_basic_set_cow(bset
);
6833 if (bset
->n_eq
== 0)
6834 return base_compute_divs(bset
);
6836 bset
= isl_basic_set_gauss(bset
, NULL
);
6839 if (isl_basic_set_plain_is_empty(bset
))
6840 return isl_set_from_basic_set(bset
);
6842 i
= first_parameter_equality(bset
);
6843 if (i
== bset
->n_eq
)
6844 return base_compute_divs(bset
);
6846 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6847 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6849 eq
= isl_mat_cow(eq
);
6850 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6851 if (T
&& T
->n_col
== 0) {
6855 bset
= isl_basic_set_set_to_empty(bset
);
6856 return isl_set_from_basic_set(bset
);
6858 bset
= basic_set_parameter_preimage(bset
, T
);
6860 i
= first_parameter_equality(bset
);
6863 else if (i
== bset
->n_eq
)
6864 set
= base_compute_divs(bset
);
6866 set
= parameter_compute_divs(bset
);
6867 set
= set_parameter_preimage(set
, T2
);
6868 set
= set_append_equalities(set
, eq
);
6872 /* Insert the divs from "ls" before those of "bmap".
6874 * The number of columns is not changed, which means that the last
6875 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6876 * The caller is responsible for removing the same number of dimensions
6877 * from the space of "bmap".
6879 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6880 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6886 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6890 old_n_div
= bmap
->n_div
;
6891 bmap
= insert_div_rows(bmap
, n_div
);
6895 for (i
= 0; i
< n_div
; ++i
) {
6896 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6897 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6903 /* Replace the space of "bmap" by the space and divs of "ls".
6905 * If "ls" has any divs, then we simplify the result since we may
6906 * have discovered some additional equalities that could simplify
6907 * the div expressions.
6909 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6910 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6914 bmap
= isl_basic_map_cow(bmap
);
6918 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6919 bmap
= insert_divs_from_local_space(bmap
, ls
);
6923 isl_space_free(bmap
->dim
);
6924 bmap
->dim
= isl_local_space_get_space(ls
);
6928 isl_local_space_free(ls
);
6930 bmap
= isl_basic_map_simplify(bmap
);
6931 bmap
= isl_basic_map_finalize(bmap
);
6934 isl_basic_map_free(bmap
);
6935 isl_local_space_free(ls
);
6939 /* Replace the space of "map" by the space and divs of "ls".
6941 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6942 __isl_take isl_local_space
*ls
)
6946 map
= isl_map_cow(map
);
6950 for (i
= 0; i
< map
->n
; ++i
) {
6951 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6952 isl_local_space_copy(ls
));
6956 isl_space_free(map
->dim
);
6957 map
->dim
= isl_local_space_get_space(ls
);
6961 isl_local_space_free(ls
);
6964 isl_local_space_free(ls
);
6969 /* Compute an explicit representation for the existentially
6970 * quantified variables for which do not know any explicit representation yet.
6972 * We first sort the existentially quantified variables so that the
6973 * existentially quantified variables for which we already have an explicit
6974 * representation are placed before those for which we do not.
6975 * The input dimensions, the output dimensions and the existentially
6976 * quantified variables for which we already have an explicit
6977 * representation are then turned into parameters.
6978 * compute_divs returns a map with the same parameters and
6979 * no input or output dimensions and the dimension specification
6980 * is reset to that of the input, including the existentially quantified
6981 * variables for which we already had an explicit representation.
6983 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6985 struct isl_basic_set
*bset
;
6986 struct isl_set
*set
;
6987 struct isl_map
*map
;
6989 isl_local_space
*ls
;
6996 bmap
= isl_basic_map_sort_divs(bmap
);
6997 bmap
= isl_basic_map_cow(bmap
);
7001 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
7002 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
7005 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7006 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7007 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7008 dim
= isl_space_set_alloc(bmap
->ctx
,
7009 nparam
+ n_in
+ n_out
+ n_known
, 0);
7013 ls
= isl_basic_map_get_local_space(bmap
);
7014 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7015 n_known
, bmap
->n_div
- n_known
);
7017 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7018 swap_div(bmap
, i
- n_known
, i
);
7019 bmap
->n_div
-= n_known
;
7020 bmap
->extra
-= n_known
;
7022 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7023 bset
= (struct isl_basic_set
*)bmap
;
7025 set
= parameter_compute_divs(bset
);
7026 map
= (struct isl_map
*)set
;
7027 map
= replace_space_by_local_space(map
, ls
);
7031 isl_basic_map_free(bmap
);
7035 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7043 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7044 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7045 if (isl_int_is_zero(bmap
->div
[i
][0]))
7047 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
7053 static int map_divs_known(__isl_keep isl_map
*map
)
7060 for (i
= 0; i
< map
->n
; ++i
) {
7061 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7069 /* If bmap contains any unknown divs, then compute explicit
7070 * expressions for them. However, this computation may be
7071 * quite expensive, so first try to remove divs that aren't
7074 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7077 struct isl_map
*map
;
7079 known
= isl_basic_map_divs_known(bmap
);
7083 return isl_map_from_basic_map(bmap
);
7085 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7087 known
= isl_basic_map_divs_known(bmap
);
7091 return isl_map_from_basic_map(bmap
);
7093 map
= compute_divs(bmap
);
7096 isl_basic_map_free(bmap
);
7100 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7104 struct isl_map
*res
;
7111 known
= map_divs_known(map
);
7119 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7120 for (i
= 1 ; i
< map
->n
; ++i
) {
7122 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7123 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7124 res
= isl_map_union_disjoint(res
, r2
);
7126 res
= isl_map_union(res
, r2
);
7133 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7135 return (struct isl_set
*)
7136 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
7139 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7141 return (struct isl_set
*)
7142 isl_map_compute_divs((struct isl_map
*)set
);
7145 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7148 struct isl_set
*set
;
7153 map
= isl_map_cow(map
);
7157 set
= (struct isl_set
*)map
;
7158 set
->dim
= isl_space_domain(set
->dim
);
7161 for (i
= 0; i
< map
->n
; ++i
) {
7162 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7166 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7167 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7174 /* Return the union of "map1" and "map2", where we assume for now that
7175 * "map1" and "map2" are disjoint. Note that the basic maps inside
7176 * "map1" or "map2" may not be disjoint from each other.
7177 * Also note that this function is also called from isl_map_union,
7178 * which takes care of handling the situation where "map1" and "map2"
7179 * may not be disjoint.
7181 * If one of the inputs is empty, we can simply return the other input.
7182 * Similarly, if one of the inputs is universal, then it is equal to the union.
7184 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7185 __isl_take isl_map
*map2
)
7189 struct isl_map
*map
= NULL
;
7195 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7196 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7197 "spaces don't match", goto error
);
7208 is_universe
= isl_map_plain_is_universe(map1
);
7209 if (is_universe
< 0)
7216 is_universe
= isl_map_plain_is_universe(map2
);
7217 if (is_universe
< 0)
7224 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7225 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7226 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7228 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7229 map1
->n
+ map2
->n
, flags
);
7232 for (i
= 0; i
< map1
->n
; ++i
) {
7233 map
= isl_map_add_basic_map(map
,
7234 isl_basic_map_copy(map1
->p
[i
]));
7238 for (i
= 0; i
< map2
->n
; ++i
) {
7239 map
= isl_map_add_basic_map(map
,
7240 isl_basic_map_copy(map2
->p
[i
]));
7254 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7255 * guaranteed to be disjoint by the caller.
7257 * Note that this functions is called from within isl_map_make_disjoint,
7258 * so we have to be careful not to touch the constraints of the inputs
7261 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7262 __isl_take isl_map
*map2
)
7264 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7267 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7268 * not be disjoint. The parameters are assumed to have been aligned.
7270 * We currently simply call map_union_disjoint, the internal operation
7271 * of which does not really depend on the inputs being disjoint.
7272 * If the result contains more than one basic map, then we clear
7273 * the disjoint flag since the result may contain basic maps from
7274 * both inputs and these are not guaranteed to be disjoint.
7276 * As a special case, if "map1" and "map2" are obviously equal,
7277 * then we simply return "map1".
7279 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7280 __isl_take isl_map
*map2
)
7287 equal
= isl_map_plain_is_equal(map1
, map2
);
7295 map1
= map_union_disjoint(map1
, map2
);
7299 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7307 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7310 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7311 __isl_take isl_map
*map2
)
7313 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7316 struct isl_set
*isl_set_union_disjoint(
7317 struct isl_set
*set1
, struct isl_set
*set2
)
7319 return (struct isl_set
*)
7320 isl_map_union_disjoint(
7321 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7324 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7326 return (struct isl_set
*)
7327 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7330 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7333 * "map" and "set" are assumed to be compatible and non-NULL.
7335 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7336 __isl_take isl_set
*set
,
7337 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7338 __isl_take isl_basic_set
*bset
))
7341 struct isl_map
*result
;
7344 if (isl_set_plain_is_universe(set
)) {
7349 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7350 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7351 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7353 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7354 map
->n
* set
->n
, flags
);
7355 for (i
= 0; result
&& i
< map
->n
; ++i
)
7356 for (j
= 0; j
< set
->n
; ++j
) {
7357 result
= isl_map_add_basic_map(result
,
7358 fn(isl_basic_map_copy(map
->p
[i
]),
7359 isl_basic_set_copy(set
->p
[j
])));
7369 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7370 __isl_take isl_set
*set
)
7375 if (!isl_map_compatible_range(map
, set
))
7376 isl_die(set
->ctx
, isl_error_invalid
,
7377 "incompatible spaces", goto error
);
7379 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7386 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7387 __isl_take isl_set
*set
)
7389 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7392 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7393 __isl_take isl_set
*set
)
7398 if (!isl_map_compatible_domain(map
, set
))
7399 isl_die(set
->ctx
, isl_error_invalid
,
7400 "incompatible spaces", goto error
);
7402 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7409 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7410 __isl_take isl_set
*set
)
7412 return isl_map_align_params_map_map_and(map
, set
,
7413 &map_intersect_domain
);
7416 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7417 __isl_take isl_map
*map2
)
7421 map1
= isl_map_reverse(map1
);
7422 map1
= isl_map_apply_range(map1
, map2
);
7423 return isl_map_reverse(map1
);
7430 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7431 __isl_take isl_map
*map2
)
7433 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7436 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7437 __isl_take isl_map
*map2
)
7439 isl_space
*dim_result
;
7440 struct isl_map
*result
;
7446 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7447 isl_space_copy(map2
->dim
));
7449 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7452 for (i
= 0; i
< map1
->n
; ++i
)
7453 for (j
= 0; j
< map2
->n
; ++j
) {
7454 result
= isl_map_add_basic_map(result
,
7455 isl_basic_map_apply_range(
7456 isl_basic_map_copy(map1
->p
[i
]),
7457 isl_basic_map_copy(map2
->p
[j
])));
7463 if (result
&& result
->n
<= 1)
7464 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7472 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7473 __isl_take isl_map
*map2
)
7475 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7479 * returns range - domain
7481 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7483 isl_space
*dims
, *target_dim
;
7484 struct isl_basic_set
*bset
;
7491 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7492 bmap
->dim
, isl_dim_out
),
7494 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7495 dim
= isl_basic_map_n_in(bmap
);
7496 nparam
= isl_basic_map_n_param(bmap
);
7497 bset
= isl_basic_set_from_basic_map(bmap
);
7498 bset
= isl_basic_set_cow(bset
);
7499 dims
= isl_basic_set_get_space(bset
);
7500 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7501 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7502 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7503 for (i
= 0; i
< dim
; ++i
) {
7504 int j
= isl_basic_map_alloc_equality(
7505 (struct isl_basic_map
*)bset
);
7507 bset
= isl_basic_set_free(bset
);
7510 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7511 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7512 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7513 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7515 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7516 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7519 isl_basic_map_free(bmap
);
7524 * returns range - domain
7526 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7530 struct isl_set
*result
;
7535 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7536 map
->dim
, isl_dim_out
),
7538 dim
= isl_map_get_space(map
);
7539 dim
= isl_space_domain(dim
);
7540 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7543 for (i
= 0; i
< map
->n
; ++i
)
7544 result
= isl_set_add_basic_set(result
,
7545 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7554 * returns [domain -> range] -> range - domain
7556 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7557 __isl_take isl_basic_map
*bmap
)
7561 isl_basic_map
*domain
;
7565 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7566 bmap
->dim
, isl_dim_out
))
7567 isl_die(bmap
->ctx
, isl_error_invalid
,
7568 "domain and range don't match", goto error
);
7570 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7571 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7573 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7574 domain
= isl_basic_map_universe(dim
);
7576 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7577 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7578 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7580 total
= isl_basic_map_total_dim(bmap
);
7582 for (i
= 0; i
< n
; ++i
) {
7583 k
= isl_basic_map_alloc_equality(bmap
);
7586 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7587 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7588 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7589 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7592 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7593 return isl_basic_map_finalize(bmap
);
7595 isl_basic_map_free(bmap
);
7600 * returns [domain -> range] -> range - domain
7602 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7605 isl_space
*domain_dim
;
7610 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7611 map
->dim
, isl_dim_out
))
7612 isl_die(map
->ctx
, isl_error_invalid
,
7613 "domain and range don't match", goto error
);
7615 map
= isl_map_cow(map
);
7619 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7620 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7621 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7624 for (i
= 0; i
< map
->n
; ++i
) {
7625 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7629 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7636 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7638 struct isl_basic_map
*bmap
;
7646 nparam
= dims
->nparam
;
7648 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7652 for (i
= 0; i
< dim
; ++i
) {
7653 int j
= isl_basic_map_alloc_equality(bmap
);
7656 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7657 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7658 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7660 return isl_basic_map_finalize(bmap
);
7662 isl_basic_map_free(bmap
);
7666 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7670 if (dim
->n_in
!= dim
->n_out
)
7671 isl_die(dim
->ctx
, isl_error_invalid
,
7672 "number of input and output dimensions needs to be "
7673 "the same", goto error
);
7674 return basic_map_identity(dim
);
7676 isl_space_free(dim
);
7680 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7682 if (!model
|| !model
->dim
)
7684 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7687 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7689 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7692 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7694 if (!model
|| !model
->dim
)
7696 return isl_map_identity(isl_space_copy(model
->dim
));
7699 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7701 if (!model
|| !model
->dim
)
7703 return isl_map_identity(isl_space_copy(model
->dim
));
7706 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7708 isl_space
*dim
= isl_set_get_space(set
);
7710 id
= isl_map_identity(isl_space_map_from_set(dim
));
7711 return isl_map_intersect_range(id
, set
);
7714 /* Construct a basic set with all set dimensions having only non-negative
7717 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7718 __isl_take isl_space
*space
)
7723 struct isl_basic_set
*bset
;
7727 nparam
= space
->nparam
;
7729 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7732 for (i
= 0; i
< dim
; ++i
) {
7733 int k
= isl_basic_set_alloc_inequality(bset
);
7736 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7737 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7741 isl_basic_set_free(bset
);
7745 /* Construct the half-space x_pos >= 0.
7747 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7751 isl_basic_set
*nonneg
;
7753 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7754 k
= isl_basic_set_alloc_inequality(nonneg
);
7757 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7758 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7760 return isl_basic_set_finalize(nonneg
);
7762 isl_basic_set_free(nonneg
);
7766 /* Construct the half-space x_pos <= -1.
7768 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7773 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7774 k
= isl_basic_set_alloc_inequality(neg
);
7777 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7778 isl_int_set_si(neg
->ineq
[k
][0], -1);
7779 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7781 return isl_basic_set_finalize(neg
);
7783 isl_basic_set_free(neg
);
7787 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7788 enum isl_dim_type type
, unsigned first
, unsigned n
)
7791 isl_basic_set
*nonneg
;
7799 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7801 for (i
= 0; i
< n
; ++i
) {
7802 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7803 pos(set
->dim
, type
) + first
+ i
);
7804 neg
= neg_halfspace(isl_set_get_space(set
),
7805 pos(set
->dim
, type
) + first
+ i
);
7807 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7816 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7817 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7824 if (isl_set_plain_is_empty(set
)) {
7829 return fn(set
, signs
, user
);
7832 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7834 half
= isl_set_intersect(half
, isl_set_copy(set
));
7835 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7839 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7841 half
= isl_set_intersect(half
, set
);
7842 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7848 /* Call "fn" on the intersections of "set" with each of the orthants
7849 * (except for obviously empty intersections). The orthant is identified
7850 * by the signs array, with each entry having value 1 or -1 according
7851 * to the sign of the corresponding variable.
7853 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7854 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7864 if (isl_set_plain_is_empty(set
))
7867 nparam
= isl_set_dim(set
, isl_dim_param
);
7868 nvar
= isl_set_dim(set
, isl_dim_set
);
7870 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7872 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7880 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7882 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7885 int isl_basic_map_is_subset(
7886 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7889 struct isl_map
*map1
;
7890 struct isl_map
*map2
;
7892 if (!bmap1
|| !bmap2
)
7895 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7896 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7898 is_subset
= isl_map_is_subset(map1
, map2
);
7906 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7907 __isl_keep isl_basic_set
*bset2
)
7909 return isl_basic_map_is_subset(bset1
, bset2
);
7912 int isl_basic_map_is_equal(
7913 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7917 if (!bmap1
|| !bmap2
)
7919 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7922 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7926 int isl_basic_set_is_equal(
7927 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7929 return isl_basic_map_is_equal(
7930 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7933 int isl_map_is_empty(struct isl_map
*map
)
7940 for (i
= 0; i
< map
->n
; ++i
) {
7941 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7950 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7952 return map
? map
->n
== 0 : -1;
7955 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7957 return isl_map_plain_is_empty(map
);
7960 int isl_set_plain_is_empty(struct isl_set
*set
)
7962 return set
? set
->n
== 0 : -1;
7965 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7967 return isl_set_plain_is_empty(set
);
7970 int isl_set_is_empty(struct isl_set
*set
)
7972 return isl_map_is_empty((struct isl_map
*)set
);
7975 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7980 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7983 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7988 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7991 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7997 is_subset
= isl_map_is_subset(map1
, map2
);
8000 is_subset
= isl_map_is_subset(map2
, map1
);
8004 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8006 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8009 int isl_basic_map_is_strict_subset(
8010 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8014 if (!bmap1
|| !bmap2
)
8016 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8019 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8020 if (is_subset
== -1)
8025 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
8031 is_subset
= isl_map_is_subset(map1
, map2
);
8034 is_subset
= isl_map_is_subset(map2
, map1
);
8035 if (is_subset
== -1)
8040 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8042 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
8045 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
8049 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8052 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
8056 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
8059 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8066 for (i
= 0; i
< map
->n
; ++i
) {
8067 int r
= isl_basic_map_is_universe(map
->p
[i
]);
8075 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8077 return isl_map_plain_is_universe((isl_map
*) set
);
8080 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
8082 return isl_set_plain_is_universe(set
);
8085 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
8087 struct isl_basic_set
*bset
= NULL
;
8088 struct isl_vec
*sample
= NULL
;
8095 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8098 if (isl_basic_map_is_universe(bmap
))
8101 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8102 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8103 copy
= isl_basic_map_remove_redundancies(copy
);
8104 empty
= isl_basic_map_plain_is_empty(copy
);
8105 isl_basic_map_free(copy
);
8109 total
= 1 + isl_basic_map_total_dim(bmap
);
8110 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
8111 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
8117 isl_vec_free(bmap
->sample
);
8118 bmap
->sample
= NULL
;
8119 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8122 sample
= isl_basic_set_sample_vec(bset
);
8125 empty
= sample
->size
== 0;
8126 isl_vec_free(bmap
->sample
);
8127 bmap
->sample
= sample
;
8129 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8134 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8138 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8141 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
8143 return isl_basic_map_plain_is_empty(bmap
);
8146 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8150 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8153 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
8155 return isl_basic_set_plain_is_empty(bset
);
8158 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
8160 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
8163 struct isl_map
*isl_basic_map_union(
8164 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8166 struct isl_map
*map
;
8167 if (!bmap1
|| !bmap2
)
8170 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8172 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8175 map
= isl_map_add_basic_map(map
, bmap1
);
8176 map
= isl_map_add_basic_map(map
, bmap2
);
8179 isl_basic_map_free(bmap1
);
8180 isl_basic_map_free(bmap2
);
8184 struct isl_set
*isl_basic_set_union(
8185 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8187 return (struct isl_set
*)isl_basic_map_union(
8188 (struct isl_basic_map
*)bset1
,
8189 (struct isl_basic_map
*)bset2
);
8192 /* Order divs such that any div only depends on previous divs */
8193 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8201 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8203 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8205 if (isl_int_is_zero(bmap
->div
[i
][0]))
8207 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8211 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8217 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8219 return (struct isl_basic_set
*)
8220 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
8223 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8230 for (i
= 0; i
< map
->n
; ++i
) {
8231 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8242 /* Apply the expansion computed by isl_merge_divs.
8243 * The expansion itself is given by "exp" while the resulting
8244 * list of divs is given by "div".
8246 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8247 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8252 bset
= isl_basic_set_cow(bset
);
8256 if (div
->n_row
< bset
->n_div
)
8257 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8258 "not an expansion", goto error
);
8260 n_div
= bset
->n_div
;
8261 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8262 div
->n_row
- n_div
, 0,
8263 2 * (div
->n_row
- n_div
));
8265 for (i
= n_div
; i
< div
->n_row
; ++i
)
8266 if (isl_basic_set_alloc_div(bset
) < 0)
8270 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8271 if (j
>= 0 && exp
[j
] == i
) {
8273 isl_basic_map_swap_div(bset
, i
, j
);
8276 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8277 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8285 isl_basic_set_free(bset
);
8290 /* Look for a div in dst that corresponds to the div "div" in src.
8291 * The divs before "div" in src and dst are assumed to be the same.
8293 * Returns -1 if no corresponding div was found and the position
8294 * of the corresponding div in dst otherwise.
8296 static int find_div(struct isl_basic_map
*dst
,
8297 struct isl_basic_map
*src
, unsigned div
)
8301 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8303 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8304 for (i
= div
; i
< dst
->n_div
; ++i
)
8305 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8306 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8307 dst
->n_div
- div
) == -1)
8312 /* Align the divs of "dst" to those of "src", adding divs from "src"
8313 * if needed. That is, make sure that the first src->n_div divs
8314 * of the result are equal to those of src.
8316 * The result is not finalized as by design it will have redundant
8317 * divs if any divs from "src" were copied.
8319 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8320 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8323 int known
, extended
;
8327 return isl_basic_map_free(dst
);
8329 if (src
->n_div
== 0)
8332 known
= isl_basic_map_divs_known(src
);
8334 return isl_basic_map_free(dst
);
8336 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8337 "some src divs are unknown",
8338 return isl_basic_map_free(dst
));
8340 src
= isl_basic_map_order_divs(src
);
8343 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8344 for (i
= 0; i
< src
->n_div
; ++i
) {
8345 int j
= find_div(dst
, src
, i
);
8348 int extra
= src
->n_div
- i
;
8349 dst
= isl_basic_map_cow(dst
);
8352 dst
= isl_basic_map_extend_space(dst
,
8353 isl_space_copy(dst
->dim
),
8354 extra
, 0, 2 * extra
);
8357 j
= isl_basic_map_alloc_div(dst
);
8359 return isl_basic_map_free(dst
);
8360 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8361 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8362 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8363 return isl_basic_map_free(dst
);
8366 isl_basic_map_swap_div(dst
, i
, j
);
8371 struct isl_basic_set
*isl_basic_set_align_divs(
8372 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8374 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8375 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8378 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8386 map
= isl_map_compute_divs(map
);
8387 map
= isl_map_cow(map
);
8391 for (i
= 1; i
< map
->n
; ++i
)
8392 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8393 for (i
= 1; i
< map
->n
; ++i
) {
8394 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8396 return isl_map_free(map
);
8399 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8403 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8405 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8408 /* Align the divs of the basic sets in "set" to those
8409 * of the basic sets in "list", as well as to the other basic sets in "set".
8410 * The elements in "list" are assumed to have known divs.
8412 __isl_give isl_set
*isl_set_align_divs_to_basic_set_list(
8413 __isl_take isl_set
*set
, __isl_keep isl_basic_set_list
*list
)
8417 set
= isl_set_compute_divs(set
);
8418 set
= isl_set_cow(set
);
8420 return isl_set_free(set
);
8424 n
= isl_basic_set_list_n_basic_set(list
);
8425 for (i
= 0; i
< n
; ++i
) {
8426 isl_basic_set
*bset
;
8428 bset
= isl_basic_set_list_get_basic_set(list
, i
);
8429 set
->p
[0] = isl_basic_set_align_divs(set
->p
[0], bset
);
8430 isl_basic_set_free(bset
);
8433 return isl_set_free(set
);
8435 return isl_set_align_divs(set
);
8438 /* Align the divs of each element of "list" to those of "bset".
8439 * Both "bset" and the elements of "list" are assumed to have known divs.
8441 __isl_give isl_basic_set_list
*isl_basic_set_list_align_divs_to_basic_set(
8442 __isl_take isl_basic_set_list
*list
, __isl_keep isl_basic_set
*bset
)
8447 return isl_basic_set_list_free(list
);
8449 n
= isl_basic_set_list_n_basic_set(list
);
8450 for (i
= 0; i
< n
; ++i
) {
8451 isl_basic_set
*bset_i
;
8453 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
8454 bset_i
= isl_basic_set_align_divs(bset_i
, bset
);
8455 list
= isl_basic_set_list_set_basic_set(list
, i
, bset_i
);
8461 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8462 __isl_take isl_map
*map
)
8466 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8467 map
= isl_map_intersect_domain(map
, set
);
8468 set
= isl_map_range(map
);
8476 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8477 __isl_take isl_map
*map
)
8479 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8482 /* There is no need to cow as removing empty parts doesn't change
8483 * the meaning of the set.
8485 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8492 for (i
= map
->n
- 1; i
>= 0; --i
)
8493 remove_if_empty(map
, i
);
8498 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8500 return (struct isl_set
*)
8501 isl_map_remove_empty_parts((struct isl_map
*)set
);
8504 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8506 struct isl_basic_map
*bmap
;
8507 if (!map
|| map
->n
== 0)
8509 bmap
= map
->p
[map
->n
-1];
8510 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8511 return isl_basic_map_copy(bmap
);
8514 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8516 return (struct isl_basic_set
*)
8517 isl_map_copy_basic_map((struct isl_map
*)set
);
8520 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8521 __isl_keep isl_basic_map
*bmap
)
8527 for (i
= map
->n
-1; i
>= 0; --i
) {
8528 if (map
->p
[i
] != bmap
)
8530 map
= isl_map_cow(map
);
8533 isl_basic_map_free(map
->p
[i
]);
8534 if (i
!= map
->n
-1) {
8535 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8536 map
->p
[i
] = map
->p
[map
->n
-1];
8547 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8548 struct isl_basic_set
*bset
)
8550 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8551 (struct isl_basic_map
*)bset
);
8554 /* Given two basic sets bset1 and bset2, compute the maximal difference
8555 * between the values of dimension pos in bset1 and those in bset2
8556 * for any common value of the parameters and dimensions preceding pos.
8558 static enum isl_lp_result
basic_set_maximal_difference_at(
8559 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8560 int pos
, isl_int
*opt
)
8563 struct isl_basic_map
*bmap1
= NULL
;
8564 struct isl_basic_map
*bmap2
= NULL
;
8565 struct isl_ctx
*ctx
;
8566 struct isl_vec
*obj
;
8569 unsigned dim1
, dim2
;
8570 enum isl_lp_result res
;
8572 if (!bset1
|| !bset2
)
8573 return isl_lp_error
;
8575 nparam
= isl_basic_set_n_param(bset1
);
8576 dim1
= isl_basic_set_n_dim(bset1
);
8577 dim2
= isl_basic_set_n_dim(bset2
);
8578 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8579 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8580 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8581 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8582 if (!bmap1
|| !bmap2
)
8584 bmap1
= isl_basic_map_cow(bmap1
);
8585 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8586 pos
, (dim1
- pos
) + (dim2
- pos
),
8587 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8588 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8591 total
= isl_basic_map_total_dim(bmap1
);
8593 obj
= isl_vec_alloc(ctx
, 1 + total
);
8596 isl_seq_clr(obj
->block
.data
, 1 + total
);
8597 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8598 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8599 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8601 isl_basic_map_free(bmap1
);
8605 isl_basic_map_free(bmap2
);
8607 isl_basic_map_free(bmap1
);
8608 return isl_lp_error
;
8611 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8612 * for any common value of the parameters and dimensions preceding pos
8613 * in both basic sets, the values of dimension pos in bset1 are
8614 * smaller or larger than those in bset2.
8617 * 1 if bset1 follows bset2
8618 * -1 if bset1 precedes bset2
8619 * 0 if bset1 and bset2 are incomparable
8620 * -2 if some error occurred.
8622 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8623 struct isl_basic_set
*bset2
, int pos
)
8626 enum isl_lp_result res
;
8631 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8633 if (res
== isl_lp_empty
)
8635 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8636 res
== isl_lp_unbounded
)
8638 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8647 /* Given two basic sets bset1 and bset2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * there is a value of dimension pos in bset1 that is larger
8650 * than a value of the same dimension in bset2.
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_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8659 __isl_keep isl_basic_set
*bset2
, int pos
)
8662 enum isl_lp_result res
;
8667 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8669 if (res
== isl_lp_empty
)
8671 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8672 res
== isl_lp_unbounded
)
8674 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8676 else if (res
== isl_lp_ok
)
8685 /* Given two sets set1 and set2, check whether
8686 * for any common value of the parameters and dimensions preceding pos
8687 * there is a value of dimension pos in set1 that is larger
8688 * than a value of the same dimension in set2.
8691 * 1 if there exists such a pair
8692 * 0 if there is no such pair, but there is a pair of equal values
8694 * -2 if some error occurred.
8696 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8697 __isl_keep isl_set
*set2
, int pos
)
8705 for (i
= 0; i
< set1
->n
; ++i
)
8706 for (j
= 0; j
< set2
->n
; ++j
) {
8708 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8709 if (f
== 1 || f
== -2)
8718 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8719 unsigned pos
, isl_int
*val
)
8727 total
= isl_basic_map_total_dim(bmap
);
8728 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8729 for (; d
+1 > pos
; --d
)
8730 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8734 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8736 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8738 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8741 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8747 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8748 unsigned pos
, isl_int
*val
)
8760 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8763 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8764 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8765 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8766 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8770 isl_int_set(*val
, v
);
8776 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8777 unsigned pos
, isl_int
*val
)
8779 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8783 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8786 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8789 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8790 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8792 if (pos
>= isl_basic_map_dim(bmap
, type
))
8794 return isl_basic_map_plain_has_fixed_var(bmap
,
8795 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8798 /* If "bmap" 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_basic_map_plain_get_val_if_fixed(
8803 __isl_keep isl_basic_map
*bmap
,
8804 enum isl_dim_type type
, unsigned pos
)
8812 ctx
= isl_basic_map_get_ctx(bmap
);
8813 v
= isl_val_alloc(ctx
);
8816 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8818 return isl_val_free(v
);
8820 isl_int_set_si(v
->d
, 1);
8824 return isl_val_nan(ctx
);
8827 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8828 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8830 if (pos
>= isl_map_dim(map
, type
))
8832 return isl_map_plain_has_fixed_var(map
,
8833 map_offset(map
, type
) - 1 + pos
, val
);
8836 /* If "map" obviously lies on a hyperplane where the given dimension
8837 * has a fixed value, then return that value.
8838 * Otherwise return NaN.
8840 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8841 enum isl_dim_type type
, unsigned pos
)
8849 ctx
= isl_map_get_ctx(map
);
8850 v
= isl_val_alloc(ctx
);
8853 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8855 return isl_val_free(v
);
8857 isl_int_set_si(v
->d
, 1);
8861 return isl_val_nan(ctx
);
8864 /* If "set" obviously lies on a hyperplane where the given dimension
8865 * has a fixed value, then return that value.
8866 * Otherwise return NaN.
8868 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8869 enum isl_dim_type type
, unsigned pos
)
8871 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8874 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8875 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8877 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8880 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8881 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8883 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8886 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8887 * then return this fixed value in *val.
8889 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8890 unsigned dim
, isl_int
*val
)
8892 return isl_basic_set_plain_has_fixed_var(bset
,
8893 isl_basic_set_n_param(bset
) + dim
, val
);
8896 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8897 * then return this fixed value in *val.
8899 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8900 unsigned dim
, isl_int
*val
)
8902 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8905 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8906 unsigned dim
, isl_int
*val
)
8908 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8911 /* Check if input variable in has fixed value and if so and if val is not NULL,
8912 * then return this fixed value in *val.
8914 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8915 unsigned in
, isl_int
*val
)
8917 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8920 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8921 * and if val is not NULL, then return this lower bound in *val.
8923 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8924 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8926 int i
, i_eq
= -1, i_ineq
= -1;
8933 total
= isl_basic_set_total_dim(bset
);
8934 nparam
= isl_basic_set_n_param(bset
);
8935 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8936 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8942 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8943 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8945 if (i_eq
!= -1 || i_ineq
!= -1)
8949 if (i_eq
== -1 && i_ineq
== -1)
8951 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8952 /* The coefficient should always be one due to normalization. */
8953 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8955 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8957 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8958 total
- nparam
- dim
- 1) != -1)
8961 isl_int_neg(*val
, c
[0]);
8965 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8966 unsigned dim
, isl_int
*val
)
8978 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8982 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8984 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8985 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8987 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8991 isl_int_set(*val
, v
);
8997 /* uset_gist depends on constraints without existentially quantified
8998 * variables sorting first.
9000 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9002 isl_int
**c1
= (isl_int
**) p1
;
9003 isl_int
**c2
= (isl_int
**) p2
;
9005 unsigned size
= *(unsigned *) arg
;
9007 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9008 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9013 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9016 static struct isl_basic_map
*isl_basic_map_sort_constraints(
9017 struct isl_basic_map
*bmap
)
9023 if (bmap
->n_ineq
== 0)
9025 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9027 total
= isl_basic_map_total_dim(bmap
);
9028 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9029 &sort_constraint_cmp
, &total
) < 0)
9030 return isl_basic_map_free(bmap
);
9034 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9035 __isl_take isl_basic_set
*bset
)
9037 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
9038 (struct isl_basic_map
*)bset
);
9041 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9045 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9047 bmap
= isl_basic_map_remove_redundancies(bmap
);
9048 bmap
= isl_basic_map_sort_constraints(bmap
);
9050 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9054 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9056 return (struct isl_basic_set
*)isl_basic_map_normalize(
9057 (struct isl_basic_map
*)bset
);
9060 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9061 const __isl_keep isl_basic_map
*bmap2
)
9068 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9069 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9070 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9071 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9072 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9073 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9074 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9075 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9076 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9077 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9078 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9080 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9082 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9084 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9085 return bmap1
->n_eq
- bmap2
->n_eq
;
9086 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9087 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9088 if (bmap1
->n_div
!= bmap2
->n_div
)
9089 return bmap1
->n_div
- bmap2
->n_div
;
9090 total
= isl_basic_map_total_dim(bmap1
);
9091 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9092 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9096 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9097 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9101 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9102 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9109 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9110 const __isl_keep isl_basic_set
*bset2
)
9112 return isl_basic_map_plain_cmp(bset1
, bset2
);
9115 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9121 if (set1
->n
!= set2
->n
)
9122 return set1
->n
- set2
->n
;
9124 for (i
= 0; i
< set1
->n
; ++i
) {
9125 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9133 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9134 __isl_keep isl_basic_map
*bmap2
)
9136 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9139 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9140 __isl_keep isl_basic_set
*bset2
)
9142 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
9143 (isl_basic_map
*)bset2
);
9146 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9148 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9149 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9151 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9154 /* Sort the basic maps of "map" and remove duplicate basic maps.
9156 * While removing basic maps, we make sure that the basic maps remain
9157 * sorted because isl_map_normalize expects the basic maps of the result
9160 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9164 map
= isl_map_remove_empty_parts(map
);
9167 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9168 for (i
= map
->n
- 1; i
>= 1; --i
) {
9169 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9171 isl_basic_map_free(map
->p
[i
-1]);
9172 for (j
= i
; j
< map
->n
; ++j
)
9173 map
->p
[j
- 1] = map
->p
[j
];
9180 /* Remove obvious duplicates among the basic maps of "map".
9182 * Unlike isl_map_normalize, this function does not remove redundant
9183 * constraints and only removes duplicates that have exactly the same
9184 * constraints in the input. It does sort the constraints and
9185 * the basic maps to ease the detection of duplicates.
9187 * If "map" has already been normalized or if the basic maps are
9188 * disjoint, then there can be no duplicates.
9190 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9193 isl_basic_map
*bmap
;
9199 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9201 for (i
= 0; i
< map
->n
; ++i
) {
9202 bmap
= isl_basic_map_copy(map
->p
[i
]);
9203 bmap
= isl_basic_map_sort_constraints(bmap
);
9205 return isl_map_free(map
);
9206 isl_basic_map_free(map
->p
[i
]);
9210 map
= sort_and_remove_duplicates(map
);
9214 /* We normalize in place, but if anything goes wrong we need
9215 * to return NULL, so we need to make sure we don't change the
9216 * meaning of any possible other copies of map.
9218 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9221 struct isl_basic_map
*bmap
;
9225 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9227 for (i
= 0; i
< map
->n
; ++i
) {
9228 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9231 isl_basic_map_free(map
->p
[i
]);
9235 map
= sort_and_remove_duplicates(map
);
9237 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9244 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9246 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
9249 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9259 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9262 map1
= isl_map_copy(map1
);
9263 map2
= isl_map_copy(map2
);
9264 map1
= isl_map_normalize(map1
);
9265 map2
= isl_map_normalize(map2
);
9268 equal
= map1
->n
== map2
->n
;
9269 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9270 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9283 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9285 return isl_map_plain_is_equal(map1
, map2
);
9288 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9290 return isl_map_plain_is_equal((struct isl_map
*)set1
,
9291 (struct isl_map
*)set2
);
9294 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9296 return isl_set_plain_is_equal(set1
, set2
);
9299 /* Return an interval that ranges from min to max (inclusive)
9301 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9302 isl_int min
, isl_int max
)
9305 struct isl_basic_set
*bset
= NULL
;
9307 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9311 k
= isl_basic_set_alloc_inequality(bset
);
9314 isl_int_set_si(bset
->ineq
[k
][1], 1);
9315 isl_int_neg(bset
->ineq
[k
][0], min
);
9317 k
= isl_basic_set_alloc_inequality(bset
);
9320 isl_int_set_si(bset
->ineq
[k
][1], -1);
9321 isl_int_set(bset
->ineq
[k
][0], max
);
9325 isl_basic_set_free(bset
);
9329 /* Return the basic sets in "set" as a list.
9331 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9332 __isl_keep isl_set
*set
)
9336 isl_basic_set_list
*list
;
9340 ctx
= isl_set_get_ctx(set
);
9341 list
= isl_basic_set_list_alloc(ctx
, set
->n
);
9343 for (i
= 0; i
< set
->n
; ++i
) {
9344 isl_basic_set
*bset
;
9346 bset
= isl_basic_set_copy(set
->p
[i
]);
9347 list
= isl_basic_set_list_add(list
, bset
);
9353 /* Return the intersection of the elements in the non-empty list "list".
9354 * All elements are assumed to live in the same space.
9356 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9357 __isl_take
struct isl_basic_set_list
*list
)
9360 isl_basic_set
*bset
;
9364 n
= isl_basic_set_list_n_basic_set(list
);
9366 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9367 "expecting non-empty list", goto error
);
9369 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9370 for (i
= 1; i
< n
; ++i
) {
9371 isl_basic_set
*bset_i
;
9373 bset_i
= isl_basic_set_list_get_basic_set(list
, i
);
9374 bset
= isl_basic_set_intersect(bset
, bset_i
);
9377 isl_basic_set_list_free(list
);
9380 isl_basic_set_list_free(list
);
9384 /* Return the Cartesian product of the basic sets in list (in the given order).
9386 __isl_give isl_basic_set
*isl_basic_set_list_product(
9387 __isl_take
struct isl_basic_set_list
*list
)
9395 struct isl_basic_set
*product
= NULL
;
9399 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9400 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9401 nparam
= isl_basic_set_n_param(list
->p
[0]);
9402 dim
= isl_basic_set_n_dim(list
->p
[0]);
9403 extra
= list
->p
[0]->n_div
;
9404 n_eq
= list
->p
[0]->n_eq
;
9405 n_ineq
= list
->p
[0]->n_ineq
;
9406 for (i
= 1; i
< list
->n
; ++i
) {
9407 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9408 isl_assert(list
->ctx
,
9409 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9410 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9411 extra
+= list
->p
[i
]->n_div
;
9412 n_eq
+= list
->p
[i
]->n_eq
;
9413 n_ineq
+= list
->p
[i
]->n_ineq
;
9415 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9420 for (i
= 0; i
< list
->n
; ++i
) {
9421 isl_basic_set_add_constraints(product
,
9422 isl_basic_set_copy(list
->p
[i
]), dim
);
9423 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9425 isl_basic_set_list_free(list
);
9428 isl_basic_set_free(product
);
9429 isl_basic_set_list_free(list
);
9433 struct isl_basic_map
*isl_basic_map_product(
9434 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9436 isl_space
*dim_result
= NULL
;
9437 struct isl_basic_map
*bmap
;
9438 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9439 struct isl_dim_map
*dim_map1
, *dim_map2
;
9441 if (!bmap1
|| !bmap2
)
9444 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9445 bmap2
->dim
, isl_dim_param
), goto error
);
9446 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9447 isl_space_copy(bmap2
->dim
));
9449 in1
= isl_basic_map_n_in(bmap1
);
9450 in2
= isl_basic_map_n_in(bmap2
);
9451 out1
= isl_basic_map_n_out(bmap1
);
9452 out2
= isl_basic_map_n_out(bmap2
);
9453 nparam
= isl_basic_map_n_param(bmap1
);
9455 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9456 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9457 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9458 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9459 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9460 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9461 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9462 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9463 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9464 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9465 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9467 bmap
= isl_basic_map_alloc_space(dim_result
,
9468 bmap1
->n_div
+ bmap2
->n_div
,
9469 bmap1
->n_eq
+ bmap2
->n_eq
,
9470 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9471 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9472 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9473 bmap
= isl_basic_map_simplify(bmap
);
9474 return isl_basic_map_finalize(bmap
);
9476 isl_basic_map_free(bmap1
);
9477 isl_basic_map_free(bmap2
);
9481 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9482 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9484 isl_basic_map
*prod
;
9486 prod
= isl_basic_map_product(bmap1
, bmap2
);
9487 prod
= isl_basic_map_flatten(prod
);
9491 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9492 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9494 return isl_basic_map_flat_range_product(bset1
, bset2
);
9497 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9498 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9500 isl_space
*space_result
= NULL
;
9501 isl_basic_map
*bmap
;
9502 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9503 struct isl_dim_map
*dim_map1
, *dim_map2
;
9505 if (!bmap1
|| !bmap2
)
9508 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9509 isl_space_copy(bmap2
->dim
));
9511 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9512 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9513 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9514 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9516 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9517 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9518 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9519 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9520 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9521 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9522 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9523 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9524 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9525 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9526 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9528 bmap
= isl_basic_map_alloc_space(space_result
,
9529 bmap1
->n_div
+ bmap2
->n_div
,
9530 bmap1
->n_eq
+ bmap2
->n_eq
,
9531 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9532 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9533 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9534 bmap
= isl_basic_map_simplify(bmap
);
9535 return isl_basic_map_finalize(bmap
);
9537 isl_basic_map_free(bmap1
);
9538 isl_basic_map_free(bmap2
);
9542 __isl_give isl_basic_map
*isl_basic_map_range_product(
9543 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9545 isl_space
*dim_result
= NULL
;
9546 isl_basic_map
*bmap
;
9547 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9548 struct isl_dim_map
*dim_map1
, *dim_map2
;
9550 if (!bmap1
|| !bmap2
)
9553 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9554 bmap2
->dim
, isl_dim_param
))
9555 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9556 "parameters don't match", goto error
);
9558 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9559 isl_space_copy(bmap2
->dim
));
9561 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9562 out1
= isl_basic_map_n_out(bmap1
);
9563 out2
= isl_basic_map_n_out(bmap2
);
9564 nparam
= isl_basic_map_n_param(bmap1
);
9566 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9567 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9568 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9569 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9570 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9571 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9572 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9573 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9574 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9575 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9576 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9578 bmap
= isl_basic_map_alloc_space(dim_result
,
9579 bmap1
->n_div
+ bmap2
->n_div
,
9580 bmap1
->n_eq
+ bmap2
->n_eq
,
9581 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9582 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9583 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9584 bmap
= isl_basic_map_simplify(bmap
);
9585 return isl_basic_map_finalize(bmap
);
9587 isl_basic_map_free(bmap1
);
9588 isl_basic_map_free(bmap2
);
9592 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9593 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9595 isl_basic_map
*prod
;
9597 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9598 prod
= isl_basic_map_flatten_range(prod
);
9602 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9603 * and collect the results.
9604 * The result live in the space obtained by calling "space_product"
9605 * on the spaces of "map1" and "map2".
9606 * If "remove_duplicates" is set then the result may contain duplicates
9607 * (even if the inputs do not) and so we try and remove the obvious
9610 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9611 __isl_take isl_map
*map2
,
9612 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9613 __isl_take isl_space
*right
),
9614 __isl_give isl_basic_map
*(*basic_map_product
)(
9615 __isl_take isl_basic_map
*left
,
9616 __isl_take isl_basic_map
*right
),
9617 int remove_duplicates
)
9620 struct isl_map
*result
;
9626 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9627 map2
->dim
, isl_dim_param
), goto error
);
9629 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9630 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9631 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9633 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9634 isl_space_copy(map2
->dim
)),
9635 map1
->n
* map2
->n
, flags
);
9638 for (i
= 0; i
< map1
->n
; ++i
)
9639 for (j
= 0; j
< map2
->n
; ++j
) {
9640 struct isl_basic_map
*part
;
9641 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9642 isl_basic_map_copy(map2
->p
[j
]));
9643 if (isl_basic_map_is_empty(part
))
9644 isl_basic_map_free(part
);
9646 result
= isl_map_add_basic_map(result
, part
);
9650 if (remove_duplicates
)
9651 result
= isl_map_remove_obvious_duplicates(result
);
9661 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9663 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9664 __isl_take isl_map
*map2
)
9666 return map_product(map1
, map2
, &isl_space_product
,
9667 &isl_basic_map_product
, 0);
9670 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9671 __isl_take isl_map
*map2
)
9673 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9676 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9678 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9679 __isl_take isl_map
*map2
)
9683 prod
= isl_map_product(map1
, map2
);
9684 prod
= isl_map_flatten(prod
);
9688 /* Given two set A and B, construct its Cartesian product A x B.
9690 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9692 return isl_map_range_product(set1
, set2
);
9695 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9696 __isl_take isl_set
*set2
)
9698 return isl_map_flat_range_product(set1
, set2
);
9701 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9703 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9704 __isl_take isl_map
*map2
)
9706 return map_product(map1
, map2
, &isl_space_domain_product
,
9707 &isl_basic_map_domain_product
, 1);
9710 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9712 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9713 __isl_take isl_map
*map2
)
9715 return map_product(map1
, map2
, &isl_space_range_product
,
9716 &isl_basic_map_range_product
, 1);
9719 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9720 __isl_take isl_map
*map2
)
9722 return isl_map_align_params_map_map_and(map1
, map2
,
9723 &map_domain_product_aligned
);
9726 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9727 __isl_take isl_map
*map2
)
9729 return isl_map_align_params_map_map_and(map1
, map2
,
9730 &map_range_product_aligned
);
9733 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9735 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9738 int total1
, keep1
, total2
, keep2
;
9742 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9743 !isl_space_range_is_wrapping(map
->dim
))
9744 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9745 "not a product", return isl_map_free(map
));
9747 space
= isl_map_get_space(map
);
9748 total1
= isl_space_dim(space
, isl_dim_in
);
9749 total2
= isl_space_dim(space
, isl_dim_out
);
9750 space
= isl_space_factor_range(space
);
9751 keep1
= isl_space_dim(space
, isl_dim_in
);
9752 keep2
= isl_space_dim(space
, isl_dim_out
);
9753 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9754 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9755 map
= isl_map_reset_space(map
, space
);
9760 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9762 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9769 if (!isl_space_domain_is_wrapping(map
->dim
))
9770 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9771 "domain is not a product", return isl_map_free(map
));
9773 space
= isl_map_get_space(map
);
9774 total
= isl_space_dim(space
, isl_dim_in
);
9775 space
= isl_space_domain_factor_domain(space
);
9776 keep
= isl_space_dim(space
, isl_dim_in
);
9777 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9778 map
= isl_map_reset_space(map
, space
);
9783 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9785 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9792 if (!isl_space_domain_is_wrapping(map
->dim
))
9793 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9794 "domain is not a product", return isl_map_free(map
));
9796 space
= isl_map_get_space(map
);
9797 total
= isl_space_dim(space
, isl_dim_in
);
9798 space
= isl_space_domain_factor_range(space
);
9799 keep
= isl_space_dim(space
, isl_dim_in
);
9800 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9801 map
= isl_map_reset_space(map
, space
);
9806 /* Given a map A -> [B -> C], extract the map A -> B.
9808 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9815 if (!isl_space_range_is_wrapping(map
->dim
))
9816 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9817 "range is not a product", return isl_map_free(map
));
9819 space
= isl_map_get_space(map
);
9820 total
= isl_space_dim(space
, isl_dim_out
);
9821 space
= isl_space_range_factor_domain(space
);
9822 keep
= isl_space_dim(space
, isl_dim_out
);
9823 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9824 map
= isl_map_reset_space(map
, space
);
9829 /* Given a map A -> [B -> C], extract the map A -> C.
9831 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9838 if (!isl_space_range_is_wrapping(map
->dim
))
9839 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9840 "range is not a product", return isl_map_free(map
));
9842 space
= isl_map_get_space(map
);
9843 total
= isl_space_dim(space
, isl_dim_out
);
9844 space
= isl_space_range_factor_range(space
);
9845 keep
= isl_space_dim(space
, isl_dim_out
);
9846 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9847 map
= isl_map_reset_space(map
, space
);
9852 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9854 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9855 __isl_take isl_map
*map2
)
9859 prod
= isl_map_domain_product(map1
, map2
);
9860 prod
= isl_map_flatten_domain(prod
);
9864 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9866 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9867 __isl_take isl_map
*map2
)
9871 prod
= isl_map_range_product(map1
, map2
);
9872 prod
= isl_map_flatten_range(prod
);
9876 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9879 uint32_t hash
= isl_hash_init();
9884 bmap
= isl_basic_map_copy(bmap
);
9885 bmap
= isl_basic_map_normalize(bmap
);
9888 total
= isl_basic_map_total_dim(bmap
);
9889 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9890 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9892 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9893 isl_hash_hash(hash
, c_hash
);
9895 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9896 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9898 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9899 isl_hash_hash(hash
, c_hash
);
9901 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9902 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9904 if (isl_int_is_zero(bmap
->div
[i
][0]))
9906 isl_hash_byte(hash
, i
& 0xFF);
9907 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9908 isl_hash_hash(hash
, c_hash
);
9910 isl_basic_map_free(bmap
);
9914 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9916 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9919 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9926 map
= isl_map_copy(map
);
9927 map
= isl_map_normalize(map
);
9931 hash
= isl_hash_init();
9932 for (i
= 0; i
< map
->n
; ++i
) {
9934 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9935 isl_hash_hash(hash
, bmap_hash
);
9943 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9945 return isl_map_get_hash((isl_map
*)set
);
9948 /* Check if the value for dimension dim is completely determined
9949 * by the values of the other parameters and variables.
9950 * That is, check if dimension dim is involved in an equality.
9952 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9959 nparam
= isl_basic_set_n_param(bset
);
9960 for (i
= 0; i
< bset
->n_eq
; ++i
)
9961 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9966 /* Check if the value for dimension dim is completely determined
9967 * by the values of the other parameters and variables.
9968 * That is, check if dimension dim is involved in an equality
9969 * for each of the subsets.
9971 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9977 for (i
= 0; i
< set
->n
; ++i
) {
9979 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9986 /* Return the number of basic maps in the (current) representation of "map".
9988 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9990 return map
? map
->n
: 0;
9993 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9995 return set
? set
->n
: 0;
9998 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9999 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10006 for (i
= 0; i
< map
->n
; ++i
)
10007 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10013 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10014 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10021 for (i
= 0; i
< set
->n
; ++i
)
10022 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10028 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10035 bset
= isl_basic_set_cow(bset
);
10039 dim
= isl_basic_set_get_space(bset
);
10040 dim
= isl_space_lift(dim
, bset
->n_div
);
10043 isl_space_free(bset
->dim
);
10045 bset
->extra
-= bset
->n_div
;
10048 bset
= isl_basic_set_finalize(bset
);
10052 isl_basic_set_free(bset
);
10056 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10062 set
= isl_set_align_divs(set
);
10067 set
= isl_set_cow(set
);
10071 n_div
= set
->p
[0]->n_div
;
10072 dim
= isl_set_get_space(set
);
10073 dim
= isl_space_lift(dim
, n_div
);
10076 isl_space_free(set
->dim
);
10079 for (i
= 0; i
< set
->n
; ++i
) {
10080 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10091 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10094 struct isl_basic_map
*bmap
;
10101 set
= isl_set_align_divs(set
);
10106 dim
= isl_set_get_space(set
);
10107 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10109 return isl_map_identity(isl_space_map_from_set(dim
));
10112 n_div
= set
->p
[0]->n_div
;
10113 dim
= isl_space_map_from_set(dim
);
10114 n_param
= isl_space_dim(dim
, isl_dim_param
);
10115 n_set
= isl_space_dim(dim
, isl_dim_in
);
10116 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10117 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10118 for (i
= 0; i
< n_set
; ++i
)
10119 bmap
= var_equal(bmap
, i
);
10121 total
= n_param
+ n_set
+ n_set
+ n_div
;
10122 for (i
= 0; i
< n_div
; ++i
) {
10123 k
= isl_basic_map_alloc_inequality(bmap
);
10126 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10127 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10128 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10129 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10130 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10131 set
->p
[0]->div
[i
][0]);
10133 l
= isl_basic_map_alloc_inequality(bmap
);
10136 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10137 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10138 set
->p
[0]->div
[i
][0]);
10139 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10143 bmap
= isl_basic_map_simplify(bmap
);
10144 bmap
= isl_basic_map_finalize(bmap
);
10145 return isl_map_from_basic_map(bmap
);
10148 isl_basic_map_free(bmap
);
10152 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10160 dim
= isl_basic_set_total_dim(bset
);
10161 size
+= bset
->n_eq
* (1 + dim
);
10162 size
+= bset
->n_ineq
* (1 + dim
);
10163 size
+= bset
->n_div
* (2 + dim
);
10168 int isl_set_size(__isl_keep isl_set
*set
)
10176 for (i
= 0; i
< set
->n
; ++i
)
10177 size
+= isl_basic_set_size(set
->p
[i
]);
10182 /* Check if there is any lower bound (if lower == 0) and/or upper
10183 * bound (if upper == 0) on the specified dim.
10185 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10186 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10193 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
10195 pos
+= isl_basic_map_offset(bmap
, type
);
10197 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10198 if (isl_int_is_zero(bmap
->div
[i
][0]))
10200 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10204 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10205 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10208 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10209 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10216 return lower
&& upper
;
10219 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10220 enum isl_dim_type type
, unsigned pos
)
10222 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10225 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10226 enum isl_dim_type type
, unsigned pos
)
10228 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10231 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10232 enum isl_dim_type type
, unsigned pos
)
10234 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10237 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10238 enum isl_dim_type type
, unsigned pos
)
10245 for (i
= 0; i
< map
->n
; ++i
) {
10247 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10248 if (bounded
< 0 || !bounded
)
10255 /* Return 1 if the specified dim is involved in both an upper bound
10256 * and a lower bound.
10258 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10259 enum isl_dim_type type
, unsigned pos
)
10261 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
10264 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10266 static int has_any_bound(__isl_keep isl_map
*map
,
10267 enum isl_dim_type type
, unsigned pos
,
10268 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10269 enum isl_dim_type type
, unsigned pos
))
10276 for (i
= 0; i
< map
->n
; ++i
) {
10278 bounded
= fn(map
->p
[i
], type
, pos
);
10279 if (bounded
< 0 || bounded
)
10286 /* Return 1 if the specified dim is involved in any lower bound.
10288 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10289 enum isl_dim_type type
, unsigned pos
)
10291 return has_any_bound(set
, type
, pos
,
10292 &isl_basic_map_dim_has_lower_bound
);
10295 /* Return 1 if the specified dim is involved in any upper bound.
10297 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10298 enum isl_dim_type type
, unsigned pos
)
10300 return has_any_bound(set
, type
, pos
,
10301 &isl_basic_map_dim_has_upper_bound
);
10304 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10306 static int has_bound(__isl_keep isl_map
*map
,
10307 enum isl_dim_type type
, unsigned pos
,
10308 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
10309 enum isl_dim_type type
, unsigned pos
))
10316 for (i
= 0; i
< map
->n
; ++i
) {
10318 bounded
= fn(map
->p
[i
], type
, pos
);
10319 if (bounded
< 0 || !bounded
)
10326 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10328 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10329 enum isl_dim_type type
, unsigned pos
)
10331 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10334 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10336 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10337 enum isl_dim_type type
, unsigned pos
)
10339 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10342 /* For each of the "n" variables starting at "first", determine
10343 * the sign of the variable and put the results in the first "n"
10344 * elements of the array "signs".
10346 * 1 means that the variable is non-negative
10347 * -1 means that the variable is non-positive
10348 * 0 means the variable attains both positive and negative values.
10350 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10351 unsigned first
, unsigned n
, int *signs
)
10353 isl_vec
*bound
= NULL
;
10354 struct isl_tab
*tab
= NULL
;
10355 struct isl_tab_undo
*snap
;
10358 if (!bset
|| !signs
)
10361 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10362 tab
= isl_tab_from_basic_set(bset
, 0);
10363 if (!bound
|| !tab
)
10366 isl_seq_clr(bound
->el
, bound
->size
);
10367 isl_int_set_si(bound
->el
[0], -1);
10369 snap
= isl_tab_snap(tab
);
10370 for (i
= 0; i
< n
; ++i
) {
10373 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10374 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10376 empty
= tab
->empty
;
10377 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10378 if (isl_tab_rollback(tab
, snap
) < 0)
10386 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10387 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10389 empty
= tab
->empty
;
10390 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10391 if (isl_tab_rollback(tab
, snap
) < 0)
10394 signs
[i
] = empty
? -1 : 0;
10398 isl_vec_free(bound
);
10402 isl_vec_free(bound
);
10406 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10407 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10409 if (!bset
|| !signs
)
10411 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10414 first
+= pos(bset
->dim
, type
) - 1;
10415 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10418 /* Is it possible for the integer division "div" to depend (possibly
10419 * indirectly) on any output dimensions?
10421 * If the div is undefined, then we conservatively assume that it
10422 * may depend on them.
10423 * Otherwise, we check if it actually depends on them or on any integer
10424 * divisions that may depend on them.
10426 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10429 unsigned n_out
, o_out
;
10430 unsigned n_div
, o_div
;
10432 if (isl_int_is_zero(bmap
->div
[div
][0]))
10435 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10436 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10438 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10441 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10442 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10444 for (i
= 0; i
< n_div
; ++i
) {
10445 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10447 if (div_may_involve_output(bmap
, i
))
10454 /* Return the index of the equality of "bmap" that defines
10455 * the output dimension "pos" in terms of earlier dimensions.
10456 * The equality may also involve integer divisions, as long
10457 * as those integer divisions are defined in terms of
10458 * parameters or input dimensions.
10459 * Return bmap->n_eq if there is no such equality.
10460 * Return -1 on error.
10462 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10466 unsigned n_out
, o_out
;
10467 unsigned n_div
, o_div
;
10472 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10473 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10474 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10475 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10477 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10478 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10480 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10481 n_out
- (pos
+ 1)) != -1)
10483 for (k
= 0; k
< n_div
; ++k
) {
10484 if (isl_int_is_zero(bmap
->eq
[j
][o_div
+ k
]))
10486 if (div_may_involve_output(bmap
, k
))
10496 /* Check if the given basic map is obviously single-valued.
10497 * In particular, for each output dimension, check that there is
10498 * an equality that defines the output dimension in terms of
10499 * earlier dimensions.
10501 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10509 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10511 for (i
= 0; i
< n_out
; ++i
) {
10514 eq
= isl_basic_map_output_defining_equality(bmap
, i
);
10517 if (eq
>= bmap
->n_eq
)
10524 /* Check if the given basic map is single-valued.
10525 * We simply compute
10529 * and check if the result is a subset of the identity mapping.
10531 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10534 isl_basic_map
*test
;
10538 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10542 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10543 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10545 space
= isl_basic_map_get_space(bmap
);
10546 space
= isl_space_map_from_set(isl_space_range(space
));
10547 id
= isl_basic_map_identity(space
);
10549 sv
= isl_basic_map_is_subset(test
, id
);
10551 isl_basic_map_free(test
);
10552 isl_basic_map_free(id
);
10557 /* Check if the given map is obviously single-valued.
10559 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10568 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10571 /* Check if the given map is single-valued.
10572 * We simply compute
10576 * and check if the result is a subset of the identity mapping.
10578 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
10585 sv
= isl_map_plain_is_single_valued(map
);
10589 test
= isl_map_reverse(isl_map_copy(map
));
10590 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10592 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10593 id
= isl_map_identity(dim
);
10595 sv
= isl_map_is_subset(test
, id
);
10597 isl_map_free(test
);
10603 int isl_map_is_injective(__isl_keep isl_map
*map
)
10607 map
= isl_map_copy(map
);
10608 map
= isl_map_reverse(map
);
10609 in
= isl_map_is_single_valued(map
);
10615 /* Check if the given map is obviously injective.
10617 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10621 map
= isl_map_copy(map
);
10622 map
= isl_map_reverse(map
);
10623 in
= isl_map_plain_is_single_valued(map
);
10629 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10633 sv
= isl_map_is_single_valued(map
);
10637 return isl_map_is_injective(map
);
10640 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10642 return isl_map_is_single_valued((isl_map
*)set
);
10645 int isl_map_is_translation(__isl_keep isl_map
*map
)
10650 delta
= isl_map_deltas(isl_map_copy(map
));
10651 ok
= isl_set_is_singleton(delta
);
10652 isl_set_free(delta
);
10657 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10659 if (isl_seq_first_non_zero(p
, pos
) != -1)
10661 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10666 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10675 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10678 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10679 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10680 for (j
= 0; j
< nvar
; ++j
) {
10681 int lower
= 0, upper
= 0;
10682 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10683 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10685 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10689 if (i
< bset
->n_eq
)
10691 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10692 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10694 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10696 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10701 if (!lower
|| !upper
)
10708 int isl_set_is_box(__isl_keep isl_set
*set
)
10715 return isl_basic_set_is_box(set
->p
[0]);
10718 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10723 return isl_space_is_wrapping(bset
->dim
);
10726 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10731 return isl_space_is_wrapping(set
->dim
);
10734 /* Is the domain of "map" a wrapped relation?
10736 int isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10741 return isl_space_domain_is_wrapping(map
->dim
);
10744 /* Is the range of "map" a wrapped relation?
10746 int isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10751 return isl_space_range_is_wrapping(map
->dim
);
10754 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10756 bmap
= isl_basic_map_cow(bmap
);
10760 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10764 bmap
= isl_basic_map_finalize(bmap
);
10766 return (isl_basic_set
*)bmap
;
10768 isl_basic_map_free(bmap
);
10772 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10776 map
= isl_map_cow(map
);
10780 for (i
= 0; i
< map
->n
; ++i
) {
10781 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10785 map
->dim
= isl_space_wrap(map
->dim
);
10789 return (isl_set
*)map
;
10795 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10797 bset
= isl_basic_set_cow(bset
);
10801 bset
->dim
= isl_space_unwrap(bset
->dim
);
10805 bset
= isl_basic_set_finalize(bset
);
10807 return (isl_basic_map
*)bset
;
10809 isl_basic_set_free(bset
);
10813 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10820 if (!isl_set_is_wrapping(set
))
10821 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10824 set
= isl_set_cow(set
);
10828 for (i
= 0; i
< set
->n
; ++i
) {
10829 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10834 set
->dim
= isl_space_unwrap(set
->dim
);
10838 return (isl_map
*)set
;
10844 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10845 enum isl_dim_type type
)
10850 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10853 bmap
= isl_basic_map_cow(bmap
);
10857 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10861 bmap
= isl_basic_map_finalize(bmap
);
10865 isl_basic_map_free(bmap
);
10869 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10870 enum isl_dim_type type
)
10877 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10880 map
= isl_map_cow(map
);
10884 for (i
= 0; i
< map
->n
; ++i
) {
10885 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10889 map
->dim
= isl_space_reset(map
->dim
, type
);
10899 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10904 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10907 bmap
= isl_basic_map_cow(bmap
);
10911 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10915 bmap
= isl_basic_map_finalize(bmap
);
10919 isl_basic_map_free(bmap
);
10923 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10925 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10928 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10929 __isl_take isl_basic_map
*bmap
)
10934 if (!bmap
->dim
->nested
[0])
10937 bmap
= isl_basic_map_cow(bmap
);
10941 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10945 bmap
= isl_basic_map_finalize(bmap
);
10949 isl_basic_map_free(bmap
);
10953 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10954 __isl_take isl_basic_map
*bmap
)
10959 if (!bmap
->dim
->nested
[1])
10962 bmap
= isl_basic_map_cow(bmap
);
10966 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10970 bmap
= isl_basic_map_finalize(bmap
);
10974 isl_basic_map_free(bmap
);
10978 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10985 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10988 map
= isl_map_cow(map
);
10992 for (i
= 0; i
< map
->n
; ++i
) {
10993 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10997 map
->dim
= isl_space_flatten(map
->dim
);
11007 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11009 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
11012 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11014 isl_space
*dim
, *flat_dim
;
11017 dim
= isl_set_get_space(set
);
11018 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11019 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11020 map
= isl_map_intersect_domain(map
, set
);
11025 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11032 if (!map
->dim
->nested
[0])
11035 map
= isl_map_cow(map
);
11039 for (i
= 0; i
< map
->n
; ++i
) {
11040 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
11044 map
->dim
= isl_space_flatten_domain(map
->dim
);
11054 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11061 if (!map
->dim
->nested
[1])
11064 map
= isl_map_cow(map
);
11068 for (i
= 0; i
< map
->n
; ++i
) {
11069 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
11073 map
->dim
= isl_space_flatten_range(map
->dim
);
11083 /* Reorder the dimensions of "bmap" according to the given dim_map
11084 * and set the dimension specification to "dim".
11086 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11087 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11089 isl_basic_map
*res
;
11092 bmap
= isl_basic_map_cow(bmap
);
11093 if (!bmap
|| !dim
|| !dim_map
)
11096 flags
= bmap
->flags
;
11097 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11098 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11099 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11100 res
= isl_basic_map_alloc_space(dim
,
11101 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11102 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11104 res
->flags
= flags
;
11105 res
= isl_basic_map_finalize(res
);
11109 isl_basic_map_free(bmap
);
11110 isl_space_free(dim
);
11114 /* Reorder the dimensions of "map" according to given reordering.
11116 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11117 __isl_take isl_reordering
*r
)
11120 struct isl_dim_map
*dim_map
;
11122 map
= isl_map_cow(map
);
11123 dim_map
= isl_dim_map_from_reordering(r
);
11124 if (!map
|| !r
|| !dim_map
)
11127 for (i
= 0; i
< map
->n
; ++i
) {
11128 struct isl_dim_map
*dim_map_i
;
11130 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11132 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11133 isl_space_copy(r
->dim
), dim_map_i
);
11139 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11141 isl_reordering_free(r
);
11147 isl_reordering_free(r
);
11151 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11152 __isl_take isl_reordering
*r
)
11154 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
11157 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11158 __isl_take isl_space
*model
)
11162 if (!map
|| !model
)
11165 ctx
= isl_space_get_ctx(model
);
11166 if (!isl_space_has_named_params(model
))
11167 isl_die(ctx
, isl_error_invalid
,
11168 "model has unnamed parameters", goto error
);
11169 if (!isl_space_has_named_params(map
->dim
))
11170 isl_die(ctx
, isl_error_invalid
,
11171 "relation has unnamed parameters", goto error
);
11172 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11173 isl_reordering
*exp
;
11175 model
= isl_space_drop_dims(model
, isl_dim_in
,
11176 0, isl_space_dim(model
, isl_dim_in
));
11177 model
= isl_space_drop_dims(model
, isl_dim_out
,
11178 0, isl_space_dim(model
, isl_dim_out
));
11179 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11180 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11181 map
= isl_map_realign(map
, exp
);
11184 isl_space_free(model
);
11187 isl_space_free(model
);
11192 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11193 __isl_take isl_space
*model
)
11195 return isl_map_align_params(set
, model
);
11198 /* Align the parameters of "bmap" to those of "model", introducing
11199 * additional parameters if needed.
11201 __isl_give isl_basic_map
*isl_basic_map_align_params(
11202 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11206 if (!bmap
|| !model
)
11209 ctx
= isl_space_get_ctx(model
);
11210 if (!isl_space_has_named_params(model
))
11211 isl_die(ctx
, isl_error_invalid
,
11212 "model has unnamed parameters", goto error
);
11213 if (!isl_space_has_named_params(bmap
->dim
))
11214 isl_die(ctx
, isl_error_invalid
,
11215 "relation has unnamed parameters", goto error
);
11216 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11217 isl_reordering
*exp
;
11218 struct isl_dim_map
*dim_map
;
11220 model
= isl_space_drop_dims(model
, isl_dim_in
,
11221 0, isl_space_dim(model
, isl_dim_in
));
11222 model
= isl_space_drop_dims(model
, isl_dim_out
,
11223 0, isl_space_dim(model
, isl_dim_out
));
11224 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11225 exp
= isl_reordering_extend_space(exp
,
11226 isl_basic_map_get_space(bmap
));
11227 dim_map
= isl_dim_map_from_reordering(exp
);
11228 bmap
= isl_basic_map_realign(bmap
,
11229 exp
? isl_space_copy(exp
->dim
) : NULL
,
11230 isl_dim_map_extend(dim_map
, bmap
));
11231 isl_reordering_free(exp
);
11235 isl_space_free(model
);
11238 isl_space_free(model
);
11239 isl_basic_map_free(bmap
);
11243 /* Align the parameters of "bset" to those of "model", introducing
11244 * additional parameters if needed.
11246 __isl_give isl_basic_set
*isl_basic_set_align_params(
11247 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11249 return isl_basic_map_align_params(bset
, model
);
11252 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11253 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11254 enum isl_dim_type c2
, enum isl_dim_type c3
,
11255 enum isl_dim_type c4
, enum isl_dim_type c5
)
11257 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11258 struct isl_mat
*mat
;
11264 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11265 isl_basic_map_total_dim(bmap
) + 1);
11268 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11269 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11270 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11271 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11272 isl_int_set(mat
->row
[i
][pos
],
11273 bmap
->eq
[i
][off
+ k
]);
11281 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11282 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11283 enum isl_dim_type c2
, enum isl_dim_type c3
,
11284 enum isl_dim_type c4
, enum isl_dim_type c5
)
11286 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11287 struct isl_mat
*mat
;
11293 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11294 isl_basic_map_total_dim(bmap
) + 1);
11297 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11298 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11299 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11300 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11301 isl_int_set(mat
->row
[i
][pos
],
11302 bmap
->ineq
[i
][off
+ k
]);
11310 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11311 __isl_take isl_space
*dim
,
11312 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11313 enum isl_dim_type c2
, enum isl_dim_type c3
,
11314 enum isl_dim_type c4
, enum isl_dim_type c5
)
11316 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11317 isl_basic_map
*bmap
;
11323 if (!dim
|| !eq
|| !ineq
)
11326 if (eq
->n_col
!= ineq
->n_col
)
11327 isl_die(dim
->ctx
, isl_error_invalid
,
11328 "equalities and inequalities matrices should have "
11329 "same number of columns", goto error
);
11331 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11333 if (eq
->n_col
< total
)
11334 isl_die(dim
->ctx
, isl_error_invalid
,
11335 "number of columns too small", goto error
);
11337 extra
= eq
->n_col
- total
;
11339 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11340 eq
->n_row
, ineq
->n_row
);
11343 for (i
= 0; i
< extra
; ++i
) {
11344 k
= isl_basic_map_alloc_div(bmap
);
11347 isl_int_set_si(bmap
->div
[k
][0], 0);
11349 for (i
= 0; i
< eq
->n_row
; ++i
) {
11350 l
= isl_basic_map_alloc_equality(bmap
);
11353 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11354 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11355 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11356 isl_int_set(bmap
->eq
[l
][off
+ k
],
11362 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11363 l
= isl_basic_map_alloc_inequality(bmap
);
11366 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11367 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11368 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11369 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11370 ineq
->row
[i
][pos
]);
11376 isl_space_free(dim
);
11378 isl_mat_free(ineq
);
11380 bmap
= isl_basic_map_simplify(bmap
);
11381 return isl_basic_map_finalize(bmap
);
11383 isl_space_free(dim
);
11385 isl_mat_free(ineq
);
11389 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11390 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11391 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11393 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
11394 c1
, c2
, c3
, c4
, isl_dim_in
);
11397 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11398 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11399 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11401 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
11402 c1
, c2
, c3
, c4
, isl_dim_in
);
11405 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11406 __isl_take isl_space
*dim
,
11407 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11408 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11410 return (isl_basic_set
*)
11411 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11412 c1
, c2
, c3
, c4
, isl_dim_in
);
11415 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11420 return isl_space_can_zip(bmap
->dim
);
11423 int isl_map_can_zip(__isl_keep isl_map
*map
)
11428 return isl_space_can_zip(map
->dim
);
11431 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11432 * (A -> C) -> (B -> D).
11434 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11443 if (!isl_basic_map_can_zip(bmap
))
11444 isl_die(bmap
->ctx
, isl_error_invalid
,
11445 "basic map cannot be zipped", goto error
);
11446 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11447 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11448 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11449 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11450 bmap
= isl_basic_map_cow(bmap
);
11451 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11454 bmap
->dim
= isl_space_zip(bmap
->dim
);
11459 isl_basic_map_free(bmap
);
11463 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11464 * (A -> C) -> (B -> D).
11466 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11473 if (!isl_map_can_zip(map
))
11474 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11477 map
= isl_map_cow(map
);
11481 for (i
= 0; i
< map
->n
; ++i
) {
11482 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11487 map
->dim
= isl_space_zip(map
->dim
);
11497 /* Can we apply isl_basic_map_curry to "bmap"?
11498 * That is, does it have a nested relation in its domain?
11500 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11505 return isl_space_can_curry(bmap
->dim
);
11508 /* Can we apply isl_map_curry to "map"?
11509 * That is, does it have a nested relation in its domain?
11511 int isl_map_can_curry(__isl_keep isl_map
*map
)
11516 return isl_space_can_curry(map
->dim
);
11519 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11522 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11528 if (!isl_basic_map_can_curry(bmap
))
11529 isl_die(bmap
->ctx
, isl_error_invalid
,
11530 "basic map cannot be curried", goto error
);
11531 bmap
= isl_basic_map_cow(bmap
);
11534 bmap
->dim
= isl_space_curry(bmap
->dim
);
11539 isl_basic_map_free(bmap
);
11543 /* Given a map (A -> B) -> C, return the corresponding map
11546 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11553 if (!isl_map_can_curry(map
))
11554 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
11557 map
= isl_map_cow(map
);
11561 for (i
= 0; i
< map
->n
; ++i
) {
11562 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
11567 map
->dim
= isl_space_curry(map
->dim
);
11577 /* Can we apply isl_basic_map_uncurry to "bmap"?
11578 * That is, does it have a nested relation in its domain?
11580 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11585 return isl_space_can_uncurry(bmap
->dim
);
11588 /* Can we apply isl_map_uncurry to "map"?
11589 * That is, does it have a nested relation in its domain?
11591 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
11596 return isl_space_can_uncurry(map
->dim
);
11599 /* Given a basic map A -> (B -> C), return the corresponding basic map
11602 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11608 if (!isl_basic_map_can_uncurry(bmap
))
11609 isl_die(bmap
->ctx
, isl_error_invalid
,
11610 "basic map cannot be uncurried",
11611 return isl_basic_map_free(bmap
));
11612 bmap
= isl_basic_map_cow(bmap
);
11615 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11617 return isl_basic_map_free(bmap
);
11621 /* Given a map A -> (B -> C), return the corresponding map
11624 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11631 if (!isl_map_can_uncurry(map
))
11632 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
11633 return isl_map_free(map
));
11635 map
= isl_map_cow(map
);
11639 for (i
= 0; i
< map
->n
; ++i
) {
11640 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
11642 return isl_map_free(map
);
11645 map
->dim
= isl_space_uncurry(map
->dim
);
11647 return isl_map_free(map
);
11652 /* Construct a basic map mapping the domain of the affine expression
11653 * to a one-dimensional range prescribed by the affine expression.
11655 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11659 isl_local_space
*ls
;
11660 isl_basic_map
*bmap
;
11665 ls
= isl_aff_get_local_space(aff
);
11666 bmap
= isl_basic_map_from_local_space(ls
);
11667 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11668 k
= isl_basic_map_alloc_equality(bmap
);
11672 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11673 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11674 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11675 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11676 aff
->v
->size
- (pos
+ 1));
11679 bmap
= isl_basic_map_finalize(bmap
);
11683 isl_basic_map_free(bmap
);
11687 /* Construct a map mapping the domain of the affine expression
11688 * to a one-dimensional range prescribed by the affine expression.
11690 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11692 isl_basic_map
*bmap
;
11694 bmap
= isl_basic_map_from_aff(aff
);
11695 return isl_map_from_basic_map(bmap
);
11698 /* Construct a basic map mapping the domain the multi-affine expression
11699 * to its range, with each dimension in the range equated to the
11700 * corresponding affine expression.
11702 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11703 __isl_take isl_multi_aff
*maff
)
11707 isl_basic_map
*bmap
;
11712 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11713 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11714 "invalid space", goto error
);
11716 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11717 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11719 for (i
= 0; i
< maff
->n
; ++i
) {
11721 isl_basic_map
*bmap_i
;
11723 aff
= isl_aff_copy(maff
->p
[i
]);
11724 bmap_i
= isl_basic_map_from_aff(aff
);
11726 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11729 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11731 isl_multi_aff_free(maff
);
11734 isl_multi_aff_free(maff
);
11738 /* Construct a map mapping the domain the multi-affine expression
11739 * to its range, with each dimension in the range equated to the
11740 * corresponding affine expression.
11742 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11744 isl_basic_map
*bmap
;
11746 bmap
= isl_basic_map_from_multi_aff(maff
);
11747 return isl_map_from_basic_map(bmap
);
11750 /* Construct a basic map mapping a domain in the given space to
11751 * to an n-dimensional range, with n the number of elements in the list,
11752 * where each coordinate in the range is prescribed by the
11753 * corresponding affine expression.
11754 * The domains of all affine expressions in the list are assumed to match
11757 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11758 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11762 isl_basic_map
*bmap
;
11767 dim
= isl_space_from_domain(domain_dim
);
11768 bmap
= isl_basic_map_universe(dim
);
11770 for (i
= 0; i
< list
->n
; ++i
) {
11772 isl_basic_map
*bmap_i
;
11774 aff
= isl_aff_copy(list
->p
[i
]);
11775 bmap_i
= isl_basic_map_from_aff(aff
);
11777 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11780 isl_aff_list_free(list
);
11784 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11785 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11787 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11790 /* Construct a basic map where the given dimensions are equal to each other.
11792 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11793 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11795 isl_basic_map
*bmap
= NULL
;
11801 if (pos1
>= isl_space_dim(space
, type1
))
11802 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11803 "index out of bounds", goto error
);
11804 if (pos2
>= isl_space_dim(space
, type2
))
11805 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11806 "index out of bounds", goto error
);
11808 if (type1
== type2
&& pos1
== pos2
)
11809 return isl_basic_map_universe(space
);
11811 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11812 i
= isl_basic_map_alloc_equality(bmap
);
11815 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11816 pos1
+= isl_basic_map_offset(bmap
, type1
);
11817 pos2
+= isl_basic_map_offset(bmap
, type2
);
11818 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11819 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11820 bmap
= isl_basic_map_finalize(bmap
);
11821 isl_space_free(space
);
11824 isl_space_free(space
);
11825 isl_basic_map_free(bmap
);
11829 /* Add a constraint imposing that the given two dimensions are equal.
11831 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11832 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11836 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11838 bmap
= isl_basic_map_intersect(bmap
, eq
);
11843 /* Add a constraint imposing that the given two dimensions are equal.
11845 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11846 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11848 isl_basic_map
*bmap
;
11850 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11852 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11857 /* Add a constraint imposing that the given two dimensions have opposite values.
11859 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11860 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11862 isl_basic_map
*bmap
= NULL
;
11868 if (pos1
>= isl_map_dim(map
, type1
))
11869 isl_die(map
->ctx
, isl_error_invalid
,
11870 "index out of bounds", goto error
);
11871 if (pos2
>= isl_map_dim(map
, type2
))
11872 isl_die(map
->ctx
, isl_error_invalid
,
11873 "index out of bounds", goto error
);
11875 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11876 i
= isl_basic_map_alloc_equality(bmap
);
11879 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11880 pos1
+= isl_basic_map_offset(bmap
, type1
);
11881 pos2
+= isl_basic_map_offset(bmap
, type2
);
11882 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11883 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11884 bmap
= isl_basic_map_finalize(bmap
);
11886 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11890 isl_basic_map_free(bmap
);
11895 /* Construct a constraint imposing that the value of the first dimension is
11896 * greater than or equal to that of the second.
11898 static __isl_give isl_constraint
*constraint_order_ge(
11899 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11900 enum isl_dim_type type2
, int pos2
)
11907 c
= isl_inequality_alloc(isl_local_space_from_space(space
));
11909 if (pos1
>= isl_constraint_dim(c
, type1
))
11910 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11911 "index out of bounds", return isl_constraint_free(c
));
11912 if (pos2
>= isl_constraint_dim(c
, type2
))
11913 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11914 "index out of bounds", return isl_constraint_free(c
));
11916 if (type1
== type2
&& pos1
== pos2
)
11919 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11920 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11925 /* Add a constraint imposing that the value of the first dimension is
11926 * greater than or equal to that of the second.
11928 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11929 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11934 if (type1
== type2
&& pos1
== pos2
)
11936 space
= isl_basic_map_get_space(bmap
);
11937 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11938 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11943 /* Add a constraint imposing that the value of the first dimension is
11944 * greater than or equal to that of the second.
11946 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11947 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11952 if (type1
== type2
&& pos1
== pos2
)
11954 space
= isl_map_get_space(map
);
11955 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11956 map
= isl_map_add_constraint(map
, c
);
11961 /* Add a constraint imposing that the value of the first dimension is
11962 * less than or equal to that of the second.
11964 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11965 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11967 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11970 /* Construct a basic map where the value of the first dimension is
11971 * greater than that of the second.
11973 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11974 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11976 isl_basic_map
*bmap
= NULL
;
11982 if (pos1
>= isl_space_dim(space
, type1
))
11983 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11984 "index out of bounds", goto error
);
11985 if (pos2
>= isl_space_dim(space
, type2
))
11986 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11987 "index out of bounds", goto error
);
11989 if (type1
== type2
&& pos1
== pos2
)
11990 return isl_basic_map_empty(space
);
11992 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11993 i
= isl_basic_map_alloc_inequality(bmap
);
11995 return isl_basic_map_free(bmap
);
11996 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11997 pos1
+= isl_basic_map_offset(bmap
, type1
);
11998 pos2
+= isl_basic_map_offset(bmap
, type2
);
11999 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12000 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12001 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12002 bmap
= isl_basic_map_finalize(bmap
);
12006 isl_space_free(space
);
12007 isl_basic_map_free(bmap
);
12011 /* Add a constraint imposing that the value of the first dimension is
12012 * greater than that of the second.
12014 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12015 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12019 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12021 bmap
= isl_basic_map_intersect(bmap
, gt
);
12026 /* Add a constraint imposing that the value of the first dimension is
12027 * greater than that of the second.
12029 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12030 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12032 isl_basic_map
*bmap
;
12034 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12036 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12041 /* Add a constraint imposing that the value of the first dimension is
12042 * smaller than that of the second.
12044 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12045 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12047 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12050 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12054 isl_local_space
*ls
;
12059 if (!isl_basic_map_divs_known(bmap
))
12060 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12061 "some divs are unknown", return NULL
);
12063 ls
= isl_basic_map_get_local_space(bmap
);
12064 div
= isl_local_space_get_div(ls
, pos
);
12065 isl_local_space_free(ls
);
12070 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12073 return isl_basic_map_get_div(bset
, pos
);
12076 /* Plug in "subs" for dimension "type", "pos" of "bset".
12078 * Let i be the dimension to replace and let "subs" be of the form
12082 * Any integer division with a non-zero coefficient for i,
12084 * floor((a i + g)/m)
12088 * floor((a f + d g)/(m d))
12090 * Constraints of the form
12098 * We currently require that "subs" is an integral expression.
12099 * Handling rational expressions may require us to add stride constraints
12100 * as we do in isl_basic_set_preimage_multi_aff.
12102 __isl_give isl_basic_set
*isl_basic_set_substitute(
12103 __isl_take isl_basic_set
*bset
,
12104 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12110 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12113 bset
= isl_basic_set_cow(bset
);
12114 if (!bset
|| !subs
)
12117 ctx
= isl_basic_set_get_ctx(bset
);
12118 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12119 isl_die(ctx
, isl_error_invalid
,
12120 "spaces don't match", goto error
);
12121 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12122 isl_die(ctx
, isl_error_unsupported
,
12123 "cannot handle divs yet", goto error
);
12124 if (!isl_int_is_one(subs
->v
->el
[0]))
12125 isl_die(ctx
, isl_error_invalid
,
12126 "can only substitute integer expressions", goto error
);
12128 pos
+= isl_basic_set_offset(bset
, type
);
12132 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12133 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12135 isl_int_set(v
, bset
->eq
[i
][pos
]);
12136 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12137 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12138 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12141 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12142 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12144 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12145 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12146 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12147 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12150 for (i
= 0; i
< bset
->n_div
; ++i
) {
12151 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12153 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12154 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12155 isl_seq_combine(bset
->div
[i
] + 1,
12156 subs
->v
->el
[0], bset
->div
[i
] + 1,
12157 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12158 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12163 bset
= isl_basic_set_simplify(bset
);
12164 return isl_basic_set_finalize(bset
);
12166 isl_basic_set_free(bset
);
12170 /* Plug in "subs" for dimension "type", "pos" of "set".
12172 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12173 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12177 if (set
&& isl_set_plain_is_empty(set
))
12180 set
= isl_set_cow(set
);
12184 for (i
= set
->n
- 1; i
>= 0; --i
) {
12185 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12186 if (remove_if_empty(set
, i
) < 0)
12196 /* Check if the range of "ma" is compatible with the domain or range
12197 * (depending on "type") of "bmap".
12198 * Return -1 if anything is wrong.
12200 static int check_basic_map_compatible_range_multi_aff(
12201 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12202 __isl_keep isl_multi_aff
*ma
)
12205 isl_space
*ma_space
;
12207 ma_space
= isl_multi_aff_get_space(ma
);
12209 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12213 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12214 "parameters don't match", goto error
);
12215 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12219 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12220 "spaces don't match", goto error
);
12222 isl_space_free(ma_space
);
12225 isl_space_free(ma_space
);
12229 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12230 * coefficients before the transformed range of dimensions,
12231 * the "n_after" coefficients after the transformed range of dimensions
12232 * and the coefficients of the other divs in "bmap".
12234 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12235 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12240 isl_local_space
*ls
;
12245 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12249 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12250 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12251 for (i
= 0; i
< n_div
; ++i
) {
12252 int o_bmap
= 0, o_ls
= 0;
12254 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12255 o_bmap
+= 1 + 1 + n_param
;
12256 o_ls
+= 1 + 1 + n_param
;
12257 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12258 o_bmap
+= n_before
;
12259 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12260 ls
->div
->row
[i
] + o_ls
, n_set
);
12263 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12265 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12266 ls
->div
->row
[i
] + o_ls
, n_div
);
12269 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12270 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12274 isl_local_space_free(ls
);
12277 isl_local_space_free(ls
);
12281 /* How many stride constraints does "ma" enforce?
12282 * That is, how many of the affine expressions have a denominator
12283 * different from one?
12285 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12290 for (i
= 0; i
< ma
->n
; ++i
)
12291 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12297 /* For each affine expression in ma of the form
12299 * x_i = (f_i y + h_i)/m_i
12301 * with m_i different from one, add a constraint to "bmap"
12304 * f_i y + h_i = m_i alpha_i
12306 * with alpha_i an additional existentially quantified variable.
12308 static __isl_give isl_basic_map
*add_ma_strides(
12309 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12310 int n_before
, int n_after
)
12319 total
= isl_basic_map_total_dim(bmap
);
12320 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12321 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12322 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
12323 for (i
= 0; i
< ma
->n
; ++i
) {
12324 int o_bmap
= 0, o_ma
= 1;
12326 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12328 div
= isl_basic_map_alloc_div(bmap
);
12329 k
= isl_basic_map_alloc_equality(bmap
);
12330 if (div
< 0 || k
< 0)
12332 isl_int_set_si(bmap
->div
[div
][0], 0);
12333 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12334 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12335 o_bmap
+= 1 + n_param
;
12336 o_ma
+= 1 + n_param
;
12337 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12338 o_bmap
+= n_before
;
12339 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12340 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12343 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12345 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12346 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
12349 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12350 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12356 isl_basic_map_free(bmap
);
12360 /* Replace the domain or range space (depending on "type) of "space" by "set".
12362 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12363 enum isl_dim_type type
, __isl_take isl_space
*set
)
12365 if (type
== isl_dim_in
) {
12366 space
= isl_space_range(space
);
12367 space
= isl_space_map_from_domain_and_range(set
, space
);
12369 space
= isl_space_domain(space
);
12370 space
= isl_space_map_from_domain_and_range(space
, set
);
12376 /* Compute the preimage of the domain or range (depending on "type")
12377 * of "bmap" under the function represented by "ma".
12378 * In other words, plug in "ma" in the domain or range of "bmap".
12379 * The result is a basic map that lives in the same space as "bmap"
12380 * except that the domain or range has been replaced by
12381 * the domain space of "ma".
12383 * If bmap is represented by
12385 * A(p) + S u + B x + T v + C(divs) >= 0,
12387 * where u and x are input and output dimensions if type == isl_dim_out
12388 * while x and v are input and output dimensions if type == isl_dim_in,
12389 * and ma is represented by
12391 * x = D(p) + F(y) + G(divs')
12393 * then the result is
12395 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12397 * The divs in the input set are similarly adjusted.
12400 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12404 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12405 * B_i G(divs') + c_i(divs))/n_i)
12407 * If bmap is not a rational map and if F(y) involves any denominators
12409 * x_i = (f_i y + h_i)/m_i
12411 * then additional constraints are added to ensure that we only
12412 * map back integer points. That is we enforce
12414 * f_i y + h_i = m_i alpha_i
12416 * with alpha_i an additional existentially quantified variable.
12418 * We first copy over the divs from "ma".
12419 * Then we add the modified constraints and divs from "bmap".
12420 * Finally, we add the stride constraints, if needed.
12422 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12423 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12424 __isl_take isl_multi_aff
*ma
)
12428 isl_basic_map
*res
= NULL
;
12429 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12430 isl_int f
, c1
, c2
, g
;
12431 int rational
, strides
;
12438 ma
= isl_multi_aff_align_divs(ma
);
12441 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12444 if (type
== isl_dim_in
) {
12446 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12448 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12451 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12452 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12454 space
= isl_multi_aff_get_domain_space(ma
);
12455 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12456 rational
= isl_basic_map_is_rational(bmap
);
12457 strides
= rational
? 0 : multi_aff_strides(ma
);
12458 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12459 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12461 res
= isl_basic_map_set_rational(res
);
12463 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12464 if (isl_basic_map_alloc_div(res
) < 0)
12467 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12470 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12471 k
= isl_basic_map_alloc_equality(res
);
12474 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12475 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12478 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12479 k
= isl_basic_map_alloc_inequality(res
);
12482 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12483 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12486 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12487 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12488 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12491 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12492 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12497 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
12503 isl_basic_map_free(bmap
);
12504 isl_multi_aff_free(ma
);
12505 res
= isl_basic_set_simplify(res
);
12506 return isl_basic_map_finalize(res
);
12512 isl_basic_map_free(bmap
);
12513 isl_multi_aff_free(ma
);
12514 isl_basic_map_free(res
);
12518 /* Compute the preimage of "bset" under the function represented by "ma".
12519 * In other words, plug in "ma" in "bset". The result is a basic set
12520 * that lives in the domain space of "ma".
12522 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12523 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12525 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12528 /* Compute the preimage of the domain of "bmap" under the function
12529 * represented by "ma".
12530 * In other words, plug in "ma" in the domain of "bmap".
12531 * The result is a basic map that lives in the same space as "bmap"
12532 * except that the domain has been replaced by the domain space of "ma".
12534 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12535 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12537 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12540 /* Compute the preimage of the range of "bmap" under the function
12541 * represented by "ma".
12542 * In other words, plug in "ma" in the range of "bmap".
12543 * The result is a basic map that lives in the same space as "bmap"
12544 * except that the range has been replaced by the domain space of "ma".
12546 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12547 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12549 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12552 /* Check if the range of "ma" is compatible with the domain or range
12553 * (depending on "type") of "map".
12554 * Return -1 if anything is wrong.
12556 static int check_map_compatible_range_multi_aff(
12557 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12558 __isl_keep isl_multi_aff
*ma
)
12561 isl_space
*ma_space
;
12563 ma_space
= isl_multi_aff_get_space(ma
);
12564 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12565 isl_space_free(ma_space
);
12567 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12568 "spaces don't match", return -1);
12572 /* Compute the preimage of the domain or range (depending on "type")
12573 * of "map" under the function represented by "ma".
12574 * In other words, plug in "ma" in the domain or range of "map".
12575 * The result is a map that lives in the same space as "map"
12576 * except that the domain or range has been replaced by
12577 * the domain space of "ma".
12579 * The parameters are assumed to have been aligned.
12581 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12582 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12587 map
= isl_map_cow(map
);
12588 ma
= isl_multi_aff_align_divs(ma
);
12591 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12594 for (i
= 0; i
< map
->n
; ++i
) {
12595 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12596 isl_multi_aff_copy(ma
));
12601 space
= isl_multi_aff_get_domain_space(ma
);
12602 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12604 isl_space_free(map
->dim
);
12609 isl_multi_aff_free(ma
);
12611 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12612 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12615 isl_multi_aff_free(ma
);
12620 /* Compute the preimage of the domain or range (depending on "type")
12621 * of "map" under the function represented by "ma".
12622 * In other words, plug in "ma" in the domain or range of "map".
12623 * The result is a map that lives in the same space as "map"
12624 * except that the domain or range has been replaced by
12625 * the domain space of "ma".
12627 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12628 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12633 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12634 return map_preimage_multi_aff(map
, type
, ma
);
12636 if (!isl_space_has_named_params(map
->dim
) ||
12637 !isl_space_has_named_params(ma
->space
))
12638 isl_die(map
->ctx
, isl_error_invalid
,
12639 "unaligned unnamed parameters", goto error
);
12640 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12641 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12643 return map_preimage_multi_aff(map
, type
, ma
);
12645 isl_multi_aff_free(ma
);
12646 return isl_map_free(map
);
12649 /* Compute the preimage of "set" under the function represented by "ma".
12650 * In other words, plug in "ma" in "set". The result is a set
12651 * that lives in the domain space of "ma".
12653 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12654 __isl_take isl_multi_aff
*ma
)
12656 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12659 /* Compute the preimage of the domain of "map" under the function
12660 * represented by "ma".
12661 * In other words, plug in "ma" in the domain of "map".
12662 * The result is a map that lives in the same space as "map"
12663 * except that the domain has been replaced by the domain space of "ma".
12665 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12666 __isl_take isl_multi_aff
*ma
)
12668 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12671 /* Compute the preimage of the range of "map" under the function
12672 * represented by "ma".
12673 * In other words, plug in "ma" in the range of "map".
12674 * The result is a map that lives in the same space as "map"
12675 * except that the range has been replaced by the domain space of "ma".
12677 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12678 __isl_take isl_multi_aff
*ma
)
12680 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12683 /* Compute the preimage of "map" under the function represented by "pma".
12684 * In other words, plug in "pma" in the domain or range of "map".
12685 * The result is a map that lives in the same space as "map",
12686 * except that the space of type "type" has been replaced by
12687 * the domain space of "pma".
12689 * The parameters of "map" and "pma" are assumed to have been aligned.
12691 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12692 __isl_take isl_map
*map
, enum isl_dim_type type
,
12693 __isl_take isl_pw_multi_aff
*pma
)
12702 isl_pw_multi_aff_free(pma
);
12703 res
= isl_map_empty(isl_map_get_space(map
));
12708 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12709 isl_multi_aff_copy(pma
->p
[0].maff
));
12710 if (type
== isl_dim_in
)
12711 res
= isl_map_intersect_domain(res
,
12712 isl_map_copy(pma
->p
[0].set
));
12714 res
= isl_map_intersect_range(res
,
12715 isl_map_copy(pma
->p
[0].set
));
12717 for (i
= 1; i
< pma
->n
; ++i
) {
12720 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12721 isl_multi_aff_copy(pma
->p
[i
].maff
));
12722 if (type
== isl_dim_in
)
12723 res_i
= isl_map_intersect_domain(res_i
,
12724 isl_map_copy(pma
->p
[i
].set
));
12726 res_i
= isl_map_intersect_range(res_i
,
12727 isl_map_copy(pma
->p
[i
].set
));
12728 res
= isl_map_union(res
, res_i
);
12731 isl_pw_multi_aff_free(pma
);
12735 isl_pw_multi_aff_free(pma
);
12740 /* Compute the preimage of "map" under the function represented by "pma".
12741 * In other words, plug in "pma" in the domain or range of "map".
12742 * The result is a map that lives in the same space as "map",
12743 * except that the space of type "type" has been replaced by
12744 * the domain space of "pma".
12746 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12747 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12752 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12753 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12755 if (!isl_space_has_named_params(map
->dim
) ||
12756 !isl_space_has_named_params(pma
->dim
))
12757 isl_die(map
->ctx
, isl_error_invalid
,
12758 "unaligned unnamed parameters", goto error
);
12759 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12760 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12762 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12764 isl_pw_multi_aff_free(pma
);
12765 return isl_map_free(map
);
12768 /* Compute the preimage of "set" under the function represented by "pma".
12769 * In other words, plug in "pma" in "set". The result is a set
12770 * that lives in the domain space of "pma".
12772 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12773 __isl_take isl_pw_multi_aff
*pma
)
12775 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12778 /* Compute the preimage of the domain of "map" under the function
12779 * represented by "pma".
12780 * In other words, plug in "pma" in the domain of "map".
12781 * The result is a map that lives in the same space as "map",
12782 * except that domain space has been replaced by the domain space of "pma".
12784 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12785 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12787 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12790 /* Compute the preimage of the range of "map" under the function
12791 * represented by "pma".
12792 * In other words, plug in "pma" in the range of "map".
12793 * The result is a map that lives in the same space as "map",
12794 * except that range space has been replaced by the domain space of "pma".
12796 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12797 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12799 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12802 /* Compute the preimage of "map" under the function represented by "mpa".
12803 * In other words, plug in "mpa" in the domain or range of "map".
12804 * The result is a map that lives in the same space as "map",
12805 * except that the space of type "type" has been replaced by
12806 * the domain space of "mpa".
12808 * If the map does not involve any constraints that refer to the
12809 * dimensions of the substituted space, then the only possible
12810 * effect of "mpa" on the map is to map the space to a different space.
12811 * We create a separate isl_multi_aff to effectuate this change
12812 * in order to avoid spurious splitting of the map along the pieces
12815 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12816 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12819 isl_pw_multi_aff
*pma
;
12824 n
= isl_map_dim(map
, type
);
12825 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12829 space
= isl_multi_pw_aff_get_space(mpa
);
12830 isl_multi_pw_aff_free(mpa
);
12831 ma
= isl_multi_aff_zero(space
);
12832 return isl_map_preimage_multi_aff(map
, type
, ma
);
12835 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12836 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12839 isl_multi_pw_aff_free(mpa
);
12843 /* Compute the preimage of "map" under the function represented by "mpa".
12844 * In other words, plug in "mpa" in the domain "map".
12845 * The result is a map that lives in the same space as "map",
12846 * except that domain space has been replaced by the domain space of "mpa".
12848 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12849 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12851 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12854 /* Compute the preimage of "set" by the function represented by "mpa".
12855 * In other words, plug in "mpa" in "set".
12857 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12858 __isl_take isl_multi_pw_aff
*mpa
)
12860 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);